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

« back to all changes in this revision

Viewing changes to common/video/iso-mpeg4/src/vtc_main_vtcenc.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
/*     Sarnoff Very Low Bit Rate Still Image Coder          */
 
52
/*     Copyright 1995, 1996, 1997, 1998 Sarnoff Corporation */
 
53
/************************************************************/
 
54
 
 
55
#include <stdlib.h>
 
56
#include <stdio.h>
 
57
#include <math.h>
 
58
#include <assert.h>
 
59
// begin: added by Sharp (99/2/16)
 
60
#include <iostream>
 
61
#include <sys/stat.h>
 
62
// end: added by Sharp (99/2/16)
 
63
 
 
64
 
 
65
#include "basic.hpp"
 
66
#include "dataStruct.hpp"
 
67
#include "startcode.hpp"
 
68
#include "bitpack.hpp"
 
69
#include "msg.hpp"
 
70
#include "globals.hpp"
 
71
#include "states.hpp" // hjlee 0901
 
72
 
 
73
 
 
74
 
 
75
CVTCEncoder::CVTCEncoder()
 
76
{
 
77
        m_cImagePath = new Char[80];
 
78
        m_cSegImagePath = new Char[80];
 
79
        m_cOutBitsFile = new Char[80];
 
80
        mzte_codec.m_cBitFile = new Char[80];
 
81
}
 
82
 
 
83
CVTCEncoder::~CVTCEncoder()
 
84
{
 
85
        delete m_cImagePath;
 
86
        delete m_cSegImagePath;
 
87
        delete m_cOutBitsFile;
 
88
        delete mzte_codec.m_cBitFile;
 
89
}
 
90
 
 
91
//CVTCEncoder::CVTCEncoder(
 
92
Void CVTCEncoder::init(
 
93
                UInt uiVerID, // added by Sharp (99/11/18)
 
94
                Char* cImagePath,
 
95
            UInt uiAlphaChannel,
 
96
                Char* cSegImagePath,
 
97
                UInt uiAlphaTh,
 
98
                UInt uiChangeCRDisable,
 
99
// FPDAM begin: deleted by Sharp
 
100
//              UInt uiShapeScalable, //added by SL@Sarnoff (03/02/99)
 
101
// FPDAM end: deleted by Sharp
 
102
                UInt uiSTOConstAlpha, //added by SL@Sarnoff (03/03/99)
 
103
                UInt uiSTOConstAlphaValue, //added by SL@Sarnoff (03/02/99)
 
104
                Char* cOutBitsFile,
 
105
                UInt uiColors,
 
106
                UInt uiFrmWidth,
 
107
                UInt uiFrmHeight,
 
108
// begin: added by Sharp (99/2/16)
 
109
    UInt uiTilingDisable,
 
110
                UInt uiTilingJump,
 
111
    UInt uiTileWidth,
 
112
    UInt uiTileHeight,
 
113
// end: added by Sharp (99/2/16)
 
114
                UInt uiWvtType,
 
115
                UInt uiWvtDownload,  // hjlee 0901
 
116
                UInt uiWvtDecmpLev,
 
117
                UInt uiWvtUniform, // hjlee 0901
 
118
                Int* iWvtFilters,  // hjlee 0901
 
119
                UInt uiQuantType,
 
120
                UInt uiScanDirection,
 
121
                Bool bStartCodeEnable,
 
122
                UInt uiTargetSpatialLev,
 
123
                UInt uiTargetSNRLev,
 
124
                UInt uiTargetShapeLev, //added by SL@Sarnoff (03/02/99)
 
125
                UInt uiFullSizeOut, //added by SL@Sarnoff (03/02/99)
 
126
// begin: added by Sharp (99/2/16)
 
127
    UInt uiTargetTileFrom,
 
128
    UInt uiTargetTileTo,
 
129
// end: added by Sharp (99/2/16)
 
130
                UInt uiQdcY,
 
131
                UInt uiQdcUV,
 
132
                UInt uiSpatialLev ,
 
133
                UInt defaultSpatialScale, // hjlee 0901
 
134
                Int  lastWvtDecompInSpaLayer[MAXDECOMPLEV], // hjlee 0901
 
135
                SNR_PARAM* Qinfo[],
 
136
//Added by Sarnoff for error resilience, 3/5/99
 
137
                Int uiErrResiDisable,   //bbc, 2/19/99
 
138
                Int uiPacketThresh,  
 
139
                Int uiSegmentThresh 
 
140
//End: Added by Sarnoff for error resilience, 3/5/99
 
141
        )
 
142
{
 
143
 
 
144
        // input/output
 
145
 
 
146
        mzte_codec.m_visual_object_verid = uiVerID; // added by Sharp(99/11/18)
 
147
        strcpy(m_cImagePath,cImagePath);
 
148
        strcpy(m_cSegImagePath,cSegImagePath);
 
149
        strcpy(m_cOutBitsFile,cOutBitsFile);
 
150
        strcpy(mzte_codec.m_cBitFile,cOutBitsFile);
 
151
        
 
152
        mzte_codec.m_iAlphaChannel = (int)uiAlphaChannel;
 
153
        mzte_codec.m_iAlphaTh = (int)uiAlphaTh;
 
154
        mzte_codec.m_iChangeCRDisable = (int)uiChangeCRDisable;
 
155
        //begin: added by SL@Sarnoff (03/02/99)
 
156
// FPDAM begin: modified by Sharp
 
157
        mzte_codec.m_iShapeScalable = 1; // (int)uiShapeScalable;
 
158
// FPDAM end: modified by Sharp
 
159
        STO_const_alpha=mzte_codec.m_iSTOConstAlpha = (int)uiSTOConstAlpha;
 
160
        STO_const_alpha_value=mzte_codec.m_iSTOConstAlphaValue = (int)uiSTOConstAlphaValue;
 
161
        //end: added by SL@Sarnoff (03/02/99)
 
162
        mzte_codec.m_iBitDepth = 8;
 
163
        mzte_codec.m_iColorFormat = 0;
 
164
        mzte_codec.m_iColors = (int)uiColors;
 
165
        mzte_codec.m_iWidth = (int)uiFrmWidth;
 
166
        mzte_codec.m_iHeight = (int)uiFrmHeight;
 
167
        mzte_codec.m_iWvtType = (int)uiWvtType;
 
168
        mzte_codec.m_iWvtDecmpLev = (int)uiWvtDecmpLev;
 
169
        mzte_codec.m_iQuantType = (int)uiQuantType;
 
170
        mzte_codec.m_iScanDirection = (int)uiScanDirection;
 
171
        mzte_codec.m_bStartCodeEnable = bStartCodeEnable;
 
172
        mzte_codec.m_iSpatialLev = (int)uiSpatialLev;
 
173
        mzte_codec.m_iTargetSpatialLev = (int)uiTargetSpatialLev;
 
174
        mzte_codec.m_iTargetSNRLev = (int)uiTargetSNRLev;
 
175
// begin: added by SL@Sarnoff (03/02/99)
 
176
        mzte_codec.m_iTargetShapeLev = (int) uiTargetShapeLev;
 
177
        mzte_codec.m_iFullSizeOut = (int)uiFullSizeOut;
 
178
// end: added by SL@Sarnoff (03/02/99)
 
179
// begin: added by Sharp (99/2/16)
 
180
  mzte_codec.m_tiling_disable = (int)uiTilingDisable;
 
181
        mzte_codec.m_tiling_jump_table_enable = (int)uiTilingJump;
 
182
  mzte_codec.m_tile_width = (int)uiTileWidth;
 
183
  mzte_codec.m_tile_height = (int)uiTileHeight;
 
184
  mzte_codec.m_target_tile_id_from = (int)uiTargetTileFrom;
 
185
  mzte_codec.m_target_tile_id_to = (int)uiTargetTileTo;
 
186
// end: added by Sharp (99/2/16)
 
187
 
 
188
        mzte_codec.m_iWvtDownload = (int)uiWvtDownload; // hjlee 0901
 
189
        mzte_codec.m_iWvtUniform = (int)uiWvtUniform;// hjlee 0901
 
190
        mzte_codec.m_WvtFilters = (int *)iWvtFilters; // hjlee 0901
 
191
        mzte_codec.m_defaultSpatialScale = (int)defaultSpatialScale; // hjlee 0901
 
192
        if (uiQuantType == 2) {
 
193
                if (uiSpatialLev != uiWvtDecmpLev) {
 
194
                  /* read in usedefault */
 
195
                  mzte_codec.m_defaultSpatialScale=(int)defaultSpatialScale;
 
196
                  if (defaultSpatialScale==0) {
 
197
                        int spa_lev;
 
198
 
 
199
                        /* read in spatial layer info */
 
200
                        for (spa_lev=0; spa_lev<(int)uiSpatialLev-1; ++spa_lev)   
 
201
                          mzte_codec.m_lastWvtDecompInSpaLayer[spa_lev][0]
 
202
                                = lastWvtDecompInSpaLayer[spa_lev];
 
203
                  }
 
204
                }
 
205
        }
 
206
 
 
207
        for (int i=0; i<(int)uiColors; i++)
 
208
                mzte_codec.m_Qinfo[i] = Qinfo[i];
 
209
 
 
210
        mzte_codec.m_iQDC[0] = (int)uiQdcY;
 
211
        mzte_codec.m_iQDC[1] = mzte_codec.m_iQDC[2] = (int)uiQdcUV;
 
212
 
 
213
/*      mzte_codec.m_iDCWidth = GetDCWidth();
 
214
        mzte_codec.m_iDCHeight = GetDCHeight(); */ //deleted by SL 030499
 
215
  
 
216
        mzte_codec.m_iSingleBitFile = 1;
 
217
//      mzte_codec.m_cBitFile = NULL;
 
218
        mzte_codec.m_cBitFileAC = NULL;
 
219
 
 
220
        // check parameter
 
221
        if (mzte_codec.m_iQuantType==1 && mzte_codec.m_iScanDirection==0)
 
222
                mzte_codec.m_bStartCodeEnable = 0;
 
223
 
 
224
//Added by Sarnoff for error resilience, 3/5/99
 
225
        //disable startcode in error resilience case
 
226
        if((mzte_codec.m_usErrResiDisable=(UShort)uiErrResiDisable)==0)
 
227
                mzte_codec.m_bStartCodeEnable=0;  
 
228
        mzte_codec.m_usPacketThresh=(UShort)uiPacketThresh; 
 
229
        mzte_codec.m_usSegmentThresh=(UShort)uiSegmentThresh;
 
230
//End: Added by Sarnoff for error resilience, 3/5/99
 
231
}
 
232
 
 
233
 
 
234
/*******************************************************/
 
235
/*  Flush buffer and close file. Only and must be used */
 
236
/*   by encoder to close bitstream file                */
 
237
/*******************************************************/
 
238
Void CVTCEncoder::flush_buffer_file()
 
239
{
 
240
  flush_bits();
 
241
  flush_bytes();
 
242
}
 
243
 
 
244
Void CVTCEncoder::close_buffer_file(FILE *fp)
 
245
{
 
246
  flush_buffer_file();
 
247
  fclose(fp);
 
248
}
 
249
 
 
250
Void CVTCEncoder::texture_packet_header_Enc(FILTER **wvtfilter) // hjlee 0901 
 
