1
/****************************************************************************/
2
/* MPEG4 Visual Texture Coding (VTC) Mode Software */
4
/* This software was jointly developed by the following participants: */
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) */
15
/* Bi-level is provided by Texas Instruments */
16
/* Jie Liang (liang@ti.com) */
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) */
23
/* OKI, Sharp, Sarnoff, TI and Microsoft contributed to bitstream */
24
/* exchange and bug fixing. */
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. */
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. */
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. */
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. */
47
/* Copyright 1995, 1996, 1997, 1998 ISO/IEC */
48
/****************************************************************************/
50
/************************************************************/
51
/* Sarnoff Very Low Bit Rate Still Image Coder */
52
/* Copyright 1995, 1996, 1997, 1998 Sarnoff Corporation */
53
/************************************************************/
59
// begin: added by Sharp (99/2/16)
62
// end: added by Sharp (99/2/16)
66
#include "dataStruct.hpp"
67
#include "startcode.hpp"
68
#include "bitpack.hpp"
70
#include "globals.hpp"
71
#include "states.hpp" // hjlee 0901
75
CVTCEncoder::CVTCEncoder()
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];
83
CVTCEncoder::~CVTCEncoder()
86
delete m_cSegImagePath;
87
delete m_cOutBitsFile;
88
delete mzte_codec.m_cBitFile;
91
//CVTCEncoder::CVTCEncoder(
92
Void CVTCEncoder::init(
93
UInt uiVerID, // added by Sharp (99/11/18)
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)
108
// begin: added by Sharp (99/2/16)
109
UInt uiTilingDisable,
113
// end: added by Sharp (99/2/16)
115
UInt uiWvtDownload, // hjlee 0901
117
UInt uiWvtUniform, // hjlee 0901
118
Int* iWvtFilters, // hjlee 0901
120
UInt uiScanDirection,
121
Bool bStartCodeEnable,
122
UInt uiTargetSpatialLev,
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,
129
// end: added by Sharp (99/2/16)
133
UInt defaultSpatialScale, // hjlee 0901
134
Int lastWvtDecompInSpaLayer[MAXDECOMPLEV], // hjlee 0901
136
//Added by Sarnoff for error resilience, 3/5/99
137
Int uiErrResiDisable, //bbc, 2/19/99
140
//End: Added by Sarnoff for error resilience, 3/5/99
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);
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)
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) {
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];
207
for (int i=0; i<(int)uiColors; i++)
208
mzte_codec.m_Qinfo[i] = Qinfo[i];
210
mzte_codec.m_iQDC[0] = (int)uiQdcY;
211
mzte_codec.m_iQDC[1] = mzte_codec.m_iQDC[2] = (int)uiQdcUV;
213
/* mzte_codec.m_iDCWidth = GetDCWidth();
214
mzte_codec.m_iDCHeight = GetDCHeight(); */ //deleted by SL 030499
216
mzte_codec.m_iSingleBitFile = 1;
217
// mzte_codec.m_cBitFile = NULL;
218
mzte_codec.m_cBitFileAC = NULL;
221
if (mzte_codec.m_iQuantType==1 && mzte_codec.m_iScanDirection==0)
222
mzte_codec.m_bStartCodeEnable = 0;
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
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()
244
Void CVTCEncoder::close_buffer_file(FILE *fp)
250
Void CVTCEncoder::texture_packet_header_Enc(FILTER **wvtfilter) // hjlee 0901
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;
259
if(!mzte_codec.m_usErrResiDisable){
260
flush_bits(); // added by Sharp(99/3/29)
261
texture_object_id=1<<15; // error resilience
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
268
//End: Added by Sarnoff for error resilience, 3/5/99
270
header_Enc_Common( wvtfilter ,1);
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
277
//End: Added by Sarnoff for error resilience, 3/5/99
280
Void CVTCEncoder::header_Enc_V1(FILTER **wvtfilter) // hjlee 0901
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
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);
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);
304
if (mzte_codec.m_iQuantType==2) {
306
emit_bits((UShort)mzte_codec.m_iSpatialLev, 4);
308
/* Calc number decomp layers for all spatial layers */
309
if (mzte_codec.m_iSpatialLev == 1)
311
mzte_codec.m_lastWvtDecompInSpaLayer[0][0]=mzte_codec.m_iWvtDecmpLev-1;
313
else if (mzte_codec.m_iSpatialLev != mzte_codec.m_iWvtDecmpLev)
315
emit_bits((UShort)mzte_codec.m_defaultSpatialScale, 1);
316
if (mzte_codec.m_defaultSpatialScale==0)
318
/* For the 1st spatial_scalability_levels-1 layers the luma componant
319
of lastWvtDecompInSpaLayer should have been filled in from the
321
for (i=0; i<mzte_codec.m_iSpatialLev-1; ++i)
322
emit_bits((UShort)mzte_codec.m_lastWvtDecompInSpaLayer[i][0], 4);
324
mzte_codec.m_lastWvtDecompInSpaLayer[mzte_codec.m_iSpatialLev-1][0]
325
=mzte_codec.m_iWvtDecmpLev-1;
331
sp0=mzte_codec.m_iWvtDecmpLev-mzte_codec.m_iSpatialLev;
332
mzte_codec.m_lastWvtDecompInSpaLayer[0][0]=sp0;
334
for (i=1; i<mzte_codec.m_iSpatialLev; ++i)
335
mzte_codec.m_lastWvtDecompInSpaLayer[i][0]=sp0+i;
340
for (i=0; i<mzte_codec.m_iSpatialLev; ++i)
341
mzte_codec.m_lastWvtDecompInSpaLayer[i][0]=i;
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;
352
if (mzte_codec.m_iWvtDownload == 1) {
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]);
360
for(i=0;i<mzte_codec.m_iWvtDecmpLev;i++)
361
upload_wavelet_filters(wvtfilter[i]);
365
emit_bits((UShort)wavelet_stuffing, 3);
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);
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();
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
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
396
long tile_table_position=0;
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);
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)
408
if(!mzte_codec.m_usErrResiDisable){
409
flush_bits(); // added by Sharp(99/3/29)
410
texture_object_id=1<<15; // error resilience
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
417
//End: Added by Sarnoff for error resilience, 3/5/99
420
// if ( mzte_codec.m_usErrResiDisable == 1) // added by Sharp (99/3/29) // @@@@@@
421
header_Enc_Common( wvtfilter );
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);
433
mzte_codec.m_extension_type = 0;
434
used_bits = current_put_bits() % 8;
436
/* printf("used_bits = %d\n", used_bits);*/
438
tile_table_position = current_fp();
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);
444
/* next_start_code */
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);
457
return tile_table_position;
459
// end: modified by Sharp (99/2/16)
461
// begin: added by Sharp (99/2/16)
462
Void CVTCEncoder::header_Enc_Common(FILTER **wvtfilter, Int SkipShape) // hjlee 0901
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
471
/*------- Write header info to bitstream file -------*/
472
emit_bits((UShort)texture_object_id, 16);
473
emit_bits((UShort)MARKER_BIT, 1);
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);
484
if (mzte_codec.m_iQuantType==2) {
486
emit_bits((UShort)mzte_codec.m_iSpatialLev, 4);
488
/* Calc number decomp layers for all spatial layers */
489
if (mzte_codec.m_iSpatialLev == 1)
491
mzte_codec.m_lastWvtDecompInSpaLayer[0][0]=mzte_codec.m_iWvtDecmpLev-1;
493
else if (mzte_codec.m_iSpatialLev != mzte_codec.m_iWvtDecmpLev)
495
emit_bits((UShort)mzte_codec.m_defaultSpatialScale, 1);
496
if (mzte_codec.m_defaultSpatialScale==0)
498
/* For the 1st spatial_scalability_levels-1 layers the luma componant
499
of lastWvtDecompInSpaLayer should have been filled in from the
501
for (i=0; i<mzte_codec.m_iSpatialLev-1; ++i)
502
emit_bits((UShort)mzte_codec.m_lastWvtDecompInSpaLayer[i][0], 4);
504
mzte_codec.m_lastWvtDecompInSpaLayer[mzte_codec.m_iSpatialLev-1][0]
505
=mzte_codec.m_iWvtDecmpLev-1;
511
sp0=mzte_codec.m_iWvtDecmpLev-mzte_codec.m_iSpatialLev;
512
mzte_codec.m_lastWvtDecompInSpaLayer[0][0]=sp0;
514
for (i=1; i<mzte_codec.m_iSpatialLev; ++i)
515
mzte_codec.m_lastWvtDecompInSpaLayer[i][0]=sp0+i;
520
for (i=0; i<mzte_codec.m_iSpatialLev; ++i)
521
mzte_codec.m_lastWvtDecompInSpaLayer[i][0]=i;
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;
532
if (mzte_codec.m_iWvtDownload == 1) {
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]);
540
for(i=0;i<mzte_codec.m_iWvtDecmpLev;i++)
541
upload_wavelet_filters(wvtfilter[i]);
545
emit_bits((UShort)wavelet_stuffing, 3);
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);
552
// ~added for FDAM1 by Samsung AIT on 2000/02/03
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)
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);
568
// end: deleted by Sharp (99/2/16)
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,
594
//end: added by SL@Sarnoff (03/03/99)
596
// FPDAM begin: added by Sharp
598
// FPDAM end: added by Sharp
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);
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
616
// end: added by Sharp (99/2/16)
618
/**********************************************************/
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)
624
/* put quant value and maximums on the bitstream */
625
put_param(snr_image->quant, 7);
626
// emit_bits((UShort)MARKER_BIT, 1); // 1124
630
for (l=0; l<=mzte_codec.m_lastWvtDecompInSpaLayer[spaLayer][color];++l)
632
emit_bits((UShort)snr_image->wvtDecompNumBitPlanes[l],5);
633
if (((l+1) % 4) == 0)
634
emit_bits((UShort)MARKER_BIT, 1);
644
Void CVTCEncoder::Put_Quant_and_Max_SQBB(SNR_IMAGE *snr_image, Int spaLayer,
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);
652
emit_bits((UShort)snr_image->wvtDecompNumBitPlanes[spaLayer],5);
654
emit_bits((UShort)snr_image->wvtDecompNumBitPlanes[spaLayer-1],5);
661
/**********************************************************/
663
Void CVTCEncoder::textureLayerDC_Enc()
667
noteProgress("Encoding DC coefficients....");
669
for (col=0; col<mzte_codec.m_iColors; col++)
671
/* Set global color variable */
672
mzte_codec.m_iCurColor=col;
674
/* initialize DC coefficient info */
675
if ((err=ztqInitDC(0, col)))
676
errorHandler("ztqInitDC");
678
/* quantize DC coefficients */
679
if ((err=encQuantizeDC(col)))
680
errorHandler("encQuantizeDC");
682
/* losslessly encoding DC coefficients */
683
wavelet_dc_encode(col);
688
noteProgress("Completed encoding DC coefficients.");
691
/**********************************************************/
693
Void CVTCEncoder::TextureSpatialLayerSQNSC_enc(Int spa_lev)
696
SNR_IMAGE *snr_image; // hjlee 0901
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);
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);
708
mzte_codec.m_iCurColor = col;
709
if (spa_lev !=0 || col ==0) {
710
wavelet_higher_bands_encode_SQ_band(col);
717
Void CVTCEncoder::TextureSpatialLayerSQ_enc(Int spa_lev, FILE *bitfile)
719
// Char fname[100]; // hjlee
721
/*------- AC: Open and initialize bitstream file -------*/
722
if (mzte_codec.m_iSingleBitFile==0)
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);
733
/* initialize the buffer */
734
init_bit_packing_fp(bitfile,1);
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);
741
TextureSpatialLayerSQNSC_enc(spa_lev);
743
/*------- AC: Close bitstream file -------*/
744
if (mzte_codec.m_iSingleBitFile)
747
close_buffer_file(bitfile);
751
Void CVTCEncoder::textureLayerSQ_Enc(FILE *bitfile)
753
Int col, err, spa_lev;
754
SNR_IMAGE *snr_image;
757
noteProgress("Encoding AC coefficients - Single-Quant Mode....");
759
/*------- AC: Set spatial and SNR levels to zero -------*/
760
mzte_codec.m_iCurSpatialLev = 0;
761
mzte_codec.m_iCurSNRLev = 0;
763
// mzte_codec.m_iSpatialLev=1; // hjlee 0901
764
setSpatialLayerDimsSQ(0); // hjlee 0901
766
for (col=0; col<mzte_codec.m_iColors; col++)
768
snr_image=&(mzte_codec.m_SPlayer[col].SNRlayer.snr_image);
770
/* Set global color variable */
771
mzte_codec.m_iCurColor = col;
772
snr_image->quant = mzte_codec.m_Qinfo[col][0].Quant[0];
774
/* initialization of spatial dimensions for each color component */
775
setSpatialLevelAndDimensions(0, col);
777
/* initialize AC coefficient info for each color component */
778
if ((err=ztqInitAC(0, col)))
779
errorHandler("ztqInitAC");
781
/* quantize and mark zerotree structure for AC coefficients */
782
if ((err=encQuantizeAndMarkAC(col)))
783
errorHandler("encQuantizeAndMarkAC");
787
/*------- AC: encode all color components -------*/
788
if (mzte_codec.m_iScanDirection==0) /* tree-depth scan */
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
796
/* losslessly encoding AC coefficients */
797
wavelet_higher_bands_encode_SQ_tree();
799
else /* band by band scan */
801
setSpatialLayerDimsSQ(1); // hjlee 0901
803
/* Assumes all three color components have the same number of SNR
805
for (col=0; col<mzte_codec.m_iColors; col++)
806
mzte_codec.m_SPlayer[col].SNR_scalability_levels = 1;
808
/* Loop through spatial layers */
809
for (spa_lev=0; spa_lev<mzte_codec.m_iSpatialLev;
812
mzte_codec.m_iCurSpatialLev=spa_lev;
813
for (col=0; col<mzte_codec.m_iColors; col++)
814
setSpatialLevelAndDimensions(spa_lev,col);
816
/*----- AC: Set global spatial layer. -----*/
817
mzte_codec.m_iCurSpatialLev = spa_lev;
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);
825
TextureSpatialLayerSQNSC_enc(spa_lev);
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)
834
noteProgress("Completed encoding AC coefficients - Single-Quant Mode.");
838
/**********************************************************/
841
Void CVTCEncoder::TextureSNRLayerMQ_encode(Int spa_lev, Int snr_lev, FILE *fp)
843
SNR_IMAGE *snr_image;
845
static Int texture_snr_layer_id=0;
847
if(mzte_codec.m_bStartCodeEnable){
848
noteProgress("Encoding Multi-Quant Mode Layer with SNR start code....");
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);
855
noteProgress("Encoding Multi-Quant Mode Layer without SNR start code....");
857
noteProgress("Multi-Quant Mode - Spatial %d, SNR %d", spa_lev,snr_lev);
863
noteDetail("width=%d height=%d",mzte_codec.m_SPlayer[col].width,
864
mzte_codec.m_SPlayer[col].height);
866
/* Set global color variable */
867
mzte_codec.m_iCurColor = col;
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]);
875
/* initialization of spatial dimensions for each color component */
877
setSpatialLevelAndDimensions(spa_lev, col);
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);
884
/* quantize and mark zerotree structure for AC coefficients */
885
if (encQuantizeAndMarkAC(col))
886
errorHandler("encQuantizeAndMarkAC");
888
// Put_Quant_and_Max(snr_image); // hjlee 0901
889
Put_Quant_and_Max(snr_image,spa_lev,col); // hjlee 0901
892
wavelet_higher_bands_encode_MQ(mzte_codec.m_iScanDirection);
898
/* Set global color variable */
899
mzte_codec.m_iCurColor = col;
901
/* Update states of AC coefficients */
902
if (encUpdateStateAC(mzte_codec.m_iCurColor))
903
errorHandler("encUpdateStateAC");
907
Void CVTCEncoder::textureLayerMQ_Enc(FILE *bitfile)
909
Int err, spa_lev, snr_lev=0, snr_scalability_levels;
910
Char fname[100]; // hjlee
912
getSpatialLayerDims(); // hjlee 0901
915
/*------- AC: Initialize QList Structure -------*/
916
if ((err=ztqQListInit()))
917
errorHandler("Allocating memory for QList information.");
919
/* Initialize coeffs */
920
setSpatialLevelAndDimensions(0,0);
921
if ((err=ztqInitAC(0,0)))
922
errorHandler("ztqInitAC");
924
if (mzte_codec.m_iColors > 1)
926
if (mzte_codec.m_lastWvtDecompInSpaLayer[0][1]<0)
927
setSpatialLevelAndDimensions(1,1);
929
setSpatialLevelAndDimensions(0,1);
930
if ((err=ztqInitAC(0,1)))
931
errorHandler("ztqInitAC");
933
if (mzte_codec.m_iColors > 2)
935
if (mzte_codec.m_lastWvtDecompInSpaLayer[0][2]<0)
936
setSpatialLevelAndDimensions(1,2);
938
setSpatialLevelAndDimensions(0,2);
939
if ((err=ztqInitAC(0,2)))
940
errorHandler("ztqInitAC");
944
/* Loop through spatial layers */
945
for (spa_lev=0; spa_lev<mzte_codec.m_iSpatialLev;
948
/*----- AC: Set global spatial layer and SNR scalability level. -----*/
949
/* Assumes all three color components have the same number of SNR
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;
956
/* Update spatial level coeff info if changing spatial levels.
957
Do this for all color components. */
960
for (mzte_codec.m_iCurColor = 0; mzte_codec.m_iCurColor<mzte_codec.m_iColors;
961
mzte_codec.m_iCurColor++)
963
setSpatialLevelAndDimensions(mzte_codec.m_iCurSpatialLev,
964
mzte_codec.m_iCurColor);
967
if (mzte_codec.m_lastWvtDecompInSpaLayer[spa_lev-1][mzte_codec.m_iCurColor]
969
spatialLayerChangeUpdate(mzte_codec.m_iCurColor);
974
if (!mzte_codec.m_bStartCodeEnable)
975
/*------- AC: Write header info to bitstream file -------*/
976
emit_bits(snr_scalability_levels, 5);
978
/* Loop through SNR layers */
979
for (snr_lev=0; snr_lev<snr_scalability_levels; snr_lev++)
981
/*----- AC: Set global SNR layer -----*/
982
mzte_codec.m_iCurSNRLev = snr_lev;
984
if (mzte_codec.m_bStartCodeEnable)
986
/*------- AC: Open and initialize bitstream file -------*/
987
if (mzte_codec.m_iSingleBitFile==0)
990
// wmay - this is bad
991
sprintf(fname,mzte_codec.m_cBitFileAC,
992
mzte_codec.m_iCurSpatialLev, mzte_codec.m_iCurSNRLev);
995
if ((bitfile=fopen(fname,"wb"))==NULL)
996
errorHandler("Can't open file '%s' for writing.",fname);
999
/* initialize the buffer */
1000
init_bit_packing_fp(bitfile,1);
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 */
1013
/*------- AC: Quantize and encode all color components -------*/
1014
TextureSNRLayerMQ_encode(spa_lev, snr_lev, bitfile);
1015
if (mzte_codec.m_bStartCodeEnable)
1017
if (mzte_codec.m_iSingleBitFile)
1018
flush_buffer_file();
1020
close_buffer_file(bitfile);
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;
1033
/*------- AC: Free Qlist structure -------*/
1038
/**********************************************************/
1040
Void CVTCEncoder::TextureObjectLayer_enc(FILE *bitfile) // modified by Sharp (99/2/16)
1041
//Void CVTCEncoder::TextureObjectLayer_enc(FILTER *wvtfilter) // hjlee 0901
1046
/*------- DC: Open and initialize bitstream file -------*/
1047
if ( iTile == 0 ){ // added by Sharp (99/2/16)
1049
if ((bitfile=fopen(mzte_codec.m_cBitFile,"wb"))==NULL)
1050
errorHandler("Can't open file '%s' for writing.",
1051
mzte_codec.m_cBitFile);
1054
// begin: added by Sharp (99/2/16)
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);
1060
fseek(bitfile,0,SEEK_END);
1062
// end: added by Sharp (99/2/16)
1064
/* initialize variables */
1065
init_bit_packing_fp(bitfile,1);
1068
/* for PEZW, always enabled */
1069
if (mzte_codec.m_iQuantType == BILEVEL_Q)
1070
mzte_codec.m_bStartCodeEnable = 1;
1072
/* Write header info to bitstream */
1073
// begin: added by Sharp (99/2/16)
1075
if ( mzte_codec.m_tiling_disable == 0 ){
1077
*tile_table = header_Enc(wvtfilter);
1079
tile_header_Enc(iTile);
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
1087
// end: added by Sharp (99/2/16)
1089
// header_Enc(wvtfilter); // deleted by Sharp (99/2/16)
1091
/*------- DC: Quantize and encode all color components -------*/
1092
textureLayerDC_Enc();
1094
/*------- DC: Close bitstream file -------*/
1096
if (mzte_codec.m_bStartCodeEnable){
1097
if(mzte_codec.m_iSingleBitFile)
1098
flush_buffer_file();
1100
close_buffer_file(bitfile);
1104
/*-------------------------------------------------*/
1105
/*--------------------- AC ------------------------*/
1106
/*-------------------------------------------------*/
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);
1118
if (mzte_codec.m_iSingleBitFile){
1119
if(!mzte_codec.m_bStartCodeEnable)
1120
close_buffer_file(bitfile);
1126
Void CVTCEncoder::TextureObjectLayer_enc_V1(FILTER **wvtfilter)
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);
1135
/* for PEZW, always enabled */
1136
if (mzte_codec.m_iQuantType == BILEVEL_Q)
1137
mzte_codec.m_bStartCodeEnable = 1;
1139
/* initialize variables */
1140
init_bit_packing_fp(bitfile,1);
1142
/* Write header info to bitstream */
1143
header_Enc_V1(wvtfilter);
1145
/*------- DC: Quantize and encode all color components -------*/
1146
textureLayerDC_Enc();
1148
/*------- DC: Close bitstream file -------*/
1150
if (mzte_codec.m_bStartCodeEnable){
1151
if(mzte_codec.m_iSingleBitFile)
1152
flush_buffer_file();
1154
close_buffer_file(bitfile);
1158
/*-------------------------------------------------*/
1159
/*--------------------- AC ------------------------*/
1160
/*-------------------------------------------------*/
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);
1172
if (mzte_codec.m_iSingleBitFile){
1173
if(!mzte_codec.m_bStartCodeEnable)
1174
close_buffer_file(bitfile);
1183
/**************************************************************/
1185
Void CVTCEncoder::encode()
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;
1196
Int col; // added by Sharp (99/2/16)
1197
// end: added by Sharp (99/2/16)
1199
// mzte_codec.m_visual_object_verid = VERSION; /* version 2 */ // deleted by Sharp (99/11/18)
1201
// begin: added by Sharp (99/4/7)
1202
struct stat file_info;
1204
// end: added by Sharp (99/4/7)
1206
if ( mzte_codec.m_visual_object_verid != 1 ){ /* version 2 */
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)
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]);
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);
1228
if (mzte_codec.m_iAlphaChannel != 0) { // arbitrary shape coding
1229
// allocate meory for segmentation
1230
mzte_codec.m_SegImage = new PICTURE[3];
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);
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;
1249
if ( mzte_codec.m_tiling_disable == 0 ){
1250
printf("Wavelet Tiling ON....\n");
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,
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);
1262
// FPDAM end: added by Sharp
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();
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));
1278
/* printf("%d %d\n", mzte_codec.m_ImageOrg[0].width, mzte_codec.m_ImageOrg[0].height);*/
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)
1284
table = (Int *)malloc(sizeof(Int)*mzte_codec.m_iNumOfTile);
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);
1294
mzte_codec.m_iNumOfTile = 1;
1296
// end: added by Sharp (99/2/16)
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)
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;
1315
/* FPDAM end : added by Samsung AIT (99/09/03) */
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);
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)
1328
mzte_codec.m_iAcmOrder = 0;
1329
mzte_codec.m_iAcmMaxFreqChg = 0;
1331
init_acm_maxf_enc();
1333
// end: deleted by Sharp (99/2/16)
1335
// fprintf(stdout,"init ac model!\n");
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;
1344
// printf("SPlayer %d %d\n", width, height);
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;
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;
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);
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
1376
// begin: added by Sharp (99/2/16)
1377
for (Int iTile = 0; iTile<mzte_codec.m_iNumOfTile; iTile++ ){
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
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
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);
1400
// FPDAM begin: modified by Sharp
1401
// tile_header_Enc(iTile);
1402
tile_header_Enc(synfilter,iTile);
1403
// FPDAM end: modified by Sharp
1406
if( !mzte_codec.m_usErrResiDisable )
1407
texture_packet_header_Enc(synfilter);
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;
1414
init_acm_maxf_enc();
1415
// end: added by Sharp (99/2/16)
1417
/* DISCRETE WAVELET TRANSFORM */
1418
noteProgress("Wavelet Transform....");
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); //*********
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);
1432
// end: added by Sharp (99/2/16)
1433
// end: modified by Sharp (99/5/10)
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.");
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;
1446
TextureObjectLayer_enc(bitfile);
1448
if (mzte_codec.m_tiling_disable==0 && mzte_codec.m_tiling_jump_table_enable == 1) {
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;
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;
1460
/* printf("Encoded Table[%d] = %d\n", iTile, table[iTile]);*/
1462
// end: added by Sharp (99/4/7)
1465
// begin: deleted by Sharp (99/4/7)
1467
// begin: added by Sharp (99/2/16)
1468
if (mzte_codec.m_tiling_disable==0){
1469
struct stat file_info;
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;
1480
stat(mzte_codec.m_cBitFile, &file_info);
1481
init_size = file_info.st_size;
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;
1488
/* printf("Encoded Table[%d] = %d\n", iTile, table[iTile]);*/
1491
TextureObjectLayer_enc(synfilter, iTile, &garbage);
1493
// end: added by Sharp (99/2/16)
1495
// end: deleted by Sharp (99/4/7)
1497
// TextureObjectLayer_enc(synfilter); deleted by Sharp (99/2/16)
1498
// begin: added by Sharp (99/2/16)
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");
1508
garbage = ftell(bitfile);
1511
unsigned char byte_buf;
1514
fread(&byte_buf, sizeof(char), 1, 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 );
1526
fread(&byte_buf, sizeof(char), 1, bitfile);
1528
fseek(bitfile, -1L, SEEK_CUR);
1529
emit_bits_local ( byte_buf, bits, bitfile );
1532
free(table); // added by Sharp (99/5/10)
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);*/
1546
// end: added by Sharp (99/2/16)
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;
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)
1562
// end: added by Sharp (99/3/29)
1563
noteDetail("Completed freeing up encoding data structures.");
1565
noteProgress("\n----- Encoding Completed. -----\n");
1567
else { /* this part should be copyed from VERSION 1 FCD software later */
1569
// allocate memory for source image
1570
mzte_codec.m_Image = new PICTURE[3];
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]);
1583
// read source image
1584
read_image( m_cImagePath,
1585
mzte_codec.m_iWidth,
1586
mzte_codec.m_iHeight,
1587
mzte_codec.m_iColors,
1592
if (mzte_codec.m_iAlphaChannel != 0) { // arbitrary shape coding
1594
// allocate meory for segmentation
1595
mzte_codec.m_SegImage = new PICTURE[3];
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);
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,
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);
1619
mzte_codec.m_iAcmOrder = 0;
1620
mzte_codec.m_iAcmMaxFreqChg = 0;
1622
init_acm_maxf_enc();
1624
// fprintf(stdout,"init ac model!\n");
1628
for (col=0; col<mzte_codec.m_iColors; col++) {
1631
height = mzte_codec.m_Image[col].height;
1632
width = mzte_codec.m_Image[col].width;
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;
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;
1651
// fprintf(stdout,"Coeffinfo memory allocation done!\n");
1653
/* DISCRETE WAVELET TRANSFORM */
1654
noteProgress("Wavelet Transform....");
1658
// choose_wavelet_filter(&anafilter, &synfilter, mzte_codec.m_iWvtType); // hjlee 0901
1659
perform_DWT(anafilter);
1660
noteProgress("Completed wavelet transform.");
1662
TextureObjectLayer_enc_V1(synfilter);
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;
1674
noteDetail("Completed freeing up encoding data structures.");
1676
noteProgress("\n----- Encoding Completed. -----\n");
1682
// begin: added by Sharp (99/2/16)
1683
Int CVTCEncoder::emit_bits_local( UShort data, Int size, FILE *fp )
1685
static Int remain_bits = 0;
1686
static UInt buf = 0;
1687
UInt put_buffer = data;
1690
assert(sizeof(UShort)==2);
1691
/* Mask off any excess bits in code */
1692
put_buffer &= (((Int)1) << size) - 1;
1694
/* new number of bits in buffer */
1695
remain_bits += size;
1697
/* align incoming bits */
1698
put_buffer <<= 24 - remain_bits;
1700
/* and merge with old buffer contents */
1703
while (remain_bits >= 8) {
1704
c = (unsigned char) ((put_buffer >> 16) & 0xFF);
1705
// printf("%d at %d\n", c, ftell(fp));
1707
fwrite(&c, sizeof(char), 1, fp);
1716
Void CVTCEncoder::tile_header_Enc(FILTER **wvtfilter, Int tile_id)
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);
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 */
1729
// FPDAM begin: added by Sharp
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);
1737
fprintf(stderr,".............texture_tile_type=%d\n",mzte_codec.m_iTextureTileType);
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,
1754
// FPDAM end: added by Sharp
1757
// end: added by Sharp (99/2/16)