~ubuntu-branches/ubuntu/maverick/x264/maverick-proposed

« back to all changes in this revision

Viewing changes to common/quant.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2010-07-04 16:23:51 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100704162351-pymrwxxpugrzptjv
Tags: 2:0.98.1653+git88b90d9-1
* require libavformat-dev from 0.6 or better
* remove conflicts/replaces to ancient package x264-bin (dapper!
  times)
* new upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
#include "common.h"
25
25
 
26
 
#ifdef HAVE_MMX
 
26
#if HAVE_MMX
27
27
#include "x86/quant.h"
28
28
#endif
29
 
#ifdef ARCH_PPC
 
29
#if ARCH_PPC
30
30
#   include "ppc/quant.h"
31
31
#endif
32
 
#ifdef ARCH_ARM
 
32
#if ARCH_ARM
33
33
#   include "arm/quant.h"
34
34
#endif
35
35
 
42
42
    nz |= (coef); \
43
43
}
44
44
 
45
 
static int quant_8x8( int16_t dct[64], uint16_t mf[64], uint16_t bias[64] )
46
 
{
47
 
    int i, nz = 0;
48
 
    for( i = 0; i < 64; i++ )
49
 
        QUANT_ONE( dct[i], mf[i], bias[i] );
50
 
    return !!nz;
51
 
}
52
 
 
53
 
static int quant_4x4( int16_t dct[16], uint16_t mf[16], uint16_t bias[16] )
54
 
{
55
 
    int i, nz = 0;
56
 
    for( i = 0; i < 16; i++ )
57
 
        QUANT_ONE( dct[i], mf[i], bias[i] );
58
 
    return !!nz;
59
 
}
60
 
 
61
 
static int quant_4x4_dc( int16_t dct[16], int mf, int bias )
62
 
