[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 723 - (view) (download)
Original Path: trunk/xvidcore/src/bitstream/bitstream.c

1 : edgomez 469 /*****************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 :     * - Bitstream reader/writer functions -
5 :     *
6 : suxen_drol 499 * Copyright (C) 2001-2002 - Peter Ross <pross@xvid.org>
7 : edgomez 469 *
8 : edgomez 655 * This file is part of XviD, a free MPEG-4 video encoder/decoder
9 : edgomez 469 *
10 : edgomez 655 * XviD is free software; you can redistribute it and/or modify it
11 :     * under the terms of the GNU General Public License as published by
12 :     * the Free Software Foundation; either version 2 of the License, or
13 : edgomez 469 * (at your option) any later version.
14 :     *
15 :     * This program is distributed in the hope that it will be useful,
16 : edgomez 655 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : edgomez 469 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 :     * GNU General Public License for more details.
19 :     *
20 :     * You should have received a copy of the GNU General Public License
21 : edgomez 655 * along with this program; if not, write to the Free Software
22 : edgomez 469 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 :     *
24 : edgomez 655 * Under section 8 of the GNU General Public License, the copyright
25 :     * holders of XVID explicitly forbid distribution in the following
26 :     * countries:
27 : edgomez 469 *
28 : edgomez 655 * - Japan
29 :     * - United States of America
30 :     *
31 :     * Linking XviD statically or dynamically with other modules is making a
32 :     * combined work based on XviD. Thus, the terms and conditions of the
33 :     * GNU General Public License cover the whole combination.
34 :     *
35 :     * As a special exception, the copyright holders of XviD give you
36 :     * permission to link XviD with independent modules that communicate with
37 :     * XviD solely through the VFW1.1 and DShow interfaces, regardless of the
38 :     * license terms of these independent modules, and to copy and distribute
39 :     * the resulting combined work under terms of your choice, provided that
40 :     * every copy of the combined work is accompanied by a complete copy of
41 :     * the source code of XviD (the version of XviD used to produce the
42 :     * combined work), being distributed under the terms of the GNU General
43 :     * Public License plus this exception. An independent module is a module
44 :     * which is not derived from or based on XviD.
45 :     *
46 :     * Note that people who make modified versions of XviD are not obligated
47 :     * to grant this special exception for their modified versions; it is
48 :     * their choice whether to do so. The GNU General Public License gives
49 :     * permission to release a modified version without this exception; this
50 :     * exception also makes it possible to release a modified version which
51 :     * carries forward this exception.
52 :     *
53 : edgomez 723 * $Id: bitstream.c,v 1.37 2002-12-15 01:21:12 edgomez Exp $
54 : edgomez 655 *
55 : edgomez 469 ****************************************************************************/
56 : Isibaar 3
57 :     #include "bitstream.h"
58 :     #include "zigzag.h"
59 : Isibaar 4 #include "../quant/quant_matrix.h"
60 : Isibaar 3
61 : edgomez 469 /*****************************************************************************
62 :     * Functions
63 :     ****************************************************************************/
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 : suxen_drol 529 #if !defined(_MSC_VER)
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 : edgomez 677 i--; /* fix little bug at coeff not full */
112 : edgomez 195
113 :     while (i < 64) {
114 :     matrix[scan_tables[0][i++]] = last;
115 :     }
116 :     }
117 :    
118 : suxen_drol 248
119 :    
120 : edgomez 677 /* for PVOP addbits == fcode - 1 */
121 :     /* for BVOP addbits == max(fcode,bcode) - 1 */
122 :     /* returns mbpos */
123 : suxen_drol 248 int
124 : suxen_drol 252 read_video_packet_header(Bitstream *bs, const int addbits, int * quant)
125 : suxen_drol 248 {
126 :     int nbits;
127 :     int mbnum;
128 :     int hec;
129 :    
130 : suxen_drol 252 nbits = NUMBITS_VP_RESYNC_MARKER + addbits;
131 : suxen_drol 248
132 :     BitstreamSkip(bs, BitstreamNumBitsToByteAlign(bs));
133 :     BitstreamSkip(bs, nbits);
134 :    
135 : suxen_drol 252 DPRINTF(DPRINTF_STARTCODE, "<video_packet_header>");
136 :    
137 : edgomez 677 /* if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { */
138 :     /* hec */
139 :     /* vop_width */
140 :     /* marker_bit */
141 :     /* vop_height */
142 :     /* marker_bit */
143 : suxen_drol 248
144 : edgomez 677 /*} */
145 : suxen_drol 248
146 :     mbnum = BitstreamGetBits(bs, 9);
147 : suxen_drol 252 DPRINTF(DPRINTF_HEADER, "mbnum %i", mbnum);
148 : suxen_drol 248
149 : edgomez 677 /* if (dec->shape != VIDOBJLAY_SHAPE_BINARYONLY) */
150 : suxen_drol 252 *quant = BitstreamGetBits(bs, 5);
151 :     DPRINTF(DPRINTF_HEADER, "quant %i", *quant);
152 : suxen_drol 248
153 : edgomez 677 /* if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) */
154 : suxen_drol 248 hec = BitstreamGetBit(bs);
155 : suxen_drol 252 DPRINTF(DPRINTF_HEADER, "header_extension_code %i", hec);
156 : edgomez 677 /* if (hec) */
157 :     /* .. decoder hec-header ... */
158 : suxen_drol 248
159 :     return mbnum;
160 :     }
161 :    
162 :    
163 :    
164 : Isibaar 3 /*
165 :     decode headers
166 :     returns coding_type, or -1 if error
167 :     */
168 :    
169 : suxen_drol 252 #define VIDOBJ_START_CODE_MASK 0x0000001f
170 :     #define VIDOBJLAY_START_CODE_MASK 0x0000000f
171 :    
172 : edgomez 195 int
173 :     BitstreamReadHeaders(Bitstream * bs,
174 :     DECODER * dec,
175 :     uint32_t * rounding,
176 :     uint32_t * quant,
177 :     uint32_t * fcode_forward,
178 :     uint32_t * fcode_backward,
179 :     uint32_t * intra_dc_threshold)
180 : Isibaar 3 {
181 :     uint32_t vol_ver_id;
182 : chenm001 156 static uint32_t time_increment_resolution;
183 : Isibaar 3 uint32_t coding_type;
184 :     uint32_t start_code;
185 : edgomez 195 uint32_t time_incr = 0;
186 : edgomez 478 int32_t time_increment = 0;
187 : chenm001 156
188 : edgomez 195 do {
189 : Isibaar 3 BitstreamByteAlign(bs);
190 :     start_code = BitstreamShowBits(bs, 32);
191 :    
192 : edgomez 195 if (start_code == VISOBJSEQ_START_CODE) {
193 : suxen_drol 252
194 :     int profile;
195 :    
196 :     DPRINTF(DPRINTF_STARTCODE, "<visual_object_sequence>");
197 :    
198 : edgomez 677 BitstreamSkip(bs, 32); /* visual_object_sequence_start_code */
199 :     profile = BitstreamGetBits(bs, 8); /* profile_and_level_indication */
200 : suxen_drol 252
201 :     DPRINTF(DPRINTF_HEADER, "profile_and_level_indication %i", profile);
202 :    
203 : edgomez 195 } else if (start_code == VISOBJSEQ_STOP_CODE) {
204 : suxen_drol 252
205 : edgomez 677 BitstreamSkip(bs, 32); /* visual_object_sequence_stop_code */
206 : suxen_drol 252
207 :     DPRINTF(DPRINTF_STARTCODE, "</visual_object_sequence>");
208 :    
209 : edgomez 195 } else if (start_code == VISOBJ_START_CODE) {
210 : suxen_drol 252
211 :     DPRINTF(DPRINTF_STARTCODE, "<visual_object>");
212 :    
213 : edgomez 677 BitstreamSkip(bs, 32); /* visual_object_start_code */
214 :     if (BitstreamGetBit(bs)) /* is_visual_object_identified */
215 : Isibaar 3 {
216 : edgomez 677 vol_ver_id = BitstreamGetBits(bs, 4); /* visual_object_ver_id */
217 : suxen_drol 252 DPRINTF(DPRINTF_HEADER,"ver_id %i", vol_ver_id);
218 : edgomez 677 BitstreamSkip(bs, 3); /* visual_object_priority */
219 : edgomez 195 } else {
220 : Isibaar 3 vol_ver_id = 1;
221 :     }
222 :    
223 : edgomez 677 if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) /* visual_object_type */
224 : Isibaar 3 {
225 : suxen_drol 252 DPRINTF(DPRINTF_ERROR, "visual_object_type != video");
226 : Isibaar 3 return -1;
227 :     }
228 :     BitstreamSkip(bs, 4);
229 :    
230 : edgomez 677 /* video_signal_type */
231 : Isibaar 3
232 : edgomez 677 if (BitstreamGetBit(bs)) /* video_signal_type */
233 : Isibaar 3 {
234 : suxen_drol 252 DPRINTF(DPRINTF_HEADER,"+ video_signal_type");
235 : edgomez 677 BitstreamSkip(bs, 3); /* video_format */
236 :     BitstreamSkip(bs, 1); /* video_range */
237 :     if (BitstreamGetBit(bs)) /* color_description */
238 : Isibaar 3 {
239 : suxen_drol 252 DPRINTF(DPRINTF_HEADER,"+ color_description");
240 : edgomez 677 BitstreamSkip(bs, 8); /* color_primaries */
241 :     BitstreamSkip(bs, 8); /* transfer_characteristics */
242 :     BitstreamSkip(bs, 8); /* matrix_coefficients */
243 : Isibaar 3 }
244 :     }
245 : suxen_drol 252 } else if ((start_code & ~VIDOBJ_START_CODE_MASK) == VIDOBJ_START_CODE) {
246 :    
247 :     DPRINTF(DPRINTF_STARTCODE, "<video_object>");
248 :     DPRINTF(DPRINTF_HEADER, "vo id %i", start_code & VIDOBJ_START_CODE_MASK);
249 :    
250 : edgomez 677 BitstreamSkip(bs, 32); /* video_object_start_code */
251 : suxen_drol 252
252 :     } else if ((start_code & ~VIDOBJLAY_START_CODE_MASK) == VIDOBJLAY_START_CODE) {
253 :    
254 :     DPRINTF(DPRINTF_STARTCODE, "<video_object_layer>");
255 :     DPRINTF(DPRINTF_HEADER, "vol id %i", start_code & VIDOBJLAY_START_CODE_MASK);
256 :    
257 : edgomez 677 BitstreamSkip(bs, 32); /* video_object_layer_start_code */
258 : Isibaar 3
259 : edgomez 677 BitstreamSkip(bs, 1); /* random_accessible_vol */
260 : Isibaar 3
261 : edgomez 677 /* video_object_type_indication */
262 :     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 */
263 : Isibaar 3 {
264 : suxen_drol 252 DPRINTF(DPRINTF_ERROR,"video_object_type_indication %i not supported ",
265 :     BitstreamShowBits(bs, 8));
266 : Isibaar 3 return -1;
267 :     }
268 :     BitstreamSkip(bs, 8);
269 :    
270 :    
271 : edgomez 677 if (BitstreamGetBit(bs)) /* is_object_layer_identifier */
272 : Isibaar 3 {
273 : suxen_drol 252 DPRINTF(DPRINTF_HEADER, "+ is_object_layer_identifier");
274 : edgomez 677 vol_ver_id = BitstreamGetBits(bs, 4); /* video_object_layer_verid */
275 : suxen_drol 252 DPRINTF(DPRINTF_HEADER,"ver_id %i", vol_ver_id);
276 : edgomez 677 BitstreamSkip(bs, 3); /* video_object_layer_priority */
277 : edgomez 195 } else {
278 : Isibaar 3 vol_ver_id = 1;
279 :     }
280 :    
281 : edgomez 677 if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR) /* aspect_ratio_info */
282 : Isibaar 3 {
283 : suxen_drol 252 DPRINTF(DPRINTF_HEADER, "+ aspect_ratio_info");
284 : edgomez 677 BitstreamSkip(bs, 8); /* par_width */
285 :     BitstreamSkip(bs, 8); /* par_height */
286 : Isibaar 3 }
287 :    
288 : edgomez 677 if (BitstreamGetBit(bs)) /* vol_control_parameters */
289 : Isibaar 3 {
290 : suxen_drol 252 DPRINTF(DPRINTF_HEADER, "+ vol_control_parameters");
291 : edgomez 677 BitstreamSkip(bs, 2); /* chroma_format */
292 : edgomez 723 dec->low_delay = (uint8_t)BitstreamGetBit(bs); /* low_delay */
293 : suxen_drol 252 DPRINTF(DPRINTF_HEADER, "low_delay %i", dec->low_delay);
294 : edgomez 677 if (BitstreamGetBit(bs)) /* vbv_parameters */
295 : Isibaar 3 {
296 : suxen_drol 252 DPRINTF(DPRINTF_HEADER,"+ vbv_parameters");
297 : edgomez 677 BitstreamSkip(bs, 15); /* first_half_bitrate */
298 : Isibaar 3 READ_MARKER();
299 : edgomez 677 BitstreamSkip(bs, 15); /* latter_half_bitrate */
300 : Isibaar 3 READ_MARKER();
301 : edgomez 677 BitstreamSkip(bs, 15); /* first_half_vbv_buffer_size */
302 : Isibaar 3 READ_MARKER();
303 : edgomez 677 BitstreamSkip(bs, 3); /* latter_half_vbv_buffer_size */
304 :     BitstreamSkip(bs, 11); /* first_half_vbv_occupancy */
305 : Isibaar 3 READ_MARKER();
306 : edgomez 677 BitstreamSkip(bs, 15); /* latter_half_vbv_occupancy */
307 : Isibaar 3 READ_MARKER();
308 : edgomez 195
309 : Isibaar 3 }
310 :     }
311 :    
312 : edgomez 677 dec->shape = BitstreamGetBits(bs, 2); /* video_object_layer_shape */
313 : suxen_drol 252 DPRINTF(DPRINTF_HEADER, "shape %i", dec->shape);
314 : edgomez 195
315 :     if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) {
316 : edgomez 677 BitstreamSkip(bs, 4); /* video_object_layer_shape_extension */
317 : Isibaar 3 }
318 :    
319 :     READ_MARKER();
320 :    
321 : edgomez 677 /* *************************** for decode B-frame time *********************** */
322 :     time_increment_resolution = BitstreamGetBits(bs, 16); /* vop_time_increment_resolution */
323 : suxen_drol 252
324 :     DPRINTF(DPRINTF_HEADER,"vop_time_increment_resolution %i", time_increment_resolution);
325 :    
326 : edgomez 677 /* time_increment_resolution--; */
327 : suxen_drol 252
328 : edgomez 195 if (time_increment_resolution > 0) {
329 : chl 313 dec->time_inc_bits = log2bin(time_increment_resolution-1);
330 : edgomez 195 } else {
331 : edgomez 677 /* dec->time_inc_bits = 0; */
332 :     /* for "old" xvid compatibility, set time_inc_bits = 1 */
333 : Isibaar 3 dec->time_inc_bits = 1;
334 :     }
335 :    
336 :     READ_MARKER();
337 :    
338 : edgomez 677 if (BitstreamGetBit(bs)) /* fixed_vop_rate */
339 : Isibaar 3 {
340 : suxen_drol 252 DPRINTF(DPRINTF_HEADER, "+ fixed_vop_rate");
341 : edgomez 677 BitstreamSkip(bs, dec->time_inc_bits); /* fixed_vop_time_increment */
342 : Isibaar 3 }
343 :    
344 : edgomez 195 if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) {
345 : Isibaar 3
346 : edgomez 195 if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) {
347 : Isibaar 3 uint32_t width, height;
348 :    
349 :     READ_MARKER();
350 : edgomez 677 width = BitstreamGetBits(bs, 13); /* video_object_layer_width */
351 : Isibaar 3 READ_MARKER();
352 : edgomez 677 height = BitstreamGetBits(bs, 13); /* video_object_layer_height */
353 : Isibaar 3 READ_MARKER();
354 :    
355 : suxen_drol 252 DPRINTF(DPRINTF_HEADER, "width %i", width);
356 :     DPRINTF(DPRINTF_HEADER, "height %i", height);
357 :    
358 : edgomez 677 /* for auto set width & height */
359 : chenm001 294 if (dec->width == 0)
360 :     dec->width = width;
361 :     if (dec->height == 0)
362 :     dec->height = height;
363 :    
364 : edgomez 195 if (width != dec->width || height != dec->height) {
365 : suxen_drol 252 DPRINTF(DPRINTF_ERROR, "XVID_DEC_PARAM width/height does not match bitstream");
366 : Isibaar 3 return -1;
367 :     }
368 :    
369 :     }
370 : h 69
371 : suxen_drol 252 dec->interlacing = BitstreamGetBit(bs);
372 :     DPRINTF(DPRINTF_HEADER, "interlace", dec->interlacing);
373 : Isibaar 3
374 : edgomez 677 if (!BitstreamGetBit(bs)) /* obmc_disable */
375 : Isibaar 3 {
376 : suxen_drol 252 DPRINTF(DPRINTF_ERROR, "obmc_disabled==false not supported");
377 : edgomez 677 /* TODO */
378 :     /* fucking divx4.02 has this enabled */
379 : Isibaar 3 }
380 :    
381 : edgomez 677 if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2))) /* sprite_enable */
382 : Isibaar 3 {
383 : suxen_drol 252 DPRINTF(DPRINTF_ERROR, "spriate_enabled not supported");
384 : Isibaar 3 return -1;
385 :     }
386 : edgomez 195
387 :     if (vol_ver_id != 1 &&
388 :     dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {
389 : edgomez 677 BitstreamSkip(bs, 1); /* sadct_disable */
390 : Isibaar 3 }
391 :    
392 : edgomez 677 if (BitstreamGetBit(bs)) /* not_8_bit */
393 : Isibaar 3 {
394 : suxen_drol 252 DPRINTF(DPRINTF_HEADER, "not_8_bit==true (ignored)");
395 : edgomez 677 dec->quant_bits = BitstreamGetBits(bs, 4); /* quant_precision */
396 :     BitstreamSkip(bs, 4); /* bits_per_pixel */
397 : edgomez 195 } else {
398 : Isibaar 3 dec->quant_bits = 5;
399 :     }
400 :    
401 : edgomez 195 if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) {
402 : edgomez 677 BitstreamSkip(bs, 1); /* no_gray_quant_update */
403 :     BitstreamSkip(bs, 1); /* composition_method */
404 :     BitstreamSkip(bs, 1); /* linear_composition */
405 : Isibaar 3 }
406 :    
407 : edgomez 677 dec->quant_type = BitstreamGetBit(bs); /* quant_type */
408 : suxen_drol 252 DPRINTF(DPRINTF_HEADER, "quant_type %i", dec->quant_type);
409 : Isibaar 3
410 : edgomez 195 if (dec->quant_type) {
411 : edgomez 677 if (BitstreamGetBit(bs)) /* load_intra_quant_mat */
412 : Isibaar 3 {
413 : Isibaar 20 uint8_t matrix[64];
414 : edgomez 195
415 : suxen_drol 252 DPRINTF(DPRINTF_HEADER, "load_intra_quant_mat");
416 :    
417 : Isibaar 4 bs_get_matrix(bs, matrix);
418 :     set_intra_matrix(matrix);
419 : edgomez 195 } else
420 : Isibaar 20 set_intra_matrix(get_default_intra_matrix());
421 : Isibaar 4
422 : edgomez 677 if (BitstreamGetBit(bs)) /* load_inter_quant_mat */
423 : Isibaar 3 {
424 : edgomez 195 uint8_t matrix[64];
425 : suxen_drol 252
426 :     DPRINTF(DPRINTF_HEADER, "load_inter_quant_mat");
427 : edgomez 195
428 : Isibaar 4 bs_get_matrix(bs, matrix);
429 :     set_inter_matrix(matrix);
430 : edgomez 195 } else
431 : Isibaar 20 set_inter_matrix(get_default_inter_matrix());
432 : Isibaar 3
433 : edgomez 195 if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) {
434 : suxen_drol 252 DPRINTF(DPRINTF_ERROR, "greyscale matrix not supported");
435 : Isibaar 3 return -1;
436 :     }
437 : Isibaar 4
438 : Isibaar 3 }
439 :    
440 : edgomez 195
441 :     if (vol_ver_id != 1) {
442 : edgomez 514 DPRINTF(DPRINTF_DEBUG, "QUARTERPEL BITSTREAM");
443 : edgomez 677 dec->quarterpel = BitstreamGetBit(bs); /* quarter_sample */
444 : Isibaar 3 }
445 : Isibaar 333 else
446 :     dec->quarterpel = 0;
447 :    
448 : Isibaar 3
449 : edgomez 677 if (!BitstreamGetBit(bs)) /* complexity_estimation_disable */
450 : Isibaar 3 {
451 : suxen_drol 252 DPRINTF(DPRINTF_ERROR, "complexity_estimation not supported");
452 : Isibaar 3 return -1;
453 :     }
454 :    
455 : edgomez 677 BitstreamSkip(bs, 1); /* resync_marker_disable */
456 : Isibaar 3
457 : edgomez 677 if (BitstreamGetBit(bs)) /* data_partitioned */
458 : Isibaar 3 {
459 : suxen_drol 252 DPRINTF(DPRINTF_ERROR, "data_partitioned not supported");
460 : edgomez 677 BitstreamSkip(bs, 1); /* reversible_vlc */
461 : Isibaar 3 }
462 :    
463 : edgomez 195 if (vol_ver_id != 1) {
464 : edgomez 677 if (BitstreamGetBit(bs)) /* newpred_enable */
465 : Isibaar 3 {
466 : suxen_drol 252 DPRINTF(DPRINTF_HEADER, "+ newpred_enable");
467 : edgomez 677 BitstreamSkip(bs, 2); /* requested_upstream_message_type */
468 :     BitstreamSkip(bs, 1); /* newpred_segment_type */
469 : Isibaar 3 }
470 : edgomez 677 if (BitstreamGetBit(bs)) /* reduced_resolution_vop_enable */
471 : Isibaar 3 {
472 : suxen_drol 252 DPRINTF(DPRINTF_ERROR, "reduced_resolution_vop not supported");
473 : Isibaar 3 return -1;
474 :     }
475 :     }
476 : edgomez 195
477 : edgomez 723 if ((dec->scalability = (int8_t)BitstreamGetBit(bs))) /* scalability */
478 : Isibaar 3 {
479 : suxen_drol 252 DPRINTF(DPRINTF_ERROR, "scalability not supported");
480 : Isibaar 3 return -1;
481 :     }
482 : edgomez 677 } else /* dec->shape == BINARY_ONLY */
483 : Isibaar 3 {
484 : edgomez 195 if (vol_ver_id != 1) {
485 : edgomez 677 if (BitstreamGetBit(bs)) /* scalability */
486 : Isibaar 3 {
487 : suxen_drol 252 DPRINTF(DPRINTF_ERROR, "scalability not supported");
488 : Isibaar 3 return -1;
489 :     }
490 :     }
491 : edgomez 677 BitstreamSkip(bs, 1); /* resync_marker_disable */
492 : Isibaar 3
493 :     }
494 :    
495 : edgomez 195 } else if (start_code == GRPOFVOP_START_CODE) {
496 : suxen_drol 252
497 :     DPRINTF(DPRINTF_STARTCODE, "<group_of_vop>");
498 :    
499 : Isibaar 3 BitstreamSkip(bs, 32);
500 :     {
501 :     int hours, minutes, seconds;
502 : edgomez 195
503 : Isibaar 3 hours = BitstreamGetBits(bs, 5);
504 :     minutes = BitstreamGetBits(bs, 6);
505 :     READ_MARKER();
506 :     seconds = BitstreamGetBits(bs, 6);
507 : suxen_drol 252
508 :     DPRINTF(DPRINTF_HEADER, "time %ih%im%is", hours);
509 : Isibaar 3 }
510 : edgomez 677 BitstreamSkip(bs, 1); /* closed_gov */
511 :     BitstreamSkip(bs, 1); /* broken_link */
512 : suxen_drol 252
513 : edgomez 195 } else if (start_code == VOP_START_CODE) {
514 : suxen_drol 252
515 :     DPRINTF(DPRINTF_STARTCODE, "<vop>");
516 :    
517 : edgomez 677 BitstreamSkip(bs, 32); /* vop_start_code */
518 : Isibaar 3
519 : edgomez 677 coding_type = BitstreamGetBits(bs, 2); /* vop_coding_type */
520 : suxen_drol 252 DPRINTF(DPRINTF_HEADER, "coding_type %i", coding_type);
521 : Isibaar 3
522 : edgomez 677 /* *************************** for decode B-frame time *********************** */
523 :     while (BitstreamGetBit(bs) != 0) /* time_base */
524 : chenm001 156 time_incr++;
525 : edgomez 195
526 : Isibaar 3 READ_MARKER();
527 : edgomez 195
528 :     if (dec->time_inc_bits) {
529 : edgomez 677 time_increment = (BitstreamGetBits(bs, dec->time_inc_bits)); /* vop_time_increment */
530 : Isibaar 3 }
531 : suxen_drol 240
532 : suxen_drol 252 DPRINTF(DPRINTF_HEADER, "time_base %i", time_incr);
533 :     DPRINTF(DPRINTF_HEADER, "time_increment %i", time_increment);
534 :    
535 :     DPRINTF(DPRINTF_TIMECODE, "%c %i:%i",
536 : suxen_drol 240 coding_type == I_VOP ? 'I' : coding_type == P_VOP ? 'P' : 'B',
537 :     time_incr, time_increment);
538 :    
539 : edgomez 195 if (coding_type != B_VOP) {
540 :     dec->last_time_base = dec->time_base;
541 : chenm001 156 dec->time_base += time_incr;
542 : chl 316 dec->time = time_increment;
543 :    
544 :     /* dec->time_base * time_increment_resolution +
545 : edgomez 195 time_increment;
546 : chl 316 */ dec->time_pp = (uint32_t)
547 : chl 313 (time_increment_resolution + dec->time - dec->last_non_b_time)%time_increment_resolution;
548 : edgomez 195 dec->last_non_b_time = dec->time;
549 :     } else {
550 : chl 316 dec->time = time_increment;
551 :     /*
552 : edgomez 195 (dec->last_time_base +
553 : chl 316 time_incr) * time_increment_resolution + time_increment;
554 :     */
555 : chl 313 dec->time_bp = (uint32_t)
556 :     (time_increment_resolution + dec->last_non_b_time - dec->time)%time_increment_resolution;
557 : chenm001 156 }
558 : Isibaar 3
559 :     READ_MARKER();
560 :    
561 : edgomez 677 if (!BitstreamGetBit(bs)) /* vop_coded */
562 : Isibaar 3 {
563 : suxen_drol 252 DPRINTF(DPRINTF_HEADER, "vop_coded==false");
564 : Isibaar 3 return N_VOP;
565 :     }
566 :    
567 :     /* if (newpred_enable)
568 : edgomez 195 {
569 :     }
570 :     */
571 : Isibaar 3
572 : edgomez 677 /* fix a little bug by MinChen <chenm002@163.com> */
573 : edgomez 195 if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) &&
574 :     (coding_type == P_VOP)) {
575 : edgomez 677 *rounding = BitstreamGetBit(bs); /* rounding_type */
576 : suxen_drol 252 DPRINTF(DPRINTF_HEADER, "rounding %i", *rounding);
577 : Isibaar 3 }
578 :    
579 :     /* if (reduced_resolution_enable)
580 : edgomez 195 {
581 :     }
582 :     */
583 : Isibaar 3
584 : edgomez 195 if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {
585 : Isibaar 3 uint32_t width, height;
586 :     uint32_t horiz_mc_ref, vert_mc_ref;
587 : edgomez 195
588 : Isibaar 3 width = BitstreamGetBits(bs, 13);
589 :     READ_MARKER();
590 :     height = BitstreamGetBits(bs, 13);
591 :     READ_MARKER();
592 :     horiz_mc_ref = BitstreamGetBits(bs, 13);
593 :     READ_MARKER();
594 :     vert_mc_ref = BitstreamGetBits(bs, 13);
595 :     READ_MARKER();
596 :    
597 : suxen_drol 252 DPRINTF(DPRINTF_HEADER, "width %i", width);
598 :     DPRINTF(DPRINTF_HEADER, "height %i", height);
599 :     DPRINTF(DPRINTF_HEADER, "horiz_mc_ref %i", horiz_mc_ref);
600 :     DPRINTF(DPRINTF_HEADER, "vert_mc_ref %i", vert_mc_ref);
601 : Isibaar 3
602 : edgomez 677 BitstreamSkip(bs, 1); /* change_conv_ratio_disable */
603 :     if (BitstreamGetBit(bs)) /* vop_constant_alpha */
604 : Isibaar 3 {
605 : edgomez 677 BitstreamSkip(bs, 8); /* vop_constant_alpha_value */
606 : Isibaar 3 }
607 :     }
608 :    
609 : edgomez 195
610 :     if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) {
611 : edgomez 677 /* intra_dc_vlc_threshold */
612 : edgomez 195 *intra_dc_threshold =
613 :     intra_dc_threshold_table[BitstreamGetBits(bs, 3)];
614 : Isibaar 3
615 : edgomez 195 if (dec->interlacing) {
616 : suxen_drol 252 dec->top_field_first = BitstreamGetBit(bs);
617 :     DPRINTF(DPRINTF_HEADER, "interlace top_field_first %i", dec->top_field_first);
618 :     dec->alternate_vertical_scan = BitstreamGetBit(bs);
619 :     DPRINTF(DPRINTF_HEADER, "interlace alternate_vertical_scan %i", dec->alternate_vertical_scan);
620 :    
621 : h 69 }
622 : Isibaar 3 }
623 : edgomez 195
624 : edgomez 677 if ((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1) /* vop_quant */
625 : Isibaar 157 *quant = 1;
626 :    
627 : suxen_drol 252 DPRINTF(DPRINTF_HEADER, "quant %i", *quant);
628 : edgomez 195
629 :     if (coding_type != I_VOP) {
630 : edgomez 677 *fcode_forward = BitstreamGetBits(bs, 3); /* fcode_forward */
631 : suxen_drol 252 DPRINTF(DPRINTF_HEADER, "fcode_forward %i", *fcode_forward);
632 : Isibaar 3 }
633 : edgomez 195
634 :     if (coding_type == B_VOP) {
635 : edgomez 677 *fcode_backward = BitstreamGetBits(bs, 3); /* fcode_backward */
636 : suxen_drol 252 DPRINTF(DPRINTF_HEADER, "fcode_backward %i", *fcode_backward);
637 : Isibaar 3 }
638 : edgomez 195 if (!dec->scalability) {
639 :     if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) &&
640 :     (coding_type != I_VOP)) {
641 : edgomez 677 BitstreamSkip(bs, 1); /* vop_shape_coding_type */
642 : chenm001 156 }
643 :     }
644 : Isibaar 3 return coding_type;
645 : suxen_drol 252
646 : edgomez 195 } else if (start_code == USERDATA_START_CODE) {
647 : suxen_drol 252
648 :     DPRINTF(DPRINTF_STARTCODE, "<user_data>");
649 :    
650 : edgomez 677 BitstreamSkip(bs, 32); /* user_data_start_code */
651 : suxen_drol 252
652 : edgomez 677 } else /* start_code == ? */
653 : Isibaar 3 {
654 : edgomez 195 if (BitstreamShowBits(bs, 24) == 0x000001) {
655 : suxen_drol 252 DPRINTF(DPRINTF_STARTCODE, "<unknown: %x>", BitstreamShowBits(bs, 32));
656 : Isibaar 3 }
657 :     BitstreamSkip(bs, 8);
658 :     }
659 :     }
660 :     while ((BitstreamPos(bs) >> 3) < bs->length);
661 :    
662 : edgomez 677 /*DPRINTF("*** WARNING: no vop_start_code found"); */
663 : edgomez 195 return -1; /* ignore it */
664 : Isibaar 3 }
665 :    
666 :    
667 :     /* write custom quant matrix */
668 :    
669 : edgomez 195 static void
670 :     bs_put_matrix(Bitstream * bs,
671 :     const int16_t * matrix)
672 : Isibaar 3 {
673 :     int i, j;
674 :     const int last = matrix[scan_tables[0][63]];
675 :    
676 : suxen_drol 233 for (j = 63; j > 0 && matrix[scan_tables[0][j - 1]] == last; j--);
677 : Isibaar 3
678 : edgomez 195 for (i = 0; i <= j; i++) {
679 : Isibaar 3 BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8);
680 :     }
681 :    
682 : edgomez 195 if (j < 63) {
683 : Isibaar 3 BitstreamPutBits(bs, 0, 8);
684 :     }
685 :     }
686 :    
687 :    
688 :     /*
689 :     write vol header
690 :     */
691 : edgomez 195 void
692 :     BitstreamWriteVolHeader(Bitstream * const bs,
693 :     const MBParam * pParam,
694 :     const FRAMEINFO * frame)
695 : Isibaar 3 {
696 : edgomez 677 /* video object_start_code & vo_id */
697 : edgomez 195 BitstreamPad(bs);
698 : Isibaar 3 BitstreamPutBits(bs, VO_START_CODE, 27);
699 : edgomez 195 BitstreamPutBits(bs, 0, 5);
700 : Isibaar 3
701 : edgomez 677 /* video_object_layer_start_code & vol_id */
702 : Isibaar 3 BitstreamPutBits(bs, VOL_START_CODE, 28);
703 :     BitstreamPutBits(bs, 0, 4);
704 :    
705 : edgomez 677 BitstreamPutBit(bs, 0); /* random_accessible_vol */
706 :     BitstreamPutBits(bs, 0, 8); /* video_object_type_indication */
707 :     BitstreamPutBit(bs, 0); /* is_object_layer_identified (0=not given) */
708 :     BitstreamPutBits(bs, 1, 4); /* aspect_ratio_info (1=1:1) */
709 : suxen_drol 164
710 : edgomez 677 BitstreamPutBit(bs, 1); /* vol_control_parameters */
711 :     BitstreamPutBits(bs, 1, 2); /* chroma_format 1="4:2:0" */
712 : chl 387
713 : edgomez 677 BitstreamPutBit(bs, 1); /* low_delay */
714 : edgomez 487
715 : edgomez 677 BitstreamPutBit(bs, 0); /* vbv_parameters (0=not given) */
716 : suxen_drol 164
717 : edgomez 677 BitstreamPutBits(bs, 0, 2); /* video_object_layer_shape (0=rectangular) */
718 : Isibaar 3
719 :     WRITE_MARKER();
720 : edgomez 195
721 : edgomez 487 /*
722 :     * time_increment_resolution; ignored by current decore versions
723 :     * eg. 2fps res=2 inc=1
724 :     * 25fps res=25 inc=1
725 :     * 29.97fps res=30000 inc=1001
726 : edgomez 195 */
727 : suxen_drol 163 BitstreamPutBits(bs, pParam->fbase, 16);
728 : Isibaar 3
729 : edgomez 487
730 : Isibaar 3 WRITE_MARKER();
731 :    
732 : edgomez 677 BitstreamPutBit(bs, 1); /* fixed_vop_rate = 1 */
733 :     BitstreamPutBits(bs, pParam->fincr, log2bin(pParam->fbase)); /* fixed_vop_time_increment */
734 : Isibaar 3
735 :     WRITE_MARKER();
736 : edgomez 677 BitstreamPutBits(bs, pParam->width, 13); /* width */
737 : Isibaar 3 WRITE_MARKER();
738 : edgomez 677 BitstreamPutBits(bs, pParam->height, 13); /* height */
739 : Isibaar 3 WRITE_MARKER();
740 : edgomez 195
741 : edgomez 677 BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING); /* interlace */
742 :     BitstreamPutBit(bs, 1); /* obmc_disable (overlapped block motion compensation) */
743 :     BitstreamPutBit(bs, 0); /* sprite_enable */
744 :     BitstreamPutBit(bs, 0); /* not_in_bit */
745 : Isibaar 3
746 : edgomez 677 /* quant_type 0=h.263 1=mpeg4(quantizer tables) */
747 : suxen_drol 136 BitstreamPutBit(bs, pParam->m_quant_type);
748 : edgomez 195
749 :     if (pParam->m_quant_type) {
750 : edgomez 677 BitstreamPutBit(bs, get_intra_matrix_status()); /* load_intra_quant_mat */
751 : edgomez 195 if (get_intra_matrix_status()) {
752 : Isibaar 4 bs_put_matrix(bs, get_intra_matrix());
753 : Isibaar 3 }
754 :    
755 : edgomez 677 BitstreamPutBit(bs, get_inter_matrix_status()); /* load_inter_quant_mat */
756 : edgomez 195 if (get_inter_matrix_status()) {
757 : Isibaar 4 bs_put_matrix(bs, get_inter_matrix());
758 : Isibaar 3 }
759 :    
760 :     }
761 :    
762 : edgomez 677 BitstreamPutBit(bs, 1); /* complexity_estimation_disable */
763 :     BitstreamPutBit(bs, 1); /* resync_marker_disable */
764 :     BitstreamPutBit(bs, 0); /* data_partitioned */
765 :     BitstreamPutBit(bs, 0); /* scalability */
766 : Isibaar 3 }
767 :    
768 :    
769 :     /*
770 :     write vop header
771 :    
772 :     NOTE: doesnt handle bother with time_base & time_inc
773 :     time_base = n seconds since last resync (eg. last iframe)
774 :     time_inc = nth of a second since last resync
775 :     (decoder uses these values to determine precise time since last resync)
776 :     */
777 : edgomez 195 void
778 :     BitstreamWriteVopHeader(Bitstream * const bs,
779 :     const MBParam * pParam,
780 : suxen_drol 229 const FRAMEINFO * frame,
781 :     int vop_coded)
782 : Isibaar 3 {
783 : suxen_drol 152 uint32_t i;
784 : chl 387
785 : edgomez 195 BitstreamPad(bs);
786 :     BitstreamPutBits(bs, VOP_START_CODE, 32);
787 : Isibaar 3
788 : edgomez 195 BitstreamPutBits(bs, frame->coding_type, 2);
789 :    
790 : edgomez 677 /* time_base = 0 write n x PutBit(1), PutBit(0) */
791 : edgomez 195 for (i = 0; i < frame->seconds; i++) {
792 : suxen_drol 152 BitstreamPutBit(bs, 1);
793 :     }
794 :     BitstreamPutBit(bs, 0);
795 : Isibaar 3
796 :     WRITE_MARKER();
797 :    
798 : edgomez 677 /* time_increment: value=nth_of_sec, nbits = log2(resolution) */
799 : edgomez 195 BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase));
800 : Isibaar 3
801 :     WRITE_MARKER();
802 :    
803 : suxen_drol 229 if (!vop_coded) {
804 :     BitstreamPutBits(bs, 0, 1);
805 :     return;
806 :     }
807 :    
808 : edgomez 677 BitstreamPutBits(bs, 1, 1); /* vop_coded */
809 : Isibaar 3
810 : suxen_drol 163 if (frame->coding_type == P_VOP)
811 : suxen_drol 136 BitstreamPutBits(bs, frame->rounding_type, 1);
812 : Isibaar 3
813 : edgomez 677 BitstreamPutBits(bs, 0, 3); /* intra_dc_vlc_threshold */
814 : edgomez 195
815 :     if (frame->global_flags & XVID_INTERLACING) {
816 : edgomez 677 BitstreamPutBit(bs, 1); /* top field first */
817 :     BitstreamPutBit(bs, 0); /* alternate vertical scan */
818 : h 69 }
819 :    
820 : edgomez 677 BitstreamPutBits(bs, frame->quant, 5); /* quantizer */
821 : edgomez 195
822 : suxen_drol 136 if (frame->coding_type != I_VOP)
823 : edgomez 677 BitstreamPutBits(bs, frame->fcode, 3); /* forward_fixed_code */
824 : suxen_drol 152
825 :     if (frame->coding_type == B_VOP)
826 : edgomez 677 BitstreamPutBits(bs, frame->bcode, 3); /* backward_fixed_code */
827 : suxen_drol 152
828 : Isibaar 3 }

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