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.

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