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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 164 - (view) (download)

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

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