1 /********************************************************************
3 * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2003 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
11 ********************************************************************
16 ********************************************************************/
19 #include "codec_internal.h"
20 #include "block_inline.h"
22 /* Long run bit string coding */
23 static ogg_uint32_t
FrArrayCodeSBRun( CP_INSTANCE
*cpi
, ogg_uint32_t value
){
24 ogg_uint32_t CodedVal
= 0;
25 ogg_uint32_t CodedBits
= 0;
35 111111xxxxxxxxxxxx 34-4129 */
40 } else if ( value
<= 3 ) {
41 CodedVal
= 0x0004 + (value
- 2);
43 } else if ( value
<= 5 ) {
44 CodedVal
= 0x000C + (value
- 4);
46 } else if ( value
<= 9 ) {
47 CodedVal
= 0x0038 + (value
- 6);
49 } else if ( value
<= 17 ) {
50 CodedVal
= 0x00F0 + (value
- 10);
52 } else if ( value
<= 33 ) {
53 CodedVal
= 0x03E0 + (value
- 18);
56 CodedVal
= 0x3F000 + (value
- 34);
59 /* todo: handle value > 4129 extension */
61 /* Add the bits to the encode holding buffer. */
62 oggpackB_write( cpi
->oggbuffer
, CodedVal
, CodedBits
);
67 /* Short run bit string coding */
68 static ogg_uint32_t
FrArrayCodeBlockRun( CP_INSTANCE
*cpi
,
69 ogg_uint32_t value
) {
70 ogg_uint32_t CodedVal
= 0;
71 ogg_uint32_t CodedBits
= 0;
85 } else if ( value
<= 4 ) {
86 CodedVal
= 0x0004 + (value
- 3);
89 } else if ( value
<= 6 ) {
90 CodedVal
= 0x000C + (value
- 5);
93 } else if ( value
<= 10 ) {
94 CodedVal
= 0x0038 + (value
- 7);
97 } else if ( value
<= 14 ) {
98 CodedVal
= 0x0078 + (value
- 11);
101 CodedVal
= 0x01F0 + (value
- 15);
105 /* Add the bits to the encode holding buffer. */
106 oggpackB_write( cpi
->oggbuffer
, CodedVal
, CodedBits
);
111 void PackAndWriteDFArray( CP_INSTANCE
*cpi
){
114 ogg_uint32_t run_count
;
116 ogg_uint32_t SB
, MB
, B
; /* Block, MB and SB loop variables */
117 ogg_uint32_t BListIndex
= 0;
118 ogg_uint32_t LastSbBIndex
= 0;
119 ogg_int32_t DfBlockIndex
; /* Block index in display_fragments */
121 /* Initialise workspaces */
122 memset( cpi
->pb
.SBFullyFlags
, 1, cpi
->pb
.SuperBlocks
);
123 memset( cpi
->pb
.SBCodedFlags
, 0, cpi
->pb
.SuperBlocks
);
124 memset( cpi
->PartiallyCodedFlags
, 0, cpi
->pb
.SuperBlocks
);
125 memset( cpi
->BlockCodedFlags
, 0, cpi
->pb
.UnitFragments
);
127 for( SB
= 0; SB
< cpi
->pb
.SuperBlocks
; SB
++ ) {
128 /* Check for coded blocks and macro-blocks */
129 for ( MB
=0; MB
<4; MB
++ ) {
131 if ( QuadMapToMBTopLeft(cpi
->pb
.BlockMap
,SB
,MB
) >= 0 ) {
132 for ( B
=0; B
<4; B
++ ) {
133 DfBlockIndex
= QuadMapToIndex1( cpi
->pb
.BlockMap
,SB
, MB
, B
);
135 /* Does Block lie in frame: */
136 if ( DfBlockIndex
>= 0 ) {
137 /* In Frame: If it is not coded then this SB is only
139 if ( cpi
->pb
.display_fragments
[DfBlockIndex
] ) {
140 cpi
->pb
.SBCodedFlags
[SB
] = 1; /* SB at least partly coded */
141 cpi
->BlockCodedFlags
[BListIndex
] = 1; /* Block is coded */
143 cpi
->pb
.SBFullyFlags
[SB
] = 0; /* SB not fully coded */
144 cpi
->BlockCodedFlags
[BListIndex
] = 0; /* Block is not coded */
153 /* Is the SB fully coded or uncoded.
154 If so then backup BListIndex and MBListIndex */
155 if ( cpi
->pb
.SBFullyFlags
[SB
] || !cpi
->pb
.SBCodedFlags
[SB
] ) {
156 BListIndex
= LastSbBIndex
; /* Reset to values from previous SB */
158 cpi
->PartiallyCodedFlags
[SB
] = 1; /* Set up list of partially
160 LastSbBIndex
= BListIndex
;
164 /* Code list of partially coded Super-Block. */
165 val
= cpi
->PartiallyCodedFlags
[0];
166 oggpackB_write( cpi
->oggbuffer
, (ogg_uint32_t
)val
, 1);
168 while ( i
< cpi
->pb
.SuperBlocks
) {
170 while ( (i
<cpi
->pb
.SuperBlocks
) && (cpi
->PartiallyCodedFlags
[i
]==val
) ) {
176 FrArrayCodeSBRun( cpi
, run_count
);
177 val
= ( val
== 0 ) ? 1 : 0;
180 /* RLC Super-Block fully/not coded. */
183 /* Skip partially coded blocks */
184 while( (i
< cpi
->pb
.SuperBlocks
) && cpi
->PartiallyCodedFlags
[i
] )
187 if ( i
< cpi
->pb
.SuperBlocks
) {
188 val
= cpi
->pb
.SBFullyFlags
[i
];
189 oggpackB_write( cpi
->oggbuffer
, (ogg_uint32_t
)val
, 1);
191 while ( i
< cpi
->pb
.SuperBlocks
) {
193 while ( (i
< cpi
->pb
.SuperBlocks
) && (cpi
->pb
.SBFullyFlags
[i
] == val
) ) {
195 /* Skip partially coded blocks */
196 while( (i
< cpi
->pb
.SuperBlocks
) && cpi
->PartiallyCodedFlags
[i
] )
202 FrArrayCodeSBRun( cpi
, run_count
);
203 val
= ( val
== 0 ) ? 1 : 0;
207 /* Now code the block flags */
208 if ( BListIndex
> 0 ) {
209 /* Code the block flags start value */
210 val
= cpi
->BlockCodedFlags
[0];
211 oggpackB_write( cpi
->oggbuffer
, (ogg_uint32_t
)val
, 1);
213 /* Now code the block flags. */
214 for ( i
= 0; i
< BListIndex
; ) {
216 while ( (cpi
->BlockCodedFlags
[i
] == val
) && (i
< BListIndex
) ) {
221 FrArrayCodeBlockRun( cpi
, run_count
);
222 val
= ( val
== 0 ) ? 1 : 0;
228 static void FrArrayDeCodeInit(PB_INSTANCE
*pbi
){
229 /* Initialise the decoding of a run. */
230 pbi
->bit_pattern
= 0;
231 pbi
->bits_so_far
= 0;
234 /* Short run bit string decoding */
235 static int FrArrayDeCodeBlockRun( PB_INSTANCE
*pbi
, ogg_uint32_t bit_value
,
236 ogg_int32_t
* run_value
){
239 /* Add in the new bit value. */
241 pbi
->bit_pattern
= (pbi
->bit_pattern
<< 1) + (bit_value
& 1);
253 switch ( pbi
->bits_so_far
){
255 /* If bit 1 is clear */
256 if ( !(pbi
->bit_pattern
& 0x0002) ){
258 *run_value
= (pbi
->bit_pattern
& 0x0001) + 1;
263 /* If bit 1 is clear */
264 if ( !(pbi
->bit_pattern
& 0x0002) ){
266 *run_value
= (pbi
->bit_pattern
& 0x0001) + 3;
271 /* If bit 1 is clear */
272 if ( !(pbi
->bit_pattern
& 0x0002) ){
274 *run_value
= (pbi
->bit_pattern
& 0x0001) + 5;
279 /* If bit 2 is clear */
280 if ( !(pbi
->bit_pattern
& 0x0004) ){
282 *run_value
= (pbi
->bit_pattern
& 0x0003) + 7;
287 /* If bit 2 is clear */
288 if ( !(pbi
->bit_pattern
& 0x0004) ){
290 *run_value
= (pbi
->bit_pattern
& 0x0003) + 11;
296 *run_value
= (pbi
->bit_pattern
& 0x000F) + 15;
303 /* Long run bit string decoding */
304 static int FrArrayDeCodeSBRun (PB_INSTANCE
*pbi
, ogg_uint32_t bit_value
,
305 ogg_int32_t
* run_value
){
308 /* Add in the new bit value. */
310 pbi
->bit_pattern
= (pbi
->bit_pattern
<< 1) + (bit_value
& 1);
320 111111xxxxxxxxxxxx 34-4129
323 switch ( pbi
->bits_so_far
){
325 if ( pbi
->bit_pattern
== 0 ){
333 if ( !(pbi
->bit_pattern
& 0x0002) ){
335 *run_value
= (pbi
->bit_pattern
& 0x0001) + 2;
341 if ( !(pbi
->bit_pattern
& 0x0002) ){
343 *run_value
= (pbi
->bit_pattern
& 0x0001) + 4;
349 if ( !(pbi
->bit_pattern
& 0x0004) ){
351 *run_value
= (pbi
->bit_pattern
& 0x0003) + 6;
357 if ( !(pbi
->bit_pattern
& 0x0008) ){
359 *run_value
= (pbi
->bit_pattern
& 0x0007) + 10;
365 if ( !(pbi
->bit_pattern
& 0x0010) ){
367 *run_value
= (pbi
->bit_pattern
& 0x000F) + 18;
373 *run_value
= (pbi
->bit_pattern
& 0x0FFF) + 34;
381 /* todo: handle additional bits for values over 4129 */
386 static void GetNextBInit(PB_INSTANCE
*pbi
){
389 theora_read(pbi
->opb
,1,&ret
);
390 pbi
->NextBit
= (unsigned char)ret
;
392 /* Read run length */
393 FrArrayDeCodeInit(pbi
);
394 do theora_read(pbi
->opb
,1,&ret
);
395 while (FrArrayDeCodeBlockRun(pbi
,ret
,&pbi
->BitsLeft
)==0);
399 static unsigned char GetNextBBit (PB_INSTANCE
*pbi
){
401 if ( !pbi
->BitsLeft
){
402 /* Toggle the value. */
403 pbi
->NextBit
= ( pbi
->NextBit
== 1 ) ? 0 : 1;
406 FrArrayDeCodeInit(pbi
);
407 do theora_read(pbi
->opb
,1,&ret
);
408 while (FrArrayDeCodeBlockRun(pbi
,ret
,&pbi
->BitsLeft
)==0);
412 /* Have read a bit */
415 /* Return next bit value */
419 static void GetNextSbInit(PB_INSTANCE
*pbi
){
422 theora_read(pbi
->opb
,1,&ret
);
423 pbi
->NextBit
= (unsigned char)ret
;
425 /* Read run length */
426 FrArrayDeCodeInit(pbi
);
427 do theora_read(pbi
->opb
,1,&ret
);
428 while (FrArrayDeCodeSBRun(pbi
,ret
,&pbi
->BitsLeft
)==0);
432 static unsigned char GetNextSbBit (PB_INSTANCE
*pbi
){
435 if ( !pbi
->BitsLeft
){
436 /* Toggle the value. */
437 pbi
->NextBit
= ( pbi
->NextBit
== 1 ) ? 0 : 1;
440 FrArrayDeCodeInit(pbi
);
441 do theora_read(pbi
->opb
,1,&ret
);
442 while (FrArrayDeCodeSBRun(pbi
,ret
,&pbi
->BitsLeft
)==0);
446 /* Have read a bit */
449 /* Return next bit value */
453 void QuadDecodeDisplayFragments ( PB_INSTANCE
*pbi
){
454 ogg_uint32_t SB
, MB
, B
;
458 ogg_uint32_t MBIndex
= 0;
460 /* Reset various data structures common to key frames and inter frames. */
461 pbi
->CodedBlockIndex
= 0;
462 memset ( pbi
->display_fragments
, 0, pbi
->UnitFragments
);
464 /* For "Key frames" mark all blocks as coded and return. */
465 /* Else initialise the ArrayPtr array to 0 (all blocks uncoded by default) */
466 if ( GetFrameType(pbi
) == KEY_FRAME
) {
467 memset( pbi
->SBFullyFlags
, 1, pbi
->SuperBlocks
);
468 memset( pbi
->SBCodedFlags
, 1, pbi
->SuperBlocks
);
469 memset( pbi
->MBCodedFlags
, 0, pbi
->MacroBlocks
);
471 memset( pbi
->SBFullyFlags
, 0, pbi
->SuperBlocks
);
472 memset( pbi
->MBCodedFlags
, 0, pbi
->MacroBlocks
);
474 /* Un-pack the list of partially coded Super-Blocks */
476 for( SB
= 0; SB
< pbi
->SuperBlocks
; SB
++){
477 pbi
->SBCodedFlags
[SB
] = GetNextSbBit (pbi
);
480 /* Scan through the list of super blocks. Unless all are marked
481 as partially coded we have more to do. */
483 for ( SB
=0; SB
<pbi
->SuperBlocks
; SB
++ ) {
484 if ( !pbi
->SBCodedFlags
[SB
] ) {
490 /* Are there further block map bits to decode ? */
491 if ( DataToDecode
) {
492 /* Un-pack the Super-Block fully coded flags. */
494 for( SB
= 0; SB
< pbi
->SuperBlocks
; SB
++) {
495 /* Skip blocks already marked as partially coded */
496 while( (SB
< pbi
->SuperBlocks
) && pbi
->SBCodedFlags
[SB
] )
499 if ( SB
< pbi
->SuperBlocks
) {
500 pbi
->SBFullyFlags
[SB
] = GetNextSbBit (pbi
);
502 if ( pbi
->SBFullyFlags
[SB
] ) /* If SB is fully coded. */
503 pbi
->SBCodedFlags
[SB
] = 1; /* Mark the SB as coded */
508 /* Scan through the list of coded super blocks. If at least one
509 is marked as partially coded then we have a block list to
511 for ( SB
=0; SB
<pbi
->SuperBlocks
; SB
++ ) {
512 if ( pbi
->SBCodedFlags
[SB
] && !pbi
->SBFullyFlags
[SB
] ) {
513 /* Initialise the block list decoder. */
520 /* Decode the block data from the bit stream. */
521 for ( SB
=0; SB
<pbi
->SuperBlocks
; SB
++ ){
522 for ( MB
=0; MB
<4; MB
++ ){
523 /* If MB is in the frame */
524 if ( QuadMapToMBTopLeft(pbi
->BlockMap
, SB
,MB
) >= 0 ){
525 /* Only read block level data if SB was fully or partially coded */
526 if ( pbi
->SBCodedFlags
[SB
] ) {
527 for ( B
=0; B
<4; B
++ ){
528 /* If block is valid (in frame)... */
529 dfIndex
= QuadMapToIndex1( pbi
->BlockMap
, SB
, MB
, B
);
531 if ( pbi
->SBFullyFlags
[SB
] )
532 pbi
->display_fragments
[dfIndex
] = 1;
534 pbi
->display_fragments
[dfIndex
] = GetNextBBit(pbi
);
536 /* Create linear list of coded block indices */
537 if ( pbi
->display_fragments
[dfIndex
] ) {
538 pbi
->MBCodedFlags
[MBIndex
] = 1;
539 pbi
->CodedBlockList
[pbi
->CodedBlockIndex
] = dfIndex
;
540 pbi
->CodedBlockIndex
++;
552 CODING_MODE
FrArrayUnpackMode(PB_INSTANCE
*pbi
){
556 Entry 0 (most frequent) 0 1
566 /* Initialise the decoding. */
567 pbi
->bits_so_far
= 0;
569 theora_read(pbi
->opb
,1,&ret
);
570 pbi
->bit_pattern
= ret
;
572 /* Do we have a match */
573 if ( pbi
->bit_pattern
== 0 )
574 return (CODING_MODE
)0;
576 /* Get the next bit */
577 theora_read(pbi
->opb
,1,&ret
);
578 pbi
->bit_pattern
= (pbi
->bit_pattern
<< 1) | ret
;
580 /* Do we have a match */
581 if ( pbi
->bit_pattern
== 0x0002 )
582 return (CODING_MODE
)1;
584 theora_read(pbi
->opb
,1,&ret
);
585 pbi
->bit_pattern
= (pbi
->bit_pattern
<< 1) | ret
;
587 /* Do we have a match */
588 if ( pbi
->bit_pattern
== 0x0006 )
589 return (CODING_MODE
)2;
591 theora_read(pbi
->opb
,1,&ret
);
592 pbi
->bit_pattern
= (pbi
->bit_pattern
<< 1) | ret
;
594 /* Do we have a match */
595 if ( pbi
->bit_pattern
== 0x000E )
596 return (CODING_MODE
)3;
598 theora_read(pbi
->opb
,1,&ret
);
599 pbi
->bit_pattern
= (pbi
->bit_pattern
<< 1) | ret
;
601 /* Do we have a match */
602 if ( pbi
->bit_pattern
== 0x001E )
603 return (CODING_MODE
)4;
605 theora_read(pbi
->opb
,1,&ret
);
606 pbi
->bit_pattern
= (pbi
->bit_pattern
<< 1) | ret
;
608 /* Do we have a match */
609 if ( pbi
->bit_pattern
== 0x003E )
610 return (CODING_MODE
)5;
612 theora_read(pbi
->opb
,1,&ret
);
613 pbi
->bit_pattern
= (pbi
->bit_pattern
<< 1) | ret
;
615 /* Do we have a match */
616 if ( pbi
->bit_pattern
== 0x007E )
617 return (CODING_MODE
)6;
619 return (CODING_MODE
)7;