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: * |
|
|
* * |
|
|
* 05.01.2003 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 * |
|
|
* * |
|
|
******************************************************************************/ |
|
|
|
|
|
|
|
|
#include <string.h> |
|
|
#include <stdio.h> |
|
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) |
99 |
// for BVOP addbits == max(fcode,bcode) - 1 |
// for BVOP addbits == max(fcode,bcode) - 1 |
100 |
// returns mbpos |
// returns mbpos |
101 |
int |
int |
102 |
read_video_packet_header(Bitstream *bs, |
read_video_packet_header(Bitstream *bs, const int addbits, int * quant) |
|
DECODER * dec, |
|
|
const int addbits, |
|
|
int * quant, |
|
|
int * fcode_forward, |
|
|
int * fcode_backward, |
|
|
int * intra_dc_threshold) |
|
103 |
{ |
{ |
104 |
int startcode_bits = NUMBITS_VP_RESYNC_MARKER + addbits; |
int nbits; |
|
int mbnum_bits = log2bin(dec->mb_width * dec->mb_height - 1); |
|
105 |
int mbnum; |
int mbnum; |
106 |
int hec = 0; |
int hec; |
107 |
|
|
108 |
|
nbits = NUMBITS_VP_RESYNC_MARKER + addbits; |
109 |
|
|
110 |
BitstreamSkip(bs, BitstreamNumBitsToByteAlign(bs)); |
BitstreamSkip(bs, BitstreamNumBitsToByteAlign(bs)); |
111 |
BitstreamSkip(bs, startcode_bits); |
BitstreamSkip(bs, nbits); |
112 |
|
|
113 |
DPRINTF(DPRINTF_STARTCODE, "<video_packet_header>"); |
DPRINTF(DPRINTF_STARTCODE, "<video_packet_header>"); |
114 |
|
|
115 |
if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) |
// if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { |
116 |
{ |
// hec |
117 |
hec = BitstreamGetBit(bs); /* header_extension_code */ |
// vop_width |
118 |
if (hec && !(dec->sprite_enable == SPRITE_STATIC /* && current_coding_type = I_VOP */)) |
// marker_bit |
119 |
{ |
// vop_height |
120 |
BitstreamSkip(bs, 13); /* vop_width */ |
// marker_bit |
|
READ_MARKER(); |
|
|
BitstreamSkip(bs, 13); /* vop_height */ |
|
|
READ_MARKER(); |
|
|
BitstreamSkip(bs, 13); /* vop_horizontal_mc_spatial_ref */ |
|
|
READ_MARKER(); |
|
|
BitstreamSkip(bs, 13); /* vop_vertical_mc_spatial_ref */ |
|
|
READ_MARKER(); |
|
|
} |
|
|
} |
|
121 |
|
|
122 |
mbnum = BitstreamGetBits(bs, mbnum_bits); /* macroblock_number */ |
//} |
123 |
|
|
124 |
|
mbnum = BitstreamGetBits(bs, 9); |
125 |
DPRINTF(DPRINTF_HEADER, "mbnum %i", mbnum); |
DPRINTF(DPRINTF_HEADER, "mbnum %i", mbnum); |
126 |
|
|
127 |
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) |
// if (dec->shape != VIDOBJLAY_SHAPE_BINARYONLY) |
128 |
{ |
*quant = BitstreamGetBits(bs, 5); |
|
*quant = BitstreamGetBits(bs, dec->quant_bits); /* quant_scale */ |
|
129 |
DPRINTF(DPRINTF_HEADER, "quant %i", *quant); |
DPRINTF(DPRINTF_HEADER, "quant %i", *quant); |
|
} |
|
|
|
|
|
if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) |
|
|
hec = BitstreamGetBit(bs); /* header_extension_code */ |
|
|
|
|
130 |
|
|
131 |
|
// if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) |
132 |
|
hec = BitstreamGetBit(bs); |
133 |
DPRINTF(DPRINTF_HEADER, "header_extension_code %i", hec); |
DPRINTF(DPRINTF_HEADER, "header_extension_code %i", hec); |
134 |
if (hec) |
// if (hec) |
135 |
{ |
// .. decoder hec-header ... |
|
int time_base; |
|
|
int time_increment; |
|
|
int coding_type; |
|
|
|
|
|
for (time_base=0; BitstreamGetBit(bs)!=0; time_base++); /* modulo_time_base */ |
|
|
READ_MARKER(); |
|
|
if (dec->time_inc_bits) |
|
|
time_increment = (BitstreamGetBits(bs, dec->time_inc_bits)); /* vop_time_increment */ |
|
|
READ_MARKER(); |
|
|
DPRINTF(DPRINTF_HEADER,"time %i:%i", time_base, time_increment); |
|
|
|
|
|
coding_type = BitstreamGetBits(bs, 2); |
|
|
DPRINTF(DPRINTF_HEADER,"coding_type %i", coding_type); |
|
|
|
|
|
if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) |
|
|
{ |
|
|
BitstreamSkip(bs, 1); /* change_conv_ratio_disable */ |
|
|
if (coding_type != I_VOP) |
|
|
BitstreamSkip(bs, 1); /* vop_shape_coding_type */ |
|
|
} |
|
|
|
|
|
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) |
|
|
{ |
|
|
*intra_dc_threshold = intra_dc_threshold_table[BitstreamGetBits(bs, 3)]; |
|
|
|
|
|
if (dec->sprite_enable == SPRITE_GMC && coding_type == S_VOP && |
|
|
dec->sprite_warping_points > 0) |
|
|
{ |
|
|
// TODO: sprite trajectory |
|
|
} |
|
|
if (dec->reduced_resolution_enable && |
|
|
dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR && |
|
|
(coding_type == P_VOP || coding_type == I_VOP)) |
|
|
{ |
|
|
BitstreamSkip(bs, 1); /* XXX: vop_reduced_resolution */ |
|
|
} |
|
|
|
|
|
if (coding_type != I_VOP && fcode_forward) |
|
|
{ |
|
|
*fcode_forward = BitstreamGetBits(bs, 3); |
|
|
DPRINTF(DPRINTF_HEADER,"fcode_forward %i", *fcode_forward); |
|
|
} |
|
|
|
|
|
if (coding_type == B_VOP && fcode_backward) |
|
|
{ |
|
|
*fcode_backward = BitstreamGetBits(bs, 3); |
|
|
DPRINTF(DPRINTF_HEADER,"fcode_backward %i", *fcode_backward); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (dec->newpred_enable) |
|
|
{ |
|
|
int vop_id; |
|
|
int vop_id_for_prediction; |
|
|
|
|
|
vop_id = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15)); |
|
|
DPRINTF(DPRINTF_HEADER, "vop_id %i", vop_id); |
|
|
if (BitstreamGetBit(bs)) /* vop_id_for_prediction_indication */ |
|
|
{ |
|
|
vop_id_for_prediction = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15)); |
|
|
DPRINTF(DPRINTF_HEADER, "vop_id_for_prediction %i", vop_id_for_prediction); |
|
|
} |
|
|
READ_MARKER(); |
|
|
} |
|
136 |
|
|
137 |
return mbnum; |
return mbnum; |
138 |
} |
} |
139 |
|
|
140 |
|
|
141 |
|
|
|
|
|
|
/* vol estimation header */ |
|
|
static void |
|
|
read_vol_complexity_estimation_header(Bitstream * bs, DECODER * dec) |
|
|
{ |
|
|
ESTIMATION * e = &dec->estimation; |
|
|
|
|
|
e->method = BitstreamGetBits(bs, 2); /* estimation_method */ |
|
|
DPRINTF(DPRINTF_HEADER,"+ complexity_estimation_header; method=%i", e->method); |
|
|
|
|
|
if (e->method == 0 || e->method == 1) |
|
|
{ |
|
|
if (!BitstreamGetBit(bs)) /* shape_complexity_estimation_disable */ |
|
|
{ |
|
|
e->opaque = BitstreamGetBit(bs); /* opaque */ |
|
|
e->transparent = BitstreamGetBit(bs); /* transparent */ |
|
|
e->intra_cae = BitstreamGetBit(bs); /* intra_cae */ |
|
|
e->inter_cae = BitstreamGetBit(bs); /* inter_cae */ |
|
|
e->no_update = BitstreamGetBit(bs); /* no_update */ |
|
|
e->upsampling = BitstreamGetBit(bs); /* upsampling */ |
|
|
} |
|
|
|
|
|
if (!BitstreamGetBit(bs)) /* texture_complexity_estimation_set_1_disable */ |
|
|
{ |
|
|
e->intra_blocks = BitstreamGetBit(bs); /* intra_blocks */ |
|
|
e->inter_blocks = BitstreamGetBit(bs); /* inter_blocks */ |
|
|
e->inter4v_blocks = BitstreamGetBit(bs); /* inter4v_blocks */ |
|
|
e->not_coded_blocks = BitstreamGetBit(bs); /* not_coded_blocks */ |
|
|
} |
|
|
} |
|
|
|
|
|
READ_MARKER(); |
|
|
|
|
|
if (!BitstreamGetBit(bs)) /* texture_complexity_estimation_set_2_disable */ |
|
|
{ |
|
|
e->dct_coefs = BitstreamGetBit(bs); /* dct_coefs */ |
|
|
e->dct_lines = BitstreamGetBit(bs); /* dct_lines */ |
|
|
e->vlc_symbols = BitstreamGetBit(bs); /* vlc_symbols */ |
|
|
e->vlc_bits = BitstreamGetBit(bs); /* vlc_bits */ |
|
|
} |
|
|
|
|
|
if (!BitstreamGetBit(bs)) /* motion_compensation_complexity_disable */ |
|
|
{ |
|
|
e->apm = BitstreamGetBit(bs); /* apm */ |
|
|
e->npm = BitstreamGetBit(bs); /* npm */ |
|
|
e->interpolate_mc_q = BitstreamGetBit(bs); /* interpolate_mc_q */ |
|
|
e->forw_back_mc_q = BitstreamGetBit(bs); /* forw_back_mc_q */ |
|
|
e->halfpel2 = BitstreamGetBit(bs); /* halfpel2 */ |
|
|
e->halfpel4 = BitstreamGetBit(bs); /* halfpel4 */ |
|
|
} |
|
|
|
|
|
READ_MARKER(); |
|
|
|
|
|
if (e->method == 1) |
|
|
{ |
|
|
if (!BitstreamGetBit(bs)) /* version2_complexity_estimation_disable */ |
|
|
{ |
|
|
e->sadct = BitstreamGetBit(bs); /* sadct */ |
|
|
e->quarterpel = BitstreamGetBit(bs); /* quarterpel */ |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/* vop estimation header */ |
|
|
static void |
|
|
read_vop_complexity_estimation_header(Bitstream * bs, DECODER * dec, int coding_type) |
|
|
{ |
|
|
ESTIMATION * e = &dec->estimation; |
|
|
|
|
|
if (e->method == 0 || e->method == 1) |
|
|
{ |
|
|
if (coding_type == I_VOP) { |
|
|
if (e->opaque) BitstreamSkip(bs, 8); /* dcecs_opaque */ |
|
|
if (e->transparent) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->intra_cae) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->inter_cae) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->no_update) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->upsampling) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->intra_blocks) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->not_coded_blocks) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->dct_coefs) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->dct_lines) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->vlc_symbols) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->vlc_bits) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->sadct) BitstreamSkip(bs, 8); /* */ |
|
|
} |
|
|
|
|
|
if (coding_type == P_VOP) { |
|
|
if (e->opaque) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->transparent) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->intra_cae) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->inter_cae) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->no_update) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->upsampling) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->intra_blocks) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->not_coded_blocks) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->dct_coefs) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->dct_lines) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->vlc_symbols) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->vlc_bits) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->inter_blocks) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->inter4v_blocks) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->apm) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->npm) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->forw_back_mc_q) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->halfpel2) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->halfpel4) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->sadct) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->quarterpel) BitstreamSkip(bs, 8); /* */ |
|
|
} |
|
|
if (coding_type == B_VOP) { |
|
|
if (e->opaque) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->transparent) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->intra_cae) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->inter_cae) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->no_update) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->upsampling) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->intra_blocks) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->not_coded_blocks) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->dct_coefs) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->dct_lines) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->vlc_symbols) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->vlc_bits) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->inter_blocks) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->inter4v_blocks) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->apm) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->npm) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->forw_back_mc_q) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->halfpel2) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->halfpel4) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->interpolate_mc_q) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->sadct) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->quarterpel) BitstreamSkip(bs, 8); /* */ |
|
|
} |
|
|
|
|
|
if (coding_type == S_VOP && dec->sprite_enable == SPRITE_STATIC) { |
|
|
if (e->intra_blocks) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->not_coded_blocks) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->dct_coefs) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->dct_lines) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->vlc_symbols) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->vlc_bits) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->inter_blocks) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->inter4v_blocks) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->apm) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->npm) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->forw_back_mc_q) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->halfpel2) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->halfpel4) BitstreamSkip(bs, 8); /* */ |
|
|
if (e->interpolate_mc_q) BitstreamSkip(bs, 8); /* */ |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
/* |
/* |
143 |
decode headers |
decode headers |
144 |
returns coding_type, or -1 if error |
returns coding_type, or -1 if error |
151 |
BitstreamReadHeaders(Bitstream * bs, |
BitstreamReadHeaders(Bitstream * bs, |
152 |
DECODER * dec, |
DECODER * dec, |
153 |
uint32_t * rounding, |
uint32_t * rounding, |
|
uint32_t * reduced_resolution, |
|
154 |
uint32_t * quant, |
uint32_t * quant, |
155 |
uint32_t * fcode_forward, |
uint32_t * fcode_forward, |
156 |
uint32_t * fcode_backward, |
uint32_t * fcode_backward, |
157 |
uint32_t * intra_dc_threshold, |
uint32_t * intra_dc_threshold) |
|
WARPPOINTS *gmc_warp) |
|
158 |
{ |
{ |
159 |
uint32_t vol_ver_id; |
uint32_t vol_ver_id; |
160 |
|
static uint32_t time_increment_resolution; |
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; |
|
int resize = 0; |
|
165 |
|
|
166 |
do { |
do { |
|
|
|
167 |
BitstreamByteAlign(bs); |
BitstreamByteAlign(bs); |
168 |
start_code = BitstreamShowBits(bs, 32); |
start_code = BitstreamShowBits(bs, 32); |
169 |
|
|
185 |
DPRINTF(DPRINTF_STARTCODE, "</visual_object_sequence>"); |
DPRINTF(DPRINTF_STARTCODE, "</visual_object_sequence>"); |
186 |
|
|
187 |
} else if (start_code == VISOBJ_START_CODE) { |
} else if (start_code == VISOBJ_START_CODE) { |
|
int visobj_ver_id; |
|
188 |
|
|
189 |
DPRINTF(DPRINTF_STARTCODE, "<visual_object>"); |
DPRINTF(DPRINTF_STARTCODE, "<visual_object>"); |
190 |
|
|
191 |
BitstreamSkip(bs, 32); // visual_object_start_code |
BitstreamSkip(bs, 32); // visual_object_start_code |
192 |
if (BitstreamGetBit(bs)) // is_visual_object_identified |
if (BitstreamGetBit(bs)) // is_visual_object_identified |
193 |
{ |
{ |
194 |
visobj_ver_id = BitstreamGetBits(bs, 4); // visual_object_ver_id |
vol_ver_id = BitstreamGetBits(bs, 4); // visual_object_ver_id |
195 |
DPRINTF(DPRINTF_HEADER,"visobj_ver_id %i", visobj_ver_id); |
DPRINTF(DPRINTF_HEADER,"ver_id %i", vol_ver_id); |
196 |
BitstreamSkip(bs, 3); // visual_object_priority |
BitstreamSkip(bs, 3); // visual_object_priority |
197 |
} else { |
} else { |
198 |
visobj_ver_id = 1; |
vol_ver_id = 1; |
199 |
} |
} |
200 |
|
|
201 |
if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) // visual_object_type |
if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) // visual_object_type |
237 |
BitstreamSkip(bs, 1); // random_accessible_vol |
BitstreamSkip(bs, 1); // random_accessible_vol |
238 |
|
|
239 |
// video_object_type_indication |
// video_object_type_indication |
240 |
if (BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_SIMPLE && |
if (BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_SIMPLE && BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_CORE && BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_MAIN && BitstreamShowBits(bs, 8) != 0) // BUGGY DIVX |
|
BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_CORE && |
|
|
BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_MAIN && |
|
|
BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_ACE && |
|
|
BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_ART_SIMPLE && |
|
|
BitstreamShowBits(bs, 8) != 0) // BUGGY DIVX |
|
241 |
{ |
{ |
242 |
DPRINTF(DPRINTF_ERROR,"video_object_type_indication %i not supported ", |
DPRINTF(DPRINTF_ERROR,"video_object_type_indication %i not supported ", |
243 |
BitstreamShowBits(bs, 8)); |
BitstreamShowBits(bs, 8)); |
256 |
vol_ver_id = 1; |
vol_ver_id = 1; |
257 |
} |
} |
258 |
|
|
259 |
dec->aspect_ratio = BitstreamGetBits(bs, 4); |
if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR) // aspect_ratio_info |
|
|
|
|
if (dec->aspect_ratio == VIDOBJLAY_AR_EXTPAR) // aspect_ratio_info |
|
260 |
{ |
{ |
261 |
DPRINTF(DPRINTF_HEADER, "+ aspect_ratio_info"); |
DPRINTF(DPRINTF_HEADER, "+ aspect_ratio_info"); |
262 |
dec->par_width = BitstreamGetBits(bs, 8); // par_width |
BitstreamSkip(bs, 8); // par_width |
263 |
dec->par_height = BitstreamGetBits(bs, 8); // par_height |
BitstreamSkip(bs, 8); // par_height |
264 |
} |
} |
265 |
|
|
266 |
if (BitstreamGetBit(bs)) // vol_control_parameters |
if (BitstreamGetBit(bs)) // vol_control_parameters |
271 |
DPRINTF(DPRINTF_HEADER, "low_delay %i", dec->low_delay); |
DPRINTF(DPRINTF_HEADER, "low_delay %i", dec->low_delay); |
272 |
if (BitstreamGetBit(bs)) // vbv_parameters |
if (BitstreamGetBit(bs)) // vbv_parameters |
273 |
{ |
{ |
|
unsigned int bitrate; |
|
|
unsigned int buffer_size; |
|
|
unsigned int occupancy; |
|
|
|
|
274 |
DPRINTF(DPRINTF_HEADER,"+ vbv_parameters"); |
DPRINTF(DPRINTF_HEADER,"+ vbv_parameters"); |
275 |
|
BitstreamSkip(bs, 15); // first_half_bitrate |
|
bitrate = BitstreamGetBits(bs,15) << 15; // first_half_bit_rate |
|
276 |
READ_MARKER(); |
READ_MARKER(); |
277 |
bitrate |= BitstreamGetBits(bs,15); // latter_half_bit_rate |
BitstreamSkip(bs, 15); // latter_half_bitrate |
278 |
READ_MARKER(); |
READ_MARKER(); |
279 |
|
BitstreamSkip(bs, 15); // first_half_vbv_buffer_size |
|
buffer_size = BitstreamGetBits(bs, 15) << 3; // first_half_vbv_buffer_size |
|
280 |
READ_MARKER(); |
READ_MARKER(); |
281 |
buffer_size |= BitstreamGetBits(bs, 3); // latter_half_vbv_buffer_size |
BitstreamSkip(bs, 3); // latter_half_vbv_buffer_size |
282 |
|
BitstreamSkip(bs, 11); // first_half_vbv_occupancy |
|
occupancy = BitstreamGetBits(bs, 11) << 15; // first_half_vbv_occupancy |
|
283 |
READ_MARKER(); |
READ_MARKER(); |
284 |
occupancy |= BitstreamGetBits(bs, 15); // latter_half_vbv_occupancy |
BitstreamSkip(bs, 15); // latter_half_vbv_occupancy |
285 |
READ_MARKER(); |
READ_MARKER(); |
286 |
|
|
|
DPRINTF(DPRINTF_HEADER,"bitrate %d (unit=400 bps)", bitrate); |
|
|
DPRINTF(DPRINTF_HEADER,"buffer_size %d (unit=16384 bits)", buffer_size); |
|
|
DPRINTF(DPRINTF_HEADER,"occupancy %d (unit=64 bits)", occupancy); |
|
287 |
} |
} |
|
}else{ |
|
|
dec->low_delay = dec->low_delay_default; |
|
288 |
} |
} |
289 |
|
|
290 |
dec->shape = BitstreamGetBits(bs, 2); // video_object_layer_shape |
dec->shape = BitstreamGetBits(bs, 2); // video_object_layer_shape |
|
|
|
291 |
DPRINTF(DPRINTF_HEADER, "shape %i", dec->shape); |
DPRINTF(DPRINTF_HEADER, "shape %i", dec->shape); |
|
if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) |
|
|
{ |
|
|
DPRINTF(DPRINTF_ERROR,"non-rectangular shapes are not supported"); |
|
|
} |
|
292 |
|
|
293 |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) { |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) { |
294 |
BitstreamSkip(bs, 4); // video_object_layer_shape_extension |
BitstreamSkip(bs, 4); // video_object_layer_shape_extension |
297 |
READ_MARKER(); |
READ_MARKER(); |
298 |
|
|
299 |
// *************************** for decode B-frame time *********************** |
// *************************** for decode B-frame time *********************** |
300 |
dec->time_inc_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution |
time_increment_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution |
|
DPRINTF(DPRINTF_HEADER,"vop_time_increment_resolution %i", dec->time_inc_resolution); |
|
301 |
|
|
302 |
// dec->time_inc_resolution--; |
DPRINTF(DPRINTF_HEADER,"vop_time_increment_resolution %i", time_increment_resolution); |
303 |
|
|
304 |
if (dec->time_inc_resolution > 0) { |
// time_increment_resolution--; |
305 |
dec->time_inc_bits = log2bin(dec->time_inc_resolution-1); |
|
306 |
|
if (time_increment_resolution > 0) { |
307 |
|
dec->time_inc_bits = log2bin(time_increment_resolution-1); |
308 |
} else { |
} else { |
309 |
// dec->time_inc_bits = 0; |
// dec->time_inc_bits = 0; |
310 |
// for "old" xvid compatibility, set time_inc_bits = 1 |
// for "old" xvid compatibility, set time_inc_bits = 1 |
333 |
DPRINTF(DPRINTF_HEADER, "width %i", width); |
DPRINTF(DPRINTF_HEADER, "width %i", width); |
334 |
DPRINTF(DPRINTF_HEADER, "height %i", height); |
DPRINTF(DPRINTF_HEADER, "height %i", height); |
335 |
|
|
336 |
if (dec->width != width || dec->height != height) |
// for auto set width & height |
337 |
{ |
if (dec->width == 0) |
|
if (dec->fixed_dimensions) |
|
|
{ |
|
|
DPRINTF(DPRINTF_ERROR, "XVID_DEC_PARAM width/height does not match bitstream"); |
|
|
return -1; |
|
|
} |
|
|
resize = 1; |
|
338 |
dec->width = width; |
dec->width = width; |
339 |
|
if (dec->height == 0) |
340 |
dec->height = height; |
dec->height = height; |
341 |
|
|
342 |
|
if (width != dec->width || height != dec->height) { |
343 |
|
DPRINTF(DPRINTF_ERROR, "XVID_DEC_PARAM width/height does not match bitstream"); |
344 |
|
return -1; |
345 |
} |
} |
346 |
|
|
347 |
} |
} |
348 |
|
|
349 |
dec->interlacing = BitstreamGetBit(bs); |
dec->interlacing = BitstreamGetBit(bs); |
350 |
DPRINTF(DPRINTF_HEADER, "interlacing %i", dec->interlacing); |
DPRINTF(DPRINTF_HEADER, "interlace", dec->interlacing); |
351 |
|
|
352 |
if (!BitstreamGetBit(bs)) // obmc_disable |
if (!BitstreamGetBit(bs)) // obmc_disable |
353 |
{ |
{ |
356 |
// fucking divx4.02 has this enabled |
// fucking divx4.02 has this enabled |
357 |
} |
} |
358 |
|
|
359 |
dec->sprite_enable = BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2)); // sprite_enable |
if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2))) // sprite_enable |
|
|
|
|
if (dec->sprite_enable == SPRITE_STATIC || dec->sprite_enable == SPRITE_GMC) |
|
|
{ |
|
|
int low_latency_sprite_enable; |
|
|
|
|
|
if (dec->sprite_enable != SPRITE_GMC) |
|
360 |
{ |
{ |
361 |
int sprite_width; |
DPRINTF(DPRINTF_ERROR, "spriate_enabled not supported"); |
362 |
int sprite_height; |
return -1; |
|
int sprite_left_coord; |
|
|
int sprite_top_coord; |
|
|
sprite_width = BitstreamGetBits(bs, 13); // sprite_width |
|
|
READ_MARKER(); |
|
|
sprite_height = BitstreamGetBits(bs, 13); // sprite_height |
|
|
READ_MARKER(); |
|
|
sprite_left_coord = BitstreamGetBits(bs, 13); // sprite_left_coordinate |
|
|
READ_MARKER(); |
|
|
sprite_top_coord = BitstreamGetBits(bs, 13); // sprite_top_coordinate |
|
|
READ_MARKER(); |
|
|
} |
|
|
dec->sprite_warping_points = BitstreamGetBits(bs, 6); // no_of_sprite_warping_points |
|
|
dec->sprite_warping_accuracy = BitstreamGetBits(bs, 2); // sprite_warping_accuracy |
|
|
dec->sprite_brightness_change = BitstreamGetBits(bs, 1); // brightness_change |
|
|
if (dec->sprite_enable != SPRITE_GMC) |
|
|
{ |
|
|
low_latency_sprite_enable = BitstreamGetBits(bs, 1); // low_latency_sprite_enable |
|
|
} |
|
363 |
} |
} |
364 |
|
|
365 |
if (vol_ver_id != 1 && |
if (vol_ver_id != 1 && |
417 |
|
|
418 |
|
|
419 |
if (vol_ver_id != 1) { |
if (vol_ver_id != 1) { |
420 |
|
DPRINTF(DPRINTF_DEBUG, "QUARTERPEL BITSTREAM"); |
421 |
dec->quarterpel = BitstreamGetBit(bs); // quarter_sample |
dec->quarterpel = BitstreamGetBit(bs); // quarter_sample |
|
DPRINTF(DPRINTF_HEADER,"quarterpel %i", dec->quarterpel); |
|
422 |
} |
} |
423 |
else |
else |
424 |
dec->quarterpel = 0; |
dec->quarterpel = 0; |
425 |
|
|
426 |
|
|
427 |
dec->complexity_estimation_disable = BitstreamGetBit(bs); /* complexity estimation disable */ |
if (!BitstreamGetBit(bs)) // complexity_estimation_disable |
|
if (!dec->complexity_estimation_disable) |
|
428 |
{ |
{ |
429 |
read_vol_complexity_estimation_header(bs, dec); |
DPRINTF(DPRINTF_ERROR, "complexity_estimation not supported"); |
430 |
|
return -1; |
431 |
} |
} |
432 |
|
|
433 |
BitstreamSkip(bs, 1); // resync_marker_disable |
BitstreamSkip(bs, 1); // resync_marker_disable |
439 |
} |
} |
440 |
|
|
441 |
if (vol_ver_id != 1) { |
if (vol_ver_id != 1) { |
442 |
dec->newpred_enable = BitstreamGetBit(bs); |
if (BitstreamGetBit(bs)) // newpred_enable |
|
if (dec->newpred_enable) // newpred_enable |
|
443 |
{ |
{ |
444 |
DPRINTF(DPRINTF_HEADER, "+ newpred_enable"); |
DPRINTF(DPRINTF_HEADER, "+ newpred_enable"); |
445 |
BitstreamSkip(bs, 2); // requested_upstream_message_type |
BitstreamSkip(bs, 2); // requested_upstream_message_type |
446 |
BitstreamSkip(bs, 1); // newpred_segment_type |
BitstreamSkip(bs, 1); // newpred_segment_type |
447 |
} |
} |
448 |
dec->reduced_resolution_enable = BitstreamGetBit(bs); /* reduced_resolution_vop_enable */ |
if (BitstreamGetBit(bs)) // reduced_resolution_vop_enable |
|
DPRINTF(DPRINTF_HEADER, "reduced_resolution_enable %i", dec->reduced_resolution_enable); |
|
|
} |
|
|
else |
|
449 |
{ |
{ |
450 |
dec->newpred_enable = 0; |
DPRINTF(DPRINTF_ERROR, "reduced_resolution_vop not supported"); |
451 |
dec->reduced_resolution_enable = 0; |
return -1; |
452 |
|
} |
453 |
} |
} |
454 |
|
|
455 |
dec->scalability = BitstreamGetBit(bs); /* scalability */ |
if ((dec->scalability = BitstreamGetBit(bs))) // scalability |
|
if (dec->scalability) |
|
456 |
{ |
{ |
457 |
DPRINTF(DPRINTF_ERROR, "scalability not supported"); |
DPRINTF(DPRINTF_ERROR, "scalability not supported"); |
|
BitstreamSkip(bs, 1); /* hierarchy_type */ |
|
|
BitstreamSkip(bs, 4); /* ref_layer_id */ |
|
|
BitstreamSkip(bs, 1); /* ref_layer_sampling_direc */ |
|
|
BitstreamSkip(bs, 5); /* hor_sampling_factor_n */ |
|
|
BitstreamSkip(bs, 5); /* hor_sampling_factor_m */ |
|
|
BitstreamSkip(bs, 5); /* vert_sampling_factor_n */ |
|
|
BitstreamSkip(bs, 5); /* vert_sampling_factor_m */ |
|
|
BitstreamSkip(bs, 1); /* enhancement_type */ |
|
|
if(dec->shape == VIDOBJLAY_SHAPE_BINARY /* && hierarchy_type==0 */) { |
|
|
BitstreamSkip(bs, 1); /* use_ref_shape */ |
|
|
BitstreamSkip(bs, 1); /* use_ref_texture */ |
|
|
BitstreamSkip(bs, 5); /* shape_hor_sampling_factor_n */ |
|
|
BitstreamSkip(bs, 5); /* shape_hor_sampling_factor_m */ |
|
|
BitstreamSkip(bs, 5); /* shape_vert_sampling_factor_n */ |
|
|
BitstreamSkip(bs, 5); /* shape_vert_sampling_factor_m */ |
|
|
} |
|
458 |
return -1; |
return -1; |
459 |
} |
} |
460 |
} else // dec->shape == BINARY_ONLY |
} else // dec->shape == BINARY_ONLY |
461 |
{ |
{ |
462 |
if (vol_ver_id != 1) { |
if (vol_ver_id != 1) { |
463 |
dec->scalability = BitstreamGetBit(bs); /* scalability */ |
if (BitstreamGetBit(bs)) // scalability |
|
if (dec->scalability) |
|
464 |
{ |
{ |
465 |
DPRINTF(DPRINTF_ERROR, "scalability not supported"); |
DPRINTF(DPRINTF_ERROR, "scalability not supported"); |
|
BitstreamSkip(bs, 4); /* ref_layer_id */ |
|
|
BitstreamSkip(bs, 5); /* hor_sampling_factor_n */ |
|
|
BitstreamSkip(bs, 5); /* hor_sampling_factor_m */ |
|
|
BitstreamSkip(bs, 5); /* vert_sampling_factor_n */ |
|
|
BitstreamSkip(bs, 5); /* vert_sampling_factor_m */ |
|
466 |
return -1; |
return -1; |
467 |
} |
} |
468 |
} |
} |
470 |
|
|
471 |
} |
} |
472 |
|
|
|
return (resize ? -3 : -2 ); /* VOL */ |
|
|
|
|
473 |
} else if (start_code == GRPOFVOP_START_CODE) { |
} else if (start_code == GRPOFVOP_START_CODE) { |
474 |
|
|
475 |
DPRINTF(DPRINTF_STARTCODE, "<group_of_vop>"); |
DPRINTF(DPRINTF_STARTCODE, "<group_of_vop>"); |
483 |
READ_MARKER(); |
READ_MARKER(); |
484 |
seconds = BitstreamGetBits(bs, 6); |
seconds = BitstreamGetBits(bs, 6); |
485 |
|
|
486 |
DPRINTF(DPRINTF_HEADER, "time %ih%im%is", hours,minutes,seconds); |
DPRINTF(DPRINTF_HEADER, "time %ih%im%is", hours); |
487 |
} |
} |
488 |
BitstreamSkip(bs, 1); // closed_gov |
BitstreamSkip(bs, 1); // closed_gov |
489 |
BitstreamSkip(bs, 1); // broken_link |
BitstreamSkip(bs, 1); // broken_link |
511 |
DPRINTF(DPRINTF_HEADER, "time_increment %i", time_increment); |
DPRINTF(DPRINTF_HEADER, "time_increment %i", time_increment); |
512 |
|
|
513 |
DPRINTF(DPRINTF_TIMECODE, "%c %i:%i", |
DPRINTF(DPRINTF_TIMECODE, "%c %i:%i", |
514 |
coding_type == I_VOP ? 'I' : coding_type == P_VOP ? 'P' : coding_type == B_VOP ? 'B' : 'S', |
coding_type == I_VOP ? 'I' : coding_type == P_VOP ? 'P' : 'B', |
515 |
time_incr, time_increment); |
time_incr, time_increment); |
516 |
|
|
517 |
if (coding_type != B_VOP) { |
if (coding_type != B_VOP) { |
519 |
dec->time_base += time_incr; |
dec->time_base += time_incr; |
520 |
dec->time = time_increment; |
dec->time = time_increment; |
521 |
|
|
522 |
/* dec->time_base * dec->time_inc_resolution + |
/* dec->time_base * time_increment_resolution + |
523 |
time_increment; |
time_increment; |
524 |
*/ dec->time_pp = (uint32_t) |
*/ dec->time_pp = (uint32_t) |
525 |
(dec->time_inc_resolution + dec->time - dec->last_non_b_time)%dec->time_inc_resolution; |
(time_increment_resolution + dec->time - dec->last_non_b_time)%time_increment_resolution; |
526 |
dec->last_non_b_time = dec->time; |
dec->last_non_b_time = dec->time; |
527 |
} else { |
} else { |
528 |
dec->time = time_increment; |
dec->time = time_increment; |
529 |
/* |
/* |
530 |
(dec->last_time_base + |
(dec->last_time_base + |
531 |
time_incr) * dec->time_inc_resolution + time_increment; |
time_incr) * time_increment_resolution + time_increment; |
532 |
*/ |
*/ |
533 |
dec->time_bp = (uint32_t) |
dec->time_bp = (uint32_t) |
534 |
(dec->time_inc_resolution + dec->last_non_b_time - dec->time)%dec->time_inc_resolution; |
(time_increment_resolution + dec->last_non_b_time - dec->time)%time_increment_resolution; |
535 |
} |
} |
|
DPRINTF(DPRINTF_HEADER,"time_pp=%i", dec->time_pp); |
|
|
DPRINTF(DPRINTF_HEADER,"time_bp=%i", dec->time_bp); |
|
536 |
|
|
537 |
READ_MARKER(); |
READ_MARKER(); |
538 |
|
|
542 |
return N_VOP; |
return N_VOP; |
543 |
} |
} |
544 |
|
|
545 |
if (dec->newpred_enable) |
/* if (newpred_enable) |
|
{ |
|
|
int vop_id; |
|
|
int vop_id_for_prediction; |
|
|
|
|
|
vop_id = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15)); |
|
|
DPRINTF(DPRINTF_HEADER, "vop_id %i", vop_id); |
|
|
if (BitstreamGetBit(bs)) /* vop_id_for_prediction_indication */ |
|
546 |
{ |
{ |
|
vop_id_for_prediction = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15)); |
|
|
DPRINTF(DPRINTF_HEADER, "vop_id_for_prediction %i", vop_id_for_prediction); |
|
|
} |
|
|
READ_MARKER(); |
|
547 |
} |
} |
548 |
|
*/ |
|
|
|
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 && dec->sprite_enable == SPRITE_GMC) ) ) { |
(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 |
} |
} |
556 |
|
|
557 |
if (dec->reduced_resolution_enable && |
/* if (reduced_resolution_enable) |
|
dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR && |
|
|
(coding_type == P_VOP || coding_type == I_VOP)) { |
|
|
|
|
|
*reduced_resolution = BitstreamGetBit(bs); |
|
|
DPRINTF(DPRINTF_HEADER, "reduced_resolution %i", *reduced_resolution); |
|
|
} |
|
|
else |
|
558 |
{ |
{ |
|
*reduced_resolution = 0; |
|
559 |
} |
} |
560 |
|
*/ |
561 |
|
|
562 |
if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { |
if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { |
|
if(!(dec->sprite_enable == SPRITE_STATIC && coding_type == I_VOP)) { |
|
|
|
|
563 |
uint32_t width, height; |
uint32_t width, height; |
564 |
uint32_t horiz_mc_ref, vert_mc_ref; |
uint32_t horiz_mc_ref, vert_mc_ref; |
565 |
|
|
576 |
DPRINTF(DPRINTF_HEADER, "height %i", height); |
DPRINTF(DPRINTF_HEADER, "height %i", height); |
577 |
DPRINTF(DPRINTF_HEADER, "horiz_mc_ref %i", horiz_mc_ref); |
DPRINTF(DPRINTF_HEADER, "horiz_mc_ref %i", horiz_mc_ref); |
578 |
DPRINTF(DPRINTF_HEADER, "vert_mc_ref %i", vert_mc_ref); |
DPRINTF(DPRINTF_HEADER, "vert_mc_ref %i", vert_mc_ref); |
|
} |
|
579 |
|
|
580 |
BitstreamSkip(bs, 1); // change_conv_ratio_disable |
BitstreamSkip(bs, 1); // change_conv_ratio_disable |
581 |
if (BitstreamGetBit(bs)) // vop_constant_alpha |
if (BitstreamGetBit(bs)) // vop_constant_alpha |
584 |
} |
} |
585 |
} |
} |
586 |
|
|
|
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) { |
|
|
|
|
|
if (!dec->complexity_estimation_disable) |
|
|
{ |
|
|
read_vop_complexity_estimation_header(bs, dec, coding_type); |
|
|
} |
|
587 |
|
|
588 |
|
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) { |
589 |
// intra_dc_vlc_threshold |
// intra_dc_vlc_threshold |
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); |
599 |
} |
} |
600 |
} |
} |
601 |
|
|
|
if ((dec->sprite_enable == SPRITE_STATIC || dec->sprite_enable== SPRITE_GMC) && coding_type == S_VOP) { |
|
|
|
|
|
int i; |
|
|
|
|
|
for (i = 0 ; i < dec->sprite_warping_points; i++) |
|
|
{ |
|
|
int length; |
|
|
int x = 0, y = 0; |
|
|
|
|
|
/* sprite code borowed from ffmpeg; thx Michael Niedermayer <michaelni@gmx.at> */ |
|
|
length = bs_get_spritetrajectory(bs); |
|
|
if(length){ |
|
|
x= BitstreamGetBits(bs, length); |
|
|
if ((x >> (length - 1)) == 0) /* if MSB not set it is negative*/ |
|
|
x = - (x ^ ((1 << length) - 1)); |
|
|
} |
|
|
READ_MARKER(); |
|
|
|
|
|
length = bs_get_spritetrajectory(bs); |
|
|
if(length){ |
|
|
y = BitstreamGetBits(bs, length); |
|
|
if ((y >> (length - 1)) == 0) /* if MSB not set it is negative*/ |
|
|
y = - (y ^ ((1 << length) - 1)); |
|
|
} |
|
|
READ_MARKER(); |
|
|
|
|
|
gmc_warp->duv[i].x = x; |
|
|
gmc_warp->duv[i].y = y; |
|
|
|
|
|
DPRINTF(DPRINTF_HEADER,"sprite_warping_point[%i] xy=(%i,%i)", i, x, y); |
|
|
} |
|
|
|
|
|
if (dec->sprite_brightness_change) |
|
|
{ |
|
|
// XXX: brightness_change_factor() |
|
|
} |
|
|
if (dec->sprite_enable == SPRITE_STATIC) |
|
|
{ |
|
|
// XXX: todo |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
602 |
if ((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1) // vop_quant |
if ((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1) // vop_quant |
603 |
*quant = 1; |
*quant = 1; |
604 |
|
|
605 |
DPRINTF(DPRINTF_HEADER, "quant %i", *quant); |
DPRINTF(DPRINTF_HEADER, "quant %i", *quant); |
606 |
|
|
607 |
if (coding_type != I_VOP) { |
if (coding_type != I_VOP) { |
622 |
return coding_type; |
return coding_type; |
623 |
|
|
624 |
} else if (start_code == USERDATA_START_CODE) { |
} else if (start_code == USERDATA_START_CODE) { |
|
char tmp[256]; |
|
|
int i, version, build; |
|
|
char packed; |
|
|
|
|
|
BitstreamSkip(bs, 32); // user_data_start_code |
|
|
|
|
|
tmp[0] = BitstreamShowBits(bs, 8); |
|
625 |
|
|
626 |
for(i = 1; i < 256; i++){ |
DPRINTF(DPRINTF_STARTCODE, "<user_data>"); |
|
tmp[i] = (BitstreamShowBits(bs, 16) & 0xFF); |
|
627 |
|
|
628 |
if(tmp[i] == 0) |
BitstreamSkip(bs, 32); // user_data_start_code |
|
break; |
|
|
|
|
|
BitstreamSkip(bs, 8); |
|
|
} |
|
|
|
|
|
DPRINTF(DPRINTF_STARTCODE, "<user_data>: %s\n", tmp); |
|
|
|
|
|
/* divx detection */ |
|
|
i = sscanf(tmp, "DivX%dBuild%d%c", &version, &build, &packed); |
|
|
if (i < 2) |
|
|
i = sscanf(tmp, "DivX%db%d%c", &version, &build, &packed); |
|
|
|
|
|
if (i >= 2) |
|
|
{ |
|
|
dec->packed_mode = (i == 3 && packed == 'p'); |
|
|
DPRINTF(DPRINTF_HEADER, "divx version=%i, build=%i packed=%i", |
|
|
version, build, dec->packed_mode); |
|
|
} |
|
629 |
|
|
630 |
} else // start_code == ? |
} else // start_code == ? |
631 |
{ |
{ |
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 |
{ |
{ |
|
static const unsigned int vo_id = 0; |
|
|
static const unsigned int vol_id = 0; |
|
|
int vol_ver_id=1; |
|
|
int profile = 0x03; /* simple profile/level 3 */ |
|
|
|
|
|
if ( pParam->m_quarterpel || (frame->global_flags & XVID_GMC) || |
|
|
(pParam->global & XVID_GLOBAL_REDUCED)) |
|
|
vol_ver_id = 2; |
|
|
|
|
|
if ((pParam->global & XVID_GLOBAL_REDUCED)) |
|
|
profile = 0x93; /* advanced realtime simple profile/level 3 */ |
|
|
|
|
|
if (pParam->m_quarterpel || (frame->global_flags & XVID_GMC)) |
|
|
profile = 0xf3; /* advanced simple profile/level 2 */ |
|
|
|
|
|
// visual_object_sequence_start_code |
|
|
// BitstreamPad(bs); |
|
|
/* no padding here, anymore. You have to make sure that you are |
|
|
byte aligned, and that always 1-8 padding bits have been written */ |
|
|
|
|
|
BitstreamPutBits(bs, VISOBJSEQ_START_CODE, 32); |
|
|
BitstreamPutBits(bs, profile, 8); |
|
|
|
|
|
// visual_object_start_code |
|
|
BitstreamPad(bs); |
|
|
BitstreamPutBits(bs, VISOBJ_START_CODE, 32); |
|
|
BitstreamPutBits(bs, 0, 1); // is_visual_object_identifier |
|
|
BitstreamPutBits(bs, VISOBJ_TYPE_VIDEO, 4); // visual_object_type |
|
|
|
|
674 |
// video object_start_code & vo_id |
// video object_start_code & vo_id |
675 |
BitstreamPad(bs); |
BitstreamPad(bs); |
676 |
BitstreamPutBits(bs, VIDOBJ_START_CODE|(vo_id&0x5), 32); |
BitstreamPutBits(bs, VO_START_CODE, 27); |
677 |
|
BitstreamPutBits(bs, 0, 5); |
678 |
|
|
679 |
// video_object_layer_start_code & vol_id |
// video_object_layer_start_code & vol_id |
680 |
BitstreamPad(bs); |
BitstreamPutBits(bs, VOL_START_CODE, 28); |
681 |
BitstreamPutBits(bs, VIDOBJLAY_START_CODE|(vol_id&0x4), 32); |
BitstreamPutBits(bs, 0, 4); |
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_inc_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, (pParam->global & XVID_GLOBAL_REDUCED)?1:0); |
|
|
/* reduced_resolution_vop_enabled */ |
|
|
} |
|
|
|
|
743 |
BitstreamPutBit(bs, 0); // scalability |
BitstreamPutBit(bs, 0); // scalability |
|
|
|
|
/* fake divx5 id, to ensure compatibility with divx5 decoder */ |
|
|
#define DIVX5_ID "DivX501b481p" |
|
|
if (pParam->max_bframes > 0 && (pParam->global & XVID_GLOBAL_PACKED)) { |
|
|
BitstreamWriteUserData(bs, DIVX5_ID, strlen(DIVX5_ID)); |
|
|
} |
|
|
|
|
|
/* xvid id */ |
|
|
#define XVID_ID "XviD" XVID_BS_VERSION |
|
|
BitstreamWriteUserData(bs, XVID_ID, strlen(XVID_ID)); |
|
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( |
BitstreamWriteVopHeader(Bitstream * const bs, |
|
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; |
762 |
|
|
763 |
// BitstreamPad(bs); |
BitstreamPad(bs); |
|
/* no padding here, anymore. You have to make sure that you are |
|
|
byte aligned, and that always 1-8 padding bits have been written */ |
|
|
|
|
764 |
BitstreamPutBits(bs, VOP_START_CODE, 32); |
BitstreamPutBits(bs, VOP_START_CODE, 32); |
765 |
|
|
766 |
BitstreamPutBits(bs, frame->coding_type, 2); |
BitstreamPutBits(bs, frame->coding_type, 2); |
|
DPRINTF(DPRINTF_HEADER, "coding_type = %i", frame->coding_type); |
|
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", 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 |
|
|
|
if ((pParam->global & XVID_GLOBAL_REDUCED)) |
|
|
BitstreamPutBit(bs, (frame->global_flags & XVID_REDUCED)?1:0); |
|
|
|
|
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 |
796 |
} |
} |
797 |
|
|
|
if (frame->coding_type == S_VOP) { |
|
|
if (1) { // no_of_sprite_warping_points>=1 (we use 2!) |
|
|
int k; |
|
|
for (k=0;k<2;k++) |
|
|
{ |
|
|
bs_put_spritetrajectory(bs, frame->warp.duv[k].x ); // du[k] |
|
|
WRITE_MARKER(); |
|
|
|
|
|
bs_put_spritetrajectory(bs, frame->warp.duv[k].y ); // dv[k] |
|
|
WRITE_MARKER(); |
|
|
|
|
|
if (pParam->m_quarterpel) |
|
|
{ |
|
|
DPRINTF(DPRINTF_HEADER,"sprite_warping_point[%i] xy=(%i,%i) *QPEL*", k, frame->warp.duv[k].x/2, frame->warp.duv[k].y/2); |
|
|
} |
|
|
else |
|
|
{ |
|
|
DPRINTF(DPRINTF_HEADER,"sprite_warping_point[%i] xy=(%i,%i)", k, frame->warp.duv[k].x, frame->warp.duv[k].y); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
DPRINTF(DPRINTF_HEADER, "quant = %i", frame->quant); |
|
|
|
|
798 |
BitstreamPutBits(bs, frame->quant, 5); // quantizer |
BitstreamPutBits(bs, frame->quant, 5); // quantizer |
799 |
|
|
800 |
if (frame->coding_type != I_VOP) |
if (frame->coding_type != I_VOP) |
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); |
|
|
} |
|
|
|
|
|
} |
|