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.

266 lines
8.7KB

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