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.

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