seq_decompress.cpp

00001 /* ***** BEGIN LICENSE BLOCK *****
00002 *
00003 * $Id: seq_decompress.cpp,v 1.3 2005/01/30 05:11:41 gabest Exp $ $Name:  $
00004 *
00005 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00006 *
00007 * The contents of this file are subject to the Mozilla Public License
00008 * Version 1.1 (the "License"); you may not use this file except in compliance
00009 * with the License. You may obtain a copy of the License at
00010 * http://www.mozilla.org/MPL/
00011 *
00012 * Software distributed under the License is distributed on an "AS IS" basis,
00013 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
00014 * the specific language governing rights and limitations under the License.
00015 *
00016 * The Original Code is BBC Research and Development code.
00017 *
00018 * The Initial Developer of the Original Code is the British Broadcasting
00019 * Corporation.
00020 * Portions created by the Initial Developer are Copyright (C) 2004.
00021 * All Rights Reserved.
00022 *
00023 * Contributor(s): Thomas Davies (Original Author),
00024 *                 Anuradha Suraparaju
00025 *
00026 * Alternatively, the contents of this file may be used under the terms of
00027 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser
00028 * Public License Version 2.1 (the "LGPL"), in which case the provisions of
00029 * the GPL or the LGPL are applicable instead of those above. If you wish to
00030 * allow use of your version of this file only under the terms of the either
00031 * the GPL or LGPL and not to allow others to use your version of this file
00032 * under the MPL, indicate your decision by deleting the provisions above
00033 * and replace them with the notice and other provisions required by the GPL
00034 * or LGPL. If you do not delete the provisions above, a recipient may use
00035 * your version of this file under the terms of any one of the MPL, the GPL
00036 * or the LGPL.
00037 * ***** END LICENSE BLOCK ***** */
00038 
00039 
00041 //---------------------------------------//
00042 //Class to manage decompressing sequences//
00043 //---------------------------------------//
00045 
00046 #include <libdirac_common/dirac_assertions.h>
00047 #include <libdirac_decoder/seq_decompress.h>
00048 #include <libdirac_common/common.h>
00049 #include <libdirac_common/golomb.h>
00050 #include <libdirac_common/frame_buffer.h>
00051 #include <libdirac_decoder/frame_decompress.h>
00052 using namespace dirac;
00053 
00054 SequenceDecompressor::SequenceDecompressor(std::istream* ip,bool verbosity)
00055 : 
00056 m_all_done(false),
00057 m_infile(ip),
00058 m_current_code_fnum(0),
00059 m_delay(1),
00060 m_last_frame_read(-1),
00061 m_show_fnum(-1)
00062 {
00063     m_decparams.SetBitsIn( new BitInputManager(m_infile) );
00064     m_decparams.SetVerbose( verbosity );
00065     ReadStreamHeader();
00066 
00067     //Amount of horizontal padding for Y,U and V components
00068     int xpad_luma,xpad_chroma;
00069 
00070     //Amount of vertical padding for Y,U and V components
00071     int ypad_luma,ypad_chroma;
00072 
00073     //scaling factors for chroma based on chroma format
00074     int x_chroma_fac,y_chroma_fac;
00075 
00076     //First, we need to have sufficient padding to take account of the blocksizes.
00077     //It's sufficient to check for chroma
00078 
00079     if ( m_sparams.CFormat() == format411 )
00080     {
00081         x_chroma_fac = 4; 
00082         y_chroma_fac = 1;
00083     }
00084     else if ( m_sparams.CFormat() == format420 )
00085     {
00086         x_chroma_fac = 2; 
00087         y_chroma_fac = 2;
00088     }
00089     else if ( m_sparams.CFormat() == format422 )
00090     {
00091         x_chroma_fac = 2; 
00092         y_chroma_fac = 1;
00093     }
00094     else
00095     {
00096         x_chroma_fac = 1; 
00097         y_chroma_fac = 1;
00098     }
00099 
00100     int xl_chroma=m_sparams.Xl() / x_chroma_fac;
00101     int yl_chroma=m_sparams.Yl() / y_chroma_fac;
00102 
00103     //make sure we have enough macroblocks to cover the pictures 
00104     m_decparams.SetXNumMB( m_sparams.Xl() / m_decparams.LumaBParams(0).Xbsep() );
00105     m_decparams.SetYNumMB( m_sparams.Yl() / m_decparams.LumaBParams(0).Ybsep() );
00106     if ( m_decparams.XNumMB() * m_decparams.ChromaBParams(0).Xbsep() < xl_chroma )
00107     {
00108         m_decparams.SetXNumMB( m_decparams.XNumMB() + 1 );
00109         xpad_chroma = m_decparams.XNumMB() * m_decparams.ChromaBParams(0).Xbsep() - xl_chroma;
00110     }
00111     else
00112         xpad_chroma=0;
00113 
00114     if (m_decparams.YNumMB()*m_decparams.ChromaBParams(0).Ybsep()<yl_chroma)
00115     {
00116         m_decparams.SetYNumMB( m_decparams.YNumMB() + 1 );
00117         ypad_chroma=m_decparams.YNumMB()*m_decparams.ChromaBParams(0).Ybsep()-yl_chroma;
00118     }
00119     else
00120         ypad_chroma=0;    
00121 
00122     //Now we have an integral number of macroblocks in a picture and we set the number of blocks
00123     m_decparams.SetXNumBlocks( 4*m_decparams.XNumMB() );
00124     m_decparams.SetYNumBlocks( 4*m_decparams.YNumMB() );
00125 
00126     //Next we work out the additional padding due to the wavelet transform
00127     //For the moment, we'll fix the transform depth to be 4, so we need divisibility by 16.
00128     //In the future we'll want arbitrary transform depths. It's sufficient to check for
00129     //chroma only
00130 
00131     int xpad_len = xl_chroma+xpad_chroma;
00132     int ypad_len = yl_chroma+ypad_chroma;
00133 
00134     if ( xpad_len%16 != 0 )
00135         xpad_chroma=( ( xpad_len/16 ) + 1 )*16 - xl_chroma;
00136     if ( ypad_len%16 != 0)
00137         ypad_chroma = ( ( ypad_len/16 ) + 1 )*16 - yl_chroma;    
00138 
00139     xpad_luma = xpad_chroma*x_chroma_fac;
00140     ypad_luma = ypad_chroma*y_chroma_fac;
00141 
00142     //set up padded picture sizes, based on original picture sizes, the block parameters and the wavelet transform depth
00143     m_fbuffer= new FrameBuffer( m_sparams.CFormat() , m_sparams.Xl() + xpad_luma , m_sparams.Yl() + ypad_luma );
00144 
00145     m_fdecoder = new FrameDecompressor (m_decparams , m_sparams.CFormat() );
00146 }
00147 
00148 SequenceDecompressor::~SequenceDecompressor()
00149 {
00150     delete m_fbuffer;
00151     delete m_fdecoder;
00152     delete &m_decparams.BitsIn();
00153 }
00154 
00155 bool SequenceDecompressor::ReadNextFrameHeader()
00156 {
00157     return m_fdecoder->ReadFrameHeader(*m_fbuffer);
00158 }
00159 
00160 const FrameParams& SequenceDecompressor::GetNextFrameParams() const
00161 {
00162     return m_fdecoder->GetFrameParams();
00163 }
00164 
00165 Frame& SequenceDecompressor::DecompressNextFrame(bool skip /* = false */)
00166 {
00167     //this function decodes the next frame in coding order and returns the next frame in display order
00168     //In general these will differ, and because of re-ordering there is a m_delay which needs to be imposed.
00169     //This creates problems at the start and at the end of the sequence which must be dealt with.
00170     //At the start we just keep outputting frame 0. At the end you will need to loop for longer to get all
00171     //the frames out. It's up to the calling function to do something with the decoded frames as they
00172     //come out - write them to screen or to file, as required.
00173 
00174     TEST (m_fdecoder != NULL);
00175 
00176     if (m_current_code_fnum!=0){
00177         //if we're not at the beginning, clean the buffer of frames that can be discarded
00178         m_fbuffer->Clean(m_show_fnum);
00179     }
00180 
00181     bool new_frame_to_display=false;
00182        
00183     if (!skip)
00184        new_frame_to_display = m_fdecoder->Decompress(*m_fbuffer);
00185 
00186     //if we've exited with success, there's a new frame to display, so increment
00187     //the counters. Otherwise, freeze on the last frame shown
00188     m_show_fnum=std::max(m_current_code_fnum-m_delay,0);
00189     if (new_frame_to_display || skip)
00190     {
00191         m_current_code_fnum++;
00192     }
00193 
00194     return m_fbuffer->GetFrame(m_show_fnum);
00195 }
00196 
00197 Frame& SequenceDecompressor::GetNextFrame()
00198 {
00199     return m_fbuffer->GetFrame(m_show_fnum);
00200 }
00201 
00202 void SequenceDecompressor::ReadStreamHeader()
00203 {    //called from constructor
00204 
00205     //read the stream header parameters
00206     //begin with the identifying string
00207     OLBParams bparams;
00208     //char kwname[9];
00209     //for (int i=0; i<8; ++i)
00210     //{
00211     //    kwname[i]=m_decparams.BitsIn().InputByte();    
00212     //}    
00213     //kwname[8]='\0';
00214 
00215     char seq_start[5];
00216     for (int i=0;i<5;++i)
00217     {
00218         seq_start[i]=m_decparams.BitsIn().InputByte();    
00219     }    
00220     //TBC: test that kwname="KW-DIRAC"    
00221 
00222     //bit stream version
00223     m_sparams.SetBitstreamVersion( m_decparams.BitsIn().InputByte() );
00224 
00225     // TODO: test if this bit stream version is supported. Report an error
00226     // otherwise
00227     TESTM (m_sparams.BitstreamVersion() == BITSTREAM_VERSION, "Bitstream version match");
00228 
00229     //picture dimensions
00230     m_sparams.SetXl( int(UnsignedGolombDecode( m_decparams.BitsIn() )) );
00231     m_sparams.SetYl( int(UnsignedGolombDecode( m_decparams.BitsIn() )) );    
00232 
00233     //picture rate
00234     m_sparams.SetFrameRate( int(UnsignedGolombDecode( m_decparams.BitsIn() )) );
00235 
00236      //block parameters
00237     bparams.SetXblen( int(UnsignedGolombDecode( m_decparams.BitsIn() )) );
00238     bparams.SetYblen( int(UnsignedGolombDecode( m_decparams.BitsIn() )) );    
00239     bparams.SetXbsep( int(UnsignedGolombDecode( m_decparams.BitsIn() )) );
00240     bparams.SetYbsep( int(UnsignedGolombDecode( m_decparams.BitsIn() )) );
00241 
00242     //dimensions of block arrays (remember there may need to be padding for some block and picture sizes)
00243     m_decparams.SetXNumBlocks( int(UnsignedGolombDecode( m_decparams.BitsIn())) );
00244     m_decparams.SetYNumBlocks( int(UnsignedGolombDecode( m_decparams.BitsIn())) );
00245     m_decparams.SetXNumMB( m_decparams.XNumBlocks()/4 );
00246     m_decparams.SetYNumMB( m_decparams.YNumBlocks()/4 );
00247 
00248     //chroma format
00249     m_sparams.SetCFormat( ChromaFormat(UnsignedGolombDecode( m_decparams.BitsIn())) );        
00250     m_decparams.SetBlockSizes( bparams , m_sparams.CFormat() );
00251 
00252      //interlace marker
00253     m_decparams.SetInterlace( m_decparams.BitsIn().InputBit() );
00254     m_sparams.SetInterlace( m_decparams.Interlace() );
00255 
00256     //Flush the input to the end of the header
00257     m_decparams.BitsIn().FlushInput();    
00258 }

Generated on Tue Dec 13 14:47:16 2005 for guliverkli by  doxygen 1.4.5