You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

392 lines
15KB

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