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.

591 lines
21KB

  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/eval.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "libavutil/parseutils.h"
  24. #include "libavutil/audioconvert.h"
  25. #include "avfilter.h"
  26. #include "internal.h"
  27. #include "formats.h"
  28. /**
  29. * Add all refs from a to ret and destroy a.
  30. */
  31. #define MERGE_REF(ret, a, fmts, type, fail) \
  32. do { \
  33. type ***tmp; \
  34. int i; \
  35. \
  36. if (!(tmp = av_realloc(ret->refs, \
  37. sizeof(*tmp) * (ret->refcount + a->refcount)))) \
  38. goto fail; \
  39. ret->refs = tmp; \
  40. \
  41. for (i = 0; i < a->refcount; i ++) { \
  42. ret->refs[ret->refcount] = a->refs[i]; \
  43. *ret->refs[ret->refcount++] = ret; \
  44. } \
  45. \
  46. av_freep(&a->refs); \
  47. av_freep(&a->fmts); \
  48. av_freep(&a); \
  49. } while (0)
  50. /**
  51. * Add all formats common for a and b to ret, copy the refs and destroy
  52. * a and b.
  53. */
  54. #define MERGE_FORMATS(ret, a, b, fmts, nb, type, fail) \
  55. do { \
  56. int i, j, k = 0, count = FFMIN(a->nb, b->nb); \
  57. \
  58. if (!(ret = av_mallocz(sizeof(*ret)))) \
  59. goto fail; \
  60. \
  61. if (count) { \
  62. if (!(ret->fmts = av_malloc(sizeof(*ret->fmts) * count))) \
  63. goto fail; \
  64. for (i = 0; i < a->nb; i++) \
  65. for (j = 0; j < b->nb; j++) \
  66. if (a->fmts[i] == b->fmts[j]) { \
  67. if(k >= FFMIN(a->nb, b->nb)){ \
  68. av_log(0, AV_LOG_ERROR, "Duplicate formats in avfilter_merge_formats() detected\n"); \
  69. av_free(ret->fmts); \
  70. av_free(ret); \
  71. return NULL; \
  72. } \
  73. ret->fmts[k++] = a->fmts[i]; \
  74. } \
  75. } \
  76. ret->nb = k; \
  77. /* check that there was at least one common format */ \
  78. if (!ret->nb) \
  79. goto fail; \
  80. \
  81. MERGE_REF(ret, a, fmts, type, fail); \
  82. MERGE_REF(ret, b, fmts, type, fail); \
  83. } while (0)
  84. AVFilterFormats *ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
  85. {
  86. AVFilterFormats *ret = NULL;
  87. if (a == b)
  88. return a;
  89. MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
  90. return ret;
  91. fail:
  92. if (ret) {
  93. av_freep(&ret->refs);
  94. av_freep(&ret->formats);
  95. }
  96. av_freep(&ret);
  97. return NULL;
  98. }
  99. AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a,
  100. AVFilterFormats *b)
  101. {
  102. AVFilterFormats *ret = NULL;
  103. if (a == b) return a;
  104. if (a->format_count && b->format_count) {
  105. MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
  106. } else if (a->format_count) {
  107. MERGE_REF(a, b, formats, AVFilterFormats, fail);
  108. ret = a;
  109. } else {
  110. MERGE_REF(b, a, formats, AVFilterFormats, fail);
  111. ret = b;
  112. }
  113. return ret;
  114. fail:
  115. if (ret) {
  116. av_freep(&ret->refs);
  117. av_freep(&ret->formats);
  118. }
  119. av_freep(&ret);
  120. return NULL;
  121. }
  122. AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a,
  123. AVFilterChannelLayouts *b)
  124. {
  125. AVFilterChannelLayouts *ret = NULL;
  126. if (a == b) return a;
  127. if (a->nb_channel_layouts && b->nb_channel_layouts) {
  128. MERGE_FORMATS(ret, a, b, channel_layouts, nb_channel_layouts,
  129. AVFilterChannelLayouts, fail);
  130. } else if (a->nb_channel_layouts) {
  131. MERGE_REF(a, b, channel_layouts, AVFilterChannelLayouts, fail);
  132. ret = a;
  133. } else {
  134. MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, fail);
  135. ret = b;
  136. }
  137. return ret;
  138. fail:
  139. if (ret) {
  140. av_freep(&ret->refs);
  141. av_freep(&ret->channel_layouts);
  142. }
  143. av_freep(&ret);
  144. return NULL;
  145. }
  146. int ff_fmt_is_in(int fmt, const int *fmts)
  147. {
  148. const int *p;
  149. for (p = fmts; *p != -1; p++) {
  150. if (fmt == *p)
  151. return 1;
  152. }
  153. return 0;
  154. }
  155. #define COPY_INT_LIST(list_copy, list, type) { \
  156. int count = 0; \
  157. if (list) \
  158. for (count = 0; list[count] != -1; count++) \
  159. ; \
  160. list_copy = av_calloc(count+1, sizeof(type)); \
  161. if (list_copy) { \
  162. memcpy(list_copy, list, sizeof(type) * count); \
  163. list_copy[count] = -1; \
  164. } \
  165. }
  166. int *ff_copy_int_list(const int * const list)
  167. {
  168. int *ret = NULL;
  169. COPY_INT_LIST(ret, list, int);
  170. return ret;
  171. }
  172. int64_t *ff_copy_int64_list(const int64_t * const list)
  173. {
  174. int64_t *ret = NULL;
  175. COPY_INT_LIST(ret, list, int64_t);
  176. return ret;
  177. }
  178. #define MAKE_FORMAT_LIST(type, field, count_field) \
  179. type *formats; \
  180. int count = 0; \
  181. if (fmts) \
  182. for (count = 0; fmts[count] != -1; count++) \
  183. ; \
  184. formats = av_mallocz(sizeof(*formats)); \
  185. if (!formats) return NULL; \
  186. formats->count_field = count; \
  187. if (count) { \
  188. formats->field = av_malloc(sizeof(*formats->field)*count); \
  189. if (!formats->field) { \
  190. av_free(formats); \
  191. return NULL; \
  192. } \
  193. }
  194. AVFilterFormats *ff_make_format_list(const int *fmts)
  195. {
  196. MAKE_FORMAT_LIST(AVFilterFormats, formats, format_count);
  197. while (count--)
  198. formats->formats[count] = fmts[count];
  199. return formats;
  200. }
  201. AVFilterChannelLayouts *avfilter_make_format64_list(const int64_t *fmts)
  202. {
  203. MAKE_FORMAT_LIST(AVFilterChannelLayouts,
  204. channel_layouts, nb_channel_layouts);
  205. if (count)
  206. memcpy(formats->channel_layouts, fmts,
  207. sizeof(*formats->channel_layouts) * count);
  208. return formats;
  209. }
  210. #define ADD_FORMAT(f, fmt, type, list, nb) \
  211. do { \
  212. type *fmts; \
  213. \
  214. if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) \
  215. return AVERROR(ENOMEM); \
  216. \
  217. fmts = av_realloc((*f)->list, \
  218. sizeof(*(*f)->list) * ((*f)->nb + 1));\
  219. if (!fmts) \
  220. return AVERROR(ENOMEM); \
  221. \
  222. (*f)->list = fmts; \
  223. (*f)->list[(*f)->nb++] = fmt; \
  224. return 0; \
  225. } while (0)
  226. int ff_add_format(AVFilterFormats **avff, int64_t fmt)
  227. {
  228. ADD_FORMAT(avff, fmt, int, formats, format_count);
  229. }
  230. int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
  231. {
  232. ADD_FORMAT(l, channel_layout, uint64_t, channel_layouts, nb_channel_layouts);
  233. }
  234. AVFilterFormats *ff_all_formats(enum AVMediaType type)
  235. {
  236. return avfilter_make_all_formats(type);
  237. }
  238. AVFilterFormats *avfilter_make_all_formats(enum AVMediaType type)
  239. {
  240. AVFilterFormats *ret = NULL;
  241. int fmt;
  242. int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
  243. type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
  244. for (fmt = 0; fmt < num_formats; fmt++)
  245. if ((type != AVMEDIA_TYPE_VIDEO) ||
  246. (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
  247. ff_add_format(&ret, fmt);
  248. return ret;
  249. }
  250. const int64_t avfilter_all_channel_layouts[] = {
  251. #include "all_channel_layouts.inc"
  252. -1
  253. };
  254. // AVFilterFormats *avfilter_make_all_channel_layouts(void)
  255. // {
  256. // return avfilter_make_format64_list(avfilter_all_channel_layouts);
  257. // }
  258. #if FF_API_PACKING
  259. AVFilterFormats *avfilter_make_all_packing_formats(void)
  260. {
  261. static const int packing[] = {
  262. AVFILTER_PACKED,
  263. AVFILTER_PLANAR,
  264. -1,
  265. };
  266. return avfilter_make_format_list(packing);
  267. }
  268. #endif
  269. AVFilterFormats *ff_all_samplerates(void)
  270. {
  271. AVFilterFormats *ret = av_mallocz(sizeof(*ret));
  272. return ret;
  273. }
  274. AVFilterChannelLayouts *ff_all_channel_layouts(void)
  275. {
  276. AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
  277. return ret;
  278. }
  279. #define FORMATS_REF(f, ref) \
  280. do { \
  281. *ref = f; \
  282. f->refs = av_realloc(f->refs, sizeof(*f->refs) * ++f->refcount); \
  283. f->refs[f->refcount-1] = ref; \
  284. } while (0)
  285. void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
  286. {
  287. FORMATS_REF(f, ref);
  288. }
  289. void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
  290. {
  291. FORMATS_REF(f, ref);
  292. }
  293. #define FIND_REF_INDEX(ref, idx) \
  294. do { \
  295. int i; \
  296. for (i = 0; i < (*ref)->refcount; i ++) \
  297. if((*ref)->refs[i] == ref) { \
  298. idx = i; \
  299. break; \
  300. } \
  301. } while (0)
  302. #define FORMATS_UNREF(ref, list) \
  303. do { \
  304. int idx = -1; \
  305. \
  306. if (!*ref) \
  307. return; \
  308. \
  309. FIND_REF_INDEX(ref, idx); \
  310. \
  311. if (idx >= 0) \
  312. memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
  313. sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
  314. \
  315. if(!--(*ref)->refcount) { \
  316. av_free((*ref)->list); \
  317. av_free((*ref)->refs); \
  318. av_free(*ref); \
  319. } \
  320. *ref = NULL; \
  321. } while (0)
  322. void ff_formats_unref(AVFilterFormats **ref)
  323. {
  324. FORMATS_UNREF(ref, formats);
  325. }
  326. void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
  327. {
  328. FORMATS_UNREF(ref, channel_layouts);
  329. }
  330. #define FORMATS_CHANGEREF(oldref, newref) \
  331. do { \
  332. int idx = -1; \
  333. \
  334. FIND_REF_INDEX(oldref, idx); \
  335. \
  336. if (idx >= 0) { \
  337. (*oldref)->refs[idx] = newref; \
  338. *newref = *oldref; \
  339. *oldref = NULL; \
  340. } \
  341. } while (0)
  342. void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
  343. AVFilterChannelLayouts **newref)
  344. {
  345. FORMATS_CHANGEREF(oldref, newref);
  346. }
  347. void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
  348. {
  349. FORMATS_CHANGEREF(oldref, newref);
  350. }
  351. #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
  352. { \
  353. int count = 0, i; \
  354. \
  355. for (i = 0; i < ctx->input_count; i++) { \
  356. if (ctx->inputs[i] && !ctx->inputs[i]->out_fmts) { \
  357. ref(fmts, &ctx->inputs[i]->out_fmts); \
  358. count++; \
  359. } \
  360. } \
  361. for (i = 0; i < ctx->output_count; i++) { \
  362. if (ctx->outputs[i] && !ctx->outputs[i]->in_fmts) { \
  363. ref(fmts, &ctx->outputs[i]->in_fmts); \
  364. count++; \
  365. } \
  366. } \
  367. \
  368. if (!count) { \
  369. av_freep(&fmts->list); \
  370. av_freep(&fmts->refs); \
  371. av_freep(&fmts); \
  372. } \
  373. }
  374. void ff_set_common_channel_layouts(AVFilterContext *ctx,
  375. AVFilterChannelLayouts *layouts)
  376. {
  377. SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
  378. ff_channel_layouts_ref, channel_layouts);
  379. }
  380. void ff_set_common_samplerates(AVFilterContext *ctx,
  381. AVFilterFormats *samplerates)
  382. {
  383. SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
  384. ff_formats_ref, formats);
  385. }
  386. /**
  387. * A helper for query_formats() which sets all links to the same list of
  388. * formats. If there are no links hooked to this filter, the list of formats is
  389. * freed.
  390. */
  391. void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  392. {
  393. SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
  394. ff_formats_ref, formats);
  395. }
  396. int ff_default_query_formats(AVFilterContext *ctx)
  397. {
  398. enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
  399. ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
  400. AVMEDIA_TYPE_VIDEO;
  401. ff_set_common_formats(ctx, ff_all_formats(type));
  402. if (type == AVMEDIA_TYPE_AUDIO) {
  403. ff_set_common_channel_layouts(ctx, ff_all_channel_layouts());
  404. ff_set_common_samplerates(ctx, ff_all_samplerates());
  405. }
  406. return 0;
  407. }
  408. /* internal functions for parsing audio format arguments */
  409. int ff_parse_pixel_format(enum PixelFormat *ret, const char *arg, void *log_ctx)
  410. {
  411. char *tail;
  412. int pix_fmt = av_get_pix_fmt(arg);
  413. if (pix_fmt == PIX_FMT_NONE) {
  414. pix_fmt = strtol(arg, &tail, 0);
  415. if (*tail || (unsigned)pix_fmt >= PIX_FMT_NB) {
  416. av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
  417. return AVERROR(EINVAL);
  418. }
  419. }
  420. *ret = pix_fmt;
  421. return 0;
  422. }
  423. int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
  424. {
  425. char *tail;
  426. int sfmt = av_get_sample_fmt(arg);
  427. if (sfmt == AV_SAMPLE_FMT_NONE) {
  428. sfmt = strtol(arg, &tail, 0);
  429. if (*tail || (unsigned)sfmt >= AV_SAMPLE_FMT_NB) {
  430. av_log(log_ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
  431. return AVERROR(EINVAL);
  432. }
  433. }
  434. *ret = sfmt;
  435. return 0;
  436. }
  437. int ff_parse_time_base(AVRational *ret, const char *arg, void *log_ctx)
  438. {
  439. AVRational r;
  440. if(av_parse_ratio(&r, arg, INT_MAX, 0, log_ctx) < 0 ||r.num<=0 ||r.den<=0) {
  441. av_log(log_ctx, AV_LOG_ERROR, "Invalid time base '%s'\n", arg);
  442. return AVERROR(EINVAL);
  443. }
  444. *ret = r;
  445. return 0;
  446. }
  447. int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
  448. {
  449. char *tail;
  450. double srate = av_strtod(arg, &tail);
  451. if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
  452. av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
  453. return AVERROR(EINVAL);
  454. }
  455. *ret = srate;
  456. return 0;
  457. }
  458. int ff_parse_channel_layout(int64_t *ret, const char *arg, void *log_ctx)
  459. {
  460. char *tail;
  461. int64_t chlayout = av_get_channel_layout(arg);
  462. if (chlayout == 0) {
  463. chlayout = strtol(arg, &tail, 10);
  464. if (*tail || chlayout == 0) {
  465. av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
  466. return AVERROR(EINVAL);
  467. }
  468. }
  469. *ret = chlayout;
  470. return 0;
  471. }
  472. #if FF_API_FILTERS_PUBLIC
  473. int avfilter_default_query_formats(AVFilterContext *ctx)
  474. {
  475. return ff_default_query_formats(ctx);
  476. }
  477. void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  478. {
  479. ff_set_common_formats(ctx, formats);
  480. }
  481. AVFilterFormats *avfilter_make_format_list(const int *fmts)
  482. {
  483. return ff_make_format_list(fmts);
  484. }
  485. int avfilter_add_format(AVFilterFormats **avff, int64_t fmt)
  486. {
  487. return ff_add_format(avff, fmt);
  488. }
  489. AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
  490. {
  491. return ff_all_formats(type);
  492. }
  493. AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
  494. {
  495. return ff_merge_formats(a, b);
  496. }
  497. void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
  498. {
  499. ff_formats_ref(f, ref);
  500. }
  501. void avfilter_formats_unref(AVFilterFormats **ref)
  502. {
  503. ff_formats_unref(ref);
  504. }
  505. void avfilter_formats_changeref(AVFilterFormats **oldref,
  506. AVFilterFormats **newref)
  507. {
  508. ff_formats_changeref(oldref, newref);
  509. }
  510. #endif
  511. #ifdef TEST
  512. #undef printf
  513. int main(void)
  514. {
  515. const int64_t *cl;
  516. char buf[512];
  517. for (cl = avfilter_all_channel_layouts; *cl != -1; cl++) {
  518. av_get_channel_layout_string(buf, sizeof(buf), -1, *cl);
  519. printf("%s\n", buf);
  520. }
  521. return 0;
  522. }
  523. #endif