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.

513 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) != av_frame_get_channels(frame)) {
  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 av_buffersrc_add_frame_internal(AVFilterContext *ctx,
  151. AVFrame *frame, int flags)
  152. {
  153. BufferSourceContext *s = ctx->priv;
  154. AVFrame *copy;
  155. int refcounted, ret;
  156. s->nb_failed_requests = 0;
  157. if (!frame) {
  158. s->eof = 1;
  159. return 0;
  160. } else if (s->eof)
  161. return AVERROR(EINVAL);
  162. refcounted = !!frame->buf[0];
  163. if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
  164. switch (ctx->outputs[0]->type) {
  165. case AVMEDIA_TYPE_VIDEO:
  166. CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
  167. frame->format);
  168. break;
  169. case AVMEDIA_TYPE_AUDIO:
  170. /* For layouts unknown on input but known on link after negotiation. */
  171. if (!frame->channel_layout)
  172. frame->channel_layout = s->channel_layout;
  173. CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
  174. av_frame_get_channels(frame), frame->format);
  175. break;
  176. default:
  177. return AVERROR(EINVAL);
  178. }
  179. }
  180. if (!av_fifo_space(s->fifo) &&
  181. (ret = av_fifo_realloc2(s->fifo, av_fifo_size(s->fifo) +
  182. sizeof(copy))) < 0)
  183. return ret;
  184. if (!(copy = av_frame_alloc()))
  185. return AVERROR(ENOMEM);
  186. if (refcounted) {
  187. av_frame_move_ref(copy, frame);
  188. } else {
  189. ret = av_frame_ref(copy, frame);
  190. if (ret < 0) {
  191. av_frame_free(&copy);
  192. return ret;
  193. }
  194. }
  195. if ((ret = av_fifo_generic_write(s->fifo, &copy, sizeof(copy), NULL)) < 0) {
  196. if (refcounted)
  197. av_frame_move_ref(frame, copy);
  198. av_frame_free(&copy);
  199. return ret;
  200. }
  201. if ((flags & AV_BUFFERSRC_FLAG_PUSH))
  202. if ((ret = ctx->output_pads[0].request_frame(ctx->outputs[0])) < 0)
  203. return ret;
  204. return 0;
  205. }
  206. static av_cold int init_video(AVFilterContext *ctx)
  207. {
  208. BufferSourceContext *c = ctx->priv;
  209. if (!(c->pix_fmt != AV_PIX_FMT_NONE || c->got_format_from_params) || !c->w || !c->h ||
  210. av_q2d(c->time_base) <= 0) {
  211. av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n");
  212. return AVERROR(EINVAL);
  213. }
  214. if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*))))
  215. return AVERROR(ENOMEM);
  216. 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",
  217. c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
  218. c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
  219. c->pixel_aspect.num, c->pixel_aspect.den, (char *)av_x_if_null(c->sws_param, ""));
  220. c->warning_limit = 100;
  221. return 0;
  222. }
  223. unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
  224. {
  225. return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
  226. }
  227. #define OFFSET(x) offsetof(BufferSourceContext, x)
  228. #define A AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  229. #define V AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  230. static const AVOption buffer_options[] = {
  231. { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  232. { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = V },
  233. { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  234. { "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 },
  235. #if FF_API_OLD_FILTER_OPTS
  236. /* those 4 are for compatibility with the old option passing system where each filter
  237. * did its own parsing */
  238. { "time_base_num", "deprecated, do not use", OFFSET(time_base.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  239. { "time_base_den", "deprecated, do not use", OFFSET(time_base.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  240. { "sar_num", "deprecated, do not use", OFFSET(pixel_aspect.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  241. { "sar_den", "deprecated, do not use", OFFSET(pixel_aspect.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  242. #endif
  243. { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  244. { "pixel_aspect", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  245. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  246. { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  247. { "sws_param", NULL, OFFSET(sws_param), AV_OPT_TYPE_STRING, .flags = V },
  248. { NULL },
  249. };
  250. AVFILTER_DEFINE_CLASS(buffer);
  251. static const AVOption abuffer_options[] = {
  252. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
  253. { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
  254. { "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 },
  255. { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
  256. { "channels", NULL, OFFSET(channels), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
  257. { NULL },
  258. };
  259. AVFILTER_DEFINE_CLASS(abuffer);
  260. static av_cold int init_audio(AVFilterContext *ctx)
  261. {
  262. BufferSourceContext *s = ctx->priv;
  263. int ret = 0;
  264. if (!(s->sample_fmt != AV_SAMPLE_FMT_NONE || s->got_format_from_params)) {
  265. av_log(ctx, AV_LOG_ERROR, "Sample format was not set or was invalid\n");
  266. return AVERROR(EINVAL);
  267. }
  268. if (s->channel_layout_str) {
  269. int n;
  270. s->channel_layout = av_get_channel_layout(s->channel_layout_str);
  271. if (!s->channel_layout) {
  272. av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
  273. s->channel_layout_str);
  274. return AVERROR(EINVAL);
  275. }
  276. n = av_get_channel_layout_nb_channels(s->channel_layout);
  277. if (s->channels) {
  278. if (n != s->channels) {
  279. av_log(ctx, AV_LOG_ERROR,
  280. "Mismatching channel count %d and layout '%s' "
  281. "(%d channels)\n",
  282. s->channels, s->channel_layout_str, n);
  283. return AVERROR(EINVAL);
  284. }
  285. }
  286. s->channels = n;
  287. } else if (!s->channels) {
  288. av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor "
  289. "channel layout specified\n");
  290. return AVERROR(EINVAL);
  291. }
  292. if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*))))
  293. return AVERROR(ENOMEM);
  294. if (!s->time_base.num)
  295. s->time_base = (AVRational){1, s->sample_rate};
  296. av_log(ctx, AV_LOG_VERBOSE,
  297. "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
  298. s->time_base.num, s->time_base.den, av_get_sample_fmt_name(s->sample_fmt),
  299. s->sample_rate, s->channel_layout_str);
  300. s->warning_limit = 100;
  301. return ret;
  302. }
  303. static av_cold void uninit(AVFilterContext *ctx)
  304. {
  305. BufferSourceContext *s = ctx->priv;
  306. while (s->fifo && av_fifo_size(s->fifo)) {
  307. AVFrame *frame;
  308. av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
  309. av_frame_free(&frame);
  310. }
  311. av_buffer_unref(&s->hw_frames_ctx);
  312. av_fifo_freep(&s->fifo);
  313. }
  314. static int query_formats(AVFilterContext *ctx)
  315. {
  316. BufferSourceContext *c = ctx->priv;
  317. AVFilterChannelLayouts *channel_layouts = NULL;
  318. AVFilterFormats *formats = NULL;
  319. AVFilterFormats *samplerates = NULL;
  320. int ret;
  321. switch (ctx->outputs[0]->type) {
  322. case AVMEDIA_TYPE_VIDEO:
  323. if ((ret = ff_add_format (&formats, c->pix_fmt)) < 0 ||
  324. (ret = ff_set_common_formats (ctx , formats )) < 0)
  325. return ret;
  326. break;
  327. case AVMEDIA_TYPE_AUDIO:
  328. if ((ret = ff_add_format (&formats , c->sample_fmt )) < 0 ||
  329. (ret = ff_set_common_formats (ctx , formats )) < 0 ||
  330. (ret = ff_add_format (&samplerates, c->sample_rate)) < 0 ||
  331. (ret = ff_set_common_samplerates (ctx , samplerates )) < 0)
  332. return ret;
  333. if ((ret = ff_add_channel_layout(&channel_layouts,
  334. c->channel_layout ? c->channel_layout :
  335. FF_COUNT2LAYOUT(c->channels))) < 0)
  336. return ret;
  337. if ((ret = ff_set_common_channel_layouts(ctx, channel_layouts)) < 0)
  338. return ret;
  339. break;
  340. default:
  341. return AVERROR(EINVAL);
  342. }
  343. return 0;
  344. }
  345. static int config_props(AVFilterLink *link)
  346. {
  347. BufferSourceContext *c = link->src->priv;
  348. switch (link->type) {
  349. case AVMEDIA_TYPE_VIDEO:
  350. link->w = c->w;
  351. link->h = c->h;
  352. link->sample_aspect_ratio = c->pixel_aspect;
  353. if (c->hw_frames_ctx) {
  354. link->hw_frames_ctx = av_buffer_ref(c->hw_frames_ctx);
  355. if (!link->hw_frames_ctx)
  356. return AVERROR(ENOMEM);
  357. }
  358. break;
  359. case AVMEDIA_TYPE_AUDIO:
  360. if (!c->channel_layout)
  361. c->channel_layout = link->channel_layout;
  362. break;
  363. default:
  364. return AVERROR(EINVAL);
  365. }
  366. link->time_base = c->time_base;
  367. link->frame_rate = c->frame_rate;
  368. return 0;
  369. }
  370. static int request_frame(AVFilterLink *link)
  371. {
  372. BufferSourceContext *c = link->src->priv;
  373. AVFrame *frame;
  374. int ret;
  375. if (!av_fifo_size(c->fifo)) {
  376. if (c->eof)
  377. return AVERROR_EOF;
  378. c->nb_failed_requests++;
  379. return AVERROR(EAGAIN);
  380. }
  381. av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
  382. ret = ff_filter_frame(link, frame);
  383. return ret;
  384. }
  385. static int poll_frame(AVFilterLink *link)
  386. {
  387. BufferSourceContext *c = link->src->priv;
  388. int size = av_fifo_size(c->fifo);
  389. if (!size && c->eof)
  390. return AVERROR_EOF;
  391. return size/sizeof(AVFrame*);
  392. }
  393. static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
  394. {
  395. .name = "default",
  396. .type = AVMEDIA_TYPE_VIDEO,
  397. .request_frame = request_frame,
  398. .poll_frame = poll_frame,
  399. .config_props = config_props,
  400. },
  401. { NULL }
  402. };
  403. AVFilter ff_vsrc_buffer = {
  404. .name = "buffer",
  405. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  406. .priv_size = sizeof(BufferSourceContext),
  407. .query_formats = query_formats,
  408. .init = init_video,
  409. .uninit = uninit,
  410. .inputs = NULL,
  411. .outputs = avfilter_vsrc_buffer_outputs,
  412. .priv_class = &buffer_class,
  413. };
  414. static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
  415. {
  416. .name = "default",
  417. .type = AVMEDIA_TYPE_AUDIO,
  418. .request_frame = request_frame,
  419. .poll_frame = poll_frame,
  420. .config_props = config_props,
  421. },
  422. { NULL }
  423. };
  424. AVFilter ff_asrc_abuffer = {
  425. .name = "abuffer",
  426. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
  427. .priv_size = sizeof(BufferSourceContext),
  428. .query_formats = query_formats,
  429. .init = init_audio,
  430. .uninit = uninit,
  431. .inputs = NULL,
  432. .outputs = avfilter_asrc_abuffer_outputs,
  433. .priv_class = &abuffer_class,
  434. };