Mercurial > mplayer.hg
annotate libmpeg2/header.c @ 30634:9c043456a260
Mark ad_functions_t structures and mpcodecs_ad_drivers array as const.
author | diego |
---|---|
date | Sat, 20 Feb 2010 21:27:49 +0000 |
parents | e83eef58b30a |
children | 020ae1402728 |
rev | line source |
---|---|
1 | 1 /* |
9852 | 2 * header.c |
12932 | 3 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org> |
4 * Copyright (C) 2003 Regis Duchesne <hpreg@zoy.org> | |
9852 | 5 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca> |
1 | 6 * |
7 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder. | |
9852 | 8 * See http://libmpeg2.sourceforge.net/ for updates. |
1 | 9 * |
10 * mpeg2dec is free software; you can redistribute it and/or modify | |
11 * it under the terms of the GNU General Public License as published by | |
12 * the Free Software Foundation; either version 2 of the License, or | |
13 * (at your option) any later version. | |
14 * | |
15 * mpeg2dec is distributed in the hope that it will be useful, | |
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 * GNU General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU General Public License | |
21 * along with this program; if not, write to the Free Software | |
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
14732
1385ec491ffb
Mark locally modified files as such to comply more closely with GPL 2a.
diego
parents:
13123
diff
changeset
|
23 * |
27571
fd18fa10de53
libmpeg-0.4.1.diff was renamed to libmpeg2_changes.diff.
diego
parents:
21526
diff
changeset
|
24 * Modified for use with MPlayer, see libmpeg2_changes.diff for the exact changes. |
18783 | 25 * detailed changelog at http://svn.mplayerhq.hu/mplayer/trunk/ |
14732
1385ec491ffb
Mark locally modified files as such to comply more closely with GPL 2a.
diego
parents:
13123
diff
changeset
|
26 * $Id$ |
1 | 27 */ |
28 | |
29 #include "config.h" | |
30 | |
31 #include <inttypes.h> | |
9852 | 32 #include <stdlib.h> /* defines NULL */ |
33 #include <string.h> /* memcmp */ | |
1 | 34 |
9852 | 35 #include "mpeg2.h" |
12932 | 36 #include "attributes.h" |
1 | 37 #include "mpeg2_internal.h" |
38 | |
9852 | 39 #define SEQ_EXT 2 |
40 #define SEQ_DISPLAY_EXT 4 | |
41 #define QUANT_MATRIX_EXT 8 | |
42 #define COPYRIGHT_EXT 0x10 | |
43 #define PIC_DISPLAY_EXT 0x80 | |
44 #define PIC_CODING_EXT 0x100 | |
45 | |
36 | 46 /* default intra quant matrix, in zig-zag order */ |
9852 | 47 static const uint8_t default_intra_quantizer_matrix[64] ATTR_ALIGN(16) = { |
1 | 48 8, |
49 16, 16, | |
50 19, 16, 19, | |
51 22, 22, 22, 22, | |
52 22, 22, 26, 24, 26, | |
53 27, 27, 27, 26, 26, 26, | |
54 26, 27, 27, 27, 29, 29, 29, | |
55 34, 34, 34, 29, 29, 29, 27, 27, | |
56 29, 29, 32, 32, 34, 34, 37, | |
57 38, 37, 35, 35, 34, 35, | |
58 38, 38, 40, 40, 40, | |
59 48, 48, 46, 46, | |
60 56, 56, 58, | |
61 69, 69, | |
62 83 | |
63 }; | |
64 | |
9852 | 65 uint8_t mpeg2_scan_norm[64] ATTR_ALIGN(16) = { |
36 | 66 /* Zig-Zag scan pattern */ |
9852 | 67 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, |
68 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, | |
69 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, | |
70 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63 | |
71 }; | |
72 | |
73 uint8_t mpeg2_scan_alt[64] ATTR_ALIGN(16) = { | |
74 /* Alternate scan pattern */ | |
75 0, 8, 16, 24, 1, 9, 2, 10, 17, 25, 32, 40, 48, 56, 57, 49, | |
76 41, 33, 26, 18, 3, 11, 4, 12, 19, 27, 34, 42, 50, 58, 35, 43, | |
77 51, 59, 20, 28, 5, 13, 6, 14, 21, 29, 36, 44, 52, 60, 37, 45, | |
78 53, 61, 22, 30, 7, 15, 23, 31, 38, 46, 54, 62, 39, 47, 55, 63 | |
1 | 79 }; |
80 | |
9852 | 81 void mpeg2_header_state_init (mpeg2dec_t * mpeg2dec) |
1 | 82 { |
12932 | 83 if (mpeg2dec->sequence.width != (unsigned)-1) { |
84 int i; | |
85 | |
86 mpeg2dec->sequence.width = (unsigned)-1; | |
87 if (!mpeg2dec->custom_fbuf) | |
88 for (i = mpeg2dec->alloc_index_user; | |
89 i < mpeg2dec->alloc_index; i++) { | |
90 mpeg2_free (mpeg2dec->fbuf_alloc[i].fbuf.buf[0]); | |
91 mpeg2_free (mpeg2dec->fbuf_alloc[i].fbuf.buf[1]); | |
92 mpeg2_free (mpeg2dec->fbuf_alloc[i].fbuf.buf[2]); | |
93 } | |
94 if (mpeg2dec->convert_start) | |
95 for (i = 0; i < 3; i++) { | |
96 mpeg2_free (mpeg2dec->yuv_buf[i][0]); | |
97 mpeg2_free (mpeg2dec->yuv_buf[i][1]); | |
98 mpeg2_free (mpeg2dec->yuv_buf[i][2]); | |
99 } | |
100 if (mpeg2dec->decoder.convert_id) | |
101 mpeg2_free (mpeg2dec->decoder.convert_id); | |
102 } | |
103 mpeg2dec->decoder.coding_type = I_TYPE; | |
104 mpeg2dec->decoder.convert = NULL; | |
105 mpeg2dec->decoder.convert_id = NULL; | |
9852 | 106 mpeg2dec->picture = mpeg2dec->pictures; |
12932 | 107 memset(&mpeg2dec->fbuf_alloc[0].fbuf, 0, sizeof(mpeg2_fbuf_t)); |
108 memset(&mpeg2dec->fbuf_alloc[1].fbuf, 0, sizeof(mpeg2_fbuf_t)); | |
109 memset(&mpeg2dec->fbuf_alloc[2].fbuf, 0, sizeof(mpeg2_fbuf_t)); | |
9852 | 110 mpeg2dec->fbuf[0] = &mpeg2dec->fbuf_alloc[0].fbuf; |
111 mpeg2dec->fbuf[1] = &mpeg2dec->fbuf_alloc[1].fbuf; | |
112 mpeg2dec->fbuf[2] = &mpeg2dec->fbuf_alloc[2].fbuf; | |
113 mpeg2dec->first = 1; | |
114 mpeg2dec->alloc_index = 0; | |
115 mpeg2dec->alloc_index_user = 0; | |
12932 | 116 mpeg2dec->first_decode_slice = 1; |
117 mpeg2dec->nb_decode_slices = 0xb0 - 1; | |
118 mpeg2dec->convert = NULL; | |
119 mpeg2dec->convert_start = NULL; | |
120 mpeg2dec->custom_fbuf = 0; | |
121 mpeg2dec->yuv_index = 0; | |
1 | 122 } |
123 | |
12932 | 124 void mpeg2_reset_info (mpeg2_info_t * info) |
1 | 125 { |
9852 | 126 info->current_picture = info->current_picture_2nd = NULL; |
127 info->display_picture = info->display_picture_2nd = NULL; | |
128 info->current_fbuf = info->display_fbuf = info->discard_fbuf = NULL; | |
12932 | 129 } |
130 | |
131 static void info_user_data (mpeg2dec_t * mpeg2dec) | |
132 { | |
133 if (mpeg2dec->user_data_len) { | |
134 mpeg2dec->info.user_data = mpeg2dec->chunk_buffer; | |
135 mpeg2dec->info.user_data_len = mpeg2dec->user_data_len - 3; | |
136 } | |
9852 | 137 } |
138 | |
139 int mpeg2_header_sequence (mpeg2dec_t * mpeg2dec) | |
140 { | |
141 uint8_t * buffer = mpeg2dec->chunk_start; | |
12932 | 142 mpeg2_sequence_t * sequence = &(mpeg2dec->new_sequence); |
143 static unsigned int frame_period[16] = { | |
144 0, 1126125, 1125000, 1080000, 900900, 900000, 540000, 450450, 450000, | |
145 /* unofficial: xing 15 fps */ | |
146 1800000, | |
147 /* unofficial: libmpeg3 "Unofficial economy rates" 5/10/12/15 fps */ | |
148 5400000, 2700000, 2250000, 1800000, 0, 0 | |
9852 | 149 }; |
1 | 150 int i; |
151 | |
9852 | 152 if ((buffer[6] & 0x20) != 0x20) /* missing marker_bit */ |
153 return 1; | |
1 | 154 |
9852 | 155 i = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2]; |
12932 | 156 if (! (sequence->display_width = sequence->picture_width = i >> 12)) |
157 return 1; | |
158 if (! (sequence->display_height = sequence->picture_height = i & 0xfff)) | |
159 return 1; | |
160 sequence->width = (sequence->picture_width + 15) & ~15; | |
161 sequence->height = (sequence->picture_height + 15) & ~15; | |
162 sequence->chroma_width = sequence->width >> 1; | |
163 sequence->chroma_height = sequence->height >> 1; | |
12617 | 164 |
12932 | 165 sequence->flags = (SEQ_FLAG_PROGRESSIVE_SEQUENCE | |
166 SEQ_VIDEO_FORMAT_UNSPECIFIED); | |
9852 | 167 |
168 sequence->pixel_width = buffer[3] >> 4; /* aspect ratio */ | |
12932 | 169 sequence->frame_period = frame_period[buffer[3] & 15]; |
36 | 170 |
9852 | 171 sequence->byte_rate = (buffer[4]<<10) | (buffer[5]<<2) | (buffer[6]>>6); |
172 | |
173 sequence->vbv_buffer_size = ((buffer[6]<<16)|(buffer[7]<<8))&0x1ff800; | |
174 | |
175 if (buffer[7] & 4) | |
176 sequence->flags |= SEQ_FLAG_CONSTRAINED_PARAMETERS; | |
1 | 177 |
12932 | 178 mpeg2dec->copy_matrix = 3; |
1 | 179 if (buffer[7] & 2) { |
180 for (i = 0; i < 64; i++) | |
12932 | 181 mpeg2dec->new_quantizer_matrix[0][mpeg2_scan_norm[i]] = |
1 | 182 (buffer[i+7] << 7) | (buffer[i+8] >> 1); |
183 buffer += 64; | |
9852 | 184 } else |
185 for (i = 0; i < 64; i++) | |
12932 | 186 mpeg2dec->new_quantizer_matrix[0][mpeg2_scan_norm[i]] = |
187 default_intra_quantizer_matrix[i]; | |
9852 | 188 |
189 if (buffer[7] & 1) | |
190 for (i = 0; i < 64; i++) | |
12932 | 191 mpeg2dec->new_quantizer_matrix[1][mpeg2_scan_norm[i]] = |
9852 | 192 buffer[i+8]; |
193 else | |
12932 | 194 memset (mpeg2dec->new_quantizer_matrix[1], 16, 64); |
9852 | 195 |
196 sequence->profile_level_id = 0x80; | |
12932 | 197 sequence->colour_primaries = 0; |
198 sequence->transfer_characteristics = 0; | |
199 sequence->matrix_coefficients = 0; | |
9852 | 200 |
201 mpeg2dec->ext_state = SEQ_EXT; | |
202 mpeg2dec->state = STATE_SEQUENCE; | |
203 mpeg2dec->display_offset_x = mpeg2dec->display_offset_y = 0; | |
204 | |
205 return 0; | |
206 } | |
207 | |
208 static int sequence_ext (mpeg2dec_t * mpeg2dec) | |
209 { | |
210 uint8_t * buffer = mpeg2dec->chunk_start; | |
12932 | 211 mpeg2_sequence_t * sequence = &(mpeg2dec->new_sequence); |
9852 | 212 uint32_t flags; |
213 | |
214 if (!(buffer[3] & 1)) | |
215 return 1; | |
216 | |
217 sequence->profile_level_id = (buffer[0] << 4) | (buffer[1] >> 4); | |
218 | |
12932 | 219 sequence->display_width = sequence->picture_width += |
9852 | 220 ((buffer[1] << 13) | (buffer[2] << 5)) & 0x3000; |
12932 | 221 sequence->display_height = sequence->picture_height += |
9852 | 222 (buffer[2] << 7) & 0x3000; |
12932 | 223 sequence->width = (sequence->picture_width + 15) & ~15; |
224 sequence->height = (sequence->picture_height + 15) & ~15; | |
9852 | 225 flags = sequence->flags | SEQ_FLAG_MPEG2; |
226 if (!(buffer[1] & 8)) { | |
227 flags &= ~SEQ_FLAG_PROGRESSIVE_SEQUENCE; | |
12932 | 228 sequence->height = (sequence->height + 31) & ~31; |
9852 | 229 } |
230 if (buffer[5] & 0x80) | |
231 flags |= SEQ_FLAG_LOW_DELAY; | |
232 sequence->flags = flags; | |
12932 | 233 sequence->chroma_width = sequence->width; |
234 sequence->chroma_height = sequence->height; | |
9852 | 235 switch (buffer[1] & 6) { |
236 case 0: /* invalid */ | |
237 return 1; | |
238 case 2: /* 4:2:0 */ | |
12932 | 239 sequence->chroma_height >>= 1; |
9852 | 240 case 4: /* 4:2:2 */ |
12932 | 241 sequence->chroma_width >>= 1; |
9852 | 242 } |
243 | |
244 sequence->byte_rate += ((buffer[2]<<25) | (buffer[3]<<17)) & 0x3ffc0000; | |
245 | |
246 sequence->vbv_buffer_size |= buffer[4] << 21; | |
247 | |
248 sequence->frame_period = | |
27572 | 249 sequence->frame_period * ((buffer[5]&31)+1) / (((buffer[5]>>5)&3)+1); |
9852 | 250 |
251 mpeg2dec->ext_state = SEQ_DISPLAY_EXT; | |
252 | |
253 return 0; | |
254 } | |
255 | |
256 static int sequence_display_ext (mpeg2dec_t * mpeg2dec) | |
257 { | |
258 uint8_t * buffer = mpeg2dec->chunk_start; | |
12932 | 259 mpeg2_sequence_t * sequence = &(mpeg2dec->new_sequence); |
9852 | 260 |
27572 | 261 sequence->flags = ((sequence->flags & ~SEQ_MASK_VIDEO_FORMAT) | |
262 ((buffer[0]<<4) & SEQ_MASK_VIDEO_FORMAT)); | |
9852 | 263 if (buffer[0] & 1) { |
27572 | 264 sequence->flags |= SEQ_FLAG_COLOUR_DESCRIPTION; |
9852 | 265 sequence->colour_primaries = buffer[1]; |
266 sequence->transfer_characteristics = buffer[2]; | |
267 sequence->matrix_coefficients = buffer[3]; | |
268 buffer += 3; | |
1 | 269 } |
270 | |
9852 | 271 if (!(buffer[2] & 2)) /* missing marker_bit */ |
272 return 1; | |
1 | 273 |
21399
111bc7023189
ignore invalid values in the sequence display extension (backport from 0.4.1)
henry
parents:
18783
diff
changeset
|
274 if( (buffer[1] << 6) | (buffer[2] >> 2) ) |
111bc7023189
ignore invalid values in the sequence display extension (backport from 0.4.1)
henry
parents:
18783
diff
changeset
|
275 sequence->display_width = (buffer[1] << 6) | (buffer[2] >> 2); |
111bc7023189
ignore invalid values in the sequence display extension (backport from 0.4.1)
henry
parents:
18783
diff
changeset
|
276 if( ((buffer[2]& 1 ) << 13) | (buffer[3] << 5) | (buffer[4] >> 3) ) |
111bc7023189
ignore invalid values in the sequence display extension (backport from 0.4.1)
henry
parents:
18783
diff
changeset
|
277 sequence->display_height = |
111bc7023189
ignore invalid values in the sequence display extension (backport from 0.4.1)
henry
parents:
18783
diff
changeset
|
278 ((buffer[2]& 1 ) << 13) | (buffer[3] << 5) | (buffer[4] >> 3); |
1 | 279 |
280 return 0; | |
281 } | |
282 | |
27572 | 283 static inline void simplify (unsigned int * u, unsigned int * v) |
284 { | |
285 unsigned int a, b, tmp; | |
286 | |
287 a = *u; b = *v; | |
288 while (a) { /* find greatest common divisor */ | |
289 tmp = a; a = b % tmp; b = tmp; | |
290 } | |
291 *u /= b; *v /= b; | |
292 } | |
293 | |
12932 | 294 static inline void finalize_sequence (mpeg2_sequence_t * sequence) |
1 | 295 { |
9852 | 296 int width; |
297 int height; | |
298 | |
299 sequence->byte_rate *= 50; | |
300 | |
301 if (sequence->flags & SEQ_FLAG_MPEG2) { | |
302 switch (sequence->pixel_width) { | |
303 case 1: /* square pixels */ | |
304 sequence->pixel_width = sequence->pixel_height = 1; return; | |
305 case 2: /* 4:3 aspect ratio */ | |
306 width = 4; height = 3; break; | |
307 case 3: /* 16:9 aspect ratio */ | |
308 width = 16; height = 9; break; | |
309 case 4: /* 2.21:1 aspect ratio */ | |
310 width = 221; height = 100; break; | |
311 default: /* illegal */ | |
312 sequence->pixel_width = sequence->pixel_height = 0; return; | |
313 } | |
314 width *= sequence->display_height; | |
315 height *= sequence->display_width; | |
316 | |
317 } else { | |
29264
e83eef58b30a
Remove all kind of trailing whitespaces from all MPlayer's files.
bircoph
parents:
27572
diff
changeset
|
318 if (sequence->byte_rate == 50 * 0x3ffff) |
e83eef58b30a
Remove all kind of trailing whitespaces from all MPlayer's files.
bircoph
parents:
27572
diff
changeset
|
319 sequence->byte_rate = 0; /* mpeg-1 VBR */ |
1 | 320 |
9852 | 321 switch (sequence->pixel_width) { |
322 case 0: case 15: /* illegal */ | |
323 sequence->pixel_width = sequence->pixel_height = 0; return; | |
324 case 1: /* square pixels */ | |
325 sequence->pixel_width = sequence->pixel_height = 1; return; | |
326 case 3: /* 720x576 16:9 */ | |
327 sequence->pixel_width = 64; sequence->pixel_height = 45; return; | |
328 case 6: /* 720x480 16:9 */ | |
329 sequence->pixel_width = 32; sequence->pixel_height = 27; return; | |
27572 | 330 case 8: /* BT.601 625 lines 4:3 */ |
331 sequence->pixel_width = 59; sequence->pixel_height = 54; return; | |
332 case 12: /* BT.601 525 lines 4:3 */ | |
333 sequence->pixel_width = 10; sequence->pixel_height = 11; return; | |
9852 | 334 default: |
335 height = 88 * sequence->pixel_width + 1171; | |
336 width = 2000; | |
337 } | |
338 } | |
1 | 339 |
9852 | 340 sequence->pixel_width = width; |
341 sequence->pixel_height = height; | |
27572 | 342 simplify (&sequence->pixel_width, &sequence->pixel_height); |
9852 | 343 } |
344 | |
27572 | 345 int mpeg2_guess_aspect (const mpeg2_sequence_t * sequence, |
346 unsigned int * pixel_width, | |
347 unsigned int * pixel_height) | |
12932 | 348 { |
27572 | 349 static struct { |
350 unsigned int width, height; | |
351 } video_modes[] = { | |
352 {720, 576}, /* 625 lines, 13.5 MHz (D1, DV, DVB, DVD) */ | |
353 {704, 576}, /* 625 lines, 13.5 MHz (1/1 D1, DVB, DVD, 4CIF) */ | |
354 {544, 576}, /* 625 lines, 10.125 MHz (DVB, laserdisc) */ | |
355 {528, 576}, /* 625 lines, 10.125 MHz (3/4 D1, DVB, laserdisc) */ | |
356 {480, 576}, /* 625 lines, 9 MHz (2/3 D1, DVB, SVCD) */ | |
357 {352, 576}, /* 625 lines, 6.75 MHz (D2, 1/2 D1, CVD, DVB, DVD) */ | |
358 {352, 288}, /* 625 lines, 6.75 MHz, 1 field (D4, VCD, DVB, DVD, CIF) */ | |
359 {176, 144}, /* 625 lines, 3.375 MHz, half field (QCIF) */ | |
360 {720, 486}, /* 525 lines, 13.5 MHz (D1) */ | |
361 {704, 486}, /* 525 lines, 13.5 MHz */ | |
362 {720, 480}, /* 525 lines, 13.5 MHz (DV, DSS, DVD) */ | |
363 {704, 480}, /* 525 lines, 13.5 MHz (1/1 D1, ATSC, DVD) */ | |
364 {544, 480}, /* 525 lines. 10.125 MHz (DSS, laserdisc) */ | |
365 {528, 480}, /* 525 lines. 10.125 MHz (3/4 D1, laserdisc) */ | |
366 {480, 480}, /* 525 lines, 9 MHz (2/3 D1, SVCD) */ | |
367 {352, 480}, /* 525 lines, 6.75 MHz (D2, 1/2 D1, CVD, DVD) */ | |
368 {352, 240} /* 525 lines. 6.75 MHz, 1 field (D4, VCD, DSS, DVD) */ | |
369 }; | |
370 unsigned int width, height, pix_width, pix_height, i, DAR_16_9; | |
371 | |
372 *pixel_width = sequence->pixel_width; | |
373 *pixel_height = sequence->pixel_height; | |
374 width = sequence->picture_width; | |
375 height = sequence->picture_height; | |
376 for (i = 0; i < sizeof (video_modes) / sizeof (video_modes[0]); i++) | |
377 if (width == video_modes[i].width && height == video_modes[i].height) | |
378 break; | |
379 if (i == sizeof (video_modes) / sizeof (video_modes[0]) || | |
380 (sequence->pixel_width == 1 && sequence->pixel_height == 1) || | |
381 width != sequence->display_width || height != sequence->display_height) | |
382 return 0; | |
383 | |
384 for (pix_height = 1; height * pix_height < 480; pix_height <<= 1); | |
385 height *= pix_height; | |
386 for (pix_width = 1; width * pix_width <= 352; pix_width <<= 1); | |
387 width *= pix_width; | |
388 | |
389 if (! (sequence->flags & SEQ_FLAG_MPEG2)) { | |
390 static unsigned int mpeg1_check[2][2] = {{11, 54}, {27, 45}}; | |
391 DAR_16_9 = (sequence->pixel_height == 27 || | |
392 sequence->pixel_height == 45); | |
393 if (width < 704 || | |
394 sequence->pixel_height != mpeg1_check[DAR_16_9][height == 576]) | |
395 return 0; | |
396 } else { | |
397 DAR_16_9 = (3 * sequence->picture_width * sequence->pixel_width > | |
398 4 * sequence->picture_height * sequence->pixel_height); | |
399 switch (width) { | |
400 case 528: case 544: pix_width *= 4; pix_height *= 3; break; | |
401 case 480: pix_width *= 3; pix_height *= 2; break; | |
402 } | |
403 } | |
404 if (DAR_16_9) { | |
405 pix_width *= 4; pix_height *= 3; | |
406 } | |
407 if (height == 576) { | |
408 pix_width *= 59; pix_height *= 54; | |
409 } else { | |
410 pix_width *= 10; pix_height *= 11; | |
411 } | |
412 *pixel_width = pix_width; | |
413 *pixel_height = pix_height; | |
414 simplify (pixel_width, pixel_height); | |
415 return (height == 576) ? 1 : 2; | |
416 } | |
417 | |
418 static void copy_matrix (mpeg2dec_t * mpeg2dec, int idx) | |
419 { | |
420 if (memcmp (mpeg2dec->quantizer_matrix[idx], | |
421 mpeg2dec->new_quantizer_matrix[idx], 64)) { | |
422 memcpy (mpeg2dec->quantizer_matrix[idx], | |
423 mpeg2dec->new_quantizer_matrix[idx], 64); | |
424 mpeg2dec->scaled[idx] = -1; | |
12932 | 425 } |
426 } | |
427 | |
428 static void finalize_matrix (mpeg2dec_t * mpeg2dec) | |
429 { | |
430 mpeg2_decoder_t * decoder = &(mpeg2dec->decoder); | |
431 int i; | |
432 | |
433 for (i = 0; i < 2; i++) { | |
434 if (mpeg2dec->copy_matrix & (1 << i)) | |
435 copy_matrix (mpeg2dec, i); | |
436 if ((mpeg2dec->copy_matrix & (4 << i)) && | |
437 memcmp (mpeg2dec->quantizer_matrix[i], | |
438 mpeg2dec->new_quantizer_matrix[i+2], 64)) { | |
439 copy_matrix (mpeg2dec, i + 2); | |
440 decoder->chroma_quantizer[i] = decoder->quantizer_prescale[i+2]; | |
441 } else if (mpeg2dec->copy_matrix & (5 << i)) | |
442 decoder->chroma_quantizer[i] = decoder->quantizer_prescale[i]; | |
443 } | |
444 } | |
445 | |
446 static mpeg2_state_t invalid_end_action (mpeg2dec_t * mpeg2dec) | |
447 { | |
448 mpeg2_reset_info (&(mpeg2dec->info)); | |
449 mpeg2dec->info.gop = NULL; | |
450 info_user_data (mpeg2dec); | |
451 mpeg2_header_state_init (mpeg2dec); | |
452 mpeg2dec->sequence = mpeg2dec->new_sequence; | |
453 mpeg2dec->action = mpeg2_seek_header; | |
454 mpeg2dec->state = STATE_SEQUENCE; | |
455 return STATE_SEQUENCE; | |
456 } | |
457 | |
9852 | 458 void mpeg2_header_sequence_finalize (mpeg2dec_t * mpeg2dec) |
459 { | |
12932 | 460 mpeg2_sequence_t * sequence = &(mpeg2dec->new_sequence); |
461 mpeg2_decoder_t * decoder = &(mpeg2dec->decoder); | |
9852 | 462 |
463 finalize_sequence (sequence); | |
12932 | 464 finalize_matrix (mpeg2dec); |
36 | 465 |
12932 | 466 decoder->mpeg1 = !(sequence->flags & SEQ_FLAG_MPEG2); |
467 decoder->width = sequence->width; | |
468 decoder->height = sequence->height; | |
469 decoder->vertical_position_extension = (sequence->picture_height > 2800); | |
470 decoder->chroma_format = ((sequence->chroma_width == sequence->width) + | |
471 (sequence->chroma_height == sequence->height)); | |
472 | |
473 if (mpeg2dec->sequence.width != (unsigned)-1) { | |
474 /* | |
475 * According to 6.1.1.6, repeat sequence headers should be | |
27572 | 476 * identical to the original. However some encoders do not |
477 * respect that and change various fields (including bitrate | |
478 * and aspect ratio) in the repeat sequence headers. So we | |
479 * choose to be as conservative as possible and only restart | |
480 * the decoder if the width, height, chroma_width, | |
481 * chroma_height or low_delay flag are modified. | |
12932 | 482 */ |
27572 | 483 if (sequence->width != mpeg2dec->sequence.width || |
484 sequence->height != mpeg2dec->sequence.height || | |
485 sequence->chroma_width != mpeg2dec->sequence.chroma_width || | |
486 sequence->chroma_height != mpeg2dec->sequence.chroma_height || | |
487 ((sequence->flags ^ mpeg2dec->sequence.flags) & | |
488 SEQ_FLAG_LOW_DELAY)) { | |
12932 | 489 decoder->stride_frame = sequence->width; |
490 mpeg2_header_end (mpeg2dec); | |
491 mpeg2dec->action = invalid_end_action; | |
492 mpeg2dec->state = STATE_INVALID_END; | |
493 return; | |
494 } | |
27572 | 495 mpeg2dec->state = (memcmp (&(mpeg2dec->sequence), sequence, |
496 sizeof (mpeg2_sequence_t)) ? | |
497 STATE_SEQUENCE_MODIFIED : STATE_SEQUENCE_REPEATED); | |
12932 | 498 } else |
499 decoder->stride_frame = sequence->width; | |
9852 | 500 mpeg2dec->sequence = *sequence; |
12932 | 501 mpeg2_reset_info (&(mpeg2dec->info)); |
9852 | 502 mpeg2dec->info.sequence = &(mpeg2dec->sequence); |
12932 | 503 mpeg2dec->info.gop = NULL; |
504 info_user_data (mpeg2dec); | |
9852 | 505 } |
506 | |
507 int mpeg2_header_gop (mpeg2dec_t * mpeg2dec) | |
508 { | |
12932 | 509 uint8_t * buffer = mpeg2dec->chunk_start; |
510 mpeg2_gop_t * gop = &(mpeg2dec->new_gop); | |
511 | |
512 if (! (buffer[1] & 8)) | |
513 return 1; | |
514 gop->hours = (buffer[0] >> 2) & 31; | |
515 gop->minutes = ((buffer[0] << 4) | (buffer[1] >> 4)) & 63; | |
516 gop->seconds = ((buffer[1] << 3) | (buffer[2] >> 5)) & 63; | |
517 gop->pictures = ((buffer[2] << 1) | (buffer[3] >> 7)) & 63; | |
518 gop->flags = (buffer[0] >> 7) | ((buffer[3] >> 4) & 6); | |
9852 | 519 mpeg2dec->state = STATE_GOP; |
1 | 520 return 0; |
521 } | |
522 | |
12932 | 523 void mpeg2_header_gop_finalize (mpeg2dec_t * mpeg2dec) |
524 { | |
525 mpeg2dec->gop = mpeg2dec->new_gop; | |
526 mpeg2_reset_info (&(mpeg2dec->info)); | |
527 mpeg2dec->info.gop = &(mpeg2dec->gop); | |
528 info_user_data (mpeg2dec); | |
529 } | |
530 | |
531 void mpeg2_set_fbuf (mpeg2dec_t * mpeg2dec, int b_type) | |
1 | 532 { |
533 int i; | |
534 | |
9852 | 535 for (i = 0; i < 3; i++) |
536 if (mpeg2dec->fbuf[1] != &mpeg2dec->fbuf_alloc[i].fbuf && | |
537 mpeg2dec->fbuf[2] != &mpeg2dec->fbuf_alloc[i].fbuf) { | |
538 mpeg2dec->fbuf[0] = &mpeg2dec->fbuf_alloc[i].fbuf; | |
539 mpeg2dec->info.current_fbuf = mpeg2dec->fbuf[0]; | |
12932 | 540 if (b_type || (mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY)) { |
541 if (b_type || mpeg2dec->convert) | |
9852 | 542 mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[0]; |
543 mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[0]; | |
544 } | |
545 break; | |
546 } | |
547 } | |
548 | |
549 int mpeg2_header_picture (mpeg2dec_t * mpeg2dec) | |
550 { | |
551 uint8_t * buffer = mpeg2dec->chunk_start; | |
12932 | 552 mpeg2_picture_t * picture = &(mpeg2dec->new_picture); |
553 mpeg2_decoder_t * decoder = &(mpeg2dec->decoder); | |
9852 | 554 int type; |
555 | |
27572 | 556 mpeg2dec->state = ((mpeg2dec->state != STATE_SLICE_1ST) ? |
557 STATE_PICTURE : STATE_PICTURE_2ND); | |
9852 | 558 mpeg2dec->ext_state = PIC_CODING_EXT; |
559 | |
560 picture->temporal_reference = (buffer[0] << 2) | (buffer[1] >> 6); | |
561 | |
27572 | 562 type = (buffer [1] >> 3) & 7; |
9852 | 563 if (type == PIC_FLAG_CODING_TYPE_P || type == PIC_FLAG_CODING_TYPE_B) { |
564 /* forward_f_code and backward_f_code - used in mpeg1 only */ | |
565 decoder->f_motion.f_code[1] = (buffer[3] >> 2) & 1; | |
566 decoder->f_motion.f_code[0] = | |
567 (((buffer[3] << 1) | (buffer[4] >> 7)) & 7) - 1; | |
568 decoder->b_motion.f_code[1] = (buffer[4] >> 6) & 1; | |
569 decoder->b_motion.f_code[0] = ((buffer[4] >> 3) & 7) - 1; | |
1 | 570 } |
571 | |
27572 | 572 picture->flags = PIC_FLAG_PROGRESSIVE_FRAME | type; |
573 picture->tag = picture->tag2 = 0; | |
574 if (mpeg2dec->num_tags) { | |
575 if (mpeg2dec->bytes_since_tag >= mpeg2dec->chunk_ptr - buffer + 4) { | |
576 mpeg2dec->num_tags = 0; | |
577 picture->tag = mpeg2dec->tag_current; | |
578 picture->tag2 = mpeg2dec->tag2_current; | |
579 picture->flags |= PIC_FLAG_TAGS; | |
580 } else if (mpeg2dec->num_tags > 1) { | |
581 mpeg2dec->num_tags = 1; | |
582 picture->tag = mpeg2dec->tag_previous; | |
583 picture->tag2 = mpeg2dec->tag2_previous; | |
584 picture->flags |= PIC_FLAG_TAGS; | |
585 } | |
586 } | |
587 picture->nb_fields = 2; | |
588 picture->display_offset[0].x = picture->display_offset[1].x = | |
589 picture->display_offset[2].x = mpeg2dec->display_offset_x; | |
590 picture->display_offset[0].y = picture->display_offset[1].y = | |
591 picture->display_offset[2].y = mpeg2dec->display_offset_y; | |
592 | |
9852 | 593 /* XXXXXX decode extra_information_picture as well */ |
594 | |
27572 | 595 decoder->q_scale_type = 0; |
12932 | 596 decoder->intra_dc_precision = 7; |
597 decoder->frame_pred_frame_dct = 1; | |
598 decoder->concealment_motion_vectors = 0; | |
599 decoder->scan = mpeg2_scan_norm; | |
600 decoder->picture_structure = FRAME_PICTURE; | |
601 mpeg2dec->copy_matrix = 0; | |
602 | |
1 | 603 return 0; |
604 } | |
605 | |
9852 | 606 static int picture_coding_ext (mpeg2dec_t * mpeg2dec) |
1 | 607 { |
9852 | 608 uint8_t * buffer = mpeg2dec->chunk_start; |
12932 | 609 mpeg2_picture_t * picture = &(mpeg2dec->new_picture); |
610 mpeg2_decoder_t * decoder = &(mpeg2dec->decoder); | |
9852 | 611 uint32_t flags; |
1 | 612 |
9852 | 613 /* pre subtract 1 for use later in compute_motion_vector */ |
614 decoder->f_motion.f_code[0] = (buffer[0] & 15) - 1; | |
615 decoder->f_motion.f_code[1] = (buffer[1] >> 4) - 1; | |
616 decoder->b_motion.f_code[0] = (buffer[1] & 15) - 1; | |
617 decoder->b_motion.f_code[1] = (buffer[2] >> 4) - 1; | |
1 | 618 |
9852 | 619 flags = picture->flags; |
12932 | 620 decoder->intra_dc_precision = 7 - ((buffer[2] >> 2) & 3); |
9852 | 621 decoder->picture_structure = buffer[2] & 3; |
622 switch (decoder->picture_structure) { | |
623 case TOP_FIELD: | |
624 flags |= PIC_FLAG_TOP_FIELD_FIRST; | |
625 case BOTTOM_FIELD: | |
626 picture->nb_fields = 1; | |
627 break; | |
628 case FRAME_PICTURE: | |
629 if (!(mpeg2dec->sequence.flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE)) { | |
630 picture->nb_fields = (buffer[3] & 2) ? 3 : 2; | |
12932 | 631 flags |= (buffer[3] & 128) ? PIC_FLAG_TOP_FIELD_FIRST : 0; |
10509 | 632 flags |= (buffer[3] & 2) ? PIC_FLAG_REPEAT_FIRST_FIELD : 0; |
9852 | 633 } else |
634 picture->nb_fields = (buffer[3]&2) ? ((buffer[3]&128) ? 6 : 4) : 2; | |
635 break; | |
636 default: | |
637 return 1; | |
638 } | |
639 decoder->top_field_first = buffer[3] >> 7; | |
640 decoder->frame_pred_frame_dct = (buffer[3] >> 6) & 1; | |
641 decoder->concealment_motion_vectors = (buffer[3] >> 5) & 1; | |
27572 | 642 decoder->q_scale_type = buffer[3] & 16; |
9852 | 643 decoder->intra_vlc_format = (buffer[3] >> 3) & 1; |
644 decoder->scan = (buffer[3] & 4) ? mpeg2_scan_alt : mpeg2_scan_norm; | |
27572 | 645 if (!(buffer[4] & 0x80)) |
646 flags &= ~PIC_FLAG_PROGRESSIVE_FRAME; | |
9852 | 647 if (buffer[4] & 0x40) |
648 flags |= (((buffer[4]<<26) | (buffer[5]<<18) | (buffer[6]<<10)) & | |
649 PIC_MASK_COMPOSITE_DISPLAY) | PIC_FLAG_COMPOSITE_DISPLAY; | |
650 picture->flags = flags; | |
1 | 651 |
9852 | 652 mpeg2dec->ext_state = PIC_DISPLAY_EXT | COPYRIGHT_EXT | QUANT_MATRIX_EXT; |
653 | |
1 | 654 return 0; |
655 } | |
656 | |
9852 | 657 static int picture_display_ext (mpeg2dec_t * mpeg2dec) |
1 | 658 { |
9852 | 659 uint8_t * buffer = mpeg2dec->chunk_start; |
12932 | 660 mpeg2_picture_t * picture = &(mpeg2dec->new_picture); |
9852 | 661 int i, nb_pos; |
662 | |
663 nb_pos = picture->nb_fields; | |
664 if (mpeg2dec->sequence.flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE) | |
665 nb_pos >>= 1; | |
666 | |
667 for (i = 0; i < nb_pos; i++) { | |
668 int x, y; | |
1 | 669 |
9852 | 670 x = ((buffer[4*i] << 24) | (buffer[4*i+1] << 16) | |
671 (buffer[4*i+2] << 8) | buffer[4*i+3]) >> (11-2*i); | |
672 y = ((buffer[4*i+2] << 24) | (buffer[4*i+3] << 16) | | |
673 (buffer[4*i+4] << 8) | buffer[4*i+5]) >> (10-2*i); | |
674 if (! (x & y & 1)) | |
675 return 1; | |
676 picture->display_offset[i].x = mpeg2dec->display_offset_x = x >> 1; | |
677 picture->display_offset[i].y = mpeg2dec->display_offset_y = y >> 1; | |
678 } | |
679 for (; i < 3; i++) { | |
680 picture->display_offset[i].x = mpeg2dec->display_offset_x; | |
681 picture->display_offset[i].y = mpeg2dec->display_offset_y; | |
682 } | |
683 return 0; | |
684 } | |
1 | 685 |
12932 | 686 void mpeg2_header_picture_finalize (mpeg2dec_t * mpeg2dec, uint32_t accels) |
687 { | |
688 mpeg2_decoder_t * decoder = &(mpeg2dec->decoder); | |
689 int old_type_b = (decoder->coding_type == B_TYPE); | |
690 int low_delay = mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY; | |
691 | |
692 finalize_matrix (mpeg2dec); | |
693 decoder->coding_type = mpeg2dec->new_picture.flags & PIC_MASK_CODING_TYPE; | |
694 | |
695 if (mpeg2dec->state == STATE_PICTURE) { | |
696 mpeg2_picture_t * picture; | |
697 mpeg2_picture_t * other; | |
698 | |
699 decoder->second_field = 0; | |
700 | |
701 picture = other = mpeg2dec->pictures; | |
702 if (old_type_b ^ (mpeg2dec->picture < mpeg2dec->pictures + 2)) | |
703 picture += 2; | |
704 else | |
705 other += 2; | |
706 mpeg2dec->picture = picture; | |
707 *picture = mpeg2dec->new_picture; | |
708 | |
709 if (!old_type_b) { | |
710 mpeg2dec->fbuf[2] = mpeg2dec->fbuf[1]; | |
711 mpeg2dec->fbuf[1] = mpeg2dec->fbuf[0]; | |
712 } | |
713 mpeg2dec->fbuf[0] = NULL; | |
714 mpeg2_reset_info (&(mpeg2dec->info)); | |
715 mpeg2dec->info.current_picture = picture; | |
716 mpeg2dec->info.display_picture = picture; | |
717 if (decoder->coding_type != B_TYPE) { | |
718 if (!low_delay) { | |
719 if (mpeg2dec->first) { | |
720 mpeg2dec->info.display_picture = NULL; | |
721 mpeg2dec->first = 0; | |
722 } else { | |
723 mpeg2dec->info.display_picture = other; | |
724 if (other->nb_fields == 1) | |
725 mpeg2dec->info.display_picture_2nd = other + 1; | |
726 mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[1]; | |
727 } | |
728 } | |
729 if (!low_delay + !mpeg2dec->convert) | |
730 mpeg2dec->info.discard_fbuf = | |
731 mpeg2dec->fbuf[!low_delay + !mpeg2dec->convert]; | |
732 } | |
733 if (mpeg2dec->convert) { | |
734 mpeg2_convert_init_t convert_init; | |
735 if (!mpeg2dec->convert_start) { | |
736 int y_size, uv_size; | |
737 | |
738 mpeg2dec->decoder.convert_id = | |
739 mpeg2_malloc (mpeg2dec->convert_id_size, | |
740 MPEG2_ALLOC_CONVERT_ID); | |
741 mpeg2dec->convert (MPEG2_CONVERT_START, | |
742 mpeg2dec->decoder.convert_id, | |
743 &(mpeg2dec->sequence), | |
744 mpeg2dec->convert_stride, accels, | |
745 mpeg2dec->convert_arg, &convert_init); | |
746 mpeg2dec->convert_start = convert_init.start; | |
747 mpeg2dec->decoder.convert = convert_init.copy; | |
748 | |
749 y_size = decoder->stride_frame * mpeg2dec->sequence.height; | |
750 uv_size = y_size >> (2 - mpeg2dec->decoder.chroma_format); | |
751 mpeg2dec->yuv_buf[0][0] = | |
752 (uint8_t *) mpeg2_malloc (y_size, MPEG2_ALLOC_YUV); | |
753 mpeg2dec->yuv_buf[0][1] = | |
754 (uint8_t *) mpeg2_malloc (uv_size, MPEG2_ALLOC_YUV); | |
755 mpeg2dec->yuv_buf[0][2] = | |
756 (uint8_t *) mpeg2_malloc (uv_size, MPEG2_ALLOC_YUV); | |
757 mpeg2dec->yuv_buf[1][0] = | |
758 (uint8_t *) mpeg2_malloc (y_size, MPEG2_ALLOC_YUV); | |
759 mpeg2dec->yuv_buf[1][1] = | |
760 (uint8_t *) mpeg2_malloc (uv_size, MPEG2_ALLOC_YUV); | |
761 mpeg2dec->yuv_buf[1][2] = | |
762 (uint8_t *) mpeg2_malloc (uv_size, MPEG2_ALLOC_YUV); | |
763 y_size = decoder->stride_frame * 32; | |
764 uv_size = y_size >> (2 - mpeg2dec->decoder.chroma_format); | |
765 mpeg2dec->yuv_buf[2][0] = | |
766 (uint8_t *) mpeg2_malloc (y_size, MPEG2_ALLOC_YUV); | |
767 mpeg2dec->yuv_buf[2][1] = | |
768 (uint8_t *) mpeg2_malloc (uv_size, MPEG2_ALLOC_YUV); | |
769 mpeg2dec->yuv_buf[2][2] = | |
770 (uint8_t *) mpeg2_malloc (uv_size, MPEG2_ALLOC_YUV); | |
771 } | |
772 if (!mpeg2dec->custom_fbuf) { | |
773 while (mpeg2dec->alloc_index < 3) { | |
774 mpeg2_fbuf_t * fbuf; | |
775 | |
776 fbuf = &mpeg2dec->fbuf_alloc[mpeg2dec->alloc_index++].fbuf; | |
777 fbuf->id = NULL; | |
778 fbuf->buf[0] = | |
779 (uint8_t *) mpeg2_malloc (convert_init.buf_size[0], | |
780 MPEG2_ALLOC_CONVERTED); | |
781 fbuf->buf[1] = | |
782 (uint8_t *) mpeg2_malloc (convert_init.buf_size[1], | |
783 MPEG2_ALLOC_CONVERTED); | |
784 fbuf->buf[2] = | |
785 (uint8_t *) mpeg2_malloc (convert_init.buf_size[2], | |
786 MPEG2_ALLOC_CONVERTED); | |
787 } | |
788 mpeg2_set_fbuf (mpeg2dec, (decoder->coding_type == B_TYPE)); | |
789 } | |
790 } else if (!mpeg2dec->custom_fbuf) { | |
791 while (mpeg2dec->alloc_index < 3) { | |
792 mpeg2_fbuf_t * fbuf; | |
793 int y_size, uv_size; | |
794 | |
795 fbuf = &(mpeg2dec->fbuf_alloc[mpeg2dec->alloc_index++].fbuf); | |
796 fbuf->id = NULL; | |
797 y_size = decoder->stride_frame * mpeg2dec->sequence.height; | |
798 uv_size = y_size >> (2 - decoder->chroma_format); | |
799 fbuf->buf[0] = (uint8_t *) mpeg2_malloc (y_size, | |
800 MPEG2_ALLOC_YUV); | |
801 fbuf->buf[1] = (uint8_t *) mpeg2_malloc (uv_size, | |
802 MPEG2_ALLOC_YUV); | |
803 fbuf->buf[2] = (uint8_t *) mpeg2_malloc (uv_size, | |
804 MPEG2_ALLOC_YUV); | |
805 } | |
806 mpeg2_set_fbuf (mpeg2dec, (decoder->coding_type == B_TYPE)); | |
807 } | |
808 } else { | |
809 decoder->second_field = 1; | |
810 mpeg2dec->picture++; /* second field picture */ | |
811 *(mpeg2dec->picture) = mpeg2dec->new_picture; | |
812 mpeg2dec->info.current_picture_2nd = mpeg2dec->picture; | |
813 if (low_delay || decoder->coding_type == B_TYPE) | |
814 mpeg2dec->info.display_picture_2nd = mpeg2dec->picture; | |
815 } | |
816 | |
817 info_user_data (mpeg2dec); | |
818 } | |
819 | |
9852 | 820 static int copyright_ext (mpeg2dec_t * mpeg2dec) |
821 { | |
822 return 0; | |
823 } | |
824 | |
825 static int quant_matrix_ext (mpeg2dec_t * mpeg2dec) | |
826 { | |
827 uint8_t * buffer = mpeg2dec->chunk_start; | |
12932 | 828 int i, j; |
9852 | 829 |
12932 | 830 for (i = 0; i < 4; i++) |
831 if (buffer[0] & (8 >> i)) { | |
832 for (j = 0; j < 64; j++) | |
833 mpeg2dec->new_quantizer_matrix[i][mpeg2_scan_norm[j]] = | |
834 (buffer[j] << (i+5)) | (buffer[j+1] >> (3-i)); | |
835 mpeg2dec->copy_matrix |= 1 << i; | |
836 buffer += 64; | |
837 } | |
9852 | 838 |
1 | 839 return 0; |
840 } | |
841 | |
9852 | 842 int mpeg2_header_extension (mpeg2dec_t * mpeg2dec) |
1 | 843 { |
9852 | 844 static int (* parser[]) (mpeg2dec_t *) = { |
845 0, sequence_ext, sequence_display_ext, quant_matrix_ext, | |
846 copyright_ext, 0, 0, picture_display_ext, picture_coding_ext | |
847 }; | |
848 int ext, ext_bit; | |
849 | |
850 ext = mpeg2dec->chunk_start[0] >> 4; | |
851 ext_bit = 1 << ext; | |
1 | 852 |
9852 | 853 if (!(mpeg2dec->ext_state & ext_bit)) |
854 return 0; /* ignore illegal extensions */ | |
855 mpeg2dec->ext_state &= ~ext_bit; | |
856 return parser[ext] (mpeg2dec); | |
857 } | |
1 | 858 |
9852 | 859 int mpeg2_header_user_data (mpeg2dec_t * mpeg2dec) |
860 { | |
12932 | 861 mpeg2dec->user_data_len += mpeg2dec->chunk_ptr - 1 - mpeg2dec->chunk_start; |
9852 | 862 mpeg2dec->chunk_start = mpeg2dec->chunk_ptr - 1; |
29264
e83eef58b30a
Remove all kind of trailing whitespaces from all MPlayer's files.
bircoph
parents:
27572
diff
changeset
|
863 |
1 | 864 return 0; |
865 } | |
9852 | 866 |
27572 | 867 static void prescale (mpeg2dec_t * mpeg2dec, int idx) |
9852 | 868 { |
12932 | 869 static int non_linear_scale [] = { |
870 0, 1, 2, 3, 4, 5, 6, 7, | |
871 8, 10, 12, 14, 16, 18, 20, 22, | |
872 24, 28, 32, 36, 40, 44, 48, 52, | |
873 56, 64, 72, 80, 88, 96, 104, 112 | |
874 }; | |
875 int i, j, k; | |
876 mpeg2_decoder_t * decoder = &(mpeg2dec->decoder); | |
877 | |
27572 | 878 if (mpeg2dec->scaled[idx] != decoder->q_scale_type) { |
879 mpeg2dec->scaled[idx] = decoder->q_scale_type; | |
12932 | 880 for (i = 0; i < 32; i++) { |
27572 | 881 k = decoder->q_scale_type ? non_linear_scale[i] : (i << 1); |
13123 | 882 decoder->quantizer_scales[i] = k; |
12932 | 883 for (j = 0; j < 64; j++) |
27572 | 884 decoder->quantizer_prescale[idx][i][j] = |
885 k * mpeg2dec->quantizer_matrix[idx][j]; | |
12932 | 886 } |
887 } | |
888 } | |
889 | |
890 mpeg2_state_t mpeg2_header_slice_start (mpeg2dec_t * mpeg2dec) | |
891 { | |
892 mpeg2_decoder_t * decoder = &(mpeg2dec->decoder); | |
893 | |
894 mpeg2dec->info.user_data = NULL; mpeg2dec->info.user_data_len = 0; | |
9852 | 895 mpeg2dec->state = ((mpeg2dec->picture->nb_fields > 1 || |
896 mpeg2dec->state == STATE_PICTURE_2ND) ? | |
897 STATE_SLICE : STATE_SLICE_1ST); | |
898 | |
12932 | 899 if (mpeg2dec->decoder.coding_type != D_TYPE) { |
900 prescale (mpeg2dec, 0); | |
901 if (decoder->chroma_quantizer[0] == decoder->quantizer_prescale[2]) | |
902 prescale (mpeg2dec, 2); | |
903 if (mpeg2dec->decoder.coding_type != I_TYPE) { | |
904 prescale (mpeg2dec, 1); | |
905 if (decoder->chroma_quantizer[1] == decoder->quantizer_prescale[3]) | |
906 prescale (mpeg2dec, 3); | |
907 } | |
908 } | |
909 | |
9852 | 910 if (!(mpeg2dec->nb_decode_slices)) |
911 mpeg2dec->picture->flags |= PIC_FLAG_SKIP; | |
912 else if (mpeg2dec->convert_start) { | |
12932 | 913 mpeg2dec->convert_start (decoder->convert_id, mpeg2dec->fbuf[0], |
914 mpeg2dec->picture, mpeg2dec->info.gop); | |
9852 | 915 |
916 if (mpeg2dec->decoder.coding_type == B_TYPE) | |
917 mpeg2_init_fbuf (&(mpeg2dec->decoder), mpeg2dec->yuv_buf[2], | |
918 mpeg2dec->yuv_buf[mpeg2dec->yuv_index ^ 1], | |
919 mpeg2dec->yuv_buf[mpeg2dec->yuv_index]); | |
920 else { | |
921 mpeg2_init_fbuf (&(mpeg2dec->decoder), | |
922 mpeg2dec->yuv_buf[mpeg2dec->yuv_index ^ 1], | |
923 mpeg2dec->yuv_buf[mpeg2dec->yuv_index], | |
924 mpeg2dec->yuv_buf[mpeg2dec->yuv_index]); | |
925 if (mpeg2dec->state == STATE_SLICE) | |
926 mpeg2dec->yuv_index ^= 1; | |
927 } | |
928 } else { | |
929 int b_type; | |
930 | |
931 b_type = (mpeg2dec->decoder.coding_type == B_TYPE); | |
932 mpeg2_init_fbuf (&(mpeg2dec->decoder), mpeg2dec->fbuf[0]->buf, | |
933 mpeg2dec->fbuf[b_type + 1]->buf, | |
934 mpeg2dec->fbuf[b_type]->buf); | |
935 } | |
936 mpeg2dec->action = NULL; | |
27572 | 937 return STATE_INTERNAL_NORETURN; |
9852 | 938 } |
939 | |
12932 | 940 static mpeg2_state_t seek_sequence (mpeg2dec_t * mpeg2dec) |
9852 | 941 { |
12932 | 942 mpeg2_reset_info (&(mpeg2dec->info)); |
943 mpeg2dec->info.sequence = NULL; | |
944 mpeg2dec->info.gop = NULL; | |
945 mpeg2_header_state_init (mpeg2dec); | |
946 mpeg2dec->action = mpeg2_seek_header; | |
947 return mpeg2_seek_header (mpeg2dec); | |
948 } | |
949 | |
950 mpeg2_state_t mpeg2_header_end (mpeg2dec_t * mpeg2dec) | |
951 { | |
952 mpeg2_picture_t * picture; | |
9852 | 953 int b_type; |
954 | |
12932 | 955 b_type = (mpeg2dec->decoder.coding_type == B_TYPE); |
9852 | 956 picture = mpeg2dec->pictures; |
12932 | 957 if ((mpeg2dec->picture >= picture + 2) ^ b_type) |
9852 | 958 picture = mpeg2dec->pictures + 2; |
959 | |
12932 | 960 mpeg2_reset_info (&(mpeg2dec->info)); |
9852 | 961 if (!(mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY)) { |
962 mpeg2dec->info.display_picture = picture; | |
963 if (picture->nb_fields == 1) | |
964 mpeg2dec->info.display_picture_2nd = picture + 1; | |
965 mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[b_type]; | |
12932 | 966 if (!mpeg2dec->convert) |
9852 | 967 mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[b_type + 1]; |
12932 | 968 } else if (!mpeg2dec->convert) |
9852 | 969 mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[b_type]; |
12932 | 970 mpeg2dec->action = seek_sequence; |
9852 | 971 return STATE_END; |
972 } |