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.

219 lines
7.4KB

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