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.

204 lines
6.7KB

  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 slice_dir; ///< detected slice direction order for the current frame
  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. if (ctx->inputs[0]) {
  63. formats = avfilter_all_colorspaces();
  64. avfilter_formats_ref(formats, &ctx->inputs[0]->out_formats);
  65. }
  66. if (ctx->outputs[0]) {
  67. formats = avfilter_all_colorspaces();
  68. avfilter_formats_ref(formats, &ctx->outputs[0]->in_formats);
  69. }
  70. return 0;
  71. }
  72. static int config_props(AVFilterLink *outlink)
  73. {
  74. AVFilterContext *ctx = outlink->src;
  75. AVFilterLink *inlink = outlink->src->inputs[0];
  76. ScaleContext *scale = ctx->priv;
  77. int64_t w, h;
  78. if (!(w = scale->w))
  79. w = inlink->w;
  80. if (!(h = scale->h))
  81. h = inlink->h;
  82. if (w == -1)
  83. w = av_rescale(h, inlink->w, inlink->h);
  84. if (h == -1)
  85. h = av_rescale(w, inlink->h, inlink->w);
  86. if (w > INT_MAX || h > INT_MAX ||
  87. (h * inlink->w) > INT_MAX ||
  88. (w * inlink->h) > INT_MAX)
  89. av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
  90. outlink->w = w;
  91. outlink->h = h;
  92. /* TODO: make algorithm configurable */
  93. scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
  94. outlink->w, outlink->h, outlink->format,
  95. SWS_BILINEAR, NULL, NULL, NULL);
  96. av_log(ctx, AV_LOG_INFO, "w:%d h:%d fmt:%s\n",
  97. outlink->w, outlink->h, avcodec_get_pix_fmt_name(outlink->format));
  98. scale->input_is_pal = inlink->format == PIX_FMT_PAL8 ||
  99. inlink->format == PIX_FMT_BGR4_BYTE ||
  100. inlink->format == PIX_FMT_RGB4_BYTE ||
  101. inlink->format == PIX_FMT_BGR8 ||
  102. inlink->format == PIX_FMT_RGB8;
  103. return !scale->sws;
  104. }
  105. static void start_frame(AVFilterLink *link, AVFilterPicRef *picref)
  106. {
  107. ScaleContext *scale = link->dst->priv;
  108. AVFilterLink *outlink = link->dst->outputs[0];
  109. AVFilterPicRef *outpicref;
  110. avcodec_get_chroma_sub_sample(link->format, &scale->hsub, &scale->vsub);
  111. outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  112. outpicref->pts = picref->pts;
  113. outlink->outpic = outpicref;
  114. av_reduce(&outpicref->pixel_aspect.num, &outpicref->pixel_aspect.den,
  115. (int64_t)picref->pixel_aspect.num * outlink->h * link->w,
  116. (int64_t)picref->pixel_aspect.den * outlink->w * link->h,
  117. INT_MAX);
  118. scale->slice_dir = 0;
  119. avfilter_start_frame(outlink, avfilter_ref_pic(outpicref, ~0));
  120. }
  121. static void draw_slice(AVFilterLink *link, int y, int h)
  122. {
  123. ScaleContext *scale = link->dst->priv;
  124. int out_h;
  125. AVFilterPicRef *cur_pic = link->cur_pic;
  126. uint8_t *data[4];
  127. if (!scale->slice_dir) {
  128. if (y != 0 && y + h != link->h) {
  129. av_log(link->dst, AV_LOG_ERROR, "Slices start in the middle!\n");
  130. return;
  131. }
  132. scale->slice_dir = y ? -1 : 1;
  133. scale->slice_y = y ? link->dst->outputs[0]->h : y;
  134. }
  135. data[0] = cur_pic->data[0] + y * cur_pic->linesize[0];
  136. data[1] = scale->input_is_pal ?
  137. cur_pic->data[1] :
  138. cur_pic->data[1] + (y>>scale->vsub) * cur_pic->linesize[1];
  139. data[2] = cur_pic->data[2] + (y>>scale->vsub) * cur_pic->linesize[2];
  140. data[3] = cur_pic->data[3] + y * cur_pic->linesize[3];
  141. out_h = sws_scale(scale->sws, data, cur_pic->linesize, y, h,
  142. link->dst->outputs[0]->outpic->data,
  143. link->dst->outputs[0]->outpic->linesize);
  144. if (scale->slice_dir == -1)
  145. scale->slice_y -= out_h;
  146. avfilter_draw_slice(link->dst->outputs[0], scale->slice_y, out_h);
  147. if (scale->slice_dir == 1)
  148. scale->slice_y += out_h;
  149. }
  150. AVFilter avfilter_vf_scale = {
  151. .name = "scale",
  152. .description = "Scale the input video to width:height size and/or convert the image format.",
  153. .init = init,
  154. .uninit = uninit,
  155. .query_formats = query_formats,
  156. .priv_size = sizeof(ScaleContext),
  157. .inputs = (AVFilterPad[]) {{ .name = "default",
  158. .type = CODEC_TYPE_VIDEO,
  159. .start_frame = start_frame,
  160. .draw_slice = draw_slice,
  161. .min_perms = AV_PERM_READ, },
  162. { .name = NULL}},
  163. .outputs = (AVFilterPad[]) {{ .name = "default",
  164. .type = CODEC_TYPE_VIDEO,
  165. .config_props = config_props, },
  166. { .name = NULL}},
  167. };