]> git.sesse.net Git - x264/blob - common/set.c
move quant_mf[] from x264_t to the heap, and merge duplicate entries
[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, j, i_list;
76
77     for( i = 0; i < 6; i++ )
78     {
79         int size = i<4 ? 16 : 64;
80         for( j = (i<4 ? 0 : 4); j < i; j++ )
81             if( !memcmp( h->pps->scaling_list[i], h->pps->scaling_list[j], size*sizeof(uint8_t) ) )
82                 break;
83         if( j < i )
84         {
85             h->  quant4_mf[i] = h->  quant4_mf[j];
86             h->dequant4_mf[i] = h->dequant4_mf[j];
87             h->unquant4_mf[i] = h->unquant4_mf[j];
88         }
89         else
90         {
91             h->  quant4_mf[i] = x264_malloc( 6*size*sizeof(int) );
92             h->dequant4_mf[i] = x264_malloc( 6*size*sizeof(int) );
93             h->unquant4_mf[i] = x264_malloc(52*size*sizeof(int) );
94         }
95     }
96
97     for( q = 0; q < 6; q++ )
98     {
99         for( i = 0; i < 16; i++ )
100         {
101             int j = (i&1) + ((i>>2)&1);
102             def_dequant4[q][i] = dequant4_scale[q][j];
103             def_quant4[q][i]   =   quant4_scale[q][j];
104         }
105         for( i = 0; i < 64; i++ )
106         {
107             int j = quant8_scan[((i>>1)&12) | (i&3)];
108             def_dequant8[q][i] = dequant8_scale[q][j];
109             def_quant8[q][i]   =   quant8_scale[q][j];
110         }
111     }
112
113     for( q = 0; q < 6; q++ )
114     {
115         for( i_list = 0; i_list < 4; i_list++ )
116             for( i = 0; i < 16; i++ )
117             {
118                 h->dequant4_mf[i_list][q][0][i] = def_dequant4[q][i] * h->pps->scaling_list[i_list][i];
119                 h->  quant4_mf[i_list][q][0][i] = def_quant4[q][i] * 16 / h->pps->scaling_list[i_list][i];
120             }
121         for( i_list = 0; i_list < 2; i_list++ )
122             for( i = 0; i < 64; i++ )
123             {
124                 h->dequant8_mf[i_list][q][0][i] = def_dequant8[q][i] * h->pps->scaling_list[4+i_list][i];
125                 h->  quant8_mf[i_list][q][0][i] = def_quant8[q][i] * 16 / h->pps->scaling_list[4+i_list][i];
126             }
127     }
128     for( q = 0; q < 52; q++ )
129     {
130         for( i_list = 0; i_list < 4; i_list++ )
131             for( i = 0; i < 16; i++ )
132                 h->unquant4_mf[i_list][q][i] = (1 << (q/6 + 15 + 8)) / h->quant4_mf[i_list][q%6][0][i];
133         for( i_list = 0; i_list < 2; i_list++ )
134             for( i = 0; i < 64; i++ )
135                 h->unquant8_mf[i_list][q][i] = (1 << (q/6 + 16 + 8)) / h->quant8_mf[i_list][q%6][0][i];
136     }
137 }
138
139 void x264_cqm_delete( x264_t *h )
140 {
141     int i, j;
142     for( i = 0; i < 6; i++ )
143     {
144         for( j = 0; j < i; j++ )
145             if( h->quant4_mf[i] == h->quant4_mf[j] )
146                 break;
147         if( j == i )
148         {
149             x264_free( h->  quant4_mf[i] );
150             x264_free( h->dequant4_mf[i] );
151             x264_free( h->unquant4_mf[i] );
152         }
153     }
154 }
155
156 int x264_cqm_parse_jmlist( x264_t *h, const char *buf, const char *name,
157                            uint8_t *cqm, const uint8_t *jvt, int length )
158 {
159     char *p;
160     char *nextvar;
161     int i;
162
163     p = strstr( buf, name );
164     if( !p )
165     {
166         memset( cqm, 16, length );
167         return 0;
168     }
169
170     p += strlen( name );
171     if( *p == 'U' || *p == 'V' )
172         p++;
173
174     nextvar = strstr( p, "INT" );
175
176     for( i = 0; i < length && (p = strpbrk( p, " \t\n," )) && (p = strpbrk( p, "0123456789" )); i++ )
177     {
178         int coef = -1;
179         sscanf( p, "%d", &coef );
180         if( i == 0 && coef == 0 )
181         {
182             memcpy( cqm, jvt, length );
183             return 0;
184         }
185         if( coef < 1 || coef > 255 )
186         {
187             x264_log( h, X264_LOG_ERROR, "bad coefficient in list '%s'\n", name );
188             return -1;
189         }
190         cqm[i] = coef;
191     }
192
193     if( (nextvar && p > nextvar) || i != length )
194     {
195         x264_log( h, X264_LOG_ERROR, "not enough coefficients in list '%s'\n", name );
196         return -1;
197     }
198
199     return 0;
200 }
201
202 int x264_cqm_parse_file( x264_t *h, const char *filename )
203 {
204     char *buf, *p;
205     int b_error = 0;
206
207     h->param.i_cqm_preset = X264_CQM_CUSTOM;
208     
209     buf = x264_slurp_file( filename );
210     if( !buf )
211     {
212         x264_log( h, X264_LOG_ERROR, "can't open file '%s'\n", filename );
213         return -1;
214     }
215
216     while( (p = strchr( buf, '#' )) != NULL )
217         memset( p, ' ', strcspn( p, "\n" ) );
218
219     b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA4X4_LUMA",   h->param.cqm_4iy, x264_cqm_jvt4i, 16 );
220     b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA4X4_CHROMA", h->param.cqm_4ic, x264_cqm_jvt4i, 16 );
221     b_error |= x264_cqm_parse_jmlist( h, buf, "INTER4X4_LUMA",   h->param.cqm_4py, x264_cqm_jvt4p, 16 );
222     b_error |= x264_cqm_parse_jmlist( h, buf, "INTER4X4_CHROMA", h->param.cqm_4pc, x264_cqm_jvt4p, 16 );
223     b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA8X8_LUMA",   h->param.cqm_8iy, x264_cqm_jvt8i, 64 );
224     b_error |= x264_cqm_parse_jmlist( h, buf, "INTER8X8_LUMA",   h->param.cqm_8py, x264_cqm_jvt8p, 64 );
225
226     x264_free( buf );
227     return b_error;
228 }
229