[FFmpeg-devel] [PATCH 1/3] lavu: add ROUNDED_RSHIFT and use it in various places.

Stefano Sabatini stefasab at gmail.com
Wed May 8 00:43:59 CEST 2013


On date Tuesday 2013-05-07 16:39:31 +0200, Clément Bœsch encoded:
> ---
>  libavcodec/ffv1dec.c              |  4 ++--
>  libavcodec/ffv1enc.c              |  4 ++--
>  libavcodec/mimic.c                |  4 ++--
>  libavcodec/mjpegdec.c             |  4 ++--
>  libavcodec/snow.c                 |  4 ++--
>  libavcodec/utils.c                | 13 +++++++------
>  libavfilter/deshake_opencl.c      |  3 ++-
>  libavfilter/vf_gradfun.c          |  4 ++--
>  libavfilter/vf_tinterlace.c       |  2 +-
>  libavutil/common.h                |  2 ++
>  libavutil/frame.c                 |  2 +-
>  libavutil/imgutils.c              |  2 +-
>  libswscale/rgb2rgb_template.c     |  8 ++++----
>  libswscale/swscale.c              |  6 +++---
>  libswscale/swscale_unscaled.c     |  6 +++---
>  libswscale/utils.c                | 10 +++++-----
>  libswscale/x86/rgb2rgb_template.c |  8 ++++----
>  17 files changed, 45 insertions(+), 41 deletions(-)
> 
> diff --git a/libavcodec/ffv1dec.c b/libavcodec/ffv1dec.c
> index adc47be..2a9aac2 100644
> --- a/libavcodec/ffv1dec.c
> +++ b/libavcodec/ffv1dec.c
> @@ -357,8 +357,8 @@ static int decode_slice(AVCodecContext *c, void *arg)
>  
>      av_assert1(width && height);
>      if (f->colorspace == 0) {
> -        const int chroma_width  = -((-width) >> f->chroma_h_shift);
> -        const int chroma_height = -((-height) >> f->chroma_v_shift);
> +        const int chroma_width  = ROUNDED_RSHIFT(width,  f->chroma_h_shift);
> +        const int chroma_height = ROUNDED_RSHIFT(height, f->chroma_v_shift);
>          const int cx            = x >> f->chroma_h_shift;
>          const int cy            = y >> f->chroma_v_shift;
>          decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
> diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c
> index 41848ec..9e44e02 100644
> --- a/libavcodec/ffv1enc.c
> +++ b/libavcodec/ffv1enc.c
> @@ -973,8 +973,8 @@ static int encode_slice(AVCodecContext *c, void *arg)
>      }
>  
>      if (f->colorspace == 0) {
> -        const int chroma_width  = -((-width) >> f->chroma_h_shift);
> -        const int chroma_height = -((-height) >> f->chroma_v_shift);
> +        const int chroma_width  = ROUNDED_RSHIFT(width,  f->chroma_h_shift);
> +        const int chroma_height = ROUNDED_RSHIFT(height, f->chroma_v_shift);
>          const int cx            = x >> f->chroma_h_shift;
>          const int cy            = y >> f->chroma_v_shift;
>  
> diff --git a/libavcodec/mimic.c b/libavcodec/mimic.c
> index 85c6a1a..aa135b5 100644
> --- a/libavcodec/mimic.c
> +++ b/libavcodec/mimic.c
> @@ -388,8 +388,8 @@ static int mimic_decode_frame(AVCodecContext *avctx, void *data,
>          avctx->height  = height;
>          avctx->pix_fmt = AV_PIX_FMT_YUV420P;
>          for (i = 0; i < 3; i++) {
> -            ctx->num_vblocks[i] = -((-height) >> (3 + !!i));
> -            ctx->num_hblocks[i] =     width   >> (3 + !!i);
> +            ctx->num_vblocks[i] = ROUNDED_RSHIFT(height,   3 + !!i);
> +            ctx->num_hblocks[i] =                width >> (3 + !!i);
>          }
>      } else if (width != ctx->avctx->width || height != ctx->avctx->height) {
>          avpriv_request_sample(avctx, "Resolution changing");
> diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
> index b439547..2324d1f 100644
> --- a/libavcodec/mjpegdec.c
> +++ b/libavcodec/mjpegdec.c
> @@ -1847,8 +1847,8 @@ the_end:
>              int w = s->width;
>              int h = s->height;
>              if(index && index<3){
> -                w = -((-w) >> hshift);
> -                h = -((-h) >> vshift);
> +                w = ROUNDED_RSHIFT(w, hshift);
> +                h = ROUNDED_RSHIFT(h, vshift);
>              }
>              if(dst){
>                  uint8_t *dst2 = dst + s->linesize[index]*(h-1);
> diff --git a/libavcodec/snow.c b/libavcodec/snow.c
> index e2ecdf0..a997f5d 100644
> --- a/libavcodec/snow.c
> +++ b/libavcodec/snow.c
> @@ -81,8 +81,8 @@ void ff_snow_reset_contexts(SnowContext *s){ //FIXME better initial contexts
>  }
>  
>  int ff_snow_alloc_blocks(SnowContext *s){
> -    int w= -((-s->avctx->width )>>LOG2_MB_SIZE);
> -    int h= -((-s->avctx->height)>>LOG2_MB_SIZE);
> +    int w= ROUNDED_RSHIFT(s->avctx->width,  LOG2_MB_SIZE);
> +    int h= ROUNDED_RSHIFT(s->avctx->height, LOG2_MB_SIZE);
>  
>      s->b_width = w;
>      s->b_height= h;
> diff --git a/libavcodec/utils.c b/libavcodec/utils.c
> index f4aeb19..e54477e 100644
> --- a/libavcodec/utils.c
> +++ b/libavcodec/utils.c
> @@ -179,8 +179,8 @@ void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
>  {
>      s->coded_width  = width;
>      s->coded_height = height;
> -    s->width        = -((-width ) >> s->lowres);
> -    s->height       = -((-height) >> s->lowres);
> +    s->width        = ROUNDED_RSHIFT(width,  s->lowres);
> +    s->height       = ROUNDED_RSHIFT(height, s->lowres);
>  }
>  
>  #if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
> @@ -572,8 +572,9 @@ void avpriv_color_frame(AVFrame *frame, const int c[4])
>      for (p = 0; p<desc->nb_components; p++) {
>          uint8_t *dst = frame->data[p];
>          int is_chroma = p == 1 || p == 2;
> -        int bytes = -((-frame->width) >> (is_chroma ? desc->log2_chroma_w : 0));
> -        for (y = 0; y<-((-frame->height) >> (is_chroma ? desc->log2_chroma_h : 0)); y++){
> +        int bytes  = is_chroma ? ROUNDED_RSHIFT(frame->width,  desc->log2_chroma_w) : frame->width;
> +        int height = is_chroma ? ROUNDED_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
> +        for (y = 0; y < height; y++) {
>              if (desc->comp[0].depth_minus1 >= 8) {
>                  for (x = 0; x<bytes; x++)
>                      ((uint16_t*)dst)[x] = c[p];
> @@ -622,8 +623,8 @@ int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
>  
>      switch (avctx->codec->type) {
>      case AVMEDIA_TYPE_VIDEO:
> -        frame->width  = FFMAX(avctx->width , -((-avctx->coded_width )>>avctx->lowres));
> -        frame->height = FFMAX(avctx->height, -((-avctx->coded_height)>>avctx->lowres));
> +        frame->width  = FFMAX(avctx->width,  ROUNDED_RSHIFT(avctx->coded_width,  avctx->lowres));
> +        frame->height = FFMAX(avctx->height, ROUNDED_RSHIFT(avctx->coded_height, avctx->lowres));
>          if (frame->format < 0)
>              frame->format              = avctx->pix_fmt;
>          if (!frame->sample_aspect_ratio.num)
> diff --git a/libavfilter/deshake_opencl.c b/libavfilter/deshake_opencl.c
> index adca5ea..e428a81 100644
> --- a/libavfilter/deshake_opencl.c
> +++ b/libavfilter/deshake_opencl.c
> @@ -135,7 +135,8 @@ int ff_opencl_deshake_process_inout_buf(AVFilterContext *ctx, AVFrame *in, AVFra
>      int ret = 0;
>      AVFilterLink *link = ctx->inputs[0];
>      DeshakeContext *deshake = ctx->priv;
> -    int chroma_height = -((-link->h) >> av_pix_fmt_desc_get(link->format)->log2_chroma_h);
> +    const int hshift = av_pix_fmt_desc_get(link->format)->log2_chroma_h;
> +    int chroma_height = ROUNDED_RSHIFT(link->h, hshift);
>  
>      if ((!deshake->opencl_ctx.cl_inbuf) || (!deshake->opencl_ctx.cl_outbuf)) {
>          deshake->opencl_ctx.in_plane_size[0]  = (in->linesize[0] * in->height);
> diff --git a/libavfilter/vf_gradfun.c b/libavfilter/vf_gradfun.c
> index 48ac378..19c1cb8 100644
> --- a/libavfilter/vf_gradfun.c
> +++ b/libavfilter/vf_gradfun.c
> @@ -172,8 +172,8 @@ static int config_input(AVFilterLink *inlink)
>      if (!gf->buf)
>          return AVERROR(ENOMEM);
>  
> -    gf->chroma_w = -((-inlink->w) >> hsub);
> -    gf->chroma_h = -((-inlink->h) >> vsub);
> +    gf->chroma_w = ROUNDED_RSHIFT(inlink->w, hsub);
> +    gf->chroma_h = ROUNDED_RSHIFT(inlink->h, vsub);
>      gf->chroma_r = av_clip(((((gf->radius >> hsub) + (gf->radius >> vsub)) / 2 ) + 1) & ~1, 4, 32);
>  
>      return 0;
> diff --git a/libavfilter/vf_tinterlace.c b/libavfilter/vf_tinterlace.c
> index 598e314..458cbda 100644
> --- a/libavfilter/vf_tinterlace.c
> +++ b/libavfilter/vf_tinterlace.c
> @@ -175,7 +175,7 @@ void copy_picture_field(uint8_t *dst[4], int dst_linesize[4],
>      int h, i;
>  
>      for (plane = 0; plane < desc->nb_components; plane++) {
> -        int lines = plane == 1 || plane == 2 ? -((-src_h) >> vsub) : src_h;
> +        int lines = plane == 1 || plane == 2 ? ROUNDED_RSHIFT(src_h, vsub) : src_h;
>          int linesize = av_image_get_linesize(format, w, plane);
>          uint8_t *dstp = dst[plane];
>          const uint8_t *srcp = src[plane];
> diff --git a/libavutil/common.h b/libavutil/common.h
> index 13f2ffb..5041914 100644
> --- a/libavutil/common.h
> +++ b/libavutil/common.h
> @@ -48,6 +48,8 @@
>  #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
>  /* assume b>0 */
>  #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
> +/* assume a>0 and b>0 */
> +#define ROUNDED_RSHIFT(a,b) (-((-(a)) >> (b)))

Maybe add an FF at the beginning.

[...]
-- 
FFmpeg = Frightening and Funny Mastodontic Perfectionist Extended Gargoyle


More information about the ffmpeg-devel mailing list