Parent Directory
|
Revision Log
Revision 163 -
(view)
(download)
Original Path: trunk/xvidcore/src/bitstream/bitstream.c
1 : | Isibaar | 3 | /****************************************************************************** |
2 : | * * | ||
3 : | * This file is part of XviD, a free MPEG-4 video encoder/decoder * | ||
4 : | * * | ||
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 * | ||
7 : | * software module in hardware or software products are advised that its * | ||
8 : | * use may infringe existing patents or copyrights, and any such use * | ||
9 : | * would be at such party's own risk. The original developer of this * | ||
10 : | * software module and his/her company, and subsequent editors and their * | ||
11 : | * companies, will have no liability for use of this software or * | ||
12 : | * modifications or derivatives thereof. * | ||
13 : | * * | ||
14 : | * XviD is free software; you can redistribute it and/or modify it * | ||
15 : | * under the terms of the GNU General Public License as published by * | ||
16 : | * the Free Software Foundation; either version 2 of the License, or * | ||
17 : | * (at your option) any later version. * | ||
18 : | * * | ||
19 : | * XviD is distributed in the hope that it will be useful, but * | ||
20 : | * WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
21 : | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
22 : | * GNU General Public License for more details. * | ||
23 : | * * | ||
24 : | * You should have received a copy of the GNU General Public License * | ||
25 : | * along with this program; if not, write to the Free Software * | ||
26 : | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | ||
27 : | * * | ||
28 : | ******************************************************************************/ | ||
29 : | |||
30 : | /****************************************************************************** | ||
31 : | * * | ||
32 : | * bitstream.c * | ||
33 : | * * | ||
34 : | * Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au> * | ||
35 : | * * | ||
36 : | * For more information visit the XviD homepage: http://www.xvid.org * | ||
37 : | * * | ||
38 : | ******************************************************************************/ | ||
39 : | |||
40 : | /****************************************************************************** | ||
41 : | * * | ||
42 : | * Revision history: * | ||
43 : | chenm001 | 124 | * * |
44 : | suxen_drol | 163 | * 06.05.2002 fixed fincr/fbase error * |
45 : | * 01.05.2002 added BVOP support to BitstreamWriteVopHeader * | ||
46 : | chenm001 | 124 | * 15.04.2002 rewrite log2bin use asm386 By MinChen <chenm001@163.com> * |
47 : | * 26.03.2002 interlacing support * | ||
48 : | Isibaar | 3 | * 03.03.2002 qmatrix writing * |
49 : | * 03.03.2002 merged BITREADER and BITWRITER * | ||
50 : | * 30.02.2002 intra_dc_threshold support * | ||
51 : | * 04.12.2001 support for additional headers * | ||
52 : | * 16.12.2001 inital version * | ||
53 : | * * | ||
54 : | ******************************************************************************/ | ||
55 : | |||
56 : | |||
57 : | #include "bitstream.h" | ||
58 : | #include "zigzag.h" | ||
59 : | Isibaar | 4 | #include "../quant/quant_matrix.h" |
60 : | Isibaar | 3 | |
61 : | chenm001 | 124 | |
62 : | suxen_drol | 163 | static uint32_t __inline log2bin(uint32_t value) |
63 : | Isibaar | 3 | { |
64 : | chenm001 | 124 | /* Changed by Chenm001 */ |
65 : | #ifndef WIN32 | ||
66 : | Isibaar | 3 | int n = 0; |
67 : | while (value) | ||
68 : | { | ||
69 : | value >>= 1; | ||
70 : | n++; | ||
71 : | } | ||
72 : | return n; | ||
73 : | chenm001 | 124 | #else |
74 : | __asm{ | ||
75 : | bsr eax,value | ||
76 : | inc eax | ||
77 : | } | ||
78 : | #endif | ||
79 : | Isibaar | 3 | } |
80 : | |||
81 : | |||
82 : | static const uint32_t intra_dc_threshold_table[] = | ||
83 : | { | ||
84 : | 32, /* never use */ | ||
85 : | 13, | ||
86 : | 15, | ||
87 : | 17, | ||
88 : | 19, | ||
89 : | 21, | ||
90 : | 23, | ||
91 : | 1, | ||
92 : | }; | ||
93 : | |||
94 : | |||
95 : | Isibaar | 4 | void bs_get_matrix(Bitstream * bs, uint8_t * matrix) |
96 : | { | ||
97 : | int i = 0; | ||
98 : | int last, value = 0; | ||
99 : | |||
100 : | do | ||
101 : | { | ||
102 : | last = value; | ||
103 : | value = BitstreamGetBits(bs, 8); | ||
104 : | matrix[ scan_tables[0][i++] ] = value; | ||
105 : | } | ||
106 : | while (value != 0 && i < 64); | ||
107 : | |||
108 : | while (i < 64) | ||
109 : | { | ||
110 : | matrix[ scan_tables[0][i++] ] = last; | ||
111 : | } | ||
112 : | } | ||
113 : | |||
114 : | Isibaar | 3 | /* |
115 : | decode headers | ||
116 : | returns coding_type, or -1 if error | ||
117 : | */ | ||
118 : | |||
119 : | chenm001 | 156 | int BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding, uint32_t * quant, uint32_t * fcode_forward, uint32_t * fcode_backward, uint32_t * intra_dc_threshold) |
120 : | Isibaar | 3 | { |
121 : | uint32_t vol_ver_id; | ||
122 : | chenm001 | 156 | static uint32_t time_increment_resolution; |
123 : | Isibaar | 3 | uint32_t coding_type; |
124 : | uint32_t start_code; | ||
125 : | chenm001 | 156 | uint32_t time_incr=0; |
126 : | int32_t time_increment; | ||
127 : | |||
128 : | Isibaar | 3 | do |
129 : | { | ||
130 : | BitstreamByteAlign(bs); | ||
131 : | start_code = BitstreamShowBits(bs, 32); | ||
132 : | |||
133 : | if (start_code == VISOBJSEQ_START_CODE) | ||
134 : | { | ||
135 : | // DEBUG("visual_object_sequence"); | ||
136 : | BitstreamSkip(bs, 32); // visual_object_sequence_start_code | ||
137 : | BitstreamSkip(bs, 8); // profile_and_level_indication | ||
138 : | } | ||
139 : | else if (start_code == VISOBJSEQ_STOP_CODE) | ||
140 : | { | ||
141 : | BitstreamSkip(bs, 32); // visual_object_sequence_stop_code | ||
142 : | } | ||
143 : | else if (start_code == VISOBJ_START_CODE) | ||
144 : | { | ||
145 : | // DEBUG("visual_object"); | ||
146 : | BitstreamSkip(bs,32); // visual_object_start_code | ||
147 : | if (BitstreamGetBit(bs)) // is_visual_object_identified | ||
148 : | { | ||
149 : | vol_ver_id = BitstreamGetBits(bs,4); // visual_object_ver_id | ||
150 : | BitstreamSkip(bs, 3); // visual_object_priority | ||
151 : | } | ||
152 : | else | ||
153 : | { | ||
154 : | vol_ver_id = 1; | ||
155 : | } | ||
156 : | |||
157 : | if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) // visual_object_type | ||
158 : | { | ||
159 : | DEBUG("visual_object_type != video"); | ||
160 : | return -1; | ||
161 : | } | ||
162 : | BitstreamSkip(bs, 4); | ||
163 : | |||
164 : | // video_signal_type | ||
165 : | |||
166 : | if (BitstreamGetBit(bs)) // video_signal_type | ||
167 : | { | ||
168 : | DEBUG("+ video_signal_type"); | ||
169 : | BitstreamSkip(bs, 3); // video_format | ||
170 : | BitstreamSkip(bs, 1); // video_range | ||
171 : | if (BitstreamGetBit(bs)) // color_description | ||
172 : | { | ||
173 : | DEBUG("+ color_description"); | ||
174 : | BitstreamSkip(bs, 8); // color_primaries | ||
175 : | BitstreamSkip(bs, 8); // transfer_characteristics | ||
176 : | BitstreamSkip(bs, 8); // matrix_coefficients | ||
177 : | } | ||
178 : | } | ||
179 : | } | ||
180 : | else if ((start_code & ~0x1f) == VIDOBJ_START_CODE) | ||
181 : | { | ||
182 : | BitstreamSkip(bs, 32); // video_object_start_code | ||
183 : | } | ||
184 : | else if ((start_code & ~0xf) == VIDOBJLAY_START_CODE) | ||
185 : | { | ||
186 : | // DEBUG("video_object_layer"); | ||
187 : | BitstreamSkip(bs, 32); // video_object_layer_start_code | ||
188 : | |||
189 : | chenm001 | 156 | BitstreamSkip(bs, 1); // random_accessible_vol |
190 : | Isibaar | 3 | |
191 : | // video_object_type_indication | ||
192 : | if (BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_SIMPLE && | ||
193 : | BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_CORE && | ||
194 : | BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_MAIN && | ||
195 : | BitstreamShowBits(bs, 8) != 0) // BUGGY DIVX | ||
196 : | { | ||
197 : | DEBUG1("video_object_type_indication not supported", BitstreamShowBits(bs, 8)); | ||
198 : | return -1; | ||
199 : | } | ||
200 : | BitstreamSkip(bs, 8); | ||
201 : | |||
202 : | |||
203 : | if (BitstreamGetBit(bs)) // is_object_layer_identifier | ||
204 : | { | ||
205 : | DEBUG("+ is_object_layer_identifier"); | ||
206 : | vol_ver_id = BitstreamGetBits(bs,4); // video_object_layer_verid | ||
207 : | BitstreamSkip(bs, 3); // video_object_layer_priority | ||
208 : | } | ||
209 : | else | ||
210 : | { | ||
211 : | vol_ver_id = 1; | ||
212 : | } | ||
213 : | //DEBUGI("vol_ver_id", vol_ver_id); | ||
214 : | |||
215 : | if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR) // aspect_ratio_info | ||
216 : | { | ||
217 : | DEBUG("+ aspect_ratio_info"); | ||
218 : | BitstreamSkip(bs, 8); // par_width | ||
219 : | BitstreamSkip(bs, 8); // par_height | ||
220 : | } | ||
221 : | |||
222 : | chenm001 | 156 | if (BitstreamGetBit(bs)) // vol_control_parameters |
223 : | Isibaar | 3 | { |
224 : | DEBUG("+ vol_control_parameters"); | ||
225 : | BitstreamSkip(bs, 2); // chroma_format | ||
226 : | BitstreamSkip(bs, 1); // low_delay | ||
227 : | if (BitstreamGetBit(bs)) // vbv_parameters | ||
228 : | { | ||
229 : | DEBUG("+ vbv_parameters"); | ||
230 : | BitstreamSkip(bs, 15); // first_half_bitrate | ||
231 : | READ_MARKER(); | ||
232 : | BitstreamSkip(bs, 15); // latter_half_bitrate | ||
233 : | READ_MARKER(); | ||
234 : | BitstreamSkip(bs, 15); // first_half_vbv_buffer_size | ||
235 : | READ_MARKER(); | ||
236 : | BitstreamSkip(bs, 3); // latter_half_vbv_buffer_size | ||
237 : | BitstreamSkip(bs, 11); // first_half_vbv_occupancy | ||
238 : | READ_MARKER(); | ||
239 : | BitstreamSkip(bs, 15); // latter_half_vbv_occupancy | ||
240 : | READ_MARKER(); | ||
241 : | |||
242 : | } | ||
243 : | } | ||
244 : | |||
245 : | dec->shape = BitstreamGetBits(bs, 2); // video_object_layer_shape | ||
246 : | // DEBUG1("shape", dec->shape); | ||
247 : | |||
248 : | if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) | ||
249 : | { | ||
250 : | BitstreamSkip(bs, 4); // video_object_layer_shape_extension | ||
251 : | } | ||
252 : | |||
253 : | READ_MARKER(); | ||
254 : | |||
255 : | chenm001 | 156 | // *************************** for decode B-frame time *********************** |
256 : | time_increment_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution | ||
257 : | time_increment_resolution--; | ||
258 : | //DEBUG1("time_increment_resolution=",time_increment_resolution); | ||
259 : | if (time_increment_resolution > 0) | ||
260 : | Isibaar | 3 | { |
261 : | chenm001 | 156 | dec->time_inc_bits = log2bin(time_increment_resolution); |
262 : | Isibaar | 3 | } |
263 : | else | ||
264 : | { | ||
265 : | // dec->time_inc_bits = 0; | ||
266 : | |||
267 : | // for "old" xvid compatibility, set time_inc_bits = 1 | ||
268 : | dec->time_inc_bits = 1; | ||
269 : | } | ||
270 : | |||
271 : | READ_MARKER(); | ||
272 : | |||
273 : | if (BitstreamGetBit(bs)) // fixed_vop_rate | ||
274 : | { | ||
275 : | BitstreamSkip(bs, dec->time_inc_bits); // fixed_vop_time_increment | ||
276 : | } | ||
277 : | |||
278 : | if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) | ||
279 : | { | ||
280 : | |||
281 : | if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) | ||
282 : | { | ||
283 : | uint32_t width, height; | ||
284 : | |||
285 : | READ_MARKER(); | ||
286 : | width = BitstreamGetBits(bs, 13); // video_object_layer_width | ||
287 : | //DEBUGI("width", width); | ||
288 : | READ_MARKER(); | ||
289 : | height = BitstreamGetBits(bs, 13); // video_object_layer_height | ||
290 : | //DEBUGI("height", height); | ||
291 : | READ_MARKER(); | ||
292 : | |||
293 : | if (width != dec->width || height != dec->height) | ||
294 : | { | ||
295 : | DEBUG("FATAL: video dimension discrepancy ***"); | ||
296 : | DEBUG2("bitstream width/height", width, height); | ||
297 : | DEBUG2("param width/height", dec->width, dec->height); | ||
298 : | return -1; | ||
299 : | } | ||
300 : | |||
301 : | } | ||
302 : | h | 69 | |
303 : | canard | 74 | if ((dec->interlacing = BitstreamGetBit(bs))) |
304 : | Isibaar | 3 | { |
305 : | h | 69 | DEBUG("vol: interlacing"); |
306 : | Isibaar | 3 | } |
307 : | |||
308 : | if (!BitstreamGetBit(bs)) // obmc_disable | ||
309 : | { | ||
310 : | DEBUG("IGNORED/TODO: !obmc_disable"); | ||
311 : | // TODO | ||
312 : | // fucking divx4.02 has this enabled | ||
313 : | } | ||
314 : | |||
315 : | if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2))) // sprite_enable | ||
316 : | { | ||
317 : | DEBUG("sprite_enable; not supported"); | ||
318 : | return -1; | ||
319 : | } | ||
320 : | |||
321 : | if (vol_ver_id != 1 && dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) | ||
322 : | { | ||
323 : | BitstreamSkip(bs, 1); // sadct_disable | ||
324 : | } | ||
325 : | |||
326 : | if (BitstreamGetBit(bs)) // not_8_bit | ||
327 : | { | ||
328 : | DEBUG("+ not_8_bit [IGNORED/TODO]"); | ||
329 : | dec->quant_bits = BitstreamGetBits(bs, 4); // quant_precision | ||
330 : | BitstreamSkip(bs, 4); // bits_per_pixel | ||
331 : | } | ||
332 : | else | ||
333 : | { | ||
334 : | dec->quant_bits = 5; | ||
335 : | } | ||
336 : | |||
337 : | if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) | ||
338 : | { | ||
339 : | BitstreamSkip(bs, 1); // no_gray_quant_update | ||
340 : | BitstreamSkip(bs, 1); // composition_method | ||
341 : | BitstreamSkip(bs, 1); // linear_composition | ||
342 : | } | ||
343 : | |||
344 : | dec->quant_type = BitstreamGetBit(bs); // quant_type | ||
345 : | // DEBUG1("**** quant_type", dec->quant_type); | ||
346 : | |||
347 : | if (dec->quant_type) | ||
348 : | { | ||
349 : | if (BitstreamGetBit(bs)) // load_intra_quant_mat | ||
350 : | { | ||
351 : | Isibaar | 20 | uint8_t matrix[64]; |
352 : | Isibaar | 4 | bs_get_matrix(bs, matrix); |
353 : | set_intra_matrix(matrix); | ||
354 : | Isibaar | 3 | } |
355 : | Isibaar | 20 | else |
356 : | set_intra_matrix(get_default_intra_matrix()); | ||
357 : | Isibaar | 4 | |
358 : | Isibaar | 3 | if (BitstreamGetBit(bs)) // load_inter_quant_mat |
359 : | { | ||
360 : | Isibaar | 20 | uint8_t matrix[64]; |
361 : | Isibaar | 4 | bs_get_matrix(bs, matrix); |
362 : | set_inter_matrix(matrix); | ||
363 : | Isibaar | 3 | } |
364 : | Isibaar | 20 | else |
365 : | set_inter_matrix(get_default_inter_matrix()); | ||
366 : | Isibaar | 3 | |
367 : | if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) | ||
368 : | { | ||
369 : | // TODO | ||
370 : | DEBUG("TODO: grayscale matrix stuff"); | ||
371 : | return -1; | ||
372 : | } | ||
373 : | Isibaar | 4 | |
374 : | Isibaar | 3 | } |
375 : | |||
376 : | |||
377 : | if (vol_ver_id != 1) | ||
378 : | { | ||
379 : | dec->quarterpel = BitstreamGetBit(bs); // quarter_sampe | ||
380 : | if (dec->quarterpel) | ||
381 : | { | ||
382 : | DEBUG("IGNORED/TODO: quarter_sample"); | ||
383 : | } | ||
384 : | } | ||
385 : | else | ||
386 : | { | ||
387 : | dec->quarterpel = 0; | ||
388 : | } | ||
389 : | |||
390 : | if (!BitstreamGetBit(bs)) // complexity_estimation_disable | ||
391 : | { | ||
392 : | DEBUG("TODO: complexity_estimation header"); | ||
393 : | // TODO | ||
394 : | return -1; | ||
395 : | } | ||
396 : | |||
397 : | if (!BitstreamGetBit(bs)) // resync_marker_disable | ||
398 : | { | ||
399 : | DEBUG("IGNORED/TODO: !resync_marker_disable"); | ||
400 : | // TODO | ||
401 : | } | ||
402 : | |||
403 : | if (BitstreamGetBit(bs)) // data_partitioned | ||
404 : | { | ||
405 : | DEBUG("+ data_partitioned"); | ||
406 : | BitstreamSkip(bs, 1); // reversible_vlc | ||
407 : | } | ||
408 : | |||
409 : | if (vol_ver_id != 1) | ||
410 : | { | ||
411 : | if (BitstreamGetBit(bs)) // newpred_enable | ||
412 : | { | ||
413 : | DEBUG("+ newpred_enable"); | ||
414 : | BitstreamSkip(bs, 2); // requested_upstream_message_type | ||
415 : | BitstreamSkip(bs, 1); // newpred_segment_type | ||
416 : | } | ||
417 : | if (BitstreamGetBit(bs)) // reduced_resolution_vop_enable | ||
418 : | { | ||
419 : | DEBUG("TODO: reduced_resolution_vop_enable"); | ||
420 : | // TODO | ||
421 : | return -1; | ||
422 : | } | ||
423 : | } | ||
424 : | |||
425 : | chenm001 | 156 | if ((dec->scalability=BitstreamGetBit(bs))) // scalability |
426 : | Isibaar | 3 | { |
427 : | // TODO | ||
428 : | DEBUG("TODO: scalability"); | ||
429 : | return -1; | ||
430 : | } | ||
431 : | } | ||
432 : | else // dec->shape == BINARY_ONLY | ||
433 : | { | ||
434 : | if (vol_ver_id != 1) | ||
435 : | { | ||
436 : | if (BitstreamGetBit(bs)) // scalability | ||
437 : | { | ||
438 : | // TODO | ||
439 : | DEBUG("TODO: scalability"); | ||
440 : | return -1; | ||
441 : | } | ||
442 : | } | ||
443 : | BitstreamSkip(bs, 1); // resync_marker_disable | ||
444 : | |||
445 : | } | ||
446 : | |||
447 : | } | ||
448 : | else if (start_code == GRPOFVOP_START_CODE) | ||
449 : | { | ||
450 : | // DEBUG("group_of_vop"); | ||
451 : | BitstreamSkip(bs, 32); | ||
452 : | { | ||
453 : | int hours, minutes, seconds; | ||
454 : | hours = BitstreamGetBits(bs, 5); | ||
455 : | minutes = BitstreamGetBits(bs, 6); | ||
456 : | READ_MARKER(); | ||
457 : | seconds = BitstreamGetBits(bs, 6); | ||
458 : | // DEBUG3("hms", hours, minutes, seconds); | ||
459 : | } | ||
460 : | BitstreamSkip(bs, 1); // closed_gov | ||
461 : | BitstreamSkip(bs, 1); // broken_link | ||
462 : | } | ||
463 : | else if (start_code == VOP_START_CODE) | ||
464 : | { | ||
465 : | // DEBUG("vop_start_code"); | ||
466 : | BitstreamSkip(bs, 32); // vop_start_code | ||
467 : | |||
468 : | coding_type = BitstreamGetBits(bs, 2); // vop_coding_type | ||
469 : | //DEBUG1("coding_type", coding_type); | ||
470 : | |||
471 : | chenm001 | 156 | // *************************** for decode B-frame time *********************** |
472 : | while (BitstreamGetBit(bs) != 0) // time_base | ||
473 : | time_incr++; | ||
474 : | Isibaar | 3 | |
475 : | READ_MARKER(); | ||
476 : | |||
477 : | //DEBUG1("time_inc_bits", dec->time_inc_bits); | ||
478 : | //DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits)); | ||
479 : | if (dec->time_inc_bits) | ||
480 : | { | ||
481 : | chenm001 | 156 | //BitstreamSkip(bs, dec->time_inc_bits); // vop_time_increment |
482 : | time_increment = (BitstreamGetBits(bs, dec->time_inc_bits)); // vop_time_increment | ||
483 : | Isibaar | 3 | } |
484 : | chenm001 | 156 | if(coding_type != B_VOP){ |
485 : | dec->last_time_base = dec->time_base; | ||
486 : | dec->time_base += time_incr; | ||
487 : | dec->time = dec->time_base*time_increment_resolution + time_increment; | ||
488 : | dec->time_pp= (uint32_t)(dec->time - dec->last_non_b_time); | ||
489 : | dec->last_non_b_time= dec->time; | ||
490 : | }else{ | ||
491 : | dec->time = (dec->last_time_base + time_incr)*time_increment_resolution + time_increment; | ||
492 : | dec->time_bp= (uint32_t)(dec->last_non_b_time - dec->time); | ||
493 : | } | ||
494 : | //DEBUG1("time_increment=",time_increment); | ||
495 : | Isibaar | 3 | |
496 : | READ_MARKER(); | ||
497 : | |||
498 : | if (!BitstreamGetBit(bs)) // vop_coded | ||
499 : | { | ||
500 : | return N_VOP; | ||
501 : | } | ||
502 : | |||
503 : | /* if (newpred_enable) | ||
504 : | { | ||
505 : | } | ||
506 : | */ | ||
507 : | |||
508 : | chenm001 | 156 | // fix a little bug by MinChen <chenm002@163.com> |
509 : | if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) && (coding_type == P_VOP)) | ||
510 : | Isibaar | 3 | { |
511 : | *rounding = BitstreamGetBit(bs); // rounding_type | ||
512 : | //DEBUG1("rounding", *rounding); | ||
513 : | } | ||
514 : | |||
515 : | /* if (reduced_resolution_enable) | ||
516 : | { | ||
517 : | } | ||
518 : | */ | ||
519 : | |||
520 : | if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) | ||
521 : | { | ||
522 : | uint32_t width, height; | ||
523 : | uint32_t horiz_mc_ref, vert_mc_ref; | ||
524 : | |||
525 : | width = BitstreamGetBits(bs, 13); | ||
526 : | READ_MARKER(); | ||
527 : | height = BitstreamGetBits(bs, 13); | ||
528 : | READ_MARKER(); | ||
529 : | horiz_mc_ref = BitstreamGetBits(bs, 13); | ||
530 : | READ_MARKER(); | ||
531 : | vert_mc_ref = BitstreamGetBits(bs, 13); | ||
532 : | READ_MARKER(); | ||
533 : | |||
534 : | // DEBUG2("vop_width/height", width, height); | ||
535 : | // DEBUG2("ref ", horiz_mc_ref, vert_mc_ref); | ||
536 : | |||
537 : | BitstreamSkip(bs, 1); // change_conv_ratio_disable | ||
538 : | if (BitstreamGetBit(bs)) // vop_constant_alpha | ||
539 : | { | ||
540 : | BitstreamSkip(bs, 8); // vop_constant_alpha_value | ||
541 : | } | ||
542 : | } | ||
543 : | |||
544 : | |||
545 : | if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) | ||
546 : | { | ||
547 : | // intra_dc_vlc_threshold | ||
548 : | *intra_dc_threshold = intra_dc_threshold_table[ BitstreamGetBits(bs,3) ]; | ||
549 : | |||
550 : | h | 69 | if (dec->interlacing) |
551 : | { | ||
552 : | canard | 74 | if ((dec->top_field_first = BitstreamGetBit(bs))) |
553 : | Isibaar | 3 | { |
554 : | h | 69 | DEBUG("vop: top_field_first"); |
555 : | } | ||
556 : | canard | 74 | if ((dec->alternate_vertical_scan = BitstreamGetBit(bs))) |
557 : | h | 69 | { |
558 : | DEBUG("vop: alternate_vertical_scan"); | ||
559 : | } | ||
560 : | } | ||
561 : | Isibaar | 3 | } |
562 : | |||
563 : | Isibaar | 157 | if((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1) // vop_quant |
564 : | *quant = 1; | ||
565 : | |||
566 : | Isibaar | 3 | //DEBUG1("quant", *quant); |
567 : | |||
568 : | if (coding_type != I_VOP) | ||
569 : | { | ||
570 : | chenm001 | 156 | *fcode_forward = BitstreamGetBits(bs, 3); // fcode_forward |
571 : | Isibaar | 3 | } |
572 : | |||
573 : | if (coding_type == B_VOP) | ||
574 : | { | ||
575 : | chenm001 | 156 | *fcode_backward = BitstreamGetBits(bs, 3); // fcode_backward |
576 : | Isibaar | 3 | } |
577 : | chenm001 | 156 | if (!dec->scalability){ |
578 : | if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) && (coding_type != I_VOP)){ | ||
579 : | BitstreamSkip(bs, 1); // vop_shape_coding_type | ||
580 : | } | ||
581 : | } | ||
582 : | Isibaar | 3 | return coding_type; |
583 : | } | ||
584 : | else if (start_code == USERDATA_START_CODE) | ||
585 : | { | ||
586 : | // DEBUG("user_data"); | ||
587 : | BitstreamSkip(bs, 32); // user_data_start_code | ||
588 : | } | ||
589 : | else // start_code == ? | ||
590 : | { | ||
591 : | if (BitstreamShowBits(bs, 24) == 0x000001) | ||
592 : | { | ||
593 : | DEBUG1("*** WARNING: unknown start_code", BitstreamShowBits(bs, 32)); | ||
594 : | } | ||
595 : | BitstreamSkip(bs, 8); | ||
596 : | } | ||
597 : | } | ||
598 : | while ((BitstreamPos(bs) >> 3) < bs->length); | ||
599 : | |||
600 : | DEBUG("*** WARNING: no vop_start_code found"); | ||
601 : | return -1; /* ignore it */ | ||
602 : | } | ||
603 : | |||
604 : | |||
605 : | /* write custom quant matrix */ | ||
606 : | |||
607 : | Isibaar | 4 | static void bs_put_matrix(Bitstream * bs, const int16_t *matrix) |
608 : | Isibaar | 3 | { |
609 : | int i, j; | ||
610 : | const int last = matrix[scan_tables[0][63]]; | ||
611 : | |||
612 : | for (j = 63; j >= 0 && matrix[scan_tables[0][j - 1]] == last; j--) ; | ||
613 : | |||
614 : | for (i = 0; i <= j; i++) | ||
615 : | { | ||
616 : | BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8); | ||
617 : | } | ||
618 : | |||
619 : | if (j < 63) | ||
620 : | { | ||
621 : | BitstreamPutBits(bs, 0, 8); | ||
622 : | } | ||
623 : | } | ||
624 : | |||
625 : | |||
626 : | /* | ||
627 : | write vol header | ||
628 : | */ | ||
629 : | void BitstreamWriteVolHeader(Bitstream * const bs, | ||
630 : | suxen_drol | 136 | const MBParam * pParam, const FRAMEINFO * frame) |
631 : | Isibaar | 3 | { |
632 : | // video object_start_code & vo_id | ||
633 : | BitstreamPad(bs); | ||
634 : | BitstreamPutBits(bs, VO_START_CODE, 27); | ||
635 : | BitstreamPutBits(bs, 0, 5); | ||
636 : | |||
637 : | // video_object_layer_start_code & vol_id | ||
638 : | BitstreamPutBits(bs, VOL_START_CODE, 28); | ||
639 : | BitstreamPutBits(bs, 0, 4); | ||
640 : | |||
641 : | BitstreamPutBit(bs, 0); // random_accessible_vol | ||
642 : | BitstreamPutBits(bs, 0, 8); // video_object_type_indication | ||
643 : | BitstreamPutBit(bs, 0); // is_object_layer_identified (0=not given) | ||
644 : | BitstreamPutBits(bs, 1, 4); // aspect_ratio_info (1=1:1) | ||
645 : | BitstreamPutBit(bs, 0); // vol_control_parameters (0=not given) | ||
646 : | BitstreamPutBits(bs, 0, 2); // video_object_layer_shape (0=rectangular) | ||
647 : | |||
648 : | WRITE_MARKER(); | ||
649 : | |||
650 : | /* time_increment_resolution; ignored by current decore versions | ||
651 : | eg. 2fps res=2 inc=1 | ||
652 : | 25fps res=25 inc=1 | ||
653 : | 29.97fps res=30000 inc=1001 | ||
654 : | */ | ||
655 : | suxen_drol | 163 | #ifdef BFRAMES |
656 : | BitstreamPutBits(bs, pParam->fbase, 16); | ||
657 : | #else | ||
658 : | Isibaar | 3 | BitstreamPutBits(bs, 2, 16); |
659 : | suxen_drol | 163 | #endif |
660 : | Isibaar | 3 | |
661 : | WRITE_MARKER(); | ||
662 : | |||
663 : | // fixed_vop_rate | ||
664 : | BitstreamPutBit(bs, 0); | ||
665 : | |||
666 : | // fixed_time_increment: value=nth_of_sec, nbits = log2(resolution) | ||
667 : | // BitstreamPutBits(bs, 0, 15); | ||
668 : | |||
669 : | WRITE_MARKER(); | ||
670 : | h | 69 | BitstreamPutBits(bs, pParam->width, 13); // width |
671 : | Isibaar | 3 | WRITE_MARKER(); |
672 : | h | 69 | BitstreamPutBits(bs, pParam->height, 13); // height |
673 : | Isibaar | 3 | WRITE_MARKER(); |
674 : | |||
675 : | suxen_drol | 136 | BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING); // interlace |
676 : | Isibaar | 3 | BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) |
677 : | BitstreamPutBit(bs, 0); // sprite_enable | ||
678 : | BitstreamPutBit(bs, 0); // not_in_bit | ||
679 : | |||
680 : | // quant_type 0=h.263 1=mpeg4(quantizer tables) | ||
681 : | suxen_drol | 136 | BitstreamPutBit(bs, pParam->m_quant_type); |
682 : | Isibaar | 4 | |
683 : | suxen_drol | 136 | if (pParam->m_quant_type) |
684 : | Isibaar | 3 | { |
685 : | Isibaar | 4 | BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat |
686 : | if (get_intra_matrix_status()) | ||
687 : | Isibaar | 3 | { |
688 : | Isibaar | 4 | bs_put_matrix(bs, get_intra_matrix()); |
689 : | Isibaar | 3 | } |
690 : | |||
691 : | Isibaar | 4 | BitstreamPutBit(bs, get_inter_matrix_status()); // load_inter_quant_mat |
692 : | if (get_inter_matrix_status()) | ||
693 : | Isibaar | 3 | { |
694 : | Isibaar | 4 | bs_put_matrix(bs, get_inter_matrix()); |
695 : | Isibaar | 3 | } |
696 : | |||
697 : | } | ||
698 : | |||
699 : | BitstreamPutBit(bs, 1); // complexity_estimation_disable | ||
700 : | BitstreamPutBit(bs, 1); // resync_marker_disable | ||
701 : | BitstreamPutBit(bs, 0); // data_partitioned | ||
702 : | BitstreamPutBit(bs, 0); // scalability | ||
703 : | } | ||
704 : | |||
705 : | |||
706 : | /* | ||
707 : | write vop header | ||
708 : | |||
709 : | NOTE: doesnt handle bother with time_base & time_inc | ||
710 : | time_base = n seconds since last resync (eg. last iframe) | ||
711 : | time_inc = nth of a second since last resync | ||
712 : | (decoder uses these values to determine precise time since last resync) | ||
713 : | */ | ||
714 : | void BitstreamWriteVopHeader(Bitstream * const bs, | ||
715 : | suxen_drol | 136 | const MBParam * pParam, |
716 : | const FRAMEINFO * frame) | ||
717 : | Isibaar | 3 | { |
718 : | suxen_drol | 152 | #ifdef BFRAMES |
719 : | uint32_t i; | ||
720 : | #endif | ||
721 : | Isibaar | 3 | BitstreamPad(bs); |
722 : | BitstreamPutBits(bs, VOP_START_CODE, 32); | ||
723 : | |||
724 : | suxen_drol | 136 | BitstreamPutBits(bs, frame->coding_type, 2); |
725 : | Isibaar | 3 | |
726 : | // time_base = 0 write n x PutBit(1), PutBit(0) | ||
727 : | suxen_drol | 152 | #ifdef BFRAMES |
728 : | for (i = 0; i < frame->seconds; i++) | ||
729 : | { | ||
730 : | BitstreamPutBit(bs, 1); | ||
731 : | } | ||
732 : | BitstreamPutBit(bs, 0); | ||
733 : | #else | ||
734 : | Isibaar | 3 | BitstreamPutBits(bs, 0, 1); |
735 : | suxen_drol | 152 | #endif |
736 : | Isibaar | 3 | |
737 : | WRITE_MARKER(); | ||
738 : | |||
739 : | // time_increment: value=nth_of_sec, nbits = log2(resolution) | ||
740 : | suxen_drol | 152 | #ifdef BFRAMES |
741 : | suxen_drol | 163 | BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase)); |
742 : | suxen_drol | 152 | dprintf("[%i:%i] %c\n", frame->seconds, frame->ticks, frame->coding_type == I_VOP ? 'I' : frame->coding_type == P_VOP ? 'P' : 'B'); |
743 : | #else | ||
744 : | Isibaar | 3 | BitstreamPutBits(bs, 1, 1); |
745 : | suxen_drol | 152 | #endif |
746 : | Isibaar | 3 | |
747 : | WRITE_MARKER(); | ||
748 : | |||
749 : | BitstreamPutBits(bs, 1, 1); // vop_coded | ||
750 : | |||
751 : | suxen_drol | 163 | if (frame->coding_type == P_VOP) |
752 : | suxen_drol | 136 | BitstreamPutBits(bs, frame->rounding_type, 1); |
753 : | Isibaar | 3 | |
754 : | BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold | ||
755 : | |||
756 : | suxen_drol | 136 | if (frame->global_flags & XVID_INTERLACING) |
757 : | h | 69 | { |
758 : | BitstreamPutBit(bs, 1); // top field first | ||
759 : | BitstreamPutBit(bs, 0); // alternate vertical scan | ||
760 : | } | ||
761 : | |||
762 : | suxen_drol | 136 | BitstreamPutBits(bs, frame->quant, 5); // quantizer |
763 : | Isibaar | 3 | |
764 : | suxen_drol | 136 | if (frame->coding_type != I_VOP) |
765 : | suxen_drol | 152 | BitstreamPutBits(bs, frame->fcode, 3); // forward_fixed_code |
766 : | |||
767 : | if (frame->coding_type == B_VOP) | ||
768 : | BitstreamPutBits(bs, frame->bcode, 3); // backward_fixed_code | ||
769 : | |||
770 : | Isibaar | 3 | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |