00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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
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 {
00081 SIGN0_CTX,
00082 SIGN_POS_CTX,
00083 SIGN_NEG_CTX,
00084
00085
00086 Z_BIN1z_CTX,
00087 Z_BIN1nz_CTX,
00088 Z_BIN2_CTX,
00089 Z_BIN3_CTX,
00090 Z_BIN4_CTX,
00091 Z_BIN5plus_CTX,
00092
00093 NZ_BIN1z_CTX,
00094 NZ_BIN1a_CTX,
00095 NZ_BIN1b_CTX,
00096 NZ_BIN2_CTX,
00097 NZ_BIN3_CTX,
00098 NZ_BIN4_CTX,
00099 NZ_BIN5plus_CTX,
00100
00101 ZTz_CTX,
00102 ZTnz_CTX,
00103 ZTzb_CTX,
00104 ZTnzb_CTX
00105 };
00106
00108 enum MvCtxAliases
00109 {
00110
00111 YDC_BIN1_CTX,
00112 YDC_BIN2plus_CTX,
00113 YDC_SIGN0_CTX,
00114 UDC_BIN1_CTX,
00115 UDC_BIN2plus_CTX,
00116 UDC_SIGN0_CTX,
00117 VDC_BIN1_CTX,
00118 VDC_BIN2plus_CTX,
00119 VDC_SIGN0_CTX,
00120
00121 REF1x_BIN1_CTX,
00122 REF1x_BIN2_CTX,
00123 REF1x_BIN3_CTX,
00124 REF1x_BIN4_CTX,
00125 REF1x_BIN5plus_CTX,
00126 REF1x_SIGN0_CTX,
00127 REF1x_SIGNP_CTX,
00128 REF1x_SIGNN_CTX,
00129
00130 REF1y_BIN1_CTX,
00131 REF1y_BIN2_CTX,
00132 REF1y_BIN3_CTX,
00133 REF1y_BIN4_CTX,
00134 REF1y_BIN5plus_CTX,
00135 REF1y_SIGN0_CTX,
00136 REF1y_SIGNP_CTX,
00137 REF1y_SIGNN_CTX,
00138
00139 REF2x_BIN1_CTX,
00140 REF2x_BIN2_CTX,
00141 REF2x_BIN3_CTX,
00142 REF2x_BIN4_CTX,
00143 REF2x_BIN5plus_CTX,
00144 REF2x_SIGN0_CTX,
00145 REF2x_SIGNP_CTX,
00146 REF2x_SIGNN_CTX,
00147
00148 REF2y_BIN1_CTX,
00149 REF2y_BIN2_CTX,
00150 REF2y_BIN3_CTX,
00151 REF2y_BIN4_CTX,
00152 REF2y_BIN5plus_CTX,
00153 REF2y_SIGN0_CTX,
00154 REF2y_SIGNP_CTX,
00155 REF2y_SIGNN_CTX,
00156
00157 PMODE_BIN1_CTX,
00158 PMODE_BIN2_CTX,
00159 PMODE_BIN3_CTX,
00160
00161 MB_CMODE_CTX,
00162 MB_SPLIT_BIN1_CTX,
00163 MB_SPLIT_BIN2_CTX
00164
00165 };
00166
00167
00168
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
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
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
00286
00287
00288
00289
00290
00291
00292
00293
00294 OLBParams(const int xblen, int const yblen, int const xbsep, int const ybsep);
00295
00296
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
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
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
00355
00356
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
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
00453
00454 FrameParams(const SeqParams& sparams);
00455
00457
00458
00459
00460 FrameParams(const SeqParams& sparams, const FrameSort& fs);
00461
00463
00465
00466
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
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
00552
00553
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
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
00659
00660 public:
00662 EncoderParams();
00663
00665
00666
00668
00669
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
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
00832
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 }
00858
00859 #endif