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.

557 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/audioconvert.h"
  24. #include "avfilter.h"
  25. #include "internal.h"
  26. #include "formats.h"
  27. /**
  28. * Add all refs from a to ret and destroy a.
  29. */
  30. #define MERGE_REF(ret, a, fmts, type, fail) \
  31. do { \
  32. type ***tmp; \
  33. int i; \
  34. \
  35. if (!(tmp = av_realloc(ret->refs, \
  36. sizeof(*tmp) * (ret->refcount + a->refcount)))) \
  37. goto fail; \
  38. ret->refs = tmp; \
  39. \
  40. for (i = 0; i < a->refcount; i ++) { \
  41. ret->refs[ret->refcount] = a->refs[i]; \
  42. *ret->refs[ret->refcount++] = ret; \
  43. } \
  44. \
  45. av_freep(&a->refs); \
  46. av_freep(&a->fmts); \
  47. av_freep(&a); \
  48. } while (0)
  49. /**
  50. * Add all formats common for a and b to ret, copy the refs and destroy
  51. * a and b.
  52. */
  53. #define MERGE_FORMATS(ret, a, b, fmts, nb, type, fail) \
  54. do { \
  55. int i, j, k = 0, count = FFMIN(a->nb, b->nb); \
  56. \
  57. if (!(ret = av_mallocz(sizeof(*ret)))) \
  58. goto fail; \
  59. \
  60. if (count) { \
  61. if (!(ret->fmts = av_malloc(sizeof(*ret->fmts) * count))) \
  62. goto fail; \
  63. for (i = 0; i < a->nb; i++) \
  64. for (j = 0; j < b->nb; j++) \
  65. if (a->fmts[i] == b->fmts[j]) { \
  66. if(k >= FFMIN(a->nb, b->nb)){ \
  67. av_log(0, AV_LOG_ERROR, "Duplicate formats in avfilter_merge_formats() detected\n"); \
  68. av_free(ret->fmts); \
  69. av_free(ret); \
  70. return NULL; \
  71. } \
  72. ret->fmts[k++] = a->fmts[i]; \
  73. } \
  74. } \
  75. ret->nb = k; \
  76. /* check that there was at least one common format */ \
  77. if (!ret->nb) \
  78. goto fail; \
  79. \
  80. MERGE_REF(ret, a, fmts, type, fail); \
  81. MERGE_REF(ret, b, fmts, type, fail); \
  82. } while (0)
  83. AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
  84. {
  85. AVFilterFormats *ret = NULL;
  86. if (a == b)
  87. return a;
  88. MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
  89. return ret;
  90. fail:
  91. if (ret) {
  92. av_freep(&ret->refs);
  93. av_freep(&ret->formats);
  94. }
  95. av_freep(&ret);
  96. return NULL;
  97. }
  98. AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a,
  99. AVFilterFormats *b)
  100. {
  101. AVFilterFormats *ret = NULL;
  102. if (a == b) return a;
  103. if (a->format_count && b->format_count) {
  104. MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
  105. } else if (a->format_count) {
  106. MERGE_REF(a, b, formats, AVFilterFormats, fail);
  107. ret = a;
  108. } else {
  109. MERGE_REF(b, a, formats, AVFilterFormats, fail);
  110. ret = b;
  111. }
  112. return ret;
  113. fail:
  114. if (ret) {
  115. av_freep(&ret->refs);
  116. av_freep(&ret->formats);
  117. }
  118. av_freep(&ret);
  119. return NULL;
  120. }
  121. AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a,
  122. AVFilterChannelLayouts *b)
  123. {
  124. AVFilterChannelLayouts *ret = NULL;
  125. if (a == b) return a;
  126. if (a->nb_channel_layouts && b->nb_channel_layouts) {
  127. MERGE_FORMATS(ret, a, b, channel_layouts, nb_channel_layouts,
  128. AVFilterChannelLayouts, fail);
  129. } else if (a->nb_channel_layouts) {
  130. MERGE_REF(a, b, channel_layouts, AVFilterChannelLayouts, fail);
  131. ret = a;
  132. } else {
  133. MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, fail);
  134. ret = b;
  135. }
  136. return ret;
  137. fail:
  138. if (ret) {
  139. av_freep(&ret->refs);
  140. av_freep(&ret->channel_layouts);
  141. }
  142. av_freep(&ret);
  143. return NULL;
  144. }
  145. int ff_fmt_is_in(int fmt, const int *fmts)
  146. {
  147. const int *p;
  148. for (p = fmts; *p != -1; p++) {
  149. if (fmt == *p)
  150. return 1;
  151. }
  152. return 0;
  153. }
  154. #define COPY_INT_LIST(list_copy, list, type) { \
  155. int count = 0; \
  156. if (list) \
  157. for (count = 0; list[count] != -1; count++) \
  158. ; \
  159. list_copy = av_calloc(count+1, sizeof(type)); \
  160. if (list_copy) { \
  161. memcpy(list_copy, list, sizeof(type) * count); \
  162. list_copy[count] = -1; \
  163. } \
  164. }
  165. int *ff_copy_int_list(const int * const list)
  166. {
  167. int *ret = NULL;
  168. COPY_INT_LIST(ret, list, int);
  169. return ret;
  170. }
  171. int64_t *ff_copy_int64_list(const int64_t * const list)
  172. {
  173. int64_t *ret = NULL;
  174. COPY_INT_LIST(ret, list, int64_t);
  175. return ret;
  176. }
  177. #define MAKE_FORMAT_LIST() \
  178. AVFilterFormats *formats; \
  179. int count = 0; \
  180. if (fmts) \
  181. for (count = 0; fmts[count] != -1; count++) \
  182. ; \
  183. formats = av_mallocz(sizeof(AVFilterFormats)); \
  184. if (!formats) return NULL; \
  185. formats->format_count = count; \
  186. if (count) { \
  187. formats->formats = av_malloc(sizeof(*formats->formats)*count); \
  188. if (!formats->formats) { \
  189. av_free(formats); \
  190. return NULL; \
  191. } \
  192. }
  193. AVFilterFormats *avfilter_make_format_list(const int *fmts)
  194. {
  195. MAKE_FORMAT_LIST();
  196. while (count--)
  197. formats->formats[count] = fmts[count];
  198. return formats;
  199. }
  200. AVFilterChannelLayouts *avfilter_make_format64_list(const int64_t *fmts)
  201. {
  202. MAKE_FORMAT_LIST();
  203. if (count)
  204. memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
  205. return (AVFilterChannelLayouts*)formats;
  206. }
  207. #define ADD_FORMAT(f, fmt, type, list, nb) \
  208. do { \
  209. type *fmts; \
  210. \
  211. if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) \
  212. return AVERROR(ENOMEM); \
  213. \
  214. fmts = av_realloc((*f)->list, \
  215. sizeof(*(*f)->list) * ((*f)->nb + 1));\
  216. if (!fmts) \
  217. return AVERROR(ENOMEM); \
  218. \
  219. (*f)->list = fmts; \
  220. (*f)->list[(*f)->nb++] = fmt; \
  221. return 0; \
  222. } while (0)
  223. int avfilter_add_format(AVFilterFormats **avff, int64_t fmt)
  224. {
  225. ADD_FORMAT(avff, fmt, int64_t, formats, format_count);
  226. }
  227. int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
  228. {
  229. ADD_FORMAT(l, channel_layout, uint64_t, channel_layouts, nb_channel_layouts);
  230. }
  231. #if FF_API_OLD_ALL_FORMATS_API
  232. AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
  233. {
  234. return avfilter_make_all_formats(type);
  235. }
  236. #endif
  237. AVFilterFormats *avfilter_make_all_formats(enum AVMediaType type)
  238. {
  239. AVFilterFormats *ret = NULL;
  240. int fmt;
  241. int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
  242. type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
  243. for (fmt = 0; fmt < num_formats; fmt++)
  244. if ((type != AVMEDIA_TYPE_VIDEO) ||
  245. (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
  246. avfilter_add_format(&ret, fmt);
  247. return ret;
  248. }
  249. const int64_t avfilter_all_channel_layouts[] = {
  250. #include "all_channel_layouts.h"
  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 *avfilter_make_all_packing_formats(void)
  258. {
  259. static const int packing[] = {
  260. AVFILTER_PACKED,
  261. AVFILTER_PLANAR,
  262. -1,
  263. };
  264. return avfilter_make_format_list(packing);
  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 avfilter_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 avfilter_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 avfilter_formats_changeref(AVFilterFormats **oldref,
  345. AVFilterFormats **newref)
  346. {
  347. FORMATS_CHANGEREF(oldref, newref);
  348. }
  349. #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
  350. { \
  351. int count = 0, i; \
  352. \
  353. for (i = 0; i < ctx->input_count; i++) { \
  354. if (ctx->inputs[i]) { \
  355. ref(fmts, &ctx->inputs[i]->out_fmts); \
  356. count++; \
  357. } \
  358. } \
  359. for (i = 0; i < ctx->output_count; i++) { \
  360. if (ctx->outputs[i]) { \
  361. ref(fmts, &ctx->outputs[i]->in_fmts); \
  362. count++; \
  363. } \
  364. } \
  365. \
  366. if (!count) { \
  367. av_freep(&fmts->list); \
  368. av_freep(&fmts->refs); \
  369. av_freep(&fmts); \
  370. } \
  371. }
  372. void ff_set_common_channel_layouts(AVFilterContext *ctx,
  373. AVFilterChannelLayouts *layouts)
  374. {
  375. SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
  376. ff_channel_layouts_ref, channel_layouts);
  377. }
  378. void ff_set_common_samplerates(AVFilterContext *ctx,
  379. AVFilterFormats *samplerates)
  380. {
  381. SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
  382. avfilter_formats_ref, formats);
  383. }
  384. /**
  385. * A helper for query_formats() which sets all links to the same list of
  386. * formats. If there are no links hooked to this filter, the list of formats is
  387. * freed.
  388. */
  389. void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  390. {
  391. SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
  392. avfilter_formats_ref, formats);
  393. }
  394. int avfilter_default_query_formats(AVFilterContext *ctx)
  395. {
  396. enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
  397. ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
  398. AVMEDIA_TYPE_VIDEO;
  399. avfilter_set_common_formats(ctx, avfilter_all_formats(type));
  400. if (type == AVMEDIA_TYPE_AUDIO) {
  401. ff_set_common_channel_layouts(ctx, ff_all_channel_layouts());
  402. ff_set_common_samplerates(ctx, ff_all_samplerates());
  403. }
  404. return 0;
  405. }
  406. /* internal functions for parsing audio format arguments */
  407. int ff_parse_pixel_format(enum PixelFormat *ret, const char *arg, void *log_ctx)
  408. {
  409. char *tail;
  410. int pix_fmt = av_get_pix_fmt(arg);
  411. if (pix_fmt == PIX_FMT_NONE) {
  412. pix_fmt = strtol(arg, &tail, 0);
  413. if (*tail || (unsigned)pix_fmt >= PIX_FMT_NB) {
  414. av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
  415. return AVERROR(EINVAL);
  416. }
  417. }
  418. *ret = pix_fmt;
  419. return 0;
  420. }
  421. int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
  422. {
  423. char *tail;
  424. int sfmt = av_get_sample_fmt(arg);
  425. if (sfmt == AV_SAMPLE_FMT_NONE) {
  426. sfmt = strtol(arg, &tail, 0);
  427. if (*tail || (unsigned)sfmt >= AV_SAMPLE_FMT_NB) {
  428. av_log(log_ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
  429. return AVERROR(EINVAL);
  430. }
  431. }
  432. *ret = sfmt;
  433. return 0;
  434. }
  435. int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
  436. {
  437. char *tail;
  438. double srate = av_strtod(arg, &tail);
  439. if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
  440. av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
  441. return AVERROR(EINVAL);
  442. }
  443. *ret = srate;
  444. return 0;
  445. }
  446. int ff_parse_channel_layout(int64_t *ret, const char *arg, void *log_ctx)
  447. {
  448. char *tail;
  449. int64_t chlayout = av_get_channel_layout(arg);
  450. if (chlayout == 0) {
  451. chlayout = strtol(arg, &tail, 10);
  452. if (*tail || chlayout == 0) {
  453. av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
  454. return AVERROR(EINVAL);
  455. }
  456. }
  457. *ret = chlayout;
  458. return 0;
  459. }
  460. int ff_parse_packing_format(int *ret, const char *arg, void *log_ctx)
  461. {
  462. char *tail;
  463. int planar = strtol(arg, &tail, 10);
  464. if (*tail) {
  465. planar = !strcmp(arg, "packed") ? 0:
  466. !strcmp(arg, "planar") ? 1: -1;
  467. }
  468. if (planar != 0 && planar != 1) {
  469. av_log(log_ctx, AV_LOG_ERROR, "Invalid packing format '%s'\n", arg);
  470. return AVERROR(EINVAL);
  471. }
  472. *ret = planar;
  473. return 0;
  474. }
  475. #ifdef TEST
  476. #undef printf
  477. int main(void)
  478. {
  479. const int64_t *cl;
  480. char buf[512];
  481. for (cl = avfilter_all_channel_layouts; *cl != -1; cl++) {
  482. av_get_channel_layout_string(buf, sizeof(buf), -1, *cl);
  483. printf("%s\n", buf);
  484. }
  485. return 0;
  486. }
  487. #endif