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

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