1 /*****************************************************************************
2 *
3 * XVID MPEG-4 VIDEO CODEC
4 * - Bitstream reader/writer -
5 *
6 * Copyright (C) 2001-2003 Peter Ross <pross@xvid.org>
7 * 2003 Cristoph Lampert <gruel@web.de>
8 *
9 * This program is free software ; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation ; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY ; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program ; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * $Id: bitstream.c 2180 2019-11-12 14:48:35Z Isibaar $
24 *
25 ****************************************************************************/
26
27 #include <string.h>
28 #include <stdio.h>
29
30 #include "bitstream.h"
31 #include "zigzag.h"
32 #include "../quant/quant_matrix.h"
33 #include "mbcoding.h"
34
35
36 static const uint8_t log2_tab_16[16] = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 };
37
log2bin(uint32_t value)38 static uint32_t __inline log2bin(uint32_t value)
39 {
40 int n = 0;
41 if (value & 0xffff0000) {
42 value >>= 16;
43 n += 16;
44 }
45 if (value & 0xff00) {
46 value >>= 8;
47 n += 8;
48 }
49 if (value & 0xf0) {
50 value >>= 4;
51 n += 4;
52 }
53 return n + log2_tab_16[value];
54 }
55
56 static const uint32_t intra_dc_threshold_table[] = {
57 32, /* never use */
58 13,
59 15,
60 17,
61 19,
62 21,
63 23,
64 1,
65 };
66
67
68 static void
bs_get_matrix(Bitstream * bs,uint8_t * matrix)69 bs_get_matrix(Bitstream * bs,
70 uint8_t * matrix)
71 {
72 int i = 0;
73 int last, value = 0;
74
75 do {
76 last = value;
77 value = BitstreamGetBits(bs, 8);
78 matrix[scan_tables[0][i++]] = value;
79 }
80 while (value != 0 && i < 64);
81
82 if (value != 0) return;
83
84 i--;
85 while (i < 64) {
86 matrix[scan_tables[0][i++]] = last;
87 }
88 }
89
90
91
92 /*
93 * for PVOP addbits == fcode - 1
94 * for BVOP addbits == max(fcode,bcode) - 1
95 * returns mbpos
96 */
97 int
read_video_packet_header(Bitstream * bs,DECODER * dec,const int addbits,int * quant,int * fcode_forward,int * fcode_backward,int * intra_dc_threshold)98 read_video_packet_header(Bitstream *bs,
99 DECODER * dec,
100 const int addbits,
101 int * quant,
102 int * fcode_forward,
103 int * fcode_backward,
104 int * intra_dc_threshold)
105 {
106 int startcode_bits = NUMBITS_VP_RESYNC_MARKER + addbits;
107 int mbnum_bits = log2bin(dec->mb_width * dec->mb_height - 1);
108 int mbnum;
109 int hec = 0;
110
111 BitstreamSkip(bs, BitstreamNumBitsToByteAlign(bs));
112 BitstreamSkip(bs, startcode_bits);
113
114 DPRINTF(XVID_DEBUG_STARTCODE, "<video_packet_header>\n");
115
116 if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)
117 {
118 hec = BitstreamGetBit(bs); /* header_extension_code */
119 if (hec && !(dec->sprite_enable == SPRITE_STATIC /* && current_coding_type = I_VOP */))
120 {
121 BitstreamSkip(bs, 13); /* vop_width */
122 READ_MARKER();
123 BitstreamSkip(bs, 13); /* vop_height */
124 READ_MARKER();
125 BitstreamSkip(bs, 13); /* vop_horizontal_mc_spatial_ref */
126 READ_MARKER();
127 BitstreamSkip(bs, 13); /* vop_vertical_mc_spatial_ref */
128 READ_MARKER();
129 }
130 }
131
132 mbnum = BitstreamGetBits(bs, mbnum_bits); /* macroblock_number */
133 DPRINTF(XVID_DEBUG_HEADER, "mbnum %i\n", mbnum);
134
135 if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY)
136 {
137 *quant = BitstreamGetBits(bs, dec->quant_bits); /* quant_scale */
138 DPRINTF(XVID_DEBUG_HEADER, "quant %i\n", *quant);
139 }
140
141 if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR)
142 hec = BitstreamGetBit(bs); /* header_extension_code */
143
144
145 DPRINTF(XVID_DEBUG_HEADER, "header_extension_code %i\n", hec);
146 if (hec)
147 {
148 int time_base;
149 int time_increment;
150 int coding_type;
151
152 for (time_base=0; BitstreamGetBit(bs)!=0; time_base++); /* modulo_time_base */
153 READ_MARKER();
154 if (dec->time_inc_bits)
155 time_increment = (BitstreamGetBits(bs, dec->time_inc_bits)); /* vop_time_increment */
156 else
157 time_increment = 0;
158 READ_MARKER();
159 DPRINTF(XVID_DEBUG_HEADER,"time %i:%i\n", time_base, time_increment);
160
161 coding_type = BitstreamGetBits(bs, 2);
162 DPRINTF(XVID_DEBUG_HEADER,"coding_type %i\n", coding_type);
163
164 if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)
165 {
166 BitstreamSkip(bs, 1); /* change_conv_ratio_disable */
167 if (coding_type != I_VOP)
168 BitstreamSkip(bs, 1); /* vop_shape_coding_type */
169 }
170
171 if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY)
172 {
173 *intra_dc_threshold = intra_dc_threshold_table[BitstreamGetBits(bs, 3)];
174
175 if (dec->sprite_enable == SPRITE_GMC && coding_type == S_VOP &&
176 dec->sprite_warping_points > 0)
177 {
178 /* TODO: sprite trajectory */
179 }
180 if (dec->reduced_resolution_enable &&
181 dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR &&
182 (coding_type == P_VOP || coding_type == I_VOP))
183 {
184 BitstreamSkip(bs, 1); /* XXX: vop_reduced_resolution */
185 }
186
187 if (coding_type != I_VOP && fcode_forward)
188 {
189 *fcode_forward = BitstreamGetBits(bs, 3);
190 DPRINTF(XVID_DEBUG_HEADER,"fcode_forward %i\n", *fcode_forward);
191 }
192
193 if (coding_type == B_VOP && fcode_backward)
194 {
195 *fcode_backward = BitstreamGetBits(bs, 3);
196 DPRINTF(XVID_DEBUG_HEADER,"fcode_backward %i\n", *fcode_backward);
197 }
198 }
199 }
200
201 if (dec->newpred_enable)
202 {
203 int vop_id;
204 int vop_id_for_prediction;
205
206 vop_id = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15));
207 DPRINTF(XVID_DEBUG_HEADER, "vop_id %i\n", vop_id);
208 if (BitstreamGetBit(bs)) /* vop_id_for_prediction_indication */
209 {
210 vop_id_for_prediction = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15));
211 DPRINTF(XVID_DEBUG_HEADER, "vop_id_for_prediction %i\n", vop_id_for_prediction);
212 }
213 READ_MARKER();
214 }
215
216 return mbnum;
217 }
218
219
220
221
222 /* vol estimation header */
223 static void
read_vol_complexity_estimation_header(Bitstream * bs,DECODER * dec)224 read_vol_complexity_estimation_header(Bitstream * bs, DECODER * dec)
225 {
226 ESTIMATION * e = &dec->estimation;
227
228 e->method = BitstreamGetBits(bs, 2); /* estimation_method */
229 DPRINTF(XVID_DEBUG_HEADER,"+ complexity_estimation_header; method=%i\n", e->method);
230
231 if (e->method == 0 || e->method == 1)
232 {
233 if (!BitstreamGetBit(bs)) /* shape_complexity_estimation_disable */
234 {
235 e->opaque = BitstreamGetBit(bs); /* opaque */
236 e->transparent = BitstreamGetBit(bs); /* transparent */
237 e->intra_cae = BitstreamGetBit(bs); /* intra_cae */
238 e->inter_cae = BitstreamGetBit(bs); /* inter_cae */
239 e->no_update = BitstreamGetBit(bs); /* no_update */
240 e->upsampling = BitstreamGetBit(bs); /* upsampling */
241 }
242
243 if (!BitstreamGetBit(bs)) /* texture_complexity_estimation_set_1_disable */
244 {
245 e->intra_blocks = BitstreamGetBit(bs); /* intra_blocks */
246 e->inter_blocks = BitstreamGetBit(bs); /* inter_blocks */
247 e->inter4v_blocks = BitstreamGetBit(bs); /* inter4v_blocks */
248 e->not_coded_blocks = BitstreamGetBit(bs); /* not_coded_blocks */
249 }
250 }
251
252 READ_MARKER();
253
254 if (!BitstreamGetBit(bs)) /* texture_complexity_estimation_set_2_disable */
255 {
256 e->dct_coefs = BitstreamGetBit(bs); /* dct_coefs */
257 e->dct_lines = BitstreamGetBit(bs); /* dct_lines */
258 e->vlc_symbols = BitstreamGetBit(bs); /* vlc_symbols */
259 e->vlc_bits = BitstreamGetBit(bs); /* vlc_bits */
260 }
261
262 if (!BitstreamGetBit(bs)) /* motion_compensation_complexity_disable */
263 {
264 e->apm = BitstreamGetBit(bs); /* apm */
265 e->npm = BitstreamGetBit(bs); /* npm */
266 e->interpolate_mc_q = BitstreamGetBit(bs); /* interpolate_mc_q */
267 e->forw_back_mc_q = BitstreamGetBit(bs); /* forw_back_mc_q */
268 e->halfpel2 = BitstreamGetBit(bs); /* halfpel2 */
269 e->halfpel4 = BitstreamGetBit(bs); /* halfpel4 */
270 }
271
272 READ_MARKER();
273
274 if (e->method == 1)
275 {
276 if (!BitstreamGetBit(bs)) /* version2_complexity_estimation_disable */
277 {
278 e->sadct = BitstreamGetBit(bs); /* sadct */
279 e->quarterpel = BitstreamGetBit(bs); /* quarterpel */
280 }
281 }
282 }
283
284
285 /* vop estimation header */
286 static void
read_vop_complexity_estimation_header(Bitstream * bs,DECODER * dec,int coding_type)287 read_vop_complexity_estimation_header(Bitstream * bs, DECODER * dec, int coding_type)
288 {
289 ESTIMATION * e = &dec->estimation;
290
291 if (e->method == 0 || e->method == 1)
292 {
293 if (coding_type == I_VOP) {
294 if (e->opaque) BitstreamSkip(bs, 8); /* dcecs_opaque */
295 if (e->transparent) BitstreamSkip(bs, 8); /* */
296 if (e->intra_cae) BitstreamSkip(bs, 8); /* */
297 if (e->inter_cae) BitstreamSkip(bs, 8); /* */
298 if (e->no_update) BitstreamSkip(bs, 8); /* */
299 if (e->upsampling) BitstreamSkip(bs, 8); /* */
300 if (e->intra_blocks) BitstreamSkip(bs, 8); /* */
301 if (e->not_coded_blocks) BitstreamSkip(bs, 8); /* */
302 if (e->dct_coefs) BitstreamSkip(bs, 8); /* */
303 if (e->dct_lines) BitstreamSkip(bs, 8); /* */
304 if (e->vlc_symbols) BitstreamSkip(bs, 8); /* */
305 if (e->vlc_bits) BitstreamSkip(bs, 8); /* */
306 if (e->sadct) BitstreamSkip(bs, 8); /* */
307 }
308
309 if (coding_type == P_VOP) {
310 if (e->opaque) BitstreamSkip(bs, 8); /* */
311 if (e->transparent) BitstreamSkip(bs, 8); /* */
312 if (e->intra_cae) BitstreamSkip(bs, 8); /* */
313 if (e->inter_cae) BitstreamSkip(bs, 8); /* */
314 if (e->no_update) BitstreamSkip(bs, 8); /* */
315 if (e->upsampling) BitstreamSkip(bs, 8); /* */
316 if (e->intra_blocks) BitstreamSkip(bs, 8); /* */
317 if (e->not_coded_blocks) BitstreamSkip(bs, 8); /* */
318 if (e->dct_coefs) BitstreamSkip(bs, 8); /* */
319 if (e->dct_lines) BitstreamSkip(bs, 8); /* */
320 if (e->vlc_symbols) BitstreamSkip(bs, 8); /* */
321 if (e->vlc_bits) BitstreamSkip(bs, 8); /* */
322 if (e->inter_blocks) BitstreamSkip(bs, 8); /* */
323 if (e->inter4v_blocks) BitstreamSkip(bs, 8); /* */
324 if (e->apm) BitstreamSkip(bs, 8); /* */
325 if (e->npm) BitstreamSkip(bs, 8); /* */
326 if (e->forw_back_mc_q) BitstreamSkip(bs, 8); /* */
327 if (e->halfpel2) BitstreamSkip(bs, 8); /* */
328 if (e->halfpel4) BitstreamSkip(bs, 8); /* */
329 if (e->sadct) BitstreamSkip(bs, 8); /* */
330 if (e->quarterpel) BitstreamSkip(bs, 8); /* */
331 }
332 if (coding_type == B_VOP) {
333 if (e->opaque) BitstreamSkip(bs, 8); /* */
334 if (e->transparent) BitstreamSkip(bs, 8); /* */
335 if (e->intra_cae) BitstreamSkip(bs, 8); /* */
336 if (e->inter_cae) BitstreamSkip(bs, 8); /* */
337 if (e->no_update) BitstreamSkip(bs, 8); /* */
338 if (e->upsampling) BitstreamSkip(bs, 8); /* */
339 if (e->intra_blocks) BitstreamSkip(bs, 8); /* */
340 if (e->not_coded_blocks) BitstreamSkip(bs, 8); /* */
341 if (e->dct_coefs) BitstreamSkip(bs, 8); /* */
342 if (e->dct_lines) BitstreamSkip(bs, 8); /* */
343 if (e->vlc_symbols) BitstreamSkip(bs, 8); /* */
344 if (e->vlc_bits) BitstreamSkip(bs, 8); /* */
345 if (e->inter_blocks) BitstreamSkip(bs, 8); /* */
346 if (e->inter4v_blocks) BitstreamSkip(bs, 8); /* */
347 if (e->apm) BitstreamSkip(bs, 8); /* */
348 if (e->npm) BitstreamSkip(bs, 8); /* */
349 if (e->forw_back_mc_q) BitstreamSkip(bs, 8); /* */
350 if (e->halfpel2) BitstreamSkip(bs, 8); /* */
351 if (e->halfpel4) BitstreamSkip(bs, 8); /* */
352 if (e->interpolate_mc_q) BitstreamSkip(bs, 8); /* */
353 if (e->sadct) BitstreamSkip(bs, 8); /* */
354 if (e->quarterpel) BitstreamSkip(bs, 8); /* */
355 }
356
357 if (coding_type == S_VOP && dec->sprite_enable == SPRITE_STATIC) {
358 if (e->intra_blocks) BitstreamSkip(bs, 8); /* */
359 if (e->not_coded_blocks) BitstreamSkip(bs, 8); /* */
360 if (e->dct_coefs) BitstreamSkip(bs, 8); /* */
361 if (e->dct_lines) BitstreamSkip(bs, 8); /* */
362 if (e->vlc_symbols) BitstreamSkip(bs, 8); /* */
363 if (e->vlc_bits) BitstreamSkip(bs, 8); /* */
364 if (e->inter_blocks) BitstreamSkip(bs, 8); /* */
365 if (e->inter4v_blocks) BitstreamSkip(bs, 8); /* */
366 if (e->apm) BitstreamSkip(bs, 8); /* */
367 if (e->npm) BitstreamSkip(bs, 8); /* */
368 if (e->forw_back_mc_q) BitstreamSkip(bs, 8); /* */
369 if (e->halfpel2) BitstreamSkip(bs, 8); /* */
370 if (e->halfpel4) BitstreamSkip(bs, 8); /* */
371 if (e->interpolate_mc_q) BitstreamSkip(bs, 8); /* */
372 }
373 }
374 }
375
376
377
378
379
380 /*
381 decode headers
382 returns coding_type, or -1 if error
383 */
384
385 #define VIDOBJ_START_CODE_MASK 0x0000001f
386 #define VIDOBJLAY_START_CODE_MASK 0x0000000f
387
388 int
BitstreamReadHeaders(Bitstream * bs,DECODER * dec,uint32_t * rounding,uint32_t * quant,uint32_t * fcode_forward,uint32_t * fcode_backward,uint32_t * intra_dc_threshold,WARPPOINTS * gmc_warp)389 BitstreamReadHeaders(Bitstream * bs,
390 DECODER * dec,
391 uint32_t * rounding,
392 uint32_t * quant,
393 uint32_t * fcode_forward,
394 uint32_t * fcode_backward,
395 uint32_t * intra_dc_threshold,
396 WARPPOINTS *gmc_warp)
397 {
398 uint32_t vol_ver_id;
399 uint32_t coding_type;
400 uint32_t start_code;
401 uint32_t time_incr = 0;
402 int32_t time_increment = 0;
403
404 while ((BitstreamPos(bs) >> 3) + 4 <= bs->length) {
405
406 BitstreamByteAlign(bs);
407 start_code = BitstreamShowBits(bs, 32);
408
409 if (start_code == VISOBJSEQ_START_CODE) {
410
411 int profile;
412
413 DPRINTF(XVID_DEBUG_STARTCODE, "<visual_object_sequence>\n");
414
415 BitstreamSkip(bs, 32); /* visual_object_sequence_start_code */
416 profile = BitstreamGetBits(bs, 8); /* profile_and_level_indication */
417
418 DPRINTF(XVID_DEBUG_HEADER, "profile_and_level_indication %i\n", profile);
419
420 } else if (start_code == VISOBJSEQ_STOP_CODE) {
421
422 BitstreamSkip(bs, 32); /* visual_object_sequence_stop_code */
423
424 DPRINTF(XVID_DEBUG_STARTCODE, "</visual_object_sequence>\n");
425
426 } else if (start_code == VISOBJ_START_CODE) {
427
428 DPRINTF(XVID_DEBUG_STARTCODE, "<visual_object>\n");
429
430 BitstreamSkip(bs, 32); /* visual_object_start_code */
431 if (BitstreamGetBit(bs)) /* is_visual_object_identified */
432 {
433 dec->ver_id = BitstreamGetBits(bs, 4); /* visual_object_ver_id */
434 DPRINTF(XVID_DEBUG_HEADER,"visobj_ver_id %i\n", dec->ver_id);
435 BitstreamSkip(bs, 3); /* visual_object_priority */
436 } else {
437 dec->ver_id = 1;
438 }
439
440 if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) /* visual_object_type */
441 {
442 DPRINTF(XVID_DEBUG_ERROR, "visual_object_type != video\n");
443 return -1;
444 }
445 BitstreamSkip(bs, 4);
446
447 /* video_signal_type */
448
449 if (BitstreamGetBit(bs)) /* video_signal_type */
450 {
451 DPRINTF(XVID_DEBUG_HEADER,"+ video_signal_type\n");
452 BitstreamSkip(bs, 3); /* video_format */
453 BitstreamSkip(bs, 1); /* video_range */
454 if (BitstreamGetBit(bs)) /* color_description */
455 {
456 DPRINTF(XVID_DEBUG_HEADER,"+ color_description");
457 BitstreamSkip(bs, 8); /* color_primaries */
458 BitstreamSkip(bs, 8); /* transfer_characteristics */
459 BitstreamSkip(bs, 8); /* matrix_coefficients */
460 }
461 }
462 } else if ((start_code & ~VIDOBJ_START_CODE_MASK) == VIDOBJ_START_CODE) {
463
464 DPRINTF(XVID_DEBUG_STARTCODE, "<video_object>\n");
465 DPRINTF(XVID_DEBUG_HEADER, "vo id %i\n", start_code & VIDOBJ_START_CODE_MASK);
466
467 BitstreamSkip(bs, 32); /* video_object_start_code */
468
469 } else if ((start_code & ~VIDOBJLAY_START_CODE_MASK) == VIDOBJLAY_START_CODE) {
470 uint32_t width = 0, height = 0;
471
472 DPRINTF(XVID_DEBUG_STARTCODE, "<video_object_layer>\n");
473 DPRINTF(XVID_DEBUG_HEADER, "vol id %i\n", start_code & VIDOBJLAY_START_CODE_MASK);
474
475 BitstreamSkip(bs, 32); /* video_object_layer_start_code */
476 BitstreamSkip(bs, 1); /* random_accessible_vol */
477
478 BitstreamSkip(bs, 8); /* video_object_type_indication */
479
480 if (BitstreamGetBit(bs)) /* is_object_layer_identifier */
481 {
482 DPRINTF(XVID_DEBUG_HEADER, "+ is_object_layer_identifier\n");
483 vol_ver_id = BitstreamGetBits(bs, 4); /* video_object_layer_verid */
484 DPRINTF(XVID_DEBUG_HEADER,"ver_id %i\n", vol_ver_id);
485 BitstreamSkip(bs, 3); /* video_object_layer_priority */
486 } else {
487 vol_ver_id = dec->ver_id;
488 }
489
490 dec->aspect_ratio = BitstreamGetBits(bs, 4);
491
492 if (dec->aspect_ratio == VIDOBJLAY_AR_EXTPAR) /* aspect_ratio_info */
493 {
494 DPRINTF(XVID_DEBUG_HEADER, "+ aspect_ratio_info\n");
495 dec->par_width = BitstreamGetBits(bs, 8); /* par_width */
496 dec->par_height = BitstreamGetBits(bs, 8); /* par_height */
497 }
498
499 if (BitstreamGetBit(bs)) /* vol_control_parameters */
500 {
501 DPRINTF(XVID_DEBUG_HEADER, "+ vol_control_parameters\n");
502 BitstreamSkip(bs, 2); /* chroma_format */
503 dec->low_delay = BitstreamGetBit(bs); /* low_delay */
504 DPRINTF(XVID_DEBUG_HEADER, "low_delay %i\n", dec->low_delay);
505 if (BitstreamGetBit(bs)) /* vbv_parameters */
506 {
507 unsigned int bitrate;
508 unsigned int buffer_size;
509 unsigned int occupancy;
510
511 DPRINTF(XVID_DEBUG_HEADER,"+ vbv_parameters\n");
512
513 bitrate = BitstreamGetBits(bs,15) << 15; /* first_half_bit_rate */
514 READ_MARKER();
515 bitrate |= BitstreamGetBits(bs,15); /* latter_half_bit_rate */
516 READ_MARKER();
517
518 buffer_size = BitstreamGetBits(bs, 15) << 3; /* first_half_vbv_buffer_size */
519 READ_MARKER();
520 buffer_size |= BitstreamGetBits(bs, 3); /* latter_half_vbv_buffer_size */
521
522 occupancy = BitstreamGetBits(bs, 11) << 15; /* first_half_vbv_occupancy */
523 READ_MARKER();
524 occupancy |= BitstreamGetBits(bs, 15); /* latter_half_vbv_occupancy */
525 READ_MARKER();
526
527 DPRINTF(XVID_DEBUG_HEADER,"bitrate %d (unit=400 bps)\n", bitrate);
528 DPRINTF(XVID_DEBUG_HEADER,"buffer_size %d (unit=16384 bits)\n", buffer_size);
529 DPRINTF(XVID_DEBUG_HEADER,"occupancy %d (unit=64 bits)\n", occupancy);
530 }
531 }else{
532 dec->low_delay = dec->low_delay_default;
533 }
534
535 dec->shape = BitstreamGetBits(bs, 2); /* video_object_layer_shape */
536
537 DPRINTF(XVID_DEBUG_HEADER, "shape %i\n", dec->shape);
538 if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)
539 {
540 DPRINTF(XVID_DEBUG_ERROR,"non-rectangular shapes are not supported\n");
541 }
542
543 if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) {
544 BitstreamSkip(bs, 4); /* video_object_layer_shape_extension */
545 }
546
547 READ_MARKER();
548
549 /********************** for decode B-frame time ***********************/
550 dec->time_inc_resolution = BitstreamGetBits(bs, 16); /* vop_time_increment_resolution */
551 DPRINTF(XVID_DEBUG_HEADER,"vop_time_increment_resolution %i\n", dec->time_inc_resolution);
552
553 if (dec->time_inc_resolution > 0) {
554 dec->time_inc_bits = MAX(log2bin(dec->time_inc_resolution-1), 1);
555 } else {
556 /* for "old" xvid compatibility, set time_inc_bits = 1 */
557 dec->time_inc_bits = 1;
558 }
559
560 READ_MARKER();
561
562 if (BitstreamGetBit(bs)) /* fixed_vop_rate */
563 {
564 DPRINTF(XVID_DEBUG_HEADER, "+ fixed_vop_rate\n");
565 BitstreamSkip(bs, dec->time_inc_bits); /* fixed_vop_time_increment */
566 }
567
568 if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) {
569
570 if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) {
571 READ_MARKER();
572 width = BitstreamGetBits(bs, 13); /* video_object_layer_width */
573 READ_MARKER();
574 height = BitstreamGetBits(bs, 13); /* video_object_layer_height */
575 READ_MARKER();
576
577 DPRINTF(XVID_DEBUG_HEADER, "width %i\n", width);
578 DPRINTF(XVID_DEBUG_HEADER, "height %i\n", height);
579 }
580
581 dec->interlacing = BitstreamGetBit(bs);
582 DPRINTF(XVID_DEBUG_HEADER, "interlacing %i\n", dec->interlacing);
583
584 if (!BitstreamGetBit(bs)) /* obmc_disable */
585 {
586 DPRINTF(XVID_DEBUG_ERROR, "obmc_disabled==false not supported\n");
587 /* TODO */
588 /* fucking divx4.02 has this enabled */
589 }
590
591 dec->sprite_enable = BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2)); /* sprite_enable */
592
593 if (dec->sprite_enable == SPRITE_STATIC || dec->sprite_enable == SPRITE_GMC)
594 {
595 int low_latency_sprite_enable;
596
597 if (dec->sprite_enable != SPRITE_GMC)
598 {
599 int sprite_width;
600 int sprite_height;
601 int sprite_left_coord;
602 int sprite_top_coord;
603 sprite_width = BitstreamGetBits(bs, 13); /* sprite_width */
604 READ_MARKER();
605 sprite_height = BitstreamGetBits(bs, 13); /* sprite_height */
606 READ_MARKER();
607 sprite_left_coord = BitstreamGetBits(bs, 13); /* sprite_left_coordinate */
608 READ_MARKER();
609 sprite_top_coord = BitstreamGetBits(bs, 13); /* sprite_top_coordinate */
610 READ_MARKER();
611 }
612 dec->sprite_warping_points = BitstreamGetBits(bs, 6); /* no_of_sprite_warping_points */
613 dec->sprite_warping_accuracy = BitstreamGetBits(bs, 2); /* sprite_warping_accuracy */
614 dec->sprite_brightness_change = BitstreamGetBits(bs, 1); /* brightness_change */
615 if (dec->sprite_enable != SPRITE_GMC)
616 {
617 low_latency_sprite_enable = BitstreamGetBits(bs, 1); /* low_latency_sprite_enable */
618 }
619 }
620
621 if (vol_ver_id != 1 &&
622 dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {
623 BitstreamSkip(bs, 1); /* sadct_disable */
624 }
625
626 if (BitstreamGetBit(bs)) /* not_8_bit */
627 {
628 DPRINTF(XVID_DEBUG_HEADER, "not_8_bit==true (ignored)\n");
629 dec->quant_bits = BitstreamGetBits(bs, 4); /* quant_precision */
630 BitstreamSkip(bs, 4); /* bits_per_pixel */
631 } else {
632 dec->quant_bits = 5;
633 }
634
635 if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) {
636 BitstreamSkip(bs, 1); /* no_gray_quant_update */
637 BitstreamSkip(bs, 1); /* composition_method */
638 BitstreamSkip(bs, 1); /* linear_composition */
639 }
640
641 dec->quant_type = BitstreamGetBit(bs); /* quant_type */
642 DPRINTF(XVID_DEBUG_HEADER, "quant_type %i\n", dec->quant_type);
643
644 if (dec->quant_type) {
645 if (BitstreamGetBit(bs)) /* load_intra_quant_mat */
646 {
647 uint8_t matrix[64];
648
649 DPRINTF(XVID_DEBUG_HEADER, "load_intra_quant_mat\n");
650
651 bs_get_matrix(bs, matrix);
652 set_intra_matrix(dec->mpeg_quant_matrices, matrix);
653 } else
654 set_intra_matrix(dec->mpeg_quant_matrices, get_default_intra_matrix());
655
656 if (BitstreamGetBit(bs)) /* load_inter_quant_mat */
657 {
658 uint8_t matrix[64];
659
660 DPRINTF(XVID_DEBUG_HEADER, "load_inter_quant_mat\n");
661
662 bs_get_matrix(bs, matrix);
663 set_inter_matrix(dec->mpeg_quant_matrices, matrix);
664 } else
665 set_inter_matrix(dec->mpeg_quant_matrices, get_default_inter_matrix());
666
667 if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) {
668 DPRINTF(XVID_DEBUG_ERROR, "greyscale matrix not supported\n");
669 return -1;
670 }
671
672 }
673
674
675 if (vol_ver_id != 1) {
676 dec->quarterpel = BitstreamGetBit(bs); /* quarter_sample */
677 DPRINTF(XVID_DEBUG_HEADER,"quarterpel %i\n", dec->quarterpel);
678 }
679 else
680 dec->quarterpel = 0;
681
682
683 dec->complexity_estimation_disable = BitstreamGetBit(bs); /* complexity estimation disable */
684 if (!dec->complexity_estimation_disable)
685 {
686 read_vol_complexity_estimation_header(bs, dec);
687 }
688
689 BitstreamSkip(bs, 1); /* resync_marker_disable */
690
691 if (BitstreamGetBit(bs)) /* data_partitioned */
692 {
693 DPRINTF(XVID_DEBUG_ERROR, "data_partitioned not supported\n");
694 BitstreamSkip(bs, 1); /* reversible_vlc */
695 }
696
697 if (vol_ver_id != 1) {
698 dec->newpred_enable = BitstreamGetBit(bs);
699 if (dec->newpred_enable) /* newpred_enable */
700 {
701 DPRINTF(XVID_DEBUG_HEADER, "+ newpred_enable\n");
702 BitstreamSkip(bs, 2); /* requested_upstream_message_type */
703 BitstreamSkip(bs, 1); /* newpred_segment_type */
704 }
705 dec->reduced_resolution_enable = BitstreamGetBit(bs); /* reduced_resolution_vop_enable */
706 DPRINTF(XVID_DEBUG_HEADER, "reduced_resolution_enable %i\n", dec->reduced_resolution_enable);
707 }
708 else
709 {
710 dec->newpred_enable = 0;
711 dec->reduced_resolution_enable = 0;
712 }
713
714 dec->scalability = BitstreamGetBit(bs); /* scalability */
715 if (dec->scalability)
716 {
717 DPRINTF(XVID_DEBUG_ERROR, "scalability not supported\n");
718 BitstreamSkip(bs, 1); /* hierarchy_type */
719 BitstreamSkip(bs, 4); /* ref_layer_id */
720 BitstreamSkip(bs, 1); /* ref_layer_sampling_direc */
721 BitstreamSkip(bs, 5); /* hor_sampling_factor_n */
722 BitstreamSkip(bs, 5); /* hor_sampling_factor_m */
723 BitstreamSkip(bs, 5); /* vert_sampling_factor_n */
724 BitstreamSkip(bs, 5); /* vert_sampling_factor_m */
725 BitstreamSkip(bs, 1); /* enhancement_type */
726 if(dec->shape == VIDOBJLAY_SHAPE_BINARY /* && hierarchy_type==0 */) {
727 BitstreamSkip(bs, 1); /* use_ref_shape */
728 BitstreamSkip(bs, 1); /* use_ref_texture */
729 BitstreamSkip(bs, 5); /* shape_hor_sampling_factor_n */
730 BitstreamSkip(bs, 5); /* shape_hor_sampling_factor_m */
731 BitstreamSkip(bs, 5); /* shape_vert_sampling_factor_n */
732 BitstreamSkip(bs, 5); /* shape_vert_sampling_factor_m */
733 }
734 return -1;
735 }
736 } else /* dec->shape == BINARY_ONLY */
737 {
738 if (vol_ver_id != 1) {
739 dec->scalability = BitstreamGetBit(bs); /* scalability */
740 if (dec->scalability)
741 {
742 DPRINTF(XVID_DEBUG_ERROR, "scalability not supported\n");
743 BitstreamSkip(bs, 4); /* ref_layer_id */
744 BitstreamSkip(bs, 5); /* hor_sampling_factor_n */
745 BitstreamSkip(bs, 5); /* hor_sampling_factor_m */
746 BitstreamSkip(bs, 5); /* vert_sampling_factor_n */
747 BitstreamSkip(bs, 5); /* vert_sampling_factor_m */
748 return -1;
749 }
750 }
751 BitstreamSkip(bs, 1); /* resync_marker_disable */
752
753 }
754
755 if (((width > 0) && (height > 0)) && (dec->width != width || dec->height != height))
756 {
757 if (dec->fixed_dimensions)
758 {
759 DPRINTF(XVID_DEBUG_ERROR, "decoder width/height does not match bitstream\n");
760 return -1;
761 }
762 dec->width = width;
763 dec->height = height;
764 return -3;
765 }
766
767 return -2; /* VOL */
768
769 } else if (start_code == GRPOFVOP_START_CODE) {
770
771 DPRINTF(XVID_DEBUG_STARTCODE, "<group_of_vop>\n");
772
773 BitstreamSkip(bs, 32);
774 {
775 int hours, minutes, seconds;
776
777 hours = BitstreamGetBits(bs, 5);
778 minutes = BitstreamGetBits(bs, 6);
779 READ_MARKER();
780 seconds = BitstreamGetBits(bs, 6);
781
782 DPRINTF(XVID_DEBUG_HEADER, "time %ih%im%is\n", hours,minutes,seconds);
783 }
784 BitstreamSkip(bs, 1); /* closed_gov */
785 BitstreamSkip(bs, 1); /* broken_link */
786
787 } else if (start_code == VOP_START_CODE) {
788
789 DPRINTF(XVID_DEBUG_STARTCODE, "<vop>\n");
790
791 BitstreamSkip(bs, 32); /* vop_start_code */
792
793 coding_type = BitstreamGetBits(bs, 2); /* vop_coding_type */
794 DPRINTF(XVID_DEBUG_HEADER, "coding_type %i\n", coding_type);
795
796 /*********************** for decode B-frame time ***********************/
797 while (BitstreamGetBit(bs) != 0) /* time_base */
798 time_incr++;
799
800 READ_MARKER();
801
802 if (dec->time_inc_bits) {
803 time_increment = (BitstreamGetBits(bs, dec->time_inc_bits)); /* vop_time_increment */
804 }
805
806 DPRINTF(XVID_DEBUG_HEADER, "time_base %i\n", time_incr);
807 DPRINTF(XVID_DEBUG_HEADER, "time_increment %i\n", time_increment);
808
809 DPRINTF(XVID_DEBUG_TIMECODE, "%c %i:%i\n",
810 coding_type == I_VOP ? 'I' : coding_type == P_VOP ? 'P' : coding_type == B_VOP ? 'B' : 'S',
811 time_incr, time_increment);
812
813 if (coding_type != B_VOP) {
814 dec->last_time_base = dec->time_base;
815 dec->time_base += time_incr;
816 dec->time = dec->time_base*dec->time_inc_resolution + time_increment;
817 dec->time_pp = (int32_t)(dec->time - dec->last_non_b_time);
818 dec->last_non_b_time = dec->time;
819 } else {
820 dec->time = (dec->last_time_base + time_incr)*dec->time_inc_resolution + time_increment;
821 dec->time_bp = dec->time_pp - (int32_t)(dec->last_non_b_time - dec->time);
822 }
823 if (dec->time_pp <= 0) dec->time_pp = 1;
824 DPRINTF(XVID_DEBUG_HEADER,"time_pp=%i\n", dec->time_pp);
825 DPRINTF(XVID_DEBUG_HEADER,"time_bp=%i\n", dec->time_bp);
826
827 READ_MARKER();
828
829 if (!BitstreamGetBit(bs)) /* vop_coded */
830 {
831 DPRINTF(XVID_DEBUG_HEADER, "vop_coded==false\n");
832 return N_VOP;
833 }
834
835 if (dec->newpred_enable)
836 {
837 int vop_id;
838 int vop_id_for_prediction;
839
840 vop_id = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15));
841 DPRINTF(XVID_DEBUG_HEADER, "vop_id %i\n", vop_id);
842 if (BitstreamGetBit(bs)) /* vop_id_for_prediction_indication */
843 {
844 vop_id_for_prediction = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15));
845 DPRINTF(XVID_DEBUG_HEADER, "vop_id_for_prediction %i\n", vop_id_for_prediction);
846 }
847 READ_MARKER();
848 }
849
850
851
852 /* fix a little bug by MinChen <chenm002@163.com> */
853 if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) &&
854 ( (coding_type == P_VOP) || (coding_type == S_VOP && dec->sprite_enable == SPRITE_GMC) ) ) {
855 *rounding = BitstreamGetBit(bs); /* rounding_type */
856 DPRINTF(XVID_DEBUG_HEADER, "rounding %i\n", *rounding);
857 }
858
859 if (dec->reduced_resolution_enable &&
860 dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR &&
861 (coding_type == P_VOP || coding_type == I_VOP)) {
862
863 if (BitstreamGetBit(bs)) {
864 DPRINTF(XVID_DEBUG_ERROR, "RRV not supported (anymore)\n");
865 }
866 }
867
868 if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {
869 if(!(dec->sprite_enable == SPRITE_STATIC && coding_type == I_VOP)) {
870
871 uint32_t width, height;
872 uint32_t horiz_mc_ref, vert_mc_ref;
873
874 width = BitstreamGetBits(bs, 13);
875 READ_MARKER();
876 height = BitstreamGetBits(bs, 13);
877 READ_MARKER();
878 horiz_mc_ref = BitstreamGetBits(bs, 13);
879 READ_MARKER();
880 vert_mc_ref = BitstreamGetBits(bs, 13);
881 READ_MARKER();
882
883 DPRINTF(XVID_DEBUG_HEADER, "width %i\n", width);
884 DPRINTF(XVID_DEBUG_HEADER, "height %i\n", height);
885 DPRINTF(XVID_DEBUG_HEADER, "horiz_mc_ref %i\n", horiz_mc_ref);
886 DPRINTF(XVID_DEBUG_HEADER, "vert_mc_ref %i\n", vert_mc_ref);
887 }
888
889 BitstreamSkip(bs, 1); /* change_conv_ratio_disable */
890 if (BitstreamGetBit(bs)) /* vop_constant_alpha */
891 {
892 BitstreamSkip(bs, 8); /* vop_constant_alpha_value */
893 }
894 }
895
896 if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) {
897
898 if (!dec->complexity_estimation_disable)
899 {
900 read_vop_complexity_estimation_header(bs, dec, coding_type);
901 }
902
903 /* intra_dc_vlc_threshold */
904 *intra_dc_threshold =
905 intra_dc_threshold_table[BitstreamGetBits(bs, 3)];
906
907 dec->top_field_first = 0;
908 dec->alternate_vertical_scan = 0;
909
910 if (dec->interlacing) {
911 dec->top_field_first = BitstreamGetBit(bs);
912 DPRINTF(XVID_DEBUG_HEADER, "interlace top_field_first %i\n", dec->top_field_first);
913 dec->alternate_vertical_scan = BitstreamGetBit(bs);
914 DPRINTF(XVID_DEBUG_HEADER, "interlace alternate_vertical_scan %i\n", dec->alternate_vertical_scan);
915
916 }
917 }
918
919 if ((dec->sprite_enable == SPRITE_STATIC || dec->sprite_enable== SPRITE_GMC) && coding_type == S_VOP) {
920
921 int i;
922
923 for (i = 0 ; i < MIN(4, dec->sprite_warping_points); i++)
924 {
925 int length;
926 int x = 0, y = 0;
927
928 /* sprite code borowed from ffmpeg; thx Michael Niedermayer <michaelni@gmx.at> */
929 length = bs_get_spritetrajectory(bs);
930 if(length){
931 x= BitstreamGetBits(bs, length);
932 if ((x >> (length - 1)) == 0) /* if MSB not set it is negative*/
933 x = - (x ^ ((1 << length) - 1));
934 }
935 READ_MARKER();
936
937 length = bs_get_spritetrajectory(bs);
938 if(length){
939 y = BitstreamGetBits(bs, length);
940 if ((y >> (length - 1)) == 0) /* if MSB not set it is negative*/
941 y = - (y ^ ((1 << length) - 1));
942 }
943 READ_MARKER();
944
945 gmc_warp->duv[i].x = x;
946 gmc_warp->duv[i].y = y;
947
948 DPRINTF(XVID_DEBUG_HEADER,"sprite_warping_point[%i] xy=(%i,%i)\n", i, x, y);
949 }
950
951 if (dec->sprite_brightness_change)
952 {
953 /* XXX: brightness_change_factor() */
954 }
955 if (dec->sprite_enable == SPRITE_STATIC)
956 {
957 /* XXX: todo */
958 }
959
960 }
961
962 if ((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1) /* vop_quant */
963 *quant = 1;
964 DPRINTF(XVID_DEBUG_HEADER, "quant %i\n", *quant);
965
966 if (coding_type != I_VOP) {
967 *fcode_forward = BitstreamGetBits(bs, 3); /* fcode_forward */
968 DPRINTF(XVID_DEBUG_HEADER, "fcode_forward %i\n", *fcode_forward);
969 }
970
971 if (coding_type == B_VOP) {
972 *fcode_backward = BitstreamGetBits(bs, 3); /* fcode_backward */
973 DPRINTF(XVID_DEBUG_HEADER, "fcode_backward %i\n", *fcode_backward);
974 }
975 if (!dec->scalability) {
976 if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) &&
977 (coding_type != I_VOP)) {
978 BitstreamSkip(bs, 1); /* vop_shape_coding_type */
979 }
980 }
981 return coding_type;
982
983 } else if (start_code == USERDATA_START_CODE) {
984 char tmp[256];
985 int i, version = 0, build = 0;
986 char packed;
987
988 BitstreamSkip(bs, 32); /* user_data_start_code */
989
990 memset(tmp, 0, 256);
991 tmp[0] = BitstreamShowBits(bs, 8);
992
993 for(i = 1; i < 255; i++){
994 tmp[i] = (BitstreamShowBits(bs, 16) & 0xFF);
995
996 if(tmp[i] == 0)
997 break;
998
999 BitstreamSkip(bs, 8);
1000 }
1001
1002 DPRINTF(XVID_DEBUG_STARTCODE, "<user_data>: %s\n", tmp);
1003
1004 /* read xvid bitstream version */
1005 if(strncmp(tmp, "XviD", 4) == 0) {
1006 if (tmp[strlen(tmp)-1] == 'C') {
1007 sscanf(tmp, "XviD%dC", &dec->bs_version);
1008 dec->cartoon_mode = 1;
1009 }
1010 else
1011 sscanf(tmp, "XviD%d", &dec->bs_version);
1012
1013 DPRINTF(XVID_DEBUG_HEADER, "xvid bitstream version=%i\n", dec->bs_version);
1014 }
1015
1016 /* divx detection */
1017 i = sscanf(tmp, "DivX%dBuild%d%c", &version, &build, &packed);
1018 if (i < 2)
1019 i = sscanf(tmp, "DivX%db%d%c", &version, &build, &packed);
1020
1021 if (i >= 2)
1022 {
1023 dec->packed_mode = (i == 3 && packed == 'p');
1024 DPRINTF(XVID_DEBUG_HEADER, "divx version=%i, build=%i packed=%i\n",
1025 version, build, dec->packed_mode);
1026 }
1027
1028 if ((dec->bs_version == 0) && (build > 0) &&
1029 (build != 1393)) { /* non-xvid stream with xvid fourcc */
1030 dec->bs_version = 0xffff;
1031 }
1032
1033 } else /* start_code == ? */
1034 {
1035 if (BitstreamShowBits(bs, 24) == 0x000001) {
1036 DPRINTF(XVID_DEBUG_STARTCODE, "<unknown: %x>\n", BitstreamShowBits(bs, 32));
1037 }
1038 BitstreamSkip(bs, 8);
1039 }
1040 }
1041
1042 #if 0
1043 DPRINTF("*** WARNING: no vop_start_code found");
1044 #endif
1045 return -1; /* ignore it */
1046 }
1047
1048
1049 /* write custom quant matrix */
1050
1051 static void
bs_put_matrix(Bitstream * bs,const uint16_t * matrix)1052 bs_put_matrix(Bitstream * bs,
1053 const uint16_t * matrix)
1054 {
1055 int i, j;
1056 const int last = matrix[scan_tables[0][63]];
1057
1058 for (j = 63; j > 0 && matrix[scan_tables[0][j - 1]] == last; j--);
1059
1060 for (i = 0; i <= j; i++) {
1061 BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8);
1062 }
1063
1064 if (j < 63) {
1065 BitstreamPutBits(bs, 0, 8);
1066 }
1067 }
1068
1069
1070 /*
1071 write vol header
1072 */
1073 void
BitstreamWriteVolHeader(Bitstream * const bs,const MBParam * pParam,const FRAMEINFO * const frame,const int num_slices)1074 BitstreamWriteVolHeader(Bitstream * const bs,
1075 const MBParam * pParam,
1076 const FRAMEINFO * const frame,
1077 const int num_slices)
1078 {
1079 static const unsigned int vo_id = 0;
1080 static const unsigned int vol_id = 0;
1081 int vol_ver_id = 1;
1082 int vol_type_ind = VIDOBJLAY_TYPE_SIMPLE;
1083 int vol_profile = pParam->profile;
1084
1085 if ( (pParam->vol_flags & XVID_VOL_QUARTERPEL) ||
1086 (pParam->vol_flags & XVID_VOL_GMC))
1087 vol_ver_id = 2;
1088
1089 if ((pParam->vol_flags & (XVID_VOL_MPEGQUANT|XVID_VOL_QUARTERPEL|XVID_VOL_GMC|XVID_VOL_INTERLACING)) ||
1090 pParam->max_bframes>0) {
1091 vol_type_ind = VIDOBJLAY_TYPE_ASP;
1092 }
1093
1094 /* visual_object_sequence_start_code */
1095 #if 0
1096 BitstreamPad(bs);
1097 #endif
1098
1099 /*
1100 * no padding here, anymore. You have to make sure that you are
1101 * byte aligned, and that always 1-8 padding bits have been written
1102 */
1103
1104 if (!vol_profile) {
1105 /* Profile was not set by client app, use the more permissive profile
1106 * compatible with the vol_type_id */
1107 switch(vol_type_ind) {
1108 case VIDOBJLAY_TYPE_ASP:
1109 vol_profile = 0xf5; /* ASP level 5 */
1110 break;
1111 case VIDOBJLAY_TYPE_ART_SIMPLE:
1112 vol_profile = 0x94; /* ARTS level 4 */
1113 break;
1114 default:
1115 vol_profile = 0x03; /* Simple level 3 */
1116 break;
1117 }
1118 }
1119
1120 /* Write the VOS header */
1121 BitstreamPutBits(bs, VISOBJSEQ_START_CODE, 32);
1122 BitstreamPutBits(bs, vol_profile, 8); /* profile_and_level_indication */
1123
1124
1125 /* visual_object_start_code */
1126 BitstreamPad(bs);
1127 BitstreamPutBits(bs, VISOBJ_START_CODE, 32);
1128 BitstreamPutBits(bs, 0, 1); /* is_visual_object_identifier */
1129
1130 /* Video type */
1131 BitstreamPutBits(bs, VISOBJ_TYPE_VIDEO, 4); /* visual_object_type */
1132 BitstreamPutBit(bs, 0); /* video_signal_type */
1133
1134 /* video object_start_code & vo_id */
1135 BitstreamPadAlways(bs); /* next_start_code() */
1136 BitstreamPutBits(bs, VIDOBJ_START_CODE|(vo_id&0x5), 32);
1137
1138 /* video_object_layer_start_code & vol_id */
1139 BitstreamPad(bs);
1140 BitstreamPutBits(bs, VIDOBJLAY_START_CODE|(vol_id&0x4), 32);
1141
1142 BitstreamPutBit(bs, 0); /* random_accessible_vol */
1143 BitstreamPutBits(bs, vol_type_ind, 8); /* video_object_type_indication */
1144
1145 if (vol_ver_id == 1) {
1146 BitstreamPutBit(bs, 0); /* is_object_layer_identified (0=not given) */
1147 } else {
1148 BitstreamPutBit(bs, 1); /* is_object_layer_identified */
1149 BitstreamPutBits(bs, vol_ver_id, 4); /* vol_ver_id == 2 */
1150 BitstreamPutBits(bs, 4, 3); /* vol_ver_priority (1==highest, 7==lowest) */
1151 }
1152
1153 /* Aspect ratio */
1154 BitstreamPutBits(bs, pParam->par, 4); /* aspect_ratio_info (1=1:1) */
1155 if(pParam->par == XVID_PAR_EXT) {
1156 BitstreamPutBits(bs, pParam->par_width, 8);
1157 BitstreamPutBits(bs, pParam->par_height, 8);
1158 }
1159
1160 BitstreamPutBit(bs, 1); /* vol_control_parameters */
1161 BitstreamPutBits(bs, 1, 2); /* chroma_format 1="4:2:0" */
1162
1163 if (pParam->max_bframes > 0) {
1164 BitstreamPutBit(bs, 0); /* low_delay */
1165 } else
1166 {
1167 BitstreamPutBit(bs, 1); /* low_delay */
1168 }
1169 BitstreamPutBit(bs, 0); /* vbv_parameters (0=not given) */
1170
1171 BitstreamPutBits(bs, 0, 2); /* video_object_layer_shape (0=rectangular) */
1172
1173 WRITE_MARKER();
1174
1175 /*
1176 * time_inc_resolution; ignored by current decore versions
1177 * eg. 2fps res=2 inc=1
1178 * 25fps res=25 inc=1
1179 * 29.97fps res=30000 inc=1001
1180 */
1181 BitstreamPutBits(bs, pParam->fbase, 16);
1182
1183 WRITE_MARKER();
1184
1185 if (pParam->fincr>0) {
1186 BitstreamPutBit(bs, 1); /* fixed_vop_rate = 1 */
1187 BitstreamPutBits(bs, pParam->fincr, MAX(log2bin(pParam->fbase-1),1)); /* fixed_vop_time_increment */
1188 }else{
1189 BitstreamPutBit(bs, 0); /* fixed_vop_rate = 0 */
1190 }
1191
1192 WRITE_MARKER();
1193 BitstreamPutBits(bs, pParam->width, 13); /* width */
1194 WRITE_MARKER();
1195 BitstreamPutBits(bs, pParam->height, 13); /* height */
1196 WRITE_MARKER();
1197
1198 BitstreamPutBit(bs, pParam->vol_flags & XVID_VOL_INTERLACING); /* interlace */
1199 BitstreamPutBit(bs, 1); /* obmc_disable (overlapped block motion compensation) */
1200
1201 if (vol_ver_id != 1)
1202 { if ((pParam->vol_flags & XVID_VOL_GMC))
1203 { BitstreamPutBits(bs, 2, 2); /* sprite_enable=='GMC' */
1204 BitstreamPutBits(bs, 3, 6); /* no_of_sprite_warping_points */
1205 BitstreamPutBits(bs, 3, 2); /* sprite_warping_accuracy 0==1/2, 1=1/4, 2=1/8, 3=1/16 */
1206 BitstreamPutBit(bs, 0); /* sprite_brightness_change (not supported) */
1207
1208 /*
1209 * currently we use no_of_sprite_warping_points==2, sprite_warping_accuracy==3
1210 * for DivX5 compatability
1211 */
1212
1213 } else
1214 BitstreamPutBits(bs, 0, 2); /* sprite_enable==off */
1215 }
1216 else
1217 BitstreamPutBit(bs, 0); /* sprite_enable==off */
1218
1219 BitstreamPutBit(bs, 0); /* not_8_bit */
1220
1221 /* quant_type 0=h.263 1=mpeg4(quantizer tables) */
1222 BitstreamPutBit(bs, pParam->vol_flags & XVID_VOL_MPEGQUANT);
1223
1224 if ((pParam->vol_flags & XVID_VOL_MPEGQUANT)) {
1225 BitstreamPutBit(bs, is_custom_intra_matrix(pParam->mpeg_quant_matrices)); /* load_intra_quant_mat */
1226 if(is_custom_intra_matrix(pParam->mpeg_quant_matrices))
1227 bs_put_matrix(bs, get_intra_matrix(pParam->mpeg_quant_matrices));
1228
1229 BitstreamPutBit(bs, is_custom_inter_matrix(pParam->mpeg_quant_matrices)); /* load_inter_quant_mat */
1230 if(is_custom_inter_matrix(pParam->mpeg_quant_matrices))
1231 bs_put_matrix(bs, get_inter_matrix(pParam->mpeg_quant_matrices));
1232 }
1233
1234 if (vol_ver_id != 1) {
1235 if ((pParam->vol_flags & XVID_VOL_QUARTERPEL))
1236 BitstreamPutBit(bs, 1); /* quarterpel */
1237 else
1238 BitstreamPutBit(bs, 0); /* no quarterpel */
1239 }
1240
1241 BitstreamPutBit(bs, 1); /* complexity_estimation_disable */
1242
1243 if (num_slices > 1)
1244 BitstreamPutBit(bs, 0); /* resync_marker_enabled */
1245 else
1246 BitstreamPutBit(bs, 1); /* resync_marker_disabled */
1247
1248 BitstreamPutBit(bs, 0); /* data_partitioned */
1249
1250 if (vol_ver_id != 1) {
1251 BitstreamPutBit(bs, 0); /* newpred_enable */
1252 BitstreamPutBit(bs, 0); /* reduced_resolution_vop_enabled */
1253 }
1254
1255 BitstreamPutBit(bs, 0); /* scalability */
1256
1257 BitstreamPadAlways(bs); /* next_start_code(); */
1258
1259 /* divx5 userdata string */
1260 #define DIVX5_ID ((char *)"DivX503b1393")
1261 if ((pParam->global_flags & XVID_GLOBAL_DIVX5_USERDATA)) {
1262 BitstreamWriteUserData(bs, DIVX5_ID, (uint32_t) strlen(DIVX5_ID));
1263 if (pParam->max_bframes > 0 && (pParam->global_flags & XVID_GLOBAL_PACKED))
1264 BitstreamPutBits(bs, 'p', 8);
1265 }
1266
1267 /* xvid id */
1268 {
1269 const char xvid_user_format[] = "XviD%04d%c";
1270 char xvid_user_data[100];
1271 sprintf(xvid_user_data,
1272 xvid_user_format,
1273 XVID_BS_VERSION,
1274 (frame->vop_flags & XVID_VOP_CARTOON)?'C':'\0');
1275 BitstreamWriteUserData(bs, xvid_user_data, (uint32_t) strlen(xvid_user_data));
1276 }
1277 }
1278
1279
1280 /*
1281 write vop header
1282 */
1283 void
BitstreamWriteVopHeader(Bitstream * const bs,const MBParam * pParam,const FRAMEINFO * const frame,int vop_coded,unsigned int quant)1284 BitstreamWriteVopHeader(
1285 Bitstream * const bs,
1286 const MBParam * pParam,
1287 const FRAMEINFO * const frame,
1288 int vop_coded,
1289 unsigned int quant)
1290 {
1291 uint32_t i;
1292
1293 #if 0
1294 BitstreamPad(bs);
1295 #endif
1296
1297 /*
1298 * no padding here, anymore. You have to make sure that you are
1299 * byte aligned, and that always 1-8 padding bits have been written
1300 */
1301
1302 BitstreamPutBits(bs, VOP_START_CODE, 32);
1303
1304 BitstreamPutBits(bs, frame->coding_type, 2);
1305 #if 0
1306 DPRINTF(XVID_DEBUG_HEADER, "coding_type = %i\n", frame->coding_type);
1307 #endif
1308
1309 for (i = 0; i < frame->seconds; i++) {
1310 BitstreamPutBit(bs, 1);
1311 }
1312 BitstreamPutBit(bs, 0);
1313
1314 WRITE_MARKER();
1315
1316 /* time_increment: value=nth_of_sec, nbits = log2(resolution) */
1317 BitstreamPutBits(bs, frame->ticks, MAX(log2bin(pParam->fbase-1), 1));
1318 #if 0
1319 DPRINTF("[%i:%i] %c",
1320 frame->seconds, frame->ticks,
1321 frame->coding_type == I_VOP ? 'I' :
1322 frame->coding_type == P_VOP ? 'P' :
1323 frame->coding_type == S_VOP ? 'S' : 'B');
1324 #endif
1325
1326 WRITE_MARKER();
1327
1328 if (!vop_coded) {
1329 BitstreamPutBits(bs, 0, 1);
1330 #if 0
1331 BitstreamPadAlways(bs); /* next_start_code() */
1332 #endif
1333 /* NB: It's up to the function caller to write the next_start_code().
1334 * At the moment encoder.c respects that requisite because a VOP
1335 * always ends with a next_start_code either if it's coded or not
1336 * and encoder.c terminates a frame with a next_start_code in whatever
1337 * case */
1338 return;
1339 }
1340
1341 BitstreamPutBits(bs, 1, 1); /* vop_coded */
1342
1343 if ( (frame->coding_type == P_VOP) || (frame->coding_type == S_VOP) )
1344 BitstreamPutBits(bs, frame->rounding_type, 1);
1345
1346 BitstreamPutBits(bs, 0, 3); /* intra_dc_vlc_threshold */
1347
1348 if ((frame->vol_flags & XVID_VOL_INTERLACING)) {
1349 BitstreamPutBit(bs, (frame->vop_flags & XVID_VOP_TOPFIELDFIRST));
1350 BitstreamPutBit(bs, (frame->vop_flags & XVID_VOP_ALTERNATESCAN));
1351 }
1352
1353 if (frame->coding_type == S_VOP) {
1354 if (1) { /* no_of_sprite_warping_points>=1 (we use 2!) */
1355 int k;
1356 for (k=0;k<3;k++)
1357 {
1358 bs_put_spritetrajectory(bs, frame->warp.duv[k].x ); /* du[k] */
1359 WRITE_MARKER();
1360
1361 bs_put_spritetrajectory(bs, frame->warp.duv[k].y ); /* dv[k] */
1362 WRITE_MARKER();
1363
1364 if ((frame->vol_flags & XVID_VOL_QUARTERPEL))
1365 {
1366 DPRINTF(XVID_DEBUG_HEADER,"sprite_warping_point[%i] xy=(%i,%i) *QPEL*\n", k, frame->warp.duv[k].x/2, frame->warp.duv[k].y/2);
1367 }
1368 else
1369 {
1370 DPRINTF(XVID_DEBUG_HEADER,"sprite_warping_point[%i] xy=(%i,%i)\n", k, frame->warp.duv[k].x, frame->warp.duv[k].y);
1371 }
1372 }
1373 }
1374 }
1375
1376
1377 #if 0
1378 DPRINTF(XVID_DEBUG_HEADER, "quant = %i\n", quant);
1379 #endif
1380
1381 BitstreamPutBits(bs, quant, 5); /* quantizer */
1382
1383 if (frame->coding_type != I_VOP)
1384 BitstreamPutBits(bs, frame->fcode, 3); /* forward_fixed_code */
1385
1386 if (frame->coding_type == B_VOP)
1387 BitstreamPutBits(bs, frame->bcode, 3); /* backward_fixed_code */
1388
1389 }
1390
1391 void
BitstreamWriteUserData(Bitstream * const bs,const char * data,const unsigned int length)1392 BitstreamWriteUserData(Bitstream * const bs,
1393 const char *data,
1394 const unsigned int length)
1395 {
1396 unsigned int i;
1397
1398 BitstreamPad(bs);
1399 BitstreamPutBits(bs, USERDATA_START_CODE, 32);
1400
1401 for (i = 0; i < length; i++) {
1402 BitstreamPutBits(bs, data[i], 8);
1403 }
1404
1405 }
1406
1407 /*
1408 * Group of VOP
1409 */
1410 void
BitstreamWriteGroupOfVopHeader(Bitstream * const bs,const MBParam * pParam,uint32_t is_closed_gov)1411 BitstreamWriteGroupOfVopHeader(Bitstream * const bs,
1412 const MBParam * pParam,
1413 uint32_t is_closed_gov)
1414 {
1415 int64_t time = (pParam->m_stamp + (pParam->fbase/2)) / pParam->fbase;
1416 int hours, minutes, seconds;
1417
1418 /* compute time_code */
1419 seconds = time % 60; time /= 60;
1420 minutes = time % 60; time /= 60;
1421 hours = time % 24; /* don't overflow */
1422
1423 BitstreamPutBits(bs, GRPOFVOP_START_CODE, 32);
1424 BitstreamPutBits(bs, hours, 5);
1425 BitstreamPutBits(bs, minutes, 6);
1426 BitstreamPutBit(bs, 1);
1427 BitstreamPutBits(bs, seconds, 6);
1428 BitstreamPutBits(bs, is_closed_gov, 1);
1429 BitstreamPutBits(bs, 0, 1); /* broken_link */
1430 }
1431
1432 /*
1433 * End of Sequence
1434 */
1435 void
BitstreamWriteEndOfSequence(Bitstream * const bs)1436 BitstreamWriteEndOfSequence(Bitstream * const bs)
1437 {
1438 BitstreamPadAlways(bs);
1439 BitstreamPutBits(bs, VISOBJSEQ_STOP_CODE, 32);
1440 }
1441
1442 /*
1443 * Video Packet (resync marker)
1444 */
1445
write_video_packet_header(Bitstream * const bs,const MBParam * pParam,const FRAMEINFO * const frame,int mbnum)1446 void write_video_packet_header(Bitstream * const bs,
1447 const MBParam * pParam,
1448 const FRAMEINFO * const frame,
1449 int mbnum)
1450 {
1451 const int mbnum_bits = log2bin(pParam->mb_width * pParam->mb_height - 1);
1452 uint32_t nbitsresyncmarker;
1453
1454 if (frame->coding_type == I_VOP)
1455 nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER; /* 16 zeros followed by a 1. */
1456 else if (frame->coding_type == B_VOP) /* B_VOP */
1457 nbitsresyncmarker = MAX(NUMBITS_VP_RESYNC_MARKER+1, NUMBITS_VP_RESYNC_MARKER + MAX(frame->fcode, frame->bcode) - 1);
1458 else /*(frame->coding_type == P_VOP)*/
1459 nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + frame->fcode - 1;
1460
1461 BitstreamPutBits(bs, RESYNC_MARKER, nbitsresyncmarker);
1462 BitstreamPutBits(bs, mbnum, mbnum_bits);
1463 BitstreamPutBits(bs, frame->quant, 5);
1464 BitstreamPutBit(bs, 0); /* hec */
1465 }
1466