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.

239 lines
7.9KB

  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/imgutils.h"
  30. typedef struct {
  31. AVFilterBufferRef *picref;
  32. int h, w;
  33. enum PixelFormat pix_fmt;
  34. AVRational time_base; ///< time_base to set in the output link
  35. AVRational sample_aspect_ratio;
  36. char sws_param[256];
  37. } BufferSourceContext;
  38. int av_vsrc_buffer_add_video_buffer_ref(AVFilterContext *buffer_filter,
  39. AVFilterBufferRef *picref, int flags)
  40. {
  41. BufferSourceContext *c = buffer_filter->priv;
  42. AVFilterLink *outlink = buffer_filter->outputs[0];
  43. int ret;
  44. if (c->picref) {
  45. if (flags & AV_VSRC_BUF_FLAG_OVERWRITE) {
  46. avfilter_unref_buffer(c->picref);
  47. c->picref = NULL;
  48. } else {
  49. av_log(buffer_filter, AV_LOG_ERROR,
  50. "Buffering several frames is not supported. "
  51. "Please consume all available frames before adding a new one.\n");
  52. return AVERROR(EINVAL);
  53. }
  54. }
  55. if (picref->video->w != c->w || picref->video->h != c->h || picref->format != c->pix_fmt) {
  56. AVFilterContext *scale = buffer_filter->outputs[0]->dst;
  57. AVFilterLink *link;
  58. char scale_param[1024];
  59. av_log(buffer_filter, AV_LOG_INFO,
  60. "Buffer video input changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
  61. c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
  62. picref->video->w, picref->video->h, av_pix_fmt_descriptors[picref->format].name);
  63. if (!scale || strcmp(scale->filter->name, "scale")) {
  64. AVFilter *f = avfilter_get_by_name("scale");
  65. av_log(buffer_filter, AV_LOG_INFO, "Inserting scaler filter\n");
  66. if ((ret = avfilter_open(&scale, f, "Input equalizer")) < 0)
  67. return ret;
  68. snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s", c->w, c->h, c->sws_param);
  69. if ((ret = avfilter_init_filter(scale, scale_param, NULL)) < 0) {
  70. avfilter_free(scale);
  71. return ret;
  72. }
  73. if ((ret = avfilter_insert_filter(buffer_filter->outputs[0], scale, 0, 0)) < 0) {
  74. avfilter_free(scale);
  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. c->picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
  92. picref->video->w, picref->video->h);
  93. av_image_copy(c->picref->data, c->picref->linesize,
  94. (void*)picref->data, picref->linesize,
  95. picref->format, picref->video->w, picref->video->h);
  96. avfilter_copy_buffer_ref_props(c->picref, picref);
  97. return 0;
  98. }
  99. int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
  100. {
  101. BufferSourceContext *c = s->priv;
  102. if (c->picref) {
  103. av_log(s, AV_LOG_ERROR,
  104. "Buffering several frames is not supported. "
  105. "Please consume all available frames before adding a new one.\n"
  106. );
  107. return AVERROR(EINVAL);
  108. }
  109. c->picref = buf;
  110. return 0;
  111. }
  112. #if CONFIG_AVCODEC
  113. #include "avcodec.h"
  114. int av_vsrc_buffer_add_frame(AVFilterContext *buffer_src,
  115. const AVFrame *frame, int flags)
  116. {
  117. int ret;
  118. AVFilterBufferRef *picref =
  119. avfilter_get_video_buffer_ref_from_frame(frame, AV_PERM_WRITE);
  120. if (!picref)
  121. return AVERROR(ENOMEM);
  122. ret = av_vsrc_buffer_add_video_buffer_ref(buffer_src, picref, flags);
  123. picref->buf->data[0] = NULL;
  124. avfilter_unref_buffer(picref);
  125. return ret;
  126. }
  127. #endif
  128. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  129. {
  130. BufferSourceContext *c = ctx->priv;
  131. char pix_fmt_str[128];
  132. int ret, n = 0;
  133. *c->sws_param = 0;
  134. if (!args ||
  135. (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
  136. &c->time_base.num, &c->time_base.den,
  137. &c->sample_aspect_ratio.num, &c->sample_aspect_ratio.den, c->sws_param)) < 7) {
  138. av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args);
  139. return AVERROR(EINVAL);
  140. }
  141. if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
  142. return ret;
  143. av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n",
  144. c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
  145. c->time_base.num, c->time_base.den,
  146. c->sample_aspect_ratio.num, c->sample_aspect_ratio.den, c->sws_param);
  147. return 0;
  148. }
  149. static int query_formats(AVFilterContext *ctx)
  150. {
  151. BufferSourceContext *c = ctx->priv;
  152. enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
  153. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  154. return 0;
  155. }
  156. static int config_props(AVFilterLink *link)
  157. {
  158. BufferSourceContext *c = link->src->priv;
  159. link->w = c->w;
  160. link->h = c->h;
  161. link->sample_aspect_ratio = c->sample_aspect_ratio;
  162. link->time_base = c->time_base;
  163. return 0;
  164. }
  165. static int request_frame(AVFilterLink *link)
  166. {
  167. BufferSourceContext *c = link->src->priv;
  168. if (!c->picref) {
  169. av_log(link->src, AV_LOG_WARNING,
  170. "request_frame() called with no available frame!\n");
  171. return AVERROR(EINVAL);
  172. }
  173. avfilter_start_frame(link, avfilter_ref_buffer(c->picref, ~0));
  174. avfilter_draw_slice(link, 0, link->h, 1);
  175. avfilter_end_frame(link);
  176. avfilter_unref_buffer(c->picref);
  177. c->picref = NULL;
  178. return 0;
  179. }
  180. static int poll_frame(AVFilterLink *link)
  181. {
  182. BufferSourceContext *c = link->src->priv;
  183. return !!(c->picref);
  184. }
  185. AVFilter avfilter_vsrc_buffer = {
  186. .name = "buffer",
  187. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  188. .priv_size = sizeof(BufferSourceContext),
  189. .query_formats = query_formats,
  190. .init = init,
  191. .inputs = (const AVFilterPad[]) {{ .name = NULL }},
  192. .outputs = (const AVFilterPad[]) {{ .name = "default",
  193. .type = AVMEDIA_TYPE_VIDEO,
  194. .request_frame = request_frame,
  195. .poll_frame = poll_frame,
  196. .config_props = config_props, },
  197. { .name = NULL}},
  198. };