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.

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