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.

614 lines
23KB

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