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.

542 lines
20KB

  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/eval.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "libavutil/audioconvert.h"
  24. #include "avfilter.h"
  25. #include "internal.h"
  26. #include "formats.h"
  27. /**
  28. * Add all refs from a to ret and destroy a.
  29. */
  30. #define MERGE_REF(ret, a, fmts, type, fail) \
  31. do { \
  32. type ***tmp; \
  33. int i; \
  34. \
  35. if (!(tmp = av_realloc(ret->refs, \
  36. sizeof(*tmp) * (ret->refcount + a->refcount)))) \
  37. goto fail; \
  38. ret->refs = tmp; \
  39. \
  40. for (i = 0; i < a->refcount; i ++) { \
  41. ret->refs[ret->refcount] = a->refs[i]; \
  42. *ret->refs[ret->refcount++] = ret; \
  43. } \
  44. \
  45. av_freep(&a->refs); \
  46. av_freep(&a->fmts); \
  47. av_freep(&a); \
  48. } while (0)
  49. /**
  50. * Add all formats common for a and b to ret, copy the refs and destroy
  51. * a and b.
  52. */
  53. #define MERGE_FORMATS(ret, a, b, fmts, nb, type, fail) \
  54. do { \
  55. int i, j, k = 0, count = FFMIN(a->nb, b->nb); \
  56. \
  57. if (!(ret = av_mallocz(sizeof(*ret)))) \
  58. goto fail; \
  59. \
  60. if (count) { \
  61. if (!(ret->fmts = av_malloc(sizeof(*ret->fmts) * count))) \
  62. goto fail; \
  63. for (i = 0; i < a->nb; i++) \
  64. for (j = 0; j < b->nb; j++) \
  65. if (a->fmts[i] == b->fmts[j]) { \
  66. if(k >= FFMIN(a->nb, b->nb)){ \
  67. av_log(0, AV_LOG_ERROR, "Duplicate formats in avfilter_merge_formats() detected\n"); \
  68. av_free(ret->fmts); \
  69. av_free(ret); \
  70. return NULL; \
  71. } \
  72. ret->fmts[k++] = a->fmts[i]; \
  73. } \
  74. } \
  75. ret->nb = k; \
  76. /* check that there was at least one common format */ \
  77. if (!ret->nb) \
  78. goto fail; \
  79. \
  80. MERGE_REF(ret, a, fmts, type, fail); \
  81. MERGE_REF(ret, b, fmts, type, fail); \
  82. } while (0)
  83. AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
  84. {
  85. AVFilterFormats *ret = NULL;
  86. if (a == b)
  87. return a;
  88. MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
  89. return ret;
  90. fail:
  91. if (ret) {
  92. av_freep(&ret->refs);
  93. av_freep(&ret->formats);
  94. }
  95. av_freep(&ret);
  96. return NULL;
  97. }
  98. AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a,
  99. AVFilterFormats *b)
  100. {
  101. AVFilterFormats *ret = NULL;
  102. if (a == b) return a;
  103. if (a->format_count && b->format_count) {
  104. MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
  105. } else if (a->format_count) {
  106. MERGE_REF(a, b, formats, AVFilterFormats, fail);
  107. ret = a;
  108. } else {
  109. MERGE_REF(b, a, formats, AVFilterFormats, fail);
  110. ret = b;
  111. }
  112. return ret;
  113. fail:
  114. if (ret) {
  115. av_freep(&ret->refs);
  116. av_freep(&ret->formats);
  117. }
  118. av_freep(&ret);
  119. return NULL;
  120. }
  121. AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a,
  122. AVFilterChannelLayouts *b)
  123. {
  124. AVFilterChannelLayouts *ret = NULL;
  125. if (a == b) return a;
  126. if (a->nb_channel_layouts && b->nb_channel_layouts) {
  127. MERGE_FORMATS(ret, a, b, channel_layouts, nb_channel_layouts,
  128. AVFilterChannelLayouts, fail);
  129. } else if (a->nb_channel_layouts) {
  130. MERGE_REF(a, b, channel_layouts, AVFilterChannelLayouts, fail);
  131. ret = a;
  132. } else {
  133. MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, fail);
  134. ret = b;
  135. }
  136. return ret;
  137. fail:
  138. if (ret) {
  139. av_freep(&ret->refs);
  140. av_freep(&ret->channel_layouts);
  141. }
  142. av_freep(&ret);
  143. return NULL;
  144. }
  145. int ff_fmt_is_in(int fmt, const int *fmts)
  146. {
  147. const int *p;
  148. for (p = fmts; *p != -1; p++) {
  149. if (fmt == *p)
  150. return 1;
  151. }
  152. return 0;
  153. }
  154. #define COPY_INT_LIST(list_copy, list, type) { \
  155. int count = 0; \
  156. if (list) \
  157. for (count = 0; list[count] != -1; count++) \
  158. ; \
  159. list_copy = av_calloc(count+1, sizeof(type)); \
  160. if (list_copy) { \
  161. memcpy(list_copy, list, sizeof(type) * count); \
  162. list_copy[count] = -1; \
  163. } \
  164. }
  165. int *ff_copy_int_list(const int * const list)
  166. {
  167. int *ret = NULL;
  168. COPY_INT_LIST(ret, list, int);
  169. return ret;
  170. }
  171. int64_t *ff_copy_int64_list(const int64_t * const list)
  172. {
  173. int64_t *ret = NULL;
  174. COPY_INT_LIST(ret, list, int64_t);
  175. return ret;
  176. }
  177. #define MAKE_FORMAT_LIST() \
  178. AVFilterFormats *formats; \
  179. int count = 0; \
  180. if (fmts) \
  181. for (count = 0; fmts[count] != -1; count++) \
  182. ; \
  183. formats = av_mallocz(sizeof(AVFilterFormats)); \
  184. if (!formats) return NULL; \
  185. formats->format_count = count; \
  186. if (count) { \
  187. formats->formats = av_malloc(sizeof(*formats->formats)*count); \
  188. if (!formats->formats) { \
  189. av_free(formats); \
  190. return NULL; \
  191. } \
  192. }
  193. AVFilterFormats *avfilter_make_format_list(const int *fmts)
  194. {
  195. MAKE_FORMAT_LIST();
  196. while (count--)
  197. formats->formats[count] = fmts[count];
  198. return formats;
  199. }
  200. AVFilterChannelLayouts *avfilter_make_format64_list(const int64_t *fmts)
  201. {
  202. MAKE_FORMAT_LIST();
  203. if (count)
  204. memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
  205. return (AVFilterChannelLayouts*)formats;
  206. }
  207. #define ADD_FORMAT(f, fmt, type, list, nb) \
  208. do { \
  209. type *fmts; \
  210. \
  211. if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) \
  212. return AVERROR(ENOMEM); \
  213. \
  214. fmts = av_realloc((*f)->list, \
  215. sizeof(*(*f)->list) * ((*f)->nb + 1));\
  216. if (!fmts) \
  217. return AVERROR(ENOMEM); \
  218. \
  219. (*f)->list = fmts; \
  220. (*f)->list[(*f)->nb++] = fmt; \
  221. return 0; \
  222. } while (0)
  223. int avfilter_add_format(AVFilterFormats **avff, int64_t fmt)
  224. {
  225. ADD_FORMAT(avff, fmt, int64_t, formats, format_count);
  226. }
  227. int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
  228. {
  229. ADD_FORMAT(l, channel_layout, uint64_t, channel_layouts, nb_channel_layouts);
  230. }
  231. #if FF_API_OLD_ALL_FORMATS_API
  232. AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
  233. {
  234. return avfilter_make_all_formats(type);
  235. }
  236. #endif
  237. AVFilterFormats *avfilter_make_all_formats(enum AVMediaType type)
  238. {
  239. AVFilterFormats *ret = NULL;
  240. int fmt;
  241. int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
  242. type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
  243. for (fmt = 0; fmt < num_formats; fmt++)
  244. if ((type != AVMEDIA_TYPE_VIDEO) ||
  245. (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
  246. avfilter_add_format(&ret, fmt);
  247. return ret;
  248. }
  249. const int64_t avfilter_all_channel_layouts[] = {
  250. #include "all_channel_layouts.h"
  251. -1
  252. };
  253. // AVFilterFormats *avfilter_make_all_channel_layouts(void)
  254. // {
  255. // return avfilter_make_format64_list(avfilter_all_channel_layouts);
  256. // }
  257. #if FF_API_PACKING
  258. AVFilterFormats *avfilter_make_all_packing_formats(void)
  259. {
  260. static const int packing[] = {
  261. AVFILTER_PACKED,
  262. AVFILTER_PLANAR,
  263. -1,
  264. };
  265. return avfilter_make_format_list(packing);
  266. }
  267. #endif
  268. AVFilterFormats *ff_all_samplerates(void)
  269. {
  270. AVFilterFormats *ret = av_mallocz(sizeof(*ret));
  271. return ret;
  272. }
  273. AVFilterChannelLayouts *ff_all_channel_layouts(void)
  274. {
  275. AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
  276. return ret;
  277. }
  278. #define FORMATS_REF(f, ref) \
  279. do { \
  280. *ref = f; \
  281. f->refs = av_realloc(f->refs, sizeof(*f->refs) * ++f->refcount); \
  282. f->refs[f->refcount-1] = ref; \
  283. } while (0)
  284. void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
  285. {
  286. FORMATS_REF(f, ref);
  287. }
  288. void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
  289. {
  290. FORMATS_REF(f, ref);
  291. }
  292. #define FIND_REF_INDEX(ref, idx) \
  293. do { \
  294. int i; \
  295. for (i = 0; i < (*ref)->refcount; i ++) \
  296. if((*ref)->refs[i] == ref) { \
  297. idx = i; \
  298. break; \
  299. } \
  300. } while (0)
  301. #define FORMATS_UNREF(ref, list) \
  302. do { \
  303. int idx = -1; \
  304. \
  305. if (!*ref) \
  306. return; \
  307. \
  308. FIND_REF_INDEX(ref, idx); \
  309. \
  310. if (idx >= 0) \
  311. memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
  312. sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
  313. \
  314. if(!--(*ref)->refcount) { \
  315. av_free((*ref)->list); \
  316. av_free((*ref)->refs); \
  317. av_free(*ref); \
  318. } \
  319. *ref = NULL; \
  320. } while (0)
  321. void avfilter_formats_unref(AVFilterFormats **ref)
  322. {
  323. FORMATS_UNREF(ref, formats);
  324. }
  325. void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
  326. {
  327. FORMATS_UNREF(ref, channel_layouts);
  328. }
  329. #define FORMATS_CHANGEREF(oldref, newref) \
  330. do { \
  331. int idx = -1; \
  332. \
  333. FIND_REF_INDEX(oldref, idx); \
  334. \
  335. if (idx >= 0) { \
  336. (*oldref)->refs[idx] = newref; \
  337. *newref = *oldref; \
  338. *oldref = NULL; \
  339. } \
  340. } while (0)
  341. void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
  342. AVFilterChannelLayouts **newref)
  343. {
  344. FORMATS_CHANGEREF(oldref, newref);
  345. }
  346. void avfilter_formats_changeref(AVFilterFormats **oldref,
  347. AVFilterFormats **newref)
  348. {
  349. FORMATS_CHANGEREF(oldref, newref);
  350. }
  351. #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
  352. { \
  353. int count = 0, i; \
  354. \
  355. for (i = 0; i < ctx->input_count; i++) { \
  356. if (ctx->inputs[i]) { \
  357. ref(fmts, &ctx->inputs[i]->out_fmts); \
  358. count++; \
  359. } \
  360. } \
  361. for (i = 0; i < ctx->output_count; i++) { \
  362. if (ctx->outputs[i]) { \
  363. ref(fmts, &ctx->outputs[i]->in_fmts); \
  364. count++; \
  365. } \
  366. } \
  367. \
  368. if (!count) { \
  369. av_freep(&fmts->list); \
  370. av_freep(&fmts->refs); \
  371. av_freep(&fmts); \
  372. } \
  373. }
  374. void ff_set_common_channel_layouts(AVFilterContext *ctx,
  375. AVFilterChannelLayouts *layouts)
  376. {
  377. SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
  378. ff_channel_layouts_ref, channel_layouts);
  379. }
  380. void ff_set_common_samplerates(AVFilterContext *ctx,
  381. AVFilterFormats *samplerates)
  382. {
  383. SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
  384. avfilter_formats_ref, formats);
  385. }
  386. /**
  387. * A helper for query_formats() which sets all links to the same list of
  388. * formats. If there are no links hooked to this filter, the list of formats is
  389. * freed.
  390. */
  391. void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  392. {
  393. SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
  394. avfilter_formats_ref, formats);
  395. }
  396. int avfilter_default_query_formats(AVFilterContext *ctx)
  397. {
  398. enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
  399. ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
  400. AVMEDIA_TYPE_VIDEO;
  401. avfilter_set_common_formats(ctx, avfilter_all_formats(type));
  402. if (type == AVMEDIA_TYPE_AUDIO) {
  403. ff_set_common_channel_layouts(ctx, ff_all_channel_layouts());
  404. ff_set_common_samplerates(ctx, ff_all_samplerates());
  405. }
  406. return 0;
  407. }
  408. /* internal functions for parsing audio format arguments */
  409. int ff_parse_pixel_format(enum PixelFormat *ret, const char *arg, void *log_ctx)
  410. {
  411. char *tail;
  412. int pix_fmt = av_get_pix_fmt(arg);
  413. if (pix_fmt == PIX_FMT_NONE) {
  414. pix_fmt = strtol(arg, &tail, 0);
  415. if (*tail || (unsigned)pix_fmt >= PIX_FMT_NB) {
  416. av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
  417. return AVERROR(EINVAL);
  418. }
  419. }
  420. *ret = pix_fmt;
  421. return 0;
  422. }
  423. int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
  424. {
  425. char *tail;
  426. int sfmt = av_get_sample_fmt(arg);
  427. if (sfmt == AV_SAMPLE_FMT_NONE) {
  428. sfmt = strtol(arg, &tail, 0);
  429. if (*tail || (unsigned)sfmt >= AV_SAMPLE_FMT_NB) {
  430. av_log(log_ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
  431. return AVERROR(EINVAL);
  432. }
  433. }
  434. *ret = sfmt;
  435. return 0;
  436. }
  437. int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
  438. {
  439. char *tail;
  440. double srate = av_strtod(arg, &tail);
  441. if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
  442. av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
  443. return AVERROR(EINVAL);
  444. }
  445. *ret = srate;
  446. return 0;
  447. }
  448. int ff_parse_channel_layout(int64_t *ret, const char *arg, void *log_ctx)
  449. {
  450. char *tail;
  451. int64_t chlayout = av_get_channel_layout(arg);
  452. if (chlayout == 0) {
  453. chlayout = strtol(arg, &tail, 10);
  454. if (*tail || chlayout == 0) {
  455. av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
  456. return AVERROR(EINVAL);
  457. }
  458. }
  459. *ret = chlayout;
  460. return 0;
  461. }
  462. #ifdef TEST
  463. #undef printf
  464. int main(void)
  465. {
  466. const int64_t *cl;
  467. char buf[512];
  468. for (cl = avfilter_all_channel_layouts; *cl != -1; cl++) {
  469. av_get_channel_layout_string(buf, sizeof(buf), -1, *cl);
  470. printf("%s\n", buf);
  471. }
  472. return 0;
  473. }
  474. #endif