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.

390 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 tb:%d/%d sar:%d/%d\n",
  144. c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
  145. c->time_base.num, c->time_base.den,
  146. c->pixel_aspect.num, c->pixel_aspect.den);
  147. return 0;
  148. }
  149. #define OFFSET(x) offsetof(BufferSourceContext, x)
  150. #define A AV_OPT_FLAG_AUDIO_PARAM
  151. #define V AV_OPT_FLAG_VIDEO_PARAM
  152. static const AVOption video_options[] = {
  153. { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  154. { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  155. { "pix_fmt", NULL, OFFSET(pix_fmt_str), AV_OPT_TYPE_STRING, .flags = V },
  156. #if FF_API_OLD_FILTER_OPTS
  157. /* those 4 are for compatibility with the old option passing system where each filter
  158. * did its own parsing */
  159. { "time_base_num", "deprecated, do not use", OFFSET(time_base.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  160. { "time_base_den", "deprecated, do not use", OFFSET(time_base.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  161. { "sar_num", "deprecated, do not use", OFFSET(pixel_aspect.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  162. { "sar_den", "deprecated, do not use", OFFSET(pixel_aspect.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  163. #endif
  164. { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V },
  165. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  166. { NULL },
  167. };
  168. static const AVClass buffer_class = {
  169. .class_name = "buffer source",
  170. .item_name = av_default_item_name,
  171. .option = video_options,
  172. .version = LIBAVUTIL_VERSION_INT,
  173. };
  174. static const AVOption audio_options[] = {
  175. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
  176. { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
  177. { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = A },
  178. { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
  179. { NULL },
  180. };
  181. static const AVClass abuffer_class = {
  182. .class_name = "abuffer source",
  183. .item_name = av_default_item_name,
  184. .option = audio_options,
  185. .version = LIBAVUTIL_VERSION_INT,
  186. };
  187. static av_cold int init_audio(AVFilterContext *ctx)
  188. {
  189. BufferSourceContext *s = ctx->priv;
  190. int ret = 0;
  191. s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
  192. if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
  193. av_log(ctx, AV_LOG_ERROR, "Invalid sample format %s.\n",
  194. s->sample_fmt_str);
  195. return AVERROR(EINVAL);
  196. }
  197. s->channel_layout = av_get_channel_layout(s->channel_layout_str);
  198. if (!s->channel_layout) {
  199. av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
  200. s->channel_layout_str);
  201. return AVERROR(EINVAL);
  202. }
  203. if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*))))
  204. return AVERROR(ENOMEM);
  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. return ret;
  211. }
  212. static av_cold void uninit(AVFilterContext *ctx)
  213. {
  214. BufferSourceContext *s = ctx->priv;
  215. while (s->fifo && av_fifo_size(s->fifo)) {
  216. AVFrame *frame;
  217. av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
  218. av_frame_free(&frame);
  219. }
  220. av_fifo_free(s->fifo);
  221. s->fifo = NULL;
  222. }
  223. static int query_formats(AVFilterContext *ctx)
  224. {
  225. BufferSourceContext *c = ctx->priv;
  226. AVFilterChannelLayouts *channel_layouts = NULL;
  227. AVFilterFormats *formats = NULL;
  228. AVFilterFormats *samplerates = NULL;
  229. switch (ctx->outputs[0]->type) {
  230. case AVMEDIA_TYPE_VIDEO:
  231. ff_add_format(&formats, c->pix_fmt);
  232. ff_set_common_formats(ctx, formats);
  233. break;
  234. case AVMEDIA_TYPE_AUDIO:
  235. ff_add_format(&formats, c->sample_fmt);
  236. ff_set_common_formats(ctx, formats);
  237. ff_add_format(&samplerates, c->sample_rate);
  238. ff_set_common_samplerates(ctx, samplerates);
  239. ff_add_channel_layout(&channel_layouts, c->channel_layout);
  240. ff_set_common_channel_layouts(ctx, channel_layouts);
  241. break;
  242. default:
  243. return AVERROR(EINVAL);
  244. }
  245. return 0;
  246. }
  247. static int config_props(AVFilterLink *link)
  248. {
  249. BufferSourceContext *c = link->src->priv;
  250. switch (link->type) {
  251. case AVMEDIA_TYPE_VIDEO:
  252. link->w = c->w;
  253. link->h = c->h;
  254. link->sample_aspect_ratio = c->pixel_aspect;
  255. break;
  256. case AVMEDIA_TYPE_AUDIO:
  257. link->channel_layout = c->channel_layout;
  258. link->sample_rate = c->sample_rate;
  259. break;
  260. default:
  261. return AVERROR(EINVAL);
  262. }
  263. link->time_base = c->time_base;
  264. return 0;
  265. }
  266. static int request_frame(AVFilterLink *link)
  267. {
  268. BufferSourceContext *c = link->src->priv;
  269. AVFrame *frame;
  270. int ret = 0;
  271. if (!av_fifo_size(c->fifo)) {
  272. if (c->eof)
  273. return AVERROR_EOF;
  274. return AVERROR(EAGAIN);
  275. }
  276. av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
  277. ff_filter_frame(link, frame);
  278. return ret;
  279. }
  280. static int poll_frame(AVFilterLink *link)
  281. {
  282. BufferSourceContext *c = link->src->priv;
  283. int size = av_fifo_size(c->fifo);
  284. if (!size && c->eof)
  285. return AVERROR_EOF;
  286. return size/sizeof(AVFrame*);
  287. }
  288. static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
  289. {
  290. .name = "default",
  291. .type = AVMEDIA_TYPE_VIDEO,
  292. .request_frame = request_frame,
  293. .poll_frame = poll_frame,
  294. .config_props = config_props,
  295. },
  296. { NULL }
  297. };
  298. AVFilter ff_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. .priv_class = &buffer_class,
  303. .query_formats = query_formats,
  304. .init = init_video,
  305. .uninit = uninit,
  306. .inputs = NULL,
  307. .outputs = avfilter_vsrc_buffer_outputs,
  308. };
  309. static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
  310. {
  311. .name = "default",
  312. .type = AVMEDIA_TYPE_AUDIO,
  313. .request_frame = request_frame,
  314. .poll_frame = poll_frame,
  315. .config_props = config_props,
  316. },
  317. { NULL }
  318. };
  319. AVFilter ff_asrc_abuffer = {
  320. .name = "abuffer",
  321. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
  322. .priv_size = sizeof(BufferSourceContext),
  323. .priv_class = &abuffer_class,
  324. .query_formats = query_formats,
  325. .init = init_audio,
  326. .uninit = uninit,
  327. .inputs = NULL,
  328. .outputs = avfilter_asrc_abuffer_outputs,
  329. };