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

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