]> git.sesse.net Git - x264/blob - common/set.c
x86inc: Fix AVX emulation of scalar float instructions
[x264] / common / set.c
1 /*****************************************************************************
2  * set.c: quantization init
3  *****************************************************************************
4  * Copyright (C) 2005-2016 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., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
21  *
22  * This program is also available under a commercial proprietary license.
23  * For more information, contact us at licensing@x264.com.
24  *****************************************************************************/
25
26 #include "common.h"
27
28 #define SHIFT(x,s) ((s)<=0 ? (x)<<-(s) : ((x)+(1<<((s)-1)))>>(s))
29 #define DIV(n,d) (((n) + ((d)>>1)) / (d))
30
31 static const uint8_t dequant4_scale[6][3] =
32 {
33     { 10, 13, 16 },
34     { 11, 14, 18 },
35     { 13, 16, 20 },
36     { 14, 18, 23 },
37     { 16, 20, 25 },
38     { 18, 23, 29 }
39 };
40 static const uint16_t quant4_scale[6][3] =
41 {
42     { 13107, 8066, 5243 },
43     { 11916, 7490, 4660 },
44     { 10082, 6554, 4194 },
45     {  9362, 5825, 3647 },
46     {  8192, 5243, 3355 },
47     {  7282, 4559, 2893 },
48 };
49
50 static const uint8_t quant8_scan[16] =
51 {
52     0,3,4,3, 3,1,5,1, 4,5,2,5, 3,1,5,1
53 };
54 static const uint8_t dequant8_scale[6][6] =
55 {
56     { 20, 18, 32, 19, 25, 24 },
57     { 22, 19, 35, 21, 28, 26 },
58     { 26, 23, 42, 24, 33, 31 },
59     { 28, 25, 45, 26, 35, 33 },
60     { 32, 28, 51, 30, 40, 38 },
61     { 36, 32, 58, 34, 46, 43 },
62 };
63 static const uint16_t quant8_scale[6][6] =
64 {
65     { 13107, 11428, 20972, 12222, 16777, 15481 },
66     { 11916, 10826, 19174, 11058, 14980, 14290 },
67     { 10082,  8943, 15978,  9675, 12710, 11985 },
68     {  9362,  8228, 14913,  8931, 11984, 11259 },
69     {  8192,  7346, 13159,  7740, 10486,  9777 },
70     {  7282,  6428, 11570,  6830,  9118,  8640 }
71 };
72
73 int x264_cqm_init( x264_t *h )
74 {
75     int def_quant4[6][16];
76     int def_quant8[6][64];
77     int def_dequant4[6][16];
78     int def_dequant8[6][64];
79     int quant4_mf[4][6][16];
80     int quant8_mf[4][6][64];
81     int deadzone[4] = { 32 - h->param.analyse.i_luma_deadzone[1],
82                         32 - h->param.analyse.i_luma_deadzone[0],
83                         32 - 11, 32 - 21 };
84     int max_qp_err = -1;
85     int max_chroma_qp_err = -1;
86     int min_qp_err = QP_MAX+1;
87     int num_8x8_lists = h->sps->i_chroma_format_idc == CHROMA_444 ? 4
88                       : h->param.analyse.b_transform_8x8 ? 2 : 0; /* Checkasm may segfault if optimized out by --chroma-format */
89
90 #define CQM_ALLOC( w, count )\
91     for( int i = 0; i < count; i++ )\
92     {\
93         int size = w*w;\
94         int start = w == 8 ? 4 : 0;\
95         int j;\
96         for( j = 0; j < i; j++ )\
97             if( !memcmp( h->pps->scaling_list[i+start], h->pps->scaling_list[j+start], size*sizeof(uint8_t) ) )\
98                 break;\
99         if( j < i )\
100         {\
101             h->  quant##w##_mf[i] = h->  quant##w##_mf[j];\
102             h->dequant##w##_mf[i] = h->dequant##w##_mf[j];\
103             h->unquant##w##_mf[i] = h->unquant##w##_mf[j];\
104         }\
105         else\
106         {\
107             CHECKED_MALLOC( h->  quant##w##_mf[i], (QP_MAX_SPEC+1)*size*sizeof(udctcoef) );\
108             CHECKED_MALLOC( h->dequant##w##_mf[i],  6*size*sizeof(int) );\
109             CHECKED_MALLOC( h->unquant##w##_mf[i], (QP_MAX_SPEC+1)*size*sizeof(int) );\
110         }\
111         for( j = 0; j < i; j++ )\
112             if( deadzone[j] == deadzone[i] &&\
113                 !memcmp( h->pps->scaling_list[i+start], h->pps->scaling_list[j+start], size*sizeof(uint8_t) ) )\
114                 break;\
115         if( j < i )\
116         {\
117             h->quant##w##_bias[i] = h->quant##w##_bias[j];\
118             h->quant##w##_bias0[i] = h->quant##w##_bias0[j];\
119         }\
120         else\
121         {\
122             CHECKED_MALLOC( h->quant##w##_bias[i], (QP_MAX_SPEC+1)*size*sizeof(udctcoef) );\
123             CHECKED_MALLOC( h->quant##w##_bias0[i], (QP_MAX_SPEC+1)*size*sizeof(udctcoef) );\
124         }\
125     }
126
127     CQM_ALLOC( 4, 4 )
128     CQM_ALLOC( 8, num_8x8_lists )
129
130     for( int q = 0; q < 6; q++ )
131     {
132         for( int i = 0; i < 16; i++ )
133         {
134             int j = (i&1) + ((i>>2)&1);
135             def_dequant4[q][i] = dequant4_scale[q][j];
136             def_quant4[q][i]   =   quant4_scale[q][j];
137         }
138         for( int i = 0; i < 64; i++ )
139         {
140             int j = quant8_scan[((i>>1)&12) | (i&3)];
141             def_dequant8[q][i] = dequant8_scale[q][j];
142             def_quant8[q][i]   =   quant8_scale[q][j];
143         }
144     }
145
146     for( int q = 0; q < 6; q++ )
147     {
148         for( int i_list = 0; i_list < 4; i_list++ )
149             for( int i = 0; i < 16; i++ )
150             {
151                 h->dequant4_mf[i_list][q][i] = def_dequant4[q][i] * h->pps->scaling_list[i_list][i];
152                      quant4_mf[i_list][q][i] = DIV(def_quant4[q][i] * 16, h->pps->scaling_list[i_list][i]);
153             }
154         for( int i_list = 0; i_list < num_8x8_lists; i_list++ )
155             for( int i = 0; i < 64; i++ )
156             {
157                 h->dequant8_mf[i_list][q][i] = def_dequant8[q][i] * h->pps->scaling_list[4+i_list][i];
158                      quant8_mf[i_list][q][i] = DIV(def_quant8[q][i] * 16, h->pps->scaling_list[4+i_list][i]);
159             }
160     }
161     for( int q = 0; q <= QP_MAX_SPEC; q++ )
162     {
163         int j;
164         for( int i_list = 0; i_list < 4; i_list++ )
165             for( int i = 0; i < 16; i++ )
166             {
167                 h->unquant4_mf[i_list][q][i] = (1ULL << (q/6 + 15 + 8)) / quant4_mf[i_list][q%6][i];
168                 h->quant4_mf[i_list][q][i] = j = SHIFT(quant4_mf[i_list][q%6][i], q/6 - 1);
169                 if( !j )
170                 {
171                     min_qp_err = X264_MIN( min_qp_err, q );
172                     continue;
173                 }
174                 // round to nearest, unless that would cause the deadzone to be negative
175                 h->quant4_bias[i_list][q][i] = X264_MIN( DIV(deadzone[i_list]<<10, j), (1<<15)/j );
176                 h->quant4_bias0[i_list][q][i] = (1<<15)/j;
177                 if( j > 0xffff && q > max_qp_err && (i_list == CQM_4IY || i_list == CQM_4PY) )
178                     max_qp_err = q;
179                 if( j > 0xffff && q > max_chroma_qp_err && (i_list == CQM_4IC || i_list == CQM_4PC) )
180                     max_chroma_qp_err = q;
181             }
182         if( h->param.analyse.b_transform_8x8 )
183             for( int i_list = 0; i_list < num_8x8_lists; i_list++ )
184                 for( int i = 0; i < 64; i++ )
185                 {
186                     h->unquant8_mf[i_list][q][i] = (1ULL << (q/6 + 16 + 8)) / quant8_mf[i_list][q%6][i];
187                     j = SHIFT(quant8_mf[i_list][q%6][i], q/6);
188                     h->quant8_mf[i_list][q][i] = (uint16_t)j;
189
190                     if( !j )
191                     {
192                         min_qp_err = X264_MIN( min_qp_err, q );
193                         continue;
194                     }
195                     h->quant8_bias[i_list][q][i] = X264_MIN( DIV(deadzone[i_list]<<10, j), (1<<15)/j );
196                     h->quant8_bias0[i_list][q][i] = (1<<15)/j;
197                     if( j > 0xffff && q > max_qp_err && (i_list == CQM_8IY || i_list == CQM_8PY) )
198                         max_qp_err = q;
199                     if( j > 0xffff && q > max_chroma_qp_err && (i_list == CQM_8IC || i_list == CQM_8PC) )
200                         max_chroma_qp_err = q;
201                 }
202     }
203
204     /* Emergency mode denoising. */
205     x264_emms();
206     CHECKED_MALLOC( h->nr_offset_emergency, sizeof(*h->nr_offset_emergency)*(QP_MAX-QP_MAX_SPEC) );
207     for( int q = 0; q < QP_MAX - QP_MAX_SPEC; q++ )
208         for( int cat = 0; cat < 3 + CHROMA444; cat++ )
209         {
210             int dct8x8 = cat&1;
211             if( !h->param.analyse.b_transform_8x8 && dct8x8 )
212                 continue;
213
214             int size = dct8x8 ? 64 : 16;
215             udctcoef *nr_offset = h->nr_offset_emergency[q][cat];
216             /* Denoise chroma first (due to h264's chroma QP offset), then luma, then DC. */
217             int dc_threshold =    (QP_MAX-QP_MAX_SPEC)*2/3;
218             int luma_threshold =  (QP_MAX-QP_MAX_SPEC)*2/3;
219             int chroma_threshold = 0;
220
221             for( int i = 0; i < size; i++ )
222             {
223                 int max = (1 << (7 + BIT_DEPTH)) - 1;
224                 /* True "emergency mode": remove all DCT coefficients */
225                 if( q == QP_MAX - QP_MAX_SPEC - 1 )
226                 {
227                     nr_offset[i] = max;
228                     continue;
229                 }
230
231                 int thresh = i == 0 ? dc_threshold : cat >= 2 ? chroma_threshold : luma_threshold;
232                 if( q < thresh )
233                 {
234                     nr_offset[i] = 0;
235                     continue;
236                 }
237                 double pos = (double)(q-thresh+1) / (QP_MAX - QP_MAX_SPEC - thresh);
238
239                 /* XXX: this math is largely tuned for /dev/random input. */
240                 double start = dct8x8 ? h->unquant8_mf[CQM_8PY][QP_MAX_SPEC][i]
241                                       : h->unquant4_mf[CQM_4PY][QP_MAX_SPEC][i];
242                 /* Formula chosen as an exponential scale to vaguely mimic the effects
243                  * of a higher quantizer. */
244                 double bias = (pow( 2, pos*(QP_MAX - QP_MAX_SPEC)/10. )*0.003-0.003) * start;
245                 nr_offset[i] = X264_MIN( bias + 0.5, max );
246             }
247         }
248
249     if( !h->mb.b_lossless )
250     {
251         while( h->chroma_qp_table[SPEC_QP(h->param.rc.i_qp_min)] <= max_chroma_qp_err )
252             h->param.rc.i_qp_min++;
253         if( min_qp_err <= h->param.rc.i_qp_max )
254             h->param.rc.i_qp_max = min_qp_err-1;
255         if( max_qp_err >= h->param.rc.i_qp_min )
256             h->param.rc.i_qp_min = max_qp_err+1;
257         /* If long level-codes aren't allowed, we need to allow QP high enough to avoid them. */
258         if( !h->param.b_cabac && h->sps->i_profile_idc < PROFILE_HIGH )
259             while( h->chroma_qp_table[SPEC_QP(h->param.rc.i_qp_max)] <= 12 || h->param.rc.i_qp_max <= 12 )
260                 h->param.rc.i_qp_max++;
261         if( h->param.rc.i_qp_min > h->param.rc.i_qp_max )
262         {
263             x264_log( h, X264_LOG_ERROR, "Impossible QP constraints for CQM (min=%d, max=%d)\n", h->param.rc.i_qp_min, h->param.rc.i_qp_max );
264             return -1;
265         }
266     }
267     return 0;
268 fail:
269     x264_cqm_delete( h );
270     return -1;
271 }
272
273 #define CQM_DELETE( n, max )\
274     for( int i = 0; i < (max); i++ )\
275     {\
276         int j;\
277         for( j = 0; j < i; j++ )\
278             if( h->quant##n##_mf[i] == h->quant##n##_mf[j] )\
279                 break;\
280         if( j == i )\
281         {\
282             x264_free( h->  quant##n##_mf[i] );\
283             x264_free( h->dequant##n##_mf[i] );\
284             x264_free( h->unquant##n##_mf[i] );\
285         }\
286         for( j = 0; j < i; j++ )\
287             if( h->quant##n##_bias[i] == h->quant##n##_bias[j] )\
288                 break;\
289         if( j == i )\
290         {\
291             x264_free( h->quant##n##_bias[i] );\
292             x264_free( h->quant##n##_bias0[i] );\
293         }\
294     }
295
296 void x264_cqm_delete( x264_t *h )
297 {
298     CQM_DELETE( 4, 4 );
299     CQM_DELETE( 8, CHROMA444 ? 4 : 2 );
300     x264_free( h->nr_offset_emergency );
301 }
302
303 static int x264_cqm_parse_jmlist( x264_t *h, const char *buf, const char *name,
304                                   uint8_t *cqm, const uint8_t *jvt, int length )
305 {
306     int i;
307
308     char *p = strstr( buf, name );
309     if( !p )
310     {
311         memset( cqm, 16, length );
312         return 0;
313     }
314
315     p += strlen( name );
316     if( *p == 'U' || *p == 'V' )
317         p++;
318
319     char *nextvar = strstr( p, "INT" );
320
321     for( i = 0; i < length && (p = strpbrk( p, " \t\n," )) && (p = strpbrk( p, "0123456789" )); i++ )
322     {
323         int coef = -1;
324         sscanf( p, "%d", &coef );
325         if( i == 0 && coef == 0 )
326         {
327             memcpy( cqm, jvt, length );
328             return 0;
329         }
330         if( coef < 1 || coef > 255 )
331         {
332             x264_log( h, X264_LOG_ERROR, "bad coefficient in list '%s'\n", name );
333             return -1;
334         }
335         cqm[i] = coef;
336     }
337
338     if( (nextvar && p > nextvar) || i != length )
339     {
340         x264_log( h, X264_LOG_ERROR, "not enough coefficients in list '%s'\n", name );
341         return -1;
342     }
343
344     return 0;
345 }
346
347 int x264_cqm_parse_file( x264_t *h, const char *filename )
348 {
349     char *p;
350     int b_error = 0;
351
352     h->param.i_cqm_preset = X264_CQM_CUSTOM;
353
354     char *buf = x264_slurp_file( filename );
355     if( !buf )
356     {
357         x264_log( h, X264_LOG_ERROR, "can't open file '%s'\n", filename );
358         return -1;
359     }
360
361     while( (p = strchr( buf, '#' )) != NULL )
362         memset( p, ' ', strcspn( p, "\n" ) );
363
364     b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA4X4_LUMA",   h->param.cqm_4iy, x264_cqm_jvt4i, 16 );
365     b_error |= x264_cqm_parse_jmlist( h, buf, "INTER4X4_LUMA",   h->param.cqm_4py, x264_cqm_jvt4p, 16 );
366     b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA4X4_CHROMA", h->param.cqm_4ic, x264_cqm_jvt4i, 16 );
367     b_error |= x264_cqm_parse_jmlist( h, buf, "INTER4X4_CHROMA", h->param.cqm_4pc, x264_cqm_jvt4p, 16 );
368     b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA8X8_LUMA",   h->param.cqm_8iy, x264_cqm_jvt8i, 64 );
369     b_error |= x264_cqm_parse_jmlist( h, buf, "INTER8X8_LUMA",   h->param.cqm_8py, x264_cqm_jvt8p, 64 );
370     if( CHROMA444 )
371     {
372         b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA8X8_CHROMA", h->param.cqm_8ic, x264_cqm_jvt8i, 64 );
373         b_error |= x264_cqm_parse_jmlist( h, buf, "INTER8X8_CHROMA", h->param.cqm_8pc, x264_cqm_jvt8p, 64 );
374     }
375
376     x264_free( buf );
377     return b_error;
378 }
379