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.

534 lines
17KB

  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 <float.h>
  25. #include "libavutil/channel_layout.h"
  26. #include "libavutil/common.h"
  27. #include "libavutil/fifo.h"
  28. #include "libavutil/frame.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/internal.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/samplefmt.h"
  33. #include "audio.h"
  34. #include "avfilter.h"
  35. #include "buffersrc.h"
  36. #include "formats.h"
  37. #include "internal.h"
  38. #include "video.h"
  39. typedef struct BufferSourceContext {
  40. const AVClass *class;
  41. AVFifoBuffer *fifo;
  42. AVRational time_base; ///< time_base to set in the output link
  43. AVRational frame_rate; ///< frame_rate to set in the output link
  44. unsigned nb_failed_requests;
  45. unsigned warning_limit;
  46. /* video only */
  47. int w, h;
  48. enum AVPixelFormat pix_fmt;
  49. AVRational pixel_aspect;
  50. char *sws_param;
  51. AVBufferRef *hw_frames_ctx;
  52. /* audio only */
  53. int sample_rate;
  54. enum AVSampleFormat sample_fmt;
  55. int channels;
  56. uint64_t channel_layout;
  57. char *channel_layout_str;
  58. int got_format_from_params;
  59. int eof;
  60. } BufferSourceContext;
  61. #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format)\
  62. if (c->w != width || c->h != height || c->pix_fmt != format) {\
  63. av_log(s, AV_LOG_INFO, "Changing frame properties on the fly is not supported by all filters.\n");\
  64. }
  65. #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, ch_count, format)\
  66. if (c->sample_fmt != format || c->sample_rate != srate ||\
  67. c->channel_layout != ch_layout || c->channels != ch_count) {\
  68. av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
  69. return AVERROR(EINVAL);\
  70. }
  71. AVBufferSrcParameters *av_buffersrc_parameters_alloc(void)
  72. {
  73. AVBufferSrcParameters *par = av_mallocz(sizeof(*par));
  74. if (!par)
  75. return NULL;
  76. par->format = -1;
  77. return par;
  78. }
  79. int av_buffersrc_parameters_set(AVFilterContext *ctx, AVBufferSrcParameters *param)
  80. {
  81. BufferSourceContext *s = ctx->priv;
  82. if (param->time_base.num > 0 && param->time_base.den > 0)
  83. s->time_base = param->time_base;
  84. switch (ctx->filter->outputs[0].type) {
  85. case AVMEDIA_TYPE_VIDEO:
  86. if (param->format != AV_PIX_FMT_NONE) {
  87. s->got_format_from_params = 1;
  88. s->pix_fmt = param->format;
  89. }
  90. if (param->width > 0)
  91. s->w = param->width;
  92. if (param->height > 0)
  93. s->h = param->height;
  94. if (param->sample_aspect_ratio.num > 0 && param->sample_aspect_ratio.den > 0)
  95. s->pixel_aspect = param->sample_aspect_ratio;
  96. if (param->frame_rate.num > 0 && param->frame_rate.den > 0)
  97. s->frame_rate = param->frame_rate;
  98. if (param->hw_frames_ctx) {
  99. av_buffer_unref(&s->hw_frames_ctx);
  100. s->hw_frames_ctx = av_buffer_ref(param->hw_frames_ctx);
  101. if (!s->hw_frames_ctx)
  102. return AVERROR(ENOMEM);
  103. }
  104. break;
  105. case AVMEDIA_TYPE_AUDIO:
  106. if (param->format != AV_SAMPLE_FMT_NONE) {
  107. s->got_format_from_params = 1;
  108. s->sample_fmt = param->format;
  109. }
  110. if (param->sample_rate > 0)
  111. s->sample_rate = param->sample_rate;
  112. if (param->channel_layout)
  113. s->channel_layout = param->channel_layout;
  114. break;
  115. default:
  116. return AVERROR_BUG;
  117. }
  118. return 0;
  119. }
  120. int attribute_align_arg av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
  121. {
  122. return av_buffersrc_add_frame_flags(ctx, (AVFrame *)frame,
  123. AV_BUFFERSRC_FLAG_KEEP_REF);
  124. }
  125. int attribute_align_arg av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame)
  126. {
  127. return av_buffersrc_add_frame_flags(ctx, frame, 0);
  128. }
  129. static int av_buffersrc_add_frame_internal(AVFilterContext *ctx,
  130. AVFrame *frame, int flags);
  131. int attribute_align_arg av_buffersrc_add_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
  132. {
  133. AVFrame *copy = NULL;
  134. int ret = 0;
  135. if (frame && frame->channel_layout &&
  136. av_get_channel_layout_nb_channels(frame->channel_layout) != frame->channels) {
  137. av_log(ctx, AV_LOG_ERROR, "Layout indicates a different number of channels than actually present\n");
  138. return AVERROR(EINVAL);
  139. }
  140. if (!(flags & AV_BUFFERSRC_FLAG_KEEP_REF) || !frame)
  141. return av_buffersrc_add_frame_internal(ctx, frame, flags);
  142. if (!(copy = av_frame_alloc()))
  143. return AVERROR(ENOMEM);
  144. ret = av_frame_ref(copy, frame);
  145. if (ret >= 0)
  146. ret = av_buffersrc_add_frame_internal(ctx, copy, flags);
  147. av_frame_free(&copy);
  148. return ret;
  149. }
  150. static int push_frame(AVFilterGraph *graph)
  151. {
  152. int ret;
  153. while (1) {
  154. ret = ff_filter_graph_run_once(graph);
  155. if (ret == AVERROR(EAGAIN))
  156. break;
  157. if (ret < 0)
  158. return ret;
  159. }
  160. return 0;
  161. }
  162. static int av_buffersrc_add_frame_internal(AVFilterContext *ctx,
  163. AVFrame *frame, int flags)
  164. {
  165. BufferSourceContext *s = ctx->priv;
  166. AVFrame *copy;
  167. int refcounted, ret;
  168. s->nb_failed_requests = 0;
  169. if (!frame)
  170. return av_buffersrc_close(ctx, AV_NOPTS_VALUE, flags);
  171. if (s->eof)
  172. return AVERROR(EINVAL);
  173. refcounted = !!frame->buf[0];
  174. if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
  175. switch (ctx->outputs[0]->type) {
  176. case AVMEDIA_TYPE_VIDEO:
  177. CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
  178. frame->format);
  179. break;
  180. case AVMEDIA_TYPE_AUDIO:
  181. /* For layouts unknown on input but known on link after negotiation. */
  182. if (!frame->channel_layout)
  183. frame->channel_layout = s->channel_layout;
  184. CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
  185. frame->channels, frame->format);
  186. break;
  187. default:
  188. return AVERROR(EINVAL);
  189. }
  190. }
  191. if (!av_fifo_space(s->fifo) &&
  192. (ret = av_fifo_realloc2(s->fifo, av_fifo_size(s->fifo) +
  193. sizeof(copy))) < 0)
  194. return ret;
  195. if (!(copy = av_frame_alloc()))
  196. return AVERROR(ENOMEM);
  197. if (refcounted) {
  198. av_frame_move_ref(copy, frame);
  199. } else {
  200. ret = av_frame_ref(copy, frame);
  201. if (ret < 0) {
  202. av_frame_free(&copy);
  203. return ret;
  204. }
  205. }
  206. if ((ret = av_fifo_generic_write(s->fifo, &copy, sizeof(copy), NULL)) < 0) {
  207. if (refcounted)
  208. av_frame_move_ref(frame, copy);
  209. av_frame_free(&copy);
  210. return ret;
  211. }
  212. if ((ret = ctx->output_pads[0].request_frame(ctx->outputs[0])) < 0)
  213. return ret;
  214. if ((flags & AV_BUFFERSRC_FLAG_PUSH)) {
  215. ret = push_frame(ctx->graph);
  216. if (ret < 0)
  217. return ret;
  218. }
  219. return 0;
  220. }
  221. int av_buffersrc_close(AVFilterContext *ctx, int64_t pts, unsigned flags)
  222. {
  223. BufferSourceContext *s = ctx->priv;
  224. s->eof = 1;
  225. ff_avfilter_link_set_in_status(ctx->outputs[0], AVERROR_EOF, pts);
  226. return (flags & AV_BUFFERSRC_FLAG_PUSH) ? push_frame(ctx->graph) : 0;
  227. }
  228. static av_cold int init_video(AVFilterContext *ctx)
  229. {
  230. BufferSourceContext *c = ctx->priv;
  231. if (!(c->pix_fmt != AV_PIX_FMT_NONE || c->got_format_from_params) || !c->w || !c->h ||
  232. av_q2d(c->time_base) <= 0) {
  233. av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n");
  234. return AVERROR(EINVAL);
  235. }
  236. if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*))))
  237. return AVERROR(ENOMEM);
  238. 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",
  239. c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
  240. c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
  241. c->pixel_aspect.num, c->pixel_aspect.den, (char *)av_x_if_null(c->sws_param, ""));
  242. c->warning_limit = 100;
  243. return 0;
  244. }
  245. unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
  246. {
  247. return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
  248. }
  249. #define OFFSET(x) offsetof(BufferSourceContext, x)
  250. #define A AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  251. #define V AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  252. static const AVOption buffer_options[] = {
  253. { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  254. { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = V },
  255. { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  256. { "pix_fmt", NULL, OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, { .i64 = AV_PIX_FMT_NONE }, .min = AV_PIX_FMT_NONE, .max = INT_MAX, .flags = V },
  257. { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  258. { "pixel_aspect", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  259. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  260. { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  261. { "sws_param", NULL, OFFSET(sws_param), AV_OPT_TYPE_STRING, .flags = V },
  262. { NULL },
  263. };
  264. AVFILTER_DEFINE_CLASS(buffer);
  265. static const AVOption abuffer_options[] = {
  266. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
  267. { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
  268. { "sample_fmt", NULL, OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, { .i64 = AV_SAMPLE_FMT_NONE }, .min = AV_SAMPLE_FMT_NONE, .max = INT_MAX, .flags = A },
  269. { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
  270. { "channels", NULL, OFFSET(channels), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
  271. { NULL },
  272. };
  273. AVFILTER_DEFINE_CLASS(abuffer);
  274. static av_cold int init_audio(AVFilterContext *ctx)
  275. {
  276. BufferSourceContext *s = ctx->priv;
  277. int ret = 0;
  278. if (!(s->sample_fmt != AV_SAMPLE_FMT_NONE || s->got_format_from_params)) {
  279. av_log(ctx, AV_LOG_ERROR, "Sample format was not set or was invalid\n");
  280. return AVERROR(EINVAL);
  281. }
  282. if (s->channel_layout_str || s->channel_layout) {
  283. int n;
  284. if (!s->channel_layout) {
  285. s->channel_layout = av_get_channel_layout(s->channel_layout_str);
  286. if (!s->channel_layout) {
  287. av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
  288. s->channel_layout_str);
  289. return AVERROR(EINVAL);
  290. }
  291. }
  292. n = av_get_channel_layout_nb_channels(s->channel_layout);
  293. if (s->channels) {
  294. if (n != s->channels) {
  295. av_log(ctx, AV_LOG_ERROR,
  296. "Mismatching channel count %d and layout '%s' "
  297. "(%d channels)\n",
  298. s->channels, s->channel_layout_str, n);
  299. return AVERROR(EINVAL);
  300. }
  301. }
  302. s->channels = n;
  303. } else if (!s->channels) {
  304. av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor "
  305. "channel layout specified\n");
  306. return AVERROR(EINVAL);
  307. }
  308. if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*))))
  309. return AVERROR(ENOMEM);
  310. if (!s->time_base.num)
  311. s->time_base = (AVRational){1, s->sample_rate};
  312. av_log(ctx, AV_LOG_VERBOSE,
  313. "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
  314. s->time_base.num, s->time_base.den, av_get_sample_fmt_name(s->sample_fmt),
  315. s->sample_rate, s->channel_layout_str);
  316. s->warning_limit = 100;
  317. return ret;
  318. }
  319. static av_cold void uninit(AVFilterContext *ctx)
  320. {
  321. BufferSourceContext *s = ctx->priv;
  322. while (s->fifo && av_fifo_size(s->fifo)) {
  323. AVFrame *frame;
  324. av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
  325. av_frame_free(&frame);
  326. }
  327. av_buffer_unref(&s->hw_frames_ctx);
  328. av_fifo_freep(&s->fifo);
  329. }
  330. static int query_formats(AVFilterContext *ctx)
  331. {
  332. BufferSourceContext *c = ctx->priv;
  333. AVFilterChannelLayouts *channel_layouts = NULL;
  334. AVFilterFormats *formats = NULL;
  335. AVFilterFormats *samplerates = NULL;
  336. int ret;
  337. switch (ctx->outputs[0]->type) {
  338. case AVMEDIA_TYPE_VIDEO:
  339. if ((ret = ff_add_format (&formats, c->pix_fmt)) < 0 ||
  340. (ret = ff_set_common_formats (ctx , formats )) < 0)
  341. return ret;
  342. break;
  343. case AVMEDIA_TYPE_AUDIO:
  344. if ((ret = ff_add_format (&formats , c->sample_fmt )) < 0 ||
  345. (ret = ff_set_common_formats (ctx , formats )) < 0 ||
  346. (ret = ff_add_format (&samplerates, c->sample_rate)) < 0 ||
  347. (ret = ff_set_common_samplerates (ctx , samplerates )) < 0)
  348. return ret;
  349. if ((ret = ff_add_channel_layout(&channel_layouts,
  350. c->channel_layout ? c->channel_layout :
  351. FF_COUNT2LAYOUT(c->channels))) < 0)
  352. return ret;
  353. if ((ret = ff_set_common_channel_layouts(ctx, channel_layouts)) < 0)
  354. return ret;
  355. break;
  356. default:
  357. return AVERROR(EINVAL);
  358. }
  359. return 0;
  360. }
  361. static int config_props(AVFilterLink *link)
  362. {
  363. BufferSourceContext *c = link->src->priv;
  364. switch (link->type) {
  365. case AVMEDIA_TYPE_VIDEO:
  366. link->w = c->w;
  367. link->h = c->h;
  368. link->sample_aspect_ratio = c->pixel_aspect;
  369. if (c->hw_frames_ctx) {
  370. link->hw_frames_ctx = av_buffer_ref(c->hw_frames_ctx);
  371. if (!link->hw_frames_ctx)
  372. return AVERROR(ENOMEM);
  373. }
  374. break;
  375. case AVMEDIA_TYPE_AUDIO:
  376. if (!c->channel_layout)
  377. c->channel_layout = link->channel_layout;
  378. break;
  379. default:
  380. return AVERROR(EINVAL);
  381. }
  382. link->time_base = c->time_base;
  383. link->frame_rate = c->frame_rate;
  384. return 0;
  385. }
  386. static int request_frame(AVFilterLink *link)
  387. {
  388. BufferSourceContext *c = link->src->priv;
  389. AVFrame *frame;
  390. int ret;
  391. if (!av_fifo_size(c->fifo)) {
  392. if (c->eof)
  393. return AVERROR_EOF;
  394. c->nb_failed_requests++;
  395. return AVERROR(EAGAIN);
  396. }
  397. av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
  398. ret = ff_filter_frame(link, frame);
  399. return ret;
  400. }
  401. static int poll_frame(AVFilterLink *link)
  402. {
  403. BufferSourceContext *c = link->src->priv;
  404. int size = av_fifo_size(c->fifo);
  405. if (!size && c->eof)
  406. return AVERROR_EOF;
  407. return size/sizeof(AVFrame*);
  408. }
  409. static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
  410. {
  411. .name = "default",
  412. .type = AVMEDIA_TYPE_VIDEO,
  413. .request_frame = request_frame,
  414. .poll_frame = poll_frame,
  415. .config_props = config_props,
  416. },
  417. { NULL }
  418. };
  419. AVFilter ff_vsrc_buffer = {
  420. .name = "buffer",
  421. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  422. .priv_size = sizeof(BufferSourceContext),
  423. .query_formats = query_formats,
  424. .init = init_video,
  425. .uninit = uninit,
  426. .inputs = NULL,
  427. .outputs = avfilter_vsrc_buffer_outputs,
  428. .priv_class = &buffer_class,
  429. };
  430. static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
  431. {
  432. .name = "default",
  433. .type = AVMEDIA_TYPE_AUDIO,
  434. .request_frame = request_frame,
  435. .poll_frame = poll_frame,
  436. .config_props = config_props,
  437. },
  438. { NULL }
  439. };
  440. AVFilter ff_asrc_abuffer = {
  441. .name = "abuffer",
  442. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
  443. .priv_size = sizeof(BufferSourceContext),
  444. .query_formats = query_formats,
  445. .init = init_audio,
  446. .uninit = uninit,
  447. .inputs = NULL,
  448. .outputs = avfilter_asrc_abuffer_outputs,
  449. .priv_class = &abuffer_class,
  450. };