buffersrc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 || s->channel_layout) {
  269. int n;
  270. if (!s->channel_layout) {
  271. s->channel_layout = av_get_channel_layout(s->channel_layout_str);
  272. if (!s->channel_layout) {
  273. av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
  274. s->channel_layout_str);
  275. return AVERROR(EINVAL);
  276. }
  277. }
  278. n = av_get_channel_layout_nb_channels(s->channel_layout);
  279. if (s->channels) {
  280. if (n != s->channels) {
  281. av_log(ctx, AV_LOG_ERROR,
  282. "Mismatching channel count %d and layout '%s' "
  283. "(%d channels)\n",
  284. s->channels, s->channel_layout_str, n);
  285. return AVERROR(EINVAL);
  286. }
  287. }
  288. s->channels = n;
  289. } else if (!s->channels) {
  290. av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor "
  291. "channel layout specified\n");
  292. return AVERROR(EINVAL);
  293. }
  294. if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*))))
  295. return AVERROR(ENOMEM);
  296. if (!s->time_base.num)
  297. s->time_base = (AVRational){1, s->sample_rate};
  298. av_log(ctx, AV_LOG_VERBOSE,
  299. "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
  300. s->time_base.num, s->time_base.den, av_get_sample_fmt_name(s->sample_fmt),
  301. s->sample_rate, s->channel_layout_str);
  302. s->warning_limit = 100;
  303. return ret;
  304. }
  305. static av_cold void uninit(AVFilterContext *ctx)
  306. {
  307. BufferSourceContext *s = ctx->priv;
  308. while (s->fifo && av_fifo_size(s->fifo)) {
  309. AVFrame *frame;
  310. av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
  311. av_frame_free(&frame);
  312. }
  313. av_buffer_unref(&s->hw_frames_ctx);
  314. av_fifo_freep(&s->fifo);
  315. }
  316. static int query_formats(AVFilterContext *ctx)
  317. {
  318. BufferSourceContext *c = ctx->priv;
  319. AVFilterChannelLayouts *channel_layouts = NULL;
  320. AVFilterFormats *formats = NULL;
  321. AVFilterFormats *samplerates = NULL;
  322. int ret;
  323. switch (ctx->outputs[0]->type) {
  324. case AVMEDIA_TYPE_VIDEO:
  325. if ((ret = ff_add_format (&formats, c->pix_fmt)) < 0 ||
  326. (ret = ff_set_common_formats (ctx , formats )) < 0)
  327. return ret;
  328. break;
  329. case AVMEDIA_TYPE_AUDIO:
  330. if ((ret = ff_add_format (&formats , c->sample_fmt )) < 0 ||
  331. (ret = ff_set_common_formats (ctx , formats )) < 0 ||
  332. (ret = ff_add_format (&samplerates, c->sample_rate)) < 0 ||
  333. (ret = ff_set_common_samplerates (ctx , samplerates )) < 0)
  334. return ret;
  335. if ((ret = ff_add_channel_layout(&channel_layouts,
  336. c->channel_layout ? c->channel_layout :
  337. FF_COUNT2LAYOUT(c->channels))) < 0)
  338. return ret;
  339. if ((ret = ff_set_common_channel_layouts(ctx, channel_layouts)) < 0)
  340. return ret;
  341. break;
  342. default:
  343. return AVERROR(EINVAL);
  344. }
  345. return 0;
  346. }
  347. static int config_props(AVFilterLink *link)
  348. {
  349. BufferSourceContext *c = link->src->priv;
  350. switch (link->type) {
  351. case AVMEDIA_TYPE_VIDEO:
  352. link->w = c->w;
  353. link->h = c->h;
  354. link->sample_aspect_ratio = c->pixel_aspect;
  355. if (c->hw_frames_ctx) {
  356. link->hw_frames_ctx = av_buffer_ref(c->hw_frames_ctx);
  357. if (!link->hw_frames_ctx)
  358. return AVERROR(ENOMEM);
  359. }
  360. break;
  361. case AVMEDIA_TYPE_AUDIO:
  362. if (!c->channel_layout)
  363. c->channel_layout = link->channel_layout;
  364. break;
  365. default:
  366. return AVERROR(EINVAL);
  367. }
  368. link->time_base = c->time_base;
  369. link->frame_rate = c->frame_rate;
  370. return 0;
  371. }
  372. static int request_frame(AVFilterLink *link)
  373. {
  374. BufferSourceContext *c = link->src->priv;
  375. AVFrame *frame;
  376. int ret;
  377. if (!av_fifo_size(c->fifo)) {
  378. if (c->eof)
  379. return AVERROR_EOF;
  380. c->nb_failed_requests++;
  381. return AVERROR(EAGAIN);
  382. }
  383. av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
  384. ret = ff_filter_frame(link, frame);
  385. return ret;
  386. }
  387. static int poll_frame(AVFilterLink *link)
  388. {
  389. BufferSourceContext *c = link->src->priv;
  390. int size = av_fifo_size(c->fifo);
  391. if (!size && c->eof)
  392. return AVERROR_EOF;
  393. return size/sizeof(AVFrame*);
  394. }
  395. static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
  396. {
  397. .name = "default",
  398. .type = AVMEDIA_TYPE_VIDEO,
  399. .request_frame = request_frame,
  400. .poll_frame = poll_frame,
  401. .config_props = config_props,
  402. },
  403. { NULL }
  404. };
  405. AVFilter ff_vsrc_buffer = {
  406. .name = "buffer",
  407. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  408. .priv_size = sizeof(BufferSourceContext),
  409. .query_formats = query_formats,
  410. .init = init_video,
  411. .uninit = uninit,
  412. .inputs = NULL,
  413. .outputs = avfilter_vsrc_buffer_outputs,
  414. .priv_class = &buffer_class,
  415. };
  416. static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
  417. {
  418. .name = "default",
  419. .type = AVMEDIA_TYPE_AUDIO,
  420. .request_frame = request_frame,
  421. .poll_frame = poll_frame,
  422. .config_props = config_props,
  423. },
  424. { NULL }
  425. };
  426. AVFilter ff_asrc_abuffer = {
  427. .name = "abuffer",
  428. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
  429. .priv_size = sizeof(BufferSourceContext),
  430. .query_formats = query_formats,
  431. .init = init_audio,
  432. .uninit = uninit,
  433. .inputs = NULL,
  434. .outputs = avfilter_asrc_abuffer_outputs,
  435. .priv_class = &abuffer_class,
  436. };