~diresu/blender/blender-command-port

« back to all changes in this revision

Viewing changes to extern/x264/common/set.c

  • Committer: theeth
  • Date: 2008-10-14 16:52:04 UTC
  • Revision ID: vcs-imports@canonical.com-20081014165204-r32w2gm6s0osvdhn
copy back trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 * set.c: h264 encoder library
 
3
 *****************************************************************************
 
4
 * Copyright (C) 2005 x264 project
 
5
 *
 
6
 * Authors: Loren Merritt <lorenm@u.washington.edu>
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License as published by
 
10
 * the Free Software Foundation; either version 2 of the License, or
 
11
 * (at your option) any later version.
 
12
 *
 
13
 * This program 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
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program; if not, write to the Free Software
 
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 
21
 *****************************************************************************/
 
22
 
 
23
#include "common.h"
 
24
 
 
25
#define SHIFT(x,s) ((s)<0 ? (x)<<-(s) : (s)==0 ? (x) : ((x)+(1<<((s)-1)))>>(s))
 
26
#define DIV(n,d) (((n) + ((d)>>1)) / (d))
 
27
 
 
28
static const int dequant4_scale[6][3] =
 
29
{
 
30
    { 10, 13, 16 },
 
31
    { 11, 14, 18 },
 
32
    { 13, 16, 20 },
 
33
    { 14, 18, 23 },
 
34
    { 16, 20, 25 },
 
35
    { 18, 23, 29 }
 
36
};
 
37
static const int quant4_scale[6][3] =
 
38
{
 
39
    { 13107, 8066, 5243 },
 
40
    { 11916, 7490, 4660 },
 
41
    { 10082, 6554, 4194 },
 
42
    {  9362, 5825, 3647 },
 
43
    {  8192, 5243, 3355 },
 
44
    {  7282, 4559, 2893 },
 
45
};
 
46
 
 
47
static const int quant8_scan[16] =
 
48
{
 
49
    0,3,4,3, 3,1,5,1, 4,5,2,5, 3,1,5,1
 
50
};
 
51
static const int dequant8_scale[6][6] =
 
52
{
 
53
    { 20, 18, 32, 19, 25, 24 },
 
54
    { 22, 19, 35, 21, 28, 26 },
 
55
    { 26, 23, 42, 24, 33, 31 },
 
56
    { 28, 25, 45, 26, 35, 33 },
 
57
    { 32, 28, 51, 30, 40, 38 },
 
58
    { 36, 32, 58, 34, 46, 43 },
 
59
};
 
60
static const int quant8_scale[6][6] =
 
61
{
 
62
    { 13107, 11428, 20972, 12222, 16777, 15481 },
 
63
    { 11916, 10826, 19174, 11058, 14980, 14290 },
 
64
    { 10082,  8943, 15978,  9675, 12710, 11985 },
 
65
    {  9362,  8228, 14913,  8931, 11984, 11259 },
 
66
    {  8192,  7346, 13159,  7740, 10486,  9777 },
 
67
    {  7282,  6428, 11570,  6830,  9118,  8640 }
 
68
};
 
69
 
 
70
int x264_cqm_init( x264_t *h )
 
