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.

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