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.

393 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/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, format_count, 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->format_count && b->format_count) {
  97. MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
  98. } else if (a->format_count) {
  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 (count)
  155. formats->formats = av_malloc(sizeof(*formats->formats) * count);
  156. formats->format_count = count;
  157. memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
  158. return formats;
  159. }
  160. #define ADD_FORMAT(f, fmt, type, list, nb) \
  161. do { \
  162. type *fmts; \
  163. \
  164. if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) \
  165. return AVERROR(ENOMEM); \
  166. \
  167. fmts = av_realloc((*f)->list, \
  168. sizeof(*(*f)->list) * ((*f)->nb + 1));\
  169. if (!fmts) \
  170. return AVERROR(ENOMEM); \
  171. \
  172. (*f)->list = fmts; \
  173. (*f)->list[(*f)->nb++] = fmt; \
  174. return 0; \
  175. } while (0)
  176. int ff_add_format(AVFilterFormats **avff, int fmt)
  177. {
  178. ADD_FORMAT(avff, fmt, int, formats, format_count);
  179. }
  180. int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
  181. {
  182. ADD_FORMAT(l, channel_layout, uint64_t, channel_layouts, nb_channel_layouts);
  183. }
  184. AVFilterFormats *ff_all_formats(enum AVMediaType type)
  185. {
  186. AVFilterFormats *ret = NULL;
  187. int fmt;
  188. int num_formats = type == AVMEDIA_TYPE_VIDEO ? AV_PIX_FMT_NB :
  189. type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
  190. for (fmt = 0; fmt < num_formats; fmt++)
  191. if ((type != AVMEDIA_TYPE_VIDEO) ||
  192. (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
  193. ff_add_format(&ret, fmt);
  194. return ret;
  195. }
  196. AVFilterFormats *ff_planar_sample_fmts(void)
  197. {
  198. AVFilterFormats *ret = NULL;
  199. int fmt;
  200. for (fmt = 0; fmt < AV_SAMPLE_FMT_NB; fmt++)
  201. if (av_sample_fmt_is_planar(fmt))
  202. ff_add_format(&ret, fmt);
  203. return ret;
  204. }
  205. AVFilterFormats *ff_all_samplerates(void)
  206. {
  207. AVFilterFormats *ret = av_mallocz(sizeof(*ret));
  208. return ret;
  209. }
  210. AVFilterChannelLayouts *ff_all_channel_layouts(void)
  211. {
  212. AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
  213. return ret;
  214. }
  215. #define FORMATS_REF(f, ref) \
  216. do { \
  217. *ref = f; \
  218. f->refs = av_realloc(f->refs, sizeof(*f->refs) * ++f->refcount); \
  219. f->refs[f->refcount-1] = ref; \
  220. } while (0)
  221. void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
  222. {
  223. FORMATS_REF(f, ref);
  224. }
  225. void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
  226. {
  227. FORMATS_REF(f, ref);
  228. }
  229. #define FIND_REF_INDEX(ref, idx) \
  230. do { \
  231. int i; \
  232. for (i = 0; i < (*ref)->refcount; i ++) \
  233. if((*ref)->refs[i] == ref) { \
  234. idx = i; \
  235. break; \
  236. } \
  237. } while (0)
  238. #define FORMATS_UNREF(ref, list) \
  239. do { \
  240. int idx = -1; \
  241. \
  242. if (!*ref) \
  243. return; \
  244. \
  245. FIND_REF_INDEX(ref, idx); \
  246. \
  247. if (idx >= 0) \
  248. memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
  249. sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
  250. \
  251. if(!--(*ref)->refcount) { \
  252. av_free((*ref)->list); \
  253. av_free((*ref)->refs); \
  254. av_free(*ref); \
  255. } \
  256. *ref = NULL; \
  257. } while (0)
  258. void ff_formats_unref(AVFilterFormats **ref)
  259. {
  260. FORMATS_UNREF(ref, formats);
  261. }
  262. void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
  263. {
  264. FORMATS_UNREF(ref, channel_layouts);
  265. }
  266. #define FORMATS_CHANGEREF(oldref, newref) \
  267. do { \
  268. int idx = -1; \
  269. \
  270. FIND_REF_INDEX(oldref, idx); \
  271. \
  272. if (idx >= 0) { \
  273. (*oldref)->refs[idx] = newref; \
  274. *newref = *oldref; \
  275. *oldref = NULL; \
  276. } \
  277. } while (0)
  278. void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
  279. AVFilterChannelLayouts **newref)
  280. {
  281. FORMATS_CHANGEREF(oldref, newref);
  282. }
  283. void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
  284. {
  285. FORMATS_CHANGEREF(oldref, newref);
  286. }
  287. #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
  288. { \
  289. int count = 0, i; \
  290. \
  291. for (i = 0; i < ctx->nb_inputs; i++) { \
  292. if (ctx->inputs[i]) { \
  293. ref(fmts, &ctx->inputs[i]->out_fmts); \
  294. count++; \
  295. } \
  296. } \
  297. for (i = 0; i < ctx->nb_outputs; i++) { \
  298. if (ctx->outputs[i]) { \
  299. ref(fmts, &ctx->outputs[i]->in_fmts); \
  300. count++; \
  301. } \
  302. } \
  303. \
  304. if (!count) { \
  305. av_freep(&fmts->list); \
  306. av_freep(&fmts->refs); \
  307. av_freep(&fmts); \
  308. } \
  309. }
  310. void ff_set_common_channel_layouts(AVFilterContext *ctx,
  311. AVFilterChannelLayouts *layouts)
  312. {
  313. SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
  314. ff_channel_layouts_ref, channel_layouts);
  315. }
  316. void ff_set_common_samplerates(AVFilterContext *ctx,
  317. AVFilterFormats *samplerates)
  318. {
  319. SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
  320. ff_formats_ref, formats);
  321. }
  322. /**
  323. * A helper for query_formats() which sets all links to the same list of
  324. * formats. If there are no links hooked to this filter, the list of formats is
  325. * freed.
  326. */
  327. void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  328. {
  329. SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
  330. ff_formats_ref, formats);
  331. }
  332. int ff_default_query_formats(AVFilterContext *ctx)
  333. {
  334. enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
  335. ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
  336. AVMEDIA_TYPE_VIDEO;
  337. ff_set_common_formats(ctx, ff_all_formats(type));
  338. if (type == AVMEDIA_TYPE_AUDIO) {
  339. ff_set_common_channel_layouts(ctx, ff_all_channel_layouts());
  340. ff_set_common_samplerates(ctx, ff_all_samplerates());
  341. }
  342. return 0;
  343. }