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.

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