1 
2 module vlc;
3 
4 import std.array;
5 import std.exception;
6 import std.typecons;
7 import std.string;
8 import bitstream;
9 import decoder;
10 
11 private alias VlcTableEntry = Tuple!(uint, uint);
12 private alias VlcTable = immutable VlcTableEntry[];
13 private struct VlcTab
14 {
15 	int val;
16 	int skip;
17 }
18 
19 struct DCTtab
20 {
21 	short run, level, len;
22 }
23 
24 const int ERROR = -1;
25 
26 private uint read_vlc(uint max_bits)(BitstreamReader bs, VlcTable table)
27 {
28 	uint bits = bs.nextbits(max_bits);
29 
30 	int i = 0;
31 	//std.stdio.writefln("---");
32 	//std.stdio.writefln("vlc: %b =?= %b (%d,%d,%d)", bits, table[i][0], max_bits, i, table.length);
33 	while(i < table.length && bits < table[i][0])
34 	{
35 		++i;
36 		//if(i < table.length) std.stdio.writefln("vlc: %b =?= %b (%d,%d,%d)", bits, table[i][0], max_bits, i, table.length);
37 	}
38 	assert(i < table.length);
39 	bs.skip_u(table[i][1]);
40 
41 	return i;
42 }
43 
44 public ubyte read_mb_inc(BitstreamReader bs)
45 {
46 	static VlcTable table = [
47 		tuple(0b1 << 10,              1),
48 
49 		tuple(0b011 << 8,             3),
50 		tuple(0b010 << 8,             3),
51 
52 		tuple(0b0011 << 7,            4),
53 		tuple(0b0010 << 7,            4),
54 
55 		tuple(0b0001_1 << 6,          5),
56 		tuple(0b0001_0 << 6,          5),
57 
58 		tuple(0b0000_111 << 4,        7),
59 		tuple(0b0000_110 << 4,        7),
60 
61 		tuple(0b0000_1011 << 3,       8),
62 		tuple(0b0000_1010 << 3,       8),
63 		tuple(0b0000_1001 << 3,       8),
64 		tuple(0b0000_1000 << 3,       8),
65 		tuple(0b0000_0111 << 3,       8),
66 		tuple(0b0000_0110 << 3,       8),
67 
68 		tuple(0b0000_0101_11 << 1,   10),
69 		tuple(0b0000_0101_10 << 1,   10),
70 		tuple(0b0000_0101_01 << 1,   10),
71 		tuple(0b0000_0101_00 << 1,   10),
72 		tuple(0b0000_0100_11 << 1,   10),
73 		tuple(0b0000_0100_10 << 1,   10),
74 
75 		tuple(0b0000_0100_011,       11),
76 		tuple(0b0000_0100_010,       11),
77 		tuple(0b0000_0100_001,       11),
78 		tuple(0b0000_0100_000,       11),
79 
80 		tuple(0b0000_0011_111,       11),
81 		tuple(0b0000_0011_110,       11),
82 		tuple(0b0000_0011_101,       11),
83 		tuple(0b0000_0011_100,       11),
84 		tuple(0b0000_0011_011,       11),
85 		tuple(0b0000_0011_010,       11),
86 		tuple(0b0000_0011_001,       11),
87 		tuple(0b0000_0011_000,       11),
88 		tuple(0b0000_0001_000,       11),	// escape
89 	];
90 
91 	auto v = bs.read_vlc!11(table);
92 	assert(v < 34);
93 	return cast(ubyte)(v+1);
94 }
95 
96 // Tables B.2 - B.8
97 public uint read_mb_type(BitstreamReader bs, PictureType picture_coding_type)
98 {
99 	static VlcTable table2 = [
100 		tuple(0b1 << 1, 1),
101 		tuple(0b01,     2),
102 	];
103 
104 	static VlcTable table3 = [
105 		tuple(0b1      << 5, 1),
106 		tuple(0b01     << 4, 2),
107 		tuple(0b001    << 3, 3),
108 		tuple(0b00011  << 1, 5),
109 		tuple(0b00010  << 1, 5),
110 		tuple(0b00001  << 1, 5),
111 		tuple(0b000001     , 6),
112 	];
113 
114 	static Tuple!(int,int,int)[] table4_first = [
115 
116 		tuple(0b0000, 0, -1),
117 		tuple(0b0001, 0, -1),
118 
119 		tuple(0b0010, 4, 4),
120 		tuple(0b0011, 4, 5),
121 
122 		tuple(0b0100, 3, 2),
123 		tuple(0b0101, 3, 2),
124 
125 		tuple(0b0110, 3, 3),
126 		tuple(0b0111, 3, 3),
127 
128 		tuple(0b1000, 2, 0),
129 		tuple(0b1001, 2, 0),
130 		tuple(0b1010, 2, 0),
131 		tuple(0b1011, 2, 0),
132 
133 		tuple(0b1100, 2, 1),
134 		tuple(0b1101, 2, 1),
135 		tuple(0b1110, 2, 1),
136 		tuple(0b1111, 2, 1),
137 	];
138 
139 	static VlcTable table4_second = [
140 		tuple(0b00011  << 1, 5),
141 		tuple(0b00010  << 1, 5),
142 		tuple(0b000011     , 6),
143 		tuple(0b000010     , 6),
144 		tuple(0b000001     , 6),
145 	];
146 
147 	uint v = -1;
148 	uint tmp;
149 	final switch(picture_coding_type)
150 	{
151 		case PictureType.I:
152 			v = bs.read_vlc!2(table2);
153 			break;
154 		case PictureType.P:
155 			v = bs.read_vlc!6(table3);
156 			break;
157 		case PictureType.B:
158 			tmp = bs.nextbits(4);
159 			auto t = table4_first[tmp];
160 			if(t[2] != -1)
161 			{
162 				bs.skip_u(t[1]);
163 				assert(tmp == t[1]);
164 				v = t[2];
165 				break;
166 			}
167 
168 			v = bs.read_vlc!6(table4_second);
169 			v += 6;
170 			break;
171 	}
172 
173 	return v;
174 }
175 
176 // Table B.9
177 public ubyte read_cbp(BitstreamReader bs)
178 {
179 	static VlcTable table = [
180 		tuple(0b111            << 6, 3),
181 
182 		tuple(0b1101           << 5, 4),
183 		tuple(0b1100           << 5, 4),
184 		tuple(0b1011           << 5, 4),
185 		tuple(0b1010           << 5, 4),
186 
187 		tuple(0b10011          << 4, 5),
188 		tuple(0b10010          << 4, 5),
189 		tuple(0b10001          << 4, 5),
190 		tuple(0b10000          << 4, 5),
191 		tuple(0b01111          << 4, 5),
192 		tuple(0b01110          << 4, 5),
193 		tuple(0b01101          << 4, 5),
194 		tuple(0b01100          << 4, 5),
195 		tuple(0b01011          << 4, 5),
196 		tuple(0b01010          << 4, 5),
197 		tuple(0b01001          << 4, 5),
198 		tuple(0b01000          << 4, 5),
199 
200 		tuple(0b001111         << 3, 6),
201 		tuple(0b001110         << 3, 6),
202 		tuple(0b001101         << 3, 6),
203 		tuple(0b001100         << 3, 6),
204 
205 		tuple(0b0010_111       << 2, 7),
206 		tuple(0b0010_110       << 2, 7),
207 		tuple(0b0010_101       << 2, 7),
208 		tuple(0b0010_100       << 2, 7),
209 		tuple(0b0010_011       << 2, 7),
210 		tuple(0b0010_010       << 2, 7),
211 		tuple(0b0010_001       << 2, 7),
212 		tuple(0b0010_000       << 2, 7),
213 
214 		tuple(0b0001_1111      << 1, 8),
215 		tuple(0b0001_1110      << 1, 8),
216 		tuple(0b0001_1101      << 1, 8),
217 		tuple(0b0001_1100      << 1, 8),
218 		tuple(0b0001_1011      << 1, 8),
219 		tuple(0b0001_1010      << 1, 8),
220 		tuple(0b0001_1001      << 1, 8),
221 		tuple(0b0001_1000      << 1, 8),
222 		tuple(0b0001_0111      << 1, 8),
223 		tuple(0b0001_0110      << 1, 8),
224 		tuple(0b0001_0101      << 1, 8),
225 		tuple(0b0001_0100      << 1, 8),
226 		tuple(0b0001_0011      << 1, 8),
227 		tuple(0b0001_0010      << 1, 8),
228 		tuple(0b0001_0001      << 1, 8),
229 		tuple(0b0001_0000      << 1, 8),
230 		tuple(0b0000_1111      << 1, 8),
231 		tuple(0b0000_1110      << 1, 8),
232 		tuple(0b0000_1101      << 1, 8),
233 		tuple(0b0000_1100      << 1, 8),
234 		tuple(0b0000_1011      << 1, 8),
235 		tuple(0b0000_1010      << 1, 8),
236 		tuple(0b0000_1001      << 1, 8),
237 		tuple(0b0000_1000      << 1, 8),
238 		tuple(0b0000_0111      << 1, 8),
239 		tuple(0b0000_0110      << 1, 8),
240 		tuple(0b0000_0101      << 1, 8),
241 		tuple(0b0000_0100      << 1, 8),
242 
243 		tuple(0b0000_0011_1        , 9),
244 		tuple(0b0000_0011_0        , 9),
245 		tuple(0b0000_0010_1        , 9),
246 		tuple(0b0000_0010_0        , 9),
247 		tuple(0b0000_0001_1        , 9),
248 		tuple(0b0000_0001_0        , 9),
249 		tuple(0b0000_0000_1        , 9),
250 	];
251 
252 	static immutable ubyte[] cbp = [
253 		60,  4,  8, 16, 32, 12, 48, 20, 40, 28,
254 		44, 52, 56,  1, 61,  2, 62, 24, 36,  3,
255 		63,  5,  9, 17, 33,  6, 10, 18, 34,  7,
256 		11, 19, 35, 13, 49, 21, 41, 14, 50, 22,
257 		42, 15, 51, 23, 43, 25, 37, 26, 38, 29,
258 		45, 53, 57, 30, 46, 54, 58, 31, 47, 55,
259 		59, 27, 39,  0
260 	];
261 
262 	static assert(cbp.length == table.length);
263 
264 	auto idx = bs.read_vlc!9(table);
265 	return cbp[idx];
266 }
267 
268 // Table B.10
269 public byte read_mc(BitstreamReader bs)
270 {
271 	static VlcTable table = [
272 		tuple(0b1            << 10, 1),
273 		tuple(0b010          << 08, 3),
274 		tuple(0b0010         << 07, 4),
275 		tuple(0b00010        << 6,  5),
276 		tuple(0b0000_110     << 4,  7),
277 		tuple(0b0000_1010    << 3,  8),
278 		tuple(0b0000_1000    << 3,  8),
279 		tuple(0b0000_0110    << 3,  8),
280 
281 		tuple(0b0000_0101_10 << 1, 10),
282 		tuple(0b0000_0101_00 << 1, 10),
283 		tuple(0b0000_0100_10 << 1, 10),
284 
285 		tuple(0b0000_0100_010    , 11),
286 		tuple(0b0000_0100_000    , 11),
287 		tuple(0b0000_0011_110    , 11),
288 		tuple(0b0000_0011_100    , 11),
289 		tuple(0b0000_0011_010    , 11),
290 		tuple(0b0000_0011_000    , 11),
291 	];
292 
293 	static assert(table.length == 17);
294 
295 	auto bits = bs.nextbits(11);
296 	int i = 0;
297 	while(i < table.length && bits < table[i][0])
298 	{
299 		++i;
300 	}
301 
302 	bs.skip(table[i][1]);
303 
304 	bits >>= 11 - table[i][1];
305 
306 	if(bits & 0x01)
307 	{
308 		i = -i;
309 	}
310 
311 	return cast(byte) i;
312 }
313 
314 // Table B.11
315 public byte read_dmvector(BitstreamReader bs)
316 {
317 	byte v = bs.read_u1;
318 
319 	if(v != 0)
320 	{
321 		v = bs.read_b(2);
322 
323 		if(v & 0x1) v = -v;
324 	}
325 
326 	return v;
327 }
328 
329 /* Table B-12, dct_dc_size_luminance, codes 00xxx ... 11110 */
330 static VlcTab[32] DClumtab0 =
331 [
332 	{1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
333 	{2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
334 	{0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
335 	{4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}, {ERROR, 0}
336 ];
337 
338 /* Table B-12, dct_dc_size_luminance, codes 111110xxx ... 111111111 */
339 static VlcTab[16] DClumtab1 =
340 [
341 	{7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6},
342 	{8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10,9}, {11,9}
343 ];
344 
345 /* Table B-13, dct_dc_size_chrominance, codes 00xxx ... 11110 */
346 static VlcTab[32] DCchromtab0 =
347 [
348 	{0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
349 	{1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
350 	{2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
351 	{3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}, {ERROR, 0}
352 ];
353 
354 /* Table B-13, dct_dc_size_chrominance, codes 111110xxxx ... 1111111111 */
355 static VlcTab[32] DCchromtab1 =
356 [
357 	{6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
358 	{6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
359 	{7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7},
360 	{8, 8}, {8, 8}, {8, 8}, {8, 8}, {9, 9}, {9, 9}, {10,10}, {11,10}
361 ];
362 
363 public ubyte read_dc_size_luma(BitstreamReader bs)
364 {
365 	auto bits = bs.nextbits(5);
366 	VlcTab e;
367 	if (bits<31)
368 	{
369 		e = DClumtab0[bits];
370 	}
371 	else
372 	{
373 		bits = bs.nextbits(9) - 0x1f0;
374 		e = DClumtab1[bits];
375 	}
376 
377 	bs.skip_u(e.skip);
378 	return cast(ubyte) e.val;
379 }
380 
381 public ubyte read_dc_size_chroma(BitstreamReader bs)
382 {
383 	auto bits = bs.nextbits(5);
384 	VlcTab e;
385 	if (bits<31)
386 	{
387 		e = DCchromtab0[bits];
388 	}
389 	else
390 	{
391 		bits = bs.nextbits(10) - 0x3e0;
392 		e = DCchromtab1[bits];
393 	}
394 
395 	bs.skip_u(e.skip);
396 	return cast(ubyte) e.val;
397 }
398 
399 public ubyte read_dc_size(BitstreamReader bs, bool luma)
400 {
401 	if(luma)
402 		return bs.read_dc_size_luma();
403 	else
404 		return bs.read_dc_size_chroma();
405 }
406 
407 /* Table B-14, DCT coefficients table zero,
408  * codes 0100 ... 1xxx (used for first (DC) coefficient)
409  */
410 DCTtab[12] DCTtabfirst =
411 [
412 	{0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
413 	{0,1,1}, {0,1,1}, {0,1,1}, {0,1,1},
414 	{0,1,1}, {0,1,1}, {0,1,1}, {0,1,1}
415 ];
416 
417 /* Table B-14, DCT coefficients table zero,
418  * codes 0100 ... 1xxx (used for all other coefficients)
419  */
420 DCTtab[12] DCTtabnext =
421 [
422 	{0,2,4},  {2,1,4},  {1,1,3},  {1,1,3},
423 	{64,0,2}, {64,0,2}, {64,0,2}, {64,0,2}, /* EOB */
424 	{0,1,2},  {0,1,2},  {0,1,2},  {0,1,2}
425 ];
426 
427 /* Table B-14, DCT coefficients table zero,
428  * codes 000001xx ... 00111xxx
429  */
430 DCTtab[60] DCTtab0 =
431 [
432 	{65,0,6}, {65,0,6}, {65,0,6}, {65,0,6}, /* Escape */
433 	{2,2,7}, {2,2,7}, {9,1,7}, {9,1,7},
434 	{0,4,7}, {0,4,7}, {8,1,7}, {8,1,7},
435 	{7,1,6}, {7,1,6}, {7,1,6}, {7,1,6},
436 	{6,1,6}, {6,1,6}, {6,1,6}, {6,1,6},
437 	{1,2,6}, {1,2,6}, {1,2,6}, {1,2,6},
438 	{5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
439 	{13,1,8}, {0,6,8}, {12,1,8}, {11,1,8},
440 	{3,2,8}, {1,3,8}, {0,5,8}, {10,1,8},
441 	{0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
442 	{0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
443 	{4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
444 	{4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
445 	{3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
446 	{3,1,5}, {3,1,5}, {3,1,5}, {3,1,5}
447 ];
448 
449 /* Table B-15, DCT coefficients table one,
450  * codes 000001xx ... 11111111
451 */
452 DCTtab[252] DCTtab0a =
453 [
454 	{65,0,6}, {65,0,6}, {65,0,6}, {65,0,6}, /* Escape */
455 	{7,1,7}, {7,1,7}, {8,1,7}, {8,1,7},
456 	{6,1,7}, {6,1,7}, {2,2,7}, {2,2,7},
457 	{0,7,6}, {0,7,6}, {0,7,6}, {0,7,6},
458 	{0,6,6}, {0,6,6}, {0,6,6}, {0,6,6},
459 	{4,1,6}, {4,1,6}, {4,1,6}, {4,1,6},
460 	{5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
461 	{1,5,8}, {11,1,8}, {0,11,8}, {0,10,8},
462 	{13,1,8}, {12,1,8}, {3,2,8}, {1,4,8},
463 	{2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
464 	{2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
465 	{1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
466 	{1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
467 	{3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
468 	{3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
469 	{1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
470 	{1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
471 	{1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
472 	{1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
473 	{1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
474 	{1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
475 	{1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
476 	{1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
477 	{64,0,4}, {64,0,4}, {64,0,4}, {64,0,4}, /* EOB */
478 	{64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
479 	{64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
480 	{64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
481 	{0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
482 	{0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
483 	{0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
484 	{0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
485 	{0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
486 	{0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
487 	{0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
488 	{0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
489 	{0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
490 	{0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
491 	{0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
492 	{0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
493 	{0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
494 	{0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
495 	{0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
496 	{0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
497 	{0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
498 	{0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
499 	{0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
500 	{0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
501 	{0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
502 	{0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
503 	{0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
504 	{0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
505 	{0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
506 	{0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
507 	{0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
508 	{0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
509 	{0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
510 	{0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
511 	{0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
512 	{0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
513 	{9,1,7}, {9,1,7}, {1,3,7}, {1,3,7},
514 	{10,1,7}, {10,1,7}, {0,8,7}, {0,8,7},
515 	{0,9,7}, {0,9,7}, {0,12,8}, {0,13,8},
516 	{2,3,8}, {4,2,8}, {0,14,8}, {0,15,8}
517 ];
518 
519 /* Table B-14, DCT coefficients table zero,
520  * codes 0000001000 ... 0000001111
521  */
522 DCTtab[8] DCTtab1 =
523 [
524 	{16,1,10}, {5,2,10}, {0,7,10}, {2,3,10},
525 	{1,4,10}, {15,1,10}, {14,1,10}, {4,2,10}
526 ];
527 
528 /* Table B-15, DCT coefficients table one,
529  * codes 000000100x ... 000000111x
530  */
531 DCTtab[8] DCTtab1a =
532 [
533 	{5,2,9}, {5,2,9}, {14,1,9}, {14,1,9},
534 	{2,4,10}, {16,1,10}, {15,1,9}, {15,1,9}
535 ];
536 
537 /* Table B-14/15, DCT coefficients table zero / one,
538  * codes 000000010000 ... 000000011111
539  */
540 DCTtab[16] DCTtab2 =
541 [
542 	{0,11,12}, {8,2,12}, {4,3,12}, {0,10,12},
543 	{2,4,12}, {7,2,12}, {21,1,12}, {20,1,12},
544 	{0,9,12}, {19,1,12}, {18,1,12}, {1,5,12},
545 	{3,3,12}, {0,8,12}, {6,2,12}, {17,1,12}
546 ];
547 
548 /* Table B-14/15, DCT coefficients table zero / one,
549  * codes 0000000010000 ... 0000000011111
550  */
551 DCTtab[16] DCTtab3 =
552 [
553 	{10,2,13}, {9,2,13}, {5,3,13}, {3,4,13},
554 	{2,5,13}, {1,7,13}, {1,6,13}, {0,15,13},
555 	{0,14,13}, {0,13,13}, {0,12,13}, {26,1,13},
556 	{25,1,13}, {24,1,13}, {23,1,13}, {22,1,13}
557 ];
558 
559 /* Table B-14/15, DCT coefficients table zero / one,
560  * codes 00000000010000 ... 00000000011111
561  */
562 DCTtab[16] DCTtab4 =
563 [
564 	{0,31,14}, {0,30,14}, {0,29,14}, {0,28,14},
565 	{0,27,14}, {0,26,14}, {0,25,14}, {0,24,14},
566 	{0,23,14}, {0,22,14}, {0,21,14}, {0,20,14},
567 	{0,19,14}, {0,18,14}, {0,17,14}, {0,16,14}
568 ];
569 
570 /* Table B-14/15, DCT coefficients table zero / one,
571  * codes 000000000010000 ... 000000000011111
572  */
573 DCTtab[16] DCTtab5 =
574 [
575 	{0,40,15}, {0,39,15}, {0,38,15}, {0,37,15},
576 	{0,36,15}, {0,35,15}, {0,34,15}, {0,33,15},
577 	{0,32,15}, {1,14,15}, {1,13,15}, {1,12,15},
578 	{1,11,15}, {1,10,15}, {1,9,15}, {1,8,15}
579 ];
580 
581 /* Table B-14/15, DCT coefficients table zero / one,
582  * codes 0000000000010000 ... 0000000000011111
583  */
584 DCTtab[16] DCTtab6 =
585 [
586 	{1,18,16}, {1,17,16}, {1,16,16}, {1,15,16},
587 	{6,3,16}, {16,2,16}, {15,2,16}, {14,2,16},
588 	{13,2,16}, {12,2,16}, {11,2,16}, {31,1,16},
589 	{30,1,16}, {29,1,16}, {28,1,16}, {27,1,16}
590 ];
591 
592 public bool read_dct(BitstreamReader bs, bool first, out short run, out short level, bool intra_vlc_format)
593 {
594 	auto code = bs.nextbits(16);
595 	int sign;
596 	DCTtab tab;
597 
598 	DCTtab[] DCTtabfirst_next = first?DCTtabfirst:DCTtabnext;
599 	if (code>=16384 && !intra_vlc_format)
600 		tab = DCTtabfirst_next[(code>>12)-4];
601 	else if (code>=1024)
602 	{
603 		if (intra_vlc_format)
604 			tab = DCTtab0a[(code>>8)-4];
605 		else
606 			tab = DCTtab0[(code>>8)-4];
607 	}
608 	else if (code>=512)
609 	{
610 		if (intra_vlc_format)
611 			tab = DCTtab1a[(code>>6)-8];
612 		else
613 			tab = DCTtab1[(code>>6)-8];
614 	}
615 	else if (code>=256)
616 		tab = DCTtab2[(code>>4)-16];
617 	else if (code>=128)
618 		tab = DCTtab3[(code>>3)-16];
619 	else if (code>=64)
620 		tab = DCTtab4[(code>>2)-16];
621 	else if (code>=32)
622 		tab = DCTtab5[(code>>1)-16];
623 	else if (code>=16)
624 		tab = DCTtab6[code-16];
625 	else
626 	{
627 		throw new Exception("invalid Huffman code for code %016b".format(code));
628 	}
629 
630 	bs.skip_u(tab.len);
631 
632 	if (tab.run == 64) /* end_of_block */
633 		return false;
634 
635 	if (tab.run==65) /* escape */
636 	{
637 		//std.stdio.writefln("mb escape");
638 		tab.run = cast(short) bs.read_u(6);
639 		tab.level = cast(short) bs.read_u(12);
640 
641 		enforce((tab.level&2047) != 0, "invalid macroblock DCT escape code");
642 		sign = tab.level>=2048;
643 		if(sign)
644 		{
645 			tab.level = cast(short)(4096 - tab.level);
646 		}
647 	}
648 	else
649 	{
650 		sign = bs.read_u1;
651 	}
652 
653 	if(sign)
654 		tab.level = -tab.level;
655 
656 	run = tab.run;
657 	level = tab.level;
658 	return true;
659 }
660 
661 unittest
662 {
663 	static VlcTable table = [
664 		tuple(0b100,1),
665 		tuple(0b010,2),
666 		tuple(0b001,3),
667 	];
668 
669 	void test(ubyte b, int v)
670 	{
671 		auto bs = new BitstreamReader([b]);
672 		assert(bs.read_vlc!3(table) == v);
673 	}
674 
675 	test(0b100 << 5, 0);
676 	test(0b101 << 5, 0);
677 	test(0b110 << 5, 0);
678 	test(0b111 << 5, 0);
679 
680 	test(0b010 << 5, 1);
681 	test(0b010 << 5, 1);
682 
683 	test(0b001 << 5, 2);
684 }
685 
686 unittest
687 {
688 	auto bs = new BitstreamReader([0b10110100, 0b010_0011_1, 0]);
689 
690 	assert(bs.read_mb_inc() == 1);
691 	assert(bs.read_mb_inc() == 2);
692 	assert(bs.read_mb_inc() == 3);
693 	assert(bs.read_mb_inc() == 5);
694 	assert(bs.read_mb_inc() == 4);
695 	assert(bs.read_mb_inc() == 1);
696 }
697 
698 unittest
699 {
700 	auto bs = new BitstreamReader([0xff, 0xff, 0xff, 0]);
701 	assert(bs.read_mb_inc() == 1);
702 	assert(bs.read_mb_inc() == 1);
703 	assert(bs.read_mb_inc() == 1);
704 	assert(bs.read_mb_inc() == 1);
705 	assert(bs.read_mb_inc() == 1);
706 	assert(bs.read_mb_inc() == 1);
707 	assert(bs.read_mb_inc() == 1);
708 	assert(bs.read_mb_inc() == 1);
709 
710 }
711 
712 unittest
713 {
714 	auto bs = new BitstreamReader([0b11101111, 0b00100110, 0b00000111, 1,1]);
715 	// 60, 28, 6, 31
716 	assert(bs.read_cbp() == 60);
717 	assert(bs.read_cbp() == 28);
718 	assert(bs.read_cbp() == 06);
719 	assert(bs.read_cbp() == 31);
720 
721 	void test(ushort b, int v)
722 	{
723 		auto bs = new BitstreamReader([b >> 8, b & 0xff]);
724 		assert(bs.read_cbp() == v);
725 	}
726 
727 	test(0b0001_1000_0000_0000, 41);
728 	test(0b0000_1111_0000_0000, 25);
729 	test(0b0000_0011_0000_0000, 47);
730 	test(0b0000_0001_0000_0000, 39);
731 
732 	test(0b0001_0011_0000_0000, 15);
733 	test(0b1011_0000_0000_0000, 16);
734 
735 	test(0b0000_0000_1000_0000, 0);
736 }
737 
738 unittest
739 {
740 	auto bs = new BitstreamReader([0b01011011, 0b01000000, 0, 1,1]);
741 	bool r;
742 	short run, level;
743 	r = bs.read_dct(true, run, level, false);
744 	assert(r);
745 	assert(run == 2);
746 	assert(level == -1);
747 
748 	r = bs.read_dct(false, run, level, false);
749 	assert(r);
750 	assert(run == 1);
751 	assert(level == 1);
752 
753 	r = bs.read_dct(false, run, level, false);
754 	assert(!r);
755 }
756 
757 unittest
758 {
759 	void test(ushort b, int v)
760 	{
761 		auto bs = new BitstreamReader([b >> 8, b & 0xff]);
762 		assert(bs.read_mc() == v);
763 	}
764 
765 	test(0b1000_0000_0000_0000, 0);
766 
767 	test(0b0100_0000_0000_0000, +1);
768 	test(0b0110_0000_0000_0000, -1);
769 
770 	test(0b0010_0000_0000_0000, +2);
771 	test(0b0011_0000_0000_0000, -2);
772 
773 	test(0b0001_0000_0000_0000, +3);
774 	test(0b0001_1000_0000_0000, -3);
775 
776 	test(0b0000_1100_0000_0000, +4);
777 	test(0b0000_1110_0000_0000, -4);
778 }