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.

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