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.3KB

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