Parent Directory
|
Revision Log
Revision 1.4 - (view) (download)
1 : | Isibaar | 1.1 | /****************************************************************************** |
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 : | * * | ||
44 : | h | 1.4 | * 26.03.2002 interlacing support |
45 : | Isibaar | 1.1 | * 03.03.2002 qmatrix writing * |
46 : | * 03.03.2002 merged BITREADER and BITWRITER * | ||
47 : | * 30.02.2002 intra_dc_threshold support * | ||
48 : | * 04.12.2001 support for additional headers * | ||
49 : | * 16.12.2001 inital version * | ||
50 : | * * | ||
51 : | ******************************************************************************/ | ||
52 : | |||
53 : | |||
54 : | #include "bitstream.h" | ||
55 : | #include "zigzag.h" | ||
56 : | Isibaar | 1.2 | #include "../quant/quant_matrix.h" |
57 : | Isibaar | 1.1 | |
58 : | static int __inline log2bin(int value) | ||
59 : | { | ||
60 : | int n = 0; | ||
61 : | while (value) | ||
62 : | { | ||
63 : | value >>= 1; | ||
64 : | n++; | ||
65 : | } | ||
66 : | return n; | ||
67 : | } | ||
68 : | |||
69 : | |||
70 : | static const uint32_t intra_dc_threshold_table[] = | ||
71 : | { | ||
72 : | 32, /* never use */ | ||
73 : | 13, | ||
74 : | 15, | ||
75 : | 17, | ||
76 : | 19, | ||
77 : | 21, | ||
78 : | 23, | ||
79 : | 1, | ||
80 : | }; | ||
81 : | |||
82 : | |||
83 : | Isibaar | 1.2 | void bs_get_matrix(Bitstream * bs, uint8_t * matrix) |
84 : | { | ||
85 : | int i = 0; | ||
86 : | int last, value = 0; | ||
87 : | |||
88 : | do | ||
89 : | { | ||
90 : | last = value; | ||
91 : | value = BitstreamGetBits(bs, 8); | ||
92 : | matrix[ scan_tables[0][i++] ] = value; | ||
93 : | } | ||
94 : | while (value != 0 && i < 64); | ||
95 : | |||
96 : | while (i < 64) | ||
97 : | { | ||
98 : | matrix[ scan_tables[0][i++] ] = last; | ||
99 : | } | ||
100 : | } | ||
101 : | |||
102 : | Isibaar | 1.1 | /* |
103 : | decode headers | ||
104 : | returns coding_type, or -1 if error | ||
105 : | */ | ||
106 : | |||
107 : | int BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding, uint32_t * quant, uint32_t * fcode, uint32_t * intra_dc_threshold) | ||
108 : | { | ||
109 : | uint32_t vol_ver_id; | ||
110 : | uint32_t time_inc_resolution; | ||
111 : | uint32_t coding_type; | ||
112 : | uint32_t start_code; | ||
113 : | |||
114 : | do | ||
115 : | { | ||
116 : | BitstreamByteAlign(bs); | ||
117 : | start_code = BitstreamShowBits(bs, 32); | ||
118 : | |||
119 : | if (start_code == VISOBJSEQ_START_CODE) | ||
120 : | { | ||
121 : | // DEBUG("visual_object_sequence"); | ||
122 : | BitstreamSkip(bs, 32); // visual_object_sequence_start_code | ||
123 : | BitstreamSkip(bs, 8); // profile_and_level_indication | ||
124 : | } | ||
125 : | else if (start_code == VISOBJSEQ_STOP_CODE) | ||
126 : | { | ||
127 : | BitstreamSkip(bs, 32); // visual_object_sequence_stop_code | ||
128 : | } | ||
129 : | else if (start_code == VISOBJ_START_CODE) | ||
130 : | { | ||
131 : | // DEBUG("visual_object"); | ||
132 : | BitstreamSkip(bs,32); // visual_object_start_code | ||
133 : | if (BitstreamGetBit(bs)) // is_visual_object_identified | ||
134 : | { | ||
135 : | vol_ver_id = BitstreamGetBits(bs,4); // visual_object_ver_id | ||
136 : | BitstreamSkip(bs, 3); // visual_object_priority | ||
137 : | } | ||
138 : | else | ||
139 : | { | ||
140 : | vol_ver_id = 1; | ||
141 : | } | ||
142 : | |||
143 : | if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) // visual_object_type | ||
144 : | { | ||
145 : | DEBUG("visual_object_type != video"); | ||
146 : | return -1; | ||
147 : | } | ||
148 : | BitstreamSkip(bs, 4); | ||
149 : | |||
150 : | // video_signal_type | ||
151 : | |||
152 : | if (BitstreamGetBit(bs)) // video_signal_type | ||
153 : | { | ||
154 : | DEBUG("+ video_signal_type"); | ||
155 : | BitstreamSkip(bs, 3); // video_format | ||
156 : | BitstreamSkip(bs, 1); // video_range | ||
157 : | if (BitstreamGetBit(bs)) // color_description | ||
158 : | { | ||
159 : | DEBUG("+ color_description"); | ||
160 : | BitstreamSkip(bs, 8); // color_primaries | ||
161 : | BitstreamSkip(bs, 8); // transfer_characteristics | ||
162 : | BitstreamSkip(bs, 8); // matrix_coefficients | ||
163 : | } | ||
164 : | } | ||
165 : | } | ||
166 : | else if ((start_code & ~0x1f) == VIDOBJ_START_CODE) | ||
167 : | { | ||
168 : | BitstreamSkip(bs, 32); // video_object_start_code | ||
169 : | } | ||
170 : | else if ((start_code & ~0xf) == VIDOBJLAY_START_CODE) | ||
171 : | { | ||
172 : | // DEBUG("video_object_layer"); | ||
173 : | BitstreamSkip(bs, 32); // video_object_layer_start_code | ||
174 : | |||
175 : | BitstreamSkip(bs, 1); // random_accessible_vol | ||
176 : | |||
177 : | // video_object_type_indication | ||
178 : | if (BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_SIMPLE && | ||
179 : | BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_CORE && | ||
180 : | BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_MAIN && | ||
181 : | BitstreamShowBits(bs, 8) != 0) // BUGGY DIVX | ||
182 : | { | ||
183 : | DEBUG1("video_object_type_indication not supported", BitstreamShowBits(bs, 8)); | ||
184 : | return -1; | ||
185 : | } | ||
186 : | BitstreamSkip(bs, 8); | ||
187 : | |||
188 : | |||
189 : | if (BitstreamGetBit(bs)) // is_object_layer_identifier | ||
190 : | { | ||
191 : | DEBUG("+ is_object_layer_identifier"); | ||
192 : | vol_ver_id = BitstreamGetBits(bs,4); // video_object_layer_verid | ||
193 : | BitstreamSkip(bs, 3); // video_object_layer_priority | ||
194 : | } | ||
195 : | else | ||
196 : | { | ||
197 : | vol_ver_id = 1; | ||
198 : | } | ||
199 : | //DEBUGI("vol_ver_id", vol_ver_id); | ||
200 : | |||
201 : | if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR) // aspect_ratio_info | ||
202 : | { | ||
203 : | DEBUG("+ aspect_ratio_info"); | ||
204 : | BitstreamSkip(bs, 8); // par_width | ||
205 : | BitstreamSkip(bs, 8); // par_height | ||
206 : | } | ||
207 : | |||
208 : | if (BitstreamGetBit(bs)) // vol_control_parameters | ||
209 : | { | ||
210 : | DEBUG("+ vol_control_parameters"); | ||
211 : | BitstreamSkip(bs, 2); // chroma_format | ||
212 : | BitstreamSkip(bs, 1); // low_delay | ||
213 : | if (BitstreamGetBit(bs)) // vbv_parameters | ||
214 : | { | ||
215 : | DEBUG("+ vbv_parameters"); | ||
216 : | BitstreamSkip(bs, 15); // first_half_bitrate | ||
217 : | READ_MARKER(); | ||
218 : | BitstreamSkip(bs, 15); // latter_half_bitrate | ||
219 : | READ_MARKER(); | ||
220 : | BitstreamSkip(bs, 15); // first_half_vbv_buffer_size | ||
221 : | READ_MARKER(); | ||
222 : | BitstreamSkip(bs, 3); // latter_half_vbv_buffer_size | ||
223 : | BitstreamSkip(bs, 11); // first_half_vbv_occupancy | ||
224 : | READ_MARKER(); | ||
225 : | BitstreamSkip(bs, 15); // latter_half_vbv_occupancy | ||
226 : | READ_MARKER(); | ||
227 : | |||
228 : | } | ||
229 : | } | ||
230 : | |||
231 : | dec->shape = BitstreamGetBits(bs, 2); // video_object_layer_shape | ||
232 : | // DEBUG1("shape", dec->shape); | ||
233 : | |||
234 : | if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) | ||
235 : | { | ||
236 : | BitstreamSkip(bs, 4); // video_object_layer_shape_extension | ||
237 : | } | ||
238 : | |||
239 : | READ_MARKER(); | ||
240 : | |||
241 : | time_inc_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution | ||
242 : | time_inc_resolution--; | ||
243 : | if (time_inc_resolution > 0) | ||
244 : | { | ||
245 : | dec->time_inc_bits = log2bin(time_inc_resolution); | ||
246 : | } | ||
247 : | else | ||
248 : | { | ||
249 : | // dec->time_inc_bits = 0; | ||
250 : | |||
251 : | // for "old" xvid compatibility, set time_inc_bits = 1 | ||
252 : | dec->time_inc_bits = 1; | ||
253 : | } | ||
254 : | |||
255 : | READ_MARKER(); | ||
256 : | |||
257 : | if (BitstreamGetBit(bs)) // fixed_vop_rate | ||
258 : | { | ||
259 : | BitstreamSkip(bs, dec->time_inc_bits); // fixed_vop_time_increment | ||
260 : | } | ||
261 : | |||
262 : | if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) | ||
263 : | { | ||
264 : | |||
265 : | if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) | ||
266 : | { | ||
267 : | uint32_t width, height; | ||
268 : | |||
269 : | READ_MARKER(); | ||
270 : | width = BitstreamGetBits(bs, 13); // video_object_layer_width | ||
271 : | //DEBUGI("width", width); | ||
272 : | READ_MARKER(); | ||
273 : | height = BitstreamGetBits(bs, 13); // video_object_layer_height | ||
274 : | //DEBUGI("height", height); | ||
275 : | READ_MARKER(); | ||
276 : | |||
277 : | if (width != dec->width || height != dec->height) | ||
278 : | { | ||
279 : | DEBUG("FATAL: video dimension discrepancy ***"); | ||
280 : | DEBUG2("bitstream width/height", width, height); | ||
281 : | DEBUG2("param width/height", dec->width, dec->height); | ||
282 : | return -1; | ||
283 : | } | ||
284 : | |||
285 : | } | ||
286 : | h | 1.4 | |
287 : | if (dec->interlacing = BitstreamGetBit(bs)) | ||
288 : | Isibaar | 1.1 | { |
289 : | h | 1.4 | DEBUG("vol: interlacing"); |
290 : | Isibaar | 1.1 | } |
291 : | |||
292 : | if (!BitstreamGetBit(bs)) // obmc_disable | ||
293 : | { | ||
294 : | DEBUG("IGNORED/TODO: !obmc_disable"); | ||
295 : | // TODO | ||
296 : | // fucking divx4.02 has this enabled | ||
297 : | } | ||
298 : | |||
299 : | if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2))) // sprite_enable | ||
300 : | { | ||
301 : | DEBUG("sprite_enable; not supported"); | ||
302 : | return -1; | ||
303 : | } | ||
304 : | |||
305 : | if (vol_ver_id != 1 && dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) | ||
306 : | { | ||
307 : | BitstreamSkip(bs, 1); // sadct_disable | ||
308 : | } | ||
309 : | |||
310 : | if (BitstreamGetBit(bs)) // not_8_bit | ||
311 : | { | ||
312 : | DEBUG("+ not_8_bit [IGNORED/TODO]"); | ||
313 : | dec->quant_bits = BitstreamGetBits(bs, 4); // quant_precision | ||
314 : | BitstreamSkip(bs, 4); // bits_per_pixel | ||
315 : | } | ||
316 : | else | ||
317 : | { | ||
318 : | dec->quant_bits = 5; | ||
319 : | } | ||
320 : | |||
321 : | if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) | ||
322 : | { | ||
323 : | BitstreamSkip(bs, 1); // no_gray_quant_update | ||
324 : | BitstreamSkip(bs, 1); // composition_method | ||
325 : | BitstreamSkip(bs, 1); // linear_composition | ||
326 : | } | ||
327 : | |||
328 : | dec->quant_type = BitstreamGetBit(bs); // quant_type | ||
329 : | // DEBUG1("**** quant_type", dec->quant_type); | ||
330 : | |||
331 : | if (dec->quant_type) | ||
332 : | { | ||
333 : | if (BitstreamGetBit(bs)) // load_intra_quant_mat | ||
334 : | { | ||
335 : | Isibaar | 1.3 | uint8_t matrix[64]; |
336 : | Isibaar | 1.2 | bs_get_matrix(bs, matrix); |
337 : | set_intra_matrix(matrix); | ||
338 : | Isibaar | 1.1 | } |
339 : | Isibaar | 1.3 | else |
340 : | set_intra_matrix(get_default_intra_matrix()); | ||
341 : | Isibaar | 1.2 | |
342 : | Isibaar | 1.1 | if (BitstreamGetBit(bs)) // load_inter_quant_mat |
343 : | { | ||
344 : | Isibaar | 1.3 | uint8_t matrix[64]; |
345 : | Isibaar | 1.2 | bs_get_matrix(bs, matrix); |
346 : | set_inter_matrix(matrix); | ||
347 : | Isibaar | 1.1 | } |
348 : | Isibaar | 1.3 | else |
349 : | set_inter_matrix(get_default_inter_matrix()); | ||
350 : | Isibaar | 1.1 | |
351 : | if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) | ||
352 : | { | ||
353 : | // TODO | ||
354 : | DEBUG("TODO: grayscale matrix stuff"); | ||
355 : | return -1; | ||
356 : | } | ||
357 : | Isibaar | 1.2 | |
358 : | Isibaar | 1.1 | } |
359 : | |||
360 : | |||
361 : | if (vol_ver_id != 1) | ||
362 : | { | ||
363 : | dec->quarterpel = BitstreamGetBit(bs); // quarter_sampe | ||
364 : | if (dec->quarterpel) | ||
365 : | { | ||
366 : | DEBUG("IGNORED/TODO: quarter_sample"); | ||
367 : | } | ||
368 : | } | ||
369 : | else | ||
370 : | { | ||
371 : | dec->quarterpel = 0; | ||
372 : | } | ||
373 : | |||
374 : | if (!BitstreamGetBit(bs)) // complexity_estimation_disable | ||
375 : | { | ||
376 : | DEBUG("TODO: complexity_estimation header"); | ||
377 : | // TODO | ||
378 : | return -1; | ||
379 : | } | ||
380 : | |||
381 : | if (!BitstreamGetBit(bs)) // resync_marker_disable | ||
382 : | { | ||
383 : | DEBUG("IGNORED/TODO: !resync_marker_disable"); | ||
384 : | // TODO | ||
385 : | } | ||
386 : | |||
387 : | if (BitstreamGetBit(bs)) // data_partitioned | ||
388 : | { | ||
389 : | DEBUG("+ data_partitioned"); | ||
390 : | BitstreamSkip(bs, 1); // reversible_vlc | ||
391 : | } | ||
392 : | |||
393 : | if (vol_ver_id != 1) | ||
394 : | { | ||
395 : | if (BitstreamGetBit(bs)) // newpred_enable | ||
396 : | { | ||
397 : | DEBUG("+ newpred_enable"); | ||
398 : | BitstreamSkip(bs, 2); // requested_upstream_message_type | ||
399 : | BitstreamSkip(bs, 1); // newpred_segment_type | ||
400 : | } | ||
401 : | if (BitstreamGetBit(bs)) // reduced_resolution_vop_enable | ||
402 : | { | ||
403 : | DEBUG("TODO: reduced_resolution_vop_enable"); | ||
404 : | // TODO | ||
405 : | return -1; | ||
406 : | } | ||
407 : | } | ||
408 : | |||
409 : | if (BitstreamGetBit(bs)) // scalability | ||
410 : | { | ||
411 : | // TODO | ||
412 : | DEBUG("TODO: scalability"); | ||
413 : | return -1; | ||
414 : | } | ||
415 : | } | ||
416 : | else // dec->shape == BINARY_ONLY | ||
417 : | { | ||
418 : | if (vol_ver_id != 1) | ||
419 : | { | ||
420 : | if (BitstreamGetBit(bs)) // scalability | ||
421 : | { | ||
422 : | // TODO | ||
423 : | DEBUG("TODO: scalability"); | ||
424 : | return -1; | ||
425 : | } | ||
426 : | } | ||
427 : | BitstreamSkip(bs, 1); // resync_marker_disable | ||
428 : | |||
429 : | } | ||
430 : | |||
431 : | } | ||
432 : | else if (start_code == GRPOFVOP_START_CODE) | ||
433 : | { | ||
434 : | // DEBUG("group_of_vop"); | ||
435 : | BitstreamSkip(bs, 32); | ||
436 : | { | ||
437 : | int hours, minutes, seconds; | ||
438 : | hours = BitstreamGetBits(bs, 5); | ||
439 : | minutes = BitstreamGetBits(bs, 6); | ||
440 : | READ_MARKER(); | ||
441 : | seconds = BitstreamGetBits(bs, 6); | ||
442 : | // DEBUG3("hms", hours, minutes, seconds); | ||
443 : | } | ||
444 : | BitstreamSkip(bs, 1); // closed_gov | ||
445 : | BitstreamSkip(bs, 1); // broken_link | ||
446 : | } | ||
447 : | else if (start_code == VOP_START_CODE) | ||
448 : | { | ||
449 : | // DEBUG("vop_start_code"); | ||
450 : | BitstreamSkip(bs, 32); // vop_start_code | ||
451 : | |||
452 : | coding_type = BitstreamGetBits(bs, 2); // vop_coding_type | ||
453 : | //DEBUG1("coding_type", coding_type); | ||
454 : | |||
455 : | while (BitstreamGetBit(bs) != 0) ; // time_base | ||
456 : | |||
457 : | READ_MARKER(); | ||
458 : | |||
459 : | //DEBUG1("time_inc_bits", dec->time_inc_bits); | ||
460 : | //DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits)); | ||
461 : | if (dec->time_inc_bits) | ||
462 : | { | ||
463 : | BitstreamSkip(bs, dec->time_inc_bits); // vop_time_increment | ||
464 : | } | ||
465 : | |||
466 : | READ_MARKER(); | ||
467 : | |||
468 : | if (!BitstreamGetBit(bs)) // vop_coded | ||
469 : | { | ||
470 : | return N_VOP; | ||
471 : | } | ||
472 : | |||
473 : | /* if (newpred_enable) | ||
474 : | { | ||
475 : | } | ||
476 : | */ | ||
477 : | |||
478 : | if (coding_type != I_VOP) | ||
479 : | { | ||
480 : | *rounding = BitstreamGetBit(bs); // rounding_type | ||
481 : | //DEBUG1("rounding", *rounding); | ||
482 : | } | ||
483 : | |||
484 : | /* if (reduced_resolution_enable) | ||
485 : | { | ||
486 : | } | ||
487 : | */ | ||
488 : | |||
489 : | if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) | ||
490 : | { | ||
491 : | uint32_t width, height; | ||
492 : | uint32_t horiz_mc_ref, vert_mc_ref; | ||
493 : | |||
494 : | width = BitstreamGetBits(bs, 13); | ||
495 : | READ_MARKER(); | ||
496 : | height = BitstreamGetBits(bs, 13); | ||
497 : | READ_MARKER(); | ||
498 : | horiz_mc_ref = BitstreamGetBits(bs, 13); | ||
499 : | READ_MARKER(); | ||
500 : | vert_mc_ref = BitstreamGetBits(bs, 13); | ||
501 : | READ_MARKER(); | ||
502 : | |||
503 : | // DEBUG2("vop_width/height", width, height); | ||
504 : | // DEBUG2("ref ", horiz_mc_ref, vert_mc_ref); | ||
505 : | |||
506 : | BitstreamSkip(bs, 1); // change_conv_ratio_disable | ||
507 : | if (BitstreamGetBit(bs)) // vop_constant_alpha | ||
508 : | { | ||
509 : | BitstreamSkip(bs, 8); // vop_constant_alpha_value | ||
510 : | } | ||
511 : | } | ||
512 : | |||
513 : | |||
514 : | if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) | ||
515 : | { | ||
516 : | // intra_dc_vlc_threshold | ||
517 : | *intra_dc_threshold = intra_dc_threshold_table[ BitstreamGetBits(bs,3) ]; | ||
518 : | |||
519 : | h | 1.4 | if (dec->interlacing) |
520 : | { | ||
521 : | if (dec->top_field_first = BitstreamGetBit(bs)) | ||
522 : | { | ||
523 : | DEBUG("vop: top_field_first"); | ||
524 : | } | ||
525 : | if (dec->alternate_vertical_scan = BitstreamGetBit(bs)) | ||
526 : | Isibaar | 1.1 | { |
527 : | h | 1.4 | DEBUG("vop: alternate_vertical_scan"); |
528 : | } | ||
529 : | } | ||
530 : | Isibaar | 1.1 | } |
531 : | |||
532 : | *quant = BitstreamGetBits(bs, dec->quant_bits); // vop_quant | ||
533 : | //DEBUG1("quant", *quant); | ||
534 : | |||
535 : | if (coding_type != I_VOP) | ||
536 : | { | ||
537 : | *fcode = BitstreamGetBits(bs, 3); // fcode_forward | ||
538 : | } | ||
539 : | |||
540 : | if (coding_type == B_VOP) | ||
541 : | { | ||
542 : | // *fcode_backward = BitstreamGetBits(bs, 3); // fcode_backward | ||
543 : | } | ||
544 : | return coding_type; | ||
545 : | } | ||
546 : | else if (start_code == USERDATA_START_CODE) | ||
547 : | { | ||
548 : | // DEBUG("user_data"); | ||
549 : | BitstreamSkip(bs, 32); // user_data_start_code | ||
550 : | } | ||
551 : | else // start_code == ? | ||
552 : | { | ||
553 : | if (BitstreamShowBits(bs, 24) == 0x000001) | ||
554 : | { | ||
555 : | DEBUG1("*** WARNING: unknown start_code", BitstreamShowBits(bs, 32)); | ||
556 : | } | ||
557 : | BitstreamSkip(bs, 8); | ||
558 : | } | ||
559 : | } | ||
560 : | while ((BitstreamPos(bs) >> 3) < bs->length); | ||
561 : | |||
562 : | DEBUG("*** WARNING: no vop_start_code found"); | ||
563 : | return -1; /* ignore it */ | ||
564 : | } | ||
565 : | |||
566 : | |||
567 : | /* write custom quant matrix */ | ||
568 : | |||
569 : | Isibaar | 1.2 | static void bs_put_matrix(Bitstream * bs, const int16_t *matrix) |
570 : | Isibaar | 1.1 | { |
571 : | int i, j; | ||
572 : | const int last = matrix[scan_tables[0][63]]; | ||
573 : | |||
574 : | for (j = 63; j >= 0 && matrix[scan_tables[0][j - 1]] == last; j--) ; | ||
575 : | |||
576 : | for (i = 0; i <= j; i++) | ||
577 : | { | ||
578 : | BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8); | ||
579 : | } | ||
580 : | |||
581 : | if (j < 63) | ||
582 : | { | ||
583 : | BitstreamPutBits(bs, 0, 8); | ||
584 : | } | ||
585 : | } | ||
586 : | |||
587 : | |||
588 : | /* | ||
589 : | write vol header | ||
590 : | */ | ||
591 : | void BitstreamWriteVolHeader(Bitstream * const bs, | ||
592 : | h | 1.4 | const MBParam * pParam) |
593 : | Isibaar | 1.1 | { |
594 : | // video object_start_code & vo_id | ||
595 : | BitstreamPad(bs); | ||
596 : | BitstreamPutBits(bs, VO_START_CODE, 27); | ||
597 : | BitstreamPutBits(bs, 0, 5); | ||
598 : | |||
599 : | // video_object_layer_start_code & vol_id | ||
600 : | BitstreamPutBits(bs, VOL_START_CODE, 28); | ||
601 : | BitstreamPutBits(bs, 0, 4); | ||
602 : | |||
603 : | BitstreamPutBit(bs, 0); // random_accessible_vol | ||
604 : | BitstreamPutBits(bs, 0, 8); // video_object_type_indication | ||
605 : | BitstreamPutBit(bs, 0); // is_object_layer_identified (0=not given) | ||
606 : | BitstreamPutBits(bs, 1, 4); // aspect_ratio_info (1=1:1) | ||
607 : | BitstreamPutBit(bs, 0); // vol_control_parameters (0=not given) | ||
608 : | BitstreamPutBits(bs, 0, 2); // video_object_layer_shape (0=rectangular) | ||
609 : | |||
610 : | WRITE_MARKER(); | ||
611 : | |||
612 : | /* time_increment_resolution; ignored by current decore versions | ||
613 : | eg. 2fps res=2 inc=1 | ||
614 : | 25fps res=25 inc=1 | ||
615 : | 29.97fps res=30000 inc=1001 | ||
616 : | */ | ||
617 : | BitstreamPutBits(bs, 2, 16); | ||
618 : | |||
619 : | WRITE_MARKER(); | ||
620 : | |||
621 : | // fixed_vop_rate | ||
622 : | BitstreamPutBit(bs, 0); | ||
623 : | |||
624 : | // fixed_time_increment: value=nth_of_sec, nbits = log2(resolution) | ||
625 : | // BitstreamPutBits(bs, 0, 15); | ||
626 : | |||
627 : | WRITE_MARKER(); | ||
628 : | h | 1.4 | BitstreamPutBits(bs, pParam->width, 13); // width |
629 : | Isibaar | 1.1 | WRITE_MARKER(); |
630 : | h | 1.4 | BitstreamPutBits(bs, pParam->height, 13); // height |
631 : | Isibaar | 1.1 | WRITE_MARKER(); |
632 : | |||
633 : | h | 1.4 | BitstreamPutBit(bs, pParam->global_flags & XVID_INTERLACING); // interlace |
634 : | Isibaar | 1.1 | BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) |
635 : | BitstreamPutBit(bs, 0); // sprite_enable | ||
636 : | BitstreamPutBit(bs, 0); // not_in_bit | ||
637 : | |||
638 : | // quant_type 0=h.263 1=mpeg4(quantizer tables) | ||
639 : | h | 1.4 | BitstreamPutBit(bs, pParam->quant_type); |
640 : | Isibaar | 1.2 | |
641 : | h | 1.4 | if (pParam->quant_type) |
642 : | Isibaar | 1.1 | { |
643 : | Isibaar | 1.2 | BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat |
644 : | if (get_intra_matrix_status()) | ||
645 : | Isibaar | 1.1 | { |
646 : | Isibaar | 1.2 | bs_put_matrix(bs, get_intra_matrix()); |
647 : | Isibaar | 1.1 | } |
648 : | |||
649 : | Isibaar | 1.2 | BitstreamPutBit(bs, get_inter_matrix_status()); // load_inter_quant_mat |
650 : | if (get_inter_matrix_status()) | ||
651 : | Isibaar | 1.1 | { |
652 : | Isibaar | 1.2 | bs_put_matrix(bs, get_inter_matrix()); |
653 : | Isibaar | 1.1 | } |
654 : | |||
655 : | } | ||
656 : | |||
657 : | BitstreamPutBit(bs, 1); // complexity_estimation_disable | ||
658 : | BitstreamPutBit(bs, 1); // resync_marker_disable | ||
659 : | BitstreamPutBit(bs, 0); // data_partitioned | ||
660 : | BitstreamPutBit(bs, 0); // scalability | ||
661 : | } | ||
662 : | |||
663 : | |||
664 : | /* | ||
665 : | write vop header | ||
666 : | |||
667 : | NOTE: doesnt handle bother with time_base & time_inc | ||
668 : | time_base = n seconds since last resync (eg. last iframe) | ||
669 : | time_inc = nth of a second since last resync | ||
670 : | (decoder uses these values to determine precise time since last resync) | ||
671 : | */ | ||
672 : | void BitstreamWriteVopHeader(Bitstream * const bs, | ||
673 : | h | 1.4 | const MBParam * pParam) |
674 : | Isibaar | 1.1 | { |
675 : | BitstreamPad(bs); | ||
676 : | BitstreamPutBits(bs, VOP_START_CODE, 32); | ||
677 : | |||
678 : | h | 1.4 | BitstreamPutBits(bs, pParam->coding_type, 2); |
679 : | Isibaar | 1.1 | |
680 : | // time_base = 0 write n x PutBit(1), PutBit(0) | ||
681 : | BitstreamPutBits(bs, 0, 1); | ||
682 : | |||
683 : | WRITE_MARKER(); | ||
684 : | |||
685 : | // time_increment: value=nth_of_sec, nbits = log2(resolution) | ||
686 : | BitstreamPutBits(bs, 1, 1); | ||
687 : | |||
688 : | WRITE_MARKER(); | ||
689 : | |||
690 : | BitstreamPutBits(bs, 1, 1); // vop_coded | ||
691 : | |||
692 : | h | 1.4 | if (pParam->coding_type != I_VOP) |
693 : | BitstreamPutBits(bs, pParam->rounding_type, 1); | ||
694 : | Isibaar | 1.1 | |
695 : | BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold | ||
696 : | |||
697 : | h | 1.4 | if (pParam->global_flags & XVID_INTERLACING) |
698 : | { | ||
699 : | BitstreamPutBit(bs, 1); // top field first | ||
700 : | BitstreamPutBit(bs, 0); // alternate vertical scan | ||
701 : | } | ||
702 : | |||
703 : | BitstreamPutBits(bs, pParam->quant, 5); // quantizer | ||
704 : | Isibaar | 1.1 | |
705 : | h | 1.4 | if (pParam->coding_type != I_VOP) |
706 : | BitstreamPutBits(bs, pParam->fixed_code, 3); // fixed_code = [1,4] | ||
707 : | Isibaar | 1.1 | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |