sequencepoint / J2ME_MPEG

MPEG decoder for J2ME

Clone this repository (size: 438.8 KB): HTTPS / SSH
$ hg clone http://code.seqpoint.com/j2me_mpeg
commit 1: 01984a0b5939
parent 0: 9a1b9408acac
branch: default
tags: tip
Fixed license text
sequencepoint
8 months ago
J2ME_MPEG / Vlc.java
r1:01984a0b5939 733 loc 27.9 KB embed / history / annotate / raw /
  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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/*
 * J2ME_MPEG: MPEG-1 decoder for J2ME
 *
 * Copyright (c) 2009 Sequence Point Software S.L.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */

import java.io.*;

/*
 * Variable-length coding (VLC) is a statistical coding technique that 
 * assigns codewords to values to be encoded. Values of high frequency 
 * of occurrence are assigned short codewords, and those of infrequent 
 * occurrence are assigned long codewords. On average, the more frequent 
 * shorter codewords dominate such that the code string is shorter than 
 * the original data.
 */
public class Vlc {
	public static final short NEXT1    = (short)0xdead;
	public static final short NEXT2    = (short)0xbeef;
	public static final short ESCAPE   = 0x080b;
	public static final short STUFFING = 0x0f0b;

	// Store in pair value, length
	private static final short[] macroblock_address_increment =	{
		NEXT1,   NEXT2, 0x0705, 0x0605, 0x0504, 0x0504, 0x0404, 0x0404,
		0x0303, 0x0303, 0x0303, 0x0303, 0x0203, 0x0203, 0x0203, 0x0203,
		0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
		0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101
	};

	private static final short[] macroblock_address_increment2 = {
		0x0d08, 0x0d08, 0x0d08, 0x0d08, 0x0d08, 0x0d08, 0x0d08, 0x0d08,
		0x0c08, 0x0c08, 0x0c08, 0x0c08, 0x0c08, 0x0c08, 0x0c08, 0x0c08,
		0x0b08, 0x0b08, 0x0b08, 0x0b08, 0x0b08, 0x0b08, 0x0b08, 0x0b08,
		0x0a08, 0x0a08, 0x0a08, 0x0a08, 0x0a08, 0x0a08, 0x0a08, 0x0a08,
		0x0907, 0x0907, 0x0907, 0x0907, 0x0907, 0x0907, 0x0907, 0x0907,
		0x0907, 0x0907, 0x0907, 0x0907, 0x0907, 0x0907, 0x0907, 0x0907,
		0x0807, 0x0807, 0x0807, 0x0807, 0x0807, 0x0807, 0x0807, 0x0807,
		0x0807, 0x0807, 0x0807, 0x0807, 0x0807, 0x0807, 0x0807, 0x0807
	};

	private static final short[] macroblock_address_increment1 = {
		0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
		ESCAPE, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, STUFFING,
		0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
		0x210b, 0x200b, 0x1f0b, 0x1e0b, 0x1d0b, 0x1c0b, 0x1b0b, 0x1a0b,
		0x190b, 0x180b, 0x170b, 0x160b, 0x150a, 0x150a, 0x140a, 0x140a,
		0x130a, 0x130a, 0x120a, 0x120a, 0x110a, 0x110a, 0x100a, 0x100a,
		0x0f08, 0x0f08, 0x0f08, 0x0f08, 0x0f08, 0x0f08, 0x0f08, 0x0f08,
		0x0e08, 0x0e08, 0x0e08, 0x0e08, 0x0e08, 0x0e08, 0x0e08, 0x0e08
	};

	public int getMacroblockAddressIncrement(InputBitStream input) throws IOException {
		int index = input.nextBits(11);
		int value = macroblock_address_increment[index >> 6];

		if (value == NEXT1)
			value = macroblock_address_increment1[index & 0x3f];
		else if (value == NEXT2)
			value = macroblock_address_increment2[index & 0x3f];

		input.getBits(value & 0xff);

		return value >> 8;
	}


	private static final byte[] macroblock_type_i = {
        0x00, 0x12, 0x01, 0x01
    };

	private static final short[] macroblock_type_p = {
    	0x0000, 0x1106, 0x1205, 0x1205, 0x1a05, 0x1a05, 0x0105, 0x0105,
    	0x0803, 0x0803, 0x0803, 0x0803, 0x0803, 0x0803, 0x0803, 0x0803,
    	0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202,
    	0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202
    };

	private static final short[] macroblock_type_b = {
    	0x0000, 0x1106, 0x1606, 0x1a06, 0x1e05, 0x1e05, 0x0105, 0x0105,
    	0x0804, 0x0804, 0x0804, 0x0804, 0x0a04, 0x0a04, 0x0a04, 0x0a04,
    	0x0403, 0x0403, 0x0403, 0x0403, 0x0403, 0x0403, 0x0403, 0x0403,
    	0x0603, 0x0603, 0x0603, 0x0603, 0x0603, 0x0603, 0x0603, 0x0603
    };

    public class MacroblockType {
    	public boolean mMacroblockQuant;
    	public boolean mMacroblockMotionForward;
    	public boolean mMacroblockMotionBackward;
    	public boolean mMacroblockPattern;
    	public boolean mMacroblockIntra;
    }

