[svn] / branches / dev-api-4 / xvidcore / src / bitstream / bitstream.c Repository:
ViewVC logotype

Annotation of /branches/dev-api-4/xvidcore/src/bitstream/bitstream.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 206 - (view) (download)
Original Path: trunk/xvidcore/src/bitstream/bitstream.c

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

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