Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / quicktime / encore50 / putvlc.c
blob0a34f19c2f570808d01e4257df4ff82934cae0f1
2 /**************************************************************************
3 * *
4 * This code is developed by Adam Li. This software is an *
5 * implementation of a part of one or more MPEG-4 Video tools as *
6 * specified in ISO/IEC 14496-2 standard. Those intending to use this *
7 * software module in hardware or software products are advised that its *
8 * use may infringe existing patents or copyrights, and any such use *
9 * would be at such party's own risk. The original developer of this *
10 * software module and his/her company, and subsequent editors and their *
11 * companies (including Project Mayo), will have no liability for use of *
12 * this software or modifications or derivatives thereof. *
13 * *
14 * Project Mayo gives users of the Codec a license to this software *
15 * module or modifications thereof for use in hardware or software *
16 * products claiming conformance to the MPEG-4 Video Standard as *
17 * described in the Open DivX license. *
18 * *
19 * The complete Open DivX license can be found at *
20 * http://www.projectmayo.com/opendivx/license.php . *
21 * *
22 **************************************************************************/
24 /**************************************************************************
26 * putvlc.c
28 * Copyright (C) 2001 Project Mayo
30 * Adam Li
32 * DivX Advance Research Center <darc@projectmayo.com>
34 **************************************************************************/
36 /* This file contains some functions to code the RVLC code for bitstream. */
37 /* Some codes of this project come from MoMuSys MPEG-4 implementation. */
38 /* Please see seperate acknowledgement file for a list of contributors. */
40 #include "momusys.h"
41 #include "vlc.h"
42 #include "putvlc.h"
43 #include "bitstream.h"
45 /***********************************************************CommentBegin******
47 * -- Putxxx -- Write bits from huffman tables to file
49 * Int PutCoeff_Inter(Int run, Int level, Int last, Image *bitstream)
50 * Int PutCoeff_Intra(Int run, Int level, Int last, Image *bitstream)
51 * Int PutCBPY (Int cbpy, Int *MB_transp_pattren,Image *bitstream)
52 * Int PutMCBPC_Inter (Int cbpc, Int mode, Image *bitstream)
53 * Int PutMCBPC_Sprite (Int cbpc, Int mode, Image *bitstream)
54 * Int PutMCBPC_Intra (Int cbpc, Int mode, Image *bitstream)
55 * Int PutMV (Int mvint, Image *bitstream)
56 * Int PutDCsize_chrom (Int size, Image *bitstream)
57 * Int PutDCsize_lum (Int size, Image *bitstream)
58 * Int PutDCsize_lum (Int size, Image *bitstream)
59 * Int PutCoeff_Inter_RVLC (Int run, Int level, Int last, Image *bitstream)
60 * Int PutCoeff_Intra_RVLC (Int run, Int level, Int last, Image *bitstream)
61 * Int PutRunCoeff_Inter (Int run, Int level, Int last, Image *bitstream)
62 * Int PutRunCoeff_Intra (Int run, Int level, Int last, Image *bitstream)
63 * Int PutLevelCoeff_Inter (Int run, Int level, Int last, Image *bitstream)
64 * Int PutLevelCoeff_Intra (Int run, Int level, Int last, Image *bitstream)
66 * Purpose :
67 * Writes bits from huffman tables to bitstream
69 * Arguments in :
70 * various, see prototypes above
72 * Return values :
73 * Number of bits written
75 ***********************************************************CommentEnd********/
77 Int
78 PutDCsize_lum (Int size, Image *bitstream)
80 MOMCHECK(size >= 0 && size < 13);
82 BitstreamPutBits (bitstream, DCtab_lum[size].code, DCtab_lum[size].len);
84 return DCtab_lum[size].len;
88 Int
89 PutDCsize_chrom (Int size, Image *bitstream)
91 MOMCHECK (size >= 0 && size < 13);
93 BitstreamPutBits (bitstream, DCtab_chrom[size].code, DCtab_chrom[size].len);
95 return DCtab_chrom[size].len;
99 Int
100 PutMV (Int mvint, Image *bitstream)
102 Int sign = 0;
103 Int absmv;
105 if (mvint > 32)
107 absmv = -mvint + 65;
108 sign = 1;
110 else
111 absmv = mvint;
113 BitstreamPutBits (bitstream, mvtab[absmv].code, mvtab[absmv].len);
115 if (mvint != 0)
117 BitstreamPutBits (bitstream, sign, 1);
118 return mvtab[absmv].len + 1;
120 else
121 return mvtab[absmv].len;
126 PutMCBPC_Intra (Int cbpc, Int mode, Image *bitstream)
128 Int ind;
130 ind = ((mode >> 1) & 3) | ((cbpc & 3) << 2);
132 BitstreamPutBits (bitstream, mcbpc_intra_tab[ind].code, mcbpc_intra_tab[ind].len);
134 return mcbpc_intra_tab[ind].len;
139 PutMCBPC_Inter (Int cbpc, Int mode, Image *bitstream)
141 Int ind;
143 ind = (mode & 7) | ((cbpc & 3) << 3);
145 BitstreamPutBits (bitstream, mcbpc_inter_tab[ind].code,
146 mcbpc_inter_tab[ind].len);
148 return mcbpc_inter_tab[ind].len;
153 PutMCBPC_Sprite (Int cbpc, Int mode, Image *bitstream)
155 Int ind;
157 ind = (mode & 7) | ((cbpc & 3) << 3);
159 BitstreamPutBits (bitstream, mcbpc_sprite_tab[ind].code,
160 mcbpc_sprite_tab[ind].len);
162 return mcbpc_sprite_tab[ind].len;
167 PutCBPY (Int cbpy, Char intra, Int *MB_transp_pattern, Image *bitstream)
169 Int ind;//,i,ptr;
170 Int index=0;
172 /* Changed due to bug report from Yoshinori Suzuki; MW 11-JUN-1998 */
173 /* if ((intra==0)&&(index!=3)) cbpy = 15 - cbpy; */
174 if (intra == 0) cbpy = 15 - cbpy;
176 ind = cbpy;
177 BitstreamPutBits (bitstream, cbpy_tab[ind].code, cbpy_tab[ind].len);
178 return cbpy_tab[ind].len;
183 PutCoeff_Inter(Int run, Int level, Int last, Image *bitstream)
185 Int length = 0;
187 MOMCHECK (last >= 0 && last < 2);
188 MOMCHECK (run >= 0 && run < 64);
189 MOMCHECK (level > 0 && level < 128);
191 if (last == 0)
193 if (run < 2 && level < 13 )
195 BitstreamPutBits (bitstream, (LInt)coeff_tab0[run][level-1].code,
196 (LInt)coeff_tab0[run][level-1].len);
198 length = coeff_tab0[run][level-1].len;
200 else if (run > 1 && run < 27 && level < 5)
202 BitstreamPutBits (bitstream, (LInt)coeff_tab1[run-2][level-1].code,
203 (LInt)coeff_tab1[run-2][level-1].len);
205 length = coeff_tab1[run-2][level-1].len;
208 else if (last == 1)
210 if (run < 2 && level < 4)
212 BitstreamPutBits (bitstream, (LInt)coeff_tab2[run][level-1].code,
213 (LInt)coeff_tab2[run][level-1].len);
215 length = coeff_tab2[run][level-1].len;
217 else if (run > 1 && run < 42 && level == 1)
219 BitstreamPutBits (bitstream, (LInt)coeff_tab3[run-2].code,
220 (LInt)coeff_tab3[run-2].len);
222 length = coeff_tab3[run-2].len;
225 return length;
230 PutCoeff_Intra(Int run, Int level, Int last, Image *bitstream)
232 Int length = 0;
234 MOMCHECK (last >= 0 && last < 2);
235 MOMCHECK (run >= 0 && run < 64);
236 MOMCHECK (level > 0 && level < 128);
238 if (last == 0)
240 if (run == 0 && level < 28 )
242 BitstreamPutBits(bitstream, (LInt)coeff_tab4[level-1].code,
243 (LInt)coeff_tab4[level-1].len);
245 length = coeff_tab4[level-1].len;
247 else if (run == 1 && level < 11)
249 BitstreamPutBits(bitstream, (LInt)coeff_tab5[level-1].code,
250 (LInt)coeff_tab5[level-1].len);
252 length = coeff_tab5[level-1].len;
254 else if (run > 1 && run < 10 && level < 6)
256 BitstreamPutBits(bitstream, (LInt)coeff_tab6[run-2][level-1].code,
257 (LInt)coeff_tab6[run-2][level-1].len);
259 length = coeff_tab6[run-2][level-1].len;
261 else if (run > 9 && run < 15 && level == 1)
263 BitstreamPutBits(bitstream, (LInt)coeff_tab7[run-10].code,
264 (LInt)coeff_tab7[run-10].len);
266 length = coeff_tab7[run-10].len;
269 else if (last == 1)
271 if (run == 0 && level < 9)
273 BitstreamPutBits(bitstream, (LInt)coeff_tab8[level-1].code,
274 (LInt)coeff_tab8[level-1].len);
276 length = coeff_tab8[level-1].len;
278 else if (run > 0 && run < 7 && level < 4)
280 BitstreamPutBits(bitstream, (LInt)coeff_tab9[run-1][level-1].code,
281 (LInt)coeff_tab9[run-1][level-1].len);
283 length = coeff_tab9[run-1][level-1].len;
285 else if (run > 6 && run < 21 && level == 1)
287 BitstreamPutBits(bitstream, (LInt)coeff_tab10[run-7].code,
288 (LInt)coeff_tab10[run-7].len);
290 length = coeff_tab10[run-7].len;
293 return length;
298 PutCoeff_Inter_RVLC(Int run, Int level, Int last, Image *bitstream)
300 Int length = 0;
302 MOMCHECK (last >= 0 && last < 2);
303 MOMCHECK (run >= 0 && run < 64);
304 MOMCHECK (level > 0 && level < 128);
306 if (last == 0)
308 if (run == 0 && level < 20 )
310 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab14[level-1].code,
311 (LInt)coeff_RVLCtab14[level-1].len);
313 length = coeff_RVLCtab14[level-1].len;
315 else if (run == 1 && level < 11)
317 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab15[level-1].code,
318 (LInt)coeff_RVLCtab15[level-1].len);
320 length = coeff_RVLCtab15[level-1].len;
322 else if (run > 1 && run < 4 && level < 8)
324 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab16[run-2][level-1].code,
325 (LInt)coeff_RVLCtab16[run-2][level-1].len);
326 length = coeff_RVLCtab16[run-2][level-1].len;
328 else if (run == 4 && level < 6)
330 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab17[level-1].code,
331 (LInt)coeff_RVLCtab17[level-1].len);
333 length = coeff_RVLCtab17[level-1].len;
335 else if (run > 4 && run < 8 && level < 5)
337 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab18[run-5][level-1].code,
338 (LInt)coeff_RVLCtab18[run-5][level-1].len);
340 length = coeff_RVLCtab18[run-5][level-1].len;
342 else if (run > 7 && run < 10 && level < 4)
344 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab19[run-8][level-1].code,
345 (LInt)coeff_RVLCtab19[run-8][level-1].len);
347 length = coeff_RVLCtab19[run-8][level-1].len;
349 else if (run > 9 && run < 18 && level < 3)
351 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab20[run-10][level-1].code,
352 (LInt)coeff_RVLCtab20[run-10][level-1].len);
354 length = coeff_RVLCtab20[run-10][level-1].len;
356 else if (run > 17 && run < 39 && level == 1)
358 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab21[run-18].code,
359 (LInt)coeff_RVLCtab21[run-18].len);
361 length = coeff_RVLCtab21[run-18].len;
364 else if (last == 1)
366 if (run >= 0 && run < 2 && level < 6)
368 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab22[run][level-1].code,
369 (LInt)coeff_RVLCtab22[run][level-1].len);
371 length = coeff_RVLCtab22[run][level-1].len;
373 else if (run == 2 && level < 4)
375 BitstreamPutBits (bitstream, (LInt)coeff_RVLCtab23[level-1].code,
376 (LInt)coeff_RVLCtab23[level-1].len);
378 length = coeff_RVLCtab23[level-1].len;
380 else if (run > 2 && run < 14 && level < 3)
382 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab24[run-3][level-1].code,
383 (LInt)coeff_RVLCtab24[run-3][level-1].len);
385 length = coeff_RVLCtab24[run-3][level-1].len;
387 else if (run > 13 && run < 46 && level == 1)
389 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab25[run-14].code,
390 (LInt)coeff_RVLCtab25[run-14].len);
392 length = coeff_RVLCtab25[run-14].len;
395 return length;
400 PutCoeff_Intra_RVLC(Int run, Int level, Int last, Image *bitstream)
402 Int length = 0;
404 MOMCHECK (last >= 0 && last < 2);
405 MOMCHECK (run >= 0 && run < 64);
406 MOMCHECK (level > 0 && level < 128);
408 if (last == 0)
410 if (run == 0 && level < 28 )
412 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab1[level-1].code,
413 (LInt)coeff_RVLCtab1[level-1].len);
415 length = coeff_RVLCtab1[level-1].len;
417 else if (run == 1 && level < 14)
419 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab2[level-1].code,
420 (LInt)coeff_RVLCtab2[level-1].len);
422 length = coeff_RVLCtab2[level-1].len;
424 else if (run == 2 && level < 12)
426 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab3[level-1].code,
427 (LInt)coeff_RVLCtab3[level-1].len);
429 length = coeff_RVLCtab3[level-1].len;
431 else if (run == 3 && level < 10)
433 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab4[level-1].code,
434 (LInt)coeff_RVLCtab4[level-1].len);
436 length = coeff_RVLCtab4[level-1].len;
438 else if (run > 3 && run < 6 && level < 7)
440 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab5[run-4][level-1].code,
441 (LInt)coeff_RVLCtab5[run-4][level-1].len);
443 length = coeff_RVLCtab5[run-4][level-1].len;
445 else if (run > 5 && run < 8 && level < 6)
447 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab6[run-6][level-1].code,
448 (LInt)coeff_RVLCtab6[run-6][level-1].len);
450 length = coeff_RVLCtab6[run-6][level-1].len;
452 else if (run > 7 && run < 10 && level < 5)
454 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab7[run-8][level-1].code,
455 (LInt)coeff_RVLCtab7[run-8][level-1].len);
457 length = coeff_RVLCtab7[run-8][level-1].len;
459 else if (run > 9 && run < 13 && level < 3)
461 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab8[run-10][level-1].code,
462 (LInt)coeff_RVLCtab8[run-10][level-1].len);
464 length = coeff_RVLCtab8[run-10][level-1].len;
466 else if (run > 12 && run < 20 && level == 1)
468 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab9[run-13].code,
469 (LInt)coeff_RVLCtab9[run-13].len);
471 length = coeff_RVLCtab9[run-13].len;
474 else if (last == 1)
476 if (run >= 0 && run < 2 && level < 6)
478 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab10[run][level-1].code,
479 (LInt)coeff_RVLCtab10[run][level-1].len);
481 length = coeff_RVLCtab10[run][level-1].len;
483 else if (run == 2 && level < 4)
485 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab11[level-1].code,
486 (LInt)coeff_RVLCtab11[level-1].len);
488 length = coeff_RVLCtab11[level-1].len;
490 else if (run > 2 && run < 14 && level < 3)
492 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab12[run-3][level-1].code,
493 (LInt)coeff_RVLCtab12[run-3][level-1].len);
495 length = coeff_RVLCtab12[run-3][level-1].len;
497 else if (run > 13 && run < 46 && level == 1)
499 BitstreamPutBits(bitstream, (LInt)coeff_RVLCtab13[run-14].code,
500 (LInt)coeff_RVLCtab13[run-14].len);
502 length = coeff_RVLCtab13[run-14].len;
505 return length;
509 /* The following is for 3-mode VLC */
512 PutRunCoeff_Inter(Int run, Int level, Int last, Image *bitstream)
514 Int length = 0;
516 MOMCHECK (last >= 0 && last < 2);
517 MOMCHECK (run >= 0 && run < 64);
518 MOMCHECK (level > 0 && level < 128);
520 if (last == 0)
522 if (run < 2 && level < 13 )
524 length = coeff_tab0[run][level-1].len;
525 if (length != 0)
527 /* boon 120697 */
528 BitstreamPutBits(bitstream, 3L, 7L);
529 /* boon 120697 */
530 BitstreamPutBits(bitstream, 2L, 2L);
531 length += 9; /* boon */
532 BitstreamPutBits (bitstream, (LInt)coeff_tab0[run][level-1].code,
533 (LInt)coeff_tab0[run][level-1].len);
536 else if (run > 1 && run < 27 && level < 5)
538 length = coeff_tab1[run-2][level-1].len;
539 if (length != 0)
541 /* boon 120697 */
542 BitstreamPutBits(bitstream, 3L, 7L);
543 /* boon 120697 */
544 BitstreamPutBits(bitstream, 2L, 2L);
545 length += 9; /* boon */
546 BitstreamPutBits (bitstream, (LInt)coeff_tab1[run-2][level-1].code,
547 (LInt)coeff_tab1[run-2][level-1].len);
551 else if (last == 1)
553 if (run < 2 && level < 4)
555 length = coeff_tab2[run][level-1].len;
556 if (length != 0)
558 /* boon 120697 */
559 BitstreamPutBits(bitstream, 3L, 7L);
560 /* boon 120697 */
561 BitstreamPutBits(bitstream, 2L, 2L);
562 length += 9; /* boon */
563 BitstreamPutBits (bitstream, (LInt)coeff_tab2[run][level-1].code,
564 (LInt)coeff_tab2[run][level-1].len);
567 else if (run > 1 && run < 42 && level == 1)
569 length = coeff_tab3[run-2].len;
570 if (length != 0)
572 /* boon 120697 */
573 BitstreamPutBits(bitstream, 3L, 7L);
574 /* boon 120697 */
575 BitstreamPutBits(bitstream, 2L, 2L);
576 length += 9; /* boon */
577 BitstreamPutBits (bitstream, (LInt)coeff_tab3[run-2].code,
578 (LInt)coeff_tab3[run-2].len);
582 return length;
587 PutRunCoeff_Intra(Int run, Int level, Int last, Image *bitstream)
589 Int length = 0;
591 MOMCHECK (last >= 0 && last < 2);
592 MOMCHECK (run >= 0 && run < 64);
593 MOMCHECK (level > 0 && level < 128);
595 if (last == 0)
597 if (run == 0 && level < 28 )
599 length = coeff_tab4[level-1].len;
600 if (length != 0)
602 /* boon 120697 */
603 BitstreamPutBits(bitstream, 3L, 7L);
604 /* boon 120697 */
605 BitstreamPutBits(bitstream, 2L, 2L);
606 length += 9; /* boon */
607 BitstreamPutBits(bitstream, (LInt)coeff_tab4[level-1].code,
608 (LInt)coeff_tab4[level-1].len);
611 else if (run == 1 && level < 11)
613 length = coeff_tab5[level-1].len;
614 if (length != 0)
616 /* boon 120697 */
617 BitstreamPutBits(bitstream, 3L, 7L);
618 /* boon 120697 */
619 BitstreamPutBits(bitstream, 2L, 2L);
620 length += 9; /* boon */
621 BitstreamPutBits(bitstream, (LInt)coeff_tab5[level-1].code,
622 (LInt)coeff_tab5[level-1].len);
625 else if (run > 1 && run < 10 && level < 6)
627 length = coeff_tab6[run-2][level-1].len;
628 if (length != 0)
630 /* boon 120697 */
631 BitstreamPutBits(bitstream, 3L, 7L);
632 /* boon 120697 */
633 BitstreamPutBits(bitstream, 2L, 2L);
634 length += 9; /* boon */
635 BitstreamPutBits(bitstream, (LInt)coeff_tab6[run-2][level-1].code,
636 (LInt)coeff_tab6[run-2][level-1].len);
639 else if (run > 9 && run < 15 && level == 1)
641 length = coeff_tab7[run-10].len;
642 if (length != 0)
644 /* boon 120697 */
645 BitstreamPutBits(bitstream, 3L, 7L);
646 /* boon 120697 */
647 BitstreamPutBits(bitstream, 2L, 2L);
648 length += 9; /* boon */
649 BitstreamPutBits(bitstream, (LInt)coeff_tab7[run-10].code,
650 (LInt)coeff_tab7[run-10].len);
654 else if (last == 1)
656 if (run == 0 && level < 9)
658 length = coeff_tab8[level-1].len;
659 if (length != 0)
661 /* boon 120697 */
662 BitstreamPutBits(bitstream, 3L, 7L);
663 /* boon 120697 */
664 BitstreamPutBits(bitstream, 2L, 2L);
665 length += 9; /* boon */
666 BitstreamPutBits(bitstream, (LInt)coeff_tab8[level-1].code,
667 (LInt)coeff_tab8[level-1].len);
670 else if (run > 0 && run < 7 && level < 4)
672 length = coeff_tab9[run-1][level-1].len;
673 if (length != 0)
675 /* boon 120697 */
676 BitstreamPutBits(bitstream, 3L, 7L);
677 /* boon 120697 */
678 BitstreamPutBits(bitstream, 2L, 2L);
679 length += 9; /* boon */
680 BitstreamPutBits(bitstream, (LInt)coeff_tab9[run-1][level-1].code,
681 (LInt)coeff_tab9[run-1][level-1].len);
684 else if (run > 6 && run < 21 && level == 1)
686 length = coeff_tab10[run-7].len;
687 if (length != 0)
689 /* boon 120697 */
690 BitstreamPutBits(bitstream, 3L, 7L);
691 /* boon 120697 */
692 BitstreamPutBits(bitstream, 2L, 2L);
693 length += 9; /* boon */
694 BitstreamPutBits(bitstream, (LInt)coeff_tab10[run-7].code,
695 (LInt)coeff_tab10[run-7].len);
699 return length;
704 PutLevelCoeff_Inter(Int run, Int level, Int last, Image *bitstream)
706 Int length = 0;
708 MOMCHECK (last >= 0 && last < 2);
709 MOMCHECK (run >= 0 && run < 64);
710 MOMCHECK (level > 0 && level < 128);
712 if (last == 0)
714 if (run < 2 && level < 13 )
716 length = coeff_tab0[run][level-1].len;
717 if (length != 0)
719 BitstreamPutBits(bitstream, 3L, 7L);
720 /* boon19970701 */
721 BitstreamPutBits(bitstream, 0L, 1L);
722 length += 8; /* boon */
723 BitstreamPutBits (bitstream, (LInt)coeff_tab0[run][level-1].code,
724 (LInt)coeff_tab0[run][level-1].len);
727 else if (run > 1 && run < 27 && level < 5)
729 length = coeff_tab1[run-2][level-1].len;
730 if (length != 0)
732 BitstreamPutBits(bitstream, 3L, 7L);
733 /* boon19970701 */
734 BitstreamPutBits(bitstream, 0L, 1L);
735 length += 8; /* boon */
736 BitstreamPutBits (bitstream, (LInt)coeff_tab1[run-2][level-1].code,
737 (LInt)coeff_tab1[run-2][level-1].len);
741 else if (last == 1)
743 if (run < 2 && level < 4)
745 length = coeff_tab2[run][level-1].len;
746 if (length != 0)
748 BitstreamPutBits(bitstream, 3L, 7L);
749 /* boon19970701 */
750 BitstreamPutBits(bitstream, 0L, 1L);
751 length += 8; /* boon */
752 BitstreamPutBits (bitstream, (LInt)coeff_tab2[run][level-1].code,
753 (LInt)coeff_tab2[run][level-1].len);
756 else if (run > 1 && run < 42 && level == 1)
758 length = coeff_tab3[run-2].len;
759 if (length != 0)
761 BitstreamPutBits(bitstream, 3L, 7L);
762 /* boon19970701 */
763 BitstreamPutBits(bitstream, 0L, 1L);
764 length += 8; /* boon */
765 BitstreamPutBits (bitstream, (LInt)coeff_tab3[run-2].code,
766 (LInt)coeff_tab3[run-2].len);
770 return length;
775 PutLevelCoeff_Intra(Int run, Int level, Int last, Image *bitstream)
777 Int length = 0;
779 MOMCHECK (last >= 0 && last < 2);
780 MOMCHECK (run >= 0 && run < 64);
781 MOMCHECK (level > 0 && level < 128);
783 if (last == 0)
785 if (run == 0 && level < 28 )
787 length = coeff_tab4[level-1].len;
788 if (length != 0)
790 BitstreamPutBits(bitstream, 3L, 7L);
791 /* boon19970701 */
792 BitstreamPutBits(bitstream, 0L, 1L);
793 length += 8; /* boon */
794 BitstreamPutBits(bitstream, (LInt)coeff_tab4[level-1].code,
795 (LInt)coeff_tab4[level-1].len);
798 else if (run == 1 && level < 11)
800 length = coeff_tab5[level-1].len;
801 if (length != 0)
803 BitstreamPutBits(bitstream, 3L, 7L);
804 /* boon19970701 */
805 BitstreamPutBits(bitstream, 0L, 1L);
806 length += 8; /* boon */
807 BitstreamPutBits(bitstream, (LInt)coeff_tab5[level-1].code,
808 (LInt)coeff_tab5[level-1].len);
811 else if (run > 1 && run < 10 && level < 6)
813 length = coeff_tab6[run-2][level-1].len;
814 if (length != 0)
816 BitstreamPutBits(bitstream, 3L, 7L);
817 /* boon19970701 */
818 BitstreamPutBits(bitstream, 0L, 1L);
819 length += 8; /* boon */
820 BitstreamPutBits(bitstream, (LInt)coeff_tab6[run-2][level-1].code,
821 (LInt)coeff_tab6[run-2][level-1].len);
824 else if (run > 9 && run < 15 && level == 1)
826 length = coeff_tab7[run-10].len;
827 if (length != 0)
829 BitstreamPutBits(bitstream, 3L, 7L);
830 /* boon19970701 */
831 BitstreamPutBits(bitstream, 0L, 1L);
832 length += 8; /* boon */
833 BitstreamPutBits(bitstream, (LInt)coeff_tab7[run-10].code,
834 (LInt)coeff_tab7[run-10].len);
838 else if (last == 1)
840 if (run == 0 && level < 9)
842 length = coeff_tab8[level-1].len;
843 if (length != 0)
845 BitstreamPutBits(bitstream, 3L, 7L);
846 /* boon19970701 */
847 BitstreamPutBits(bitstream, 0L, 1L);
848 length += 8; /* boon */
849 BitstreamPutBits(bitstream, (LInt)coeff_tab8[level-1].code,
850 (LInt)coeff_tab8[level-1].len);
853 else if (run > 0 && run < 7 && level < 4)
855 length = coeff_tab9[run-1][level-1].len;
856 if (length != 0)
858 BitstreamPutBits(bitstream, 3L, 7L);
859 /* boon19970701 */
860 BitstreamPutBits(bitstream, 0L, 1L);
861 length += 8; /* boon */
862 BitstreamPutBits(bitstream, (LInt)coeff_tab9[run-1][level-1].code,
863 (LInt)coeff_tab9[run-1][level-1].len);
866 else if (run > 6 && run < 21 && level == 1)
868 length = coeff_tab10[run-7].len;
869 if (length != 0)
871 BitstreamPutBits(bitstream, 3L, 7L);
872 /* boon19970701 */
873 BitstreamPutBits(bitstream, 0L, 1L);
874 length += 8; /* boon */
875 BitstreamPutBits(bitstream, (LInt)coeff_tab10[run-7].code,
876 (LInt)coeff_tab10[run-7].len);
880 return length;