Updating Contact email
[BrunelResearch-dirac.git] / libdirac_encoder / comp_compress.cpp
blob208c9e48033235a3cc17fc2bde3b3844cfa07bdf
1 /* ***** BEGIN LICENSE BLOCK *****
3 * $Id$ $Name$
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
16 * The Original Code is BBC Research and Development code.
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.
23 * Contributor(s): Thomas Davies (Original Author),
24 * Scott R Ladd,
25 * Anuradha Suraparaju
26 * Andrew Kennedy
27 * Tim Borer
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 ***** */
43 //Compression of an individual component,
44 //after motion compensation if appropriate
45 //////////////////////////////////////////
47 #include <libdirac_encoder/comp_compress.h>
48 #include <libdirac_common/band_codec.h>
49 #include <libdirac_common/band_vlc.h>
50 #include <libdirac_common/motion.h>
52 using namespace dirac;
54 #include <ctime>
55 #include <vector>
56 #include <iostream>
58 CompCompressor::CompCompressor( EncoderParams& encp,const PictureParams& pp)
59 : m_encparams(encp),
60 m_pparams(pp),
61 m_psort( m_pparams.PicSort() ),
62 m_cformat( m_pparams.CFormat() )
65 ComponentByteIO* CompCompressor::Compress( CoeffArray& coeff_data ,
66 SubbandList& bands,
67 CompSort csort,
68 const OneDArray<unsigned int>& estimated_bits)
70 // Need to transform, select quantisers for each band,
71 // and then compress each component in turn
73 unsigned int num_band_bytes( 0 );
75 // create byte output
76 ComponentByteIO *p_component_byteio = new ComponentByteIO(csort);
78 // Loop over all the bands (from DC to HF) quantising and coding them
79 for (int b=bands.Length() ; b>=1 ; --b )
82 // create subband byte io
83 SubbandByteIO subband_byteio(bands(b));
85 if ( !bands(b).Skipped() )
86 { // If not skipped ...
87 if (m_pparams.UsingAC())
89 // A pointer to an object for coding the subband data
90 BandCodec* bcoder;
93 // Pick the right codec according to the picture type and subband
94 if (b >= bands.Length()-3)
96 if ( m_psort.IsIntra() && b == bands.Length() )
97 bcoder=new IntraDCBandCodec(&subband_byteio,
98 TOTAL_COEFF_CTXS , bands );
99 else
100 bcoder=new LFBandCodec(&subband_byteio ,TOTAL_COEFF_CTXS,
101 bands , b, m_psort.IsIntra());
103 else
104 bcoder=new BandCodec(&subband_byteio , TOTAL_COEFF_CTXS ,
105 bands , b, m_psort.IsIntra() );
107 num_band_bytes = bcoder->Compress(coeff_data);
110 delete bcoder;
112 else
114 // A pointer to an object for coding the subband data
115 BandVLC* bcoder;
117 if ( m_psort.IsIntra() && b == bands.Length() )
118 bcoder=new IntraDCBandVLC(&subband_byteio, bands );
119 else
120 bcoder=new BandVLC(&subband_byteio , bands , b,
121 m_psort.IsIntra() );
123 num_band_bytes = bcoder->Compress(coeff_data);
125 delete bcoder;
127 // Update the entropy correction factors
128 m_encparams.EntropyFactors().Update(b , m_pparams , csort ,
129 estimated_bits[b] , 8*num_band_bytes);
131 else
132 { // ... skipped
133 SetToVal( coeff_data , bands(b) , 0 );
136 // output sub-band data
137 p_component_byteio->AddSubband(&subband_byteio);
140 }//b
142 return p_component_byteio;
145 void CompCompressor::SetToVal(CoeffArray& coeff_data,const Subband& node,ValueType val)
148 for (int j=node.Yp() ; j<node.Yp() + node.Yl() ; ++j)
150 for (int i=node.Xp(); i<node.Xp() + node.Xl() ; ++i)
152 coeff_data[j][i] = val;
153 }// i
154 }// j