Created by the British Broadcasting Corporation.
00001 /* ***** BEGIN LICENSE BLOCK ***** 00002 * 00003 * $Id: wavelet_utils.h,v 1.30 2008/04/29 08:51:52 tjdwave Exp $ $Name: Dirac_0_10_0 $ 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 * Anuradha Suraparaju 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 _WAVELET_UTILS_H_ 00041 #define _WAVELET_UTILS_H_ 00042 00043 #include <libdirac_common/arrays.h> 00044 #include <libdirac_common/common.h> 00045 #include <vector> 00046 #include <cmath> 00047 #include <iostream> 00048 00049 //utilities for subband and wavelet transforms 00050 //Includes fast transform using lifting 00051 00052 namespace dirac 00053 { 00054 00055 class PicArray; 00056 class Subband; 00057 00059 class CodeBlock 00060 { 00061 00062 friend class Subband; 00063 00064 public: 00066 /* 00067 Default constructor - sets all dimensions to zero 00068 */ 00069 CodeBlock(); 00070 00072 /* 00073 Initialise the code block 00074 \param xstart the x-coord of the first coefficient in the block 00075 \param xend one past the last coefficient, horizontally 00076 \param ystart the y-coord of the first coefficient in the block 00077 \param yend one past the last coefficient, vertically 00078 */ 00079 CodeBlock( const int xstart , const int ystart , const int xend , const int yend); 00080 00082 int Xstart() const { return m_xstart; } 00083 00085 int Ystart() const { return m_ystart; } 00086 00088 int Xend() const { return m_xend; } 00089 00091 int Yend() const { return m_yend; } 00092 00094 int Xl() const { return m_xl; } 00095 00097 int Yl() const { return m_yl; } 00098 00100 int QuantIndex() const{ return m_quantindex; } 00101 00103 bool Skipped() const { return m_skipped; } 00104 00106 void SetQuantIndex( const int quantindex ){ m_quantindex = quantindex; } 00107 00109 void SetSkip( bool skip ){ m_skipped = skip; } 00110 00111 private: 00112 00114 /* 00115 Initialise the code block 00116 \param xstart the x-coord of the first coefficient in the block 00117 \param xend one past the last coefficient, horizontally 00118 \param ystart the y-coord of the first coefficient in the block 00119 \param yend one past the last coefficient, vertically 00120 */ 00121 void Init( const int xstart , const int ystart , const int xend , const int yend ); 00122 00123 private: 00124 00125 int m_xstart; 00126 int m_ystart; 00127 int m_xend; 00128 int m_yend; 00129 int m_xl; 00130 int m_yl; 00131 00132 int m_quantindex; 00133 00134 bool m_skipped; 00135 }; 00136 00137 00139 class Subband 00140 { 00141 public: 00142 00144 Subband(); 00145 00147 00155 Subband(int xpos, int ypos, int xlen, int ylen); 00156 00158 00167 Subband(int xpos, int ypos, int xlen, int ylen, int d); 00168 00170 ~Subband(); 00171 00172 //Default (shallow) copy constructor and operator= used 00173 00175 int Xl() const {return m_xl;} 00176 00178 int Xp() const {return m_xp;} 00179 00181 int Yl() const {return m_yl;} 00182 00184 int Yp() const {return m_yp;} 00185 00187 int Max() const {return m_max_bit;} 00188 00190 double Wt() const {return m_wt;} 00191 00193 int Depth() const {return m_depth;} 00194 00196 int Scale() const {return ( 1<<m_depth );} 00197 00199 int QuantIndex() const {return m_qindex;} 00200 00202 bool UsingMultiQuants() const {return m_multi_quants; } 00203 00205 int Parent() const {return m_parent;} 00206 00208 const std::vector<int>& Children() const {return m_children;} 00209 00211 int Child(const int n) const {return m_children[n];} 00212 00214 TwoDArray<CodeBlock>& GetCodeBlocks(){ return m_code_block_array; } 00215 00217 const TwoDArray<CodeBlock>& GetCodeBlocks() const { return m_code_block_array; } 00218 00220 bool Skipped() const { return m_skipped; } 00221 00223 void SetWt( const float w ); 00224 00226 void SetParent( const int p ){ m_parent=p; } 00227 00229 void SetDepth( const int d ){ m_depth=d;} 00230 00232 void SetMax( const int m ){ m_max_bit=m; }; 00233 00235 void SetNumBlocks( const int ynum , const int xnum ); 00236 00238 void SetQuantIndex( const int idx){ m_qindex = idx; } 00239 00241 void SetUsingMultiQuants( const bool multi){ m_multi_quants = multi; } 00242 00244 void SetSkip(const bool skip ){ m_skipped = skip; } 00245 00246 private: 00247 // subband bounds 00248 int m_xp , m_yp , m_xl , m_yl; 00249 00250 // perceptual weight for quantisation 00251 double m_wt; 00252 00253 // depth in the transform 00254 int m_depth; 00255 00256 // quantiser index 00257 int m_qindex; 00258 00259 // position of parent in a subband list 00260 int m_parent; 00261 00262 // positions of children in the subband list 00263 std::vector<int> m_children; 00264 00265 // position of the MSB of the largest absolute value 00266 int m_max_bit; 00267 00268 // The code blocks 00269 TwoDArray<CodeBlock> m_code_block_array; 00270 00271 // A flag indicating whether we're using one qf for each code block 00272 bool m_multi_quants; 00273 00274 // Whether the subband is skipped or not 00275 bool m_skipped; 00276 }; 00277 00279 class SubbandList 00280 { 00281 public: 00283 SubbandList(){} 00284 00286 ~SubbandList(){} 00287 00288 //Default (shallow) copy constructor and operator= used 00290 void Init(const int depth,const int xlen,const int ylen); 00291 00293 int Length() const {return bands.size();} 00294 00296 Subband& operator()(const int n){return bands[n-1];} 00297 00299 const Subband& operator()(const int n) const {return bands[n-1];} 00300 00302 void AddBand(const Subband& b){bands.push_back(b);} 00303 00305 void Clear(){bands.clear();} 00306 00307 private: 00308 00310 float PerceptualWeight( const float xf , const float yf , const CompSort cs); 00311 00312 private: 00313 std::vector<Subband> bands; 00314 }; 00315 00317 00321 class WaveletTransform 00322 { 00323 public: 00325 WaveletTransform(int d = 4, WltFilter f = DAUB9_7); 00326 00328 virtual ~WaveletTransform(); 00329 00331 00337 void Transform(const Direction d, PicArray& pic_data, CoeffArray& coeff_data); 00338 00340 SubbandList& BandList(){return m_band_list;} 00341 00343 const SubbandList& BandList() const {return m_band_list;} 00344 00346 00357 void SetBandWeights (const float cpd, 00358 const PictureSort& fsort, 00359 const ChromaFormat& cformat, 00360 const CompSort csort, 00361 const bool field_coding); 00362 00363 private: 00364 // Classes used within wavelet transform 00365 00367 class VHFilter 00368 { 00369 00370 public: 00371 00372 VHFilter(){} 00373 00374 virtual ~VHFilter(){} 00375 00377 virtual void Split(const int xp, const int yp, const int xl, const int yl, CoeffArray& coeff_data)=0; 00378 00380 virtual void Synth(const int xp, const int yp, const int xl, const int yl, CoeffArray& coeff_data)=0; 00381 00383 virtual double GetLowFactor() const=0; 00384 00386 virtual double GetHighFactor() const =0; 00387 00389 virtual int GetShift() const =0; 00390 00391 protected: 00392 00394 inline void Interleave( const int xp, const int yp, const int xl, const int yl, CoeffArray& coeff_data ); 00395 00396 00398 inline void DeInterleave( const int xp, const int yp, const int xl, const int yl, CoeffArray& coeff_data ); 00399 00401 void ShiftRowLeft(CoeffType *row, int length, int shift); 00402 00404 void ShiftRowRight(CoeffType *row, int length, int shift); 00405 }; 00406 00408 class VHFilterDAUB9_7 : public VHFilter 00409 { 00410 00411 public: 00412 00414 void Split(const int xp, const int yp, const int xl, const int yl, CoeffArray& coeff_data); 00415 00417 void Synth(const int xp, const int yp, const int xl, const int yl, CoeffArray& coeff_data); 00418 00420 double GetLowFactor() const { return 1.149604398;} 00421 00423 double GetHighFactor() const { return 0.869864452;} 00424 00426 int GetShift() const {return 1;} 00427 00428 00429 }; 00430 00432 class VHFilterLEGALL5_3 : public VHFilter 00433 { 00434 00435 public: 00436 00438 void Split(const int xp, const int yp, const int xl, const int yl, CoeffArray& coeff_data); 00439 00441 void Synth(const int xp, const int yp, const int xl, const int yl, CoeffArray& coeff_data); 00442 00444 double GetLowFactor() const { return 1.179535649;} 00445 00447 double GetHighFactor() const { return 0.81649658;} 00448 00450 int GetShift() const {return 1;} 00451 00452 00453 #ifdef HAVE_MMX 00454 inline void HorizSynth (int xp, int xl, int ystart, int yend, CoeffArray &coeff_data); 00455 #endif 00456 00457 }; 00458 00460 class VHFilterDD9_7 : public VHFilter 00461 { 00462 00463 public: 00464 00466 void Split(const int xp, const int yp, const int xl, const int yl, CoeffArray& coeff_data); 00467 00469 void Synth(const int xp, const int yp, const int xl, const int yl, CoeffArray& coeff_data); 00470 00472 double GetLowFactor() const { return 1.218660804;} 00473 00475 double GetHighFactor() const { return 0.780720058;} 00476 00478 int GetShift() const {return 1;} 00479 00480 }; 00481 00482 00484 class VHFilterDD13_7 : public VHFilter 00485 { 00486 00487 public: 00488 00490 void Split(const int xp, const int yp, const int xl, const int yl, CoeffArray& coeff_data); 00491 00493 void Synth(const int xp, const int yp, const int xl, const int yl, CoeffArray& coeff_data); 00494 00496 double GetLowFactor() const { return 1.235705971;} 00497 00499 double GetHighFactor() const { return 0.780719354;} 00500 00502 int GetShift() const {return 1;} 00503 00504 }; 00505 00507 class VHFilterHAAR0 : public VHFilter 00508 { 00509 00510 public: 00511 00513 void Split(const int xp, const int yp, const int xl, const int yl, CoeffArray& coeff_data); 00514 00516 void Synth(const int xp, const int yp, const int xl, const int yl, CoeffArray& coeff_data); 00517 00519 double GetLowFactor() const { return 1.414213562;} 00520 00522 double GetHighFactor() const { return 0.707106781;} 00523 00525 int GetShift() const {return 0;} 00526 00527 00528 }; 00529 00531 class VHFilterHAAR1 : public VHFilter 00532 { 00533 00534 public: 00535 00537 void Split(const int xp, const int yp, const int xl, const int yl, CoeffArray& coeff_data); 00538 00540 void Synth(const int xp, const int yp, const int xl, const int yl, CoeffArray& coeff_data); 00541 00543 double GetLowFactor() const { return 1.414213562;} 00544 00546 double GetHighFactor() const { return 0.707106781;} 00547 00549 int GetShift() const {return 1;} 00550 00551 00552 }; 00553 00554 00556 class VHFilterHAAR2 : public VHFilter 00557 { 00558 00559 public: 00560 00562 void Split(const int xp, const int yp, const int xl, const int yl, CoeffArray& coeff_data); 00563 00565 void Synth(const int xp, const int yp, const int xl, const int yl, CoeffArray& coeff_data); 00566 00568 double GetLowFactor() const { return 1.414213562;} 00569 00571 double GetHighFactor() const { return 0.707106781;} 00572 00574 int GetShift() const {return 2;} 00575 00576 }; 00577 00578 00579 00580 // Lifting steps used in the filters 00581 00583 template<int shift> 00584 class PredictStepShift 00585 { 00586 00587 public: 00588 00590 PredictStepShift(){} 00591 00592 // Assume default copy constructor, assignment= and destructor // 00593 00595 /* 00596 Do the filtering. 00597 \param in_val the value being predicted 00598 \param val1 the first value being used for prediction 00599 \param val2 the second value being used for prediction 00600 */ 00601 inline void Filter(CoeffType& in_val, const CoeffType& val1, const CoeffType& val2) const 00602 { 00603 in_val -= (( val1 + val2 + (1<<(shift-1)) ) >>shift ); 00604 } 00605 00606 }; 00607 00609 template<int shift> 00610 class UpdateStepShift 00611 { 00612 00613 public: 00615 UpdateStepShift(){} 00616 00618 /* 00619 Do the filtering. 00620 \param in_val the value being updated 00621 \param val1 the first value being used for updating 00622 \param val2 the second value being used for updating 00623 */ 00624 inline void Filter(CoeffType& in_val, const CoeffType& val1, const CoeffType& val2) const 00625 { 00626 in_val += ( ( val1 + val2 + (1<<(shift-1)) ) >>shift ); 00627 } 00628 00629 }; 00630 00632 template <int shift , int tap1, int tap2> 00633 class PredictStepFourTap 00634 { 00635 public: 00636 00638 PredictStepFourTap(){} 00639 00640 // Assume default copy constructor, assignment= and destructor // 00641 00643 inline void Filter(CoeffType& in_val, const CoeffType& val1, const CoeffType& val2 , 00644 const CoeffType& val3, const CoeffType& val4 ) const 00645 { 00646 in_val -= ( tap1*( val1 + val2 ) + tap2*( val3 + val4 ) + (1<<(shift-1)))>>shift; 00647 } 00648 }; 00649 00651 template <int shift , int tap1 , int tap2> 00652 class UpdateStepFourTap 00653 { 00654 00655 public: 00657 UpdateStepFourTap(){} 00658 00660 inline void Filter(CoeffType& in_val, const CoeffType& val1, const CoeffType& val2 , 00661 const CoeffType& val3, const CoeffType& val4 ) const 00662 { 00663 in_val += ( tap1*( val1 + val2 ) + tap2*( val3 + val4 ) + (1<<(shift-1)) )>>shift; 00664 } 00665 }; 00666 00668 template <int gain> class PredictStep97 00669 { 00670 public: 00671 00673 PredictStep97(){} 00674 00675 // Assume default copy constructor, assignment= and destructor // 00676 00678 /* 00679 Do the filtering. 00680 \param in_val the value being predicted 00681 \param val1 the first value being used for prediction 00682 \param val2 the second value being used for prediction 00683 */ 00684 inline void Filter(CoeffType& in_val, const CoeffType& val1, const CoeffType& val2) const 00685 { 00686 in_val -= static_cast< CoeffType >( (gain * static_cast< int >( val1 + val2 )) >>12 ); 00687 } 00688 }; 00689 00691 template <int gain> class UpdateStep97 00692 { 00693 00694 public: 00696 UpdateStep97(){} 00697 00699 /* 00700 Do the filtering. 00701 \param in_val the value being updated 00702 \param val1 the first value being used for updating 00703 \param val2 the second value being used for updating 00704 */ 00705 inline void Filter(CoeffType& in_val, const CoeffType& val1, const CoeffType& val2) const 00706 { 00707 in_val += static_cast< CoeffType >( (gain * static_cast< int >( val1 + val2 )) >>12 ); 00708 } 00709 }; 00710 00711 private: 00712 00713 // Private variables 00714 00715 // The subband list to be used for conventional transform apps 00716 SubbandList m_band_list; 00717 00719 int m_depth; 00720 00722 WltFilter m_filt_sort; 00723 00725 VHFilter* m_vhfilter; 00726 00727 private: 00728 // Private functions 00730 WaveletTransform(const WaveletTransform& cpy); 00731 00733 WaveletTransform& operator=(const WaveletTransform& rhs); 00734 00736 float PerceptualWeight(float xf,float yf,CompSort cs); 00737 00738 }; 00739 }// end namespace dirac 00740 00741 #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.