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.

257 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. scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
  128. outlink->w, outlink->h, outlink->format,
  129. scale->flags, NULL, NULL, NULL);
  130. scale->isws[0] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
  131. outlink->w, outlink->h/2, outlink->format,
  132. scale->flags, NULL, NULL, NULL);
  133. scale->isws[1] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
  134. outlink->w, outlink->h/2, outlink->format,
  135. scale->flags, NULL, NULL, NULL);
  136. if (!scale->sws)
  137. return AVERROR(EINVAL);
  138. return 0;
  139. }
  140. static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  141. {
  142. ScaleContext *scale = link->dst->priv;
  143. AVFilterLink *outlink = link->dst->outputs[0];
  144. AVFilterBufferRef *outpicref;
  145. scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
  146. scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  147. outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  148. avfilter_copy_buffer_ref_props(outpicref, picref);
  149. outpicref->video->w = outlink->w;
  150. outpicref->video->h = outlink->h;
  151. outlink->out_buf = outpicref;
  152. av_reduce(&outpicref->video->pixel_aspect.num, &outpicref->video->pixel_aspect.den,
  153. (int64_t)picref->video->pixel_aspect.num * outlink->h * link->w,
  154. (int64_t)picref->video->pixel_aspect.den * outlink->w * link->h,
  155. INT_MAX);
  156. scale->slice_y = 0;
  157. avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
  158. }
  159. static int scale_slice(AVFilterLink *link, struct SwsContext *sws, int y, int h, int mul, int field)
  160. {
  161. ScaleContext *scale = link->dst->priv;
  162. AVFilterBufferRef *cur_pic = link->cur_buf;
  163. AVFilterBufferRef *out_buf = link->dst->outputs[0]->out_buf;
  164. const uint8_t *in[4], *out[4];
  165. int in_stride[4],out_stride[4];
  166. int i;
  167. for(i=0; i<4; i++){
  168. int vsub= ((i+1)&2) ? scale->vsub : 0;
  169. in_stride[i] = cur_pic->linesize[i] * mul;
  170. out_stride[i] = out_buf->linesize[i] * mul;
  171. in[i] = cur_pic->data[i] + ((y>>vsub)+field) * cur_pic->linesize[i];
  172. out[i] = out_buf->data[i] + field * out_buf->linesize[i];
  173. }
  174. if(scale->input_is_pal){
  175. in[1] = cur_pic->data[1];
  176. out[1] = out_buf->data[1];
  177. }
  178. return sws_scale(sws, in, in_stride, y/mul, h,
  179. out,out_stride);
  180. }
  181. static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  182. {
  183. ScaleContext *scale = link->dst->priv;
  184. int out_h;
  185. if (scale->slice_y == 0 && slice_dir == -1)
  186. scale->slice_y = link->dst->outputs[0]->h;
  187. if(scale->interlaced>0 || (scale->interlaced<0 && link->cur_buf->video->interlaced)){
  188. av_assert0(y%4 == 0);
  189. out_h = scale_slice(link, scale->isws[0], y, (h+1)/2, 2, 0);
  190. out_h+= scale_slice(link, scale->isws[1], y, h /2, 2, 1);
  191. }else{
  192. out_h = scale_slice(link, scale->sws, y, h, 1, 0);
  193. }
  194. if (slice_dir == -1)
  195. scale->slice_y -= out_h;
  196. avfilter_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
  197. if (slice_dir == 1)
  198. scale->slice_y += out_h;
  199. }
  200. AVFilter avfilter_vf_scale = {
  201. .name = "scale",
  202. .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
  203. .init = init,
  204. .uninit = uninit,
  205. .query_formats = query_formats,
  206. .priv_size = sizeof(ScaleContext),
  207. .inputs = (AVFilterPad[]) {{ .name = "default",
  208. .type = AVMEDIA_TYPE_VIDEO,
  209. .start_frame = start_frame,
  210. .draw_slice = draw_slice,
  211. .min_perms = AV_PERM_READ, },
  212. { .name = NULL}},
  213. .outputs = (AVFilterPad[]) {{ .name = "default",
  214. .type = AVMEDIA_TYPE_VIDEO,
  215. .config_props = config_props, },
  216. { .name = NULL}},
  217. };