	public void getMacroblockType(int pictureType, InputBitStream input, MacroblockType macroblockType) throws IOException {
		int index  = 0;
		int value  = 0;
		int length = 0;

		switch (pictureType) {
			case Picture.I_TYPE:
				index  = input.nextBits(2);
				length = macroblock_type_i[index] & 0x0f;

				macroblockType.mMacroblockQuant 		 = (macroblock_type_i[index] >> 4) != 0;
				macroblockType.mMacroblockMotionForward  = false;
				macroblockType.mMacroblockMotionBackward = false;
				macroblockType.mMacroblockPattern 		 = false;
				macroblockType.mMacroblockIntra 		 = true;

				input.getBits(length);
				break;

			case Picture.P_TYPE:
				index = input.nextBits(6);

				// Handle special case: highest bit is 1
				value  = index < 0x20? macroblock_type_p[index] >> 8 : 0x0a;
				length = index < 0x20? macroblock_type_p[index] & 0xff : 1;

				macroblockType.mMacroblockQuant 		 = (value & 0x10) != 0;
				macroblockType.mMacroblockMotionForward  = (value & 0x08) != 0;
				macroblockType.mMacroblockMotionBackward = (value & 0x04) != 0;
				macroblockType.mMacroblockPattern 		 = (value & 0x02) != 0;
				macroblockType.mMacroblockIntra 		 = (value & 0x01) != 0;

				input.getBits(length);
				break;

			case Picture.B_TYPE:
				index = input.nextBits(6);

				// Handle 2 special cases: highest bit 1
				value  = index < 0x20? macroblock_type_b[index] >> 8 :	index < 0x30? 0x0c : 0x0e;
				length = index < 0x20? macroblock_type_b[index] & 0xff : 2;

				macroblockType.mMacroblockQuant 		 = (value & 0x10) != 0;
				macroblockType.mMacroblockMotionForward  = (value & 0x08) != 0;
				macroblockType.mMacroblockMotionBackward = (value & 0x04) != 0;
				macroblockType.mMacroblockPattern 		 = (value & 0x02) != 0;
				macroblockType.mMacroblockIntra 		 = (value & 0x01) != 0;

				input.getBits(length);
				break;

			case Picture.D_TYPE:
				break;
		}
	}


	private static final short[] motion_vector = {
		0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
		0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
		0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
		0x0000100b, 0xfffff00b, 0x00000f0b, 0xfffff10b, 0x00000e0b, 0xfffff20b, 0x00000d0b, 0xfffff30b,
		0x00000c0b, 0xfffff40b, 0x00000b0b, 0xfffff50b, 0x00000a0a, 0x00000a0a, 0xfffff60a, 0xfffff60a,
		0x0000090a, 0x0000090a, 0xfffff70a, 0xfffff70a, 0x0000080a, 0x0000080a, 0xfffff80a, 0xfffff80a,
		0x00000708, 0x00000708, 0x00000708, 0x00000708, 0x00000708, 0x00000708, 0x00000708, 0x00000708,
		0xfffff908, 0xfffff908, 0xfffff908, 0xfffff908, 0xfffff908, 0xfffff908, 0xfffff908, 0xfffff908,
		0x00000608, 0x00000608, 0x00000608, 0x00000608, 0x00000608, 0x00000608, 0x00000608, 0x00000608,
		0xfffffa08, 0xfffffa08, 0xfffffa08, 0xfffffa08, 0xfffffa08, 0xfffffa08, 0xfffffa08, 0xfffffa08,
		0x00000508, 0x00000508, 0x00000508, 0x00000508, 0x00000508, 0x00000508, 0x00000508, 0x00000508,
		0xfffffb08, 0xfffffb08, 0xfffffb08, 0xfffffb08, 0xfffffb08, 0xfffffb08, 0xfffffb08, 0xfffffb08,
		0x00000407, 0x00000407, 0x00000407, 0x00000407, 0x00000407, 0x00000407, 0x00000407, 0x00000407,
		0x00000407, 0x00000407, 0x00000407, 0x00000407, 0x00000407, 0x00000407, 0x00000407, 0x00000407,
		0xfffffc07, 0xfffffc07, 0xfffffc07, 0xfffffc07, 0xfffffc07, 0xfffffc07, 0xfffffc07, 0xfffffc07,
		0xfffffc07, 0xfffffc07, 0xfffffc07, 0xfffffc07, 0xfffffc07, 0xfffffc07, 0xfffffc07, 0xfffffc07,
	};

	private static final short[] motion_vector1 = {
		0x00000000, 0x00000000, 0x00000305, 0xfffffd05, 0x00000204, 0x00000204, 0xfffffe04, 0xfffffe04,
		0x00000103, 0x00000103, 0x00000103, 0x00000103, 0xffffff03, 0xffffff03, 0xffffff03, 0xffffff03,
		0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001,
		0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001
	};

