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.

653 lines
24KB

  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(ret->refs, \
  40. sizeof(*tmp) * (ret->refcount + a->refcount)))) \
  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(sizeof(*ret->fmts) * count))) \
  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 avfilter_merge_formats() detected\n"); \
  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. {
  89. AVFilterFormats *ret = NULL;
  90. int i, j;
  91. int alpha1=0, alpha2=0;
  92. int chroma1=0, chroma2=0;
  93. if (a == b)
  94. return a;
  95. for (i = 0; i < a->format_count; i++)
  96. for (j = 0; j < b->format_count; j++) {
  97. const AVPixFmtDescriptor *adesc = av_pix_fmt_desc_get(a->formats[i]);
  98. const AVPixFmtDescriptor *bdesc = av_pix_fmt_desc_get(b->formats[j]);
  99. alpha2 |= adesc->flags & bdesc->flags & PIX_FMT_ALPHA;
  100. chroma2|= adesc->nb_components > 1 && bdesc->nb_components > 1;
  101. if (a->formats[i] == b->formats[j]) {
  102. alpha1 |= adesc->flags & PIX_FMT_ALPHA;
  103. chroma1|= adesc->nb_components > 1;
  104. }
  105. }
  106. // If chroma or alpha can be lost through merging then do not merge
  107. if (alpha2 > alpha1 || chroma2 > chroma1)
  108. return NULL;
  109. MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
  110. return ret;
  111. fail:
  112. if (ret) {
  113. av_freep(&ret->refs);
  114. av_freep(&ret->formats);
  115. }
  116. av_freep(&ret);
  117. return NULL;
  118. }
  119. AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a,
  120. AVFilterFormats *b)
  121. {
  122. AVFilterFormats *ret = NULL;
  123. if (a == b) return a;
  124. if (a->format_count && b->format_count) {
  125. MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
  126. } else if (a->format_count) {
  127. MERGE_REF(a, b, formats, AVFilterFormats, fail);
  128. ret = a;
  129. } else {
  130. MERGE_REF(b, a, formats, AVFilterFormats, fail);
  131. ret = b;
  132. }
  133. return ret;
  134. fail:
  135. if (ret) {
  136. av_freep(&ret->refs);
  137. av_freep(&ret->formats);
  138. }
  139. av_freep(&ret);
  140. return NULL;
  141. }
  142. AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a,
  143. AVFilterChannelLayouts *b)
  144. {
  145. AVFilterChannelLayouts *ret = NULL;
  146. unsigned a_all = a->all_layouts + a->all_counts;
  147. unsigned b_all = b->all_layouts + b->all_counts;
  148. int ret_max, ret_nb = 0, i, j, round;
  149. if (a == b) return a;
  150. /* Put the most generic set in a, to avoid doing everything twice */
  151. if (a_all < b_all) {
  152. FFSWAP(AVFilterChannelLayouts *, a, b);
  153. FFSWAP(unsigned, a_all, b_all);
  154. }
  155. if (a_all) {
  156. if (a_all == 1 && !b_all) {
  157. /* keep only known layouts in b; works also for b_all = 1 */
  158. for (i = j = 0; i < b->nb_channel_layouts; i++)
  159. if (KNOWN(b->channel_layouts[i]))
  160. b->channel_layouts[j++] = b->channel_layouts[i];
  161. b->nb_channel_layouts = j;
  162. }
  163. MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, fail);
  164. return b;
  165. }
  166. ret_max = a->nb_channel_layouts + b->nb_channel_layouts;
  167. if (!(ret = av_mallocz(sizeof(*ret))) ||
  168. !(ret->channel_layouts = av_malloc(sizeof(*ret->channel_layouts) *
  169. ret_max)))
  170. goto fail;
  171. /* a[known] intersect b[known] */
  172. for (i = 0; i < a->nb_channel_layouts; i++) {
  173. if (!KNOWN(a->channel_layouts[i]))
  174. continue;
  175. for (j = 0; j < b->nb_channel_layouts; j++) {
  176. if (a->channel_layouts[i] == b->channel_layouts[j]) {
  177. ret->channel_layouts[ret_nb++] = a->channel_layouts[i];
  178. a->channel_layouts[i] = b->channel_layouts[j] = 0;
  179. }
  180. }
  181. }
  182. /* 1st round: a[known] intersect b[generic]
  183. 2nd round: a[generic] intersect b[known] */
  184. for (round = 0; round < 2; round++) {
  185. for (i = 0; i < a->nb_channel_layouts; i++) {
  186. uint64_t fmt = a->channel_layouts[i], bfmt;
  187. if (!fmt || !KNOWN(fmt))
  188. continue;
  189. bfmt = FF_COUNT2LAYOUT(av_get_channel_layout_nb_channels(fmt));
  190. for (j = 0; j < b->nb_channel_layouts; j++)
  191. if (b->channel_layouts[j] == bfmt)
  192. ret->channel_layouts[ret_nb++] = a->channel_layouts[i];
  193. }
  194. /* 1st round: swap to prepare 2nd round; 2nd round: put it back */
  195. FFSWAP(AVFilterChannelLayouts *, a, b);
  196. }
  197. /* a[generic] intersect b[generic] */
  198. for (i = 0; i < a->nb_channel_layouts; i++) {
  199. if (KNOWN(a->channel_layouts[i]))
  200. continue;
  201. for (j = 0; j < b->nb_channel_layouts; j++)
  202. if (a->channel_layouts[i] == b->channel_layouts[j])
  203. ret->channel_layouts[ret_nb++] = a->channel_layouts[i];
  204. }
  205. ret->nb_channel_layouts = ret_nb;
  206. if (!ret->nb_channel_layouts)
  207. goto fail;
  208. MERGE_REF(ret, a, channel_layouts, AVFilterChannelLayouts, fail);
  209. MERGE_REF(ret, b, channel_layouts, AVFilterChannelLayouts, fail);
  210. return ret;
  211. fail:
  212. if (ret) {
  213. av_freep(&ret->refs);
  214. av_freep(&ret->channel_layouts);
  215. }
  216. av_freep(&ret);
  217. return NULL;
  218. }
  219. int ff_fmt_is_in(int fmt, const int *fmts)
  220. {
  221. const int *p;
  222. for (p = fmts; *p != -1; p++) {
  223. if (fmt == *p)
  224. return 1;
  225. }
  226. return 0;
  227. }
  228. #define COPY_INT_LIST(list_copy, list, type) { \
  229. int count = 0; \
  230. if (list) \
  231. for (count = 0; list[count] != -1; count++) \
  232. ; \
  233. list_copy = av_calloc(count+1, sizeof(type)); \
  234. if (list_copy) { \
  235. memcpy(list_copy, list, sizeof(type) * count); \
  236. list_copy[count] = -1; \
  237. } \
  238. }
  239. int *ff_copy_int_list(const int * const list)
  240. {
  241. int *ret = NULL;
  242. COPY_INT_LIST(ret, list, int);
  243. return ret;
  244. }
  245. int64_t *ff_copy_int64_list(const int64_t * const list)
  246. {
  247. int64_t *ret = NULL;
  248. COPY_INT_LIST(ret, list, int64_t);
  249. return ret;
  250. }
  251. #define MAKE_FORMAT_LIST(type, field, count_field) \
  252. type *formats; \
  253. int count = 0; \
  254. if (fmts) \
  255. for (count = 0; fmts[count] != -1; count++) \
  256. ; \
  257. formats = av_mallocz(sizeof(*formats)); \
  258. if (!formats) return NULL; \
  259. formats->count_field = count; \
  260. if (count) { \
  261. formats->field = av_malloc(sizeof(*formats->field)*count); \
  262. if (!formats->field) { \
  263. av_free(formats); \
  264. return NULL; \
  265. } \
  266. }
  267. AVFilterFormats *ff_make_format_list(const int *fmts)
  268. {
  269. MAKE_FORMAT_LIST(AVFilterFormats, formats, format_count);
  270. while (count--)
  271. formats->formats[count] = fmts[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, type, list, nb) \
  284. do { \
  285. type *fmts; \
  286. \
  287. if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) \
  288. return AVERROR(ENOMEM); \
  289. \
  290. fmts = av_realloc((*f)->list, \
  291. sizeof(*(*f)->list) * ((*f)->nb + 1));\
  292. if (!fmts) \
  293. return AVERROR(ENOMEM); \
  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, int, formats, format_count);
  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, 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. int fmt;
  313. int num_formats = type == AVMEDIA_TYPE_VIDEO ? AV_PIX_FMT_NB :
  314. type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
  315. for (fmt = 0; fmt < num_formats; fmt++) {
  316. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  317. if ((type != AVMEDIA_TYPE_VIDEO) ||
  318. (type == AVMEDIA_TYPE_VIDEO && !(desc->flags & PIX_FMT_HWACCEL)))
  319. ff_add_format(&ret, fmt);
  320. }
  321. return ret;
  322. }
  323. const int64_t avfilter_all_channel_layouts[] = {
  324. #include "all_channel_layouts.inc"
  325. -1
  326. };
  327. // AVFilterFormats *avfilter_make_all_channel_layouts(void)
  328. // {
  329. // return avfilter_make_format64_list(avfilter_all_channel_layouts);
  330. // }
  331. AVFilterFormats *ff_planar_sample_fmts(void)
  332. {
  333. AVFilterFormats *ret = NULL;
  334. int fmt;
  335. for (fmt = 0; fmt < AV_SAMPLE_FMT_NB; fmt++)
  336. if (av_sample_fmt_is_planar(fmt))
  337. ff_add_format(&ret, fmt);
  338. return ret;
  339. }
  340. AVFilterFormats *ff_all_samplerates(void)
  341. {
  342. AVFilterFormats *ret = av_mallocz(sizeof(*ret));
  343. return ret;
  344. }
  345. AVFilterChannelLayouts *ff_all_channel_layouts(void)
  346. {
  347. AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
  348. if (!ret)
  349. return NULL;
  350. ret->all_layouts = 1;
  351. return ret;
  352. }
  353. AVFilterChannelLayouts *ff_all_channel_counts(void)
  354. {
  355. AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
  356. if (!ret)
  357. return NULL;
  358. ret->all_layouts = ret->all_counts = 1;
  359. return ret;
  360. }
  361. #define FORMATS_REF(f, ref) \
  362. do { \
  363. *ref = f; \
  364. f->refs = av_realloc(f->refs, sizeof(*f->refs) * ++f->refcount); \
  365. f->refs[f->refcount-1] = ref; \
  366. } while (0)
  367. void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
  368. {
  369. FORMATS_REF(f, ref);
  370. }
  371. void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
  372. {
  373. FORMATS_REF(f, ref);
  374. }
  375. #define FIND_REF_INDEX(ref, idx) \
  376. do { \
  377. int i; \
  378. for (i = 0; i < (*ref)->refcount; i ++) \
  379. if((*ref)->refs[i] == ref) { \
  380. idx = i; \
  381. break; \
  382. } \
  383. } while (0)
  384. #define FORMATS_UNREF(ref, list) \
  385. do { \
  386. int idx = -1; \
  387. \
  388. if (!*ref) \
  389. return; \
  390. \
  391. FIND_REF_INDEX(ref, idx); \
  392. \
  393. if (idx >= 0) \
  394. memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
  395. sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
  396. \
  397. if(!--(*ref)->refcount) { \
  398. av_free((*ref)->list); \
  399. av_free((*ref)->refs); \
  400. av_free(*ref); \
  401. } \
  402. *ref = NULL; \
  403. } while (0)
  404. void ff_formats_unref(AVFilterFormats **ref)
  405. {
  406. FORMATS_UNREF(ref, formats);
  407. }
  408. void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
  409. {
  410. FORMATS_UNREF(ref, channel_layouts);
  411. }
  412. #define FORMATS_CHANGEREF(oldref, newref) \
  413. do { \
  414. int idx = -1; \
  415. \
  416. FIND_REF_INDEX(oldref, idx); \
  417. \
  418. if (idx >= 0) { \
  419. (*oldref)->refs[idx] = newref; \
  420. *newref = *oldref; \
  421. *oldref = NULL; \
  422. } \
  423. } while (0)
  424. void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
  425. AVFilterChannelLayouts **newref)
  426. {
  427. FORMATS_CHANGEREF(oldref, newref);
  428. }
  429. void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
  430. {
  431. FORMATS_CHANGEREF(oldref, newref);
  432. }
  433. #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
  434. { \
  435. int count = 0, i; \
  436. \
  437. for (i = 0; i < ctx->nb_inputs; i++) { \
  438. if (ctx->inputs[i] && !ctx->inputs[i]->out_fmts) { \
  439. ref(fmts, &ctx->inputs[i]->out_fmts); \
  440. count++; \
  441. } \
  442. } \
  443. for (i = 0; i < ctx->nb_outputs; i++) { \
  444. if (ctx->outputs[i] && !ctx->outputs[i]->in_fmts) { \
  445. ref(fmts, &ctx->outputs[i]->in_fmts); \
  446. count++; \
  447. } \
  448. } \
  449. \
  450. if (!count) { \
  451. av_freep(&fmts->list); \
  452. av_freep(&fmts->refs); \
  453. av_freep(&fmts); \
  454. } \
  455. }
  456. void ff_set_common_channel_layouts(AVFilterContext *ctx,
  457. AVFilterChannelLayouts *layouts)
  458. {
  459. SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
  460. ff_channel_layouts_ref, channel_layouts);
  461. }
  462. void ff_set_common_samplerates(AVFilterContext *ctx,
  463. AVFilterFormats *samplerates)
  464. {
  465. SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
  466. ff_formats_ref, formats);
  467. }
  468. /**
  469. * A helper for query_formats() which sets all links to the same list of
  470. * formats. If there are no links hooked to this filter, the list of formats is
  471. * freed.
  472. */
  473. void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  474. {
  475. SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
  476. ff_formats_ref, formats);
  477. }
  478. static int default_query_formats_common(AVFilterContext *ctx,
  479. AVFilterChannelLayouts *(layouts)(void))
  480. {
  481. enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
  482. ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
  483. AVMEDIA_TYPE_VIDEO;
  484. ff_set_common_formats(ctx, ff_all_formats(type));
  485. if (type == AVMEDIA_TYPE_AUDIO) {
  486. ff_set_common_channel_layouts(ctx, layouts());
  487. ff_set_common_samplerates(ctx, ff_all_samplerates());
  488. }
  489. return 0;
  490. }
  491. int ff_default_query_formats(AVFilterContext *ctx)
  492. {
  493. return default_query_formats_common(ctx, ff_all_channel_layouts);
  494. }
  495. int ff_query_formats_all(AVFilterContext *ctx)
  496. {
  497. return default_query_formats_common(ctx, ff_all_channel_counts);
  498. }
  499. /* internal functions for parsing audio format arguments */
  500. int ff_parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx)
  501. {
  502. char *tail;
  503. int pix_fmt = av_get_pix_fmt(arg);
  504. if (pix_fmt == AV_PIX_FMT_NONE) {
  505. pix_fmt = strtol(arg, &tail, 0);
  506. if (*tail || (unsigned)pix_fmt >= AV_PIX_FMT_NB) {
  507. av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
  508. return AVERROR(EINVAL);
  509. }
  510. }
  511. *ret = pix_fmt;
  512. return 0;
  513. }
  514. int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
  515. {
  516. char *tail;
  517. int sfmt = av_get_sample_fmt(arg);
  518. if (sfmt == AV_SAMPLE_FMT_NONE) {
  519. sfmt = strtol(arg, &tail, 0);
  520. if (*tail || (unsigned)sfmt >= AV_SAMPLE_FMT_NB) {
  521. av_log(log_ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
  522. return AVERROR(EINVAL);
  523. }
  524. }
  525. *ret = sfmt;
  526. return 0;
  527. }
  528. int ff_parse_time_base(AVRational *ret, const char *arg, void *log_ctx)
  529. {
  530. AVRational r;
  531. if(av_parse_ratio(&r, arg, INT_MAX, 0, log_ctx) < 0 ||r.num<=0 ||r.den<=0) {
  532. av_log(log_ctx, AV_LOG_ERROR, "Invalid time base '%s'\n", arg);
  533. return AVERROR(EINVAL);
  534. }
  535. *ret = r;
  536. return 0;
  537. }
  538. int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
  539. {
  540. char *tail;
  541. double srate = av_strtod(arg, &tail);
  542. if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
  543. av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
  544. return AVERROR(EINVAL);
  545. }
  546. *ret = srate;
  547. return 0;
  548. }
  549. int ff_parse_channel_layout(int64_t *ret, const char *arg, void *log_ctx)
  550. {
  551. char *tail;
  552. int64_t chlayout = av_get_channel_layout(arg);
  553. if (chlayout == 0) {
  554. chlayout = strtol(arg, &tail, 10);
  555. if (*tail || chlayout == 0) {
  556. av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
  557. return AVERROR(EINVAL);
  558. }
  559. }
  560. *ret = chlayout;
  561. return 0;
  562. }
  563. #ifdef TEST
  564. #undef printf
  565. int main(void)
  566. {
  567. const int64_t *cl;
  568. char buf[512];
  569. for (cl = avfilter_all_channel_layouts; *cl != -1; cl++) {
  570. av_get_channel_layout_string(buf, sizeof(buf), -1, *cl);
  571. printf("%s\n", buf);
  572. }
  573. return 0;
  574. }
  575. #endif