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 file is part of XviD, a free MPEG-4 video encoder/decoder |
9 |
* would be at such party's own risk. The original developer of this * |
* |
10 |
* software module and his/her company, and subsequent editors and their * |
* XviD is free software; you can redistribute it and/or modify it |
11 |
* companies, will have no liability for use of this software or * |
* under the terms of the GNU General Public License as published by |
12 |
* modifications or derivatives thereof. * |
* the Free Software Foundation; either version 2 of the License, or |
13 |
* * |
* (at your option) any later version. |
14 |
* XviD is free software; you can redistribute it and/or modify it * |
* |
15 |
* under the terms of the GNU General Public License as published by * |
* This program is distributed in the hope that it will be useful, |
16 |
* the Free Software Foundation; either version 2 of the License, or * |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
* (at your option) any later version. * |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
* * |
* GNU General Public License for more details. |
19 |
* XviD is distributed in the hope that it will be useful, but * |
* |
20 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
* You should have received a copy of the GNU General Public License |
21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
* along with this program; if not, write to the Free Software |
22 |
* GNU General Public License for more details. * |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 |
* * |
* |
24 |
* You should have received a copy of the GNU General Public License * |
* Under section 8 of the GNU General Public License, the copyright |
25 |
* along with this program; if not, write to the Free Software * |
* holders of XVID explicitly forbid distribution in the following |
26 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
* countries: |
27 |
* * |
* |
28 |
******************************************************************************/ |
* - Japan |
29 |
|
* - United States of America |
30 |
/****************************************************************************** |
* |
31 |
* * |
* Linking XviD statically or dynamically with other modules is making a |
32 |
* bitstream.c * |
* combined work based on XviD. Thus, the terms and conditions of the |
33 |
* * |
* GNU General Public License cover the whole combination. |
34 |
* Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au> * |
* |
35 |
* * |
* As a special exception, the copyright holders of XviD give you |
36 |
* For more information visit the XviD homepage: http://www.xvid.org * |
* permission to link XviD with independent modules that communicate with |
37 |
* * |
* XviD solely through the VFW1.1 and DShow interfaces, regardless of the |
38 |
******************************************************************************/ |
* license terms of these independent modules, and to copy and distribute |
39 |
|
* the resulting combined work under terms of your choice, provided that |
40 |
/****************************************************************************** |
* every copy of the combined work is accompanied by a complete copy of |
41 |
* * |
* the source code of XviD (the version of XviD used to produce the |
42 |
* Revision history: * |
* combined work), being distributed under the terms of the GNU General |
43 |
* * |
* Public License plus this exception. An independent module is a module |
44 |
* 05.01.2003 GMC support - gruel * |
* which is not derived from or based on XviD. |
45 |
* 04.10.2002 qpel support - Isibaar * |
* |
46 |
* 11.07.2002 add VOP width & height return to dec when dec->width * |
* Note that people who make modified versions of XviD are not obligated |
47 |
* or dec->height is 0 (for use in examples/ex1.c) * |
* to grant this special exception for their modified versions; it is |
48 |
* MinChen <chenm001@163.com> * |
* their choice whether to do so. The GNU General Public License gives |
49 |
* 22.05.2002 bs_put_matrix fix * |
* permission to release a modified version without this exception; this |
50 |
* 20.05.2002 added BitstreamWriteUserData * |
* exception also makes it possible to release a modified version which |
51 |
* 19.06.2002 Fix a little bug in use custom quant matrix * |
* carries forward this exception. |
52 |
* MinChen <chenm001@163.com> * |
* |
53 |
* 08.05.2002 add low_delay support for B_VOP decode * |
* $Id$ |
54 |
* MinChen <chenm001@163.com> * |
* |
55 |
* 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> |
|
56 |
|
|
57 |
#include "bitstream.h" |
#include "bitstream.h" |
58 |
#include "zigzag.h" |
#include "zigzag.h" |
59 |
#include "../quant/quant_matrix.h" |
#include "../quant/quant_matrix.h" |
|
#include "mbcoding.h" |
|
60 |
|
|
61 |
|
/***************************************************************************** |
62 |
|
* Functions |
63 |
|
****************************************************************************/ |
64 |
|
|
65 |
static uint32_t __inline |
static uint32_t __inline |
66 |
log2bin(uint32_t value) |
log2bin(uint32_t value) |
121 |
// for BVOP addbits == max(fcode,bcode) - 1 |
// for BVOP addbits == max(fcode,bcode) - 1 |
122 |
// returns mbpos |
// returns mbpos |
123 |
int |
int |
124 |
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) |
|
125 |
{ |
{ |
126 |
int startcode_bits = NUMBITS_VP_RESYNC_MARKER + addbits; |
int nbits; |
|
int mbnum_bits = log2bin(dec->mb_width * dec->mb_height - 1); |
|
127 |
int mbnum; |
int mbnum; |
128 |
int hec = 0; |
int hec; |
129 |
|
|
130 |
|
nbits = NUMBITS_VP_RESYNC_MARKER + addbits; |
131 |
|
|
132 |
BitstreamSkip(bs, BitstreamNumBitsToByteAlign(bs)); |
BitstreamSkip(bs, BitstreamNumBitsToByteAlign(bs)); |
133 |
BitstreamSkip(bs, startcode_bits); |
BitstreamSkip(bs, nbits); |
134 |
|
|
135 |
DPRINTF(DPRINTF_STARTCODE, "<video_packet_header>"); |
DPRINTF(DPRINTF_STARTCODE, "<video_packet_header>"); |
136 |
|
|
137 |
if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) |
// if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { |
138 |
{ |
// hec |
139 |
hec = BitstreamGetBit(bs); /* header_extension_code */ |
// vop_width |
140 |
if (hec && !(dec->sprite_enable == SPRITE_STATIC /* && current_coding_type = I_VOP */)) |
// marker_bit |
141 |
{ |
// vop_height |
142 |
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(); |
|
|
} |
|
|
} |
|
143 |
|
|
144 |
mbnum = BitstreamGetBits(bs, mbnum_bits); /* macroblock_number */ |
//} |
145 |
|
|
146 |
|
mbnum = BitstreamGetBits(bs, 9); |
147 |
DPRINTF(DPRINTF_HEADER, "mbnum %i", mbnum); |
DPRINTF(DPRINTF_HEADER, "mbnum %i", mbnum); |
148 |
|
|
149 |
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) |
// if (dec->shape != VIDOBJLAY_SHAPE_BINARYONLY) |
150 |
{ |
*quant = BitstreamGetBits(bs, 5); |
|
*quant = BitstreamGetBits(bs, dec->quant_bits); /* quant_scale */ |
|
151 |
DPRINTF(DPRINTF_HEADER, "quant %i", *quant); |
DPRINTF(DPRINTF_HEADER, "quant %i", *quant); |
|
} |
|
|
|
|
|
if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) |
|
|
hec = BitstreamGetBit(bs); /* header_extension_code */ |
|
|
|
|
152 |
|
|
153 |
|
// if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) |
154 |
|
hec = BitstreamGetBit(bs); |
155 |
DPRINTF(DPRINTF_HEADER, "header_extension_code %i", hec); |
DPRINTF(DPRINTF_HEADER, "header_extension_code %i", hec); |
156 |
if (hec) |
// if (hec) |
157 |
{ |
// .. 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(); |
|
|
} |
|
158 |
|
|
159 |
return mbnum; |
return mbnum; |
160 |
} |
} |
161 |
|
|
162 |
|
|
163 |
|
|
|
|
|
|
/* 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); /* */ |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
/* |
/* |
165 |
decode headers |
decode headers |
166 |
returns coding_type, or -1 if error |
returns coding_type, or -1 if error |
173 |
BitstreamReadHeaders(Bitstream * bs, |
BitstreamReadHeaders(Bitstream * bs, |
174 |
DECODER * dec, |
DECODER * dec, |
175 |
uint32_t * rounding, |
uint32_t * rounding, |
|
uint32_t * reduced_resolution, |
|
176 |
uint32_t * quant, |
uint32_t * quant, |
177 |
uint32_t * fcode_forward, |
uint32_t * fcode_forward, |
178 |
uint32_t * fcode_backward, |
uint32_t * fcode_backward, |
179 |
uint32_t * intra_dc_threshold, |
uint32_t * intra_dc_threshold) |
|
WARPPOINTS *gmc_warp) |
|
180 |
{ |
{ |
181 |
uint32_t vol_ver_id; |
uint32_t vol_ver_id; |
182 |
|
static uint32_t time_increment_resolution; |
183 |
uint32_t coding_type; |
uint32_t coding_type; |
184 |
uint32_t start_code; |
uint32_t start_code; |
185 |
uint32_t time_incr = 0; |
uint32_t time_incr = 0; |
186 |
int32_t time_increment; |
int32_t time_increment = 0; |
|
int resize = 0; |
|
187 |
|
|
188 |
do { |
do { |
|
|
|
189 |
BitstreamByteAlign(bs); |
BitstreamByteAlign(bs); |
190 |
start_code = BitstreamShowBits(bs, 32); |
start_code = BitstreamShowBits(bs, 32); |
191 |
|
|
207 |
DPRINTF(DPRINTF_STARTCODE, "</visual_object_sequence>"); |
DPRINTF(DPRINTF_STARTCODE, "</visual_object_sequence>"); |
208 |
|
|
209 |
} else if (start_code == VISOBJ_START_CODE) { |
} else if (start_code == VISOBJ_START_CODE) { |
|
int visobj_ver_id; |
|
210 |
|
|
211 |
DPRINTF(DPRINTF_STARTCODE, "<visual_object>"); |
DPRINTF(DPRINTF_STARTCODE, "<visual_object>"); |
212 |
|
|
213 |
BitstreamSkip(bs, 32); // visual_object_start_code |
BitstreamSkip(bs, 32); // visual_object_start_code |
214 |
if (BitstreamGetBit(bs)) // is_visual_object_identified |
if (BitstreamGetBit(bs)) // is_visual_object_identified |
215 |
{ |
{ |
216 |
visobj_ver_id = BitstreamGetBits(bs, 4); // visual_object_ver_id |
vol_ver_id = BitstreamGetBits(bs, 4); // visual_object_ver_id |
217 |
DPRINTF(DPRINTF_HEADER,"visobj_ver_id %i", visobj_ver_id); |
DPRINTF(DPRINTF_HEADER,"ver_id %i", vol_ver_id); |
218 |
BitstreamSkip(bs, 3); // visual_object_priority |
BitstreamSkip(bs, 3); // visual_object_priority |
219 |
} else { |
} else { |
220 |
visobj_ver_id = 1; |
vol_ver_id = 1; |
221 |
} |
} |
222 |
|
|
223 |
if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) // visual_object_type |
if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) // visual_object_type |
259 |
BitstreamSkip(bs, 1); // random_accessible_vol |
BitstreamSkip(bs, 1); // random_accessible_vol |
260 |
|
|
261 |
// video_object_type_indication |
// video_object_type_indication |
262 |
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 |
|
263 |
{ |
{ |
264 |
DPRINTF(DPRINTF_ERROR,"video_object_type_indication %i not supported ", |
DPRINTF(DPRINTF_ERROR,"video_object_type_indication %i not supported ", |
265 |
BitstreamShowBits(bs, 8)); |
BitstreamShowBits(bs, 8)); |
278 |
vol_ver_id = 1; |
vol_ver_id = 1; |
279 |
} |
} |
280 |
|
|
281 |
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 |
|
282 |
{ |
{ |
283 |
DPRINTF(DPRINTF_HEADER, "+ aspect_ratio_info"); |
DPRINTF(DPRINTF_HEADER, "+ aspect_ratio_info"); |
284 |
dec->par_width = BitstreamGetBits(bs, 8); // par_width |
BitstreamSkip(bs, 8); // par_width |
285 |
dec->par_height = BitstreamGetBits(bs, 8); // par_height |
BitstreamSkip(bs, 8); // par_height |
286 |
} |
} |
287 |
|
|
288 |
if (BitstreamGetBit(bs)) // vol_control_parameters |
if (BitstreamGetBit(bs)) // vol_control_parameters |
305 |
READ_MARKER(); |
READ_MARKER(); |
306 |
BitstreamSkip(bs, 15); // latter_half_vbv_occupancy |
BitstreamSkip(bs, 15); // latter_half_vbv_occupancy |
307 |
READ_MARKER(); |
READ_MARKER(); |
308 |
|
|
309 |
} |
} |
|
}else{ |
|
|
dec->low_delay = dec->low_delay_default; |
|
310 |
} |
} |
311 |
|
|
312 |
dec->shape = BitstreamGetBits(bs, 2); // video_object_layer_shape |
dec->shape = BitstreamGetBits(bs, 2); // video_object_layer_shape |
|
|
|
313 |
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"); |
|
|
} |
|
314 |
|
|
315 |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) { |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) { |
316 |
BitstreamSkip(bs, 4); // video_object_layer_shape_extension |
BitstreamSkip(bs, 4); // video_object_layer_shape_extension |
319 |
READ_MARKER(); |
READ_MARKER(); |
320 |
|
|
321 |
// *************************** for decode B-frame time *********************** |
// *************************** for decode B-frame time *********************** |
322 |
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); |
|
323 |
|
|
324 |
// dec->time_inc_resolution--; |
DPRINTF(DPRINTF_HEADER,"vop_time_increment_resolution %i", time_increment_resolution); |
325 |
|
|
326 |
if (dec->time_inc_resolution > 0) { |
// time_increment_resolution--; |
327 |
dec->time_inc_bits = log2bin(dec->time_inc_resolution-1); |
|
328 |
|
if (time_increment_resolution > 0) { |
329 |
|
dec->time_inc_bits = log2bin(time_increment_resolution-1); |
330 |
} else { |
} else { |
331 |
// dec->time_inc_bits = 0; |
// dec->time_inc_bits = 0; |
332 |
// for "old" xvid compatibility, set time_inc_bits = 1 |
// for "old" xvid compatibility, set time_inc_bits = 1 |
355 |
DPRINTF(DPRINTF_HEADER, "width %i", width); |
DPRINTF(DPRINTF_HEADER, "width %i", width); |
356 |
DPRINTF(DPRINTF_HEADER, "height %i", height); |
DPRINTF(DPRINTF_HEADER, "height %i", height); |
357 |
|
|
358 |
if (dec->width != width || dec->height != height) |
// for auto set width & height |
359 |
{ |
if (dec->width == 0) |
|
if (dec->fixed_dimensions) |
|
|
{ |
|
|
DPRINTF(DPRINTF_ERROR, "XVID_DEC_PARAM width/height does not match bitstream"); |
|
|
return -1; |
|
|
} |
|
|
resize = 1; |
|
360 |
dec->width = width; |
dec->width = width; |
361 |
|
if (dec->height == 0) |
362 |
dec->height = height; |
dec->height = height; |
363 |
|
|
364 |
|
if (width != dec->width || height != dec->height) { |
365 |
|
DPRINTF(DPRINTF_ERROR, "XVID_DEC_PARAM width/height does not match bitstream"); |
366 |
|
return -1; |
367 |
} |
} |
368 |
|
|
369 |
} |
} |
370 |
|
|
371 |
dec->interlacing = BitstreamGetBit(bs); |
dec->interlacing = BitstreamGetBit(bs); |
372 |
DPRINTF(DPRINTF_HEADER, "interlacing %i", dec->interlacing); |
DPRINTF(DPRINTF_HEADER, "interlace", dec->interlacing); |
373 |
|
|
374 |
if (!BitstreamGetBit(bs)) // obmc_disable |
if (!BitstreamGetBit(bs)) // obmc_disable |
375 |
{ |
{ |
378 |
// fucking divx4.02 has this enabled |
// fucking divx4.02 has this enabled |
379 |
} |
} |
380 |
|
|
381 |
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) |
|
382 |
{ |
{ |
383 |
int sprite_width; |
DPRINTF(DPRINTF_ERROR, "spriate_enabled not supported"); |
384 |
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 |
|
|
} |
|
385 |
} |
} |
386 |
|
|
387 |
if (vol_ver_id != 1 && |
if (vol_ver_id != 1 && |
439 |
|
|
440 |
|
|
441 |
if (vol_ver_id != 1) { |
if (vol_ver_id != 1) { |
442 |
|
DPRINTF(DPRINTF_DEBUG, "QUARTERPEL BITSTREAM"); |
443 |
dec->quarterpel = BitstreamGetBit(bs); // quarter_sample |
dec->quarterpel = BitstreamGetBit(bs); // quarter_sample |
|
DPRINTF(DPRINTF_HEADER,"quarterpel %i", dec->quarterpel); |
|
444 |
} |
} |
445 |
else |
else |
446 |
dec->quarterpel = 0; |
dec->quarterpel = 0; |
447 |
|
|
448 |
|
|
449 |
dec->complexity_estimation_disable = BitstreamGetBit(bs); /* complexity estimation disable */ |
if (!BitstreamGetBit(bs)) // complexity_estimation_disable |
|
if (!dec->complexity_estimation_disable) |
|
450 |
{ |
{ |
451 |
read_vol_complexity_estimation_header(bs, dec); |
DPRINTF(DPRINTF_ERROR, "complexity_estimation not supported"); |
452 |
|
return -1; |
453 |
} |
} |
454 |
|
|
455 |
BitstreamSkip(bs, 1); // resync_marker_disable |
BitstreamSkip(bs, 1); // resync_marker_disable |
461 |
} |
} |
462 |
|
|
463 |
if (vol_ver_id != 1) { |
if (vol_ver_id != 1) { |
464 |
dec->newpred_enable = BitstreamGetBit(bs); |
if (BitstreamGetBit(bs)) // newpred_enable |
|
if (dec->newpred_enable) // newpred_enable |
|
465 |
{ |
{ |
466 |
DPRINTF(DPRINTF_HEADER, "+ newpred_enable"); |
DPRINTF(DPRINTF_HEADER, "+ newpred_enable"); |
467 |
BitstreamSkip(bs, 2); // requested_upstream_message_type |
BitstreamSkip(bs, 2); // requested_upstream_message_type |
468 |
BitstreamSkip(bs, 1); // newpred_segment_type |
BitstreamSkip(bs, 1); // newpred_segment_type |
469 |
} |
} |
470 |
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 |
|
471 |
{ |
{ |
472 |
dec->newpred_enable = 0; |
DPRINTF(DPRINTF_ERROR, "reduced_resolution_vop not supported"); |
473 |
dec->reduced_resolution_enable = 0; |
return -1; |
474 |
|
} |
475 |
} |
} |
476 |
|
|
477 |
dec->scalability = BitstreamGetBit(bs); /* scalability */ |
if ((dec->scalability = BitstreamGetBit(bs))) // scalability |
|
if (dec->scalability) |
|
478 |
{ |
{ |
479 |
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 */ |
|
|
} |
|
480 |
return -1; |
return -1; |
481 |
} |
} |
482 |
} else // dec->shape == BINARY_ONLY |
} else // dec->shape == BINARY_ONLY |
483 |
{ |
{ |
484 |
if (vol_ver_id != 1) { |
if (vol_ver_id != 1) { |
485 |
dec->scalability = BitstreamGetBit(bs); /* scalability */ |
if (BitstreamGetBit(bs)) // scalability |
|
if (dec->scalability) |
|
486 |
{ |
{ |
487 |
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 */ |
|
488 |
return -1; |
return -1; |
489 |
} |
} |
490 |
} |
} |
492 |
|
|
493 |
} |
} |
494 |
|
|
|
return (resize ? -3 : -2 ); /* VOL */ |
|
|
|
|
495 |
} else if (start_code == GRPOFVOP_START_CODE) { |
} else if (start_code == GRPOFVOP_START_CODE) { |
496 |
|
|
497 |
DPRINTF(DPRINTF_STARTCODE, "<group_of_vop>"); |
DPRINTF(DPRINTF_STARTCODE, "<group_of_vop>"); |
505 |
READ_MARKER(); |
READ_MARKER(); |
506 |
seconds = BitstreamGetBits(bs, 6); |
seconds = BitstreamGetBits(bs, 6); |
507 |
|
|
508 |
DPRINTF(DPRINTF_HEADER, "time %ih%im%is", hours,minutes,seconds); |
DPRINTF(DPRINTF_HEADER, "time %ih%im%is", hours); |
509 |
} |
} |
510 |
BitstreamSkip(bs, 1); // closed_gov |
BitstreamSkip(bs, 1); // closed_gov |
511 |
BitstreamSkip(bs, 1); // broken_link |
BitstreamSkip(bs, 1); // broken_link |
533 |
DPRINTF(DPRINTF_HEADER, "time_increment %i", time_increment); |
DPRINTF(DPRINTF_HEADER, "time_increment %i", time_increment); |
534 |
|
|
535 |
DPRINTF(DPRINTF_TIMECODE, "%c %i:%i", |
DPRINTF(DPRINTF_TIMECODE, "%c %i:%i", |
536 |
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', |
537 |
time_incr, time_increment); |
time_incr, time_increment); |
538 |
|
|
539 |
if (coding_type != B_VOP) { |
if (coding_type != B_VOP) { |
541 |
dec->time_base += time_incr; |
dec->time_base += time_incr; |
542 |
dec->time = time_increment; |
dec->time = time_increment; |
543 |
|
|
544 |
/* dec->time_base * dec->time_inc_resolution + |
/* dec->time_base * time_increment_resolution + |
545 |
time_increment; |
time_increment; |
546 |
*/ dec->time_pp = (uint32_t) |
*/ dec->time_pp = (uint32_t) |
547 |
(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; |
548 |
dec->last_non_b_time = dec->time; |
dec->last_non_b_time = dec->time; |
549 |
} else { |
} else { |
550 |
dec->time = time_increment; |
dec->time = time_increment; |
551 |
/* |
/* |
552 |
(dec->last_time_base + |
(dec->last_time_base + |
553 |
time_incr) * dec->time_inc_resolution + time_increment; |
time_incr) * time_increment_resolution + time_increment; |
554 |
*/ |
*/ |
555 |
dec->time_bp = (uint32_t) |
dec->time_bp = (uint32_t) |
556 |
(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; |
557 |
} |
} |
|
DPRINTF(DPRINTF_HEADER,"time_pp=%i", dec->time_pp); |
|
|
DPRINTF(DPRINTF_HEADER,"time_bp=%i", dec->time_bp); |
|
558 |
|
|
559 |
READ_MARKER(); |
READ_MARKER(); |
560 |
|
|
564 |
return N_VOP; |
return N_VOP; |
565 |
} |
} |
566 |
|
|
567 |
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 */ |
|
568 |
{ |
{ |
|
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(); |
|
569 |
} |
} |
570 |
|
*/ |
|
|
|
571 |
|
|
572 |
// fix a little bug by MinChen <chenm002@163.com> |
// fix a little bug by MinChen <chenm002@163.com> |
573 |
if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) && |
if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) && |
574 |
( (coding_type == P_VOP) || (coding_type == S_VOP && dec->sprite_enable == SPRITE_GMC) ) ) { |
(coding_type == P_VOP)) { |
575 |
*rounding = BitstreamGetBit(bs); // rounding_type |
*rounding = BitstreamGetBit(bs); // rounding_type |
576 |
DPRINTF(DPRINTF_HEADER, "rounding %i", *rounding); |
DPRINTF(DPRINTF_HEADER, "rounding %i", *rounding); |
577 |
} |
} |
578 |
|
|
579 |
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 |
|
580 |
{ |
{ |
|
*reduced_resolution = 0; |
|
581 |
} |
} |
582 |
|
*/ |
583 |
|
|
584 |
if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { |
if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { |
|
if(!(dec->sprite_enable == SPRITE_STATIC && coding_type == I_VOP)) { |
|
|
|
|
585 |
uint32_t width, height; |
uint32_t width, height; |
586 |
uint32_t horiz_mc_ref, vert_mc_ref; |
uint32_t horiz_mc_ref, vert_mc_ref; |
587 |
|
|
598 |
DPRINTF(DPRINTF_HEADER, "height %i", height); |
DPRINTF(DPRINTF_HEADER, "height %i", height); |
599 |
DPRINTF(DPRINTF_HEADER, "horiz_mc_ref %i", horiz_mc_ref); |
DPRINTF(DPRINTF_HEADER, "horiz_mc_ref %i", horiz_mc_ref); |
600 |
DPRINTF(DPRINTF_HEADER, "vert_mc_ref %i", vert_mc_ref); |
DPRINTF(DPRINTF_HEADER, "vert_mc_ref %i", vert_mc_ref); |
|
} |
|
601 |
|
|
602 |
BitstreamSkip(bs, 1); // change_conv_ratio_disable |
BitstreamSkip(bs, 1); // change_conv_ratio_disable |
603 |
if (BitstreamGetBit(bs)) // vop_constant_alpha |
if (BitstreamGetBit(bs)) // vop_constant_alpha |
606 |
} |
} |
607 |
} |
} |
608 |
|
|
|
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) { |
|
|
|
|
|
if (!dec->complexity_estimation_disable) |
|
|
{ |
|
|
read_vop_complexity_estimation_header(bs, dec, coding_type); |
|
|
} |
|
609 |
|
|
610 |
|
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) { |
611 |
// intra_dc_vlc_threshold |
// intra_dc_vlc_threshold |
612 |
*intra_dc_threshold = |
*intra_dc_threshold = |
613 |
intra_dc_threshold_table[BitstreamGetBits(bs, 3)]; |
intra_dc_threshold_table[BitstreamGetBits(bs, 3)]; |
614 |
|
|
|
dec->top_field_first = 0; |
|
|
dec->alternate_vertical_scan = 0; |
|
|
|
|
615 |
if (dec->interlacing) { |
if (dec->interlacing) { |
616 |
dec->top_field_first = BitstreamGetBit(bs); |
dec->top_field_first = BitstreamGetBit(bs); |
617 |
DPRINTF(DPRINTF_HEADER, "interlace top_field_first %i", dec->top_field_first); |
DPRINTF(DPRINTF_HEADER, "interlace top_field_first %i", dec->top_field_first); |
621 |
} |
} |
622 |
} |
} |
623 |
|
|
|
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 |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
624 |
if ((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1) // vop_quant |
if ((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1) // vop_quant |
625 |
*quant = 1; |
*quant = 1; |
626 |
|
|
627 |
DPRINTF(DPRINTF_HEADER, "quant %i", *quant); |
DPRINTF(DPRINTF_HEADER, "quant %i", *quant); |
628 |
|
|
629 |
if (coding_type != I_VOP) { |
if (coding_type != I_VOP) { |
644 |
return coding_type; |
return coding_type; |
645 |
|
|
646 |
} 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 |
|
647 |
|
|
648 |
tmp[0] = BitstreamShowBits(bs, 8); |
DPRINTF(DPRINTF_STARTCODE, "<user_data>"); |
649 |
|
|
650 |
for(i = 1; i < 256; i++){ |
BitstreamSkip(bs, 32); // user_data_start_code |
|
tmp[i] = (BitstreamShowBits(bs, 16) & 0xFF); |
|
|
|
|
|
if(tmp[i] == 0) |
|
|
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); |
|
|
} |
|
651 |
|
|
652 |
} else // start_code == ? |
} else // start_code == ? |
653 |
{ |
{ |
691 |
void |
void |
692 |
BitstreamWriteVolHeader(Bitstream * const bs, |
BitstreamWriteVolHeader(Bitstream * const bs, |
693 |
const MBParam * pParam, |
const MBParam * pParam, |
694 |
const FRAMEINFO * const frame) |
const FRAMEINFO * frame) |
695 |
{ |
{ |
|
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); |
|
|
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 |
|
|
|
|
696 |
// video object_start_code & vo_id |
// video object_start_code & vo_id |
697 |
BitstreamPad(bs); |
BitstreamPad(bs); |
698 |
BitstreamPutBits(bs, VIDOBJ_START_CODE|(vo_id&0x5), 32); |
BitstreamPutBits(bs, VO_START_CODE, 27); |
699 |
|
BitstreamPutBits(bs, 0, 5); |
700 |
|
|
701 |
// video_object_layer_start_code & vol_id |
// video_object_layer_start_code & vol_id |
702 |
BitstreamPutBits(bs, VIDOBJLAY_START_CODE|(vol_id&0x4), 32); |
BitstreamPutBits(bs, VOL_START_CODE, 28); |
703 |
|
BitstreamPutBits(bs, 0, 4); |
704 |
|
|
705 |
BitstreamPutBit(bs, 0); // random_accessible_vol |
BitstreamPutBit(bs, 0); // random_accessible_vol |
706 |
BitstreamPutBits(bs, 0, 8); // video_object_type_indication |
BitstreamPutBits(bs, 0, 8); // video_object_type_indication |
|
|
|
|
if (vol_ver_id == 1) |
|
|
{ |
|
707 |
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) ?? |
|
|
} |
|
|
|
|
708 |
BitstreamPutBits(bs, 1, 4); // aspect_ratio_info (1=1:1) |
BitstreamPutBits(bs, 1, 4); // aspect_ratio_info (1=1:1) |
709 |
|
|
710 |
BitstreamPutBit(bs, 1); // vol_control_parameters |
BitstreamPutBit(bs, 1); // vol_control_parameters |
711 |
BitstreamPutBits(bs, 1, 2); // chroma_format 1="4:2:0" |
BitstreamPutBits(bs, 1, 2); // chroma_format 1="4:2:0" |
712 |
|
|
|
if (pParam->max_bframes > 0) { |
|
|
BitstreamPutBit(bs, 0); // low_delay |
|
|
} else |
|
|
{ |
|
713 |
BitstreamPutBit(bs, 1); // low_delay |
BitstreamPutBit(bs, 1); // low_delay |
714 |
} |
|
715 |
BitstreamPutBit(bs, 0); // vbv_parameters (0=not given) |
BitstreamPutBit(bs, 0); // vbv_parameters (0=not given) |
716 |
|
|
717 |
BitstreamPutBits(bs, 0, 2); // video_object_layer_shape (0=rectangular) |
BitstreamPutBits(bs, 0, 2); // video_object_layer_shape (0=rectangular) |
718 |
|
|
719 |
WRITE_MARKER(); |
WRITE_MARKER(); |
720 |
|
|
721 |
/* time_inc_resolution; ignored by current decore versions |
/* |
722 |
eg. 2fps res=2 inc=1 |
* time_increment_resolution; ignored by current decore versions |
723 |
25fps res=25 inc=1 |
* eg. 2fps res=2 inc=1 |
724 |
29.97fps res=30000 inc=1001 |
* 25fps res=25 inc=1 |
725 |
|
* 29.97fps res=30000 inc=1001 |
726 |
*/ |
*/ |
727 |
BitstreamPutBits(bs, pParam->fbase, 16); |
BitstreamPutBits(bs, pParam->fbase, 16); |
728 |
|
|
729 |
|
|
730 |
WRITE_MARKER(); |
WRITE_MARKER(); |
731 |
|
|
732 |
BitstreamPutBit(bs, 1); // fixed_vop_rate = 1 |
BitstreamPutBit(bs, 1); // fixed_vop_rate = 1 |
740 |
|
|
741 |
BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING); // interlace |
BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING); // interlace |
742 |
BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) |
BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) |
743 |
|
BitstreamPutBit(bs, 0); // sprite_enable |
744 |
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 |
|
745 |
|
|
746 |
// quant_type 0=h.263 1=mpeg4(quantizer tables) |
// quant_type 0=h.263 1=mpeg4(quantizer tables) |
747 |
BitstreamPutBit(bs, pParam->m_quant_type); |
BitstreamPutBit(bs, pParam->m_quant_type); |
759 |
|
|
760 |
} |
} |
761 |
|
|
|
if (vol_ver_id != 1) { |
|
|
if (pParam->m_quarterpel) |
|
|
BitstreamPutBit(bs, 1); // quarterpel |
|
|
else |
|
|
BitstreamPutBit(bs, 0); // no quarterpel |
|
|
} |
|
|
|
|
762 |
BitstreamPutBit(bs, 1); // complexity_estimation_disable |
BitstreamPutBit(bs, 1); // complexity_estimation_disable |
763 |
BitstreamPutBit(bs, 1); // resync_marker_disable |
BitstreamPutBit(bs, 1); // resync_marker_disable |
764 |
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 */ |
|
|
} |
|
|
|
|
765 |
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)); |
|
766 |
} |
} |
767 |
|
|
768 |
|
|
769 |
/* |
/* |
770 |
write vop header |
write vop header |
771 |
|
|
772 |
|
NOTE: doesnt handle bother with time_base & time_inc |
773 |
|
time_base = n seconds since last resync (eg. last iframe) |
774 |
|
time_inc = nth of a second since last resync |
775 |
|
(decoder uses these values to determine precise time since last resync) |
776 |
*/ |
*/ |
777 |
void |
void |
778 |
BitstreamWriteVopHeader( |
BitstreamWriteVopHeader(Bitstream * const bs, |
|
Bitstream * const bs, |
|
779 |
const MBParam * pParam, |
const MBParam * pParam, |
780 |
const FRAMEINFO * const frame, |
const FRAMEINFO * frame, |
781 |
int vop_coded) |
int vop_coded) |
782 |
{ |
{ |
783 |
uint32_t i; |
uint32_t i; |
786 |
BitstreamPutBits(bs, VOP_START_CODE, 32); |
BitstreamPutBits(bs, VOP_START_CODE, 32); |
787 |
|
|
788 |
BitstreamPutBits(bs, frame->coding_type, 2); |
BitstreamPutBits(bs, frame->coding_type, 2); |
|
DPRINTF(DPRINTF_HEADER, "coding_type = %i", frame->coding_type); |
|
789 |
|
|
790 |
|
// time_base = 0 write n x PutBit(1), PutBit(0) |
791 |
for (i = 0; i < frame->seconds; i++) { |
for (i = 0; i < frame->seconds; i++) { |
792 |
BitstreamPutBit(bs, 1); |
BitstreamPutBit(bs, 1); |
793 |
} |
} |
796 |
WRITE_MARKER(); |
WRITE_MARKER(); |
797 |
|
|
798 |
// time_increment: value=nth_of_sec, nbits = log2(resolution) |
// time_increment: value=nth_of_sec, nbits = log2(resolution) |
|
|
|
799 |
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');*/ |
|
800 |
|
|
801 |
WRITE_MARKER(); |
WRITE_MARKER(); |
802 |
|
|
807 |
|
|
808 |
BitstreamPutBits(bs, 1, 1); // vop_coded |
BitstreamPutBits(bs, 1, 1); // vop_coded |
809 |
|
|
810 |
if ( (frame->coding_type == P_VOP) || (frame->coding_type == S_VOP) ) |
if (frame->coding_type == P_VOP) |
811 |
BitstreamPutBits(bs, frame->rounding_type, 1); |
BitstreamPutBits(bs, frame->rounding_type, 1); |
812 |
|
|
|
if ((pParam->global & XVID_GLOBAL_REDUCED)) |
|
|
BitstreamPutBit(bs, (frame->global_flags & XVID_REDUCED)?1:0); |
|
|
|
|
813 |
BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold |
BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold |
814 |
|
|
815 |
if (frame->global_flags & XVID_INTERLACING) { |
if (frame->global_flags & XVID_INTERLACING) { |
816 |
BitstreamPutBit(bs, (frame->global_flags & XVID_TOPFIELDFIRST)); |
BitstreamPutBit(bs, 1); // top field first |
817 |
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 (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); |
|
|
} |
|
|
} |
|
|
} |
|
818 |
} |
} |
819 |
|
|
|
|
|
|
DPRINTF(DPRINTF_HEADER, "quant = %i", frame->quant); |
|
|
|
|
820 |
BitstreamPutBits(bs, frame->quant, 5); // quantizer |
BitstreamPutBits(bs, frame->quant, 5); // quantizer |
821 |
|
|
822 |
if (frame->coding_type != I_VOP) |
if (frame->coding_type != I_VOP) |
826 |
BitstreamPutBits(bs, frame->bcode, 3); // backward_fixed_code |
BitstreamPutBits(bs, frame->bcode, 3); // backward_fixed_code |
827 |
|
|
828 |
} |
} |
|
|
|
|
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); |
|
|
} |
|
|
|
|
|
} |
|