common.h

Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002 *
00003 * $Id: common.h,v 1.2 2005/01/30 05:11:40 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 *                 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         ZTz_CTX,            //16    -zerotree, neighbouring symbols are zerotree elements
00102         ZTnz_CTX,           //17    -zerotree, neighbouring symbols are not zerotree elements
00103         ZTzb_CTX,           //16    -zerotree, neighbouring symbols are zerotree elements
00104         ZTnzb_CTX           //17    -zerotree, neighbouring symbols are not zerotree elements
00105     };
00106 
00108     enum MvCtxAliases
00109     {
00110         
00111         YDC_BIN1_CTX,       //0     -1st bin of DC value for Y
00112         YDC_BIN2plus_CTX,   //1     -remaining DC bins
00113         YDC_SIGN0_CTX,      //2     -sign of Y DC value, previous value 0
00114         UDC_BIN1_CTX,       //3     --ditto
00115         UDC_BIN2plus_CTX,   //4     --for
00116         UDC_SIGN0_CTX,      //5     --U
00117         VDC_BIN1_CTX,       //6     --and
00118         VDC_BIN2plus_CTX,   //7     --V
00119         VDC_SIGN0_CTX,      //8     --components
00120         
00121         REF1x_BIN1_CTX,     //9     -bin 1, REF1 x vals
00122         REF1x_BIN2_CTX,     //10    -bin 2, REF1 x vals
00123         REF1x_BIN3_CTX,     //11    -bin 3, REF1 x vals
00124         REF1x_BIN4_CTX,     //12    -bin 4, REF1 x vals
00125         REF1x_BIN5plus_CTX, //13    -bin 5, REF1 x vals
00126         REF1x_SIGN0_CTX,    //14    -sign, REF1 x vals, previous value 0
00127         REF1x_SIGNP_CTX,    //15    -sign, REF1 x vals, previous value +ve
00128         REF1x_SIGNN_CTX,    //16    -sign, REF1 x vals, previous value -ve
00129         
00130         REF1y_BIN1_CTX,     //17    -bin 1, REF1 y vals
00131         REF1y_BIN2_CTX,     //18    -bin 2, REF1 y vals
00132         REF1y_BIN3_CTX,     //19    -bin 3, REF1 y vals
00133         REF1y_BIN4_CTX,     //20    -bin 4, REF1 y vals
00134         REF1y_BIN5plus_CTX, //21    -bin 5, REF1 y vals
00135         REF1y_SIGN0_CTX,    //22    -sign, REF1 y vals, previous value 0
00136         REF1y_SIGNP_CTX,    //23    -sign, REF1 y vals, previous value +ve
00137         REF1y_SIGNN_CTX,    //24    -sign, REF1 y vals, previous value -ve
00138         
00139         REF2x_BIN1_CTX,     //25    -bin 1, REF2 x vals
00140         REF2x_BIN2_CTX,     //26    -bin 2, REF2 x vals
00141         REF2x_BIN3_CTX,     //27    -bin 3, REF2 x vals
00142         REF2x_BIN4_CTX,     //28    -bin 4, REF2 x vals
00143         REF2x_BIN5plus_CTX, //29    -bin 5, REF2 x vals
00144         REF2x_SIGN0_CTX,    //30    -sign, REF2 x vals, previous value 0
00145         REF2x_SIGNP_CTX,    //31    -sign, REF1 y vals, previous value +ve
00146         REF2x_SIGNN_CTX,    //32    -sign, REF1 y vals, previous value -ve
00147         
00148         REF2y_BIN1_CTX,     //33    -bin 1, REF2 y vals
00149         REF2y_BIN2_CTX,     //34    -bin 2, REF2 y vals
00150         REF2y_BIN3_CTX,     //35    -bin 3, REF2 y vals
00151         REF2y_BIN4_CTX,     //36    -bin 4, REF2 y vals
00152         REF2y_BIN5plus_CTX, //37    -bin 5, REF2 y vals
00153         REF2y_SIGN0_CTX,    //38    -sign, REF2 y vals, previous value 0
00154         REF2y_SIGNP_CTX,    //39    -sign, REF2 y vals, previous value +ve
00155         REF2y_SIGNN_CTX,    //40    -sign, REF2 y vals, previous value -ve
00156         
00157         PMODE_BIN1_CTX,     //41    -bin 1, prediction mode value
00158         PMODE_BIN2_CTX,     //42    -bin 2, prediction mode value
00159         PMODE_BIN3_CTX,     //43    -bin 3, prediction mode value. Bin 4 not required
00160         
00161         MB_CMODE_CTX,       //44    -context for MB common block mode
00162         MB_SPLIT_BIN1_CTX,  //45    -bin1, MB split mode vals
00163         MB_SPLIT_BIN2_CTX   //46    -bin2, MB split mode vals. Bin 3 not required
00164         
00165     };
00166 
00167 
00168     //Classes used throughout the codec//
00170 
00171 
00173 
00179     class PicArray: public TwoDArray<ValueType>
00180     {
00181     public:
00183 
00186         PicArray(): TwoDArray<ValueType>(){}
00187         
00189 
00193         PicArray(int height, int width, CompSort cs=Y_COMP);
00194         
00195         //copy constructor and assignment= derived by inheritance
00196         
00198         ~PicArray(){}
00199         
00201         const CompSort& CSort() const;
00202         
00204         void SetCSort(const CompSort cs);
00205         
00206     private:
00207         
00208         CompSort m_csort;
00209     };
00210 
00211 
00213     class CostType
00214     {
00215     public:
00217         double MSE;
00218         
00220         double ENTROPY;
00221         
00223         double TOTAL;
00224     };
00225 
00226 
00228 
00233     class EntropyCorrector
00234     {
00235     public:
00237 
00241         EntropyCorrector(int depth);
00242         
00244         //NB: Assume default copy constructor, assignment = and destructor//
00246 
00248 
00252         float Factor(const int bandnum, const FrameSort fsort,const CompSort c) const;
00253 
00255 
00263         void Update(int bandnum, FrameSort fsort, CompSort c,int est_bits,int actual_bits);
00264         
00265     private:
00267         void Init();
00268 
00269         TwoDArray<float> m_Yfctrs;
00270         TwoDArray<float> m_Ufctrs;
00271         TwoDArray<float> m_Vfctrs;
00272     };
00273 
00275     class OLBParams
00276     {
00277         
00278     public:
00279         
00281         OLBParams(){}
00282         
00284         /*
00285             Constructor rationalises proposed parameters to allow suitable 
00286             overlap and fit in with
00287             chroma format
00288             \param    xblen    the horizontal block length    
00289             \param    yblen    the vertical block length
00290             \param    xblen    the horizontal block separation
00291             \param    yblen    the vertical block separation
00292 
00293         */
00294         OLBParams(const int xblen, int const yblen, int const xbsep, int const ybsep);
00295         
00296         // Gets ...
00297         
00299         int Xblen() const {return m_xblen;}
00300         
00302         int Yblen() const {return m_yblen;}
00303         
00305         int Xbsep() const {return m_xbsep;}
00306         
00308         int Ybsep() const {return m_ybsep;}
00309         
00311         int Xoffset() const {return m_xoffset;}
00312         
00314         int Yoffset() const {return m_yoffset;}
00315         
00316         // ... and sets
00317         
00319         void SetXblen( int xblen ){ m_xblen = xblen; m_xoffset = (m_xblen-m_xbsep)/2;}
00320         
00322         void SetYblen( int yblen ){ m_yblen = yblen; m_yoffset = (m_yblen-m_ybsep)/2;}
00323         
00325         void SetXbsep( int xbsep ){ m_xbsep = xbsep; m_xoffset = (m_xblen-m_xbsep)/2;}
00326         
00328         void SetYbsep( int ybsep ){ m_ybsep = ybsep; m_yoffset = (m_yblen-m_ybsep)/2;}
00329         
00330         // overloaded stream operators
00331         friend std::ostream & operator<< (std::ostream &, OLBParams &);
00332         friend std::istream & operator>> (std::istream &, OLBParams &);
00333         
00334         
00335     private:
00336         
00337         int m_xblen;
00338         int m_yblen;
00339         int m_xbsep;
00340         int m_ybsep;
00341         int m_xoffset;
00342         int m_yoffset;
00343     };
00344 
00346     class SeqParams
00347     {
00348     public:        
00350         SeqParams();
00351         
00353         //NB: Assume default copy constructor, assignment = and destructor//
00355         
00356         //gets ...
00358         int Xl() const {return m_xl;}
00359         
00361         int Yl() const {return m_yl;}
00362         
00364         ChromaFormat CFormat() const {return m_cformat;}
00365         
00367         int ChromaWidth() const;
00368         
00370         int ChromaHeight() const;
00371         
00373         bool Interlace() const {return m_interlace;}
00374         
00376         bool TopFieldFirst() const {return m_topfieldfirst;}
00377         
00379         int FrameRate() const {return m_framerate;}
00380         
00382         int BitstreamVersion() const {return m_bs_ver;}
00383         
00384         // ... Sets
00385         
00387         void SetXl(int xlen) {m_xl = xlen;}
00388         
00390         void SetYl(int ylen) {m_yl = ylen;}
00391         
00393         void SetCFormat(ChromaFormat cf) {m_cformat=cf;}
00394         
00396         void SetInterlace(bool ilace) {m_interlace=ilace;}
00397         
00399         void SetTopFieldFirst(bool tff) {m_topfieldfirst=tff;}
00400         
00402         void SetFrameRate(int fr){m_framerate=fr;}
00403         
00405         void SetBitstreamVersion(int bs_ver){m_bs_ver=bs_ver;}
00406         
00407     private:
00409         int m_xl;
00410         
00412         int m_yl;
00413         
00415         ChromaFormat m_cformat;
00416         
00418         bool m_interlace;
00419         
00421         bool m_topfieldfirst;
00422         
00424         int m_framerate;
00425         
00427         unsigned char  m_bs_ver;
00428     };
00429 
00431     class FrameParams 
00432     {
00433         
00434     public:
00436         FrameParams();
00437         
00439 
00442         FrameParams(const ChromaFormat& cf, int xlen, int ylen);
00443         
00445 
00448         FrameParams(const ChromaFormat& cf, const FrameSort& fs);
00449         
00451         /*
00452             All data is derived from the sequence parameters
00453         */
00454         FrameParams(const SeqParams& sparams);
00455         
00457         /*
00458            All data is derived from the sequence parameters
00459         */
00460         FrameParams(const SeqParams& sparams, const FrameSort& fs);
00461         
00463         //NB: Assume default copy constructor, assignment = and destructor//
00465         
00466         // Gets ...
00467         
00469         const ChromaFormat& CFormat() const{return m_cformat;}
00470         
00472         int Xl() const{return m_xl;}
00473         
00475         int Yl() const{return m_yl;}
00476         
00478         const FrameSort& FSort() const {return m_fsort;}
00479         
00481         int FrameNum() const {return m_fnum;}
00482         
00484         int ExpiryTime() const {return m_expiry_time;}
00485         
00487         bool Output() const {return m_output;}
00488         
00490         const std::vector<int>& Refs() const {return m_refs;}
00491         
00493         std::vector<int>& Refs(){return m_refs;}
00494         
00495         
00496         // ... Sets
00497         
00499         void SetFSort( const FrameSort& fs ){ m_fsort=fs; }
00500         
00502         void SetFrameNum( const int fn ){ m_fnum=fn; }
00503         
00505         void SetExpiryTime( const int expt ){ m_expiry_time=expt; }
00506         
00508         void SetAsOutput(){m_output=true;}
00509         
00510     private:
00511         
00513         ChromaFormat m_cformat;
00514         
00516         int m_xl;
00517         
00519         int m_yl;
00520         
00522         FrameSort m_fsort;
00523         
00525         std::vector<int> m_refs;
00526         
00528         int m_expiry_time;
00529         
00531         bool m_output;
00532         
00534         int m_fnum;        
00535     };
00536 
00537 
00539 
00542     class CodecParams
00543     {
00544     public:
00545         
00547         CodecParams();
00548         
00550             //NB: Assume default copy constructor, assignment = and destructor//
00552         
00553         // Gets ...    
00554         
00556         int XNumMB() const {return m_x_num_mb;}
00557         
00559         int YNumMB() const {return m_y_num_mb;}
00560         
00562         int XNumBlocks() const {return m_x_num_blocks;}
00563         
00565         int YNumBlocks() const {return m_y_num_blocks;}
00566         
00568         bool Verbose() const {return m_verbose;}
00569         
00571         bool Interlace() const {return m_interlace;}
00572         
00574         bool TopFieldFirst() const {return m_topfieldfirst;}    
00575         
00577         const OLBParams& LumaBParams(int n) const {return m_lbparams[n];}
00578         
00580         const OLBParams& ChromaBParams(int n) const {return m_cbparams[n];}    
00581 
00583         int OrigXl() const {return m_orig_xl;}
00584 
00586         int OrigYl() const {return m_orig_yl;}
00587         
00588         // ... and Sets
00590         void SetXNumMB(const int xn){m_x_num_mb=xn;}    
00591         
00593         void SetYNumMB(const int yn){m_y_num_mb=yn;}
00594         
00596         void SetXNumBlocks(const int xn){m_x_num_blocks=xn;}
00597         
00599         void SetYNumBlocks(const int yn){m_y_num_blocks=yn;}
00600         
00602         void SetVerbose(bool v){m_verbose=v;}
00603         
00605         void SetInterlace(bool intlc){m_interlace=intlc;}
00606         
00608         void SetTopFieldFirst(bool topf){m_topfieldfirst=topf;}
00609         
00611         void SetBlockSizes(const OLBParams& olbparams , const ChromaFormat cformat);
00612 
00614         void SetOrigXl(const int x){m_orig_xl=x;}
00615 
00617         void SetOrigYl(const int y){m_orig_yl=y;}
00618 
00619     private:
00620         
00622         int m_x_num_mb;
00623         
00625         int m_y_num_mb;
00626         
00628         int m_x_num_blocks;    
00629         
00631         int m_y_num_blocks;
00632         
00634         bool m_verbose;
00635         
00637         bool m_interlace;
00638         
00640         bool m_topfieldfirst;
00641         
00642         OneDArray<OLBParams> m_lbparams;
00643         OneDArray<OLBParams> m_cbparams;
00644 
00646         int m_orig_xl;
00647 
00649         int m_orig_yl;
00650     };
00651 
00653 
00656     class EncoderParams: public CodecParams
00657     {
00658         //codec params plus parameters relating solely to the operation of the encoder
00659         
00660     public:
00662         EncoderParams();
00663         
00665             //NB: Assume default copy constructor, assignment = and destructor//
00666             //This means pointers are copied, not the objects they point to.////       
00668         
00669          // Gets ...
00670 
00672         float Qf() const {return m_qf;}
00673 
00675 
00680         int NumL1() const {return m_num_L1;}
00681 
00683         int L1Sep() const {return m_L1_sep;}
00684 
00686         float UFactor() const {return m_ufactor;}
00687 
00689         float VFactor() const {return m_vfactor;}
00690 
00692         float CPD() const {return m_cpd;}
00693 
00695         float ILambda() const {return m_I_lambda;}
00696 
00698         float L1Lambda() const {return m_L1_lambda;}
00699 
00701         float L2Lambda() const {return m_L2_lambda;}
00702 
00704         float Lambda(const FrameSort& fsort) const;
00705 
00707         float L1MELambda() const {return m_L1_me_lambda;}
00708 
00710         float L2MELambda() const {return m_L2_me_lambda;}
00711 
00713         char * OutputPath() const {return ( char* ) m_output_path.c_str();}
00714         
00716         const EntropyCorrector& EntropyFactors() const {return *m_ent_correct;}
00717         
00719         EntropyCorrector& EntropyFactors() {return *m_ent_correct;}
00720         
00722         const SequenceOutputManager& BitsOut() const {return *m_bit_out;}
00723         
00725         SequenceOutputManager& BitsOut() {return *m_bit_out;}
00726         
00727         // ... and Sets
00728 
00730         void SetQf(const float qfac){m_qf=qfac;}
00731 
00733         void SetNumL1(const int nl){m_num_L1=nl;}
00734 
00736         void SetL1Sep(const int lsep){m_L1_sep=lsep;}
00737 
00739         void SetUFactor(const float uf){m_ufactor=uf;}
00740 
00742         void SetVFactor(const float vf){m_vfactor=vf;}
00743 
00745         void SetCPD(const float cpd){m_cpd=cpd;}
00746 
00748         void SetILambda(const float l){m_I_lambda=l;}
00749 
00751         void SetL1Lambda(const float l){m_L1_lambda=l;}
00752 
00754         void SetL2Lambda(const float l){m_L2_lambda=l;}
00755 
00757         void SetLambda(const FrameSort& fsort, const float l);
00758 
00760         void SetL1MELambda(const float l){m_L1_me_lambda=l;}
00761 
00763         void SetL2MELambda(const float l){m_L2_me_lambda=l;}
00764 
00766         void SetOutputPath(const char * op){ m_output_path = op; }
00767         
00769         void SetEntropyFactors(EntropyCorrector* entcorrect){m_ent_correct=entcorrect;}
00770         
00772         void SetBitsOut( SequenceOutputManager* so ){ m_bit_out=so; }
00773         
00774     private:
00776         float m_qf; 
00777         
00779         int m_num_L1;
00780         
00782         int m_L1_sep;    
00783         
00785         float m_ufactor;
00786         
00788         float m_vfactor;
00789         
00791         float m_cpd;
00792         
00794         float m_I_lambda;
00795 
00797         float m_L1_lambda;
00798 
00800         float m_L2_lambda;
00801         
00803         float m_L1_me_lambda;
00804 
00806         float m_L2_me_lambda; 
00807         
00809         EntropyCorrector* m_ent_correct;
00810         
00812         SequenceOutputManager* m_bit_out;   
00813         
00815         std::string m_output_path;
00816     };
00817 
00819 
00822     class DecoderParams: public CodecParams
00823     {
00824     public:
00826         DecoderParams():
00827         CodecParams(),
00828         m_bit_in(0){}
00829         
00831             //NB: Assume default copy constructor, assignment = and destructor//
00832             //This means pointers are copied, not the objects they point to.////       
00834         
00836         const BitInputManager& BitsIn() const {return *m_bit_in;}
00837         
00839         BitInputManager& BitsIn() {return *m_bit_in;}
00840         
00842         void SetBitsIn(BitInputManager* bi){m_bit_in=bi;}
00843         
00844     private:        
00846         BitInputManager* m_bit_in;
00847     };
00848 
00850     inline ValueType BChk(const ValueType &num, const ValueType &max)
00851     {
00852         if(num < 0) return 0;
00853         else if(num >= max) return max-1;
00854         else return num;
00855     }
00856 
00857 } // namespace dirac
00858 
00859 #endif

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