formats.c 28 KB

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