1
2 /*!
3 ***************************************************************************
4 * \file vlc.c
5 *
6 * \brief
7 * (CA)VLC coding functions
8 *
9 * \author
10 * Main contributors (see contributors.h for copyright, address and affiliation details)
11 * - Inge Lille-Langoy <inge.lille-langoy@telenor.com>
12 * - Detlev Marpe
13 * - Stephan Wenger <stewe@cs.tu-berlin.de>
14 ***************************************************************************
15 */
16
17 #include "contributors.h"
18
19 #include <math.h>
20
21 #include "global.h"
22 #include "enc_statistics.h"
23 #include "vlc.h"
24
25 #if TRACE
26 #define SYMTRACESTRING(s) strncpy(sym.tracestring,s,TRACESTRING_SIZE)
27 #else
28 #define SYMTRACESTRING(s) // do nothing
29 #endif
30
31 //! gives codeword number from CBP value, both for intra and inter
32 static const byte NCBP[2][48][2]=
33 {
34 { // 0 1 2 3 4 5 6 7 8 9 10 11
35 { 1, 0},{10, 1},{11, 2},{ 6, 5},{12, 3},{ 7, 6},{14,14},{ 2,10},{13, 4},{15,15},{ 8, 7},{ 3,11},
36 { 9, 8},{ 4,12},{ 5,13},{ 0, 9},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},
37 { 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},
38 { 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0}
39 },
40 {
41 { 3, 0},{29, 2},{30, 3},{17, 7},{31, 4},{18, 8},{37,17},{ 8,13},{32, 5},{38,18},{19, 9},{ 9,14},
42 {20,10},{10,15},{11,16},{ 2,11},{16, 1},{33,32},{34,33},{21,36},{35,34},{22,37},{39,44},{ 4,40},
43 {36,35},{40,45},{23,38},{ 5,41},{24,39},{ 6,42},{ 7,43},{ 1,19},{41, 6},{42,24},{43,25},{25,20},
44 {44,26},{26,21},{46,46},{12,28},{45,27},{47,47},{27,22},{13,29},{28,23},{14,30},{15,31},{ 0,12}
45 }
46 };
47
48
49 /*!
50 *************************************************************************************
51 * \brief
52 * write_ue_v, writes an ue(v) syntax element, returns the length in bits
53 *
54 * \param tracestring
55 * the string for the trace file
56 * \param value
57 * the value to be coded
58 * \param bitstream
59 * the target bitstream the value should be coded into
60 *
61 * \return
62 * Number of bits used by the coded syntax element
63 *
64 * \ note
65 * This function writes always the bit buffer for the progressive scan flag, and
66 * should not be used (or should be modified appropriately) for the interlace crap
67 * When used in the context of the Parameter Sets, this is obviously not a
68 * problem.
69 *
70 *************************************************************************************
71 */
write_ue_v(char * tracestring,int value,Bitstream * bitstream)72 int write_ue_v (char *tracestring, int value, Bitstream *bitstream)
73 {
74 SyntaxElement symbol, *sym=&symbol;
75 sym->value1 = value;
76 sym->value2 = 0;
77
78 //assert (bitstream->streamBuffer != NULL);
79
80 ue_linfo(sym->value1,sym->value2,&(sym->len),&(sym->inf));
81 symbol2uvlc(sym);
82
83 writeUVLC2buffer (sym, bitstream);
84
85 #if TRACE
86 strncpy(sym->tracestring,tracestring,TRACESTRING_SIZE);
87 trace2out (sym);
88 #endif
89
90 return (sym->len);
91 }
92
93
94 /*!
95 *************************************************************************************
96 * \brief
97 * write_se_v, writes an se(v) syntax element, returns the length in bits
98 *
99 * \param tracestring
100 * the string for the trace file
101 * \param value
102 * the value to be coded
103 * \param bitstream
104 * the target bitstream the value should be coded into
105 *
106 * \return
107 * Number of bits used by the coded syntax element
108 *
109 * \ note
110 * This function writes always the bit buffer for the progressive scan flag, and
111 * should not be used (or should be modified appropriately) for the interlace crap
112 * When used in the context of the Parameter Sets, this is obviously not a
113 * problem.
114 *
115 *************************************************************************************
116 */
write_se_v(char * tracestring,int value,Bitstream * bitstream)117 int write_se_v (char *tracestring, int value, Bitstream *bitstream)
118 {
119 SyntaxElement symbol, *sym=&symbol;
120 sym->value1 = value;
121 sym->value2 = 0;
122
123 //assert (bitstream->streamBuffer != NULL);
124
125 se_linfo(sym->value1,sym->value2,&(sym->len),&(sym->inf));
126 symbol2uvlc(sym);
127
128 writeUVLC2buffer (sym, bitstream);
129
130 #if TRACE
131 strncpy(sym->tracestring,tracestring,TRACESTRING_SIZE);
132 trace2out (sym);
133 #endif
134
135 return (sym->len);
136 }
137
138
139 /*!
140 *************************************************************************************
141 * \brief
142 * write_u_1, writes a flag (u(1) syntax element, returns the length in bits,
143 * always 1
144 *
145 * \param tracestring
146 * the string for the trace file
147 * \param value
148 * the value to be coded
149 * \param bitstream
150 * the target bitstream the value should be coded into
151 *
152 * \return
153 * Number of bits used by the coded syntax element (always 1)
154 *
155 * \ note
156 * This function writes always the bit buffer for the progressive scan flag, and
157 * should not be used (or should be modified appropriately) for the interlace crap
158 * When used in the context of the Parameter Sets, this is obviously not a
159 * problem.
160 *
161 *************************************************************************************
162 */
write_u_1(char * tracestring,int value,Bitstream * bitstream)163 Boolean write_u_1 (char *tracestring, int value, Bitstream *bitstream)
164 {
165 SyntaxElement symbol, *sym=&symbol;
166
167 sym->bitpattern = value;
168 sym->value1 = value;
169 sym->len = 1;
170
171 //assert (bitstream->streamBuffer != NULL);
172
173 writeUVLC2buffer(sym, bitstream);
174
175 #if TRACE
176 strncpy(sym->tracestring,tracestring,TRACESTRING_SIZE);
177 trace2out (sym);
178 #endif
179
180 return ((Boolean) sym->len);
181 }
182
183
184 /*!
185 *************************************************************************************
186 * \brief
187 * write_u_v, writes a n bit fixed length syntax element, returns the length in bits,
188 *
189 * \param n
190 * length in bits
191 * \param tracestring
192 * the string for the trace file
193 * \param value
194 * the value to be coded
195 * \param bitstream
196 * the target bitstream the value should be coded into
197 *
198 * \return
199 * Number of bits used by the coded syntax element
200 *
201 * \ note
202 * This function writes always the bit buffer for the progressive scan flag, and
203 * should not be used (or should be modified appropriately) for the interlace crap
204 * When used in the context of the Parameter Sets, this is obviously not a
205 * problem.
206 *
207 *************************************************************************************
208 */
209
write_u_v(int n,char * tracestring,int value,Bitstream * bitstream)210 int write_u_v (int n, char *tracestring, int value, Bitstream *bitstream)
211 {
212 SyntaxElement symbol, *sym=&symbol;
213
214 sym->bitpattern = value;
215 sym->value1 = value;
216 sym->len = n;
217
218 //assert (bitstream->streamBuffer != NULL);
219
220 writeUVLC2buffer(sym, bitstream);
221
222 #if TRACE
223 strncpy(sym->tracestring,tracestring,TRACESTRING_SIZE);
224 trace2out (sym);
225 #endif
226
227 return (sym->len);
228 }
229
230
231 /*!
232 ************************************************************************
233 * \brief
234 * mapping for ue(v) syntax elements
235 * \param ue
236 * value to be mapped
237 * \param dummy
238 * dummy parameter
239 * \param info
240 * returns mapped value
241 * \param len
242 * returns mapped value length
243 ************************************************************************
244 */
ue_linfo(int ue,int dummy,int * len,int * info)245 void ue_linfo(int ue, int dummy, int *len,int *info)
246 {
247 int i, nn =(ue + 1) >> 1;
248
249 for (i=0; i < 33 && nn != 0; i++)
250 {
251 nn >>= 1;
252 }
253 *len = (i << 1) + 1;
254 *info = ue + 1 - (1 << i);
255 }
256
257
258 /*!
259 ************************************************************************
260 * \brief
261 * mapping for se(v) syntax elements
262 * \param se
263 * value to be mapped
264 * \param dummy
265 * dummy parameter
266 * \param len
267 * returns mapped value length
268 * \param info
269 * returns mapped value
270 ************************************************************************
271 */
se_linfo(int se,int dummy,int * len,int * info)272 void se_linfo(int se, int dummy, int *len,int *info)
273 {
274 int sign = (se <= 0) ? 1 : 0;
275 int n = iabs(se) << 1; // n+1 is the number in the code table. Based on this we find length and info
276 int nn = (n >> 1);
277 int i;
278 for (i = 0; i < 33 && nn != 0; i++)
279 {
280 nn >>= 1;
281 }
282 *len = (i << 1) + 1;
283 *info = n - (1 << i) + sign;
284 }
285
286
287 /*!
288 ************************************************************************
289 * \par Input:
290 * Number in the code table
291 * \par Output:
292 * length and info
293 ************************************************************************
294 */
cbp_linfo_intra_other(int cbp,int dummy,int * len,int * info)295 void cbp_linfo_intra_other(int cbp, int dummy, int *len,int *info)
296 {
297 ue_linfo(NCBP[0][cbp][0], dummy, len, info);
298 }
299
300
301 /*!
302 ************************************************************************
303 * \par Input:
304 * Number in the code table
305 * \par Output:
306 * length and info
307 ************************************************************************
308 */
cbp_linfo_intra_normal(int cbp,int dummy,int * len,int * info)309 void cbp_linfo_intra_normal(int cbp, int dummy, int *len,int *info)
310 {
311 ue_linfo(NCBP[1][cbp][0], dummy, len, info);
312 }
313
314 /*!
315 ************************************************************************
316 * \par Input:
317 * Number in the code table
318 * \par Output:
319 * length and info
320 ************************************************************************
321 */
cbp_linfo_inter_other(int cbp,int dummy,int * len,int * info)322 void cbp_linfo_inter_other(int cbp, int dummy, int *len,int *info)
323 {
324 ue_linfo(NCBP[0][cbp][1], dummy, len, info);
325 }
326
327 /*!
328 ************************************************************************
329 * \par Input:
330 * Number in the code table
331 * \par Output:
332 * length and info
333 ************************************************************************
334 */
cbp_linfo_inter_normal(int cbp,int dummy,int * len,int * info)335 void cbp_linfo_inter_normal(int cbp, int dummy, int *len,int *info)
336 {
337 ue_linfo(NCBP[1][cbp][1], dummy, len, info);
338 }
339
340 /*!
341 ************************************************************************
342 * \brief
343 * 2x2 transform of chroma DC
344 * \par Input:
345 * level and run for coefficients
346 * \par Output:
347 * length and info
348 * \note
349 * see ITU document for bit assignment
350 ************************************************************************
351 */
levrun_linfo_c2x2(int level,int run,int * len,int * info)352 void levrun_linfo_c2x2(int level,int run,int *len,int *info)
353 {
354 static const byte NTAB[2][2]=
355 {
356 {1,5},
357 {3,0}
358 };
359 static const byte LEVRUN[4]=
360 {
361 2,1,0,0
362 };
363
364 if (level == 0) // check if the coefficient sign EOB (level=0)
365 {
366 *len=1;
367 return;
368 }
369 else
370 {
371 int levabs = iabs(level);
372 int sign = level <= 0 ? 1 : 0;
373 int n = (levabs <= LEVRUN[run]) ? NTAB[levabs - 1][run] + 1 : (levabs - LEVRUN[run]) * 8 + run * 2;
374 int nn = n >> 1;
375 int i;
376
377 for (i=0; i < 16 && nn != 0; i++)
378 {
379 nn >>= 1;
380 }
381 *len = (i << 1) + 1;
382 *info = n - (1 << i) + sign;
383 }
384 }
385
386
387 /*!
388 ************************************************************************
389 * \brief
390 * Single scan coefficients
391 * \par Input:
392 * level and run for coefficients
393 * \par Output:
394 * length and info
395 * \note
396 * see ITU document for bit assignment
397 ************************************************************************
398 */
levrun_linfo_inter(int level,int run,int * len,int * info)399 void levrun_linfo_inter(int level,int run,int *len,int *info)
400 {
401 static const byte LEVRUN[16]=
402 {
403 4,2,2,1,1,1,1,1,1,1,0,0,0,0,0,0
404 };
405 static const byte NTAB[4][10]=
406 {
407 { 1, 3, 5, 9,11,13,21,23,25,27},
408 { 7,17,19, 0, 0, 0, 0, 0, 0, 0},
409 {15, 0, 0, 0, 0, 0, 0, 0, 0, 0},
410 {29, 0, 0, 0, 0, 0, 0, 0, 0, 0},
411 };
412
413 if (level == 0) // check for EOB
414 {
415 *len=1;
416 return;
417 }
418 else
419 {
420
421 int sign = (level <= 0) ? 1 : 0;
422 int levabs = iabs(level);
423
424 int n = (levabs <= LEVRUN[run]) ? NTAB[levabs - 1][run] + 1 : (levabs - LEVRUN[run]) * 32 + run * 2;
425
426 int nn = n >> 1;
427 int i;
428
429 for (i=0; i < 16 && nn != 0; i++)
430 {
431 nn >>= 1;
432 }
433 *len = (i << 1) + 1;
434 *info = n - (1 << i) + sign;
435 }
436 }
437
438
439 /*!
440 ************************************************************************
441 * \brief
442 * Makes code word and passes it back
443 * A code word has the following format: 0 0 0 ... 1 Xn ...X2 X1 X0.
444 *
445 * \par Input:
446 * Info : Xn..X2 X1 X0 \n
447 * Length : Total number of bits in the codeword
448 ************************************************************************
449 */
450 // NOTE this function is called with sym->inf > (1<<(sym->len >> 1)). The upper bits of inf are junk
symbol2uvlc(SyntaxElement * sym)451 int symbol2uvlc(SyntaxElement *sym)
452 {
453 int suffix_len = sym->len >> 1;
454 //assert (suffix_len < 32);
455 suffix_len = (1 << suffix_len);
456 sym->bitpattern = suffix_len | (sym->inf & (suffix_len - 1));
457 return 0;
458 }
459
460 /*!
461 ************************************************************************
462 * \brief
463 * generates UVLC code and passes the codeword to the buffer
464 ************************************************************************
465 */
writeSE_UVLC(SyntaxElement * se,DataPartition * dp)466 void writeSE_UVLC(SyntaxElement *se, DataPartition *dp)
467 {
468 ue_linfo (se->value1,se->value2,&(se->len),&(se->inf));
469 symbol2uvlc(se);
470
471 writeUVLC2buffer(se, dp->bitstream);
472
473 if(se->type != SE_HEADER)
474 dp->bitstream->write_flag = 1;
475
476 #if TRACE
477 if(dp->bitstream->trace_enabled)
478 trace2out (se);
479 #endif
480 }
481
482 /*!
483 ************************************************************************
484 * \brief
485 * generates UVLC code and passes the codeword to the buffer
486 ************************************************************************
487 */
writeSE_SVLC(SyntaxElement * se,DataPartition * dp)488 void writeSE_SVLC(SyntaxElement *se, DataPartition *dp)
489 {
490 se_linfo (se->value1,se->value2,&(se->len),&(se->inf));
491 symbol2uvlc(se);
492
493 writeUVLC2buffer(se, dp->bitstream);
494
495 if(se->type != SE_HEADER)
496 dp->bitstream->write_flag = 1;
497
498 #if TRACE
499 if(dp->bitstream->trace_enabled)
500 trace2out (se);
501 #endif
502 }
503
504 /*!
505 ************************************************************************
506 * \brief
507 * generates UVLC code and passes the codeword to the buffer
508 ************************************************************************
509 */
writeCBP_VLC(Macroblock * currMB,SyntaxElement * se,DataPartition * dp)510 void writeCBP_VLC (Macroblock* currMB, SyntaxElement *se, DataPartition *dp)
511 {
512 if (currMB->mb_type == I4MB || currMB->mb_type == SI4MB || currMB->mb_type == I8MB)
513 {
514 currMB->cbp_linfo_intra (se->value1, se->value2, &(se->len), &(se->inf));
515 }
516 else
517 {
518 currMB->cbp_linfo_inter (se->value1,se->value2,&(se->len),&(se->inf));
519 }
520 symbol2uvlc(se);
521
522 writeUVLC2buffer(se, dp->bitstream);
523
524 if(se->type != SE_HEADER)
525 dp->bitstream->write_flag = 1;
526
527 #if TRACE
528 if(dp->bitstream->trace_enabled)
529 trace2out (se);
530 #endif
531 }
532
533
534 /*!
535 ************************************************************************
536 * \brief
537 * generates code and passes the codeword to the buffer
538 ************************************************************************
539 */
writeIntraPredMode_CAVLC(SyntaxElement * se,DataPartition * dp)540 void writeIntraPredMode_CAVLC(SyntaxElement *se, DataPartition *dp)
541 {
542
543 if (se->value1 == -1)
544 {
545 se->len = 1;
546 se->inf = 1;
547 }
548 else
549 {
550 se->len = 4;
551 se->inf = se->value1;
552 }
553
554 se->bitpattern = se->inf;
555 writeUVLC2buffer(se, dp->bitstream);
556
557 if(se->type != SE_HEADER)
558 dp->bitstream->write_flag = 1;
559
560 #if TRACE
561 if(dp->bitstream->trace_enabled)
562 trace2out (se);
563 #endif
564
565 return;
566 }
567
568
569 /*!
570 ************************************************************************
571 * \brief
572 * generates UVLC code and passes the codeword to the buffer
573 * \author
574 * Tian Dong
575 ************************************************************************
576 */
writeSyntaxElement2Buf_UVLC(SyntaxElement * se,Bitstream * this_streamBuffer)577 int writeSyntaxElement2Buf_UVLC(SyntaxElement *se, Bitstream* this_streamBuffer )
578 {
579
580 se->mapping(se->value1,se->value2,&(se->len),&(se->inf));
581
582 symbol2uvlc(se);
583
584 writeUVLC2buffer(se, this_streamBuffer );
585
586 #if TRACE
587 if(se->type <= 1)
588 trace2out (se);
589 #endif
590
591 return (se->len);
592 }
593
594
595 /*!
596 ************************************************************************
597 * \brief
598 * writes UVLC code to the appropriate buffer
599 ************************************************************************
600 */
writeUVLC2buffer(SyntaxElement * se,Bitstream * currStream)601 void writeUVLC2buffer(SyntaxElement *se, Bitstream *currStream)
602 {
603 unsigned int mask = 1 << (se->len - 1);
604 byte *byte_buf = &currStream->byte_buf;
605 int *bits_to_go = &currStream->bits_to_go;
606 int i;
607
608 // Add the new bits to the bitstream.
609 // Write out a byte if it is full
610 if ( se->len < 33 )
611 {
612 for (i = 0; i < se->len; i++)
613 {
614 *byte_buf <<= 1;
615
616 if (se->bitpattern & mask)
617 *byte_buf |= 1;
618
619 mask >>= 1;
620
621 if ((--(*bits_to_go)) == 0)
622 {
623 *bits_to_go = 8;
624 currStream->streamBuffer[currStream->byte_pos++] = *byte_buf;
625 *byte_buf = 0;
626 }
627 }
628 }
629 else
630 {
631 // zeros
632 for (i = 0; i < (se->len - 32); i++)
633 {
634 *byte_buf <<= 1;
635
636 if ((--(*bits_to_go)) == 0)
637 {
638 *bits_to_go = 8;
639 currStream->streamBuffer[currStream->byte_pos++] = *byte_buf;
640 *byte_buf = 0;
641 }
642 }
643 // actual info
644 mask = (unsigned int) 1 << 31;
645 for (i = 0; i < 32; i++)
646 {
647 *byte_buf <<= 1;
648
649 if (se->bitpattern & mask)
650 *byte_buf |= 1;
651
652 mask >>= 1;
653
654 if ((--(*bits_to_go)) == 0)
655 {
656 *bits_to_go = 8;
657 currStream->streamBuffer[currStream->byte_pos++] = *byte_buf;
658 *byte_buf = 0;
659 }
660 }
661 }
662 }
663
664
665 /*!
666 ************************************************************************
667 * \brief
668 * generates UVLC code and passes the codeword to the buffer
669 * \author
670 * Tian Dong
671 ************************************************************************
672 */
writeSyntaxElement2Buf_Fixed(SyntaxElement * se,Bitstream * this_streamBuffer)673 int writeSyntaxElement2Buf_Fixed(SyntaxElement *se, Bitstream* this_streamBuffer )
674 {
675 writeUVLC2buffer(se, this_streamBuffer );
676
677 #if TRACE
678 if(se->type <= 1)
679 trace2out (se);
680 #endif
681 return (se->len);
682 }
683
684 /*!
685 ************************************************************************
686 * \brief
687 * generates UVLC code and passes the codeword to the buffer
688 * \author
689 * Tian Dong
690 ************************************************************************
691 */
writeSE_Flag(SyntaxElement * se,DataPartition * dp)692 void writeSE_Flag(SyntaxElement *se, DataPartition *dp )
693 {
694 se->len = 1;
695 se->bitpattern = (se->value1 & 1);
696
697 writeUVLC2buffer(se, dp->bitstream );
698
699 #if TRACE
700 if(dp->bitstream->trace_enabled)
701 trace2out (se);
702 #endif
703 }
704
705 /*!
706 ************************************************************************
707 * \brief
708 * generates UVLC code and passes the codeword to the buffer
709 * \author
710 * Tian Dong
711 ************************************************************************
712 */
writeSE_invFlag(SyntaxElement * se,DataPartition * dp)713 void writeSE_invFlag(SyntaxElement *se, DataPartition *dp )
714 {
715 se->len = 1;
716 se->bitpattern = 1 - (se->value1 & 1);
717
718 writeUVLC2buffer(se, dp->bitstream );
719
720 #if TRACE
721 if(dp->bitstream->trace_enabled)
722 trace2out (se);
723 #endif
724 }
725
726 /*!
727 ************************************************************************
728 * \brief
729 * generates UVLC code and passes the codeword to the buffer
730 * \author
731 * Tian Dong
732 ************************************************************************
733 */
writeSE_Dummy(SyntaxElement * se,DataPartition * dp)734 void writeSE_Dummy(SyntaxElement *se, DataPartition *dp )
735 {
736 se->len = 0;
737 }
738
739
740 /*!
741 ************************************************************************
742 * \brief
743 * generates UVLC code and passes the codeword to the buffer
744 * \author
745 * Tian Dong
746 ************************************************************************
747 */
writeSE_Fix(SyntaxElement * se,Bitstream * bitstream)748 void writeSE_Fix(SyntaxElement *se, Bitstream *bitstream )
749 {
750 writeUVLC2buffer(se, bitstream);
751
752 #if TRACE
753 if(bitstream->trace_enabled)
754 trace2out (se);
755 #endif
756 }
757
758
759 /*!
760 ************************************************************************
761 * \brief
762 * Makes code word and passes it back
763 *
764 * \par Input:
765 * Info : Xn..X2 X1 X0 \n
766 * Length : Total number of bits in the codeword
767 ************************************************************************
768 */
769
symbol2vlc(SyntaxElement * sym)770 int symbol2vlc(SyntaxElement *sym)
771 {
772 int info_len = sym->len;
773
774 // Convert info into a bitpattern int
775 sym->bitpattern = 0;
776
777 // vlc coding
778 while(--info_len >= 0)
779 {
780 sym->bitpattern <<= 1;
781 sym->bitpattern |= (0x01 & (sym->inf >> info_len));
782 }
783 return 0;
784 }
785
786
787 /*!
788 ************************************************************************
789 * \brief
790 * generates VLC code and passes the codeword to the buffer
791 ************************************************************************
792 */
writeSyntaxElement_VLC(SyntaxElement * se,DataPartition * dp)793 int writeSyntaxElement_VLC(SyntaxElement *se, DataPartition *dp)
794 {
795 se->inf = se->value1;
796 se->len = se->value2;
797 symbol2vlc(se);
798
799 writeUVLC2buffer(se, dp->bitstream);
800
801 if(se->type != SE_HEADER)
802 dp->bitstream->write_flag = 1;
803
804 #if TRACE
805 if(dp->bitstream->trace_enabled)
806 trace2out (se);
807 #endif
808
809 return (se->len);
810 }
811
812
813 /*!
814 ************************************************************************
815 * \brief
816 * write VLC for NumCoeff and TrailingOnes
817 ************************************************************************
818 */
819
writeSyntaxElement_NumCoeffTrailingOnes(SyntaxElement * se,DataPartition * dp)820 int writeSyntaxElement_NumCoeffTrailingOnes(SyntaxElement *se, DataPartition *dp)
821 {
822 static const byte lentab[3][4][17] =
823 {
824 { // 0702
825 { 1, 6, 8, 9,10,11,13,13,13,14,14,15,15,16,16,16,16},
826 { 0, 2, 6, 8, 9,10,11,13,13,14,14,15,15,15,16,16,16},
827 { 0, 0, 3, 7, 8, 9,10,11,13,13,14,14,15,15,16,16,16},
828 { 0, 0, 0, 5, 6, 7, 8, 9,10,11,13,14,14,15,15,16,16},
829 },
830 {
831 { 2, 6, 6, 7, 8, 8, 9,11,11,12,12,12,13,13,13,14,14},
832 { 0, 2, 5, 6, 6, 7, 8, 9,11,11,12,12,13,13,14,14,14},
833 { 0, 0, 3, 6, 6, 7, 8, 9,11,11,12,12,13,13,13,14,14},
834 { 0, 0, 0, 4, 4, 5, 6, 6, 7, 9,11,11,12,13,13,13,14},
835 },
836 {
837 { 4, 6, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 9,10,10,10,10},
838 { 0, 4, 5, 5, 5, 5, 6, 6, 7, 8, 8, 9, 9, 9,10,10,10},
839 { 0, 0, 4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,10,10,10},
840 { 0, 0, 0, 4, 4, 4, 4, 4, 5, 6, 7, 8, 8, 9,10,10,10},
841 },
842
843 };
844
845 static const byte codtab[3][4][17] =
846 {
847 {
848 { 1, 5, 7, 7, 7, 7,15,11, 8,15,11,15,11,15,11, 7,4},
849 { 0, 1, 4, 6, 6, 6, 6,14,10,14,10,14,10, 1,14,10,6},
850 { 0, 0, 1, 5, 5, 5, 5, 5,13, 9,13, 9,13, 9,13, 9,5},
851 { 0, 0, 0, 3, 3, 4, 4, 4, 4, 4,12,12, 8,12, 8,12,8},
852 },
853 {
854 { 3,11, 7, 7, 7, 4, 7,15,11,15,11, 8,15,11, 7, 9,7},
855 { 0, 2, 7,10, 6, 6, 6, 6,14,10,14,10,14,10,11, 8,6},
856 { 0, 0, 3, 9, 5, 5, 5, 5,13, 9,13, 9,13, 9, 6,10,5},
857 { 0, 0, 0, 5, 4, 6, 8, 4, 4, 4,12, 8,12,12, 8, 1,4},
858 },
859 {
860 {15,15,11, 8,15,11, 9, 8,15,11,15,11, 8,13, 9, 5,1},
861 { 0,14,15,12,10, 8,14,10,14,14,10,14,10, 7,12, 8,4},
862 { 0, 0,13,14,11, 9,13, 9,13,10,13, 9,13, 9,11, 7,3},
863 { 0, 0, 0,12,11,10, 9, 8,13,12,12,12, 8,12,10, 6,2},
864 },
865 };
866 int vlcnum = se->len;
867
868 // se->value1 : numcoeff
869 // se->value2 : numtrailingones
870
871 if (vlcnum == 3)
872 {
873 se->len = 6; // 4 + 2 bit FLC
874 if (se->value1 > 0)
875 {
876 se->inf = ((se->value1-1) << 2) | se->value2;
877 }
878 else
879 {
880 se->inf = 3;
881 }
882 }
883 else
884 {
885 se->len = lentab[vlcnum][se->value2][se->value1];
886 se->inf = codtab[vlcnum][se->value2][se->value1];
887 }
888
889 if (se->len == 0)
890 {
891 printf("ERROR: (numcoeff,trailingones) not valid: vlc=%d (%d, %d)\n",
892 vlcnum, se->value1, se->value2);
893 exit(-1);
894 }
895
896 symbol2vlc(se);
897
898 writeUVLC2buffer(se, dp->bitstream);
899
900 if(se->type != SE_HEADER)
901 dp->bitstream->write_flag = 1;
902
903 #if TRACE
904 if(dp->bitstream->trace_enabled)
905 trace2out (se);
906 #endif
907
908 return (se->len);
909 }
910
911
912 /*!
913 ************************************************************************
914 * \brief
915 * write VLC for NumCoeff and TrailingOnes for Chroma DC
916 ************************************************************************
917 */
writeSyntaxElement_NumCoeffTrailingOnesChromaDC(VideoParameters * p_Vid,SyntaxElement * se,DataPartition * dp)918 int writeSyntaxElement_NumCoeffTrailingOnesChromaDC(VideoParameters *p_Vid, SyntaxElement *se, DataPartition *dp)
919 {
920 static const byte lentab[3][4][17] =
921 {
922 //YUV420
923 {{ 2, 6, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
924 { 0, 1, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
925 { 0, 0, 3, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
926 { 0, 0, 0, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
927 //YUV422
928 {{ 1, 7, 7, 9, 9,10,11,12,13, 0, 0, 0, 0, 0, 0, 0, 0},
929 { 0, 2, 7, 7, 9,10,11,12,12, 0, 0, 0, 0, 0, 0, 0, 0},
930 { 0, 0, 3, 7, 7, 9,10,11,12, 0, 0, 0, 0, 0, 0, 0, 0},
931 { 0, 0, 0, 5, 6, 7, 7,10,11, 0, 0, 0, 0, 0, 0, 0, 0}},
932 //YUV444
933 {{ 1, 6, 8, 9,10,11,13,13,13,14,14,15,15,16,16,16,16},
934 { 0, 2, 6, 8, 9,10,11,13,13,14,14,15,15,15,16,16,16},
935 { 0, 0, 3, 7, 8, 9,10,11,13,13,14,14,15,15,16,16,16},
936 { 0, 0, 0, 5, 6, 7, 8, 9,10,11,13,14,14,15,15,16,16}}
937 };
938
939 static const byte codtab[3][4][17] =
940 {
941 //YUV420
942 {{ 1, 7, 4, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
943 { 0, 1, 6, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
944 { 0, 0, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
945 { 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
946 //YUV422
947 {{ 1,15,14, 7, 6, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0},
948 { 0, 1,13,12, 5, 6, 6, 6, 5, 0, 0, 0, 0, 0, 0, 0, 0},
949 { 0, 0, 1,11,10, 4, 5, 5, 4, 0, 0, 0, 0, 0, 0, 0, 0},
950 { 0, 0, 0, 1, 1, 9, 8, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0}},
951 //YUV444
952 {{ 1, 5, 7, 7, 7, 7,15,11, 8,15,11,15,11,15,11, 7, 4},
953 { 0, 1, 4, 6, 6, 6, 6,14,10,14,10,14,10, 1,14,10, 6},
954 { 0, 0, 1, 5, 5, 5, 5, 5,13, 9,13, 9,13, 9,13, 9, 5},
955 { 0, 0, 0, 3, 3, 4, 4, 4, 4, 4,12,12, 8,12, 8,12, 8}}
956
957 };
958 int yuv = p_Vid->yuv_format - 1;
959
960 // se->value1 : numcoeff
961 // se->value2 : numtrailingones
962 se->len = lentab[yuv][se->value2][se->value1];
963 se->inf = codtab[yuv][se->value2][se->value1];
964
965 if (se->len == 0)
966 {
967 printf("ERROR: (numcoeff,trailingones) not valid: (%d, %d)\n",
968 se->value1, se->value2);
969 exit(-1);
970 }
971
972 symbol2vlc(se);
973
974 writeUVLC2buffer(se, dp->bitstream);
975
976 if(se->type != SE_HEADER)
977 dp->bitstream->write_flag = 1;
978
979 #if TRACE
980 if(dp->bitstream->trace_enabled)
981 trace2out (se);
982 #endif
983
984 return (se->len);
985 }
986
987
988 /*!
989 ************************************************************************
990 * \brief
991 * write VLC for TotalZeros
992 ************************************************************************
993 */
writeSyntaxElement_TotalZeros(SyntaxElement * se,DataPartition * dp)994 int writeSyntaxElement_TotalZeros(SyntaxElement *se, DataPartition *dp)
995 {
996 static const byte lentab[TOTRUN_NUM][16] =
997 {
998 { 1,3,3,4,4,5,5,6,6,7,7,8,8,9,9,9},
999 { 3,3,3,3,3,4,4,4,4,5,5,6,6,6,6},
1000 { 4,3,3,3,4,4,3,3,4,5,5,6,5,6},
1001 { 5,3,4,4,3,3,3,4,3,4,5,5,5},
1002 { 4,4,4,3,3,3,3,3,4,5,4,5},
1003 { 6,5,3,3,3,3,3,3,4,3,6},
1004 { 6,5,3,3,3,2,3,4,3,6},
1005 { 6,4,5,3,2,2,3,3,6},
1006 { 6,6,4,2,2,3,2,5},
1007 { 5,5,3,2,2,2,4},
1008 { 4,4,3,3,1,3},
1009 { 4,4,2,1,3},
1010 { 3,3,1,2},
1011 { 2,2,1},
1012 { 1,1},
1013 };
1014
1015 static const byte codtab[TOTRUN_NUM][16] =
1016 {
1017 {1,3,2,3,2,3,2,3,2,3,2,3,2,3,2,1},
1018 {7,6,5,4,3,5,4,3,2,3,2,3,2,1,0},
1019 {5,7,6,5,4,3,4,3,2,3,2,1,1,0},
1020 {3,7,5,4,6,5,4,3,3,2,2,1,0},
1021 {5,4,3,7,6,5,4,3,2,1,1,0},
1022 {1,1,7,6,5,4,3,2,1,1,0},
1023 {1,1,5,4,3,3,2,1,1,0},
1024 {1,1,1,3,3,2,2,1,0},
1025 {1,0,1,3,2,1,1,1,},
1026 {1,0,1,3,2,1,1,},
1027 {0,1,1,2,1,3},
1028 {0,1,1,1,1},
1029 {0,1,1,1},
1030 {0,1,1},
1031 {0,1},
1032 };
1033 int vlcnum = se->len;
1034
1035 // se->value1 : TotalZeros
1036 se->len = lentab[vlcnum][se->value1];
1037 se->inf = codtab[vlcnum][se->value1];
1038
1039 if (se->len == 0)
1040 {
1041 printf("ERROR: (TotalZeros) not valid: (%d)\n",se->value1);
1042 exit(-1);
1043 }
1044
1045 symbol2vlc(se);
1046
1047 writeUVLC2buffer(se, dp->bitstream);
1048
1049 if(se->type != SE_HEADER)
1050 dp->bitstream->write_flag = 1;
1051
1052 #if TRACE
1053 if(dp->bitstream->trace_enabled)
1054 trace2out (se);
1055 #endif
1056
1057 return (se->len);
1058 }
1059
1060
1061 /*!
1062 ************************************************************************
1063 * \brief
1064 * write VLC for TotalZeros for Chroma DC
1065 ************************************************************************
1066 */
writeSyntaxElement_TotalZerosChromaDC(VideoParameters * p_Vid,SyntaxElement * se,DataPartition * dp)1067 int writeSyntaxElement_TotalZerosChromaDC(VideoParameters *p_Vid, SyntaxElement *se, DataPartition *dp)
1068 {
1069 static const byte lentab[3][TOTRUN_NUM][16] =
1070 {
1071 //YUV420
1072 {{ 1,2,3,3},
1073 { 1,2,2},
1074 { 1,1}},
1075 //YUV422
1076 {{ 1,3,3,4,4,4,5,5},
1077 { 3,2,3,3,3,3,3},
1078 { 3,3,2,2,3,3},
1079 { 3,2,2,2,3},
1080 { 2,2,2,2},
1081 { 2,2,1},
1082 { 1,1}},
1083 //YUV444
1084 {{ 1,3,3,4,4,5,5,6,6,7,7,8,8,9,9,9},
1085 { 3,3,3,3,3,4,4,4,4,5,5,6,6,6,6},
1086 { 4,3,3,3,4,4,3,3,4,5,5,6,5,6},
1087 { 5,3,4,4,3,3,3,4,3,4,5,5,5},
1088 { 4,4,4,3,3,3,3,3,4,5,4,5},
1089 { 6,5,3,3,3,3,3,3,4,3,6},
1090 { 6,5,3,3,3,2,3,4,3,6},
1091 { 6,4,5,3,2,2,3,3,6},
1092 { 6,6,4,2,2,3,2,5},
1093 { 5,5,3,2,2,2,4},
1094 { 4,4,3,3,1,3},
1095 { 4,4,2,1,3},
1096 { 3,3,1,2},
1097 { 2,2,1},
1098 { 1,1}}
1099 };
1100
1101 static const byte codtab[3][TOTRUN_NUM][16] =
1102 {
1103 //YUV420
1104 {{ 1,1,1,0},
1105 { 1,1,0},
1106 { 1,0}},
1107 //YUV422
1108 {{ 1,2,3,2,3,1,1,0},
1109 { 0,1,1,4,5,6,7},
1110 { 0,1,1,2,6,7},
1111 { 6,0,1,2,7},
1112 { 0,1,2,3},
1113 { 0,1,1},
1114 { 0,1}},
1115 //YUV444
1116 {{1,3,2,3,2,3,2,3,2,3,2,3,2,3,2,1},
1117 {7,6,5,4,3,5,4,3,2,3,2,3,2,1,0},
1118 {5,7,6,5,4,3,4,3,2,3,2,1,1,0},
1119 {3,7,5,4,6,5,4,3,3,2,2,1,0},
1120 {5,4,3,7,6,5,4,3,2,1,1,0},
1121 {1,1,7,6,5,4,3,2,1,1,0},
1122 {1,1,5,4,3,3,2,1,1,0},
1123 {1,1,1,3,3,2,2,1,0},
1124 {1,0,1,3,2,1,1,1,},
1125 {1,0,1,3,2,1,1,},
1126 {0,1,1,2,1,3},
1127 {0,1,1,1,1},
1128 {0,1,1,1},
1129 {0,1,1},
1130 {0,1}}
1131 };
1132 int vlcnum = se->len;
1133 int yuv = p_Vid->yuv_format - 1;
1134
1135 // se->value1 : TotalZeros
1136 se->len = lentab[yuv][vlcnum][se->value1];
1137 se->inf = codtab[yuv][vlcnum][se->value1];
1138
1139 if (se->len == 0)
1140 {
1141 printf("ERROR: (TotalZeros) not valid: (%d)\n",se->value1);
1142 exit(-1);
1143 }
1144
1145 symbol2vlc(se);
1146
1147 writeUVLC2buffer(se, dp->bitstream);
1148
1149 if(se->type != SE_HEADER)
1150 dp->bitstream->write_flag = 1;
1151
1152 #if TRACE
1153 if(dp->bitstream->trace_enabled)
1154 trace2out (se);
1155 #endif
1156
1157 return (se->len);
1158 }
1159
1160
1161 /*!
1162 ************************************************************************
1163 * \brief
1164 * write VLC for Run Before Next Coefficient, VLC0
1165 ************************************************************************
1166 */
writeSyntaxElement_Run(SyntaxElement * se,DataPartition * dp)1167 int writeSyntaxElement_Run(SyntaxElement *se, DataPartition *dp)
1168 {
1169 static const byte lentab[TOTRUN_NUM][16] =
1170 {
1171 {1,1},
1172 {1,2,2},
1173 {2,2,2,2},
1174 {2,2,2,3,3},
1175 {2,2,3,3,3,3},
1176 {2,3,3,3,3,3,3},
1177 {3,3,3,3,3,3,3,4,5,6,7,8,9,10,11},
1178 };
1179
1180 static const byte codtab[TOTRUN_NUM][16] =
1181 {
1182 {1,0},
1183 {1,1,0},
1184 {3,2,1,0},
1185 {3,2,1,1,0},
1186 {3,2,3,2,1,0},
1187 {3,0,1,3,2,5,4},
1188 {7,6,5,4,3,2,1,1,1,1,1,1,1,1,1},
1189 };
1190 int vlcnum = se->len;
1191
1192 // se->value1 : run
1193 se->len = lentab[vlcnum][se->value1];
1194 se->inf = codtab[vlcnum][se->value1];
1195
1196 if (se->len == 0)
1197 {
1198 printf("ERROR: (run) not valid: (%d)\n",se->value1);
1199 exit(-1);
1200 }
1201
1202 symbol2vlc(se);
1203
1204 writeUVLC2buffer(se, dp->bitstream);
1205
1206 if(se->type != SE_HEADER)
1207 dp->bitstream->write_flag = 1;
1208
1209 #if TRACE
1210 if(dp->bitstream->trace_enabled)
1211 trace2out (se);
1212 #endif
1213
1214 return (se->len);
1215 }
1216
1217
1218 /*!
1219 ************************************************************************
1220 * \brief
1221 * write VLC for Coeff Level (VLC1)
1222 ************************************************************************
1223 */
writeSyntaxElement_Level_VLC1(SyntaxElement * se,DataPartition * dp,int profile_idc)1224 int writeSyntaxElement_Level_VLC1(SyntaxElement *se, DataPartition *dp, int profile_idc)
1225 {
1226 int level = se->value1;
1227 int sign = (level < 0 ? 1 : 0);
1228 int levabs = iabs(level);
1229
1230 if (levabs < 8)
1231 {
1232 se->len = levabs * 2 + sign - 1;
1233 se->inf = 1;
1234 }
1235 else if (levabs < 16)
1236 {
1237 // escape code1
1238 se->len = 19;
1239 se->inf = 16 | ((levabs << 1) - 16) | sign;
1240 }
1241 else
1242 {
1243 int iMask = 4096, numPrefix = 0;
1244 int levabsm16 = levabs + 2032;
1245
1246 // escape code2
1247 if ((levabsm16) >= 4096)
1248 {
1249 numPrefix++;
1250 while ((levabsm16) >= (4096 << numPrefix))
1251 {
1252 numPrefix++;
1253 }
1254 }
1255
1256 iMask <<= numPrefix;
1257 se->inf = iMask | ((levabsm16 << 1) - iMask) | sign;
1258
1259 /* Assert to make sure that the code fits in the VLC */
1260 /* make sure that we are in High Profile to represent level_prefix > 15 */
1261 if (numPrefix > 0 && !is_FREXT_profile( profile_idc ))
1262 {
1263 //error( "level_prefix must be <= 15 except in High Profile\n", 1000 );
1264 se->len = 0x0000FFFF; // This can be some other big number
1265 return (se->len);
1266 }
1267
1268 se->len = 28 + (numPrefix << 1);
1269 }
1270
1271 symbol2vlc(se);
1272
1273 writeUVLC2buffer(se, dp->bitstream);
1274
1275 if(se->type != SE_HEADER)
1276 dp->bitstream->write_flag = 1;
1277
1278 #if TRACE
1279 if(dp->bitstream->trace_enabled)
1280 trace2out (se);
1281 #endif
1282
1283 return (se->len);
1284 }
1285
1286
1287 /*!
1288 ************************************************************************
1289 * \brief
1290 * write VLC for Coeff Level
1291 ************************************************************************
1292 */
writeSyntaxElement_Level_VLCN(SyntaxElement * se,int vlc,DataPartition * dp,int profile_idc)1293 int writeSyntaxElement_Level_VLCN(SyntaxElement *se, int vlc, DataPartition *dp, int profile_idc)
1294 {
1295 int level = se->value1;
1296 int sign = (level < 0 ? 1 : 0);
1297 int levabs = iabs(level) - 1;
1298
1299 int shift = vlc - 1;
1300 int escape = (15 << shift);
1301
1302 if (levabs < escape)
1303 {
1304 int sufmask = ~((0xffffffff) << shift);
1305 int suffix = (levabs) & sufmask;
1306
1307 se->len = ((levabs) >> shift) + 1 + vlc;
1308 se->inf = (2 << shift) | (suffix << 1) | sign;
1309 }
1310 else
1311 {
1312 int iMask = 4096;
1313 int levabsesc = levabs - escape + 2048;
1314 int numPrefix = 0;
1315
1316 if ((levabsesc) >= 4096)
1317 {
1318 numPrefix++;
1319 while ((levabsesc) >= (4096 << numPrefix))
1320 {
1321 numPrefix++;
1322 }
1323 }
1324
1325 iMask <<= numPrefix;
1326 se->inf = iMask | ((levabsesc << 1) - iMask) | sign;
1327
1328 /* Assert to make sure that the code fits in the VLC */
1329 /* make sure that we are in High Profile to represent level_prefix > 15 */
1330 if (numPrefix > 0 && !is_FREXT_profile( profile_idc ))
1331 {
1332 //error( "level_prefix must be <= 15 except in High Profile\n", 1000 );
1333 se->len = 0x0000FFFF; // This can be some other big number
1334 return (se->len);
1335 }
1336 se->len = 28 + (numPrefix << 1);
1337 }
1338
1339 symbol2vlc(se);
1340
1341 writeUVLC2buffer(se, dp->bitstream);
1342
1343 if(se->type != SE_HEADER)
1344 dp->bitstream->write_flag = 1;
1345
1346 #if TRACE
1347 if(dp->bitstream->trace_enabled)
1348 trace2out (se);
1349 #endif
1350
1351 return (se->len);
1352 }
1353
1354
1355 #if TRACE
1356 int bitcounter = 0;
1357
1358 /*!
1359 ************************************************************************
1360 * \brief
1361 * Write out a trace string on the trace file
1362 ************************************************************************
1363 */
trace2out(SyntaxElement * sym)1364 void trace2out(SyntaxElement *sym)
1365 {
1366 int i, chars;
1367
1368 if (p_Enc->p_trace != NULL)
1369 {
1370 putc('@', p_Enc->p_trace);
1371 chars = fprintf(p_Enc->p_trace, "%i", bitcounter);
1372 while(chars++ < 6)
1373 putc(' ',p_Enc->p_trace);
1374
1375 chars += fprintf(p_Enc->p_trace, "%s", sym->tracestring);
1376 while(chars++ < 55)
1377 putc(' ',p_Enc->p_trace);
1378
1379 // align bit pattern
1380 if(sym->len<15)
1381 {
1382 for(i=0 ; i<15-sym->len ; i++)
1383 fputc(' ', p_Enc->p_trace);
1384 }
1385
1386 // print bit pattern
1387 bitcounter += sym->len;
1388 for(i=1 ; i<=sym->len ; i++)
1389 {
1390 if((sym->bitpattern >> (sym->len-i)) & 0x1)
1391 fputc('1', p_Enc->p_trace);
1392 else
1393 fputc('0', p_Enc->p_trace);
1394 }
1395 fprintf(p_Enc->p_trace, " (%3d) \n",sym->value1);
1396 }
1397 fflush (p_Enc->p_trace);
1398 }
1399
trace2out_cabac(SyntaxElement * sym)1400 void trace2out_cabac(SyntaxElement *sym)
1401 {
1402 int chars;
1403
1404 if (p_Enc->p_trace != NULL)
1405 {
1406 putc('@', p_Enc->p_trace);
1407 chars = fprintf(p_Enc->p_trace, "%i", bitcounter);
1408 while(chars++ < 6)
1409 putc(' ',p_Enc->p_trace);
1410
1411 chars += fprintf(p_Enc->p_trace, "%s", sym->tracestring);
1412 while(chars++ < 70)
1413 putc(' ',p_Enc->p_trace);
1414
1415 fprintf(p_Enc->p_trace, " (%3d) \n",sym->value1);
1416 }
1417 fflush (p_Enc->p_trace);
1418 bitcounter += sym->len;
1419 }
1420 #endif
1421
1422
1423 /*!
1424 ************************************************************************
1425 * \brief
1426 * puts the less than 8 bits in the byte buffer of the Bitstream into
1427 * the streamBuffer.
1428 *
1429 * \param p_Vid
1430 * VideoParameters for current picture encoding
1431 * \param currStream
1432 * the Bitstream the alignment should be established
1433 * \param cur_stats
1434 * currently used statistics parameters
1435 *
1436 ************************************************************************
1437 */
writeVlcByteAlign(VideoParameters * p_Vid,Bitstream * currStream,StatParameters * cur_stats)1438 void writeVlcByteAlign(VideoParameters *p_Vid, Bitstream* currStream, StatParameters *cur_stats)
1439 {
1440 if (currStream->bits_to_go < 8)
1441 { // trailing bits to process
1442 currStream->byte_buf = (byte) ((currStream->byte_buf << currStream->bits_to_go) | (0xff >> (8 - currStream->bits_to_go)));
1443 currStream->streamBuffer[currStream->byte_pos++] = currStream->byte_buf;
1444 cur_stats->bit_use_stuffing_bits[p_Vid->type] += currStream->bits_to_go;
1445 currStream->bits_to_go = 8;
1446 }
1447 }
1448
1449 /*!
1450 ************************************************************************
1451 * \brief
1452 * Resets the nz_coeff parameters for a macroblock
1453 ************************************************************************/
reset_mb_nz_coeff(VideoParameters * p_Vid,int mb_number)1454 void reset_mb_nz_coeff(VideoParameters *p_Vid, int mb_number)
1455 {
1456 memset(&p_Vid->nz_coeff [mb_number][0][0], 0, BLOCK_SIZE * (BLOCK_SIZE + p_Vid->num_blk8x8_uv) * sizeof(int));
1457 }
1458
1459
writeUVLC_CAVLC(Macroblock * currMB,SyntaxElement * se,DataPartition * dp)1460 void writeUVLC_CAVLC(Macroblock *currMB, SyntaxElement *se, DataPartition *dp)
1461 {
1462 writeSE_UVLC(se, dp);
1463 }
1464
writeSVLC_CAVLC(Macroblock * currMB,SyntaxElement * se,DataPartition * dp)1465 void writeSVLC_CAVLC(Macroblock *currMB, SyntaxElement *se, DataPartition *dp)
1466 {
1467 writeSE_SVLC(se, dp);
1468 }
1469
writeFlag_CAVLC(Macroblock * currMB,SyntaxElement * se,DataPartition * dp)1470 void writeFlag_CAVLC(Macroblock *currMB, SyntaxElement *se, DataPartition *dp)
1471 {
1472 writeSE_Flag(se, dp);
1473 }
1474
bs_bitlength(Bitstream * bitstream)1475 int bs_bitlength(Bitstream *bitstream)
1476 {
1477 return (bitstream->byte_pos*8+(8-bitstream->bits_to_go));
1478 }
1479
1480 /*!
1481 ************************************************************************
1482 * \brief
1483 * Dummy function for reference picture bit writing
1484 * \author
1485 ************************************************************************
1486 */
writeRefPic_Dummy(Macroblock * currMB,SyntaxElement * se,DataPartition * dp)1487 void writeRefPic_Dummy(Macroblock *currMB, SyntaxElement *se, DataPartition *dp )
1488 {
1489 se->len = 0;
1490 }
1491
1492 /*!
1493 ************************************************************************
1494 * \brief
1495 * Function for writing the bits for the reference index when up to 2 refs are available
1496 * \author
1497 ************************************************************************
1498 */
writeRefPic_2Ref_CAVLC(Macroblock * currMB,SyntaxElement * se,DataPartition * dp)1499 void writeRefPic_2Ref_CAVLC(Macroblock *currMB, SyntaxElement *se, DataPartition *dp )
1500 {
1501 writeSE_invFlag( se, dp );
1502 }
1503
1504 /*!
1505 ************************************************************************
1506 * \brief
1507 * Function for writing the bits for the reference index when N (N>2) refs are available
1508 * \author
1509 ************************************************************************
1510 */
writeRefPic_NRef_CAVLC(Macroblock * currMB,SyntaxElement * se,DataPartition * dp)1511 void writeRefPic_NRef_CAVLC(Macroblock *currMB, SyntaxElement *se, DataPartition *dp )
1512 {
1513 writeSE_UVLC( se, dp );
1514 }
1515
1516 /*!
1517 ************************************************************************
1518 * \brief
1519 * Reads bits from the bitstream buffer
1520 *
1521 * \param buffer
1522 * containing VLC-coded data bits
1523 * \param totbitoffset
1524 * bit offset from start of partition
1525 * \param info
1526 * returns value of the read bits
1527 * \param bitcount
1528 * total bytes in bitstream
1529 * \param numbits
1530 * number of bits to read
1531 *
1532 ************************************************************************
1533 */
GetBits(byte * buffer,int totbitoffset,int * info,int bitcount,int numbits)1534 int GetBits (byte *buffer,
1535 int totbitoffset,
1536 int *info,
1537 int bitcount,
1538 int numbits)
1539 {
1540 if ((totbitoffset + numbits ) > bitcount)
1541 {
1542 return 0;
1543 }
1544 else
1545 {
1546 int bitoffset = 7 - (totbitoffset & 0x07); // bit from start of byte
1547 int byteoffset = (totbitoffset >> 3); // byte from start of buffer
1548 int bitcounter = numbits;
1549 byte *curbyte = &(buffer[byteoffset]);
1550 int inf = 0;
1551
1552 while (numbits--)
1553 {
1554 inf <<=1;
1555 inf |= ((*curbyte)>> (bitoffset--)) & 0x01;
1556 if (bitoffset == -1 )
1557 { //Move onto next byte to get all of numbits
1558 curbyte++;
1559 bitoffset = 7;
1560 }
1561 // Above conditional could also be avoided using the following:
1562 // curbyte -= (bitoffset >> 3);
1563 // bitoffset &= 0x07;
1564 }
1565 *info = inf;
1566
1567 return bitcounter; // return absolute offset in bit from start of frame
1568 }
1569 }
1570