[svn] / branches / release-1_0-branch / xvidcore / src / bitstream / bitstream.c Repository:
ViewVC logotype

Annotation of /branches/release-1_0-branch/xvidcore/src/bitstream/bitstream.c

Parent Directory Parent Directory | Revision Log Revision Log


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

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