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.

217 lines
7.1KB

  1. /*
  2. * copyright (c) 2007 Bobby Bingham
  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. * scale video filter
  23. */
  24. #include "avfilter.h"
  25. #include "libavutil/pixdesc.h"
  26. #include "libswscale/swscale.h"
  27. typedef struct {
  28. struct SwsContext *sws; ///< software scaler context
  29. /**
  30. * New dimensions. Special values are:
  31. * 0 = original width/height
  32. * -1 = keep original aspect
  33. */
  34. int w, h;
  35. unsigned int flags; ///sws flags
  36. int hsub, vsub; ///< chroma subsampling
  37. int slice_y; ///< top of current output slice
  38. int input_is_pal; ///< set to 1 if the input format is paletted
  39. } ScaleContext;
  40. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  41. {
  42. ScaleContext *scale = ctx->priv;
  43. scale->flags = SWS_BILINEAR;
  44. if (args){
  45. sscanf(args, "%d:%d", &scale->w, &scale->h);
  46. sscanf(strstr(args,"flags="), "flags=%i", &scale->flags);
  47. }
  48. /* sanity check params */
  49. if (scale->w < -1 || scale->h < -1) {
  50. av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
  51. return -1;
  52. }
  53. if (scale->w == -1 && scale->h == -1)
  54. scale->w = scale->h = 0;
  55. return 0;
  56. }
  57. static av_cold void uninit(AVFilterContext *ctx)
  58. {
  59. ScaleContext *scale = ctx->priv;
  60. sws_freeContext(scale->sws);
  61. scale->sws = NULL;
  62. }
  63. static int query_formats(AVFilterContext *ctx)
  64. {
  65. AVFilterFormats *formats;
  66. enum PixelFormat pix_fmt;
  67. int ret;
  68. if (ctx->inputs[0]) {
  69. formats = NULL;
  70. for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
  71. if ( sws_isSupportedInput(pix_fmt)
  72. && (ret = avfilter_add_colorspace(&formats, pix_fmt)) < 0) {
  73. avfilter_formats_unref(&formats);
  74. return ret;
  75. }
  76. avfilter_formats_ref(formats, &ctx->inputs[0]->out_formats);
  77. }
  78. if (ctx->outputs[0]) {
  79. formats = NULL;
  80. for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
  81. if ( sws_isSupportedOutput(pix_fmt)
  82. && (ret = avfilter_add_colorspace(&formats, pix_fmt)) < 0) {
  83. avfilter_formats_unref(&formats);
  84. return ret;
  85. }
  86. avfilter_formats_ref(formats, &ctx->outputs[0]->in_formats);
  87. }
  88. return 0;
  89. }
  90. static int config_props(AVFilterLink *outlink)
  91. {
  92. AVFilterContext *ctx = outlink->src;
  93. AVFilterLink *inlink = outlink->src->inputs[0];
  94. ScaleContext *scale = ctx->priv;
  95. int64_t w, h;
  96. if (!(w = scale->w))
  97. w = inlink->w;
  98. if (!(h = scale->h))
  99. h = inlink->h;
  100. if (w == -1)
  101. w = av_rescale(h, inlink->w, inlink->h);
  102. if (h == -1)
  103. h = av_rescale(w, inlink->h, inlink->w);
  104. if (w > INT_MAX || h > INT_MAX ||
  105. (h * inlink->w) > INT_MAX ||
  106. (w * inlink->h) > INT_MAX)
  107. av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
  108. outlink->w = w;
  109. outlink->h = h;
  110. /* TODO: make algorithm configurable */
  111. scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
  112. outlink->w, outlink->h, outlink->format,
  113. scale->flags, NULL, NULL, NULL);
  114. av_log(ctx, AV_LOG_INFO, "w:%d h:%d fmt:%s\n",
  115. outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name);
  116. scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL;
  117. return !scale->sws;
  118. }
  119. static void start_frame(AVFilterLink *link, AVFilterPicRef *picref)
  120. {
  121. ScaleContext *scale = link->dst->priv;
  122. AVFilterLink *outlink = link->dst->outputs[0];
  123. AVFilterPicRef *outpicref;
  124. scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
  125. scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  126. outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  127. outpicref->pts = picref->pts;
  128. outpicref->pos = picref->pos;
  129. outpicref->interlaced = picref->interlaced;
  130. outpicref->top_field_first = picref->top_field_first;
  131. outlink->outpic = outpicref;
  132. av_reduce(&outpicref->pixel_aspect.num, &outpicref->pixel_aspect.den,
  133. (int64_t)picref->pixel_aspect.num * outlink->h * link->w,
  134. (int64_t)picref->pixel_aspect.den * outlink->w * link->h,
  135. INT_MAX);
  136. scale->slice_y = 0;
  137. avfilter_start_frame(outlink, avfilter_ref_pic(outpicref, ~0));
  138. }
  139. static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  140. {
  141. ScaleContext *scale = link->dst->priv;
  142. int out_h;
  143. AVFilterPicRef *cur_pic = link->cur_pic;
  144. uint8_t *data[4];
  145. if (scale->slice_y == 0 && slice_dir == -1)
  146. scale->slice_y = link->dst->outputs[0]->h;
  147. data[0] = cur_pic->data[0] + y * cur_pic->linesize[0];
  148. data[1] = scale->input_is_pal ?
  149. cur_pic->data[1] :
  150. cur_pic->data[1] + (y>>scale->vsub) * cur_pic->linesize[1];
  151. data[2] = cur_pic->data[2] + (y>>scale->vsub) * cur_pic->linesize[2];
  152. data[3] = cur_pic->data[3] + y * cur_pic->linesize[3];
  153. out_h = sws_scale(scale->sws, data, cur_pic->linesize, y, h,
  154. link->dst->outputs[0]->outpic->data,
  155. link->dst->outputs[0]->outpic->linesize);
  156. if (slice_dir == -1)
  157. scale->slice_y -= out_h;
  158. avfilter_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
  159. if (slice_dir == 1)
  160. scale->slice_y += out_h;
  161. }
  162. AVFilter avfilter_vf_scale = {
  163. .name = "scale",
  164. .description = "Scale the input video to width:height size and/or convert the image format.",
  165. .init = init,
  166. .uninit = uninit,
  167. .query_formats = query_formats,
  168. .priv_size = sizeof(ScaleContext),
  169. .inputs = (AVFilterPad[]) {{ .name = "default",
  170. .type = AVMEDIA_TYPE_VIDEO,
  171. .start_frame = start_frame,
  172. .draw_slice = draw_slice,
  173. .min_perms = AV_PERM_READ, },
  174. { .name = NULL}},
  175. .outputs = (AVFilterPad[]) {{ .name = "default",
  176. .type = AVMEDIA_TYPE_VIDEO,
  177. .config_props = config_props, },
  178. { .name = NULL}},
  179. };