wvtpezw_tree_decode.cpp
上传用户:sun1608
上传日期:2007-02-02
资源大小:6116k
文件大小:14k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

Visual C++

  1. /****************************************************************************/
  2. /*   MPEG4 Visual Texture Coding (VTC) Mode Software                        */
  3. /*                                                                          */
  4. /*   This software was jointly developed by the following participants:     */
  5. /*                                                                          */
  6. /*   Single-quant,  multi-quant and flow control                            */
  7. /*   are provided by  Sarnoff Corporation                                   */
  8. /*     Iraj Sodagar   (iraj@sarnoff.com)                                    */
  9. /*     Hung-Ju Lee    (hjlee@sarnoff.com)                                   */
  10. /*     Paul Hatrack   (hatrack@sarnoff.com)                                 */
  11. /*     Shipeng Li     (shipeng@sarnoff.com)                                 */
  12. /*     Bing-Bing Chai (bchai@sarnoff.com)                                   */
  13. /*     B.S. Srinivas  (bsrinivas@sarnoff.com)                               */
  14. /*                                                                          */
  15. /*   Bi-level is provided by Texas Instruments                              */
  16. /*     Jie Liang      (liang@ti.com)                                        */
  17. /*                                                                          */
  18. /*   Shape Coding is provided by  OKI Electric Industry Co., Ltd.           */
  19. /*     Zhixiong Wu    (sgo@hlabs.oki.co.jp)                                 */
  20. /*     Yoshihiro Ueda (yueda@hlabs.oki.co.jp)                               */
  21. /*     Toshifumi Kanamaru (kanamaru@hlabs.oki.co.jp)                        */
  22. /*                                                                          */
  23. /*   OKI, Sharp, Sarnoff, TI and Microsoft contributed to bitstream         */
  24. /*   exchange and bug fixing.                                               */
  25. /*                                                                          */
  26. /*                                                                          */
  27. /* In the course of development of the MPEG-4 standard, this software       */
  28. /* module is an implementation of a part of one or more MPEG-4 tools as     */
  29. /* specified by the MPEG-4 standard.                                        */
  30. /*                                                                          */
  31. /* The copyright of this software belongs to ISO/IEC. ISO/IEC gives use     */
  32. /* of the MPEG-4 standard free license to use this  software module or      */
  33. /* modifications thereof for hardware or software products claiming         */
  34. /* conformance to the MPEG-4 standard.                                      */
  35. /*                                                                          */
  36. /* Those intending to use this software module in hardware or software      */
  37. /* products are advised that use may infringe existing  patents. The        */
  38. /* original developers of this software module and their companies, the     */
  39. /* subsequent editors and their companies, and ISO/IEC have no liability    */
  40. /* and ISO/IEC have no liability for use of this software module or         */
  41. /* modification thereof in an implementation.                               */
  42. /*                                                                          */
  43. /* Permission is granted to MPEG members to use, copy, modify,              */
  44. /* and distribute the software modules ( or portions thereof )              */
  45. /* for standardization activity within ISO/IEC JTC1/SC29/WG11.              */
  46. /*                                                                          */
  47. /* Copyright 1995, 1996, 1997, 1998 ISO/IEC                                 */
  48. /****************************************************************************/
  49. /****************************************************************************/
  50. /*     Texas Instruments Predictive Embedded Zerotree (PEZW) Image Codec    */
  51. /*    Developed by Jie Liang (liang@ti.com)                                */
  52. /*                         */ 
  53. /*     Copyright 1996, 1997, 1998 Texas Instruments                    */
  54. /****************************************************************************/
  55. /****************************************************************************
  56.    File name:         wvtpezw_tree_decode.c
  57.    Author:            Jie Liang  (liang@ti.com)
  58.    Functions:         Core functions for decoding the tree blocks of coeffs.
  59.    Revisions:         v1.0 (10/04/98)
  60. *****************************************************************************/
  61. #include "wvtPEZW.hpp"
  62. #include "PEZW_zerotree.hpp"
  63. #include "wvtpezw_tree_codec.hpp"
  64. #include "PEZW_functions.hpp"
  65. /* decode a block of wavelet coefficients */
  66. void PEZW_decode_block (WINT **coeffsBlk, Int width, Int height)
  67. {
  68.     Int dc_hsize, dc_vsize;
  69.     Int i,j,k,m,n,band, hpos, vpos;
  70.     int pos;
  71. int hpos_start, hpos_end;
  72. int vpos_start, vpos_end;
  73.     Int lev, npix;
  74.     Int hstart, vstart,start, end;
  75.     Int x,y;
  76.     Char SignBit;
  77.     Int levels;
  78. #ifdef DEBUG_FILE
  79.     Int NumTree=0;
  80. #endif
  81.     levels = tree_depth;
  82.     dc_hsize = width>>levels;
  83.     dc_vsize = height>>levels;
  84. /* decode the AC coefficients:
  85.        loop over all zerotrees */
  86. for(i=0;i<dc_vsize;i++)
  87. for (j=0;j<dc_hsize;j++)
  88. for(band=0;band<3;band++)
  89. {
  90. if(band==0)  { /* HL band */
  91. hpos = j+dc_hsize;
  92. vpos = i;
  93. }
  94. else if (band==1) { /* LH band */
  95. hpos = j;
  96. vpos = i+dc_vsize;
  97. }
  98. else{
  99. hpos = j+dc_hsize;
  100. vpos = i+dc_vsize;
  101. }
  102. y=vpos;x=hpos;
  103. /* determin weighting */
  104. for (lev=0;lev<levels;lev++)
  105. snr_weight[lev] = 0;
  106. /* initialization */
  107. num_Sig=0;
  108. the_wvt_tree[0]=0;
  109. prev_label[0]=ZTRZ;
  110. for(m=1;m<len_tree_struct;m++){
  111. the_wvt_tree[m]=0;
  112. prev_label[m]=DZ;
  113. }
  114. #ifdef DEBUG_FILE
  115. NumTree++;
  116. if(DEBUG_SYMBOL)
  117. fprintf(fp_debug,"n***NEW TREE %d***", NumTree);
  118. #endif
  119. /* encoder one wavelet tree. this function demonstrates that 
  120. the requirement for memory is very low for this entropy coder.
  121. if combined with a localized wavelet transform, the total
  122. memory requirement will be extremely low. */
  123. PEZW_tree_decode (band,the_wvt_tree,snr_weight);
  124. /* dequantize the coefficients */
  125. for(k=0;k<num_Sig;k++){
  126. SignBit=sign_bit[k];
  127. n=sig_pos[k];
  128. m=the_wvt_tree[n];
  129. the_wvt_tree[n]=(SignBit>0)?m:-m;
  130. }
  131. #ifdef DEBUG_FILE
  132. if(DEBUG_VALUE){
  133. fprintf(fp_debug,"n***NEW TREE %d *****n",NumTree);
  134. fprintf(fp_debug,"%d ",the_wvt_tree[0]);
  135. }
  136. #endif
  137. /* copy the data from wvt_tree to output */
  138. coeffsBlk[y][x]=the_wvt_tree[0];
  139. for (lev=1;lev<levels;lev++)
  140. {
  141. npix=1<<(2*(lev-1));
  142. pos = level_pos[lev];
  143. hstart=hpos*(1<<(lev-1));
  144. vstart=vpos*(1<<(lev-1));
  145. start=level_pos[lev-1];
  146. end=level_pos[lev];
  147. for(k=start;k<end;k++)
  148. {
  149. hpos_start = (hstart+hloc_map[k])*2;
  150. hpos_end = hpos_start+2;
  151. vpos_start = (vstart+vloc_map[k])*2;
  152. vpos_end = vpos_start+2;
  153. for(y=vpos_start;y<vpos_end;y++)
  154. for(x=hpos_start;x<hpos_end;x++){
  155. coeffsBlk[y][x]=the_wvt_tree[pos++];
  156. #ifdef DEBUG_FILE
  157. if(DEBUG_VALUE){
  158. fprintf(fp_debug,"%d ",coeffsBlk[y][x]);
  159. fflush(fp_debug);
  160. }
  161. #endif
  162. }
  163. } /* end of k */
  164. } /* end of for(lev) */
  165. } /* end of band */
  166. }
  167. /* decode a tree structure */
  168. void PEZW_tree_decode (int band,WINT *wvt_tree,int *snr_weight)
  169. {
  170. int i,j,k,m,n;
  171. int npix;
  172. int bplane;
  173. int scan_tree_done;
  174. int context;
  175. unsigned char label;
  176. int num_ScanTree, next_numScanTree;
  177. int temp1;
  178. short *temp_ptr;
  179. char Bit;
  180. //char IsStream=1;
  181. unsigned char bpos;
  182. char skip_flag;
  183. #define AC_offset 3
  184. for(bplane=Max_Bitplane-1;bplane>=Min_Bitplane;bplane--){
  185. #ifdef DEBUG_FILE
  186. if(DEBUG_SYMBOL)
  187. fprintf(fp_debug,"n****bitplane: %d",bplane);
  188. #endif
  189.     /* determine bitplane */
  190.   skip_flag=0;
  191.     for(i=0;i<tree_depth-spatial_leveloff;i++){
  192. bitplane[i] = bplane+snr_weight[i];
  193. if(bitplane[i]>=Max_Bitplane)
  194. skip_flag=1;
  195. }
  196. if(skip_flag)
  197. continue;   /* skip this bitplane */
  198. /* receive refinement bits */
  199. if(bplane<Max_Bitplane-1){
  200. for(i=0;i<num_Sig;i++){
  201. m=sig_layer[i];
  202.                 n = bitplane[m];
  203.                 /* context for refinement bit */
  204.                 context = m*MAX_BITPLANE+bitplane[m];
  205.                 /* rate control */
  206. // #define FLAG reach_budget[m][n] deleted by swinder
  207.                 if(reach_budget[m][n]==1)
  208.                     continue;                   
  209. Bit=Ac_decode_symbol(&Decoder[m][bitplane[m]],
  210.                     &model_sub[context]);
  211.                /* rate control */
  212.                if( (Decoder[m][n].stream - PEZW_bitstream[m][n]) >= Init_Bufsize[m][n]+AC_offset){
  213.                        reach_budget[m][n] = 1;
  214.                        return;
  215.                }
  216.    wvt_tree[sig_pos[i]] |= (Bit<<bitplane[m]);
  217. }
  218. }
  219. /* form the zerotree and encode the zerotree symbols
  220.    and encode the sign bit for new sigifnicant coeffs
  221.    ScanTree: the intervals of coefficients that are 
  222.              not descendants of zerotree roots.
  223.    Use ScanTree and next_ScanTree to alternate as 
  224.    buffer, so that we do not need to spend the cycles
  225.    to copy from one buffer to the other. 
  226. numScanTree and next_numScanTrees are the number 
  227. of intervals that need to be looked at.
  228. */
  229. next_numScanTree=1;
  230. next_ScanTrees[0]=0;
  231. next_ScanTrees[1]=1;
  232. for(i=0;i<tree_depth-spatial_leveloff;i++){
  233. #ifdef DEBUG_FILE
  234. if(DEBUG_SYMBOL)
  235. fprintf(fp_debug,"n****tree_depth %d:  ",i);
  236. #endif
  237. scan_tree_done=1;
  238. num_ScanTree=next_numScanTree;
  239. next_numScanTree=0;
  240. temp_ptr=ScanTrees;
  241. ScanTrees=next_ScanTrees;
  242. next_ScanTrees=temp_ptr;
  243. bpos=bitplane[i];
  244.             /* rate control */
  245. // #define FLAG reach_budget[i][bpos] deleted by swinder
  246.             if(reach_budget[i][bpos]==1)
  247.                     break; 
  248. npix=1<<(2*i);
  249. for(j=0;j<num_ScanTree;j++)
  250.   for(k=ScanTrees[2*j];k<ScanTrees[2*j+1];k++)
  251.   {
  252. /* decide the zerotree symbol */
  253. if((prev_label[k]==IVAL)||(prev_label[k]==ZTRV)){
  254. #ifdef DEBUG_FILE
  255.             if(DEBUG_SYMBOL){
  256.             fprintf(fp_debug,"SKIP%d ", prev_label[k]);
  257.             fflush(fp_debug);
  258.             }
  259. #endif
  260. if(i<tree_depth-1){
  261. /* set upt the scann tree for next level */
  262. temp1=2*next_numScanTree;
  263. next_ScanTrees[temp1]=level_pos[i+1]+4*(k-level_pos[i]);
  264. next_ScanTrees[temp1+1]=next_ScanTrees[temp1]+4;
  265. next_numScanTree++;
  266. scan_tree_done=0;
  267. }
  268. continue;
  269. }
  270. else{
  271. /* decode the zerotree symbol */
  272.     context=bpos*NumContexts*tree_depth+i*NumContexts+
  273.         prev_label[k]*NumBands+band;
  274.     label = Ac_decode_symbol(&Decoder[i][bpos],&context_model[context]);   
  275.                     /* rate control */
  276.                     if( (Decoder[i][bpos].stream - PEZW_bitstream[i][bpos]) >= Init_Bufsize[i][bpos]+AC_offset){
  277.                            reach_budget[i][bpos] = 1;
  278.                            return;
  279.                     }
  280. #ifdef DEBUG_FILE
  281. if(DEBUG_SYMBOL){
  282. fprintf(fp_debug,"%d ", label);
  283. fflush(fp_debug);
  284. }
  285. #endif
  286.                     /* context for sign model */
  287.                     context = i*MAX_BITPLANE+bpos;
  288. if((label==IVAL)||(label==IZER)){
  289. scan_tree_done=0;
  290. if(label==IVAL){
  291.   Bit=1;
  292.   wvt_tree[k] |= (Bit<<bitplane[i]);
  293.   sig_pos[num_Sig]=k;
  294.       sig_layer[num_Sig]=i;
  295.   sign_bit[num_Sig]= Ac_decode_symbol(&Decoder[i][bpos],&model_sign[context]);
  296.                               if( (Decoder[i][bpos].stream - PEZW_bitstream[i][bpos]) >= Init_Bufsize[i][bpos]+AC_offset){
  297.                                   reach_budget[i][bpos] = 1;
  298.                                   return;
  299.                               }
  300. #ifdef DEBUG_FILE
  301.   if(DEBUG_SYMBOL){
  302. if(sign_bit[num_Sig]==1)
  303. fprintf(fp_debug,"%d+ ",sign_bit[num_Sig]);
  304. else
  305. fprintf(fp_debug,"%d- ",sign_bit[num_Sig]);
  306. fflush(fp_debug);
  307. }
  308. #endif   
  309.      num_Sig++;
  310. }
  311. /* set upt the scann tree for next level */
  312. if(i<tree_depth-1){
  313.     temp1=2*next_numScanTree;
  314. next_ScanTrees[temp1]=level_pos[i+1]+4*(k-level_pos[i]);
  315. next_ScanTrees[temp1+1]=next_ScanTrees[temp1]+4;
  316. next_numScanTree++;
  317. scan_tree_done=0;
  318. }
  319. }
  320. else{
  321. if(label==ZTRV){
  322.   Bit=1;   
  323.   wvt_tree[k] |= (Bit<<bitplane[i]);
  324.       sig_pos[num_Sig]=k;
  325.       sig_layer[num_Sig]=i;
  326.   sign_bit[num_Sig]= Ac_decode_symbol(&Decoder[i][bpos],&model_sign[context]);
  327.                               if( (Decoder[i][bpos].stream - PEZW_bitstream[i][bpos]) >= Init_Bufsize[i][bpos]+AC_offset){
  328.                                   reach_budget[i][bpos] = 1;
  329.                                   return;
  330.                               }
  331. #ifdef DEBUG_FILE
  332.   if(DEBUG_SYMBOL){
  333. if(sign_bit[num_Sig]==1)
  334. fprintf(fp_debug,"%d+ ",sign_bit[num_Sig]);
  335. else
  336. fprintf(fp_debug,"%d- ",sign_bit[num_Sig]);
  337. fflush(fp_debug);
  338. }
  339. #endif
  340.   num_Sig++;
  341. }
  342. } /* end of else */
  343. /* set up prev_label */
  344. prev_label[k]=label;
  345. } /* end of if(prev_label */
  346. } /* end of k */
  347. /* no subordinate none zero descendants */
  348. if(scan_tree_done)
  349.   break;
  350. }  /* end of tree depth */
  351. } /* end of bitplane */
  352. return;
  353. }