Mercurial > mplayer.hg
annotate libmpeg2/header.c @ 12615:84e078fd64b4
uniform VOBsub spelling
author | diego |
---|---|
date | Mon, 21 Jun 2004 12:47:41 +0000 |
parents | d9d24093db2e |
children | 55b9c352d019 |
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; | |
114 decoder->width = sequence->width = width = (width + 15) & ~15; | |
115 decoder->height = sequence->height = height = (height + 15) & ~15; | |
116 decoder->vertical_position_extension = (height > 2800); | |
117 sequence->chroma_width = width >> 1; | |
118 sequence->chroma_height = height >> 1; | |
1 | 119 |
9852 | 120 sequence->flags = SEQ_FLAG_PROGRESSIVE_SEQUENCE; |
121 | |
122 sequence->pixel_width = buffer[3] >> 4; /* aspect ratio */ | |
123 sequence->frame_period = 0; | |
124 if ((buffer[3] & 15) < 9) | |
125 sequence->frame_period = frame_period[buffer[3] & 15]; | |
36 | 126 |
9852 | 127 sequence->byte_rate = (buffer[4]<<10) | (buffer[5]<<2) | (buffer[6]>>6); |
128 | |
129 sequence->vbv_buffer_size = ((buffer[6]<<16)|(buffer[7]<<8))&0x1ff800; | |
130 | |
131 if (buffer[7] & 4) | |
132 sequence->flags |= SEQ_FLAG_CONSTRAINED_PARAMETERS; | |
1 | 133 |
134 if (buffer[7] & 2) { | |
135 for (i = 0; i < 64; i++) | |
9852 | 136 decoder->intra_quantizer_matrix[mpeg2_scan_norm[i]] = |
1 | 137 (buffer[i+7] << 7) | (buffer[i+8] >> 1); |
138 buffer += 64; | |
9852 | 139 } else |
140 for (i = 0; i < 64; i++) | |
141 decoder->intra_quantizer_matrix[mpeg2_scan_norm[i]] = | |
142 default_intra_quantizer_matrix [i]; | |
143 | |
144 if (buffer[7] & 1) | |
145 for (i = 0; i < 64; i++) | |
146 decoder->non_intra_quantizer_matrix[mpeg2_scan_norm[i]] = | |
147 buffer[i+8]; | |
148 else | |
1 | 149 for (i = 0; i < 64; i++) |
9852 | 150 decoder->non_intra_quantizer_matrix[i] = 16; |
151 | |
152 sequence->profile_level_id = 0x80; | |
153 sequence->colour_primaries = 1; | |
154 sequence->transfer_characteristics = 1; | |
155 sequence->matrix_coefficients = 1; | |
156 | |
157 decoder->mpeg1 = 1; | |
158 decoder->intra_dc_precision = 0; | |
159 decoder->frame_pred_frame_dct = 1; | |
160 decoder->q_scale_type = 0; | |
161 decoder->concealment_motion_vectors = 0; | |
162 decoder->scan = mpeg2_scan_norm; | |
163 decoder->picture_structure = FRAME_PICTURE; | |
164 | |
165 mpeg2dec->ext_state = SEQ_EXT; | |
166 mpeg2dec->state = STATE_SEQUENCE; | |
167 mpeg2dec->display_offset_x = mpeg2dec->display_offset_y = 0; | |
168 | |
169 reset_info (&(mpeg2dec->info)); | |
170 return 0; | |
171 } | |
172 | |
173 static int sequence_ext (mpeg2dec_t * mpeg2dec) | |
174 { | |
175 uint8_t * buffer = mpeg2dec->chunk_start; | |
176 sequence_t * sequence = &(mpeg2dec->new_sequence); | |
177 decoder_t * decoder = &(mpeg2dec->decoder); | |
178 int width, height; | |
179 uint32_t flags; | |
180 | |
181 if (!(buffer[3] & 1)) | |
182 return 1; | |
183 | |
184 sequence->profile_level_id = (buffer[0] << 4) | (buffer[1] >> 4); | |
185 | |
186 width = sequence->display_width = sequence->picture_width += | |
187 ((buffer[1] << 13) | (buffer[2] << 5)) & 0x3000; | |
188 height = sequence->display_height = sequence->picture_height += | |
189 (buffer[2] << 7) & 0x3000; | |
190 decoder->vertical_position_extension = (height > 2800); | |
191 flags = sequence->flags | SEQ_FLAG_MPEG2; | |
192 if (!(buffer[1] & 8)) { | |
193 flags &= ~SEQ_FLAG_PROGRESSIVE_SEQUENCE; | |
194 height = (height + 31) & ~31; | |
195 } | |
196 if (buffer[5] & 0x80) | |
197 flags |= SEQ_FLAG_LOW_DELAY; | |
198 sequence->flags = flags; | |
199 decoder->width = sequence->width = width = (width + 15) & ~15; | |
200 decoder->height = sequence->height = height = (height + 15) & ~15; | |
201 switch (buffer[1] & 6) { | |
202 case 0: /* invalid */ | |
203 return 1; | |
204 case 2: /* 4:2:0 */ | |
205 height >>= 1; | |
206 case 4: /* 4:2:2 */ | |
207 width >>= 1; | |
208 } | |
209 sequence->chroma_width = width; | |
210 sequence->chroma_height = height; | |
211 | |
212 sequence->byte_rate += ((buffer[2]<<25) | (buffer[3]<<17)) & 0x3ffc0000; | |
213 | |
214 sequence->vbv_buffer_size |= buffer[4] << 21; | |
215 | |
216 sequence->frame_period = | |
217 sequence->frame_period * ((buffer[5]&31)+1) / (((buffer[5]>>2)&3)+1); | |
218 | |
219 decoder->mpeg1 = 0; | |
220 | |
221 mpeg2dec->ext_state = SEQ_DISPLAY_EXT; | |
222 | |
223 return 0; | |
224 } | |
225 | |
226 static int sequence_display_ext (mpeg2dec_t * mpeg2dec) | |
227 { | |
228 uint8_t * buffer = mpeg2dec->chunk_start; | |
229 sequence_t * sequence = &(mpeg2dec->new_sequence); | |
230 uint32_t flags; | |
231 | |
232 flags = ((sequence->flags & ~SEQ_MASK_VIDEO_FORMAT) | | |
233 ((buffer[0]<<4) & SEQ_MASK_VIDEO_FORMAT)); | |
234 if (buffer[0] & 1) { | |
235 flags |= SEQ_FLAG_COLOUR_DESCRIPTION; | |
236 sequence->colour_primaries = buffer[1]; | |
237 sequence->transfer_characteristics = buffer[2]; | |
238 sequence->matrix_coefficients = buffer[3]; | |
239 buffer += 3; | |
1 | 240 } |
241 | |
9852 | 242 if (!(buffer[2] & 2)) /* missing marker_bit */ |
243 return 1; | |
1 | 244 |
9852 | 245 sequence->display_width = (buffer[1] << 6) | (buffer[2] >> 2); |
246 sequence->display_height = | |
247 ((buffer[2]& 1 ) << 13) | (buffer[3] << 5) | (buffer[4] >> 3); | |
1 | 248 |
249 return 0; | |
250 } | |
251 | |
9852 | 252 static inline void finalize_sequence (sequence_t * sequence) |
1 | 253 { |
9852 | 254 int width; |
255 int height; | |
256 | |
257 sequence->byte_rate *= 50; | |
258 | |
259 if (sequence->flags & SEQ_FLAG_MPEG2) { | |
260 switch (sequence->pixel_width) { | |
261 case 1: /* square pixels */ | |
262 sequence->pixel_width = sequence->pixel_height = 1; return; | |
263 case 2: /* 4:3 aspect ratio */ | |
264 width = 4; height = 3; break; | |
265 case 3: /* 16:9 aspect ratio */ | |
266 width = 16; height = 9; break; | |
267 case 4: /* 2.21:1 aspect ratio */ | |
268 width = 221; height = 100; break; | |
269 default: /* illegal */ | |
270 sequence->pixel_width = sequence->pixel_height = 0; return; | |
271 } | |
272 width *= sequence->display_height; | |
273 height *= sequence->display_width; | |
274 | |
275 } else { | |
276 if (sequence->byte_rate == 50 * 0x3ffff) | |
277 sequence->byte_rate = 0; /* mpeg-1 VBR */ | |
1 | 278 |
9852 | 279 switch (sequence->pixel_width) { |
280 case 0: case 15: /* illegal */ | |
281 sequence->pixel_width = sequence->pixel_height = 0; return; | |
282 case 1: /* square pixels */ | |
283 sequence->pixel_width = sequence->pixel_height = 1; return; | |
284 case 3: /* 720x576 16:9 */ | |
285 sequence->pixel_width = 64; sequence->pixel_height = 45; return; | |
286 case 6: /* 720x480 16:9 */ | |
287 sequence->pixel_width = 32; sequence->pixel_height = 27; return; | |
288 case 12: /* 720*480 4:3 */ | |
289 sequence->pixel_width = 8; sequence->pixel_height = 9; return; | |
290 default: | |
291 height = 88 * sequence->pixel_width + 1171; | |
292 width = 2000; | |
293 } | |
294 } | |
1 | 295 |
9852 | 296 sequence->pixel_width = width; |
297 sequence->pixel_height = height; | |
298 while (width) { /* find greatest common divisor */ | |
299 int tmp = width; | |
300 width = height % tmp; | |
301 height = tmp; | |
302 } | |
303 sequence->pixel_width /= height; | |
304 sequence->pixel_height /= height; | |
305 } | |
306 | |
307 void mpeg2_header_sequence_finalize (mpeg2dec_t * mpeg2dec) | |
308 { | |
309 sequence_t * sequence = &(mpeg2dec->new_sequence); | |
310 | |
311 finalize_sequence (sequence); | |
36 | 312 |
9852 | 313 /* |
314 * according to 6.1.1.6, repeat sequence headers should be | |
315 * identical to the original. However some DVDs dont respect that | |
316 * and have different bitrates in the repeat sequence headers. So | |
317 * we'll ignore that in the comparison and still consider these as | |
318 * repeat sequence headers. | |
319 */ | |
320 mpeg2dec->sequence.byte_rate = sequence->byte_rate; | |
321 if (!memcmp (&(mpeg2dec->sequence), sequence, sizeof (sequence_t))) | |
322 mpeg2dec->state = STATE_SEQUENCE_REPEATED; | |
323 mpeg2dec->sequence = *sequence; | |
1 | 324 |
9852 | 325 mpeg2dec->info.sequence = &(mpeg2dec->sequence); |
326 } | |
327 | |
328 int mpeg2_header_gop (mpeg2dec_t * mpeg2dec) | |
329 { | |
330 mpeg2dec->state = STATE_GOP; | |
331 reset_info (&(mpeg2dec->info)); | |
1 | 332 return 0; |
333 } | |
334 | |
9852 | 335 void mpeg2_set_fbuf (mpeg2dec_t * mpeg2dec, int coding_type) |
1 | 336 { |
337 int i; | |
338 | |
9852 | 339 for (i = 0; i < 3; i++) |
340 if (mpeg2dec->fbuf[1] != &mpeg2dec->fbuf_alloc[i].fbuf && | |
341 mpeg2dec->fbuf[2] != &mpeg2dec->fbuf_alloc[i].fbuf) { | |
342 mpeg2dec->fbuf[0] = &mpeg2dec->fbuf_alloc[i].fbuf; | |
343 mpeg2dec->info.current_fbuf = mpeg2dec->fbuf[0]; | |
344 if ((coding_type == B_TYPE) || | |
345 (mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY)) { | |
346 if ((coding_type == B_TYPE) || (mpeg2dec->convert_start)) | |
347 mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[0]; | |
348 mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[0]; | |
349 } | |
350 break; | |
351 } | |
352 } | |
353 | |
354 int mpeg2_header_picture_start (mpeg2dec_t * mpeg2dec) | |
355 { | |
356 decoder_t * decoder = &(mpeg2dec->decoder); | |
357 picture_t * picture; | |
358 | |
359 if (mpeg2dec->state != STATE_SLICE_1ST) { | |
360 mpeg2dec->state = STATE_PICTURE; | |
361 picture = mpeg2dec->pictures; | |
362 if ((decoder->coding_type != PIC_FLAG_CODING_TYPE_B) ^ | |
363 (mpeg2dec->picture >= mpeg2dec->pictures + 2)) | |
364 picture += 2; | |
365 } else { | |
366 mpeg2dec->state = STATE_PICTURE_2ND; | |
367 picture = mpeg2dec->picture + 1; /* second field picture */ | |
368 } | |
369 mpeg2dec->picture = picture; | |
370 picture->flags = 0; | |
371 if (mpeg2dec->num_pts) { | |
372 if (mpeg2dec->bytes_since_pts >= 4) { | |
373 mpeg2dec->num_pts = 0; | |
374 picture->pts = mpeg2dec->pts_current; | |
375 picture->flags = PIC_FLAG_PTS; | |
376 } else if (mpeg2dec->num_pts > 1) { | |
377 mpeg2dec->num_pts = 1; | |
378 picture->pts = mpeg2dec->pts_previous; | |
379 picture->flags = PIC_FLAG_PTS; | |
380 } | |
381 } | |
382 picture->display_offset[0].x = picture->display_offset[1].x = | |
383 picture->display_offset[2].x = mpeg2dec->display_offset_x; | |
384 picture->display_offset[0].y = picture->display_offset[1].y = | |
385 picture->display_offset[2].y = mpeg2dec->display_offset_y; | |
386 return mpeg2_parse_header (mpeg2dec); | |
387 } | |
388 | |
389 int mpeg2_header_picture (mpeg2dec_t * mpeg2dec) | |
390 { | |
391 uint8_t * buffer = mpeg2dec->chunk_start; | |
392 picture_t * picture = mpeg2dec->picture; | |
393 decoder_t * decoder = &(mpeg2dec->decoder); | |
394 int type; | |
395 int low_delay; | |
396 | |
397 type = (buffer [1] >> 3) & 7; | |
398 low_delay = mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY; | |
399 | |
400 if (mpeg2dec->state == STATE_PICTURE) { | |
401 picture_t * other; | |
402 | |
403 decoder->second_field = 0; | |
404 other = mpeg2dec->pictures; | |
405 if (other == picture) | |
406 other += 2; | |
407 if (decoder->coding_type != PIC_FLAG_CODING_TYPE_B) { | |
408 mpeg2dec->fbuf[2] = mpeg2dec->fbuf[1]; | |
409 mpeg2dec->fbuf[1] = mpeg2dec->fbuf[0]; | |
410 } | |
411 mpeg2dec->fbuf[0] = NULL; | |
412 reset_info (&(mpeg2dec->info)); | |
413 mpeg2dec->info.current_picture = picture; | |
414 mpeg2dec->info.display_picture = picture; | |
415 if (type != PIC_FLAG_CODING_TYPE_B) { | |
416 if (!low_delay) { | |
417 if (mpeg2dec->first) { | |
418 mpeg2dec->info.display_picture = NULL; | |
419 mpeg2dec->first = 0; | |
420 } else { | |
421 mpeg2dec->info.display_picture = other; | |
422 if (other->nb_fields == 1) | |
423 mpeg2dec->info.display_picture_2nd = other + 1; | |
424 mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[1]; | |
425 } | |
426 } | |
427 if (!low_delay + !mpeg2dec->convert_start) | |
428 mpeg2dec->info.discard_fbuf = | |
429 mpeg2dec->fbuf[!low_delay + !mpeg2dec->convert_start]; | |
430 } | |
431 if (!mpeg2dec->custom_fbuf) { | |
432 while (mpeg2dec->alloc_index < 3) { | |
433 fbuf_t * fbuf; | |
434 | |
435 fbuf = &(mpeg2dec->fbuf_alloc[mpeg2dec->alloc_index++].fbuf); | |
436 fbuf->id = NULL; | |
437 if (mpeg2dec->convert_start) { | |
438 fbuf->buf[0] = | |
439 (uint8_t *) mpeg2_malloc (mpeg2dec->convert_size[0], | |
440 ALLOC_CONVERTED); | |
441 fbuf->buf[1] = fbuf->buf[0] + mpeg2dec->convert_size[1]; | |
442 fbuf->buf[2] = fbuf->buf[0] + mpeg2dec->convert_size[2]; | |
443 } else { | |
444 int size; | |
445 size = mpeg2dec->decoder.width * mpeg2dec->decoder.height; | |
446 fbuf->buf[0] = (uint8_t *) mpeg2_malloc (6 * size >> 2, | |
447 ALLOC_YUV); | |
448 fbuf->buf[1] = fbuf->buf[0] + size; | |
449 fbuf->buf[2] = fbuf->buf[1] + (size >> 2); | |
450 } | |
451 } | |
452 mpeg2_set_fbuf (mpeg2dec, type); | |
453 } | |
454 } else { | |
455 decoder->second_field = 1; | |
456 mpeg2dec->info.current_picture_2nd = picture; | |
457 mpeg2dec->info.user_data = NULL; mpeg2dec->info.user_data_len = 0; | |
458 if (low_delay || type == PIC_FLAG_CODING_TYPE_B) | |
459 mpeg2dec->info.display_picture_2nd = picture; | |
460 } | |
461 mpeg2dec->ext_state = PIC_CODING_EXT; | |
462 | |
463 picture->temporal_reference = (buffer[0] << 2) | (buffer[1] >> 6); | |
464 | |
465 decoder->coding_type = type; | |
466 picture->flags |= type; | |
467 | |
468 if (type == PIC_FLAG_CODING_TYPE_P || type == PIC_FLAG_CODING_TYPE_B) { | |
469 /* forward_f_code and backward_f_code - used in mpeg1 only */ | |
470 decoder->f_motion.f_code[1] = (buffer[3] >> 2) & 1; | |
471 decoder->f_motion.f_code[0] = | |
472 (((buffer[3] << 1) | (buffer[4] >> 7)) & 7) - 1; | |
473 decoder->b_motion.f_code[1] = (buffer[4] >> 6) & 1; | |
474 decoder->b_motion.f_code[0] = ((buffer[4] >> 3) & 7) - 1; | |
1 | 475 } |
476 | |
9852 | 477 /* XXXXXX decode extra_information_picture as well */ |
478 | |
479 picture->nb_fields = 2; | |
1 | 480 |
481 return 0; | |
482 } | |
483 | |
9852 | 484 static int picture_coding_ext (mpeg2dec_t * mpeg2dec) |
1 | 485 { |
9852 | 486 uint8_t * buffer = mpeg2dec->chunk_start; |
487 picture_t * picture = mpeg2dec->picture; | |
488 decoder_t * decoder = &(mpeg2dec->decoder); | |
489 uint32_t flags; | |
1 | 490 |
9852 | 491 /* pre subtract 1 for use later in compute_motion_vector */ |
492 decoder->f_motion.f_code[0] = (buffer[0] & 15) - 1; | |
493 decoder->f_motion.f_code[1] = (buffer[1] >> 4) - 1; | |
494 decoder->b_motion.f_code[0] = (buffer[1] & 15) - 1; | |
495 decoder->b_motion.f_code[1] = (buffer[2] >> 4) - 1; | |
1 | 496 |
9852 | 497 flags = picture->flags; |
498 decoder->intra_dc_precision = (buffer[2] >> 2) & 3; | |
499 decoder->picture_structure = buffer[2] & 3; | |
500 switch (decoder->picture_structure) { | |
501 case TOP_FIELD: | |
502 flags |= PIC_FLAG_TOP_FIELD_FIRST; | |
503 case BOTTOM_FIELD: | |
504 picture->nb_fields = 1; | |
505 break; | |
506 case FRAME_PICTURE: | |
507 if (!(mpeg2dec->sequence.flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE)) { | |
508 picture->nb_fields = (buffer[3] & 2) ? 3 : 2; | |
10509 | 509 flags |= (buffer[3] & 128) ? PIC_FLAG_TOP_FIELD_FIRST : 0; |
510 flags |= (buffer[3] & 2) ? PIC_FLAG_REPEAT_FIRST_FIELD : 0; | |
9852 | 511 } else |
512 picture->nb_fields = (buffer[3]&2) ? ((buffer[3]&128) ? 6 : 4) : 2; | |
513 break; | |
514 default: | |
515 return 1; | |
516 } | |
517 decoder->top_field_first = buffer[3] >> 7; | |
518 decoder->frame_pred_frame_dct = (buffer[3] >> 6) & 1; | |
519 decoder->concealment_motion_vectors = (buffer[3] >> 5) & 1; | |
520 decoder->q_scale_type = (buffer[3] >> 4) & 1; | |
521 decoder->intra_vlc_format = (buffer[3] >> 3) & 1; | |
522 decoder->scan = (buffer[3] & 4) ? mpeg2_scan_alt : mpeg2_scan_norm; | |
523 flags |= (buffer[4] & 0x80) ? PIC_FLAG_PROGRESSIVE_FRAME : 0; | |
524 if (buffer[4] & 0x40) | |
525 flags |= (((buffer[4]<<26) | (buffer[5]<<18) | (buffer[6]<<10)) & | |
526 PIC_MASK_COMPOSITE_DISPLAY) | PIC_FLAG_COMPOSITE_DISPLAY; | |
527 picture->flags = flags; | |
1 | 528 |
9852 | 529 mpeg2dec->ext_state = PIC_DISPLAY_EXT | COPYRIGHT_EXT | QUANT_MATRIX_EXT; |
530 | |
1 | 531 return 0; |
532 } | |
533 | |
9852 | 534 static int picture_display_ext (mpeg2dec_t * mpeg2dec) |
1 | 535 { |
9852 | 536 uint8_t * buffer = mpeg2dec->chunk_start; |
537 picture_t * picture = mpeg2dec->picture; | |
538 int i, nb_pos; | |
539 | |
540 nb_pos = picture->nb_fields; | |
541 if (mpeg2dec->sequence.flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE) | |
542 nb_pos >>= 1; | |
543 | |
544 for (i = 0; i < nb_pos; i++) { | |
545 int x, y; | |
1 | 546 |
9852 | 547 x = ((buffer[4*i] << 24) | (buffer[4*i+1] << 16) | |
548 (buffer[4*i+2] << 8) | buffer[4*i+3]) >> (11-2*i); | |
549 y = ((buffer[4*i+2] << 24) | (buffer[4*i+3] << 16) | | |
550 (buffer[4*i+4] << 8) | buffer[4*i+5]) >> (10-2*i); | |
551 if (! (x & y & 1)) | |
552 return 1; | |
553 picture->display_offset[i].x = mpeg2dec->display_offset_x = x >> 1; | |
554 picture->display_offset[i].y = mpeg2dec->display_offset_y = y >> 1; | |
555 } | |
556 for (; i < 3; i++) { | |
557 picture->display_offset[i].x = mpeg2dec->display_offset_x; | |
558 picture->display_offset[i].y = mpeg2dec->display_offset_y; | |
559 } | |
560 return 0; | |
561 } | |
1 | 562 |
9852 | 563 static int copyright_ext (mpeg2dec_t * mpeg2dec) |
564 { | |
565 return 0; | |
566 } | |
567 | |
568 static int quant_matrix_ext (mpeg2dec_t * mpeg2dec) | |
569 { | |
570 uint8_t * buffer = mpeg2dec->chunk_start; | |
571 decoder_t * decoder = &(mpeg2dec->decoder); | |
572 int i; | |
573 | |
574 if (buffer[0] & 8) { | |
575 for (i = 0; i < 64; i++) | |
576 decoder->intra_quantizer_matrix[mpeg2_scan_norm[i]] = | |
577 (buffer[i] << 5) | (buffer[i+1] >> 3); | |
578 buffer += 64; | |
1 | 579 } |
580 | |
9852 | 581 if (buffer[0] & 4) |
582 for (i = 0; i < 64; i++) | |
583 decoder->non_intra_quantizer_matrix[mpeg2_scan_norm[i]] = | |
584 (buffer[i] << 6) | (buffer[i+1] >> 2); | |
585 | |
1 | 586 return 0; |
587 } | |
588 | |
9852 | 589 int mpeg2_header_extension (mpeg2dec_t * mpeg2dec) |
1 | 590 { |
9852 | 591 static int (* parser[]) (mpeg2dec_t *) = { |
592 0, sequence_ext, sequence_display_ext, quant_matrix_ext, | |
593 copyright_ext, 0, 0, picture_display_ext, picture_coding_ext | |
594 }; | |
595 int ext, ext_bit; | |
596 | |
597 ext = mpeg2dec->chunk_start[0] >> 4; | |
598 ext_bit = 1 << ext; | |
1 | 599 |
9852 | 600 if (!(mpeg2dec->ext_state & ext_bit)) |
601 return 0; /* ignore illegal extensions */ | |
602 mpeg2dec->ext_state &= ~ext_bit; | |
603 return parser[ext] (mpeg2dec); | |
604 } | |
1 | 605 |
9852 | 606 int mpeg2_header_user_data (mpeg2dec_t * mpeg2dec) |
607 { | |
608 if (!mpeg2dec->info.user_data_len) | |
609 mpeg2dec->info.user_data = mpeg2dec->chunk_start; | |
610 else | |
611 mpeg2dec->info.user_data_len += 3; | |
612 mpeg2dec->info.user_data_len += (mpeg2dec->chunk_ptr - 4 - | |
613 mpeg2dec->chunk_start); | |
614 mpeg2dec->chunk_start = mpeg2dec->chunk_ptr - 1; | |
615 | |
1 | 616 return 0; |
617 } | |
9852 | 618 |
619 int mpeg2_header_slice_start (mpeg2dec_t * mpeg2dec) | |
620 { | |
621 mpeg2dec->state = ((mpeg2dec->picture->nb_fields > 1 || | |
622 mpeg2dec->state == STATE_PICTURE_2ND) ? | |
623 STATE_SLICE : STATE_SLICE_1ST); | |
624 | |
625 if (!(mpeg2dec->nb_decode_slices)) | |
626 mpeg2dec->picture->flags |= PIC_FLAG_SKIP; | |
627 else if (mpeg2dec->convert_start) { | |
628 int flags; | |
629 | |
630 switch (mpeg2dec->decoder.picture_structure) { | |
631 case TOP_FIELD: flags = CONVERT_TOP_FIELD; break; | |
632 case BOTTOM_FIELD: flags = CONVERT_BOTTOM_FIELD; break; | |
633 default: | |
634 flags = | |
635 ((mpeg2dec->sequence.flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE) ? | |
636 CONVERT_FRAME : CONVERT_BOTH_FIELDS); | |
637 } | |
638 mpeg2dec->convert_start (mpeg2dec->convert_id, | |
639 mpeg2dec->fbuf[0]->buf, flags); | |
640 | |
641 mpeg2dec->decoder.convert = mpeg2dec->convert_copy; | |
642 mpeg2dec->decoder.fbuf_id = mpeg2dec->convert_id; | |
643 | |
644 if (mpeg2dec->decoder.coding_type == B_TYPE) | |
645 mpeg2_init_fbuf (&(mpeg2dec->decoder), mpeg2dec->yuv_buf[2], | |
646 mpeg2dec->yuv_buf[mpeg2dec->yuv_index ^ 1], | |
647 mpeg2dec->yuv_buf[mpeg2dec->yuv_index]); | |
648 else { | |
649 mpeg2_init_fbuf (&(mpeg2dec->decoder), | |
650 mpeg2dec->yuv_buf[mpeg2dec->yuv_index ^ 1], | |
651 mpeg2dec->yuv_buf[mpeg2dec->yuv_index], | |
652 mpeg2dec->yuv_buf[mpeg2dec->yuv_index]); | |
653 if (mpeg2dec->state == STATE_SLICE) | |
654 mpeg2dec->yuv_index ^= 1; | |
655 } | |
656 } else { | |
657 int b_type; | |
658 | |
9858
9af61fc7955c
changed input and output buffer handling to be mplayer-compatible...
arpi
parents:
9852
diff
changeset
|
659 //mpeg2dec->decoder.convert = NULL; |
9852 | 660 b_type = (mpeg2dec->decoder.coding_type == B_TYPE); |
661 mpeg2_init_fbuf (&(mpeg2dec->decoder), mpeg2dec->fbuf[0]->buf, | |
662 mpeg2dec->fbuf[b_type + 1]->buf, | |
663 mpeg2dec->fbuf[b_type]->buf); | |
664 } | |
665 mpeg2dec->action = NULL; | |
666 return 0; | |
667 } | |
668 | |
669 int mpeg2_header_end (mpeg2dec_t * mpeg2dec) | |
670 { | |
671 picture_t * picture; | |
672 int b_type; | |
673 | |
674 picture = mpeg2dec->pictures; | |
675 if (mpeg2dec->picture < picture + 2) | |
676 picture = mpeg2dec->pictures + 2; | |
677 | |
678 mpeg2dec->state = STATE_INVALID; | |
679 reset_info (&(mpeg2dec->info)); | |
680 b_type = (mpeg2dec->decoder.coding_type == B_TYPE); | |
681 if (!(mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY)) { | |
682 mpeg2dec->info.display_picture = picture; | |
683 if (picture->nb_fields == 1) | |
684 mpeg2dec->info.display_picture_2nd = picture + 1; | |
685 mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[b_type]; | |
686 if (!mpeg2dec->convert_start) | |
687 mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[b_type + 1]; | |
688 } else if (!mpeg2dec->convert_start) | |
689 mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[b_type]; | |
690 mpeg2dec->action = mpeg2_seek_sequence; | |
691 return STATE_END; | |
692 } |