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.

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