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

流媒体/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_encode.c
  57.    Author:            Jie Liang  (liang@ti.com)
  58.    Functions:         Core functions for encoding 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. /* encode a block of wavelet coefficients */
  66. void PEZW_encode_block (WINT **coeffsBlk,int width, int height)
  67. {
  68.     Int dc_hsize, dc_vsize;
  69.     Int i,j,k,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.     Int levels;
  77. #ifdef DEBUG_FILE
  78.     Int NumTree=0;
  79. #endif
  80.     levels = tree_depth;
  81.     dc_hsize = width>>levels;
  82.     dc_vsize = height>>levels;
  83. /* encode the AC coefficients:
  84.        loop over all zerotrees */
  85. for(i=0;i<dc_vsize;i++)
  86. for (j=0;j<dc_hsize;j++)
  87. for(band=0;band<3;band++)
  88. {
  89. /* copy the data into the wavelet tree */
  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.                 /* quantization and copy */
  104. the_wvt_tree[0]=coeffsBlk[y][x];
  105. for (lev=1;lev<levels;lev++)
  106. {
  107. npix=1<<(2*(lev-1));
  108. pos = level_pos[lev];
  109. hstart=hpos*(1<<(lev-1));
  110. vstart=vpos*(1<<(lev-1));
  111. start=level_pos[lev-1];
  112. end=level_pos[lev];
  113. for(k=start;k<end;k++)
  114. {
  115. hpos_start = (hstart+hloc_map[k])<<1;
  116. hpos_end = hpos_start+2;
  117. vpos_start = (vstart+vloc_map[k])<<1;
  118. vpos_end = vpos_start+2;
  119. for(y=vpos_start;y<vpos_end;y++)
  120. for(x=hpos_start;x<hpos_end;x++){
  121.                                         /* quantization and copy */
  122.                         the_wvt_tree[pos]=coeffsBlk[y][x]; 
  123. pos++;
  124. }
  125. } /* end of k */
  126. } /* end of for(lev) */
  127. /* determin weighting */
  128. for (lev=0;lev<levels;lev++)
  129. snr_weight[lev] = 0;
  130. /* initialization */
  131. num_Sig=0;
  132. prev_label[0]=ZTRZ;
  133. for(k=1;k<len_tree_struct;k++)
  134. prev_label[k]=DZ;
  135. #ifdef DEBUG_FILE
  136. NumTree++;
  137. if(DEBUG_SYMBOL)
  138. fprintf(fp_debug,"n***NEW TREE   %d***", NumTree);
  139. #endif
  140. /* encoder one wavelet tree. this function demonstrates that 
  141. the requirement for memory is very low for this entropy coder.
  142. if combined with a localized wavelet transform, the total
  143. memory requirement will be extremely low. */
  144. PEZW_tree_encode (band,the_wvt_tree);
  145. if(abs_wvt_tree[0]>MaxValue)
  146. MaxValue=abs_wvt_tree[0];
  147. if(wvt_tree_maxval[0]>MaxValue)
  148. MaxValue=wvt_tree_maxval[0];
  149. } /* end of band */
  150. }
  151. /* encode a tree of wavelet coefficients */
  152. void PEZW_tree_encode (int band,WINT *wvt_tree)
  153. {
  154. int i,j,k,m;
  155. int npix_block, npix;
  156. int pix_pos, max_pos;
  157. int maxval;
  158. int bplane;
  159. int scan_tree_done;
  160. int context;
  161. unsigned char label;
  162. int num_ScanTree, next_numScanTree;
  163. int temp1;
  164. short *temp_ptr;
  165. char signBit, Bit;
  166. // char IsStream=1;
  167. unsigned char bpos;
  168. char skip_flag;
  169. char VAL_flag;
  170. char treeBit, NoTree_flag;
  171. /* first find out the largest value of the coefficients */
  172. for (i=tree_depth-1;i>=1;i--){
  173. npix_block = 1<<(2*(i-1));
  174. pix_pos=level_pos[i];
  175. max_pos=level_pos[i-1];
  176. for(j=0;j<npix_block;j++){
  177. maxval=0;
  178. for(k=0;k<4;k++){
  179. abs_wvt_tree[pix_pos]=ABS(wvt_tree[pix_pos]);
  180. if(abs_wvt_tree[pix_pos]>maxval)
  181. maxval=abs_wvt_tree[pix_pos];
  182. if(i<tree_depth-1){
  183. if(wvt_tree_maxval[pix_pos]>maxval)
  184. maxval=wvt_tree_maxval[pix_pos];
  185. }
  186. pix_pos++;
  187. }
  188. wvt_tree_maxval[max_pos++]=maxval;
  189. }
  190. }
  191. /* last level */
  192. abs_wvt_tree[0]=ABS(wvt_tree[0]);
  193. for(bplane=Max_Bitplane-1;bplane>=Min_Bitplane;bplane--){
  194. #ifdef DEBUG_FILE
  195. if(DEBUG_SYMBOL)
  196. fprintf(fp_debug,"n****bitplane: %d: ",bplane);
  197. #endif
  198.     /* determine bitplane */
  199. skip_flag=0;
  200.     for(i=0;i<tree_depth;i++){
  201. bitplane[i] = bplane+snr_weight[i];
  202. if(bitplane[i]>=Max_Bitplane)
  203. skip_flag=1;
  204. }
  205. if(skip_flag)
  206. continue;   /* skip this bitplane */
  207. /* send refinement bits */
  208. if(bplane<Max_Bitplane-1){
  209. for(i=0;i<tree_depth;i++)
  210. maskbit[i] = (1<<bitplane[i]);
  211. for(i=0;i<num_Sig;i++){
  212. m=sig_layer[i];
  213. Bit=((abs_wvt_tree[sig_pos[i]]&maskbit[m])>0);
  214.                 /* context for refinement bit */
  215.                 context = m*MAX_BITPLANE+bitplane[m];
  216. Ac_encode_symbol(&Encoder[m][bitplane[m]],
  217.                     &model_sub[context],Bit);
  218. }
  219. }
  220. /* form the zerotree and encode the zerotree symbols
  221.    and encode the sign bit for new sigifnicant coeffs
  222.    ScanTree: the intervals of coefficients that are 
  223.              not descendants of zerotree roots.
  224.    Use ScanTree and next_ScanTree to alternate as 
  225.    buffer, so that we do not need to spend the cycles
  226.    to copy from one buffer to the other. 
  227. numScanTree and next_numScanTrees are the number 
  228. of intervals that need to be looked at.
  229. */
  230. next_numScanTree=1;
  231. next_ScanTrees[0]=0;
  232. next_ScanTrees[1]=1;
  233. for(i=0;i<tree_depth;i++){
  234. #ifdef DEBUG_FILE
  235. if(DEBUG_SYMBOL)
  236. fprintf(fp_debug,"n++++tree_depth %d: ",i);
  237. #endif
  238. scan_tree_done=1;
  239. num_ScanTree=next_numScanTree;
  240. next_numScanTree=0;
  241. temp_ptr=ScanTrees;
  242. ScanTrees=next_ScanTrees;
  243. next_ScanTrees=temp_ptr;
  244. bpos=bitplane[i];
  245. npix=1<<(2*i);
  246. for(j=0;j<num_ScanTree;j++)
  247.   for(k=ScanTrees[2*j];k<ScanTrees[2*j+1];k++)
  248.   {
  249. VAL_flag=0;
  250. NoTree_flag=0;
  251. /* decide the zerotree symbol */
  252. if((prev_label[k]==IVAL)||(prev_label[k]==ZTRV)){
  253. #ifdef DEBUG_FILE
  254.             if(DEBUG_SYMBOL){
  255.             fprintf(fp_debug,"SKIP%d ", prev_label[k]);
  256.             fflush(fp_debug);
  257.             }
  258. #endif
  259.                     
  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.   if(i==tree_depth-1){
  272. NoTree_flag=1;
  273.     /* the last level */
  274.     if(abs_wvt_tree[k]>>bpos){
  275.       label=IVAL;
  276.   VAL_flag=1;
  277.       
  278.       sig_pos[num_Sig]=k;
  279.       sig_layer[num_Sig]=i;
  280.   num_Sig++;
  281.     }
  282.     else
  283.       label=IZER;        
  284.   }
  285.   else if(wvt_tree_maxval[k]>>bpos)
  286.     { /* not a zerotree */
  287.   treeBit=0;
  288.       if(abs_wvt_tree[k]>>bpos){
  289. label=IVAL;
  290. VAL_flag=1;
  291. if(prev_label[k]!=ZTRV){
  292. sig_pos[num_Sig]=k;
  293. sig_layer[num_Sig]=i;
  294. num_Sig++;
  295. }
  296. else
  297. VAL_flag=0;
  298. }
  299.       else
  300. label=IZER;
  301.       
  302.       /* set upt the scann tree for next level */
  303.       temp1=2*next_numScanTree;
  304.       next_ScanTrees[temp1]=level_pos[i+1]+4*(k-level_pos[i]);
  305.       next_ScanTrees[temp1+1]=next_ScanTrees[temp1]+4;
  306.       next_numScanTree++;
  307.       scan_tree_done=0;
  308.     }
  309.   else
  310.     { /* a zerotree root */
  311.   treeBit=1;
  312.       if(abs_wvt_tree[k]>>bpos){
  313. label=ZTRV;
  314. VAL_flag=1;
  315. sig_pos[num_Sig]=k;
  316. sig_layer[num_Sig]=i;
  317. num_Sig++;
  318. }
  319.       else{
  320. label=ZTRZ;
  321.       }
  322.       
  323.     }
  324.   
  325.   /* encode the zerotree symbol */
  326. #ifdef DEBUG_FILE
  327. if(DEBUG_SYMBOL){
  328. fprintf(fp_debug,"%d ", label);
  329. fflush(fp_debug);
  330. }
  331. #endif
  332.   context=bpos*NumContexts*tree_depth+i*NumContexts+
  333.     prev_label[k]*NumBands+band;
  334.   Ac_encode_symbol(&Encoder[i][bpos],&context_model[context],label);
  335.   /* encode the sign bit */
  336.   if(VAL_flag){
  337.   signBit = (wvt_tree[k]>0);
  338. #ifdef DEBUG_FILE
  339.   if(DEBUG_SYMBOL){
  340. if(signBit==1)
  341. fprintf(fp_debug,"%d+ ",signBit);
  342. else /* signBit == 0 */
  343. fprintf(fp_debug,"%d- ",signBit);
  344. fflush(fp_debug);
  345. }
  346. #endif
  347.                        /* context for sign model */
  348.                       context = i*MAX_BITPLANE+bpos;
  349.   Ac_encode_symbol(&Encoder[i][bpos],&model_sign[context],signBit);
  350.   }
  351.   
  352.   /* set up prev_label */
  353.   prev_label[k]=label;
  354.   
  355. } /* end of if(prev_label) */
  356.   }  /* end of k pixel */
  357. /* no subordinate none zero descendants */
  358. if(scan_tree_done)
  359.   break;
  360. }  /* end of tree depth */
  361. } /* end of bitplane */
  362. return;
  363. }