formats.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Filter layer - format negotiation
  3. * Copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/common.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "avfilter.h"
  24. #include "internal.h"
  25. #include "formats.h"
  26. /**
  27. * Add all refs from a to ret and destroy a.
  28. */
  29. #define MERGE_REF(ret, a, fmts, type, fail) \
  30. do { \
  31. type ***tmp; \
  32. int i; \
  33. \
  34. if (!(tmp = av_realloc(ret->refs, \
  35. sizeof(*tmp) * (ret->refcount + a->refcount)))) \
  36. goto fail; \
  37. ret->refs = tmp; \
  38. \
  39. for (i = 0; i < a->refcount; i ++) { \
  40. ret->refs[ret->refcount] = a->refs[i]; \
  41. *ret->refs[ret->refcount++] = ret; \
  42. } \
  43. \
  44. av_freep(&a->refs); \
  45. av_freep(&a->fmts); \
  46. av_freep(&a); \
  47. } while (0)
  48. /**
  49. * Add all formats common for a and b to ret, copy the refs and destroy
  50. * a and b.
  51. */
  52. #define MERGE_FORMATS(ret, a, b, fmts, nb, type, fail) \
  53. do { \
  54. int i, j, k = 0, count = FFMIN(a->nb, b->nb); \
  55. \
  56. if (!(ret = av_mallocz(sizeof(*ret)))) \
  57. goto fail; \
  58. \
  59. if (count) { \
  60. if (!(ret->fmts = av_malloc(sizeof(*ret->fmts) * count))) \
  61. goto fail; \
  62. for (i = 0; i < a->nb; i++) \
  63. for (j = 0; j < b->nb; j++) \
  64. if (a->fmts[i] == b->fmts[j]) \
  65. ret->fmts[k++] = a->fmts[i]; \
  66. \
  67. ret->nb = k; \
  68. } \
  69. /* check that there was at least one common format */ \
  70. if (!ret->nb) \
  71. goto fail; \
  72. \
  73. MERGE_REF(ret, a, fmts, type, fail); \
  74. MERGE_REF(ret, b, fmts, type, fail); \
  75. } while (0)
  76. AVFilterFormats *ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
  77. {
  78. AVFilterFormats *ret = NULL;
  79. if (a == b)
  80. return a;
  81. MERGE_FORMATS(ret, a, b, formats, nb_formats, AVFilterFormats, fail);
  82. return ret;
  83. fail:
  84. if (ret) {
  85. av_freep(&ret->refs);
  86. av_freep(&ret->formats);
  87. }
  88. av_freep(&ret);
  89. return NULL;
  90. }
  91. AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a,
  92. AVFilterFormats *b)
  93. {
  94. AVFilterFormats *ret = NULL;
  95. if (a == b) return a;
  96. if (a->nb_formats && b->nb_formats) {
  97. MERGE_FORMATS(ret, a, b, formats, nb_formats, AVFilterFormats, fail);
  98. } else if (a->nb_formats) {
  99. MERGE_REF(a, b, formats, AVFilterFormats, fail);
  100. ret = a;
  101. } else {
  102. MERGE_REF(b, a, formats, AVFilterFormats, fail);
  103. ret = b;
  104. }
  105. return ret;
  106. fail:
  107. if (ret) {
  108. av_freep(&ret->refs);
  109. av_freep(&ret->formats);
  110. }
  111. av_freep(&ret);
  112. return NULL;
  113. }
  114. AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a,
  115. AVFilterChannelLayouts *b)
  116. {
  117. AVFilterChannelLayouts *ret = NULL;
  118. if (a == b) return a;
  119. if (a->nb_channel_layouts && b->nb_channel_layouts) {
  120. MERGE_FORMATS(ret, a, b, channel_layouts, nb_channel_layouts,
  121. AVFilterChannelLayouts, fail);
  122. } else if (a->nb_channel_layouts) {
  123. MERGE_REF(a, b, channel_layouts, AVFilterChannelLayouts, fail);
  124. ret = a;
  125. } else {
  126. MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, fail);
  127. ret = b;
  128. }
  129. return ret;
  130. fail:
  131. if (ret) {
  132. av_freep(&ret->refs);
  133. av_freep(&ret->channel_layouts);
  134. }
  135. av_freep(&ret);
  136. return NULL;
  137. }
  138. int ff_fmt_is_in(int fmt, const int *fmts)
  139. {
  140. const int *p;
  141. for (p = fmts; *p != AV_PIX_FMT_NONE; p++) {
  142. if (fmt == *p)
  143. return 1;
  144. }
  145. return 0;
  146. }
  147. AVFilterFormats *ff_make_format_list(const int *fmts)
  148. {
  149. AVFilterFormats *formats;
  150. int count;
  151. for (count = 0; fmts[count] != -1; count++)
  152. ;
  153. formats = av_mallocz(sizeof(*formats));
  154. if (!formats)
  155. return NULL;
  156. if (count) {
  157. formats->formats = av_malloc(sizeof(*formats->formats) * count);
  158. if (!formats->formats) {
  159. av_freep(&formats);
  160. return NULL;
  161. }
  162. }
  163. formats->nb_formats = count;
  164. memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
  165. return formats;
  166. }
  167. #define ADD_FORMAT(f, fmt, type, list, nb) \
  168. do { \
  169. type *fmts; \
  170. \
  171. if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) \
  172. return AVERROR(ENOMEM); \
  173. \
  174. fmts = av_realloc((*f)->list, \
  175. sizeof(*(*f)->list) * ((*f)->nb + 1));\
  176. if (!fmts) { \
  177. av_freep(&f); \
  178. return AVERROR(ENOMEM); \
  179. } \
  180. \
  181. (*f)->list = fmts; \
  182. (*f)->list[(*f)->nb++] = fmt; \
  183. return 0; \
  184. } while (0)
  185. int ff_add_format(AVFilterFormats **avff, int fmt)
  186. {
  187. ADD_FORMAT(avff, fmt, int, formats, nb_formats);
  188. }
  189. int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
  190. {
  191. ADD_FORMAT(l, channel_layout, uint64_t, channel_layouts, nb_channel_layouts);
  192. }
  193. AVFilterFormats *ff_all_formats(enum AVMediaType type)
  194. {
  195. AVFilterFormats *ret = NULL;
  196. if (type == AVMEDIA_TYPE_VIDEO) {
  197. const AVPixFmtDescriptor *desc = NULL;
  198. while ((desc = av_pix_fmt_desc_next(desc))) {
  199. ff_add_format(&ret, av_pix_fmt_desc_get_id(desc));
  200. }
  201. } else if (type == AVMEDIA_TYPE_AUDIO) {
  202. enum AVSampleFormat fmt = 0;
  203. while (av_get_sample_fmt_name(fmt)) {
  204. ff_add_format(&ret, fmt);
  205. fmt++;
  206. }
  207. }
  208. return ret;
  209. }
  210. AVFilterFormats *ff_planar_sample_fmts(void)
  211. {
  212. AVFilterFormats *ret = NULL;
  213. int fmt;
  214. for (fmt = 0; fmt < AV_SAMPLE_FMT_NB; fmt++)
  215. if (av_sample_fmt_is_planar(fmt))
  216. ff_add_format(&ret, fmt);
  217. return ret;
  218. }
  219. AVFilterFormats *ff_all_samplerates(void)
  220. {
  221. AVFilterFormats *ret = av_mallocz(sizeof(*ret));
  222. return ret;
  223. }
  224. AVFilterChannelLayouts *ff_all_channel_layouts(void)
  225. {
  226. AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
  227. return ret;
  228. }
  229. #define FORMATS_REF(f, ref) \
  230. do { \
  231. *ref = f; \
  232. f->refs = av_realloc(f->refs, sizeof(*f->refs) * ++f->refcount); \
  233. if (!f->refs) \
  234. return; \
  235. f->refs[f->refcount-1] = ref; \
  236. } while (0)
  237. void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
  238. {
  239. FORMATS_REF(f, ref);
  240. }
  241. void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
  242. {
  243. FORMATS_REF(f, ref);
  244. }
  245. #define FIND_REF_INDEX(ref, idx) \
  246. do { \
  247. int i; \
  248. for (i = 0; i < (*ref)->refcount; i ++) \
  249. if((*ref)->refs[i] == ref) { \
  250. idx = i; \
  251. break; \
  252. } \
  253. } while (0)
  254. #define FORMATS_UNREF(ref, list) \
  255. do { \
  256. int idx = -1; \
  257. \
  258. if (!*ref) \
  259. return; \
  260. \
  261. FIND_REF_INDEX(ref, idx); \
  262. \
  263. if (idx >= 0) \
  264. memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
  265. sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
  266. \
  267. if(!--(*ref)->refcount) { \
  268. av_free((*ref)->list); \
  269. av_free((*ref)->refs); \
  270. av_free(*ref); \
  271. } \
  272. *ref = NULL; \
  273. } while (0)
  274. void ff_formats_unref(AVFilterFormats **ref)
  275. {
  276. FORMATS_UNREF(ref, formats);
  277. }
  278. void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
  279. {
  280. FORMATS_UNREF(ref, channel_layouts);
  281. }
  282. #define FORMATS_CHANGEREF(oldref, newref) \
  283. do { \
  284. int idx = -1; \
  285. \
  286. FIND_REF_INDEX(oldref, idx); \
  287. \
  288. if (idx >= 0) { \
  289. (*oldref)->refs[idx] = newref; \
  290. *newref = *oldref; \
  291. *oldref = NULL; \
  292. } \
  293. } while (0)
  294. void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
  295. AVFilterChannelLayouts **newref)
  296. {
  297. FORMATS_CHANGEREF(oldref, newref);
  298. }
  299. void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
  300. {
  301. FORMATS_CHANGEREF(oldref, newref);
  302. }
  303. #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
  304. { \
  305. int count = 0, i; \
  306. \
  307. for (i = 0; i < ctx->nb_inputs; i++) { \
  308. if (ctx->inputs[i]) { \
  309. ref(fmts, &ctx->inputs[i]->out_fmts); \
  310. count++; \
  311. } \
  312. } \
  313. for (i = 0; i < ctx->nb_outputs; i++) { \
  314. if (ctx->outputs[i]) { \
  315. ref(fmts, &ctx->outputs[i]->in_fmts); \
  316. count++; \
  317. } \
  318. } \
  319. \
  320. if (!count) { \
  321. av_freep(&fmts->list); \
  322. av_freep(&fmts->refs); \
  323. av_freep(&fmts); \
  324. } \
  325. }
  326. void ff_set_common_channel_layouts(AVFilterContext *ctx,
  327. AVFilterChannelLayouts *layouts)
  328. {
  329. SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
  330. ff_channel_layouts_ref, channel_layouts);
  331. }
  332. void ff_set_common_samplerates(AVFilterContext *ctx,
  333. AVFilterFormats *samplerates)
  334. {
  335. SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
  336. ff_formats_ref, formats);
  337. }
  338. /**
  339. * A helper for query_formats() which sets all links to the same list of
  340. * formats. If there are no links hooked to this filter, the list of formats is
  341. * freed.
  342. */
  343. void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  344. {
  345. SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
  346. ff_formats_ref, formats);
  347. }
  348. int ff_default_query_formats(AVFilterContext *ctx)
  349. {
  350. enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
  351. ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
  352. AVMEDIA_TYPE_VIDEO;
  353. ff_set_common_formats(ctx, ff_all_formats(type));
  354. if (type == AVMEDIA_TYPE_AUDIO) {
  355. ff_set_common_channel_layouts(ctx, ff_all_channel_layouts());
  356. ff_set_common_samplerates(ctx, ff_all_samplerates());
  357. }
  358. return 0;
  359. }