Mercurial > mplayer.hg
annotate libmpeg2/header.c @ 12875:32104203ddea
reorganized, reformatted, explanation improved, typos, wording
author | diego |
---|---|
date | Tue, 20 Jul 2004 18:02:22 +0000 |
parents | 55b9c352d019 |
children | d0a8810e155c |
rev | line source |
---|---|
1 | 1 /* |
9852 | 2 * header.c |
3 * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org> | |
4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca> | |
1 | 5 * |
6 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder. | |
9852 | 7 * See http://libmpeg2.sourceforge.net/ for updates. |
1 | 8 * |
9 * mpeg2dec is free software; you can redistribute it and/or modify | |
10 * it under the terms of the GNU General Public License as published by | |
11 * the Free Software Foundation; either version 2 of the License, or | |
12 * (at your option) any later version. | |
13 * | |
14 * mpeg2dec is distributed in the hope that it will be useful, | |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 * GNU General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU General Public License | |
20 * along with this program; if not, write to the Free Software | |
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 */ | |
23 | |
24 #include "config.h" | |
25 | |
26 #include <inttypes.h> | |
9852 | 27 #include <stdlib.h> /* defines NULL */ |
28 #include <string.h> /* memcmp */ | |
1 | 29 |
9852 | 30 #include "mpeg2.h" |
1 | 31 #include "mpeg2_internal.h" |
9852 | 32 #include "convert.h" |
1 | 33 #include "attributes.h" |
34 | |
9852 | 35 #define SEQ_EXT 2 |
36 #define SEQ_DISPLAY_EXT 4 | |
37 #define QUANT_MATRIX_EXT 8 | |
38 #define COPYRIGHT_EXT 0x10 | |
39 #define PIC_DISPLAY_EXT 0x80 | |
40 #define PIC_CODING_EXT 0x100 | |
41 | |
36 | 42 /* default intra quant matrix, in zig-zag order */ |
9852 | 43 static const uint8_t default_intra_quantizer_matrix[64] ATTR_ALIGN(16) = { |
1 | 44 8, |
45 16, 16, | |
46 19, 16, 19, | |
47 22, 22, 22, 22, | |
48 22, 22, 26, 24, 26, | |
49 27, 27, 27, 26, 26, 26, | |
50 26, 27, 27, 27, 29, 29, 29, | |
51 34, 34, 34, 29, 29, 29, 27, 27, | |
52 29, 29, 32, 32, 34, 34, 37, | |
53 38, 37, 35, 35, 34, 35, | |
54 38, 38, 40, 40, 40, | |
55 48, 48, 46, 46, | |
56 56, 56, 58, | |
57 69, 69, | |
58 83 | |
59 }; | |
60 | |
9852 | 61 uint8_t mpeg2_scan_norm[64] ATTR_ALIGN(16) = { |
36 | 62 /* Zig-Zag scan pattern */ |
9852 | 63 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, |
64 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, | |
65 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, | |
66 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63 | |
67 }; | |
68 | |
69 uint8_t mpeg2_scan_alt[64] ATTR_ALIGN(16) = { | |
70 /* Alternate scan pattern */ | |
71 0, 8, 16, 24, 1, 9, 2, 10, 17, 25, 32, 40, 48, 56, 57, 49, | |
72 41, 33, 26, 18, 3, 11, 4, 12, 19, 27, 34, 42, 50, 58, 35, 43, | |
73 51, 59, 20, 28, 5, 13, 6, 14, 21, 29, 36, 44, 52, 60, 37, 45, | |
74 53, 61, 22, 30, 7, 15, 23, 31, 38, 46, 54, 62, 39, 47, 55, 63 | |
1 | 75 }; |
76 | |
9852 | 77 void mpeg2_header_state_init (mpeg2dec_t * mpeg2dec) |
1 | 78 { |
9852 | 79 mpeg2dec->decoder.scan = mpeg2_scan_norm; |
80 mpeg2dec->picture = mpeg2dec->pictures; | |
81 mpeg2dec->fbuf[0] = &mpeg2dec->fbuf_alloc[0].fbuf; | |
82 mpeg2dec->fbuf[1] = &mpeg2dec->fbuf_alloc[1].fbuf; | |
83 mpeg2dec->fbuf[2] = &mpeg2dec->fbuf_alloc[2].fbuf; | |
84 mpeg2dec->first = 1; | |
85 mpeg2dec->alloc_index = 0; | |
86 mpeg2dec->alloc_index_user = 0; | |
1 | 87 } |
88 | |
9852 | 89 static void reset_info (mpeg2_info_t * info) |
1 | 90 { |
9852 | 91 info->current_picture = info->current_picture_2nd = NULL; |
92 info->display_picture = info->display_picture_2nd = NULL; | |
93 info->current_fbuf = info->display_fbuf = info->discard_fbuf = NULL; | |
94 info->user_data = NULL; info->user_data_len = 0; | |
95 } | |
96 | |
97 int mpeg2_header_sequence (mpeg2dec_t * mpeg2dec) | |
98 { | |
99 uint8_t * buffer = mpeg2dec->chunk_start; | |
100 sequence_t * sequence = &(mpeg2dec->new_sequence); | |
101 decoder_t * decoder = &(mpeg2dec->decoder); | |
102 static unsigned int frame_period[9] = { | |
103 0, 1126125, 1125000, 1080000, 900900, 900000, 540000, 450450, 450000 | |
104 }; | |
36 | 105 int width, height; |
1 | 106 int i; |
107 | |
9852 | 108 if ((buffer[6] & 0x20) != 0x20) /* missing marker_bit */ |
109 return 1; | |
1 | 110 |
9852 | 111 i = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2]; |
112 sequence->display_width = sequence->picture_width = width = i >> 12; | |
113 sequence->display_height = sequence->picture_height = height = i & 0xfff; | |
12617 | 114 |
115 if(width == 0 || height == 0) | |
116 return 1; | |
117 | |
9852 | 118 decoder->width = sequence->width = width = (width + 15) & ~15; |
119 decoder->height = sequence->height = height = (height + 15) & ~15; | |
120 decoder->vertical_position_extension = (height > 2800); | |
121 sequence->chroma_width = width >> 1; | |
122 sequence->chroma_height = height >> 1; | |
1 | 123 |
9852 | 124 sequence->flags = SEQ_FLAG_PROGRESSIVE_SEQUENCE; |
125 | |
126 sequence->pixel_width = buffer[3] >> 4; /* aspect ratio */ | |
127 sequence->frame_period = 0; | |
128 if ((buffer[3] & 15) < 9) | |
129 sequence->frame_period = frame_period[buffer[3] & 15]; | |
36 | 130 |
9852 | 131 sequence->byte_rate = (buffer[4]<<10) | (buffer[5]<<2) | (buffer[6]>>6); |
132 | |
133 sequence->vbv_buffer_size = ((buffer[6]<<16)|(buffer[7]<<8))&0x1ff800; | |
134 | |
135 if (buffer[7] & 4) | |
136 sequence->flags |= SEQ_FLAG_CONSTRAINED_PARAMETERS; | |
1 | 137 |
138 if (buffer[7] & 2) { | |
139 for (i = 0; i < 64; i++) | |
9852 | 140 decoder->intra_quantizer_matrix[mpeg2_scan_norm[i]] = |
1 | 141 (buffer[i+7] << 7) | (buffer[i+8] >> 1); |
142 buffer += 64; | |
9852 | 143 } else |
144 for (i = 0; i < 64; i++) | |
145 decoder->intra_quantizer_matrix[mpeg2_scan_norm[i]] = | |
146 default_intra_quantizer_matrix [i]; | |
147 | |
148 if (buffer[7] & 1) | |
149 for (i = 0; i < 64; i++) | |
150 decoder->non_intra_quantizer_matrix[mpeg2_scan_norm[i]] = | |
151 buffer[i+8]; | |
152 else | |
1 | 153 for (i = 0; i < 64; i++) |
9852 | 154 decoder->non_intra_quantizer_matrix[i] = 16; |
155 | |
156 sequence->profile_level_id = 0x80; | |
157 sequence->colour_primaries = 1; | |
158 sequence->transfer_characteristics = 1; | |
159 sequence->matrix_coefficients = 1; | |
160 | |
161 decoder->mpeg1 = 1; | |
162 decoder->intra_dc_precision = 0; | |
163 decoder->frame_pred_frame_dct = 1; | |
164 decoder->q_scale_type = 0; | |
165 decoder->concealment_motion_vectors = 0; | |
166 decoder->scan = mpeg2_scan_norm; | |
167 decoder->picture_structure = FRAME_PICTURE; | |
168 | |
169 mpeg2dec->ext_state = SEQ_EXT; | |
170 mpeg2dec->state = STATE_SEQUENCE; | |
171 mpeg2dec->display_offset_x = mpeg2dec->display_offset_y = 0; | |
172 | |
173 reset_info (&(mpeg2dec->info)); | |
174 return 0; | |
175 } | |
176 | |
177 static int sequence_ext (mpeg2dec_t * mpeg2dec) | |
178 { | |
179 uint8_t * buffer = mpeg2dec->chunk_start; | |
180 sequence_t * sequence = &(mpeg2dec->new_sequence); | |
181 decoder_t * decoder = &(mpeg2dec->decoder); | |
182 int width, height; | |
183 uint32_t flags; | |
184 | |
185 if (!(buffer[3] & 1)) | |
186 return 1; | |
187 | |
188 sequence->profile_level_id = (buffer[0] << 4) | (buffer[1] >> 4); | |
189 | |
190 width = sequence->display_width = sequence->picture_width += | |
191 ((buffer[1] << 13) | (buffer[2] << 5)) & 0x3000; | |
192 height = sequence->display_height = sequence->picture_height += | |
193 (buffer[2] << 7) & 0x3000; | |
194 decoder->vertical_position_extension = (height > 2800); | |
195 flags = sequence->flags | SEQ_FLAG_MPEG2; | |
196 if (!(buffer[1] & 8)) { | |
197 flags &= ~SEQ_FLAG_PROGRESSIVE_SEQUENCE; | |
198 height = (height + 31) & ~31; | |
199 } | |
200 if (buffer[5] & 0x80) | |
201 flags |= SEQ_FLAG_LOW_DELAY; | |
202 sequence->flags = flags; | |
203 decoder->width = sequence->width = width = (width + 15) & ~15; | |
204 decoder->height = sequence->height = height = (height + 15) & ~15; | |
205 switch (buffer[1] & 6) { | |
206 case 0: /* invalid */ | |
207 return 1; | |
208 case 2: /* 4:2:0 */ | |
209 height >>= 1; | |
210 case 4: /* 4:2:2 */ | |
211 width >>= 1; | |
212 } | |
213 sequence->chroma_width = width; | |
214 sequence->chroma_height = height; | |
215 | |
216 sequence->byte_rate += ((buffer[2]<<25) | (buffer[3]<<17)) & 0x3ffc0000; | |
217 | |
218 sequence->vbv_buffer_size |= buffer[4] << 21; | |
219 | |
220 sequence->frame_period = | |
221 sequence->frame_period * ((buffer[5]&31)+1) / (((buffer[5]>>2)&3)+1); | |
222 | |
223 decoder->mpeg1 = 0; | |
224 | |
225 mpeg2dec->ext_state = SEQ_DISPLAY_EXT; | |
226 | |
227 return 0; | |
228 } | |
229 | |
230 static int sequence_display_ext (mpeg2dec_t * mpeg2dec) | |
231 { | |
232 uint8_t * buffer = mpeg2dec->chunk_start; | |
233 sequence_t * sequence = &(mpeg2dec->new_sequence); | |
234 uint32_t flags; | |
235 | |
236 flags = ((sequence->flags & ~SEQ_MASK_VIDEO_FORMAT) | | |
237 ((buffer[0]<<4) & SEQ_MASK_VIDEO_FORMAT)); | |
238 if (buffer[0] & 1) { | |
239 flags |= SEQ_FLAG_COLOUR_DESCRIPTION; | |
240 sequence->colour_primaries = buffer[1]; | |
241 sequence->transfer_characteristics = buffer[2]; | |
242 sequence->matrix_coefficients = buffer[3]; | |
243 buffer += 3; | |
1 | 244 } |
245 | |
9852 | 246 if (!(buffer[2] & 2)) /* missing marker_bit */ |
247 return 1; | |
1 | 248 |
9852 | 249 sequence->display_width = (buffer[1] << 6) | (buffer[2] >> 2); |
250 sequence->display_height = | |
251 ((buffer[2]& 1 ) << 13) | (buffer[3] << 5) | (buffer[4] >> 3); | |
1 | 252 |
253 return 0; | |
254 } | |
255 | |
9852 | 256 static inline void finalize_sequence (sequence_t * sequence) |
1 | 257 { |
9852 | 258 int width; |
259 int height; | |
260 | |
261 sequence->byte_rate *= 50; | |
262 | |
263 if (sequence->flags & SEQ_FLAG_MPEG2) { | |
264 switch (sequence->pixel_width) { | |
265 case 1: /* square pixels */ | |
266 sequence->pixel_width = sequence->pixel_height = 1; return; | |
267 case 2: /* 4:3 aspect ratio */ | |
268 width = 4; height = 3; break; | |
269 case 3: /* 16:9 aspect ratio */ | |
270 width = 16; height = 9; break; | |
271 case 4: /* 2.21:1 aspect ratio */ | |
272 width = 221; height = 100; break; | |
273 default: /* illegal */ | |
274 sequence->pixel_width = sequence->pixel_height = 0; return; | |
275 } | |
276 width *= sequence->display_height; | |
277 height *= sequence->display_width; | |
278 | |
279 } else { | |
280 if (sequence->byte_rate == 50 * 0x3ffff) | |
281 sequence->byte_rate = 0; /* mpeg-1 VBR */ | |
1 | 282 |
9852 | 283 switch (sequence->pixel_width) { |
284 case 0: case 15: /* illegal */ | |
285 sequence->pixel_width = sequence->pixel_height = 0; return; | |
286 case 1: /* square pixels */ | |
287 sequence->pixel_width = sequence->pixel_height = 1; return; | |
288 case 3: /* 720x576 16:9 */ | |
289 sequence->pixel_width = 64; sequence->pixel_height = 45; return; | |
290 case 6: /* 720x480 16:9 */ | |
291 sequence->pixel_width = 32; sequence->pixel_height = 27; return; | |
292 case 12: /* 720*480 4:3 */ | |
293 sequence->pixel_width = 8; sequence->pixel_height = 9; return; | |
294 default: | |
295 height = 88 * sequence->pixel_width + 1171; | |
296 width = 2000; | |
297 } | |
298 } | |
1 | 299 |
9852 | 300 sequence->pixel_width = width; |
301 sequence->pixel_height = height; | |
302 while (width) { /* find greatest common divisor */ | |
303 int tmp = width; | |
304 width = height % tmp; | |
305 height = tmp; | |
306 } | |
307 sequence->pixel_width /= height; | |
308 sequence->pixel_height /= height; | |
309 } | |
310 | |
311 void mpeg2_header_sequence_finalize (mpeg2dec_t * mpeg2dec) | |
312 { | |
313 sequence_t * sequence = &(mpeg2dec->new_sequence); | |
314 | |
315 finalize_sequence (sequence); | |
36 | 316 |
9852 | 317 /* |
318 * according to 6.1.1.6, repeat sequence headers should be | |
319 * identical to the original. However some DVDs dont respect that | |
320 * and have different bitrates in the repeat sequence headers. So | |
321 * we'll ignore that in the comparison and still consider these as | |
322 * repeat sequence headers. | |
323 */ | |
324 mpeg2dec->sequence.byte_rate = sequence->byte_rate; | |
325 if (!memcmp (&(mpeg2dec->sequence), sequence, sizeof (sequence_t))) | |
326 mpeg2dec->state = STATE_SEQUENCE_REPEATED; | |
327 mpeg2dec->sequence = *sequence; | |
1 | 328 |
9852 | 329 mpeg2dec->info.sequence = &(mpeg2dec->sequence); |
330 } | |
331 | |
332 int mpeg2_header_gop (mpeg2dec_t * mpeg2dec) | |
333 { | |
334 mpeg2dec->state = STATE_GOP; | |
335 reset_info (&(mpeg2dec->info)); | |
1 | 336 return 0; |
337 } | |
338 | |
9852 | 339 void mpeg2_set_fbuf (mpeg2dec_t * mpeg2dec, int coding_type) |
1 | 340 { |
341 int i; | |
342 | |
9852 | 343 for (i = 0; i < 3; i++) |
344 if (mpeg2dec->fbuf[1] != &mpeg2dec->fbuf_alloc[i].fbuf && | |
345 mpeg2dec->fbuf[2] != &mpeg2dec->fbuf_alloc[i].fbuf) { | |
346 mpeg2dec->fbuf[0] = &mpeg2dec->fbuf_alloc[i].fbuf; | |
347 mpeg2dec->info.current_fbuf = mpeg2dec->fbuf[0]; | |
348 if ((coding_type == B_TYPE) || | |
349 (mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY)) { | |
350 if ((coding_type == B_TYPE) || (mpeg2dec->convert_start)) | |
351 mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[0]; | |
352 mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[0]; | |
353 } | |
354 break; | |
355 } | |
356 } | |
357 | |
358 int mpeg2_header_picture_start (mpeg2dec_t * mpeg2dec) | |
359 { | |
360 decoder_t * decoder = &(mpeg2dec->decoder); | |
361 picture_t * picture; | |
362 | |
363 if (mpeg2dec->state != STATE_SLICE_1ST) { | |
364 mpeg2dec->state = STATE_PICTURE; | |
365 picture = mpeg2dec->pictures; | |
366 if ((decoder->coding_type != PIC_FLAG_CODING_TYPE_B) ^ | |
367 (mpeg2dec->picture >= mpeg2dec->pictures + 2)) | |
368 picture += 2; | |
369 } else { | |
370 mpeg2dec->state = STATE_PICTURE_2ND; | |
371 picture = mpeg2dec->picture + 1; /* second field picture */ | |
372 } | |
373 mpeg2dec->picture = picture; | |
374 picture->flags = 0; | |
375 if (mpeg2dec->num_pts) { | |
376 if (mpeg2dec->bytes_since_pts >= 4) { | |
377 mpeg2dec->num_pts = 0; | |
378 picture->pts = mpeg2dec->pts_current; | |
379 picture->flags = PIC_FLAG_PTS; | |
380 } else if (mpeg2dec->num_pts > 1) { | |
381 mpeg2dec->num_pts = 1; | |
382 picture->pts = mpeg2dec->pts_previous; | |
383 picture->flags = PIC_FLAG_PTS; | |
384 } | |
385 } | |
386 picture->display_offset[0].x = picture->display_offset[1].x = | |
387 picture->display_offset[2].x = mpeg2dec->display_offset_x; | |
388 picture->display_offset[0].y = picture->display_offset[1].y = | |
389 picture->display_offset[2].y = mpeg2dec->display_offset_y; | |
390 return mpeg2_parse_header (mpeg2dec); | |
391 } | |
392 | |
393 int mpeg2_header_picture (mpeg2dec_t * mpeg2dec) | |
394 { | |
395 uint8_t * buffer = mpeg2dec->chunk_start; | |
396 picture_t * picture = mpeg2dec->picture; | |
397 decoder_t * decoder = &(mpeg2dec->decoder); | |
398 int type; | |
399 int low_delay; | |
400 | |
401 type = (buffer [1] >> 3) & 7; | |
402 low_delay = mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY; | |
403 | |
404 if (mpeg2dec->state == STATE_PICTURE) { | |
405 picture_t * other; | |
406 | |
407 decoder->second_field = 0; | |
408 other = mpeg2dec->pictures; | |
409 if (other == picture) | |
410 other += 2; | |
411 if (decoder->coding_type != PIC_FLAG_CODING_TYPE_B) { | |
412 mpeg2dec->fbuf[2] = mpeg2dec->fbuf[1]; | |
413 mpeg2dec->fbuf[1] = mpeg2dec->fbuf[0]; | |
414 } | |
415 mpeg2dec->fbuf[0] = NULL; | |
416 reset_info (&(mpeg2dec->info)); | |
417 mpeg2dec->info.current_picture = picture; | |
418 mpeg2dec->info.display_picture = picture; | |
419 if (type != PIC_FLAG_CODING_TYPE_B) { | |
420 if (!low_delay) { | |
421 if (mpeg2dec->first) { | |
422 mpeg2dec->info.display_picture = NULL; | |
423 mpeg2dec->first = 0; | |
424 } else { | |
425 mpeg2dec->info.display_picture = other; | |
426 if (other->nb_fields == 1) | |
427 mpeg2dec->info.display_picture_2nd = other + 1; | |
428 mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[1]; | |
429 } | |
430 } | |
431 if (!low_delay + !mpeg2dec->convert_start) | |
432 mpeg2dec->info.discard_fbuf = | |
433 mpeg2dec->fbuf[!low_delay + !mpeg2dec->convert_start]; | |
434 } | |
435 if (!mpeg2dec->custom_fbuf) { | |
436 while (mpeg2dec->alloc_index < 3) { | |
437 fbuf_t * fbuf; | |
438 | |
439 fbuf = &(mpeg2dec->fbuf_alloc[mpeg2dec->alloc_index++].fbuf); | |
440 fbuf->id = NULL; | |
441 if (mpeg2dec->convert_start) { | |
442 fbuf->buf[0] = | |
443 (uint8_t *) mpeg2_malloc (mpeg2dec->convert_size[0], | |
444 ALLOC_CONVERTED); | |
445 fbuf->buf[1] = fbuf->buf[0] + mpeg2dec->convert_size[1]; | |
446 fbuf->buf[2] = fbuf->buf[0] + mpeg2dec->convert_size[2]; | |
447 } else { | |
448 int size; | |
449 size = mpeg2dec->decoder.width * mpeg2dec->decoder.height; | |
450 fbuf->buf[0] = (uint8_t *) mpeg2_malloc (6 * size >> 2, | |
451 ALLOC_YUV); | |
452 fbuf->buf[1] = fbuf->buf[0] + size; | |
453 fbuf->buf[2] = fbuf->buf[1] + (size >> 2); | |
454 } | |
455 } | |
456 mpeg2_set_fbuf (mpeg2dec, type); | |
457 } | |
458 } else { | |
459 decoder->second_field = 1; | |
460 mpeg2dec->info.current_picture_2nd = picture; | |
461 mpeg2dec->info.user_data = NULL; mpeg2dec->info.user_data_len = 0; | |
462 if (low_delay || type == PIC_FLAG_CODING_TYPE_B) | |
463 mpeg2dec->info.display_picture_2nd = picture; | |
464 } | |
465 mpeg2dec->ext_state = PIC_CODING_EXT; | |
466 | |
467 picture->temporal_reference = (buffer[0] << 2) | (buffer[1] >> 6); | |
468 | |
469 decoder->coding_type = type; | |
470 picture->flags |= type; | |
471 | |
472 if (type == PIC_FLAG_CODING_TYPE_P || type == PIC_FLAG_CODING_TYPE_B) { | |
473 /* forward_f_code and backward_f_code - used in mpeg1 only */ | |
474 decoder->f_motion.f_code[1] = (buffer[3] >> 2) & 1; | |
475 decoder->f_motion.f_code[0] = | |
476 (((buffer[3] << 1) | (buffer[4] >> 7)) & 7) - 1; | |
477 decoder->b_motion.f_code[1] = (buffer[4] >> 6) & 1; | |
478 decoder->b_motion.f_code[0] = ((buffer[4] >> 3) & 7) - 1; | |
1 | 479 } |
480 | |
9852 | 481 /* XXXXXX decode extra_information_picture as well */ |
482 | |
483 picture->nb_fields = 2; | |
1 | 484 |
485 return 0; | |
486 } | |
487 | |
9852 | 488 static int picture_coding_ext (mpeg2dec_t * mpeg2dec) |
1 | 489 { |
9852 | 490 uint8_t * buffer = mpeg2dec->chunk_start; |
491 picture_t * picture = mpeg2dec->picture; | |
492 decoder_t * decoder = &(mpeg2dec->decoder); | |
493 uint32_t flags; | |
1 | 494 |
9852 | 495 /* pre subtract 1 for use later in compute_motion_vector */ |
496 decoder->f_motion.f_code[0] = (buffer[0] & 15) - 1; | |
497 decoder->f_motion.f_code[1] = (buffer[1] >> 4) - 1; | |
498 decoder->b_motion.f_code[0] = (buffer[1] & 15) - 1; | |
499 decoder->b_motion.f_code[1] = (buffer[2] >> 4) - 1; | |
1 | 500 |
9852 | 501 flags = picture->flags; |
502 decoder->intra_dc_precision = (buffer[2] >> 2) & 3; | |
503 decoder->picture_structure = buffer[2] & 3; | |
504 switch (decoder->picture_structure) { | |
505 case TOP_FIELD: | |
506 flags |= PIC_FLAG_TOP_FIELD_FIRST; | |
507 case BOTTOM_FIELD: | |
508 picture->nb_fields = 1; | |
509 break; | |
510 case FRAME_PICTURE: | |
511 if (!(mpeg2dec->sequence.flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE)) { | |
512 picture->nb_fields = (buffer[3] & 2) ? 3 : 2; | |
10509 | 513 flags |= (buffer[3] & 128) ? PIC_FLAG_TOP_FIELD_FIRST : 0; |
514 flags |= (buffer[3] & 2) ? PIC_FLAG_REPEAT_FIRST_FIELD : 0; | |
9852 | 515 } else |
516 picture->nb_fields = (buffer[3]&2) ? ((buffer[3]&128) ? 6 : 4) : 2; | |
517 break; | |
518 default: | |
519 return 1; | |
520 } | |
521 decoder->top_field_first = buffer[3] >> 7; | |
522 decoder->frame_pred_frame_dct = (buffer[3] >> 6) & 1; | |
523 decoder->concealment_motion_vectors = (buffer[3] >> 5) & 1; | |
524 decoder->q_scale_type = (buffer[3] >> 4) & 1; | |
525 decoder->intra_vlc_format = (buffer[3] >> 3) & 1; | |
526 decoder->scan = (buffer[3] & 4) ? mpeg2_scan_alt : mpeg2_scan_norm; | |
527 flags |= (buffer[4] & 0x80) ? PIC_FLAG_PROGRESSIVE_FRAME : 0; | |
528 if (buffer[4] & 0x40) | |
529 flags |= (((buffer[4]<<26) | (buffer[5]<<18) | (buffer[6]<<10)) & | |
530 PIC_MASK_COMPOSITE_DISPLAY) | PIC_FLAG_COMPOSITE_DISPLAY; | |
531 picture->flags = flags; | |
1 | 532 |
9852 | 533 mpeg2dec->ext_state = PIC_DISPLAY_EXT | COPYRIGHT_EXT | QUANT_MATRIX_EXT; |
534 | |
1 | 535 return 0; |
536 } | |
537 | |
9852 | 538 static int picture_display_ext (mpeg2dec_t * mpeg2dec) |
1 | 539 { |
9852 | 540 uint8_t * buffer = mpeg2dec->chunk_start; |
541 picture_t * picture = mpeg2dec->picture; | |
542 int i, nb_pos; | |
543 | |
544 nb_pos = picture->nb_fields; | |
545 if (mpeg2dec->sequence.flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE) | |
546 nb_pos >>= 1; | |
547 | |
548 for (i = 0; i < nb_pos; i++) { | |
549 int x, y; | |
1 | 550 |
9852 | 551 x = ((buffer[4*i] << 24) | (buffer[4*i+1] << 16) | |
552 (buffer[4*i+2] << 8) | buffer[4*i+3]) >> (11-2*i); | |
553 y = ((buffer[4*i+2] << 24) | (buffer[4*i+3] << 16) | | |
554 (buffer[4*i+4] << 8) | buffer[4*i+5]) >> (10-2*i); | |
555 if (! (x & y & 1)) | |
556 return 1; | |
557 picture->display_offset[i].x = mpeg2dec->display_offset_x = x >> 1; | |
558 picture->display_offset[i].y = mpeg2dec->display_offset_y = y >> 1; | |
559 } | |
560 for (; i < 3; i++) { | |
561 picture->display_offset[i].x = mpeg2dec->display_offset_x; | |
562 picture->display_offset[i].y = mpeg2dec->display_offset_y; | |
563 } | |
564 return 0; | |
565 } | |
1 | 566 |
9852 | 567 static int copyright_ext (mpeg2dec_t * mpeg2dec) |
568 { | |
569 return 0; | |
570 } | |
571 | |
572 static int quant_matrix_ext (mpeg2dec_t * mpeg2dec) | |
573 { | |
574 uint8_t * buffer = mpeg2dec->chunk_start; | |
575 decoder_t * decoder = &(mpeg2dec->decoder); | |
576 int i; | |
577 | |
578 if (buffer[0] & 8) { | |
579 for (i = 0; i < 64; i++) | |
580 decoder->intra_quantizer_matrix[mpeg2_scan_norm[i]] = | |
581 (buffer[i] << 5) | (buffer[i+1] >> 3); | |
582 buffer += 64; | |
1 | 583 } |
584 | |
9852 | 585 if (buffer[0] & 4) |
586 for (i = 0; i < 64; i++) | |
587 decoder->non_intra_quantizer_matrix[mpeg2_scan_norm[i]] = | |
588 (buffer[i] << 6) | (buffer[i+1] >> 2); | |
589 | |
1 | 590 return 0; |
591 } | |
592 | |
9852 | 593 int mpeg2_header_extension (mpeg2dec_t * mpeg2dec) |
1 | 594 { |
9852 | 595 static int (* parser[]) (mpeg2dec_t *) = { |
596 0, sequence_ext, sequence_display_ext, quant_matrix_ext, | |
597 copyright_ext, 0, 0, picture_display_ext, picture_coding_ext | |
598 }; | |
599 int ext, ext_bit; | |
600 | |
601 ext = mpeg2dec->chunk_start[0] >> 4; | |
602 ext_bit = 1 << ext; | |
1 | 603 |
9852 | 604 if (!(mpeg2dec->ext_state & ext_bit)) |
605 return 0; /* ignore illegal extensions */ | |
606 mpeg2dec->ext_state &= ~ext_bit; | |
607 return parser[ext] (mpeg2dec); | |
608 } | |
1 | 609 |
9852 | 610 int mpeg2_header_user_data (mpeg2dec_t * mpeg2dec) |
611 { | |
612 if (!mpeg2dec->info.user_data_len) | |
613 mpeg2dec->info.user_data = mpeg2dec->chunk_start; | |
614 else | |
615 mpeg2dec->info.user_data_len += 3; | |
616 mpeg2dec->info.user_data_len += (mpeg2dec->chunk_ptr - 4 - | |
617 mpeg2dec->chunk_start); | |
618 mpeg2dec->chunk_start = mpeg2dec->chunk_ptr - 1; | |
619 | |
1 | 620 return 0; |
621 } | |
9852 | 622 |
623 int mpeg2_header_slice_start (mpeg2dec_t * mpeg2dec) | |
624 { | |
625 mpeg2dec->state = ((mpeg2dec->picture->nb_fields > 1 || | |
626 mpeg2dec->state == STATE_PICTURE_2ND) ? | |
627 STATE_SLICE : STATE_SLICE_1ST); | |
628 | |
629 if (!(mpeg2dec->nb_decode_slices)) | |
630 mpeg2dec->picture->flags |= PIC_FLAG_SKIP; | |
631 else if (mpeg2dec->convert_start) { | |
632 int flags; | |
633 | |
634 switch (mpeg2dec->decoder.picture_structure) { | |
635 case TOP_FIELD: flags = CONVERT_TOP_FIELD; break; | |
636 case BOTTOM_FIELD: flags = CONVERT_BOTTOM_FIELD; break; | |
637 default: | |
638 flags = | |
639 ((mpeg2dec->sequence.flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE) ? | |
640 CONVERT_FRAME : CONVERT_BOTH_FIELDS); | |
641 } | |
642 mpeg2dec->convert_start (mpeg2dec->convert_id, | |
643 mpeg2dec->fbuf[0]->buf, flags); | |
644 | |
645 mpeg2dec->decoder.convert = mpeg2dec->convert_copy; | |
646 mpeg2dec->decoder.fbuf_id = mpeg2dec->convert_id; | |
647 | |
648 if (mpeg2dec->decoder.coding_type == B_TYPE) | |
649 mpeg2_init_fbuf (&(mpeg2dec->decoder), mpeg2dec->yuv_buf[2], | |
650 mpeg2dec->yuv_buf[mpeg2dec->yuv_index ^ 1], | |
651 mpeg2dec->yuv_buf[mpeg2dec->yuv_index]); | |
652 else { | |
653 mpeg2_init_fbuf (&(mpeg2dec->decoder), | |
654 mpeg2dec->yuv_buf[mpeg2dec->yuv_index ^ 1], | |
655 mpeg2dec->yuv_buf[mpeg2dec->yuv_index], | |
656 mpeg2dec->yuv_buf[mpeg2dec->yuv_index]); | |
657 if (mpeg2dec->state == STATE_SLICE) | |
658 mpeg2dec->yuv_index ^= 1; | |
659 } | |
660 } else { | |
661 int b_type; | |
662 | |
9858
9af61fc7955c
changed input and output buffer handling to be mplayer-compatible...
arpi
parents:
9852
diff
changeset
|
663 //mpeg2dec->decoder.convert = NULL; |
9852 | 664 b_type = (mpeg2dec->decoder.coding_type == B_TYPE); |
665 mpeg2_init_fbuf (&(mpeg2dec->decoder), mpeg2dec->fbuf[0]->buf, | |
666 mpeg2dec->fbuf[b_type + 1]->buf, | |
667 mpeg2dec->fbuf[b_type]->buf); | |
668 } | |
669 mpeg2dec->action = NULL; | |
670 return 0; | |
671 } | |
672 | |
673 int mpeg2_header_end (mpeg2dec_t * mpeg2dec) | |
674 { | |
675 picture_t * picture; | |
676 int b_type; | |
677 | |
678 picture = mpeg2dec->pictures; | |
679 if (mpeg2dec->picture < picture + 2) | |
680 picture = mpeg2dec->pictures + 2; | |
681 | |
682 mpeg2dec->state = STATE_INVALID; | |
683 reset_info (&(mpeg2dec->info)); | |
684 b_type = (mpeg2dec->decoder.coding_type == B_TYPE); | |
685 if (!(mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY)) { | |
686 mpeg2dec->info.display_picture = picture; | |
687 if (picture->nb_fields == 1) | |
688 mpeg2dec->info.display_picture_2nd = picture + 1; | |
689 mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[b_type]; | |
690 if (!mpeg2dec->convert_start) | |
691 mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[b_type + 1]; | |
692 } else if (!mpeg2dec->convert_start) | |
693 mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[b_type]; | |
694 mpeg2dec->action = mpeg2_seek_sequence; | |
695 return STATE_END; | |
696 } |