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.

478 lines
15KB

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