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: bitstream.c,v 1.33 2002-09-22 17:01:36 edgomez Exp $ |
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: * |
|
|
* * |
|
|
* 08.05.2002 add low_delay support for B_VOP decode * |
|
|
* MinChen <chenm001@163.com> * |
|
|
* 06.05.2002 low_delay * |
|
|
* 06.05.2002 fixed fincr/fbase error * |
|
|
* 01.05.2002 added BVOP support to BitstreamWriteVopHeader * |
|
|
* 15.04.2002 rewrite log2bin use asm386 By MinChen <chenm001@163.com> * |
|
|
* 26.03.2002 interlacing support * |
|
|
* 03.03.2002 qmatrix writing * |
|
|
* 03.03.2002 merged BITREADER and BITWRITER * |
|
|
* 30.02.2002 intra_dc_threshold support * |
|
|
* 04.12.2001 support for additional headers * |
|
|
* 16.12.2001 inital version * |
|
|
* * |
|
|
******************************************************************************/ |
|
|
|
|
34 |
|
|
35 |
#include "bitstream.h" |
#include "bitstream.h" |
36 |
#include "zigzag.h" |
#include "zigzag.h" |
37 |
#include "../quant/quant_matrix.h" |
#include "../quant/quant_matrix.h" |
38 |
|
|
39 |
|
/***************************************************************************** |
40 |
|
* Functions |
41 |
|
****************************************************************************/ |
42 |
|
|
43 |
static uint32_t __inline |
static uint32_t __inline |
44 |
log2bin(uint32_t value) |
log2bin(uint32_t value) |
54 |
return n; |
return n; |
55 |
#else |
#else |
56 |
__asm { |
__asm { |
57 |
bsr eax, value inc eax} |
bsr eax, value |
58 |
|
inc eax |
59 |
|
} |
60 |
#endif |
#endif |
61 |
} |
} |
62 |
|
|
86 |
matrix[scan_tables[0][i++]] = value; |
matrix[scan_tables[0][i++]] = value; |
87 |
} |
} |
88 |
while (value != 0 && i < 64); |
while (value != 0 && i < 64); |
89 |
|
i--; // fix little bug at coeff not full |
90 |
|
|
91 |
while (i < 64) { |
while (i < 64) { |
92 |
matrix[scan_tables[0][i++]] = last; |
matrix[scan_tables[0][i++]] = last; |
93 |
} |
} |
94 |
} |
} |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
// for PVOP addbits == fcode - 1 |
99 |
|
// for BVOP addbits == max(fcode,bcode) - 1 |
100 |
|
// returns mbpos |
101 |
|
int |
102 |
|
read_video_packet_header(Bitstream *bs, const int addbits, int * quant) |
103 |
|
{ |
104 |
|
int nbits; |
105 |
|
int mbnum; |
106 |
|
int hec; |
107 |
|
|
108 |
|
nbits = NUMBITS_VP_RESYNC_MARKER + addbits; |
109 |
|
|
110 |
|
BitstreamSkip(bs, BitstreamNumBitsToByteAlign(bs)); |
111 |
|
BitstreamSkip(bs, nbits); |
112 |
|
|
113 |
|
DPRINTF(DPRINTF_STARTCODE, "<video_packet_header>"); |
114 |
|
|
115 |
|
// if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { |
116 |
|
// hec |
117 |
|
// vop_width |
118 |
|
// marker_bit |
119 |
|
// vop_height |
120 |
|
// marker_bit |
121 |
|
|
122 |
|
//} |
123 |
|
|
124 |
|
mbnum = BitstreamGetBits(bs, 9); |
125 |
|
DPRINTF(DPRINTF_HEADER, "mbnum %i", mbnum); |
126 |
|
|
127 |
|
// if (dec->shape != VIDOBJLAY_SHAPE_BINARYONLY) |
128 |
|
*quant = BitstreamGetBits(bs, 5); |
129 |
|
DPRINTF(DPRINTF_HEADER, "quant %i", *quant); |
130 |
|
|
131 |
|
// if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) |
132 |
|
hec = BitstreamGetBit(bs); |
133 |
|
DPRINTF(DPRINTF_HEADER, "header_extension_code %i", hec); |
134 |
|
// if (hec) |
135 |
|
// .. decoder hec-header ... |
136 |
|
|
137 |
|
return mbnum; |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
|
|
142 |
/* |
/* |
143 |
decode headers |
decode headers |
144 |
returns coding_type, or -1 if error |
returns coding_type, or -1 if error |
145 |
*/ |
*/ |
146 |
|
|
147 |
|
#define VIDOBJ_START_CODE_MASK 0x0000001f |
148 |
|
#define VIDOBJLAY_START_CODE_MASK 0x0000000f |
149 |
|
|
150 |
int |
int |
151 |
BitstreamReadHeaders(Bitstream * bs, |
BitstreamReadHeaders(Bitstream * bs, |
152 |
DECODER * dec, |
DECODER * dec, |
161 |
uint32_t coding_type; |
uint32_t coding_type; |
162 |
uint32_t start_code; |
uint32_t start_code; |
163 |
uint32_t time_incr = 0; |
uint32_t time_incr = 0; |
164 |
int32_t time_increment; |
int32_t time_increment = 0; |
165 |
|
|
166 |
do { |
do { |
167 |
BitstreamByteAlign(bs); |
BitstreamByteAlign(bs); |
168 |
start_code = BitstreamShowBits(bs, 32); |
start_code = BitstreamShowBits(bs, 32); |
169 |
|
|
170 |
if (start_code == VISOBJSEQ_START_CODE) { |
if (start_code == VISOBJSEQ_START_CODE) { |
171 |
// DEBUG("visual_object_sequence"); |
|
172 |
|
int profile; |
173 |
|
|
174 |
|
DPRINTF(DPRINTF_STARTCODE, "<visual_object_sequence>"); |
175 |
|
|
176 |
BitstreamSkip(bs, 32); // visual_object_sequence_start_code |
BitstreamSkip(bs, 32); // visual_object_sequence_start_code |
177 |
BitstreamSkip(bs, 8); // profile_and_level_indication |
profile = BitstreamGetBits(bs, 8); // profile_and_level_indication |
178 |
|
|
179 |
|
DPRINTF(DPRINTF_HEADER, "profile_and_level_indication %i", profile); |
180 |
|
|
181 |
} else if (start_code == VISOBJSEQ_STOP_CODE) { |
} else if (start_code == VISOBJSEQ_STOP_CODE) { |
182 |
|
|
183 |
BitstreamSkip(bs, 32); // visual_object_sequence_stop_code |
BitstreamSkip(bs, 32); // visual_object_sequence_stop_code |
184 |
|
|
185 |
|
DPRINTF(DPRINTF_STARTCODE, "</visual_object_sequence>"); |
186 |
|
|
187 |
} else if (start_code == VISOBJ_START_CODE) { |
} else if (start_code == VISOBJ_START_CODE) { |
188 |
// DEBUG("visual_object"); |
|
189 |
|
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 |
vol_ver_id = BitstreamGetBits(bs, 4); // visual_object_ver_id |
vol_ver_id = BitstreamGetBits(bs, 4); // visual_object_ver_id |
195 |
|
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 |
vol_ver_id = 1; |
vol_ver_id = 1; |
200 |
|
|
201 |
if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) // visual_object_type |
if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) // visual_object_type |
202 |
{ |
{ |
203 |
DEBUG("visual_object_type != video"); |
DPRINTF(DPRINTF_ERROR, "visual_object_type != video"); |
204 |
return -1; |
return -1; |
205 |
} |
} |
206 |
BitstreamSkip(bs, 4); |
BitstreamSkip(bs, 4); |
209 |
|
|
210 |
if (BitstreamGetBit(bs)) // video_signal_type |
if (BitstreamGetBit(bs)) // video_signal_type |
211 |
{ |
{ |
212 |
DEBUG("+ video_signal_type"); |
DPRINTF(DPRINTF_HEADER,"+ video_signal_type"); |
213 |
BitstreamSkip(bs, 3); // video_format |
BitstreamSkip(bs, 3); // video_format |
214 |
BitstreamSkip(bs, 1); // video_range |
BitstreamSkip(bs, 1); // video_range |
215 |
if (BitstreamGetBit(bs)) // color_description |
if (BitstreamGetBit(bs)) // color_description |
216 |
{ |
{ |
217 |
DEBUG("+ color_description"); |
DPRINTF(DPRINTF_HEADER,"+ color_description"); |
218 |
BitstreamSkip(bs, 8); // color_primaries |
BitstreamSkip(bs, 8); // color_primaries |
219 |
BitstreamSkip(bs, 8); // transfer_characteristics |
BitstreamSkip(bs, 8); // transfer_characteristics |
220 |
BitstreamSkip(bs, 8); // matrix_coefficients |
BitstreamSkip(bs, 8); // matrix_coefficients |
221 |
} |
} |
222 |
} |
} |
223 |
} else if ((start_code & ~0x1f) == VIDOBJ_START_CODE) { |
} else if ((start_code & ~VIDOBJ_START_CODE_MASK) == VIDOBJ_START_CODE) { |
224 |
|
|
225 |
|
DPRINTF(DPRINTF_STARTCODE, "<video_object>"); |
226 |
|
DPRINTF(DPRINTF_HEADER, "vo id %i", start_code & VIDOBJ_START_CODE_MASK); |
227 |
|
|
228 |
BitstreamSkip(bs, 32); // video_object_start_code |
BitstreamSkip(bs, 32); // video_object_start_code |
229 |
} else if ((start_code & ~0xf) == VIDOBJLAY_START_CODE) { |
|
230 |
// DEBUG("video_object_layer"); |
} else if ((start_code & ~VIDOBJLAY_START_CODE_MASK) == VIDOBJLAY_START_CODE) { |
231 |
|
|
232 |
|
DPRINTF(DPRINTF_STARTCODE, "<video_object_layer>"); |
233 |
|
DPRINTF(DPRINTF_HEADER, "vol id %i", start_code & VIDOBJLAY_START_CODE_MASK); |
234 |
|
|
235 |
BitstreamSkip(bs, 32); // video_object_layer_start_code |
BitstreamSkip(bs, 32); // video_object_layer_start_code |
236 |
|
|
237 |
BitstreamSkip(bs, 1); // random_accessible_vol |
BitstreamSkip(bs, 1); // random_accessible_vol |
239 |
// video_object_type_indication |
// video_object_type_indication |
240 |
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 |
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 |
241 |
{ |
{ |
242 |
DEBUG1("video_object_type_indication not supported", |
DPRINTF(DPRINTF_ERROR,"video_object_type_indication %i not supported ", |
243 |
BitstreamShowBits(bs, 8)); |
BitstreamShowBits(bs, 8)); |
244 |
return -1; |
return -1; |
245 |
} |
} |
248 |
|
|
249 |
if (BitstreamGetBit(bs)) // is_object_layer_identifier |
if (BitstreamGetBit(bs)) // is_object_layer_identifier |
250 |
{ |
{ |
251 |
DEBUG("+ is_object_layer_identifier"); |
DPRINTF(DPRINTF_HEADER, "+ is_object_layer_identifier"); |
252 |
vol_ver_id = BitstreamGetBits(bs, 4); // video_object_layer_verid |
vol_ver_id = BitstreamGetBits(bs, 4); // video_object_layer_verid |
253 |
|
DPRINTF(DPRINTF_HEADER,"ver_id %i", vol_ver_id); |
254 |
BitstreamSkip(bs, 3); // video_object_layer_priority |
BitstreamSkip(bs, 3); // video_object_layer_priority |
255 |
} else { |
} else { |
256 |
vol_ver_id = 1; |
vol_ver_id = 1; |
257 |
} |
} |
|
//DEBUGI("vol_ver_id", vol_ver_id); |
|
258 |
|
|
259 |
if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR) // aspect_ratio_info |
if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR) // aspect_ratio_info |
260 |
{ |
{ |
261 |
DEBUG("+ aspect_ratio_info"); |
DPRINTF(DPRINTF_HEADER, "+ aspect_ratio_info"); |
262 |
BitstreamSkip(bs, 8); // par_width |
BitstreamSkip(bs, 8); // par_width |
263 |
BitstreamSkip(bs, 8); // par_height |
BitstreamSkip(bs, 8); // par_height |
264 |
} |
} |
265 |
|
|
266 |
if (BitstreamGetBit(bs)) // vol_control_parameters |
if (BitstreamGetBit(bs)) // vol_control_parameters |
267 |
{ |
{ |
268 |
DEBUG("+ vol_control_parameters"); |
DPRINTF(DPRINTF_HEADER, "+ vol_control_parameters"); |
269 |
BitstreamSkip(bs, 2); // chroma_format |
BitstreamSkip(bs, 2); // chroma_format |
270 |
dec->low_delay = BitstreamGetBit(bs); // low_delay |
dec->low_delay = BitstreamGetBit(bs); // low_delay |
271 |
|
DPRINTF(DPRINTF_HEADER, "low_delay %i", dec->low_delay); |
272 |
if (BitstreamGetBit(bs)) // vbv_parameters |
if (BitstreamGetBit(bs)) // vbv_parameters |
273 |
{ |
{ |
274 |
DEBUG("+ vbv_parameters"); |
DPRINTF(DPRINTF_HEADER,"+ vbv_parameters"); |
275 |
BitstreamSkip(bs, 15); // first_half_bitrate |
BitstreamSkip(bs, 15); // first_half_bitrate |
276 |
READ_MARKER(); |
READ_MARKER(); |
277 |
BitstreamSkip(bs, 15); // latter_half_bitrate |
BitstreamSkip(bs, 15); // latter_half_bitrate |
288 |
} |
} |
289 |
|
|
290 |
dec->shape = BitstreamGetBits(bs, 2); // video_object_layer_shape |
dec->shape = BitstreamGetBits(bs, 2); // video_object_layer_shape |
291 |
// DEBUG1("shape", dec->shape); |
DPRINTF(DPRINTF_HEADER, "shape %i", dec->shape); |
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 |
298 |
|
|
299 |
// *************************** for decode B-frame time *********************** |
// *************************** for decode B-frame time *********************** |
300 |
time_increment_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution |
time_increment_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution |
301 |
time_increment_resolution--; |
|
302 |
//DEBUG1("time_increment_resolution=",time_increment_resolution); |
DPRINTF(DPRINTF_HEADER,"vop_time_increment_resolution %i", time_increment_resolution); |
303 |
|
|
304 |
|
// time_increment_resolution--; |
305 |
|
|
306 |
if (time_increment_resolution > 0) { |
if (time_increment_resolution > 0) { |
307 |
dec->time_inc_bits = log2bin(time_increment_resolution); |
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 |
311 |
dec->time_inc_bits = 1; |
dec->time_inc_bits = 1; |
312 |
} |
} |
315 |
|
|
316 |
if (BitstreamGetBit(bs)) // fixed_vop_rate |
if (BitstreamGetBit(bs)) // fixed_vop_rate |
317 |
{ |
{ |
318 |
|
DPRINTF(DPRINTF_HEADER, "+ fixed_vop_rate"); |
319 |
BitstreamSkip(bs, dec->time_inc_bits); // fixed_vop_time_increment |
BitstreamSkip(bs, dec->time_inc_bits); // fixed_vop_time_increment |
320 |
} |
} |
321 |
|
|
326 |
|
|
327 |
READ_MARKER(); |
READ_MARKER(); |
328 |
width = BitstreamGetBits(bs, 13); // video_object_layer_width |
width = BitstreamGetBits(bs, 13); // video_object_layer_width |
|
//DEBUGI("width", width); |
|
329 |
READ_MARKER(); |
READ_MARKER(); |
330 |
height = BitstreamGetBits(bs, 13); // video_object_layer_height |
height = BitstreamGetBits(bs, 13); // video_object_layer_height |
|
//DEBUGI("height", height); |
|
331 |
READ_MARKER(); |
READ_MARKER(); |
332 |
|
|
333 |
|
DPRINTF(DPRINTF_HEADER, "width %i", width); |
334 |
|
DPRINTF(DPRINTF_HEADER, "height %i", height); |
335 |
|
|
336 |
|
// for auto set width & height |
337 |
|
if (dec->width == 0) |
338 |
|
dec->width = width; |
339 |
|
if (dec->height == 0) |
340 |
|
dec->height = height; |
341 |
|
|
342 |
if (width != dec->width || height != dec->height) { |
if (width != dec->width || height != dec->height) { |
343 |
DEBUG("FATAL: video dimension discrepancy ***"); |
DPRINTF(DPRINTF_ERROR, "XVID_DEC_PARAM width/height does not match bitstream"); |
|
DEBUG2("bitstream width/height", width, height); |
|
|
DEBUG2("param width/height", dec->width, dec->height); |
|
344 |
return -1; |
return -1; |
345 |
} |
} |
346 |
|
|
347 |
} |
} |
348 |
|
|
349 |
if ((dec->interlacing = BitstreamGetBit(bs))) { |
dec->interlacing = BitstreamGetBit(bs); |
350 |
DEBUG("vol: interlacing"); |
DPRINTF(DPRINTF_HEADER, "interlace", dec->interlacing); |
|
} |
|
351 |
|
|
352 |
if (!BitstreamGetBit(bs)) // obmc_disable |
if (!BitstreamGetBit(bs)) // obmc_disable |
353 |
{ |
{ |
354 |
DEBUG("IGNORED/TODO: !obmc_disable"); |
DPRINTF(DPRINTF_ERROR, "obmc_disabled==false not supported"); |
355 |
// TODO |
// TODO |
356 |
// fucking divx4.02 has this enabled |
// fucking divx4.02 has this enabled |
357 |
} |
} |
358 |
|
|
359 |
if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2))) // sprite_enable |
if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2))) // sprite_enable |
360 |
{ |
{ |
361 |
DEBUG("sprite_enable; not supported"); |
DPRINTF(DPRINTF_ERROR, "spriate_enabled not supported"); |
362 |
return -1; |
return -1; |
363 |
} |
} |
364 |
|
|
369 |
|
|
370 |
if (BitstreamGetBit(bs)) // not_8_bit |
if (BitstreamGetBit(bs)) // not_8_bit |
371 |
{ |
{ |
372 |
DEBUG("+ not_8_bit [IGNORED/TODO]"); |
DPRINTF(DPRINTF_HEADER, "not_8_bit==true (ignored)"); |
373 |
dec->quant_bits = BitstreamGetBits(bs, 4); // quant_precision |
dec->quant_bits = BitstreamGetBits(bs, 4); // quant_precision |
374 |
BitstreamSkip(bs, 4); // bits_per_pixel |
BitstreamSkip(bs, 4); // bits_per_pixel |
375 |
} else { |
} else { |
383 |
} |
} |
384 |
|
|
385 |
dec->quant_type = BitstreamGetBit(bs); // quant_type |
dec->quant_type = BitstreamGetBit(bs); // quant_type |
386 |
// DEBUG1("**** quant_type", dec->quant_type); |
DPRINTF(DPRINTF_HEADER, "quant_type %i", dec->quant_type); |
387 |
|
|
388 |
if (dec->quant_type) { |
if (dec->quant_type) { |
389 |
if (BitstreamGetBit(bs)) // load_intra_quant_mat |
if (BitstreamGetBit(bs)) // load_intra_quant_mat |
390 |
{ |
{ |
391 |
uint8_t matrix[64]; |
uint8_t matrix[64]; |
392 |
|
|
393 |
|
DPRINTF(DPRINTF_HEADER, "load_intra_quant_mat"); |
394 |
|
|
395 |
bs_get_matrix(bs, matrix); |
bs_get_matrix(bs, matrix); |
396 |
set_intra_matrix(matrix); |
set_intra_matrix(matrix); |
397 |
} else |
} else |
401 |
{ |
{ |
402 |
uint8_t matrix[64]; |
uint8_t matrix[64]; |
403 |
|
|
404 |
|
DPRINTF(DPRINTF_HEADER, "load_inter_quant_mat"); |
405 |
|
|
406 |
bs_get_matrix(bs, matrix); |
bs_get_matrix(bs, matrix); |
407 |
set_inter_matrix(matrix); |
set_inter_matrix(matrix); |
408 |
} else |
} else |
409 |
set_inter_matrix(get_default_inter_matrix()); |
set_inter_matrix(get_default_inter_matrix()); |
410 |
|
|
411 |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) { |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) { |
412 |
// TODO |
DPRINTF(DPRINTF_ERROR, "greyscale matrix not supported"); |
|
DEBUG("TODO: grayscale matrix stuff"); |
|
413 |
return -1; |
return -1; |
414 |
} |
} |
415 |
|
|
417 |
|
|
418 |
|
|
419 |
if (vol_ver_id != 1) { |
if (vol_ver_id != 1) { |
420 |
dec->quarterpel = BitstreamGetBit(bs); // quarter_sampe |
DPRINTF(DPRINTF_DEBUG, "QUARTERPEL BITSTREAM"); |
421 |
if (dec->quarterpel) { |
dec->quarterpel = BitstreamGetBit(bs); // quarter_sample |
|
DEBUG("IGNORED/TODO: quarter_sample"); |
|
422 |
} |
} |
423 |
} else { |
else |
424 |
dec->quarterpel = 0; |
dec->quarterpel = 0; |
425 |
} |
|
426 |
|
|
427 |
if (!BitstreamGetBit(bs)) // complexity_estimation_disable |
if (!BitstreamGetBit(bs)) // complexity_estimation_disable |
428 |
{ |
{ |
429 |
DEBUG("TODO: complexity_estimation header"); |
DPRINTF(DPRINTF_ERROR, "complexity_estimation not supported"); |
|
// TODO |
|
430 |
return -1; |
return -1; |
431 |
} |
} |
432 |
|
|
433 |
if (!BitstreamGetBit(bs)) // resync_marker_disable |
BitstreamSkip(bs, 1); // resync_marker_disable |
|
{ |
|
|
DEBUG("IGNORED/TODO: !resync_marker_disable"); |
|
|
// TODO |
|
|
} |
|
434 |
|
|
435 |
if (BitstreamGetBit(bs)) // data_partitioned |
if (BitstreamGetBit(bs)) // data_partitioned |
436 |
{ |
{ |
437 |
DEBUG("+ data_partitioned"); |
DPRINTF(DPRINTF_ERROR, "data_partitioned not supported"); |
438 |
BitstreamSkip(bs, 1); // reversible_vlc |
BitstreamSkip(bs, 1); // reversible_vlc |
439 |
} |
} |
440 |
|
|
441 |
if (vol_ver_id != 1) { |
if (vol_ver_id != 1) { |
442 |
if (BitstreamGetBit(bs)) // newpred_enable |
if (BitstreamGetBit(bs)) // newpred_enable |
443 |
{ |
{ |
444 |
DEBUG("+ 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 |
if (BitstreamGetBit(bs)) // reduced_resolution_vop_enable |
if (BitstreamGetBit(bs)) // reduced_resolution_vop_enable |
449 |
{ |
{ |
450 |
DEBUG("TODO: reduced_resolution_vop_enable"); |
DPRINTF(DPRINTF_ERROR, "reduced_resolution_vop not supported"); |
|
// TODO |
|
451 |
return -1; |
return -1; |
452 |
} |
} |
453 |
} |
} |
454 |
|
|
455 |
if ((dec->scalability = BitstreamGetBit(bs))) // scalability |
if ((dec->scalability = BitstreamGetBit(bs))) // scalability |
456 |
{ |
{ |
457 |
// TODO |
DPRINTF(DPRINTF_ERROR, "scalability not supported"); |
|
DEBUG("TODO: scalability"); |
|
458 |
return -1; |
return -1; |
459 |
} |
} |
460 |
} else // dec->shape == BINARY_ONLY |
} else // dec->shape == BINARY_ONLY |
462 |
if (vol_ver_id != 1) { |
if (vol_ver_id != 1) { |
463 |
if (BitstreamGetBit(bs)) // scalability |
if (BitstreamGetBit(bs)) // scalability |
464 |
{ |
{ |
465 |
// TODO |
DPRINTF(DPRINTF_ERROR, "scalability not supported"); |
|
DEBUG("TODO: scalability"); |
|
466 |
return -1; |
return -1; |
467 |
} |
} |
468 |
} |
} |
471 |
} |
} |
472 |
|
|
473 |
} else if (start_code == GRPOFVOP_START_CODE) { |
} else if (start_code == GRPOFVOP_START_CODE) { |
474 |
// DEBUG("group_of_vop"); |
|
475 |
|
DPRINTF(DPRINTF_STARTCODE, "<group_of_vop>"); |
476 |
|
|
477 |
BitstreamSkip(bs, 32); |
BitstreamSkip(bs, 32); |
478 |
{ |
{ |
479 |
int hours, minutes, seconds; |
int hours, minutes, seconds; |
482 |
minutes = BitstreamGetBits(bs, 6); |
minutes = BitstreamGetBits(bs, 6); |
483 |
READ_MARKER(); |
READ_MARKER(); |
484 |
seconds = BitstreamGetBits(bs, 6); |
seconds = BitstreamGetBits(bs, 6); |
485 |
// DEBUG3("hms", hours, minutes, seconds); |
|
486 |
|
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 |
490 |
|
|
491 |
} else if (start_code == VOP_START_CODE) { |
} else if (start_code == VOP_START_CODE) { |
492 |
// DEBUG("vop_start_code"); |
|
493 |
|
DPRINTF(DPRINTF_STARTCODE, "<vop>"); |
494 |
|
|
495 |
BitstreamSkip(bs, 32); // vop_start_code |
BitstreamSkip(bs, 32); // vop_start_code |
496 |
|
|
497 |
coding_type = BitstreamGetBits(bs, 2); // vop_coding_type |
coding_type = BitstreamGetBits(bs, 2); // vop_coding_type |
498 |
//DEBUG1("coding_type", coding_type); |
DPRINTF(DPRINTF_HEADER, "coding_type %i", coding_type); |
499 |
|
|
500 |
// *************************** for decode B-frame time *********************** |
// *************************** for decode B-frame time *********************** |
501 |
while (BitstreamGetBit(bs) != 0) // time_base |
while (BitstreamGetBit(bs) != 0) // time_base |
503 |
|
|
504 |
READ_MARKER(); |
READ_MARKER(); |
505 |
|
|
|
//DEBUG1("time_inc_bits", dec->time_inc_bits); |
|
|
//DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits)); |
|
506 |
if (dec->time_inc_bits) { |
if (dec->time_inc_bits) { |
|
//BitstreamSkip(bs, dec->time_inc_bits); // vop_time_increment |
|
507 |
time_increment = (BitstreamGetBits(bs, dec->time_inc_bits)); // vop_time_increment |
time_increment = (BitstreamGetBits(bs, dec->time_inc_bits)); // vop_time_increment |
508 |
} |
} |
509 |
|
|
510 |
|
DPRINTF(DPRINTF_HEADER, "time_base %i", time_incr); |
511 |
|
DPRINTF(DPRINTF_HEADER, "time_increment %i", time_increment); |
512 |
|
|
513 |
|
DPRINTF(DPRINTF_TIMECODE, "%c %i:%i", |
514 |
|
coding_type == I_VOP ? 'I' : coding_type == P_VOP ? 'P' : 'B', |
515 |
|
time_incr, time_increment); |
516 |
|
|
517 |
if (coding_type != B_VOP) { |
if (coding_type != B_VOP) { |
518 |
dec->last_time_base = dec->time_base; |
dec->last_time_base = dec->time_base; |
519 |
dec->time_base += time_incr; |
dec->time_base += time_incr; |
520 |
dec->time = |
dec->time = time_increment; |
521 |
dec->time_base * time_increment_resolution + |
|
522 |
|
/* dec->time_base * time_increment_resolution + |
523 |
time_increment; |
time_increment; |
524 |
dec->time_pp = (uint32_t) (dec->time - dec->last_non_b_time); |
*/ dec->time_pp = (uint32_t) |
525 |
|
(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 = |
dec->time = time_increment; |
529 |
|
/* |
530 |
(dec->last_time_base + |
(dec->last_time_base + |
531 |
time_incr) * time_increment_resolution + time_increment; |
time_incr) * time_increment_resolution + time_increment; |
532 |
dec->time_bp = (uint32_t) (dec->last_non_b_time - dec->time); |
*/ |
533 |
|
dec->time_bp = (uint32_t) |
534 |
|
(time_increment_resolution + dec->last_non_b_time - dec->time)%time_increment_resolution; |
535 |
} |
} |
|
//DEBUG1("time_increment=",time_increment); |
|
536 |
|
|
537 |
READ_MARKER(); |
READ_MARKER(); |
538 |
|
|
539 |
if (!BitstreamGetBit(bs)) // vop_coded |
if (!BitstreamGetBit(bs)) // vop_coded |
540 |
{ |
{ |
541 |
|
DPRINTF(DPRINTF_HEADER, "vop_coded==false"); |
542 |
return N_VOP; |
return N_VOP; |
543 |
} |
} |
544 |
|
|
551 |
if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) && |
if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) && |
552 |
(coding_type == P_VOP)) { |
(coding_type == P_VOP)) { |
553 |
*rounding = BitstreamGetBit(bs); // rounding_type |
*rounding = BitstreamGetBit(bs); // rounding_type |
554 |
//DEBUG1("rounding", *rounding); |
DPRINTF(DPRINTF_HEADER, "rounding %i", *rounding); |
555 |
} |
} |
556 |
|
|
557 |
/* if (reduced_resolution_enable) |
/* if (reduced_resolution_enable) |
572 |
vert_mc_ref = BitstreamGetBits(bs, 13); |
vert_mc_ref = BitstreamGetBits(bs, 13); |
573 |
READ_MARKER(); |
READ_MARKER(); |
574 |
|
|
575 |
// DEBUG2("vop_width/height", width, height); |
DPRINTF(DPRINTF_HEADER, "width %i", width); |
576 |
// DEBUG2("ref ", horiz_mc_ref, vert_mc_ref); |
DPRINTF(DPRINTF_HEADER, "height %i", height); |
577 |
|
DPRINTF(DPRINTF_HEADER, "horiz_mc_ref %i", horiz_mc_ref); |
578 |
|
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 |
591 |
intra_dc_threshold_table[BitstreamGetBits(bs, 3)]; |
intra_dc_threshold_table[BitstreamGetBits(bs, 3)]; |
592 |
|
|
593 |
if (dec->interlacing) { |
if (dec->interlacing) { |
594 |
if ((dec->top_field_first = BitstreamGetBit(bs))) { |
dec->top_field_first = BitstreamGetBit(bs); |
595 |
DEBUG("vop: top_field_first"); |
DPRINTF(DPRINTF_HEADER, "interlace top_field_first %i", dec->top_field_first); |
596 |
} |
dec->alternate_vertical_scan = BitstreamGetBit(bs); |
597 |
if ((dec->alternate_vertical_scan = BitstreamGetBit(bs))) { |
DPRINTF(DPRINTF_HEADER, "interlace alternate_vertical_scan %i", dec->alternate_vertical_scan); |
598 |
DEBUG("vop: alternate_vertical_scan"); |
|
|
} |
|
599 |
} |
} |
600 |
} |
} |
601 |
|
|
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 |
//DEBUG1("quant", *quant); |
DPRINTF(DPRINTF_HEADER, "quant %i", *quant); |
606 |
|
|
607 |
if (coding_type != I_VOP) { |
if (coding_type != I_VOP) { |
608 |
*fcode_forward = BitstreamGetBits(bs, 3); // fcode_forward |
*fcode_forward = BitstreamGetBits(bs, 3); // fcode_forward |
609 |
|
DPRINTF(DPRINTF_HEADER, "fcode_forward %i", *fcode_forward); |
610 |
} |
} |
611 |
|
|
612 |
if (coding_type == B_VOP) { |
if (coding_type == B_VOP) { |
613 |
*fcode_backward = BitstreamGetBits(bs, 3); // fcode_backward |
*fcode_backward = BitstreamGetBits(bs, 3); // fcode_backward |
614 |
|
DPRINTF(DPRINTF_HEADER, "fcode_backward %i", *fcode_backward); |
615 |
} |
} |
616 |
if (!dec->scalability) { |
if (!dec->scalability) { |
617 |
if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) && |
if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) && |
620 |
} |
} |
621 |
} |
} |
622 |
return coding_type; |
return coding_type; |
623 |
|
|
624 |
} else if (start_code == USERDATA_START_CODE) { |
} else if (start_code == USERDATA_START_CODE) { |
625 |
// DEBUG("user_data"); |
|
626 |
|
DPRINTF(DPRINTF_STARTCODE, "<user_data>"); |
627 |
|
|
628 |
BitstreamSkip(bs, 32); // user_data_start_code |
BitstreamSkip(bs, 32); // user_data_start_code |
629 |
|
|
630 |
} else // start_code == ? |
} else // start_code == ? |
631 |
{ |
{ |
632 |
if (BitstreamShowBits(bs, 24) == 0x000001) { |
if (BitstreamShowBits(bs, 24) == 0x000001) { |
633 |
DEBUG1("*** WARNING: unknown start_code", |
DPRINTF(DPRINTF_STARTCODE, "<unknown: %x>", BitstreamShowBits(bs, 32)); |
|
BitstreamShowBits(bs, 32)); |
|
634 |
} |
} |
635 |
BitstreamSkip(bs, 8); |
BitstreamSkip(bs, 8); |
636 |
} |
} |
637 |
} |
} |
638 |
while ((BitstreamPos(bs) >> 3) < bs->length); |
while ((BitstreamPos(bs) >> 3) < bs->length); |
639 |
|
|
640 |
DEBUG("*** WARNING: no vop_start_code found"); |
//DPRINTF("*** WARNING: no vop_start_code found"); |
641 |
return -1; /* ignore it */ |
return -1; /* ignore it */ |
642 |
} |
} |
643 |
|
|
651 |
int i, j; |
int i, j; |
652 |
const int last = matrix[scan_tables[0][63]]; |
const int last = matrix[scan_tables[0][63]]; |
653 |
|
|
654 |
for (j = 63; j >= 0 && matrix[scan_tables[0][j - 1]] == last; j--); |
for (j = 63; j > 0 && matrix[scan_tables[0][j - 1]] == last; j--); |
655 |
|
|
656 |
for (i = 0; i <= j; i++) { |
for (i = 0; i <= j; i++) { |
657 |
BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8); |
BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8); |
685 |
BitstreamPutBit(bs, 0); // is_object_layer_identified (0=not given) |
BitstreamPutBit(bs, 0); // is_object_layer_identified (0=not given) |
686 |
BitstreamPutBits(bs, 1, 4); // aspect_ratio_info (1=1:1) |
BitstreamPutBits(bs, 1, 4); // aspect_ratio_info (1=1:1) |
687 |
|
|
|
#ifdef BFRAMES |
|
|
if (pParam->max_bframes > 0) { |
|
|
dprintf("low_delay=1"); |
|
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" |
|
BitstreamPutBit(bs, 0); // low_delay |
|
|
BitstreamPutBit(bs, 0); // vbv_parameters (0=not given) |
|
|
} else |
|
|
#endif |
|
|
{ |
|
|
BitstreamPutBits(bs, 0, 1); // vol_control_parameters (0=not given) |
|
|
} |
|
690 |
|
|
691 |
|
BitstreamPutBit(bs, 1); // low_delay |
692 |
|
|
693 |
|
BitstreamPutBit(bs, 0); // vbv_parameters (0=not given) |
694 |
|
|
695 |
BitstreamPutBits(bs, 0, 2); // video_object_layer_shape (0=rectangular) |
BitstreamPutBits(bs, 0, 2); // video_object_layer_shape (0=rectangular) |
696 |
|
|
697 |
WRITE_MARKER(); |
WRITE_MARKER(); |
698 |
|
|
699 |
/* time_increment_resolution; ignored by current decore versions |
/* |
700 |
eg. 2fps res=2 inc=1 |
* time_increment_resolution; ignored by current decore versions |
701 |
25fps res=25 inc=1 |
* eg. 2fps res=2 inc=1 |
702 |
29.97fps res=30000 inc=1001 |
* 25fps res=25 inc=1 |
703 |
|
* 29.97fps res=30000 inc=1001 |
704 |
*/ |
*/ |
|
#ifdef BFRAMES |
|
705 |
BitstreamPutBits(bs, pParam->fbase, 16); |
BitstreamPutBits(bs, pParam->fbase, 16); |
|
#else |
|
|
BitstreamPutBits(bs, 2, 16); |
|
|
#endif |
|
706 |
|
|
|
WRITE_MARKER(); |
|
707 |
|
|
708 |
// fixed_vop_rate |
WRITE_MARKER(); |
|
BitstreamPutBit(bs, 0); |
|
709 |
|
|
710 |
// fixed_time_increment: value=nth_of_sec, nbits = log2(resolution) |
BitstreamPutBit(bs, 1); // fixed_vop_rate = 1 |
711 |
// BitstreamPutBits(bs, 0, 15); |
BitstreamPutBits(bs, pParam->fincr, log2bin(pParam->fbase)); // fixed_vop_time_increment |
712 |
|
|
713 |
WRITE_MARKER(); |
WRITE_MARKER(); |
714 |
BitstreamPutBits(bs, pParam->width, 13); // width |
BitstreamPutBits(bs, pParam->width, 13); // width |
755 |
void |
void |
756 |
BitstreamWriteVopHeader(Bitstream * const bs, |
BitstreamWriteVopHeader(Bitstream * const bs, |
757 |
const MBParam * pParam, |
const MBParam * pParam, |
758 |
const FRAMEINFO * frame) |
const FRAMEINFO * frame, |
759 |
|
int vop_coded) |
760 |
{ |
{ |
|
#ifdef BFRAMES |
|
761 |
uint32_t i; |
uint32_t i; |
762 |
#endif |
|
763 |
BitstreamPad(bs); |
BitstreamPad(bs); |
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); |
767 |
|
|
768 |
// time_base = 0 write n x PutBit(1), PutBit(0) |
// time_base = 0 write n x PutBit(1), PutBit(0) |
|
#ifdef BFRAMES |
|
769 |
for (i = 0; i < frame->seconds; i++) { |
for (i = 0; i < frame->seconds; i++) { |
770 |
BitstreamPutBit(bs, 1); |
BitstreamPutBit(bs, 1); |
771 |
} |
} |
772 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
|
#else |
|
|
BitstreamPutBits(bs, 0, 1); |
|
|
#endif |
|
773 |
|
|
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) |
|
#ifdef BFRAMES |
|
777 |
BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase)); |
BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase)); |
|
dprintf("[%i:%i] %c\n", frame->seconds, frame->ticks, |
|
|
frame->coding_type == I_VOP ? 'I' : frame->coding_type == |
|
|
P_VOP ? 'P' : 'B'); |
|
|
#else |
|
|
BitstreamPutBits(bs, 1, 1); |
|
|
#endif |
|
778 |
|
|
779 |
WRITE_MARKER(); |
WRITE_MARKER(); |
780 |
|
|
781 |
|
if (!vop_coded) { |
782 |
|
BitstreamPutBits(bs, 0, 1); |
783 |
|
return; |
784 |
|
} |
785 |
|
|
786 |
BitstreamPutBits(bs, 1, 1); // vop_coded |
BitstreamPutBits(bs, 1, 1); // vop_coded |
787 |
|
|
788 |
if (frame->coding_type == P_VOP) |
if (frame->coding_type == P_VOP) |