41 |
* * |
* * |
42 |
* Revision history: * |
* Revision history: * |
43 |
* * |
* * |
44 |
|
* 01.05.2002 added BVOP support to BitstreamWriteVopHeader |
45 |
|
* 15.04.2002 rewrite log2bin use asm386 By MinChen <chenm001@163.com> * |
46 |
|
* 26.03.2002 interlacing support * |
47 |
* 03.03.2002 qmatrix writing * |
* 03.03.2002 qmatrix writing * |
48 |
* 03.03.2002 merged BITREADER and BITWRITER * |
* 03.03.2002 merged BITREADER and BITWRITER * |
49 |
* 30.02.2002 intra_dc_threshold support * |
* 30.02.2002 intra_dc_threshold support * |
57 |
#include "zigzag.h" |
#include "zigzag.h" |
58 |
#include "../quant/quant_matrix.h" |
#include "../quant/quant_matrix.h" |
59 |
|
|
60 |
|
|
61 |
static int __inline log2bin(int value) |
static int __inline log2bin(int value) |
62 |
{ |
{ |
63 |
|
/* Changed by Chenm001 */ |
64 |
|
#ifndef WIN32 |
65 |
int n = 0; |
int n = 0; |
66 |
while (value) |
while (value) |
67 |
{ |
{ |
69 |
n++; |
n++; |
70 |
} |
} |
71 |
return n; |
return n; |
72 |
|
#else |
73 |
|
__asm{ |
74 |
|
bsr eax,value |
75 |
|
inc eax |
76 |
|
} |
77 |
|
#endif |
78 |
} |
} |
79 |
|
|
80 |
|
|
115 |
returns coding_type, or -1 if error |
returns coding_type, or -1 if error |
116 |
*/ |
*/ |
117 |
|
|
118 |
int BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding, uint32_t * quant, uint32_t * fcode, uint32_t * intra_dc_threshold) |
int BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding, uint32_t * quant, uint32_t * fcode_forward, uint32_t * fcode_backward, uint32_t * intra_dc_threshold) |
119 |
{ |
{ |
120 |
uint32_t vol_ver_id; |
uint32_t vol_ver_id; |
121 |
uint32_t time_inc_resolution; |
static uint32_t time_increment_resolution; |
122 |
uint32_t coding_type; |
uint32_t coding_type; |
123 |
uint32_t start_code; |
uint32_t start_code; |
124 |
|
uint32_t time_incr=0; |
125 |
|
int32_t time_increment; |
126 |
|
|
127 |
do |
do |
128 |
{ |
{ |
251 |
|
|
252 |
READ_MARKER(); |
READ_MARKER(); |
253 |
|
|
254 |
time_inc_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution |
// *************************** for decode B-frame time *********************** |
255 |
time_inc_resolution--; |
time_increment_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution |
256 |
if (time_inc_resolution > 0) |
time_increment_resolution--; |
257 |
|
//DEBUG1("time_increment_resolution=",time_increment_resolution); |
258 |
|
if (time_increment_resolution > 0) |
259 |
{ |
{ |
260 |
dec->time_inc_bits = log2bin(time_inc_resolution); |
dec->time_inc_bits = log2bin(time_increment_resolution); |
261 |
} |
} |
262 |
else |
else |
263 |
{ |
{ |
299 |
|
|
300 |
} |
} |
301 |
|
|
302 |
if (BitstreamGetBit(bs)) // interlaced |
if ((dec->interlacing = BitstreamGetBit(bs))) |
303 |
{ |
{ |
304 |
DEBUG("TODO: interlaced"); |
DEBUG("vol: interlacing"); |
|
// TODO |
|
|
return -1; |
|
305 |
} |
} |
306 |
|
|
307 |
if (!BitstreamGetBit(bs)) // obmc_disable |
if (!BitstreamGetBit(bs)) // obmc_disable |
421 |
} |
} |
422 |
} |
} |
423 |
|
|
424 |
if (BitstreamGetBit(bs)) // scalability |
if ((dec->scalability=BitstreamGetBit(bs))) // scalability |
425 |
{ |
{ |
426 |
// TODO |
// TODO |
427 |
DEBUG("TODO: scalability"); |
DEBUG("TODO: scalability"); |
467 |
coding_type = BitstreamGetBits(bs, 2); // vop_coding_type |
coding_type = BitstreamGetBits(bs, 2); // vop_coding_type |
468 |
//DEBUG1("coding_type", coding_type); |
//DEBUG1("coding_type", coding_type); |
469 |
|
|
470 |
while (BitstreamGetBit(bs) != 0) ; // time_base |
// *************************** for decode B-frame time *********************** |
471 |
|
while (BitstreamGetBit(bs) != 0) // time_base |
472 |
|
time_incr++; |
473 |
|
|
474 |
READ_MARKER(); |
READ_MARKER(); |
475 |
|
|
477 |
//DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits)); |
//DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits)); |
478 |
if (dec->time_inc_bits) |
if (dec->time_inc_bits) |
479 |
{ |
{ |
480 |
BitstreamSkip(bs, dec->time_inc_bits); // vop_time_increment |
//BitstreamSkip(bs, dec->time_inc_bits); // vop_time_increment |
481 |
|
time_increment = (BitstreamGetBits(bs, dec->time_inc_bits)); // vop_time_increment |
482 |
|
} |
483 |
|
if(coding_type != B_VOP){ |
484 |
|
dec->last_time_base = dec->time_base; |
485 |
|
dec->time_base += time_incr; |
486 |
|
dec->time = dec->time_base*time_increment_resolution + time_increment; |
487 |
|
dec->time_pp= (uint32_t)(dec->time - dec->last_non_b_time); |
488 |
|
dec->last_non_b_time= dec->time; |
489 |
|
}else{ |
490 |
|
dec->time = (dec->last_time_base + time_incr)*time_increment_resolution + time_increment; |
491 |
|
dec->time_bp= (uint32_t)(dec->last_non_b_time - dec->time); |
492 |
} |
} |
493 |
|
//DEBUG1("time_increment=",time_increment); |
494 |
|
|
495 |
READ_MARKER(); |
READ_MARKER(); |
496 |
|
|
504 |
} |
} |
505 |
*/ |
*/ |
506 |
|
|
507 |
if (coding_type != I_VOP) |
// fix a little bug by MinChen <chenm002@163.com> |
508 |
|
if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) && (coding_type == P_VOP)) |
509 |
{ |
{ |
510 |
*rounding = BitstreamGetBit(bs); // rounding_type |
*rounding = BitstreamGetBit(bs); // rounding_type |
511 |
//DEBUG1("rounding", *rounding); |
//DEBUG1("rounding", *rounding); |
546 |
// intra_dc_vlc_threshold |
// intra_dc_vlc_threshold |
547 |
*intra_dc_threshold = intra_dc_threshold_table[ BitstreamGetBits(bs,3) ]; |
*intra_dc_threshold = intra_dc_threshold_table[ BitstreamGetBits(bs,3) ]; |
548 |
|
|
549 |
/* if (interlaced) |
if (dec->interlacing) |
550 |
{ |
{ |
551 |
BitstreamSkip(bs, 1); // top_field_first |
if ((dec->top_field_first = BitstreamGetBit(bs))) |
552 |
BitstreamSkip(bs, 1); // alternative_vertical_scan_flag |
{ |
553 |
*/ |
DEBUG("vop: top_field_first"); |
554 |
|
} |
555 |
|
if ((dec->alternate_vertical_scan = BitstreamGetBit(bs))) |
556 |
|
{ |
557 |
|
DEBUG("vop: alternate_vertical_scan"); |
558 |
|
} |
559 |
|
} |
560 |
} |
} |
561 |
|
|
562 |
*quant = BitstreamGetBits(bs, dec->quant_bits); // vop_quant |
*quant = BitstreamGetBits(bs, dec->quant_bits); // vop_quant |
564 |
|
|
565 |
if (coding_type != I_VOP) |
if (coding_type != I_VOP) |
566 |
{ |
{ |
567 |
*fcode = BitstreamGetBits(bs, 3); // fcode_forward |
*fcode_forward = BitstreamGetBits(bs, 3); // fcode_forward |
568 |
} |
} |
569 |
|
|
570 |
if (coding_type == B_VOP) |
if (coding_type == B_VOP) |
571 |
{ |
{ |
572 |
// *fcode_backward = BitstreamGetBits(bs, 3); // fcode_backward |
*fcode_backward = BitstreamGetBits(bs, 3); // fcode_backward |
573 |
|
} |
574 |
|
if (!dec->scalability){ |
575 |
|
if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) && (coding_type != I_VOP)){ |
576 |
|
BitstreamSkip(bs, 1); // vop_shape_coding_type |
577 |
|
} |
578 |
} |
} |
579 |
return coding_type; |
return coding_type; |
580 |
} |
} |
624 |
write vol header |
write vol header |
625 |
*/ |
*/ |
626 |
void BitstreamWriteVolHeader(Bitstream * const bs, |
void BitstreamWriteVolHeader(Bitstream * const bs, |
627 |
const int width, |
const MBParam * pParam, const FRAMEINFO * frame) |
|
const int height, |
|
|
const int quant_type) |
|
628 |
{ |
{ |
629 |
// video object_start_code & vo_id |
// video object_start_code & vo_id |
630 |
BitstreamPad(bs); |
BitstreamPad(bs); |
660 |
// BitstreamPutBits(bs, 0, 15); |
// BitstreamPutBits(bs, 0, 15); |
661 |
|
|
662 |
WRITE_MARKER(); |
WRITE_MARKER(); |
663 |
BitstreamPutBits(bs, width, 13); // width |
BitstreamPutBits(bs, pParam->width, 13); // width |
664 |
WRITE_MARKER(); |
WRITE_MARKER(); |
665 |
BitstreamPutBits(bs, height, 13); // height |
BitstreamPutBits(bs, pParam->height, 13); // height |
666 |
WRITE_MARKER(); |
WRITE_MARKER(); |
667 |
|
|
668 |
BitstreamPutBit(bs, 0); // interlace |
BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING); // interlace |
669 |
BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) |
BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) |
670 |
BitstreamPutBit(bs, 0); // sprite_enable |
BitstreamPutBit(bs, 0); // sprite_enable |
671 |
BitstreamPutBit(bs, 0); // not_in_bit |
BitstreamPutBit(bs, 0); // not_in_bit |
672 |
|
|
673 |
// quant_type 0=h.263 1=mpeg4(quantizer tables) |
// quant_type 0=h.263 1=mpeg4(quantizer tables) |
674 |
BitstreamPutBit(bs, quant_type); |
BitstreamPutBit(bs, pParam->m_quant_type); |
675 |
|
|
676 |
if (quant_type) |
if (pParam->m_quant_type) |
677 |
{ |
{ |
678 |
BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat |
BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat |
679 |
if (get_intra_matrix_status()) |
if (get_intra_matrix_status()) |
705 |
(decoder uses these values to determine precise time since last resync) |
(decoder uses these values to determine precise time since last resync) |
706 |
*/ |
*/ |
707 |
void BitstreamWriteVopHeader(Bitstream * const bs, |
void BitstreamWriteVopHeader(Bitstream * const bs, |
708 |
VOP_TYPE prediction_type, |
const MBParam * pParam, |
709 |
const int rounding_type, |
const FRAMEINFO * frame) |
|
const uint32_t quant, |
|
|
const uint32_t fcode) |
|
710 |
{ |
{ |
711 |
|
#ifdef BFRAMES |
712 |
|
uint32_t i; |
713 |
|
#endif |
714 |
BitstreamPad(bs); |
BitstreamPad(bs); |
715 |
BitstreamPutBits(bs, VOP_START_CODE, 32); |
BitstreamPutBits(bs, VOP_START_CODE, 32); |
716 |
|
|
717 |
BitstreamPutBits(bs, prediction_type, 2); |
BitstreamPutBits(bs, frame->coding_type, 2); |
718 |
|
|
719 |
// time_base = 0 write n x PutBit(1), PutBit(0) |
// time_base = 0 write n x PutBit(1), PutBit(0) |
720 |
|
#ifdef BFRAMES |
721 |
|
for (i = 0; i < frame->seconds; i++) |
722 |
|
{ |
723 |
|
BitstreamPutBit(bs, 1); |
724 |
|
} |
725 |
|
BitstreamPutBit(bs, 0); |
726 |
|
#else |
727 |
BitstreamPutBits(bs, 0, 1); |
BitstreamPutBits(bs, 0, 1); |
728 |
|
#endif |
729 |
|
|
730 |
WRITE_MARKER(); |
WRITE_MARKER(); |
731 |
|
|
732 |
// time_increment: value=nth_of_sec, nbits = log2(resolution) |
// time_increment: value=nth_of_sec, nbits = log2(resolution) |
733 |
|
#ifdef BFRAMES |
734 |
|
BitstreamPutBits(bs, frame->ticks, 5); |
735 |
|
dprintf("[%i:%i] %c\n", frame->seconds, frame->ticks, frame->coding_type == I_VOP ? 'I' : frame->coding_type == P_VOP ? 'P' : 'B'); |
736 |
|
#else |
737 |
BitstreamPutBits(bs, 1, 1); |
BitstreamPutBits(bs, 1, 1); |
738 |
|
#endif |
739 |
|
|
740 |
WRITE_MARKER(); |
WRITE_MARKER(); |
741 |
|
|
742 |
BitstreamPutBits(bs, 1, 1); // vop_coded |
BitstreamPutBits(bs, 1, 1); // vop_coded |
743 |
|
|
744 |
if (prediction_type != I_VOP) |
if (frame->coding_type != I_VOP) |
745 |
BitstreamPutBits(bs, rounding_type, 1); |
BitstreamPutBits(bs, frame->rounding_type, 1); |
746 |
|
|
747 |
BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold |
BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold |
748 |
|
|
749 |
BitstreamPutBits(bs, quant, 5); // quantizer |
if (frame->global_flags & XVID_INTERLACING) |
750 |
|
{ |
751 |
|
BitstreamPutBit(bs, 1); // top field first |
752 |
|
BitstreamPutBit(bs, 0); // alternate vertical scan |
753 |
|
} |
754 |
|
|
755 |
|
BitstreamPutBits(bs, frame->quant, 5); // quantizer |
756 |
|
|
757 |
|
if (frame->coding_type != I_VOP) |
758 |
|
BitstreamPutBits(bs, frame->fcode, 3); // forward_fixed_code |
759 |
|
|
760 |
|
if (frame->coding_type == B_VOP) |
761 |
|
BitstreamPutBits(bs, frame->bcode, 3); // backward_fixed_code |
762 |
|
|
|
if (prediction_type != I_VOP) |
|
|
BitstreamPutBits(bs, fcode, 3); // fixed_code = [1,4] |
|
763 |
} |
} |