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.

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