/
io.c
576 lines (503 loc) · 14.9 KB
/
io.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*************************************************************************
*
* This software module was originally contributed by Microsoft
* Corporation in the course of development of the
* ITU-T T.832 | ISO/IEC 29199-2 ("JPEG XR") format standard for
* reference purposes and its performance may not have been optimized.
*
* This software module is an implementation of one or more
* tools as specified by the JPEG XR standard.
*
* ITU/ISO/IEC give You a royalty-free, worldwide, non-exclusive
* copyright license to copy, distribute, and make derivative works
* of this software module or modifications thereof for use in
* products claiming conformance to the JPEG XR standard as
* specified by ITU-T T.832 | ISO/IEC 29199-2.
*
* ITU/ISO/IEC give users the same free license to this software
* module or modifications thereof for research purposes and further
* ITU/ISO/IEC standardization.
*
* Those intending to use this software module in products are advised
* that its use may infringe existing patents. ITU/ISO/IEC have no
* liability for use of this software module or modifications thereof.
*
* Copyright is not released for products that do not conform to
* to the JPEG XR standard as specified by ITU-T T.832 |
* ISO/IEC 29199-2.
*
******** Section to be removed when the standard is published ************
*
* Assurance that the contributed software module can be used
* (1) in the ITU-T "T.JXR" | ISO/IEC 29199 ("JPEG XR") standard once the
* standard has been adopted; and
* (2) to develop the JPEG XR standard:
*
* Microsoft Corporation and any subsequent contributors to the development
* of this software grant ITU/ISO/IEC all rights necessary to include
* the originally developed software module or modifications thereof in the
* JPEG XR standard and to permit ITU/ISO/IEC to offer such a royalty-free,
* worldwide, non-exclusive copyright license to copy, distribute, and make
* derivative works of this software module or modifications thereof for
* use in products claiming conformance to the JPEG XR standard as
* specified by ITU-T T.832 | ISO/IEC 29199-2, and to the extent that
* such originally developed software module or portions of it are included
* in an ITU/ISO/IEC standard. To the extent that the original contributors
* may own patent rights that would be required to make, use, or sell the
* originally developed software module or portions thereof included in the
* ITU/ISO/IEC standard in a conforming product, the contributors will
* assure ITU/ISO/IEC that they are willing to negotiate licenses under
* reasonable and non-discriminatory terms and conditions with
* applicants throughout the world and in accordance with their patent
* rights declarations made to ITU/ISO/IEC (if any).
*
* Microsoft, any subsequent contributors, and ITU/ISO/IEC additionally
* gives You a free license to this software module or modifications
* thereof for the sole purpose of developing the JPEG XR standard.
*
******** end of section to be removed when the standard is published *****
*
* Microsoft Corporation retains full right to modify and use the code
* for its own purpose, to assign or donate the code to a third party,
* and to inhibit third parties from using the code for products that
* do not conform to the JPEG XR standard as specified by ITU-T T.832 |
* ISO/IEC 29199-2.
*
* This copyright notice must be included in all copies or derivative
* works.
*
* Copyright (c) ITU-T/ISO/IEC 2008, 2009.
***********************************************************************/
#ifdef _MSC_VER
#pragma comment (user,"$Id: io.c,v 1.9 2011-06-10 21:42:17 thor Exp $")
#endif
# include "jxr_priv.h"
# include <assert.h>
void _jxr_rbitstream_initialize(struct rbitstream*str, FILE*fd)
{
str->bits_avail = 0;
str->fd = fd;
str->read_count = 0;
}
size_t _jxr_rbitstream_bitpos(struct rbitstream*str)
{
return str->read_count*8 - str->bits_avail;
}
void _jxr_rbitstream_mark(struct rbitstream*str)
{
assert(str->bits_avail == 0);
str->mark_stream_position = ftell(str->fd);
assert(str->mark_stream_position >= 0);
str->read_count = 0;
}
void _jxr_rbitstream_seek(struct rbitstream*str, uint64_t off)
{
assert(str->bits_avail == 0);
/* NOTE: Should be using fseek64? */
int rc = fseek(str->fd, str->mark_stream_position + (long)off, SEEK_SET);
str->read_count = (size_t) off;
assert(rc >= 0);
}
/*
* Cause the bitstream to consume enough bits that the next bit is on
* a byte boundary. This is for getting alignment, which is sometimes
* needed.
*/
void _jxr_rbitstream_syncbyte(struct rbitstream*str)
{
str->bits_avail = 0;
}
/*
* Get the next 8 bits from the input file and adjust the bitstream
* pointers so that the bits can be read out.
*/
static int get_byte(struct rbitstream*str)
{
int tmp;
assert(str->bits_avail == 0);
tmp = fgetc(str->fd);
if (tmp == EOF)
return EOF;
str->byte = tmp;
str->bits_avail = 8;
str->read_count += 1;
#if 0
DEBUG(" in byte: 0x%02x (bitpos=%zd)\n",
str->byte, str->read_count*8-8);
#endif
return 0;
}
/*
* The following read integers of various width from the data input
* stream. This handles the bit ordering and packing of the bits.
*/
int _jxr_rbitstream_uint1(struct rbitstream*str)
{
if (str->bits_avail == 0) {
get_byte(str);
}
assert(str->bits_avail > 0);
str->bits_avail -= 1;
return (str->byte & (1 << str->bits_avail))? 1 : 0;
}
uint8_t _jxr_rbitstream_uint2(struct rbitstream*str)
{
uint8_t tmp = 0;
tmp |= _jxr_rbitstream_uint1(str);
tmp <<= 1;
tmp |= _jxr_rbitstream_uint1(str);
return tmp;
}
uint8_t _jxr_rbitstream_uint3(struct rbitstream*str)
{
uint8_t tmp = 0;
tmp |= _jxr_rbitstream_uint1(str);
tmp <<= 1;
tmp |= _jxr_rbitstream_uint1(str);
tmp <<= 1;
tmp |= _jxr_rbitstream_uint1(str);
return tmp;
}
uint8_t _jxr_rbitstream_uint4(struct rbitstream*str)
{
uint8_t tmp;
int idx;
if (str->bits_avail == 0)
get_byte(str);
if (str->bits_avail == 4) {
str->bits_avail = 0;
return str->byte & 0x0f;
}
tmp = 0;
for (idx = 0 ; idx < 4 ; idx += 1) {
tmp <<= 1;
tmp |= _jxr_rbitstream_uint1(str);
}
return tmp;
}
uint8_t _jxr_rbitstream_uint6(struct rbitstream*str)
{
uint8_t tmp;
int idx;
tmp = _jxr_rbitstream_uint4(str);
for (idx = 4 ; idx < 6 ; idx += 1) {
tmp <<= 1;
tmp |= _jxr_rbitstream_uint1(str);
}
return tmp;
}
uint8_t _jxr_rbitstream_uint8(struct rbitstream*str)
{
uint8_t tmp;
int idx;
if (str->bits_avail == 0)
get_byte(str);
if (str->bits_avail == 8) {
str->bits_avail = 0;
return str->byte;
}
tmp = 0;
for (idx = 0 ; idx < 8 ; idx += 1) {
tmp <<= 1;
tmp |= _jxr_rbitstream_uint1(str);
}
return tmp;
}
uint16_t _jxr_rbitstream_uint12(struct rbitstream*str)
{
uint16_t tmp = 0;
tmp = _jxr_rbitstream_uint8(str);
tmp <<= 4;
tmp |= _jxr_rbitstream_uint4(str);
return tmp;
}
uint16_t _jxr_rbitstream_uint15(struct rbitstream*str)
{
uint16_t tmp = 0;
tmp = _jxr_rbitstream_uint8(str);
tmp <<= 4;
tmp |= _jxr_rbitstream_uint4(str);
tmp <<= 3;
tmp |= _jxr_rbitstream_uint3(str);
return tmp;
}
uint16_t _jxr_rbitstream_uint16(struct rbitstream*str)
{
uint16_t tmp = 0;
tmp = _jxr_rbitstream_uint8(str);
tmp <<= 8;
tmp |= _jxr_rbitstream_uint8(str);
return tmp;
}
uint32_t _jxr_rbitstream_uint32(struct rbitstream*str)
{
uint32_t tmp = 0;
tmp = _jxr_rbitstream_uint16(str);
tmp <<= 16;
tmp |= _jxr_rbitstream_uint16(str);
return tmp;
}
uint32_t _jxr_rbitstream_uintN(struct rbitstream*str, int N)
{
uint32_t tmp = 0;
assert(N <= 32);
while (N > 0) {
tmp <<= 1;
tmp |= _jxr_rbitstream_uint1(str);
N -= 1;
}
return tmp;
}
int _jxr_rbitstream_intE(struct rbitstream*str, int code_size,
const unsigned char*codeb, const signed char*codev)
{
int bits = 0;
unsigned val = 0;
while (codeb[val << (code_size-bits)] != bits) {
val <<= 1;
val |= _jxr_rbitstream_uint1(str);
bits += 1;
assert(bits <= code_size);
}
return codev[val << (code_size-bits)];
}
int64_t _jxr_rbitstream_intVLW(struct rbitstream*str)
{
uint64_t val = _jxr_rbitstream_uint8(str);
if (val < 0xfb) {
uint64_t tmp = _jxr_rbitstream_uint8(str);
val = val*256 + tmp;
} else if (val == 0xfb) {
val = _jxr_rbitstream_uint32(str);
} else if (val == 0xfc) {
val = _jxr_rbitstream_uint32(str);
uint64_t tmp = _jxr_rbitstream_uint32(str);
val = (val << (uint64_t)32) + tmp;
} else {
return 0;
}
return val;
}
void _jxr_wbitstream_initialize(struct wbitstream*str, FILE*fd)
{
str->byte = 0;
str->bits_ready = 0;
str->fd = fd;
str->write_count = 0;
}
size_t _jxr_wbitstream_bitpos(struct wbitstream*str)
{
return str->write_count*8 + str->bits_ready;
}
static void put_byte(struct wbitstream*str)
{
assert(str->bits_ready == 8);
fputc(str->byte, str->fd);
str->byte = 0;
str->bits_ready = 0;
str->write_count += 1;
}
void _jxr_wbitstream_syncbyte(struct wbitstream*str)
{
if (str->bits_ready > 0)
str->bits_ready = 8;
}
void _jxr_wbitstream_flush(struct wbitstream*str)
{
_jxr_wbitstream_syncbyte(str);
if (str->bits_ready > 0)
put_byte(str);
}
void _jxr_wbitstream_uint1(struct wbitstream*str, int val)
{
if (str->bits_ready == 8)
put_byte(str);
if (val)
str->byte |= 0x80 >> str->bits_ready;
str->bits_ready += 1;
}
void _jxr_wbitstream_uint2(struct wbitstream*str, uint8_t val)
{
int idx;
for (idx = 0 ; idx < 2 ; idx += 1) {
_jxr_wbitstream_uint1(str, val & (0x02 >> idx));
}
}
void _jxr_wbitstream_uint3(struct wbitstream*str, uint8_t val)
{
int idx;
for (idx = 0 ; idx < 3 ; idx += 1) {
_jxr_wbitstream_uint1(str, val & (0x04 >> idx));
}
}
void _jxr_wbitstream_uint4(struct wbitstream*str, uint8_t val)
{
int idx;
for (idx = 0 ; idx < 4 ; idx += 1) {
_jxr_wbitstream_uint1(str, val & (0x08 >> idx));
}
}
void _jxr_wbitstream_uint6(struct wbitstream*str, uint8_t val)
{
int idx;
for (idx = 0 ; idx < 6 ; idx += 1) {
_jxr_wbitstream_uint1(str, val & (0x20 >> idx));
}
}
void _jxr_wbitstream_uint8(struct wbitstream*str, uint8_t val)
{
if (str->bits_ready == 8)
put_byte(str);
if (str->bits_ready == 0) {
str->bits_ready = 8;
str->byte = val;
return;
}
int idx;
for (idx = 0 ; idx < 8 ; idx += 1) {
_jxr_wbitstream_uint1(str, val & (0x80 >> idx));
}
}
void _jxr_wbitstream_uint12(struct wbitstream*str, uint16_t val)
{
int idx;
for (idx = 0 ; idx < 12 ; idx += 1) {
_jxr_wbitstream_uint1(str, val & (0x800 >> idx));
}
}
void _jxr_wbitstream_uint15(struct wbitstream*str, uint16_t val)
{
int idx;
for (idx = 0 ; idx < 15 ; idx += 1) {
_jxr_wbitstream_uint1(str, val & (0x4000 >> idx));
}
}
void _jxr_wbitstream_uint16(struct wbitstream*str, uint16_t val)
{
_jxr_wbitstream_uint8(str, val >> 8);
_jxr_wbitstream_uint8(str, val >> 0);
}
void _jxr_wbitstream_uint32(struct wbitstream*str, uint32_t val)
{
_jxr_wbitstream_uint8(str, val >> 24);
_jxr_wbitstream_uint8(str, val >> 16);
_jxr_wbitstream_uint8(str, val >> 8);
_jxr_wbitstream_uint8(str, val >> 0);
}
void _jxr_wbitstream_uintN(struct wbitstream*str, uint32_t val, int N)
{
assert(N <= 32);
while (N > 0) {
_jxr_wbitstream_uint1(str, 1 & (val >> (N-1)));
N -= 1;
}
}
void _jxr_wbitstream_intVLW(struct wbitstream*str, uint64_t val)
{
if (val == 0) {
_jxr_wbitstream_uint8(str, 0xfe);
} else if (val < 0xfb00) {
_jxr_wbitstream_uint16(str, (uint16_t)val);
} else if (val < 0x100000000ULL) {
_jxr_wbitstream_uint8(str, 0xfb);
_jxr_wbitstream_uint32(str, (uint32_t)val);
} else {
_jxr_wbitstream_uint8(str, 0xfc);
_jxr_wbitstream_uint32(str, val >> 32ULL);
_jxr_wbitstream_uint32(str, val >> 0ULL);
}
}
void _jxr_wbitstream_mark(struct wbitstream*str)
{
if (str->bits_ready == 8)
_jxr_wbitstream_flush(str);
assert(str->bits_ready == 0);
/* str->mark_stream_position = ftell(str->fd); */
str->write_count = 0;
}
const char* _jxr_vlc_index_name(int vlc)
{
switch (vlc) {
case AbsLevelIndDCLum: return "AbsLevelIndDCLum";
case AbsLevelIndDCChr: return "AbsLevelIndDCChr";
case DecFirstIndLPLum: return "DecFirstIndLPLum";
case AbsLevelIndLP0: return "AbsLevelIndLP0";
case AbsLevelIndLP1: return "AbsLevelIndLP1";
case AbsLevelIndHP0: return "AbsLevelIndHP0";
case AbsLevelIndHP1: return "AbsLevelIndHP1";
case DecIndLPLum0: return "DecIndLPLum0";
case DecIndLPLum1: return "DecIndLPLum1";
case DecFirstIndLPChr: return "DecFirstIndLPChr";
case DecIndLPChr0: return "DecIndLPChr0";
case DecIndLPChr1: return "DecIndLPChr1";
case DecNumCBP: return "DecNumCBP";
case DecNumBlkCBP: return "DecNumBlkCBP";
case DecIndHPLum0: return "DecIndHPLum0";
case DecIndHPLum1: return "DecIndHPLum1";
case DecFirstIndHPLum: return "DecFirstIndHPLum";
case DecFirstIndHPChr: return "DecFirstIndHPChr";
case DecIndHPChr0: return "DecIndHPChr0";
case DecIndHPChr1: return "DecIndHPChr1";
default: return "?????";
}
}
/*
* $Log: io.c,v $
* Revision 1.9 2011-06-10 21:42:17 thor
* Removed the timing code again from the main codeline.
*
* Revision 1.7 2011-04-28 08:45:43 thor
* Fixed compiler warnings, ported to gcc 4.4, removed obsolete files.
*
* Revision 1.6 2010-03-31 07:50:58 thor
* Replaced by the latest MS version.
*
* Revision 1.16 2009/05/29 12:00:00 microsoft
* Reference Software v1.6 updates.
*
* Revision 1.15 2009/04/13 12:00:00 microsoft
* Reference Software v1.5 updates.
*
* Revision 1.14 2008/03/05 06:58:10 gus
* *** empty log message ***
*
* Revision 1.13 2008/02/28 18:50:31 steve
* Portability fixes.
*
* Revision 1.12 2008/02/26 23:52:44 steve
* Remove ident for MS compilers.
*
* Revision 1.11 2007/12/07 01:20:34 steve
* Fix adapt not adapting on line ends.
*
* Revision 1.10 2007/12/06 17:53:35 steve
* No longer need bitval dump.
*
* Revision 1.9 2007/11/30 01:50:58 steve
* Compression of DCONLY GRAY.
*
* Revision 1.8 2007/11/26 01:47:15 steve
* Add copyright notices per MS request.
*
* Revision 1.7 2007/11/19 18:22:34 steve
* Skip ESCaped FLEXBITS tiles.
*
* Revision 1.6 2007/11/14 23:56:17 steve
* Fix TILE ordering, using seeks, for FREQUENCY mode.
*
* Revision 1.5 2007/11/08 19:38:38 steve
* Get stub DCONLY compression to work.
*
* Revision 1.4 2007/11/08 02:52:32 steve
* Some progress in some encoding infrastructure.
*
* Revision 1.3 2007/08/15 01:54:11 steve
* Add level2 filter to decoder.
*
* Revision 1.2 2007/06/21 17:31:22 steve
* Successfully parse LP components.
*
* Revision 1.1 2007/06/06 17:19:12 steve
* Introduce to CVS.
*
*/