	public int getMotionVector(InputBitStream input) throws IOException	{
		int index  = input.nextBits(11);
		int value  = 0;
		int length = 0;

		if (((index >> 7) & 0xf) == 0x0) {
			value  = motion_vector[index] >> 0x8;
			length = motion_vector[index] & 0xff;
		}
		else {
			index >>= 6;

			value  = motion_vector1[index] >> 0x8;
			length = motion_vector1[index] & 0xff;
		}

		input.getBits(length);

		return value;
	}


	private static final short[] coded_block_pattern = {
		0x0000, 0x0000, 0x2709, 0x1b09, 0x3b09, 0x3709, 0x2f09, 0x1f09,
		0x3a08, 0x3a08, 0x3608, 0x3608, 0x2e08, 0x2e08, 0x1e08, 0x1e08,
		0x3908, 0x3908, 0x3508, 0x3508, 0x2d08, 0x2d08, 0x1d08, 0x1d08,
		0x2608, 0x2608, 0x1a08, 0x1a08, 0x2508, 0x2508, 0x1908, 0x1908,

		0x2b08, 0x2b08, 0x1708, 0x1708, 0x3308, 0x3308, 0x0f08, 0x0f08,
		0x2a08, 0x2a08, 0x1608, 0x1608, 0x3208, 0x3208, 0x0e08, 0x0e08,
		0x2908, 0x2908, 0x1508, 0x1508, 0x3108, 0x3108, 0x0d08, 0x0d08,
		0x2308, 0x2308, 0x1308, 0x1308, 0x0b08, 0x0b08, 0x0708, 0x0708,

		0x2207, 0x2207, 0x2207, 0x2207, 0x1207, 0x1207, 0x1207, 0x1207,
		0x0a07, 0x0a07, 0x0a07, 0x0a07, 0x0607, 0x0607, 0x0607, 0x0607,
		0x2107, 0x2107, 0x2107, 0x2107, 0x1107, 0x1107, 0x1107, 0x1107,
		0x0907, 0x0907, 0x0907, 0x0907, 0x0507, 0x0507, 0x0507, 0x0507,

		0x3f06, 0x3f06, 0x3f06, 0x3f06, 0x3f06, 0x3f06, 0x3f06, 0x3f06,
		0x0306, 0x0306, 0x0306, 0x0306, 0x0306, 0x0306, 0x0306, 0x0306,
		0x2406, 0x2406, 0x2406, 0x2406, 0x2406, 0x2406, 0x2406, 0x2406,
		0x1806, 0x1806, 0x1806, 0x1806, 0x1806, 0x1806, 0x1806, 0x1806,

		0x3e05, 0x3e05, 0x3e05, 0x3e05, 0x3e05, 0x3e05, 0x3e05, 0x3e05,
		0x3e05, 0x3e05, 0x3e05, 0x3e05, 0x3e05, 0x3e05, 0x3e05, 0x3e05,
		0x0205, 0x0205, 0x0205, 0x0205, 0x0205, 0x0205, 0x0205, 0x0205,
		0x0205, 0x0205, 0x0205, 0x0205, 0x0205, 0x0205, 0x0205, 0x0205,

		0x3d05, 0x3d05, 0x3d05, 0x3d05, 0x3d05, 0x3d05, 0x3d05, 0x3d05,
		0x3d05, 0x3d05, 0x3d05, 0x3d05, 0x3d05, 0x3d05, 0x3d05, 0x3d05,
		0x0105, 0x0105, 0x0105, 0x0105, 0x0105, 0x0105, 0x0105, 0x0105,
		0x0105, 0x0105, 0x0105, 0x0105, 0x0105, 0x0105, 0x0105, 0x0105,

		0x3805, 0x3805, 0x3805, 0x3805, 0x3805, 0x3805, 0x3805, 0x3805,
		0x3805,	0x3805, 0x3805, 0x3805, 0x3805, 0x3805, 0x3805, 0x3805,
		0x3405, 0x3405, 0x3405, 0x3405, 0x3405, 0x3405, 0x3405, 0x3405,
		0x3405, 0x3405, 0x3405, 0x3405, 0x3405, 0x3405, 0x3405, 0x3405,

		0x2c05, 0x2c05, 0x2c05, 0x2c05, 0x2c05, 0x2c05, 0x2c05, 0x2c05,
		0x2c05, 0x2c05, 0x2c05, 0x2c05, 0x2c05, 0x2c05, 0x2c05, 0x2c05,
		0x1c05, 0x1c05, 0x1c05, 0x1c05, 0x1c05, 0x1c05, 0x1c05, 0x1c05,
		0x1c05, 0x1c05, 0x1c05, 0x1c05, 0x1c05, 0x1c05, 0x1c05, 0x1c05,

		0x2805, 0x2805, 0x2805, 0x2805, 0x2805, 0x2805, 0x2805, 0x2805,
		0x2805, 0x2805, 0x2805, 0x2805, 0x2805, 0x2805, 0x2805, 0x2805,
		0x1405, 0x1405, 0x1405, 0x1405, 0x1405, 0x1405, 0x1405, 0x1405,
		0x1405, 0x1405, 0x1405, 0x1405, 0x1405, 0x1405, 0x1405, 0x1405,

		0x3005, 0x3005, 0x3005, 0x3005, 0x3005, 0x3005, 0x3005, 0x3005,
		0x3005, 0x3005, 0x3005, 0x3005, 0x3005, 0x3005, 0x3005, 0x3005,
		0x0c05, 0x0c05, 0x0c05, 0x0c05, 0x0c05, 0x0c05, 0x0c05, 0x0c05,
		0x0c05, 0x0c05, 0x0c05, 0x0c05, 0x0c05, 0x0c05, 0x0c05, 0x0c05,

		0x2004, 0x2004, 0x2004, 0x2004, 0x2004, 0x2004, 0x2004, 0x2004,
		0x2004,	0x2004, 0x2004, 0x2004, 0x2004, 0x2004, 0x2004, 0x2004,
		0x2004, 0x2004, 0x2004, 0x2004, 0x2004, 0x2004, 0x2004, 0x2004,
		0x2004, 0x2004, 0x2004, 0x2004, 0x2004, 0x2004, 0x2004, 0x2004,

		0x1004, 0x1004, 0x1004, 0x1004, 0x1004, 0x1004, 0x1004, 0x1004,
		0x1004, 0x1004, 0x1004, 0x1004, 0x1004, 0x1004, 0x1004, 0x1004,
		0x1004, 0x1004, 0x1004, 0x1004, 0x1004, 0x1004, 0x1004, 0x1004,
		0x1004, 0x1004, 0x1004, 0x1004, 0x1004, 0x1004, 0x1004, 0x1004,

		0x0804, 0x0804, 0x0804, 0x0804, 0x0804, 0x0804, 0x0804, 0x0804,
		0x0804, 0x0804, 0x0804, 0x0804, 0x0804, 0x0804, 0x0804, 0x0804,
		0x0804, 0x0804, 0x0804, 0x0804, 0x0804, 0x0804, 0x0804, 0x0804,
		0x0804, 0x0804, 0x0804, 0x0804, 0x0804, 0x0804, 0x0804, 0x0804,

		0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404,
		0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404,
		0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404,
		0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404,

		0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03,
		0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03,
		0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03,
		0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03,

		0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03,
		0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03,
		0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03,
		0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03, 0x3c03
	};

