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.

404 lines
13KB

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