Created by the British Broadcasting Corporation.
00001 /* ***** BEGIN LICENSE BLOCK ***** 00002 * 00003 * $Id: common.h,v 1.14 2004/09/21 10:14:27 asuraparaju Exp $ $Name: Dirac_0_4_3 $ 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 00056 //Some basic enumeration types used throughout the codec ...// 00058 00060 enum PredMode{ INTRA , REF1_ONLY , REF2_ONLY , REF1AND2 }; 00061 00063 enum CompSort{ Y_COMP , U_COMP , V_COMP , R_COMP , G_COMP , B_COMP }; 00064 00066 enum AddOrSub{ ADD , SUBTRACT }; 00067 00069 enum Direction { FORWARD , BACKWARD }; 00070 00072 enum WltFilter { DAUB , HAAR }; 00073 00075 enum CtxAliases 00076 {//used for residual coding 00077 SIGN0_CTX, //0 -sign, previous symbol is 0 00078 SIGN_POS_CTX, //1 -sign, previous symbol is +ve 00079 SIGN_NEG_CTX, //2 -sign, previous symbol is -ve 00080 00081 00082 Z_BIN1z_CTX, //3 -bin 1, parent is zero, neighbours zero 00083 Z_BIN1nz_CTX, //4 -bin 1, parent is zero, neighbours non-zero 00084 Z_BIN2_CTX, //5 -bin 2, parent is zero 00085 Z_BIN3_CTX, //6 -bin 3, parent is zero 00086 Z_BIN4_CTX, //7 -bin 4, parent is zero 00087 Z_BIN5plus_CTX, //8 -bins 5 plus, parent is zero 00088 00089 NZ_BIN1z_CTX, //9 -bin 1, parent is non-zero, neighbours zero 00090 NZ_BIN1a_CTX, //10 -bin 1, parent is non-zero, neighbours small 00091 NZ_BIN1b_CTX, //11 -bin 1, parent is non-zero, neighbours large 00092 NZ_BIN2_CTX, //12 -bin 2, parent is non-zero 00093 NZ_BIN3_CTX, //13 -bin 3, parent is non-zero 00094 NZ_BIN4_CTX, //14 -bin 4, parent is non-zero 00095 NZ_BIN5plus_CTX, //15 -bins 5 plus, parent is non-zero 00096 00097 ZTz_CTX, //16 -zerotree, neighbouring symbols are zerotree elements 00098 ZTnz_CTX, //17 -zerotree, neighbouring symbols are not zerotree elements 00099 ZTzb_CTX, //16 -zerotree, neighbouring symbols are zerotree elements 00100 ZTnzb_CTX //17 -zerotree, neighbouring symbols are not zerotree elements 00101 }; 00102 00104 enum MvCtxAliases 00105 { 00106 00107 YDC_BIN1_CTX, //0 -1st bin of DC value for Y 00108 YDC_BIN2plus_CTX, //1 -remaining DC bins 00109 YDC_SIGN0_CTX, //2 -sign of Y DC value, previous value 0 00110 UDC_BIN1_CTX, //3 --ditto 00111 UDC_BIN2plus_CTX, //4 --for 00112 UDC_SIGN0_CTX, //5 --U 00113 VDC_BIN1_CTX, //6 --and 00114 VDC_BIN2plus_CTX, //7 --V 00115 VDC_SIGN0_CTX, //8 --components 00116 00117 REF1x_BIN1_CTX, //9 -bin 1, REF1 x vals 00118 REF1x_BIN2_CTX, //10 -bin 2, REF1 x vals 00119 REF1x_BIN3_CTX, //11 -bin 3, REF1 x vals 00120 REF1x_BIN4_CTX, //12 -bin 4, REF1 x vals 00121 REF1x_BIN5plus_CTX, //13 -bin 5, REF1 x vals 00122 REF1x_SIGN0_CTX, //14 -sign, REF1 x vals, previous value 0 00123 REF1x_SIGNP_CTX, //15 -sign, REF1 x vals, previous value +ve 00124 REF1x_SIGNN_CTX, //16 -sign, REF1 x vals, previous value -ve 00125 00126 REF1y_BIN1_CTX, //17 -bin 1, REF1 y vals 00127 REF1y_BIN2_CTX, //18 -bin 2, REF1 y vals 00128 REF1y_BIN3_CTX, //19 -bin 3, REF1 y vals 00129 REF1y_BIN4_CTX, //20 -bin 4, REF1 y vals 00130 REF1y_BIN5plus_CTX, //21 -bin 5, REF1 y vals 00131 REF1y_SIGN0_CTX, //22 -sign, REF1 y vals, previous value 0 00132 REF1y_SIGNP_CTX, //23 -sign, REF1 y vals, previous value +ve 00133 REF1y_SIGNN_CTX, //24 -sign, REF1 y vals, previous value -ve 00134 00135 REF2x_BIN1_CTX, //25 -bin 1, REF2 x vals 00136 REF2x_BIN2_CTX, //26 -bin 2, REF2 x vals 00137 REF2x_BIN3_CTX, //27 -bin 3, REF2 x vals 00138 REF2x_BIN4_CTX, //28 -bin 4, REF2 x vals 00139 REF2x_BIN5plus_CTX, //29 -bin 5, REF2 x vals 00140 REF2x_SIGN0_CTX, //30 -sign, REF2 x vals, previous value 0 00141 REF2x_SIGNP_CTX, //31 -sign, REF1 y vals, previous value +ve 00142 REF2x_SIGNN_CTX, //32 -sign, REF1 y vals, previous value -ve 00143 00144 REF2y_BIN1_CTX, //33 -bin 1, REF2 y vals 00145 REF2y_BIN2_CTX, //34 -bin 2, REF2 y vals 00146 REF2y_BIN3_CTX, //35 -bin 3, REF2 y vals 00147 REF2y_BIN4_CTX, //36 -bin 4, REF2 y vals 00148 REF2y_BIN5plus_CTX, //37 -bin 5, REF2 y vals 00149 REF2y_SIGN0_CTX, //38 -sign, REF2 y vals, previous value 0 00150 REF2y_SIGNP_CTX, //39 -sign, REF2 y vals, previous value +ve 00151 REF2y_SIGNN_CTX, //40 -sign, REF2 y vals, previous value -ve 00152 00153 PMODE_BIN1_CTX, //41 -bin 1, prediction mode value 00154 PMODE_BIN2_CTX, //42 -bin 2, prediction mode value 00155 PMODE_BIN3_CTX, //43 -bin 3, prediction mode value. Bin 4 not required 00156 00157 MB_CMODE_CTX, //44 -context for MB common block mode 00158 MB_SPLIT_BIN1_CTX, //45 -bin1, MB split mode vals 00159 MB_SPLIT_BIN2_CTX //46 -bin2, MB split mode vals. Bin 3 not required 00160 00161 }; 00162 00163 00164 //Classes used throughout the codec// 00166 00167 00169 00174 class PicArray: public TwoDArray<ValueType> 00175 { 00176 public: 00178 00181 PicArray(): TwoDArray<ValueType>(){} 00182 00184 00187 PicArray(int height, int width, CompSort cs=Y_COMP); 00188 00189 //copy constructor and assignment= derived by inheritance 00190 00192 ~PicArray(){} 00193 00195 const CompSort& CSort() const; 00196 00198 void SetCSort(const CompSort cs); 00199 00200 private: 00201 00202 CompSort m_csort; 00203 }; 00204 00205 00207 class CostType 00208 { 00209 public: 00211 double MSE; 00212 00214 double ENTROPY; 00215 00217 double TOTAL; 00218 }; 00219 00220 00222 00226 class EntropyCorrector 00227 { 00228 public: 00230 00234 EntropyCorrector(int depth); 00235 00237 //NB: Assume default copy constructor, assignment = and destructor// 00239 00241 00244 float Factor(const int bandnum, const FrameSort fsort,const CompSort c) const; 00245 00247 00252 void Update(int bandnum, FrameSort fsort, CompSort c,int est_bits,int actual_bits); 00253 00254 private: 00256 void Init(); 00257 00258 TwoDArray<float> m_Yfctrs; 00259 TwoDArray<float> m_Ufctrs; 00260 TwoDArray<float> m_Vfctrs; 00261 }; 00262 00264 class OLBParams 00265 { 00266 00267 public: 00268 00270 OLBParams(){} 00271 00273 /* 00274 Constructor rationalises proposed parameters to allow suitable overlap and fit in with 00275 chroma format 00276 \param xblen the horizontal block length 00277 \param yblen the vertical block length 00278 \param xblen the horizontal block separation 00279 \param yblen the vertical block separation 00280 00281 */ 00282 OLBParams(const int xblen, int const yblen, int const xbsep, int const ybsep); 00283 00284 // Gets ... 00285 00287 int Xblen() const {return m_xblen;} 00288 00290 int Yblen() const {return m_yblen;} 00291 00293 int Xbsep() const {return m_xbsep;} 00294 00296 int Ybsep() const {return m_ybsep;} 00297 00299 int Xoffset() const {return m_xoffset;} 00300 00302 int Yoffset() const {return m_yoffset;} 00303 00304 // ... and sets 00305 00307 void SetXblen( int xblen ){ m_xblen = xblen; m_xoffset = (m_xblen-m_xbsep)/2;} 00308 00310 void SetYblen( int yblen ){ m_yblen = yblen; m_yoffset = (m_yblen-m_ybsep)/2;} 00311 00313 void SetXbsep( int xbsep ){ m_xbsep = xbsep; m_xoffset = (m_xblen-m_xbsep)/2;} 00314 00316 void SetYbsep( int ybsep ){ m_ybsep = ybsep; m_yoffset = (m_yblen-m_ybsep)/2;} 00317 00318 // overloaded stream operators 00319 friend std::ostream & operator<< (std::ostream &, OLBParams &); 00320 friend std::istream & operator>> (std::istream &, OLBParams &); 00321 00322 00323 private: 00324 00325 int m_xblen; 00326 int m_yblen; 00327 int m_xbsep; 00328 int m_ybsep; 00329 int m_xoffset; 00330 int m_yoffset; 00331 }; 00332 00334 class SeqParams 00335 { 00336 public: 00338 SeqParams(); 00339 00341 //NB: Assume default copy constructor, assignment = and destructor// 00343 00344 //gets ... 00346 int Xl() const {return m_xl;} 00347 00349 int Yl() const {return m_yl;} 00350 00352 int Zl() const {return m_zl;} 00353 00355 ChromaFormat CFormat() const {return m_cformat;} 00356 00358 bool Interlace() const {return m_interlace;} 00359 00361 bool TopFieldFirst() const {return m_topfieldfirst;} 00362 00364 int FrameRate() const {return m_framerate;} 00365 00367 int BitstreamVersion() const {return m_bs_ver;} 00368 00369 // ... Sets 00370 00372 void SetXl(int xlen) {m_xl = xlen;} 00373 00375 void SetYl(int ylen) {m_yl = ylen;} 00376 00378 void SetZl(int zlen) {m_zl = zlen;} 00379 00381 void SetCFormat(ChromaFormat cf) {m_cformat=cf;} 00382 00384 void SetInterlace(bool ilace) {m_interlace=ilace;} 00385 00387 void SetTopFieldFirst(bool tff) {m_topfieldfirst=tff;} 00388 00390 void SetFrameRate(int fr){m_framerate=fr;} 00391 00393 void SetBitstreamVersion(int bs_ver){m_bs_ver=bs_ver;} 00394 00395 private: 00397 int m_xl; 00398 00400 int m_yl; 00401 00403 int m_zl; 00404 00406 ChromaFormat m_cformat; 00407 00409 bool m_interlace; 00410 00412 bool m_topfieldfirst; 00413 00415 int m_framerate; 00416 00418 unsigned char m_bs_ver; 00419 }; 00420 00422 class FrameParams 00423 { 00424 00425 public: 00427 FrameParams(); 00428 00430 00433 FrameParams(const ChromaFormat& cf, int xlen, int ylen); 00434 00436 00439 FrameParams(const ChromaFormat& cf, const FrameSort& fs); 00440 00442 /* 00443 All data is derived from the sequence parameters 00444 */ 00445 FrameParams(const SeqParams& sparams); 00446 00448 /* 00449 All data is derived from the sequence parameters 00450 */ 00451 FrameParams(const SeqParams& sparams, const FrameSort& fs); 00452 00454 //NB: Assume default copy constructor, assignment = and destructor// 00456 00457 // Gets ... 00458 00460 const ChromaFormat& CFormat() const{return m_cformat;} 00461 00463 int Xl() const{return m_xl;} 00464 00466 int Yl() const{return m_yl;} 00467 00469 const FrameSort& FSort() const {return m_fsort;} 00470 00472 int FrameNum() const {return m_fnum;} 00473 00475 int ExpiryTime() const {return m_expiry_time;} 00476 00478 bool Output() const {return m_output;} 00479 00481 const std::vector<int>& Refs() const {return m_refs;} 00482 00484 std::vector<int>& Refs(){return m_refs;} 00485 00486 00487 // ... Sets 00488 00490 void SetFSort( const FrameSort& fs ){ m_fsort=fs; } 00491 00493 void SetFrameNum( const int fn ){ m_fnum=fn; } 00494 00496 void SetExpiryTime( const int expt ){ m_expiry_time=expt; } 00497 00499 void SetAsOutput(){m_output=true;} 00500 00501 private: 00502 00504 ChromaFormat m_cformat; 00505 00507 int m_xl; 00508 00510 int m_yl; 00511 00513 FrameSort m_fsort; 00514 00516 std::vector<int> m_refs; 00517 00519 int m_expiry_time; 00520 00522 bool m_output; 00523 00525 int m_fnum; 00526 }; 00527 00528 00530 00533 class CodecParams 00534 { 00535 public: 00536 00538 CodecParams(); 00539 00541 //NB: Assume default copy constructor, assignment = and destructor// 00543 00544 // Gets ... 00545 00547 int XNumMB() const {return m_x_num_mb;} 00548 00550 int YNumMB() const {return m_y_num_mb;} 00551 00553 int XNumBlocks() const {return m_x_num_blocks;} 00554 00556 int YNumBlocks() const {return m_y_num_blocks;} 00557 00559 bool Verbose() const {return m_verbose;} 00560 00562 bool Interlace() const {return m_interlace;} 00563 00565 bool TopFieldFirst() const {return m_topfieldfirst;} 00566 00568 const OLBParams& LumaBParams(int n) const {return m_lbparams[n];} 00569 00571 const OLBParams& ChromaBParams(int n) const {return m_cbparams[n];} 00572 00573 // ... and Sets 00575 void SetXNumMB(const int xn){m_x_num_mb=xn;} 00576 00578 void SetYNumMB(const int yn){m_y_num_mb=yn;} 00579 00581 void SetXNumBlocks(const int xn){m_x_num_blocks=xn;} 00582 00584 void SetYNumBlocks(const int yn){m_y_num_blocks=yn;} 00585 00587 void SetVerbose(bool v){m_verbose=v;} 00588 00590 void SetInterlace(bool intlc){m_interlace=intlc;} 00591 00593 void SetTopFieldFirst(bool topf){m_topfieldfirst=topf;} 00594 00596 void SetBlockSizes(const OLBParams& olbparams , const ChromaFormat cformat); 00597 00598 private: 00599 00601 int m_x_num_mb; 00602 00604 int m_y_num_mb; 00605 00607 int m_x_num_blocks; 00608 00610 int m_y_num_blocks; 00611 00613 bool m_verbose; 00614 00616 bool m_interlace; 00617 00619 bool m_topfieldfirst; 00620 00621 OneDArray<OLBParams> m_lbparams; 00622 OneDArray<OLBParams> m_cbparams; 00623 }; 00624 00626 00629 class EncoderParams: public CodecParams 00630 { 00631 //codec params plus parameters relating solely to the operation of the encoder 00632 00633 public: 00635 EncoderParams(); 00636 00638 //NB: Assume default copy constructor, assignment = and destructor// 00639 //This means pointers are copied, not the objects they point to.//// 00641 00642 // Gets ... 00643 00645 float Qf() const {return m_qf;} 00646 00648 00653 int NumL1() const {return m_num_L1;} 00654 00656 int L1Sep() const {return m_L1_sep;} 00657 00659 float UFactor() const {return m_ufactor;} 00660 00662 float VFactor() const {return m_vfactor;} 00663 00665 float CPD() const {return m_cpd;} 00666 00668 float ILambda() const {return m_I_lambda;} 00669 00671 float L1Lambda() const {return m_L1_lambda;} 00672 00674 float L2Lambda() const {return m_L2_lambda;} 00675 00677 float Lambda(const FrameSort& fsort) const; 00678 00680 float L1MELambda() const {return m_L1_me_lambda;} 00681 00683 float L2MELambda() const {return m_L2_me_lambda;} 00684 00686 char * OutputPath() const {return ( char* ) m_output_path;} 00687 00689 const EntropyCorrector& EntropyFactors() const {return *m_ent_correct;} 00690 00692 EntropyCorrector& EntropyFactors() {return *m_ent_correct;} 00693 00695 const SequenceOutputManager& BitsOut() const {return *m_bit_out;} 00696 00698 SequenceOutputManager& BitsOut() {return *m_bit_out;} 00699 00700 // ... and Sets 00701 00703 void SetQf(const float qfac){m_qf=qfac;} 00704 00706 void SetNumL1(const int nl){m_num_L1=nl;} 00707 00709 void SetL1Sep(const int lsep){m_L1_sep=lsep;} 00710 00712 void SetUFactor(const float uf){m_ufactor=uf;} 00713 00715 void SetVFactor(const float vf){m_vfactor=vf;} 00716 00718 void SetCPD(const float cpd){m_cpd=cpd;} 00719 00721 void SetILambda(const float l){m_I_lambda=l;} 00722 00724 void SetL1Lambda(const float l){m_L1_lambda=l;} 00725 00727 void SetL2Lambda(const float l){m_L2_lambda=l;} 00728 00730 void SetLambda(const FrameSort& fsort, const float l); 00731 00733 void SetL1MELambda(const float l){m_L1_me_lambda=l;} 00734 00736 void SetL2MELambda(const float l){m_L2_me_lambda=l;} 00737 00739 void SetOutputPath(const char * op){strcpy(m_output_path, op);} 00740 00742 void SetEntropyFactors(EntropyCorrector* entcorrect){m_ent_correct=entcorrect;} 00743 00745 void SetBitsOut( SequenceOutputManager* so ){ m_bit_out=so; } 00746 00747 private: 00749 float m_qf; 00750 00752 int m_num_L1; 00753 00755 int m_L1_sep; 00756 00758 float m_ufactor; 00759 00761 float m_vfactor; 00762 00764 float m_cpd; 00765 00767 float m_I_lambda; 00768 00770 float m_L1_lambda; 00771 00773 float m_L2_lambda; 00774 00776 float m_L1_me_lambda; 00777 00779 float m_L2_me_lambda; 00780 00782 EntropyCorrector* m_ent_correct; 00783 00785 SequenceOutputManager* m_bit_out; 00786 00788 char m_output_path[100]; 00789 }; 00790 00792 00795 class DecoderParams: public CodecParams 00796 { 00797 public: 00799 DecoderParams(): 00800 CodecParams(), 00801 m_bit_in(0){} 00802 00804 //NB: Assume default copy constructor, assignment = and destructor// 00805 //This means pointers are copied, not the objects they point to.//// 00807 00809 const BitInputManager& BitsIn() const {return *m_bit_in;} 00810 00812 BitInputManager& BitsIn() {return *m_bit_in;} 00813 00815 void SetBitsIn(BitInputManager* bi){m_bit_in=bi;} 00816 00817 private: 00819 BitInputManager* m_bit_in; 00820 }; 00821 00823 inline ValueType BChk(const ValueType &num, const ValueType &max) 00824 { 00825 if(num < 0) return 0; 00826 else if(num >= max) return max-1; 00827 else return num; 00828 } 00829 00830 #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.