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.

292 lines
9.3KB

  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 "avfilter.h"
  25. #include "internal.h"
  26. #include "avcodec.h"
  27. #include "buffersrc.h"
  28. #include "vsrc_buffer.h"
  29. #include "libavutil/fifo.h"
  30. #include "libavutil/imgutils.h"
  31. typedef struct {
  32. AVFilterContext *scale;
  33. AVFifoBuffer *fifo;
  34. int h, w;
  35. enum PixelFormat pix_fmt;
  36. AVRational time_base; ///< time_base to set in the output link
  37. AVRational sample_aspect_ratio;
  38. char sws_param[256];
  39. int eof;
  40. } BufferSourceContext;
  41. #define CHECK_PARAM_CHANGE(s, c, width, height, format)\
  42. if (c->w != width || c->h != height || c->pix_fmt != format) {\
  43. av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
  44. return AVERROR(EINVAL);\
  45. }
  46. int av_vsrc_buffer_add_video_buffer_ref(AVFilterContext *buffer_filter,
  47. AVFilterBufferRef *picref, int flags)
  48. {
  49. BufferSourceContext *c = buffer_filter->priv;
  50. AVFilterLink *outlink = buffer_filter->outputs[0];
  51. AVFilterBufferRef *buf;
  52. int ret;
  53. if (!picref) {
  54. c->eof = 1;
  55. return 0;
  56. } else if (c->eof)
  57. return AVERROR(EINVAL);
  58. if (!av_fifo_space(c->fifo) &&
  59. (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
  60. sizeof(buf))) < 0)
  61. return ret;
  62. if (picref->video->w != c->w || picref->video->h != c->h || picref->format != c->pix_fmt) {
  63. AVFilterContext *scale = buffer_filter->outputs[0]->dst;
  64. AVFilterLink *link;
  65. char scale_param[1024];
  66. av_log(buffer_filter, AV_LOG_INFO,
  67. "Buffer video input changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
  68. c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
  69. picref->video->w, picref->video->h, av_pix_fmt_descriptors[picref->format].name);
  70. if (!scale || strcmp(scale->filter->name, "scale")) {
  71. AVFilter *f = avfilter_get_by_name("scale");
  72. av_log(buffer_filter, AV_LOG_INFO, "Inserting scaler filter\n");
  73. if ((ret = avfilter_open(&scale, f, "Input equalizer")) < 0)
  74. return ret;
  75. c->scale = scale;
  76. snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s", c->w, c->h, c->sws_param);
  77. if ((ret = avfilter_init_filter(scale, scale_param, NULL)) < 0) {
  78. return ret;
  79. }
  80. if ((ret = avfilter_insert_filter(buffer_filter->outputs[0], scale, 0, 0)) < 0) {
  81. return ret;
  82. }
  83. scale->outputs[0]->time_base = scale->inputs[0]->time_base;
  84. scale->outputs[0]->format= c->pix_fmt;
  85. } else if (!strcmp(scale->filter->name, "scale")) {
  86. snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s",
  87. scale->outputs[0]->w, scale->outputs[0]->h, c->sws_param);
  88. scale->filter->init(scale, scale_param, NULL);
  89. }
  90. c->pix_fmt = scale->inputs[0]->format = picref->format;
  91. c->w = scale->inputs[0]->w = picref->video->w;
  92. c->h = scale->inputs[0]->h = picref->video->h;
  93. link = scale->outputs[0];
  94. if ((ret = link->srcpad->config_props(link)) < 0)
  95. return ret;
  96. }
  97. buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
  98. picref->video->w, picref->video->h);
  99. av_image_copy(buf->data, buf->linesize,
  100. (void*)picref->data, picref->linesize,
  101. picref->format, picref->video->w, picref->video->h);
  102. avfilter_copy_buffer_ref_props(buf, picref);
  103. if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0) {
  104. avfilter_unref_buffer(buf);
  105. return ret;
  106. }
  107. return 0;
  108. }
  109. int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
  110. {
  111. BufferSourceContext *c = s->priv;
  112. int ret;
  113. if (!buf) {
  114. c->eof = 1;
  115. return 0;
  116. } else if (c->eof)
  117. return AVERROR(EINVAL);
  118. if (!av_fifo_space(c->fifo) &&
  119. (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
  120. sizeof(buf))) < 0)
  121. return ret;
  122. // CHECK_PARAM_CHANGE(s, c, buf->video->w, buf->video->h, buf->format);
  123. if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0)
  124. return ret;
  125. return 0;
  126. }
  127. #if CONFIG_AVCODEC
  128. #include "avcodec.h"
  129. int av_vsrc_buffer_add_frame(AVFilterContext *buffer_src,
  130. const AVFrame *frame, int flags)
  131. {
  132. BufferSourceContext *c = buffer_src->priv;
  133. AVFilterBufferRef *picref;
  134. int ret;
  135. if (!frame) {
  136. c->eof = 1;
  137. return 0;
  138. } else if (c->eof)
  139. return AVERROR(EINVAL);
  140. picref = avfilter_get_video_buffer_ref_from_frame(frame, AV_PERM_WRITE);
  141. if (!picref)
  142. return AVERROR(ENOMEM);
  143. ret = av_vsrc_buffer_add_video_buffer_ref(buffer_src, picref, flags);
  144. picref->buf->data[0] = NULL;
  145. avfilter_unref_buffer(picref);
  146. return ret;
  147. }
  148. #endif
  149. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  150. {
  151. BufferSourceContext *c = ctx->priv;
  152. char pix_fmt_str[128];
  153. int ret, n = 0;
  154. *c->sws_param = 0;
  155. if (!args ||
  156. (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
  157. &c->time_base.num, &c->time_base.den,
  158. &c->sample_aspect_ratio.num, &c->sample_aspect_ratio.den, c->sws_param)) < 7) {
  159. av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args);
  160. return AVERROR(EINVAL);
  161. }
  162. if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
  163. return ret;
  164. if (!(c->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*))))
  165. return AVERROR(ENOMEM);
  166. av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n",
  167. c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
  168. c->time_base.num, c->time_base.den,
  169. c->sample_aspect_ratio.num, c->sample_aspect_ratio.den, c->sws_param);
  170. return 0;
  171. }
  172. static av_cold void uninit(AVFilterContext *ctx)
  173. {
  174. BufferSourceContext *s = ctx->priv;
  175. while (s->fifo && av_fifo_size(s->fifo)) {
  176. AVFilterBufferRef *buf;
  177. av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
  178. avfilter_unref_buffer(buf);
  179. }
  180. av_fifo_free(s->fifo);
  181. s->fifo = NULL;
  182. avfilter_free(s->scale);
  183. s->scale = NULL;
  184. }
  185. static int query_formats(AVFilterContext *ctx)
  186. {
  187. BufferSourceContext *c = ctx->priv;
  188. enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
  189. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  190. return 0;
  191. }
  192. static int config_props(AVFilterLink *link)
  193. {
  194. BufferSourceContext *c = link->src->priv;
  195. link->w = c->w;
  196. link->h = c->h;
  197. link->sample_aspect_ratio = c->sample_aspect_ratio;
  198. link->time_base = c->time_base;
  199. return 0;
  200. }
  201. static int request_frame(AVFilterLink *link)
  202. {
  203. BufferSourceContext *c = link->src->priv;
  204. AVFilterBufferRef *buf;
  205. if (!av_fifo_size(c->fifo)) {
  206. if (c->eof)
  207. return AVERROR_EOF;
  208. av_log(link->src, AV_LOG_WARNING,
  209. "request_frame() called with no available frame!\n");
  210. return AVERROR(EINVAL);
  211. }
  212. av_fifo_generic_read(c->fifo, &buf, sizeof(buf), NULL);
  213. avfilter_start_frame(link, avfilter_ref_buffer(buf, ~0));
  214. avfilter_draw_slice(link, 0, link->h, 1);
  215. avfilter_end_frame(link);
  216. avfilter_unref_buffer(buf);
  217. return 0;
  218. }
  219. static int poll_frame(AVFilterLink *link)
  220. {
  221. BufferSourceContext *c = link->src->priv;
  222. int size = av_fifo_size(c->fifo);
  223. if (!size && c->eof)
  224. return AVERROR_EOF;
  225. return size/sizeof(AVFilterBufferRef*);
  226. }
  227. AVFilter avfilter_vsrc_buffer = {
  228. .name = "buffer",
  229. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  230. .priv_size = sizeof(BufferSourceContext),
  231. .query_formats = query_formats,
  232. .init = init,
  233. .uninit = uninit,
  234. .inputs = (const AVFilterPad[]) {{ .name = NULL }},
  235. .outputs = (const AVFilterPad[]) {{ .name = "default",
  236. .type = AVMEDIA_TYPE_VIDEO,
  237. .request_frame = request_frame,
  238. .poll_frame = poll_frame,
  239. .config_props = config_props, },
  240. { .name = NULL}},
  241. };