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.

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