1 /* ***** BEGIN LICENSE BLOCK *****
2 *
3 * $Id: comp_decompress.cpp,v 1.32 2009/01/21 05:18:09 asuraparaju Exp $ $Name: Dirac_1_0_2 $
4 *
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 *
7 * The contents of this file are subject to the Mozilla Public License
8 * Version 1.1 (the "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
11 *
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14 * the specific language governing rights and limitations under the License.
15 *
16 * The Original Code is BBC Research and Development code.
17 *
18 * The Initial Developer of the Original Code is the British Broadcasting
19 * Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2004.
21 * All Rights Reserved.
22 *
23 * Contributor(s): Thomas Davies (Original Author),
24 *                 Scott R Ladd,
25 *                 Anuradha Suraparaju,
26 *                 Andrew Kennedy,
27 *                 Tim Borer
28 *
29 * Alternatively, the contents of this file may be used under the terms of
30 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser
31 * Public License Version 2.1 (the "LGPL"), in which case the provisions of
32 * the GPL or the LGPL are applicable instead of those above. If you wish to
33 * allow use of your version of this file only under the terms of the either
34 * the GPL or LGPL and not to allow others to use your version of this file
35 * under the MPL, indicate your decision by deleting the provisions above
36 * and replace them with the notice and other provisions required by the GPL
37 * or LGPL. If you do not delete the provisions above, a recipient may use
38 * your version of this file under the terms of any one of the MPL, the GPL
39 * or the LGPL.
40 * ***** END LICENSE BLOCK ***** */
41 
42 
43 #include <libdirac_decoder/comp_decompress.h>
44 #include <libdirac_common/wavelet_utils.h>
45 #include <libdirac_common/band_codec.h>
46 #include <libdirac_common/band_vlc.h>
47 using namespace dirac;
48 
49 #include <vector>
50 
51 #include <ctime>
52 
53 using std::vector;
54 
55 //Constructor
CompDecompressor(DecoderParams & decp,const PictureParams & pp)56 CompDecompressor::CompDecompressor( DecoderParams& decp, const PictureParams& pp)
57 :
58     m_decparams(decp),
59     m_pparams(pp),
60     m_psort( pp.PicSort() )
61 {}
62 
63 
Decompress(ComponentByteIO * p_component_byteio,CoeffArray & coeff_data,SubbandList & bands)64 void CompDecompressor::Decompress(ComponentByteIO* p_component_byteio,
65                                   CoeffArray& coeff_data,
66                                   SubbandList& bands)
67 {
68 
69     // Set up the code blocks
70     SetupCodeBlocks( bands );
71 
72     for ( int b=bands.Length() ; b>=1 ; --b ){
73         // Multiple quantiser are used only if
74         // a. The global code_block_mode is QUANT_MULTIPLE
75         //              and
76         // b. More than one code block is present in the subband.
77         bands(b).SetUsingMultiQuants(
78                            m_decparams.SpatialPartition() &&
79                            m_decparams.GetCodeBlockMode() == QUANT_MULTIPLE &&
80                            (bands(b).GetCodeBlocks().LengthX() > 1 ||
81                            bands(b).GetCodeBlocks().LengthY() > 1)
82                                 );
83 
84         // Read the header data first
85         SubbandByteIO subband_byteio(bands(b), *p_component_byteio);
86         subband_byteio.Input();
87 
88         if ( !bands(b).Skipped() ){
89             if (m_pparams.UsingAC()){
90                 // A pointer to the object(s) we'll be using for coding the bands
91                 BandCodec* bdecoder;
92 
93                 if ( b>=bands.Length()-3){
94                     if ( m_psort.IsIntra() && b==bands.Length() )
95                         bdecoder=new IntraDCBandCodec(&subband_byteio,
96                                                        TOTAL_COEFF_CTXS ,bands);
97                     else
98                         bdecoder=new LFBandCodec(&subband_byteio ,
99                                                  TOTAL_COEFF_CTXS, bands ,
100                                                  b, m_psort.IsIntra());
101                 }
102                 else
103                     bdecoder=new BandCodec( &subband_byteio , TOTAL_COEFF_CTXS ,
104                                             bands , b, m_psort.IsIntra());
105 
106                 bdecoder->Decompress(coeff_data , subband_byteio.GetBandDataLength());
107                 delete bdecoder;
108             }
109             else{
110                 // A pointer to the object(s) we'll be using for coding the bands
111                 BandVLC* bdecoder;
112 
113                    if ( m_psort.IsIntra() && b==bands.Length() )
114                       bdecoder=new IntraDCBandVLC(&subband_byteio, bands);
115                 else
116                     bdecoder=new BandVLC( &subband_byteio , 0, bands ,
117                                           b, m_psort.IsIntra());
118 
119                 bdecoder->Decompress(coeff_data , subband_byteio.GetBandDataLength());
120                 delete bdecoder;
121             }
122         }
123         else{
124             SetToVal( coeff_data , bands(b) , 0 );
125         }
126     }
127 }
128 
SetupCodeBlocks(SubbandList & bands)129 void CompDecompressor::SetupCodeBlocks( SubbandList& bands )
130 {
131     int xregions;
132     int yregions;
133 
134     for (int band_num = 1; band_num<=bands.Length() ; ++band_num)
135     {
136         if (m_decparams.SpatialPartition())
137         {
138             int level = m_decparams.TransformDepth() - (band_num-1)/3;
139             const CodeBlocks &cb = m_decparams.GetCodeBlocks(level);
140             xregions = cb.HorizontalCodeBlocks();
141             yregions = cb.VerticalCodeBlocks();
142         }
143         else
144         {
145                xregions = 1;
146                yregions = 1;
147         }
148 
149         bands( band_num ).SetNumBlocks( yregions ,xregions );
150 
151     }// band_num
152 }
153 
SetToVal(CoeffArray & coeff_data,const Subband & node,CoeffType val)154 void CompDecompressor::SetToVal( CoeffArray& coeff_data ,
155                                  const Subband& node ,
156                                  CoeffType val )
157 {
158 
159     for (int j=node.Yp() ; j<node.Yp()+node.Yl() ; ++j)
160         for (int i=node.Xp() ; i<node.Xp()+node.Xl() ; ++i)
161             coeff_data[j][i]=val;
162 
163 }
164