1 |
/***************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Custom matrix quantization functions - |
* - Quantization matrix management code - |
5 |
* |
* |
6 |
* Copyright(C) 2002 Michael Militzer <isibaar@xvid.org> |
* Copyright(C) 2002 Michael Militzer <isibaar@xvid.org> |
7 |
* 2002 Peter Ross <pross@xvid.org> |
* 2002 Peter Ross <pross@xvid.org> |
8 |
* |
* |
9 |
* This file is part of XviD, a free MPEG-4 video encoder/decoder |
* This program is free software ; you can redistribute it and/or modify |
10 |
* |
* it under the terms of the GNU General Public License as published by |
|
* XviD is free software; you can redistribute it and/or modify it |
|
|
* under the terms of the GNU General Public License as published by |
|
11 |
* the Free Software Foundation; either version 2 of the License, or |
* the Free Software Foundation; either version 2 of the License, or |
12 |
* (at your option) any later version. |
* (at your option) any later version. |
13 |
* |
* |
20 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
22 |
* |
* |
|
* Under section 8 of the GNU General Public License, the copyright |
|
|
* holders of XVID explicitly forbid distribution in the following |
|
|
* countries: |
|
|
* |
|
|
* - Japan |
|
|
* - United States of America |
|
|
* |
|
|
* Linking XviD statically or dynamically with other modules is making a |
|
|
* combined work based on XviD. Thus, the terms and conditions of the |
|
|
* GNU General Public License cover the whole combination. |
|
|
* |
|
|
* As a special exception, the copyright holders of XviD give you |
|
|
* permission to link XviD with independent modules that communicate with |
|
|
* XviD solely through the VFW1.1 and DShow interfaces, regardless of the |
|
|
* license terms of these independent modules, and to copy and distribute |
|
|
* the resulting combined work under terms of your choice, provided that |
|
|
* every copy of the combined work is accompanied by a complete copy of |
|
|
* the source code of XviD (the version of XviD used to produce the |
|
|
* combined work), being distributed under the terms of the GNU General |
|
|
* Public License plus this exception. An independent module is a module |
|
|
* which is not derived from or based on XviD. |
|
|
* |
|
|
* Note that people who make modified versions of XviD are not obligated |
|
|
* to grant this special exception for their modified versions; it is |
|
|
* their choice whether to do so. The GNU General Public License gives |
|
|
* permission to release a modified version without this exception; this |
|
|
* exception also makes it possible to release a modified version which |
|
|
* carries forward this exception. |
|
|
* |
|
23 |
* $Id$ |
* $Id$ |
24 |
* |
* |
25 |
****************************************************************************/ |
****************************************************************************/ |
26 |
|
|
27 |
#include "quant_matrix.h" |
#include "quant_matrix.h" |
28 |
|
|
29 |
#define FIX(X) (1 << 16) / (X) + 1 |
#define FIX(X) (((X)==1) ? 0xFFFF : ((1UL << 16) / (X) + 1)) |
30 |
|
#define FIXL(X) ((1UL << 16) / (X) - 1) |
31 |
|
|
32 |
/***************************************************************************** |
/***************************************************************************** |
33 |
* Local data |
* Default matrices |
34 |
****************************************************************************/ |
****************************************************************************/ |
35 |
|
|
36 |
/* ToDo : remove all this local to make this module thread safe */ |
static const uint8_t default_intra_matrix[64] = { |
|
uint8_t custom_intra_matrix = 0; |
|
|
uint8_t custom_inter_matrix = 0; |
|
|
|
|
|
uint8_t default_intra_matrix[64] = { |
|
37 |
8, 17, 18, 19, 21, 23, 25, 27, |
8, 17, 18, 19, 21, 23, 25, 27, |
38 |
17, 18, 19, 21, 23, 25, 27, 28, |
17, 18, 19, 21, 23, 25, 27, 28, |
39 |
20, 21, 22, 23, 24, 26, 28, 30, |
20, 21, 22, 23, 24, 26, 28, 30, |
44 |
27, 28, 30, 32, 35, 38, 41, 45 |
27, 28, 30, 32, 35, 38, 41, 45 |
45 |
}; |
}; |
46 |
|
|
47 |
int16_t intra_matrix[64] = { |
static const uint8_t default_inter_matrix[64] = { |
|
8, 17, 18, 19, 21, 23, 25, 27, |
|
|
17, 18, 19, 21, 23, 25, 27, 28, |
|
|
20, 21, 22, 23, 24, 26, 28, 30, |
|
|
21, 22, 23, 24, 26, 28, 30, 32, |
|
|
22, 23, 24, 26, 28, 30, 32, 35, |
|
|
23, 24, 26, 28, 30, 32, 35, 38, |
|
|
25, 26, 28, 30, 32, 35, 38, 41, |
|
|
27, 28, 30, 32, 35, 38, 41, 45 |
|
|
}; |
|
|
|
|
|
int16_t intra_matrix_fix[64] = { |
|
|
FIX(8), FIX(17), FIX(18), FIX(19), FIX(21), FIX(23), FIX(25), FIX(27), |
|
|
FIX(17), FIX(18), FIX(19), FIX(21), FIX(23), FIX(25), FIX(27), FIX(28), |
|
|
FIX(20), FIX(21), FIX(22), FIX(23), FIX(24), FIX(26), FIX(28), FIX(30), |
|
|
FIX(21), FIX(22), FIX(23), FIX(24), FIX(26), FIX(28), FIX(30), FIX(32), |
|
|
FIX(22), FIX(23), FIX(24), FIX(26), FIX(28), FIX(30), FIX(32), FIX(35), |
|
|
FIX(23), FIX(24), FIX(26), FIX(28), FIX(30), FIX(32), FIX(35), FIX(38), |
|
|
FIX(25), FIX(26), FIX(28), FIX(30), FIX(32), FIX(35), FIX(38), FIX(41), |
|
|
FIX(27), FIX(28), FIX(30), FIX(32), FIX(35), FIX(38), FIX(41), FIX(45) |
|
|
}; |
|
|
|
|
|
uint8_t default_inter_matrix[64] = { |
|
48 |
16, 17, 18, 19, 20, 21, 22, 23, |
16, 17, 18, 19, 20, 21, 22, 23, |
49 |
17, 18, 19, 20, 21, 22, 23, 24, |
17, 18, 19, 20, 21, 22, 23, 24, |
50 |
18, 19, 20, 21, 22, 23, 24, 25, |
18, 19, 20, 21, 22, 23, 24, 25, |
55 |
23, 24, 25, 27, 28, 30, 31, 33 |
23, 24, 25, 27, 28, 30, 31, 33 |
56 |
}; |
}; |
57 |
|
|
58 |
int16_t inter_matrix[64] = { |
const uint16_t * |
59 |
16, 17, 18, 19, 20, 21, 22, 23, |
get_intra_matrix(const uint16_t * mpeg_quant_matrices) |
|
17, 18, 19, 20, 21, 22, 23, 24, |
|
|
18, 19, 20, 21, 22, 23, 24, 25, |
|
|
19, 20, 21, 22, 23, 24, 26, 27, |
|
|
20, 21, 22, 23, 25, 26, 27, 28, |
|
|
21, 22, 23, 24, 26, 27, 28, 30, |
|
|
22, 23, 24, 26, 27, 28, 30, 31, |
|
|
23, 24, 25, 27, 28, 30, 31, 33 |
|
|
}; |
|
|
|
|
|
int16_t inter_matrix_fix[64] = { |
|
|
FIX(16), FIX(17), FIX(18), FIX(19), FIX(20), FIX(21), FIX(22), FIX(23), |
|
|
FIX(17), FIX(18), FIX(19), FIX(20), FIX(21), FIX(22), FIX(23), FIX(24), |
|
|
FIX(18), FIX(19), FIX(20), FIX(21), FIX(22), FIX(23), FIX(24), FIX(25), |
|
|
FIX(19), FIX(20), FIX(21), FIX(22), FIX(23), FIX(24), FIX(26), FIX(27), |
|
|
FIX(20), FIX(21), FIX(22), FIX(23), FIX(25), FIX(26), FIX(27), FIX(28), |
|
|
FIX(21), FIX(22), FIX(23), FIX(24), FIX(26), FIX(27), FIX(28), FIX(30), |
|
|
FIX(22), FIX(23), FIX(24), FIX(26), FIX(27), FIX(28), FIX(30), FIX(31), |
|
|
FIX(23), FIX(24), FIX(25), FIX(27), FIX(28), FIX(30), FIX(31), FIX(33) |
|
|
}; |
|
|
|
|
|
/***************************************************************************** |
|
|
* Functions |
|
|
****************************************************************************/ |
|
|
|
|
|
uint8_t |
|
|
get_intra_matrix_status(void) |
|
60 |
{ |
{ |
61 |
return custom_intra_matrix; |
return(mpeg_quant_matrices + 0*64); |
62 |
} |
} |
63 |
|
|
64 |
uint8_t |
const uint16_t * |
65 |
get_inter_matrix_status(void) |
get_inter_matrix(const uint16_t * mpeg_quant_matrices) |
66 |
{ |
{ |
67 |
return custom_inter_matrix; |
return(mpeg_quant_matrices + 4*64); |
68 |
} |
} |
69 |
|
|
70 |
void |
const uint8_t * |
71 |
set_intra_matrix_status(uint8_t status) |
get_default_intra_matrix(void) |
72 |
{ |
{ |
73 |
custom_intra_matrix = status; |
return default_intra_matrix; |
74 |
} |
} |
75 |
|
|
76 |
void |
const uint8_t * |
77 |
set_inter_matrix_status(uint8_t status) |
get_default_inter_matrix(void) |
78 |
{ |
{ |
79 |
custom_inter_matrix = status; |
return default_inter_matrix; |
80 |
} |
} |
81 |
|
|
82 |
int16_t * |
int |
83 |
get_intra_matrix(void) |
is_custom_intra_matrix(const uint16_t * mpeg_quant_matrices) |
84 |
{ |
{ |
85 |
return intra_matrix; |
int i; |
86 |
} |
const uint16_t *intra_matrix = get_intra_matrix(mpeg_quant_matrices); |
87 |
|
const uint8_t *def_intra_matrix = get_default_intra_matrix(); |
88 |
|
|
89 |
int16_t * |
for (i = 0; i < 64; i++) { |
90 |
get_inter_matrix(void) |
if(intra_matrix[i] != def_intra_matrix[i]) |
91 |
{ |
return 1; |
|
return inter_matrix; |
|
92 |
} |
} |
93 |
|
return 0; |
|
uint8_t * |
|
|
get_default_intra_matrix(void) |
|
|
{ |
|
|
return default_intra_matrix; |
|
94 |
} |
} |
95 |
|
|
96 |
uint8_t * |
int |
97 |
get_default_inter_matrix(void) |
is_custom_inter_matrix(const uint16_t * mpeg_quant_matrices) |
98 |
{ |
{ |
99 |
return default_inter_matrix; |
int i; |
100 |
|
const uint16_t *inter_matrix = get_inter_matrix(mpeg_quant_matrices); |
101 |
|
const uint8_t *def_inter_matrix = get_default_inter_matrix(); |
102 |
|
|
103 |
|
for (i = 0; i < 64; i++) { |
104 |
|
if(inter_matrix[i] != (uint16_t)def_inter_matrix[i]) |
105 |
|
return 1; |
106 |
|
} |
107 |
|
return 0; |
108 |
} |
} |
109 |
|
|
110 |
uint8_t |
void |
111 |
set_intra_matrix(uint8_t * matrix) |
set_intra_matrix(uint16_t * mpeg_quant_matrices, const uint8_t * matrix) |
112 |
{ |
{ |
113 |
int i, change = 0; |
int i; |
114 |
|
uint16_t *intra_matrix = mpeg_quant_matrices + 0*64; |
115 |
custom_intra_matrix = 0; |
uint16_t *intra_matrix1 = mpeg_quant_matrices + 1*64; |
116 |
|
uint16_t *intra_matrix_fix = mpeg_quant_matrices + 2*64; |
117 |
|
uint16_t *intra_matrix_fixl = mpeg_quant_matrices + 3*64; |
118 |
|
|
119 |
for (i = 0; i < 64; i++) { |
for (i = 0; i < 64; i++) { |
120 |
if ((int16_t) default_intra_matrix[i] != matrix[i]) |
intra_matrix[i] = (!i) ? (uint16_t)8: (uint16_t)matrix[i]; |
121 |
custom_intra_matrix = 1; |
intra_matrix1[i] = (intra_matrix[i]>>1); |
122 |
if (intra_matrix[i] != matrix[i]) |
intra_matrix1[i] += ((intra_matrix[i] == 1) ? 1: 0); |
|
change = 1; |
|
|
|
|
|
intra_matrix[i] = (int16_t) matrix[i]; |
|
123 |
intra_matrix_fix[i] = FIX(intra_matrix[i]); |
intra_matrix_fix[i] = FIX(intra_matrix[i]); |
124 |
|
intra_matrix_fixl[i] = FIXL(intra_matrix[i]); |
125 |
} |
} |
|
return /*custom_intra_matrix |*/ change; |
|
126 |
} |
} |
127 |
|
|
128 |
|
void |
129 |
uint8_t |
set_inter_matrix(uint16_t * mpeg_quant_matrices, const uint8_t * matrix) |
|
set_inter_matrix(uint8_t * matrix) |
|
130 |
{ |
{ |
131 |
int i, change = 0; |
int i; |
132 |
|
uint16_t *inter_matrix = mpeg_quant_matrices + 4*64; |
133 |
custom_inter_matrix = 0; |
uint16_t *inter_matrix1 = mpeg_quant_matrices + 5*64; |
134 |
|
uint16_t *inter_matrix_fix = mpeg_quant_matrices + 6*64; |
135 |
|
uint16_t *inter_matrix_fixl = mpeg_quant_matrices + 7*64; |
136 |
|
|
137 |
for (i = 0; i < 64; i++) { |
for (i = 0; i < 64; i++) { |
138 |
if ((int16_t) default_inter_matrix[i] != matrix[i]) |
inter_matrix1[i] = ((inter_matrix[i] = (int16_t) matrix[i])>>1); |
139 |
custom_inter_matrix = 1; |
inter_matrix1[i] += ((inter_matrix[i] == 1) ? 1: 0); |
|
if (inter_matrix[i] != matrix[i]) |
|
|
change = 1; |
|
|
|
|
|
inter_matrix[i] = (int16_t) matrix[i]; |
|
140 |
inter_matrix_fix[i] = FIX(inter_matrix[i]); |
inter_matrix_fix[i] = FIX(inter_matrix[i]); |
141 |
|
inter_matrix_fixl[i] = FIXL(inter_matrix[i]); |
142 |
|
} |
143 |
} |
} |
144 |
return /*custom_inter_matrix |*/ change; |
|
145 |
|
void |
146 |
|
init_mpeg_matrix(uint16_t * mpeg_quant_matrices) { |
147 |
|
|
148 |
|
set_intra_matrix(mpeg_quant_matrices, default_intra_matrix); |
149 |
|
set_inter_matrix(mpeg_quant_matrices, default_inter_matrix); |
150 |
} |
} |