1 |
/****************************************************************************** |
/***************************************************************************** |
2 |
* * |
* |
3 |
* This file is part of XviD, a free MPEG-4 video encoder/decoder * |
* XVID MPEG-4 VIDEO CODEC |
4 |
* * |
* - Bitstream reader/writer functions - |
5 |
* XviD is an implementation of a part of one or more MPEG-4 Video tools * |
* |
6 |
* as specified in ISO/IEC 14496-2 standard. Those intending to use this * |
* Copyright (C) 2001-2002 - Peter Ross <pross@xvid.org> |
7 |
* software module in hardware or software products are advised that its * |
* |
8 |
* use may infringe existing patents or copyrights, and any such use * |
* This program is an implementation of a part of one or more MPEG-4 |
9 |
* would be at such party's own risk. The original developer of this * |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
10 |
* software module and his/her company, and subsequent editors and their * |
* to use this software module in hardware or software products are |
11 |
* companies, will have no liability for use of this software or * |
* advised that its use may infringe existing patents or copyrights, and |
12 |
* modifications or derivatives thereof. * |
* any such use would be at such party's own risk. The original |
13 |
* * |
* developer of this software module and his/her company, and subsequent |
14 |
* XviD is free software; you can redistribute it and/or modify it * |
* editors and their companies, will have no liability for use of this |
15 |
* under the terms of the GNU General Public License as published by * |
* software or modifications or derivatives thereof. |
16 |
* the Free Software Foundation; either version 2 of the License, or * |
* |
17 |
* (at your option) any later version. * |
* This program is free software ; you can redistribute it and/or modify |
18 |
* * |
* it under the terms of the GNU General Public License as published by |
19 |
* XviD is distributed in the hope that it will be useful, but * |
* the Free Software Foundation ; either version 2 of the License, or |
20 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
* (at your option) any later version. |
21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
* |
22 |
* GNU General Public License for more details. * |
* This program is distributed in the hope that it will be useful, |
23 |
* * |
* but WITHOUT ANY WARRANTY ; without even the implied warranty of |
24 |
* You should have received a copy of the GNU General Public License * |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
25 |
* along with this program; if not, write to the Free Software * |
* GNU General Public License for more details. |
26 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
* |
27 |
* * |
* You should have received a copy of the GNU General Public License |
28 |
******************************************************************************/ |
* along with this program ; if not, write to the Free Software |
29 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
30 |
/****************************************************************************** |
* |
31 |
* * |
* $Id$ |
32 |
* bitstream.c * |
* |
33 |
* * |
****************************************************************************/ |
|
* Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au> * |
|
|
* * |
|
|
* For more information visit the XviD homepage: http://www.xvid.org * |
|
|
* * |
|
|
******************************************************************************/ |
|
|
|
|
|
/****************************************************************************** |
|
|
* * |
|
|
* Revision history: * |
|
|
* * |
|
|
* 28.10.2002 GMC support - gruel * |
|
|
* 04.10.2002 qpel support - Isibaar * |
|
|
* 11.07.2002 add VOP width & height return to dec when dec->width * |
|
|
* or dec->height is 0 (for use in examples/ex1.c) * |
|
|
* MinChen <chenm001@163.com> * |
|
|
* 22.05.2002 bs_put_matrix fix * |
|
|
* 20.05.2002 added BitstreamWriteUserData * |
|
|
* 19.06.2002 Fix a little bug in use custom quant matrix * |
|
|
* MinChen <chenm001@163.com> * |
|
|
* 08.05.2002 add low_delay support for B_VOP decode * |
|
|
* MinChen <chenm001@163.com> * |
|
|
* 06.05.2002 low_delay * |
|
|
* 06.05.2002 fixed fincr/fbase error * |
|
|
* 01.05.2002 added BVOP support to BitstreamWriteVopHeader * |
|
|
* 15.04.2002 rewrite log2bin use asm386 By MinChen <chenm001@163.com> * |
|
|
* 26.03.2002 interlacing support * |
|
|
* 03.03.2002 qmatrix writing * |
|
|
* 03.03.2002 merged BITREADER and BITWRITER * |
|
|
* 30.02.2002 intra_dc_threshold support * |
|
|
* 04.12.2001 support for additional headers * |
|
|
* 16.12.2001 inital version * |
|
|
* * |
|
|
******************************************************************************/ |
|
|
|
|
34 |
|
|
35 |
#include "bitstream.h" |
#include "bitstream.h" |
36 |
#include "zigzag.h" |
#include "zigzag.h" |
37 |
#include "../quant/quant_matrix.h" |
#include "../quant/quant_matrix.h" |
|
#include "mbcoding.h" |
|
38 |
|
|
39 |
|
/***************************************************************************** |
40 |
|
* Functions |
41 |
|
****************************************************************************/ |
42 |
|
|
43 |
static uint32_t __inline |
static uint32_t __inline |
44 |
log2bin(uint32_t value) |
log2bin(uint32_t value) |
161 |
uint32_t coding_type; |
uint32_t coding_type; |
162 |
uint32_t start_code; |
uint32_t start_code; |
163 |
uint32_t time_incr = 0; |
uint32_t time_incr = 0; |
164 |
int32_t time_increment; |
int32_t time_increment = 0; |
165 |
|
|
166 |
do { |
do { |
167 |
BitstreamByteAlign(bs); |
BitstreamByteAlign(bs); |
417 |
|
|
418 |
|
|
419 |
if (vol_ver_id != 1) { |
if (vol_ver_id != 1) { |
420 |
DEBUG("QUARTERPEL BITSTREAM"); |
DPRINTF(DPRINTF_DEBUG, "QUARTERPEL BITSTREAM"); |
421 |
dec->quarterpel = BitstreamGetBit(bs); // quarter_sample |
dec->quarterpel = BitstreamGetBit(bs); // quarter_sample |
422 |
} |
} |
423 |
else |
else |
549 |
|
|
550 |
// fix a little bug by MinChen <chenm002@163.com> |
// fix a little bug by MinChen <chenm002@163.com> |
551 |
if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) && |
if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) && |
552 |
( (coding_type == P_VOP) || (coding_type == S_VOP) ) ) { |
(coding_type == P_VOP)) { |
553 |
*rounding = BitstreamGetBit(bs); // rounding_type |
*rounding = BitstreamGetBit(bs); // rounding_type |
554 |
DPRINTF(DPRINTF_HEADER, "rounding %i", *rounding); |
DPRINTF(DPRINTF_HEADER, "rounding %i", *rounding); |
555 |
} |
} |
590 |
*intra_dc_threshold = |
*intra_dc_threshold = |
591 |
intra_dc_threshold_table[BitstreamGetBits(bs, 3)]; |
intra_dc_threshold_table[BitstreamGetBits(bs, 3)]; |
592 |
|
|
|
dec->top_field_first = 0; |
|
|
dec->alternate_vertical_scan = 0; |
|
|
|
|
593 |
if (dec->interlacing) { |
if (dec->interlacing) { |
594 |
dec->top_field_first = BitstreamGetBit(bs); |
dec->top_field_first = BitstreamGetBit(bs); |
595 |
DPRINTF(DPRINTF_HEADER, "interlace top_field_first %i", dec->top_field_first); |
DPRINTF(DPRINTF_HEADER, "interlace top_field_first %i", dec->top_field_first); |
669 |
void |
void |
670 |
BitstreamWriteVolHeader(Bitstream * const bs, |
BitstreamWriteVolHeader(Bitstream * const bs, |
671 |
const MBParam * pParam, |
const MBParam * pParam, |
672 |
const FRAMEINFO * const frame) |
const FRAMEINFO * frame) |
673 |
{ |
{ |
|
int vol_ver_id=1; |
|
|
|
|
|
if ( (pParam->m_quarterpel) || (frame->global_flags & XVID_GMC) ) |
|
|
vol_ver_id = 2; |
|
|
|
|
674 |
// video object_start_code & vo_id |
// video object_start_code & vo_id |
675 |
BitstreamPad(bs); |
BitstreamPad(bs); |
676 |
BitstreamPutBits(bs, VO_START_CODE, 27); |
BitstreamPutBits(bs, VO_START_CODE, 27); |
682 |
|
|
683 |
BitstreamPutBit(bs, 0); // random_accessible_vol |
BitstreamPutBit(bs, 0); // random_accessible_vol |
684 |
BitstreamPutBits(bs, 0, 8); // video_object_type_indication |
BitstreamPutBits(bs, 0, 8); // video_object_type_indication |
|
|
|
|
if (vol_ver_id == 1) |
|
|
{ |
|
685 |
BitstreamPutBit(bs, 0); // is_object_layer_identified (0=not given) |
BitstreamPutBit(bs, 0); // is_object_layer_identified (0=not given) |
|
} |
|
|
else |
|
|
{ |
|
|
BitstreamPutBit(bs, 1); // is_object_layer_identified |
|
|
BitstreamPutBits(bs, vol_ver_id, 4); // vol_ver_id == 2 |
|
|
BitstreamPutBits(bs, 4, 3); // vol_ver_priority (1==lowest, 7==highest) ?? |
|
|
} |
|
|
|
|
686 |
BitstreamPutBits(bs, 1, 4); // aspect_ratio_info (1=1:1) |
BitstreamPutBits(bs, 1, 4); // aspect_ratio_info (1=1:1) |
687 |
|
|
688 |
BitstreamPutBit(bs, 1); // vol_control_parameters |
BitstreamPutBit(bs, 1); // vol_control_parameters |
689 |
BitstreamPutBits(bs, 1, 2); // chroma_format 1="4:2:0" |
BitstreamPutBits(bs, 1, 2); // chroma_format 1="4:2:0" |
690 |
|
|
|
if (pParam->max_bframes > 0) { |
|
|
BitstreamPutBit(bs, 0); // low_delay |
|
|
} else |
|
|
{ |
|
691 |
BitstreamPutBit(bs, 1); // low_delay |
BitstreamPutBit(bs, 1); // low_delay |
692 |
} |
|
693 |
BitstreamPutBit(bs, 0); // vbv_parameters (0=not given) |
BitstreamPutBit(bs, 0); // vbv_parameters (0=not given) |
694 |
|
|
695 |
BitstreamPutBits(bs, 0, 2); // video_object_layer_shape (0=rectangular) |
BitstreamPutBits(bs, 0, 2); // video_object_layer_shape (0=rectangular) |
696 |
|
|
697 |
WRITE_MARKER(); |
WRITE_MARKER(); |
698 |
|
|
699 |
/* time_increment_resolution; ignored by current decore versions |
/* |
700 |
eg. 2fps res=2 inc=1 |
* time_increment_resolution; ignored by current decore versions |
701 |
25fps res=25 inc=1 |
* eg. 2fps res=2 inc=1 |
702 |
29.97fps res=30000 inc=1001 |
* 25fps res=25 inc=1 |
703 |
|
* 29.97fps res=30000 inc=1001 |
704 |
*/ |
*/ |
705 |
BitstreamPutBits(bs, pParam->fbase, 16); |
BitstreamPutBits(bs, pParam->fbase, 16); |
706 |
|
|
707 |
|
|
708 |
WRITE_MARKER(); |
WRITE_MARKER(); |
709 |
|
|
710 |
BitstreamPutBit(bs, 1); // fixed_vop_rate = 1 |
BitstreamPutBit(bs, 1); // fixed_vop_rate = 1 |
718 |
|
|
719 |
BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING); // interlace |
BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING); // interlace |
720 |
BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) |
BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) |
721 |
|
BitstreamPutBit(bs, 0); // sprite_enable |
722 |
if (vol_ver_id != 1) |
BitstreamPutBit(bs, 0); // not_in_bit |
|
{ if (frame->global_flags & XVID_GMC) |
|
|
{ BitstreamPutBits(bs, 2, 2); // sprite_enable=='GMC' |
|
|
BitstreamPutBits(bs, 2, 6); // no_of_sprite_warping_points |
|
|
BitstreamPutBits(bs, 3, 2); // sprite_warping_accuracy 0==1/2, 1=1/4, 2=1/8, 3=1/16 |
|
|
BitstreamPutBit(bs, 0); // sprite_brightness_change (not supported) |
|
|
|
|
|
/* currently we use no_of_sprite_warping_points==2, sprite_warping_accuracy==3 |
|
|
for DivX5 compatability */ |
|
|
|
|
|
} else |
|
|
BitstreamPutBits(bs, 0, 2); // sprite_enable==off |
|
|
} |
|
|
else |
|
|
BitstreamPutBit(bs, 0); // sprite_enable==off |
|
|
|
|
|
BitstreamPutBit(bs, 0); // not_8_bit |
|
723 |
|
|
724 |
// quant_type 0=h.263 1=mpeg4(quantizer tables) |
// quant_type 0=h.263 1=mpeg4(quantizer tables) |
725 |
BitstreamPutBit(bs, pParam->m_quant_type); |
BitstreamPutBit(bs, pParam->m_quant_type); |
737 |
|
|
738 |
} |
} |
739 |
|
|
|
if (vol_ver_id != 1) { |
|
|
if (pParam->m_quarterpel) |
|
|
BitstreamPutBit(bs, 1); // quarterpel |
|
|
else |
|
|
BitstreamPutBit(bs, 0); // no quarterpel |
|
|
} |
|
|
|
|
740 |
BitstreamPutBit(bs, 1); // complexity_estimation_disable |
BitstreamPutBit(bs, 1); // complexity_estimation_disable |
741 |
BitstreamPutBit(bs, 1); // resync_marker_disable |
BitstreamPutBit(bs, 1); // resync_marker_disable |
742 |
BitstreamPutBit(bs, 0); // data_partitioned |
BitstreamPutBit(bs, 0); // data_partitioned |
|
|
|
|
if (vol_ver_id != 1) |
|
|
{ |
|
|
BitstreamPutBit(bs, 0); // newpred_enable |
|
|
BitstreamPutBit(bs, 0); // reduced_resolution_vop_enabled |
|
|
} |
|
|
|
|
743 |
BitstreamPutBit(bs, 0); // scalability |
BitstreamPutBit(bs, 0); // scalability |
|
|
|
744 |
} |
} |
745 |
|
|
746 |
|
|
747 |
/* |
/* |
748 |
write vop header |
write vop header |
749 |
|
|
750 |
|
NOTE: doesnt handle bother with time_base & time_inc |
751 |
|
time_base = n seconds since last resync (eg. last iframe) |
752 |
|
time_inc = nth of a second since last resync |
753 |
|
(decoder uses these values to determine precise time since last resync) |
754 |
*/ |
*/ |
755 |
void |
void |
756 |
BitstreamWriteVopHeader(Bitstream * const bs, |
BitstreamWriteVopHeader(Bitstream * const bs, |
757 |
const MBParam * pParam, |
const MBParam * pParam, |
758 |
const FRAMEINFO * const frame, |
const FRAMEINFO * frame, |
759 |
int vop_coded) |
int vop_coded) |
760 |
{ |
{ |
761 |
uint32_t i; |
uint32_t i; |
765 |
|
|
766 |
BitstreamPutBits(bs, frame->coding_type, 2); |
BitstreamPutBits(bs, frame->coding_type, 2); |
767 |
|
|
768 |
|
// time_base = 0 write n x PutBit(1), PutBit(0) |
769 |
for (i = 0; i < frame->seconds; i++) { |
for (i = 0; i < frame->seconds; i++) { |
770 |
BitstreamPutBit(bs, 1); |
BitstreamPutBit(bs, 1); |
771 |
} |
} |
774 |
WRITE_MARKER(); |
WRITE_MARKER(); |
775 |
|
|
776 |
// time_increment: value=nth_of_sec, nbits = log2(resolution) |
// time_increment: value=nth_of_sec, nbits = log2(resolution) |
|
|
|
777 |
BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase)); |
BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase)); |
|
/*DPRINTF("[%i:%i] %c\n", frame->seconds, frame->ticks, |
|
|
frame->coding_type == I_VOP ? 'I' : frame->coding_type == |
|
|
P_VOP ? 'P' : 'B');*/ |
|
778 |
|
|
779 |
WRITE_MARKER(); |
WRITE_MARKER(); |
780 |
|
|
785 |
|
|
786 |
BitstreamPutBits(bs, 1, 1); // vop_coded |
BitstreamPutBits(bs, 1, 1); // vop_coded |
787 |
|
|
788 |
if ( (frame->coding_type == P_VOP) || (frame->coding_type == S_VOP) ) |
if (frame->coding_type == P_VOP) |
789 |
BitstreamPutBits(bs, frame->rounding_type, 1); |
BitstreamPutBits(bs, frame->rounding_type, 1); |
790 |
|
|
791 |
BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold |
BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold |
792 |
|
|
793 |
if (frame->global_flags & XVID_INTERLACING) { |
if (frame->global_flags & XVID_INTERLACING) { |
794 |
BitstreamPutBit(bs, (frame->global_flags & XVID_TOPFIELDFIRST)); |
BitstreamPutBit(bs, 1); // top field first |
795 |
BitstreamPutBit(bs, (frame->global_flags & XVID_ALTERNATESCAN)); |
BitstreamPutBit(bs, 0); // alternate vertical scan |
|
} |
|
|
|
|
|
if (frame->coding_type == S_VOP) { |
|
|
if (1) { // no_of_sprite_warping_points>=1 |
|
|
if (pParam->m_quarterpel) |
|
|
bs_put_spritetrajectory(bs, frame->GMC_MV.x/2 ); // du[0] |
|
|
else |
|
|
bs_put_spritetrajectory(bs, frame->GMC_MV.x ); // du[0] |
|
|
WRITE_MARKER(); |
|
|
|
|
|
if (pParam->m_quarterpel) |
|
|
bs_put_spritetrajectory(bs, frame->GMC_MV.y/2 ); // dv[0] |
|
|
else |
|
|
bs_put_spritetrajectory(bs, frame->GMC_MV.y ); // dv[0] |
|
|
WRITE_MARKER(); |
|
|
} |
|
|
/* GMC is halfpel in bitstream, even though GMC_MV was pseudo-qpel (2*halfpel) */ |
|
|
|
|
|
if (2) { // no_of_sprite_warping_points>=2 (for DivX5 compat) |
|
|
bs_put_spritetrajectory(bs, 0 ); |
|
|
WRITE_MARKER(); |
|
|
bs_put_spritetrajectory(bs, 0 ); |
|
|
WRITE_MARKER(); |
|
|
} |
|
|
// no support for brightness_change! |
|
796 |
} |
} |
797 |
|
|
798 |
BitstreamPutBits(bs, frame->quant, 5); // quantizer |
BitstreamPutBits(bs, frame->quant, 5); // quantizer |
804 |
BitstreamPutBits(bs, frame->bcode, 3); // backward_fixed_code |
BitstreamPutBits(bs, frame->bcode, 3); // backward_fixed_code |
805 |
|
|
806 |
} |
} |
|
|
|
|
void |
|
|
BitstreamWriteUserData(Bitstream * const bs, |
|
|
uint8_t * data, |
|
|
const int length) |
|
|
{ |
|
|
int i; |
|
|
|
|
|
BitstreamPad(bs); |
|
|
BitstreamPutBits(bs, USERDATA_START_CODE, 32); |
|
|
|
|
|
for (i = 0; i < length; i++) { |
|
|
BitstreamPutBits(bs, data[i], 8); |
|
|
} |
|
|
|
|
|
} |
|