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.

554 lines
20KB

  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() \
  179. AVFilterFormats *formats; \
  180. int count = 0; \
  181. if (fmts) \
  182. for (count = 0; fmts[count] != -1; count++) \
  183. ; \
  184. formats = av_mallocz(sizeof(AVFilterFormats)); \
  185. if (!formats) return NULL; \
  186. formats->format_count = count; \
  187. if (count) { \
  188. formats->formats = av_malloc(sizeof(*formats->formats)*count); \
  189. if (!formats->formats) { \
  190. av_free(formats); \
  191. return NULL; \
  192. } \
  193. }
  194. AVFilterFormats *avfilter_make_format_list(const int *fmts)
  195. {
  196. MAKE_FORMAT_LIST();
  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();
  204. if (count)
  205. memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
  206. return (AVFilterChannelLayouts*)formats;
  207. }
  208. #define ADD_FORMAT(f, fmt, type, list, nb) \
  209. do { \
  210. type *fmts; \
  211. \
  212. if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) \
  213. return AVERROR(ENOMEM); \
  214. \
  215. fmts = av_realloc((*f)->list, \
  216. sizeof(*(*f)->list) * ((*f)->nb + 1));\
  217. if (!fmts) \
  218. return AVERROR(ENOMEM); \
  219. \
  220. (*f)->list = fmts; \
  221. (*f)->list[(*f)->nb++] = fmt; \
  222. return 0; \
  223. } while (0)
  224. int avfilter_add_format(AVFilterFormats **avff, int64_t fmt)
  225. {
  226. ADD_FORMAT(avff, fmt, int64_t, formats, format_count);
  227. }
  228. int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
  229. {
  230. ADD_FORMAT(l, channel_layout, uint64_t, channel_layouts, nb_channel_layouts);
  231. }
  232. #if FF_API_OLD_ALL_FORMATS_API
  233. AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
  234. {
  235. return avfilter_make_all_formats(type);
  236. }
  237. #endif
  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. avfilter_add_format(&ret, fmt);
  248. return ret;
  249. }
  250. const int64_t avfilter_all_channel_layouts[] = {
  251. #include "all_channel_layouts.h"
  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 avfilter_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 avfilter_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 avfilter_formats_changeref(AVFilterFormats **oldref,
  348. AVFilterFormats **newref)
  349. {
  350. FORMATS_CHANGEREF(oldref, newref);
  351. }
  352. #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
  353. { \
  354. int count = 0, i; \
  355. \
  356. for (i = 0; i < ctx->input_count; i++) { \
  357. if (ctx->inputs[i]) { \
  358. ref(fmts, &ctx->inputs[i]->out_fmts); \
  359. count++; \
  360. } \
  361. } \
  362. for (i = 0; i < ctx->output_count; i++) { \
  363. if (ctx->outputs[i]) { \
  364. ref(fmts, &ctx->outputs[i]->in_fmts); \
  365. count++; \
  366. } \
  367. } \
  368. \
  369. if (!count) { \
  370. av_freep(&fmts->list); \
  371. av_freep(&fmts->refs); \
  372. av_freep(&fmts); \
  373. } \
  374. }
  375. void ff_set_common_channel_layouts(AVFilterContext *ctx,
  376. AVFilterChannelLayouts *layouts)
  377. {
  378. SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
  379. ff_channel_layouts_ref, channel_layouts);
  380. }
  381. void ff_set_common_samplerates(AVFilterContext *ctx,
  382. AVFilterFormats *samplerates)
  383. {
  384. SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
  385. avfilter_formats_ref, formats);
  386. }
  387. /**
  388. * A helper for query_formats() which sets all links to the same list of
  389. * formats. If there are no links hooked to this filter, the list of formats is
  390. * freed.
  391. */
  392. void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  393. {
  394. SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
  395. avfilter_formats_ref, formats);
  396. }
  397. int avfilter_default_query_formats(AVFilterContext *ctx)
  398. {
  399. enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
  400. ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
  401. AVMEDIA_TYPE_VIDEO;
  402. avfilter_set_common_formats(ctx, avfilter_all_formats(type));
  403. if (type == AVMEDIA_TYPE_AUDIO) {
  404. ff_set_common_channel_layouts(ctx, ff_all_channel_layouts());
  405. ff_set_common_samplerates(ctx, ff_all_samplerates());
  406. }
  407. return 0;
  408. }
  409. /* internal functions for parsing audio format arguments */
  410. int ff_parse_pixel_format(enum PixelFormat *ret, const char *arg, void *log_ctx)
  411. {
  412. char *tail;
  413. int pix_fmt = av_get_pix_fmt(arg);
  414. if (pix_fmt == PIX_FMT_NONE) {
  415. pix_fmt = strtol(arg, &tail, 0);
  416. if (*tail || (unsigned)pix_fmt >= PIX_FMT_NB) {
  417. av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
  418. return AVERROR(EINVAL);
  419. }
  420. }
  421. *ret = pix_fmt;
  422. return 0;
  423. }
  424. int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
  425. {
  426. char *tail;
  427. int sfmt = av_get_sample_fmt(arg);
  428. if (sfmt == AV_SAMPLE_FMT_NONE) {
  429. sfmt = strtol(arg, &tail, 0);
  430. if (*tail || (unsigned)sfmt >= AV_SAMPLE_FMT_NB) {
  431. av_log(log_ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
  432. return AVERROR(EINVAL);
  433. }
  434. }
  435. *ret = sfmt;
  436. return 0;
  437. }
  438. int ff_parse_time_base(AVRational *ret, const char *arg, void *log_ctx)
  439. {
  440. AVRational r;
  441. if(av_parse_ratio(&r, arg, INT_MAX, 0, log_ctx) < 0 ||r.num<=0 ||r.den<=0) {
  442. av_log(log_ctx, AV_LOG_ERROR, "Invalid time base '%s'\n", arg);
  443. return AVERROR(EINVAL);
  444. }
  445. *ret = r;
  446. return 0;
  447. }
  448. int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
  449. {
  450. char *tail;
  451. double srate = av_strtod(arg, &tail);
  452. if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
  453. av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
  454. return AVERROR(EINVAL);
  455. }
  456. *ret = srate;
  457. return 0;
  458. }
  459. int ff_parse_channel_layout(int64_t *ret, const char *arg, void *log_ctx)
  460. {
  461. char *tail;
  462. int64_t chlayout = av_get_channel_layout(arg);
  463. if (chlayout == 0) {
  464. chlayout = strtol(arg, &tail, 10);
  465. if (*tail || chlayout == 0) {
  466. av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
  467. return AVERROR(EINVAL);
  468. }
  469. }
  470. *ret = chlayout;
  471. return 0;
  472. }
  473. #ifdef TEST
  474. #undef printf
  475. int main(void)
  476. {
  477. const int64_t *cl;
  478. char buf[512];
  479. for (cl = avfilter_all_channel_layouts; *cl != -1; cl++) {
  480. av_get_channel_layout_string(buf, sizeof(buf), -1, *cl);
  481. printf("%s\n", buf);
  482. }
  483. return 0;
  484. }
  485. #endif