71
{
 
72
    int def_quant4[6][16];
 
73
    int def_quant8[6][64];
 
74
    int def_dequant4[6][16];
 
75
    int def_dequant8[6][64];
 
76
    int quant4_mf[4][6][4][4];
 
77
    int quant8_mf[2][6][8][8];
 
78
    int q, i, j, i_list;
 
79
    int deadzone[4] = { 32 - h->param.analyse.i_luma_deadzone[1],
 
80
                        32 - h->param.analyse.i_luma_deadzone[0], 
 
81
                        32 - 11, 32 - 21 };
 
82
    int max_qp_err = -1;
 
83
 
 
84
    for( i = 0; i < 6; i++ )
 
85
    {
 
86
        int size = i<4 ? 16 : 64;
 
87
        for( j = (i<4 ? 0 : 4); j < i; j++ )
 
88
            if( !memcmp( h->pps->scaling_list[i], h->pps->scaling_list[j], size*sizeof(uint8_t) ) )
 
89
                break;
 
90
        if( j < i )
 
91
        {
 
92
            h->  quant4_mf[i] = h->  quant4_mf[j];
 
93
            h->dequant4_mf[i] = h->dequant4_mf[j];
 
94
            h->unquant4_mf[i] = h->unquant4_mf[j];
 
95
        }
 
96
        else
 
97
        {
 
98
            h->  quant4_mf[i] = x264_malloc(52*size*sizeof(uint16_t) );
 
99
            h->dequant4_mf[i] = x264_malloc( 6*size*sizeof(int) );
 
100
            h->unquant4_mf[i] = x264_malloc(52*size*sizeof(int) );
 
101
        }
 
102
 
 
103
        for( j = (i<4 ? 0 : 4); j < i; j++ )
 
104
            if( deadzone[j&3] == deadzone[i&3] &&
 
105
                !memcmp( h->pps->scaling_list[i], h->pps->scaling_list[j], size*sizeof(uint8_t) ) )
 
106
                break;
 
107
        if( j < i )
 
108
            h->quant4_bias[i] = h->quant4_bias[j];
 
109
        else
 
110
            h->quant4_bias[i] = x264_malloc(52*size*sizeof(uint16_t) );
 
111
    }
 
112
 
 
113
    for( q = 0; q < 6; q++ )
 
114
    {
 
115
        for( i = 0; i < 16; i++ )
 
116
        {
 
117
            int j = (i&1) + ((i>>2)&1);
 
118
            def_dequant4[q][i] = dequant4_scale[q][j];
 
119
            def_quant4[q][i]   =   quant4_scale[q][j];
 
120
        }
 
121
        for( i = 0; i < 64; i++ )
 
122
        {
 
123
            int j = quant8_scan[((i>>1)&12) | (i&3)];
 
124
            def_dequant8[q][i] = dequant8_scale[q][j];
 
125
            def_quant8[q][i]   =   quant8_scale[q][j];
 
126
        }
 
127
    }
 
128
 
 
129
    for( q = 0; q < 6; q++ )
 
130
    {
 
131
        for( i_list = 0; i_list < 4; i_list++ )
 
132
            for( i = 0; i < 16; i++ )
 
133
            {
 
134
                h->dequant4_mf[i_list][q][0][i] = def_dequant4[q][i] * h->pps->scaling_list[i_list][i];
 
135
                     quant4_mf[i_list][q][0][i] = DIV(def_quant4[q][i] * 16, h->pps->scaling_list[i_list][i]);
 
136
            }
 
137
        for( i_list = 0; i_list < 2; i_list++ )
 
138
            for( i = 0; i < 64; i++ )
 
139
            {
 
140
                h->dequant8_mf[i_list][q][0][i] = def_dequant8[q][i] * h->pps->scaling_list[4+i_list][i];
 
141
                     quant8_mf[i_list][q][0][i] = DIV(def_quant8[q][i] * 16, h->pps->scaling_list[4+i_list][i]);
 
142
            }
 
143
    }
 
144
    for( q = 0; q < 52; q++ )
 
145
    {
 
146
        for( i_list = 0; i_list < 4; i_list++ )
 
147
            for( i = 0; i < 16; i++ )
 
148
            {
 
149
                h->unquant4_mf[i_list][q][i] = (1ULL << (q/6 + 15 + 8)) / quant4_mf[i_list][q%6][0][i];
 
150
                h->  quant4_mf[i_list][q][i] = j = SHIFT(quant4_mf[i_list][q%6][0][i], q/6 - 1);
 
151
                // round to nearest, unless that would cause the deadzone to be negative
 
152
                h->quant4_bias[i_list][q][i] = X264_MIN( DIV(deadzone[i_list]<<10, j), (1<<15)/j );
 
153
                if( j > 0xffff && q > max_qp_err )
 
154
                    max_qp_err = q;
 
155
            }
 
156
        if( h->param.analyse.b_transform_8x8 )
 
157
        for( i_list = 0; i_list < 2; i_list++ )
 
158
            for( i = 0; i < 64; i++ )
 
159
            {
 
160
                h->unquant8_mf[i_list][q][i] = (1ULL << (q/6 + 16 + 8)) / quant8_mf[i_list][q%6][0][i];
 
161
                h->  quant8_mf[i_list][q][i] = j = SHIFT(quant8_mf[i_list][q%6][0][i], q/6);
 
162
                h->quant8_bias[i_list][q][i] = X264_MIN( DIV(deadzone[i_list]<<10, j), (1<<15)/j );
 
163
                if( j > 0xffff && q > max_qp_err )
 
164
                    max_qp_err = q;
 
165
            }
 
166
    }
 
167
 
 
168
    if( !h->mb.b_lossless && max_qp_err >= h->param.rc.i_qp_min )
 
169
    {
 
170
        x264_log( h, X264_LOG_ERROR, "Quantization overflow.\n" );
 
171
        x264_log( h, X264_LOG_ERROR, "Your CQM is incompatible with QP < %d, but min QP is set to %d\n",
 
172
                  max_qp_err+1, h->param.rc.i_qp_min );
 
173
        return -1;
 
174
    }
 
175
    return 0;
 
176
}
 
