1 /********************************************************************
3 * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2005 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
11 ********************************************************************
16 ********************************************************************/
20 #include "codec_internal.h"
21 #include "quant_lookup.h"
23 /* the *V1 tables are the originals used by the VP3 codec */
25 static const ogg_uint32_t QThreshTableV1
[Q_TABLE_SIZE
] = {
26 500, 450, 400, 370, 340, 310, 285, 265,
27 245, 225, 210, 195, 185, 180, 170, 160,
28 150, 145, 135, 130, 125, 115, 110, 107,
29 100, 96, 93, 89, 85, 82, 75, 74,
30 70, 68, 64, 60, 57, 56, 52, 50,
31 49, 45, 44, 43, 40, 38, 37, 35,
32 33, 32, 30, 29, 28, 25, 24, 22,
33 21, 19, 18, 17, 15, 13, 12, 10
36 static const Q_LIST_ENTRY DcScaleFactorTableV1
[ Q_TABLE_SIZE
] = {
37 220, 200, 190, 180, 170, 170, 160, 160,
38 150, 150, 140, 140, 130, 130, 120, 120,
39 110, 110, 100, 100, 90, 90, 90, 80,
40 80, 80, 70, 70, 70, 60, 60, 60,
41 60, 50, 50, 50, 50, 40, 40, 40,
42 40, 40, 30, 30, 30, 30, 30, 30,
43 30, 20, 20, 20, 20, 20, 20, 20,
44 20, 10, 10, 10, 10, 10, 10, 10
47 /* dbm -- defined some alternative tables to test header packing */
51 static const Q_LIST_ENTRY Y_coeffsV1
[64] =
53 8, 16, 16, 16, 20, 20, 20, 20,
54 16, 16, 16, 16, 20, 20, 20, 20,
55 16, 16, 16, 16, 22, 22, 22, 22,
56 16, 16, 16, 16, 22, 22, 22, 22,
57 20, 20, 22, 22, 24, 24, 24, 24,
58 20, 20, 22, 22, 24, 24, 24, 24,
59 20, 20, 22, 22, 24, 24, 24, 24,
60 20, 20, 22, 22, 24, 24, 24, 24
63 static const Q_LIST_ENTRY UV_coeffsV1
[64] =
64 { 17, 18, 24, 47, 99, 99, 99, 99,
65 18, 21, 26, 66, 99, 99, 99, 99,
66 24, 26, 56, 99, 99, 99, 99, 99,
67 47, 66, 99, 99, 99, 99, 99, 99,
68 99, 99, 99, 99, 99, 99, 99, 99,
69 99, 99, 99, 99, 99, 99, 99, 99,
70 99, 99, 99, 99, 99, 99, 99, 99,
71 99, 99, 99, 99, 99, 99, 99, 99
74 static const Q_LIST_ENTRY Inter_coeffsV1
[64] =
76 12, 16, 16, 16, 20, 20, 20, 20,
77 16, 16, 16, 16, 20, 20, 20, 20,
78 16, 16, 16, 16, 22, 22, 22, 22,
79 16, 16, 16, 16, 22, 22, 22, 22,
80 20, 20, 22, 22, 24, 24, 24, 24,
81 20, 20, 22, 22, 24, 24, 24, 24,
82 20, 20, 22, 22, 24, 24, 24, 24,
83 20, 20, 22, 22, 24, 24, 24, 24
86 #else /* these are the old VP3 values: */
88 static const Q_LIST_ENTRY Y_coeffsV1
[64] ={
89 16, 11, 10, 16, 24, 40, 51, 61,
90 12, 12, 14, 19, 26, 58, 60, 55,
91 14, 13, 16, 24, 40, 57, 69, 56,
92 14, 17, 22, 29, 51, 87, 80, 62,
93 18, 22, 37, 58, 68, 109, 103, 77,
94 24, 35, 55, 64, 81, 104, 113, 92,
95 49, 64, 78, 87, 103, 121, 120, 101,
96 72, 92, 95, 98, 112, 100, 103, 99
99 static const Q_LIST_ENTRY UV_coeffsV1
[64] ={
100 17, 18, 24, 47, 99, 99, 99, 99,
101 18, 21, 26, 66, 99, 99, 99, 99,
102 24, 26, 56, 99, 99, 99, 99, 99,
103 47, 66, 99, 99, 99, 99, 99, 99,
104 99, 99, 99, 99, 99, 99, 99, 99,
105 99, 99, 99, 99, 99, 99, 99, 99,
106 99, 99, 99, 99, 99, 99, 99, 99,
107 99, 99, 99, 99, 99, 99, 99, 99
110 static const Q_LIST_ENTRY Inter_coeffsV1
[64] ={
111 16, 16, 16, 20, 24, 28, 32, 40,
112 16, 16, 20, 24, 28, 32, 40, 48,
113 16, 20, 24, 28, 32, 40, 48, 64,
114 20, 24, 28, 32, 40, 48, 64, 64,
115 24, 28, 32, 40, 48, 64, 64, 64,
116 28, 32, 40, 48, 64, 64, 64, 96,
117 32, 40, 48, 64, 64, 64, 96, 128,
118 40, 48, 64, 64, 64, 96, 128, 128
123 /* New (6) quant matrices */
125 static const Q_LIST_ENTRY Y_coeffs
[64] ={
126 16, 11, 10, 16, 24, 40, 51, 61,
127 12, 12, 14, 19, 26, 58, 60, 55,
128 14, 13, 16, 24, 40, 57, 69, 56,
129 14, 17, 22, 29, 51, 87, 80, 62,
130 18, 22, 37, 58, 68, 109, 103, 77,
131 24, 35, 55, 64, 81, 104, 113, 92,
132 49, 64, 78, 87, 103, 121, 120, 101,
133 72, 92, 95, 98, 112, 100, 103, 99
137 static const Q_LIST_ENTRY U_coeffs
[64] ={
138 17, 18, 24, 47, 99, 99, 99, 99,
139 18, 21, 26, 66, 99, 99, 99, 99,
140 24, 26, 56, 99, 99, 99, 99, 99,
141 47, 66, 99, 99, 99, 99, 99, 99,
142 99, 99, 99, 99, 99, 99, 99, 99,
143 99, 99, 99, 99, 99, 99, 99, 99,
144 99, 99, 99, 99, 99, 99, 99, 99,
145 99, 99, 99, 99, 99, 99, 99, 99
148 static const Q_LIST_ENTRY V_coeffs
[64] ={
149 17, 18, 24, 47, 99, 99, 99, 99,
150 18, 21, 26, 66, 99, 99, 99, 99,
151 24, 26, 56, 99, 99, 99, 99, 99,
152 47, 66, 99, 99, 99, 99, 99, 99,
153 99, 99, 99, 99, 99, 99, 99, 99,
154 99, 99, 99, 99, 99, 99, 99, 99,
155 99, 99, 99, 99, 99, 99, 99, 99,
156 99, 99, 99, 99, 99, 99, 99, 99
160 static const Q_LIST_ENTRY Inter_Y_coeffs
[64] ={
161 16, 16, 16, 20, 24, 28, 32, 40,
162 16, 16, 20, 24, 28, 32, 40, 48,
163 16, 20, 24, 28, 32, 40, 48, 64,
164 20, 24, 28, 32, 40, 48, 64, 64,
165 24, 28, 32, 40, 48, 64, 64, 64,
166 28, 32, 40, 48, 64, 64, 64, 96,
167 32, 40, 48, 64, 64, 64, 96, 128,
168 40, 48, 64, 64, 64, 96, 128, 128
171 static const Q_LIST_ENTRY Inter_U_coeffs
[64] ={
172 17, 18, 24, 47, 99, 99, 99, 99,
173 18, 21, 26, 66, 99, 99, 99, 99,
174 24, 26, 56, 99, 99, 99, 99, 99,
175 47, 66, 99, 99, 99, 99, 99, 99,
176 99, 99, 99, 99, 99, 99, 99, 99,
177 99, 99, 99, 99, 99, 99, 99, 99,
178 99, 99, 99, 99, 99, 99, 99, 99,
179 99, 99, 99, 99, 99, 99, 99, 99
182 static const Q_LIST_ENTRY Inter_V_coeffs
[64] ={
183 17, 18, 24, 47, 99, 99, 99, 99,
184 18, 21, 26, 66, 99, 99, 99, 99,
185 24, 26, 56, 99, 99, 99, 99, 99,
186 47, 66, 99, 99, 99, 99, 99, 99,
187 99, 99, 99, 99, 99, 99, 99, 99,
188 99, 99, 99, 99, 99, 99, 99, 99,
189 99, 99, 99, 99, 99, 99, 99, 99,
190 99, 99, 99, 99, 99, 99, 99, 99
194 static int _ilog(unsigned int v
){
204 void WriteQTables(PB_INSTANCE
*pbi
,oggpack_buffer
* opb
) {
207 oggpackB_write(opb
, bits
-1, 4);
208 for(x
=0; x
<64; x
++) {
209 oggpackB_write(opb
, pbi
->QThreshTable
[x
],bits
);
211 oggpackB_write(opb
, bits
-1, 4);
212 for(x
=0; x
<64; x
++) {
213 oggpackB_write(opb
, pbi
->DcScaleFactorTable
[x
],bits
);
216 switch(pbi
->encoder_profile
) {
218 oggpackB_write(opb
, 6 - 1, 9); /* number of base matricies */
220 for(x
=0; x
<64; x
++) {
221 oggpackB_write(opb
, pbi
->Y_coeffs
[x
],8);
223 for(x
=0; x
<64; x
++) {
224 oggpackB_write(opb
, pbi
->U_coeffs
[x
],8);
226 for(x
=0; x
<64; x
++) {
227 oggpackB_write(opb
, pbi
->V_coeffs
[x
],8);
230 for(x
=0; x
<64; x
++) {
231 oggpackB_write(opb
, pbi
->InterY_coeffs
[x
],8);
233 for(x
=0; x
<64; x
++) {
234 oggpackB_write(opb
, pbi
->InterU_coeffs
[x
],8);
236 for(x
=0; x
<64; x
++) {
237 oggpackB_write(opb
, pbi
->InterV_coeffs
[x
],8);
240 oggpackB_write(opb
, 0, 3); /* matrix 0 for intra Y */
241 oggpackB_write(opb
, 62, 6); /* used for every q */
242 oggpackB_write(opb
, 0, 3);
244 oggpackB_write(opb
, 1, 1);
245 oggpackB_write(opb
, 1, 3); /* matrix 1 for intra U */
246 oggpackB_write(opb
, 62, 6); /* used for every q */
247 oggpackB_write(opb
, 0, 3);
249 oggpackB_write(opb
, 1, 1);
250 oggpackB_write(opb
, 2, 3); /* matrix 2 for intra V */
251 oggpackB_write(opb
, 62, 6); /* used for every q */
252 oggpackB_write(opb
, 0, 3);
254 oggpackB_write(opb
, 1, 1);
255 oggpackB_write(opb
, 3, 3); /* matrix 3 for inter Y */
256 oggpackB_write(opb
, 62, 6); /* used for every q */
257 oggpackB_write(opb
, 0, 3);
259 oggpackB_write(opb
, 1, 1);
260 oggpackB_write(opb
, 4, 3); /* matrix 4 for inter U */
261 oggpackB_write(opb
, 62, 6); /* used for every q */
262 oggpackB_write(opb
, 0, 3);
264 oggpackB_write(opb
, 1, 1);
265 oggpackB_write(opb
, 5, 3); /* matrix 5 for inter V */
266 oggpackB_write(opb
, 62, 6); /* used for every q */
267 oggpackB_write(opb
, 0, 3);
271 oggpackB_write(opb
, 3 - 1, 9); /* number of base matricies */
272 for(x
=0; x
<64; x
++) {
273 oggpackB_write(opb
, pbi
->Y_coeffs
[x
],8);
275 for(x
=0; x
<64; x
++) {
276 oggpackB_write(opb
, pbi
->U_coeffs
[x
],8);
278 for(x
=0; x
<64; x
++) {
279 oggpackB_write(opb
, pbi
->InterY_coeffs
[x
],8);
282 oggpackB_write(opb
, 0, 2); /* matrix 0 for intra Y */
283 oggpackB_write(opb
, 62, 6); /* used for every q */
284 oggpackB_write(opb
, 0, 2);
285 oggpackB_write(opb
, 1, 1); /* next range is explicit */
286 oggpackB_write(opb
, 1, 2); /* matrix 1 for intra U */
287 oggpackB_write(opb
, 62, 6);
288 oggpackB_write(opb
, 1, 2);
289 oggpackB_write(opb
, 0, 1); /* intra V is the same */
290 oggpackB_write(opb
, 1, 1); /* next range is explicit */
291 oggpackB_write(opb
, 2, 2); /* matrix 2 for inter Y */
292 oggpackB_write(opb
, 62, 6);
293 oggpackB_write(opb
, 2, 2);
294 oggpackB_write(opb
, 0, 2); /* inter U the same */
295 oggpackB_write(opb
, 0, 2); /* inter V the same */
301 static int _read_qtable_range(codec_setup_info
*ci
, oggpack_buffer
* opb
,
307 qmat_range_table table
[65];
309 theora_read(opb
,_ilog(N
-1),&index
); /* qi=0 index */
310 table
[count
].startqi
= 0;
311 table
[count
++].qmat
= ci
->qmats
+ index
* Q_TABLE_SIZE
;
313 theora_read(opb
,_ilog(62-qi
),&range
); /* range to next code q matrix */
315 if(range
<=0) return OC_BADHEADER
;
317 theora_read(opb
,_ilog(N
-1),&index
); /* next index */
318 table
[count
].startqi
= qi
;
319 table
[count
++].qmat
= ci
->qmats
+ index
* Q_TABLE_SIZE
;
322 ci
->range_table
[type
] = _ogg_malloc(count
* sizeof(qmat_range_table
));
323 if (ci
->range_table
[type
] != NULL
) {
324 memcpy(ci
->range_table
[type
], table
, count
* sizeof(qmat_range_table
));
328 return OC_FAULT
; /* allocation failed */
331 int ReadQTables(codec_setup_info
*ci
, oggpack_buffer
* opb
) {
336 theora_read(opb
,4,&bits
); bits
++;
337 for(x
=0; x
<Q_TABLE_SIZE
; x
++) {
338 theora_read(opb
,bits
,&value
);
339 if(bits
<0)return OC_BADHEADER
;
340 ci
->QThreshTable
[x
]=value
;
343 theora_read(opb
,4,&bits
); bits
++;
344 for(x
=0; x
<Q_TABLE_SIZE
; x
++) {
345 theora_read(opb
,bits
,&value
);
346 if(bits
<0)return OC_BADHEADER
;
347 ci
->DcScaleFactorTable
[x
]=(Q_LIST_ENTRY
)value
;
350 theora_read(opb
,9,&N
); N
++;
351 ci
->qmats
=_ogg_malloc(N
*64*sizeof(Q_LIST_ENTRY
));
352 ci
->MaxQMatrixIndex
= N
;
354 for(x
=0; x
<64; x
++) {
355 theora_read(opb
,8,&value
);
356 if(bits
<0)return OC_BADHEADER
;
357 ci
->qmats
[(y
<<6)+x
]=(Q_LIST_ENTRY
)value
;
362 ci
->range_table
[x
] = NULL
;
367 if((ret
=_read_qtable_range(ci
,opb
,N
,0))<0) return ret
;
369 theora_read(opb
,1,&flag
);
370 if(flag
<0) return OC_BADHEADER
;
372 /* explicitly coded */
373 if((ret
=_read_qtable_range(ci
,opb
,N
,1))<0) return ret
;
375 /* same as previous */
378 theora_read(opb
,1,&flag
);
379 if(flag
<0) return OC_BADHEADER
;
381 /* explicitly coded */
382 if((ret
=_read_qtable_range(ci
,opb
,N
,2))<0) return ret
;
384 /* same as previous */
387 theora_read(opb
,1,&flag
);
388 if(flag
<0) return OC_BADHEADER
;
390 /* explicitly coded */
391 if((ret
=_read_qtable_range(ci
,opb
,N
,3))<0) return ret
;
393 theora_read(opb
,1,&flag
);
394 if(flag
<0) return OC_BADHEADER
;
396 /* same as corresponding intra */
398 /* same as previous */
402 theora_read(opb
,1,&flag
);
403 if(flag
<0) return OC_BADHEADER
;
405 /* explicitly coded */
406 if((ret
=_read_qtable_range(ci
,opb
,N
,4))<0) return ret
;
408 theora_read(opb
,1,&flag
);
409 if(flag
<0) return OC_BADHEADER
;
411 /* same as corresponding intra */
413 /* same as previous */
417 theora_read(opb
,1,&flag
);
418 if(flag
<0) return OC_BADHEADER
;
420 /* explicitly coded */
421 if((ret
=_read_qtable_range(ci
,opb
,N
,5))<0) return ret
;
423 theora_read(opb
,1,&flag
);
424 if(flag
<0) return OC_BADHEADER
;
426 /* same as corresponding intra */
428 /* same as previous */
436 void CopyQTables(PB_INSTANCE
*pbi
, codec_setup_info
*ci
) {
439 memcpy(pbi
->QThreshTable
, ci
->QThreshTable
, sizeof(pbi
->QThreshTable
));
440 memcpy(pbi
->DcScaleFactorTable
, ci
->DcScaleFactorTable
,
441 sizeof(pbi
->DcScaleFactorTable
));
443 /* the decoder only supports 6 different base matricies; do the
444 best we can with the range table. We assume the first range
445 entry is good for all qi values. A NULL range table entry
446 indicates we fall back to the previous value. */
447 qmat
= ci
->range_table
[0]->qmat
;
448 memcpy(pbi
->Y_coeffs
, qmat
, sizeof(pbi
->Y_coeffs
));
449 if (ci
->range_table
[1]) qmat
= ci
->range_table
[1]->qmat
;
450 memcpy(pbi
->U_coeffs
, qmat
, sizeof(pbi
->U_coeffs
));
451 if (ci
->range_table
[2]) qmat
= ci
->range_table
[2]->qmat
;
452 memcpy(pbi
->V_coeffs
, qmat
, sizeof(pbi
->V_coeffs
));
453 if (ci
->range_table
[3]) qmat
= ci
->range_table
[3]->qmat
;
454 memcpy(pbi
->InterY_coeffs
, qmat
, sizeof(pbi
->InterY_coeffs
));
455 if (ci
->range_table
[4]) qmat
= ci
->range_table
[4]->qmat
;
456 memcpy(pbi
->InterU_coeffs
, qmat
, sizeof(pbi
->InterU_coeffs
));
457 if (ci
->range_table
[5]) qmat
= ci
->range_table
[5]->qmat
;
458 memcpy(pbi
->InterV_coeffs
, qmat
, sizeof(pbi
->InterV_coeffs
));
461 /* Initialize custom qtables using the VP31 values.
462 Someday we can change the quant tables to be adaptive, or just plain
464 void InitQTables( PB_INSTANCE
*pbi
){
465 switch(pbi
->encoder_profile
) {
467 memcpy(pbi
->QThreshTable
, QThreshTableV1
, sizeof(pbi
->QThreshTable
));
468 memcpy(pbi
->DcScaleFactorTable
, DcScaleFactorTableV1
, sizeof(pbi
->DcScaleFactorTable
));
469 memcpy(pbi
->Y_coeffs
, Y_coeffs
, sizeof(pbi
->Y_coeffs
));
470 memcpy(pbi
->U_coeffs
, U_coeffs
, sizeof(pbi
->U_coeffs
));
471 memcpy(pbi
->V_coeffs
, V_coeffs
, sizeof(pbi
->V_coeffs
));
472 memcpy(pbi
->InterY_coeffs
, Inter_Y_coeffs
, sizeof(pbi
->InterY_coeffs
));
473 memcpy(pbi
->InterU_coeffs
, Inter_U_coeffs
, sizeof(pbi
->InterU_coeffs
));
474 memcpy(pbi
->InterV_coeffs
, Inter_V_coeffs
, sizeof(pbi
->InterV_coeffs
));
477 memcpy(pbi
->QThreshTable
, QThreshTableV1
, sizeof(pbi
->QThreshTable
));
478 memcpy(pbi
->DcScaleFactorTable
, DcScaleFactorTableV1
, sizeof(pbi
->DcScaleFactorTable
));
479 memcpy(pbi
->Y_coeffs
, Y_coeffsV1
, sizeof(pbi
->Y_coeffs
));
480 memcpy(pbi
->U_coeffs
, UV_coeffsV1
, sizeof(pbi
->U_coeffs
));
481 memcpy(pbi
->V_coeffs
, UV_coeffsV1
, sizeof(pbi
->V_coeffs
));
482 memcpy(pbi
->InterY_coeffs
, Inter_coeffsV1
, sizeof(pbi
->InterY_coeffs
));
483 memcpy(pbi
->InterU_coeffs
, Inter_coeffsV1
, sizeof(pbi
->InterU_coeffs
));
484 memcpy(pbi
->InterV_coeffs
, Inter_coeffsV1
, sizeof(pbi
->InterV_coeffs
));
489 static void BuildZigZagIndex(PB_INSTANCE
*pbi
){
492 /* invert the row to zigzag coeffient order lookup table */
493 for ( i
= 0; i
< BLOCK_SIZE
; i
++ ){
494 j
= dezigzag_index
[i
];
495 pbi
->zigzag_index
[j
] = i
;
499 static void init_quantizer ( CP_INSTANCE
*cpi
,
500 ogg_uint32_t scale_factor
,
501 unsigned char QIndex
){
504 double RoundingFactor
;
506 double temp_fp_quant_coeffs
;
507 double temp_fp_quant_round
;
508 double temp_fp_ZeroBinSize
;
509 PB_INSTANCE
*pbi
= &cpi
->pb
;
511 const Q_LIST_ENTRY
* temp_Y_coeffs
;
512 const Q_LIST_ENTRY
* temp_U_coeffs
;
513 const Q_LIST_ENTRY
* temp_V_coeffs
;
514 const Q_LIST_ENTRY
* temp_Inter_Y_coeffs
;
515 const Q_LIST_ENTRY
* temp_Inter_U_coeffs
;
516 const Q_LIST_ENTRY
* temp_Inter_V_coeffs
;
517 const Q_LIST_ENTRY
* temp_DcScaleFactorTable
;
519 /* Notes on setup of quantisers. The initial multiplication by
520 the scale factor is done in the ogg_int32_t domain to insure that the
521 precision in the quantiser is the same as in the inverse
522 quantiser where all calculations are integer. The "<< 2" is a
523 normalisation factor for the forward DCT transform. */
525 /* New version rounding and ZB characteristics. */
526 temp_Y_coeffs
= cpi
->pb
.Y_coeffs
;
527 temp_U_coeffs
= cpi
->pb
.U_coeffs
;
528 temp_V_coeffs
= cpi
->pb
.V_coeffs
;
529 temp_Inter_Y_coeffs
= cpi
->pb
.InterY_coeffs
;
530 temp_Inter_U_coeffs
= cpi
->pb
.InterU_coeffs
;
531 temp_Inter_V_coeffs
= cpi
->pb
.InterV_coeffs
;
532 temp_DcScaleFactorTable
= cpi
->pb
.DcScaleFactorTable
;
535 switch(cpi
->pb
.info
.sharpness
){
538 if ( scale_factor
<= 50 )
539 RoundingFactor
= 0.499;
541 RoundingFactor
= 0.46;
545 if ( scale_factor
<= 50 )
546 RoundingFactor
= 0.476;
548 RoundingFactor
= 0.400;
553 if ( scale_factor
<= 50 )
554 RoundingFactor
= 0.476;
556 RoundingFactor
= 0.333;
560 /* Use fixed multiplier for intra Y DC */
561 temp_fp_quant_coeffs
=
562 (((ogg_uint32_t
)(temp_DcScaleFactorTable
[QIndex
] * temp_Y_coeffs
[0])/100) << 2);
563 if ( temp_fp_quant_coeffs
< MIN_LEGAL_QUANT_ENTRY
* 2 )
564 temp_fp_quant_coeffs
= MIN_LEGAL_QUANT_ENTRY
* 2;
566 temp_fp_quant_round
= temp_fp_quant_coeffs
* RoundingFactor
;
567 pbi
->fp_quant_Y_round
[0] = (ogg_int32_t
) (0.5 + temp_fp_quant_round
);
569 temp_fp_ZeroBinSize
= temp_fp_quant_coeffs
* ZBinFactor
;
570 pbi
->fp_ZeroBinSize_Y
[0] = (ogg_int32_t
) (0.5 + temp_fp_ZeroBinSize
);
572 temp_fp_quant_coeffs
= 1.0 / temp_fp_quant_coeffs
;
573 pbi
->fp_quant_Y_coeffs
[0] = (0.5 + SHIFT16
* temp_fp_quant_coeffs
);
576 temp_fp_quant_coeffs
=
577 (((ogg_uint32_t
)(temp_DcScaleFactorTable
[QIndex
] * temp_U_coeffs
[0])/100) << 2);
578 if ( temp_fp_quant_coeffs
< MIN_LEGAL_QUANT_ENTRY
* 2)
579 temp_fp_quant_coeffs
= MIN_LEGAL_QUANT_ENTRY
* 2;
581 temp_fp_quant_round
= temp_fp_quant_coeffs
* RoundingFactor
;
582 pbi
->fp_quant_U_round
[0] = (0.5 + temp_fp_quant_round
);
584 temp_fp_ZeroBinSize
= temp_fp_quant_coeffs
* ZBinFactor
;
585 pbi
->fp_ZeroBinSize_U
[0] = (0.5 + temp_fp_ZeroBinSize
);
587 temp_fp_quant_coeffs
= 1.0 / temp_fp_quant_coeffs
;
588 pbi
->fp_quant_U_coeffs
[0]= (0.5 + SHIFT16
* temp_fp_quant_coeffs
);
591 temp_fp_quant_coeffs
=
592 (((ogg_uint32_t
)(temp_DcScaleFactorTable
[QIndex
] * temp_V_coeffs
[0])/100) << 2);
593 if ( temp_fp_quant_coeffs
< MIN_LEGAL_QUANT_ENTRY
* 2)
594 temp_fp_quant_coeffs
= MIN_LEGAL_QUANT_ENTRY
* 2;
596 temp_fp_quant_round
= temp_fp_quant_coeffs
* RoundingFactor
;
597 pbi
->fp_quant_V_round
[0] = (0.5 + temp_fp_quant_round
);
599 temp_fp_ZeroBinSize
= temp_fp_quant_coeffs
* ZBinFactor
;
600 pbi
->fp_ZeroBinSize_V
[0] = (0.5 + temp_fp_ZeroBinSize
);
602 temp_fp_quant_coeffs
= 1.0 / temp_fp_quant_coeffs
;
603 pbi
->fp_quant_V_coeffs
[0]= (0.5 + SHIFT16
* temp_fp_quant_coeffs
);
607 temp_fp_quant_coeffs
=
608 (((ogg_uint32_t
)(temp_DcScaleFactorTable
[QIndex
] * temp_Inter_Y_coeffs
[0])/100) << 2);
609 if ( temp_fp_quant_coeffs
< MIN_LEGAL_QUANT_ENTRY
* 4)
610 temp_fp_quant_coeffs
= MIN_LEGAL_QUANT_ENTRY
* 4;
612 temp_fp_quant_round
= temp_fp_quant_coeffs
* RoundingFactor
;
613 pbi
->fp_quant_Inter_Y_round
[0]= (0.5 + temp_fp_quant_round
);
615 temp_fp_ZeroBinSize
= temp_fp_quant_coeffs
* ZBinFactor
;
616 pbi
->fp_ZeroBinSize_Inter_Y
[0]= (0.5 + temp_fp_ZeroBinSize
);
618 temp_fp_quant_coeffs
= 1.0 / temp_fp_quant_coeffs
;
619 pbi
->fp_quant_Inter_Y_coeffs
[0]= (0.5 + SHIFT16
* temp_fp_quant_coeffs
);
622 temp_fp_quant_coeffs
=
623 (((ogg_uint32_t
)(temp_DcScaleFactorTable
[QIndex
] * temp_Inter_U_coeffs
[0])/100) << 2);
624 if ( temp_fp_quant_coeffs
< MIN_LEGAL_QUANT_ENTRY
* 4)
625 temp_fp_quant_coeffs
= MIN_LEGAL_QUANT_ENTRY
* 4;
627 temp_fp_quant_round
= temp_fp_quant_coeffs
* RoundingFactor
;
628 pbi
->fp_quant_Inter_U_round
[0]= (0.5 + temp_fp_quant_round
);
630 temp_fp_ZeroBinSize
= temp_fp_quant_coeffs
* ZBinFactor
;
631 pbi
->fp_ZeroBinSize_Inter_U
[0]= (0.5 + temp_fp_ZeroBinSize
);
633 temp_fp_quant_coeffs
= 1.0 / temp_fp_quant_coeffs
;
634 pbi
->fp_quant_Inter_U_coeffs
[0]= (0.5 + SHIFT16
* temp_fp_quant_coeffs
);
637 temp_fp_quant_coeffs
=
638 (((ogg_uint32_t
)(temp_DcScaleFactorTable
[QIndex
] * temp_Inter_V_coeffs
[0])/100) << 2);
639 if ( temp_fp_quant_coeffs
< MIN_LEGAL_QUANT_ENTRY
* 4)
640 temp_fp_quant_coeffs
= MIN_LEGAL_QUANT_ENTRY
* 4;
642 temp_fp_quant_round
= temp_fp_quant_coeffs
* RoundingFactor
;
643 pbi
->fp_quant_Inter_V_round
[0]= (0.5 + temp_fp_quant_round
);
645 temp_fp_ZeroBinSize
= temp_fp_quant_coeffs
* ZBinFactor
;
646 pbi
->fp_ZeroBinSize_Inter_V
[0]= (0.5 + temp_fp_ZeroBinSize
);
648 temp_fp_quant_coeffs
= 1.0 / temp_fp_quant_coeffs
;
649 pbi
->fp_quant_Inter_V_coeffs
[0]= (0.5 + SHIFT16
* temp_fp_quant_coeffs
);
652 for ( i
= 1; i
< 64; i
++ ){
653 /* now scale coefficients by required compression factor */
655 temp_fp_quant_coeffs
=
656 (((ogg_uint32_t
)(scale_factor
* temp_Y_coeffs
[i
]) / 100 ) << 2 );
657 if ( temp_fp_quant_coeffs
< (MIN_LEGAL_QUANT_ENTRY
) )
658 temp_fp_quant_coeffs
= (MIN_LEGAL_QUANT_ENTRY
);
660 temp_fp_quant_round
= temp_fp_quant_coeffs
* RoundingFactor
;
661 pbi
->fp_quant_Y_round
[i
] = (0.5 + temp_fp_quant_round
);
663 temp_fp_ZeroBinSize
= temp_fp_quant_coeffs
* ZBinFactor
;
664 pbi
->fp_ZeroBinSize_Y
[i
] = (0.5 + temp_fp_ZeroBinSize
);
666 temp_fp_quant_coeffs
= 1.0 / temp_fp_quant_coeffs
;
667 pbi
->fp_quant_Y_coeffs
[i
] = (0.5 + SHIFT16
* temp_fp_quant_coeffs
);
670 temp_fp_quant_coeffs
=
671 (((ogg_uint32_t
)(scale_factor
* temp_U_coeffs
[i
]) / 100 ) << 2 );
672 if ( temp_fp_quant_coeffs
< (MIN_LEGAL_QUANT_ENTRY
))
673 temp_fp_quant_coeffs
= (MIN_LEGAL_QUANT_ENTRY
);
675 temp_fp_quant_round
= temp_fp_quant_coeffs
* RoundingFactor
;
676 pbi
->fp_quant_U_round
[i
] = (0.5 + temp_fp_quant_round
);
678 temp_fp_ZeroBinSize
= temp_fp_quant_coeffs
* ZBinFactor
;
679 pbi
->fp_ZeroBinSize_U
[i
] = (0.5 + temp_fp_ZeroBinSize
);
681 temp_fp_quant_coeffs
= 1.0 / temp_fp_quant_coeffs
;
682 pbi
->fp_quant_U_coeffs
[i
]= (0.5 + SHIFT16
* temp_fp_quant_coeffs
);
685 temp_fp_quant_coeffs
=
686 (((ogg_uint32_t
)(scale_factor
* temp_V_coeffs
[i
]) / 100 ) << 2 );
687 if ( temp_fp_quant_coeffs
< (MIN_LEGAL_QUANT_ENTRY
))
688 temp_fp_quant_coeffs
= (MIN_LEGAL_QUANT_ENTRY
);
690 temp_fp_quant_round
= temp_fp_quant_coeffs
* RoundingFactor
;
691 pbi
->fp_quant_V_round
[i
] = (0.5 + temp_fp_quant_round
);
693 temp_fp_ZeroBinSize
= temp_fp_quant_coeffs
* ZBinFactor
;
694 pbi
->fp_ZeroBinSize_V
[i
] = (0.5 + temp_fp_ZeroBinSize
);
696 temp_fp_quant_coeffs
= 1.0 / temp_fp_quant_coeffs
;
697 pbi
->fp_quant_V_coeffs
[i
]= (0.5 + SHIFT16
* temp_fp_quant_coeffs
);
700 temp_fp_quant_coeffs
=
701 (((ogg_uint32_t
)(scale_factor
* temp_Inter_Y_coeffs
[i
]) / 100 ) << 2 );
702 if ( temp_fp_quant_coeffs
< (MIN_LEGAL_QUANT_ENTRY
* 2) )
703 temp_fp_quant_coeffs
= (MIN_LEGAL_QUANT_ENTRY
* 2);
705 temp_fp_quant_round
= temp_fp_quant_coeffs
* RoundingFactor
;
706 pbi
->fp_quant_Inter_Y_round
[i
]= (0.5 + temp_fp_quant_round
);
708 temp_fp_ZeroBinSize
= temp_fp_quant_coeffs
* ZBinFactor
;
709 pbi
->fp_ZeroBinSize_Inter_Y
[i
]= (0.5 + temp_fp_ZeroBinSize
);
711 temp_fp_quant_coeffs
= 1.0 / temp_fp_quant_coeffs
;
712 pbi
->fp_quant_Inter_Y_coeffs
[i
]= (0.5 + SHIFT16
* temp_fp_quant_coeffs
);
715 temp_fp_quant_coeffs
=
716 (((ogg_uint32_t
)(scale_factor
* temp_Inter_U_coeffs
[i
]) / 100 ) << 2 );
717 if ( temp_fp_quant_coeffs
< (MIN_LEGAL_QUANT_ENTRY
* 2) )
718 temp_fp_quant_coeffs
= (MIN_LEGAL_QUANT_ENTRY
* 2);
720 temp_fp_quant_round
= temp_fp_quant_coeffs
* RoundingFactor
;
721 pbi
->fp_quant_Inter_U_round
[i
]= (0.5 + temp_fp_quant_round
);
723 temp_fp_ZeroBinSize
= temp_fp_quant_coeffs
* ZBinFactor
;
724 pbi
->fp_ZeroBinSize_Inter_U
[i
]= (0.5 + temp_fp_ZeroBinSize
);
726 temp_fp_quant_coeffs
= 1.0 / temp_fp_quant_coeffs
;
727 pbi
->fp_quant_Inter_U_coeffs
[i
]= (0.5 + SHIFT16
* temp_fp_quant_coeffs
);
730 temp_fp_quant_coeffs
=
731 (((ogg_uint32_t
)(scale_factor
* temp_Inter_V_coeffs
[i
]) / 100 ) << 2 );
732 if ( temp_fp_quant_coeffs
< (MIN_LEGAL_QUANT_ENTRY
* 2) )
733 temp_fp_quant_coeffs
= (MIN_LEGAL_QUANT_ENTRY
* 2);
735 temp_fp_quant_round
= temp_fp_quant_coeffs
* RoundingFactor
;
736 pbi
->fp_quant_Inter_V_round
[i
]= (0.5 + temp_fp_quant_round
);
738 temp_fp_ZeroBinSize
= temp_fp_quant_coeffs
* ZBinFactor
;
739 pbi
->fp_ZeroBinSize_Inter_V
[i
]= (0.5 + temp_fp_ZeroBinSize
);
741 temp_fp_quant_coeffs
= 1.0 / temp_fp_quant_coeffs
;
742 pbi
->fp_quant_Inter_V_coeffs
[i
]= (0.5 + SHIFT16
* temp_fp_quant_coeffs
);
747 pbi
->fquant_coeffs
= pbi
->fp_quant_Y_coeffs
;
751 void select_quantiser(PB_INSTANCE
*pbi
, int type
) {
752 /* select a quantiser according to what plane has to be coded in what
753 * mode. Could be extended to a more sophisticated scheme. */
757 pbi
->fquant_coeffs
= pbi
->fp_quant_Y_coeffs
;
758 pbi
->fquant_round
= pbi
->fp_quant_Y_round
;
759 pbi
->fquant_ZbSize
= pbi
->fp_ZeroBinSize_Y
;
762 pbi
->fquant_coeffs
= pbi
->fp_quant_U_coeffs
;
763 pbi
->fquant_round
= pbi
->fp_quant_U_round
;
764 pbi
->fquant_ZbSize
= pbi
->fp_ZeroBinSize_U
;
767 pbi
->fquant_coeffs
= pbi
->fp_quant_V_coeffs
;
768 pbi
->fquant_round
= pbi
->fp_quant_V_round
;
769 pbi
->fquant_ZbSize
= pbi
->fp_ZeroBinSize_V
;
772 pbi
->fquant_coeffs
= pbi
->fp_quant_Inter_Y_coeffs
;
773 pbi
->fquant_round
= pbi
->fp_quant_Inter_Y_round
;
774 pbi
->fquant_ZbSize
= pbi
->fp_ZeroBinSize_Inter_Y
;
777 pbi
->fquant_coeffs
= pbi
->fp_quant_Inter_U_coeffs
;
778 pbi
->fquant_round
= pbi
->fp_quant_Inter_U_round
;
779 pbi
->fquant_ZbSize
= pbi
->fp_ZeroBinSize_Inter_U
;
782 pbi
->fquant_coeffs
= pbi
->fp_quant_Inter_V_coeffs
;
783 pbi
->fquant_round
= pbi
->fp_quant_Inter_V_round
;
784 pbi
->fquant_ZbSize
= pbi
->fp_ZeroBinSize_Inter_V
;
790 void quantize( PB_INSTANCE
*pbi
,
791 ogg_int16_t
* DCT_block
,
792 Q_LIST_ENTRY
* quantized_list
){
793 ogg_uint32_t i
; /* Row index */
794 Q_LIST_ENTRY val
; /* Quantised value. */
796 ogg_int32_t
* FquantRoundPtr
= pbi
->fquant_round
;
797 ogg_int32_t
* FquantCoeffsPtr
= pbi
->fquant_coeffs
;
798 ogg_int32_t
* FquantZBinSizePtr
= pbi
->fquant_ZbSize
;
799 ogg_int16_t
* DCT_blockPtr
= DCT_block
;
800 ogg_uint32_t
* ZigZagPtr
= (ogg_uint32_t
*)pbi
->zigzag_index
;
803 /* Set the quantized_list to default to 0 */
804 memset( quantized_list
, 0, 64 * sizeof(Q_LIST_ENTRY
) );
806 /* Note that we add half divisor to effect rounding on positive number */
807 for( i
= 0; i
< VFRAGPIXELS
; i
++) {
809 if ( DCT_blockPtr
[0] >= FquantZBinSizePtr
[0] ) {
810 temp
= FquantCoeffsPtr
[0] * ( DCT_blockPtr
[0] + FquantRoundPtr
[0] ) ;
811 val
= (Q_LIST_ENTRY
) (temp
>>16);
812 quantized_list
[ZigZagPtr
[0]] = ( val
> 511 ) ? 511 : val
;
813 } else if ( DCT_blockPtr
[0] <= -FquantZBinSizePtr
[0] ) {
814 temp
= FquantCoeffsPtr
[0] *
815 ( DCT_blockPtr
[0] - FquantRoundPtr
[0] ) + MIN16
;
816 val
= (Q_LIST_ENTRY
) (temp
>>16);
817 quantized_list
[ZigZagPtr
[0]] = ( val
< -511 ) ? -511 : val
;
821 if ( DCT_blockPtr
[1] >= FquantZBinSizePtr
[1] ) {
822 temp
= FquantCoeffsPtr
[1] *
823 ( DCT_blockPtr
[1] + FquantRoundPtr
[1] ) ;
824 val
= (Q_LIST_ENTRY
) (temp
>>16);
825 quantized_list
[ZigZagPtr
[1]] = ( val
> 511 ) ? 511 : val
;
826 } else if ( DCT_blockPtr
[1] <= -FquantZBinSizePtr
[1] ) {
827 temp
= FquantCoeffsPtr
[1] *
828 ( DCT_blockPtr
[1] - FquantRoundPtr
[1] ) + MIN16
;
829 val
= (Q_LIST_ENTRY
) (temp
>>16);
830 quantized_list
[ZigZagPtr
[1]] = ( val
< -511 ) ? -511 : val
;
834 if ( DCT_blockPtr
[2] >= FquantZBinSizePtr
[2] ) {
835 temp
= FquantCoeffsPtr
[2] *
836 ( DCT_blockPtr
[2] + FquantRoundPtr
[2] ) ;
837 val
= (Q_LIST_ENTRY
) (temp
>>16);
838 quantized_list
[ZigZagPtr
[2]] = ( val
> 511 ) ? 511 : val
;
839 } else if ( DCT_blockPtr
[2] <= -FquantZBinSizePtr
[2] ) {
840 temp
= FquantCoeffsPtr
[2] *
841 ( DCT_blockPtr
[2] - FquantRoundPtr
[2] ) + MIN16
;
842 val
= (Q_LIST_ENTRY
) (temp
>>16);
843 quantized_list
[ZigZagPtr
[2]] = ( val
< -511 ) ? -511 : val
;
847 if ( DCT_blockPtr
[3] >= FquantZBinSizePtr
[3] ) {
848 temp
= FquantCoeffsPtr
[3] *
849 ( DCT_blockPtr
[3] + FquantRoundPtr
[3] ) ;
850 val
= (Q_LIST_ENTRY
) (temp
>>16);
851 quantized_list
[ZigZagPtr
[3]] = ( val
> 511 ) ? 511 : val
;
852 } else if ( DCT_blockPtr
[3] <= -FquantZBinSizePtr
[3] ) {
853 temp
= FquantCoeffsPtr
[3] *
854 ( DCT_blockPtr
[3] - FquantRoundPtr
[3] ) + MIN16
;
855 val
= (Q_LIST_ENTRY
) (temp
>>16);
856 quantized_list
[ZigZagPtr
[3]] = ( val
< -511 ) ? -511 : val
;
860 if ( DCT_blockPtr
[4] >= FquantZBinSizePtr
[4] ) {
861 temp
= FquantCoeffsPtr
[4] *
862 ( DCT_blockPtr
[4] + FquantRoundPtr
[4] ) ;
863 val
= (Q_LIST_ENTRY
) (temp
>>16);
864 quantized_list
[ZigZagPtr
[4]] = ( val
> 511 ) ? 511 : val
;
865 } else if ( DCT_blockPtr
[4] <= -FquantZBinSizePtr
[4] ) {
866 temp
= FquantCoeffsPtr
[4] *
867 ( DCT_blockPtr
[4] - FquantRoundPtr
[4] ) + MIN16
;
868 val
= (Q_LIST_ENTRY
) (temp
>>16);
869 quantized_list
[ZigZagPtr
[4]] = ( val
< -511 ) ? -511 : val
;
873 if ( DCT_blockPtr
[5] >= FquantZBinSizePtr
[5] ) {
874 temp
= FquantCoeffsPtr
[5] *
875 ( DCT_blockPtr
[5] + FquantRoundPtr
[5] ) ;
876 val
= (Q_LIST_ENTRY
) (temp
>>16);
877 quantized_list
[ZigZagPtr
[5]] = ( val
> 511 ) ? 511 : val
;
878 } else if ( DCT_blockPtr
[5] <= -FquantZBinSizePtr
[5] ) {
879 temp
= FquantCoeffsPtr
[5] *
880 ( DCT_blockPtr
[5] - FquantRoundPtr
[5] ) + MIN16
;
881 val
= (Q_LIST_ENTRY
) (temp
>>16);
882 quantized_list
[ZigZagPtr
[5]] = ( val
< -511 ) ? -511 : val
;
886 if ( DCT_blockPtr
[6] >= FquantZBinSizePtr
[6] ) {
887 temp
= FquantCoeffsPtr
[6] *
888 ( DCT_blockPtr
[6] + FquantRoundPtr
[6] ) ;
889 val
= (Q_LIST_ENTRY
) (temp
>>16);
890 quantized_list
[ZigZagPtr
[6]] = ( val
> 511 ) ? 511 : val
;
891 } else if ( DCT_blockPtr
[6] <= -FquantZBinSizePtr
[6] ) {
892 temp
= FquantCoeffsPtr
[6] *
893 ( DCT_blockPtr
[6] - FquantRoundPtr
[6] ) + MIN16
;
894 val
= (Q_LIST_ENTRY
) (temp
>>16);
895 quantized_list
[ZigZagPtr
[6]] = ( val
< -511 ) ? -511 : val
;
899 if ( DCT_blockPtr
[7] >= FquantZBinSizePtr
[7] ) {
900 temp
= FquantCoeffsPtr
[7] *
901 ( DCT_blockPtr
[7] + FquantRoundPtr
[7] ) ;
902 val
= (Q_LIST_ENTRY
) (temp
>>16);
903 quantized_list
[ZigZagPtr
[7]] = ( val
> 511 ) ? 511 : val
;
904 } else if ( DCT_blockPtr
[7] <= -FquantZBinSizePtr
[7] ) {
905 temp
= FquantCoeffsPtr
[7] *
906 ( DCT_blockPtr
[7] - FquantRoundPtr
[7] ) + MIN16
;
907 val
= (Q_LIST_ENTRY
) (temp
>>16);
908 quantized_list
[ZigZagPtr
[7]] = ( val
< -511 ) ? -511 : val
;
912 FquantCoeffsPtr
+= 8;
913 FquantZBinSizePtr
+= 8;
919 static void init_dequantizer ( PB_INSTANCE
*pbi
,
920 ogg_uint32_t scale_factor
,
921 unsigned char QIndex
){
924 Q_LIST_ENTRY
* InterY_coeffs
;
925 Q_LIST_ENTRY
* InterU_coeffs
;
926 Q_LIST_ENTRY
* InterV_coeffs
;
927 Q_LIST_ENTRY
* Y_coeffs
;
928 Q_LIST_ENTRY
* U_coeffs
;
929 Q_LIST_ENTRY
* V_coeffs
;
930 Q_LIST_ENTRY
* DcScaleFactorTable
;
932 InterY_coeffs
= pbi
->InterY_coeffs
;
933 InterU_coeffs
= pbi
->InterU_coeffs
;
934 InterV_coeffs
= pbi
->InterV_coeffs
;
935 Y_coeffs
= pbi
->Y_coeffs
;
936 U_coeffs
= pbi
->U_coeffs
;
937 V_coeffs
= pbi
->V_coeffs
;
938 DcScaleFactorTable
= pbi
->DcScaleFactorTable
;
940 /* invert the dequant index into the quant index
941 the dxer has a different order than the cxer. */
942 BuildZigZagIndex(pbi
);
944 /* Reorder dequantisation coefficients into dct zigzag order. */
945 for ( i
= 0; i
< BLOCK_SIZE
; i
++ ) {
946 j
= pbi
->zigzag_index
[i
];
947 pbi
->dequant_Y_coeffs
[j
] = Y_coeffs
[i
];
949 for ( i
= 0; i
< BLOCK_SIZE
; i
++ ) {
950 j
= pbi
->zigzag_index
[i
];
951 pbi
->dequant_U_coeffs
[j
] = U_coeffs
[i
];
953 for ( i
= 0; i
< BLOCK_SIZE
; i
++ ) {
954 j
= pbi
->zigzag_index
[i
];
955 pbi
->dequant_V_coeffs
[j
] = V_coeffs
[i
];
957 for ( i
= 0; i
< BLOCK_SIZE
; i
++ ){
958 j
= pbi
->zigzag_index
[i
];
959 pbi
->dequant_InterY_coeffs
[j
] = InterY_coeffs
[i
];
961 for ( i
= 0; i
< BLOCK_SIZE
; i
++ ){
962 j
= pbi
->zigzag_index
[i
];
963 pbi
->dequant_InterU_coeffs
[j
] = InterU_coeffs
[i
];
965 for ( i
= 0; i
< BLOCK_SIZE
; i
++ ){
966 j
= pbi
->zigzag_index
[i
];
967 pbi
->dequant_InterV_coeffs
[j
] = InterV_coeffs
[i
];
970 /* Intra Y DC coeff */
971 pbi
->dequant_Y_coeffs
[0] =
972 ((DcScaleFactorTable
[QIndex
] * pbi
->dequant_Y_coeffs
[0])/100);
973 if ( pbi
->dequant_Y_coeffs
[0] < MIN_DEQUANT_VAL
* 2 )
974 pbi
->dequant_Y_coeffs
[0] = MIN_DEQUANT_VAL
* 2;
975 pbi
->dequant_Y_coeffs
[0] =
976 pbi
->dequant_Y_coeffs
[0] << IDCT_SCALE_FACTOR
;
979 pbi
->dequant_U_coeffs
[0] =
980 ((DcScaleFactorTable
[QIndex
] * pbi
->dequant_U_coeffs
[0])/100);
981 if ( pbi
->dequant_U_coeffs
[0] < MIN_DEQUANT_VAL
* 2 )
982 pbi
->dequant_U_coeffs
[0] = MIN_DEQUANT_VAL
* 2;
983 pbi
->dequant_U_coeffs
[0] =
984 pbi
->dequant_U_coeffs
[0] << IDCT_SCALE_FACTOR
;
985 pbi
->dequant_V_coeffs
[0] =
986 ((DcScaleFactorTable
[QIndex
] * pbi
->dequant_V_coeffs
[0])/100);
987 if ( pbi
->dequant_V_coeffs
[0] < MIN_DEQUANT_VAL
* 2 )
988 pbi
->dequant_V_coeffs
[0] = MIN_DEQUANT_VAL
* 2;
989 pbi
->dequant_V_coeffs
[0] =
990 pbi
->dequant_V_coeffs
[0] << IDCT_SCALE_FACTOR
;
992 /* Inter Y DC coeff */
993 pbi
->dequant_InterY_coeffs
[0] =
994 ((DcScaleFactorTable
[QIndex
] * pbi
->dequant_InterY_coeffs
[0])/100);
995 if ( pbi
->dequant_InterY_coeffs
[0] < MIN_DEQUANT_VAL
* 4 )
996 pbi
->dequant_InterY_coeffs
[0] = MIN_DEQUANT_VAL
* 4;
997 pbi
->dequant_InterY_coeffs
[0] =
998 pbi
->dequant_InterY_coeffs
[0] << IDCT_SCALE_FACTOR
;
1001 pbi
->dequant_InterU_coeffs
[0] =
1002 ((DcScaleFactorTable
[QIndex
] * pbi
->dequant_InterU_coeffs
[0])/100);
1003 if ( pbi
->dequant_InterU_coeffs
[0] < MIN_DEQUANT_VAL
* 4 )
1004 pbi
->dequant_InterU_coeffs
[0] = MIN_DEQUANT_VAL
* 4;
1005 pbi
->dequant_InterU_coeffs
[0] =
1006 pbi
->dequant_InterU_coeffs
[0] << IDCT_SCALE_FACTOR
;
1007 pbi
->dequant_InterV_coeffs
[0] =
1008 ((DcScaleFactorTable
[QIndex
] * pbi
->dequant_InterV_coeffs
[0])/100);
1009 if ( pbi
->dequant_InterV_coeffs
[0] < MIN_DEQUANT_VAL
* 4 )
1010 pbi
->dequant_InterV_coeffs
[0] = MIN_DEQUANT_VAL
* 4;
1011 pbi
->dequant_InterV_coeffs
[0] =
1012 pbi
->dequant_InterV_coeffs
[0] << IDCT_SCALE_FACTOR
;
1014 for ( i
= 1; i
< BLOCK_SIZE
; i
++ ){
1015 /* now scale coefficients by required compression factor */
1016 pbi
->dequant_Y_coeffs
[i
] =
1017 (( scale_factor
* pbi
->dequant_Y_coeffs
[i
] ) / 100);
1018 if ( pbi
->dequant_Y_coeffs
[i
] < MIN_DEQUANT_VAL
)
1019 pbi
->dequant_Y_coeffs
[i
] = MIN_DEQUANT_VAL
;
1020 pbi
->dequant_Y_coeffs
[i
] =
1021 pbi
->dequant_Y_coeffs
[i
] << IDCT_SCALE_FACTOR
;
1023 pbi
->dequant_U_coeffs
[i
] =
1024 (( scale_factor
* pbi
->dequant_U_coeffs
[i
] ) / 100);
1025 if ( pbi
->dequant_U_coeffs
[i
] < MIN_DEQUANT_VAL
)
1026 pbi
->dequant_U_coeffs
[i
] = MIN_DEQUANT_VAL
;
1027 pbi
->dequant_U_coeffs
[i
] =
1028 pbi
->dequant_U_coeffs
[i
] << IDCT_SCALE_FACTOR
;
1030 pbi
->dequant_V_coeffs
[i
] =
1031 (( scale_factor
* pbi
->dequant_V_coeffs
[i
] ) / 100);
1032 if ( pbi
->dequant_V_coeffs
[i
] < MIN_DEQUANT_VAL
)
1033 pbi
->dequant_V_coeffs
[i
] = MIN_DEQUANT_VAL
;
1034 pbi
->dequant_V_coeffs
[i
] =
1035 pbi
->dequant_V_coeffs
[i
] << IDCT_SCALE_FACTOR
;
1037 pbi
->dequant_InterY_coeffs
[i
] =
1038 (( scale_factor
* pbi
->dequant_InterY_coeffs
[i
] ) / 100);
1039 if ( pbi
->dequant_InterY_coeffs
[i
] < (MIN_DEQUANT_VAL
* 2) )
1040 pbi
->dequant_InterY_coeffs
[i
] = MIN_DEQUANT_VAL
* 2;
1041 pbi
->dequant_InterY_coeffs
[i
] =
1042 pbi
->dequant_InterY_coeffs
[i
] << IDCT_SCALE_FACTOR
;
1044 pbi
->dequant_InterU_coeffs
[i
] =
1045 (( scale_factor
* pbi
->dequant_InterU_coeffs
[i
] ) / 100);
1046 if ( pbi
->dequant_InterU_coeffs
[i
] < (MIN_DEQUANT_VAL
* 2) )
1047 pbi
->dequant_InterU_coeffs
[i
] = MIN_DEQUANT_VAL
* 2;
1048 pbi
->dequant_InterU_coeffs
[i
] =
1049 pbi
->dequant_InterU_coeffs
[i
] << IDCT_SCALE_FACTOR
;
1051 pbi
->dequant_InterV_coeffs
[i
] =
1052 (( scale_factor
* pbi
->dequant_InterV_coeffs
[i
] ) / 100);
1053 if ( pbi
->dequant_InterV_coeffs
[i
] < (MIN_DEQUANT_VAL
* 2) )
1054 pbi
->dequant_InterV_coeffs
[i
] = MIN_DEQUANT_VAL
* 2;
1055 pbi
->dequant_InterV_coeffs
[i
] =
1056 pbi
->dequant_InterV_coeffs
[i
] << IDCT_SCALE_FACTOR
;
1059 pbi
->dequant_coeffs
= pbi
->dequant_Y_coeffs
;
1062 void UpdateQ( PB_INSTANCE
*pbi
, int NewQIndex
){
1063 ogg_uint32_t qscale
;
1065 /* clamp to legal bounds */
1066 if (NewQIndex
>= Q_TABLE_SIZE
) NewQIndex
= Q_TABLE_SIZE
- 1;
1067 else if (NewQIndex
< 0) NewQIndex
= 0;
1069 pbi
->FrameQIndex
= NewQIndex
;
1070 qscale
= pbi
->QThreshTable
[NewQIndex
];
1071 pbi
->ThisFrameQualityValue
= qscale
;
1073 /* Re-initialise the Q tables for forward and reverse transforms. */
1074 init_dequantizer ( pbi
, qscale
, (unsigned char) pbi
->FrameQIndex
);
1077 void UpdateQC( CP_INSTANCE
*cpi
, ogg_uint32_t NewQ
){
1078 ogg_uint32_t qscale
;
1079 PB_INSTANCE
*pbi
= &cpi
->pb
;
1081 /* Do bounds checking and convert to a float. */
1083 if ( qscale
< pbi
->QThreshTable
[Q_TABLE_SIZE
-1] )
1084 qscale
= pbi
->QThreshTable
[Q_TABLE_SIZE
-1];
1085 else if ( qscale
> pbi
->QThreshTable
[0] )
1086 qscale
= pbi
->QThreshTable
[0];
1088 /* Set the inter/intra descision control variables. */
1089 pbi
->FrameQIndex
= Q_TABLE_SIZE
- 1;
1090 while ((ogg_int32_t
) pbi
->FrameQIndex
>= 0 ) {
1091 if ( (pbi
->FrameQIndex
== 0) ||
1092 ( pbi
->QThreshTable
[pbi
->FrameQIndex
] >= NewQ
) )
1094 pbi
->FrameQIndex
--;
1097 /* Re-initialise the Q tables for forward and reverse transforms. */
1098 init_quantizer ( cpi
, qscale
, (unsigned char) pbi
->FrameQIndex
);
1099 init_dequantizer ( pbi
, qscale
, (unsigned char) pbi
->FrameQIndex
);