{
63
 
    int i, nz = 0;
64
 
    for( i = 0; i < 16; i++ )
 
45
static int quant_8x8( dctcoef dct[64], uint16_t mf[64], uint16_t bias[64] )
 
46
{
 
47
    int nz = 0;
 
48
    for( int i = 0; i < 64; i++ )
 
49
        QUANT_ONE( dct[i], mf[i], bias[i] );
 
50
    return !!nz;
 
51
}
 
52
 
 
53
static int quant_4x4( dctcoef dct[16], uint16_t mf[16], uint16_t bias[16] )
 
54
{
 
55
    int nz = 0;
 
56
    for( int i = 0; i < 16; i++ )
 
57
        QUANT_ONE( dct[i], mf[i], bias[i] );
 
58
    return !!nz;
 
59
}
 
60
 
 
61
static int quant_4x4_dc( dctcoef dct[16], int mf, int bias )
 
62
{
 
63
    int nz = 0;
 
64
    for( int i = 0; i < 16; i++ )
65
65
        QUANT_ONE( dct[i], mf, bias );
66
66
    return !!nz;
67
67
}
68
68
 
69
 
static int quant_2x2_dc( int16_t dct[4], int mf, int bias )
 
69
static int quant_2x2_dc( dctcoef dct[4], int mf, int bias )
70
70
{
71
71
    int nz = 0;
72
72
    QUANT_ONE( dct[0], mf, bias );
82
82
#define DEQUANT_SHR( x ) \
83
83
    dct[x] = ( dct[x] * dequant_mf[i_mf][x] + f ) >> (-i_qbits)
84
84
 
85
 
static void dequant_4x4( int16_t dct[16], int dequant_mf[6][16], int i_qp )
 
85
static void dequant_4x4( dctcoef dct[16], int dequant_mf[6][16], int i_qp )
86
86
{
87
87
    const int i_mf = i_qp%6;
88
88
    const int i_qbits = i_qp/6 - 4;
89
 
    int i;
90
89
 
91
90
    if( i_qbits >= 0 )
92
91
    {
93
 
        for( i = 0; i < 16; i++ )
 
92
        for( int i = 0; i < 16; i++ )
94
93
            DEQUANT_SHL( i );
95
94
    }
96
95
    else
97
96
    {
98
97
        const int f = 1 << (-i_qbits-1);
99
 
        for( i = 0; i < 16; i++ )
 
98
        for( int i = 0; i < 16; i++ )
100
99
            DEQUANT_SHR( i );
101
100
    }
102
101
}
103
102
 
104
 
static void dequant_8x8( int16_t dct[64], int dequant_mf[6][64], int i_qp )
 
103
static void dequant_8x8( dctcoef dct[64], int dequant_mf[6][64], int i_qp )
105
104
{
106
105
    const int i_mf = i_qp%6;
107
106
    const int i_qbits = i_qp/6 - 6;
108
 
    int i;
109
107
 
110
108
    if( i_qbits >= 0 )
111
109
    {
112
 
        for( i = 0; i < 64; i++ )
 
110
        for( int i = 0; i < 64; i++ )
113
111
            DEQUANT_SHL( i );
114
112
    }
115
113
    else
116
114
    {
117
115
        const int f = 1 << (-i_qbits-1);
118
 
        for( i = 0; i < 64; i++ )
 
116
        for( int i = 0; i < 64; i++ )
119
117
            DEQUANT_SHR( i );
120
118
    }
121
119
}
122
120
 
123
 
static void dequant_4x4_dc( int16_t dct[16], int dequant_mf[6][16], int i_qp )
 
121
static void dequant_4x4_dc( dctcoef dct[16], int dequant_mf[6][16], int i_qp )
124
122
{
125
123
    const int i_qbits = i_qp/6 - 6;
126
 
    int i;
127
124
 
128
125
    if( i_qbits >= 0 )
129
126
    {
130
127
        const int i_dmf = dequant_mf[i_qp%6][0] << i_qbits;
131
 
        for( i = 0; i < 16; i++ )
 
128
        for( int i = 0; i < 16; i++ )
132
129
            dct[i] *= i_dmf;
133
130
    }
134
131
    else
135
132
    {
136
133
        const int i_dmf = dequant_mf[i_qp%6][0];
137
134
        const int f = 1 << (-i_qbits-1);
138
 
        for( i = 0; i < 16; i++ )
 
135
        for( int i = 0; i < 16; i++ )
139
136
            dct[i] = ( dct[i] * i_dmf + f ) >> (-i_qbits);
140
137
    }
141
138
}
142
139
 
143
 
static void x264_denoise_dct( int16_t *dct, uint32_t *sum, uint16_t *offset, int size )
 
140
static void x264_denoise_dct( dctcoef *dct, uint32_t *sum, uint16_t *offset, int size )
144
141
{
145
 
    int i;
146
 
    for( i=1; i<size; i++ )
 
142
    for( int i = 1; i < size; i++ )
147
143
    {
148
144
        int level = dct[i];
149
145
        int sign = level>>15;
163
159
 *  chroma: for the complete mb: if score < 7 -> null
164
160
 */
165
161
 
166
 
const uint8_t x264_decimate_table4[16] = {
167
 
    3,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0 };
168
 
const uint8_t x264_decimate_table8[64] = {
 
162
const uint8_t x264_decimate_table4[16] =
 
163
{
 
164
    3,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0
 
165
};
 
166
const uint8_t x264_decimate_table8[64] =
 
167
{
169
168
    3,3,3,3,2,2,2,2,2,2,2,2,1,1,1,1,
170
169
    1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,
171
170
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
172
 
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
 
171
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
 
172
};
173
173
 
174
 
static int ALWAYS_INLINE x264_decimate_score_internal( int16_t *dct, int i_max )
 
174
static int ALWAYS_INLINE x264_decimate_score_internal( dctcoef *dct, int i_max )
175
175
{
176
176
    const uint8_t *ds_table = (i_max == 64) ? x264_decimate_table8 : x264_decimate_table4;
177
177
    int i_score = 0;
178
178
    int idx = i_max - 1;
179
179
 
180
180
    /* Yes, dct[idx-1] is guaranteed to be 32-bit aligned.  idx>=0 instead of 1 works correctly for the same reason */
181
 
    while( idx >= 0 && M32( &dct[idx-1] ) == 0 )
 
181
    while( idx >= 0 && MDCT_X2( &dct[idx-1] ) == 0 )
182
182
        idx -= 2;
183
183
    if( idx >= 0 && dct[idx] == 0 )
184
184
        idx--;
201
201
    return i_score;
202
202
}
203
203
 
204
 
static int x264_decimate_score15( int16_t *dct )
 
204
static int x264_decimate_score15( dctcoef *dct )
205
205
{
206
206
    return x264_decimate_score_internal( dct+1, 15 );
207
207
}
208
 
static int x264_decimate_score16( int16_t *dct )
 
208
static int x264_decimate_score16( dctcoef *dct )
209
209
{
210
210
    return x264_decimate_score_internal( dct, 16 );
211
211
}
212
 
static int x264_decimate_score64( int16_t *dct )
 
212
static int x264_decimate_score64( dctcoef *dct )
213
213
{
214
214
    return x264_decimate_score_internal( dct, 64 );
215
215
}
216
216
 
217
 
static int ALWAYS_INLINE x264_coeff_last_internal( int16_t *l, int i_count )
 
217
static int ALWAYS_INLINE x264_coeff_last_internal( dctcoef *l, int i_count )
218
218
{
219
219
    int i_last;
220
220
    for( i_last = i_count-1; i_last >= 3; i_last -= 4 )
225
225
    return i_last;
226
226
}
227
227
 
228
 
static int x264_coeff_last4( int16_t *l )
 
228
static int x264_coeff_last4( dctcoef *l )
229
229
{
230
230
    return x264_coeff_last_internal( l, 4 );
231
231
}
232
 
static int x264_coeff_last15( int16_t *l )
 
232
static int x264_coeff_last15( dctcoef *l )
233
233
{
234
234
    return x264_coeff_last_internal( l, 15 );
235
235
}
236
 
static int x264_coeff_last16( int16_t *l )
 
236
static int x264_coeff_last16( dctcoef *l )
237
237
{
238
238
    return x264_coeff_last_internal( l, 16 );
239
239
}
240
 
static int x264_coeff_last64( int16_t *l )
 
240
static int x264_coeff_last64( dctcoef *l )
241
241
{
242
242
    return x264_coeff_last_internal( l, 64 );
243
243
}
244
244
 
245
245
#define level_run(num)\
246
 
static int x264_coeff_level_run##num( int16_t *dct, x264_run_level_t *runlevel )\
 
246
static int x264_coeff_level_run##num( dctcoef *dct, x264_run_level_t *runlevel )\
247
247
{\
248
248
    int i_last = runlevel->last = x264_coeff_last##num(dct);\
249
249
    int i_total = 0;\
287
287
    pf->coeff_level_run[  DCT_LUMA_AC] = x264_coeff_level_run15;
288
288
    pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16;
289
289
 
290
 
#ifdef HAVE_MMX
 
290
#if HAVE_MMX
291
291
    if( cpu&X264_CPU_MMX )
292
292
    {
293
 
#ifdef ARCH_X86
 
293
#if ARCH_X86
294
294
        pf->quant_4x4 = x264_quant_4x4_mmx;
295
295
        pf->quant_8x8 = x264_quant_8x8_mmx;
296
296
        pf->dequant_4x4 = x264_dequant_4x4_mmx;
308
308
    if( cpu&X264_CPU_MMXEXT )
309
309
    {
310
310
        pf->quant_2x2_dc = x264_quant_2x2_dc_mmxext;
311
 
#ifdef ARCH_X86
 
311
#if ARCH_X86
312
312
        pf->quant_4x4_dc = x264_quant_4x4_dc_mmxext;
313
313
        pf->decimate_score15 = x264_decimate_score15_mmxext;
314
314
        pf->decimate_score16 = x264_decimate_score16_mmxext;
 
315
        if( cpu&X264_CPU_SLOW_CTZ )
 
316
        {
 
317
            pf->decimate_score15 = x264_decimate_score15_mmxext_slowctz;
 
318
            pf->decimate_score16 = x264_decimate_score16_mmxext_slowctz;
 
319
        }
315
320
        pf->decimate_score64 = x264_decimate_score64_mmxext;
316
321
        pf->coeff_last[  DCT_LUMA_AC] = x264_coeff_last15_mmxext;
317
322
        pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16_mmxext;
345
350
        pf->decimate_score15 = x264_decimate_score15_sse2;
346
351
        pf->decimate_score16 = x264_decimate_score16_sse2;
347
352
        pf->decimate_score64 = x264_decimate_score64_sse2;
 
353
        if( cpu&X264_CPU_SLOW_CTZ )
 
354
        {
 
355
            pf->decimate_score15 = x264_decimate_score15_sse2_slowctz;
 
356
            pf->decimate_score16 = x264_decimate_score16_sse2_slowctz;
 
357
        }
348
358
        pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2;
349
359
        pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2;
350
360
        pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2;
369
379
        pf->denoise_dct = x264_denoise_dct_ssse3;
370
380
        pf->decimate_score15 = x264_decimate_score15_ssse3;
371
381
        pf->decimate_score16 = x264_decimate_score16_ssse3;
 
382
        if( cpu&X264_CPU_SLOW_CTZ )
 
383
        {
 
384
            pf->decimate_score15 = x264_decimate_score15_ssse3_slowctz;
 
385
            pf->decimate_score16 = x264_decimate_score16_ssse3_slowctz;
 
386
        }
372
387
        pf->decimate_score64 = x264_decimate_score64_ssse3;
373
388
    }
374
389
 
380
395
    }
381
396
#endif // HAVE_MMX
382
397
 
383
 
#ifdef ARCH_PPC
 
398
#if HAVE_ALTIVEC
384
399
    if( cpu&X264_CPU_ALTIVEC ) {
385
400
        pf->quant_2x2_dc = x264_quant_2x2_dc_altivec;
386
401
        pf->quant_4x4_dc = x264_quant_4x4_dc_altivec;
392
407
    }
393
408
#endif
394
409
 
395
 
#ifdef HAVE_ARMV6
 
410
#if HAVE_ARMV6
396
411
    if( cpu&X264_CPU_ARMV6 )
397
412
        pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_arm;
398
413