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.22 2005/05/19 10:21:30 asuraparaju Exp $ $Name: Dirac_0_5_2 $
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 <libdirac_common/dirac_assertions.h>
00047 #include <vector>
00048 #include <cmath>
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 { DAUB97 , APPROX97 , FIVETHREE , THIRTEENFIVE};
00078     static const int NUM_WLT_FILTERS = 4;
00079 
00081     enum CtxAliases
00082     {//used for residual coding
00083         SIGN0_CTX,          //0     -sign, previous symbol is 0
00084         SIGN_POS_CTX,       //1     -sign, previous symbol is +ve
00085         SIGN_NEG_CTX,       //2     -sign, previous symbol is -ve
00086         
00087         
00088         Z_BIN1z_CTX,        //3     -bin 1, parent is zero, neighbours zero
00089         Z_BIN1nz_CTX,       //4     -bin 1, parent is zero, neighbours non-zero
00090         Z_BIN2_CTX,         //5     -bin 2, parent is zero
00091         Z_BIN3_CTX,         //6     -bin 3, parent is zero
00092         Z_BIN4_CTX,         //7     -bin 4, parent is zero
00093         Z_BIN5plus_CTX,     //8     -bins 5 plus, parent is zero
00094         
00095         NZ_BIN1z_CTX,       //9     -bin 1, parent is non-zero, neighbours zero
00096         NZ_BIN1a_CTX,       //10    -bin 1, parent is non-zero, neighbours small
00097         NZ_BIN1b_CTX,       //11    -bin 1, parent is non-zero, neighbours large
00098         NZ_BIN2_CTX,        //12    -bin 2, parent is non-zero
00099         NZ_BIN3_CTX,        //13    -bin 3, parent is non-zero
00100         NZ_BIN4_CTX,        //14    -bin 4, parent is non-zero
00101         NZ_BIN5plus_CTX,    //15    -bins 5 plus, parent is non-zero
00102         
00103         BLOCK_SKIP_CTX      //16    - blocks are skipped
00104     };
00105 
00107     enum MvCtxAliases
00108     {
00109         
00110         YDC_BIN1_CTX,       //0     -1st bin of DC value for Y
00111         YDC_BIN2plus_CTX,   //1     -remaining DC bins
00112         YDC_SIGN0_CTX,      //2     -sign of Y DC value, previous value 0
00113         UDC_BIN1_CTX,       //3     --ditto
00114         UDC_BIN2plus_CTX,   //4     --for
00115         UDC_SIGN0_CTX,      //5     --U
00116         VDC_BIN1_CTX,       //6     --and
00117         VDC_BIN2plus_CTX,   //7     --V
00118         VDC_SIGN0_CTX,      //8     --components
00119         
00120         REF1x_BIN1_CTX,     //9     -bin 1, REF1 x vals
00121         REF1x_BIN2_CTX,     //10    -bin 2, REF1 x vals
00122         REF1x_BIN3_CTX,     //11    -bin 3, REF1 x vals
00123         REF1x_BIN4_CTX,     //12    -bin 4, REF1 x vals
00124         REF1x_BIN5plus_CTX, //13    -bin 5, REF1 x vals
00125         REF1x_SIGN0_CTX,    //14    -sign, REF1 x vals, previous value 0
00126         REF1x_SIGNP_CTX,    //15    -sign, REF1 x vals, previous value +ve
00127         REF1x_SIGNN_CTX,    //16    -sign, REF1 x vals, previous value -ve
00128         
00129         REF1y_BIN1_CTX,     //17    -bin 1, REF1 y vals
00130         REF1y_BIN2_CTX,     //18    -bin 2, REF1 y vals
00131         REF1y_BIN3_CTX,     //19    -bin 3, REF1 y vals
00132         REF1y_BIN4_CTX,     //20    -bin 4, REF1 y vals
00133         REF1y_BIN5plus_CTX, //21    -bin 5, REF1 y vals
00134         REF1y_SIGN0_CTX,    //22    -sign, REF1 y vals, previous value 0
00135         REF1y_SIGNP_CTX,    //23    -sign, REF1 y vals, previous value +ve
00136         REF1y_SIGNN_CTX,    //24    -sign, REF1 y vals, previous value -ve
00137         
00138         REF2x_BIN1_CTX,     //25    -bin 1, REF2 x vals
00139         REF2x_BIN2_CTX,     //26    -bin 2, REF2 x vals
00140         REF2x_BIN3_CTX,     //27    -bin 3, REF2 x vals
00141         REF2x_BIN4_CTX,     //28    -bin 4, REF2 x vals
00142         REF2x_BIN5plus_CTX, //29    -bin 5, REF2 x vals
00143         REF2x_SIGN0_CTX,    //30    -sign, REF2 x vals, previous value 0
00144         REF2x_SIGNP_CTX,    //31    -sign, REF1 y vals, previous value +ve
00145         REF2x_SIGNN_CTX,    //32    -sign, REF1 y vals, previous value -ve
00146         
00147         REF2y_BIN1_CTX,     //33    -bin 1, REF2 y vals
00148         REF2y_BIN2_CTX,     //34    -bin 2, REF2 y vals
00149         REF2y_BIN3_CTX,     //35    -bin 3, REF2 y vals
00150         REF2y_BIN4_CTX,     //36    -bin 4, REF2 y vals
00151         REF2y_BIN5plus_CTX, //37    -bin 5, REF2 y vals
00152         REF2y_SIGN0_CTX,    //38    -sign, REF2 y vals, previous value 0
00153         REF2y_SIGNP_CTX,    //39    -sign, REF2 y vals, previous value +ve
00154         REF2y_SIGNN_CTX,    //40    -sign, REF2 y vals, previous value -ve
00155         
00156         PMODE_BIT0_CTX,     //41    -bit 0, prediction mode value
00157         PMODE_BIT1_CTX,     //42    -bin 1, prediction mode value
00158         
00159         MB_CMODE_CTX,       //43    -context for MB common block mode
00160         MB_SPLIT_BIN1_CTX,  //44    -bin1, MB split mode vals
00161         MB_SPLIT_BIN2_CTX   //45    -bin2, MB split mode vals. Bin 3 not required
00162         
00163     };
00164 
00165 
00166     //Classes used throughout the codec//
00168 
00169 
00171 
00177     class PicArray: public TwoDArray<ValueType>
00178     {
00179     public:
00181 
00184         PicArray(): TwoDArray<ValueType>(){}
00185         
00187 
00191         PicArray(int height, int width, CompSort cs=Y_COMP);
00192         
00193         //copy constructor and assignment= derived by inheritance
00194         
00196         ~PicArray(){}
00197         
00199         const CompSort& CSort() const;
00200         
00202         void SetCSort(const CompSort cs);
00203         
00204     private:
00205         
00206         CompSort m_csort;
00207     };
00208 
00209 
00211     class CostType
00212     {
00213     public:
00215         double MSE;
00216         
00218         double ENTROPY;
00219         
00221         double TOTAL;
00222     };
00223 
00224 
00226 
00231     class EntropyCorrector
00232     {
00233     public:
00235 
00239         EntropyCorrector(int depth);
00240         
00242         //NB: Assume default copy constructor, assignment = and destructor//
00244 
00246 
00250         float Factor(const int bandnum, const FrameSort fsort,const CompSort c) const;
00251 
00253 
00261         void Update(int bandnum, FrameSort fsort, CompSort c,int est_bits,int actual_bits);
00262         
00263     private:
00265         void Init();
00266 
00267         TwoDArray<float> m_Yfctrs;
00268         TwoDArray<float> m_Ufctrs;
00269         TwoDArray<float> m_Vfctrs;
00270     };
00271 
00273     class OLBParams
00274     {
00275         
00276     public:
00277         
00279         OLBParams(){}
00280         
00282         /*
00283             Constructor rationalises proposed parameters to allow suitable 
00284             overlap and fit in with chroma format
00285             \param    xblen    the horizontal block length    
00286             \param    yblen    the vertical block length
00287             \param    xblen    the horizontal block separation
00288             \param    yblen    the vertical block separation
00289 
00290         */
00291         OLBParams(const int xblen, const int yblen, 
00292                   const int xbsep, const int ybsep);
00293         
00294         // Gets ...
00295         
00297         int Xblen() const {return m_xblen;}
00298         
00300         int Yblen() const {return m_yblen;}
00301         
00303         int Xbsep() const {return m_xbsep;}
00304         
00306         int Ybsep() const {return m_ybsep;}
00307         
00309         int Xoffset() const {return m_xoffset;}
00310         
00312         int Yoffset() const {return m_yoffset;}
00313         
00314         // ... and sets
00315         
00317         void SetXblen( int xblen ){ m_xblen = xblen; m_xoffset = (m_xblen-m_xbsep)/2;}
00318         
00320         void SetYblen( int yblen ){ m_yblen = yblen; m_yoffset = (m_yblen-m_ybsep)/2;}
00321         
00323         void SetXbsep( int xbsep ){ m_xbsep = xbsep; m_xoffset = (m_xblen-m_xbsep)/2;}
00324         
00326         void SetYbsep( int ybsep ){ m_ybsep = ybsep; m_yoffset = (m_yblen-m_ybsep)/2;}
00327         
00328         // overloaded stream operators
00329         friend std::ostream & operator<< (std::ostream &, OLBParams &);
00330         friend std::istream & operator>> (std::istream &, OLBParams &);
00331         
00332         
00333     private:
00334         
00335         int m_xblen;
00336         int m_yblen;
00337         int m_xbsep;
00338         int m_ybsep;
00339         int m_xoffset;
00340         int m_yoffset;
00341     };
00342 
00344     class SeqParams
00345     {
00346     public:        
00348         SeqParams();
00349         
00351         //NB: Assume default copy constructor, assignment = and destructor//
00353         
00354         //gets ...
00356         int Xl() const {return m_xl;}
00357         
00359         int Yl() const {return m_yl;}
00360         
00362         ChromaFormat CFormat() const {return m_cformat;}
00363         
00365         int ChromaWidth() const;
00366         
00368         int ChromaHeight() const;
00369         
00371         bool Interlace() const {return m_interlace;}
00372         
00374         bool TopFieldFirst() const {return m_topfieldfirst;}
00375         
00377         int FrameRate() const {return m_framerate;}
00378         
00380         int BitstreamVersion() const {return m_bs_ver;}
00381         
00382         // ... Sets
00383         
00385         void SetXl(int xlen) {m_xl = xlen;}
00386         
00388         void SetYl(int ylen) {m_yl = ylen;}
00389         
00391         void SetCFormat(ChromaFormat cf) {m_cformat=cf;}
00392         
00394         void SetInterlace(bool ilace) {m_interlace=ilace;}
00395         
00397         void SetTopFieldFirst(bool tff) {m_topfieldfirst=tff;}
00398         
00400         void SetFrameRate(int fr){m_framerate=fr;}
00401         
00403         void SetBitstreamVersion(int bs_ver){m_bs_ver=bs_ver;}
00404         
00405     private:
00407         int m_xl;
00408         
00410         int m_yl;
00411         
00413         ChromaFormat m_cformat;
00414         
00416         bool m_interlace;
00417         
00419         bool m_topfieldfirst;
00420         
00422         int m_framerate;
00423         
00425         unsigned char  m_bs_ver;
00426     };
00427 
00429     class FrameParams 
00430     {
00431         
00432     public:
00434         FrameParams();
00435         
00437 
00440         FrameParams(const ChromaFormat& cf, int xlen, int ylen);
00441         
00443 
00446         FrameParams(const ChromaFormat& cf, const FrameSort& fs);
00447         
00449         /*
00450             All data is derived from the sequence parameters
00451         */
00452         FrameParams(const SeqParams& sparams);
00453         
00455         /*
00456            All data is derived from the sequence parameters
00457         */
00458         FrameParams(const SeqParams& sparams, const FrameSort& fs);
00459         
00461         //NB: Assume default copy constructor, assignment = and destructor//
00463         
00464         // Gets ...
00465         
00467         const ChromaFormat& CFormat() const{return m_cformat;}
00468         
00470         int Xl() const{return m_xl;}
00471         
00473         int Yl() const{return m_yl;}
00474         
00476         const FrameSort& FSort() const {return m_fsort;}
00477         
00479         int FrameNum() const {return m_fnum;}
00480         
00482         int ExpiryTime() const {return m_expiry_time;}
00483         
00485         bool Output() const {return m_output;}
00486         
00488         const std::vector<int>& Refs() const {return m_refs;}
00489         
00491         std::vector<int>& Refs(){return m_refs;}
00492         
00493         
00494         // ... Sets
00495         
00497         void SetFSort( const FrameSort& fs ){ m_fsort=fs; }
00498         
00500         void SetFrameNum( const int fn ){ m_fnum=fn; }
00501         
00503         void SetExpiryTime( const int expt ){ m_expiry_time=expt; }
00504         
00506         void SetAsOutput(){m_output=true;}
00507         
00508     private:
00509         
00511         ChromaFormat m_cformat;
00512         
00514         int m_xl;
00515         
00517         int m_yl;
00518         
00520         FrameSort m_fsort;
00521         
00523         std::vector<int> m_refs;
00524         
00526         int m_expiry_time;
00527         
00529         bool m_output;
00530         
00532         int m_fnum;        
00533     };
00534 
00535 //    //! Operator for inputting block parameters
00536 //    std::istream & operator>> (std::istream & stream, OLBParams & params);
00537 //
00538 //    //! Operator for outputting block parameters
00539 //    std::ostream & operator<< (std::ostream & stream, OLBParams & params);
00540 
00541 
00543 
00546     class CodecParams
00547     {
00548     public:
00549         
00551         CodecParams();
00552         
00554             //NB: Assume default copy constructor, assignment = and destructor//
00556         
00557         // Gets ...    
00558         
00560         int XNumMB() const {return m_x_num_mb;}
00561         
00563         int YNumMB() const {return m_y_num_mb;}
00564         
00566         int XNumBlocks() const {return m_x_num_blocks;}
00567         
00569         int YNumBlocks() const {return m_y_num_blocks;}
00570         
00572         bool Verbose() const {return m_verbose;}
00573         
00575         bool Interlace() const {return m_interlace;}
00576         
00578         bool TopFieldFirst() const {return m_topfieldfirst;}    
00579         
00581         const OLBParams& LumaBParams(int n) const {return m_lbparams[n];}
00582         
00584         const OLBParams& ChromaBParams(int n) const {return m_cbparams[n];}    
00585 
00587         int OrigXl() const {return m_orig_xl;}
00588 
00590         int OrigYl() const {return m_orig_yl;}
00591 
00593         int MVPrecision() const { return m_mv_precision; }
00594              
00595         // ... and Sets
00597         void SetXNumMB(const int xn){m_x_num_mb=xn;}    
00598         
00600         void SetYNumMB(const int yn){m_y_num_mb=yn;}
00601         
00603         void SetXNumBlocks(const int xn){m_x_num_blocks=xn;}
00604         
00606         void SetYNumBlocks(const int yn){m_y_num_blocks=yn;}
00607         
00609         void SetVerbose(bool v){m_verbose=v;}
00610         
00612         void SetInterlace(bool intlc){m_interlace=intlc;}
00613         
00615         void SetTopFieldFirst(bool topf){m_topfieldfirst=topf;}
00616         
00618         void SetBlockSizes(const OLBParams& olbparams , const ChromaFormat cformat);
00619 
00621         void SetOrigXl(const int x){m_orig_xl=x;}
00622 
00624         void SetOrigYl(const int y){m_orig_yl=y;}
00625 
00627         void SetMVPrecision(const int p)
00628         {
00629             // Assert in debug mode. Maybe we should throw an exception???
00630             TESTM((p >=0 && p <=3), "Motion precision value in range 0..3");
00631             m_mv_precision = p;
00632         }
00633 
00634     private:
00635         
00637         int m_x_num_mb;
00638         
00640         int m_y_num_mb;
00641         
00643         int m_x_num_blocks;    
00644         
00646         int m_y_num_blocks;
00647         
00649         bool m_verbose;
00650         
00652         bool m_interlace;
00653         
00655         bool m_topfieldfirst;
00656         
00657         OneDArray<OLBParams> m_lbparams;
00658         OneDArray<OLBParams> m_cbparams;
00659 
00661         int m_orig_xl;
00662 
00664         int m_orig_yl;
00665 
00667         int m_mv_precision;
00668     };
00669 
00671 
00674     class EncoderParams: public CodecParams
00675     {
00676         //codec params plus parameters relating solely to the operation of the encoder
00677         
00678     public:
00680         EncoderParams();
00681         
00683             //NB: Assume default copy constructor, assignment = and destructor//
00684             //This means pointers are copied, not the objects they point to.////       
00686         
00687          // Gets ...
00688 
00690         float Qf() const {return m_qf;}
00691 
00693 
00698         int NumL1() const {return m_num_L1;}
00699 
00701         int L1Sep() const {return m_L1_sep;}
00702 
00704         float UFactor() const {return m_ufactor;}
00705 
00707         float VFactor() const {return m_vfactor;}
00708 
00710         float CPD() const {return m_cpd;}
00711 
00713         float ILambda() const {return m_I_lambda;}
00714 
00716         float L1Lambda() const {return m_L1_lambda;}
00717 
00719         float L2Lambda() const {return m_L2_lambda;}
00720 
00722         float Lambda(const FrameSort& fsort) const;
00723 
00725         float L1MELambda() const {return m_L1_me_lambda;}
00726 
00728         float L2MELambda() const {return m_L2_me_lambda;}
00729 
00731         char * OutputPath() const {return ( char* ) m_output_path.c_str();}
00732         
00734         const EntropyCorrector& EntropyFactors() const {return *m_ent_correct;}
00735         
00737         EntropyCorrector& EntropyFactors() {return *m_ent_correct;}
00738         
00740         const SequenceOutputManager& BitsOut() const {return *m_bit_out;}
00741         
00743         SequenceOutputManager& BitsOut() {return *m_bit_out;}
00744         
00745         // ... and Sets
00746 
00748         void SetQf(const float qfac){m_qf=qfac;}
00749 
00751         void SetNumL1(const int nl){m_num_L1=nl;}
00752 
00754         void SetL1Sep(const int lsep){m_L1_sep=lsep;}
00755 
00757         void SetUFactor(const float uf){m_ufactor=uf;}
00758 
00760         void SetVFactor(const float vf){m_vfactor=vf;}
00761 
00763         void SetCPD(const float cpd){m_cpd=cpd;}
00764 
00766         void SetILambda(const float l){m_I_lambda=l;}
00767 
00769         void SetL1Lambda(const float l){m_L1_lambda=l;}
00770 
00772         void SetL2Lambda(const float l){m_L2_lambda=l;}
00773 
00775         void SetLambda(const FrameSort& fsort, const float l);
00776 
00778         void SetL1MELambda(const float l){m_L1_me_lambda=l;}
00779 
00781         void SetL2MELambda(const float l){m_L2_me_lambda=l;}
00782 
00784         void SetOutputPath(const char * op){ m_output_path = op; }
00785         
00787         void SetEntropyFactors(EntropyCorrector* entcorrect){m_ent_correct=entcorrect;}
00788         
00790         void SetBitsOut( SequenceOutputManager* so ){ m_bit_out=so; }
00791         
00792     private:
00794         float m_qf; 
00795         
00797         int m_num_L1;
00798         
00800         int m_L1_sep;    
00801         
00803         float m_ufactor;
00804         
00806         float m_vfactor;
00807         
00809         float m_cpd;
00810         
00812         float m_I_lambda;
00813 
00815         float m_L1_lambda;
00816 
00818         float m_L2_lambda;
00819         
00821         float m_L1_me_lambda;
00822 
00824         float m_L2_me_lambda; 
00825         
00827         EntropyCorrector* m_ent_correct;
00828         
00830         SequenceOutputManager* m_bit_out;   
00831         
00833         std::string m_output_path;
00834     };
00835 
00837 
00840     class DecoderParams: public CodecParams
00841     {
00842     public:
00844         DecoderParams():
00845         CodecParams(),
00846         m_bit_in(0){}
00847         
00849             //NB: Assume default copy constructor, assignment = and destructor//
00850             //This means pointers are copied, not the objects they point to.////       
00852         
00854         const BitInputManager& BitsIn() const {return *m_bit_in;}
00855         
00857         BitInputManager& BitsIn() {return *m_bit_in;}
00858         
00860         void SetBitsIn(BitInputManager* bi){m_bit_in=bi;}
00861         
00862     private:        
00864         BitInputManager* m_bit_in;
00865     };
00866 
00868     inline ValueType BChk(const ValueType &num, const ValueType &max)
00869     {
00870         if(num < 0) return 0;
00871         else if(num >= max) return max-1;
00872         else return num;
00873     }
00874 
00876     class QuantiserLists
00877     {
00878     public:
00880         QuantiserLists();
00881 
00882         inline int QuantFactor( const int index ) const {return m_qflist[index]; }
00883         inline int InverseQuantFactor( const int index ) const {return m_qfinvlist[index]; }
00884         inline int QuantOffset( const int index ) const {return m_offset[index]; }
00885     
00886 
00887     private:
00888         OneDArray<int> m_qflist;
00889         OneDArray<int> m_qfinvlist;
00890         OneDArray<int> m_offset;
00891 
00892     };
00893 
00895     static const QuantiserLists dirac_quantiser_lists;
00896 
00897 } // namespace dirac
00898 
00899 #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.