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.

457 lines
14KB

  1. /*
  2. * Copyright (c) 2008 Vitor Sessak
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "libavutil/channel_layout.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/fifo.h"
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/samplefmt.h"
  30. #include "audio.h"
  31. #include "avfilter.h"
  32. #include "buffersrc.h"
  33. #include "formats.h"
  34. #include "internal.h"
  35. #include "video.h"
  36. #include "avcodec.h"
  37. typedef struct {
  38. const AVClass *class;
  39. AVFifoBuffer *fifo;
  40. AVRational time_base; ///< time_base to set in the output link
  41. AVRational frame_rate; ///< frame_rate to set in the output link
  42. unsigned nb_failed_requests;
  43. unsigned warning_limit;
  44. /* video only */
  45. int w, h;
  46. enum AVPixelFormat pix_fmt;
  47. AVRational pixel_aspect;
  48. char *sws_param;
  49. /* audio only */
  50. int sample_rate;
  51. enum AVSampleFormat sample_fmt;
  52. char *sample_fmt_str;
  53. int channels;
  54. uint64_t channel_layout;
  55. char *channel_layout_str;
  56. int eof;
  57. } BufferSourceContext;
  58. #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format)\
  59. if (c->w != width || c->h != height || c->pix_fmt != format) {\
  60. av_log(s, AV_LOG_INFO, "Changing frame properties on the fly is not supported by all filters.\n");\
  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 av_buffersrc_add_frame(AVFilterContext *buffer_src,
  69. const AVFrame *frame, int flags)
  70. {
  71. AVFilterBufferRef *picref;
  72. int ret;
  73. if (!frame) /* NULL for EOF */
  74. return av_buffersrc_add_ref(buffer_src, NULL, flags);
  75. picref = avfilter_get_buffer_ref_from_frame(buffer_src->outputs[0]->type,
  76. frame, AV_PERM_WRITE);
  77. if (!picref)
  78. return AVERROR(ENOMEM);
  79. ret = av_buffersrc_add_ref(buffer_src, picref, flags);
  80. picref->buf->data[0] = NULL;
  81. avfilter_unref_buffer(picref);
  82. return ret;
  83. }
  84. int av_buffersrc_write_frame(AVFilterContext *buffer_filter, const AVFrame *frame)
  85. {
  86. return av_buffersrc_add_frame(buffer_filter, frame, 0);
  87. }
  88. int av_buffersrc_add_ref(AVFilterContext *s, AVFilterBufferRef *buf, int flags)
  89. {
  90. BufferSourceContext *c = s->priv;
  91. AVFilterBufferRef *to_free = NULL;
  92. int ret;
  93. if (!buf) {
  94. c->eof = 1;
  95. return 0;
  96. } else if (c->eof)
  97. return AVERROR(EINVAL);
  98. if (!av_fifo_space(c->fifo) &&
  99. (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
  100. sizeof(buf))) < 0)
  101. return ret;
  102. if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
  103. switch (s->outputs[0]->type) {
  104. case AVMEDIA_TYPE_VIDEO:
  105. CHECK_VIDEO_PARAM_CHANGE(s, c, buf->video->w, buf->video->h, buf->format);
  106. break;
  107. case AVMEDIA_TYPE_AUDIO:
  108. CHECK_AUDIO_PARAM_CHANGE(s, c, buf->audio->sample_rate, buf->audio->channel_layout,
  109. buf->format);
  110. break;
  111. default:
  112. return AVERROR(EINVAL);
  113. }
  114. }
  115. if (!(flags & AV_BUFFERSRC_FLAG_NO_COPY))
  116. to_free = buf = ff_copy_buffer_ref(s->outputs[0], buf);
  117. if(!buf)
  118. return -1;
  119. if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0) {
  120. avfilter_unref_buffer(to_free);
  121. return ret;
  122. }
  123. c->nb_failed_requests = 0;
  124. if (c->warning_limit &&
  125. av_fifo_size(c->fifo) / sizeof(buf) >= c->warning_limit) {
  126. av_log(s, AV_LOG_WARNING,
  127. "%d buffers queued in %s, something may be wrong.\n",
  128. c->warning_limit,
  129. (char *)av_x_if_null(s->name, s->filter->name));
  130. c->warning_limit *= 10;
  131. }
  132. if ((flags & AV_BUFFERSRC_FLAG_PUSH))
  133. if ((ret = s->output_pads[0].request_frame(s->outputs[0])) < 0)
  134. return ret;
  135. return 0;
  136. }
  137. #ifdef FF_API_BUFFERSRC_BUFFER
  138. int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
  139. {
  140. return av_buffersrc_add_ref(s, buf, AV_BUFFERSRC_FLAG_NO_COPY);
  141. }
  142. #endif
  143. unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
  144. {
  145. return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
  146. }
  147. #define OFFSET(x) offsetof(BufferSourceContext, x)
  148. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  149. static const AVOption buffer_options[] = {
  150. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  151. { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  152. { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = FLAGS },
  153. { "pix_fmt", NULL, OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, .flags = FLAGS },
  154. { "pixel_aspect", NULL, OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  155. { "sws_param", NULL, OFFSET(sws_param), AV_OPT_TYPE_STRING, .flags = FLAGS },
  156. { NULL },
  157. };
  158. #undef FLAGS
  159. AVFILTER_DEFINE_CLASS(buffer);
  160. static av_cold int init_video(AVFilterContext *ctx, const char *args)
  161. {
  162. BufferSourceContext *c = ctx->priv;
  163. char pix_fmt_str[128], sws_param[256] = "", *colon, *equal;
  164. int ret, n = 0;
  165. c->class = &buffer_class;
  166. if (!args) {
  167. av_log(ctx, AV_LOG_ERROR, "Arguments required\n");
  168. return AVERROR(EINVAL);
  169. }
  170. colon = strchr(args, ':');
  171. equal = strchr(args, '=');
  172. if (equal && (!colon || equal < colon)) {
  173. av_opt_set_defaults(c);
  174. ret = av_set_options_string(c, args, "=", ":");
  175. if (ret < 0)
  176. goto fail;
  177. } else {
  178. if ((n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
  179. &c->time_base.num, &c->time_base.den,
  180. &c->pixel_aspect.num, &c->pixel_aspect.den, sws_param)) < 7) {
  181. av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args);
  182. ret = AVERROR(EINVAL);
  183. goto fail;
  184. }
  185. av_log(ctx, AV_LOG_WARNING, "Flat options syntax is deprecated, use key=value pairs\n");
  186. if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
  187. goto fail;
  188. c->sws_param = av_strdup(sws_param);
  189. if (!c->sws_param) {
  190. ret = AVERROR(ENOMEM);
  191. goto fail;
  192. }
  193. }
  194. if (!(c->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*)))) {
  195. ret = AVERROR(ENOMEM);
  196. goto fail;
  197. }
  198. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d pixfmt:%s tb:%d/%d fr:%d/%d sar:%d/%d sws_param:%s\n",
  199. c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
  200. c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
  201. c->pixel_aspect.num, c->pixel_aspect.den, (char *)av_x_if_null(c->sws_param, ""));
  202. c->warning_limit = 100;
  203. return 0;
  204. fail:
  205. av_opt_free(c);
  206. return ret;
  207. }
  208. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  209. static const AVOption abuffer_options[] = {
  210. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  211. { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  212. { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = FLAGS },
  213. { "channels", NULL, OFFSET(channels), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  214. { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = FLAGS },
  215. { NULL },
  216. };
  217. AVFILTER_DEFINE_CLASS(abuffer);
  218. static av_cold int init_audio(AVFilterContext *ctx, const char *args)
  219. {
  220. BufferSourceContext *s = ctx->priv;
  221. int ret = 0;
  222. s->class = &abuffer_class;
  223. av_opt_set_defaults(s);
  224. if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
  225. goto fail;
  226. s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
  227. if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
  228. av_log(ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n",
  229. s->sample_fmt_str);
  230. ret = AVERROR(EINVAL);
  231. goto fail;
  232. }
  233. if (s->channel_layout_str) {
  234. int n;
  235. /* TODO reindent */
  236. s->channel_layout = av_get_channel_layout(s->channel_layout_str);
  237. if (!s->channel_layout) {
  238. av_log(ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n",
  239. s->channel_layout_str);
  240. ret = AVERROR(EINVAL);
  241. goto fail;
  242. }
  243. n = av_get_channel_layout_nb_channels(s->channel_layout);
  244. if (s->channels) {
  245. if (n != s->channels) {
  246. av_log(ctx, AV_LOG_ERROR,
  247. "Mismatching channel count %d and layout '%s' "
  248. "(%d channels)\n",
  249. s->channels, s->channel_layout_str, n);
  250. ret = AVERROR(EINVAL);
  251. goto fail;
  252. }
  253. }
  254. s->channels = n;
  255. } else if (!s->channels) {
  256. av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor "
  257. "channel layout specified\n");
  258. ret = AVERROR(EINVAL);
  259. goto fail;
  260. }
  261. if (!(s->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*)))) {
  262. ret = AVERROR(ENOMEM);
  263. goto fail;
  264. }
  265. if (!s->time_base.num)
  266. s->time_base = (AVRational){1, s->sample_rate};
  267. av_log(ctx, AV_LOG_VERBOSE,
  268. "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
  269. s->time_base.num, s->time_base.den, s->sample_fmt_str,
  270. s->sample_rate, s->channel_layout_str);
  271. s->warning_limit = 100;
  272. fail:
  273. av_opt_free(s);
  274. return ret;
  275. }
  276. static av_cold void uninit(AVFilterContext *ctx)
  277. {
  278. BufferSourceContext *s = ctx->priv;
  279. while (s->fifo && av_fifo_size(s->fifo)) {
  280. AVFilterBufferRef *buf;
  281. av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
  282. avfilter_unref_buffer(buf);
  283. }
  284. av_fifo_free(s->fifo);
  285. s->fifo = NULL;
  286. av_freep(&s->sws_param);
  287. }
  288. static int query_formats(AVFilterContext *ctx)
  289. {
  290. BufferSourceContext *c = ctx->priv;
  291. AVFilterChannelLayouts *channel_layouts = NULL;
  292. AVFilterFormats *formats = NULL;
  293. AVFilterFormats *samplerates = NULL;
  294. switch (ctx->outputs[0]->type) {
  295. case AVMEDIA_TYPE_VIDEO:
  296. ff_add_format(&formats, c->pix_fmt);
  297. ff_set_common_formats(ctx, formats);
  298. break;
  299. case AVMEDIA_TYPE_AUDIO:
  300. ff_add_format(&formats, c->sample_fmt);
  301. ff_set_common_formats(ctx, formats);
  302. ff_add_format(&samplerates, c->sample_rate);
  303. ff_set_common_samplerates(ctx, samplerates);
  304. ff_add_channel_layout(&channel_layouts,
  305. c->channel_layout ? c->channel_layout :
  306. FF_COUNT2LAYOUT(c->channels));
  307. ff_set_common_channel_layouts(ctx, channel_layouts);
  308. break;
  309. default:
  310. return AVERROR(EINVAL);
  311. }
  312. return 0;
  313. }
  314. static int config_props(AVFilterLink *link)
  315. {
  316. BufferSourceContext *c = link->src->priv;
  317. switch (link->type) {
  318. case AVMEDIA_TYPE_VIDEO:
  319. link->w = c->w;
  320. link->h = c->h;
  321. link->sample_aspect_ratio = c->pixel_aspect;
  322. break;
  323. case AVMEDIA_TYPE_AUDIO:
  324. break;
  325. default:
  326. return AVERROR(EINVAL);
  327. }
  328. link->time_base = c->time_base;
  329. link->frame_rate = c->frame_rate;
  330. return 0;
  331. }
  332. static int request_frame(AVFilterLink *link)
  333. {
  334. BufferSourceContext *c = link->src->priv;
  335. AVFilterBufferRef *buf;
  336. if (!av_fifo_size(c->fifo)) {
  337. if (c->eof)
  338. return AVERROR_EOF;
  339. c->nb_failed_requests++;
  340. return AVERROR(EAGAIN);
  341. }
  342. av_fifo_generic_read(c->fifo, &buf, sizeof(buf), NULL);
  343. return ff_filter_frame(link, buf);
  344. }
  345. static int poll_frame(AVFilterLink *link)
  346. {
  347. BufferSourceContext *c = link->src->priv;
  348. int size = av_fifo_size(c->fifo);
  349. if (!size && c->eof)
  350. return AVERROR_EOF;
  351. return size/sizeof(AVFilterBufferRef*);
  352. }
  353. static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
  354. {
  355. .name = "default",
  356. .type = AVMEDIA_TYPE_VIDEO,
  357. .request_frame = request_frame,
  358. .poll_frame = poll_frame,
  359. .config_props = config_props,
  360. },
  361. { NULL }
  362. };
  363. AVFilter avfilter_vsrc_buffer = {
  364. .name = "buffer",
  365. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  366. .priv_size = sizeof(BufferSourceContext),
  367. .query_formats = query_formats,
  368. .init = init_video,
  369. .uninit = uninit,
  370. .inputs = NULL,
  371. .outputs = avfilter_vsrc_buffer_outputs,
  372. .priv_class = &buffer_class,
  373. };
  374. static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
  375. {
  376. .name = "default",
  377. .type = AVMEDIA_TYPE_AUDIO,
  378. .request_frame = request_frame,
  379. .poll_frame = poll_frame,
  380. .config_props = config_props,
  381. },
  382. { NULL }
  383. };
  384. AVFilter avfilter_asrc_abuffer = {
  385. .name = "abuffer",
  386. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
  387. .priv_size = sizeof(BufferSourceContext),
  388. .query_formats = query_formats,
  389. .init = init_audio,
  390. .uninit = uninit,
  391. .inputs = NULL,
  392. .outputs = avfilter_asrc_abuffer_outputs,
  393. .priv_class = &abuffer_class,
  394. };