]> git.sesse.net Git - x264/blob - common/set.c
custom quant matrices
[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
25 static const int dequant4_scale[6][3] =
26 {
27     { 10, 13, 16 },
28     { 11, 14, 18 },
29     { 13, 16, 20 },
30     { 14, 18, 23 },
31     { 16, 20, 25 },
32     { 18, 23, 29 }
33 };
34 static const int quant4_scale[6][3] =
35 {
36     { 13107, 8066, 5243 },
37     { 11916, 7490, 4660 },
38     { 10082, 6554, 4194 },
39     {  9362, 5825, 3647 },
40     {  8192, 5243, 3355 },
41     {  7282, 4559, 2893 },
42 };
43
44 static const int quant8_scan[16] =
45 {
46     0,3,4,3, 3,1,5,1, 4,5,2,5, 3,1,5,1
47 };
48 static const int dequant8_scale[6][6] =
49 {
50     { 20, 18, 32, 19, 25, 24 },
51     { 22, 19, 35, 21, 28, 26 },
52     { 26, 23, 42, 24, 33, 31 },
53     { 28, 25, 45, 26, 35, 33 },
54     { 32, 28, 51, 30, 40, 38 },
55     { 36, 32, 58, 34, 46, 43 },
56 };
57 static const int quant8_scale[6][6] =
58 {
59     { 13107, 11428, 20972, 12222, 16777, 15481 },
60     { 11916, 10826, 19174, 11058, 14980, 14290 },
61     { 10082,  8943, 15978,  9675, 12710, 11985 },
62     {  9362,  8228, 14913,  8931, 11984, 11259 },
63     {  8192,  7346, 13159,  7740, 10486,  9777 },
64     {  7282,  6428, 11570,  6830,  9118,  8640 }
65 };
66
67 void x264_cqm_init( x264_t *h )
68 {
69     int def_quant4[6][16];
70     int def_quant8[6][64];
71     int def_dequant4[6][16];
72     int def_dequant8[6][64];
73     int q, i, i_list;
74
75     for( q = 0; q < 6; q++ )
76     {
77         for( i = 0; i < 16; i++ )
78         {
79             int j = (i&1) + ((i>>2)&1);
80             def_dequant4[q][i] = dequant4_scale[q][j];
81             def_quant4[q][i]   =   quant4_scale[q][j];
82         }
83         for( i = 0; i < 64; i++ )
84         {
85             int j = quant8_scan[((i>>1)&12) | (i&3)];
86             def_dequant8[q][i] = dequant8_scale[q][j];
87             def_quant8[q][i]   =   quant8_scale[q][j];
88         }
89     }
90
91     for( q = 0; q < 6; q++ )
92     {
93         for( i_list = 0; i_list < 4; i_list++ )
94             for( i = 0; i < 16; i++ )
95             {
96                 h->dequant4_mf[i_list][q][0][i] = def_dequant4[q][i] * h->pps->scaling_list[i_list][i];
97                 h->  quant4_mf[i_list][q][0][i] = def_quant4[q][i] * 16 / h->pps->scaling_list[i_list][i];
98             }
99         for( i_list = 0; i_list < 2; i_list++ )
100             for( i = 0; i < 64; i++ )
101             {
102                 h->dequant8_mf[i_list][q][0][i] = def_dequant8[q][i] * h->pps->scaling_list[4+i_list][i];
103                 h->  quant8_mf[i_list][q][0][i] = def_quant8[q][i] * 16 / h->pps->scaling_list[4+i_list][i];
104             }
105     }
106 }
107