/src/mozilla-central/media/libtheora/lib/quant.c
Line | Count | Source (jump to first uncovered line) |
1 | | /******************************************************************** |
2 | | * * |
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. * |
7 | | * * |
8 | | * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 * |
9 | | * by the Xiph.Org Foundation and contributors http://www.xiph.org/ * |
10 | | * * |
11 | | ******************************************************************** |
12 | | |
13 | | function: |
14 | | last mod: $Id: quant.c 17307 2010-06-27 06:02:15Z tterribe $ |
15 | | |
16 | | ********************************************************************/ |
17 | | |
18 | | #include <stdlib.h> |
19 | | #include <string.h> |
20 | | #include <ogg/ogg.h> |
21 | | #include "quant.h" |
22 | | #include "decint.h" |
23 | | |
24 | | /*The maximum output of the DCT with +/- 255 inputs is +/- 8157. |
25 | | These minimum quantizers ensure the result after quantization (and after |
26 | | prediction for DC) will be no more than +/- 510. |
27 | | The tokenization system can handle values up to +/- 580, so there is no need |
28 | | to do any coefficient clamping. |
29 | | I would rather have allowed smaller quantizers and had to clamp, but these |
30 | | minimums were required when constructing the original VP3 matrices and have |
31 | | been formalized in the spec.*/ |
32 | | static const unsigned OC_DC_QUANT_MIN[2]={4<<2,8<<2}; |
33 | | static const unsigned OC_AC_QUANT_MIN[2]={2<<2,4<<2}; |
34 | | |
35 | | /*Initializes the dequantization tables from a set of quantizer info. |
36 | | Currently the dequantizer (and elsewhere enquantizer) tables are expected to |
37 | | be initialized as pointing to the storage reserved for them in the |
38 | | oc_theora_state (resp. oc_enc_ctx) structure. |
39 | | If some tables are duplicates of others, the pointers will be adjusted to |
40 | | point to a single copy of the tables, but the storage for them will not be |
41 | | freed. |
42 | | If you're concerned about the memory footprint, the obvious thing to do is |
43 | | to move the storage out of its fixed place in the structures and allocate |
44 | | it on demand. |
45 | | However, a much, much better option is to only store the quantization |
46 | | matrices being used for the current frame, and to recalculate these as the |
47 | | qi values change between frames (this is what VP3 did).*/ |
48 | | void oc_dequant_tables_init(ogg_uint16_t *_dequant[64][3][2], |
49 | 0 | int _pp_dc_scale[64],const th_quant_info *_qinfo){ |
50 | 0 | /*Coding mode: intra or inter.*/ |
51 | 0 | int qti; |
52 | 0 | /*Y', C_b, C_r*/ |
53 | 0 | int pli; |
54 | 0 | for(qti=0;qti<2;qti++)for(pli=0;pli<3;pli++){ |
55 | 0 | /*Quality index.*/ |
56 | 0 | int qi; |
57 | 0 | /*Range iterator.*/ |
58 | 0 | int qri; |
59 | 0 | for(qi=0,qri=0;qri<=_qinfo->qi_ranges[qti][pli].nranges;qri++){ |
60 | 0 | th_quant_base base; |
61 | 0 | ogg_uint32_t q; |
62 | 0 | int qi_start; |
63 | 0 | int qi_end; |
64 | 0 | memcpy(base,_qinfo->qi_ranges[qti][pli].base_matrices[qri], |
65 | 0 | sizeof(base)); |
66 | 0 | qi_start=qi; |
67 | 0 | if(qri==_qinfo->qi_ranges[qti][pli].nranges)qi_end=qi+1; |
68 | 0 | else qi_end=qi+_qinfo->qi_ranges[qti][pli].sizes[qri]; |
69 | 0 | /*Iterate over quality indicies in this range.*/ |
70 | 0 | for(;;){ |
71 | 0 | ogg_uint32_t qfac; |
72 | 0 | int zzi; |
73 | 0 | int ci; |
74 | 0 | /*In the original VP3.2 code, the rounding offset and the size of the |
75 | 0 | dead zone around 0 were controlled by a "sharpness" parameter. |
76 | 0 | The size of our dead zone is now controlled by the per-coefficient |
77 | 0 | quality thresholds returned by our HVS module. |
78 | 0 | We round down from a more accurate value when the quality of the |
79 | 0 | reconstruction does not fall below our threshold and it saves bits. |
80 | 0 | Hence, all of that VP3.2 code is gone from here, and the remaining |
81 | 0 | floating point code has been implemented as equivalent integer code |
82 | 0 | with exact precision.*/ |
83 | 0 | qfac=(ogg_uint32_t)_qinfo->dc_scale[qi]*base[0]; |
84 | 0 | /*For postprocessing, not dequantization.*/ |
85 | 0 | if(_pp_dc_scale!=NULL)_pp_dc_scale[qi]=(int)(qfac/160); |
86 | 0 | /*Scale DC the coefficient from the proper table.*/ |
87 | 0 | q=(qfac/100)<<2; |
88 | 0 | q=OC_CLAMPI(OC_DC_QUANT_MIN[qti],q,OC_QUANT_MAX); |
89 | 0 | _dequant[qi][pli][qti][0]=(ogg_uint16_t)q; |
90 | 0 | /*Now scale AC coefficients from the proper table.*/ |
91 | 0 | for(zzi=1;zzi<64;zzi++){ |
92 | 0 | q=((ogg_uint32_t)_qinfo->ac_scale[qi]*base[OC_FZIG_ZAG[zzi]]/100)<<2; |
93 | 0 | q=OC_CLAMPI(OC_AC_QUANT_MIN[qti],q,OC_QUANT_MAX); |
94 | 0 | _dequant[qi][pli][qti][zzi]=(ogg_uint16_t)q; |
95 | 0 | } |
96 | 0 | /*If this is a duplicate of a previous matrix, use that instead. |
97 | 0 | This simple check helps us improve cache coherency later.*/ |
98 | 0 | { |
99 | 0 | int dupe; |
100 | 0 | int qtj; |
101 | 0 | int plj; |
102 | 0 | dupe=0; |
103 | 0 | for(qtj=0;qtj<=qti;qtj++){ |
104 | 0 | for(plj=0;plj<(qtj<qti?3:pli);plj++){ |
105 | 0 | if(!memcmp(_dequant[qi][pli][qti],_dequant[qi][plj][qtj], |
106 | 0 | sizeof(oc_quant_table))){ |
107 | 0 | dupe=1; |
108 | 0 | break; |
109 | 0 | } |
110 | 0 | } |
111 | 0 | if(dupe)break; |
112 | 0 | } |
113 | 0 | if(dupe)_dequant[qi][pli][qti]=_dequant[qi][plj][qtj]; |
114 | 0 | } |
115 | 0 | if(++qi>=qi_end)break; |
116 | 0 | /*Interpolate the next base matrix.*/ |
117 | 0 | for(ci=0;ci<64;ci++){ |
118 | 0 | base[ci]=(unsigned char)( |
119 | 0 | (2*((qi_end-qi)*_qinfo->qi_ranges[qti][pli].base_matrices[qri][ci]+ |
120 | 0 | (qi-qi_start)*_qinfo->qi_ranges[qti][pli].base_matrices[qri+1][ci]) |
121 | 0 | +_qinfo->qi_ranges[qti][pli].sizes[qri])/ |
122 | 0 | (2*_qinfo->qi_ranges[qti][pli].sizes[qri])); |
123 | 0 | } |
124 | 0 | } |
125 | 0 | } |
126 | 0 | } |
127 | 0 | } |