41 |
* * |
* * |
42 |
* Revision history: * |
* Revision history: * |
43 |
* * |
* * |
44 |
|
* 28.10.2002 GMC support - gruel * |
45 |
|
* 04.10.2002 qpel support - Isibaar * |
46 |
|
* 11.07.2002 add VOP width & height return to dec when dec->width * |
47 |
|
* or dec->height is 0 (for use in examples/ex1.c) * |
48 |
|
* MinChen <chenm001@163.com> * |
49 |
|
* 22.05.2002 bs_put_matrix fix * |
50 |
|
* 20.05.2002 added BitstreamWriteUserData * |
51 |
|
* 19.06.2002 Fix a little bug in use custom quant matrix * |
52 |
|
* MinChen <chenm001@163.com> * |
53 |
|
* 08.05.2002 add low_delay support for B_VOP decode * |
54 |
|
* MinChen <chenm001@163.com> * |
55 |
|
* 06.05.2002 low_delay * |
56 |
|
* 06.05.2002 fixed fincr/fbase error * |
57 |
|
* 01.05.2002 added BVOP support to BitstreamWriteVopHeader * |
58 |
* 15.04.2002 rewrite log2bin use asm386 By MinChen <chenm001@163.com> * |
* 15.04.2002 rewrite log2bin use asm386 By MinChen <chenm001@163.com> * |
59 |
* 26.03.2002 interlacing support * |
* 26.03.2002 interlacing support * |
60 |
* 03.03.2002 qmatrix writing * |
* 03.03.2002 qmatrix writing * |
69 |
#include "bitstream.h" |
#include "bitstream.h" |
70 |
#include "zigzag.h" |
#include "zigzag.h" |
71 |
#include "../quant/quant_matrix.h" |
#include "../quant/quant_matrix.h" |
72 |
|
#include "mbcoding.h" |
73 |
|
|
74 |
|
|
75 |
static int __inline log2bin(int value) |
static uint32_t __inline |
76 |
|
log2bin(uint32_t value) |
77 |
{ |
{ |
78 |
/* Changed by Chenm001 */ |
/* Changed by Chenm001 */ |
79 |
#ifndef WIN32 |
#ifndef WIN32 |
80 |
int n = 0; |
int n = 0; |
81 |
while (value) |
|
82 |
{ |
while (value) { |
83 |
value >>= 1; |
value >>= 1; |
84 |
n++; |
n++; |
85 |
} |
} |
93 |
} |
} |
94 |
|
|
95 |
|
|
96 |
static const uint32_t intra_dc_threshold_table[] = |
static const uint32_t intra_dc_threshold_table[] = { |
|
{ |
|
97 |
32, /* never use */ |
32, /* never use */ |
98 |
13, |
13, |
99 |
15, |
15, |
105 |
}; |
}; |
106 |
|
|
107 |
|
|
108 |
void bs_get_matrix(Bitstream * bs, uint8_t * matrix) |
void |
109 |
|
bs_get_matrix(Bitstream * bs, |
110 |
|
uint8_t * matrix) |
111 |
{ |
{ |
112 |
int i = 0; |
int i = 0; |
113 |
int last, value = 0; |
int last, value = 0; |
114 |
|
|
115 |
do |
do { |
|
{ |
|
116 |
last = value; |
last = value; |
117 |
value = BitstreamGetBits(bs, 8); |
value = BitstreamGetBits(bs, 8); |
118 |
matrix[ scan_tables[0][i++] ] = value; |
matrix[ scan_tables[0][i++] ] = value; |
119 |
} |
} |
120 |
while (value != 0 && i < 64); |
while (value != 0 && i < 64); |
121 |
|
i--; // fix little bug at coeff not full |
122 |
|
|
123 |
while (i < 64) |
while (i < 64) { |
|
{ |
|
124 |
matrix[ scan_tables[0][i++] ] = last; |
matrix[ scan_tables[0][i++] ] = last; |
125 |
} |
} |
126 |
} |
} |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
// for PVOP addbits == fcode - 1 |
131 |
|
// for BVOP addbits == max(fcode,bcode) - 1 |
132 |
|
// returns mbpos |
133 |
|
int |
134 |
|
read_video_packet_header(Bitstream *bs, |
135 |
|
DECODER * dec, |
136 |
|
const int addbits, |
137 |
|
int * quant, |
138 |
|
int * fcode_forward, |
139 |
|
int * fcode_backward, |
140 |
|
int * intra_dc_threshold) |
141 |
|
{ |
142 |
|
int startcode_bits = NUMBITS_VP_RESYNC_MARKER + addbits; |
143 |
|
int mbnum_bits = log2bin(dec->mb_width * dec->mb_height - 1); |
144 |
|
int mbnum; |
145 |
|
int hec = 0; |
146 |
|
|
147 |
|
BitstreamSkip(bs, BitstreamNumBitsToByteAlign(bs)); |
148 |
|
BitstreamSkip(bs, startcode_bits); |
149 |
|
|
150 |
|
DPRINTF(DPRINTF_STARTCODE, "<video_packet_header>"); |
151 |
|
|
152 |
|
if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) |
153 |
|
{ |
154 |
|
hec = BitstreamGetBit(bs); /* header_extension_code */ |
155 |
|
if (hec && !(dec->sprite_enable == SPRITE_STATIC /* && current_coding_type = I_VOP */)) |
156 |
|
{ |
157 |
|
BitstreamSkip(bs, 13); /* vop_width */ |
158 |
|
READ_MARKER(); |
159 |
|
BitstreamSkip(bs, 13); /* vop_height */ |
160 |
|
READ_MARKER(); |
161 |
|
BitstreamSkip(bs, 13); /* vop_horizontal_mc_spatial_ref */ |
162 |
|
READ_MARKER(); |
163 |
|
BitstreamSkip(bs, 13); /* vop_vertical_mc_spatial_ref */ |
164 |
|
READ_MARKER(); |
165 |
|
} |
166 |
|
} |
167 |
|
|
168 |
|
mbnum = BitstreamGetBits(bs, mbnum_bits); /* macroblock_number */ |
169 |
|
DPRINTF(DPRINTF_HEADER, "mbnum %i", mbnum); |
170 |
|
|
171 |
|
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) |
172 |
|
{ |
173 |
|
*quant = BitstreamGetBits(bs, 5); /* quant_scale */ |
174 |
|
DPRINTF(DPRINTF_HEADER, "quant %i", *quant); |
175 |
|
} |
176 |
|
|
177 |
|
if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) |
178 |
|
hec = BitstreamGetBit(bs); /* header_extension_code */ |
179 |
|
|
180 |
|
|
181 |
|
DPRINTF(DPRINTF_HEADER, "header_extension_code %i", hec); |
182 |
|
if (hec) |
183 |
|
{ |
184 |
|
int time_base; |
185 |
|
int time_increment; |
186 |
|
int coding_type; |
187 |
|
|
188 |
|
for (time_base=0; BitstreamGetBit(bs)!=0; time_base++); /* modulo_time_base */ |
189 |
|
READ_MARKER(); |
190 |
|
if (dec->time_inc_bits) |
191 |
|
time_increment = (BitstreamGetBits(bs, dec->time_inc_bits)); /* vop_time_increment */ |
192 |
|
READ_MARKER(); |
193 |
|
DPRINTF(DPRINTF_HEADER,"time %i:%i", time_base, time_increment); |
194 |
|
|
195 |
|
coding_type = BitstreamGetBits(bs, 2); |
196 |
|
DPRINTF(DPRINTF_HEADER,"coding_type %i", coding_type); |
197 |
|
|
198 |
|
if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) |
199 |
|
{ |
200 |
|
BitstreamSkip(bs, 1); /* change_conv_ratio_disable */ |
201 |
|
if (coding_type != I_VOP) |
202 |
|
BitstreamSkip(bs, 1); /* vop_shape_coding_type */ |
203 |
|
} |
204 |
|
|
205 |
|
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) |
206 |
|
{ |
207 |
|
*intra_dc_threshold = intra_dc_threshold_table[BitstreamGetBits(bs, 3)]; |
208 |
|
|
209 |
|
if (dec->sprite_enable == SPRITE_GMC && coding_type == S_VOP && |
210 |
|
dec->sprite_warping_points > 0) |
211 |
|
{ |
212 |
|
// TODO: sprite trajectory |
213 |
|
} |
214 |
|
if (dec->reduced_resolution_enable && |
215 |
|
dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR && |
216 |
|
(coding_type == P_VOP || coding_type == I_VOP)) |
217 |
|
{ |
218 |
|
BitstreamSkip(bs, 1); /* vop_reduced_resolution */ |
219 |
|
} |
220 |
|
|
221 |
|
if (coding_type != I_VOP && fcode_forward) |
222 |
|
{ |
223 |
|
*fcode_forward = BitstreamGetBits(bs, 3); |
224 |
|
DPRINTF(DPRINTF_HEADER,"fcode_forward %i", *fcode_forward); |
225 |
|
} |
226 |
|
|
227 |
|
if (coding_type == B_VOP && fcode_backward) |
228 |
|
{ |
229 |
|
*fcode_backward = BitstreamGetBits(bs, 3); |
230 |
|
DPRINTF(DPRINTF_HEADER,"fcode_backward %i", fcode_backward); |
231 |
|
} |
232 |
|
} |
233 |
|
|
234 |
|
} |
235 |
|
|
236 |
|
if (dec->newpred_enable) |
237 |
|
{ |
238 |
|
int vop_id; |
239 |
|
int vop_id_for_prediction; |
240 |
|
|
241 |
|
vop_id = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15)); |
242 |
|
DPRINTF(DPRINTF_HEADER, "vop_id %i", vop_id); |
243 |
|
if (BitstreamGetBit(bs)) /* vop_id_for_prediction_indication */ |
244 |
|
{ |
245 |
|
vop_id_for_prediction = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15)); |
246 |
|
DPRINTF(DPRINTF_HEADER, "vop_id_for_prediction %i", vop_id_for_prediction); |
247 |
|
} |
248 |
|
READ_MARKER(); |
249 |
|
} |
250 |
|
|
251 |
|
return mbnum; |
252 |
|
} |
253 |
|
|
254 |
|
|
255 |
|
|
256 |
/* |
/* |
257 |
decode headers |
decode headers |
258 |
returns coding_type, or -1 if error |
returns coding_type, or -1 if error |
259 |
*/ |
*/ |
260 |
|
|
261 |
int BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding, uint32_t * quant, uint32_t * fcode, uint32_t * intra_dc_threshold) |
#define VIDOBJ_START_CODE_MASK 0x0000001f |
262 |
|
#define VIDOBJLAY_START_CODE_MASK 0x0000000f |
263 |
|
|
264 |
|
int |
265 |
|
BitstreamReadHeaders(Bitstream * bs, |
266 |
|
DECODER * dec, |
267 |
|
uint32_t * rounding, |
268 |
|
uint32_t * reduced_resolution, |
269 |
|
uint32_t * quant, |
270 |
|
uint32_t * fcode_forward, |
271 |
|
uint32_t * fcode_backward, |
272 |
|
uint32_t * intra_dc_threshold, |
273 |
|
VECTOR * gmc_mv) |
274 |
{ |
{ |
275 |
uint32_t vol_ver_id; |
uint32_t vol_ver_id; |
|
uint32_t time_inc_resolution; |
|
276 |
uint32_t coding_type; |
uint32_t coding_type; |
277 |
uint32_t start_code; |
uint32_t start_code; |
278 |
|
uint32_t time_incr = 0; |
279 |
|
int32_t time_increment; |
280 |
|
int resize = 0; |
281 |
|
|
282 |
|
do { |
283 |
|
|
|
do |
|
|
{ |
|
284 |
BitstreamByteAlign(bs); |
BitstreamByteAlign(bs); |
285 |
start_code = BitstreamShowBits(bs, 32); |
start_code = BitstreamShowBits(bs, 32); |
286 |
|
|
287 |
if (start_code == VISOBJSEQ_START_CODE) |
if (start_code == VISOBJSEQ_START_CODE) { |
288 |
{ |
|
289 |
// DEBUG("visual_object_sequence"); |
int profile; |
290 |
|
|
291 |
|
DPRINTF(DPRINTF_STARTCODE, "<visual_object_sequence>"); |
292 |
|
|
293 |
BitstreamSkip(bs, 32); // visual_object_sequence_start_code |
BitstreamSkip(bs, 32); // visual_object_sequence_start_code |
294 |
BitstreamSkip(bs, 8); // profile_and_level_indication |
profile = BitstreamGetBits(bs, 8); // profile_and_level_indication |
295 |
} |
|
296 |
else if (start_code == VISOBJSEQ_STOP_CODE) |
DPRINTF(DPRINTF_HEADER, "profile_and_level_indication %i", profile); |
297 |
{ |
|
298 |
|
} else if (start_code == VISOBJSEQ_STOP_CODE) { |
299 |
|
|
300 |
BitstreamSkip(bs, 32); // visual_object_sequence_stop_code |
BitstreamSkip(bs, 32); // visual_object_sequence_stop_code |
301 |
} |
|
302 |
else if (start_code == VISOBJ_START_CODE) |
DPRINTF(DPRINTF_STARTCODE, "</visual_object_sequence>"); |
303 |
{ |
|
304 |
// DEBUG("visual_object"); |
} else if (start_code == VISOBJ_START_CODE) { |
305 |
|
|
306 |
|
DPRINTF(DPRINTF_STARTCODE, "<visual_object>"); |
307 |
|
|
308 |
BitstreamSkip(bs,32); // visual_object_start_code |
BitstreamSkip(bs,32); // visual_object_start_code |
309 |
if (BitstreamGetBit(bs)) // is_visual_object_identified |
if (BitstreamGetBit(bs)) // is_visual_object_identified |
310 |
{ |
{ |
311 |
vol_ver_id = BitstreamGetBits(bs,4); // visual_object_ver_id |
vol_ver_id = BitstreamGetBits(bs,4); // visual_object_ver_id |
312 |
|
DPRINTF(DPRINTF_HEADER,"ver_id %i", vol_ver_id); |
313 |
BitstreamSkip(bs, 3); // visual_object_priority |
BitstreamSkip(bs, 3); // visual_object_priority |
314 |
} |
} else { |
|
else |
|
|
{ |
|
315 |
vol_ver_id = 1; |
vol_ver_id = 1; |
316 |
} |
} |
317 |
|
|
318 |
if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) // visual_object_type |
if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) // visual_object_type |
319 |
{ |
{ |
320 |
DEBUG("visual_object_type != video"); |
DPRINTF(DPRINTF_ERROR, "visual_object_type != video"); |
321 |
return -1; |
return -1; |
322 |
} |
} |
323 |
BitstreamSkip(bs, 4); |
BitstreamSkip(bs, 4); |
326 |
|
|
327 |
if (BitstreamGetBit(bs)) // video_signal_type |
if (BitstreamGetBit(bs)) // video_signal_type |
328 |
{ |
{ |
329 |
DEBUG("+ video_signal_type"); |
DPRINTF(DPRINTF_HEADER,"+ video_signal_type"); |
330 |
BitstreamSkip(bs, 3); // video_format |
BitstreamSkip(bs, 3); // video_format |
331 |
BitstreamSkip(bs, 1); // video_range |
BitstreamSkip(bs, 1); // video_range |
332 |
if (BitstreamGetBit(bs)) // color_description |
if (BitstreamGetBit(bs)) // color_description |
333 |
{ |
{ |
334 |
DEBUG("+ color_description"); |
DPRINTF(DPRINTF_HEADER,"+ color_description"); |
335 |
BitstreamSkip(bs, 8); // color_primaries |
BitstreamSkip(bs, 8); // color_primaries |
336 |
BitstreamSkip(bs, 8); // transfer_characteristics |
BitstreamSkip(bs, 8); // transfer_characteristics |
337 |
BitstreamSkip(bs, 8); // matrix_coefficients |
BitstreamSkip(bs, 8); // matrix_coefficients |
338 |
} |
} |
339 |
} |
} |
340 |
} |
} else if ((start_code & ~VIDOBJ_START_CODE_MASK) == VIDOBJ_START_CODE) { |
341 |
else if ((start_code & ~0x1f) == VIDOBJ_START_CODE) |
|
342 |
{ |
DPRINTF(DPRINTF_STARTCODE, "<video_object>"); |
343 |
|
DPRINTF(DPRINTF_HEADER, "vo id %i", start_code & VIDOBJ_START_CODE_MASK); |
344 |
|
|
345 |
BitstreamSkip(bs, 32); // video_object_start_code |
BitstreamSkip(bs, 32); // video_object_start_code |
346 |
} |
|
347 |
else if ((start_code & ~0xf) == VIDOBJLAY_START_CODE) |
} else if ((start_code & ~VIDOBJLAY_START_CODE_MASK) == VIDOBJLAY_START_CODE) { |
348 |
{ |
|
349 |
// DEBUG("video_object_layer"); |
DPRINTF(DPRINTF_STARTCODE, "<video_object_layer>"); |
350 |
|
DPRINTF(DPRINTF_HEADER, "vol id %i", start_code & VIDOBJLAY_START_CODE_MASK); |
351 |
|
|
352 |
BitstreamSkip(bs, 32); // video_object_layer_start_code |
BitstreamSkip(bs, 32); // video_object_layer_start_code |
353 |
|
|
354 |
BitstreamSkip(bs, 1); // random_accessible_vol |
BitstreamSkip(bs, 1); // random_accessible_vol |
357 |
if (BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_SIMPLE && |
if (BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_SIMPLE && |
358 |
BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_CORE && |
BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_CORE && |
359 |
BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_MAIN && |
BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_MAIN && |
360 |
|
BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_ACE && |
361 |
|
BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_ART_SIMPLE && |
362 |
BitstreamShowBits(bs, 8) != 0) // BUGGY DIVX |
BitstreamShowBits(bs, 8) != 0) // BUGGY DIVX |
363 |
{ |
{ |
364 |
DEBUG1("video_object_type_indication not supported", BitstreamShowBits(bs, 8)); |
DPRINTF(DPRINTF_ERROR,"video_object_type_indication %i not supported ", |
365 |
|
BitstreamShowBits(bs, 8)); |
366 |
return -1; |
return -1; |
367 |
} |
} |
368 |
BitstreamSkip(bs, 8); |
BitstreamSkip(bs, 8); |
370 |
|
|
371 |
if (BitstreamGetBit(bs)) // is_object_layer_identifier |
if (BitstreamGetBit(bs)) // is_object_layer_identifier |
372 |
{ |
{ |
373 |
DEBUG("+ is_object_layer_identifier"); |
DPRINTF(DPRINTF_HEADER, "+ is_object_layer_identifier"); |
374 |
vol_ver_id = BitstreamGetBits(bs,4); // video_object_layer_verid |
vol_ver_id = BitstreamGetBits(bs,4); // video_object_layer_verid |
375 |
|
DPRINTF(DPRINTF_HEADER,"ver_id %i", vol_ver_id); |
376 |
BitstreamSkip(bs, 3); // video_object_layer_priority |
BitstreamSkip(bs, 3); // video_object_layer_priority |
377 |
} |
} else { |
|
else |
|
|
{ |
|
378 |
vol_ver_id = 1; |
vol_ver_id = 1; |
379 |
} |
} |
|
//DEBUGI("vol_ver_id", vol_ver_id); |
|
380 |
|
|
381 |
if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR) // aspect_ratio_info |
dec->aspect_ratio = BitstreamGetBits(bs, 4); |
382 |
|
|
383 |
|
if (dec->aspect_ratio == VIDOBJLAY_AR_EXTPAR) // aspect_ratio_info |
384 |
{ |
{ |
385 |
DEBUG("+ aspect_ratio_info"); |
DPRINTF(DPRINTF_HEADER, "+ aspect_ratio_info"); |
386 |
BitstreamSkip(bs, 8); // par_width |
dec->par_width = BitstreamGetBits(bs, 8); // par_width |
387 |
BitstreamSkip(bs, 8); // par_height |
dec->par_height = BitstreamGetBits(bs, 8); // par_height |
388 |
} |
} |
389 |
|
|
390 |
if (BitstreamGetBit(bs)) // vol_control_parameters |
if (BitstreamGetBit(bs)) // vol_control_parameters |
391 |
{ |
{ |
392 |
DEBUG("+ vol_control_parameters"); |
DPRINTF(DPRINTF_HEADER, "+ vol_control_parameters"); |
393 |
BitstreamSkip(bs, 2); // chroma_format |
BitstreamSkip(bs, 2); // chroma_format |
394 |
BitstreamSkip(bs, 1); // low_delay |
dec->low_delay = BitstreamGetBit(bs); // low_delay |
395 |
|
DPRINTF(DPRINTF_HEADER, "low_delay %i", dec->low_delay); |
396 |
if (BitstreamGetBit(bs)) // vbv_parameters |
if (BitstreamGetBit(bs)) // vbv_parameters |
397 |
{ |
{ |
398 |
DEBUG("+ vbv_parameters"); |
DPRINTF(DPRINTF_HEADER,"+ vbv_parameters"); |
399 |
BitstreamSkip(bs, 15); // first_half_bitrate |
BitstreamSkip(bs, 15); // first_half_bitrate |
400 |
READ_MARKER(); |
READ_MARKER(); |
401 |
BitstreamSkip(bs, 15); // latter_half_bitrate |
BitstreamSkip(bs, 15); // latter_half_bitrate |
407 |
READ_MARKER(); |
READ_MARKER(); |
408 |
BitstreamSkip(bs, 15); // latter_half_vbv_occupancy |
BitstreamSkip(bs, 15); // latter_half_vbv_occupancy |
409 |
READ_MARKER(); |
READ_MARKER(); |
|
|
|
410 |
} |
} |
411 |
} |
} |
412 |
|
|
413 |
dec->shape = BitstreamGetBits(bs, 2); // video_object_layer_shape |
dec->shape = BitstreamGetBits(bs, 2); // video_object_layer_shape |
|
// DEBUG1("shape", dec->shape); |
|
414 |
|
|
415 |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) |
DPRINTF(DPRINTF_HEADER, "shape %i", dec->shape); |
416 |
|
if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) |
417 |
{ |
{ |
418 |
|
DPRINTF(DPRINTF_ERROR,"non-rectangular shapes are not supported"); |
419 |
|
} |
420 |
|
|
421 |
|
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) { |
422 |
BitstreamSkip(bs, 4); // video_object_layer_shape_extension |
BitstreamSkip(bs, 4); // video_object_layer_shape_extension |
423 |
} |
} |
424 |
|
|
425 |
READ_MARKER(); |
READ_MARKER(); |
426 |
|
|
427 |
time_inc_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution |
// *************************** for decode B-frame time *********************** |
428 |
time_inc_resolution--; |
dec->time_inc_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution |
429 |
if (time_inc_resolution > 0) |
DPRINTF(DPRINTF_HEADER,"vop_time_increment_resolution %i", dec->time_inc_resolution); |
430 |
{ |
|
431 |
dec->time_inc_bits = log2bin(time_inc_resolution); |
// dec->time_inc_resolution--; |
432 |
} |
|
433 |
else |
if (dec->time_inc_resolution > 0) { |
434 |
{ |
dec->time_inc_bits = log2bin(dec->time_inc_resolution-1); |
435 |
|
} else { |
436 |
// dec->time_inc_bits = 0; |
// dec->time_inc_bits = 0; |
|
|
|
437 |
// for "old" xvid compatibility, set time_inc_bits = 1 |
// for "old" xvid compatibility, set time_inc_bits = 1 |
438 |
dec->time_inc_bits = 1; |
dec->time_inc_bits = 1; |
439 |
} |
} |
442 |
|
|
443 |
if (BitstreamGetBit(bs)) // fixed_vop_rate |
if (BitstreamGetBit(bs)) // fixed_vop_rate |
444 |
{ |
{ |
445 |
|
DPRINTF(DPRINTF_HEADER, "+ fixed_vop_rate"); |
446 |
BitstreamSkip(bs, dec->time_inc_bits); // fixed_vop_time_increment |
BitstreamSkip(bs, dec->time_inc_bits); // fixed_vop_time_increment |
447 |
} |
} |
448 |
|
|
449 |
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) |
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) { |
|
{ |
|
450 |
|
|
451 |
if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) |
if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) { |
|
{ |
|
452 |
uint32_t width, height; |
uint32_t width, height; |
453 |
|
|
454 |
READ_MARKER(); |
READ_MARKER(); |
455 |
width = BitstreamGetBits(bs, 13); // video_object_layer_width |
width = BitstreamGetBits(bs, 13); // video_object_layer_width |
|
//DEBUGI("width", width); |
|
456 |
READ_MARKER(); |
READ_MARKER(); |
457 |
height = BitstreamGetBits(bs, 13); // video_object_layer_height |
height = BitstreamGetBits(bs, 13); // video_object_layer_height |
|
//DEBUGI("height", height); |
|
458 |
READ_MARKER(); |
READ_MARKER(); |
459 |
|
|
460 |
if (width != dec->width || height != dec->height) |
DPRINTF(DPRINTF_HEADER, "width %i", width); |
461 |
|
DPRINTF(DPRINTF_HEADER, "height %i", height); |
462 |
|
|
463 |
|
if (dec->width != width || dec->height != height) |
464 |
|
{ |
465 |
|
if (dec->fixed_dimensions) |
466 |
{ |
{ |
467 |
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); |
|
468 |
return -1; |
return -1; |
469 |
} |
} |
470 |
|
resize = 1; |
471 |
|
dec->width = width; |
472 |
|
dec->height = height; |
473 |
} |
} |
|
|
|
|
if ((dec->interlacing = BitstreamGetBit(bs))) |
|
|
{ |
|
|
DEBUG("vol: interlacing"); |
|
474 |
} |
} |
475 |
|
|
476 |
|
dec->interlacing = BitstreamGetBit(bs); |
477 |
|
DPRINTF(DPRINTF_HEADER, "interlacing %i", dec->interlacing); |
478 |
|
|
479 |
if (!BitstreamGetBit(bs)) // obmc_disable |
if (!BitstreamGetBit(bs)) // obmc_disable |
480 |
{ |
{ |
481 |
DEBUG("IGNORED/TODO: !obmc_disable"); |
DPRINTF(DPRINTF_ERROR, "obmc_disabled==false not supported"); |
482 |
// TODO |
// TODO |
483 |
// fucking divx4.02 has this enabled |
// fucking divx4.02 has this enabled |
484 |
} |
} |
485 |
|
|
486 |
if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2))) // sprite_enable |
dec->sprite_enable = BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2)); // sprite_enable |
487 |
|
|
488 |
|
if (dec->sprite_enable == SPRITE_STATIC || dec->sprite_enable == SPRITE_GMC) |
489 |
{ |
{ |
490 |
DEBUG("sprite_enable; not supported"); |
int low_latency_sprite_enable; |
|
return -1; |
|
|
} |
|
491 |
|
|
492 |
if (vol_ver_id != 1 && dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) |
if (dec->sprite_enable != SPRITE_GMC) |
493 |
|
{ |
494 |
|
int sprite_width; |
495 |
|
int sprite_height; |
496 |
|
int sprite_left_coord; |
497 |
|
int sprite_top_coord; |
498 |
|
sprite_width = BitstreamGetBits(bs, 13); // sprite_width |
499 |
|
READ_MARKER(); |
500 |
|
sprite_height = BitstreamGetBits(bs, 13); // sprite_height |
501 |
|
READ_MARKER(); |
502 |
|
sprite_left_coord = BitstreamGetBits(bs, 13); // sprite_left_coordinate |
503 |
|
READ_MARKER(); |
504 |
|
sprite_top_coord = BitstreamGetBits(bs, 13); // sprite_top_coordinate |
505 |
|
READ_MARKER(); |
506 |
|
} |
507 |
|
dec->sprite_warping_points = BitstreamGetBits(bs, 6); // no_of_sprite_warping_points |
508 |
|
dec->sprite_warping_accuracy = BitstreamGetBits(bs, 2); // sprite_warping_accuracy |
509 |
|
dec->sprite_brightness_change = BitstreamGetBits(bs, 1); // brightness_change |
510 |
|
if (dec->sprite_enable != SPRITE_GMC) |
511 |
{ |
{ |
512 |
|
low_latency_sprite_enable = BitstreamGetBits(bs, 1); // low_latency_sprite_enable |
513 |
|
} |
514 |
|
} |
515 |
|
|
516 |
|
if (vol_ver_id != 1 && |
517 |
|
dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { |
518 |
BitstreamSkip(bs, 1); // sadct_disable |
BitstreamSkip(bs, 1); // sadct_disable |
519 |
} |
} |
520 |
|
|
521 |
if (BitstreamGetBit(bs)) // not_8_bit |
if (BitstreamGetBit(bs)) // not_8_bit |
522 |
{ |
{ |
523 |
DEBUG("+ not_8_bit [IGNORED/TODO]"); |
DPRINTF(DPRINTF_HEADER, "not_8_bit==true (ignored)"); |
524 |
dec->quant_bits = BitstreamGetBits(bs, 4); // quant_precision |
dec->quant_bits = BitstreamGetBits(bs, 4); // quant_precision |
525 |
BitstreamSkip(bs, 4); // bits_per_pixel |
BitstreamSkip(bs, 4); // bits_per_pixel |
526 |
} |
} else { |
|
else |
|
|
{ |
|
527 |
dec->quant_bits = 5; |
dec->quant_bits = 5; |
528 |
} |
} |
529 |
|
|
530 |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) { |
|
{ |
|
531 |
BitstreamSkip(bs, 1); // no_gray_quant_update |
BitstreamSkip(bs, 1); // no_gray_quant_update |
532 |
BitstreamSkip(bs, 1); // composition_method |
BitstreamSkip(bs, 1); // composition_method |
533 |
BitstreamSkip(bs, 1); // linear_composition |
BitstreamSkip(bs, 1); // linear_composition |
534 |
} |
} |
535 |
|
|
536 |
dec->quant_type = BitstreamGetBit(bs); // quant_type |
dec->quant_type = BitstreamGetBit(bs); // quant_type |
537 |
// DEBUG1("**** quant_type", dec->quant_type); |
DPRINTF(DPRINTF_HEADER, "quant_type %i", dec->quant_type); |
538 |
|
|
539 |
if (dec->quant_type) |
if (dec->quant_type) { |
|
{ |
|
540 |
if (BitstreamGetBit(bs)) // load_intra_quant_mat |
if (BitstreamGetBit(bs)) // load_intra_quant_mat |
541 |
{ |
{ |
542 |
uint8_t matrix[64]; |
uint8_t matrix[64]; |
543 |
|
|
544 |
|
DPRINTF(DPRINTF_HEADER, "load_intra_quant_mat"); |
545 |
|
|
546 |
bs_get_matrix(bs, matrix); |
bs_get_matrix(bs, matrix); |
547 |
set_intra_matrix(matrix); |
set_intra_matrix(matrix); |
548 |
} |
} else |
|
else |
|
549 |
set_intra_matrix(get_default_intra_matrix()); |
set_intra_matrix(get_default_intra_matrix()); |
550 |
|
|
551 |
if (BitstreamGetBit(bs)) // load_inter_quant_mat |
if (BitstreamGetBit(bs)) // load_inter_quant_mat |
552 |
{ |
{ |
553 |
uint8_t matrix[64]; |
uint8_t matrix[64]; |
554 |
|
|
555 |
|
DPRINTF(DPRINTF_HEADER, "load_inter_quant_mat"); |
556 |
|
|
557 |
bs_get_matrix(bs, matrix); |
bs_get_matrix(bs, matrix); |
558 |
set_inter_matrix(matrix); |
set_inter_matrix(matrix); |
559 |
} |
} else |
|
else |
|
560 |
set_inter_matrix(get_default_inter_matrix()); |
set_inter_matrix(get_default_inter_matrix()); |
561 |
|
|
562 |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) { |
563 |
{ |
DPRINTF(DPRINTF_ERROR, "greyscale matrix not supported"); |
|
// TODO |
|
|
DEBUG("TODO: grayscale matrix stuff"); |
|
564 |
return -1; |
return -1; |
565 |
} |
} |
566 |
|
|
567 |
} |
} |
568 |
|
|
569 |
|
|
570 |
if (vol_ver_id != 1) |
if (vol_ver_id != 1) { |
571 |
{ |
dec->quarterpel = BitstreamGetBit(bs); // quarter_sample |
572 |
dec->quarterpel = BitstreamGetBit(bs); // quarter_sampe |
DPRINTF(DPRINTF_HEADER,"quarterpel %i", dec->quarterpel); |
|
if (dec->quarterpel) |
|
|
{ |
|
|
DEBUG("IGNORED/TODO: quarter_sample"); |
|
|
} |
|
573 |
} |
} |
574 |
else |
else |
|
{ |
|
575 |
dec->quarterpel = 0; |
dec->quarterpel = 0; |
576 |
} |
|
577 |
|
|
578 |
if (!BitstreamGetBit(bs)) // complexity_estimation_disable |
if (!BitstreamGetBit(bs)) // complexity_estimation_disable |
579 |
{ |
{ |
580 |
DEBUG("TODO: complexity_estimation header"); |
DPRINTF(DPRINTF_ERROR, "complexity_estimation not supported"); |
|
// TODO |
|
581 |
return -1; |
return -1; |
582 |
} |
} |
583 |
|
|
584 |
if (!BitstreamGetBit(bs)) // resync_marker_disable |
BitstreamSkip(bs, 1); // resync_marker_disable |
|
{ |
|
|
DEBUG("IGNORED/TODO: !resync_marker_disable"); |
|
|
// TODO |
|
|
} |
|
585 |
|
|
586 |
if (BitstreamGetBit(bs)) // data_partitioned |
if (BitstreamGetBit(bs)) // data_partitioned |
587 |
{ |
{ |
588 |
DEBUG("+ data_partitioned"); |
DPRINTF(DPRINTF_ERROR, "data_partitioned not supported"); |
589 |
BitstreamSkip(bs, 1); // reversible_vlc |
BitstreamSkip(bs, 1); // reversible_vlc |
590 |
} |
} |
591 |
|
|
592 |
if (vol_ver_id != 1) |
if (vol_ver_id != 1) { |
593 |
{ |
dec->newpred_enable = BitstreamGetBit(bs); |
594 |
if (BitstreamGetBit(bs)) // newpred_enable |
if (dec->newpred_enable) // newpred_enable |
595 |
{ |
{ |
596 |
DEBUG("+ newpred_enable"); |
DPRINTF(DPRINTF_HEADER, "+ newpred_enable"); |
597 |
BitstreamSkip(bs, 2); // requested_upstream_message_type |
BitstreamSkip(bs, 2); // requested_upstream_message_type |
598 |
BitstreamSkip(bs, 1); // newpred_segment_type |
BitstreamSkip(bs, 1); // newpred_segment_type |
599 |
} |
} |
600 |
if (BitstreamGetBit(bs)) // reduced_resolution_vop_enable |
dec->reduced_resolution_enable = BitstreamGetBit(bs); |
601 |
|
if (dec->reduced_resolution_enable) // reduced_resolution_vop_enable |
602 |
{ |
{ |
603 |
DEBUG("TODO: reduced_resolution_vop_enable"); |
DPRINTF(DPRINTF_ERROR, "reduced_resolution_vop not supported"); |
604 |
// TODO |
//return -1; |
605 |
return -1; |
} |
606 |
} |
} |
607 |
|
else |
608 |
|
{ |
609 |
|
dec->newpred_enable = 0; |
610 |
|
dec->reduced_resolution_enable = 0; |
611 |
} |
} |
612 |
|
|
613 |
if (BitstreamGetBit(bs)) // scalability |
if ((dec->scalability = BitstreamGetBit(bs))) // scalability |
614 |
{ |
{ |
615 |
// TODO |
DPRINTF(DPRINTF_ERROR, "scalability not supported"); |
|
DEBUG("TODO: scalability"); |
|
616 |
return -1; |
return -1; |
617 |
} |
} |
618 |
} |
} else // dec->shape == BINARY_ONLY |
|
else // dec->shape == BINARY_ONLY |
|
|
{ |
|
|
if (vol_ver_id != 1) |
|
619 |
{ |
{ |
620 |
|
if (vol_ver_id != 1) { |
621 |
if (BitstreamGetBit(bs)) // scalability |
if (BitstreamGetBit(bs)) // scalability |
622 |
{ |
{ |
623 |
// TODO |
DPRINTF(DPRINTF_ERROR, "scalability not supported"); |
|
DEBUG("TODO: scalability"); |
|
624 |
return -1; |
return -1; |
625 |
} |
} |
626 |
} |
} |
628 |
|
|
629 |
} |
} |
630 |
|
|
631 |
} |
return (resize ? -3 : -2 ); /* VOL */ |
632 |
else if (start_code == GRPOFVOP_START_CODE) |
|
633 |
{ |
} else if (start_code == GRPOFVOP_START_CODE) { |
634 |
// DEBUG("group_of_vop"); |
|
635 |
|
DPRINTF(DPRINTF_STARTCODE, "<group_of_vop>"); |
636 |
|
|
637 |
BitstreamSkip(bs, 32); |
BitstreamSkip(bs, 32); |
638 |
{ |
{ |
639 |
int hours, minutes, seconds; |
int hours, minutes, seconds; |
640 |
|
|
641 |
hours = BitstreamGetBits(bs, 5); |
hours = BitstreamGetBits(bs, 5); |
642 |
minutes = BitstreamGetBits(bs, 6); |
minutes = BitstreamGetBits(bs, 6); |
643 |
READ_MARKER(); |
READ_MARKER(); |
644 |
seconds = BitstreamGetBits(bs, 6); |
seconds = BitstreamGetBits(bs, 6); |
645 |
// DEBUG3("hms", hours, minutes, seconds); |
|
646 |
|
DPRINTF(DPRINTF_HEADER, "time %ih%im%is", hours); |
647 |
} |
} |
648 |
BitstreamSkip(bs, 1); // closed_gov |
BitstreamSkip(bs, 1); // closed_gov |
649 |
BitstreamSkip(bs, 1); // broken_link |
BitstreamSkip(bs, 1); // broken_link |
650 |
} |
|
651 |
else if (start_code == VOP_START_CODE) |
} else if (start_code == VOP_START_CODE) { |
652 |
{ |
|
653 |
// DEBUG("vop_start_code"); |
DPRINTF(DPRINTF_STARTCODE, "<vop>"); |
654 |
|
|
655 |
BitstreamSkip(bs, 32); // vop_start_code |
BitstreamSkip(bs, 32); // vop_start_code |
656 |
|
|
657 |
coding_type = BitstreamGetBits(bs, 2); // vop_coding_type |
coding_type = BitstreamGetBits(bs, 2); // vop_coding_type |
658 |
//DEBUG1("coding_type", coding_type); |
DPRINTF(DPRINTF_HEADER, "coding_type %i", coding_type); |
659 |
|
|
660 |
while (BitstreamGetBit(bs) != 0) ; // time_base |
// *************************** for decode B-frame time *********************** |
661 |
|
while (BitstreamGetBit(bs) != 0) // time_base |
662 |
|
time_incr++; |
663 |
|
|
664 |
READ_MARKER(); |
READ_MARKER(); |
665 |
|
|
666 |
//DEBUG1("time_inc_bits", dec->time_inc_bits); |
if (dec->time_inc_bits) { |
667 |
//DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits)); |
time_increment = (BitstreamGetBits(bs, dec->time_inc_bits)); // vop_time_increment |
668 |
if (dec->time_inc_bits) |
} |
669 |
{ |
|
670 |
BitstreamSkip(bs, dec->time_inc_bits); // vop_time_increment |
DPRINTF(DPRINTF_HEADER, "time_base %i", time_incr); |
671 |
|
DPRINTF(DPRINTF_HEADER, "time_increment %i", time_increment); |
672 |
|
|
673 |
|
DPRINTF(DPRINTF_TIMECODE, "%c %i:%i", |
674 |
|
coding_type == I_VOP ? 'I' : coding_type == P_VOP ? 'P' : coding_type == B_VOP ? 'B' : 'S', |
675 |
|
time_incr, time_increment); |
676 |
|
|
677 |
|
if (coding_type != B_VOP) { |
678 |
|
dec->last_time_base = dec->time_base; |
679 |
|
dec->time_base += time_incr; |
680 |
|
dec->time = time_increment; |
681 |
|
|
682 |
|
/* dec->time_base * dec->time_inc_resolution + |
683 |
|
time_increment; |
684 |
|
*/ dec->time_pp = (uint32_t) |
685 |
|
(dec->time_inc_resolution + dec->time - dec->last_non_b_time)%dec->time_inc_resolution; |
686 |
|
dec->last_non_b_time = dec->time; |
687 |
|
} else { |
688 |
|
dec->time = time_increment; |
689 |
|
/* |
690 |
|
(dec->last_time_base + |
691 |
|
time_incr) * dec->time_inc_resolution + time_increment; |
692 |
|
*/ |
693 |
|
dec->time_bp = (uint32_t) |
694 |
|
(dec->time_inc_resolution + dec->last_non_b_time - dec->time)%dec->time_inc_resolution; |
695 |
} |
} |
696 |
|
|
697 |
READ_MARKER(); |
READ_MARKER(); |
698 |
|
|
699 |
if (!BitstreamGetBit(bs)) // vop_coded |
if (!BitstreamGetBit(bs)) // vop_coded |
700 |
{ |
{ |
701 |
|
DPRINTF(DPRINTF_HEADER, "vop_coded==false"); |
702 |
return N_VOP; |
return N_VOP; |
703 |
} |
} |
704 |
|
|
705 |
/* if (newpred_enable) |
if (dec->newpred_enable) |
706 |
{ |
{ |
707 |
} |
int vop_id; |
708 |
*/ |
int vop_id_for_prediction; |
709 |
|
|
710 |
if (coding_type != I_VOP) |
vop_id = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15)); |
711 |
|
DPRINTF(DPRINTF_HEADER, "vop_id %i", vop_id); |
712 |
|
if (BitstreamGetBit(bs)) /* vop_id_for_prediction_indication */ |
713 |
{ |
{ |
714 |
|
vop_id_for_prediction = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15)); |
715 |
|
DPRINTF(DPRINTF_HEADER, "vop_id_for_prediction %i", vop_id_for_prediction); |
716 |
|
} |
717 |
|
READ_MARKER(); |
718 |
|
} |
719 |
|
|
720 |
|
|
721 |
|
|
722 |
|
// fix a little bug by MinChen <chenm002@163.com> |
723 |
|
if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) && |
724 |
|
( (coding_type == P_VOP) || (coding_type == S_VOP && dec->sprite_enable == SPRITE_GMC) ) ) { |
725 |
*rounding = BitstreamGetBit(bs); // rounding_type |
*rounding = BitstreamGetBit(bs); // rounding_type |
726 |
//DEBUG1("rounding", *rounding); |
DPRINTF(DPRINTF_HEADER, "rounding %i", *rounding); |
727 |
} |
} |
728 |
|
|
729 |
/* if (reduced_resolution_enable) |
if (dec->reduced_resolution_enable && |
730 |
|
dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR && |
731 |
|
(coding_type == P_VOP || coding_type == I_VOP)) { |
732 |
|
|
733 |
|
*reduced_resolution = BitstreamGetBit(bs); |
734 |
|
} |
735 |
|
else |
736 |
{ |
{ |
737 |
|
*reduced_resolution = 0; |
738 |
} |
} |
|
*/ |
|
739 |
|
|
740 |
if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) |
if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { |
741 |
{ |
if(!(dec->sprite_enable == SPRITE_STATIC && coding_type == I_VOP)) { |
742 |
|
|
743 |
uint32_t width, height; |
uint32_t width, height; |
744 |
uint32_t horiz_mc_ref, vert_mc_ref; |
uint32_t horiz_mc_ref, vert_mc_ref; |
745 |
|
|
752 |
vert_mc_ref = BitstreamGetBits(bs, 13); |
vert_mc_ref = BitstreamGetBits(bs, 13); |
753 |
READ_MARKER(); |
READ_MARKER(); |
754 |
|
|
755 |
// DEBUG2("vop_width/height", width, height); |
DPRINTF(DPRINTF_HEADER, "width %i", width); |
756 |
// DEBUG2("ref ", horiz_mc_ref, vert_mc_ref); |
DPRINTF(DPRINTF_HEADER, "height %i", height); |
757 |
|
DPRINTF(DPRINTF_HEADER, "horiz_mc_ref %i", horiz_mc_ref); |
758 |
|
DPRINTF(DPRINTF_HEADER, "vert_mc_ref %i", vert_mc_ref); |
759 |
|
} |
760 |
|
|
761 |
BitstreamSkip(bs, 1); // change_conv_ratio_disable |
BitstreamSkip(bs, 1); // change_conv_ratio_disable |
762 |
if (BitstreamGetBit(bs)) // vop_constant_alpha |
if (BitstreamGetBit(bs)) // vop_constant_alpha |
765 |
} |
} |
766 |
} |
} |
767 |
|
|
768 |
|
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) { |
|
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) |
|
|
{ |
|
769 |
// intra_dc_vlc_threshold |
// intra_dc_vlc_threshold |
770 |
*intra_dc_threshold = intra_dc_threshold_table[ BitstreamGetBits(bs,3) ]; |
*intra_dc_threshold = |
771 |
|
intra_dc_threshold_table[BitstreamGetBits(bs, 3)]; |
772 |
|
|
773 |
if (dec->interlacing) |
dec->top_field_first = 0; |
774 |
{ |
dec->alternate_vertical_scan = 0; |
775 |
if ((dec->top_field_first = BitstreamGetBit(bs))) |
|
776 |
|
if (dec->interlacing) { |
777 |
|
dec->top_field_first = BitstreamGetBit(bs); |
778 |
|
DPRINTF(DPRINTF_HEADER, "interlace top_field_first %i", dec->top_field_first); |
779 |
|
dec->alternate_vertical_scan = BitstreamGetBit(bs); |
780 |
|
DPRINTF(DPRINTF_HEADER, "interlace alternate_vertical_scan %i", dec->alternate_vertical_scan); |
781 |
|
|
782 |
|
} |
783 |
|
} |
784 |
|
|
785 |
|
if ((dec->sprite_enable == SPRITE_STATIC || dec->sprite_enable== SPRITE_GMC) && coding_type == S_VOP) { |
786 |
|
|
787 |
|
int i; |
788 |
|
|
789 |
|
for (i = 0 ; i < dec->sprite_warping_points; i++) |
790 |
{ |
{ |
791 |
DEBUG("vop: top_field_first"); |
int length; |
792 |
|
int x = 0, y = 0; |
793 |
|
|
794 |
|
/* sprite code borowed from ffmpeg; thx Michael Niedermayer <michaelni@gmx.at> */ |
795 |
|
length = bs_get_spritetrajectory(bs); |
796 |
|
if(length){ |
797 |
|
x= BitstreamGetBits(bs, length); |
798 |
|
if ((x >> (length - 1)) == 0) /* if MSB not set it is negative*/ |
799 |
|
x = - (x ^ ((1 << length) - 1)); |
800 |
|
} |
801 |
|
READ_MARKER(); |
802 |
|
|
803 |
|
length = bs_get_spritetrajectory(bs); |
804 |
|
if(length){ |
805 |
|
y = BitstreamGetBits(bs, length); |
806 |
|
if ((y >> (length - 1)) == 0) /* if MSB not set it is negative*/ |
807 |
|
y = - (y ^ ((1 << length) - 1)); |
808 |
} |
} |
809 |
if ((dec->alternate_vertical_scan = BitstreamGetBit(bs))) |
READ_MARKER(); |
810 |
|
|
811 |
|
gmc_mv[i].x = x; |
812 |
|
gmc_mv[i].y = y; |
813 |
|
|
814 |
|
DPRINTF(DPRINTF_HEADER,"sprite_warping_point[%i] xy=(%i,%i)", i, x, y); |
815 |
|
} |
816 |
|
|
817 |
|
if (dec->sprite_brightness_change) |
818 |
{ |
{ |
819 |
DEBUG("vop: alternate_vertical_scan"); |
// XXX: brightness_change_factor() |
820 |
} |
} |
821 |
|
if (dec->sprite_enable == SPRITE_STATIC) |
822 |
|
{ |
823 |
|
// XXX: todo |
824 |
} |
} |
825 |
|
|
826 |
} |
} |
827 |
|
|
828 |
*quant = BitstreamGetBits(bs, dec->quant_bits); // vop_quant |
if ((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1) // vop_quant |
829 |
//DEBUG1("quant", *quant); |
*quant = 1; |
830 |
|
DPRINTF(DPRINTF_HEADER, "quant %i", *quant); |
831 |
|
|
832 |
if (coding_type != I_VOP) |
if (coding_type != I_VOP) { |
833 |
{ |
*fcode_forward = BitstreamGetBits(bs, 3); // fcode_forward |
834 |
*fcode = BitstreamGetBits(bs, 3); // fcode_forward |
DPRINTF(DPRINTF_HEADER, "fcode_forward %i", *fcode_forward); |
835 |
} |
} |
836 |
|
|
837 |
if (coding_type == B_VOP) |
if (coding_type == B_VOP) { |
838 |
{ |
*fcode_backward = BitstreamGetBits(bs, 3); // fcode_backward |
839 |
// *fcode_backward = BitstreamGetBits(bs, 3); // fcode_backward |
DPRINTF(DPRINTF_HEADER, "fcode_backward %i", *fcode_backward); |
840 |
} |
} |
841 |
return coding_type; |
if (!dec->scalability) { |
842 |
|
if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) && |
843 |
|
(coding_type != I_VOP)) { |
844 |
|
BitstreamSkip(bs, 1); // vop_shape_coding_type |
845 |
} |
} |
|
else if (start_code == USERDATA_START_CODE) |
|
|
{ |
|
|
// DEBUG("user_data"); |
|
|
BitstreamSkip(bs, 32); // user_data_start_code |
|
846 |
} |
} |
847 |
else // start_code == ? |
return coding_type; |
848 |
{ |
|
849 |
if (BitstreamShowBits(bs, 24) == 0x000001) |
} else if (start_code == USERDATA_START_CODE) { |
850 |
|
|
851 |
|
DPRINTF(DPRINTF_STARTCODE, "<user_data>"); |
852 |
|
|
853 |
|
BitstreamSkip(bs, 32); // user_data_start_code |
854 |
|
|
855 |
|
} else // start_code == ? |
856 |
{ |
{ |
857 |
DEBUG1("*** WARNING: unknown start_code", BitstreamShowBits(bs, 32)); |
if (BitstreamShowBits(bs, 24) == 0x000001) { |
858 |
|
DPRINTF(DPRINTF_STARTCODE, "<unknown: %x>", BitstreamShowBits(bs, 32)); |
859 |
} |
} |
860 |
BitstreamSkip(bs, 8); |
BitstreamSkip(bs, 8); |
861 |
} |
} |
862 |
} |
} |
863 |
while ((BitstreamPos(bs) >> 3) < bs->length); |
while ((BitstreamPos(bs) >> 3) < bs->length); |
864 |
|
|
865 |
DEBUG("*** WARNING: no vop_start_code found"); |
//DPRINTF("*** WARNING: no vop_start_code found"); |
866 |
return -1; /* ignore it */ |
return -1; /* ignore it */ |
867 |
} |
} |
868 |
|
|
869 |
|
|
870 |
/* write custom quant matrix */ |
/* write custom quant matrix */ |
871 |
|
|
872 |
static void bs_put_matrix(Bitstream * bs, const int16_t *matrix) |
static void |
873 |
|
bs_put_matrix(Bitstream * bs, |
874 |
|
const int16_t * matrix) |
875 |
{ |
{ |
876 |
int i, j; |
int i, j; |
877 |
const int last = matrix[scan_tables[0][63]]; |
const int last = matrix[scan_tables[0][63]]; |
878 |
|
|
879 |
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--); |
880 |
|
|
881 |
for (i = 0; i <= j; i++) |
for (i = 0; i <= j; i++) { |
|
{ |
|
882 |
BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8); |
BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8); |
883 |
} |
} |
884 |
|
|
885 |
if (j < 63) |
if (j < 63) { |
|
{ |
|
886 |
BitstreamPutBits(bs, 0, 8); |
BitstreamPutBits(bs, 0, 8); |
887 |
} |
} |
888 |
} |
} |
891 |
/* |
/* |
892 |
write vol header |
write vol header |
893 |
*/ |
*/ |
894 |
void BitstreamWriteVolHeader(Bitstream * const bs, |
void |
895 |
const MBParam * pParam) |
BitstreamWriteVolHeader(Bitstream * const bs, |
896 |
|
const MBParam * pParam, |
897 |
|
const FRAMEINFO * const frame) |
898 |
{ |
{ |
899 |
|
int vol_ver_id=1; |
900 |
|
|
901 |
|
if ( (pParam->m_quarterpel) || (frame->global_flags & XVID_GMC) ) |
902 |
|
vol_ver_id = 2; |
903 |
|
|
904 |
// video object_start_code & vo_id |
// video object_start_code & vo_id |
905 |
BitstreamPad(bs); |
BitstreamPad(bs); |
906 |
BitstreamPutBits(bs, VO_START_CODE, 27); |
BitstreamPutBits(bs, VO_START_CODE, 27); |
912 |
|
|
913 |
BitstreamPutBit(bs, 0); // random_accessible_vol |
BitstreamPutBit(bs, 0); // random_accessible_vol |
914 |
BitstreamPutBits(bs, 0, 8); // video_object_type_indication |
BitstreamPutBits(bs, 0, 8); // video_object_type_indication |
915 |
|
|
916 |
|
if (vol_ver_id == 1) |
917 |
|
{ |
918 |
BitstreamPutBit(bs, 0); // is_object_layer_identified (0=not given) |
BitstreamPutBit(bs, 0); // is_object_layer_identified (0=not given) |
919 |
|
} |
920 |
|
else |
921 |
|
{ |
922 |
|
BitstreamPutBit(bs, 1); // is_object_layer_identified |
923 |
|
BitstreamPutBits(bs, vol_ver_id, 4); // vol_ver_id == 2 |
924 |
|
BitstreamPutBits(bs, 4, 3); // vol_ver_priority (1==lowest, 7==highest) ?? |
925 |
|
} |
926 |
|
|
927 |
BitstreamPutBits(bs, 1, 4); // aspect_ratio_info (1=1:1) |
BitstreamPutBits(bs, 1, 4); // aspect_ratio_info (1=1:1) |
928 |
BitstreamPutBit(bs, 0); // vol_control_parameters (0=not given) |
|
929 |
|
BitstreamPutBit(bs, 1); // vol_control_parameters |
930 |
|
BitstreamPutBits(bs, 1, 2); // chroma_format 1="4:2:0" |
931 |
|
|
932 |
|
if (pParam->max_bframes > 0) { |
933 |
|
BitstreamPutBit(bs, 0); // low_delay |
934 |
|
} else |
935 |
|
{ |
936 |
|
BitstreamPutBit(bs, 1); // low_delay |
937 |
|
} |
938 |
|
BitstreamPutBit(bs, 0); // vbv_parameters (0=not given) |
939 |
|
|
940 |
BitstreamPutBits(bs, 0, 2); // video_object_layer_shape (0=rectangular) |
BitstreamPutBits(bs, 0, 2); // video_object_layer_shape (0=rectangular) |
941 |
|
|
942 |
WRITE_MARKER(); |
WRITE_MARKER(); |
943 |
|
|
944 |
/* time_increment_resolution; ignored by current decore versions |
/* time_inc_resolution; ignored by current decore versions |
945 |
eg. 2fps res=2 inc=1 |
eg. 2fps res=2 inc=1 |
946 |
25fps res=25 inc=1 |
25fps res=25 inc=1 |
947 |
29.97fps res=30000 inc=1001 |
29.97fps res=30000 inc=1001 |
948 |
*/ |
*/ |
949 |
BitstreamPutBits(bs, 2, 16); |
BitstreamPutBits(bs, pParam->fbase, 16); |
950 |
|
|
951 |
WRITE_MARKER(); |
WRITE_MARKER(); |
952 |
|
|
953 |
// fixed_vop_rate |
BitstreamPutBit(bs, 1); // fixed_vop_rate = 1 |
954 |
BitstreamPutBit(bs, 0); |
BitstreamPutBits(bs, pParam->fincr, log2bin(pParam->fbase)); // fixed_vop_time_increment |
|
|
|
|
// fixed_time_increment: value=nth_of_sec, nbits = log2(resolution) |
|
|
// BitstreamPutBits(bs, 0, 15); |
|
955 |
|
|
956 |
WRITE_MARKER(); |
WRITE_MARKER(); |
957 |
BitstreamPutBits(bs, pParam->width, 13); // width |
BitstreamPutBits(bs, pParam->width, 13); // width |
959 |
BitstreamPutBits(bs, pParam->height, 13); // height |
BitstreamPutBits(bs, pParam->height, 13); // height |
960 |
WRITE_MARKER(); |
WRITE_MARKER(); |
961 |
|
|
962 |
BitstreamPutBit(bs, pParam->global_flags & XVID_INTERLACING); // interlace |
BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING); // interlace |
963 |
BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) |
BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) |
964 |
BitstreamPutBit(bs, 0); // sprite_enable |
|
965 |
BitstreamPutBit(bs, 0); // not_in_bit |
if (vol_ver_id != 1) |
966 |
|
{ if (frame->global_flags & XVID_GMC) |
967 |
|
{ BitstreamPutBits(bs, 2, 2); // sprite_enable=='GMC' |
968 |
|
BitstreamPutBits(bs, 2, 6); // no_of_sprite_warping_points |
969 |
|
BitstreamPutBits(bs, 3, 2); // sprite_warping_accuracy 0==1/2, 1=1/4, 2=1/8, 3=1/16 |
970 |
|
BitstreamPutBit(bs, 0); // sprite_brightness_change (not supported) |
971 |
|
|
972 |
|
/* currently we use no_of_sprite_warping_points==2, sprite_warping_accuracy==3 |
973 |
|
for DivX5 compatability */ |
974 |
|
|
975 |
|
} else |
976 |
|
BitstreamPutBits(bs, 0, 2); // sprite_enable==off |
977 |
|
} |
978 |
|
else |
979 |
|
BitstreamPutBit(bs, 0); // sprite_enable==off |
980 |
|
|
981 |
|
BitstreamPutBit(bs, 0); // not_8_bit |
982 |
|
|
983 |
// quant_type 0=h.263 1=mpeg4(quantizer tables) |
// quant_type 0=h.263 1=mpeg4(quantizer tables) |
984 |
BitstreamPutBit(bs, pParam->quant_type); |
BitstreamPutBit(bs, pParam->m_quant_type); |
985 |
|
|
986 |
if (pParam->quant_type) |
if (pParam->m_quant_type) { |
|
{ |
|
987 |
BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat |
BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat |
988 |
if (get_intra_matrix_status()) |
if (get_intra_matrix_status()) { |
|
{ |
|
989 |
bs_put_matrix(bs, get_intra_matrix()); |
bs_put_matrix(bs, get_intra_matrix()); |
990 |
} |
} |
991 |
|
|
992 |
BitstreamPutBit(bs, get_inter_matrix_status()); // load_inter_quant_mat |
BitstreamPutBit(bs, get_inter_matrix_status()); // load_inter_quant_mat |
993 |
if (get_inter_matrix_status()) |
if (get_inter_matrix_status()) { |
|
{ |
|
994 |
bs_put_matrix(bs, get_inter_matrix()); |
bs_put_matrix(bs, get_inter_matrix()); |
995 |
} |
} |
996 |
|
|
997 |
} |
} |
998 |
|
|
999 |
|
if (vol_ver_id != 1) { |
1000 |
|
if (pParam->m_quarterpel) |
1001 |
|
BitstreamPutBit(bs, 1); // quarterpel |
1002 |
|
else |
1003 |
|
BitstreamPutBit(bs, 0); // no quarterpel |
1004 |
|
} |
1005 |
|
|
1006 |
BitstreamPutBit(bs, 1); // complexity_estimation_disable |
BitstreamPutBit(bs, 1); // complexity_estimation_disable |
1007 |
BitstreamPutBit(bs, 1); // resync_marker_disable |
BitstreamPutBit(bs, 1); // resync_marker_disable |
1008 |
BitstreamPutBit(bs, 0); // data_partitioned |
BitstreamPutBit(bs, 0); // data_partitioned |
1009 |
|
|
1010 |
|
if (vol_ver_id != 1) |
1011 |
|
{ |
1012 |
|
BitstreamPutBit(bs, 0); // newpred_enable |
1013 |
|
BitstreamPutBit(bs, 0); // reduced_resolution_vop_enabled |
1014 |
|
} |
1015 |
|
|
1016 |
BitstreamPutBit(bs, 0); // scalability |
BitstreamPutBit(bs, 0); // scalability |
1017 |
|
|
1018 |
} |
} |
1019 |
|
|
1020 |
|
|
1021 |
/* |
/* |
1022 |
write vop header |
write vop header |
|
|
|
|
NOTE: doesnt handle bother with time_base & time_inc |
|
|
time_base = n seconds since last resync (eg. last iframe) |
|
|
time_inc = nth of a second since last resync |
|
|
(decoder uses these values to determine precise time since last resync) |
|
1023 |
*/ |
*/ |
1024 |
void BitstreamWriteVopHeader(Bitstream * const bs, |
void |
1025 |
const MBParam * pParam) |
BitstreamWriteVopHeader(Bitstream * const bs, |
1026 |
|
const MBParam * pParam, |
1027 |
|
const FRAMEINFO * const frame, |
1028 |
|
int vop_coded) |
1029 |
{ |
{ |
1030 |
|
uint32_t i; |
1031 |
|
|
1032 |
BitstreamPad(bs); |
BitstreamPad(bs); |
1033 |
BitstreamPutBits(bs, VOP_START_CODE, 32); |
BitstreamPutBits(bs, VOP_START_CODE, 32); |
1034 |
|
|
1035 |
BitstreamPutBits(bs, pParam->coding_type, 2); |
BitstreamPutBits(bs, frame->coding_type, 2); |
1036 |
|
DPRINTF(DPRINTF_HEADER, "coding_type = %i", frame->coding_type); |
1037 |
|
|
1038 |
// time_base = 0 write n x PutBit(1), PutBit(0) |
for (i = 0; i < frame->seconds; i++) { |
1039 |
BitstreamPutBits(bs, 0, 1); |
BitstreamPutBit(bs, 1); |
1040 |
|
} |
1041 |
|
BitstreamPutBit(bs, 0); |
1042 |
|
|
1043 |
WRITE_MARKER(); |
WRITE_MARKER(); |
1044 |
|
|
1045 |
// time_increment: value=nth_of_sec, nbits = log2(resolution) |
// time_increment: value=nth_of_sec, nbits = log2(resolution) |
1046 |
BitstreamPutBits(bs, 1, 1); |
|
1047 |
|
BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase)); |
1048 |
|
/*DPRINTF("[%i:%i] %c", frame->seconds, frame->ticks, |
1049 |
|
frame->coding_type == I_VOP ? 'I' : frame->coding_type == |
1050 |
|
P_VOP ? 'P' : 'B');*/ |
1051 |
|
|
1052 |
WRITE_MARKER(); |
WRITE_MARKER(); |
1053 |
|
|
1054 |
|
if (!vop_coded) { |
1055 |
|
BitstreamPutBits(bs, 0, 1); |
1056 |
|
return; |
1057 |
|
} |
1058 |
|
|
1059 |
BitstreamPutBits(bs, 1, 1); // vop_coded |
BitstreamPutBits(bs, 1, 1); // vop_coded |
1060 |
|
|
1061 |
if (pParam->coding_type != I_VOP) |
if ( (frame->coding_type == P_VOP) || (frame->coding_type == S_VOP) ) |
1062 |
BitstreamPutBits(bs, pParam->rounding_type, 1); |
BitstreamPutBits(bs, frame->rounding_type, 1); |
1063 |
|
|
1064 |
BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold |
BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold |
1065 |
|
|
1066 |
if (pParam->global_flags & XVID_INTERLACING) |
if (frame->global_flags & XVID_INTERLACING) { |
1067 |
|
BitstreamPutBit(bs, (frame->global_flags & XVID_TOPFIELDFIRST)); |
1068 |
|
BitstreamPutBit(bs, (frame->global_flags & XVID_ALTERNATESCAN)); |
1069 |
|
} |
1070 |
|
|
1071 |
|
if (frame->coding_type == S_VOP) { |
1072 |
|
if (1) { // no_of_sprite_warping_points>=1 |
1073 |
|
if (pParam->m_quarterpel) |
1074 |
|
bs_put_spritetrajectory(bs, frame->GMC_MV.x/2 ); // du[0] |
1075 |
|
else |
1076 |
|
bs_put_spritetrajectory(bs, frame->GMC_MV.x ); // du[0] |
1077 |
|
WRITE_MARKER(); |
1078 |
|
|
1079 |
|
if (pParam->m_quarterpel) |
1080 |
|
bs_put_spritetrajectory(bs, frame->GMC_MV.y/2 ); // dv[0] |
1081 |
|
else |
1082 |
|
bs_put_spritetrajectory(bs, frame->GMC_MV.y ); // dv[0] |
1083 |
|
WRITE_MARKER(); |
1084 |
|
|
1085 |
|
|
1086 |
|
if (pParam->m_quarterpel) |
1087 |
{ |
{ |
1088 |
BitstreamPutBit(bs, 1); // top field first |
DPRINTF(DPRINTF_HEADER,"sprite_warping_point[%i] xy=(%i,%i) *QPEL*", 0, frame->GMC_MV.x/2, frame->GMC_MV.y/2); |
1089 |
BitstreamPutBit(bs, 0); // alternate vertical scan |
} |
1090 |
|
else |
1091 |
|
{ |
1092 |
|
DPRINTF(DPRINTF_HEADER,"sprite_warping_point[%i] xy=(%i,%i)", 0, frame->GMC_MV.x, frame->GMC_MV.y); |
1093 |
|
} |
1094 |
|
|
1095 |
|
} |
1096 |
|
/* GMC is halfpel in bitstream, even though GMC_MV was pseudo-qpel (2*halfpel) */ |
1097 |
|
|
1098 |
|
if (2) { // no_of_sprite_warping_points>=2 (for DivX5 compat) |
1099 |
|
bs_put_spritetrajectory(bs, 0 ); |
1100 |
|
WRITE_MARKER(); |
1101 |
|
bs_put_spritetrajectory(bs, 0 ); |
1102 |
|
WRITE_MARKER(); |
1103 |
|
} |
1104 |
|
// no support for brightness_change! |
1105 |
} |
} |
1106 |
|
|
1107 |
BitstreamPutBits(bs, pParam->quant, 5); // quantizer |
BitstreamPutBits(bs, frame->quant, 5); // quantizer |
1108 |
|
|
1109 |
|
if (frame->coding_type != I_VOP) |
1110 |
|
BitstreamPutBits(bs, frame->fcode, 3); // forward_fixed_code |
1111 |
|
|
1112 |
|
if (frame->coding_type == B_VOP) |
1113 |
|
BitstreamPutBits(bs, frame->bcode, 3); // backward_fixed_code |
1114 |
|
|
1115 |
|
} |
1116 |
|
|
1117 |
|
void |
1118 |
|
BitstreamWriteUserData(Bitstream * const bs, |
1119 |
|
uint8_t * data, |
1120 |
|
const int length) |
1121 |
|
{ |
1122 |
|
int i; |
1123 |
|
|
1124 |
|
BitstreamPad(bs); |
1125 |
|
BitstreamPutBits(bs, USERDATA_START_CODE, 32); |
1126 |
|
|
1127 |
|
for (i = 0; i < length; i++) { |
1128 |
|
BitstreamPutBits(bs, data[i], 8); |
1129 |
|
} |
1130 |
|
|
|
if (pParam->coding_type != I_VOP) |
|
|
BitstreamPutBits(bs, pParam->fixed_code, 3); // fixed_code = [1,4] |
|
1131 |
} |
} |