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.

480 lines
15KB

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