[cvs] / xvidcore / src / bitstream / bitstream.c Repository:
ViewVC logotype

Annotation of /xvidcore/src/bitstream/bitstream.c

Parent Directory Parent Directory | Revision Log Revision Log


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

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4