~ubuntu-branches/ubuntu/precise/mpeg4ip/precise

« back to all changes in this revision

Viewing changes to common/video/iso-mpeg4/src/vtc_pezw_wvtpezw_tree_decode.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mario Limonciello
  • Date: 2008-01-12 15:59:56 UTC
  • Revision ID: james.westby@ubuntu.com-20080112155956-1vznw5njidvrh649
Tags: upstream-1.6dfsg
ImportĀ upstreamĀ versionĀ 1.6dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
/****************************************************************************/
 
51
/*     Texas Instruments Predictive Embedded Zerotree (PEZW) Image Codec    */
 
52
/*         Developed by Jie Liang (liang@ti.com)                                */
 
53
/*                                                                                                                              */ 
 
54
/*     Copyright 1996, 1997, 1998 Texas Instruments                                 */
 
55
/****************************************************************************/
 
56
 
 
57
/****************************************************************************
 
58
   File name:         wvtpezw_tree_decode.c
 
59
   Author:            Jie Liang  (liang@ti.com)
 
60
   Functions:         Core functions for decoding the tree blocks of coeffs.
 
61
   Revisions:         v1.0 (10/04/98)
 
62
*****************************************************************************/
 
63
 
 
64
#include "wvtPEZW.hpp"
 
65
#include "PEZW_zerotree.hpp"
 
66
#include "wvtpezw_tree_codec.hpp"
 
67
#include "PEZW_functions.hpp"
 
68
 
 
69
/* decode a block of wavelet coefficients */
 
70
 
 
71
void PEZW_decode_block (WINT **coeffsBlk, Int width, Int height)
 
72
{
 
73
    Int dc_hsize, dc_vsize;
 
74
    Int i,j,k,m,n,band, hpos, vpos;
 
75
    int pos;
 
76
        int hpos_start, hpos_end;
 
77
        int vpos_start, vpos_end;
 
78
    Int lev, npix;
 
79
    Int hstart, vstart,start, end;
 
80
    Int x,y;
 
81
    Char SignBit;
 
82
    Int levels;
 
83
 
 
84
#ifdef DEBUG_FILE
 
85
    Int NumTree=0;
 
86
#endif
 
87
 
 
88
    levels = tree_depth;
 
89
    dc_hsize = width>>levels;
 
90
    dc_vsize = height>>levels;
 
91
 
 
92
        /* decode the AC coefficients:
 
93
       loop over all zerotrees */
 
94
 
 
95
        for(i=0;i<dc_vsize;i++)
 
96
                for (j=0;j<dc_hsize;j++)
 
97
                        for(band=0;band<3;band++)
 
98
                        {
 
99
                                if(band==0)  { /* HL band */
 
100
                                        hpos = j+dc_hsize;
 
101
                                        vpos = i;
 
102
                                }
 
103
                                else if (band==1) { /* LH band */
 
104
                                        hpos = j;
 
105
                                        vpos = i+dc_vsize;
 
106
                                }
 
107
                                else{
 
108
                                        hpos = j+dc_hsize;
 
109
                                        vpos = i+dc_vsize;
 
110
                                }
 
111
 
 
112
                                y=vpos;x=hpos;
 
113
 
 
114
                                /* determin weighting */
 
115
                                for (lev=0;lev<levels;lev++)
 
116
                                        snr_weight[lev] = 0;
 
117
 
 
118
                                /* initialization */
 
119
                                num_Sig=0;
 
120
                                the_wvt_tree[0]=0;
 
121
                                prev_label[0]=ZTRZ;
 
122
                                for(m=1;m<len_tree_struct;m++){
 
123
                                        the_wvt_tree[m]=0;
 
124
                                        prev_label[m]=DZ;
 
125
                                }
 
126
 
 
127
#ifdef DEBUG_FILE
 
128
                                NumTree++;
 
129
                                if(DEBUG_SYMBOL)
 
130
                                        fprintf(fp_debug,"\n***NEW TREE %d***", NumTree);
 
131
#endif
 
132
                                /* encoder one wavelet tree. this function demonstrates that 
 
133
                                the requirement for memory is very low for this entropy coder.
 
134
                                if combined with a localized wavelet transform, the total
 
135
                                memory requirement will be extremely low. */
 
136
                                PEZW_tree_decode (band,the_wvt_tree,snr_weight);
 
137
 
 
138
                                /* dequantize the coefficients */
 
139
                                for(k=0;k<num_Sig;k++){
 
140
                                        SignBit=sign_bit[k];
 
141
                                        n=sig_pos[k];
 
142
                                        m=the_wvt_tree[n];
 
143
                                        the_wvt_tree[n]=(SignBit>0)?m:-m;
 
144
                                }
 
145
 
 
146
#ifdef DEBUG_FILE
 
147
                                if(DEBUG_VALUE){
 
148
                                        fprintf(fp_debug,"\n***NEW TREE %d *****\n",NumTree);
 
149
                                        fprintf(fp_debug,"%d ",the_wvt_tree[0]);
 
150
                                }
 
151
#endif
 
152
 
 
153
                                /* copy the data from wvt_tree to output */
 
154
                                coeffsBlk[y][x]=the_wvt_tree[0];
 
155
                                for (lev=1;lev<levels;lev++)
 
156
                                        {
 
157
                                                npix=1<<(2*(lev-1));
 
158
                                                pos = level_pos[lev];
 
159
                                                hstart=hpos*(1<<(lev-1));
 
160
                                                vstart=vpos*(1<<(lev-1));
 
161
                                                start=level_pos[lev-1];
 
162
                                                end=level_pos[lev];
 
163
 
 
164
                                                for(k=start;k<end;k++)
 
165
                                                        {
 
166
                                                                hpos_start = (hstart+hloc_map[k])*2;
 
167
                                                                hpos_end = hpos_start+2;
 
168
                                                                vpos_start = (vstart+vloc_map[k])*2;
 
169
                                                                vpos_end = vpos_start+2;
 
170
                                                                for(y=vpos_start;y<vpos_end;y++)
 
171
                                                                        for(x=hpos_start;x<hpos_end;x++){
 
172
                                                                                coeffsBlk[y][x]=the_wvt_tree[pos++];
 
173
#ifdef DEBUG_FILE
 
174
                                                                                if(DEBUG_VALUE){
 
175
                                                                                        fprintf(fp_debug,"%d ",coeffsBlk[y][x]);
 
176
                                                                                        fflush(fp_debug);
 
177
                                                                                }
 
178
#endif
 
179
                                                                        }
 
180
                                                                } /* end of k */
 
181
                                        } /* end of for(lev) */                         
 
182
                        } /* end of band */
 
183
 
 
184
}
 
