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.

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