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.

577 lines
18KB

  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, format)\
  64. if (c->sample_fmt != format || c->sample_rate != srate ||\
  65. c->channel_layout != ch_layout) {\
  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 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. CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
  118. frame->format);
  119. break;
  120. default:
  121. return AVERROR(EINVAL);
  122. }
  123. }
  124. if (!av_fifo_space(s->fifo) &&
  125. (ret = av_fifo_realloc2(s->fifo, av_fifo_size(s->fifo) +
  126. sizeof(copy))) < 0)
  127. return ret;
  128. if (!(copy = av_frame_alloc()))
  129. return AVERROR(ENOMEM);
  130. av_frame_move_ref(copy, frame);
  131. if ((ret = av_fifo_generic_write(s->fifo, &copy, sizeof(copy), NULL)) < 0) {
  132. av_frame_move_ref(frame, copy);
  133. av_frame_free(&copy);
  134. return ret;
  135. }
  136. if ((flags & AV_BUFFERSRC_FLAG_PUSH))
  137. if ((ret = ctx->output_pads[0].request_frame(ctx->outputs[0])) < 0)
  138. return ret;
  139. return 0;
  140. }
  141. #if FF_API_AVFILTERBUFFER
  142. static void compat_free_buffer(void *opaque, uint8_t *data)
  143. {
  144. AVFilterBufferRef *buf = opaque;
  145. avfilter_unref_buffer(buf);
  146. }
  147. static void compat_unref_buffer(void *opaque, uint8_t *data)
  148. {
  149. AVBufferRef *buf = opaque;
  150. av_buffer_unref(&buf);
  151. }
  152. int av_buffersrc_add_ref(AVFilterContext *ctx, AVFilterBufferRef *buf,
  153. int flags)
  154. {
  155. BufferSourceContext *s = ctx->priv;
  156. AVFrame *frame = NULL;
  157. AVBufferRef *dummy_buf = NULL;
  158. int ret = 0, planes, i;
  159. if (!buf) {
  160. s->eof = 1;
  161. return 0;
  162. } else if (s->eof)
  163. return AVERROR(EINVAL);
  164. frame = av_frame_alloc();
  165. if (!frame)
  166. return AVERROR(ENOMEM);
  167. dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, buf,
  168. (buf->perms & AV_PERM_WRITE) ? 0 : AV_BUFFER_FLAG_READONLY);
  169. if (!dummy_buf) {
  170. ret = AVERROR(ENOMEM);
  171. goto fail;
  172. }
  173. if ((ret = avfilter_copy_buf_props(frame, buf)) < 0)
  174. goto fail;
  175. #define WRAP_PLANE(ref_out, data, data_size) \
  176. do { \
  177. AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
  178. if (!dummy_ref) { \
  179. ret = AVERROR(ENOMEM); \
  180. goto fail; \
  181. } \
  182. ref_out = av_buffer_create(data, data_size, compat_unref_buffer, \
  183. dummy_ref, (buf->perms & AV_PERM_WRITE) ? 0 : AV_BUFFER_FLAG_READONLY); \
  184. if (!ref_out) { \
  185. av_frame_unref(frame); \
  186. ret = AVERROR(ENOMEM); \
  187. goto fail; \
  188. } \
  189. } while (0)
  190. if (ctx->outputs[0]->type == AVMEDIA_TYPE_VIDEO) {
  191. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  192. if (!desc) {
  193. ret = AVERROR(EINVAL);
  194. goto fail;
  195. }
  196. planes = (desc->flags & PIX_FMT_PLANAR) ? desc->nb_components : 1;
  197. for (i = 0; i < planes; i++) {
  198. int h_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  199. int plane_size = (frame->width >> h_shift) * frame->linesize[i];
  200. WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
  201. }
  202. } else {
  203. int planar = av_sample_fmt_is_planar(frame->format);
  204. int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  205. planes = planar ? channels : 1;
  206. if (planes > FF_ARRAY_ELEMS(frame->buf)) {
  207. frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
  208. frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
  209. frame->nb_extended_buf);
  210. if (!frame->extended_buf) {
  211. ret = AVERROR(ENOMEM);
  212. goto fail;
  213. }
  214. }
  215. for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
  216. WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
  217. for (i = 0; i < planes - FF_ARRAY_ELEMS(frame->buf); i++)
  218. WRAP_PLANE(frame->extended_buf[i],
  219. frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
  220. frame->linesize[0]);
  221. }
  222. ret = av_buffersrc_add_frame_flags(ctx, frame, flags);
  223. fail:
  224. av_buffer_unref(&dummy_buf);
  225. av_frame_free(&frame);
  226. return ret;
  227. }
  228. int av_buffersrc_buffer(AVFilterContext *ctx, AVFilterBufferRef *buf)
  229. {
  230. return av_buffersrc_add_ref(ctx, buf, 0);
  231. }
  232. #endif
  233. #define OFFSET(x) offsetof(BufferSourceContext, x)
  234. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  235. static const AVOption buffer_options[] = {
  236. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  237. { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  238. { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = FLAGS },
  239. { "pix_fmt", NULL, OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, .flags = FLAGS },
  240. { "pixel_aspect", NULL, OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  241. { "sws_param", NULL, OFFSET(sws_param), AV_OPT_TYPE_STRING, .flags = FLAGS },
  242. { NULL },
  243. };
  244. #undef FLAGS
  245. AVFILTER_DEFINE_CLASS(buffer);
  246. static av_cold int init_video(AVFilterContext *ctx, const char *args)
  247. {
  248. BufferSourceContext *c = ctx->priv;
  249. char pix_fmt_str[128], *colon, *equal;
  250. int ret, n = 0;
  251. c->class = &buffer_class;
  252. if (!args) {
  253. av_log(ctx, AV_LOG_ERROR, "Arguments required\n");
  254. return AVERROR(EINVAL);
  255. }
  256. colon = strchr(args, ':');
  257. equal = strchr(args, '=');
  258. if (equal && (!colon || equal < colon)) {
  259. av_opt_set_defaults(c);
  260. ret = av_set_options_string(c, args, "=", ":");
  261. if (ret < 0)
  262. goto fail;
  263. } else {
  264. if (!args ||
  265. (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d", &c->w, &c->h, pix_fmt_str,
  266. &c->time_base.num, &c->time_base.den,
  267. &c->pixel_aspect.num, &c->pixel_aspect.den)) != 7) {
  268. av_log(ctx, AV_LOG_ERROR, "Expected 7 arguments, but %d found in '%s'\n", n, args);
  269. return AVERROR(EINVAL);
  270. }
  271. if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == AV_PIX_FMT_NONE) {
  272. char *tail;
  273. c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
  274. if (*tail || c->pix_fmt < 0 || c->pix_fmt >= AV_PIX_FMT_NB) {
  275. av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
  276. return AVERROR(EINVAL);
  277. }
  278. }
  279. }
  280. if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*))))
  281. return AVERROR(ENOMEM);
  282. 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",
  283. c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
  284. c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
  285. c->pixel_aspect.num, c->pixel_aspect.den, (char *)av_x_if_null(c->sws_param, ""));
  286. c->warning_limit = 100;
  287. return 0;
  288. fail:
  289. av_opt_free(c);
  290. return ret;
  291. }
  292. unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
  293. {
  294. return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
  295. }
  296. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  297. static const AVOption abuffer_options[] = {
  298. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  299. { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  300. { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = FLAGS },
  301. { "channels", NULL, OFFSET(channels), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  302. { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = FLAGS },
  303. { NULL },
  304. };
  305. AVFILTER_DEFINE_CLASS(abuffer);
  306. static av_cold int init_audio(AVFilterContext *ctx, const char *args)
  307. {
  308. BufferSourceContext *s = ctx->priv;
  309. int ret = 0;
  310. s->class = &abuffer_class;
  311. av_opt_set_defaults(s);
  312. if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
  313. goto fail;
  314. s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
  315. if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
  316. av_log(ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n",
  317. s->sample_fmt_str);
  318. ret = AVERROR(EINVAL);
  319. goto fail;
  320. }
  321. if (s->channel_layout_str) {
  322. int n;
  323. /* TODO reindent */
  324. s->channel_layout = av_get_channel_layout(s->channel_layout_str);
  325. if (!s->channel_layout) {
  326. av_log(ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n",
  327. s->channel_layout_str);
  328. ret = AVERROR(EINVAL);
  329. goto fail;
  330. }
  331. n = av_get_channel_layout_nb_channels(s->channel_layout);
  332. if (s->channels) {
  333. if (n != s->channels) {
  334. av_log(ctx, AV_LOG_ERROR,
  335. "Mismatching channel count %d and layout '%s' "
  336. "(%d channels)\n",
  337. s->channels, s->channel_layout_str, n);
  338. ret = AVERROR(EINVAL);
  339. goto fail;
  340. }
  341. }
  342. s->channels = n;
  343. } else if (!s->channels) {
  344. av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor "
  345. "channel layout specified\n");
  346. ret = AVERROR(EINVAL);
  347. goto fail;
  348. }
  349. if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*)))) {
  350. ret = AVERROR(ENOMEM);
  351. goto fail;
  352. }
  353. if (!s->time_base.num)
  354. s->time_base = (AVRational){1, s->sample_rate};
  355. av_log(ctx, AV_LOG_VERBOSE,
  356. "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
  357. s->time_base.num, s->time_base.den, s->sample_fmt_str,
  358. s->sample_rate, s->channel_layout_str);
  359. s->warning_limit = 100;
  360. fail:
  361. av_opt_free(s);
  362. return ret;
  363. }
  364. static av_cold void uninit(AVFilterContext *ctx)
  365. {
  366. BufferSourceContext *s = ctx->priv;
  367. while (s->fifo && av_fifo_size(s->fifo)) {
  368. AVFrame *frame;
  369. av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
  370. av_frame_free(&frame);
  371. }
  372. av_fifo_free(s->fifo);
  373. s->fifo = NULL;
  374. av_freep(&s->sws_param);
  375. }
  376. static int query_formats(AVFilterContext *ctx)
  377. {
  378. BufferSourceContext *c = ctx->priv;
  379. AVFilterChannelLayouts *channel_layouts = NULL;
  380. AVFilterFormats *formats = NULL;
  381. AVFilterFormats *samplerates = NULL;
  382. switch (ctx->outputs[0]->type) {
  383. case AVMEDIA_TYPE_VIDEO:
  384. ff_add_format(&formats, c->pix_fmt);
  385. ff_set_common_formats(ctx, formats);
  386. break;
  387. case AVMEDIA_TYPE_AUDIO:
  388. ff_add_format(&formats, c->sample_fmt);
  389. ff_set_common_formats(ctx, formats);
  390. ff_add_format(&samplerates, c->sample_rate);
  391. ff_set_common_samplerates(ctx, samplerates);
  392. ff_add_channel_layout(&channel_layouts,
  393. c->channel_layout ? c->channel_layout :
  394. FF_COUNT2LAYOUT(c->channels));
  395. ff_set_common_channel_layouts(ctx, channel_layouts);
  396. break;
  397. default:
  398. return AVERROR(EINVAL);
  399. }
  400. return 0;
  401. }
  402. static int config_props(AVFilterLink *link)
  403. {
  404. BufferSourceContext *c = link->src->priv;
  405. switch (link->type) {
  406. case AVMEDIA_TYPE_VIDEO:
  407. link->w = c->w;
  408. link->h = c->h;
  409. link->sample_aspect_ratio = c->pixel_aspect;
  410. break;
  411. case AVMEDIA_TYPE_AUDIO:
  412. if (!c->channel_layout)
  413. c->channel_layout = link->channel_layout;
  414. break;
  415. default:
  416. return AVERROR(EINVAL);
  417. }
  418. link->time_base = c->time_base;
  419. link->frame_rate = c->frame_rate;
  420. return 0;
  421. }
  422. static int request_frame(AVFilterLink *link)
  423. {
  424. BufferSourceContext *c = link->src->priv;
  425. AVFrame *frame;
  426. int ret = 0;
  427. if (!av_fifo_size(c->fifo)) {
  428. if (c->eof)
  429. return AVERROR_EOF;
  430. c->nb_failed_requests++;
  431. return AVERROR(EAGAIN);
  432. }
  433. av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
  434. /* CIG TODO do not ignore error */
  435. ff_filter_frame(link, frame);
  436. return ret;
  437. }
  438. static int poll_frame(AVFilterLink *link)
  439. {
  440. BufferSourceContext *c = link->src->priv;
  441. int size = av_fifo_size(c->fifo);
  442. if (!size && c->eof)
  443. return AVERROR_EOF;
  444. return size/sizeof(AVFrame*);
  445. }
  446. static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
  447. {
  448. .name = "default",
  449. .type = AVMEDIA_TYPE_VIDEO,
  450. .request_frame = request_frame,
  451. .poll_frame = poll_frame,
  452. .config_props = config_props,
  453. },
  454. { NULL }
  455. };
  456. AVFilter avfilter_vsrc_buffer = {
  457. .name = "buffer",
  458. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  459. .priv_size = sizeof(BufferSourceContext),
  460. .query_formats = query_formats,
  461. .init = init_video,
  462. .uninit = uninit,
  463. .inputs = NULL,
  464. .outputs = avfilter_vsrc_buffer_outputs,
  465. .priv_class = &buffer_class,
  466. };
  467. static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
  468. {
  469. .name = "default",
  470. .type = AVMEDIA_TYPE_AUDIO,
  471. .request_frame = request_frame,
  472. .poll_frame = poll_frame,
  473. .config_props = config_props,
  474. },
  475. { NULL }
  476. };
  477. AVFilter avfilter_asrc_abuffer = {
  478. .name = "abuffer",
  479. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
  480. .priv_size = sizeof(BufferSourceContext),
  481. .query_formats = query_formats,
  482. .init = init_audio,
  483. .uninit = uninit,
  484. .inputs = NULL,
  485. .outputs = avfilter_asrc_abuffer_outputs,
  486. .priv_class = &abuffer_class,
  487. };