LCOV - code coverage report
Current view: top level - libavcodec - xvididct.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 198 207 95.7 %
Date: 2017-12-13 18:07:29 Functions: 6 8 75.0 %

          Line data    Source code
       1             : /*
       2             :  * Xvid MPEG-4 IDCT
       3             :  *
       4             :  * Copyright (C) 2006-2011 Xvid Solutions GmbH
       5             :  *
       6             :  * This file is part of FFmpeg.
       7             :  *
       8             :  * FFmpeg is free software; you can redistribute it and/or
       9             :  * modify it under the terms of the GNU Lesser General Public
      10             :  * License as published by the Free Software Foundation; either
      11             :  * version 2.1 of the License, or (at your option) any later version.
      12             :  *
      13             :  * FFmpeg is distributed in the hope that it will be useful,
      14             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16             :  * Lesser General Public License for more details.
      17             :  *
      18             :  * You should have received a copy of the GNU Lesser General Public
      19             :  * License along with FFmpeg; if not, write to the Free Software
      20             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      21             :  */
      22             : 
      23             : /**
      24             :  * @file
      25             :  * Walken IDCT
      26             :  * Alternative IDCT implementation for decoding compatibility.
      27             :  *
      28             :  * @author Skal
      29             :  * @note This C version is not the original IDCT, but a modified one that
      30             :  *       yields the same error profile as the MMX/MMXEXT/SSE2 versions.
      31             :  */
      32             : 
      33             : #include "config.h"
      34             : #include "libavutil/attributes.h"
      35             : #include "avcodec.h"
      36             : #include "idctdsp.h"
      37             : #include "xvididct.h"
      38             : 
      39             : #define ROW_SHIFT 11
      40             : #define COL_SHIFT  6
      41             : 
      42             : // #define FIX(x)   (int)((x) * (1 << ROW_SHIFT))
      43             : #define RND0 65536 // 1 << (COL_SHIFT + ROW_SHIFT - 1);
      44             : #define RND1 3597  // FIX (1.75683487303);
      45             : #define RND2 2260  // FIX (1.10355339059);
      46             : #define RND3 1203  // FIX (0.587788325588);
      47             : #define RND4 0
      48             : #define RND5 120   // FIX (0.058658283817);
      49             : #define RND6 512   // FIX (0.25);
      50             : #define RND7 512   // FIX (0.25);
      51             : 
      52             : static const int TAB04[] = { 22725, 21407, 19266, 16384, 12873,  8867, 4520 };
      53             : static const int TAB17[] = { 31521, 29692, 26722, 22725, 17855, 12299, 6270 };
      54             : static const int TAB26[] = { 29692, 27969, 25172, 21407, 16819, 11585, 5906 };
      55             : static const int TAB35[] = { 26722, 25172, 22654, 19266, 15137, 10426, 5315 };
      56             : 
      57      480000 : static int idct_row(short *in, const int *const tab, int rnd)
      58             : {
      59      480000 :     const int c1 = tab[0];
      60      480000 :     const int c2 = tab[1];
      61      480000 :     const int c3 = tab[2];
      62      480000 :     const int c4 = tab[3];
      63      480000 :     const int c5 = tab[4];
      64      480000 :     const int c6 = tab[5];
      65      480000 :     const int c7 = tab[6];
      66             : 
      67      480000 :     const int right = in[5] | in[6] | in[7];
      68      480000 :     const int left  = in[1] | in[2] | in[3];
      69      480000 :     if (!(right | in[4])) {
      70      264353 :         const int k = c4 * in[0] + rnd;
      71      264353 :         if (left) {
      72       24730 :             const int a0 = k + c2 * in[2];
      73       24730 :             const int a1 = k + c6 * in[2];
      74       24730 :             const int a2 = k - c6 * in[2];
      75       24730 :             const int a3 = k - c2 * in[2];
      76             : 
      77       24730 :             const int b0 = c1 * in[1] + c3 * in[3];
      78       24730 :             const int b1 = c3 * in[1] - c7 * in[3];
      79       24730 :             const int b2 = c5 * in[1] - c1 * in[3];
      80       24730 :             const int b3 = c7 * in[1] - c5 * in[3];
      81             : 
      82       24730 :             in[0] = (a0 + b0) >> ROW_SHIFT;
      83       24730 :             in[1] = (a1 + b1) >> ROW_SHIFT;
      84       24730 :             in[2] = (a2 + b2) >> ROW_SHIFT;
      85       24730 :             in[3] = (a3 + b3) >> ROW_SHIFT;
      86       24730 :             in[4] = (a3 - b3) >> ROW_SHIFT;
      87       24730 :             in[5] = (a2 - b2) >> ROW_SHIFT;
      88       24730 :             in[6] = (a1 - b1) >> ROW_SHIFT;
      89       24730 :             in[7] = (a0 - b0) >> ROW_SHIFT;
      90             :         } else {
      91      239623 :             const int a0 = k >> ROW_SHIFT;
      92      239623 :             if (a0) {
      93       97939 :                 in[0] =
      94      195878 :                 in[1] =
      95      293817 :                 in[2] =
      96      293817 :                 in[3] =
      97      293817 :                 in[4] =
      98      293817 :                 in[5] =
      99      293817 :                 in[6] =
     100      195878 :                 in[7] = a0;
     101             :             } else
     102      141684 :                 return 0;
     103             :         }
     104      215647 :     } else if (!(left | right)) {
     105        7429 :         const int a0 = (rnd + c4 * (in[0] + in[4])) >> ROW_SHIFT;
     106        7429 :         const int a1 = (rnd + c4 * (in[0] - in[4])) >> ROW_SHIFT;
     107             : 
     108        7429 :         in[0] = a0;
     109        7429 :         in[3] = a0;
     110        7429 :         in[4] = a0;
     111        7429 :         in[7] = a0;
     112        7429 :         in[1] = a1;
     113        7429 :         in[2] = a1;
     114        7429 :         in[5] = a1;
     115        7429 :         in[6] = a1;
     116             :     } else {
     117      208218 :         const int k  = c4 * in[0] + rnd;
     118      208218 :         const int a0 = k + c2 * in[2] + c4 * in[4] + c6 * in[6];
     119      208218 :         const int a1 = k + c6 * in[2] - c4 * in[4] - c2 * in[6];
     120      208218 :         const int a2 = k - c6 * in[2] - c4 * in[4] + c2 * in[6];
     121      208218 :         const int a3 = k - c2 * in[2] + c4 * in[4] - c6 * in[6];
     122             : 
     123      208218 :         const int b0 = c1 * in[1] + c3 * in[3] + c5 * in[5] + c7 * in[7];
     124      208218 :         const int b1 = c3 * in[1] - c7 * in[3] - c1 * in[5] - c5 * in[7];
     125      208218 :         const int b2 = c5 * in[1] - c1 * in[3] + c7 * in[5] + c3 * in[7];
     126      208218 :         const int b3 = c7 * in[1] - c5 * in[3] + c3 * in[5] - c1 * in[7];
     127             : 
     128      208218 :         in[0] = (a0 + b0) >> ROW_SHIFT;
     129      208218 :         in[1] = (a1 + b1) >> ROW_SHIFT;
     130      208218 :         in[2] = (a2 + b2) >> ROW_SHIFT;
     131      208218 :         in[3] = (a3 + b3) >> ROW_SHIFT;
     132      208218 :         in[4] = (a3 - b3) >> ROW_SHIFT;
     133      208218 :         in[5] = (a2 - b2) >> ROW_SHIFT;
     134      208218 :         in[6] = (a1 - b1) >> ROW_SHIFT;
     135      208218 :         in[7] = (a0 - b0) >> ROW_SHIFT;
     136             :     }
     137      338316 :     return 1;
     138             : }
     139             : 
     140             : #define TAN1  0x32EC
     141             : #define TAN2  0x6A0A
     142             : #define TAN3  0xAB0E
     143             : #define SQRT2 0x5A82
     144             : 
     145             : #define MULT(c, x, n)  (((c) * (x)) >> (n))
     146             : // 12b version => #define MULT(c,x, n)  ((((c) >> 3) * (x)) >> ((n) - 3))
     147             : // 12b zero-testing version:
     148             : 
     149             : #define BUTTERFLY(a, b, tmp)     \
     150             :     (tmp) = (a) + (b);           \
     151             :     (b)   = (a) - (b);           \
     152             :     (a)   = (tmp)
     153             : 
     154             : #define LOAD_BUTTERFLY(m1, m2, a, b, tmp, s)   \
     155             :     (m1) = (s)[(a)] + (s)[(b)];                \
     156             :     (m2) = (s)[(a)] - (s)[(b)]
     157             : 
     158      384504 : static void idct_col_8(short *const in)
     159             : {
     160             :     int mm0, mm1, mm2, mm3, mm4, mm5, mm6, mm7, spill;
     161             : 
     162             :     // odd
     163             : 
     164      384504 :     mm4 = (int) in[7 * 8];
     165      384504 :     mm5 = (int) in[5 * 8];
     166      384504 :     mm6 = (int) in[3 * 8];
     167      384504 :     mm7 = (int) in[1 * 8];
     168             : 
     169      384504 :     mm0 = MULT(TAN1, mm4, 16) + mm7;
     170      384504 :     mm1 = MULT(TAN1, mm7, 16) - mm4;
     171      384504 :     mm2 = MULT(TAN3, mm5, 16) + mm6;
     172      384504 :     mm3 = MULT(TAN3, mm6, 16) - mm5;
     173             : 
     174      384504 :     mm7 = mm0 + mm2;
     175      384504 :     mm4 = mm1 - mm3;
     176      384504 :     mm0 = mm0 - mm2;
     177      384504 :     mm1 = mm1 + mm3;
     178      384504 :     mm6 = mm0 + mm1;
     179      384504 :     mm5 = mm0 - mm1;
     180      384504 :     mm5 = 2 * MULT(SQRT2, mm5, 16); // 2*sqrt2
     181      384504 :     mm6 = 2 * MULT(SQRT2, mm6, 16); // Watch out: precision loss but done to match
     182             :                                     // the pmulhw used in MMX/MMXEXT/SSE2 versions
     183             : 
     184             :     // even
     185             : 
     186      384504 :     mm1 = (int) in[2 * 8];
     187      384504 :     mm2 = (int) in[6 * 8];
     188      384504 :     mm3 = MULT(TAN2, mm2, 16) + mm1;
     189      384504 :     mm2 = MULT(TAN2, mm1, 16) - mm2;
     190             : 
     191      384504 :     LOAD_BUTTERFLY(mm0, mm1, 0 * 8, 4 * 8, spill, in);
     192             : 
     193      384504 :     BUTTERFLY(mm0, mm3, spill);
     194      384504 :     BUTTERFLY(mm0, mm7, spill);
     195      384504 :     in[8 * 0] = (int16_t) (mm0 >> COL_SHIFT);
     196      384504 :     in[8 * 7] = (int16_t) (mm7 >> COL_SHIFT);
     197      384504 :     BUTTERFLY(mm3, mm4, mm0);
     198      384504 :     in[8 * 3] = (int16_t) (mm3 >> COL_SHIFT);
     199      384504 :     in[8 * 4] = (int16_t) (mm4 >> COL_SHIFT);
     200             : 
     201      384504 :     BUTTERFLY(mm1, mm2, mm0);
     202      384504 :     BUTTERFLY(mm1, mm6, mm0);
     203      384504 :     in[8 * 1] = (int16_t) (mm1 >> COL_SHIFT);
     204      384504 :     in[8 * 6] = (int16_t) (mm6 >> COL_SHIFT);
     205      384504 :     BUTTERFLY(mm2, mm5, mm0);
     206      384504 :     in[8 * 2] = (int16_t) (mm2 >> COL_SHIFT);
     207      384504 :     in[8 * 5] = (int16_t) (mm5 >> COL_SHIFT);
     208      384504 : }
     209             : 
     210        5896 : static void idct_col_4(short *const in)
     211             : {
     212             :     int mm0, mm1, mm2, mm3, mm4, mm5, mm6, mm7, spill;
     213             : 
     214             :     // odd
     215             : 
     216        5896 :     mm0 = (int) in[1 * 8];
     217        5896 :     mm2 = (int) in[3 * 8];
     218             : 
     219        5896 :     mm1 = MULT(TAN1, mm0, 16);
     220        5896 :     mm3 = MULT(TAN3, mm2, 16);
     221             : 
     222        5896 :     mm7 = mm0 + mm2;
     223        5896 :     mm4 = mm1 - mm3;
     224        5896 :     mm0 = mm0 - mm2;
     225        5896 :     mm1 = mm1 + mm3;
     226        5896 :     mm6 = mm0 + mm1;
     227        5896 :     mm5 = mm0 - mm1;
     228        5896 :     mm6 = 2 * MULT(SQRT2, mm6, 16); // 2*sqrt2
     229        5896 :     mm5 = 2 * MULT(SQRT2, mm5, 16);
     230             : 
     231             :     // even
     232             : 
     233        5896 :     mm0 = mm1 = (int) in[0 * 8];
     234        5896 :     mm3 = (int) in[2 * 8];
     235        5896 :     mm2 = MULT(TAN2, mm3, 16);
     236             : 
     237        5896 :     BUTTERFLY(mm0, mm3, spill);
     238        5896 :     BUTTERFLY(mm0, mm7, spill);
     239        5896 :     in[8 * 0] = (int16_t) (mm0 >> COL_SHIFT);
     240        5896 :     in[8 * 7] = (int16_t) (mm7 >> COL_SHIFT);
     241        5896 :     BUTTERFLY(mm3, mm4, mm0);
     242        5896 :     in[8 * 3] = (int16_t) (mm3 >> COL_SHIFT);
     243        5896 :     in[8 * 4] = (int16_t) (mm4 >> COL_SHIFT);
     244             : 
     245        5896 :     BUTTERFLY(mm1, mm2, mm0);
     246        5896 :     BUTTERFLY(mm1, mm6, mm0);
     247        5896 :     in[8 * 1] = (int16_t) (mm1 >> COL_SHIFT);
     248        5896 :     in[8 * 6] = (int16_t) (mm6 >> COL_SHIFT);
     249        5896 :     BUTTERFLY(mm2, mm5, mm0);
     250        5896 :     in[8 * 2] = (int16_t) (mm2 >> COL_SHIFT);
     251        5896 :     in[8 * 5] = (int16_t) (mm5 >> COL_SHIFT);
     252        5896 : }
     253             : 
     254       89600 : static void idct_col_3(short *const in)
     255             : {
     256             :     int mm0, mm1, mm2, mm3, mm4, mm5, mm6, mm7, spill;
     257             : 
     258             :     // odd
     259             : 
     260       89600 :     mm7 = (int) in[1 * 8];
     261       89600 :     mm4 = MULT(TAN1, mm7, 16);
     262             : 
     263       89600 :     mm6 = mm7 + mm4;
     264       89600 :     mm5 = mm7 - mm4;
     265       89600 :     mm6 = 2 * MULT(SQRT2, mm6, 16); // 2*sqrt2
     266       89600 :     mm5 = 2 * MULT(SQRT2, mm5, 16);
     267             : 
     268             :     // even
     269             : 
     270       89600 :     mm0 = mm1 = (int) in[0 * 8];
     271       89600 :     mm3 = (int) in[2 * 8];
     272       89600 :     mm2 = MULT(TAN2, mm3, 16);
     273             : 
     274       89600 :     BUTTERFLY(mm0, mm3, spill);
     275       89600 :     BUTTERFLY(mm0, mm7, spill);
     276       89600 :     in[8 * 0] = (int16_t) (mm0 >> COL_SHIFT);
     277       89600 :     in[8 * 7] = (int16_t) (mm7 >> COL_SHIFT);
     278       89600 :     BUTTERFLY(mm3, mm4, mm0);
     279       89600 :     in[8 * 3] = (int16_t) (mm3 >> COL_SHIFT);
     280       89600 :     in[8 * 4] = (int16_t) (mm4 >> COL_SHIFT);
     281             : 
     282       89600 :     BUTTERFLY(mm1, mm2, mm0);
     283       89600 :     BUTTERFLY(mm1, mm6, mm0);
     284       89600 :     in[8 * 1] = (int16_t) (mm1 >> COL_SHIFT);
     285       89600 :     in[8 * 6] = (int16_t) (mm6 >> COL_SHIFT);
     286       89600 :     BUTTERFLY(mm2, mm5, mm0);
     287       89600 :     in[8 * 2] = (int16_t) (mm2 >> COL_SHIFT);
     288       89600 :     in[8 * 5] = (int16_t) (mm5 >> COL_SHIFT);
     289       89600 : }
     290             : 
     291       60000 : void ff_xvid_idct(int16_t *const in)
     292             : {
     293       60000 :     int i, rows = 0x07;
     294             : 
     295       60000 :     idct_row(in + 0 * 8, TAB04, RND0);
     296       60000 :     idct_row(in + 1 * 8, TAB17, RND1);
     297       60000 :     idct_row(in + 2 * 8, TAB26, RND2);
     298       60000 :     if (idct_row(in + 3 * 8, TAB35, RND3))
     299       29558 :         rows |= 0x08;
     300       60000 :     if (idct_row(in + 4 * 8, TAB04, RND4))
     301       29661 :         rows |= 0x10;
     302       60000 :     if (idct_row(in + 5 * 8, TAB35, RND5))
     303       29720 :         rows |= 0x20;
     304       60000 :     if (idct_row(in + 6 * 8, TAB26, RND6))
     305       29731 :         rows |= 0x40;
     306       60000 :     if (idct_row(in + 7 * 8, TAB17, RND7))
     307       39653 :         rows |= 0x80;
     308             : 
     309       60000 :     if (rows & 0xF0) {
     310      432567 :         for (i = 0; i < 8; i++)
     311      384504 :             idct_col_8(in + i);
     312       11937 :     } else if (rows & 0x08) {
     313        6633 :         for (i = 0; i < 8; i++)
     314        5896 :             idct_col_4(in + i);
     315             :     } else {
     316      100800 :         for (i = 0; i < 8; i++)
     317       89600 :             idct_col_3(in + i);
     318             :     }
     319       60000 : }
     320             : 
     321           0 : static void xvid_idct_put(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
     322             : {
     323           0 :     ff_xvid_idct(block);
     324           0 :     ff_put_pixels_clamped_c(block, dest, line_size);
     325           0 : }
     326             : 
     327           0 : static void xvid_idct_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
     328             : {
     329           0 :     ff_xvid_idct(block);
     330           0 :     ff_add_pixels_clamped_c(block, dest, line_size);
     331           0 : }
     332             : 
     333           4 : av_cold void ff_xvid_idct_init(IDCTDSPContext *c, AVCodecContext *avctx)
     334             : {
     335           4 :     const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
     336             : 
     337           8 :     if (high_bit_depth || avctx->lowres ||
     338           8 :         !(avctx->idct_algo == FF_IDCT_AUTO ||
     339           4 :           avctx->idct_algo == FF_IDCT_XVID))
     340           0 :         return;
     341             : 
     342           4 :     if (avctx->idct_algo == FF_IDCT_XVID) {
     343           4 :         c->idct_put  = xvid_idct_put;
     344           4 :         c->idct_add  = xvid_idct_add;
     345           4 :         c->idct      = ff_xvid_idct;
     346           4 :         c->perm_type = FF_IDCT_PERM_NONE;
     347             :     }
     348             : 
     349             :     if (ARCH_X86)
     350           4 :         ff_xvid_idct_init_x86(c, avctx, high_bit_depth);
     351             :     if (ARCH_MIPS)
     352             :         ff_xvid_idct_init_mips(c, avctx, high_bit_depth);
     353             : 
     354           4 :     ff_init_scantable_permutation(c->idct_permutation, c->perm_type);
     355             : }

Generated by: LCOV version 1.13