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.

386 lines
12KB

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