formats.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 *avfilter_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 *avfilter_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 avfilter_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. #if FF_API_OLD_ALL_FORMATS_API
  235. AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
  236. {
  237. return avfilter_make_all_formats(type);
  238. }
  239. #endif
  240. AVFilterFormats *avfilter_make_all_formats(enum AVMediaType type)
  241. {
  242. AVFilterFormats *ret = NULL;
  243. int fmt;
  244. int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
  245. type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
  246. for (fmt = 0; fmt < num_formats; fmt++)
  247. if ((type != AVMEDIA_TYPE_VIDEO) ||
  248. (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
  249. avfilter_add_format(&ret, fmt);
  250. return ret;
  251. }
  252. const int64_t avfilter_all_channel_layouts[] = {
  253. #include "all_channel_layouts.inc"
  254. -1
  255. };
  256. // AVFilterFormats *avfilter_make_all_channel_layouts(void)
  257. // {
  258. // return avfilter_make_format64_list(avfilter_all_channel_layouts);
  259. // }
  260. #if FF_API_PACKING
  261. AVFilterFormats *avfilter_make_all_packing_formats(void)
  262. {
  263. static const int packing[] = {
  264. AVFILTER_PACKED,
  265. AVFILTER_PLANAR,
  266. -1,
  267. };
  268. return avfilter_make_format_list(packing);
  269. }
  270. #endif
  271. AVFilterFormats *ff_all_samplerates(void)
  272. {
  273. AVFilterFormats *ret = av_mallocz(sizeof(*ret));
  274. return ret;
  275. }
  276. AVFilterChannelLayouts *ff_all_channel_layouts(void)
  277. {
  278. AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
  279. return ret;
  280. }
  281. #define FORMATS_REF(f, ref) \
  282. do { \
  283. *ref = f; \
  284. f->refs = av_realloc(f->refs, sizeof(*f->refs) * ++f->refcount); \
  285. f->refs[f->refcount-1] = ref; \
  286. } while (0)
  287. void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
  288. {
  289. FORMATS_REF(f, ref);
  290. }
  291. void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
  292. {
  293. FORMATS_REF(f, ref);
  294. }
  295. #define FIND_REF_INDEX(ref, idx) \
  296. do { \
  297. int i; \
  298. for (i = 0; i < (*ref)->refcount; i ++) \
  299. if((*ref)->refs[i] == ref) { \
  300. idx = i; \
  301. break; \
  302. } \
  303. } while (0)
  304. #define FORMATS_UNREF(ref, list) \
  305. do { \
  306. int idx = -1; \
  307. \
  308. if (!*ref) \
  309. return; \
  310. \
  311. FIND_REF_INDEX(ref, idx); \
  312. \
  313. if (idx >= 0) \
  314. memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
  315. sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
  316. \
  317. if(!--(*ref)->refcount) { \
  318. av_free((*ref)->list); \
  319. av_free((*ref)->refs); \
  320. av_free(*ref); \
  321. } \
  322. *ref = NULL; \
  323. } while (0)
  324. void avfilter_formats_unref(AVFilterFormats **ref)
  325. {
  326. FORMATS_UNREF(ref, formats);
  327. }
  328. void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
  329. {
  330. FORMATS_UNREF(ref, channel_layouts);
  331. }
  332. #define FORMATS_CHANGEREF(oldref, newref) \
  333. do { \
  334. int idx = -1; \
  335. \
  336. FIND_REF_INDEX(oldref, idx); \
  337. \
  338. if (idx >= 0) { \
  339. (*oldref)->refs[idx] = newref; \
  340. *newref = *oldref; \
  341. *oldref = NULL; \
  342. } \
  343. } while (0)
  344. void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
  345. AVFilterChannelLayouts **newref)
  346. {
  347. FORMATS_CHANGEREF(oldref, newref);
  348. }
  349. void avfilter_formats_changeref(AVFilterFormats **oldref,
  350. AVFilterFormats **newref)
  351. {
  352. FORMATS_CHANGEREF(oldref, newref);
  353. }
  354. #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
  355. { \
  356. int count = 0, i; \
  357. \
  358. for (i = 0; i < ctx->input_count; i++) { \
  359. if (ctx->inputs[i]) { \
  360. ref(fmts, &ctx->inputs[i]->out_fmts); \
  361. count++; \
  362. } \
  363. } \
  364. for (i = 0; i < ctx->output_count; i++) { \
  365. if (ctx->outputs[i]) { \
  366. ref(fmts, &ctx->outputs[i]->in_fmts); \
  367. count++; \
  368. } \
  369. } \
  370. \
  371. if (!count) { \
  372. av_freep(&fmts->list); \
  373. av_freep(&fmts->refs); \
  374. av_freep(&fmts); \
  375. } \
  376. }
  377. void ff_set_common_channel_layouts(AVFilterContext *ctx,
  378. AVFilterChannelLayouts *layouts)
  379. {
  380. SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
  381. ff_channel_layouts_ref, channel_layouts);
  382. }
  383. void ff_set_common_samplerates(AVFilterContext *ctx,
  384. AVFilterFormats *samplerates)
  385. {
  386. SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
  387. avfilter_formats_ref, formats);
  388. }
  389. /**
  390. * A helper for query_formats() which sets all links to the same list of
  391. * formats. If there are no links hooked to this filter, the list of formats is
  392. * freed.
  393. */
  394. void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  395. {
  396. SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
  397. avfilter_formats_ref, formats);
  398. }
  399. int ff_default_query_formats(AVFilterContext *ctx)
  400. {
  401. enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
  402. ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
  403. AVMEDIA_TYPE_VIDEO;
  404. avfilter_set_common_formats(ctx, avfilter_all_formats(type));
  405. if (type == AVMEDIA_TYPE_AUDIO) {
  406. ff_set_common_channel_layouts(ctx, ff_all_channel_layouts());
  407. ff_set_common_samplerates(ctx, ff_all_samplerates());
  408. }
  409. return 0;
  410. }
  411. /* internal functions for parsing audio format arguments */
  412. int ff_parse_pixel_format(enum PixelFormat *ret, const char *arg, void *log_ctx)
  413. {
  414. char *tail;
  415. int pix_fmt = av_get_pix_fmt(arg);
  416. if (pix_fmt == PIX_FMT_NONE) {
  417. pix_fmt = strtol(arg, &tail, 0);
  418. if (*tail || (unsigned)pix_fmt >= PIX_FMT_NB) {
  419. av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
  420. return AVERROR(EINVAL);
  421. }
  422. }
  423. *ret = pix_fmt;
  424. return 0;
  425. }
  426. int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
  427. {
  428. char *tail;
  429. int sfmt = av_get_sample_fmt(arg);
  430. if (sfmt == AV_SAMPLE_FMT_NONE) {
  431. sfmt = strtol(arg, &tail, 0);
  432. if (*tail || (unsigned)sfmt >= AV_SAMPLE_FMT_NB) {
  433. av_log(log_ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
  434. return AVERROR(EINVAL);
  435. }
  436. }
  437. *ret = sfmt;
  438. return 0;
  439. }
  440. int ff_parse_time_base(AVRational *ret, const char *arg, void *log_ctx)
  441. {
  442. AVRational r;
  443. if(av_parse_ratio(&r, arg, INT_MAX, 0, log_ctx) < 0 ||r.num<=0 ||r.den<=0) {
  444. av_log(log_ctx, AV_LOG_ERROR, "Invalid time base '%s'\n", arg);
  445. return AVERROR(EINVAL);
  446. }
  447. *ret = r;
  448. return 0;
  449. }
  450. int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
  451. {
  452. char *tail;
  453. double srate = av_strtod(arg, &tail);
  454. if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
  455. av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
  456. return AVERROR(EINVAL);
  457. }
  458. *ret = srate;
  459. return 0;
  460. }
  461. int ff_parse_channel_layout(int64_t *ret, const char *arg, void *log_ctx)
  462. {
  463. char *tail;
  464. int64_t chlayout = av_get_channel_layout(arg);
  465. if (chlayout == 0) {
  466. chlayout = strtol(arg, &tail, 10);
  467. if (*tail || chlayout == 0) {
  468. av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
  469. return AVERROR(EINVAL);
  470. }
  471. }
  472. *ret = chlayout;
  473. return 0;
  474. }
  475. #if FF_API_FILTERS_PUBLIC
  476. int avfilter_default_query_formats(AVFilterContext *ctx)
  477. {
  478. return ff_default_query_formats(ctx);
  479. }
  480. #endif
  481. #ifdef TEST
  482. #undef printf
  483. int main(void)
  484. {
  485. const int64_t *cl;
  486. char buf[512];
  487. for (cl = avfilter_all_channel_layouts; *cl != -1; cl++) {
  488. av_get_channel_layout_string(buf, sizeof(buf), -1, *cl);
  489. printf("%s\n", buf);
  490. }
  491. return 0;
  492. }
  493. #endif