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.

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