Dirac - A Video Codec

Created by the British Broadcasting Corporation.


common.h

Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002 *
00003 * $Id: common.h,v 1.19 2005/02/02 14:42:41 tjdwave Exp $ $Name: Dirac_0_5_1 $
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 *                 Scott R Ladd,
00025 *                 Tim Borer
00026 *
00027 * Alternatively, the contents of this file may be used under the terms of
00028 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser
00029 * Public License Version 2.1 (the "LGPL"), in which case the provisions of
00030 * the GPL or the LGPL are applicable instead of those above. If you wish to
00031 * allow use of your version of this file only under the terms of the either
00032 * the GPL or LGPL and not to allow others to use your version of this file
00033 * under the MPL, indicate your decision by deleting the provisions above
00034 * and replace them with the notice and other provisions required by the GPL
00035 * or LGPL. If you do not delete the provisions above, a recipient may use
00036 * your version of this file under the terms of any one of the MPL, the GPL
00037 * or the LGPL.
00038 * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef _COMMON_H_
00041 #define _COMMON_H_
00042 
00043 #include <libdirac_common/bit_manager.h>
00044 #include <libdirac_common/arrays.h>
00045 #include <libdirac_common/common_types.h>
00046 #include <vector>
00047 #include <cmath>
00048 
00049 namespace dirac
00050 {
00060     //Some basic enumeration types used throughout the codec ...//
00062 
00064     enum PredMode{ INTRA , REF1_ONLY , REF2_ONLY , REF1AND2 };
00065 
00067     enum CompSort{ Y_COMP , U_COMP , V_COMP , R_COMP , G_COMP , B_COMP };
00068 
00070     enum AddOrSub{ ADD , SUBTRACT };
00071 
00073     enum Direction { FORWARD , BACKWARD };
00074 
00076     enum WltFilter { DAUB , HAAR };
00077 
00079     enum CtxAliases
00080     {//used for residual coding
00081         SIGN0_CTX,          //0     -sign, previous symbol is 0
00082         SIGN_POS_CTX,       //1     -sign, previous symbol is +ve
00083         SIGN_NEG_CTX,       //2     -sign, previous symbol is -ve
00084         
00085         
00086         Z_BIN1z_CTX,        //3     -bin 1, parent is zero, neighbours zero
00087         Z_BIN1nz_CTX,       //4     -bin 1, parent is zero, neighbours non-zero
00088         Z_BIN2_CTX,         //5     -bin 2, parent is zero
00089         Z_BIN3_CTX,         //6     -bin 3, parent is zero
00090         Z_BIN4_CTX,         //7     -bin 4, parent is zero
00091         Z_BIN5plus_CTX,     //8     -bins 5 plus, parent is zero
00092         
00093         NZ_BIN1z_CTX,       //9     -bin 1, parent is non-zero, neighbours zero
00094         NZ_BIN1a_CTX,       //10    -bin 1, parent is non-zero, neighbours small
00095         NZ_BIN1b_CTX,       //11    -bin 1, parent is non-zero, neighbours large
00096         NZ_BIN2_CTX,        //12    -bin 2, parent is non-zero
00097         NZ_BIN3_CTX,        //13    -bin 3, parent is non-zero
00098         NZ_BIN4_CTX,        //14    -bin 4, parent is non-zero
00099         NZ_BIN5plus_CTX,    //15    -bins 5 plus, parent is non-zero
00100         
00101         BLOCK_SKIP_CTX      //16    - blocks are skipped
00102     };
00103 
00105     enum MvCtxAliases
00106     {
00107         
00108         YDC_BIN1_CTX,       //0     -1st bin of DC value for Y
00109         YDC_BIN2plus_CTX,   //1     -remaining DC bins
00110         YDC_SIGN0_CTX,      //2     -sign of Y DC value, previous value 0
00111         UDC_BIN1_CTX,       //3     --ditto
00112         UDC_BIN2plus_CTX,   //4     --for
00113         UDC_SIGN0_CTX,      //5     --U
00114         VDC_BIN1_CTX,       //6     --and
00115         VDC_BIN2plus_CTX,   //7     --V
00116         VDC_SIGN0_CTX,      //8     --components
00117         
00118         REF1x_BIN1_CTX,     //9     -bin 1, REF1 x vals
00119         REF1x_BIN2_CTX,     //10    -bin 2, REF1 x vals
00120         REF1x_BIN3_CTX,     //11    -bin 3, REF1 x vals
00121         REF1x_BIN4_CTX,     //12    -bin 4, REF1 x vals
00122         REF1x_BIN5plus_CTX, //13    -bin 5, REF1 x vals
00123         REF1x_SIGN0_CTX,    //14    -sign, REF1 x vals, previous value 0
00124         REF1x_SIGNP_CTX,    //15    -sign, REF1 x vals, previous value +ve
00125         REF1x_SIGNN_CTX,    //16    -sign, REF1 x vals, previous value -ve
00126         
00127         REF1y_BIN1_CTX,     //17    -bin 1, REF1 y vals
00128         REF1y_BIN2_CTX,     //18    -bin 2, REF1 y vals
00129         REF1y_BIN3_CTX,     //19    -bin 3, REF1 y vals
00130         REF1y_BIN4_CTX,     //20    -bin 4, REF1 y vals
00131         REF1y_BIN5plus_CTX, //21    -bin 5, REF1 y vals
00132         REF1y_SIGN0_CTX,    //22    -sign, REF1 y vals, previous value 0
00133         REF1y_SIGNP_CTX,    //23    -sign, REF1 y vals, previous value +ve
00134         REF1y_SIGNN_CTX,    //24    -sign, REF1 y vals, previous value -ve
00135         
00136         REF2x_BIN1_CTX,     //25    -bin 1, REF2 x vals
00137         REF2x_BIN2_CTX,     //26    -bin 2, REF2 x vals
00138         REF2x_BIN3_CTX,     //27    -bin 3, REF2 x vals
00139         REF2x_BIN4_CTX,     //28    -bin 4, REF2 x vals
00140         REF2x_BIN5plus_CTX, //29    -bin 5, REF2 x vals
00141         REF2x_SIGN0_CTX,    //30    -sign, REF2 x vals, previous value 0
00142         REF2x_SIGNP_CTX,    //31    -sign, REF1 y vals, previous value +ve
00143         REF2x_SIGNN_CTX,    //32    -sign, REF1 y vals, previous value -ve
00144         
00145         REF2y_BIN1_CTX,     //33    -bin 1, REF2 y vals
00146         REF2y_BIN2_CTX,     //34    -bin 2, REF2 y vals
00147         REF2y_BIN3_CTX,     //35    -bin 3, REF2 y vals
00148         REF2y_BIN4_CTX,     //36    -bin 4, REF2 y vals
00149         REF2y_BIN5plus_CTX, //37    -bin 5, REF2 y vals
00150         REF2y_SIGN0_CTX,    //38    -sign, REF2 y vals, previous value 0
00151         REF2y_SIGNP_CTX,    //39    -sign, REF2 y vals, previous value +ve
00152         REF2y_SIGNN_CTX,    //40    -sign, REF2 y vals, previous value -ve
00153         
00154         PMODE_BIT0_CTX,     //41    -bit 0, prediction mode value
00155         PMODE_BIT1_CTX,     //42    -bin 1, prediction mode value
00156         
00157         MB_CMODE_CTX,       //43    -context for MB common block mode
00158         MB_SPLIT_BIN1_CTX,  //44    -bin1, MB split mode vals
00159         MB_SPLIT_BIN2_CTX   //45    -bin2, MB split mode vals. Bin 3 not required
00160         
00161     };
00162 
00163 
00164     //Classes used throughout the codec//
00166 
00167 
00169 
00175     class PicArray: public TwoDArray<ValueType>
00176     {
00177     public:
00179 
00182         PicArray(): TwoDArray<ValueType>(){}
00183         
00185 
00189         PicArray(int height, int width, CompSort cs=Y_COMP);
00190         
00191         //copy constructor and assignment= derived by inheritance
00192         
00194         ~PicArray(){}
00195         
00197         const CompSort& CSort() const;
00198         
00200         void SetCSort(const CompSort cs);
00201         
00202     private:
00203         
00204         CompSort m_csort;
00205     };
00206 
00207 
00209     class CostType
00210     {
00211     public:
00213         double MSE;
00214         
00216         double ENTROPY;
00217         
00219         double TOTAL;
00220     };
00221 
00222 
00224 
00229     class EntropyCorrector
00230     {
00231     public:
00233 
00237         EntropyCorrector(int depth);
00238         
00240         //NB: Assume default copy constructor, assignment = and destructor//
00242 
00244 
00248         float Factor(const int bandnum, const FrameSort fsort,const CompSort c) const;
00249 
00251 
00259         void Update(int bandnum, FrameSort fsort, CompSort c,int est_bits,int actual_bits);
00260         
00261     private:
00263         void Init();
00264 
00265         TwoDArray<float> m_Yfctrs;
00266         TwoDArray<float> m_Ufctrs;
00267         TwoDArray<float> m_Vfctrs;
00268     };
00269 
00271     class OLBParams
00272     {
00273         
00274     public:
00275         
00277         OLBParams(){}
00278         
00280         /*
00281             Constructor rationalises proposed parameters to allow suitable 
00282             overlap and fit in with chroma format
00283             \param    xblen    the horizontal block length    
00284             \param    yblen    the vertical block length
00285             \param    xblen    the horizontal block separation
00286             \param    yblen    the vertical block separation
00287 
00288         */
00289         OLBParams(const int xblen, const int yblen, 
00290                   const int xbsep, const int ybsep);
00291         
00292         // Gets ...
00293         
00295         int Xblen() const {return m_xblen;}
00296         
00298         int Yblen() const {return m_yblen;}
00299         
00301         int Xbsep() const {return m_xbsep;}
00302         
00304         int Ybsep() const {return m_ybsep;}
00305         
00307         int Xoffset() const {return m_xoffset;}
00308         
00310         int Yoffset() const {return m_yoffset;}
00311         
00312         // ... and sets
00313         
00315         void SetXblen( int xblen ){ m_xblen = xblen; m_xoffset = (m_xblen-m_xbsep)/2;}
00316         
00318         void SetYblen( int yblen ){ m_yblen = yblen; m_yoffset = (m_yblen-m_ybsep)/2;}
00319         
00321         void SetXbsep( int xbsep ){ m_xbsep = xbsep; m_xoffset = (m_xblen-m_xbsep)/2;}
00322         
00324         void SetYbsep( int ybsep ){ m_ybsep = ybsep; m_yoffset = (m_yblen-m_ybsep)/2;}
00325         
00326         // overloaded stream operators
00327         friend std::ostream & operator<< (std::ostream &, OLBParams &);
00328         friend std::istream & operator>> (std::istream &, OLBParams &);
00329         
00330         
00331     private:
00332         
00333         int m_xblen;
00334         int m_yblen;
00335         int m_xbsep;
00336         int m_ybsep;
00337         int m_xoffset;
00338         int m_yoffset;
00339     };
00340 
00342     class SeqParams
00343     {
00344     public:        
00346         SeqParams();
00347         
00349         //NB: Assume default copy constructor, assignment = and destructor//
00351         
00352         //gets ...
00354         int Xl() const {return m_xl;}
00355         
00357         int Yl() const {return m_yl;}
00358         
00360         ChromaFormat CFormat() const {return m_cformat;}
00361         
00363         int ChromaWidth() const;
00364         
00366         int ChromaHeight() const;
00367         
00369         bool Interlace() const {return m_interlace;}
00370         
00372         bool TopFieldFirst() const {return m_topfieldfirst;}
00373         
00375         int FrameRate() const {return m_framerate;}
00376         
00378         int BitstreamVersion() const {return m_bs_ver;}
00379         
00380         // ... Sets
00381         
00383         void SetXl(int xlen) {m_xl = xlen;}
00384         
00386         void SetYl(int ylen) {m_yl = ylen;}
00387         
00389         void SetCFormat(ChromaFormat cf) {m_cformat=cf;}
00390         
00392         void SetInterlace(bool ilace) {m_interlace=ilace;}
00393         
00395         void SetTopFieldFirst(bool tff) {m_topfieldfirst=tff;}
00396         
00398         void SetFrameRate(int fr){m_framerate=fr;}
00399         
00401         void SetBitstreamVersion(int bs_ver){m_bs_ver=bs_ver;}
00402         
00403     private:
00405         int m_xl;
00406         
00408         int m_yl;
00409         
00411         ChromaFormat m_cformat;
00412         
00414         bool m_interlace;
00415         
00417         bool m_topfieldfirst;
00418         
00420         int m_framerate;
00421         
00423         unsigned char  m_bs_ver;
00424     };
00425 
00427     class FrameParams 
00428     {
00429         
00430     public:
00432         FrameParams();
00433         
00435 
00438         FrameParams(const ChromaFormat& cf, int xlen, int ylen);
00439         
00441 
00444         FrameParams(const ChromaFormat& cf, const FrameSort& fs);
00445         
00447         /*
00448             All data is derived from the sequence parameters
00449         */
00450         FrameParams(const SeqParams& sparams);
00451         
00453         /*
00454            All data is derived from the sequence parameters
00455         */
00456         FrameParams(const SeqParams& sparams, const FrameSort& fs);
00457         
00459         //NB: Assume default copy constructor, assignment = and destructor//
00461         
00462         // Gets ...
00463         
00465         const ChromaFormat& CFormat() const{return m_cformat;}
00466         
00468         int Xl() const{return m_xl;}
00469         
00471         int Yl() const{return m_yl;}
00472         
00474         const FrameSort& FSort() const {return m_fsort;}
00475         
00477         int FrameNum() const {return m_fnum;}
00478         
00480         int ExpiryTime() const {return m_expiry_time;}
00481         
00483         bool Output() const {return m_output;}
00484         
00486         const std::vector<int>& Refs() const {return m_refs;}
00487         
00489         std::vector<int>& Refs(){return m_refs;}
00490         
00491         
00492         // ... Sets
00493         
00495         void SetFSort( const FrameSort& fs ){ m_fsort=fs; }
00496         
00498         void SetFrameNum( const int fn ){ m_fnum=fn; }
00499         
00501         void SetExpiryTime( const int expt ){ m_expiry_time=expt; }
00502         
00504         void SetAsOutput(){m_output=true;}
00505         
00506     private:
00507         
00509         ChromaFormat m_cformat;
00510         
00512         int m_xl;
00513         
00515         int m_yl;
00516         
00518         FrameSort m_fsort;
00519         
00521         std::vector<int> m_refs;
00522         
00524         int m_expiry_time;
00525         
00527         bool m_output;
00528         
00530         int m_fnum;        
00531     };
00532 
00533 //    //! Operator for inputting block parameters
00534 //    std::istream & operator>> (std::istream & stream, OLBParams & params);
00535 //
00536 //    //! Operator for outputting block parameters
00537 //    std::ostream & operator<< (std::ostream & stream, OLBParams & params);
00538 
00539 
00541 
00544     class CodecParams
00545     {
00546     public:
00547         
00549         CodecParams();
00550         
00552             //NB: Assume default copy constructor, assignment = and destructor//
00554         
00555         // Gets ...    
00556         
00558         int XNumMB() const {return m_x_num_mb;}
00559         
00561         int YNumMB() const {return m_y_num_mb;}
00562         
00564         int XNumBlocks() const {return m_x_num_blocks;}
00565         
00567         int YNumBlocks() const {return m_y_num_blocks;}
00568         
00570         bool Verbose() const {return m_verbose;}
00571         
00573         bool Interlace() const {return m_interlace;}
00574         
00576         bool TopFieldFirst() const {return m_topfieldfirst;}    
00577         
00579         const OLBParams& LumaBParams(int n) const {return m_lbparams[n];}
00580         
00582         const OLBParams& ChromaBParams(int n) const {return m_cbparams[n];}    
00583 
00585         int OrigXl() const {return m_orig_xl;}
00586 
00588         int OrigYl() const {return m_orig_yl;}
00589         
00590         // ... and Sets
00592         void SetXNumMB(const int xn){m_x_num_mb=xn;}    
00593         
00595         void SetYNumMB(const int yn){m_y_num_mb=yn;}
00596         
00598         void SetXNumBlocks(const int xn){m_x_num_blocks=xn;}
00599         
00601         void SetYNumBlocks(const int yn){m_y_num_blocks=yn;}
00602         
00604         void SetVerbose(bool v){m_verbose=v;}
00605         
00607         void SetInterlace(bool intlc){m_interlace=intlc;}
00608         
00610         void SetTopFieldFirst(bool topf){m_topfieldfirst=topf;}
00611         
00613         void SetBlockSizes(const OLBParams& olbparams , const ChromaFormat cformat);
00614 
00616         void SetOrigXl(const int x){m_orig_xl=x;}
00617 
00619         void SetOrigYl(const int y){m_orig_yl=y;}
00620 
00621     private:
00622         
00624         int m_x_num_mb;
00625         
00627         int m_y_num_mb;
00628         
00630         int m_x_num_blocks;    
00631         
00633         int m_y_num_blocks;
00634         
00636         bool m_verbose;
00637         
00639         bool m_interlace;
00640         
00642         bool m_topfieldfirst;
00643         
00644         OneDArray<OLBParams> m_lbparams;
00645         OneDArray<OLBParams> m_cbparams;
00646 
00648         int m_orig_xl;
00649 
00651         int m_orig_yl;
00652     };
00653 
00655 
00658     class EncoderParams: public CodecParams
00659     {
00660         //codec params plus parameters relating solely to the operation of the encoder
00661         
00662     public:
00664         EncoderParams();
00665         
00667             //NB: Assume default copy constructor, assignment = and destructor//
00668             //This means pointers are copied, not the objects they point to.////       
00670         
00671          // Gets ...
00672 
00674         float Qf() const {return m_qf;}
00675 
00677 
00682         int NumL1() const {return m_num_L1;}
00683 
00685         int L1Sep() const {return m_L1_sep;}
00686 
00688         float UFactor() const {return m_ufactor;}
00689 
00691         float VFactor() const {return m_vfactor;}
00692 
00694         float CPD() const {return m_cpd;}
00695 
00697         float ILambda() const {return m_I_lambda;}
00698 
00700         float L1Lambda() const {return m_L1_lambda;}
00701 
00703         float L2Lambda() const {return m_L2_lambda;}
00704 
00706         float Lambda(const FrameSort& fsort) const;
00707 
00709         float L1MELambda() const {return m_L1_me_lambda;}
00710 
00712         float L2MELambda() const {return m_L2_me_lambda;}
00713 
00715         char * OutputPath() const {return ( char* ) m_output_path.c_str();}
00716         
00718         const EntropyCorrector& EntropyFactors() const {return *m_ent_correct;}
00719         
00721         EntropyCorrector& EntropyFactors() {return *m_ent_correct;}
00722         
00724         const SequenceOutputManager& BitsOut() const {return *m_bit_out;}
00725         
00727         SequenceOutputManager& BitsOut() {return *m_bit_out;}
00728         
00729         // ... and Sets
00730 
00732         void SetQf(const float qfac){m_qf=qfac;}
00733 
00735         void SetNumL1(const int nl){m_num_L1=nl;}
00736 
00738         void SetL1Sep(const int lsep){m_L1_sep=lsep;}
00739 
00741         void SetUFactor(const float uf){m_ufactor=uf;}
00742 
00744         void SetVFactor(const float vf){m_vfactor=vf;}
00745 
00747         void SetCPD(const float cpd){m_cpd=cpd;}
00748 
00750         void SetILambda(const float l){m_I_lambda=l;}
00751 
00753         void SetL1Lambda(const float l){m_L1_lambda=l;}
00754 
00756         void SetL2Lambda(const float l){m_L2_lambda=l;}
00757 
00759         void SetLambda(const FrameSort& fsort, const float l);
00760 
00762         void SetL1MELambda(const float l){m_L1_me_lambda=l;}
00763 
00765         void SetL2MELambda(const float l){m_L2_me_lambda=l;}
00766 
00768         void SetOutputPath(const char * op){ m_output_path = op; }
00769         
00771         void SetEntropyFactors(EntropyCorrector* entcorrect){m_ent_correct=entcorrect;}
00772         
00774         void SetBitsOut( SequenceOutputManager* so ){ m_bit_out=so; }
00775         
00776     private:
00778         float m_qf; 
00779         
00781         int m_num_L1;
00782         
00784         int m_L1_sep;    
00785         
00787         float m_ufactor;
00788         
00790         float m_vfactor;
00791         
00793         float m_cpd;
00794         
00796         float m_I_lambda;
00797 
00799         float m_L1_lambda;
00800 
00802         float m_L2_lambda;
00803         
00805         float m_L1_me_lambda;
00806 
00808         float m_L2_me_lambda; 
00809         
00811         EntropyCorrector* m_ent_correct;
00812         
00814         SequenceOutputManager* m_bit_out;   
00815         
00817         std::string m_output_path;
00818     };
00819 
00821 
00824     class DecoderParams: public CodecParams
00825     {
00826     public:
00828         DecoderParams():
00829         CodecParams(),
00830         m_bit_in(0){}
00831         
00833             //NB: Assume default copy constructor, assignment = and destructor//
00834             //This means pointers are copied, not the objects they point to.////       
00836         
00838         const BitInputManager& BitsIn() const {return *m_bit_in;}
00839         
00841         BitInputManager& BitsIn() {return *m_bit_in;}
00842         
00844         void SetBitsIn(BitInputManager* bi){m_bit_in=bi;}
00845         
00846     private:        
00848         BitInputManager* m_bit_in;
00849     };
00850 
00852     inline ValueType BChk(const ValueType &num, const ValueType &max)
00853     {
00854         if(num < 0) return 0;
00855         else if(num >= max) return max-1;
00856         else return num;
00857     }
00858 
00860     class QuantiserLists
00861     {
00862     public:
00864         QuantiserLists();
00865 
00866         inline int QuantFactor( const int index ) const {return m_qflist[index]; }
00867         inline int InverseQuantFactor( const int index ) const {return m_qfinvlist[index]; }
00868         inline int QuantOffset( const int index ) const {return m_offset[index]; }
00869     
00870 
00871     private:
00872         OneDArray<int> m_qflist;
00873         OneDArray<int> m_qfinvlist;
00874         OneDArray<int> m_offset;
00875 
00876     };
00877 
00879     extern const QuantiserLists dirac_quantiser_lists;
00880 
00881 } // namespace dirac
00882 
00883 #endif

© 2004 British Broadcasting Corporation. Dirac code licensed under the Mozilla Public License (MPL) Version 1.1.
HTML documentation generated by Dimitri van Heesch's excellent Doxygen tool.