185
 
 
186
 
 
187
/* decode a tree structure */
 
188
void PEZW_tree_decode (int band,WINT *wvt_tree,int *snr_weight)
 
189
{
 
190
        int i,j,k,m,n;
 
191
        int npix;
 
192
        int bplane;
 
193
        int scan_tree_done;
 
194
        int context;
 
195
        unsigned char label;
 
196
        int num_ScanTree, next_numScanTree;
 
197
        int temp1;
 
198
        short *temp_ptr;
 
199
        char Bit;
 
200
        //      char IsStream=1;
 
201
        unsigned char bpos;
 
202
        char skip_flag;
 
203
 
 
204
#define AC_offset 3
 
205
 
 
206
        for(bplane=Max_Bitplane-1;bplane>=Min_Bitplane;bplane--){
 
207
#ifdef DEBUG_FILE
 
208
                if(DEBUG_SYMBOL)
 
209
                        fprintf(fp_debug,"\n****bitplane: %d",bplane);
 
210
#endif
 
211
            /* determine bitplane */
 
212
                skip_flag=0;
 
213
            for(i=0;i<tree_depth-spatial_leveloff;i++){
 
214
                        bitplane[i] = bplane+snr_weight[i];
 
215
                        if(bitplane[i]>=Max_Bitplane)
 
216
                                skip_flag=1;
 
217
                }
 
218
                if(skip_flag)
 
219
                        continue;   /* skip this bitplane */
 
220
 
 
221
                /* receive refinement bits */
 
222
                if(bplane<Max_Bitplane-1){
 
223
                        for(i=0;i<num_Sig;i++){
 
224
                                m=sig_layer[i];
 
225
                n = bitplane[m];
 
226
 
 
227
                /* context for refinement bit */
 
228
                context = m*MAX_BITPLANE+bitplane[m];
 
229
 
 
230
                /* rate control */
 
231
// #define FLAG reach_budget[m][n] deleted by swinder
 
232
                if(reach_budget[m][n]==1)
 
233
                    continue;                   
 
234
 
 
235
                                Bit=Ac_decode_symbol(&Decoder[m][bitplane[m]],
 
236
                    &model_sub[context]);
 
237
               /* rate control */
 
238
               if( (Decoder[m][n].stream - PEZW_bitstream[m][n]) >= Init_Bufsize[m][n]+AC_offset){
 
239
                       reach_budget[m][n] = 1;
 
240
                       return;
 
241
               }
 
242
 
 
243
                           wvt_tree[sig_pos[i]] |= (Bit<<bitplane[m]);
 
244
                        }
 
245
                }
 
246
 
 
247
                /* form the zerotree and encode the zerotree symbols
 
248
                   and encode the sign bit for new sigifnicant coeffs
 
249
                   ScanTree: the intervals of coefficients that are 
 
250
                             not descendants of zerotree roots.
 
251
                   Use ScanTree and next_ScanTree to alternate as 
 
252
                   buffer, so that we do not need to spend the cycles
 
253
                   to copy from one buffer to the other. 
 
254
 
 
255
                        numScanTree and next_numScanTrees are the number 
 
256
                        of intervals that need to be looked at.
 
257
                */
 
258
 
 
259
                next_numScanTree=1;
 
260
                next_ScanTrees[0]=0;
 
261
                next_ScanTrees[1]=1;
 
262
                for(i=0;i<tree_depth-spatial_leveloff;i++){
 
263
 
 
264
#ifdef DEBUG_FILE
 
265
                if(DEBUG_SYMBOL)
 
266
                        fprintf(fp_debug,"\n****tree_depth %d:  ",i);
 
267
#endif
 
268
                        scan_tree_done=1;
 
269
                        num_ScanTree=next_numScanTree;
 
270
                        next_numScanTree=0;
 
271
                        temp_ptr=ScanTrees;
 
272
                        ScanTrees=next_ScanTrees;
 
273
                        next_ScanTrees=temp_ptr;                        
 
274
                        bpos=bitplane[i];
 
275
 
 
276
            /* rate control */
 
277
// #define FLAG reach_budget[i][bpos] deleted by swinder
 
278
            if(reach_budget[i][bpos]==1)
 
279
                    break; 
 
280
 
 
281
                        npix=1<<(2*i);
 
282
                        for(j=0;j<num_ScanTree;j++)
 
283
                          for(k=ScanTrees[2*j];k<ScanTrees[2*j+1];k++)
 
284
                          {
 
285
                                /* decide the zerotree symbol */
 
286
                                if((prev_label[k]==IVAL)||(prev_label[k]==ZTRV)){
 
287
#ifdef DEBUG_FILE
 
288
                            if(DEBUG_SYMBOL){
 
289
                                    fprintf(fp_debug,"SKIP%d ", prev_label[k]);
 
290
                                    fflush(fp_debug);
 
291
                            }
 
292
#endif
 
293
 
 
294
                                        if(i<tree_depth-1){
 
295
                                                /* set upt the scann tree for next level */
 
296
                                                temp1=2*next_numScanTree;
 
297
                                                next_ScanTrees[temp1]=level_pos[i+1]+4*(k-level_pos[i]);
 
298
                                                next_ScanTrees[temp1+1]=next_ScanTrees[temp1]+4;
 
299
                                                next_numScanTree++;
 
300
                                                scan_tree_done=0;
 
301
                                        }
 
302
 
 
303
                                        continue;
 
304
                                }
 
305
                                else{
 
306
                                        /* decode the zerotree symbol */
 
307
                                    context=bpos*NumContexts*tree_depth+i*NumContexts+
 
308
                                        prev_label[k]*NumBands+band;
 
309
                                    label = Ac_decode_symbol(&Decoder[i][bpos],&context_model[context]);                                  
 
310
 
 
311
                    /* rate control */
 
312
                    if( (Decoder[i][bpos].stream - PEZW_bitstream[i][bpos]) >= Init_Bufsize[i][bpos]+AC_offset){
 
313
                           reach_budget[i][bpos] = 1;
 
314
                           return;
 
315
                    }
 
316
 
 
317
#ifdef DEBUG_FILE
 
318
                if(DEBUG_SYMBOL){
 
319
                        fprintf(fp_debug,"%d ", label);
 
320
                        fflush(fp_debug);
 
321
                }
 
322
#endif
 
323
 
 
324
 
 
325
                    /* context for sign model */
 
326
                    context = i*MAX_BITPLANE+bpos;
 
327
 
 
328
                                        if((label==IVAL)||(label==IZER)){
 
329
                                                scan_tree_done=0;
 
330
                                                if(label==IVAL){                                                        
 
331
                                                          Bit=1;
 
332
                                                          wvt_tree[k] |= (Bit<<bitplane[i]);
 
333
 
 
334
                                                          sig_pos[num_Sig]=k;
 
335
                                                      sig_layer[num_Sig]=i;
 
336
 
 
337
                                                          sign_bit[num_Sig]= Ac_decode_symbol(&Decoder[i][bpos],&model_sign[context]);  
 
338
                              if( (Decoder[i][bpos].stream - PEZW_bitstream[i][bpos]) >= Init_Bufsize[i][bpos]+AC_offset){
 
339
                                  reach_budget[i][bpos] = 1;
 
340
                                  return;
 
341
                              }
 
342
 
 
343
 
 
344
#ifdef DEBUG_FILE
 
345
                                                          if(DEBUG_SYMBOL){
 
346
                                                                if(sign_bit[num_Sig]==1)
 
347
                                                                        fprintf(fp_debug,"%d+ ",sign_bit[num_Sig]);
 
348
                                                                else
 
349
                                                                        fprintf(fp_debug,"%d- ",sign_bit[num_Sig]);
 
350
                                                                fflush(fp_debug);
 
351
                                                                }
 
352
#endif            
 
353
                                                        num_Sig++;
 
354
                                                }
 
355
 
 
356
                                                /* set upt the scann tree for next level */
 
357
                                                if(i<tree_depth-1){
 
358
                                                    temp1=2*next_numScanTree;
 
359
                                                        next_ScanTrees[temp1]=level_pos[i+1]+4*(k-level_pos[i]);
 
360
                                                        next_ScanTrees[temp1+1]=next_ScanTrees[temp1]+4;
 
361
                                                        next_numScanTree++;
 
362
                                                        scan_tree_done=0;
 
363
                                                }
 
364
                                        }
 
365
                                        else{
 
366
                                                if(label==ZTRV){
 
367
                                                          Bit=1;                                                  
 
368
                                                          wvt_tree[k] |= (Bit<<bitplane[i]);
 
369
                                                      sig_pos[num_Sig]=k;
 
370
                                                      sig_layer[num_Sig]=i;
 
371
 
 
372
                                                          sign_bit[num_Sig]= Ac_decode_symbol(&Decoder[i][bpos],&model_sign[context]);
 
373
                              if( (Decoder[i][bpos].stream - PEZW_bitstream[i][bpos]) >= Init_Bufsize[i][bpos]+AC_offset){
 
374
                                  reach_budget[i][bpos] = 1;
 
375
                                  return;
 
376
                              }
 
377
 
 
378
#ifdef DEBUG_FILE
 
379
                                                          if(DEBUG_SYMBOL){
 
380
                                                                if(sign_bit[num_Sig]==1)
 
381
                                                                        fprintf(fp_debug,"%d+ ",sign_bit[num_Sig]);
 
382
                                                                else
 
383
                                                                        fprintf(fp_debug,"%d- ",sign_bit[num_Sig]);
 
384
                                                                fflush(fp_debug);
 
385
                                                                }
 
386
#endif                  
 
387
                                                          num_Sig++;
 
388
                                                }
 
389
                                        } /* end of else */                     
 
390
 
 
391
                                        /* set up prev_label */
 
392
                                        prev_label[k]=label;
 
393
                                } /* end of if(prev_label */
 
394
                        } /* end of k */
 
395
                        
 
396
                        /* no subordinate none zero descendants */
 
397
                        if(scan_tree_done)
 
398
                          break;
 
399
                        
 
400
                }  /* end of tree depth */
 
401
                
 
402
        } /* end of bitplane */
 
403
        
 
404
        return;
 
405
}