libavcodec/simple_idct_template.c
Go to the documentation of this file.
00001 /*
00002  * Simple IDCT
00003  *
00004  * Copyright (c) 2001 Michael Niedermayer <michaelni@gmx.at>
00005  *
00006  * This file is part of Libav.
00007  *
00008  * Libav is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * Libav is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with Libav; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00028 /*
00029   based upon some outcommented c code from mpeg2dec (idct_mmx.c
00030   written by Aaron Holtzman <aholtzma@ess.engr.uvic.ca>)
00031  */
00032 
00033 #include "bit_depth_template.c"
00034 
00035 #undef W1
00036 #undef W2
00037 #undef W3
00038 #undef W4
00039 #undef W5
00040 #undef W6
00041 #undef W7
00042 #undef ROW_SHIFT
00043 #undef COL_SHIFT
00044 #undef DC_SHIFT
00045 #undef MUL
00046 #undef MAC
00047 
00048 #if BIT_DEPTH == 8
00049 
00050 #define W1  22725  //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
00051 #define W2  21407  //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
00052 #define W3  19266  //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
00053 #define W4  16383  //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
00054 #define W5  12873  //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
00055 #define W6  8867   //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
00056 #define W7  4520   //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
00057 
00058 #define ROW_SHIFT 11
00059 #define COL_SHIFT 20
00060 #define DC_SHIFT 3
00061 
00062 #define MUL(a, b)    MUL16(a, b)
00063 #define MAC(a, b, c) MAC16(a, b, c)
00064 
00065 #elif BIT_DEPTH == 10
00066 
00067 #define W1 90901
00068 #define W2 85627
00069 #define W3 77062
00070 #define W4 65535
00071 #define W5 51491
00072 #define W6 35468
00073 #define W7 18081
00074 
00075 #define ROW_SHIFT 15
00076 #define COL_SHIFT 20
00077 #define DC_SHIFT 1
00078 
00079 #define MUL(a, b)    ((a) * (b))
00080 #define MAC(a, b, c) ((a) += (b) * (c))
00081 
00082 #else
00083 
00084 #error "Unsupported bitdepth"
00085 
00086 #endif
00087 
00088 static inline void FUNC(idctRowCondDC)(DCTELEM *row, int extra_shift)
00089 {
00090     int a0, a1, a2, a3, b0, b1, b2, b3;
00091 
00092 #if HAVE_FAST_64BIT
00093 #define ROW0_MASK (0xffffLL << 48 * HAVE_BIGENDIAN)
00094     if (((((uint64_t *)row)[0] & ~ROW0_MASK) | ((uint64_t *)row)[1]) == 0) {
00095         uint64_t temp;
00096         if (DC_SHIFT - extra_shift > 0) {
00097             temp = (row[0] << (DC_SHIFT - extra_shift)) & 0xffff;
00098         } else {
00099             temp = (row[0] >> (extra_shift - DC_SHIFT)) & 0xffff;
00100         }
00101         temp += temp << 16;
00102         temp += temp << 32;
00103         ((uint64_t *)row)[0] = temp;
00104         ((uint64_t *)row)[1] = temp;
00105         return;
00106     }
00107 #else
00108     if (!(((uint32_t*)row)[1] |
00109           ((uint32_t*)row)[2] |
00110           ((uint32_t*)row)[3] |
00111           row[1])) {
00112         uint32_t temp;
00113         if (DC_SHIFT - extra_shift > 0) {
00114             temp = (row[0] << (DC_SHIFT - extra_shift)) & 0xffff;
00115         } else {
00116             temp = (row[0] >> (extra_shift - DC_SHIFT)) & 0xffff;
00117         }
00118         temp += temp << 16;
00119         ((uint32_t*)row)[0]=((uint32_t*)row)[1] =
00120             ((uint32_t*)row)[2]=((uint32_t*)row)[3] = temp;
00121         return;
00122     }
00123 #endif
00124 
00125     a0 = (W4 * row[0]) + (1 << (ROW_SHIFT - 1));
00126     a1 = a0;
00127     a2 = a0;
00128     a3 = a0;
00129 
00130     a0 += W2 * row[2];
00131     a1 += W6 * row[2];
00132     a2 -= W6 * row[2];
00133     a3 -= W2 * row[2];
00134 
00135     b0 = MUL(W1, row[1]);
00136     MAC(b0, W3, row[3]);
00137     b1 = MUL(W3, row[1]);
00138     MAC(b1, -W7, row[3]);
00139     b2 = MUL(W5, row[1]);
00140     MAC(b2, -W1, row[3]);
00141     b3 = MUL(W7, row[1]);
00142     MAC(b3, -W5, row[3]);
00143 
00144     if (AV_RN64A(row + 4)) {
00145         a0 +=   W4*row[4] + W6*row[6];
00146         a1 += - W4*row[4] - W2*row[6];
00147         a2 += - W4*row[4] + W2*row[6];
00148         a3 +=   W4*row[4] - W6*row[6];
00149 
00150         MAC(b0,  W5, row[5]);
00151         MAC(b0,  W7, row[7]);
00152 
00153         MAC(b1, -W1, row[5]);
00154         MAC(b1, -W5, row[7]);
00155 
00156         MAC(b2,  W7, row[5]);
00157         MAC(b2,  W3, row[7]);
00158 
00159         MAC(b3,  W3, row[5]);
00160         MAC(b3, -W1, row[7]);
00161     }
00162 
00163     row[0] = (a0 + b0) >> (ROW_SHIFT + extra_shift);
00164     row[7] = (a0 - b0) >> (ROW_SHIFT + extra_shift);
00165     row[1] = (a1 + b1) >> (ROW_SHIFT + extra_shift);
00166     row[6] = (a1 - b1) >> (ROW_SHIFT + extra_shift);
00167     row[2] = (a2 + b2) >> (ROW_SHIFT + extra_shift);
00168     row[5] = (a2 - b2) >> (ROW_SHIFT + extra_shift);
00169     row[3] = (a3 + b3) >> (ROW_SHIFT + extra_shift);
00170     row[4] = (a3 - b3) >> (ROW_SHIFT + extra_shift);
00171 }
00172 
00173 #define IDCT_COLS do {                                  \
00174         a0 = W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4)); \
00175         a1 = a0;                                        \
00176         a2 = a0;                                        \
00177         a3 = a0;                                        \
00178                                                         \
00179         a0 +=  W2*col[8*2];                             \
00180         a1 +=  W6*col[8*2];                             \
00181         a2 += -W6*col[8*2];                             \
00182         a3 += -W2*col[8*2];                             \
00183                                                         \
00184         b0 = MUL(W1, col[8*1]);                         \
00185         b1 = MUL(W3, col[8*1]);                         \
00186         b2 = MUL(W5, col[8*1]);                         \
00187         b3 = MUL(W7, col[8*1]);                         \
00188                                                         \
00189         MAC(b0,  W3, col[8*3]);                         \
00190         MAC(b1, -W7, col[8*3]);                         \
00191         MAC(b2, -W1, col[8*3]);                         \
00192         MAC(b3, -W5, col[8*3]);                         \
00193                                                         \
00194         if (col[8*4]) {                                 \
00195             a0 +=  W4*col[8*4];                         \
00196             a1 += -W4*col[8*4];                         \
00197             a2 += -W4*col[8*4];                         \
00198             a3 +=  W4*col[8*4];                         \
00199         }                                               \
00200                                                         \
00201         if (col[8*5]) {                                 \
00202             MAC(b0,  W5, col[8*5]);                     \
00203             MAC(b1, -W1, col[8*5]);                     \
00204             MAC(b2,  W7, col[8*5]);                     \
00205             MAC(b3,  W3, col[8*5]);                     \
00206         }                                               \
00207                                                         \
00208         if (col[8*6]) {                                 \
00209             a0 +=  W6*col[8*6];                         \
00210             a1 += -W2*col[8*6];                         \
00211             a2 +=  W2*col[8*6];                         \
00212             a3 += -W6*col[8*6];                         \
00213         }                                               \
00214                                                         \
00215         if (col[8*7]) {                                 \
00216             MAC(b0,  W7, col[8*7]);                     \
00217             MAC(b1, -W5, col[8*7]);                     \
00218             MAC(b2,  W3, col[8*7]);                     \
00219             MAC(b3, -W1, col[8*7]);                     \
00220         }                                               \
00221     } while (0)
00222 
00223 static inline void FUNC(idctSparseColPut)(pixel *dest, int line_size,
00224                                           DCTELEM *col)
00225 {
00226     int a0, a1, a2, a3, b0, b1, b2, b3;
00227 
00228     IDCT_COLS;
00229 
00230     dest[0] = av_clip_pixel((a0 + b0) >> COL_SHIFT);
00231     dest += line_size;
00232     dest[0] = av_clip_pixel((a1 + b1) >> COL_SHIFT);
00233     dest += line_size;
00234     dest[0] = av_clip_pixel((a2 + b2) >> COL_SHIFT);
00235     dest += line_size;
00236     dest[0] = av_clip_pixel((a3 + b3) >> COL_SHIFT);
00237     dest += line_size;
00238     dest[0] = av_clip_pixel((a3 - b3) >> COL_SHIFT);
00239     dest += line_size;
00240     dest[0] = av_clip_pixel((a2 - b2) >> COL_SHIFT);
00241     dest += line_size;
00242     dest[0] = av_clip_pixel((a1 - b1) >> COL_SHIFT);
00243     dest += line_size;
00244     dest[0] = av_clip_pixel((a0 - b0) >> COL_SHIFT);
00245 }
00246 
00247 static inline void FUNC(idctSparseColAdd)(pixel *dest, int line_size,
00248                                           DCTELEM *col)
00249 {
00250     int a0, a1, a2, a3, b0, b1, b2, b3;
00251 
00252     IDCT_COLS;
00253 
00254     dest[0] = av_clip_pixel(dest[0] + ((a0 + b0) >> COL_SHIFT));
00255     dest += line_size;
00256     dest[0] = av_clip_pixel(dest[0] + ((a1 + b1) >> COL_SHIFT));
00257     dest += line_size;
00258     dest[0] = av_clip_pixel(dest[0] + ((a2 + b2) >> COL_SHIFT));
00259     dest += line_size;
00260     dest[0] = av_clip_pixel(dest[0] + ((a3 + b3) >> COL_SHIFT));
00261     dest += line_size;
00262     dest[0] = av_clip_pixel(dest[0] + ((a3 - b3) >> COL_SHIFT));
00263     dest += line_size;
00264     dest[0] = av_clip_pixel(dest[0] + ((a2 - b2) >> COL_SHIFT));
00265     dest += line_size;
00266     dest[0] = av_clip_pixel(dest[0] + ((a1 - b1) >> COL_SHIFT));
00267     dest += line_size;
00268     dest[0] = av_clip_pixel(dest[0] + ((a0 - b0) >> COL_SHIFT));
00269 }
00270 
00271 static inline void FUNC(idctSparseCol)(DCTELEM *col)
00272 {
00273     int a0, a1, a2, a3, b0, b1, b2, b3;
00274 
00275     IDCT_COLS;
00276 
00277     col[0 ] = ((a0 + b0) >> COL_SHIFT);
00278     col[8 ] = ((a1 + b1) >> COL_SHIFT);
00279     col[16] = ((a2 + b2) >> COL_SHIFT);
00280     col[24] = ((a3 + b3) >> COL_SHIFT);
00281     col[32] = ((a3 - b3) >> COL_SHIFT);
00282     col[40] = ((a2 - b2) >> COL_SHIFT);
00283     col[48] = ((a1 - b1) >> COL_SHIFT);
00284     col[56] = ((a0 - b0) >> COL_SHIFT);
00285 }
00286 
00287 void FUNC(ff_simple_idct_put)(uint8_t *dest_, int line_size, DCTELEM *block)
00288 {
00289     pixel *dest = (pixel *)dest_;
00290     int i;
00291 
00292     line_size /= sizeof(pixel);
00293 
00294     for (i = 0; i < 8; i++)
00295         FUNC(idctRowCondDC)(block + i*8, 0);
00296 
00297     for (i = 0; i < 8; i++)
00298         FUNC(idctSparseColPut)(dest + i, line_size, block + i);
00299 }
00300 
00301 void FUNC(ff_simple_idct_add)(uint8_t *dest_, int line_size, DCTELEM *block)
00302 {
00303     pixel *dest = (pixel *)dest_;
00304     int i;
00305 
00306     line_size /= sizeof(pixel);
00307 
00308     for (i = 0; i < 8; i++)
00309         FUNC(idctRowCondDC)(block + i*8, 0);
00310 
00311     for (i = 0; i < 8; i++)
00312         FUNC(idctSparseColAdd)(dest + i, line_size, block + i);
00313 }
00314 
00315 void FUNC(ff_simple_idct)(DCTELEM *block)
00316 {
00317     int i;
00318 
00319     for (i = 0; i < 8; i++)
00320         FUNC(idctRowCondDC)(block + i*8, 0);
00321 
00322     for (i = 0; i < 8; i++)
00323         FUNC(idctSparseCol)(block + i);
00324 }