vf_scale.c 6.8 KB

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