2 /**************************************************************************
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. *
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. *
19 * The complete Open DivX license can be found at *
20 * http://www.projectmayo.com/opendivx/license.php . *
22 **************************************************************************/
24 /**************************************************************************
28 * Copyright (C) 2001 Project Mayo
32 * DivX Advance Research Center <darc@projectmayo.com>
34 **************************************************************************/
36 /* This file contains some utility functions to write to bitstreams for */
37 /* texture part of the coding. */
38 /* Some codes of this project come from MoMuSys MPEG-4 implementation. */
39 /* Please see seperate acknowledgement file for a list of contributors. */
42 #include "text_defs.h"
43 #include "bitstream.h"
44 #include "text_bits.h"
46 #include "zigzag.h" /* added, 14-NOV-1996 MW */
47 #include "max_level.h" /* 3-mode esc */
49 Int
IntraDC_dpcm (Int val
, Int lum
, Image
*bitstream
);
50 Int
CodeCoeff (Int j_start
, Int Mode
, Int qcoeff
[],
51 Int block
, Int ncoeffs
, Image
*bitstream
);
52 Int
CodeCoeff_RVLC (Int j_start
, Int Mode
, Int qcoeff
[],
53 Int block
, Int ncoeffs
, Image
*bitstream
);
55 /***********************************************************CommentBegin******
57 * -- MB_CodeCoeff -- Codes coefficients, does DC/AC prediction
60 * Codes coefficients, does DC/AC prediction
63 * Int *qcoeff : quantized dct-coefficients
64 * Int Mode : encoding mode
65 * Int CBP : coded block pattern
66 * Int ncoeffs : number of coefficients per block
67 * Int x_pos : the horizontal position of the macroblock in the vop
68 * Int intra_dcpred_disable : disable the intradc prediction
69 * Int transp_pattern[]: Describes which blocks are transparent
72 * Bits *bits : struct to count the number of texture bits
73 * Image *bitstream : output bitstream
76 * The intradc prediction can be switched off by setting the variable
77 * intradc_pred_disable to '1'.
79 ***********************************************************CommentEnd********/
80 Void
MB_CodeCoeff(Bits
* bits
, Int
*qcoeff
,
81 Int Mode
, Int CBP
, Int ncoeffs
,
82 Int intra_dcpred_disable
,
85 Int transp_pattern
[], Int direction
[],
86 Int error_res_disable
,
92 Int
*zz
= alternate_scan
? zigzag_v
: zigzag
;
94 if (Mode
== MODE_INTRA
|| Mode
== MODE_INTRA_Q
)
96 if (intra_dcpred_disable
== 0)
98 for (i
= 0; i
< 6; i
++)
100 // if (i>3 || transp_pattern[i]!=1) /* Not transparent */
104 switch (direction
[i
])
106 case 1: zz
= zigzag_v
; break;
107 case 2: zz
= zigzag_h
; break;
109 default: fprintf(stderr
, "MB_CodeCoeff(): Error in zigzag direction\n");
113 /* Do the zigzag scanning of coefficients */
114 for (m
= 0; m
< 64; m
++)
116 *(coeff
+ zz
[m
]) = qcoeff
[i
*ncoeffs
+m
];
121 if (error_res_disable
)
124 bits
->Y
+= IntraDC_dpcm(coeff
[0],1,bitstream
);
126 bits
->C
+= IntraDC_dpcm(coeff
[0],0,bitstream
);
131 bits
->Y
+= IntraDC_dpcm(coeff
[0],1,DCbitstream
);
133 bits
->C
+= IntraDC_dpcm(coeff
[0],0,DCbitstream
);
137 /* Code AC coeffs. dep. on block pattern MW 15-NOV-1996 */
138 if ((i
==0 && CBP
&32) ||
145 if (error_res_disable
|| ((!error_res_disable
) && (!reverse_vlc
)))
148 bits
->Y
+= CodeCoeff(1-switched
,Mode
, coeff
,i
,ncoeffs
,bitstream
);
150 bits
->C
+= CodeCoeff(1-switched
,Mode
, coeff
,i
,ncoeffs
,
156 bits
->Y
+= CodeCoeff_RVLC(1-switched
,Mode
, coeff
, i
,
159 bits
->C
+= CodeCoeff_RVLC(1-switched
,Mode
, coeff
, i
,
166 else /* Without ACDC prediction */
168 for (i
= 0; i
< 6; i
++)
170 // if (i>3 || transp_pattern[i]!=1) /* Not transparent */
172 /* Do the zigzag scanning of coefficients */
173 for (m
= 0; m
< 64; m
++)
174 *(coeff
+ zz
[m
]) = qcoeff
[i
*ncoeffs
+m
];
178 if (error_res_disable
)
181 BitstreamPutBits(bitstream
,(long)(coeff
[0]),8L);
183 BitstreamPutBits(bitstream
, 255L, 8L);
188 BitstreamPutBits(DCbitstream
,(long)(coeff
[0]),8L);
190 BitstreamPutBits(DCbitstream
,255L, 8L);
199 if ((i
==0 && CBP
&32) || (i
==1 && CBP
&16) ||
200 (i
==2 && CBP
&8) || (i
==3 && CBP
&4) ||
201 (i
==4 && CBP
&2) || (i
==5 && CBP
&1))
203 /* send coeff, not qcoeff !!! MW 07-11-96 */
205 if (error_res_disable
|| ((!error_res_disable
) && (!reverse_vlc
)))
208 bits
->Y
+= CodeCoeff(1-switched
,Mode
, coeff
,i
,ncoeffs
,
211 bits
->C
+= CodeCoeff(1-switched
,Mode
, coeff
,i
,ncoeffs
,
217 bits
->Y
+= CodeCoeff_RVLC(1-switched
,Mode
, coeff
, i
,
220 bits
->C
+= CodeCoeff_RVLC(1-switched
,Mode
, coeff
, i
,
229 else /* inter block encoding */
231 for (i
= 0; i
< 6; i
++)
233 /* Do the zigzag scanning of coefficients */
234 for (m
= 0; m
< 64; m
++)
235 *(coeff
+ zz
[m
]) = qcoeff
[i
*ncoeffs
+m
];
236 if ((i
==0 && CBP
&32) ||
243 if (error_res_disable
|| ((!error_res_disable
) && (!reverse_vlc
)))
246 bits
->Y
+= CodeCoeff(0,Mode
, coeff
, i
, ncoeffs
, bitstream
);
248 bits
->C
+= CodeCoeff(0,Mode
, coeff
, i
, ncoeffs
, bitstream
);
253 bits
->Y
+= CodeCoeff_RVLC(0,Mode
, coeff
, i
, ncoeffs
,
256 bits
->C
+= CodeCoeff_RVLC(0,Mode
, coeff
, i
, ncoeffs
,
266 /***********************************************************CommentBegin******
268 * -- IntraDC_dpcm -- DPCM Encoding of INTRADC in case of Intra macroblocks
271 * DPCM Encoding of INTRADC in case of Intra macroblocks
274 * Int val : the difference value with respect to the previous
276 * Int lum : indicates whether the block is a luminance block (lum=1) or
277 * a chrominance block ( lum = 0)
280 * Image* bitstream : a pointer to the output bitstream
282 ***********************************************************CommentEnd********/
285 IntraDC_dpcm(Int val
, Int lum
, Image
*bitstream
)
288 Int absval
, size
= 0;
290 absval
= ( val
<0)?-val
:val
; /* abs(val) */
292 /* compute dct_dc_size */
303 n_bits
= PutDCsize_lum (size
, bitstream
);
307 n_bits
= PutDCsize_chrom (size
, bitstream
);
318 absval
= -val
; /* set to "-val" MW 14-NOV-1996 */
319 val
= (absval
^( (int)pow(2.0,(double)size
)-1) );
321 BitstreamPutBits(bitstream
, (long)(val
), (long)(size
));
325 BitstreamPutBits(bitstream
, (long)1, (long)1);
328 return n_bits
; /* # bits for intra_dc dpcm */
333 /***********************************************************CommentBegin******
335 * -- CodeCoeff -- VLC encoding of quantized DCT coefficients
338 * VLC encoding of quantized DCT coefficients, except for
339 * INTRADC in case of Intra macroblocks
340 * Used by Bits_CountCoeff
343 * Int Mode : encoding mode
344 * Int *qcoeff: pointer to quantized dct coefficients
345 * Int block : number of the block in the macroblock (0-5)
346 * Int ncoeffs : the number of coefficients per block
349 * Image *bitstream : pointer to the output bitstream
352 * Int bits : number of bits added to the bitstream (?)
354 ***********************************************************CommentEnd********/
356 Int
CodeCoeff(Int j_start
, Int Mode
, Int qcoeff
[], Int block
, Int ncoeffs
, Image
*bitstream
)
359 Int prev_run
, run
, prev_level
, level
, first
;
360 Int prev_ind
, ind
, prev_s
, s
, length
;
364 prev_run
= prev_level
= prev_ind
= level
= s
= prev_s
= ind
= 0;
366 for (j
= j_start
; j
< ncoeffs
; j
++)
368 /* The INTRADC encoding for INTRA Macroblocks is done in
372 /* do not enter this part for INTRADC coding for INTRA macro-
375 /* encode AC coeff */
379 /* Increment run if coeff is zero */
381 if ((level
= qcoeff
[j
]) == 0)
387 /* code run & level and count bits */
395 ind
= level
| run
<<4;
396 ind
= ind
| 0<<12; /* Not last coeff */
400 /* Encode the previous ind */
402 if ((prev_run
< 64) &&
403 (((prev_level
< 13) && (Mode
!= MODE_INTRA
&&
404 Mode
!= MODE_INTRA_Q
))
405 || ((prev_level
< 28) && (Mode
== MODE_INTRA
||
406 Mode
== MODE_INTRA_Q
))))
408 /* Separate tables for Intra luminance blocks */
409 if (Mode
== MODE_INTRA
|| Mode
== MODE_INTRA_Q
)
411 length
= PutCoeff_Intra(prev_run
, prev_level
,
416 length
= PutCoeff_Inter(prev_run
, prev_level
,
423 /* First escape mode. Level offset */
430 /* subtraction of Max level, last = 0 */
433 if (Mode
== MODE_INTRA
|| Mode
== MODE_INTRA_Q
)
434 level_minus_max
= prev_level
-
435 intra_max_level
[0][prev_run
];
437 level_minus_max
= prev_level
-
438 inter_max_level
[0][prev_run
];
440 if ( ( (level_minus_max
< 13) && (Mode
!= MODE_INTRA
&& Mode
!= MODE_INTRA_Q
) ) ||
441 ( (level_minus_max
< 28) && (Mode
== MODE_INTRA
|| Mode
== MODE_INTRA_Q
) ) )
443 /* Separate tables for Intra luminance blocks */
444 if (Mode
== MODE_INTRA
|| Mode
== MODE_INTRA_Q
)
446 length
= PutLevelCoeff_Intra(prev_run
, level_minus_max
, 0, bitstream
);
450 length
= PutLevelCoeff_Inter(prev_run
, level_minus_max
, 0, bitstream
);
458 /* Second escape mode. Run offset */
461 if ( ((prev_level
< 13) && (Mode
!= MODE_INTRA
&& Mode
!= MODE_INTRA_Q
)) ||
462 ((prev_level
< 28) && (Mode
== MODE_INTRA
|| Mode
== MODE_INTRA_Q
)) )
465 /* subtraction of Max Run, last = 0 */
470 fprintf (stdout
, "ERROR(CodeCoeff-second esc): level is %d\n", prev_level
);
474 if (Mode
== MODE_INTRA
|| Mode
== MODE_INTRA_Q
)
475 run_minus_max
= prev_run
- (intra_max_run0
[prev_level
]+1);
477 run_minus_max
= prev_run
- (inter_max_run0
[prev_level
]+1);
480 if (run_minus_max
< 64)
482 /* Separate tables for Intra luminance blocks */
483 if (Mode
== MODE_INTRA
|| Mode
== MODE_INTRA_Q
)
485 length
= PutRunCoeff_Intra(run_minus_max
, prev_level
, 0, bitstream
);
489 length
= PutRunCoeff_Inter(run_minus_max
, prev_level
, 0, bitstream
);
497 /* Third escape mode. FLC */
499 { /* Escape coding */
503 /* Modified due to N2171 Cl. 2.2.14 MW 25-MAR-1998 */
504 /* prev_level = (prev_level^0xff)+1; */
505 prev_level
= (prev_level
^0xfff)+1;
507 BitstreamPutBits(bitstream
, 3L, 7L);
509 BitstreamPutBits(bitstream
, 3L, 2L);
512 BitstreamPutBits(bitstream
, 0L, 1L);
514 BitstreamPutBits(bitstream
, (long)(prev_run
), 6L);
516 /* 11.08.98 Sven Brandau: "insert marker_bit" due to N2339, Clause 2.1.21 */
517 BitstreamPutBits(bitstream
, MARKER_BIT
, 1);
519 /* Modified due to N2171 Cl. 2.2.14 MW 25-MAR-1998 */
520 /* BitstreamPutBits(bitstream, (long)(prev_level), 8L); */
523 BitstreamPutBits(bitstream
, (long)(prev_level
), 12L);
525 /* 11.08.98 Sven Brandau: "insert marker_bit" due to N2339, Clause 2.1.21 */
526 BitstreamPutBits(bitstream
, MARKER_BIT
, 1);
533 BitstreamPutBits(bitstream
, (long)(prev_s
), 1L);
538 prev_run
= run
; prev_s
= s
;
539 prev_level
= level
; prev_ind
= ind
;
546 /* Encode the last coeff */
550 prev_ind
= prev_ind
| 1<<12; /* last coeff */
552 if ((prev_run
< 64) &&
553 (((prev_level
< 4) && (Mode
!= MODE_INTRA
&&
554 Mode
!= MODE_INTRA_Q
))
555 || ((prev_level
< 9) && ((Mode
== MODE_INTRA
) ||
556 (Mode
== MODE_INTRA_Q
)))))
558 /* Separate tables for Intra luminance blocks */
559 if (Mode
== MODE_INTRA
|| Mode
== MODE_INTRA_Q
)
561 length
= PutCoeff_Intra(prev_run
, prev_level
, 1,
566 length
= PutCoeff_Inter(prev_run
, prev_level
, 1,
573 /* First escape mode. Level offset */
579 /* subtraction of Max level, last = 0 */
582 if (Mode
== MODE_INTRA
|| Mode
== MODE_INTRA_Q
)
583 level_minus_max
= prev_level
- intra_max_level
[1][prev_run
];
585 level_minus_max
= prev_level
- inter_max_level
[1][prev_run
];
587 if ( ( (level_minus_max
< 4) && (Mode
!= MODE_INTRA
&& Mode
!= MODE_INTRA_Q
) ) ||
588 ( (level_minus_max
< 9) && (Mode
== MODE_INTRA
|| Mode
== MODE_INTRA_Q
) ) )
590 /* Separate tables for Intra luminance blocks */
591 if (Mode
== MODE_INTRA
|| Mode
== MODE_INTRA_Q
)
593 length
= PutLevelCoeff_Intra(prev_run
, level_minus_max
, 1, bitstream
);
597 length
= PutLevelCoeff_Inter(prev_run
, level_minus_max
, 1, bitstream
);
605 /* Second escape mode. Run offset */
608 if (((prev_level
< 4) && (Mode
!= MODE_INTRA
&& Mode
!= MODE_INTRA_Q
))||
609 ((prev_level
< 9) && (Mode
== MODE_INTRA
|| Mode
== MODE_INTRA_Q
)) )
612 /* subtraction of Max Run, last = 1 */
617 fprintf (stdout
, "ERROR(CodeCoeff-second esc): level is %d\n", prev_level
);
621 if (Mode
== MODE_INTRA
|| Mode
== MODE_INTRA_Q
)
622 run_minus_max
= prev_run
- (intra_max_run1
[prev_level
]+1);
624 run_minus_max
= prev_run
- (inter_max_run1
[prev_level
]+1);
626 if (run_minus_max
< 64) /* boon 120697 */
628 /* Separate tables for Intra luminance blocks */
629 if (Mode
== MODE_INTRA
|| Mode
== MODE_INTRA_Q
)
631 length
= PutRunCoeff_Intra(run_minus_max
, prev_level
, 1, bitstream
);
635 length
= PutRunCoeff_Inter(run_minus_max
, prev_level
, 1, bitstream
);
643 /* Third escape mode. FLC */
645 { /* Escape coding */
649 /* Modified due to N2171 Cl. 2.2.14 MW 25-MAR-1998 */
650 /* prev_level = (prev_level^0xff)+1; */
651 prev_level
= (prev_level
^0xfff)+1;
653 BitstreamPutBits(bitstream
, 3L, 7L);
654 BitstreamPutBits(bitstream
, 3L, 2L); /* boon */
656 BitstreamPutBits(bitstream
, 1L, 1L); /* last */
657 BitstreamPutBits(bitstream
, (long)(prev_run
), 6L);
659 /* 11.08.98 Sven Brandau: "insert marker_bit" due to N2339, Clause 2.1.21 */
660 BitstreamPutBits(bitstream
, MARKER_BIT
, 1);
662 /* Modified due to N2171 Cl. 2.2.14 MW 25-MAR-1998 */
663 /* BitstreamPutBits(bitstream, (long)(prev_level), 8L); */
666 BitstreamPutBits(bitstream
, (long)(prev_level
), 12L);
668 /* 11.08.98 Sven Brandau: "insert marker_bit" due to N2339, Clause 2.1.21 */
669 BitstreamPutBits(bitstream
, MARKER_BIT
, 1);
677 BitstreamPutBits(bitstream
, (long)(prev_s
), 1L);
686 /***********************************************************CommentBegin******
688 * -- CodeCoeff_RVLC -- RVLC encoding of quantized DCT coefficients
691 * RVLC encoding of quantized DCT coefficients, except for
692 * INTRADC in case of Intra macroblocks
695 * Int Mode : encoding mode
696 * Int *qcoeff: pointer to quantized dct coefficients
697 * Int block : number of the block in the macroblock (0-5)
698 * Int ncoeffs : the number of coefficients per block
701 * Image *bitstream : pointer to the output bitstream
704 * Int bits : number of bits added to the bitstream (?)
706 ***********************************************************CommentEnd********/
708 Int
CodeCoeff_RVLC(Int j_start
, Int Mode
, Int qcoeff
[], Int block
, Int ncoeffs
, Image
*bitstream
)
711 Int prev_run
, run
, prev_level
, level
, first
;
712 Int prev_ind
, ind
, prev_s
, s
, length
;
716 prev_run
= prev_level
= prev_ind
= level
= s
= prev_s
= ind
= 0;
718 for (j
= j_start
; j
< ncoeffs
; j
++)
720 /* The INTRADC encoding for INTRA Macroblocks is done in
724 /* do not enter this part for INTRADC coding for INTRA macro-
727 /* encode AC coeff */
731 /* Increment run if coeff is zero */
733 if ((level
= qcoeff
[j
]) == 0)
739 /* code run & level and count bits */
747 ind
= level
| run
<<4;
748 ind
= ind
| 0<<12; /* Not last coeff */
751 /* Encode the previous ind */
753 if (prev_level
< 28 && prev_run
< 39)
754 /* Separate tables for Intra luminance blocks */
755 if (Mode
== MODE_INTRA
|| Mode
== MODE_INTRA_Q
)
757 length
= PutCoeff_Intra_RVLC(prev_run
, prev_level
, 0,
762 length
= PutCoeff_Inter_RVLC(prev_run
, prev_level
, 0,
769 { /* Escape coding */
772 BitstreamPutBits(bitstream
, 1L, 5L);
775 BitstreamPutBits(bitstream
, 0L, 1L);
777 BitstreamPutBits(bitstream
,
778 (long)(prev_run
), 6L);/* RUN */
780 /* 11.08.98 Sven Brandau: "changed length for LEVEL (11 bit)" due to N2339, Clause 2.1.21 */
781 /* 11.08.98 Sven Brandau: "insert marker_bit befor and after LEVEL" due to N2339, Clause 2.1.21 */
782 /* BitstreamPutBits(bitstream,
783 (long)(prev_level), 7L);*/
785 BitstreamPutBits( bitstream
, MARKER_BIT
, 1 );
787 BitstreamPutBits( bitstream
, (long)(prev_level
), 11L);
788 BitstreamPutBits( bitstream
, MARKER_BIT
, 1 );
791 BitstreamPutBits(bitstream
, 0L, 4L);
794 BitstreamPutBits(bitstream
,
797 bits
+= 5 + 1 + 6 + 1 + 11 + 1 + 4 + 1;
802 BitstreamPutBits(bitstream
,
808 prev_run
= run
; prev_s
= s
;
809 prev_level
= level
; prev_ind
= ind
;
816 /* Encode the last coeff */
820 prev_ind
= prev_ind
| 1<<12; /* last coeff */
822 if (prev_level
< 5 && prev_run
< 45)
823 /* Separate tables for Intra luminance blocks */
824 if (Mode
== MODE_INTRA
|| Mode
== MODE_INTRA_Q
)
826 length
= PutCoeff_Intra_RVLC(prev_run
, prev_level
, 1,
831 length
= PutCoeff_Inter_RVLC(prev_run
, prev_level
, 1,
838 { /* Escape coding */
840 BitstreamPutBits(bitstream
, 1L, 5L); /* ESCAPE */
842 BitstreamPutBits(bitstream
, 1L, 1L); /* LAST */
845 BitstreamPutBits(bitstream
, (long)(prev_run
), 6L);
847 /* 11.08.98 Sven Brandau: "changed length for LEVEL (11 bit)" due to N2339, Clause 2.1.21 */
848 /* 11.08.98 Sven Brandau: "insert marker_bit befor and after LEVEL" due to N2339, Clause 2.1.21 */
849 /* BitstreamPutBits(bitstream, (long)(prev_level), 7L);*/
850 BitstreamPutBits( bitstream
, MARKER_BIT
, 1 );
852 BitstreamPutBits( bitstream
, (long)(prev_level
), 11L);
853 BitstreamPutBits( bitstream
, MARKER_BIT
, 1 );
855 BitstreamPutBits(bitstream
, 0L, 4L); /* ESCAPE */
858 BitstreamPutBits(bitstream
, (long)(prev_s
), 1L);
865 BitstreamPutBits(bitstream
, (long)(prev_s
), 1L);
874 /***********************************************************CommentBegin******
876 * -- Bits_Reset -- To reset the structure bits
879 * To reset the structure bits, used for counting the number
883 * Bits* bits : a pointer to the struct Bits
885 ***********************************************************CommentEnd********/
888 Bits_Reset (Bits
*bits
)
890 memset(bits
, 0, sizeof(Bits
));