251
{
 
252
  Int  texture_object_id=0;
 
253
  //  Int  texture_object_layer_shape=mzte_codec.m_iAlphaChannel;
 
254
  //  Int  wavelet_stuffing = 0x0f;
 
255
//  Int  wavelet_upload;
 
256
//  Int  wavelet_uniform;  // hjlee 0901
 
257
//  long tile_table_position=0;
 
258
 
 
259
  if(!mzte_codec.m_usErrResiDisable){
 
260
                flush_bits(); // added by Sharp(99/3/29)
 
261
                texture_object_id=1<<15; // error resilience
 
262
 
 
263
                prev_TU_first = prev_TU_last = prev_TU_err = -1;
 
264
                flush_bytes(); // to get alignment in buffer
 
265
                emit_bits((UShort)1,2); // 1st bit dummy, 2nd bit HEC=1
 
266
                packet_size=0;
 
267
  }
 
268
//End: Added by Sarnoff for error resilience, 3/5/99
 
269
 
 
270
        header_Enc_Common( wvtfilter ,1);
 
271
 
 
272
//Added by Sarnoff for error resilience, 3/5/99
 
273
  if(!mzte_codec.m_usErrResiDisable) {
 
274
          emit_bits((UShort)mzte_codec.m_usSegmentThresh,16);
 
275
          emit_bits((UShort)MARKER_BIT, 1); // added for FDAM1 by Samsung AIT on 2000/02/03
 
276
  }
 
277
//End: Added by Sarnoff for error resilience, 3/5/99
 
278
}
 
279
 
 
280
Void CVTCEncoder::header_Enc_V1(FILTER **wvtfilter) // hjlee 0901
 
281
{
 
282
  Int  texture_object_id=0;
 
283
  Int  texture_object_layer_shape=mzte_codec.m_iAlphaChannel;
 
284
  Int  wavelet_stuffing = 0x0f;
 
285
//  Int  wavelet_upload;
 
286
  Int  wavelet_uniform;  // hjlee 0901
 
287
  Int i; // hjlee 0901
 
288
 
 
289
  /*------- Write header info to bitstream file -------*/
 
290
  emit_bits((UShort)(STILL_TEXTURE_OBJECT_START_CODE>>16), 16);
 
291
  emit_bits((UShort)STILL_TEXTURE_OBJECT_START_CODE, 16);
 
292
  emit_bits((UShort)texture_object_id, 16);
 
293
  emit_bits((UShort)MARKER_BIT, 1);
 
294
 
 
295
  emit_bits((UShort)(mzte_codec.m_iWvtType==0?0:1), 1); // hjlee 0901
 
296
  emit_bits((UShort)mzte_codec.m_iWvtDownload, 1); // hjlee 0901
 
297
  emit_bits((UShort)mzte_codec.m_iWvtDecmpLev, 4); // hjlee 0901
 
298
  emit_bits((UShort)mzte_codec.m_iScanDirection,1);
 
299
  emit_bits((UShort)mzte_codec.m_bStartCodeEnable, 1);
 
300
  emit_bits((UShort)texture_object_layer_shape, 2);
 
301
  emit_bits((UShort)mzte_codec.m_iQuantType, 2);
 
302
 
 
303
  /* hjlee 0901 */
 
304
  if (mzte_codec.m_iQuantType==2) {
 
305
    Int i;
 
306
    emit_bits((UShort)mzte_codec.m_iSpatialLev, 4); 
 
307
        
 
308
    /* Calc number decomp layers for all spatial layers */
 
309
    if (mzte_codec.m_iSpatialLev == 1)
 
310
    {
 
311
      mzte_codec.m_lastWvtDecompInSpaLayer[0][0]=mzte_codec.m_iWvtDecmpLev-1;
 
312
    }
 
313
    else if (mzte_codec.m_iSpatialLev != mzte_codec.m_iWvtDecmpLev)
 
314
    {
 
315
      emit_bits((UShort)mzte_codec.m_defaultSpatialScale, 1);
 
316
      if (mzte_codec.m_defaultSpatialScale==0)
 
317
      {
 
318
             /* For the 1st spatial_scalability_levels-1 layers the luma componant
 
319
                of lastWvtDecompInSpaLayer should have been filled in from the 
 
320
                parameter file. */
 
321
             for (i=0; i<mzte_codec.m_iSpatialLev-1; ++i)
 
322
                 emit_bits((UShort)mzte_codec.m_lastWvtDecompInSpaLayer[i][0], 4);
 
323
 
 
324
             mzte_codec.m_lastWvtDecompInSpaLayer[mzte_codec.m_iSpatialLev-1][0]
 
325
                 =mzte_codec.m_iWvtDecmpLev-1;
 
326
      }
 
327
      else
 
328
      {
 
329
                Int sp0;
 
330
 
 
331
                sp0=mzte_codec.m_iWvtDecmpLev-mzte_codec.m_iSpatialLev;
 
332
                mzte_codec.m_lastWvtDecompInSpaLayer[0][0]=sp0;
 
333
                  
 
334
                for (i=1; i<mzte_codec.m_iSpatialLev; ++i)
 
335
                  mzte_codec.m_lastWvtDecompInSpaLayer[i][0]=sp0+i;
 
336
      }
 
337
    }
 
338
    else
 
339
    {
 
340
      for (i=0; i<mzte_codec.m_iSpatialLev; ++i)
 
341
                mzte_codec.m_lastWvtDecompInSpaLayer[i][0]=i;
 
342
    }
 
343
    
 
344
    /* Calculate for chroma (one less than luma) */
 
345
    for (i=0; i<mzte_codec.m_iSpatialLev; ++i)
 
346
      mzte_codec.m_lastWvtDecompInSpaLayer[i][1]
 
347
                =mzte_codec.m_lastWvtDecompInSpaLayer[i][2]
 
348
                =mzte_codec.m_lastWvtDecompInSpaLayer[i][0]-1;
 
349
  }
 
350
 
 
351
 
 
352
  if (mzte_codec.m_iWvtDownload == 1) {
 
353
        // hjlee 0901
 
354
    wavelet_uniform = (mzte_codec.m_iWvtUniform!=0)?1:0;
 
355
    emit_bits((UShort)wavelet_uniform, 1);
 
356
    if(wavelet_uniform) {
 
357
      upload_wavelet_filters(wvtfilter[0]);
 
358
    }
 
359
    else {
 
360
      for(i=0;i<mzte_codec.m_iWvtDecmpLev;i++) 
 
361
            upload_wavelet_filters(wvtfilter[i]);       
 
362
        }
 
363
  }  
 
364
 
 
365
  emit_bits((UShort)wavelet_stuffing, 3);
 
366
 
 
367
  if (texture_object_layer_shape == 0x00) {
 
368
    emit_bits((UShort)mzte_codec.m_iRealWidth, 15);
 
369
    emit_bits((UShort)MARKER_BIT, 1);
 
370
    emit_bits((UShort)mzte_codec.m_iRealHeight, 15);
 
371
    emit_bits((UShort)MARKER_BIT, 1);
 
372
  }
 
373
  else { /* Arbitrary shape info, SL */    
 
374
    emit_bits((UShort)mzte_codec.m_iOriginX, 15);  /*horizontal_ref */    
 
375
    emit_bits((UShort)MARKER_BIT, 1); /* marker_bit */   
 
376
    emit_bits((UShort)mzte_codec.m_iOriginY, 15); /*vertical_ref */    
 
377
    emit_bits((UShort)MARKER_BIT, 1);  /* marker_bit */    
 
378
    emit_bits((UShort)mzte_codec.m_iWidth, 15);  /* object_width */    
 
379
    emit_bits((UShort)MARKER_BIT, 1);   /* marker_bit */    
 
380
    emit_bits((UShort)mzte_codec.m_iHeight, 15);  /* object_height */    
 
381
    emit_bits((UShort)MARKER_BIT, 1);  /* marker_bit */
 
382
    noteProgress("Merge Shape Bitstream ....");
 
383
    // MergeShapeBitstream();
 
384
  }
 
385
}
 
386
// begin: modified by Sharp (99/2/16)
 
387
//Void CVTCEncoder::header_Enc(FILTER *wvtfilter)  // hjlee 0901
 
388
long CVTCEncoder::header_Enc(FILTER **wvtfilter) // hjlee 0901 
 
389
{
 
390
  //  Int  texture_object_id=0;
 
391
  //  Int  texture_object_layer_shape=mzte_codec.m_iAlphaChannel;
 
392
  //  Int  wavelet_stuffing = 0x0f;
 
393
//  Int  wavelet_upload;
 
394
//  Int  wavelet_uniform;  // hjlee 0901
 
395
  Int i; // hjlee 0901
 
396
  long tile_table_position=0;
 
397
 
 
398
  /*------- Write header info to bitstream file -------*/
 
399
  emit_bits((UShort)(STILL_TEXTURE_OBJECT_START_CODE>>16), 16);
 
400
  emit_bits((UShort)STILL_TEXTURE_OBJECT_START_CODE, 16);
 
401
 
 
402
  emit_bits((UShort)mzte_codec.m_tiling_disable, 1); // added by Sharp (99/2/16)
 
403
//Added by Sarnoff for error resilience, 3/5/99
 
404
  emit_bits(mzte_codec.m_usErrResiDisable,1);  /* bbc, 2/18/99 */
 
405
//  flush_bits(); // deleted by Sharp (99/3/29)
 
406
// This part is moved to texture_packet_header_Enc, by Sharp (99/4/7)
 
407
#if 0
 
408
  if(!mzte_codec.m_usErrResiDisable){
 
409
                flush_bits(); // added by Sharp(99/3/29)
 
410
                texture_object_id=1<<15; // error resilience
 
411
 
 
412
                prev_TU_first = prev_TU_last = prev_TU_err = -1;
 
413
                flush_bytes(); // to get alignment in buffer
 
414
                emit_bits((UShort)1,2); // 1st bit dummy, 2nd bit HEC=1
 
415
                packet_size=0;
 
416
  }
 
417
//End: Added by Sarnoff for error resilience, 3/5/99
 
418
#endif
 
419
 
 
420
//      if ( mzte_codec.m_usErrResiDisable == 1) // added by Sharp (99/3/29) // @@@@@@
 
421
                header_Enc_Common( wvtfilter );
 
422
 
 
423
  if ( mzte_codec.m_tiling_disable == 0 ){
 
424
    emit_bits((UShort)mzte_codec.m_tile_width, 15); // modified by Sharp (99/11/16)
 
425
    emit_bits((UShort)1, 1);
 
426
    emit_bits((UShort)mzte_codec.m_tile_height, 15); // modified by Sharp (99/11/16)
 
427
    emit_bits((UShort)1, 1);
 
428
                emit_bits((UShort)mzte_codec.m_iNumOfTile, 16);
 
429
                emit_bits((UShort)1, 1);
 
430
/*              mzte_codec.m_tiling_jump_table_enable = 1;*/
 
431
    emit_bits((UShort)mzte_codec.m_tiling_jump_table_enable, 1);
 
432
 
 
433
    mzte_codec.m_extension_type = 0;
 
434
    used_bits = current_put_bits() % 8;
 
435
 
 
436
/*              printf("used_bits = %d\n", used_bits);*/
 
437
    flush_bytes();
 
438
    tile_table_position = current_fp();
 
439
 
 
440
                if ( mzte_codec.m_tiling_jump_table_enable == 1 )
 
441
                        for ( i=0; i<mzte_codec.m_iNumOfTile; i++ ) {
 
442
                                emit_bits((UShort)1, 34);
 
443
                        }
 
444
    /* next_start_code */
 
445
    emit_bits(0, 1);
 
446
    {
 
447
                        Int bits, data;
 
448
                        bits = current_put_bits();
 
449
                        bits = 8 - (bits % 8);
 
450
                        if (bits != 0 && bits != 8) {
 
451
                                        data = (1 << bits) - 1;
 
452
                                        emit_bits(data, bits);
 
453
                        }
 
454
    }
 
455
  }
 
456
 
 
457
  return tile_table_position;
 
458
}
 
