formats.c 20 KB

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