formats.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Filter layer - format negotiation
  3. * Copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/pixdesc.h"
  22. #include "libavutil/audioconvert.h"
  23. #include "avfilter.h"
  24. #include "internal.h"
  25. /**
  26. * Add all refs from a to ret and destroy a.
  27. */
  28. static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
  29. {
  30. int i;
  31. for (i = 0; i < a->refcount; i++) {
  32. ret->refs[ret->refcount] = a->refs[i];
  33. *ret->refs[ret->refcount++] = ret;
  34. }
  35. av_free(a->refs);
  36. av_free(a->formats);
  37. av_free(a);
  38. }
  39. AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
  40. {
  41. AVFilterFormats *ret;
  42. unsigned i, j, k = 0;
  43. if (a == b) return a;
  44. ret = av_mallocz(sizeof(AVFilterFormats));
  45. /* merge list of formats */
  46. ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
  47. b->format_count));
  48. for (i = 0; i < a->format_count; i++)
  49. for (j = 0; j < b->format_count; j++)
  50. if (a->formats[i] == b->formats[j])
  51. ret->formats[k++] = a->formats[i];
  52. ret->format_count = k;
  53. /* check that there was at least one common format */
  54. if (!ret->format_count) {
  55. av_free(ret->formats);
  56. av_free(ret);
  57. return NULL;
  58. }
  59. ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
  60. merge_ref(ret, a);
  61. merge_ref(ret, b);
  62. return ret;
  63. }
  64. int ff_fmt_is_in(int fmt, const int *fmts)
  65. {
  66. const int *p;
  67. for (p = fmts; *p != -1; p++) {
  68. if (fmt == *p)
  69. return 1;
  70. }
  71. return 0;
  72. }
  73. #define MAKE_FORMAT_LIST() \
  74. AVFilterFormats *formats; \
  75. int count = 0; \
  76. if (fmts) \
  77. for (count = 0; fmts[count] != -1; count++) \
  78. ; \
  79. formats = av_mallocz(sizeof(AVFilterFormats)); \
  80. if (!formats) return NULL; \
  81. formats->format_count = count; \
  82. if (count) { \
  83. formats->formats = av_malloc(sizeof(*formats->formats)*count); \
  84. if (!formats->formats) { \
  85. av_free(formats); \
  86. return NULL; \
  87. } \
  88. }
  89. AVFilterFormats *avfilter_make_format_list(const int *fmts)
  90. {
  91. MAKE_FORMAT_LIST();
  92. while (count--)
  93. formats->formats[count] = fmts[count];
  94. return formats;
  95. }
  96. AVFilterFormats *avfilter_make_format64_list(const int64_t *fmts)
  97. {
  98. MAKE_FORMAT_LIST();
  99. if (count)
  100. memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
  101. return formats;
  102. }
  103. int avfilter_add_format(AVFilterFormats **avff, int64_t fmt)
  104. {
  105. int64_t *fmts;
  106. if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
  107. return AVERROR(ENOMEM);
  108. fmts = av_realloc((*avff)->formats,
  109. sizeof(*(*avff)->formats) * ((*avff)->format_count+1));
  110. if (!fmts)
  111. return AVERROR(ENOMEM);
  112. (*avff)->formats = fmts;
  113. (*avff)->formats[(*avff)->format_count++] = fmt;
  114. return 0;
  115. }
  116. AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
  117. {
  118. AVFilterFormats *ret = NULL;
  119. int fmt;
  120. int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
  121. type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
  122. for (fmt = 0; fmt < num_formats; fmt++)
  123. if ((type != AVMEDIA_TYPE_VIDEO) ||
  124. (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
  125. avfilter_add_format(&ret, fmt);
  126. return ret;
  127. }
  128. AVFilterFormats *avfilter_all_channel_layouts(void)
  129. {
  130. static int64_t chlayouts[] = {
  131. AV_CH_LAYOUT_MONO,
  132. AV_CH_LAYOUT_STEREO,
  133. AV_CH_LAYOUT_4POINT0,
  134. AV_CH_LAYOUT_QUAD,
  135. AV_CH_LAYOUT_5POINT0,
  136. AV_CH_LAYOUT_5POINT0_BACK,
  137. AV_CH_LAYOUT_5POINT1,
  138. AV_CH_LAYOUT_5POINT1_BACK,
  139. AV_CH_LAYOUT_5POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX,
  140. AV_CH_LAYOUT_7POINT1,
  141. AV_CH_LAYOUT_7POINT1_WIDE,
  142. AV_CH_LAYOUT_7POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX,
  143. -1,
  144. };
  145. return avfilter_make_format64_list(chlayouts);
  146. }
  147. void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
  148. {
  149. *ref = f;
  150. f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
  151. f->refs[f->refcount-1] = ref;
  152. }
  153. static int find_ref_index(AVFilterFormats **ref)
  154. {
  155. int i;
  156. for (i = 0; i < (*ref)->refcount; i++)
  157. if ((*ref)->refs[i] == ref)
  158. return i;
  159. return -1;
  160. }
  161. void avfilter_formats_unref(AVFilterFormats **ref)
  162. {
  163. int idx;
  164. if (!*ref)
  165. return;
  166. idx = find_ref_index(ref);
  167. if (idx >= 0)
  168. memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
  169. sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
  170. if (!--(*ref)->refcount) {
  171. av_free((*ref)->formats);
  172. av_free((*ref)->refs);
  173. av_free(*ref);
  174. }
  175. *ref = NULL;
  176. }
  177. void avfilter_formats_changeref(AVFilterFormats **oldref,
  178. AVFilterFormats **newref)
  179. {
  180. int idx = find_ref_index(oldref);
  181. if (idx >= 0) {
  182. (*oldref)->refs[idx] = newref;
  183. *newref = *oldref;
  184. *oldref = NULL;
  185. }
  186. }