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.

735 lines
28KB

  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/avassert.h"
  22. #include "libavutil/channel_layout.h"
  23. #include "libavutil/common.h"
  24. #include "libavutil/eval.h"
  25. #include "libavutil/pixdesc.h"
  26. #include "avfilter.h"
  27. #include "internal.h"
  28. #include "formats.h"
  29. #define KNOWN(l) (!FF_LAYOUT2COUNT(l)) /* for readability */
  30. /**
  31. * Add all refs from a to ret and destroy a.
  32. */
  33. #define MERGE_REF(ret, a, fmts, type, fail_statement) \
  34. do { \
  35. type ***tmp; \
  36. int i; \
  37. \
  38. if (!(tmp = av_realloc_array(ret->refs, ret->refcount + a->refcount, \
  39. sizeof(*tmp)))) \
  40. { fail_statement } \
  41. ret->refs = tmp; \
  42. \
  43. for (i = 0; i < a->refcount; i ++) { \
  44. ret->refs[ret->refcount] = a->refs[i]; \
  45. *ret->refs[ret->refcount++] = ret; \
  46. } \
  47. \
  48. av_freep(&a->refs); \
  49. av_freep(&a->fmts); \
  50. av_freep(&a); \
  51. } while (0)
  52. /**
  53. * Add all formats common to a and b to a, add b's refs to a and destroy b.
  54. * If check is set, nothing is modified and it is only checked whether
  55. * the formats are compatible.
  56. * If empty_allowed is set and one of a,b->nb is zero, the lists are
  57. * merged; otherwise, it is treated as error.
  58. */
  59. #define MERGE_FORMATS(a, b, fmts, nb, type, check, empty_allowed) \
  60. do { \
  61. int i, j, k = 0, skip = 0; \
  62. \
  63. if (empty_allowed) { \
  64. if (!a->nb || !b->nb) { \
  65. if (check) \
  66. return 1; \
  67. if (!a->nb) \
  68. FFSWAP(type *, a, b); \
  69. skip = 1; \
  70. } \
  71. } \
  72. if (!skip) { \
  73. for (i = 0; i < a->nb; i++) \
  74. for (j = 0; j < b->nb; j++) \
  75. if (a->fmts[i] == b->fmts[j]) { \
  76. if (check) \
  77. return 1; \
  78. a->fmts[k++] = a->fmts[i]; \
  79. break; \
  80. } \
  81. /* Check that there was at least one common format. \
  82. * Notice that both a and b are unchanged if not. */ \
  83. if (!k) \
  84. return 0; \
  85. av_assert2(!check); \
  86. a->nb = k; \
  87. } \
  88. \
  89. MERGE_REF(a, b, fmts, type, return AVERROR(ENOMEM);); \
  90. } while (0)
  91. static int merge_formats_internal(AVFilterFormats *a, AVFilterFormats *b,
  92. enum AVMediaType type, int check)
  93. {
  94. int i, j;
  95. int alpha1=0, alpha2=0;
  96. int chroma1=0, chroma2=0;
  97. if (a == b)
  98. return 1;
  99. /* Do not lose chroma or alpha in merging.
  100. It happens if both lists have formats with chroma (resp. alpha), but
  101. the only formats in common do not have it (e.g. YUV+gray vs.
  102. RGB+gray): in that case, the merging would select the gray format,
  103. possibly causing a lossy conversion elsewhere in the graph.
  104. To avoid that, pretend that there are no common formats to force the
  105. insertion of a conversion filter. */
  106. if (type == AVMEDIA_TYPE_VIDEO)
  107. for (i = 0; i < a->nb_formats; i++)
  108. for (j = 0; j < b->nb_formats; j++) {
  109. const AVPixFmtDescriptor *adesc = av_pix_fmt_desc_get(a->formats[i]);
  110. const AVPixFmtDescriptor *bdesc = av_pix_fmt_desc_get(b->formats[j]);
  111. alpha2 |= adesc->flags & bdesc->flags & AV_PIX_FMT_FLAG_ALPHA;
  112. chroma2|= adesc->nb_components > 1 && bdesc->nb_components > 1;
  113. if (a->formats[i] == b->formats[j]) {
  114. alpha1 |= adesc->flags & AV_PIX_FMT_FLAG_ALPHA;
  115. chroma1|= adesc->nb_components > 1;
  116. }
  117. }
  118. // If chroma or alpha can be lost through merging then do not merge
  119. if (alpha2 > alpha1 || chroma2 > chroma1)
  120. return 0;
  121. MERGE_FORMATS(a, b, formats, nb_formats, AVFilterFormats, check, 0);
  122. return 1;
  123. }
  124. int ff_can_merge_formats(const AVFilterFormats *a, const AVFilterFormats *b,
  125. enum AVMediaType type)
  126. {
  127. return merge_formats_internal((AVFilterFormats *)a,
  128. (AVFilterFormats *)b, type, 1);
  129. }
  130. int ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b,
  131. enum AVMediaType type)
  132. {
  133. av_assert2(a->refcount && b->refcount);
  134. return merge_formats_internal(a, b, type, 0);
  135. }
  136. static int merge_samplerates_internal(AVFilterFormats *a,
  137. AVFilterFormats *b, int check)
  138. {
  139. if (a == b) return 1;
  140. MERGE_FORMATS(a, b, formats, nb_formats, AVFilterFormats, check, 1);
  141. return 1;
  142. }
  143. int ff_can_merge_samplerates(const AVFilterFormats *a, const AVFilterFormats *b)
  144. {
  145. return merge_samplerates_internal((AVFilterFormats *)a, (AVFilterFormats *)b, 1);
  146. }
  147. int ff_merge_samplerates(AVFilterFormats *a, AVFilterFormats *b)
  148. {
  149. av_assert2(a->refcount && b->refcount);
  150. return merge_samplerates_internal(a, b, 0);
  151. }
  152. int ff_merge_channel_layouts(AVFilterChannelLayouts *a,
  153. AVFilterChannelLayouts *b)
  154. {
  155. uint64_t *channel_layouts;
  156. unsigned a_all = a->all_layouts + a->all_counts;
  157. unsigned b_all = b->all_layouts + b->all_counts;
  158. int ret_max, ret_nb = 0, i, j, round;
  159. av_assert2(a->refcount && b->refcount);
  160. if (a == b) return 1;
  161. /* Put the most generic set in a, to avoid doing everything twice */
  162. if (a_all < b_all) {
  163. FFSWAP(AVFilterChannelLayouts *, a, b);
  164. FFSWAP(unsigned, a_all, b_all);
  165. }
  166. if (a_all) {
  167. if (a_all == 1 && !b_all) {
  168. /* keep only known layouts in b; works also for b_all = 1 */
  169. for (i = j = 0; i < b->nb_channel_layouts; i++)
  170. if (KNOWN(b->channel_layouts[i]))
  171. b->channel_layouts[j++] = b->channel_layouts[i];
  172. /* Not optimal: the unknown layouts of b may become known after
  173. another merge. */
  174. if (!j)
  175. return 0;
  176. b->nb_channel_layouts = j;
  177. }
  178. MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, return AVERROR(ENOMEM););
  179. return 1;
  180. }
  181. ret_max = a->nb_channel_layouts + b->nb_channel_layouts;
  182. if (!(channel_layouts = av_malloc_array(ret_max, sizeof(*channel_layouts))))
  183. return AVERROR(ENOMEM);
  184. /* a[known] intersect b[known] */
  185. for (i = 0; i < a->nb_channel_layouts; i++) {
  186. if (!KNOWN(a->channel_layouts[i]))
  187. continue;
  188. for (j = 0; j < b->nb_channel_layouts; j++) {
  189. if (a->channel_layouts[i] == b->channel_layouts[j]) {
  190. channel_layouts[ret_nb++] = a->channel_layouts[i];
  191. a->channel_layouts[i] = b->channel_layouts[j] = 0;
  192. break;
  193. }
  194. }
  195. }
  196. /* 1st round: a[known] intersect b[generic]
  197. 2nd round: a[generic] intersect b[known] */
  198. for (round = 0; round < 2; round++) {
  199. for (i = 0; i < a->nb_channel_layouts; i++) {
  200. uint64_t fmt = a->channel_layouts[i], bfmt;
  201. if (!fmt || !KNOWN(fmt))
  202. continue;
  203. bfmt = FF_COUNT2LAYOUT(av_get_channel_layout_nb_channels(fmt));
  204. for (j = 0; j < b->nb_channel_layouts; j++)
  205. if (b->channel_layouts[j] == bfmt)
  206. channel_layouts[ret_nb++] = a->channel_layouts[i];
  207. }
  208. /* 1st round: swap to prepare 2nd round; 2nd round: put it back */
  209. FFSWAP(AVFilterChannelLayouts *, a, b);
  210. }
  211. /* a[generic] intersect b[generic] */
  212. for (i = 0; i < a->nb_channel_layouts; i++) {
  213. if (KNOWN(a->channel_layouts[i]))
  214. continue;
  215. for (j = 0; j < b->nb_channel_layouts; j++)
  216. if (a->channel_layouts[i] == b->channel_layouts[j])
  217. channel_layouts[ret_nb++] = a->channel_layouts[i];
  218. }
  219. if (!ret_nb) {
  220. av_free(channel_layouts);
  221. return 0;
  222. }
  223. if (a->refcount > b->refcount)
  224. FFSWAP(AVFilterChannelLayouts *, a, b);
  225. MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts,
  226. { av_free(channel_layouts); return AVERROR(ENOMEM); });
  227. av_freep(&b->channel_layouts);
  228. b->channel_layouts = channel_layouts;
  229. b->nb_channel_layouts = ret_nb;
  230. return 1;
  231. }
  232. int ff_fmt_is_in(int fmt, const int *fmts)
  233. {
  234. const int *p;
  235. for (p = fmts; *p != -1; p++) {
  236. if (fmt == *p)
  237. return 1;
  238. }
  239. return 0;
  240. }
  241. #define MAKE_FORMAT_LIST(type, field, count_field) \
  242. type *formats; \
  243. int count = 0; \
  244. if (fmts) \
  245. for (count = 0; fmts[count] != -1; count++) \
  246. ; \
  247. formats = av_mallocz(sizeof(*formats)); \
  248. if (!formats) \
  249. return NULL; \
  250. formats->count_field = count; \
  251. if (count) { \
  252. formats->field = av_malloc_array(count, sizeof(*formats->field)); \
  253. if (!formats->field) { \
  254. av_freep(&formats); \
  255. return NULL; \
  256. } \
  257. }
  258. AVFilterFormats *ff_make_format_list(const int *fmts)
  259. {
  260. MAKE_FORMAT_LIST(AVFilterFormats, formats, nb_formats);
  261. while (count--)
  262. formats->formats[count] = fmts[count];
  263. return formats;
  264. }
  265. AVFilterChannelLayouts *ff_make_format64_list(const int64_t *fmts)
  266. {
  267. MAKE_FORMAT_LIST(AVFilterChannelLayouts,
  268. channel_layouts, nb_channel_layouts);
  269. if (count)
  270. memcpy(formats->channel_layouts, fmts,
  271. sizeof(*formats->channel_layouts) * count);
  272. return formats;
  273. }
  274. #if LIBAVFILTER_VERSION_MAJOR < 8
  275. AVFilterChannelLayouts *avfilter_make_format64_list(const int64_t *fmts)
  276. {
  277. return ff_make_format64_list(fmts);
  278. }
  279. #endif
  280. #define ADD_FORMAT(f, fmt, unref_fn, type, list, nb) \
  281. do { \
  282. type *fmts; \
  283. \
  284. if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) { \
  285. return AVERROR(ENOMEM); \
  286. } \
  287. \
  288. fmts = av_realloc_array((*f)->list, (*f)->nb + 1, \
  289. sizeof(*(*f)->list)); \
  290. if (!fmts) { \
  291. unref_fn(f); \
  292. return AVERROR(ENOMEM); \
  293. } \
  294. \
  295. (*f)->list = fmts; \
  296. (*f)->list[(*f)->nb++] = fmt; \
  297. } while (0)
  298. int ff_add_format(AVFilterFormats **avff, int64_t fmt)
  299. {
  300. ADD_FORMAT(avff, fmt, ff_formats_unref, int, formats, nb_formats);
  301. return 0;
  302. }
  303. int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
  304. {
  305. av_assert1(!(*l && (*l)->all_layouts));
  306. ADD_FORMAT(l, channel_layout, ff_channel_layouts_unref, uint64_t, channel_layouts, nb_channel_layouts);
  307. return 0;
  308. }
  309. AVFilterFormats *ff_all_formats(enum AVMediaType type)
  310. {
  311. AVFilterFormats *ret = NULL;
  312. if (type == AVMEDIA_TYPE_VIDEO) {
  313. const AVPixFmtDescriptor *desc = NULL;
  314. while ((desc = av_pix_fmt_desc_next(desc))) {
  315. if (ff_add_format(&ret, av_pix_fmt_desc_get_id(desc)) < 0)
  316. return NULL;
  317. }
  318. } else if (type == AVMEDIA_TYPE_AUDIO) {
  319. enum AVSampleFormat fmt = 0;
  320. while (av_get_sample_fmt_name(fmt)) {
  321. if (ff_add_format(&ret, fmt) < 0)
  322. return NULL;
  323. fmt++;
  324. }
  325. }
  326. return ret;
  327. }
  328. int ff_formats_pixdesc_filter(AVFilterFormats **rfmts, unsigned want, unsigned rej)
  329. {
  330. unsigned nb_formats, fmt, flags;
  331. AVFilterFormats *formats = NULL;
  332. while (1) {
  333. nb_formats = 0;
  334. for (fmt = 0;; fmt++) {
  335. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  336. if (!desc)
  337. break;
  338. flags = desc->flags;
  339. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL) &&
  340. !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
  341. (desc->log2_chroma_w || desc->log2_chroma_h))
  342. flags |= FF_PIX_FMT_FLAG_SW_FLAT_SUB;
  343. if ((flags & (want | rej)) != want)
  344. continue;
  345. if (formats)
  346. formats->formats[nb_formats] = fmt;
  347. nb_formats++;
  348. }
  349. if (formats) {
  350. av_assert0(formats->nb_formats == nb_formats);
  351. *rfmts = formats;
  352. return 0;
  353. }
  354. formats = av_mallocz(sizeof(*formats));
  355. if (!formats)
  356. return AVERROR(ENOMEM);
  357. formats->nb_formats = nb_formats;
  358. if (nb_formats) {
  359. formats->formats = av_malloc_array(nb_formats, sizeof(*formats->formats));
  360. if (!formats->formats) {
  361. av_freep(&formats);
  362. return AVERROR(ENOMEM);
  363. }
  364. }
  365. }
  366. }
  367. AVFilterFormats *ff_planar_sample_fmts(void)
  368. {
  369. AVFilterFormats *ret = NULL;
  370. int fmt;
  371. for (fmt = 0; av_get_bytes_per_sample(fmt)>0; fmt++)
  372. if (av_sample_fmt_is_planar(fmt))
  373. if (ff_add_format(&ret, fmt) < 0)
  374. return NULL;
  375. return ret;
  376. }
  377. AVFilterFormats *ff_all_samplerates(void)
  378. {
  379. AVFilterFormats *ret = av_mallocz(sizeof(*ret));
  380. return ret;
  381. }
  382. AVFilterChannelLayouts *ff_all_channel_layouts(void)
  383. {
  384. AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
  385. if (!ret)
  386. return NULL;
  387. ret->all_layouts = 1;
  388. return ret;
  389. }
  390. AVFilterChannelLayouts *ff_all_channel_counts(void)
  391. {
  392. AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
  393. if (!ret)
  394. return NULL;
  395. ret->all_layouts = ret->all_counts = 1;
  396. return ret;
  397. }
  398. #define FORMATS_REF(f, ref, unref_fn) \
  399. void *tmp; \
  400. \
  401. if (!f) \
  402. return AVERROR(ENOMEM); \
  403. \
  404. tmp = av_realloc_array(f->refs, sizeof(*f->refs), f->refcount + 1); \
  405. if (!tmp) { \
  406. unref_fn(&f); \
  407. return AVERROR(ENOMEM); \
  408. } \
  409. f->refs = tmp; \
  410. f->refs[f->refcount++] = ref; \
  411. *ref = f; \
  412. return 0
  413. int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
  414. {
  415. FORMATS_REF(f, ref, ff_channel_layouts_unref);
  416. }
  417. int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
  418. {
  419. FORMATS_REF(f, ref, ff_formats_unref);
  420. }
  421. #define FIND_REF_INDEX(ref, idx) \
  422. do { \
  423. int i; \
  424. for (i = 0; i < (*ref)->refcount; i ++) \
  425. if((*ref)->refs[i] == ref) { \
  426. idx = i; \
  427. break; \
  428. } \
  429. } while (0)
  430. #define FORMATS_UNREF(ref, list) \
  431. do { \
  432. int idx = -1; \
  433. \
  434. if (!*ref) \
  435. return; \
  436. \
  437. FIND_REF_INDEX(ref, idx); \
  438. \
  439. if (idx >= 0) { \
  440. memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
  441. sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
  442. --(*ref)->refcount; \
  443. } \
  444. if (!(*ref)->refcount) { \
  445. av_free((*ref)->list); \
  446. av_free((*ref)->refs); \
  447. av_free(*ref); \
  448. } \
  449. *ref = NULL; \
  450. } while (0)
  451. void ff_formats_unref(AVFilterFormats **ref)
  452. {
  453. FORMATS_UNREF(ref, formats);
  454. }
  455. void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
  456. {
  457. FORMATS_UNREF(ref, channel_layouts);
  458. }
  459. #define FORMATS_CHANGEREF(oldref, newref) \
  460. do { \
  461. int idx = -1; \
  462. \
  463. FIND_REF_INDEX(oldref, idx); \
  464. \
  465. if (idx >= 0) { \
  466. (*oldref)->refs[idx] = newref; \
  467. *newref = *oldref; \
  468. *oldref = NULL; \
  469. } \
  470. } while (0)
  471. void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
  472. AVFilterChannelLayouts **newref)
  473. {
  474. FORMATS_CHANGEREF(oldref, newref);
  475. }
  476. void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
  477. {
  478. FORMATS_CHANGEREF(oldref, newref);
  479. }
  480. #define SET_COMMON_FORMATS(ctx, fmts, ref_fn, unref_fn) \
  481. int count = 0, i; \
  482. \
  483. if (!fmts) \
  484. return AVERROR(ENOMEM); \
  485. \
  486. for (i = 0; i < ctx->nb_inputs; i++) { \
  487. if (ctx->inputs[i] && !ctx->inputs[i]->outcfg.fmts) { \
  488. int ret = ref_fn(fmts, &ctx->inputs[i]->outcfg.fmts); \
  489. if (ret < 0) { \
  490. return ret; \
  491. } \
  492. count++; \
  493. } \
  494. } \
  495. for (i = 0; i < ctx->nb_outputs; i++) { \
  496. if (ctx->outputs[i] && !ctx->outputs[i]->incfg.fmts) { \
  497. int ret = ref_fn(fmts, &ctx->outputs[i]->incfg.fmts); \
  498. if (ret < 0) { \
  499. return ret; \
  500. } \
  501. count++; \
  502. } \
  503. } \
  504. \
  505. if (!count) { \
  506. unref_fn(&fmts); \
  507. } \
  508. \
  509. return 0;
  510. int ff_set_common_channel_layouts(AVFilterContext *ctx,
  511. AVFilterChannelLayouts *channel_layouts)
  512. {
  513. SET_COMMON_FORMATS(ctx, channel_layouts,
  514. ff_channel_layouts_ref, ff_channel_layouts_unref);
  515. }
  516. int ff_set_common_samplerates(AVFilterContext *ctx,
  517. AVFilterFormats *samplerates)
  518. {
  519. SET_COMMON_FORMATS(ctx, samplerates,
  520. ff_formats_ref, ff_formats_unref);
  521. }
  522. /**
  523. * A helper for query_formats() which sets all links to the same list of
  524. * formats. If there are no links hooked to this filter, the list of formats is
  525. * freed.
  526. */
  527. int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  528. {
  529. SET_COMMON_FORMATS(ctx, formats,
  530. ff_formats_ref, ff_formats_unref);
  531. }
  532. int ff_default_query_formats(AVFilterContext *ctx)
  533. {
  534. int ret;
  535. enum AVMediaType type = ctx->nb_inputs ? ctx->inputs [0]->type :
  536. ctx->nb_outputs ? ctx->outputs[0]->type :
  537. AVMEDIA_TYPE_VIDEO;
  538. ret = ff_set_common_formats(ctx, ff_all_formats(type));
  539. if (ret < 0)
  540. return ret;
  541. if (type == AVMEDIA_TYPE_AUDIO) {
  542. ret = ff_set_common_channel_layouts(ctx, ff_all_channel_counts());
  543. if (ret < 0)
  544. return ret;
  545. ret = ff_set_common_samplerates(ctx, ff_all_samplerates());
  546. if (ret < 0)
  547. return ret;
  548. }
  549. return 0;
  550. }
  551. /* internal functions for parsing audio format arguments */
  552. int ff_parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx)
  553. {
  554. char *tail;
  555. int pix_fmt = av_get_pix_fmt(arg);
  556. if (pix_fmt == AV_PIX_FMT_NONE) {
  557. pix_fmt = strtol(arg, &tail, 0);
  558. if (*tail || !av_pix_fmt_desc_get(pix_fmt)) {
  559. av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
  560. return AVERROR(EINVAL);
  561. }
  562. }
  563. *ret = pix_fmt;
  564. return 0;
  565. }
  566. int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
  567. {
  568. char *tail;
  569. double srate = av_strtod(arg, &tail);
  570. if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
  571. av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
  572. return AVERROR(EINVAL);
  573. }
  574. *ret = srate;
  575. return 0;
  576. }
  577. int ff_parse_channel_layout(int64_t *ret, int *nret, const char *arg,
  578. void *log_ctx)
  579. {
  580. int64_t chlayout;
  581. int nb_channels;
  582. if (av_get_extended_channel_layout(arg, &chlayout, &nb_channels) < 0) {
  583. av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
  584. return AVERROR(EINVAL);
  585. }
  586. if (!chlayout && !nret) {
  587. av_log(log_ctx, AV_LOG_ERROR, "Unknown channel layout '%s' is not supported.\n", arg);
  588. return AVERROR(EINVAL);
  589. }
  590. *ret = chlayout;
  591. if (nret)
  592. *nret = nb_channels;
  593. return 0;
  594. }
  595. static int check_list(void *log, const char *name, const AVFilterFormats *fmts)
  596. {
  597. unsigned i, j;
  598. if (!fmts)
  599. return 0;
  600. if (!fmts->nb_formats) {
  601. av_log(log, AV_LOG_ERROR, "Empty %s list\n", name);
  602. return AVERROR(EINVAL);
  603. }
  604. for (i = 0; i < fmts->nb_formats; i++) {
  605. for (j = i + 1; j < fmts->nb_formats; j++) {
  606. if (fmts->formats[i] == fmts->formats[j]) {
  607. av_log(log, AV_LOG_ERROR, "Duplicated %s\n", name);
  608. return AVERROR(EINVAL);
  609. }
  610. }
  611. }
  612. return 0;
  613. }
  614. int ff_formats_check_pixel_formats(void *log, const AVFilterFormats *fmts)
  615. {
  616. return check_list(log, "pixel format", fmts);
  617. }
  618. int ff_formats_check_sample_formats(void *log, const AVFilterFormats *fmts)
  619. {
  620. return check_list(log, "sample format", fmts);
  621. }
  622. int ff_formats_check_sample_rates(void *log, const AVFilterFormats *fmts)
  623. {
  624. if (!fmts || !fmts->nb_formats)
  625. return 0;
  626. return check_list(log, "sample rate", fmts);
  627. }
  628. static int layouts_compatible(uint64_t a, uint64_t b)
  629. {
  630. return a == b ||
  631. (KNOWN(a) && !KNOWN(b) && av_get_channel_layout_nb_channels(a) == FF_LAYOUT2COUNT(b)) ||
  632. (KNOWN(b) && !KNOWN(a) && av_get_channel_layout_nb_channels(b) == FF_LAYOUT2COUNT(a));
  633. }
  634. int ff_formats_check_channel_layouts(void *log, const AVFilterChannelLayouts *fmts)
  635. {
  636. unsigned i, j;
  637. if (!fmts)
  638. return 0;
  639. if (fmts->all_layouts < fmts->all_counts) {
  640. av_log(log, AV_LOG_ERROR, "Inconsistent generic list\n");
  641. return AVERROR(EINVAL);
  642. }
  643. if (!fmts->all_layouts && !fmts->nb_channel_layouts) {
  644. av_log(log, AV_LOG_ERROR, "Empty channel layout list\n");
  645. return AVERROR(EINVAL);
  646. }
  647. for (i = 0; i < fmts->nb_channel_layouts; i++) {
  648. for (j = i + 1; j < fmts->nb_channel_layouts; j++) {
  649. if (layouts_compatible(fmts->channel_layouts[i], fmts->channel_layouts[j])) {
  650. av_log(log, AV_LOG_ERROR, "Duplicated or redundant channel layout\n");
  651. return AVERROR(EINVAL);
  652. }
  653. }
  654. }
  655. return 0;
  656. }