]> git.sesse.net Git - x264/blob - common/set.c
change default direct mode to spatial
[x264] / common / set.c
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 #include <stdio.h>
25 #include <string.h>
26
27 static const int dequant4_scale[6][3] =
28 {
29     { 10, 13, 16 },
30     { 11, 14, 18 },
31     { 13, 16, 20 },
32     { 14, 18, 23 },
33     { 16, 20, 25 },
34     { 18, 23, 29 }
35 };
36 static const int quant4_scale[6][3] =
37 {
38     { 13107, 8066, 5243 },
39     { 11916, 7490, 4660 },
40     { 10082, 6554, 4194 },
41     {  9362, 5825, 3647 },
42     {  8192, 5243, 3355 },
43     {  7282, 4559, 2893 },
44 };
45
46 static const int quant8_scan[16] =
47 {
48     0,3,4,3, 3,1,5,1, 4,5,2,5, 3,1,5,1
49 };
50 static const int dequant8_scale[6][6] =
51 {
52     { 20, 18, 32, 19, 25, 24 },
53     { 22, 19, 35, 21, 28, 26 },
54     { 26, 23, 42, 24, 33, 31 },
55     { 28, 25, 45, 26, 35, 33 },
56     { 32, 28, 51, 30, 40, 38 },
57     { 36, 32, 58, 34, 46, 43 },
58 };
59 static const int quant8_scale[6][6] =
60 {
61     { 13107, 11428, 20972, 12222, 16777, 15481 },
62     { 11916, 10826, 19174, 11058, 14980, 14290 },
63     { 10082,  8943, 15978,  9675, 12710, 11985 },
64     {  9362,  8228, 14913,  8931, 11984, 11259 },
65     {  8192,  7346, 13159,  7740, 10486,  9777 },
66     {  7282,  6428, 11570,  6830,  9118,  8640 }
67 };
68
69 void x264_cqm_init( x264_t *h )
70 {
71     int def_quant4[6][16];
72     int def_quant8[6][64];
73     int def_dequant4[6][16];
74     int def_dequant8[6][64];
75     int q, i, i_list;
76
77     for( q = 0; q < 6; q++ )
78     {
79         for( i = 0; i < 16; i++ )
80         {
81             int j = (i&1) + ((i>>2)&1);
82             def_dequant4[q][i] = dequant4_scale[q][j];
83             def_quant4[q][i]   =   quant4_scale[q][j];
84         }
85         for( i = 0; i < 64; i++ )
86         {
87             int j = quant8_scan[((i>>1)&12) | (i&3)];
88             def_dequant8[q][i] = dequant8_scale[q][j];
89             def_quant8[q][i]   =   quant8_scale[q][j];
90         }
91     }
92
93     for( q = 0; q < 6; q++ )
94     {
95         for( i_list = 0; i_list < 4; i_list++ )
96             for( i = 0; i < 16; i++ )
97             {
98                 h->dequant4_mf[i_list][q][0][i] = def_dequant4[q][i] * h->pps->scaling_list[i_list][i];
99                 h->  quant4_mf[i_list][q][0][i] = def_quant4[q][i] * 16 / h->pps->scaling_list[i_list][i];
100             }
101         for( i_list = 0; i_list < 2; i_list++ )
102             for( i = 0; i < 64; i++ )
103             {
104                 h->dequant8_mf[i_list][q][0][i] = def_dequant8[q][i] * h->pps->scaling_list[4+i_list][i];
105                 h->  quant8_mf[i_list][q][0][i] = def_quant8[q][i] * 16 / h->pps->scaling_list[4+i_list][i];
106             }
107     }
108     for( q = 0; q < 52; q++ )
109     {
110         for( i_list = 0; i_list < 4; i_list++ )
111             for( i = 0; i < 16; i++ )
112                 h->unquant4_mf[i_list][q][i] = (1 << (q/6 + 15 + 8)) / h->quant4_mf[i_list][q%6][0][i];
113         for( i_list = 0; i_list < 2; i_list++ )
114             for( i = 0; i < 64; i++ )
115                 h->unquant8_mf[i_list][q][i] = (1 << (q/6 + 16 + 8)) / h->quant8_mf[i_list][q%6][0][i];
116     }
117 }
118
119 int x264_cqm_parse_jmlist( x264_t *h, const char *buf, const char *name,
120                            uint8_t *cqm, const uint8_t *jvt, int length )
121 {
122     char *p;
123     char *nextvar;
124     int i;
125
126     p = strstr( buf, name );
127     if( !p )
128     {
129         memset( cqm, 16, length );
130         return 0;
131     }
132
133     p += strlen( name );
134     if( *p == 'U' || *p == 'V' )
135         p++;
136
137     nextvar = strstr( p, "INT" );
138
139     for( i = 0; i < length && (p = strpbrk( p, " \t\n," )) && (p = strpbrk( p, "0123456789" )); i++ )
140     {
141         int coef = -1;
142         sscanf( p, "%d", &coef );
143         if( i == 0 && coef == 0 )
144         {
145             memcpy( cqm, jvt, length );
146             return 0;
147         }
148         if( coef < 1 || coef > 255 )
149         {
150             x264_log( h, X264_LOG_ERROR, "bad coefficient in list '%s'\n", name );
151             return -1;
152         }
153         cqm[i] = coef;
154     }
155
156     if( (nextvar && p > nextvar) || i != length )
157     {
158         x264_log( h, X264_LOG_ERROR, "not enough coefficients in list '%s'\n", name );
159         return -1;
160     }
161
162     return 0;
163 }
164
165 int x264_cqm_parse_file( x264_t *h, const char *filename )
166 {
167     char *buf, *p;
168     int b_error = 0;
169
170     h->param.i_cqm_preset = X264_CQM_CUSTOM;
171     
172     buf = x264_slurp_file( filename );
173     if( !buf )
174     {
175         x264_log( h, X264_LOG_ERROR, "can't open file '%s'\n", filename );
176         return -1;
177     }
178
179     while( (p = strchr( buf, '#' )) != NULL )
180         memset( p, ' ', strcspn( p, "\n" ) );
181
182     b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA4X4_LUMA",   h->param.cqm_4iy, x264_cqm_jvt4i, 16 );
183     b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA4X4_CHROMA", h->param.cqm_4ic, x264_cqm_jvt4i, 16 );
184     b_error |= x264_cqm_parse_jmlist( h, buf, "INTER4X4_LUMA",   h->param.cqm_4py, x264_cqm_jvt4p, 16 );
185     b_error |= x264_cqm_parse_jmlist( h, buf, "INTER4X4_CHROMA", h->param.cqm_4pc, x264_cqm_jvt4p, 16 );
186     b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA8X8_LUMA",   h->param.cqm_8iy, x264_cqm_jvt8i, 64 );
187     b_error |= x264_cqm_parse_jmlist( h, buf, "INTER8X8_LUMA",   h->param.cqm_8py, x264_cqm_jvt8p, 64 );
188
189     x264_free( buf );
190     return b_error;
191 }
192