177
 
 
178
void x264_cqm_delete( x264_t *h )
 
179
{
 
180
    int i, j;
 
181
    for( i = 0; i < 6; i++ )
 
182
    {
 
183
        for( j = 0; j < i; j++ )
 
184
            if( h->quant4_mf[i] == h->quant4_mf[j] )
 
185
                break;
 
186
        if( j == i )
 
187
        {
 
188
            x264_free( h->  quant4_mf[i] );
 
189
            x264_free( h->dequant4_mf[i] );
 
190
            x264_free( h->unquant4_mf[i] );
 
191
        }
 
192
        for( j = 0; j < i; j++ )
 
193
            if( h->quant4_bias[i] == h->quant4_bias[j] )
 
194
                break;
 
195
        if( j == i )
 
196
            x264_free( h->quant4_bias[i] );
 
197
    }
 
198
}
 
199
 
 
200
int x264_cqm_parse_jmlist( x264_t *h, const char *buf, const char *name,
 
201
                           uint8_t *cqm, const uint8_t *jvt, int length )
 
202
{
 
203
    char *p;
 
204
    char *nextvar;
 
205
    int i;
 
206
 
 
207
    p = strstr( buf, name );
 
208
    if( !p )
 
209
    {
 
210
        memset( cqm, 16, length );
 
211
        return 0;
 
212
    }
 
213
 
 
214
    p += strlen( name );
 
215
    if( *p == 'U' || *p == 'V' )
 
216
        p++;
 
217
 
 
218
    nextvar = strstr( p, "INT" );
 
219
 
 
220
    for( i = 0; i < length && (p = strpbrk( p, " \t\n," )) && (p = strpbrk( p, "0123456789" )); i++ )
 
221
    {
 
222
        int coef = -1;
 
223
        sscanf( p, "%d", &coef );
 
224
        if( i == 0 && coef == 0 )
 
225
        {
 
226
            memcpy( cqm, jvt, length );
 
227
            return 0;
 
228
        }
 
229
        if( coef < 1 || coef > 255 )
 
230
        {
 
231
            x264_log( h, X264_LOG_ERROR, "bad coefficient in list '%s'\n", name );
 
232
            return -1;
 
233
        }
 
234
        cqm[i] = coef;
 
235
    }
 
236
 
 
237
    if( (nextvar && p > nextvar) || i != length )
 
238
    {
 
239
        x264_log( h, X264_LOG_ERROR, "not enough coefficients in list '%s'\n", name );
 
240
        return -1;
 
241
    }
 
242
 
 
243
    return 0;
 
244
}
 
245
 
 
246
int x264_cqm_parse_file( x264_t *h, const char *filename )
 
247
{
 
248
    char *buf, *p;
 
249
    int b_error = 0;
 
250
 
 
251
    h->param.i_cqm_preset = X264_CQM_CUSTOM;
 
252
    
 
253
    buf = x264_slurp_file( filename );
 
254
    if( !buf )
 
255
    {
 
256
        x264_log( h, X264_LOG_ERROR, "can't open file '%s'\n", filename );
 
257
        return -1;
 
258
    }
 
259
 
 
260
    while( (p = strchr( buf, '#' )) != NULL )
 
261
        memset( p, ' ', strcspn( p, "\n" ) );
 
262
 
 
263
    b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA4X4_LUMA",   h->param.cqm_4iy, x264_cqm_jvt4i, 16 );
 
264
    b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA4X4_CHROMA", h->param.cqm_4ic, x264_cqm_jvt4i, 16 );
 
265
    b_error |= x264_cqm_parse_jmlist( h, buf, "INTER4X4_LUMA",   h->param.cqm_4py, x264_cqm_jvt4p, 16 );
 
266
    b_error |= x264_cqm_parse_jmlist( h, buf, "INTER4X4_CHROMA", h->param.cqm_4pc, x264_cqm_jvt4p, 16 );
 
267
    b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA8X8_LUMA",   h->param.cqm_8iy, x264_cqm_jvt8i, 64 );
 
268
    b_error |= x264_cqm_parse_jmlist( h, buf, "INTER8X8_LUMA",   h->param.cqm_8py, x264_cqm_jvt8p, 64 );
 
269
 
 
270
    x264_free( buf );
 
271
    return b_error;
 
272
}
 
273