formats.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. /**
  25. * Add all refs from a to ret and destroy a.
  26. */
  27. static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
  28. {
  29. int i;
  30. for(i = 0; i < a->refcount; i ++) {
  31. ret->refs[ret->refcount] = a->refs[i];
  32. *ret->refs[ret->refcount++] = ret;
  33. }
  34. av_free(a->refs);
  35. av_free(a->formats);
  36. av_free(a);
  37. }
  38. AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
  39. {
  40. AVFilterFormats *ret;
  41. unsigned i, j, k = 0;
  42. if (a == b)
  43. return a;
  44. if (a == b)
  45. return a;
  46. ret = av_mallocz(sizeof(AVFilterFormats));
  47. /* merge list of formats */
  48. ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
  49. b->format_count));
  50. for(i = 0; i < a->format_count; i ++)
  51. for(j = 0; j < b->format_count; j ++)
  52. if(a->formats[i] == b->formats[j])
  53. ret->formats[k++] = a->formats[i];
  54. ret->format_count = k;
  55. /* check that there was at least one common format */
  56. if(!ret->format_count) {
  57. av_free(ret->formats);
  58. av_free(ret);
  59. return NULL;
  60. }
  61. ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
  62. merge_ref(ret, a);
  63. merge_ref(ret, b);
  64. return ret;
  65. }
  66. #define MAKE_FORMAT_LIST() \
  67. AVFilterFormats *formats; \
  68. int count = 0; \
  69. if (fmts) \
  70. for (count = 0; fmts[count] != -1; count++) \
  71. ; \
  72. formats = av_mallocz(sizeof(AVFilterFormats)); \
  73. if (!formats) return NULL; \
  74. formats->format_count = count; \
  75. if (count) { \
  76. formats->formats = av_malloc(sizeof(*formats->formats)*count); \
  77. if (!formats->formats) { \
  78. av_free(formats); \
  79. return NULL; \
  80. } \
  81. }
  82. AVFilterFormats *avfilter_make_format_list(const int *fmts)
  83. {
  84. MAKE_FORMAT_LIST();
  85. while (count--)
  86. formats->formats[count] = fmts[count];
  87. return formats;
  88. }
  89. AVFilterFormats *avfilter_make_format64_list(const int64_t *fmts)
  90. {
  91. MAKE_FORMAT_LIST();
  92. if (count)
  93. memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
  94. return formats;
  95. }
  96. int avfilter_add_format(AVFilterFormats **avff, int64_t fmt)
  97. {
  98. int64_t *fmts;
  99. if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
  100. return AVERROR(ENOMEM);
  101. fmts = av_realloc((*avff)->formats,
  102. sizeof(*(*avff)->formats) * ((*avff)->format_count+1));
  103. if (!fmts)
  104. return AVERROR(ENOMEM);
  105. (*avff)->formats = fmts;
  106. (*avff)->formats[(*avff)->format_count++] = fmt;
  107. return 0;
  108. }
  109. AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
  110. {
  111. AVFilterFormats *ret = NULL;
  112. int fmt;
  113. int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
  114. type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
  115. for (fmt = 0; fmt < num_formats; fmt++)
  116. if ((type != AVMEDIA_TYPE_VIDEO) ||
  117. (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
  118. avfilter_add_format(&ret, fmt);
  119. return ret;
  120. }
  121. AVFilterFormats *avfilter_all_channel_layouts(void)
  122. {
  123. static int64_t chlayouts[] = {
  124. AV_CH_LAYOUT_MONO,
  125. AV_CH_LAYOUT_STEREO,
  126. AV_CH_LAYOUT_4POINT0,
  127. AV_CH_LAYOUT_QUAD,
  128. AV_CH_LAYOUT_5POINT0,
  129. AV_CH_LAYOUT_5POINT0_BACK,
  130. AV_CH_LAYOUT_5POINT1,
  131. AV_CH_LAYOUT_5POINT1_BACK,
  132. AV_CH_LAYOUT_5POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX,
  133. AV_CH_LAYOUT_7POINT1,
  134. AV_CH_LAYOUT_7POINT1_WIDE,
  135. AV_CH_LAYOUT_7POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX,
  136. -1,
  137. };
  138. return avfilter_make_format64_list(chlayouts);
  139. }
  140. void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
  141. {
  142. *ref = f;
  143. f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
  144. f->refs[f->refcount-1] = ref;
  145. }
  146. static int find_ref_index(AVFilterFormats **ref)
  147. {
  148. int i;
  149. for(i = 0; i < (*ref)->refcount; i ++)
  150. if((*ref)->refs[i] == ref)
  151. return i;
  152. return -1;
  153. }
  154. void avfilter_formats_unref(AVFilterFormats **ref)
  155. {
  156. int idx;
  157. if (!*ref)
  158. return;
  159. idx = find_ref_index(ref);
  160. if(idx >= 0)
  161. memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
  162. sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
  163. if(!--(*ref)->refcount) {
  164. av_free((*ref)->formats);
  165. av_free((*ref)->refs);
  166. av_free(*ref);
  167. }
  168. *ref = NULL;
  169. }
  170. void avfilter_formats_changeref(AVFilterFormats **oldref,
  171. AVFilterFormats **newref)
  172. {
  173. int idx = find_ref_index(oldref);
  174. if(idx >= 0) {
  175. (*oldref)->refs[idx] = newref;
  176. *newref = *oldref;
  177. *oldref = NULL;
  178. }
  179. }