459
// end: modified by Sharp (99/2/16)
 
460
 
 
461
// begin: added by Sharp (99/2/16)
 
462
Void CVTCEncoder::header_Enc_Common(FILTER **wvtfilter, Int SkipShape) // hjlee 0901
 
463
{
 
464
  Int  texture_object_id=0;
 
465
  Int  texture_object_layer_shape=mzte_codec.m_iAlphaChannel;
 
466
  Int  wavelet_stuffing = 0x0f;
 
467
//  Int  wavelet_upload;
 
468
  Int  wavelet_uniform;  // hjlee 0901
 
469
  Int i; // hjlee 0901
 
470
 
 
471
  /*------- Write header info to bitstream file -------*/
 
472
  emit_bits((UShort)texture_object_id, 16);
 
473
  emit_bits((UShort)MARKER_BIT, 1);
 
474
 
 
475
  emit_bits((UShort)(mzte_codec.m_iWvtType==0?0:1), 1); // hjlee 0901
 
476
  emit_bits((UShort)mzte_codec.m_iWvtDownload, 1); // hjlee 0901
 
477
  emit_bits((UShort)mzte_codec.m_iWvtDecmpLev, 4); // hjlee 0901
 
478
  emit_bits((UShort)mzte_codec.m_iScanDirection,1);
 
479
  emit_bits((UShort)mzte_codec.m_bStartCodeEnable, 1);
 
480
  emit_bits((UShort)texture_object_layer_shape, 2);
 
481
  emit_bits((UShort)mzte_codec.m_iQuantType, 2);
 
482
 
 
483
  /* hjlee 0901 */
 
484
  if (mzte_codec.m_iQuantType==2) {
 
485
    Int i;
 
486
    emit_bits((UShort)mzte_codec.m_iSpatialLev, 4); 
 
487
        
 
488
    /* Calc number decomp layers for all spatial layers */
 
489
    if (mzte_codec.m_iSpatialLev == 1)
 
490
    {
 
491
      mzte_codec.m_lastWvtDecompInSpaLayer[0][0]=mzte_codec.m_iWvtDecmpLev-1;
 
492
    }
 
493
    else if (mzte_codec.m_iSpatialLev != mzte_codec.m_iWvtDecmpLev)
 
494
    {
 
495
      emit_bits((UShort)mzte_codec.m_defaultSpatialScale, 1);
 
496
      if (mzte_codec.m_defaultSpatialScale==0)
 
497
      {
 
498
             /* For the 1st spatial_scalability_levels-1 layers the luma componant
 
499
                of lastWvtDecompInSpaLayer should have been filled in from the 
 
500
                parameter file. */
 
501
             for (i=0; i<mzte_codec.m_iSpatialLev-1; ++i)
 
502
                 emit_bits((UShort)mzte_codec.m_lastWvtDecompInSpaLayer[i][0], 4);
 
503
 
 
504
             mzte_codec.m_lastWvtDecompInSpaLayer[mzte_codec.m_iSpatialLev-1][0]
 
505
                 =mzte_codec.m_iWvtDecmpLev-1;
 
506
      }
 
507
      else
 
508
      {
 
509
                Int sp0;
 
510
 
 
511
                sp0=mzte_codec.m_iWvtDecmpLev-mzte_codec.m_iSpatialLev;
 
512
                mzte_codec.m_lastWvtDecompInSpaLayer[0][0]=sp0;
 
513
                  
 
514
                for (i=1; i<mzte_codec.m_iSpatialLev; ++i)
 
515
                  mzte_codec.m_lastWvtDecompInSpaLayer[i][0]=sp0+i;
 
516
      }
 
517
    }
 
518
    else
 
519
    {
 
520
      for (i=0; i<mzte_codec.m_iSpatialLev; ++i)
 
521
                mzte_codec.m_lastWvtDecompInSpaLayer[i][0]=i;
 
522
    }
 
523
    
 
524
    /* Calculate for chroma (one less than luma) */
 
525
    for (i=0; i<mzte_codec.m_iSpatialLev; ++i)
 
526
      mzte_codec.m_lastWvtDecompInSpaLayer[i][1]
 
527
                =mzte_codec.m_lastWvtDecompInSpaLayer[i][2]
 
528
                =mzte_codec.m_lastWvtDecompInSpaLayer[i][0]-1;
 
529
  }
 
530
 
 
531
 
 
532
  if (mzte_codec.m_iWvtDownload == 1) {
 
533
        // hjlee 0901
 
534
    wavelet_uniform = (mzte_codec.m_iWvtUniform!=0)?1:0;
 
535
    emit_bits((UShort)wavelet_uniform, 1);
 
536
    if(wavelet_uniform) {
 
537
      upload_wavelet_filters(wvtfilter[0]);
 
538
    }
 
539
    else {
 
540
      for(i=0;i<mzte_codec.m_iWvtDecmpLev;i++) 
 
541
            upload_wavelet_filters(wvtfilter[i]);       
 
542
        }
 
543
  }  
 
544
 
 
545
  emit_bits((UShort)wavelet_stuffing, 3);
 
546
 
 
547
// added for FDAM1 by Samsung AIT on 2000/02/03
 
548
  if(!mzte_codec.m_usErrResiDisable && SkipShape==0) {
 
549
          emit_bits((UShort)mzte_codec.m_usSegmentThresh,16);
 
550
      emit_bits((UShort)MARKER_BIT, 1);
 
551
  }
 
552
// ~added for FDAM1 by Samsung AIT on 2000/02/03
 
553
 
 
554
  if (texture_object_layer_shape == 0x00) {
 
555
// begin: added by Sharp (99/2/16)
 
556
    emit_bits((UShort)mzte_codec.m_display_width, 15);
 
557
    emit_bits((UShort)MARKER_BIT, 1);
 
558
    emit_bits((UShort)mzte_codec.m_display_height, 15);
 
559
    emit_bits((UShort)MARKER_BIT, 1);
 
560
// end: added by Sharp (99/2/16)
 
561
// begin: deleted by Sharp (99/2/16)
 
562
#if 0
 
563
    emit_bits((UShort)mzte_codec.m_iRealWidth, 15);
 
564
    emit_bits((UShort)MARKER_BIT, 1);
 
565
    emit_bits((UShort)mzte_codec.m_iRealHeight, 15);
 
566
    emit_bits((UShort)MARKER_BIT, 1);
 
567
#endif
 
568
// end: deleted by Sharp (99/2/16)
 
569
  }
 
570
  else { /* Arbitrary shape info, SL */    
 
571
    emit_bits((UShort)mzte_codec.m_iObjectOriginX, 15);  /*horizontal_ref */    
 
572
    emit_bits((UShort)MARKER_BIT, 1); /* marker_bit */   
 
573
    emit_bits((UShort)mzte_codec.m_iObjectOriginY, 15); /*vertical_ref */    
 
574
    emit_bits((UShort)MARKER_BIT, 1);  /* marker_bit */    
 
575
    emit_bits((UShort)mzte_codec.m_iObjectWidth, 15);  /* object_width */    
 
576
    emit_bits((UShort)MARKER_BIT, 1);   /* marker_bit */    
 
577
    emit_bits((UShort)mzte_codec.m_iObjectHeight, 15);  /* object_height */    
 
578
    emit_bits((UShort)MARKER_BIT, 1);  /* marker_bit */
 
579
    //noteProgress("Merge Shape Bitstream ....");
 
580
    // MergeShapeBitstream();
 
581
// FPDAM begin: added by Sharp
 
582
                if ( mzte_codec.m_tiling_disable == 1 && SkipShape == 0 ){ // @@@@@@@
 
583
// FPDAM end: added by Sharp
 
584
        //begin: added by SL@Sarnoff (03/03/99)
 
585
        noteProgress("Encoding Shape Bitstream ....");
 
586
    ShapeEnCoding(mzte_codec.m_Image[0].mask, mzte_codec.m_iWidth, mzte_codec.m_iHeight, 
 
587
                  mzte_codec.m_iWvtDecmpLev, 
 
588
                  mzte_codec.m_iSTOConstAlpha,
 
589
                  mzte_codec.m_iSTOConstAlphaValue, 
 
590
                  mzte_codec.m_iChangeCRDisable,
 
591
                  mzte_codec.m_iShapeScalable,
 
592
                  mzte_codec.m_bStartCodeEnable,
 
593
                  wvtfilter);
 
594
        //end: added by SL@Sarnoff (03/03/99)
 
595
 
 
596
// FPDAM begin: added by Sharp
 
597
                }
 
598
// FPDAM end: added by Sharp
 
599
  }
 
600
 
 
601
// @@@@@@@@@@@@
 
602
  if ( mzte_codec.m_tiling_disable == 0 && SkipShape == 1){
 
603
    emit_bits((UShort)mzte_codec.m_tile_width, 15); // modified by Sharp (99/11/16)
 
604
    emit_bits((UShort)1, 1);
 
605
    emit_bits((UShort)mzte_codec.m_tile_height, 15); // modified by Sharp (99/11/16)
 
606
    emit_bits((UShort)1, 1);
 
607
        }
 
608
 
 
609
#if 0
 
610
//Added by Sarnoff for error resilience, 3/5/99
 
611
  if(!mzte_codec.m_usErrResiDisable)
 
612
          emit_bits((UShort)mzte_codec.m_usSegmentThresh,16);
 
613
//End: Added by Sarnoff for error resilience, 3/5/99
 
614
#endif
 
615
}
 
616
// end: added by Sharp (99/2/16)
 
617
 
 
618
/**********************************************************/
 
619
 
 
620
/* put  quant value and maximums on the bitstream */
 
621
/* put  quant value and maximums on the bitstream */
 
622
Void CVTCEncoder::Put_Quant_and_Max(SNR_IMAGE *snr_image, Int spaLayer, Int color)
 
623
{
 
624
    /* put  quant value and maximums on the bitstream */
 
625
    put_param(snr_image->quant, 7);
 
626
//    emit_bits((UShort)MARKER_BIT, 1);  // 1124
 
627
 
 
628
    {
 
629
      Int l;
 
630
      for (l=0; l<=mzte_codec.m_lastWvtDecompInSpaLayer[spaLayer][color];++l)
 
631
      {
 
632
                emit_bits((UShort)snr_image->wvtDecompNumBitPlanes[l],5);
 
633
                if (((l+1) % 4) == 0)
 
634
                  emit_bits((UShort)MARKER_BIT, 1);
 
635
      }
 
636
    }
 
637
 
 
638
 
 
639
}
 
640
 
 
641
 
 
642
 
 
643
// hjlee 0901
 
644
Void CVTCEncoder::Put_Quant_and_Max_SQBB(SNR_IMAGE *snr_image, Int spaLayer,
 
645
                                   Int color)
 
646
{
 
647
    /* put  quant value and maximums on the bitstream */
 
648
  if ((color==0 && spaLayer==0) || (color>0 && spaLayer==1))
 
649
    put_param(snr_image->quant, 7);
 
650
 
 
651
  if (color==0)
 
652
    emit_bits((UShort)snr_image->wvtDecompNumBitPlanes[spaLayer],5);
 
653
  else if (spaLayer)
 
654
    emit_bits((UShort)snr_image->wvtDecompNumBitPlanes[spaLayer-1],5);
 
655
 
 
656
 
 
657
}
 
658
 
 
659
 
 
660
 
 
661
/**********************************************************/
 
662
 
 
663
Void CVTCEncoder::textureLayerDC_Enc()
 
