vf_scale.c 6.4 KB

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