formats.c 20 KB

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