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.

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