wvtpezw_tree_init_encode.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_init_encode.c
  57.    Author:            Jie Liang  (liang@ti.com)
  58.    Functions:         functions for initialization and closing of the 
  59.                       PEZW encoder.
  60.    Revisions:         v1.0 (10/04/98)
  61. *****************************************************************************/
  62. #include <time.h>
  63. #include <stdlib.h>
  64. #include <math.h>
  65. #include "wvtPEZW.hpp"
  66. #include "PEZW_zerotree.hpp"
  67. #include "wvtpezw_tree_codec.hpp"
  68. #include "PEZW_functions.hpp"
  69. /* initialize the global datastructure set up
  70.    in wvtpezw_tree_codec.h */
  71. void PEZW_encode_init (Int levels, Int Imgwidth, Int Imgheight)
  72. {
  73. int len;
  74. int i,j;
  75. int x,y;
  76. int nsym=No_of_symbols;
  77. int adapt=ADAPTATION_MODE;
  78. int contexts;
  79. int *freq;
  80. int pos, bpos;
  81. int hpos_start, hpos_end;
  82. int vpos_start, vpos_end;
  83.     int dc_width, dc_height;
  84. int hsize, vsize;
  85. int band;
  86. int npix;
  87. int start,end;
  88. //int IsStream=1;
  89. //int Mbplane=0;
  90. //int NumTree=0;
  91. #ifdef DEBUG_FILE
  92. fp_debug = fopen("zerotree_debug_encode.txt","w");
  93. if(DEBUG_SYMBOL)
  94. fprintf(fp_debug,"n********Encoder*********n");
  95. #endif
  96.     hsize = Imgwidth;
  97.     vsize = Imgheight;
  98. /* define the global variables defined in 
  99.  wvtpezw_tree_codec.h */
  100. Max_Bitplane = MAX_BITPLANE; /* leave enough dynamic range */
  101. Min_Bitplane = MIN_BITPLANE;
  102. tree_depth = levels;
  103. MaxValue=0;
  104.     /* dc bitstream */
  105.     dc_width = hsize>>levels;
  106.     dc_height = vsize>>levels;
  107. /* positions of each depth within the tree */
  108.     len_tree_struct = 0;
  109. level_pos = (short *)calloc(tree_depth,sizeof(short));
  110. level_pos[0]=0;
  111. for (i=1;i<levels;i++){
  112. len_tree_struct += 1<<(2*(i-1));
  113. level_pos[i] = len_tree_struct;
  114. }
  115. len_tree_struct += 1<<(2*(levels-1));
  116. /* Initial buffer size */
  117.     Init_Bufsize = (int **)calloc(tree_depth,sizeof(int *));
  118.     for (i=0;i<tree_depth;i++){
  119.    Init_Bufsize[i]=(int *)calloc(Max_Bitplane,sizeof(int));
  120.  for(j=0;j<Max_Bitplane;j++)
  121.     Init_Bufsize[i][j] = Initial_Bufsize;
  122. }
  123.     /* bits_to_go structure */
  124.     bits_to_go_inBuffer = (unsigned char **)calloc(tree_depth,sizeof(char *));
  125.     for (i=0;i<tree_depth;i++)
  126.    bits_to_go_inBuffer[i]=(unsigned char *)calloc(Max_Bitplane,sizeof(char));
  127. /* mask */
  128. snr_weight = (int *)calloc(tree_depth,sizeof(int));
  129. bitplane = (unsigned char *)calloc(tree_depth,sizeof(char));
  130. /* data structure for the wavelet coefficients */
  131. the_wvt_tree = (WINT *)calloc(len_tree_struct,sizeof(WINT));
  132. abs_wvt_tree = (WINT *)calloc(len_tree_struct,sizeof(WINT));
  133. maskbit=(unsigned int *) calloc(tree_depth,sizeof(int));
  134. /* data structure for the maximum abs value of the coeffs. */
  135. wvt_tree_maxval = (WINT *)calloc(len_tree_struct - (1<<(2*(levels-1))),
  136.    sizeof(WINT));
  137.     /* location map for reading wavelet coefficients */
  138. hloc_map=(int *)calloc(len_tree_struct,sizeof(int));
  139. vloc_map=(int *)calloc(len_tree_struct,sizeof(int));
  140. hloc_map[0]=0;
  141. vloc_map[0]=0;
  142. for(i=1;i<tree_depth;i++){
  143. npix=1<<(2*(i-1));
  144. pos=level_pos[i];
  145. start=level_pos[i-1];
  146. end=level_pos[i];
  147. for(j=start;j<end;j++)
  148. {
  149. hpos_start=2*hloc_map[j];
  150. vpos_start=2*vloc_map[j];
  151. hpos_end=hpos_start+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. hloc_map[pos]=x;
  156. vloc_map[pos]=y;
  157. pos++;
  158. }
  159. }  /* end of j */
  160. }  /* end of i */
  161. /* scan trees */
  162. len=2*(len_tree_struct-(1<<(2*(levels-1))));
  163. ScanTrees = (short *)calloc(len,sizeof(short));
  164. next_ScanTrees = (short *)calloc(len,sizeof(short));
  165. /* significant coefficents */
  166. sig_pos = (short *)calloc(len_tree_struct,sizeof(short));
  167. sig_layer = (char *)calloc(len_tree_struct,sizeof(char));
  168. /* total number of significant coefficients */
  169. num_Sig = 0;
  170. /* previous zerotree status */
  171. prev_label = (unsigned char*)calloc(len_tree_struct,sizeof(char));
  172. /* pointers to bitstream buffer */
  173. PEZW_bitstream = (unsigned char***) calloc(tree_depth,sizeof(char **));
  174.     for(i=0;i<tree_depth;i++){
  175. PEZW_bitstream[i]=(unsigned char**)calloc(Max_Bitplane,sizeof(char *));
  176.         for(bpos=Max_Bitplane-1;bpos>=0;bpos--)
  177.         PEZW_bitstream[i][bpos] = (unsigned char *) calloc(Init_Bufsize[i][bpos]
  178.  ,sizeof(char));
  179.     }
  180. /* arithmetic encoder structure */
  181. Encoder = (Ac_encoder **)calloc(tree_depth,sizeof(Ac_encoder *));
  182. for(i=0;i<tree_depth;i++)
  183. Encoder[i]=(Ac_encoder *)calloc(Max_Bitplane, sizeof(Ac_encoder));
  184.     /* context models */
  185. context_model = (Ac_model *)calloc(Max_Bitplane*levels*NumContexts,sizeof(Ac_model));
  186. for(bpos=Max_Bitplane-1;bpos>=0;bpos--)
  187.   for(i=0;i<tree_depth;i++)
  188. for(j=0;j<NumContext_per_pixel;j++)
  189. for (band=0;band<NumBands;band++)
  190. {
  191.                 nsym = 4;
  192. contexts=bpos*tree_depth*NumContexts+i*NumContexts+j*NumBands+band;
  193. if ((i==tree_depth-1)||(j==IZER))
  194.   freq = freq_dom2_IZER;   
  195. else
  196.   freq = freq_dom_ZTRZ;
  197. Ac_model_init (&context_model[contexts], nsym, freq, 
  198. Max_frequency_TI, adapt);
  199. }
  200. model_sub  = (Ac_model *)calloc(tree_depth*MAX_BITPLANE,sizeof(Ac_model));
  201. model_sign = (Ac_model *)calloc(tree_depth*MAX_BITPLANE,sizeof(Ac_model));
  202.     for(i=0;i<tree_depth*MAX_BITPLANE;i++){
  203. Ac_model_init (&model_sub[i],nsym,freq_dom2_IZER, Max_frequency_TI, adapt);
  204. Ac_model_init (&model_sign[i],nsym,freq_dom2_IZER, Max_frequency_TI, adapt);
  205. }    
  206.     return;
  207. }
  208. /* end of coding processing (for each layer):
  209.    flush the arithmetic coder  
  210.    output the pointer to the bitstream and
  211.    the lenght of the bitstream 
  212.    
  213.    bitstream and Init_Bufsize are the
  214.    return parameters (global variables )      */
  215. void PEZW_encode_done ()
  216. {
  217.     Int i, j;
  218.     Int Mbplane=0;
  219.     UChar *spbuffer;
  220.     Int bpos, band, contexts;
  221. /* determin the maximum AC bitplane */
  222.     Mbplane=0;
  223. for(i=Max_Bitplane-1;i>=0;i--){
  224. if(MaxValue>>i){
  225. Mbplane=i;
  226. break;
  227. }
  228. }
  229.     if(MaxValue==0)
  230.         Max_Bitplane=0;
  231.     else
  232.      Max_Bitplane=Mbplane+1;
  233. for(i=0;i<tree_depth;i++)
  234. {
  235.   for(j=Max_Bitplane-1;j>=Min_Bitplane;j--)
  236. {
  237. Ac_encoder_done (&Encoder[i][j]);
  238.             bits_to_go_inBuffer[i][j] = Encoder[i][j].bits_to_go;
  239.                 PEZW_bitstream[i][j] = Encoder[i][j].original_stream;
  240. spbuffer = Encoder[i][j].stream;
  241.                 Init_Bufsize[i][j] = spbuffer - PEZW_bitstream[i][j];
  242. }
  243. }
  244. /* free memory */
  245. free(level_pos);
  246. free(bitplane);
  247.     free(snr_weight);
  248. free(the_wvt_tree); 
  249. free(abs_wvt_tree);
  250. free(wvt_tree_maxval);
  251. free(maskbit);
  252. free(ScanTrees);
  253. free(next_ScanTrees);
  254. free(sig_pos);
  255. free(sig_layer);
  256. free(prev_label);
  257. for(i=0;i<tree_depth;i++){
  258. free(Encoder[i]);
  259. }
  260. free(Encoder);
  261. for(bpos=Max_Bitplane-1;bpos>=0;bpos--)
  262.   for(i=0;i<tree_depth;i++)
  263. for(j=0;j<NumContext_per_pixel;j++)
  264. for (band=0;band<NumBands;band++)
  265. {
  266. contexts=bpos*tree_depth*NumContexts+i*NumContexts+j*NumBands+band;
  267. AC_free_model(&context_model[contexts]);
  268. }
  269.     for(i=0;i<tree_depth*MAX_BITPLANE;i++){
  270. AC_free_model(&model_sign[i]);
  271. AC_free_model(&model_sub[i]);
  272. }
  273. free(model_sign);
  274. free(model_sub);
  275. free(context_model);
  276. #ifdef DEBUG_FILE
  277. fp_debug = fclose(fp_debug);
  278. #endif
  279. return;
  280. }
  281. void setbuffer_PEZW_encode ()
  282. {
  283.     Int bpos, i;
  284. /* set the bitstream buffer for encoder */
  285.     for(i=0;i<tree_depth;i++)
  286.         for(bpos=Max_Bitplane-1;bpos>=0;bpos--)
  287.         {
  288. #ifdef BILEVEL_AC
  289. buffer_ptr[i][bpos] = bitstream[i][bpos];
  290. bpinitencode (&BAC_Encoder[i][bpos],bpputbyte);
  291. #else
  292. Ac_encoder_init (&Encoder[i][bpos],PEZW_bitstream[i][bpos],
  293.             Init_Bufsize[i][bpos], 1);
  294. #endif
  295. }
  296. }
  297. void reset_PEZW_encode ()
  298. {
  299.     Int bpos;
  300.     Int i,j, band;
  301.     Int contexts;
  302.     Int nsym = 0;
  303.     Int adapt=1;
  304.     int *freq;
  305.     int len;
  306.     unsigned char *spbuffer;
  307.     /* context models */
  308. for(bpos=Max_Bitplane-1;bpos>=0;bpos--)
  309.   for(i=0;i<tree_depth;i++)
  310. for(j=0;j<NumContext_per_pixel;j++)
  311. for (band=0;band<NumBands;band++)
  312. {
  313.                 nsym = 4;
  314. contexts=bpos*tree_depth*NumContexts+i*NumContexts+j*NumBands+band;
  315. if ((i==tree_depth-1)||(j==IZER))
  316.   freq = freq_dom2_IZER;   
  317. else
  318.   freq = freq_dom_ZTRZ;
  319. AC_free_model(&context_model[contexts]);
  320. Ac_model_init (&context_model[contexts], nsym, freq, 
  321. Max_frequency_TI, adapt);
  322. }
  323.        for(i=0;i<tree_depth*MAX_BITPLANE;i++){
  324.     AC_free_model(&model_sub[i]);
  325.     AC_free_model(&model_sign[i]);
  326.     Ac_model_init (&model_sub[i],nsym,freq_dom2_IZER, Max_frequency_TI, adapt);
  327.     Ac_model_init (&model_sign[i],nsym,freq_dom2_IZER, Max_frequency_TI, adapt);
  328.     }    
  329.     /* re-initialize arithmetic coder */
  330. for(i=0;i<tree_depth;i++)
  331. {
  332.   for(j=Max_Bitplane-1;j>=Min_Bitplane;j--)
  333. {
  334.             /* flush the arithmetic coder */
  335. Ac_encoder_done (&Encoder[i][j]);
  336.                 PEZW_bitstream[i][j] = Encoder[i][j].original_stream;
  337. spbuffer = Encoder[i][j].stream;
  338.                 /* set new buffer position
  339.                    and initialize AC coders */
  340.                 len = Encoder[i][j].space_left;
  341.         Ac_encoder_init (&Encoder[i][bpos],spbuffer,
  342.                     len, 1);
  343.   }
  344. }
  345.     return;
  346. }