1 |
/************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* mpeg-4 quantization matrix stuff |
* - Quantization matrix management code - |
5 |
* |
* |
6 |
* This program is an implementation of a part of one or more MPEG-4 |
* Copyright(C) 2002 Michael Militzer <isibaar@xvid.org> |
7 |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
* 2002 Peter Ross <pross@xvid.org> |
|
* to use this software module in hardware or software products are |
|
|
* advised that its use may infringe existing patents or copyrights, and |
|
|
* any such use would be at such party's own risk. The original |
|
|
* developer of this software module and his/her company, and subsequent |
|
|
* editors and their companies, will have no liability for use of this |
|
|
* software or modifications or derivatives thereof. |
|
8 |
* |
* |
9 |
* This program is free software; you can redistribute it and/or modify |
* 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 |
* it under the terms of the GNU General Public License as published by |
18 |
* |
* |
19 |
* You should have received a copy of the GNU General Public License |
* You should have received a copy of the GNU General Public License |
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., 675 Mass Ave, Cambridge, MA 02139, USA. |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
22 |
* |
* |
23 |
*************************************************************************/ |
* $Id$ |
|
|
|
|
/************************************************************************** |
|
|
* |
|
|
* History: |
|
|
* |
|
|
* 2.3.2002 inital version <suxen_drol@hotmail.com> |
|
24 |
* |
* |
25 |
*************************************************************************/ |
****************************************************************************/ |
26 |
|
|
27 |
#include "common.h" |
#include "quant_matrix.h" |
28 |
|
|
29 |
|
#define FIX(X) (((X)==1) ? 0xFFFF : ((1UL << 16) / (X) + 1)) |
30 |
|
#define FIXL(X) ((1UL << 16) / (X) - 1) |
31 |
|
|
32 |
static const int16_t default_intra_matrix[64] = { |
/***************************************************************************** |
33 |
|
* Default matrices |
34 |
|
****************************************************************************/ |
35 |
|
|
36 |
|
static const 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 |
static const int16_t default_inter_matrix[64] = { |
static const 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 |
|
const uint16_t * |
59 |
|
get_intra_matrix(const uint16_t * mpeg_quant_matrices) |
60 |
|
{ |
61 |
|
return(mpeg_quant_matrices + 0*64); |
62 |
|
} |
63 |
|
|
64 |
void quant4_intra_init(QMATRIX * qmatrix, int use_default) |
const uint16_t * |
65 |
|
get_inter_matrix(const uint16_t * mpeg_quant_matrices) |
66 |
{ |
{ |
67 |
if (use_default) |
return(mpeg_quant_matrices + 4*64); |
68 |
|
} |
69 |
|
|
70 |
|
const uint8_t * |
71 |
|
get_default_intra_matrix(void) |
72 |
{ |
{ |
73 |
memcpy(qmatrix->intra, default_intra_matrix, 64 * sizeof (int16_t)); |
return default_intra_matrix; |
74 |
} |
} |
75 |
|
|
76 |
#ifdef ARCH_X86 |
const uint8_t * |
77 |
// TODO: generate mmx tables |
get_default_inter_matrix(void) |
78 |
#endif |
{ |
79 |
|
return default_inter_matrix; |
80 |
|
} |
81 |
|
|
82 |
|
int |
83 |
|
is_custom_intra_matrix(const uint16_t * mpeg_quant_matrices) |
84 |
|
{ |
85 |
|
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 |
|
for (i = 0; i < 64; i++) { |
90 |
|
if(intra_matrix[i] != def_intra_matrix[i]) |
91 |
|
return 1; |
92 |
|
} |
93 |
|
return 0; |
94 |
} |
} |
95 |
|
|
96 |
|
int |
97 |
|
is_custom_inter_matrix(const uint16_t * mpeg_quant_matrices) |
98 |
|
{ |
99 |
|
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 |
void quant4_inter_init(QMATRIX * qmatrix, int use_default) |
void |
111 |
|
set_intra_matrix(uint16_t * mpeg_quant_matrices, const uint8_t * matrix) |
112 |
{ |
{ |
113 |
if (use_default) |
int i; |
114 |
|
uint16_t *intra_matrix = mpeg_quant_matrices + 0*64; |
115 |
|
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++) { |
120 |
|
intra_matrix[i] = (!i) ? (uint16_t)8: (uint16_t)matrix[i]; |
121 |
|
intra_matrix1[i] = (intra_matrix[i]>>1); |
122 |
|
intra_matrix1[i] += ((intra_matrix[i] == 1) ? 1: 0); |
123 |
|
intra_matrix_fix[i] = FIX(intra_matrix[i]); |
124 |
|
intra_matrix_fixl[i] = FIXL(intra_matrix[i]); |
125 |
|
} |
126 |
|
} |
127 |
|
|
128 |
|
void |
129 |
|
set_inter_matrix(uint16_t * mpeg_quant_matrices, const uint8_t * matrix) |
130 |
{ |
{ |
131 |
memcpy(qmatrix->inter, default_inter_matrix, 64 * sizeof (int16_t)); |
int i; |
132 |
|
uint16_t *inter_matrix = mpeg_quant_matrices + 4*64; |
133 |
|
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++) { |
138 |
|
inter_matrix1[i] = ((inter_matrix[i] = (int16_t) matrix[i])>>1); |
139 |
|
inter_matrix1[i] += ((inter_matrix[i] == 1) ? 1: 0); |
140 |
|
inter_matrix_fix[i] = FIX(inter_matrix[i]); |
141 |
|
inter_matrix_fixl[i] = FIXL(inter_matrix[i]); |
142 |
} |
} |
143 |
|
} |
144 |
|
|
145 |
|
void |
146 |
|
init_mpeg_matrix(uint16_t * mpeg_quant_matrices) { |
147 |
|
|
148 |
#ifdef ARCH_X86 |
set_intra_matrix(mpeg_quant_matrices, default_intra_matrix); |
149 |
// TODO: generate mmx tables |
set_inter_matrix(mpeg_quant_matrices, default_inter_matrix); |
|
#endif |
|
150 |
} |
} |