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.

582 lines
19KB

  1. /*
  2. * Copyright (c) 2008 Vitor Sessak
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * memory buffer source filter
  23. */
  24. #include "libavutil/channel_layout.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/fifo.h"
  27. #include "libavutil/frame.h"
  28. #include "libavutil/imgutils.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/samplefmt.h"
  31. #include "audio.h"
  32. #include "avfilter.h"
  33. #include "buffersrc.h"
  34. #include "formats.h"
  35. #include "internal.h"
  36. #include "video.h"
  37. #include "avcodec.h"
  38. typedef struct {
  39. const AVClass *class;
  40. AVFifoBuffer *fifo;
  41. AVRational time_base; ///< time_base to set in the output link
  42. AVRational frame_rate; ///< frame_rate to set in the output link
  43. unsigned nb_failed_requests;
  44. unsigned warning_limit;
  45. /* video only */
  46. int w, h;
  47. enum AVPixelFormat pix_fmt;
  48. AVRational pixel_aspect;
  49. char *sws_param;
  50. /* audio only */
  51. int sample_rate;
  52. enum AVSampleFormat sample_fmt;
  53. char *sample_fmt_str;
  54. int channels;
  55. uint64_t channel_layout;
  56. char *channel_layout_str;
  57. int eof;
  58. } BufferSourceContext;
  59. #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format)\
  60. if (c->w != width || c->h != height || c->pix_fmt != format) {\
  61. av_log(s, AV_LOG_INFO, "Changing frame properties on the fly is not supported by all filters.\n");\
  62. }
  63. #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, ch_count, format)\
  64. if (c->sample_fmt != format || c->sample_rate != srate ||\
  65. c->channel_layout != ch_layout || c->channels != ch_count) {\
  66. av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
  67. return AVERROR(EINVAL);\
  68. }
  69. int av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
  70. {
  71. return av_buffersrc_add_frame_flags(ctx, (AVFrame *)frame,
  72. AV_BUFFERSRC_FLAG_KEEP_REF);
  73. }
  74. int av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame)
  75. {
  76. return av_buffersrc_add_frame_flags(ctx, frame, 0);
  77. }
  78. static int av_buffersrc_add_frame_internal(AVFilterContext *ctx,
  79. AVFrame *frame, int flags);
  80. int av_buffersrc_add_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
  81. {
  82. AVFrame *copy = NULL;
  83. int ret = 0;
  84. if (frame && frame->channel_layout &&
  85. av_get_channel_layout_nb_channels(frame->channel_layout) != av_frame_get_channels(frame)) {
  86. av_log(0, AV_LOG_ERROR, "Layout indicates a different number of channels than actually present\n");
  87. return AVERROR(EINVAL);
  88. }
  89. if (!(flags & AV_BUFFERSRC_FLAG_KEEP_REF) || !frame)
  90. return av_buffersrc_add_frame_internal(ctx, frame, flags);
  91. if (!(copy = av_frame_alloc()))
  92. return AVERROR(ENOMEM);
  93. ret = av_frame_ref(copy, frame);
  94. if (ret >= 0)
  95. ret = av_buffersrc_add_frame_internal(ctx, copy, flags);
  96. av_frame_free(&copy);
  97. return ret;
  98. }
  99. static int attribute_align_arg av_buffersrc_add_frame_internal(AVFilterContext *ctx,
  100. AVFrame *frame, int flags)
  101. {
  102. BufferSourceContext *s = ctx->priv;
  103. AVFrame *copy;
  104. int ret;
  105. if (!frame) {
  106. s->eof = 1;
  107. return 0;
  108. } else if (s->eof)
  109. return AVERROR(EINVAL);
  110. if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
  111. switch (ctx->outputs[0]->type) {
  112. case AVMEDIA_TYPE_VIDEO:
  113. CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
  114. frame->format);
  115. break;
  116. case AVMEDIA_TYPE_AUDIO:
  117. /* For layouts unknown on input but known on link after negotiation. */
  118. if (!frame->channel_layout)
  119. frame->channel_layout = s->channel_layout;
  120. CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
  121. av_frame_get_channels(frame), frame->format);
  122. break;
  123. default:
  124. return AVERROR(EINVAL);
  125. }
  126. }
  127. if (!av_fifo_space(s->fifo) &&
  128. (ret = av_fifo_realloc2(s->fifo, av_fifo_size(s->fifo) +
  129. sizeof(copy))) < 0)
  130. return ret;
  131. if (!(copy = av_frame_alloc()))
  132. return AVERROR(ENOMEM);
  133. av_frame_move_ref(copy, frame);
  134. if ((ret = av_fifo_generic_write(s->fifo, &copy, sizeof(copy), NULL)) < 0) {
  135. av_frame_move_ref(frame, copy);
  136. av_frame_free(&copy);
  137. return ret;
  138. }
  139. if ((flags & AV_BUFFERSRC_FLAG_PUSH))
  140. if ((ret = ctx->output_pads[0].request_frame(ctx->outputs[0])) < 0)
  141. return ret;
  142. return 0;
  143. }
  144. #if FF_API_AVFILTERBUFFER
  145. static void compat_free_buffer(void *opaque, uint8_t *data)
  146. {
  147. AVFilterBufferRef *buf = opaque;
  148. AV_NOWARN_DEPRECATED(
  149. avfilter_unref_buffer(buf);
  150. )
  151. }
  152. static void compat_unref_buffer(void *opaque, uint8_t *data)
  153. {
  154. AVBufferRef *buf = opaque;
  155. AV_NOWARN_DEPRECATED(
  156. av_buffer_unref(&buf);
  157. )
  158. }
  159. int av_buffersrc_add_ref(AVFilterContext *ctx, AVFilterBufferRef *buf,
  160. int flags)
  161. {
  162. BufferSourceContext *s = ctx->priv;
  163. AVFrame *frame = NULL;
  164. AVBufferRef *dummy_buf = NULL;
  165. int ret = 0, planes, i;
  166. if (!buf) {
  167. s->eof = 1;
  168. return 0;
  169. } else if (s->eof)
  170. return AVERROR(EINVAL);
  171. frame = av_frame_alloc();
  172. if (!frame)
  173. return AVERROR(ENOMEM);
  174. dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, buf,
  175. (buf->perms & AV_PERM_WRITE) ? 0 : AV_BUFFER_FLAG_READONLY);
  176. if (!dummy_buf) {
  177. ret = AVERROR(ENOMEM);
  178. goto fail;
  179. }
  180. AV_NOWARN_DEPRECATED(
  181. if ((ret = avfilter_copy_buf_props(frame, buf)) < 0)
  182. goto fail;
  183. )
  184. #define WRAP_PLANE(ref_out, data, data_size) \
  185. do { \
  186. AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
  187. if (!dummy_ref) { \
  188. ret = AVERROR(ENOMEM); \
  189. goto fail; \
  190. } \
  191. ref_out = av_buffer_create(data, data_size, compat_unref_buffer, \
  192. dummy_ref, (buf->perms & AV_PERM_WRITE) ? 0 : AV_BUFFER_FLAG_READONLY); \
  193. if (!ref_out) { \
  194. av_frame_unref(frame); \
  195. ret = AVERROR(ENOMEM); \
  196. goto fail; \
  197. } \
  198. } while (0)
  199. if (ctx->outputs[0]->type == AVMEDIA_TYPE_VIDEO) {
  200. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  201. planes = av_pix_fmt_count_planes(frame->format);
  202. if (!desc || planes <= 0) {
  203. ret = AVERROR(EINVAL);
  204. goto fail;
  205. }
  206. for (i = 0; i < planes; i++) {
  207. int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  208. int plane_size = (frame->height >> v_shift) * frame->linesize[i];
  209. WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
  210. }
  211. } else {
  212. int planar = av_sample_fmt_is_planar(frame->format);
  213. int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  214. planes = planar ? channels : 1;
  215. if (planes > FF_ARRAY_ELEMS(frame->buf)) {
  216. frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
  217. frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
  218. frame->nb_extended_buf);
  219. if (!frame->extended_buf) {
  220. ret = AVERROR(ENOMEM);
  221. goto fail;
  222. }
  223. }
  224. for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
  225. WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
  226. for (i = 0; i < planes - FF_ARRAY_ELEMS(frame->buf); i++)
  227. WRAP_PLANE(frame->extended_buf[i],
  228. frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
  229. frame->linesize[0]);
  230. }
  231. ret = av_buffersrc_add_frame_flags(ctx, frame, flags);
  232. fail:
  233. av_buffer_unref(&dummy_buf);
  234. av_frame_free(&frame);
  235. return ret;
  236. }
  237. int av_buffersrc_buffer(AVFilterContext *ctx, AVFilterBufferRef *buf)
  238. {
  239. return av_buffersrc_add_ref(ctx, buf, 0);
  240. }
  241. #endif
  242. #define OFFSET(x) offsetof(BufferSourceContext, x)
  243. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  244. static const AVOption buffer_options[] = {
  245. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  246. { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  247. { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = FLAGS },
  248. { "pix_fmt", NULL, OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, .flags = FLAGS },
  249. { "pixel_aspect", NULL, OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  250. { "sws_param", NULL, OFFSET(sws_param), AV_OPT_TYPE_STRING, .flags = FLAGS },
  251. { NULL },
  252. };
  253. #undef FLAGS
  254. AVFILTER_DEFINE_CLASS(buffer);
  255. static av_cold int init_video(AVFilterContext *ctx, const char *args)
  256. {
  257. BufferSourceContext *c = ctx->priv;
  258. char pix_fmt_str[128], *colon, *equal;
  259. int ret, n = 0;
  260. c->class = &buffer_class;
  261. if (!args) {
  262. av_log(ctx, AV_LOG_ERROR, "Arguments required\n");
  263. return AVERROR(EINVAL);
  264. }
  265. colon = strchr(args, ':');
  266. equal = strchr(args, '=');
  267. if (equal && (!colon || equal < colon)) {
  268. av_opt_set_defaults(c);
  269. ret = av_set_options_string(c, args, "=", ":");
  270. if (ret < 0)
  271. goto fail;
  272. } else {
  273. if (!args ||
  274. (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d", &c->w, &c->h, pix_fmt_str,
  275. &c->time_base.num, &c->time_base.den,
  276. &c->pixel_aspect.num, &c->pixel_aspect.den)) != 7) {
  277. av_log(ctx, AV_LOG_ERROR, "Expected 7 arguments, but %d found in '%s'\n", n, args);
  278. return AVERROR(EINVAL);
  279. }
  280. if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == AV_PIX_FMT_NONE) {
  281. char *tail;
  282. c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
  283. if (*tail || c->pix_fmt < 0 || c->pix_fmt >= AV_PIX_FMT_NB) {
  284. av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
  285. return AVERROR(EINVAL);
  286. }
  287. }
  288. }
  289. if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*))))
  290. return AVERROR(ENOMEM);
  291. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d pixfmt:%s tb:%d/%d fr:%d/%d sar:%d/%d sws_param:%s\n",
  292. c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
  293. c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
  294. c->pixel_aspect.num, c->pixel_aspect.den, (char *)av_x_if_null(c->sws_param, ""));
  295. c->warning_limit = 100;
  296. return 0;
  297. fail:
  298. av_opt_free(c);
  299. return ret;
  300. }
  301. unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
  302. {
  303. return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
  304. }
  305. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  306. static const AVOption abuffer_options[] = {
  307. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  308. { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  309. { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = FLAGS },
  310. { "channels", NULL, OFFSET(channels), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  311. { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = FLAGS },
  312. { NULL },
  313. };
  314. AVFILTER_DEFINE_CLASS(abuffer);
  315. static av_cold int init_audio(AVFilterContext *ctx, const char *args)
  316. {
  317. BufferSourceContext *s = ctx->priv;
  318. int ret = 0;
  319. s->class = &abuffer_class;
  320. av_opt_set_defaults(s);
  321. if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
  322. goto fail;
  323. s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
  324. if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
  325. av_log(ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n",
  326. s->sample_fmt_str);
  327. ret = AVERROR(EINVAL);
  328. goto fail;
  329. }
  330. if (s->channel_layout_str) {
  331. int n;
  332. /* TODO reindent */
  333. s->channel_layout = av_get_channel_layout(s->channel_layout_str);
  334. if (!s->channel_layout) {
  335. av_log(ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n",
  336. s->channel_layout_str);
  337. ret = AVERROR(EINVAL);
  338. goto fail;
  339. }
  340. n = av_get_channel_layout_nb_channels(s->channel_layout);
  341. if (s->channels) {
  342. if (n != s->channels) {
  343. av_log(ctx, AV_LOG_ERROR,
  344. "Mismatching channel count %d and layout '%s' "
  345. "(%d channels)\n",
  346. s->channels, s->channel_layout_str, n);
  347. ret = AVERROR(EINVAL);
  348. goto fail;
  349. }
  350. }
  351. s->channels = n;
  352. } else if (!s->channels) {
  353. av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor "
  354. "channel layout specified\n");
  355. ret = AVERROR(EINVAL);
  356. goto fail;
  357. }
  358. if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*)))) {
  359. ret = AVERROR(ENOMEM);
  360. goto fail;
  361. }
  362. if (!s->time_base.num)
  363. s->time_base = (AVRational){1, s->sample_rate};
  364. av_log(ctx, AV_LOG_VERBOSE,
  365. "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
  366. s->time_base.num, s->time_base.den, s->sample_fmt_str,
  367. s->sample_rate, s->channel_layout_str);
  368. s->warning_limit = 100;
  369. fail:
  370. av_opt_free(s);
  371. return ret;
  372. }
  373. static av_cold void uninit(AVFilterContext *ctx)
  374. {
  375. BufferSourceContext *s = ctx->priv;
  376. while (s->fifo && av_fifo_size(s->fifo)) {
  377. AVFrame *frame;
  378. av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
  379. av_frame_free(&frame);
  380. }
  381. av_fifo_free(s->fifo);
  382. s->fifo = NULL;
  383. av_freep(&s->sws_param);
  384. }
  385. static int query_formats(AVFilterContext *ctx)
  386. {
  387. BufferSourceContext *c = ctx->priv;
  388. AVFilterChannelLayouts *channel_layouts = NULL;
  389. AVFilterFormats *formats = NULL;
  390. AVFilterFormats *samplerates = NULL;
  391. switch (ctx->outputs[0]->type) {
  392. case AVMEDIA_TYPE_VIDEO:
  393. ff_add_format(&formats, c->pix_fmt);
  394. ff_set_common_formats(ctx, formats);
  395. break;
  396. case AVMEDIA_TYPE_AUDIO:
  397. ff_add_format(&formats, c->sample_fmt);
  398. ff_set_common_formats(ctx, formats);
  399. ff_add_format(&samplerates, c->sample_rate);
  400. ff_set_common_samplerates(ctx, samplerates);
  401. ff_add_channel_layout(&channel_layouts,
  402. c->channel_layout ? c->channel_layout :
  403. FF_COUNT2LAYOUT(c->channels));
  404. ff_set_common_channel_layouts(ctx, channel_layouts);
  405. break;
  406. default:
  407. return AVERROR(EINVAL);
  408. }
  409. return 0;
  410. }
  411. static int config_props(AVFilterLink *link)
  412. {
  413. BufferSourceContext *c = link->src->priv;
  414. switch (link->type) {
  415. case AVMEDIA_TYPE_VIDEO:
  416. link->w = c->w;
  417. link->h = c->h;
  418. link->sample_aspect_ratio = c->pixel_aspect;
  419. break;
  420. case AVMEDIA_TYPE_AUDIO:
  421. if (!c->channel_layout)
  422. c->channel_layout = link->channel_layout;
  423. break;
  424. default:
  425. return AVERROR(EINVAL);
  426. }
  427. link->time_base = c->time_base;
  428. link->frame_rate = c->frame_rate;
  429. return 0;
  430. }
  431. static int request_frame(AVFilterLink *link)
  432. {
  433. BufferSourceContext *c = link->src->priv;
  434. AVFrame *frame;
  435. if (!av_fifo_size(c->fifo)) {
  436. if (c->eof)
  437. return AVERROR_EOF;
  438. c->nb_failed_requests++;
  439. return AVERROR(EAGAIN);
  440. }
  441. av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
  442. return ff_filter_frame(link, frame);
  443. }
  444. static int poll_frame(AVFilterLink *link)
  445. {
  446. BufferSourceContext *c = link->src->priv;
  447. int size = av_fifo_size(c->fifo);
  448. if (!size && c->eof)
  449. return AVERROR_EOF;
  450. return size/sizeof(AVFrame*);
  451. }
  452. static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
  453. {
  454. .name = "default",
  455. .type = AVMEDIA_TYPE_VIDEO,
  456. .request_frame = request_frame,
  457. .poll_frame = poll_frame,
  458. .config_props = config_props,
  459. },
  460. { NULL }
  461. };
  462. AVFilter avfilter_vsrc_buffer = {
  463. .name = "buffer",
  464. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  465. .priv_size = sizeof(BufferSourceContext),
  466. .query_formats = query_formats,
  467. .init = init_video,
  468. .uninit = uninit,
  469. .inputs = NULL,
  470. .outputs = avfilter_vsrc_buffer_outputs,
  471. .priv_class = &buffer_class,
  472. };
  473. static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
  474. {
  475. .name = "default",
  476. .type = AVMEDIA_TYPE_AUDIO,
  477. .request_frame = request_frame,
  478. .poll_frame = poll_frame,
  479. .config_props = config_props,
  480. },
  481. { NULL }
  482. };
  483. AVFilter avfilter_asrc_abuffer = {
  484. .name = "abuffer",
  485. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
  486. .priv_size = sizeof(BufferSourceContext),
  487. .query_formats = query_formats,
  488. .init = init_audio,
  489. .uninit = uninit,
  490. .inputs = NULL,
  491. .outputs = avfilter_asrc_abuffer_outputs,
  492. .priv_class = &abuffer_class,
  493. };