buffersrc.c 14 KB

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