664
{
 
665
  Int col, err;
 
666
 
 
667
  noteProgress("Encoding DC coefficients....");
 
668
 
 
669
  for (col=0; col<mzte_codec.m_iColors; col++) 
 
670
  {
 
671
    /* Set global color variable */
 
672
    mzte_codec.m_iCurColor=col;
 
673
 
 
674
    /* initialize DC coefficient info */
 
675
    if ((err=ztqInitDC(0, col)))
 
676
      errorHandler("ztqInitDC");
 
677
 
 
678
    /* quantize DC coefficients */
 
679
    if ((err=encQuantizeDC(col)))
 
680
      errorHandler("encQuantizeDC");
 
681
 
 
682
    /* losslessly encoding DC coefficients */
 
683
    wavelet_dc_encode(col);
 
684
 
 
685
    // writeStats();
 
686
  }  
 
687
 
 
688
  noteProgress("Completed encoding DC coefficients.");
 
689
}
 
690
 
 
691
/**********************************************************/
 
692
 
 
693
Void CVTCEncoder::TextureSpatialLayerSQNSC_enc(Int spa_lev)
 
694
{
 
695
  Int col;
 
696
  SNR_IMAGE *snr_image; // hjlee 0901
 
697
 
 
698
  /* hjlee 0901 */
 
699
  /* hjlee 0827 */
 
700
    for (col=0; col<mzte_codec.m_iColors; col++) {
 
701
      snr_image = &(mzte_codec.m_SPlayer[col].SNRlayer.snr_image);
 
702
      Put_Quant_and_Max_SQBB(snr_image, spa_lev, col);
 
703
    }
 
704
        for (col=0; col<mzte_codec.m_iColors; col++) {
 
705
        noteProgress("Single-Quant Mode (Band by Band) - Spatial %d, SNR 0, "\
 
706
                      "Color %d",spa_lev,col);
 
707
 
 
708
        mzte_codec.m_iCurColor = col;
 
709
            if (spa_lev !=0 || col ==0) {
 
710
            wavelet_higher_bands_encode_SQ_band(col);
 
711
            }
 
712
    }
 
713
 
 
714
 
715
 
 
716
 
 
717
Void CVTCEncoder::TextureSpatialLayerSQ_enc(Int spa_lev, FILE *bitfile)
 
718
{
 
719
  //    Char fname[100]; // hjlee
 
720
 
 
721
  /*------- AC: Open and initialize bitstream file -------*/
 
722
  if (mzte_codec.m_iSingleBitFile==0)
 
723
  {
 
724
    abort();
 
725
#if 0
 
726
    this is bad
 
727
    sprintf(fname,mzte_codec.m_cBitFileAC,spa_lev,0);
 
728
    if ((bitfile=fopen(fname,"wb"))==NULL)
 
729
      errorHandler("Can't open file '%s' for writing.",fname);
 
730
#endif
 
731
  }
 
732
  
 
733
  /* initialize the buffer */
 
734
  init_bit_packing_fp(bitfile,1); 
 
735
 
 
736
  /*------- AC: Write header info to bitstream file -------*/
 
737
  emit_bits(TEXTURE_SPATIAL_LAYER_START_CODE >> 16, 16);
 
738
  emit_bits(TEXTURE_SPATIAL_LAYER_START_CODE, 16);
 
739
  emit_bits(spa_lev, 5);
 
740
 
 
741
  TextureSpatialLayerSQNSC_enc(spa_lev);
 
742
  
 
743
  /*------- AC: Close bitstream file -------*/
 
744
  if (mzte_codec.m_iSingleBitFile)
 
745
    flush_buffer_file();
 
746
  else
 
747
    close_buffer_file(bitfile);
 
748
}
 
749
 
 
750
 
 
751
Void CVTCEncoder::textureLayerSQ_Enc(FILE *bitfile)
 
752
{
 
753
  Int col, err, spa_lev;
 
754
  SNR_IMAGE *snr_image;
 
755
    
 
756
 
 
757
  noteProgress("Encoding AC coefficients - Single-Quant Mode....");
 
758
  
 
759
  /*------- AC: Set spatial and SNR levels to zero -------*/
 
760
  mzte_codec.m_iCurSpatialLev = 0;
 
761
  mzte_codec.m_iCurSNRLev = 0;
 
762
    
 
763
//  mzte_codec.m_iSpatialLev=1;  // hjlee 0901
 
764
  setSpatialLayerDimsSQ(0);  // hjlee 0901
 
765
  
 
766
  for (col=0; col<mzte_codec.m_iColors; col++)
 
767
  {     
 
768
    snr_image=&(mzte_codec.m_SPlayer[col].SNRlayer.snr_image);
 
769
    
 
770
    /* Set global color variable */
 
771
    mzte_codec.m_iCurColor = col;
 
772
    snr_image->quant = mzte_codec.m_Qinfo[col][0].Quant[0];
 
773
    
 
774
    /* initialization of spatial dimensions for each color component */
 
775
    setSpatialLevelAndDimensions(0, col);
 
776
    
 
777
    /* initialize AC coefficient info for each color component */
 
778
    if ((err=ztqInitAC(0, col)))
 
779
      errorHandler("ztqInitAC");
 
780
    
 
781
    /* quantize and mark zerotree structure for AC coefficients */
 
782
    if ((err=encQuantizeAndMarkAC(col)))
 
783
      errorHandler("encQuantizeAndMarkAC");
 
784
  }
 
785
  
 
786
 
 
787
  /*------- AC: encode all color components -------*/
 
788
  if (mzte_codec.m_iScanDirection==0) /* tree-depth scan */
 
789
  {
 
790
    /* put  quant value and maximums on the bitstream */
 
791
    for(col=0;col<mzte_codec.m_iColors; col++){
 
792
      snr_image=&(mzte_codec.m_SPlayer[col].SNRlayer.snr_image);
 
793
      Put_Quant_and_Max(snr_image,0,col);  // hjlee 0901
 
794
    }
 
795
    
 
796
    /* losslessly encoding AC coefficients */
 
797
    wavelet_higher_bands_encode_SQ_tree();      
 
798
  }
 
799
  else  /* band by band scan */
 
800
  {
 
801
    setSpatialLayerDimsSQ(1);  // hjlee 0901
 
802
 
 
803
        /* Assumes all three color components have the same number of SNR 
 
804
       levels */
 
805
    for (col=0; col<mzte_codec.m_iColors; col++)
 
806
      mzte_codec.m_SPlayer[col].SNR_scalability_levels = 1;
 
807
    
 
808
    /* Loop through spatial layers */
 
809
    for (spa_lev=0; spa_lev<mzte_codec.m_iSpatialLev; 
 
810
         spa_lev++)
 
811
    {
 
812
      mzte_codec.m_iCurSpatialLev=spa_lev;
 
813
      for (col=0; col<mzte_codec.m_iColors; col++)
 
814
                setSpatialLevelAndDimensions(spa_lev,col);
 
815
 
 
816
      /*----- AC: Set global spatial layer. -----*/
 
817
      mzte_codec.m_iCurSpatialLev = spa_lev;
 
818
      
 
819
      /* Update spatial level coeff info if changing spatial levels.
 
820
         Do this for all color components */
 
821
      if (mzte_codec.m_bStartCodeEnable) {
 
822
                TextureSpatialLayerSQ_enc(spa_lev,bitfile);
 
823
      }
 
824
      else
 
825
                TextureSpatialLayerSQNSC_enc(spa_lev);
 
826
    }
 
827
  }
 
828
  
 
829
  /* store the max snr_lev and spa_lev so that the decoder can
 
830
     decode the bitstream up to the max level. */ 
 
831
  /*mzte_codec.m_iTargetSpatialLev = 1;
 
832
  mzte_codec.m_iTargetSNRLev = 1; */ //deleted by SL@Sarnoff (03/03/99)
 
833
  
 
834
  noteProgress("Completed encoding AC coefficients - Single-Quant Mode.");
 
835
}
 
836
 
 
837
 
 
838
/**********************************************************/
 
839
 
 
840
 
 
841
Void CVTCEncoder::TextureSNRLayerMQ_encode(Int spa_lev, Int snr_lev, FILE *fp)
 
842
{
 
843
  SNR_IMAGE *snr_image;
 
844
  Int col;
 
845
  static Int texture_snr_layer_id=0;
 
846
 
 
847
  if(mzte_codec.m_bStartCodeEnable){
 
848
    noteProgress("Encoding Multi-Quant Mode Layer with SNR start code....");
 
849
    /* header */  
 
850
    emit_bits((UShort)(texture_snr_layer_start_code>>16),16);
 
851
    emit_bits((UShort)texture_snr_layer_start_code,16);
 
852
    emit_bits((UShort)texture_snr_layer_id++,5);
 
853
  }
 
854
  else
 
855
    noteProgress("Encoding Multi-Quant Mode Layer without SNR start code....");
 
856
 
 
857
  noteProgress("Multi-Quant Mode - Spatial %d, SNR %d", spa_lev,snr_lev);
 
858
 
 
859
  for(col=0;
 
860
      col < NCOL;
 
861
      col++)
 
862
  {
 
863
    noteDetail("width=%d  height=%d",mzte_codec.m_SPlayer[col].width,
 
864
              mzte_codec.m_SPlayer[col].height);    
 
865
 
 
866
    /* Set global color variable */
 
867
    mzte_codec.m_iCurColor = col;
 
868
    
 
869
    /* Set quant value */
 
870
    snr_image=&(mzte_codec.m_SPlayer[col].SNRlayer.snr_image);
 
871
    snr_image->quant = mzte_codec.m_Qinfo[col][spa_lev].Quant[snr_lev];
 
872
    noteDebug("AC quant=%d", 
 
873
               mzte_codec.m_Qinfo[col][spa_lev].Quant[snr_lev]);
 
874
        
 
875
    /* initialization of spatial dimensions for each color component */
 
876
    if (snr_lev==0) 
 
877
      setSpatialLevelAndDimensions(spa_lev, col);
 
878
 
 
879
    /* get maximum residual value - this one is derived from user Q inputs not 
 
880
       actual values. Also assign skip modes. */
 
881
    updateResidMaxAndAssignSkips(col);
 
882
    noteDebug("resid_max=%d\n",snr_image->residual_max);
 
883
 
 
884
    /* quantize and mark zerotree structure for AC coefficients */
 
885
    if (encQuantizeAndMarkAC(col))
 
886
      errorHandler("encQuantizeAndMarkAC");
 
887
 
 
888
     //   Put_Quant_and_Max(snr_image); // hjlee 0901
 
889
    Put_Quant_and_Max(snr_image,spa_lev,col);  // hjlee 0901
 
890
          }
 
891
 
 
892
  wavelet_higher_bands_encode_MQ(mzte_codec.m_iScanDirection);    
 
893
 
 
894
  for(col=0;
 
895
      col < NCOL;
 
896
      col++)
 
897
  {
 
898
    /* Set global color variable */
 
899
    mzte_codec.m_iCurColor = col;
 
900
    
 
901
    /* Update states of AC coefficients */
 
902
    if (encUpdateStateAC(mzte_codec.m_iCurColor))
 
903
      errorHandler("encUpdateStateAC");
 
904
  }
 
905
}
 
906
 
 
907
Void CVTCEncoder::textureLayerMQ_Enc(FILE *bitfile)
 
908
{
 
909
  Int err, spa_lev, snr_lev=0, snr_scalability_levels;
 
910
        Char fname[100]; // hjlee
 
911
 
 
912
  getSpatialLayerDims(); // hjlee 0901
 
913
 
 
914
  // hjlee 0901
 
915
    /*------- AC: Initialize QList Structure -------*/
 
916
  if ((err=ztqQListInit()))
 
917
    errorHandler("Allocating memory for QList information.");
 
918
  
 
919
  /* Initialize coeffs */
 
920
  setSpatialLevelAndDimensions(0,0);
 
921
  if ((err=ztqInitAC(0,0)))
 
922
    errorHandler("ztqInitAC");
 
923
  
 
924
  if (mzte_codec.m_iColors > 1)
 
925
  {
 
926
    if (mzte_codec.m_lastWvtDecompInSpaLayer[0][1]<0)
 
927
      setSpatialLevelAndDimensions(1,1);
 
928
    else
 
929
      setSpatialLevelAndDimensions(0,1);
 
930
    if ((err=ztqInitAC(0,1)))
 
931
      errorHandler("ztqInitAC");
 
932
  }
 
933
  if (mzte_codec.m_iColors > 2)
 
934
  {
 
935
    if (mzte_codec.m_lastWvtDecompInSpaLayer[0][2]<0)
 
936
      setSpatialLevelAndDimensions(1,2);
 
937
    else
 
938
      setSpatialLevelAndDimensions(0,2);
 
939
    if ((err=ztqInitAC(0,2)))
 
940
      errorHandler("ztqInitAC");
 
941
  }
 
942
 
 
943
 
 
944
  /* Loop through spatial layers */
 
945
  for (spa_lev=0; spa_lev<mzte_codec.m_iSpatialLev; 
 
946
       spa_lev++)
 
947
  {
 
948
    /*----- AC: Set global spatial layer and SNR scalability level. -----*/
 
949
    /* Assumes all three color components have the same number of SNR 
 
950
       levels */
 
951
    mzte_codec.m_iCurSpatialLev = spa_lev;
 
952
    mzte_codec.m_SPlayer[0].SNR_scalability_levels = 
 
953
      mzte_codec.m_Qinfo[0][spa_lev].SNR_scalability_levels;
 
954
    snr_scalability_levels = mzte_codec.m_SPlayer[0].SNR_scalability_levels;
 
955
    
 
956
    /* Update spatial level coeff info if changing spatial levels.
 
957
       Do this for all color components. */
 
958
    if (spa_lev != 0)
 
959
    {
 
960
      for (mzte_codec.m_iCurColor = 0; mzte_codec.m_iCurColor<mzte_codec.m_iColors;
 
961
           mzte_codec.m_iCurColor++) 
 
962
      {
 
963
                setSpatialLevelAndDimensions(mzte_codec.m_iCurSpatialLev, 
 
964
                                                 mzte_codec.m_iCurColor);
 
965
 
 
966
// hjlee 0901
 
967
                if (mzte_codec.m_lastWvtDecompInSpaLayer[spa_lev-1][mzte_codec.m_iCurColor]
 
968
                        >=0)
 
969
                  spatialLayerChangeUpdate(mzte_codec.m_iCurColor);
 
970
 
 
971
      }
 
972
    }
 
973
    
 
974
    if (!mzte_codec.m_bStartCodeEnable)
 
975
      /*------- AC: Write header info to bitstream file -------*/
 
976
      emit_bits(snr_scalability_levels, 5);    
 
977
    
 
978
    /* Loop through SNR layers */      
 
979
    for (snr_lev=0; snr_lev<snr_scalability_levels; snr_lev++) 
 
980
    {
 
981
      /*----- AC: Set global SNR layer -----*/
 
982
      mzte_codec.m_iCurSNRLev = snr_lev;
 
983
      
 
984
      if (mzte_codec.m_bStartCodeEnable)
 
985
      {
 
986
        /*------- AC: Open and initialize bitstream file -------*/
 
987
        if (mzte_codec.m_iSingleBitFile==0)
 
988
        {
 
989
#if 0
 
990
          // wmay - this is bad
 
991
          sprintf(fname,mzte_codec.m_cBitFileAC,
 
992
                  mzte_codec.m_iCurSpatialLev, mzte_codec.m_iCurSNRLev);
 
993
#endif
 
994
          abort();
 
995
          if ((bitfile=fopen(fname,"wb"))==NULL)
 
996
            errorHandler("Can't open file '%s' for writing.",fname);
 
997
        }
 
998
        
 
999
        /* initialize the buffer */
 
1000
        init_bit_packing_fp(bitfile,1);
 
1001
        
 
1002
        if (snr_lev==0) {
 
1003
          /*------- AC: Write header info to bitstream file -------*/
 
1004
          emit_bits(TEXTURE_SPATIAL_LAYER_START_CODE >> 16, 16);
 
1005
          emit_bits(TEXTURE_SPATIAL_LAYER_START_CODE, 16);
 
1006
          emit_bits(spa_lev, 5);
 
1007
          emit_bits(snr_scalability_levels, 5);
 
1008
          flush_bits();     /* byte alignment before start code */
 
1009
        }
 
1010
        
 
1011
      }
 
1012
      
 
1013
      /*------- AC: Quantize and encode all color components -------*/
 
1014
      TextureSNRLayerMQ_encode(spa_lev, snr_lev, bitfile);
 
1015
      if (mzte_codec.m_bStartCodeEnable)
 
1016
      {
 
1017
                if (mzte_codec.m_iSingleBitFile)
 
1018
                        flush_buffer_file();
 
1019
                else
 
1020
                        close_buffer_file(bitfile);
 
1021
      }
 
1022
      
 
1023
      
 
1024
    } /* snr_lev */
 
1025
    
 
1026
  }  /* spa_lev */
 
1027
  
 
1028
  /* store the max snr_lev and spa_lev so that the decoder can
 
1029
     decode the bitstream up to the max level. */ 
 
1030
  mzte_codec.m_iTargetSpatialLev = spa_lev;
 
1031
  mzte_codec.m_iTargetSNRLev = snr_lev;
 
1032
  
 
1033
  /*------- AC: Free Qlist structure -------*/
 
1034
  ztqQListExit();
 
1035
  
 
1036
}
 
1037
 
 
1038
/**********************************************************/
 
1039
 
 
1040
Void CVTCEncoder::TextureObjectLayer_enc(FILE *bitfile) // modified by Sharp (99/2/16)
 
1041
//Void CVTCEncoder::TextureObjectLayer_enc(FILTER *wvtfilter)  // hjlee 0901
 
1042
{
 
1043
#if 0
 
1044
  FILE *bitfile;
 
1045
 
 
1046
  /*------- DC: Open and initialize bitstream file -------*/
 
1047
  if ( iTile == 0 ){ // added by Sharp (99/2/16)
 
1048
 
 
1049
  if ((bitfile=fopen(mzte_codec.m_cBitFile,"wb"))==NULL)
 
1050
    errorHandler("Can't open file '%s' for writing.",
 
1051
                mzte_codec.m_cBitFile);
 
1052
  }
 
1053
 
 
1054
// begin: added by Sharp (99/2/16)
 
1055
  else {
 
1056
    /*    sprintf(filename,"%s%d",bitFile,iTile);*/
 
1057
    if ((bitfile=fopen(mzte_codec.m_cBitFile,"ab"))==NULL)
 
1058
      errorHandler("Can't open file '%s' for writing.",mzte_codec.m_cBitFile);
 
1059
 
 
1060
    fseek(bitfile,0,SEEK_END);
 
1061
  }
 
1062
// end: added by Sharp (99/2/16)
 
1063
 
 
1064
  /* initialize variables */
 
1065
  init_bit_packing_fp(bitfile,1);
 
1066
#endif
 
1067
 
 
1068
  /* for PEZW, always enabled */
 
1069
  if (mzte_codec.m_iQuantType == BILEVEL_Q) 
 
1070
    mzte_codec.m_bStartCodeEnable = 1;
 
1071
 
 
1072
  /* Write header info to bitstream */
 
1073
// begin: added by Sharp (99/2/16)
 
1074
#if 0
 
1075
        if ( mzte_codec.m_tiling_disable == 0 ){
 
1076
                if ( iTile == 0 ){
 
1077
                        *tile_table = header_Enc(wvtfilter);
 
1078
                }
 
1079
                tile_header_Enc(iTile);
 
1080
        }
 
1081
        if ( mzte_codec.m_tiling_disable == 1 ){
 
1082
                assert (iTile == 0);
 
1083
                header_Enc(wvtfilter); // Encode start_code only
 
1084
                //header_Enc_Common(wvtfilter); deleted by SL 030399
 
1085
        }
 
1086
#endif
 
1087
// end: added by Sharp (99/2/16)
 
1088
 
 
1089
//  header_Enc(wvtfilter); // deleted by Sharp (99/2/16)
 
1090
 
 
1091
  /*------- DC: Quantize and encode all color components -------*/
 
1092
  textureLayerDC_Enc();
 
1093
 
 
1094
  /*------- DC: Close bitstream file -------*/
 
1095
  /* hjlee 001 */
 
1096
  if (mzte_codec.m_bStartCodeEnable){
 
1097
     if(mzte_codec.m_iSingleBitFile) 
 
1098
        flush_buffer_file(); 
 
1099
     else  
 
1100
       close_buffer_file(bitfile); 
 
1101
  }
 
1102
 
 
1103
 
 
1104
  /*-------------------------------------------------*/
 
1105
  /*--------------------- AC ------------------------*/
 
1106
  /*-------------------------------------------------*/
 
1107
#if 1
 
1108
  /*------- AC: SINGLE-QUANT MODE -------*/
 
1109
  if (mzte_codec.m_iQuantType == SINGLE_Q)
 
1110
    textureLayerSQ_Enc(bitfile);
 
1111
  /*------- AC: MULTI-QUANT MODE -------*/
 
1112
  else if (mzte_codec.m_iQuantType == MULTIPLE_Q)
 
1113
    textureLayerMQ_Enc(bitfile);
 
1114
  else if (mzte_codec.m_iQuantType == BILEVEL_Q) {
 
1115
          textureLayerBQ_Enc(bitfile);
 
1116
  }
 
1117
#endif
 
1118
  if (mzte_codec.m_iSingleBitFile){
 
1119
    if(!mzte_codec.m_bStartCodeEnable)
 
1120
      close_buffer_file(bitfile);
 
1121
    else
 
1122
      fclose(bitfile);
 
1123
  }
 
1124
}
 
1125
 
 
1126
Void CVTCEncoder::TextureObjectLayer_enc_V1(FILTER **wvtfilter)
 
1127
{
 
1128
  FILE *bitfile;
 
1129
 
 
1130
  /*------- DC: Open and initialize bitstream file -------*/
 
1131
  if ((bitfile=fopen(mzte_codec.m_cBitFile,"wb"))==NULL)
 
1132
    errorHandler("Can't open file '%s' for writing.",
 
1133
                mzte_codec.m_cBitFile);
 
1134
 
 
1135
  /* for PEZW, always enabled */
 
1136
  if (mzte_codec.m_iQuantType == BILEVEL_Q) 
 
1137
    mzte_codec.m_bStartCodeEnable = 1;
 
1138
 
 
1139
  /* initialize variables */
 
1140
  init_bit_packing_fp(bitfile,1);
 
1141
 
 
1142
  /* Write header info to bitstream */
 
1143
  header_Enc_V1(wvtfilter);
 
1144
 
 
1145
  /*------- DC: Quantize and encode all color components -------*/
 
1146
  textureLayerDC_Enc();
 
1147
 
 
1148
  /*------- DC: Close bitstream file -------*/
 
1149
  /* hjlee 001 */
 
1150
  if (mzte_codec.m_bStartCodeEnable){
 
1151
     if(mzte_codec.m_iSingleBitFile) 
 
1152
        flush_buffer_file(); 
 
1153
     else  
 
1154
       close_buffer_file(bitfile); 
 
1155
  }
 
1156
 
 
1157
 
 
1158
  /*-------------------------------------------------*/
 
1159
  /*--------------------- AC ------------------------*/
 
1160
  /*-------------------------------------------------*/
 
1161
 
 
1162
  /*------- AC: SINGLE-QUANT MODE -------*/
 
1163
  if (mzte_codec.m_iQuantType == SINGLE_Q)
 
1164
    textureLayerSQ_Enc(bitfile);
 
1165
  /*------- AC: MULTI-QUANT MODE -------*/
 
1166
  else if (mzte_codec.m_iQuantType == MULTIPLE_Q)
 
1167
    textureLayerMQ_Enc(bitfile);
 
1168
  else if (mzte_codec.m_iQuantType == BILEVEL_Q) {
 
1169
          textureLayerBQ_Enc(bitfile);
 
1170
  }
 
1171
 
 
1172
  if (mzte_codec.m_iSingleBitFile){
 
1173
    if(!mzte_codec.m_bStartCodeEnable)
 
1174
      close_buffer_file(bitfile);
 
1175
    else
 
1176
      fclose(bitfile);
 
1177
  }
 
1178
 
 
1179
 
 
1180
 
 
1181
}
 
1182
 
 
1183
/**************************************************************/
 
1184
 
 
1185
Void CVTCEncoder::encode()
 
1186
{
 
1187
 
 
1188
        FILTER **synfilter,**anafilter;  // hjlee 0901
 
1189
        Int i; // hjlee 0901
 
1190
// begin: added by Sharp (99/2/16)
 
1191
  Int tile_width, tile_height;
 
1192
  long tile_table_pos, garbage;
 
1193
//  DATA *dat[3];
 
1194
  Int *table = NULL;
 
1195
  FILE *bitfile;
 
1196
        Int col; // added by Sharp (99/2/16)
 
1197
// end: added by Sharp (99/2/16)
 
1198
 
 
1199
//      mzte_codec.m_visual_object_verid = VERSION; /* version 2 */ // deleted by Sharp (99/11/18)
 
1200
 
 
1201
// begin: added by Sharp (99/4/7)
 
1202
        struct stat file_info;
 
1203
        int init_size = 0;
 
1204
// end: added by Sharp (99/4/7)
 
1205
 
 
1206
        if ( mzte_codec.m_visual_object_verid != 1 ){ /* version 2 */
 
1207
 
 
1208
                // allocate memory for source image
 
1209
                mzte_codec.m_Image = new PICTURE[3];
 
1210
                mzte_codec.m_ImageOrg = new PICTURE[3]; // added by Sharp (99/2/16)
 
1211
 
 
1212
                // hjlee 0901
 
1213
                anafilter = (FILTER **) malloc(sizeof(FILTER *)*mzte_codec.m_iWvtDecmpLev);
 
1214
                synfilter = (FILTER **) malloc(sizeof(FILTER *)*mzte_codec.m_iWvtDecmpLev);
 
1215
                if(anafilter == NULL || synfilter == NULL) 
 
1216
                errorHandler("Error allocating memory for filters\n");
 
1217
                for(i=0;i<mzte_codec.m_iWvtDecmpLev; i++) {
 
1218
                        choose_wavelet_filter(&(anafilter[i]),
 
1219
                                        &(synfilter[mzte_codec.m_iWvtDecmpLev-1-i]),
 
1220
                                        mzte_codec.m_WvtFilters[mzte_codec.m_iWvtUniform?0:i]);
 
1221
                }
 
1222
                        
 
1223
                        // read source image
 
1224
                // m_Image.data is allocated here
 
1225
                read_image(     m_cImagePath, mzte_codec.m_iWidth, mzte_codec.m_iHeight,
 
1226
                        mzte_codec.m_iColors, 8, mzte_codec.m_Image);
 
1227
 
 
1228
                if (mzte_codec.m_iAlphaChannel != 0) {  // arbitrary shape coding
 
1229
                        // allocate meory for segmentation
 
1230
                        mzte_codec.m_SegImage = new PICTURE[3];
 
1231
 
 
1232
                        printf("Reading in seg map '%s(%dx%d)'....\n", m_cSegImagePath,mzte_codec.m_Image[0].width,mzte_codec.m_Image[0].height );
 
1233
                        // read the segmentation map of the source image */
 
1234
                        // m_Image.mask[0] is allocated here
 
1235
                        mzte_codec.m_iAlphaChannel = read_segimage(m_cSegImagePath, 
 
1236
                                                                                                mzte_codec.m_Image[0].width, 
 
1237
                                                                                                mzte_codec.m_Image[0].height, 
 
1238
                                                                                                mzte_codec.m_iColors, 
 
1239
                                                                                                mzte_codec.m_Image); 
 
1240
 
 
1241
                }
 
1242
 
 
1243
        // begin: added by Sharp (99/2/16)
 
1244
                mzte_codec.m_display_width = mzte_codec.m_iWidth;
 
1245
                mzte_codec.m_display_height = mzte_codec.m_iHeight;
 
1246
                tile_width = mzte_codec.m_tile_width;
 
1247
                tile_height = mzte_codec.m_tile_height;
 
1248
 
 
1249
                if ( mzte_codec.m_tiling_disable == 0 ){
 
1250
                        printf("Wavelet Tiling ON....\n");  
 
1251
 
 
1252
// FPDAM begin: added by Sharp
 
1253
                get_real_image(mzte_codec.m_Image,
 
1254
                                mzte_codec.m_iWvtDecmpLev, mzte_codec.m_iAlphaChannel,
 
1255
                                mzte_codec.m_iColors, mzte_codec.m_iAlphaTh,
 
1256
                                anafilter[0]);
 
1257
 
 
1258
                for(col=0; col<3; col++) {
 
1259
                        mzte_codec.m_Image[col].width =  (mzte_codec.m_iWidth + (col>0 ? 1 : 0)) >> (col>0 ? 1 : 0);
 
1260
                        mzte_codec.m_Image[col].height = (mzte_codec.m_iHeight + (col>0 ? 1 : 0)) >> (col>0 ? 1 : 0);
 
1261
                }
 
1262
// FPDAM end: added by Sharp
 
1263
 
 
1264
                        /* change the members of mzte_codec to handle tiled image */
 
1265
                        /* original image is stored by m_ImageOrg */
 
1266
                        init_tile(tile_width, tile_height);
 
1267
                        mzte_codec.m_iWidth = tile_width;
 
1268
                        mzte_codec.m_iHeight = tile_height;
 
1269
                        mzte_codec.m_iDCWidth = GetDCWidth();
 
1270
                        mzte_codec.m_iDCHeight = GetDCHeight();
 
1271
 
 
1272
// begin: modified by Sharp (99/5/10)
 
1273
                        mzte_codec.m_iNumOfTile = 
 
1274
                                ((mzte_codec.m_ImageOrg[0].width)/tile_width + ((mzte_codec.m_ImageOrg[0].width%tile_width)?1:0) ) *
 
1275
                                ((mzte_codec.m_ImageOrg[0].height)/tile_height + ((mzte_codec.m_ImageOrg[0].height%tile_height)?1:0));
 
1276
                        
 
1277
 
 
1278
/*                      printf("%d %d\n", mzte_codec.m_ImageOrg[0].width, mzte_codec.m_ImageOrg[0].height);*/
 
1279
 
 
1280
//                      mzte_codec.m_iNumOfTile = (mzte_codec.m_ImageOrg[0].width)/tile_width *
 
1281
//                                      (mzte_codec.m_ImageOrg[0].height)/tile_height;
 
1282
// end: modified by Sharp (99/5/10)
 
1283
 
 
1284
                        table = (Int *)malloc(sizeof(Int)*mzte_codec.m_iNumOfTile);
 
1285
 
 
1286
#ifdef _FPDAM_DBG_
 
1287
fprintf(stderr,"....... origin_x=%d, origin_y=%d, org width=%d org height=%d m_iNumOfTile=%d\n",
 
1288
        mzte_codec.m_iOriginX, mzte_codec.m_iOriginY,
 
1289
  mzte_codec.m_ImageOrg[0].width, mzte_codec.m_ImageOrg[0].height, mzte_codec.m_iNumOfTile);
 
1290
        getchar();
 
1291
#endif 
 
1292
 
 
1293
        } else {
 
1294
                mzte_codec.m_iNumOfTile = 1;
 
1295
        }
 
1296
        // end: added by Sharp (99/2/16)
 
1297
 
 
1298
        // m_Image.mask is allocated here
 
1299
        get_virtual_image(mzte_codec.m_Image, mzte_codec.m_iWvtDecmpLev, 
 
1300
// FPDAM begin: modified by Sharp
 
1301
//                      mzte_codec.m_iAlphaChannel,
 
1302
                ((mzte_codec.m_tiling_disable==0) ? 0 : mzte_codec.m_iAlphaChannel),
 
1303
// FPDAM end: modified by Sharp
 
1304
                mzte_codec.m_iColors, mzte_codec.m_iAlphaTh,
 
1305
//                      mzte_codec.m_iChangeCRDisable, // modified by SL@Sarnoff (03/03/99)
 
1306
                anafilter[0]);
 
1307
 
 
1308
/* FPDAM begin : added by Samsung AIT (99/09/03) */
 
1309
        if ( mzte_codec.m_tiling_disable==1 ){
 
1310
                mzte_codec.m_iObjectWidth   = mzte_codec.m_iWidth;
 
1311
                mzte_codec.m_iObjectHeight  = mzte_codec.m_iHeight;
 
1312
                mzte_codec.m_iObjectOriginX = mzte_codec.m_iOriginX;
 
1313
                mzte_codec.m_iObjectOriginY = mzte_codec.m_iOriginY;
 
1314
        }
 
1315
/* FPDAM end : added by Samsung AIT (99/09/03) */
 
1316
 
 
1317
 
 
1318
        for (col=0; col<mzte_codec.m_iColors; col++) { // modified by Sharp (99/2/16)
 
1319
                mzte_codec.m_Image[col].height = mzte_codec.m_iHeight >> (Int)(col>0);
 
1320
                mzte_codec.m_Image[col].width  = mzte_codec.m_iWidth >> (Int)(col>0);
 
1321
        }
 
1322
 
 
1323
        //      printf("%d %d\n", mzte_codec.m_iHeight, mzte_codec.m_iWidth );
 
1324
        mzte_codec.m_iDCWidth = GetDCWidth(); //added by SL 030499 (This is the real DC-width and height)
 
1325
        mzte_codec.m_iDCHeight = GetDCHeight();
 
1326
                // begin: deleted by Sharp (99/2/16)
 
1327
#if 0
 
1328
        mzte_codec.m_iAcmOrder = 0;
 
1329
        mzte_codec.m_iAcmMaxFreqChg = 0;
 
1330
 
 
1331
        init_acm_maxf_enc();
 
1332
#endif
 
1333
        // end: deleted by Sharp (99/2/16)
 
1334
 
 
1335
        //      fprintf(stdout,"init ac model!\n");     
 
1336
                
 
1337
        int height;
 
1338
        int width;
 
1339
        int x,y;
 
1340
        for (col=0; col<mzte_codec.m_iColors; col++) {
 
1341
                height = mzte_codec.m_Image[col].height; 
 
1342
                width  = mzte_codec.m_Image[col].width;
 
1343
 
 
1344
//                      printf("SPlayer %d %d\n", width, height);
 
1345
                
 
1346
                mzte_codec.m_SPlayer[col].coeffinfo = new COEFFINFO * [height];
 
1347
                if (mzte_codec.m_SPlayer[col].coeffinfo == NULL)
 
1348
                        exit(fprintf(stderr,"Allocating memory for coefficient structure (I)."));
 
1349
                mzte_codec.m_SPlayer[col].coeffinfo[0] = new COEFFINFO [height*width];
 
1350
                if (mzte_codec.m_SPlayer[col].coeffinfo[0] == NULL)
 
1351
                        exit(fprintf(stderr,"Allocating memory for coefficient structure (II)."));
 
1352
                for (y = 1; y < height; ++y)
 
1353
                        mzte_codec.m_SPlayer[col].coeffinfo[y] = 
 
1354
                        mzte_codec.m_SPlayer[col].coeffinfo[y-1]+width;
 
1355
                
 
1356
                // for (int y=0; y<height; y++) // modified by Sharp (99/2/16) // FPDAM
 
1357
                for (y=0; y<height; y++) // FPDAM 
 
1358
                        for (x=0; x<width; x++) {
 
1359
                                mzte_codec.m_SPlayer[col].coeffinfo[y][x].skip =0;
 
1360
                        }
 
1361
 
 
1362
        }
 
1363
 
 
1364
        //      fprintf(stdout,"Coeffinfo memory allocation done!\n");
 
1365
  /*------- DC: Open and initialize bitstream file -------*/
 
1366
  if ((bitfile=fopen(mzte_codec.m_cBitFile,"wb"))==NULL)
 
1367
    errorHandler("Can't open file '%s' for writing.", mzte_codec.m_cBitFile);
 
1368
  init_bit_packing_fp(bitfile,1);
 
1369
        tile_table_pos = header_Enc(synfilter);
 
1370
//      @@@@@@@@@@
 
1371
// deleted for FDAM1 by Samsung AIT on 2000/02/03
 
1372
//  if(!mzte_codec.m_usErrResiDisable)
 
1373
//        emit_bits((UShort)mzte_codec.m_usSegmentThresh,16);
 
1374
// ~deleted for FDAM1 by Samsung AIT 2000/02/03
 
1375
 
 
1376
        // begin: added by Sharp (99/2/16)
 
1377
        for (Int iTile = 0; iTile<mzte_codec.m_iNumOfTile; iTile++ ){
 
1378
 
 
1379
                if ( mzte_codec.m_tiling_disable == 0 ){
 
1380
// FPDAM begin: modified by Sharp
 
1381
                        cut_tile_image(mzte_codec.m_Image, mzte_codec.m_ImageOrg, iTile, mzte_codec.m_iColors, tile_width, tile_height, anafilter[0]);
 
1382
// FPDAM end: modified by Sharp
 
1383
 
 
1384
// FPDAM begin: added by Sharp
 
1385
                if ( mzte_codec.m_iAlphaChannel )
 
1386
                        mzte_codec.m_iTextureTileType
 
1387
                                = CheckTextureTileType(mzte_codec.m_Image[0].mask,
 
1388
                                                mzte_codec.m_iWidth,
 
1389
                                                mzte_codec.m_iHeight,
 
1390
                                                mzte_codec.m_iRealWidth,
 
1391
                                                mzte_codec.m_iRealHeight);
 
1392
// FPDAM end: added by Sharp
 
1393
 
 
1394
                        if ( iTile != 0 ){
 
1395
                                if ((bitfile=fopen(mzte_codec.m_cBitFile,"ab"))==NULL)
 
1396
                                        errorHandler("Can't open file '%s' for writing.",mzte_codec.m_cBitFile);
 
1397
                                fseek(bitfile,0,SEEK_END);
 
1398
                                init_bit_packing_fp(bitfile,1);
 
1399
                        }
 
1400
// FPDAM begin: modified by Sharp
 
1401
//                      tile_header_Enc(iTile);
 
1402
                        tile_header_Enc(synfilter,iTile);
 
1403
// FPDAM end: modified by Sharp
 
1404
                }
 
1405
 
 
1406
                if( !mzte_codec.m_usErrResiDisable )
 
1407
                        texture_packet_header_Enc(synfilter);
 
1408
 
 
1409
                if ( mzte_codec.m_tiling_disable == 0 )
 
1410
                        printf("Encoding %d-th tile\n", iTile);
 
1411
                mzte_codec.m_iAcmOrder = 0;
 
1412
                mzte_codec.m_iAcmMaxFreqChg = 0;
 
1413
 
 
1414
                init_acm_maxf_enc();
 
1415
        // end: added by Sharp (99/2/16)
 
1416
 
 
1417
                /* DISCRETE WAVELET TRANSFORM */
 
1418
                noteProgress("Wavelet Transform....");  
 
1419
 
 
1420
 
 
1421
// begin: modified by Sharp (99/5/10)
 
1422
        // begin: added by Sharp (99/2/16)
 
1423
//              if(mzte_codec.m_tiling_disable==0){
 
1424
//                      perform_DWT_Tile(anafilter, mzte_codec.m_ImageOrg, iTile); //*********
 
1425
//              } else {
 
1426
 
 
1427
// FPDAM begin: added by Sharp
 
1428
                if ( mzte_codec.m_tiling_disable!=0 || !mzte_codec.m_iAlphaChannel || mzte_codec.m_iTextureTileType!=TRANSP_TILE)
 
1429
// FPDAM end: added by Sharp
 
1430
                        perform_DWT(anafilter);
 
1431
//              }
 
1432
        // end: added by Sharp (99/2/16)
 
1433
// end: modified by Sharp (99/5/10)
 
1434
 
 
1435
 
 
1436
        //      choose_wavelet_filter(&anafilter, &synfilter, mzte_codec.m_iWvtType); // hjlee 0901
 
1437
        //      perform_DWT(anafilter); deleted by Sharp (99/2/16)
 
1438
                noteProgress("Completed wavelet transform.");
 
1439
 
 
1440
// begin: added by Sharp (99/4/7)
 
1441
        if ( mzte_codec.m_tiling_disable == 0 ){
 
1442
                stat(mzte_codec.m_cBitFile, &file_info);
 
1443
                init_size = file_info.st_size;
 
1444
        }
 
1445
 
 
1446
        TextureObjectLayer_enc(bitfile);
 
1447
 
 
1448
        if (mzte_codec.m_tiling_disable==0 && mzte_codec.m_tiling_jump_table_enable == 1) {
 
1449
                if (iTile==0){
 
1450
/*    printf("Header = %d\n", tile_table_pos);*/
 
1451
                        stat(mzte_codec.m_cBitFile, &file_info);
 
1452
/*          printf("%d\n", file_info.st_size);*/
 
1453
                        table[0] = file_info.st_size - tile_table_pos
 
1454
                                                                - (34 * mzte_codec.m_iNumOfTile + used_bits + 8)/8;
 
1455
                } else {
 
1456
                        stat(mzte_codec.m_cBitFile, &file_info);
 
1457
/*          printf("%d\n", file_info.st_size);*/
 
1458
                        table[iTile] = file_info.st_size - init_size;
 
1459
                }
 
1460
/*    printf("Encoded Table[%d] = %d\n", iTile, table[iTile]);*/
 
1461
        }
 
1462
// end: added by Sharp (99/4/7)
 
1463
 
 
1464
        
 
1465
// begin: deleted by Sharp (99/4/7)
 
1466
#if 0
 
1467
        // begin: added by Sharp (99/2/16)
 
1468
                if (mzte_codec.m_tiling_disable==0){
 
1469
                        struct stat file_info;
 
1470
 
 
1471
                        if (iTile==0){
 
1472
                                TextureObjectLayer_enc(synfilter, iTile, &tile_table_pos);
 
1473
        /*    printf("Header = %d\n", tile_table_pos);*/
 
1474
                                stat(mzte_codec.m_cBitFile, &file_info);
 
1475
        /*          printf("%d\n", file_info.st_size);*/
 
1476
                                table[0] = file_info.st_size - tile_table_pos
 
1477
                                                                        - (34 * mzte_codec.m_iNumOfTile + used_bits + 8)/8;
 
1478
                        } else {
 
1479
                                int init_size;
 
1480
                                stat(mzte_codec.m_cBitFile, &file_info);
 
1481
                                init_size = file_info.st_size;
 
1482
 
 
1483
                                TextureObjectLayer_enc(synfilter, iTile, &garbage);
 
1484
                                stat(mzte_codec.m_cBitFile, &file_info);
 
1485
        /*          printf("%d\n", file_info.st_size);*/
 
1486
                                table[iTile] = file_info.st_size - init_size;
 
1487
                        }
 
1488
        /*    printf("Encoded Table[%d] = %d\n", iTile, table[iTile]);*/
 
1489
                }
 
1490
                else {
 
1491
                        TextureObjectLayer_enc(synfilter, iTile, &garbage);
 
1492
                }
 
1493
        // end: added by Sharp (99/2/16)
 
1494
#endif
 
1495
// end: deleted by Sharp (99/4/7)
 
1496
 
 
1497
        //      TextureObjectLayer_enc(synfilter);  deleted by Sharp (99/2/16)
 
1498
        // begin: added by Sharp (99/2/16)
 
1499
                }
 
1500
 
 
1501
                if (mzte_codec.m_tiling_disable==0 && mzte_codec.m_tiling_jump_table_enable == 1){
 
1502
                        bitfile = fopen(mzte_codec.m_cBitFile,"r+b");
 
1503
                        garbage = ftell(bitfile);
 
1504
                        if (fseek(bitfile, tile_table_pos, SEEK_SET)!=0){
 
1505
                                fprintf(stderr, "rewind failed\n");
 
1506
                                exit(111);
 
1507
                        }
 
1508
                        garbage = ftell(bitfile);
 
1509
                        
 
1510
                        {
 
1511
                                unsigned char byte_buf;
 
1512
                                Int bits;
 
1513
 
 
1514
                                fread(&byte_buf, sizeof(char), 1, bitfile);
 
1515
                                fflush(bitfile);
 
1516
                                fseek(bitfile, -1L, SEEK_CUR);
 
1517
                                byte_buf = (byte_buf >> (8 - used_bits));
 
1518
                                bits = emit_bits_local ( byte_buf, used_bits, bitfile );
 
1519
                                for (Int iTile=0; iTile<mzte_codec.m_iNumOfTile; iTile++) {
 
1520
                                        bits = emit_bits_local ( (table[iTile]>>16), 16, bitfile );
 
1521
                                        bits = emit_bits_local ( 1, 1, bitfile );
 
1522
                                        bits = emit_bits_local ( (table[iTile]&0xffff), 16, bitfile );
 
1523
                                        bits = emit_bits_local ( 1, 1, bitfile );
 
1524
                                }
 
1525
                                fflush(bitfile);
 
1526
                                fread(&byte_buf, sizeof(char), 1, bitfile);
 
1527
                                fflush(bitfile);
 
1528
                                fseek(bitfile, -1L, SEEK_CUR);
 
1529
                                emit_bits_local ( byte_buf, bits, bitfile );
 
1530
                        }
 
1531
                        fclose(bitfile);
 
1532
                        free(table); // added by Sharp (99/5/10)
 
1533
                }
 
1534
 
 
1535
        // end: added by Sharp (99/2/16)
 
1536
                /*----- free up coeff data structure -----*/
 
1537
                noteDetail("Freeing up encoding data structures....");
 
1538
                for (col=0; col<mzte_codec.m_iColors; col++) {
 
1539
        // begin: added by Sharp (99/2/16)
 
1540
                  /* delete */ free (mzte_codec.m_Image[col].data);
 
1541
/*                      delete (mzte_codec.m_Image[col].mask);*/
 
1542
                        if ( mzte_codec.m_tiling_disable == 0 ){ //modified by SL 030399
 
1543
                          free /*delete*/ (mzte_codec.m_ImageOrg[col].data);
 
1544
/*                              delete (mzte_codec.m_ImageOrg[col].mask);*/
 
1545
                        }
 
1546
        // end: added by Sharp (99/2/16)
 
1547
                        
 
1548
                        if (mzte_codec.m_SPlayer[col].coeffinfo[0] != NULL)
 
1549
                                delete (mzte_codec.m_SPlayer[col].coeffinfo[0]);
 
1550
                        mzte_codec.m_SPlayer[col].coeffinfo[0] = NULL;
 
1551
                        if (mzte_codec.m_SPlayer[col].coeffinfo)
 
1552
                                delete (mzte_codec.m_SPlayer[col].coeffinfo);
 
1553
                        mzte_codec.m_SPlayer[col].coeffinfo = NULL;
 
1554
                }
 
1555
// begin: added by Sharp (99/2/16)
 
1556
                delete (mzte_codec.m_Image);
 
1557
                delete (mzte_codec.m_ImageOrg);
 
1558
// end: added by Sharp (99/2/16)
 
1559
// begin: added by Sharp (99/3/29)
 
1560
                delete anafilter;
 
1561
                delete synfilter;
 
1562
// end: added by Sharp (99/3/29)
 
1563
                noteDetail("Completed freeing up encoding data structures.");
 
1564
 
 
1565
                noteProgress("\n----- Encoding Completed. -----\n");
 
1566
        }
 
1567
        else { /* this part should be copyed from VERSION 1 FCD software later */
 
1568
 
 
1569
        // allocate memory for source image
 
1570
        mzte_codec.m_Image = new PICTURE[3];
 
1571
 
 
1572
        // hjlee 0901
 
1573
          anafilter = (FILTER **) malloc(sizeof(FILTER *)*mzte_codec.m_iWvtDecmpLev);
 
1574
          synfilter = (FILTER **) malloc(sizeof(FILTER *)*mzte_codec.m_iWvtDecmpLev);
 
1575
          if(anafilter == NULL || synfilter == NULL) 
 
1576
                errorHandler("Error allocating memory for filters\n");
 
1577
          for(i=0;i<mzte_codec.m_iWvtDecmpLev; i++) {
 
1578
                choose_wavelet_filter(&(anafilter[i]),
 
1579
                                  &(synfilter[mzte_codec.m_iWvtDecmpLev-1-i]),
 
1580
                                  mzte_codec.m_WvtFilters[mzte_codec.m_iWvtUniform?0:i]);
 
1581
          }
 
1582
          
 
1583
          // read source image
 
1584
        read_image(     m_cImagePath,
 
1585
                                mzte_codec.m_iWidth,
 
1586
                                mzte_codec.m_iHeight,
 
1587
                                mzte_codec.m_iColors,
 
1588
                                8,
 
1589
                                mzte_codec.m_Image
 
1590
                                );
 
1591
 
 
1592
        if (mzte_codec.m_iAlphaChannel != 0) {  // arbitrary shape coding
 
1593
 
 
1594
                // allocate meory for segmentation
 
1595
                mzte_codec.m_SegImage = new PICTURE[3];
 
1596
 
 
1597
                // read the segmentation map of the source image */
 
1598
                mzte_codec.m_iAlphaChannel = read_segimage(m_cSegImagePath, 
 
1599
                                                                                  mzte_codec.m_Image[0].width, 
 
1600
                                                                                  mzte_codec.m_Image[0].height, 
 
1601
                                                                                  mzte_codec.m_iColors, 
 
1602
                                                                                  mzte_codec.m_Image); 
 
1603
 
 
1604
        }
 
1605
 
 
1606
 
 
1607
        get_virtual_image_V1(mzte_codec.m_Image,
 
1608
                                        mzte_codec.m_iWvtDecmpLev,
 
1609
                                        mzte_codec.m_iAlphaChannel,
 
1610
                                        mzte_codec.m_iColors,
 
1611
                                        mzte_codec.m_iAlphaTh,
 
1612
                                        mzte_codec.m_iChangeCRDisable,
 
1613
                                        anafilter[0]);
 
1614
 
 
1615
        for (col=0; col<mzte_codec.m_iColors; col++) {
 
1616
                mzte_codec.m_Image[col].height = mzte_codec.m_iHeight >> (Int)(col>0);
 
1617
                mzte_codec.m_Image[col].width  = mzte_codec.m_iWidth >> (Int)(col>0);
 
1618
        }
 
1619
        mzte_codec.m_iAcmOrder = 0;
 
1620
        mzte_codec.m_iAcmMaxFreqChg = 0;
 
1621
 
 
1622
        init_acm_maxf_enc();
 
1623
 
 
1624
//      fprintf(stdout,"init ac model!\n");     
 
1625
        
 
1626
        int height;
 
1627
        int width;
 
1628
        for (col=0; col<mzte_codec.m_iColors; col++) {
 
1629
                int x,y;
 
1630
 
 
1631
                height = mzte_codec.m_Image[col].height; 
 
1632
                width  = mzte_codec.m_Image[col].width;
 
1633
    
 
1634
                mzte_codec.m_SPlayer[col].coeffinfo = new COEFFINFO * [height];
 
1635
                if (mzte_codec.m_SPlayer[col].coeffinfo == NULL)
 
1636
                        exit(fprintf(stderr,"Allocating memory for coefficient structure (I)."));
 
1637
                mzte_codec.m_SPlayer[col].coeffinfo[0] = new COEFFINFO [height*width];
 
1638
                if (mzte_codec.m_SPlayer[col].coeffinfo[0] == NULL)
 
1639
                        exit(fprintf(stderr,"Allocating memory for coefficient structure (II)."));
 
1640
                for (y = 1; y < height; ++y)
 
1641
                  mzte_codec.m_SPlayer[col].coeffinfo[y] = 
 
1642
                        mzte_codec.m_SPlayer[col].coeffinfo[y-1]+width;
 
1643
                
 
1644
                for (y=0; y<height; y++)
 
1645
                        for (x=0; x<width; x++) {
 
1646
                                mzte_codec.m_SPlayer[col].coeffinfo[y][x].skip =0;
 
1647
                        }
 
1648
 
 
1649
        }
 
1650
 
 
1651
//      fprintf(stdout,"Coeffinfo memory allocation done!\n");
 
1652
 
 
1653
        /* DISCRETE WAVELET TRANSFORM */
 
1654
        noteProgress("Wavelet Transform....");  
 
1655
 
 
1656
 
 
1657
 
 
1658
//      choose_wavelet_filter(&anafilter, &synfilter, mzte_codec.m_iWvtType); // hjlee 0901
 
1659
        perform_DWT(anafilter);
 
1660
        noteProgress("Completed wavelet transform.");
 
1661
 
 
1662
        TextureObjectLayer_enc_V1(synfilter); 
 
1663
 
 
1664
        /*----- free up coeff data structure -----*/
 
1665
        noteDetail("Freeing up encoding data structures....");
 
1666
        for (col=0; col<mzte_codec.m_iColors; col++) {
 
1667
                if (mzte_codec.m_SPlayer[col].coeffinfo[0] != NULL)
 
1668
                        delete (mzte_codec.m_SPlayer[col].coeffinfo[0]);
 
1669
                mzte_codec.m_SPlayer[col].coeffinfo[0] = NULL;
 
1670
                if (mzte_codec.m_SPlayer[col].coeffinfo)
 
1671
                        delete (mzte_codec.m_SPlayer[col].coeffinfo);
 
1672
                mzte_codec.m_SPlayer[col].coeffinfo = NULL;
 
1673
        }
 
1674
        noteDetail("Completed freeing up encoding data structures.");
 
1675
 
 
1676
        noteProgress("\n----- Encoding Completed. -----\n");
 
1677
 
 
1678
        }
 
1679
}
 
1680
 
 
1681
 
 
1682
// begin: added by Sharp (99/2/16)
 
1683
Int CVTCEncoder::emit_bits_local( UShort data, Int size, FILE *fp )
 
1684
{
 
1685
    static Int remain_bits = 0;
 
1686
    static UInt buf = 0;
 
1687
    UInt put_buffer = data;
 
1688
    unsigned char c;
 
1689
 
 
1690
                assert(sizeof(UShort)==2);
 
1691
    /* Mask off any excess bits in code */
 
1692
    put_buffer &= (((Int)1) << size) - 1;
 
1693
 
 
1694
    /* new number of bits in buffer */
 
1695
    remain_bits += size;
 
1696
 
 
1697
    /* align incoming bits */
 
1698
    put_buffer <<= 24 - remain_bits;
 
1699
 
 
1700
    /* and merge with old buffer contents */
 
1701
    put_buffer |= buf;
 
1702
 
 
1703
    while (remain_bits >= 8) {
 
1704
        c = (unsigned char) ((put_buffer >> 16) & 0xFF);
 
1705
//                              printf("%d at %d\n", c, ftell(fp));
 
1706
//                              getchar();
 
1707
        fwrite(&c, sizeof(char), 1, fp);
 
1708
        put_buffer <<= 8;
 
1709
        remain_bits    -= 8;
 
1710
    }
 
1711
    buf = put_buffer;
 
1712
 
 
1713
    return remain_bits;
 
1714
}
 
1715
 
 
1716
Void CVTCEncoder::tile_header_Enc(FILTER **wvtfilter, Int tile_id)
 
1717
{
 
1718
  if ( mzte_codec.m_tiling_disable == 0 ){
 
1719
    emit_bits((UShort)(TEXTURE_TILE_START_CODE>>16), 16);
 
1720
    emit_bits((UShort)TEXTURE_TILE_START_CODE, 16);
 
1721
    emit_bits((UShort)tile_id, 16);
 
1722
 
 
1723
    if ( mzte_codec.m_extension_type == 1 ){
 
1724
      emit_bits((UShort)1, 16); /* reference tile_id1 */
 
1725
      emit_bits((UShort)1, 16); /* reference tile_id2 */
 
1726
    }
 
1727
  }
 
1728
 
 
1729
// FPDAM begin: added by Sharp
 
1730
 
 
1731
        if ( mzte_codec.m_usErrResiDisable ) {
 
1732
                if ( mzte_codec.m_iAlphaChannel ) {
 
1733
                        emit_bits((UShort)MARKER_BIT, 1);
 
1734
                        emit_bits((UShort)mzte_codec.m_iTextureTileType, 2);
 
1735
                        emit_bits((UShort)MARKER_BIT, 1);
 
1736
#ifdef  _FPDAM_DBG_
 
1737
fprintf(stderr,".............texture_tile_type=%d\n",mzte_codec.m_iTextureTileType);
 
1738
#endif
 
1739
                }
 
1740
 
 
1741
                if ( mzte_codec.m_iAlphaChannel && mzte_codec.m_iTextureTileType == BOUNDA_TILE ){
 
1742
                        noteProgress("Encoding Tile Shape Bitstream ....");
 
1743
                        ShapeEnCoding(mzte_codec.m_Image[0].mask, mzte_codec.m_iWidth, mzte_codec.m_iHeight, 
 
1744
                                mzte_codec.m_iWvtDecmpLev, 
 
1745
                                mzte_codec.m_iSTOConstAlpha,
 
1746
                                mzte_codec.m_iSTOConstAlphaValue, 
 
1747
                                mzte_codec.m_iChangeCRDisable,
 
1748
                                mzte_codec.m_iShapeScalable,
 
1749
                                mzte_codec.m_bStartCodeEnable,
 
1750
                                wvtfilter);
 
1751
                }
 
1752
        }
 
1753
 
 
1754
// FPDAM end: added by Sharp
 
1755
}
 
1756
 
 
1757
// end: added by Sharp (99/2/16)