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.

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