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.

563 lines
18KB

  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 "libavutil/channel_layout.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/fifo.h"
  27. #include "libavutil/frame.h"
  28. #include "libavutil/imgutils.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/samplefmt.h"
  31. #include "audio.h"
  32. #include "avfilter.h"
  33. #include "buffersrc.h"
  34. #include "formats.h"
  35. #include "internal.h"
  36. #include "video.h"
  37. #include "avcodec.h"
  38. typedef struct {
  39. const AVClass *class;
  40. AVFifoBuffer *fifo;
  41. AVRational time_base; ///< time_base to set in the output link
  42. AVRational frame_rate; ///< frame_rate to set in the output link
  43. unsigned nb_failed_requests;
  44. unsigned warning_limit;
  45. /* video only */
  46. int w, h;
  47. enum AVPixelFormat pix_fmt;
  48. AVRational pixel_aspect;
  49. char *sws_param;
  50. /* audio only */
  51. int sample_rate;
  52. enum AVSampleFormat sample_fmt;
  53. char *sample_fmt_str;
  54. int channels;
  55. uint64_t channel_layout;
  56. char *channel_layout_str;
  57. int eof;
  58. } BufferSourceContext;
  59. #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format)\
  60. if (c->w != width || c->h != height || c->pix_fmt != format) {\
  61. av_log(s, AV_LOG_INFO, "Changing frame properties on the fly is not supported by all filters.\n");\
  62. }
  63. #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, format)\
  64. if (c->sample_fmt != format || c->sample_rate != srate ||\
  65. c->channel_layout != ch_layout) {\
  66. av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
  67. return AVERROR(EINVAL);\
  68. }
  69. int av_buffersrc_add_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
  70. {
  71. return av_buffersrc_add_frame(ctx, frame);
  72. }
  73. int av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
  74. {
  75. AVFrame *copy;
  76. int ret = 0;
  77. int64_t layout = frame->channel_layout;
  78. if (layout && av_get_channel_layout_nb_channels(layout) != av_frame_get_channels(frame)) {
  79. av_log(0, AV_LOG_ERROR, "Layout indicates a different number of channels than actually present\n");
  80. return AVERROR(EINVAL);
  81. }
  82. if (!(copy = av_frame_alloc()))
  83. return AVERROR(ENOMEM);
  84. ret = av_frame_ref(copy, frame);
  85. if (ret >= 0)
  86. ret = av_buffersrc_add_frame(ctx, copy);
  87. av_frame_free(&copy);
  88. return ret;
  89. }
  90. int av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame)
  91. {
  92. BufferSourceContext *s = ctx->priv;
  93. AVFrame *copy;
  94. int ret;
  95. int64_t layout;
  96. if (!frame) {
  97. s->eof = 1;
  98. return 0;
  99. } else if (s->eof)
  100. return AVERROR(EINVAL);
  101. switch (ctx->outputs[0]->type) {
  102. case AVMEDIA_TYPE_VIDEO:
  103. CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
  104. frame->format);
  105. break;
  106. case AVMEDIA_TYPE_AUDIO:
  107. CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
  108. frame->format);
  109. layout = frame->channel_layout;
  110. if (layout && av_get_channel_layout_nb_channels(layout) != av_frame_get_channels(frame)) {
  111. av_log(0, AV_LOG_ERROR, "Layout indicates a different number of channels than actually present\n");
  112. return AVERROR(EINVAL);
  113. }
  114. break;
  115. default:
  116. return AVERROR(EINVAL);
  117. }
  118. if (!av_fifo_space(s->fifo) &&
  119. (ret = av_fifo_realloc2(s->fifo, av_fifo_size(s->fifo) +
  120. sizeof(copy))) < 0)
  121. return ret;
  122. if (!(copy = av_frame_alloc()))
  123. return AVERROR(ENOMEM);
  124. av_frame_move_ref(copy, frame);
  125. if ((ret = av_fifo_generic_write(s->fifo, &copy, sizeof(copy), NULL)) < 0) {
  126. av_frame_move_ref(frame, copy);
  127. av_frame_free(&copy);
  128. return ret;
  129. }
  130. return 0;
  131. }
  132. #if FF_API_AVFILTERBUFFER
  133. static void compat_free_buffer(void *opaque, uint8_t *data)
  134. {
  135. AVFilterBufferRef *buf = opaque;
  136. avfilter_unref_buffer(buf);
  137. }
  138. static void compat_unref_buffer(void *opaque, uint8_t *data)
  139. {
  140. AVBufferRef *buf = opaque;
  141. av_buffer_unref(&buf);
  142. }
  143. int av_buffersrc_add_ref(AVFilterContext *ctx, AVFilterBufferRef *buf,
  144. int flags)
  145. {
  146. BufferSourceContext *s = ctx->priv;
  147. AVFrame *frame = NULL;
  148. AVBufferRef *dummy_buf = NULL;
  149. int ret = 0, planes, i;
  150. if (!buf) {
  151. s->eof = 1;
  152. return 0;
  153. } else if (s->eof)
  154. return AVERROR(EINVAL);
  155. frame = av_frame_alloc();
  156. if (!frame)
  157. return AVERROR(ENOMEM);
  158. dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, buf,
  159. (buf->perms & AV_PERM_WRITE) ? 0 : AV_BUFFER_FLAG_READONLY);
  160. if (!dummy_buf) {
  161. ret = AVERROR(ENOMEM);
  162. goto fail;
  163. }
  164. if ((ret = avfilter_copy_buf_props(frame, buf)) < 0)
  165. goto fail;
  166. #define WRAP_PLANE(ref_out, data, data_size) \
  167. do { \
  168. AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
  169. if (!dummy_ref) { \
  170. ret = AVERROR(ENOMEM); \
  171. goto fail; \
  172. } \
  173. ref_out = av_buffer_create(data, data_size, compat_unref_buffer, \
  174. dummy_ref, (buf->perms & AV_PERM_WRITE) ? 0 : AV_BUFFER_FLAG_READONLY); \
  175. if (!ref_out) { \
  176. av_frame_unref(frame); \
  177. ret = AVERROR(ENOMEM); \
  178. goto fail; \
  179. } \
  180. } while (0)
  181. if (ctx->outputs[0]->type == AVMEDIA_TYPE_VIDEO) {
  182. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  183. if (!desc) {
  184. ret = AVERROR(EINVAL);
  185. goto fail;
  186. }
  187. planes = (desc->flags & PIX_FMT_PLANAR) ? desc->nb_components : 1;
  188. for (i = 0; i < planes; i++) {
  189. int h_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  190. int plane_size = (frame->width >> h_shift) * frame->linesize[i];
  191. WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
  192. }
  193. } else {
  194. int planar = av_sample_fmt_is_planar(frame->format);
  195. int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  196. planes = planar ? channels : 1;
  197. if (planes > FF_ARRAY_ELEMS(frame->buf)) {
  198. frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
  199. frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
  200. frame->nb_extended_buf);
  201. if (!frame->extended_buf) {
  202. ret = AVERROR(ENOMEM);
  203. goto fail;
  204. }
  205. }
  206. for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
  207. WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
  208. for (i = 0; i < planes - FF_ARRAY_ELEMS(frame->buf); i++)
  209. WRAP_PLANE(frame->extended_buf[i],
  210. frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
  211. frame->linesize[0]);
  212. }
  213. ret = av_buffersrc_add_frame_flags(ctx, frame, flags);
  214. fail:
  215. av_buffer_unref(&dummy_buf);
  216. av_frame_free(&frame);
  217. return ret;
  218. }
  219. int av_buffersrc_buffer(AVFilterContext *ctx, AVFilterBufferRef *buf)
  220. {
  221. return av_buffersrc_add_ref(ctx, buf, 0);
  222. }
  223. #endif
  224. #define OFFSET(x) offsetof(BufferSourceContext, x)
  225. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  226. static const AVOption buffer_options[] = {
  227. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  228. { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  229. { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = FLAGS },
  230. { "pix_fmt", NULL, OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, .flags = FLAGS },
  231. { "pixel_aspect", NULL, OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  232. { "sws_param", NULL, OFFSET(sws_param), AV_OPT_TYPE_STRING, .flags = FLAGS },
  233. { NULL },
  234. };
  235. #undef FLAGS
  236. AVFILTER_DEFINE_CLASS(buffer);
  237. static av_cold int init_video(AVFilterContext *ctx, const char *args)
  238. {
  239. BufferSourceContext *c = ctx->priv;
  240. char pix_fmt_str[128], *colon, *equal;
  241. int ret, n = 0;
  242. c->class = &buffer_class;
  243. if (!args) {
  244. av_log(ctx, AV_LOG_ERROR, "Arguments required\n");
  245. return AVERROR(EINVAL);
  246. }
  247. colon = strchr(args, ':');
  248. equal = strchr(args, '=');
  249. if (equal && (!colon || equal < colon)) {
  250. av_opt_set_defaults(c);
  251. ret = av_set_options_string(c, args, "=", ":");
  252. if (ret < 0)
  253. goto fail;
  254. } else {
  255. if (!args ||
  256. (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d", &c->w, &c->h, pix_fmt_str,
  257. &c->time_base.num, &c->time_base.den,
  258. &c->pixel_aspect.num, &c->pixel_aspect.den)) != 7) {
  259. av_log(ctx, AV_LOG_ERROR, "Expected 7 arguments, but %d found in '%s'\n", n, args);
  260. return AVERROR(EINVAL);
  261. }
  262. if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == AV_PIX_FMT_NONE) {
  263. char *tail;
  264. c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
  265. if (*tail || c->pix_fmt < 0 || c->pix_fmt >= AV_PIX_FMT_NB) {
  266. av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
  267. return AVERROR(EINVAL);
  268. }
  269. }
  270. }
  271. if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*))))
  272. return AVERROR(ENOMEM);
  273. 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",
  274. c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
  275. c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
  276. c->pixel_aspect.num, c->pixel_aspect.den, (char *)av_x_if_null(c->sws_param, ""));
  277. c->warning_limit = 100;
  278. return 0;
  279. fail:
  280. av_opt_free(c);
  281. return ret;
  282. }
  283. unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
  284. {
  285. return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
  286. }
  287. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  288. static const AVOption abuffer_options[] = {
  289. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  290. { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  291. { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = FLAGS },
  292. { "channels", NULL, OFFSET(channels), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  293. { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = FLAGS },
  294. { NULL },
  295. };
  296. AVFILTER_DEFINE_CLASS(abuffer);
  297. static av_cold int init_audio(AVFilterContext *ctx, const char *args)
  298. {
  299. BufferSourceContext *s = ctx->priv;
  300. int ret = 0;
  301. s->class = &abuffer_class;
  302. av_opt_set_defaults(s);
  303. if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
  304. goto fail;
  305. s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
  306. if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
  307. av_log(ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n",
  308. s->sample_fmt_str);
  309. ret = AVERROR(EINVAL);
  310. goto fail;
  311. }
  312. if (s->channel_layout_str) {
  313. int n;
  314. /* TODO reindent */
  315. s->channel_layout = av_get_channel_layout(s->channel_layout_str);
  316. if (!s->channel_layout) {
  317. av_log(ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n",
  318. s->channel_layout_str);
  319. ret = AVERROR(EINVAL);
  320. goto fail;
  321. }
  322. n = av_get_channel_layout_nb_channels(s->channel_layout);
  323. if (s->channels) {
  324. if (n != s->channels) {
  325. av_log(ctx, AV_LOG_ERROR,
  326. "Mismatching channel count %d and layout '%s' "
  327. "(%d channels)\n",
  328. s->channels, s->channel_layout_str, n);
  329. ret = AVERROR(EINVAL);
  330. goto fail;
  331. }
  332. }
  333. s->channels = n;
  334. } else if (!s->channels) {
  335. av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor "
  336. "channel layout specified\n");
  337. ret = AVERROR(EINVAL);
  338. goto fail;
  339. }
  340. if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*)))) {
  341. ret = AVERROR(ENOMEM);
  342. goto fail;
  343. }
  344. if (!s->time_base.num)
  345. s->time_base = (AVRational){1, s->sample_rate};
  346. av_log(ctx, AV_LOG_VERBOSE,
  347. "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
  348. s->time_base.num, s->time_base.den, s->sample_fmt_str,
  349. s->sample_rate, s->channel_layout_str);
  350. s->warning_limit = 100;
  351. fail:
  352. av_opt_free(s);
  353. return ret;
  354. }
  355. static av_cold void uninit(AVFilterContext *ctx)
  356. {
  357. BufferSourceContext *s = ctx->priv;
  358. while (s->fifo && av_fifo_size(s->fifo)) {
  359. AVFrame *frame;
  360. av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
  361. av_frame_free(&frame);
  362. }
  363. av_fifo_free(s->fifo);
  364. s->fifo = NULL;
  365. av_freep(&s->sws_param);
  366. }
  367. static int query_formats(AVFilterContext *ctx)
  368. {
  369. BufferSourceContext *c = ctx->priv;
  370. AVFilterChannelLayouts *channel_layouts = NULL;
  371. AVFilterFormats *formats = NULL;
  372. AVFilterFormats *samplerates = NULL;
  373. switch (ctx->outputs[0]->type) {
  374. case AVMEDIA_TYPE_VIDEO:
  375. ff_add_format(&formats, c->pix_fmt);
  376. ff_set_common_formats(ctx, formats);
  377. break;
  378. case AVMEDIA_TYPE_AUDIO:
  379. ff_add_format(&formats, c->sample_fmt);
  380. ff_set_common_formats(ctx, formats);
  381. ff_add_format(&samplerates, c->sample_rate);
  382. ff_set_common_samplerates(ctx, samplerates);
  383. ff_add_channel_layout(&channel_layouts,
  384. c->channel_layout ? c->channel_layout :
  385. FF_COUNT2LAYOUT(c->channels));
  386. ff_set_common_channel_layouts(ctx, channel_layouts);
  387. break;
  388. default:
  389. return AVERROR(EINVAL);
  390. }
  391. return 0;
  392. }
  393. static int config_props(AVFilterLink *link)
  394. {
  395. BufferSourceContext *c = link->src->priv;
  396. switch (link->type) {
  397. case AVMEDIA_TYPE_VIDEO:
  398. link->w = c->w;
  399. link->h = c->h;
  400. link->sample_aspect_ratio = c->pixel_aspect;
  401. break;
  402. case AVMEDIA_TYPE_AUDIO:
  403. if (!c->channel_layout)
  404. c->channel_layout = link->channel_layout;
  405. break;
  406. default:
  407. return AVERROR(EINVAL);
  408. }
  409. link->time_base = c->time_base;
  410. link->frame_rate = c->frame_rate;
  411. return 0;
  412. }
  413. static int request_frame(AVFilterLink *link)
  414. {
  415. BufferSourceContext *c = link->src->priv;
  416. AVFrame *frame;
  417. int ret = 0;
  418. if (!av_fifo_size(c->fifo)) {
  419. if (c->eof)
  420. return AVERROR_EOF;
  421. c->nb_failed_requests++;
  422. return AVERROR(EAGAIN);
  423. }
  424. av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
  425. /* CIG TODO do not ignore error */
  426. ff_filter_frame(link, frame);
  427. return ret;
  428. }
  429. static int poll_frame(AVFilterLink *link)
  430. {
  431. BufferSourceContext *c = link->src->priv;
  432. int size = av_fifo_size(c->fifo);
  433. if (!size && c->eof)
  434. return AVERROR_EOF;
  435. return size/sizeof(AVFrame*);
  436. }
  437. static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
  438. {
  439. .name = "default",
  440. .type = AVMEDIA_TYPE_VIDEO,
  441. .request_frame = request_frame,
  442. .poll_frame = poll_frame,
  443. .config_props = config_props,
  444. },
  445. { NULL }
  446. };
  447. AVFilter avfilter_vsrc_buffer = {
  448. .name = "buffer",
  449. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  450. .priv_size = sizeof(BufferSourceContext),
  451. .query_formats = query_formats,
  452. .init = init_video,
  453. .uninit = uninit,
  454. .inputs = NULL,
  455. .outputs = avfilter_vsrc_buffer_outputs,
  456. .priv_class = &buffer_class,
  457. };
  458. static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
  459. {
  460. .name = "default",
  461. .type = AVMEDIA_TYPE_AUDIO,
  462. .request_frame = request_frame,
  463. .poll_frame = poll_frame,
  464. .config_props = config_props,
  465. },
  466. { NULL }
  467. };
  468. AVFilter avfilter_asrc_abuffer = {
  469. .name = "abuffer",
  470. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
  471. .priv_size = sizeof(BufferSourceContext),
  472. .query_formats = query_formats,
  473. .init = init_audio,
  474. .uninit = uninit,
  475. .inputs = NULL,
  476. .outputs = avfilter_asrc_abuffer_outputs,
  477. .priv_class = &abuffer_class,
  478. };