buffersrc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 "audio.h"
  25. #include "avfilter.h"
  26. #include "buffersrc.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. #include "avcodec.h"
  31. #include "libavutil/audioconvert.h"
  32. #include "libavutil/common.h"
  33. #include "libavutil/fifo.h"
  34. #include "libavutil/imgutils.h"
  35. #include "libavutil/opt.h"
  36. #include "libavutil/samplefmt.h"
  37. typedef struct {
  38. const AVClass *class;
  39. AVFifoBuffer *fifo;
  40. AVRational time_base; ///< time_base to set in the output link
  41. AVRational frame_rate; ///< frame_rate to set in the output link
  42. unsigned nb_failed_requests;
  43. unsigned warning_limit;
  44. /* video only */
  45. int w, h;
  46. enum PixelFormat pix_fmt;
  47. AVRational pixel_aspect;
  48. char *sws_param;
  49. /* audio only */
  50. int sample_rate;
  51. enum AVSampleFormat sample_fmt;
  52. char *sample_fmt_str;
  53. uint64_t channel_layout;
  54. char *channel_layout_str;
  55. int eof;
  56. } BufferSourceContext;
  57. #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format)\
  58. if (c->w != width || c->h != height || c->pix_fmt != format) {\
  59. av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
  60. return AVERROR(EINVAL);\
  61. }
  62. #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, format)\
  63. if (c->sample_fmt != format || c->sample_rate != srate ||\
  64. c->channel_layout != ch_layout) {\
  65. av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
  66. return AVERROR(EINVAL);\
  67. }
  68. int av_buffersrc_add_frame(AVFilterContext *buffer_src,
  69. const AVFrame *frame, int flags)
  70. {
  71. AVFilterBufferRef *picref;
  72. int ret;
  73. if (!frame) /* NULL for EOF */
  74. return av_buffersrc_add_ref(buffer_src, NULL, flags);
  75. picref = avfilter_get_buffer_ref_from_frame(buffer_src->outputs[0]->type,
  76. frame, AV_PERM_WRITE);
  77. if (!picref)
  78. return AVERROR(ENOMEM);
  79. ret = av_buffersrc_add_ref(buffer_src, picref, flags);
  80. picref->buf->data[0] = NULL;
  81. avfilter_unref_buffer(picref);
  82. return ret;
  83. }
  84. int av_buffersrc_write_frame(AVFilterContext *buffer_filter, const AVFrame *frame)
  85. {
  86. return av_buffersrc_add_frame(buffer_filter, frame, 0);
  87. }
  88. int av_buffersrc_add_ref(AVFilterContext *s, AVFilterBufferRef *buf, int flags)
  89. {
  90. BufferSourceContext *c = s->priv;
  91. AVFilterBufferRef *to_free = NULL;
  92. int ret;
  93. if (!buf) {
  94. c->eof = 1;
  95. return 0;
  96. } else if (c->eof)
  97. return AVERROR(EINVAL);
  98. if (!av_fifo_space(c->fifo) &&
  99. (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
  100. sizeof(buf))) < 0)
  101. return ret;
  102. if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
  103. switch (s->outputs[0]->type) {
  104. case AVMEDIA_TYPE_VIDEO:
  105. CHECK_VIDEO_PARAM_CHANGE(s, c, buf->video->w, buf->video->h, buf->format);
  106. break;
  107. case AVMEDIA_TYPE_AUDIO:
  108. CHECK_AUDIO_PARAM_CHANGE(s, c, buf->audio->sample_rate, buf->audio->channel_layout,
  109. buf->format);
  110. break;
  111. default:
  112. return AVERROR(EINVAL);
  113. }
  114. }
  115. if (!(flags & AV_BUFFERSRC_FLAG_NO_COPY))
  116. to_free = buf = ff_copy_buffer_ref(s->outputs[0], buf);
  117. if(!buf)
  118. return -1;
  119. if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0) {
  120. avfilter_unref_buffer(to_free);
  121. return ret;
  122. }
  123. c->nb_failed_requests = 0;
  124. if (c->warning_limit &&
  125. av_fifo_size(c->fifo) / sizeof(buf) >= c->warning_limit) {
  126. av_log(s, AV_LOG_WARNING,
  127. "%d buffers queued in %s, something may be wrong.\n",
  128. c->warning_limit,
  129. (char *)av_x_if_null(s->name, s->filter->name));
  130. c->warning_limit *= 10;
  131. }
  132. if ((flags & AV_BUFFERSRC_FLAG_PUSH))
  133. if ((ret = s->output_pads[0].request_frame(s->outputs[0])) < 0)
  134. return ret;
  135. return 0;
  136. }
  137. #ifdef FF_API_BUFFERSRC_BUFFER
  138. int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
  139. {
  140. return av_buffersrc_add_ref(s, buf, AV_BUFFERSRC_FLAG_NO_COPY);
  141. }
  142. #endif
  143. unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
  144. {
  145. return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
  146. }
  147. #define OFFSET(x) offsetof(BufferSourceContext, x)
  148. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  149. static const AVOption buffer_options[] = {
  150. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  151. { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  152. { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = FLAGS },
  153. { "pix_fmt", NULL, OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, .flags = FLAGS },
  154. { "pixel_aspect", NULL, OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  155. { "sws_param", NULL, OFFSET(sws_param), AV_OPT_TYPE_STRING, .flags = FLAGS },
  156. { NULL },
  157. };
  158. #undef FLAGS
  159. AVFILTER_DEFINE_CLASS(buffer);
  160. static av_cold int init_video(AVFilterContext *ctx, const char *args)
  161. {
  162. BufferSourceContext *c = ctx->priv;
  163. char pix_fmt_str[128], sws_param[256] = "", *colon, *equal;
  164. int ret, n = 0;
  165. c->class = &buffer_class;
  166. if (!args) {
  167. av_log(ctx, AV_LOG_ERROR, "Arguments required\n");
  168. return AVERROR(EINVAL);
  169. }
  170. colon = strchr(args, ':');
  171. equal = strchr(args, '=');
  172. if (equal && (!colon || equal < colon)) {
  173. av_opt_set_defaults(c);
  174. ret = av_set_options_string(c, args, "=", ":");
  175. if (ret < 0)
  176. goto fail;
  177. } else {
  178. if ((n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
  179. &c->time_base.num, &c->time_base.den,
  180. &c->pixel_aspect.num, &c->pixel_aspect.den, sws_param)) < 7) {
  181. av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args);
  182. ret = AVERROR(EINVAL);
  183. goto fail;
  184. }
  185. av_log(ctx, AV_LOG_WARNING, "Flat options syntax is deprecated, use key=value pairs\n");
  186. if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
  187. goto fail;
  188. c->sws_param = av_strdup(sws_param);
  189. if (!c->sws_param) {
  190. ret = AVERROR(ENOMEM);
  191. goto fail;
  192. }
  193. }
  194. if (!(c->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*)))) {
  195. ret = AVERROR(ENOMEM);
  196. goto fail;
  197. }
  198. 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",
  199. c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
  200. c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
  201. c->pixel_aspect.num, c->pixel_aspect.den, (char *)av_x_if_null(c->sws_param, ""));
  202. c->warning_limit = 100;
  203. return 0;
  204. fail:
  205. av_opt_free(c);
  206. return ret;
  207. }
  208. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  209. static const AVOption abuffer_options[] = {
  210. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  211. { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  212. { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = FLAGS },
  213. { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = FLAGS },
  214. { NULL },
  215. };
  216. AVFILTER_DEFINE_CLASS(abuffer);
  217. static av_cold int init_audio(AVFilterContext *ctx, const char *args)
  218. {
  219. BufferSourceContext *s = ctx->priv;
  220. int ret = 0;
  221. s->class = &abuffer_class;
  222. av_opt_set_defaults(s);
  223. if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
  224. goto fail;
  225. s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
  226. if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
  227. av_log(ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n",
  228. s->sample_fmt_str);
  229. ret = AVERROR(EINVAL);
  230. goto fail;
  231. }
  232. s->channel_layout = av_get_channel_layout(s->channel_layout_str);
  233. if (!s->channel_layout) {
  234. av_log(ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n",
  235. s->channel_layout_str);
  236. ret = AVERROR(EINVAL);
  237. goto fail;
  238. }
  239. if (!(s->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*)))) {
  240. ret = AVERROR(ENOMEM);
  241. goto fail;
  242. }
  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, s->sample_fmt_str,
  248. s->sample_rate, s->channel_layout_str);
  249. s->warning_limit = 100;
  250. fail:
  251. av_opt_free(s);
  252. return ret;
  253. }
  254. static av_cold void uninit(AVFilterContext *ctx)
  255. {
  256. BufferSourceContext *s = ctx->priv;
  257. while (s->fifo && av_fifo_size(s->fifo)) {
  258. AVFilterBufferRef *buf;
  259. av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
  260. avfilter_unref_buffer(buf);
  261. }
  262. av_fifo_free(s->fifo);
  263. s->fifo = NULL;
  264. av_freep(&s->sws_param);
  265. }
  266. static int query_formats(AVFilterContext *ctx)
  267. {
  268. BufferSourceContext *c = ctx->priv;
  269. AVFilterChannelLayouts *channel_layouts = NULL;
  270. AVFilterFormats *formats = NULL;
  271. AVFilterFormats *samplerates = NULL;
  272. switch (ctx->outputs[0]->type) {
  273. case AVMEDIA_TYPE_VIDEO:
  274. ff_add_format(&formats, c->pix_fmt);
  275. ff_set_common_formats(ctx, formats);
  276. break;
  277. case AVMEDIA_TYPE_AUDIO:
  278. ff_add_format(&formats, c->sample_fmt);
  279. ff_set_common_formats(ctx, formats);
  280. ff_add_format(&samplerates, c->sample_rate);
  281. ff_set_common_samplerates(ctx, samplerates);
  282. ff_add_channel_layout(&channel_layouts, c->channel_layout);
  283. ff_set_common_channel_layouts(ctx, channel_layouts);
  284. break;
  285. default:
  286. return AVERROR(EINVAL);
  287. }
  288. return 0;
  289. }
  290. static int config_props(AVFilterLink *link)
  291. {
  292. BufferSourceContext *c = link->src->priv;
  293. switch (link->type) {
  294. case AVMEDIA_TYPE_VIDEO:
  295. link->w = c->w;
  296. link->h = c->h;
  297. link->sample_aspect_ratio = c->pixel_aspect;
  298. break;
  299. case AVMEDIA_TYPE_AUDIO:
  300. link->channel_layout = c->channel_layout;
  301. link->sample_rate = c->sample_rate;
  302. break;
  303. default:
  304. return AVERROR(EINVAL);
  305. }
  306. link->time_base = c->time_base;
  307. link->frame_rate = c->frame_rate;
  308. return 0;
  309. }
  310. static int request_frame(AVFilterLink *link)
  311. {
  312. BufferSourceContext *c = link->src->priv;
  313. AVFilterBufferRef *buf;
  314. int ret = 0;
  315. if (!av_fifo_size(c->fifo)) {
  316. if (c->eof)
  317. return AVERROR_EOF;
  318. c->nb_failed_requests++;
  319. return AVERROR(EAGAIN);
  320. }
  321. av_fifo_generic_read(c->fifo, &buf, sizeof(buf), NULL);
  322. switch (link->type) {
  323. case AVMEDIA_TYPE_VIDEO:
  324. if ((ret = ff_start_frame(link, buf)) < 0 ||
  325. (ret = ff_draw_slice(link, 0, link->h, 1)) < 0 ||
  326. (ret = ff_end_frame(link)) < 0)
  327. return ret;
  328. break;
  329. case AVMEDIA_TYPE_AUDIO:
  330. ret = ff_filter_samples(link, buf);
  331. break;
  332. default:
  333. avfilter_unref_bufferp(&buf);
  334. return AVERROR(EINVAL);
  335. }
  336. return ret;
  337. }
  338. static int poll_frame(AVFilterLink *link)
  339. {
  340. BufferSourceContext *c = link->src->priv;
  341. int size = av_fifo_size(c->fifo);
  342. if (!size && c->eof)
  343. return AVERROR_EOF;
  344. return size/sizeof(AVFilterBufferRef*);
  345. }
  346. AVFilter avfilter_vsrc_buffer = {
  347. .name = "buffer",
  348. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  349. .priv_size = sizeof(BufferSourceContext),
  350. .query_formats = query_formats,
  351. .init = init_video,
  352. .uninit = uninit,
  353. .inputs = NULL,
  354. .outputs = (const AVFilterPad[]) {{ .name = "default",
  355. .type = AVMEDIA_TYPE_VIDEO,
  356. .request_frame = request_frame,
  357. .poll_frame = poll_frame,
  358. .config_props = config_props, },
  359. { .name = NULL}},
  360. .priv_class = &buffer_class,
  361. };
  362. AVFilter avfilter_asrc_abuffer = {
  363. .name = "abuffer",
  364. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
  365. .priv_size = sizeof(BufferSourceContext),
  366. .query_formats = query_formats,
  367. .init = init_audio,
  368. .uninit = uninit,
  369. .inputs = NULL,
  370. .outputs = (const AVFilterPad[]) {{ .name = "default",
  371. .type = AVMEDIA_TYPE_AUDIO,
  372. .request_frame = request_frame,
  373. .poll_frame = poll_frame,
  374. .config_props = config_props, },
  375. { .name = NULL}},
  376. .priv_class = &abuffer_class,
  377. };