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.

465 lines
14KB

  1. /*
  2. * Copyright (c) 2008 Vitor Sessak
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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. typedef struct {
  38. const AVClass *class;
  39. AVFifoBuffer *fifo;
  40. AVRational time_base; ///< time_base to set in the output link
  41. /* video only */
  42. int h, w;
  43. enum AVPixelFormat pix_fmt;
  44. AVRational pixel_aspect;
  45. /* audio only */
  46. int sample_rate;
  47. enum AVSampleFormat sample_fmt;
  48. char *sample_fmt_str;
  49. uint64_t channel_layout;
  50. char *channel_layout_str;
  51. int eof;
  52. } BufferSourceContext;
  53. #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format)\
  54. if (c->w != width || c->h != height || c->pix_fmt != format) {\
  55. av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
  56. return AVERROR(EINVAL);\
  57. }
  58. #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, format)\
  59. if (c->sample_fmt != format || c->sample_rate != srate ||\
  60. c->channel_layout != ch_layout) {\
  61. av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
  62. return AVERROR(EINVAL);\
  63. }
  64. int av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
  65. {
  66. AVFrame *copy;
  67. int ret = 0;
  68. if (!(copy = av_frame_alloc()))
  69. return AVERROR(ENOMEM);
  70. ret = av_frame_ref(copy, frame);
  71. if (ret >= 0)
  72. ret = av_buffersrc_add_frame(ctx, copy);
  73. av_frame_free(&copy);
  74. return ret;
  75. }
  76. int av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame)
  77. {
  78. BufferSourceContext *s = ctx->priv;
  79. AVFrame *copy;
  80. int ret;
  81. if (!frame) {
  82. s->eof = 1;
  83. return 0;
  84. } else if (s->eof)
  85. return AVERROR(EINVAL);
  86. switch (ctx->outputs[0]->type) {
  87. case AVMEDIA_TYPE_VIDEO:
  88. CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
  89. frame->format);
  90. break;
  91. case AVMEDIA_TYPE_AUDIO:
  92. CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
  93. frame->format);
  94. break;
  95. default:
  96. return AVERROR(EINVAL);
  97. }
  98. if (!av_fifo_space(s->fifo) &&
  99. (ret = av_fifo_realloc2(s->fifo, av_fifo_size(s->fifo) +
  100. sizeof(copy))) < 0)
  101. return ret;
  102. if (!(copy = av_frame_alloc()))
  103. return AVERROR(ENOMEM);
  104. av_frame_move_ref(copy, frame);
  105. if ((ret = av_fifo_generic_write(s->fifo, &copy, sizeof(copy), NULL)) < 0) {
  106. av_frame_move_ref(frame, copy);
  107. av_frame_free(&copy);
  108. return ret;
  109. }
  110. return 0;
  111. }
  112. #if FF_API_AVFILTERBUFFER
  113. static void compat_free_buffer(void *opaque, uint8_t *data)
  114. {
  115. AVFilterBufferRef *buf = opaque;
  116. avfilter_unref_buffer(buf);
  117. }
  118. static void compat_unref_buffer(void *opaque, uint8_t *data)
  119. {
  120. AVBufferRef *buf = opaque;
  121. av_buffer_unref(&buf);
  122. }
  123. int av_buffersrc_buffer(AVFilterContext *ctx, AVFilterBufferRef *buf)
  124. {
  125. BufferSourceContext *s = ctx->priv;
  126. AVFrame *frame = NULL;
  127. AVBufferRef *dummy_buf = NULL;
  128. int ret = 0, planes, i;
  129. if (!buf) {
  130. s->eof = 1;
  131. return 0;
  132. } else if (s->eof)
  133. return AVERROR(EINVAL);
  134. frame = av_frame_alloc();
  135. if (!frame)
  136. return AVERROR(ENOMEM);
  137. dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, buf, 0);
  138. if (!dummy_buf) {
  139. ret = AVERROR(ENOMEM);
  140. goto fail;
  141. }
  142. if ((ret = avfilter_copy_buf_props(frame, buf)) < 0)
  143. goto fail;
  144. #define WRAP_PLANE(ref_out, data, data_size) \
  145. do { \
  146. AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
  147. if (!dummy_ref) { \
  148. ret = AVERROR(ENOMEM); \
  149. goto fail; \
  150. } \
  151. ref_out = av_buffer_create(data, data_size, compat_unref_buffer, \
  152. dummy_ref, 0); \
  153. if (!ref_out) { \
  154. av_frame_unref(frame); \
  155. ret = AVERROR(ENOMEM); \
  156. goto fail; \
  157. } \
  158. } while (0)
  159. if (ctx->outputs[0]->type == AVMEDIA_TYPE_VIDEO) {
  160. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  161. planes = av_pix_fmt_count_planes(frame->format);
  162. if (!desc || planes <= 0) {
  163. ret = AVERROR(EINVAL);
  164. goto fail;
  165. }
  166. for (i = 0; i < planes; i++) {
  167. int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_w : 0;
  168. int plane_size = (frame->height >> v_shift) * frame->linesize[i];
  169. WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
  170. }
  171. } else {
  172. int planar = av_sample_fmt_is_planar(frame->format);
  173. int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  174. planes = planar ? channels : 1;
  175. if (planes > FF_ARRAY_ELEMS(frame->buf)) {
  176. frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
  177. frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
  178. frame->nb_extended_buf);
  179. if (!frame->extended_buf) {
  180. ret = AVERROR(ENOMEM);
  181. goto fail;
  182. }
  183. }
  184. for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
  185. WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
  186. for (i = 0; i < planes - FF_ARRAY_ELEMS(frame->buf); i++)
  187. WRAP_PLANE(frame->extended_buf[i],
  188. frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
  189. frame->linesize[0]);
  190. }
  191. ret = av_buffersrc_add_frame(ctx, frame);
  192. fail:
  193. av_buffer_unref(&dummy_buf);
  194. av_frame_free(&frame);
  195. return ret;
  196. }
  197. #endif
  198. static av_cold int init_video(AVFilterContext *ctx, const char *args)
  199. {
  200. BufferSourceContext *c = ctx->priv;
  201. char pix_fmt_str[128];
  202. int n = 0;
  203. if (!args ||
  204. (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d", &c->w, &c->h, pix_fmt_str,
  205. &c->time_base.num, &c->time_base.den,
  206. &c->pixel_aspect.num, &c->pixel_aspect.den)) != 7) {
  207. av_log(ctx, AV_LOG_ERROR, "Expected 7 arguments, but %d found in '%s'\n", n, args);
  208. return AVERROR(EINVAL);
  209. }
  210. if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == AV_PIX_FMT_NONE) {
  211. char *tail;
  212. c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
  213. if (*tail || c->pix_fmt < 0 || c->pix_fmt >= AV_PIX_FMT_NB) {
  214. av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
  215. return AVERROR(EINVAL);
  216. }
  217. }
  218. if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*))))
  219. return AVERROR(ENOMEM);
  220. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d pixfmt:%s\n", c->w, c->h, av_get_pix_fmt_name(c->pix_fmt));
  221. return 0;
  222. }
  223. #define OFFSET(x) offsetof(BufferSourceContext, x)
  224. #define A AV_OPT_FLAG_AUDIO_PARAM
  225. static const AVOption audio_options[] = {
  226. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
  227. { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
  228. { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = A },
  229. { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
  230. { NULL },
  231. };
  232. static const AVClass abuffer_class = {
  233. .class_name = "abuffer source",
  234. .item_name = av_default_item_name,
  235. .option = audio_options,
  236. .version = LIBAVUTIL_VERSION_INT,
  237. };
  238. static av_cold int init_audio(AVFilterContext *ctx, const char *args)
  239. {
  240. BufferSourceContext *s = ctx->priv;
  241. int ret = 0;
  242. s->class = &abuffer_class;
  243. av_opt_set_defaults(s);
  244. if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
  245. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: %s.\n", args);
  246. goto fail;
  247. }
  248. s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
  249. if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
  250. av_log(ctx, AV_LOG_ERROR, "Invalid sample format %s.\n",
  251. s->sample_fmt_str);
  252. ret = AVERROR(EINVAL);
  253. goto fail;
  254. }
  255. s->channel_layout = av_get_channel_layout(s->channel_layout_str);
  256. if (!s->channel_layout) {
  257. av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
  258. s->channel_layout_str);
  259. ret = AVERROR(EINVAL);
  260. goto fail;
  261. }
  262. if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*)))) {
  263. ret = AVERROR(ENOMEM);
  264. goto fail;
  265. }
  266. if (!s->time_base.num)
  267. s->time_base = (AVRational){1, s->sample_rate};
  268. av_log(ctx, AV_LOG_VERBOSE, "tb:%d/%d samplefmt:%s samplerate: %d "
  269. "ch layout:%s\n", s->time_base.num, s->time_base.den, s->sample_fmt_str,
  270. s->sample_rate, s->channel_layout_str);
  271. fail:
  272. av_opt_free(s);
  273. return ret;
  274. }
  275. static av_cold void uninit(AVFilterContext *ctx)
  276. {
  277. BufferSourceContext *s = ctx->priv;
  278. while (s->fifo && av_fifo_size(s->fifo)) {
  279. AVFrame *frame;
  280. av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
  281. av_frame_free(&frame);
  282. }
  283. av_fifo_free(s->fifo);
  284. s->fifo = NULL;
  285. }
  286. static int query_formats(AVFilterContext *ctx)
  287. {
  288. BufferSourceContext *c = ctx->priv;
  289. AVFilterChannelLayouts *channel_layouts = NULL;
  290. AVFilterFormats *formats = NULL;
  291. AVFilterFormats *samplerates = NULL;
  292. switch (ctx->outputs[0]->type) {
  293. case AVMEDIA_TYPE_VIDEO:
  294. ff_add_format(&formats, c->pix_fmt);
  295. ff_set_common_formats(ctx, formats);
  296. break;
  297. case AVMEDIA_TYPE_AUDIO:
  298. ff_add_format(&formats, c->sample_fmt);
  299. ff_set_common_formats(ctx, formats);
  300. ff_add_format(&samplerates, c->sample_rate);
  301. ff_set_common_samplerates(ctx, samplerates);
  302. ff_add_channel_layout(&channel_layouts, c->channel_layout);
  303. ff_set_common_channel_layouts(ctx, channel_layouts);
  304. break;
  305. default:
  306. return AVERROR(EINVAL);
  307. }
  308. return 0;
  309. }
  310. static int config_props(AVFilterLink *link)
  311. {
  312. BufferSourceContext *c = link->src->priv;
  313. switch (link->type) {
  314. case AVMEDIA_TYPE_VIDEO:
  315. link->w = c->w;
  316. link->h = c->h;
  317. link->sample_aspect_ratio = c->pixel_aspect;
  318. break;
  319. case AVMEDIA_TYPE_AUDIO:
  320. link->channel_layout = c->channel_layout;
  321. link->sample_rate = c->sample_rate;
  322. break;
  323. default:
  324. return AVERROR(EINVAL);
  325. }
  326. link->time_base = c->time_base;
  327. return 0;
  328. }
  329. static int request_frame(AVFilterLink *link)
  330. {
  331. BufferSourceContext *c = link->src->priv;
  332. AVFrame *frame;
  333. int ret = 0;
  334. if (!av_fifo_size(c->fifo)) {
  335. if (c->eof)
  336. return AVERROR_EOF;
  337. return AVERROR(EAGAIN);
  338. }
  339. av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
  340. ff_filter_frame(link, frame);
  341. return ret;
  342. }
  343. static int poll_frame(AVFilterLink *link)
  344. {
  345. BufferSourceContext *c = link->src->priv;
  346. int size = av_fifo_size(c->fifo);
  347. if (!size && c->eof)
  348. return AVERROR_EOF;
  349. return size/sizeof(AVFrame*);
  350. }
  351. static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
  352. {
  353. .name = "default",
  354. .type = AVMEDIA_TYPE_VIDEO,
  355. .request_frame = request_frame,
  356. .poll_frame = poll_frame,
  357. .config_props = config_props,
  358. },
  359. { NULL }
  360. };
  361. AVFilter avfilter_vsrc_buffer = {
  362. .name = "buffer",
  363. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  364. .priv_size = sizeof(BufferSourceContext),
  365. .query_formats = query_formats,
  366. .init = init_video,
  367. .uninit = uninit,
  368. .inputs = NULL,
  369. .outputs = avfilter_vsrc_buffer_outputs,
  370. };
  371. static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
  372. {
  373. .name = "default",
  374. .type = AVMEDIA_TYPE_AUDIO,
  375. .request_frame = request_frame,
  376. .poll_frame = poll_frame,
  377. .config_props = config_props,
  378. },
  379. { NULL }
  380. };
  381. AVFilter avfilter_asrc_abuffer = {
  382. .name = "abuffer",
  383. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
  384. .priv_size = sizeof(BufferSourceContext),
  385. .query_formats = query_formats,
  386. .init = init_audio,
  387. .uninit = uninit,
  388. .inputs = NULL,
  389. .outputs = avfilter_asrc_abuffer_outputs,
  390. };