41 |
* * |
* * |
42 |
* Revision history: * |
* Revision history: * |
43 |
* * |
* * |
44 |
|
* 06.05.2002 fixed fincr/fbase error * |
45 |
|
* 01.05.2002 added BVOP support to BitstreamWriteVopHeader * |
46 |
|
* 15.04.2002 rewrite log2bin use asm386 By MinChen <chenm001@163.com> * |
47 |
|
* 26.03.2002 interlacing support * |
48 |
* 03.03.2002 qmatrix writing * |
* 03.03.2002 qmatrix writing * |
49 |
* 03.03.2002 merged BITREADER and BITWRITER * |
* 03.03.2002 merged BITREADER and BITWRITER * |
50 |
* 30.02.2002 intra_dc_threshold support * |
* 30.02.2002 intra_dc_threshold support * |
56 |
|
|
57 |
#include "bitstream.h" |
#include "bitstream.h" |
58 |
#include "zigzag.h" |
#include "zigzag.h" |
59 |
|
#include "../quant/quant_matrix.h" |
60 |
|
|
61 |
|
|
62 |
static int __inline log2bin(int value) |
static uint32_t __inline log2bin(uint32_t value) |
63 |
{ |
{ |
64 |
|
/* Changed by Chenm001 */ |
65 |
|
#ifndef WIN32 |
66 |
int n = 0; |
int n = 0; |
67 |
while (value) |
while (value) |
68 |
{ |
{ |
70 |
n++; |
n++; |
71 |
} |
} |
72 |
return n; |
return n; |
73 |
|
#else |
74 |
|
__asm{ |
75 |
|
bsr eax,value |
76 |
|
inc eax |
77 |
|
} |
78 |
|
#endif |
79 |
} |
} |
80 |
|
|
81 |
|
|
92 |
}; |
}; |
93 |
|
|
94 |
|
|
95 |
|
void bs_get_matrix(Bitstream * bs, uint8_t * matrix) |
96 |
|
{ |
97 |
|
int i = 0; |
98 |
|
int last, value = 0; |
99 |
|
|
100 |
|
do |
101 |
|
{ |
102 |
|
last = value; |
103 |
|
value = BitstreamGetBits(bs, 8); |
104 |
|
matrix[ scan_tables[0][i++] ] = value; |
105 |
|
} |
106 |
|
while (value != 0 && i < 64); |
107 |
|
|
108 |
|
while (i < 64) |
109 |
|
{ |
110 |
|
matrix[ scan_tables[0][i++] ] = last; |
111 |
|
} |
112 |
|
} |
113 |
|
|
114 |
/* |
/* |
115 |
decode headers |
decode headers |
116 |
returns coding_type, or -1 if error |
returns coding_type, or -1 if error |
117 |
*/ |
*/ |
118 |
|
|
119 |
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) |
120 |
{ |
{ |
121 |
uint32_t vol_ver_id; |
uint32_t vol_ver_id; |
122 |
uint32_t time_inc_resolution; |
static uint32_t time_increment_resolution; |
123 |
uint32_t coding_type; |
uint32_t coding_type; |
124 |
uint32_t start_code; |
uint32_t start_code; |
125 |
|
uint32_t time_incr=0; |
126 |
|
int32_t time_increment; |
127 |
|
|
128 |
do |
do |
129 |
{ |
{ |
252 |
|
|
253 |
READ_MARKER(); |
READ_MARKER(); |
254 |
|
|
255 |
time_inc_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution |
// *************************** for decode B-frame time *********************** |
256 |
time_inc_resolution--; |
time_increment_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution |
257 |
if (time_inc_resolution > 0) |
time_increment_resolution--; |
258 |
|
//DEBUG1("time_increment_resolution=",time_increment_resolution); |
259 |
|
if (time_increment_resolution > 0) |
260 |
{ |
{ |
261 |
dec->time_inc_bits = log2bin(time_inc_resolution); |
dec->time_inc_bits = log2bin(time_increment_resolution); |
262 |
} |
} |
263 |
else |
else |
264 |
{ |
{ |
300 |
|
|
301 |
} |
} |
302 |
|
|
303 |
if (BitstreamGetBit(bs)) // interlaced |
if ((dec->interlacing = BitstreamGetBit(bs))) |
304 |
{ |
{ |
305 |
DEBUG("TODO: interlaced"); |
DEBUG("vol: interlacing"); |
|
// TODO |
|
|
return -1; |
|
306 |
} |
} |
307 |
|
|
308 |
if (!BitstreamGetBit(bs)) // obmc_disable |
if (!BitstreamGetBit(bs)) // obmc_disable |
348 |
{ |
{ |
349 |
if (BitstreamGetBit(bs)) // load_intra_quant_mat |
if (BitstreamGetBit(bs)) // load_intra_quant_mat |
350 |
{ |
{ |
351 |
DEBUG("TODO: load_intra_quant_mat"); |
uint8_t matrix[64]; |
352 |
// TODO |
bs_get_matrix(bs, matrix); |
353 |
return -1; |
set_intra_matrix(matrix); |
354 |
} |
} |
355 |
|
else |
356 |
|
set_intra_matrix(get_default_intra_matrix()); |
357 |
|
|
358 |
if (BitstreamGetBit(bs)) // load_inter_quant_mat |
if (BitstreamGetBit(bs)) // load_inter_quant_mat |
359 |
{ |
{ |
360 |
DEBUG("TODO: load_inter_quant_mat"); |
uint8_t matrix[64]; |
361 |
// TODO |
bs_get_matrix(bs, matrix); |
362 |
return -1; |
set_inter_matrix(matrix); |
363 |
} |
} |
364 |
|
else |
365 |
|
set_inter_matrix(get_default_inter_matrix()); |
366 |
|
|
367 |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) |
368 |
{ |
{ |
370 |
DEBUG("TODO: grayscale matrix stuff"); |
DEBUG("TODO: grayscale matrix stuff"); |
371 |
return -1; |
return -1; |
372 |
} |
} |
373 |
|
|
374 |
} |
} |
375 |
|
|
376 |
|
|
422 |
} |
} |
423 |
} |
} |
424 |
|
|
425 |
if (BitstreamGetBit(bs)) // scalability |
if ((dec->scalability=BitstreamGetBit(bs))) // scalability |
426 |
{ |
{ |
427 |
// TODO |
// TODO |
428 |
DEBUG("TODO: scalability"); |
DEBUG("TODO: scalability"); |
468 |
coding_type = BitstreamGetBits(bs, 2); // vop_coding_type |
coding_type = BitstreamGetBits(bs, 2); // vop_coding_type |
469 |
//DEBUG1("coding_type", coding_type); |
//DEBUG1("coding_type", coding_type); |
470 |
|
|
471 |
while (BitstreamGetBit(bs) != 0) ; // time_base |
// *************************** for decode B-frame time *********************** |
472 |
|
while (BitstreamGetBit(bs) != 0) // time_base |
473 |
|
time_incr++; |
474 |
|
|
475 |
READ_MARKER(); |
READ_MARKER(); |
476 |
|
|
478 |
//DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits)); |
//DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits)); |
479 |
if (dec->time_inc_bits) |
if (dec->time_inc_bits) |
480 |
{ |
{ |
481 |
BitstreamSkip(bs, dec->time_inc_bits); // vop_time_increment |
//BitstreamSkip(bs, dec->time_inc_bits); // vop_time_increment |
482 |
|
time_increment = (BitstreamGetBits(bs, dec->time_inc_bits)); // vop_time_increment |
483 |
|
} |
484 |
|
if(coding_type != B_VOP){ |
485 |
|
dec->last_time_base = dec->time_base; |
486 |
|
dec->time_base += time_incr; |
487 |
|
dec->time = dec->time_base*time_increment_resolution + time_increment; |
488 |
|
dec->time_pp= (uint32_t)(dec->time - dec->last_non_b_time); |
489 |
|
dec->last_non_b_time= dec->time; |
490 |
|
}else{ |
491 |
|
dec->time = (dec->last_time_base + time_incr)*time_increment_resolution + time_increment; |
492 |
|
dec->time_bp= (uint32_t)(dec->last_non_b_time - dec->time); |
493 |
} |
} |
494 |
|
//DEBUG1("time_increment=",time_increment); |
495 |
|
|
496 |
READ_MARKER(); |
READ_MARKER(); |
497 |
|
|
505 |
} |
} |
506 |
*/ |
*/ |
507 |
|
|
508 |
if (coding_type != I_VOP) |
// fix a little bug by MinChen <chenm002@163.com> |
509 |
|
if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) && (coding_type == P_VOP)) |
510 |
{ |
{ |
511 |
*rounding = BitstreamGetBit(bs); // rounding_type |
*rounding = BitstreamGetBit(bs); // rounding_type |
512 |
//DEBUG1("rounding", *rounding); |
//DEBUG1("rounding", *rounding); |
547 |
// intra_dc_vlc_threshold |
// intra_dc_vlc_threshold |
548 |
*intra_dc_threshold = intra_dc_threshold_table[ BitstreamGetBits(bs,3) ]; |
*intra_dc_threshold = intra_dc_threshold_table[ BitstreamGetBits(bs,3) ]; |
549 |
|
|
550 |
/* if (interlaced) |
if (dec->interlacing) |
551 |
{ |
{ |
552 |
BitstreamSkip(bs, 1); // top_field_first |
if ((dec->top_field_first = BitstreamGetBit(bs))) |
553 |
BitstreamSkip(bs, 1); // alternative_vertical_scan_flag |
{ |
554 |
*/ |
DEBUG("vop: top_field_first"); |
555 |
|
} |
556 |
|
if ((dec->alternate_vertical_scan = BitstreamGetBit(bs))) |
557 |
|
{ |
558 |
|
DEBUG("vop: alternate_vertical_scan"); |
559 |
|
} |
560 |
} |
} |
561 |
|
} |
562 |
|
|
563 |
|
if((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1) // vop_quant |
564 |
|
*quant = 1; |
565 |
|
|
|
*quant = BitstreamGetBits(bs, dec->quant_bits); // vop_quant |
|
566 |
//DEBUG1("quant", *quant); |
//DEBUG1("quant", *quant); |
567 |
|
|
568 |
if (coding_type != I_VOP) |
if (coding_type != I_VOP) |
569 |
{ |
{ |
570 |
*fcode = BitstreamGetBits(bs, 3); // fcode_forward |
*fcode_forward = BitstreamGetBits(bs, 3); // fcode_forward |
571 |
} |
} |
572 |
|
|
573 |
if (coding_type == B_VOP) |
if (coding_type == B_VOP) |
574 |
{ |
{ |
575 |
// *fcode_backward = BitstreamGetBits(bs, 3); // fcode_backward |
*fcode_backward = BitstreamGetBits(bs, 3); // fcode_backward |
576 |
|
} |
577 |
|
if (!dec->scalability){ |
578 |
|
if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) && (coding_type != I_VOP)){ |
579 |
|
BitstreamSkip(bs, 1); // vop_shape_coding_type |
580 |
|
} |
581 |
} |
} |
582 |
return coding_type; |
return coding_type; |
583 |
} |
} |
627 |
write vol header |
write vol header |
628 |
*/ |
*/ |
629 |
void BitstreamWriteVolHeader(Bitstream * const bs, |
void BitstreamWriteVolHeader(Bitstream * const bs, |
630 |
const int width, |
const MBParam * pParam, const FRAMEINFO * frame) |
|
const int height, |
|
|
const int quant_type) |
|
631 |
{ |
{ |
632 |
// video object_start_code & vo_id |
// video object_start_code & vo_id |
633 |
BitstreamPad(bs); |
BitstreamPad(bs); |
652 |
25fps res=25 inc=1 |
25fps res=25 inc=1 |
653 |
29.97fps res=30000 inc=1001 |
29.97fps res=30000 inc=1001 |
654 |
*/ |
*/ |
655 |
|
#ifdef BFRAMES |
656 |
|
BitstreamPutBits(bs, pParam->fbase, 16); |
657 |
|
#else |
658 |
BitstreamPutBits(bs, 2, 16); |
BitstreamPutBits(bs, 2, 16); |
659 |
|
#endif |
660 |
|
|
661 |
WRITE_MARKER(); |
WRITE_MARKER(); |
662 |
|
|
667 |
// BitstreamPutBits(bs, 0, 15); |
// BitstreamPutBits(bs, 0, 15); |
668 |
|
|
669 |
WRITE_MARKER(); |
WRITE_MARKER(); |
670 |
BitstreamPutBits(bs, width, 13); // width |
BitstreamPutBits(bs, pParam->width, 13); // width |
671 |
WRITE_MARKER(); |
WRITE_MARKER(); |
672 |
BitstreamPutBits(bs, height, 13); // height |
BitstreamPutBits(bs, pParam->height, 13); // height |
673 |
WRITE_MARKER(); |
WRITE_MARKER(); |
674 |
|
|
675 |
BitstreamPutBit(bs, 0); // interlace |
BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING); // interlace |
676 |
BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) |
BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) |
677 |
BitstreamPutBit(bs, 0); // sprite_enable |
BitstreamPutBit(bs, 0); // sprite_enable |
678 |
BitstreamPutBit(bs, 0); // not_in_bit |
BitstreamPutBit(bs, 0); // not_in_bit |
679 |
|
|
680 |
// quant_type 0=h.263 1=mpeg4(quantizer tables) |
// quant_type 0=h.263 1=mpeg4(quantizer tables) |
681 |
BitstreamPutBit(bs, quant_type); |
BitstreamPutBit(bs, pParam->m_quant_type); |
682 |
/* |
|
683 |
if (quant_type) |
if (pParam->m_quant_type) |
684 |
{ |
{ |
685 |
BitstreamPutBit(bs, qmatrix->custom_intra); // load_intra_quant_mat |
BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat |
686 |
if (qmatrix->custom_intra) |
if (get_intra_matrix_status()) |
687 |
{ |
{ |
688 |
bs_put_matrix(bs, qmatrix->intra); |
bs_put_matrix(bs, get_intra_matrix()); |
689 |
} |
} |
690 |
|
|
691 |
BitstreamPutBit(bs, qmatrix->custom_inter); // load_inter_quant_mat |
BitstreamPutBit(bs, get_inter_matrix_status()); // load_inter_quant_mat |
692 |
if (qmatrix->custom_inter) |
if (get_inter_matrix_status()) |
693 |
{ |
{ |
694 |
bs_put_matrix(bs, qmatrix->inter); |
bs_put_matrix(bs, get_inter_matrix()); |
695 |
} |
} |
696 |
|
|
697 |
} |
} |
|
*/ |
|
|
if (quant_type) |
|
|
{ |
|
|
BitstreamPutBit(bs, 0); // load_intra_quant_mat |
|
|
BitstreamPutBit(bs, 0); // load_inter_quant_mat |
|
|
} |
|
698 |
|
|
699 |
BitstreamPutBit(bs, 1); // complexity_estimation_disable |
BitstreamPutBit(bs, 1); // complexity_estimation_disable |
700 |
BitstreamPutBit(bs, 1); // resync_marker_disable |
BitstreamPutBit(bs, 1); // resync_marker_disable |
712 |
(decoder uses these values to determine precise time since last resync) |
(decoder uses these values to determine precise time since last resync) |
713 |
*/ |
*/ |
714 |
void BitstreamWriteVopHeader(Bitstream * const bs, |
void BitstreamWriteVopHeader(Bitstream * const bs, |
715 |
VOP_TYPE prediction_type, |
const MBParam * pParam, |
716 |
const int rounding_type, |
const FRAMEINFO * frame) |
|
const uint32_t quant, |
|
|
const uint32_t fcode) |
|
717 |
{ |
{ |
718 |
|
#ifdef BFRAMES |
719 |
|
uint32_t i; |
720 |
|
#endif |
721 |
BitstreamPad(bs); |
BitstreamPad(bs); |
722 |
BitstreamPutBits(bs, VOP_START_CODE, 32); |
BitstreamPutBits(bs, VOP_START_CODE, 32); |
723 |
|
|
724 |
BitstreamPutBits(bs, prediction_type, 2); |
BitstreamPutBits(bs, frame->coding_type, 2); |
725 |
|
|
726 |
// time_base = 0 write n x PutBit(1), PutBit(0) |
// time_base = 0 write n x PutBit(1), PutBit(0) |
727 |
|
#ifdef BFRAMES |
728 |
|
for (i = 0; i < frame->seconds; i++) |
729 |
|
{ |
730 |
|
BitstreamPutBit(bs, 1); |
731 |
|
} |
732 |
|
BitstreamPutBit(bs, 0); |
733 |
|
#else |
734 |
BitstreamPutBits(bs, 0, 1); |
BitstreamPutBits(bs, 0, 1); |
735 |
|
#endif |
736 |
|
|
737 |
WRITE_MARKER(); |
WRITE_MARKER(); |
738 |
|
|
739 |
// time_increment: value=nth_of_sec, nbits = log2(resolution) |
// time_increment: value=nth_of_sec, nbits = log2(resolution) |
740 |
|
#ifdef BFRAMES |
741 |
|
BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase)); |
742 |
|
dprintf("[%i:%i] %c\n", frame->seconds, frame->ticks, frame->coding_type == I_VOP ? 'I' : frame->coding_type == P_VOP ? 'P' : 'B'); |
743 |
|
#else |
744 |
BitstreamPutBits(bs, 1, 1); |
BitstreamPutBits(bs, 1, 1); |
745 |
|
#endif |
746 |
|
|
747 |
WRITE_MARKER(); |
WRITE_MARKER(); |
748 |
|
|
749 |
BitstreamPutBits(bs, 1, 1); // vop_coded |
BitstreamPutBits(bs, 1, 1); // vop_coded |
750 |
|
|
751 |
if (prediction_type != I_VOP) |
if (frame->coding_type == P_VOP) |
752 |
BitstreamPutBits(bs, rounding_type, 1); |
BitstreamPutBits(bs, frame->rounding_type, 1); |
753 |
|
|
754 |
BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold |
BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold |
755 |
|
|
756 |
BitstreamPutBits(bs, quant, 5); // quantizer |
if (frame->global_flags & XVID_INTERLACING) |
757 |
|
{ |
758 |
|
BitstreamPutBit(bs, 1); // top field first |
759 |
|
BitstreamPutBit(bs, 0); // alternate vertical scan |
760 |
|
} |
761 |
|
|
762 |
|
BitstreamPutBits(bs, frame->quant, 5); // quantizer |
763 |
|
|
764 |
|
if (frame->coding_type != I_VOP) |
765 |
|
BitstreamPutBits(bs, frame->fcode, 3); // forward_fixed_code |
766 |
|
|
767 |
|
if (frame->coding_type == B_VOP) |
768 |
|
BitstreamPutBits(bs, frame->bcode, 3); // backward_fixed_code |
769 |
|
|
|
if (prediction_type != I_VOP) |
|
|
BitstreamPutBits(bs, fcode, 3); // fixed_code = [1,4] |
|
770 |
} |
} |