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.

271 lines
8.8KB

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