libavcodec/diracdsp.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 David Conrad
00003  *
00004  * This file is part of FFmpeg.
00005  *
00006  * FFmpeg is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * FFmpeg is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with FFmpeg; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00021 #include "dsputil.h"
00022 #include "diracdsp.h"
00023 #include "libavcodec/x86/diracdsp_mmx.h"
00024 
00025 #define FILTER(src, stride)                                     \
00026     ((21*((src)[ 0*stride] + (src)[1*stride])                   \
00027       -7*((src)[-1*stride] + (src)[2*stride])                   \
00028       +3*((src)[-2*stride] + (src)[3*stride])                   \
00029       -1*((src)[-3*stride] + (src)[4*stride]) + 16) >> 5)
00030 
00031 static void dirac_hpel_filter(uint8_t *dsth, uint8_t *dstv, uint8_t *dstc, uint8_t *src,
00032                               int stride, int width, int height)
00033 {
00034     int x, y;
00035 
00036     for (y = 0; y < height; y++) {
00037         for (x = -3; x < width+5; x++)
00038             dstv[x] = av_clip_uint8(FILTER(src+x, stride));
00039 
00040         for (x = 0; x < width; x++)
00041             dstc[x] = av_clip_uint8(FILTER(dstv+x, 1));
00042 
00043         for (x = 0; x < width; x++)
00044             dsth[x] = av_clip_uint8(FILTER(src+x, 1));
00045 
00046         src  += stride;
00047         dsth += stride;
00048         dstv += stride;
00049         dstc += stride;
00050     }
00051 }
00052 
00053 #define PIXOP_BILINEAR(PFX, OP, WIDTH)                                  \
00054     static void ff_ ## PFX ## _dirac_pixels ## WIDTH ## _bilinear_c(uint8_t *dst, const uint8_t *src[5], int stride, int h) \
00055     {                                                                   \
00056         int x;                                                          \
00057         const uint8_t *s0 = src[0];                                     \
00058         const uint8_t *s1 = src[1];                                     \
00059         const uint8_t *s2 = src[2];                                     \
00060         const uint8_t *s3 = src[3];                                     \
00061         const uint8_t *w  = src[4];                                     \
00062                                                                         \
00063         while (h--) {                                                   \
00064             for (x = 0; x < WIDTH; x++) {                               \
00065                 OP(dst[x], (s0[x]*w[0] + s1[x]*w[1] + s2[x]*w[2] + s3[x]*w[3] + 8) >> 4); \
00066             }                                                           \
00067                                                                         \
00068             dst += stride;                                              \
00069             s0 += stride;                                               \
00070             s1 += stride;                                               \
00071             s2 += stride;                                               \
00072             s3 += stride;                                               \
00073         }                                                               \
00074     }
00075 
00076 #define OP_PUT(dst, val) (dst) = (val)
00077 #define OP_AVG(dst, val) (dst) = (((dst) + (val) + 1)>>1)
00078 
00079 PIXOP_BILINEAR(put, OP_PUT, 8)
00080 PIXOP_BILINEAR(put, OP_PUT, 16)
00081 PIXOP_BILINEAR(put, OP_PUT, 32)
00082 PIXOP_BILINEAR(avg, OP_AVG, 8)
00083 PIXOP_BILINEAR(avg, OP_AVG, 16)
00084 PIXOP_BILINEAR(avg, OP_AVG, 32)
00085 
00086 #define op_scale1(x)  block[x] = av_clip_uint8( (block[x]*weight + (1<<(log2_denom-1))) >> log2_denom)
00087 #define op_scale2(x)  dst[x] = av_clip_uint8( (src[x]*weights + dst[x]*weightd + (1<<(log2_denom-1))) >> log2_denom)
00088 
00089 #define DIRAC_WEIGHT(W)                                                 \
00090     static void weight_dirac_pixels ## W ## _c(uint8_t *block, int stride, int log2_denom, \
00091                                                int weight, int h) {     \
00092         int x;                                                          \
00093         while (h--) {                                                   \
00094             for (x = 0; x < W; x++) {                                   \
00095                 op_scale1(x);                                           \
00096                 op_scale1(x+1);                                         \
00097             }                                                           \
00098             block += stride;                                            \
00099         }                                                               \
00100     }                                                                   \
00101     static void biweight_dirac_pixels ## W ## _c(uint8_t *dst, uint8_t *src, int stride, int log2_denom, \
00102                                                  int weightd, int weights, int h) { \
00103         int x;                                                          \
00104         while (h--) {                                                   \
00105             for (x = 0; x < W; x++) {                                   \
00106                 op_scale2(x);                                           \
00107                 op_scale2(x+1);                                         \
00108             }                                                           \
00109             dst += stride;                                              \
00110             src += stride;                                              \
00111         }                                                               \
00112     }
00113 
00114 DIRAC_WEIGHT(8)
00115 DIRAC_WEIGHT(16)
00116 DIRAC_WEIGHT(32)
00117 
00118 #define ADD_OBMC(xblen)                                                 \
00119     static void add_obmc ## xblen ## _c(uint16_t *dst, const uint8_t *src, int stride, \
00120                                         const uint8_t *obmc_weight, int yblen) \
00121     {                                                                   \
00122         int x;                                                          \
00123         while (yblen--) {                                               \
00124             for (x = 0; x < xblen; x += 2) {                            \
00125                 dst[x  ] += src[x  ] * obmc_weight[x  ];                \
00126                 dst[x+1] += src[x+1] * obmc_weight[x+1];                \
00127             }                                                           \
00128             dst += stride;                                              \
00129             src += stride;                                              \
00130             obmc_weight += 32;                                          \
00131         }                                                               \
00132     }
00133 
00134 ADD_OBMC(8)
00135 ADD_OBMC(16)
00136 ADD_OBMC(32)
00137 
00138 static void put_signed_rect_clamped_c(uint8_t *dst, int dst_stride, const int16_t *src, int src_stride, int width, int height)
00139 {
00140     int x, y;
00141     for (y = 0; y < height; y++) {
00142         for (x = 0; x < width; x+=4) {
00143             dst[x  ] = av_clip_uint8(src[x  ] + 128);
00144             dst[x+1] = av_clip_uint8(src[x+1] + 128);
00145             dst[x+2] = av_clip_uint8(src[x+2] + 128);
00146             dst[x+3] = av_clip_uint8(src[x+3] + 128);
00147         }
00148         dst += dst_stride;
00149         src += src_stride;
00150     }
00151 }
00152 
00153 static void add_rect_clamped_c(uint8_t *dst, const uint16_t *src, int stride,
00154                                const int16_t *idwt, int idwt_stride,
00155                                int width, int height)
00156 {
00157     int x, y;
00158 
00159     for (y = 0; y < height; y++) {
00160         for (x = 0; x < width; x+=2) {
00161             dst[x  ] = av_clip_uint8(((src[x  ]+32)>>6) + idwt[x  ]);
00162             dst[x+1] = av_clip_uint8(((src[x+1]+32)>>6) + idwt[x+1]);
00163         }
00164         dst += stride;
00165         src += stride;
00166         idwt += idwt_stride;
00167     }
00168 }
00169 
00170 #define PIXFUNC(PFX, WIDTH)                                             \
00171     c->PFX ## _dirac_pixels_tab[WIDTH>>4][0] = ff_ ## PFX ## _dirac_pixels ## WIDTH ## _c; \
00172     c->PFX ## _dirac_pixels_tab[WIDTH>>4][1] = ff_ ## PFX ## _dirac_pixels ## WIDTH ## _l2_c; \
00173     c->PFX ## _dirac_pixels_tab[WIDTH>>4][2] = ff_ ## PFX ## _dirac_pixels ## WIDTH ## _l4_c; \
00174     c->PFX ## _dirac_pixels_tab[WIDTH>>4][3] = ff_ ## PFX ## _dirac_pixels ## WIDTH ## _bilinear_c
00175 
00176 void ff_diracdsp_init(DiracDSPContext *c)
00177 {
00178     c->dirac_hpel_filter = dirac_hpel_filter;
00179     c->add_rect_clamped = add_rect_clamped_c;
00180     c->put_signed_rect_clamped = put_signed_rect_clamped_c;
00181 
00182     c->add_dirac_obmc[0] = add_obmc8_c;
00183     c->add_dirac_obmc[1] = add_obmc16_c;
00184     c->add_dirac_obmc[2] = add_obmc32_c;
00185 
00186     c->weight_dirac_pixels_tab[0] = weight_dirac_pixels8_c;
00187     c->weight_dirac_pixels_tab[1] = weight_dirac_pixels16_c;
00188     c->weight_dirac_pixels_tab[2] = weight_dirac_pixels32_c;
00189     c->biweight_dirac_pixels_tab[0] = biweight_dirac_pixels8_c;
00190     c->biweight_dirac_pixels_tab[1] = biweight_dirac_pixels16_c;
00191     c->biweight_dirac_pixels_tab[2] = biweight_dirac_pixels32_c;
00192 
00193     PIXFUNC(put, 8);
00194     PIXFUNC(put, 16);
00195     PIXFUNC(put, 32);
00196     PIXFUNC(avg, 8);
00197     PIXFUNC(avg, 16);
00198     PIXFUNC(avg, 32);
00199 
00200     if (HAVE_MMX && HAVE_YASM) ff_diracdsp_init_mmx(c);
00201 }