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.

436 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 "audio.h"
  25. #include "avfilter.h"
  26. #include "buffersrc.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. #include "avcodec.h"
  31. #include "libavutil/audioconvert.h"
  32. #include "libavutil/common.h"
  33. #include "libavutil/fifo.h"
  34. #include "libavutil/imgutils.h"
  35. #include "libavutil/opt.h"
  36. #include "libavutil/samplefmt.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 PixelFormat 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. 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 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, 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. return 0;
  133. }
  134. #ifdef FF_API_BUFFERSRC_BUFFER
  135. int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
  136. {
  137. return av_buffersrc_add_ref(s, buf, AV_BUFFERSRC_FLAG_NO_COPY);
  138. }
  139. #endif
  140. unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
  141. {
  142. return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
  143. }
  144. #define OFFSET(x) offsetof(BufferSourceContext, x)
  145. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  146. static const AVOption buffer_options[] = {
  147. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { 0 }, 0, INT_MAX, FLAGS },
  148. { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { 0 }, 0, INT_MAX, FLAGS },
  149. { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = FLAGS },
  150. { "pix_fmt", NULL, OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, .flags = FLAGS },
  151. { "pixel_aspect", NULL, OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { 0 }, 0, INT_MAX, FLAGS },
  152. { "sws_param", NULL, OFFSET(sws_param), AV_OPT_TYPE_STRING, .flags = FLAGS },
  153. { NULL },
  154. };
  155. #undef FLAGS
  156. AVFILTER_DEFINE_CLASS(buffer);
  157. static av_cold int init_video(AVFilterContext *ctx, const char *args)
  158. {
  159. BufferSourceContext *c = ctx->priv;
  160. char pix_fmt_str[128], sws_param[256] = "", *colon, *equal;
  161. int ret, n = 0;
  162. c->class = &buffer_class;
  163. if (!args) {
  164. av_log(ctx, AV_LOG_ERROR, "Arguments required\n");
  165. return AVERROR(EINVAL);
  166. }
  167. colon = strchr(args, ':');
  168. equal = strchr(args, '=');
  169. if (equal && (!colon || equal < colon)) {
  170. av_opt_set_defaults(c);
  171. ret = av_set_options_string(c, args, "=", ":");
  172. if (ret < 0)
  173. goto fail;
  174. } else {
  175. if ((n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
  176. &c->time_base.num, &c->time_base.den,
  177. &c->pixel_aspect.num, &c->pixel_aspect.den, sws_param)) < 7) {
  178. av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args);
  179. ret = AVERROR(EINVAL);
  180. goto fail;
  181. }
  182. av_log(ctx, AV_LOG_WARNING, "Flat options syntax is deprecated, use key=value pairs\n");
  183. if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
  184. goto fail;
  185. c->sws_param = av_strdup(sws_param);
  186. if (!c->sws_param) {
  187. ret = AVERROR(ENOMEM);
  188. goto fail;
  189. }
  190. }
  191. if (!(c->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*)))) {
  192. ret = AVERROR(ENOMEM);
  193. goto fail;
  194. }
  195. 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",
  196. c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
  197. c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
  198. c->pixel_aspect.num, c->pixel_aspect.den, (char *)av_x_if_null(c->sws_param, ""));
  199. c->warning_limit = 100;
  200. return 0;
  201. fail:
  202. av_opt_free(c);
  203. return ret;
  204. }
  205. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  206. static const AVOption abuffer_options[] = {
  207. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { 0 }, 0, INT_MAX, FLAGS },
  208. { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { 0 }, 0, INT_MAX, FLAGS },
  209. { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = FLAGS },
  210. { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = FLAGS },
  211. { NULL },
  212. };
  213. AVFILTER_DEFINE_CLASS(abuffer);
  214. static av_cold int init_audio(AVFilterContext *ctx, const char *args)
  215. {
  216. BufferSourceContext *s = ctx->priv;
  217. int ret = 0;
  218. s->class = &abuffer_class;
  219. av_opt_set_defaults(s);
  220. if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
  221. goto fail;
  222. s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
  223. if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
  224. av_log(ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n",
  225. s->sample_fmt_str);
  226. ret = AVERROR(EINVAL);
  227. goto fail;
  228. }
  229. s->channel_layout = av_get_channel_layout(s->channel_layout_str);
  230. if (!s->channel_layout) {
  231. av_log(ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n",
  232. s->channel_layout_str);
  233. ret = AVERROR(EINVAL);
  234. goto fail;
  235. }
  236. if (!(s->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*)))) {
  237. ret = AVERROR(ENOMEM);
  238. goto fail;
  239. }
  240. if (!s->time_base.num)
  241. s->time_base = (AVRational){1, s->sample_rate};
  242. av_log(ctx, AV_LOG_VERBOSE,
  243. "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
  244. s->time_base.num, s->time_base.den, s->sample_fmt_str,
  245. s->sample_rate, s->channel_layout_str);
  246. s->warning_limit = 100;
  247. fail:
  248. av_opt_free(s);
  249. return ret;
  250. }
  251. static av_cold void uninit(AVFilterContext *ctx)
  252. {
  253. BufferSourceContext *s = ctx->priv;
  254. while (s->fifo && av_fifo_size(s->fifo)) {
  255. AVFilterBufferRef *buf;
  256. av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
  257. avfilter_unref_buffer(buf);
  258. }
  259. av_fifo_free(s->fifo);
  260. s->fifo = NULL;
  261. av_freep(&s->sws_param);
  262. }
  263. static int query_formats(AVFilterContext *ctx)
  264. {
  265. BufferSourceContext *c = ctx->priv;
  266. AVFilterChannelLayouts *channel_layouts = NULL;
  267. AVFilterFormats *formats = NULL;
  268. AVFilterFormats *samplerates = NULL;
  269. switch (ctx->outputs[0]->type) {
  270. case AVMEDIA_TYPE_VIDEO:
  271. ff_add_format(&formats, c->pix_fmt);
  272. ff_set_common_formats(ctx, formats);
  273. break;
  274. case AVMEDIA_TYPE_AUDIO:
  275. ff_add_format(&formats, c->sample_fmt);
  276. ff_set_common_formats(ctx, formats);
  277. ff_add_format(&samplerates, c->sample_rate);
  278. ff_set_common_samplerates(ctx, samplerates);
  279. ff_add_channel_layout(&channel_layouts, c->channel_layout);
  280. ff_set_common_channel_layouts(ctx, channel_layouts);
  281. break;
  282. default:
  283. return AVERROR(EINVAL);
  284. }
  285. return 0;
  286. }
  287. static int config_props(AVFilterLink *link)
  288. {
  289. BufferSourceContext *c = link->src->priv;
  290. switch (link->type) {
  291. case AVMEDIA_TYPE_VIDEO:
  292. link->w = c->w;
  293. link->h = c->h;
  294. link->sample_aspect_ratio = c->pixel_aspect;
  295. break;
  296. case AVMEDIA_TYPE_AUDIO:
  297. link->channel_layout = c->channel_layout;
  298. link->sample_rate = c->sample_rate;
  299. break;
  300. default:
  301. return AVERROR(EINVAL);
  302. }
  303. link->time_base = c->time_base;
  304. link->frame_rate = c->frame_rate;
  305. return 0;
  306. }
  307. static int request_frame(AVFilterLink *link)
  308. {
  309. BufferSourceContext *c = link->src->priv;
  310. AVFilterBufferRef *buf;
  311. int ret = 0;
  312. if (!av_fifo_size(c->fifo)) {
  313. if (c->eof)
  314. return AVERROR_EOF;
  315. c->nb_failed_requests++;
  316. return AVERROR(EAGAIN);
  317. }
  318. av_fifo_generic_read(c->fifo, &buf, sizeof(buf), NULL);
  319. switch (link->type) {
  320. case AVMEDIA_TYPE_VIDEO:
  321. if ((ret = ff_start_frame(link, buf)) < 0 ||
  322. (ret = ff_draw_slice(link, 0, link->h, 1)) < 0 ||
  323. (ret = ff_end_frame(link)) < 0)
  324. return ret;
  325. break;
  326. case AVMEDIA_TYPE_AUDIO:
  327. ret = ff_filter_samples(link, buf);
  328. break;
  329. default:
  330. avfilter_unref_bufferp(&buf);
  331. return AVERROR(EINVAL);
  332. }
  333. return ret;
  334. }
  335. static int poll_frame(AVFilterLink *link)
  336. {
  337. BufferSourceContext *c = link->src->priv;
  338. int size = av_fifo_size(c->fifo);
  339. if (!size && c->eof)
  340. return AVERROR_EOF;
  341. return size/sizeof(AVFilterBufferRef*);
  342. }
  343. AVFilter avfilter_vsrc_buffer = {
  344. .name = "buffer",
  345. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  346. .priv_size = sizeof(BufferSourceContext),
  347. .query_formats = query_formats,
  348. .init = init_video,
  349. .uninit = uninit,
  350. .inputs = (const AVFilterPad[]) {{ .name = NULL }},
  351. .outputs = (const AVFilterPad[]) {{ .name = "default",
  352. .type = AVMEDIA_TYPE_VIDEO,
  353. .request_frame = request_frame,
  354. .poll_frame = poll_frame,
  355. .config_props = config_props, },
  356. { .name = NULL}},
  357. .priv_class = &buffer_class,
  358. };
  359. AVFilter avfilter_asrc_abuffer = {
  360. .name = "abuffer",
  361. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
  362. .priv_size = sizeof(BufferSourceContext),
  363. .query_formats = query_formats,
  364. .init = init_audio,
  365. .uninit = uninit,
  366. .inputs = (const AVFilterPad[]) {{ .name = NULL }},
  367. .outputs = (const AVFilterPad[]) {{ .name = "default",
  368. .type = AVMEDIA_TYPE_AUDIO,
  369. .request_frame = request_frame,
  370. .poll_frame = poll_frame,
  371. .config_props = config_props, },
  372. { .name = NULL}},
  373. .priv_class = &abuffer_class,
  374. };