	public int getCodedBlockPattern(InputBitStream input) throws IOException {
		int index = input.nextBits(9);

		int value  = coded_block_pattern[index] >> 0x8;
		int length = coded_block_pattern[index] & 0xff;

		input.getBits(length);

		return value;
	}


	/* Decoding tables for dct_dc_size_luminance */

	private static final short[] dct_dc_size_luminance = {
		0x12, 0x12, 0x12, 0x12, 0x22, 0x22, 0x22, 0x22,
		0x03, 0x03, 0x33, 0x33, 0x43, 0x43, 0x54, 0x00
	};

	private static final short[] dct_dc_size_luminance1 = {
		0x65, 0x65, 0x65, 0x65, 0x76, 0x76, 0x87, 0x00
	}; 

	public int decodeDCTDCSizeLuminance(InputBitStream input) throws IOException {
		int index = input.nextBits(7);
		int value = dct_dc_size_luminance[index >> 3];

		if (value == 0)
			value = dct_dc_size_luminance1[index & 0x07];

		input.getBits(value & 0xf);

		return value >> 4;
	}

	/* Decoding tables for dct_dc_size_chrominance */

	private static final short[] dct_dc_size_chrominance = {
		0x02, 0x02, 0x02, 0x02, 0x12, 0x12, 0x12, 0x12,
		0x22, 0x22, 0x22, 0x22, 0x33, 0x33, 0x44, 0x00
	};

	private static final short[] dct_dc_size_chrominance1 = {
		0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
		0x66, 0x66, 0x66, 0x66, 0x77, 0x77, 0x88, 0x00
	};

	public int decodeDCTDCSizeChrominance(InputBitStream input) throws IOException {
		int index = input.nextBits(8);
		int value = dct_dc_size_chrominance[index >> 4];

		if (value == 0)
			value = dct_dc_size_chrominance1[index & 0xf];

		input.getBits(value & 0xf);

		return value >> 4;
	}


	/* Decoding tables for dct_coeff_first & dct_coeff_next
	 *
	 * This were originally 15 arrays. Now they are merged to form a single array,
	 * and using a scale to retrieve data from the correct array of coefficients.
	 */ 

	// Total number of entries 16 * 32 = 512
	// First 32 entries are not used in fact

