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.

387 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 <float.h>
  25. #include "libavutil/channel_layout.h"
  26. #include "libavutil/common.h"
  27. #include "libavutil/fifo.h"
  28. #include "libavutil/frame.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/internal.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/samplefmt.h"
  33. #include "audio.h"
  34. #include "avfilter.h"
  35. #include "buffersrc.h"
  36. #include "formats.h"
  37. #include "internal.h"
  38. #include "video.h"
  39. typedef struct BufferSourceContext {
  40. const AVClass *class;
  41. AVFifoBuffer *fifo;
  42. AVRational time_base; ///< time_base to set in the output link
  43. /* video only */
  44. int h, w;
  45. enum AVPixelFormat pix_fmt;
  46. char *pix_fmt_str;
  47. AVRational pixel_aspect;
  48. /* audio only */
  49. int sample_rate;
  50. enum AVSampleFormat sample_fmt;
  51. char *sample_fmt_str;
  52. uint64_t channel_layout;
  53. char *channel_layout_str;
  54. int eof;
  55. } BufferSourceContext;
  56. #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format)\
  57. if (c->w != width || c->h != height || c->pix_fmt != format) {\
  58. av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
  59. return AVERROR(EINVAL);\
  60. }
  61. #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, format)\
  62. if (c->sample_fmt != format || c->sample_rate != srate ||\
  63. c->channel_layout != ch_layout) {\
  64. av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
  65. return AVERROR(EINVAL);\
  66. }
  67. int attribute_align_arg av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
  68. {
  69. AVFrame *copy;
  70. int ret = 0;
  71. if (!(copy = av_frame_alloc()))
  72. return AVERROR(ENOMEM);
  73. ret = av_frame_ref(copy, frame);
  74. if (ret >= 0)
  75. ret = av_buffersrc_add_frame(ctx, copy);
  76. av_frame_free(&copy);
  77. return ret;
  78. }
  79. int attribute_align_arg av_buffersrc_add_frame(AVFilterContext *ctx,
  80. AVFrame *frame)
  81. {
  82. BufferSourceContext *s = ctx->priv;
  83. AVFrame *copy;
  84. int refcounted, ret;
  85. if (!frame) {
  86. s->eof = 1;
  87. return 0;
  88. } else if (s->eof)
  89. return AVERROR(EINVAL);
  90. refcounted = !!frame->buf[0];
  91. switch (ctx->outputs[0]->type) {
  92. case AVMEDIA_TYPE_VIDEO:
  93. CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
  94. frame->format);
  95. break;
  96. case AVMEDIA_TYPE_AUDIO:
  97. CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
  98. frame->format);
  99. break;
  100. default:
  101. return AVERROR(EINVAL);
  102. }
  103. if (!av_fifo_space(s->fifo) &&
  104. (ret = av_fifo_realloc2(s->fifo, av_fifo_size(s->fifo) +
  105. sizeof(copy))) < 0)
  106. return ret;
  107. if (!(copy = av_frame_alloc()))
  108. return AVERROR(ENOMEM);
  109. if (refcounted) {
  110. av_frame_move_ref(copy, frame);
  111. } else {
  112. ret = av_frame_ref(copy, frame);
  113. if (ret < 0) {
  114. av_frame_free(&copy);
  115. return ret;
  116. }
  117. }
  118. if ((ret = av_fifo_generic_write(s->fifo, &copy, sizeof(copy), NULL)) < 0) {
  119. if (refcounted)
  120. av_frame_move_ref(frame, copy);
  121. av_frame_free(&copy);
  122. return ret;
  123. }
  124. return 0;
  125. }
  126. static av_cold int init_video(AVFilterContext *ctx)
  127. {
  128. BufferSourceContext *c = ctx->priv;
  129. if (!c->pix_fmt_str || !c->w || !c->h || av_q2d(c->time_base) <= 0) {
  130. av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n");
  131. return AVERROR(EINVAL);
  132. }
  133. if ((c->pix_fmt = av_get_pix_fmt(c->pix_fmt_str)) == AV_PIX_FMT_NONE) {
  134. char *tail;
  135. c->pix_fmt = strtol(c->pix_fmt_str, &tail, 10);
  136. if (*tail || c->pix_fmt < 0 || !av_pix_fmt_desc_get(c->pix_fmt)) {
  137. av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", c->pix_fmt_str);
  138. return AVERROR(EINVAL);
  139. }
  140. }
  141. if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*))))
  142. return AVERROR(ENOMEM);
  143. 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));
  144. return 0;
  145. }
  146. #define OFFSET(x) offsetof(BufferSourceContext, x)
  147. #define A AV_OPT_FLAG_AUDIO_PARAM
  148. #define V AV_OPT_FLAG_VIDEO_PARAM
  149. static const AVOption video_options[] = {
  150. { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  151. { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  152. { "pix_fmt", NULL, OFFSET(pix_fmt_str), AV_OPT_TYPE_STRING, .flags = V },
  153. #if FF_API_OLD_FILTER_OPTS
  154. /* those 4 are for compatibility with the old option passing system where each filter
  155. * did its own parsing */
  156. { "time_base_num", "deprecated, do not use", OFFSET(time_base.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  157. { "time_base_den", "deprecated, do not use", OFFSET(time_base.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  158. { "sar_num", "deprecated, do not use", OFFSET(pixel_aspect.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  159. { "sar_den", "deprecated, do not use", OFFSET(pixel_aspect.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  160. #endif
  161. { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V },
  162. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  163. { NULL },
  164. };
  165. static const AVClass buffer_class = {
  166. .class_name = "buffer source",
  167. .item_name = av_default_item_name,
  168. .option = video_options,
  169. .version = LIBAVUTIL_VERSION_INT,
  170. };
  171. static const AVOption audio_options[] = {
  172. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
  173. { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
  174. { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = A },
  175. { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
  176. { NULL },
  177. };
  178. static const AVClass abuffer_class = {
  179. .class_name = "abuffer source",
  180. .item_name = av_default_item_name,
  181. .option = audio_options,
  182. .version = LIBAVUTIL_VERSION_INT,
  183. };
  184. static av_cold int init_audio(AVFilterContext *ctx)
  185. {
  186. BufferSourceContext *s = ctx->priv;
  187. int ret = 0;
  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. return AVERROR(EINVAL);
  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. return AVERROR(EINVAL);
  199. }
  200. if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*))))
  201. return AVERROR(ENOMEM);
  202. if (!s->time_base.num)
  203. s->time_base = (AVRational){1, s->sample_rate};
  204. av_log(ctx, AV_LOG_VERBOSE, "tb:%d/%d samplefmt:%s samplerate: %d "
  205. "ch layout:%s\n", s->time_base.num, s->time_base.den, s->sample_fmt_str,
  206. s->sample_rate, s->channel_layout_str);
  207. return ret;
  208. }
  209. static av_cold void uninit(AVFilterContext *ctx)
  210. {
  211. BufferSourceContext *s = ctx->priv;
  212. while (s->fifo && av_fifo_size(s->fifo)) {
  213. AVFrame *frame;
  214. av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
  215. av_frame_free(&frame);
  216. }
  217. av_fifo_free(s->fifo);
  218. s->fifo = NULL;
  219. }
  220. static int query_formats(AVFilterContext *ctx)
  221. {
  222. BufferSourceContext *c = ctx->priv;
  223. AVFilterChannelLayouts *channel_layouts = NULL;
  224. AVFilterFormats *formats = NULL;
  225. AVFilterFormats *samplerates = NULL;
  226. switch (ctx->outputs[0]->type) {
  227. case AVMEDIA_TYPE_VIDEO:
  228. ff_add_format(&formats, c->pix_fmt);
  229. ff_set_common_formats(ctx, formats);
  230. break;
  231. case AVMEDIA_TYPE_AUDIO:
  232. ff_add_format(&formats, c->sample_fmt);
  233. ff_set_common_formats(ctx, formats);
  234. ff_add_format(&samplerates, c->sample_rate);
  235. ff_set_common_samplerates(ctx, samplerates);
  236. ff_add_channel_layout(&channel_layouts, c->channel_layout);
  237. ff_set_common_channel_layouts(ctx, channel_layouts);
  238. break;
  239. default:
  240. return AVERROR(EINVAL);
  241. }
  242. return 0;
  243. }
  244. static int config_props(AVFilterLink *link)
  245. {
  246. BufferSourceContext *c = link->src->priv;
  247. switch (link->type) {
  248. case AVMEDIA_TYPE_VIDEO:
  249. link->w = c->w;
  250. link->h = c->h;
  251. link->sample_aspect_ratio = c->pixel_aspect;
  252. break;
  253. case AVMEDIA_TYPE_AUDIO:
  254. link->channel_layout = c->channel_layout;
  255. link->sample_rate = c->sample_rate;
  256. break;
  257. default:
  258. return AVERROR(EINVAL);
  259. }
  260. link->time_base = c->time_base;
  261. return 0;
  262. }
  263. static int request_frame(AVFilterLink *link)
  264. {
  265. BufferSourceContext *c = link->src->priv;
  266. AVFrame *frame;
  267. int ret = 0;
  268. if (!av_fifo_size(c->fifo)) {
  269. if (c->eof)
  270. return AVERROR_EOF;
  271. return AVERROR(EAGAIN);
  272. }
  273. av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
  274. ff_filter_frame(link, frame);
  275. return ret;
  276. }
  277. static int poll_frame(AVFilterLink *link)
  278. {
  279. BufferSourceContext *c = link->src->priv;
  280. int size = av_fifo_size(c->fifo);
  281. if (!size && c->eof)
  282. return AVERROR_EOF;
  283. return size/sizeof(AVFrame*);
  284. }
  285. static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
  286. {
  287. .name = "default",
  288. .type = AVMEDIA_TYPE_VIDEO,
  289. .request_frame = request_frame,
  290. .poll_frame = poll_frame,
  291. .config_props = config_props,
  292. },
  293. { NULL }
  294. };
  295. AVFilter ff_vsrc_buffer = {
  296. .name = "buffer",
  297. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  298. .priv_size = sizeof(BufferSourceContext),
  299. .priv_class = &buffer_class,
  300. .query_formats = query_formats,
  301. .init = init_video,
  302. .uninit = uninit,
  303. .inputs = NULL,
  304. .outputs = avfilter_vsrc_buffer_outputs,
  305. };
  306. static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
  307. {
  308. .name = "default",
  309. .type = AVMEDIA_TYPE_AUDIO,
  310. .request_frame = request_frame,
  311. .poll_frame = poll_frame,
  312. .config_props = config_props,
  313. },
  314. { NULL }
  315. };
  316. AVFilter ff_asrc_abuffer = {
  317. .name = "abuffer",
  318. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
  319. .priv_size = sizeof(BufferSourceContext),
  320. .priv_class = &abuffer_class,
  321. .query_formats = query_formats,
  322. .init = init_audio,
  323. .uninit = uninit,
  324. .inputs = NULL,
  325. .outputs = avfilter_asrc_abuffer_outputs,
  326. };