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