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

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