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.

440 lines
15KB

  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. #include "avcodec.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. /* 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 eof;
  59. } BufferSourceContext;
  60. #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format)\
  61. if (c->w != width || c->h != height || c->pix_fmt != format) {\
  62. av_log(s, AV_LOG_INFO, "Changing frame properties on the fly is not supported by all filters.\n");\
  63. }
  64. #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, ch_count, format)\
  65. if (c->sample_fmt != format || c->sample_rate != srate ||\
  66. c->channel_layout != ch_layout || c->channels != ch_count) {\
  67. av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
  68. return AVERROR(EINVAL);\
  69. }
  70. int attribute_align_arg av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
  71. {
  72. return av_buffersrc_add_frame_flags(ctx, (AVFrame *)frame,
  73. AV_BUFFERSRC_FLAG_KEEP_REF);
  74. }
  75. int attribute_align_arg av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame)
  76. {
  77. return av_buffersrc_add_frame_flags(ctx, frame, 0);
  78. }
  79. static int av_buffersrc_add_frame_internal(AVFilterContext *ctx,
  80. AVFrame *frame, int flags);
  81. int attribute_align_arg av_buffersrc_add_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
  82. {
  83. AVFrame *copy = NULL;
  84. int ret = 0;
  85. if (frame && frame->channel_layout &&
  86. av_get_channel_layout_nb_channels(frame->channel_layout) != av_frame_get_channels(frame)) {
  87. av_log(ctx, AV_LOG_ERROR, "Layout indicates a different number of channels than actually present\n");
  88. return AVERROR(EINVAL);
  89. }
  90. if (!(flags & AV_BUFFERSRC_FLAG_KEEP_REF) || !frame)
  91. return av_buffersrc_add_frame_internal(ctx, frame, flags);
  92. if (!(copy = av_frame_alloc()))
  93. return AVERROR(ENOMEM);
  94. ret = av_frame_ref(copy, frame);
  95. if (ret >= 0)
  96. ret = av_buffersrc_add_frame_internal(ctx, copy, flags);
  97. av_frame_free(&copy);
  98. return ret;
  99. }
  100. static int av_buffersrc_add_frame_internal(AVFilterContext *ctx,
  101. AVFrame *frame, int flags)
  102. {
  103. BufferSourceContext *s = ctx->priv;
  104. AVFrame *copy;
  105. int refcounted, ret;
  106. s->nb_failed_requests = 0;
  107. if (!frame) {
  108. s->eof = 1;
  109. return 0;
  110. } else if (s->eof)
  111. return AVERROR(EINVAL);
  112. refcounted = !!frame->buf[0];
  113. if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
  114. switch (ctx->outputs[0]->type) {
  115. case AVMEDIA_TYPE_VIDEO:
  116. CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
  117. frame->format);
  118. break;
  119. case AVMEDIA_TYPE_AUDIO:
  120. /* For layouts unknown on input but known on link after negotiation. */
  121. if (!frame->channel_layout)
  122. frame->channel_layout = s->channel_layout;
  123. CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
  124. av_frame_get_channels(frame), frame->format);
  125. break;
  126. default:
  127. return AVERROR(EINVAL);
  128. }
  129. }
  130. if (!av_fifo_space(s->fifo) &&
  131. (ret = av_fifo_realloc2(s->fifo, av_fifo_size(s->fifo) +
  132. sizeof(copy))) < 0)
  133. return ret;
  134. if (!(copy = av_frame_alloc()))
  135. return AVERROR(ENOMEM);
  136. if (refcounted) {
  137. av_frame_move_ref(copy, frame);
  138. } else {
  139. ret = av_frame_ref(copy, frame);
  140. if (ret < 0) {
  141. av_frame_free(&copy);
  142. return ret;
  143. }
  144. }
  145. if ((ret = av_fifo_generic_write(s->fifo, &copy, sizeof(copy), NULL)) < 0) {
  146. if (refcounted)
  147. av_frame_move_ref(frame, copy);
  148. av_frame_free(&copy);
  149. return ret;
  150. }
  151. if ((flags & AV_BUFFERSRC_FLAG_PUSH))
  152. if ((ret = ctx->output_pads[0].request_frame(ctx->outputs[0])) < 0)
  153. return ret;
  154. return 0;
  155. }
  156. static av_cold int init_video(AVFilterContext *ctx)
  157. {
  158. BufferSourceContext *c = ctx->priv;
  159. if (c->pix_fmt == AV_PIX_FMT_NONE || !c->w || !c->h || av_q2d(c->time_base) <= 0) {
  160. av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n");
  161. return AVERROR(EINVAL);
  162. }
  163. if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*))))
  164. return AVERROR(ENOMEM);
  165. 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",
  166. c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
  167. c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
  168. c->pixel_aspect.num, c->pixel_aspect.den, (char *)av_x_if_null(c->sws_param, ""));
  169. c->warning_limit = 100;
  170. return 0;
  171. }
  172. unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
  173. {
  174. return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
  175. }
  176. #define OFFSET(x) offsetof(BufferSourceContext, x)
  177. #define A AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  178. #define V AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  179. static const AVOption buffer_options[] = {
  180. { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  181. { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = V },
  182. { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  183. { "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 },
  184. #if FF_API_OLD_FILTER_OPTS
  185. /* those 4 are for compatibility with the old option passing system where each filter
  186. * did its own parsing */
  187. { "time_base_num", "deprecated, do not use", OFFSET(time_base.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  188. { "time_base_den", "deprecated, do not use", OFFSET(time_base.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  189. { "sar_num", "deprecated, do not use", OFFSET(pixel_aspect.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  190. { "sar_den", "deprecated, do not use", OFFSET(pixel_aspect.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  191. #endif
  192. { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V },
  193. { "pixel_aspect", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V },
  194. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  195. { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  196. { "sws_param", NULL, OFFSET(sws_param), AV_OPT_TYPE_STRING, .flags = V },
  197. { NULL },
  198. };
  199. AVFILTER_DEFINE_CLASS(buffer);
  200. static const AVOption abuffer_options[] = {
  201. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
  202. { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
  203. { "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 },
  204. { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
  205. { "channels", NULL, OFFSET(channels), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
  206. { NULL },
  207. };
  208. AVFILTER_DEFINE_CLASS(abuffer);
  209. static av_cold int init_audio(AVFilterContext *ctx)
  210. {
  211. BufferSourceContext *s = ctx->priv;
  212. int ret = 0;
  213. if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
  214. av_log(ctx, AV_LOG_ERROR, "Sample format was not set or was invalid\n");
  215. return AVERROR(EINVAL);
  216. }
  217. if (s->channel_layout_str) {
  218. int n;
  219. s->channel_layout = av_get_channel_layout(s->channel_layout_str);
  220. if (!s->channel_layout) {
  221. av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
  222. s->channel_layout_str);
  223. return AVERROR(EINVAL);
  224. }
  225. n = av_get_channel_layout_nb_channels(s->channel_layout);
  226. if (s->channels) {
  227. if (n != s->channels) {
  228. av_log(ctx, AV_LOG_ERROR,
  229. "Mismatching channel count %d and layout '%s' "
  230. "(%d channels)\n",
  231. s->channels, s->channel_layout_str, n);
  232. return AVERROR(EINVAL);
  233. }
  234. }
  235. s->channels = n;
  236. } else if (!s->channels) {
  237. av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor "
  238. "channel layout specified\n");
  239. return AVERROR(EINVAL);
  240. }
  241. if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*))))
  242. return AVERROR(ENOMEM);
  243. if (!s->time_base.num)
  244. s->time_base = (AVRational){1, s->sample_rate};
  245. av_log(ctx, AV_LOG_VERBOSE,
  246. "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
  247. s->time_base.num, s->time_base.den, av_get_sample_fmt_name(s->sample_fmt),
  248. s->sample_rate, s->channel_layout_str);
  249. s->warning_limit = 100;
  250. return ret;
  251. }
  252. static av_cold void uninit(AVFilterContext *ctx)
  253. {
  254. BufferSourceContext *s = ctx->priv;
  255. while (s->fifo && av_fifo_size(s->fifo)) {
  256. AVFrame *frame;
  257. av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
  258. av_frame_free(&frame);
  259. }
  260. av_fifo_freep(&s->fifo);
  261. }
  262. static int query_formats(AVFilterContext *ctx)
  263. {
  264. BufferSourceContext *c = ctx->priv;
  265. AVFilterChannelLayouts *channel_layouts = NULL;
  266. AVFilterFormats *formats = NULL;
  267. AVFilterFormats *samplerates = NULL;
  268. switch (ctx->outputs[0]->type) {
  269. case AVMEDIA_TYPE_VIDEO:
  270. ff_add_format(&formats, c->pix_fmt);
  271. ff_set_common_formats(ctx, formats);
  272. break;
  273. case AVMEDIA_TYPE_AUDIO:
  274. ff_add_format(&formats, c->sample_fmt);
  275. ff_set_common_formats(ctx, formats);
  276. ff_add_format(&samplerates, c->sample_rate);
  277. ff_set_common_samplerates(ctx, samplerates);
  278. ff_add_channel_layout(&channel_layouts,
  279. c->channel_layout ? c->channel_layout :
  280. FF_COUNT2LAYOUT(c->channels));
  281. ff_set_common_channel_layouts(ctx, channel_layouts);
  282. break;
  283. default:
  284. return AVERROR(EINVAL);
  285. }
  286. return 0;
  287. }
  288. static int config_props(AVFilterLink *link)
  289. {
  290. BufferSourceContext *c = link->src->priv;
  291. switch (link->type) {
  292. case AVMEDIA_TYPE_VIDEO:
  293. link->w = c->w;
  294. link->h = c->h;
  295. link->sample_aspect_ratio = c->pixel_aspect;
  296. break;
  297. case AVMEDIA_TYPE_AUDIO:
  298. if (!c->channel_layout)
  299. c->channel_layout = link->channel_layout;
  300. break;
  301. default:
  302. return AVERROR(EINVAL);
  303. }
  304. link->time_base = c->time_base;
  305. link->frame_rate = c->frame_rate;
  306. return 0;
  307. }
  308. static int request_frame(AVFilterLink *link)
  309. {
  310. BufferSourceContext *c = link->src->priv;
  311. AVFrame *frame;
  312. if (!av_fifo_size(c->fifo)) {
  313. if (c->eof)
  314. return AVERROR_EOF;
  315. c->nb_failed_requests++;
  316. return AVERROR(EAGAIN);
  317. }
  318. av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
  319. return ff_filter_frame(link, frame);
  320. }
  321. static int poll_frame(AVFilterLink *link)
  322. {
  323. BufferSourceContext *c = link->src->priv;
  324. int size = av_fifo_size(c->fifo);
  325. if (!size && c->eof)
  326. return AVERROR_EOF;
  327. return size/sizeof(AVFrame*);
  328. }
  329. static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
  330. {
  331. .name = "default",
  332. .type = AVMEDIA_TYPE_VIDEO,
  333. .request_frame = request_frame,
  334. .poll_frame = poll_frame,
  335. .config_props = config_props,
  336. },
  337. { NULL }
  338. };
  339. AVFilter ff_vsrc_buffer = {
  340. .name = "buffer",
  341. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  342. .priv_size = sizeof(BufferSourceContext),
  343. .query_formats = query_formats,
  344. .init = init_video,
  345. .uninit = uninit,
  346. .inputs = NULL,
  347. .outputs = avfilter_vsrc_buffer_outputs,
  348. .priv_class = &buffer_class,
  349. };
  350. static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
  351. {
  352. .name = "default",
  353. .type = AVMEDIA_TYPE_AUDIO,
  354. .request_frame = request_frame,
  355. .poll_frame = poll_frame,
  356. .config_props = config_props,
  357. },
  358. { NULL }
  359. };
  360. AVFilter ff_asrc_abuffer = {
  361. .name = "abuffer",
  362. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
  363. .priv_size = sizeof(BufferSourceContext),
  364. .query_formats = query_formats,
  365. .init = init_audio,
  366. .uninit = uninit,
  367. .inputs = NULL,
  368. .outputs = avfilter_asrc_abuffer_outputs,
  369. .priv_class = &abuffer_class,
  370. };