41 |
* * |
* * |
42 |
* Revision history: * |
* Revision history: * |
43 |
* * |
* * |
44 |
|
* 11.07.2002 add VOP width & height return to dec when dec->width * |
45 |
|
* or dec->height is 0 (for use in examples/ex1.c) * |
46 |
|
* MinChen <chenm001@163.com> * |
47 |
|
* 22.05.2002 bs_put_matrix fix * |
48 |
|
* 20.05.2002 added BitstreamWriteUserData * |
49 |
|
* 19.06.2002 Fix a little bug in use custom quant matrix * |
50 |
|
* MinChen <chenm001@163.com> * |
51 |
|
* 08.05.2002 add low_delay support for B_VOP decode * |
52 |
|
* MinChen <chenm001@163.com> * |
53 |
|
* 06.05.2002 low_delay * |
54 |
|
* 06.05.2002 fixed fincr/fbase error * |
55 |
|
* 01.05.2002 added BVOP support to BitstreamWriteVopHeader * |
56 |
|
* 15.04.2002 rewrite log2bin use asm386 By MinChen <chenm001@163.com> * |
57 |
|
* 26.03.2002 interlacing support * |
58 |
* 03.03.2002 qmatrix writing * |
* 03.03.2002 qmatrix writing * |
59 |
* 03.03.2002 merged BITREADER and BITWRITER * |
* 03.03.2002 merged BITREADER and BITWRITER * |
60 |
* 30.02.2002 intra_dc_threshold support * |
* 30.02.2002 intra_dc_threshold support * |
61 |
* 04.12.2001 support for additional headers * |
* 04.12.2001 support for additional headers * |
62 |
* 16.12.2001 inital version * |
* 16.12.2001 inital version * |
63 |
* * |
* |
64 |
******************************************************************************/ |
******************************************************************************/ |
65 |
|
|
66 |
|
|
67 |
#include "bitstream.h" |
#include "bitstream.h" |
68 |
#include "zigzag.h" |
#include "zigzag.h" |
69 |
|
#include "../quant/quant_matrix.h" |
70 |
|
|
71 |
|
|
72 |
static int __inline log2bin(int value) |
static uint32_t __inline |
73 |
|
log2bin(uint32_t value) |
74 |
{ |
{ |
75 |
|
/* Changed by Chenm001 */ |
76 |
|
#ifndef WIN32 |
77 |
int n = 0; |
int n = 0; |
78 |
while (value) |
|
79 |
{ |
while (value) { |
80 |
value >>= 1; |
value >>= 1; |
81 |
n++; |
n++; |
82 |
} |
} |
83 |
return n; |
return n; |
84 |
|
#else |
85 |
|
__asm { |
86 |
|
bsr eax, value |
87 |
|
inc eax |
88 |
|
} |
89 |
|
#endif |
90 |
} |
} |
91 |
|
|
92 |
|
|
93 |
static const uint32_t intra_dc_threshold_table[] = |
static const uint32_t intra_dc_threshold_table[] = { |
|
{ |
|
94 |
32, /* never use */ |
32, /* never use */ |
95 |
13, |
13, |
96 |
15, |
15, |
102 |
}; |
}; |
103 |
|
|
104 |
|
|
105 |
|
void |
106 |
|
bs_get_matrix(Bitstream * bs, |
107 |
|
uint8_t * matrix) |
108 |
|
{ |
109 |
|
int i = 0; |
110 |
|
int last, value = 0; |
111 |
|
|
112 |
|
do { |
113 |
|
last = value; |
114 |
|
value = BitstreamGetBits(bs, 8); |
115 |
|
matrix[scan_tables[0][i++]] = value; |
116 |
|
} |
117 |
|
while (value != 0 && i < 64); |
118 |
|
i--; // fix little bug at coeff not full |
119 |
|
|
120 |
|
while (i < 64) { |
121 |
|
matrix[scan_tables[0][i++]] = last; |
122 |
|
} |
123 |
|
} |
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
// for PVOP addbits == fcode - 1 |
128 |
|
// for BVOP addbits == max(fcode,bcode) - 1 |
129 |
|
// returns mbpos |
130 |
|
int |
131 |
|
read_video_packet_header(Bitstream *bs, const int addbits, int * quant) |
132 |
|
{ |
133 |
|
int nbits; |
134 |
|
int mbnum; |
135 |
|
int hec; |
136 |
|
|
137 |
|
nbits = NUMBITS_VP_RESYNC_MARKER + addbits; |
138 |
|
|
139 |
|
BitstreamSkip(bs, BitstreamNumBitsToByteAlign(bs)); |
140 |
|
BitstreamSkip(bs, nbits); |
141 |
|
|
142 |
|
DPRINTF(DPRINTF_STARTCODE, "<video_packet_header>"); |
143 |
|
|
144 |
|
// if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { |
145 |
|
// hec |
146 |
|
// vop_width |
147 |
|
// marker_bit |
148 |
|
// vop_height |
149 |
|
// marker_bit |
150 |
|
|
151 |
|
//} |
152 |
|
|
153 |
|
mbnum = BitstreamGetBits(bs, 9); |
154 |
|
DPRINTF(DPRINTF_HEADER, "mbnum %i", mbnum); |
155 |
|
|
156 |
|
// if (dec->shape != VIDOBJLAY_SHAPE_BINARYONLY) |
157 |
|
*quant = BitstreamGetBits(bs, 5); |
158 |
|
DPRINTF(DPRINTF_HEADER, "quant %i", *quant); |
159 |
|
|
160 |
|
// if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) |
161 |
|
hec = BitstreamGetBit(bs); |
162 |
|
DPRINTF(DPRINTF_HEADER, "header_extension_code %i", hec); |
163 |
|
// if (hec) |
164 |
|
// .. decoder hec-header ... |
165 |
|
|
166 |
|
return mbnum; |
167 |
|
} |
168 |
|
|
169 |
|
|
170 |
|
|
171 |
/* |
/* |
172 |
decode headers |
decode headers |
173 |
returns coding_type, or -1 if error |
returns coding_type, or -1 if error |
174 |
*/ |
*/ |
175 |
|
|
176 |
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 |
177 |
|
#define VIDOBJLAY_START_CODE_MASK 0x0000000f |
178 |
|
|
179 |
|
int |
180 |
|
BitstreamReadHeaders(Bitstream * bs, |
181 |
|
DECODER * dec, |
182 |
|
uint32_t * rounding, |
183 |
|
uint32_t * quant, |
184 |
|
uint32_t * fcode_forward, |
185 |
|
uint32_t * fcode_backward, |
186 |
|
uint32_t * intra_dc_threshold) |
187 |
{ |
{ |
188 |
uint32_t vol_ver_id; |
uint32_t vol_ver_id; |
189 |
uint32_t time_inc_resolution; |
static uint32_t time_increment_resolution; |
190 |
uint32_t coding_type; |
uint32_t coding_type; |
191 |
uint32_t start_code; |
uint32_t start_code; |
192 |
|
uint32_t time_incr = 0; |
193 |
|
int32_t time_increment; |
194 |
|
|
195 |
do |
do { |
|
{ |
|
196 |
BitstreamByteAlign(bs); |
BitstreamByteAlign(bs); |
197 |
start_code = BitstreamShowBits(bs, 32); |
start_code = BitstreamShowBits(bs, 32); |
198 |
|
|
199 |
if (start_code == VISOBJSEQ_START_CODE) |
if (start_code == VISOBJSEQ_START_CODE) { |
200 |
{ |
|
201 |
// DEBUG("visual_object_sequence"); |
int profile; |
202 |
|
|
203 |
|
DPRINTF(DPRINTF_STARTCODE, "<visual_object_sequence>"); |
204 |
|
|
205 |
BitstreamSkip(bs, 32); // visual_object_sequence_start_code |
BitstreamSkip(bs, 32); // visual_object_sequence_start_code |
206 |
BitstreamSkip(bs, 8); // profile_and_level_indication |
profile = BitstreamGetBits(bs, 8); // profile_and_level_indication |
207 |
} |
|
208 |
else if (start_code == VISOBJSEQ_STOP_CODE) |
DPRINTF(DPRINTF_HEADER, "profile_and_level_indication %i", profile); |
209 |
{ |
|
210 |
|
} else if (start_code == VISOBJSEQ_STOP_CODE) { |
211 |
|
|
212 |
BitstreamSkip(bs, 32); // visual_object_sequence_stop_code |
BitstreamSkip(bs, 32); // visual_object_sequence_stop_code |
213 |
} |
|
214 |
else if (start_code == VISOBJ_START_CODE) |
DPRINTF(DPRINTF_STARTCODE, "</visual_object_sequence>"); |
215 |
{ |
|
216 |
// DEBUG("visual_object"); |
} else if (start_code == VISOBJ_START_CODE) { |
217 |
|
|
218 |
|
DPRINTF(DPRINTF_STARTCODE, "<visual_object>"); |
219 |
|
|
220 |
BitstreamSkip(bs,32); // visual_object_start_code |
BitstreamSkip(bs,32); // visual_object_start_code |
221 |
if (BitstreamGetBit(bs)) // is_visual_object_identified |
if (BitstreamGetBit(bs)) // is_visual_object_identified |
222 |
{ |
{ |
223 |
vol_ver_id = BitstreamGetBits(bs,4); // visual_object_ver_id |
vol_ver_id = BitstreamGetBits(bs,4); // visual_object_ver_id |
224 |
|
DPRINTF(DPRINTF_HEADER,"ver_id %i", vol_ver_id); |
225 |
BitstreamSkip(bs, 3); // visual_object_priority |
BitstreamSkip(bs, 3); // visual_object_priority |
226 |
} |
} else { |
|
else |
|
|
{ |
|
227 |
vol_ver_id = 1; |
vol_ver_id = 1; |
228 |
} |
} |
229 |
|
|
230 |
if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) // visual_object_type |
if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) // visual_object_type |
231 |
{ |
{ |
232 |
DEBUG("visual_object_type != video"); |
DPRINTF(DPRINTF_ERROR, "visual_object_type != video"); |
233 |
return -1; |
return -1; |
234 |
} |
} |
235 |
BitstreamSkip(bs, 4); |
BitstreamSkip(bs, 4); |
238 |
|
|
239 |
if (BitstreamGetBit(bs)) // video_signal_type |
if (BitstreamGetBit(bs)) // video_signal_type |
240 |
{ |
{ |
241 |
DEBUG("+ video_signal_type"); |
DPRINTF(DPRINTF_HEADER,"+ video_signal_type"); |
242 |
BitstreamSkip(bs, 3); // video_format |
BitstreamSkip(bs, 3); // video_format |
243 |
BitstreamSkip(bs, 1); // video_range |
BitstreamSkip(bs, 1); // video_range |
244 |
if (BitstreamGetBit(bs)) // color_description |
if (BitstreamGetBit(bs)) // color_description |
245 |
{ |
{ |
246 |
DEBUG("+ color_description"); |
DPRINTF(DPRINTF_HEADER,"+ color_description"); |
247 |
BitstreamSkip(bs, 8); // color_primaries |
BitstreamSkip(bs, 8); // color_primaries |
248 |
BitstreamSkip(bs, 8); // transfer_characteristics |
BitstreamSkip(bs, 8); // transfer_characteristics |
249 |
BitstreamSkip(bs, 8); // matrix_coefficients |
BitstreamSkip(bs, 8); // matrix_coefficients |
250 |
} |
} |
251 |
} |
} |
252 |
} |
} else if ((start_code & ~VIDOBJ_START_CODE_MASK) == VIDOBJ_START_CODE) { |
253 |
else if ((start_code & ~0x1f) == VIDOBJ_START_CODE) |
|
254 |
{ |
DPRINTF(DPRINTF_STARTCODE, "<video_object>"); |
255 |
|
DPRINTF(DPRINTF_HEADER, "vo id %i", start_code & VIDOBJ_START_CODE_MASK); |
256 |
|
|
257 |
BitstreamSkip(bs, 32); // video_object_start_code |
BitstreamSkip(bs, 32); // video_object_start_code |
258 |
} |
|
259 |
else if ((start_code & ~0xf) == VIDOBJLAY_START_CODE) |
} else if ((start_code & ~VIDOBJLAY_START_CODE_MASK) == VIDOBJLAY_START_CODE) { |
260 |
{ |
|
261 |
// DEBUG("video_object_layer"); |
DPRINTF(DPRINTF_STARTCODE, "<video_object_layer>"); |
262 |
|
DPRINTF(DPRINTF_HEADER, "vol id %i", start_code & VIDOBJLAY_START_CODE_MASK); |
263 |
|
|
264 |
BitstreamSkip(bs, 32); // video_object_layer_start_code |
BitstreamSkip(bs, 32); // video_object_layer_start_code |
265 |
|
|
266 |
BitstreamSkip(bs, 1); // random_accessible_vol |
BitstreamSkip(bs, 1); // random_accessible_vol |
267 |
|
|
268 |
// video_object_type_indication |
// video_object_type_indication |
269 |
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) != 0) // BUGGY DIVX |
|
270 |
{ |
{ |
271 |
DEBUG1("video_object_type_indication not supported", BitstreamShowBits(bs, 8)); |
DPRINTF(DPRINTF_ERROR,"video_object_type_indication %i not supported ", |
272 |
|
BitstreamShowBits(bs, 8)); |
273 |
return -1; |
return -1; |
274 |
} |
} |
275 |
BitstreamSkip(bs, 8); |
BitstreamSkip(bs, 8); |
277 |
|
|
278 |
if (BitstreamGetBit(bs)) // is_object_layer_identifier |
if (BitstreamGetBit(bs)) // is_object_layer_identifier |
279 |
{ |
{ |
280 |
DEBUG("+ is_object_layer_identifier"); |
DPRINTF(DPRINTF_HEADER, "+ is_object_layer_identifier"); |
281 |
vol_ver_id = BitstreamGetBits(bs,4); // video_object_layer_verid |
vol_ver_id = BitstreamGetBits(bs,4); // video_object_layer_verid |
282 |
|
DPRINTF(DPRINTF_HEADER,"ver_id %i", vol_ver_id); |
283 |
BitstreamSkip(bs, 3); // video_object_layer_priority |
BitstreamSkip(bs, 3); // video_object_layer_priority |
284 |
} |
} else { |
|
else |
|
|
{ |
|
285 |
vol_ver_id = 1; |
vol_ver_id = 1; |
286 |
} |
} |
|
//DEBUGI("vol_ver_id", vol_ver_id); |
|
287 |
|
|
288 |
if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR) // aspect_ratio_info |
if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR) // aspect_ratio_info |
289 |
{ |
{ |
290 |
DEBUG("+ aspect_ratio_info"); |
DPRINTF(DPRINTF_HEADER, "+ aspect_ratio_info"); |
291 |
BitstreamSkip(bs, 8); // par_width |
BitstreamSkip(bs, 8); // par_width |
292 |
BitstreamSkip(bs, 8); // par_height |
BitstreamSkip(bs, 8); // par_height |
293 |
} |
} |
294 |
|
|
295 |
if (BitstreamGetBit(bs)) // vol_control_parameters |
if (BitstreamGetBit(bs)) // vol_control_parameters |
296 |
{ |
{ |
297 |
DEBUG("+ vol_control_parameters"); |
DPRINTF(DPRINTF_HEADER, "+ vol_control_parameters"); |
298 |
BitstreamSkip(bs, 2); // chroma_format |
BitstreamSkip(bs, 2); // chroma_format |
299 |
BitstreamSkip(bs, 1); // low_delay |
dec->low_delay = BitstreamGetBit(bs); // low_delay |
300 |
|
DPRINTF(DPRINTF_HEADER, "low_delay %i", dec->low_delay); |
301 |
if (BitstreamGetBit(bs)) // vbv_parameters |
if (BitstreamGetBit(bs)) // vbv_parameters |
302 |
{ |
{ |
303 |
DEBUG("+ vbv_parameters"); |
DPRINTF(DPRINTF_HEADER,"+ vbv_parameters"); |
304 |
BitstreamSkip(bs, 15); // first_half_bitrate |
BitstreamSkip(bs, 15); // first_half_bitrate |
305 |
READ_MARKER(); |
READ_MARKER(); |
306 |
BitstreamSkip(bs, 15); // latter_half_bitrate |
BitstreamSkip(bs, 15); // latter_half_bitrate |
317 |
} |
} |
318 |
|
|
319 |
dec->shape = BitstreamGetBits(bs, 2); // video_object_layer_shape |
dec->shape = BitstreamGetBits(bs, 2); // video_object_layer_shape |
320 |
// DEBUG1("shape", dec->shape); |
DPRINTF(DPRINTF_HEADER, "shape %i", dec->shape); |
321 |
|
|
322 |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) { |
|
{ |
|
323 |
BitstreamSkip(bs, 4); // video_object_layer_shape_extension |
BitstreamSkip(bs, 4); // video_object_layer_shape_extension |
324 |
} |
} |
325 |
|
|
326 |
READ_MARKER(); |
READ_MARKER(); |
327 |
|
|
328 |
time_inc_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution |
// *************************** for decode B-frame time *********************** |
329 |
time_inc_resolution--; |
time_increment_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution |
|
if (time_inc_resolution > 0) |
|
|
{ |
|
|
dec->time_inc_bits = log2bin(time_inc_resolution); |
|
|
} |
|
|
else |
|
|
{ |
|
|
// dec->time_inc_bits = 0; |
|
330 |
|
|
331 |
|
DPRINTF(DPRINTF_HEADER,"vop_time_increment_resolution %i", time_increment_resolution); |
332 |
|
|
333 |
|
// time_increment_resolution--; |
334 |
|
|
335 |
|
if (time_increment_resolution > 0) { |
336 |
|
dec->time_inc_bits = log2bin(time_increment_resolution-1); |
337 |
|
} else { |
338 |
|
// dec->time_inc_bits = 0; |
339 |
// for "old" xvid compatibility, set time_inc_bits = 1 |
// for "old" xvid compatibility, set time_inc_bits = 1 |
340 |
dec->time_inc_bits = 1; |
dec->time_inc_bits = 1; |
341 |
} |
} |
344 |
|
|
345 |
if (BitstreamGetBit(bs)) // fixed_vop_rate |
if (BitstreamGetBit(bs)) // fixed_vop_rate |
346 |
{ |
{ |
347 |
|
DPRINTF(DPRINTF_HEADER, "+ fixed_vop_rate"); |
348 |
BitstreamSkip(bs, dec->time_inc_bits); // fixed_vop_time_increment |
BitstreamSkip(bs, dec->time_inc_bits); // fixed_vop_time_increment |
349 |
} |
} |
350 |
|
|
351 |
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) |
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) { |
|
{ |
|
352 |
|
|
353 |
if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) |
if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) { |
|
{ |
|
354 |
uint32_t width, height; |
uint32_t width, height; |
355 |
|
|
356 |
READ_MARKER(); |
READ_MARKER(); |
357 |
width = BitstreamGetBits(bs, 13); // video_object_layer_width |
width = BitstreamGetBits(bs, 13); // video_object_layer_width |
|
//DEBUGI("width", width); |
|
358 |
READ_MARKER(); |
READ_MARKER(); |
359 |
height = BitstreamGetBits(bs, 13); // video_object_layer_height |
height = BitstreamGetBits(bs, 13); // video_object_layer_height |
|
//DEBUGI("height", height); |
|
360 |
READ_MARKER(); |
READ_MARKER(); |
361 |
|
|
362 |
if (width != dec->width || height != dec->height) |
DPRINTF(DPRINTF_HEADER, "width %i", width); |
363 |
{ |
DPRINTF(DPRINTF_HEADER, "height %i", height); |
364 |
DEBUG("FATAL: video dimension discrepancy ***"); |
|
365 |
DEBUG2("bitstream width/height", width, height); |
// for auto set width & height |
366 |
DEBUG2("param width/height", dec->width, dec->height); |
if (dec->width == 0) |
367 |
|
dec->width = width; |
368 |
|
if (dec->height == 0) |
369 |
|
dec->height = height; |
370 |
|
|
371 |
|
if (width != dec->width || height != dec->height) { |
372 |
|
DPRINTF(DPRINTF_ERROR, "XVID_DEC_PARAM width/height does not match bitstream"); |
373 |
return -1; |
return -1; |
374 |
} |
} |
375 |
|
|
376 |
} |
} |
377 |
|
|
378 |
if (BitstreamGetBit(bs)) // interlaced |
dec->interlacing = BitstreamGetBit(bs); |
379 |
{ |
DPRINTF(DPRINTF_HEADER, "interlace", dec->interlacing); |
|
DEBUG("TODO: interlaced"); |
|
|
// TODO |
|
|
return -1; |
|
|
} |
|
380 |
|
|
381 |
if (!BitstreamGetBit(bs)) // obmc_disable |
if (!BitstreamGetBit(bs)) // obmc_disable |
382 |
{ |
{ |
383 |
DEBUG("IGNORED/TODO: !obmc_disable"); |
DPRINTF(DPRINTF_ERROR, "obmc_disabled==false not supported"); |
384 |
// TODO |
// TODO |
385 |
// fucking divx4.02 has this enabled |
// fucking divx4.02 has this enabled |
386 |
} |
} |
387 |
|
|
388 |
if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2))) // sprite_enable |
if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2))) // sprite_enable |
389 |
{ |
{ |
390 |
DEBUG("sprite_enable; not supported"); |
DPRINTF(DPRINTF_ERROR, "spriate_enabled not supported"); |
391 |
return -1; |
return -1; |
392 |
} |
} |
393 |
|
|
394 |
if (vol_ver_id != 1 && dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) |
if (vol_ver_id != 1 && |
395 |
{ |
dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { |
396 |
BitstreamSkip(bs, 1); // sadct_disable |
BitstreamSkip(bs, 1); // sadct_disable |
397 |
} |
} |
398 |
|
|
399 |
if (BitstreamGetBit(bs)) // not_8_bit |
if (BitstreamGetBit(bs)) // not_8_bit |
400 |
{ |
{ |
401 |
DEBUG("+ not_8_bit [IGNORED/TODO]"); |
DPRINTF(DPRINTF_HEADER, "not_8_bit==true (ignored)"); |
402 |
dec->quant_bits = BitstreamGetBits(bs, 4); // quant_precision |
dec->quant_bits = BitstreamGetBits(bs, 4); // quant_precision |
403 |
BitstreamSkip(bs, 4); // bits_per_pixel |
BitstreamSkip(bs, 4); // bits_per_pixel |
404 |
} |
} else { |
|
else |
|
|
{ |
|
405 |
dec->quant_bits = 5; |
dec->quant_bits = 5; |
406 |
} |
} |
407 |
|
|
408 |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) { |
|
{ |
|
409 |
BitstreamSkip(bs, 1); // no_gray_quant_update |
BitstreamSkip(bs, 1); // no_gray_quant_update |
410 |
BitstreamSkip(bs, 1); // composition_method |
BitstreamSkip(bs, 1); // composition_method |
411 |
BitstreamSkip(bs, 1); // linear_composition |
BitstreamSkip(bs, 1); // linear_composition |
412 |
} |
} |
413 |
|
|
414 |
dec->quant_type = BitstreamGetBit(bs); // quant_type |
dec->quant_type = BitstreamGetBit(bs); // quant_type |
415 |
// DEBUG1("**** quant_type", dec->quant_type); |
DPRINTF(DPRINTF_HEADER, "quant_type %i", dec->quant_type); |
416 |
|
|
417 |
if (dec->quant_type) |
if (dec->quant_type) { |
|
{ |
|
418 |
if (BitstreamGetBit(bs)) // load_intra_quant_mat |
if (BitstreamGetBit(bs)) // load_intra_quant_mat |
419 |
{ |
{ |
420 |
DEBUG("TODO: load_intra_quant_mat"); |
uint8_t matrix[64]; |
421 |
// TODO |
|
422 |
return -1; |
DPRINTF(DPRINTF_HEADER, "load_intra_quant_mat"); |
423 |
} |
|
424 |
|
bs_get_matrix(bs, matrix); |
425 |
|
set_intra_matrix(matrix); |
426 |
|
} else |
427 |
|
set_intra_matrix(get_default_intra_matrix()); |
428 |
|
|
429 |
if (BitstreamGetBit(bs)) // load_inter_quant_mat |
if (BitstreamGetBit(bs)) // load_inter_quant_mat |
430 |
{ |
{ |
431 |
DEBUG("TODO: load_inter_quant_mat"); |
uint8_t matrix[64]; |
|
// TODO |
|
|
return -1; |
|
|
} |
|
432 |
|
|
433 |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) |
DPRINTF(DPRINTF_HEADER, "load_inter_quant_mat"); |
434 |
{ |
|
435 |
// TODO |
bs_get_matrix(bs, matrix); |
436 |
DEBUG("TODO: grayscale matrix stuff"); |
set_inter_matrix(matrix); |
437 |
|
} else |
438 |
|
set_inter_matrix(get_default_inter_matrix()); |
439 |
|
|
440 |
|
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) { |
441 |
|
DPRINTF(DPRINTF_ERROR, "greyscale matrix not supported"); |
442 |
return -1; |
return -1; |
443 |
} |
} |
444 |
|
|
445 |
} |
} |
446 |
|
|
447 |
|
|
448 |
if (vol_ver_id != 1) |
if (vol_ver_id != 1) { |
449 |
{ |
DEBUG("QUARTERPEL BITSTREAM"); |
450 |
dec->quarterpel = BitstreamGetBit(bs); // quarter_sampe |
dec->quarterpel = BitstreamGetBit(bs); // quarter_sample |
|
if (dec->quarterpel) |
|
|
{ |
|
|
DEBUG("IGNORED/TODO: quarter_sample"); |
|
|
} |
|
451 |
} |
} |
452 |
else |
else |
|
{ |
|
453 |
dec->quarterpel = 0; |
dec->quarterpel = 0; |
454 |
} |
|
455 |
|
|
456 |
if (!BitstreamGetBit(bs)) // complexity_estimation_disable |
if (!BitstreamGetBit(bs)) // complexity_estimation_disable |
457 |
{ |
{ |
458 |
DEBUG("TODO: complexity_estimation header"); |
DPRINTF(DPRINTF_ERROR, "complexity_estimation not supported"); |
|
// TODO |
|
459 |
return -1; |
return -1; |
460 |
} |
} |
461 |
|
|
462 |
if (!BitstreamGetBit(bs)) // resync_marker_disable |
BitstreamSkip(bs, 1); // resync_marker_disable |
|
{ |
|
|
DEBUG("IGNORED/TODO: !resync_marker_disable"); |
|
|
// TODO |
|
|
} |
|
463 |
|
|
464 |
if (BitstreamGetBit(bs)) // data_partitioned |
if (BitstreamGetBit(bs)) // data_partitioned |
465 |
{ |
{ |
466 |
DEBUG("+ data_partitioned"); |
DPRINTF(DPRINTF_ERROR, "data_partitioned not supported"); |
467 |
BitstreamSkip(bs, 1); // reversible_vlc |
BitstreamSkip(bs, 1); // reversible_vlc |
468 |
} |
} |
469 |
|
|
470 |
if (vol_ver_id != 1) |
if (vol_ver_id != 1) { |
|
{ |
|
471 |
if (BitstreamGetBit(bs)) // newpred_enable |
if (BitstreamGetBit(bs)) // newpred_enable |
472 |
{ |
{ |
473 |
DEBUG("+ newpred_enable"); |
DPRINTF(DPRINTF_HEADER, "+ newpred_enable"); |
474 |
BitstreamSkip(bs, 2); // requested_upstream_message_type |
BitstreamSkip(bs, 2); // requested_upstream_message_type |
475 |
BitstreamSkip(bs, 1); // newpred_segment_type |
BitstreamSkip(bs, 1); // newpred_segment_type |
476 |
} |
} |
477 |
if (BitstreamGetBit(bs)) // reduced_resolution_vop_enable |
if (BitstreamGetBit(bs)) // reduced_resolution_vop_enable |
478 |
{ |
{ |
479 |
DEBUG("TODO: reduced_resolution_vop_enable"); |
DPRINTF(DPRINTF_ERROR, "reduced_resolution_vop not supported"); |
|
// TODO |
|
480 |
return -1; |
return -1; |
481 |
} |
} |
482 |
} |
} |
483 |
|
|
484 |
if (BitstreamGetBit(bs)) // scalability |
if ((dec->scalability = BitstreamGetBit(bs))) // scalability |
485 |
{ |
{ |
486 |
// TODO |
DPRINTF(DPRINTF_ERROR, "scalability not supported"); |
|
DEBUG("TODO: scalability"); |
|
487 |
return -1; |
return -1; |
488 |
} |
} |
489 |
} |
} else // dec->shape == BINARY_ONLY |
|
else // dec->shape == BINARY_ONLY |
|
|
{ |
|
|
if (vol_ver_id != 1) |
|
490 |
{ |
{ |
491 |
|
if (vol_ver_id != 1) { |
492 |
if (BitstreamGetBit(bs)) // scalability |
if (BitstreamGetBit(bs)) // scalability |
493 |
{ |
{ |
494 |
// TODO |
DPRINTF(DPRINTF_ERROR, "scalability not supported"); |
|
DEBUG("TODO: scalability"); |
|
495 |
return -1; |
return -1; |
496 |
} |
} |
497 |
} |
} |
499 |
|
|
500 |
} |
} |
501 |
|
|
502 |
} |
} else if (start_code == GRPOFVOP_START_CODE) { |
503 |
else if (start_code == GRPOFVOP_START_CODE) |
|
504 |
{ |
DPRINTF(DPRINTF_STARTCODE, "<group_of_vop>"); |
505 |
// DEBUG("group_of_vop"); |
|
506 |
BitstreamSkip(bs, 32); |
BitstreamSkip(bs, 32); |
507 |
{ |
{ |
508 |
int hours, minutes, seconds; |
int hours, minutes, seconds; |
509 |
|
|
510 |
hours = BitstreamGetBits(bs, 5); |
hours = BitstreamGetBits(bs, 5); |
511 |
minutes = BitstreamGetBits(bs, 6); |
minutes = BitstreamGetBits(bs, 6); |
512 |
READ_MARKER(); |
READ_MARKER(); |
513 |
seconds = BitstreamGetBits(bs, 6); |
seconds = BitstreamGetBits(bs, 6); |
514 |
// DEBUG3("hms", hours, minutes, seconds); |
|
515 |
|
DPRINTF(DPRINTF_HEADER, "time %ih%im%is", hours); |
516 |
} |
} |
517 |
BitstreamSkip(bs, 1); // closed_gov |
BitstreamSkip(bs, 1); // closed_gov |
518 |
BitstreamSkip(bs, 1); // broken_link |
BitstreamSkip(bs, 1); // broken_link |
519 |
} |
|
520 |
else if (start_code == VOP_START_CODE) |
} else if (start_code == VOP_START_CODE) { |
521 |
{ |
|
522 |
// DEBUG("vop_start_code"); |
DPRINTF(DPRINTF_STARTCODE, "<vop>"); |
523 |
|
|
524 |
BitstreamSkip(bs, 32); // vop_start_code |
BitstreamSkip(bs, 32); // vop_start_code |
525 |
|
|
526 |
coding_type = BitstreamGetBits(bs, 2); // vop_coding_type |
coding_type = BitstreamGetBits(bs, 2); // vop_coding_type |
527 |
//DEBUG1("coding_type", coding_type); |
DPRINTF(DPRINTF_HEADER, "coding_type %i", coding_type); |
528 |
|
|
529 |
while (BitstreamGetBit(bs) != 0) ; // time_base |
// *************************** for decode B-frame time *********************** |
530 |
|
while (BitstreamGetBit(bs) != 0) // time_base |
531 |
|
time_incr++; |
532 |
|
|
533 |
READ_MARKER(); |
READ_MARKER(); |
534 |
|
|
535 |
//DEBUG1("time_inc_bits", dec->time_inc_bits); |
if (dec->time_inc_bits) { |
536 |
//DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits)); |
time_increment = (BitstreamGetBits(bs, dec->time_inc_bits)); // vop_time_increment |
537 |
if (dec->time_inc_bits) |
} |
538 |
{ |
|
539 |
BitstreamSkip(bs, dec->time_inc_bits); // vop_time_increment |
DPRINTF(DPRINTF_HEADER, "time_base %i", time_incr); |
540 |
|
DPRINTF(DPRINTF_HEADER, "time_increment %i", time_increment); |
541 |
|
|
542 |
|
DPRINTF(DPRINTF_TIMECODE, "%c %i:%i", |
543 |
|
coding_type == I_VOP ? 'I' : coding_type == P_VOP ? 'P' : 'B', |
544 |
|
time_incr, time_increment); |
545 |
|
|
546 |
|
if (coding_type != B_VOP) { |
547 |
|
dec->last_time_base = dec->time_base; |
548 |
|
dec->time_base += time_incr; |
549 |
|
dec->time = time_increment; |
550 |
|
|
551 |
|
/* dec->time_base * time_increment_resolution + |
552 |
|
time_increment; |
553 |
|
*/ dec->time_pp = (uint32_t) |
554 |
|
(time_increment_resolution + dec->time - dec->last_non_b_time)%time_increment_resolution; |
555 |
|
dec->last_non_b_time = dec->time; |
556 |
|
} else { |
557 |
|
dec->time = time_increment; |
558 |
|
/* |
559 |
|
(dec->last_time_base + |
560 |
|
time_incr) * time_increment_resolution + time_increment; |
561 |
|
*/ |
562 |
|
dec->time_bp = (uint32_t) |
563 |
|
(time_increment_resolution + dec->last_non_b_time - dec->time)%time_increment_resolution; |
564 |
} |
} |
565 |
|
|
566 |
READ_MARKER(); |
READ_MARKER(); |
567 |
|
|
568 |
if (!BitstreamGetBit(bs)) // vop_coded |
if (!BitstreamGetBit(bs)) // vop_coded |
569 |
{ |
{ |
570 |
|
DPRINTF(DPRINTF_HEADER, "vop_coded==false"); |
571 |
return N_VOP; |
return N_VOP; |
572 |
} |
} |
573 |
|
|
576 |
} |
} |
577 |
*/ |
*/ |
578 |
|
|
579 |
if (coding_type != I_VOP) |
// fix a little bug by MinChen <chenm002@163.com> |
580 |
{ |
if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) && |
581 |
|
(coding_type == P_VOP)) { |
582 |
*rounding = BitstreamGetBit(bs); // rounding_type |
*rounding = BitstreamGetBit(bs); // rounding_type |
583 |
//DEBUG1("rounding", *rounding); |
DPRINTF(DPRINTF_HEADER, "rounding %i", *rounding); |
584 |
} |
} |
585 |
|
|
586 |
/* if (reduced_resolution_enable) |
/* if (reduced_resolution_enable) |
588 |
} |
} |
589 |
*/ |
*/ |
590 |
|
|
591 |
if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) |
if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { |
|
{ |
|
592 |
uint32_t width, height; |
uint32_t width, height; |
593 |
uint32_t horiz_mc_ref, vert_mc_ref; |
uint32_t horiz_mc_ref, vert_mc_ref; |
594 |
|
|
601 |
vert_mc_ref = BitstreamGetBits(bs, 13); |
vert_mc_ref = BitstreamGetBits(bs, 13); |
602 |
READ_MARKER(); |
READ_MARKER(); |
603 |
|
|
604 |
// DEBUG2("vop_width/height", width, height); |
DPRINTF(DPRINTF_HEADER, "width %i", width); |
605 |
// DEBUG2("ref ", horiz_mc_ref, vert_mc_ref); |
DPRINTF(DPRINTF_HEADER, "height %i", height); |
606 |
|
DPRINTF(DPRINTF_HEADER, "horiz_mc_ref %i", horiz_mc_ref); |
607 |
|
DPRINTF(DPRINTF_HEADER, "vert_mc_ref %i", vert_mc_ref); |
608 |
|
|
609 |
BitstreamSkip(bs, 1); // change_conv_ratio_disable |
BitstreamSkip(bs, 1); // change_conv_ratio_disable |
610 |
if (BitstreamGetBit(bs)) // vop_constant_alpha |
if (BitstreamGetBit(bs)) // vop_constant_alpha |
614 |
} |
} |
615 |
|
|
616 |
|
|
617 |
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) |
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) { |
|
{ |
|
618 |
// intra_dc_vlc_threshold |
// intra_dc_vlc_threshold |
619 |
*intra_dc_threshold = intra_dc_threshold_table[ BitstreamGetBits(bs,3) ]; |
*intra_dc_threshold = |
620 |
|
intra_dc_threshold_table[BitstreamGetBits(bs, 3)]; |
621 |
|
|
622 |
/* if (interlaced) |
if (dec->interlacing) { |
623 |
{ |
dec->top_field_first = BitstreamGetBit(bs); |
624 |
BitstreamSkip(bs, 1); // top_field_first |
DPRINTF(DPRINTF_HEADER, "interlace top_field_first %i", dec->top_field_first); |
625 |
BitstreamSkip(bs, 1); // alternative_vertical_scan_flag |
dec->alternate_vertical_scan = BitstreamGetBit(bs); |
626 |
*/ |
DPRINTF(DPRINTF_HEADER, "interlace alternate_vertical_scan %i", dec->alternate_vertical_scan); |
627 |
|
|
628 |
|
} |
629 |
} |
} |
630 |
|
|
631 |
*quant = BitstreamGetBits(bs, dec->quant_bits); // vop_quant |
if ((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1) // vop_quant |
632 |
//DEBUG1("quant", *quant); |
*quant = 1; |
633 |
|
|
634 |
if (coding_type != I_VOP) |
DPRINTF(DPRINTF_HEADER, "quant %i", *quant); |
635 |
{ |
|
636 |
*fcode = BitstreamGetBits(bs, 3); // fcode_forward |
if (coding_type != I_VOP) { |
637 |
|
*fcode_forward = BitstreamGetBits(bs, 3); // fcode_forward |
638 |
|
DPRINTF(DPRINTF_HEADER, "fcode_forward %i", *fcode_forward); |
639 |
} |
} |
640 |
|
|
641 |
if (coding_type == B_VOP) |
if (coding_type == B_VOP) { |
642 |
{ |
*fcode_backward = BitstreamGetBits(bs, 3); // fcode_backward |
643 |
// *fcode_backward = BitstreamGetBits(bs, 3); // fcode_backward |
DPRINTF(DPRINTF_HEADER, "fcode_backward %i", *fcode_backward); |
644 |
} |
} |
645 |
return coding_type; |
if (!dec->scalability) { |
646 |
|
if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) && |
647 |
|
(coding_type != I_VOP)) { |
648 |
|
BitstreamSkip(bs, 1); // vop_shape_coding_type |
649 |
} |
} |
|
else if (start_code == USERDATA_START_CODE) |
|
|
{ |
|
|
// DEBUG("user_data"); |
|
|
BitstreamSkip(bs, 32); // user_data_start_code |
|
650 |
} |
} |
651 |
else // start_code == ? |
return coding_type; |
652 |
{ |
|
653 |
if (BitstreamShowBits(bs, 24) == 0x000001) |
} else if (start_code == USERDATA_START_CODE) { |
654 |
|
|
655 |
|
DPRINTF(DPRINTF_STARTCODE, "<user_data>"); |
656 |
|
|
657 |
|
BitstreamSkip(bs, 32); // user_data_start_code |
658 |
|
|
659 |
|
} else // start_code == ? |
660 |
{ |
{ |
661 |
DEBUG1("*** WARNING: unknown start_code", BitstreamShowBits(bs, 32)); |
if (BitstreamShowBits(bs, 24) == 0x000001) { |
662 |
|
DPRINTF(DPRINTF_STARTCODE, "<unknown: %x>", BitstreamShowBits(bs, 32)); |
663 |
} |
} |
664 |
BitstreamSkip(bs, 8); |
BitstreamSkip(bs, 8); |
665 |
} |
} |
666 |
} |
} |
667 |
while ((BitstreamPos(bs) >> 3) < bs->length); |
while ((BitstreamPos(bs) >> 3) < bs->length); |
668 |
|
|
669 |
DEBUG("*** WARNING: no vop_start_code found"); |
//DPRINTF("*** WARNING: no vop_start_code found"); |
670 |
return -1; /* ignore it */ |
return -1; /* ignore it */ |
671 |
} |
} |
672 |
|
|
673 |
|
|
674 |
/* write custom quant matrix */ |
/* write custom quant matrix */ |
675 |
|
|
676 |
static void bs_put_matrix(Bitstream * bs, const int16_t * matrix) |
static void |
677 |
|
bs_put_matrix(Bitstream * bs, |
678 |
|
const int16_t * matrix) |
679 |
{ |
{ |
680 |
int i, j; |
int i, j; |
681 |
const int last = matrix[scan_tables[0][63]]; |
const int last = matrix[scan_tables[0][63]]; |
682 |
|
|
683 |
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--); |
684 |
|
|
685 |
for (i = 0; i <= j; i++) |
for (i = 0; i <= j; i++) { |
|
{ |
|
686 |
BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8); |
BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8); |
687 |
} |
} |
688 |
|
|
689 |
if (j < 63) |
if (j < 63) { |
|
{ |
|
690 |
BitstreamPutBits(bs, 0, 8); |
BitstreamPutBits(bs, 0, 8); |
691 |
} |
} |
692 |
} |
} |
695 |
/* |
/* |
696 |
write vol header |
write vol header |
697 |
*/ |
*/ |
698 |
void BitstreamWriteVolHeader(Bitstream * const bs, |
void |
699 |
const int width, |
BitstreamWriteVolHeader(Bitstream * const bs, |
700 |
const int height, |
const MBParam * pParam, |
701 |
const int quant_type) |
const FRAMEINFO * frame) |
702 |
{ |
{ |
703 |
// video object_start_code & vo_id |
// video object_start_code & vo_id |
704 |
BitstreamPad(bs); |
BitstreamPad(bs); |
713 |
BitstreamPutBits(bs, 0, 8); // video_object_type_indication |
BitstreamPutBits(bs, 0, 8); // video_object_type_indication |
714 |
BitstreamPutBit(bs, 0); // is_object_layer_identified (0=not given) |
BitstreamPutBit(bs, 0); // is_object_layer_identified (0=not given) |
715 |
BitstreamPutBits(bs, 1, 4); // aspect_ratio_info (1=1:1) |
BitstreamPutBits(bs, 1, 4); // aspect_ratio_info (1=1:1) |
716 |
BitstreamPutBit(bs, 0); // vol_control_parameters (0=not given) |
|
717 |
|
#ifdef BFRAMES |
718 |
|
if (pParam->max_bframes > 0) { |
719 |
|
BitstreamPutBit(bs, 1); // vol_control_parameters |
720 |
|
BitstreamPutBits(bs, 1, 2); // chroma_format 1="4:2:0" |
721 |
|
BitstreamPutBit(bs, 0); // low_delay |
722 |
|
BitstreamPutBit(bs, 0); // vbv_parameters (0=not given) |
723 |
|
} else |
724 |
|
#endif |
725 |
|
{ |
726 |
|
BitstreamPutBit(bs, 1); // vol_control_parameters |
727 |
|
BitstreamPutBits(bs, 1, 2); // chroma_format 1="4:2:0" |
728 |
|
BitstreamPutBit(bs, 1); // low_delay |
729 |
|
BitstreamPutBit(bs, 0); // vbv_parameters (0=not given) |
730 |
|
} |
731 |
|
|
732 |
|
|
733 |
BitstreamPutBits(bs, 0, 2); // video_object_layer_shape (0=rectangular) |
BitstreamPutBits(bs, 0, 2); // video_object_layer_shape (0=rectangular) |
734 |
|
|
735 |
WRITE_MARKER(); |
WRITE_MARKER(); |
739 |
25fps res=25 inc=1 |
25fps res=25 inc=1 |
740 |
29.97fps res=30000 inc=1001 |
29.97fps res=30000 inc=1001 |
741 |
*/ |
*/ |
742 |
|
#ifdef BFRAMES |
743 |
|
BitstreamPutBits(bs, pParam->fbase, 16); |
744 |
|
#else |
745 |
BitstreamPutBits(bs, 2, 16); |
BitstreamPutBits(bs, 2, 16); |
746 |
|
#endif |
747 |
|
|
748 |
WRITE_MARKER(); |
WRITE_MARKER(); |
749 |
|
|
750 |
// fixed_vop_rate |
#ifdef BFRAMES |
751 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 1); // fixed_vop_rate = 1 |
752 |
|
BitstreamPutBits(bs, pParam->fincr, log2bin(pParam->fbase)); // fixed_vop_time_increment |
753 |
// fixed_time_increment: value=nth_of_sec, nbits = log2(resolution) |
#else |
754 |
// BitstreamPutBits(bs, 0, 15); |
BitstreamPutBit(bs, 0); // fixed_vop_rate = 0 |
755 |
|
#endif |
756 |
|
|
757 |
WRITE_MARKER(); |
WRITE_MARKER(); |
758 |
BitstreamPutBits(bs, width, 13); // width |
BitstreamPutBits(bs, pParam->width, 13); // width |
759 |
WRITE_MARKER(); |
WRITE_MARKER(); |
760 |
BitstreamPutBits(bs, height, 13); // height |
BitstreamPutBits(bs, pParam->height, 13); // height |
761 |
WRITE_MARKER(); |
WRITE_MARKER(); |
762 |
|
|
763 |
BitstreamPutBit(bs, 0); // interlace |
BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING); // interlace |
764 |
BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) |
BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) |
765 |
BitstreamPutBit(bs, 0); // sprite_enable |
BitstreamPutBit(bs, 0); // sprite_enable |
766 |
BitstreamPutBit(bs, 0); // not_in_bit |
BitstreamPutBit(bs, 0); // not_in_bit |
767 |
|
|
768 |
// quant_type 0=h.263 1=mpeg4(quantizer tables) |
// quant_type 0=h.263 1=mpeg4(quantizer tables) |
769 |
BitstreamPutBit(bs, quant_type); |
BitstreamPutBit(bs, pParam->m_quant_type); |
|
/* |
|
|
if (quant_type) |
|
|
{ |
|
|
BitstreamPutBit(bs, qmatrix->custom_intra); // load_intra_quant_mat |
|
|
if (qmatrix->custom_intra) |
|
|
{ |
|
|
bs_put_matrix(bs, qmatrix->intra); |
|
|
} |
|
770 |
|
|
771 |
BitstreamPutBit(bs, qmatrix->custom_inter); // load_inter_quant_mat |
if (pParam->m_quant_type) { |
772 |
if (qmatrix->custom_inter) |
BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat |
773 |
{ |
if (get_intra_matrix_status()) { |
774 |
bs_put_matrix(bs, qmatrix->inter); |
bs_put_matrix(bs, get_intra_matrix()); |
775 |
} |
} |
776 |
|
|
777 |
|
BitstreamPutBit(bs, get_inter_matrix_status()); // load_inter_quant_mat |
778 |
|
if (get_inter_matrix_status()) { |
779 |
|
bs_put_matrix(bs, get_inter_matrix()); |
780 |
} |
} |
781 |
*/ |
|
|
if (quant_type) |
|
|
{ |
|
|
BitstreamPutBit(bs, 0); // load_intra_quant_mat |
|
|
BitstreamPutBit(bs, 0); // load_inter_quant_mat |
|
782 |
} |
} |
783 |
|
|
784 |
BitstreamPutBit(bs, 1); // complexity_estimation_disable |
BitstreamPutBit(bs, 1); // complexity_estimation_disable |
796 |
time_inc = nth of a second since last resync |
time_inc = nth of a second since last resync |
797 |
(decoder uses these values to determine precise time since last resync) |
(decoder uses these values to determine precise time since last resync) |
798 |
*/ |
*/ |
799 |
void BitstreamWriteVopHeader(Bitstream * const bs, |
void |
800 |
VOP_TYPE prediction_type, |
BitstreamWriteVopHeader(Bitstream * const bs, |
801 |
const int rounding_type, |
const MBParam * pParam, |
802 |
const uint32_t quant, |
const FRAMEINFO * frame, |
803 |
const uint32_t fcode) |
int vop_coded) |
804 |
{ |
{ |
805 |
|
#ifdef BFRAMES |
806 |
|
uint32_t i; |
807 |
|
#endif |
808 |
BitstreamPad(bs); |
BitstreamPad(bs); |
809 |
BitstreamPutBits(bs, VOP_START_CODE, 32); |
BitstreamPutBits(bs, VOP_START_CODE, 32); |
810 |
|
|
811 |
BitstreamPutBits(bs, prediction_type, 2); |
BitstreamPutBits(bs, frame->coding_type, 2); |
812 |
|
|
813 |
// time_base = 0 write n x PutBit(1), PutBit(0) |
// time_base = 0 write n x PutBit(1), PutBit(0) |
814 |
|
#ifdef BFRAMES |
815 |
|
for (i = 0; i < frame->seconds; i++) { |
816 |
|
BitstreamPutBit(bs, 1); |
817 |
|
} |
818 |
|
BitstreamPutBit(bs, 0); |
819 |
|
#else |
820 |
BitstreamPutBits(bs, 0, 1); |
BitstreamPutBits(bs, 0, 1); |
821 |
|
#endif |
822 |
|
|
823 |
WRITE_MARKER(); |
WRITE_MARKER(); |
824 |
|
|
825 |
// time_increment: value=nth_of_sec, nbits = log2(resolution) |
// time_increment: value=nth_of_sec, nbits = log2(resolution) |
826 |
|
#ifdef BFRAMES |
827 |
|
BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase)); |
828 |
|
/*DPRINTF("[%i:%i] %c\n", frame->seconds, frame->ticks, |
829 |
|
frame->coding_type == I_VOP ? 'I' : frame->coding_type == |
830 |
|
P_VOP ? 'P' : 'B');*/ |
831 |
|
#else |
832 |
BitstreamPutBits(bs, 1, 1); |
BitstreamPutBits(bs, 1, 1); |
833 |
|
#endif |
834 |
|
|
835 |
WRITE_MARKER(); |
WRITE_MARKER(); |
836 |
|
|
837 |
|
if (!vop_coded) { |
838 |
|
BitstreamPutBits(bs, 0, 1); |
839 |
|
return; |
840 |
|
} |
841 |
|
|
842 |
BitstreamPutBits(bs, 1, 1); // vop_coded |
BitstreamPutBits(bs, 1, 1); // vop_coded |
843 |
|
|
844 |
if (prediction_type != I_VOP) |
if (frame->coding_type == P_VOP) |
845 |
BitstreamPutBits(bs, rounding_type, 1); |
BitstreamPutBits(bs, frame->rounding_type, 1); |
846 |
|
|
847 |
BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold |
BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold |
848 |
|
|
849 |
BitstreamPutBits(bs, quant, 5); // quantizer |
if (frame->global_flags & XVID_INTERLACING) { |
850 |
|
BitstreamPutBit(bs, 1); // top field first |
851 |
|
BitstreamPutBit(bs, 0); // alternate vertical scan |
852 |
|
} |
853 |
|
|
854 |
|
BitstreamPutBits(bs, frame->quant, 5); // quantizer |
855 |
|
|
856 |
|
if (frame->coding_type != I_VOP) |
857 |
|
BitstreamPutBits(bs, frame->fcode, 3); // forward_fixed_code |
858 |
|
|
859 |
|
if (frame->coding_type == B_VOP) |
860 |
|
BitstreamPutBits(bs, frame->bcode, 3); // backward_fixed_code |
861 |
|
|
862 |
|
} |
863 |
|
|
864 |
|
|
865 |
|
void |
866 |
|
BitstreamWriteUserData(Bitstream * const bs, |
867 |
|
uint8_t * data, |
868 |
|
const int length) |
869 |
|
{ |
870 |
|
int i; |
871 |
|
|
872 |
|
BitstreamPad(bs); |
873 |
|
BitstreamPutBits(bs, USERDATA_START_CODE, 32); |
874 |
|
|
875 |
|
for (i = 0; i < length; i++) { |
876 |
|
BitstreamPutBits(bs, data[i], 8); |
877 |
|
} |
878 |
|
|
|
if (prediction_type != I_VOP) |
|
|
BitstreamPutBits(bs, fcode, 3); // fixed_code = [1,4] |
|
879 |
} |
} |