formats.c 6.2 KB

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