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.

393 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. AVRational frame_rate; ///< frame_rate to set in the output link
  44. /* video only */
  45. int h, w;
  46. enum AVPixelFormat pix_fmt;
  47. char *pix_fmt_str;
  48. AVRational pixel_aspect;
  49. /* audio only */
  50. int sample_rate;
  51. enum AVSampleFormat sample_fmt;
  52. char *sample_fmt_str;
  53. uint64_t channel_layout;
  54. char *channel_layout_str;
  55. int eof;
  56. } BufferSourceContext;
  57. #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format)\
  58. if (c->w != width || c->h != height || c->pix_fmt != format) {\
  59. av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
  60. return AVERROR(EINVAL);\
  61. }
  62. #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, format)\
  63. if (c->sample_fmt != format || c->sample_rate != srate ||\
  64. c->channel_layout != ch_layout) {\
  65. av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
  66. return AVERROR(EINVAL);\
  67. }
  68. int attribute_align_arg av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
  69. {
  70. AVFrame *copy;
  71. int ret = 0;
  72. if (!(copy = av_frame_alloc()))
  73. return AVERROR(ENOMEM);
  74. ret = av_frame_ref(copy, frame);
  75. if (ret >= 0)
  76. ret = av_buffersrc_add_frame(ctx, copy);
  77. av_frame_free(&copy);
  78. return ret;
  79. }
  80. int attribute_align_arg av_buffersrc_add_frame(AVFilterContext *ctx,
  81. AVFrame *frame)
  82. {
  83. BufferSourceContext *s = ctx->priv;
  84. AVFrame *copy;
  85. int refcounted, ret;
  86. if (!frame) {
  87. s->eof = 1;
  88. return 0;
  89. } else if (s->eof)
  90. return AVERROR(EINVAL);
  91. refcounted = !!frame->buf[0];
  92. switch (ctx->outputs[0]->type) {
  93. case AVMEDIA_TYPE_VIDEO:
  94. CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
  95. frame->format);
  96. break;
  97. case AVMEDIA_TYPE_AUDIO:
  98. CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
  99. frame->format);
  100. break;
  101. default:
  102. return AVERROR(EINVAL);
  103. }
  104. if (!av_fifo_space(s->fifo) &&
  105. (ret = av_fifo_realloc2(s->fifo, av_fifo_size(s->fifo) +
  106. sizeof(copy))) < 0)
  107. return ret;
  108. if (!(copy = av_frame_alloc()))
  109. return AVERROR(ENOMEM);
  110. if (refcounted) {
  111. av_frame_move_ref(copy, frame);
  112. } else {
  113. ret = av_frame_ref(copy, frame);
  114. if (ret < 0) {
  115. av_frame_free(&copy);
  116. return ret;
  117. }
  118. }
  119. if ((ret = av_fifo_generic_write(s->fifo, &copy, sizeof(copy), NULL)) < 0) {
  120. if (refcounted)
  121. av_frame_move_ref(frame, copy);
  122. av_frame_free(&copy);
  123. return ret;
  124. }
  125. return 0;
  126. }
  127. static av_cold int init_video(AVFilterContext *ctx)
  128. {
  129. BufferSourceContext *c = ctx->priv;
  130. if (!c->pix_fmt_str || !c->w || !c->h || av_q2d(c->time_base) <= 0) {
  131. av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n");
  132. return AVERROR(EINVAL);
  133. }
  134. if ((c->pix_fmt = av_get_pix_fmt(c->pix_fmt_str)) == AV_PIX_FMT_NONE) {
  135. char *tail;
  136. c->pix_fmt = strtol(c->pix_fmt_str, &tail, 10);
  137. if (*tail || c->pix_fmt < 0 || !av_pix_fmt_desc_get(c->pix_fmt)) {
  138. av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", c->pix_fmt_str);
  139. return AVERROR(EINVAL);
  140. }
  141. }
  142. if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*))))
  143. return AVERROR(ENOMEM);
  144. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d\n",
  145. c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
  146. c->time_base.num, c->time_base.den,
  147. c->pixel_aspect.num, c->pixel_aspect.den);
  148. return 0;
  149. }
  150. #define OFFSET(x) offsetof(BufferSourceContext, x)
  151. #define A AV_OPT_FLAG_AUDIO_PARAM
  152. #define V AV_OPT_FLAG_VIDEO_PARAM
  153. static const AVOption video_options[] = {
  154. { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  155. { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  156. { "pix_fmt", NULL, OFFSET(pix_fmt_str), AV_OPT_TYPE_STRING, .flags = V },
  157. #if FF_API_OLD_FILTER_OPTS
  158. /* those 4 are for compatibility with the old option passing system where each filter
  159. * did its own parsing */
  160. { "time_base_num", "deprecated, do not use", OFFSET(time_base.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  161. { "time_base_den", "deprecated, do not use", OFFSET(time_base.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  162. { "sar_num", "deprecated, do not use", OFFSET(pixel_aspect.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  163. { "sar_den", "deprecated, do not use", OFFSET(pixel_aspect.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  164. #endif
  165. { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V },
  166. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  167. { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  168. { NULL },
  169. };
  170. static const AVClass buffer_class = {
  171. .class_name = "buffer source",
  172. .item_name = av_default_item_name,
  173. .option = video_options,
  174. .version = LIBAVUTIL_VERSION_INT,
  175. };
  176. static const AVOption audio_options[] = {
  177. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
  178. { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
  179. { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = A },
  180. { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
  181. { NULL },
  182. };
  183. static const AVClass abuffer_class = {
  184. .class_name = "abuffer source",
  185. .item_name = av_default_item_name,
  186. .option = audio_options,
  187. .version = LIBAVUTIL_VERSION_INT,
  188. };
  189. static av_cold int init_audio(AVFilterContext *ctx)
  190. {
  191. BufferSourceContext *s = ctx->priv;
  192. int ret = 0;
  193. s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
  194. if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
  195. av_log(ctx, AV_LOG_ERROR, "Invalid sample format %s.\n",
  196. s->sample_fmt_str);
  197. return AVERROR(EINVAL);
  198. }
  199. s->channel_layout = av_get_channel_layout(s->channel_layout_str);
  200. if (!s->channel_layout) {
  201. av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
  202. s->channel_layout_str);
  203. return AVERROR(EINVAL);
  204. }
  205. if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*))))
  206. return AVERROR(ENOMEM);
  207. if (!s->time_base.num)
  208. s->time_base = (AVRational){1, s->sample_rate};
  209. av_log(ctx, AV_LOG_VERBOSE, "tb:%d/%d samplefmt:%s samplerate: %d "
  210. "ch layout:%s\n", s->time_base.num, s->time_base.den, s->sample_fmt_str,
  211. s->sample_rate, s->channel_layout_str);
  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. AVFrame *frame;
  219. av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
  220. av_frame_free(&frame);
  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. link->frame_rate = c->frame_rate;
  267. return 0;
  268. }
  269. static int request_frame(AVFilterLink *link)
  270. {
  271. BufferSourceContext *c = link->src->priv;
  272. AVFrame *frame;
  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, &frame, sizeof(frame), NULL);
  280. ff_filter_frame(link, frame);
  281. return ret;
  282. }
  283. static int poll_frame(AVFilterLink *link)
  284. {
  285. BufferSourceContext *c = link->src->priv;
  286. int size = av_fifo_size(c->fifo);
  287. if (!size && c->eof)
  288. return AVERROR_EOF;
  289. return size/sizeof(AVFrame*);
  290. }
  291. static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
  292. {
  293. .name = "default",
  294. .type = AVMEDIA_TYPE_VIDEO,
  295. .request_frame = request_frame,
  296. .poll_frame = poll_frame,
  297. .config_props = config_props,
  298. },
  299. { NULL }
  300. };
  301. AVFilter ff_vsrc_buffer = {
  302. .name = "buffer",
  303. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  304. .priv_size = sizeof(BufferSourceContext),
  305. .priv_class = &buffer_class,
  306. .query_formats = query_formats,
  307. .init = init_video,
  308. .uninit = uninit,
  309. .inputs = NULL,
  310. .outputs = avfilter_vsrc_buffer_outputs,
  311. };
  312. static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
  313. {
  314. .name = "default",
  315. .type = AVMEDIA_TYPE_AUDIO,
  316. .request_frame = request_frame,
  317. .poll_frame = poll_frame,
  318. .config_props = config_props,
  319. },
  320. { NULL }
  321. };
  322. AVFilter ff_asrc_abuffer = {
  323. .name = "abuffer",
  324. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
  325. .priv_size = sizeof(BufferSourceContext),
  326. .priv_class = &abuffer_class,
  327. .query_formats = query_formats,
  328. .init = init_audio,
  329. .uninit = uninit,
  330. .inputs = NULL,
  331. .outputs = avfilter_asrc_abuffer_outputs,
  332. };