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

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