	private static final byte[][] dct_coeff = {
		// 0000 0000 0000 xxxx x
		{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
		{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
		{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
		{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
		{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
		{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
		{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
		{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},

		// 0000 0000 0001 xxxx s
		{1, 18, 17}, {1, -18, 17}, {1, 17, 17}, {1, -17, 17}, 
		{1, 16, 17}, {1, -16, 17}, {1, 15, 17}, {1, -15, 17},
		{6,  3, 17}, {6,  -3, 17}, {16, 2, 17}, {16, -2, 17}, 
		{15, 2, 17}, {15, -2, 17}, {14, 2, 17}, {14, -2, 17},
		{13, 2, 17}, {13, -2, 17}, {12, 2, 17}, {12, -2, 17},
		{11, 2, 17}, {11, -2, 17}, {31, 1, 17}, {31, -1, 17},
		{30, 1, 17}, {30, -1, 17}, {29, 1, 17}, {29, -1, 17}, 
		{28, 1, 17}, {28, -1, 17}, {27, 1, 17}, {27, -1, 17},

		// 0000 0000 0010 xxxs x
		{0, 40, 16}, {0, 40, 16}, {0, -40, 16}, {0, -40, 16}, 
		{0, 39, 16}, {0, 39, 16}, {0, -39, 16}, {0, -39, 16}, 
		{0, 38, 16}, {0, 38, 16}, {0, -38, 16}, {0, -38, 16}, 
		{0, 37, 16}, {0, 37, 16}, {0, -37, 16}, {0, -37, 16},
		{0, 36, 16}, {0, 36, 16}, {0, -36, 16}, {0, -36, 16}, 
		{0, 35, 16}, {0, 35, 16}, {0, -35, 16}, {0, -35, 16},
		{0, 34, 16}, {0, 34, 16}, {0, -34, 16}, {0, -34, 16}, 
		{0, 33, 16}, {0, 33, 16}, {0, -33, 16}, {0, -33, 16},

		// 0000 0000 0011 xxxs x
		{0, 32, 16}, {0, 32, 16}, {0, -32, 16}, {0, -32, 16}, 
		{1, 14, 16}, {1, 14, 16}, {1, -14, 16}, {1, -14, 16}, 
		{1, 13, 16}, {1, 13, 16}, {1, -13, 16}, {1, -13, 16},
		{1, 12, 16}, {1, 12, 16}, {1, -12, 16}, {1, -12, 16}, 
		{1, 11, 16}, {1, 11, 16}, {1, -11, 16}, {1, -11, 16}, 
		{1, 10, 16}, {1, 10, 16}, {1, -10, 16}, {1, -10, 16}, 
		{1,  9, 16}, {1,  9, 16}, {1,  -9, 16}, {1,  -9, 16}, 
		{1,  8, 16}, {1,  8, 16}, {1,  -8, 16}, {1,  -8, 16},

		// 0000 0000 0100 xxsx x
		{0,  31, 15}, {0,  31, 15}, {0,  31, 15}, {0,  31, 15},
		{0, -31, 15}, {0, -31, 15}, {0, -31, 15}, {0, -31, 15}, 
		{0,  30, 15}, {0,  30, 15}, {0,  30, 15}, {0,  30, 15},
		{0, -30, 15}, {0, -30, 15}, {0, -30, 15}, {0, -30, 15}, 
		{0,  29, 15}, {0,  29, 15}, {0,  29, 15}, {0,  29, 15}, 
		{0, -29, 15}, {0, -29, 15}, {0, -29, 15}, {0, -29, 15}, 
		{0,  28, 15}, {0,  28, 15}, {0,  28, 15}, {0,  28, 15},
		{0, -28, 15}, {0, -28, 15}, {0, -28, 15}, {0, -28, 15},

		// 0000 0000 0101 xxsx x
		{0,  27, 15}, {0,  27, 15}, {0,  27, 15}, {0,  27, 15}, 
    	{0, -27, 15}, {0, -27, 15}, {0, -27, 15}, {0, -27, 15},
    	{0,  26, 15}, {0,  26, 15}, {0,  26, 15}, {0,  26, 15},
    	{0, -26, 15}, {0, -26, 15}, {0, -26, 15}, {0, -26, 15},
    	{0,  25, 15}, {0,  25, 15}, {0,  25, 15}, {0,  25, 15}, 
    	{0, -25, 15}, {0, -25, 15}, {0, -25, 15}, {0, -25, 15},
    	{0,  24, 15}, {0,  24, 15}, {0,  24, 15}, {0,  24, 15}, 
    	{0, -24, 15}, {0, -24, 15}, {0, -24, 15}, {0, -24, 15},

    	// 0000 0000 0110 xxsx x
    	{0,  23, 15}, {0,  23, 15}, {0,  23, 15}, {0,  23, 15}, 
    	{0, -23, 15}, {0, -23, 15}, {0, -23, 15}, {0, -23, 15}, 
    	{0,  22, 15}, {0,  22, 15}, {0,  22, 15}, {0,  22, 15}, 
    	{0, -22, 15}, {0, -22, 15}, {0, -22, 15}, {0, -22, 15},
    	{0,  21, 15}, {0,  21, 15}, {0,  21, 15}, {0,  21, 15}, 
    	{0, -21, 15}, {0, -21, 15}, {0, -21, 15}, {0, -21, 15},
    	{0,  20, 15}, {0,  20, 15}, {0,  20, 15}, {0,  20, 15}, 
    	{0, -20, 15}, {0, -20, 15}, {0, -20, 15}, {0, -20, 15},

    	// 0000 0000 0111 xxsx x
    	{0,  19, 15}, {0,  19, 15}, {0,  19, 15}, {0,  19, 15}, 
    	{0, -19, 15}, {0, -19, 15}, {0, -19, 15}, {0, -19, 15}, 
    	{0,  18, 15}, {0,  18, 15}, {0,  18, 15}, {0,  18, 15}, 
    	{0, -18, 15}, {0, -18, 15}, {0, -18, 15}, {0, -18, 15},
    	{0,  17, 15}, {0,  17, 15}, {0,  17, 15}, {0,  17, 15}, 
    	{0, -17, 15}, {0, -17, 15}, {0, -17, 15}, {0, -17, 15}, 
    	{0,  16, 15}, {0,  16, 15}, {0,  16, 15}, {0,  16, 15}, 
    	{0, -16, 15}, {0, -16, 15}, {0, -16, 15}, {0, -16, 15},

    	// 0000 0000 1000 xsxx x    
    	{10,  2, 14}, {10,  2, 14}, {10,  2, 14}, {10,  2, 14},
    	{10,  2, 14}, {10,  2, 14}, {10,  2, 14}, {10,  2, 14},
    	{10, -2, 14}, {10, -2, 14}, {10, -2, 14}, {10, -2, 14},
    	{10, -2, 14}, {10, -2, 14}, {10, -2, 14}, {10, -2, 14},
    	{ 9,  2, 14}, { 9,  2, 14}, { 9,  2, 14}, { 9,  2, 14}, 
    	{ 9,  2, 14}, { 9,  2, 14}, { 9,  2, 14}, { 9,  2, 14}, 
    	{ 9, -2, 14}, { 9, -2, 14}, { 9, -2, 14}, { 9, -2, 14},
    	{ 9, -2, 14}, { 9, -2, 14}, { 9, -2, 14}, { 9, -2, 14},

    	// 0000 0000 1001 xsxx x    
    	{5,  3, 14}, {5,  3, 14}, {5,  3, 14}, {5,  3, 14},
    	{5,  3, 14}, {5,  3, 14}, {5,  3, 14}, {5,  3, 14},
    	{5, -3, 14}, {5, -3, 14}, {5, -3, 14}, {5, -3, 14},
    	{5, -3, 14}, {5, -3, 14}, {5, -3, 14}, {5, -3, 14},
    	{3,  4, 14}, {3,  4, 14}, {3,  4, 14}, {3,  4, 14}, 
    	{3,  4, 14}, {3,  4, 14}, {3,  4, 14}, {3,  4, 14}, 
    	{3, -4, 14}, {3, -4, 14}, {3, -4, 14}, {3, -4, 14},
    	{3, -4, 14}, {3, -4, 14}, {3, -4, 14}, {3, -4, 14},

    	// 0000 0000 1010 xsxx x
    	{2,  5, 14}, {2,  5, 14}, {2,  5, 14}, {2,  5, 14},
    	{2,  5, 14}, {2,  5, 14}, {2,  5, 14}, {2,  5, 14},
    	{2, -5, 14}, {2, -5, 14}, {2, -5, 14}, {2, -5, 14},
    	{2, -5, 14}, {2, -5, 14}, {2, -5, 14}, {2, -5, 14},
    	{1,  7, 14}, {1,  7, 14}, {1,  7, 14}, {1,  7, 14},
    	{1,  7, 14}, {1,  7, 14}, {1,  7, 14}, {1,  7, 14},
    	{1, -7, 14}, {1, -7, 14}, {1, -7, 14}, {1, -7, 14},
    	{1, -7, 14}, {1, -7, 14}, {1, -7, 14}, {1, -7, 14},
    
    	// 0000 0000 1011 xsxx x
    	{1,   6, 14}, {1,   6, 14}, {1,   6, 14}, {1,   6, 14},
    	{1,   6, 14}, {1,   6, 14}, {1,   6, 14}, {1,   6, 14},
    	{1,  -6, 14}, {1,  -6, 14}, {1,  -6, 14}, {1,  -6, 14},
    	{1,  -6, 14}, {1,  -6, 14}, {1,  -6, 14}, {1,  -6, 14},
    	{0,  15, 14}, {0,  15, 14}, {0,  15, 14}, {0,  15, 14},
    	{0,  15, 14}, {0,  15, 14}, {0,  15, 14}, {0,  15, 14},
    	{0, -15, 14}, {0, -15, 14}, {0, -15, 14}, {0, -15, 14},
    	{0, -15, 14}, {0, -15, 14}, {0, -15, 14}, {0, -15, 14},

    	// 0000 0000 1100 xsxx x
    	{0,  14, 14}, {0,  14, 14}, {0,  14, 14}, {0,  14, 14}, 
    	{0,  14, 14}, {0,  14, 14}, {0,  14, 14}, {0,  14, 14}, 
    	{0, -14, 14}, {0, -14, 14}, {0, -14, 14}, {0, -14, 14}, 
    	{0, -14, 14}, {0, -14, 14}, {0, -14, 14}, {0, -14, 14},
    	{0,  13, 14}, {0,  13, 14}, {0,  13, 14}, {0,  13, 14}, 
    	{0,  13, 14}, {0,  13, 14}, {0,  13, 14}, {0,  13, 14},
    	{0, -13, 14}, {0, -13, 14}, {0, -13, 14}, {0, -13, 14}, 
    	{0, -13, 14}, {0, -13, 14}, {0, -13, 14}, {0, -13, 14},

    	// 0000 0000 1101 xsxx x
    	{0,  12, 14}, {0,  12, 14}, {0,  12, 14}, {0,  12, 14},
    	{0,  12, 14}, {0,  12, 14}, {0,  12, 14}, {0,  12, 14}, 
    	{0, -12, 14}, {0, -12, 14}, {0, -12, 14}, {0, -12, 14}, 
    	{0, -12, 14}, {0, -12, 14}, {0, -12, 14}, {0, -12, 14},
    	{26,  1, 14}, {26,  1, 14}, {26,  1, 14}, {26,  1, 14}, 
    	{26,  1, 14}, {26,  1, 14}, {26,  1, 14}, {26,  1, 14}, 
    	{26, -1, 14}, {26, -1, 14}, {26, -1, 14}, {26, -1, 14},
    	{26, -1, 14}, {26, -1, 14}, {26, -1, 14}, {26, -1, 14},

    	// 0000 0000 1110 xsxx x
    	{25,  1, 14}, {25,  1, 14}, {25,  1, 14}, {25,  1, 14}, 
    	{25,  1, 14}, {25,  1, 14}, {25,  1, 14}, {25,  1, 14}, 
    	{25, -1, 14}, {25, -1, 14}, {25, -1, 14}, {25, -1, 14},
    	{25, -1, 14}, {25, -1, 14}, {25, -1, 14}, {25, -1, 14}, 
    	{24,  1, 14}, {24,  1, 14}, {24,  1, 14}, {24,  1, 14},
    	{24,  1, 14}, {24,  1, 14}, {24,  1, 14}, {24,  1, 14}, 
    	{24, -1, 14}, {24, -1, 14}, {24, -1, 14}, {24, -1, 14}, 
    	{24, -1, 14}, {24, -1, 14}, {24, -1, 14}, {24, -1, 14},

    	// 0000 0000 1111 xsxx x
    	{23,  1, 14}, {23,  1, 14}, {23,  1, 14}, {23,  1, 14}, 
    	{23,  1, 14}, {23,  1, 14}, {23,  1, 14}, {23,  1, 14}, 
    	{23, -1, 14}, {23, -1, 14}, {23, -1, 14}, {23, -1, 14}, 
    	{23, -1, 14}, {23, -1, 14}, {23, -1, 14}, {23, -1, 14}, 
    	{22,  1, 14}, {22,  1, 14}, {22,  1, 14}, {22,  1, 14},
    	{22,  1, 14}, {22,  1, 14}, {22,  1, 14}, {22,  1, 14}, 
    	{22, -1, 14}, {22, -1, 14}, {22, -1, 14}, {22, -1, 14}, 
    	{22, -1, 14}, {22, -1, 14}, {22, -1, 14}, {22, -1, 14}
	};

	// 0000 0001 xxxx s??? ?
	private static final byte[][] dct_coeff1 = {
		{0, 11, 13}, {0, -11, 13}, {8,  2, 13}, {8,  -2, 13},
		{4,  3, 13}, {4,  -3, 13}, {0, 10, 13}, {0, -10, 13},
		{2,  4, 13}, {2,  -4, 13}, {7,  2, 13}, {7,  -2, 13},
		{21, 1, 13}, {21, -1, 13}, {20, 1, 13}, {20, -1, 13},
		{0,  9, 13}, {0,  -9, 13}, {19, 1, 13}, {19, -1, 13},
		{18, 1, 13}, {18, -1, 13}, {1,  5, 13}, {1,  -5, 13},
		{3,  3, 13}, {3,  -3, 13}, {0,  8, 13}, {0,  -8, 13},
		{6,  2, 13}, {6,  -2, 13}, {17, 1, 13}, {17, -1, 13}
	};

	// 0000 0010 xxs? ???? ?
	private static final byte[][] dct_coeff2 = {
		{16, 1, 11}, {16, -1, 11}, {5, 2, 11}, {5, -2, 11},
		{ 0, 7, 11}, { 0, -7, 11}, {2, 3, 11}, {2, -3, 11}
	};

	// 0000 0011 xxs? ???? ?
	private static final byte[][] dct_coeff3 = {
		{1 , 4, 11}, {1 , -4, 11}, {15, 1, 11}, {15, -1, 11},
		{14, 1, 11}, {14, -1, 11}, {4 , 2, 11}, {4 , -2, 11}
	};

	// 0000 xxxs ???? ???? ?
	private static final byte[][] dct_coeff4 = {
		{0, 0, 0}, {0,  0, 0}, {0, 0, 0}, {0,  0, 0},
		{0, 0, 6}, {0,  0, 6}, {0, 0, 6}, {0,  0, 6},     // ESCAPE
		{2, 2, 8}, {2, -2, 8}, {9, 1, 8}, {9, -1, 8},
		{0, 4, 8}, {0, -4, 8}, {8, 1, 8}, {8, -1, 8}
	};

	public class RunLevel {
		int run;
		int level;
	};

	public boolean decodeDCTCoeff(InputBitStream input, boolean first, RunLevel runLevel) throws IOException {
		int value = input.nextBits(17);
		int index = (value >> 5) & 0xfff;

		int run = 0;
		int	level = 0;

		int discard = 0;

		boolean escape = false;

		if (index >= 0x1 && index <= 0xf) {
			int offset = (index << 5);    // Multiply by 32, the size of each original array
			offset += (value & 0x1f);     // Get 5 least significant bits, which determine the offset within each array

			run   = dct_coeff[offset][0];
			level = dct_coeff[offset][1];

			discard = dct_coeff[offset][2];
		}
		else if (index >= 0x10 && index <= 0x1f) {
			// Discard 4 least significant bits
			int offset = (value >> 4) & 0x1f;

			run   = dct_coeff1[offset][0];
			level = dct_coeff1[offset][1];

			discard = dct_coeff1[offset][2];
		}
		else if (index >= 0x20 && index <= 0x2f) {
			// Discard 6 least significant bits
			int offset = (value >> 6) & 0x7;

			run   = dct_coeff2[offset][0];
			level = dct_coeff2[offset][1];

			discard = dct_coeff2[offset][2];
		}
		else if (index >= 0x30 && index <= 0x3f) {
			// Discard 6 least significant bits
			int offset = (value >> 6) & 0x7;
    
			run   = dct_coeff3[offset][0];
			level = dct_coeff3[offset][1];

			discard = dct_coeff3[offset][2];
		}
		else if (index >= 0x40 && index <= 0xff) {
			// Discard 9 least significant bits
			int offset = (value >> 9) & 0xf;

			if (offset >= 4 && offset <= 7)
				escape = true;

			run   = dct_coeff4[offset][0];
			level = dct_coeff4[offset][1];

			discard = dct_coeff4[offset][2];
		}
		else {
			index = (value >> 13) & 0xf;

			if (index == 0x1) {
				final byte[][] dct_coeff = {
					{7, 1, 7}, {7, -1, 7}, {6, 1, 7}, {6, -1, 7},
					{1, 2, 7}, {1, -2, 7}, {5, 1, 7}, {5, -1, 7}
				};

				int offset = (value >> 10) & 0x7;

				run   = dct_coeff[offset][0];
				level = dct_coeff[offset][1];

				discard = dct_coeff[offset][2];
			}
			else if (index == 0x2) {
				final byte[][] dct_coeff = {
					{13, 1, 9}, {13, -1, 9}, {0, 6, 9}, {0, -6, 9}, {12, 1, 9}, {12, -1, 9}, {11, 1, 9}, {11, -1, 9},
					{ 3, 2, 9}, { 3, -2, 9}, {1, 3, 9}, {1, -3, 9}, { 0, 5, 9}, { 0, -5, 9}, {10, 1, 9}, {10, -1, 9},
					{ 0,  3, 6}, { 0,  3, 6}, {0,  3, 6}, {0,  3, 6}, { 0,  3, 6}, { 0,  3, 6}, { 0,  3, 6}, { 0,  3, 6},
					{ 0, -3, 6}, { 0, -3, 6}, {0, -3, 6}, {0, -3, 6}, { 0, -3, 6}, { 0, -3, 6}, { 0, -3, 6}, { 0, -3, 6},
				};

				int offset = (value >> 8) & 0x1f;

				run   = dct_coeff[offset][0];
				level = dct_coeff[offset][1];

				discard = dct_coeff[offset][2];
			}
			else if (index == 0x3) {
				final byte[][] dct_coeff = {
					{4, 1, 6}, {4, -1, 6}, {3, 1, 6}, {3, -1, 6}
				};

				int offset = (value >> 11) & 0x3;

				run   = dct_coeff[offset][0];
				level = dct_coeff[offset][1];

				discard = dct_coeff[offset][2];
			}
			else if (index == 0x4) {
				run   = 0;
				level = ((value & 0x1000) != 0? -2 : 2);

				discard = 5;
			}
			else if (index == 0x5) {
				run   = 2;
				level = ((value & 0x1000) != 0? -1 : 1);

				discard = 5;
			}
			else if (index == 0x6) {
				run   = 1;
				level = 1;

				discard = 4;
			}
			else if (index == 0x7) {
				run   = 1;
				level = -1;

				discard = 4;
			}
			// Only for CoeffFirst
			else if (first && index >= 0x8 && index <= 0xb) {
				run   = 0;
				level = 1;

				discard = 2;
			}
			else if (first && index >= 0xc && index <= 0xf)	{
				run   = 0;
				level = -1;

				discard = 2;
			}
			// Only for CoeffNext
			else if (index >= 0xc && index <= 0xd) {
				run   = 0;
				level = 1;

				discard = 3;
			}
			else if (index >= 0xe && index <= 0xf) {
				run   = 0;
				level = -1;

				discard = 3;
			}
			else {
				return false;
			}
		}

		input.getBits(discard);

		if (escape) {
			int r = input.getBits(6);
			run   = r;

			int l = input.nextBits(8);
			if (l == 0 || l == 0x80) {
				l = input.getSignedBits(16);
			}
			else {
				l = input.getSignedBits(8);
			}
			level = l;
		}

		runLevel.run   = run;
		runLevel.level = level;

		return true;
	}
}