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.

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