typo fixes
[mplayer/greg.git] / libmpeg2 / header.c
blob8424b220a273526ced96d1d723ac8024a20a1e3c
1 /*
2 * header.c
3 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
4 * Copyright (C) 2003 Regis Duchesne <hpreg@zoy.org>
5 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
7 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
8 * See http://libmpeg2.sourceforge.net/ for updates.
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.
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.
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
24 * Modified for use with MPlayer, see libmpeg-0.4.0.diff for the exact changes.
25 * detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
26 * $Id$
29 #include "config.h"
31 #include <inttypes.h>
32 #include <stdlib.h> /* defines NULL */
33 #include <string.h> /* memcmp */
35 #include "mpeg2.h"
36 #include "attributes.h"
37 #include "mpeg2_internal.h"
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
46 /* default intra quant matrix, in zig-zag order */
47 static const uint8_t default_intra_quantizer_matrix[64] ATTR_ALIGN(16) = {
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,
65 uint8_t mpeg2_scan_norm[64] ATTR_ALIGN(16) = {
66 /* Zig-Zag scan pattern */
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
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
81 void mpeg2_header_state_init (mpeg2dec_t * mpeg2dec)
83 if (mpeg2dec->sequence.width != (unsigned)-1) {
84 int i;
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]);
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]);
100 if (mpeg2dec->decoder.convert_id)
101 mpeg2_free (mpeg2dec->decoder.convert_id);
103 mpeg2dec->decoder.coding_type = I_TYPE;
104 mpeg2dec->decoder.convert = NULL;
105 mpeg2dec->decoder.convert_id = NULL;
106 mpeg2dec->picture = mpeg2dec->pictures;
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));
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;
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;
124 void mpeg2_reset_info (mpeg2_info_t * info)
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;
131 static void info_user_data (mpeg2dec_t * mpeg2dec)
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;
139 int mpeg2_header_sequence (mpeg2dec_t * mpeg2dec)
141 uint8_t * buffer = mpeg2dec->chunk_start;
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
150 int i;
152 if ((buffer[6] & 0x20) != 0x20) /* missing marker_bit */
153 return 1;
155 i = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2];
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;
165 sequence->flags = (SEQ_FLAG_PROGRESSIVE_SEQUENCE |
166 SEQ_VIDEO_FORMAT_UNSPECIFIED);
168 sequence->pixel_width = buffer[3] >> 4; /* aspect ratio */
169 sequence->frame_period = frame_period[buffer[3] & 15];
171 sequence->byte_rate = (buffer[4]<<10) | (buffer[5]<<2) | (buffer[6]>>6);
173 sequence->vbv_buffer_size = ((buffer[6]<<16)|(buffer[7]<<8))&0x1ff800;
175 if (buffer[7] & 4)
176 sequence->flags |= SEQ_FLAG_CONSTRAINED_PARAMETERS;
178 mpeg2dec->copy_matrix = 3;
179 if (buffer[7] & 2) {
180 for (i = 0; i < 64; i++)
181 mpeg2dec->new_quantizer_matrix[0][mpeg2_scan_norm[i]] =
182 (buffer[i+7] << 7) | (buffer[i+8] >> 1);
183 buffer += 64;
184 } else
185 for (i = 0; i < 64; i++)
186 mpeg2dec->new_quantizer_matrix[0][mpeg2_scan_norm[i]] =
187 default_intra_quantizer_matrix[i];
189 if (buffer[7] & 1)
190 for (i = 0; i < 64; i++)
191 mpeg2dec->new_quantizer_matrix[1][mpeg2_scan_norm[i]] =
192 buffer[i+8];
193 else
194 memset (mpeg2dec->new_quantizer_matrix[1], 16, 64);
196 sequence->profile_level_id = 0x80;
197 sequence->colour_primaries = 0;
198 sequence->transfer_characteristics = 0;
199 sequence->matrix_coefficients = 0;
201 mpeg2dec->ext_state = SEQ_EXT;
202 mpeg2dec->state = STATE_SEQUENCE;
203 mpeg2dec->display_offset_x = mpeg2dec->display_offset_y = 0;
205 return 0;
208 static int sequence_ext (mpeg2dec_t * mpeg2dec)
210 uint8_t * buffer = mpeg2dec->chunk_start;
211 mpeg2_sequence_t * sequence = &(mpeg2dec->new_sequence);
212 uint32_t flags;
214 if (!(buffer[3] & 1))
215 return 1;
217 sequence->profile_level_id = (buffer[0] << 4) | (buffer[1] >> 4);
219 sequence->display_width = sequence->picture_width +=
220 ((buffer[1] << 13) | (buffer[2] << 5)) & 0x3000;
221 sequence->display_height = sequence->picture_height +=
222 (buffer[2] << 7) & 0x3000;
223 sequence->width = (sequence->picture_width + 15) & ~15;
224 sequence->height = (sequence->picture_height + 15) & ~15;
225 flags = sequence->flags | SEQ_FLAG_MPEG2;
226 if (!(buffer[1] & 8)) {
227 flags &= ~SEQ_FLAG_PROGRESSIVE_SEQUENCE;
228 sequence->height = (sequence->height + 31) & ~31;
230 if (buffer[5] & 0x80)
231 flags |= SEQ_FLAG_LOW_DELAY;
232 sequence->flags = flags;
233 sequence->chroma_width = sequence->width;
234 sequence->chroma_height = sequence->height;
235 switch (buffer[1] & 6) {
236 case 0: /* invalid */
237 return 1;
238 case 2: /* 4:2:0 */
239 sequence->chroma_height >>= 1;
240 case 4: /* 4:2:2 */
241 sequence->chroma_width >>= 1;
244 sequence->byte_rate += ((buffer[2]<<25) | (buffer[3]<<17)) & 0x3ffc0000;
246 sequence->vbv_buffer_size |= buffer[4] << 21;
248 sequence->frame_period =
249 sequence->frame_period * ((buffer[5]&31)+1) / (((buffer[5]>>2)&3)+1);
251 mpeg2dec->ext_state = SEQ_DISPLAY_EXT;
253 return 0;
256 static int sequence_display_ext (mpeg2dec_t * mpeg2dec)
258 uint8_t * buffer = mpeg2dec->chunk_start;
259 mpeg2_sequence_t * sequence = &(mpeg2dec->new_sequence);
260 uint32_t flags;
262 flags = ((sequence->flags & ~SEQ_MASK_VIDEO_FORMAT) |
263 ((buffer[0]<<4) & SEQ_MASK_VIDEO_FORMAT));
264 if (buffer[0] & 1) {
265 flags |= SEQ_FLAG_COLOUR_DESCRIPTION;
266 sequence->colour_primaries = buffer[1];
267 sequence->transfer_characteristics = buffer[2];
268 sequence->matrix_coefficients = buffer[3];
269 buffer += 3;
272 if (!(buffer[2] & 2)) /* missing marker_bit */
273 return 1;
275 sequence->display_width = (buffer[1] << 6) | (buffer[2] >> 2);
276 sequence->display_height =
277 ((buffer[2]& 1 ) << 13) | (buffer[3] << 5) | (buffer[4] >> 3);
279 return 0;
282 static inline void finalize_sequence (mpeg2_sequence_t * sequence)
284 int width;
285 int height;
287 sequence->byte_rate *= 50;
289 if (sequence->flags & SEQ_FLAG_MPEG2) {
290 switch (sequence->pixel_width) {
291 case 1: /* square pixels */
292 sequence->pixel_width = sequence->pixel_height = 1; return;
293 case 2: /* 4:3 aspect ratio */
294 width = 4; height = 3; break;
295 case 3: /* 16:9 aspect ratio */
296 width = 16; height = 9; break;
297 case 4: /* 2.21:1 aspect ratio */
298 width = 221; height = 100; break;
299 default: /* illegal */
300 sequence->pixel_width = sequence->pixel_height = 0; return;
302 width *= sequence->display_height;
303 height *= sequence->display_width;
305 } else {
306 if (sequence->byte_rate == 50 * 0x3ffff)
307 sequence->byte_rate = 0; /* mpeg-1 VBR */
309 switch (sequence->pixel_width) {
310 case 0: case 15: /* illegal */
311 sequence->pixel_width = sequence->pixel_height = 0; return;
312 case 1: /* square pixels */
313 sequence->pixel_width = sequence->pixel_height = 1; return;
314 case 3: /* 720x576 16:9 */
315 sequence->pixel_width = 64; sequence->pixel_height = 45; return;
316 case 6: /* 720x480 16:9 */
317 sequence->pixel_width = 32; sequence->pixel_height = 27; return;
318 case 12: /* 720*480 4:3 */
319 sequence->pixel_width = 8; sequence->pixel_height = 9; return;
320 default:
321 height = 88 * sequence->pixel_width + 1171;
322 width = 2000;
326 sequence->pixel_width = width;
327 sequence->pixel_height = height;
328 while (width) { /* find greatest common divisor */
329 int tmp = width;
330 width = height % tmp;
331 height = tmp;
333 sequence->pixel_width /= height;
334 sequence->pixel_height /= height;
337 static void copy_matrix (mpeg2dec_t * mpeg2dec, int index)
339 if (memcmp (mpeg2dec->quantizer_matrix[index],
340 mpeg2dec->new_quantizer_matrix[index], 64)) {
341 memcpy (mpeg2dec->quantizer_matrix[index],
342 mpeg2dec->new_quantizer_matrix[index], 64);
343 mpeg2dec->scaled[index] = -1;
347 static void finalize_matrix (mpeg2dec_t * mpeg2dec)
349 mpeg2_decoder_t * decoder = &(mpeg2dec->decoder);
350 int i;
352 for (i = 0; i < 2; i++) {
353 if (mpeg2dec->copy_matrix & (1 << i))
354 copy_matrix (mpeg2dec, i);
355 if ((mpeg2dec->copy_matrix & (4 << i)) &&
356 memcmp (mpeg2dec->quantizer_matrix[i],
357 mpeg2dec->new_quantizer_matrix[i+2], 64)) {
358 copy_matrix (mpeg2dec, i + 2);
359 decoder->chroma_quantizer[i] = decoder->quantizer_prescale[i+2];
360 } else if (mpeg2dec->copy_matrix & (5 << i))
361 decoder->chroma_quantizer[i] = decoder->quantizer_prescale[i];
365 static mpeg2_state_t invalid_end_action (mpeg2dec_t * mpeg2dec)
367 mpeg2_reset_info (&(mpeg2dec->info));
368 mpeg2dec->info.gop = NULL;
369 info_user_data (mpeg2dec);
370 mpeg2_header_state_init (mpeg2dec);
371 mpeg2dec->sequence = mpeg2dec->new_sequence;
372 mpeg2dec->action = mpeg2_seek_header;
373 mpeg2dec->state = STATE_SEQUENCE;
374 return STATE_SEQUENCE;
377 void mpeg2_header_sequence_finalize (mpeg2dec_t * mpeg2dec)
379 mpeg2_sequence_t * sequence = &(mpeg2dec->new_sequence);
380 mpeg2_decoder_t * decoder = &(mpeg2dec->decoder);
382 finalize_sequence (sequence);
383 finalize_matrix (mpeg2dec);
385 decoder->mpeg1 = !(sequence->flags & SEQ_FLAG_MPEG2);
386 decoder->width = sequence->width;
387 decoder->height = sequence->height;
388 decoder->vertical_position_extension = (sequence->picture_height > 2800);
389 decoder->chroma_format = ((sequence->chroma_width == sequence->width) +
390 (sequence->chroma_height == sequence->height));
392 if (mpeg2dec->sequence.width != (unsigned)-1) {
393 unsigned int new_byte_rate;
396 * According to 6.1.1.6, repeat sequence headers should be
397 * identical to the original. However some DVDs dont respect
398 * that and have different bitrates in the repeat sequence
399 * headers. So we'll ignore that in the comparison and still
400 * consider these as repeat sequence headers.
402 * However, be careful not to alter the current sequence when
403 * returning STATE_INVALID_END.
405 new_byte_rate = sequence->byte_rate;
406 sequence->byte_rate = mpeg2dec->sequence.byte_rate;
407 if (memcmp (&(mpeg2dec->sequence), sequence,
408 sizeof (mpeg2_sequence_t))) {
409 decoder->stride_frame = sequence->width;
410 sequence->byte_rate = new_byte_rate;
411 mpeg2_header_end (mpeg2dec);
412 mpeg2dec->action = invalid_end_action;
413 mpeg2dec->state = STATE_INVALID_END;
414 return;
416 sequence->byte_rate = new_byte_rate;
417 mpeg2dec->state = STATE_SEQUENCE_REPEATED;
418 } else
419 decoder->stride_frame = sequence->width;
420 mpeg2dec->sequence = *sequence;
421 mpeg2_reset_info (&(mpeg2dec->info));
422 mpeg2dec->info.sequence = &(mpeg2dec->sequence);
423 mpeg2dec->info.gop = NULL;
424 info_user_data (mpeg2dec);
427 int mpeg2_header_gop (mpeg2dec_t * mpeg2dec)
429 uint8_t * buffer = mpeg2dec->chunk_start;
430 mpeg2_gop_t * gop = &(mpeg2dec->new_gop);
432 if (! (buffer[1] & 8))
433 return 1;
434 gop->hours = (buffer[0] >> 2) & 31;
435 gop->minutes = ((buffer[0] << 4) | (buffer[1] >> 4)) & 63;
436 gop->seconds = ((buffer[1] << 3) | (buffer[2] >> 5)) & 63;
437 gop->pictures = ((buffer[2] << 1) | (buffer[3] >> 7)) & 63;
438 gop->flags = (buffer[0] >> 7) | ((buffer[3] >> 4) & 6);
439 mpeg2dec->state = STATE_GOP;
440 return 0;
443 void mpeg2_header_gop_finalize (mpeg2dec_t * mpeg2dec)
445 mpeg2dec->gop = mpeg2dec->new_gop;
446 mpeg2_reset_info (&(mpeg2dec->info));
447 mpeg2dec->info.gop = &(mpeg2dec->gop);
448 info_user_data (mpeg2dec);
451 void mpeg2_set_fbuf (mpeg2dec_t * mpeg2dec, int b_type)
453 int i;
455 for (i = 0; i < 3; i++)
456 if (mpeg2dec->fbuf[1] != &mpeg2dec->fbuf_alloc[i].fbuf &&
457 mpeg2dec->fbuf[2] != &mpeg2dec->fbuf_alloc[i].fbuf) {
458 mpeg2dec->fbuf[0] = &mpeg2dec->fbuf_alloc[i].fbuf;
459 mpeg2dec->info.current_fbuf = mpeg2dec->fbuf[0];
460 if (b_type || (mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY)) {
461 if (b_type || mpeg2dec->convert)
462 mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[0];
463 mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[0];
465 break;
469 mpeg2_state_t mpeg2_header_picture_start (mpeg2dec_t * mpeg2dec)
471 mpeg2_picture_t * picture = &(mpeg2dec->new_picture);
473 mpeg2dec->state = ((mpeg2dec->state != STATE_SLICE_1ST) ?
474 STATE_PICTURE : STATE_PICTURE_2ND);
475 picture->flags = 0;
476 picture->tag = picture->tag2 = 0;
477 if (mpeg2dec->num_tags) {
478 if (mpeg2dec->bytes_since_tag >= 4) {
479 mpeg2dec->num_tags = 0;
480 picture->tag = mpeg2dec->tag_current;
481 picture->tag2 = mpeg2dec->tag2_current;
482 picture->flags = PIC_FLAG_TAGS;
483 } else if (mpeg2dec->num_tags > 1) {
484 mpeg2dec->num_tags = 1;
485 picture->tag = mpeg2dec->tag_previous;
486 picture->tag2 = mpeg2dec->tag2_previous;
487 picture->flags = PIC_FLAG_TAGS;
490 picture->display_offset[0].x = picture->display_offset[1].x =
491 picture->display_offset[2].x = mpeg2dec->display_offset_x;
492 picture->display_offset[0].y = picture->display_offset[1].y =
493 picture->display_offset[2].y = mpeg2dec->display_offset_y;
494 return mpeg2_parse_header (mpeg2dec);
497 int mpeg2_header_picture (mpeg2dec_t * mpeg2dec)
499 uint8_t * buffer = mpeg2dec->chunk_start;
500 mpeg2_picture_t * picture = &(mpeg2dec->new_picture);
501 mpeg2_decoder_t * decoder = &(mpeg2dec->decoder);
502 int type;
504 type = (buffer [1] >> 3) & 7;
505 mpeg2dec->ext_state = PIC_CODING_EXT;
507 picture->temporal_reference = (buffer[0] << 2) | (buffer[1] >> 6);
509 picture->flags |= type;
511 if (type == PIC_FLAG_CODING_TYPE_P || type == PIC_FLAG_CODING_TYPE_B) {
512 /* forward_f_code and backward_f_code - used in mpeg1 only */
513 decoder->f_motion.f_code[1] = (buffer[3] >> 2) & 1;
514 decoder->f_motion.f_code[0] =
515 (((buffer[3] << 1) | (buffer[4] >> 7)) & 7) - 1;
516 decoder->b_motion.f_code[1] = (buffer[4] >> 6) & 1;
517 decoder->b_motion.f_code[0] = ((buffer[4] >> 3) & 7) - 1;
520 /* XXXXXX decode extra_information_picture as well */
522 picture->nb_fields = 2;
524 mpeg2dec->q_scale_type = 0;
525 decoder->intra_dc_precision = 7;
526 decoder->frame_pred_frame_dct = 1;
527 decoder->concealment_motion_vectors = 0;
528 decoder->scan = mpeg2_scan_norm;
529 decoder->picture_structure = FRAME_PICTURE;
530 mpeg2dec->copy_matrix = 0;
532 return 0;
535 static int picture_coding_ext (mpeg2dec_t * mpeg2dec)
537 uint8_t * buffer = mpeg2dec->chunk_start;
538 mpeg2_picture_t * picture = &(mpeg2dec->new_picture);
539 mpeg2_decoder_t * decoder = &(mpeg2dec->decoder);
540 uint32_t flags;
542 /* pre subtract 1 for use later in compute_motion_vector */
543 decoder->f_motion.f_code[0] = (buffer[0] & 15) - 1;
544 decoder->f_motion.f_code[1] = (buffer[1] >> 4) - 1;
545 decoder->b_motion.f_code[0] = (buffer[1] & 15) - 1;
546 decoder->b_motion.f_code[1] = (buffer[2] >> 4) - 1;
548 flags = picture->flags;
549 decoder->intra_dc_precision = 7 - ((buffer[2] >> 2) & 3);
550 decoder->picture_structure = buffer[2] & 3;
551 switch (decoder->picture_structure) {
552 case TOP_FIELD:
553 flags |= PIC_FLAG_TOP_FIELD_FIRST;
554 case BOTTOM_FIELD:
555 picture->nb_fields = 1;
556 break;
557 case FRAME_PICTURE:
558 if (!(mpeg2dec->sequence.flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE)) {
559 picture->nb_fields = (buffer[3] & 2) ? 3 : 2;
560 flags |= (buffer[3] & 128) ? PIC_FLAG_TOP_FIELD_FIRST : 0;
561 flags |= (buffer[3] & 2) ? PIC_FLAG_REPEAT_FIRST_FIELD : 0;
562 } else
563 picture->nb_fields = (buffer[3]&2) ? ((buffer[3]&128) ? 6 : 4) : 2;
564 break;
565 default:
566 return 1;
568 decoder->top_field_first = buffer[3] >> 7;
569 decoder->frame_pred_frame_dct = (buffer[3] >> 6) & 1;
570 decoder->concealment_motion_vectors = (buffer[3] >> 5) & 1;
571 mpeg2dec->q_scale_type = buffer[3] & 16;
572 decoder->intra_vlc_format = (buffer[3] >> 3) & 1;
573 decoder->scan = (buffer[3] & 4) ? mpeg2_scan_alt : mpeg2_scan_norm;
574 flags |= (buffer[4] & 0x80) ? PIC_FLAG_PROGRESSIVE_FRAME : 0;
575 if (buffer[4] & 0x40)
576 flags |= (((buffer[4]<<26) | (buffer[5]<<18) | (buffer[6]<<10)) &
577 PIC_MASK_COMPOSITE_DISPLAY) | PIC_FLAG_COMPOSITE_DISPLAY;
578 picture->flags = flags;
580 mpeg2dec->ext_state = PIC_DISPLAY_EXT | COPYRIGHT_EXT | QUANT_MATRIX_EXT;
582 return 0;
585 static int picture_display_ext (mpeg2dec_t * mpeg2dec)
587 uint8_t * buffer = mpeg2dec->chunk_start;
588 mpeg2_picture_t * picture = &(mpeg2dec->new_picture);
589 int i, nb_pos;
591 nb_pos = picture->nb_fields;
592 if (mpeg2dec->sequence.flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE)
593 nb_pos >>= 1;
595 for (i = 0; i < nb_pos; i++) {
596 int x, y;
598 x = ((buffer[4*i] << 24) | (buffer[4*i+1] << 16) |
599 (buffer[4*i+2] << 8) | buffer[4*i+3]) >> (11-2*i);
600 y = ((buffer[4*i+2] << 24) | (buffer[4*i+3] << 16) |
601 (buffer[4*i+4] << 8) | buffer[4*i+5]) >> (10-2*i);
602 if (! (x & y & 1))
603 return 1;
604 picture->display_offset[i].x = mpeg2dec->display_offset_x = x >> 1;
605 picture->display_offset[i].y = mpeg2dec->display_offset_y = y >> 1;
607 for (; i < 3; i++) {
608 picture->display_offset[i].x = mpeg2dec->display_offset_x;
609 picture->display_offset[i].y = mpeg2dec->display_offset_y;
611 return 0;
614 void mpeg2_header_picture_finalize (mpeg2dec_t * mpeg2dec, uint32_t accels)
616 mpeg2_decoder_t * decoder = &(mpeg2dec->decoder);
617 int old_type_b = (decoder->coding_type == B_TYPE);
618 int low_delay = mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY;
620 finalize_matrix (mpeg2dec);
621 decoder->coding_type = mpeg2dec->new_picture.flags & PIC_MASK_CODING_TYPE;
623 if (mpeg2dec->state == STATE_PICTURE) {
624 mpeg2_picture_t * picture;
625 mpeg2_picture_t * other;
627 decoder->second_field = 0;
629 picture = other = mpeg2dec->pictures;
630 if (old_type_b ^ (mpeg2dec->picture < mpeg2dec->pictures + 2))
631 picture += 2;
632 else
633 other += 2;
634 mpeg2dec->picture = picture;
635 *picture = mpeg2dec->new_picture;
637 if (!old_type_b) {
638 mpeg2dec->fbuf[2] = mpeg2dec->fbuf[1];
639 mpeg2dec->fbuf[1] = mpeg2dec->fbuf[0];
641 mpeg2dec->fbuf[0] = NULL;
642 mpeg2_reset_info (&(mpeg2dec->info));
643 mpeg2dec->info.current_picture = picture;
644 mpeg2dec->info.display_picture = picture;
645 if (decoder->coding_type != B_TYPE) {
646 if (!low_delay) {
647 if (mpeg2dec->first) {
648 mpeg2dec->info.display_picture = NULL;
649 mpeg2dec->first = 0;
650 } else {
651 mpeg2dec->info.display_picture = other;
652 if (other->nb_fields == 1)
653 mpeg2dec->info.display_picture_2nd = other + 1;
654 mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[1];
657 if (!low_delay + !mpeg2dec->convert)
658 mpeg2dec->info.discard_fbuf =
659 mpeg2dec->fbuf[!low_delay + !mpeg2dec->convert];
661 if (mpeg2dec->convert) {
662 mpeg2_convert_init_t convert_init;
663 if (!mpeg2dec->convert_start) {
664 int y_size, uv_size;
666 mpeg2dec->decoder.convert_id =
667 mpeg2_malloc (mpeg2dec->convert_id_size,
668 MPEG2_ALLOC_CONVERT_ID);
669 mpeg2dec->convert (MPEG2_CONVERT_START,
670 mpeg2dec->decoder.convert_id,
671 &(mpeg2dec->sequence),
672 mpeg2dec->convert_stride, accels,
673 mpeg2dec->convert_arg, &convert_init);
674 mpeg2dec->convert_start = convert_init.start;
675 mpeg2dec->decoder.convert = convert_init.copy;
677 y_size = decoder->stride_frame * mpeg2dec->sequence.height;
678 uv_size = y_size >> (2 - mpeg2dec->decoder.chroma_format);
679 mpeg2dec->yuv_buf[0][0] =
680 (uint8_t *) mpeg2_malloc (y_size, MPEG2_ALLOC_YUV);
681 mpeg2dec->yuv_buf[0][1] =
682 (uint8_t *) mpeg2_malloc (uv_size, MPEG2_ALLOC_YUV);
683 mpeg2dec->yuv_buf[0][2] =
684 (uint8_t *) mpeg2_malloc (uv_size, MPEG2_ALLOC_YUV);
685 mpeg2dec->yuv_buf[1][0] =
686 (uint8_t *) mpeg2_malloc (y_size, MPEG2_ALLOC_YUV);
687 mpeg2dec->yuv_buf[1][1] =
688 (uint8_t *) mpeg2_malloc (uv_size, MPEG2_ALLOC_YUV);
689 mpeg2dec->yuv_buf[1][2] =
690 (uint8_t *) mpeg2_malloc (uv_size, MPEG2_ALLOC_YUV);
691 y_size = decoder->stride_frame * 32;
692 uv_size = y_size >> (2 - mpeg2dec->decoder.chroma_format);
693 mpeg2dec->yuv_buf[2][0] =
694 (uint8_t *) mpeg2_malloc (y_size, MPEG2_ALLOC_YUV);
695 mpeg2dec->yuv_buf[2][1] =
696 (uint8_t *) mpeg2_malloc (uv_size, MPEG2_ALLOC_YUV);
697 mpeg2dec->yuv_buf[2][2] =
698 (uint8_t *) mpeg2_malloc (uv_size, MPEG2_ALLOC_YUV);
700 if (!mpeg2dec->custom_fbuf) {
701 while (mpeg2dec->alloc_index < 3) {
702 mpeg2_fbuf_t * fbuf;
704 fbuf = &mpeg2dec->fbuf_alloc[mpeg2dec->alloc_index++].fbuf;
705 fbuf->id = NULL;
706 fbuf->buf[0] =
707 (uint8_t *) mpeg2_malloc (convert_init.buf_size[0],
708 MPEG2_ALLOC_CONVERTED);
709 fbuf->buf[1] =
710 (uint8_t *) mpeg2_malloc (convert_init.buf_size[1],
711 MPEG2_ALLOC_CONVERTED);
712 fbuf->buf[2] =
713 (uint8_t *) mpeg2_malloc (convert_init.buf_size[2],
714 MPEG2_ALLOC_CONVERTED);
716 mpeg2_set_fbuf (mpeg2dec, (decoder->coding_type == B_TYPE));
718 } else if (!mpeg2dec->custom_fbuf) {
719 while (mpeg2dec->alloc_index < 3) {
720 mpeg2_fbuf_t * fbuf;
721 int y_size, uv_size;
723 fbuf = &(mpeg2dec->fbuf_alloc[mpeg2dec->alloc_index++].fbuf);
724 fbuf->id = NULL;
725 y_size = decoder->stride_frame * mpeg2dec->sequence.height;
726 uv_size = y_size >> (2 - decoder->chroma_format);
727 fbuf->buf[0] = (uint8_t *) mpeg2_malloc (y_size,
728 MPEG2_ALLOC_YUV);
729 fbuf->buf[1] = (uint8_t *) mpeg2_malloc (uv_size,
730 MPEG2_ALLOC_YUV);
731 fbuf->buf[2] = (uint8_t *) mpeg2_malloc (uv_size,
732 MPEG2_ALLOC_YUV);
734 mpeg2_set_fbuf (mpeg2dec, (decoder->coding_type == B_TYPE));
736 } else {
737 decoder->second_field = 1;
738 mpeg2dec->picture++; /* second field picture */
739 *(mpeg2dec->picture) = mpeg2dec->new_picture;
740 mpeg2dec->info.current_picture_2nd = mpeg2dec->picture;
741 if (low_delay || decoder->coding_type == B_TYPE)
742 mpeg2dec->info.display_picture_2nd = mpeg2dec->picture;
745 info_user_data (mpeg2dec);
748 static int copyright_ext (mpeg2dec_t * mpeg2dec)
750 return 0;
753 static int quant_matrix_ext (mpeg2dec_t * mpeg2dec)
755 uint8_t * buffer = mpeg2dec->chunk_start;
756 int i, j;
758 for (i = 0; i < 4; i++)
759 if (buffer[0] & (8 >> i)) {
760 for (j = 0; j < 64; j++)
761 mpeg2dec->new_quantizer_matrix[i][mpeg2_scan_norm[j]] =
762 (buffer[j] << (i+5)) | (buffer[j+1] >> (3-i));
763 mpeg2dec->copy_matrix |= 1 << i;
764 buffer += 64;
767 return 0;
770 int mpeg2_header_extension (mpeg2dec_t * mpeg2dec)
772 static int (* parser[]) (mpeg2dec_t *) = {
773 0, sequence_ext, sequence_display_ext, quant_matrix_ext,
774 copyright_ext, 0, 0, picture_display_ext, picture_coding_ext
776 int ext, ext_bit;
778 ext = mpeg2dec->chunk_start[0] >> 4;
779 ext_bit = 1 << ext;
781 if (!(mpeg2dec->ext_state & ext_bit))
782 return 0; /* ignore illegal extensions */
783 mpeg2dec->ext_state &= ~ext_bit;
784 return parser[ext] (mpeg2dec);
787 int mpeg2_header_user_data (mpeg2dec_t * mpeg2dec)
789 mpeg2dec->user_data_len += mpeg2dec->chunk_ptr - 1 - mpeg2dec->chunk_start;
790 mpeg2dec->chunk_start = mpeg2dec->chunk_ptr - 1;
792 return 0;
795 static void prescale (mpeg2dec_t * mpeg2dec, int index)
797 static int non_linear_scale [] = {
798 0, 1, 2, 3, 4, 5, 6, 7,
799 8, 10, 12, 14, 16, 18, 20, 22,
800 24, 28, 32, 36, 40, 44, 48, 52,
801 56, 64, 72, 80, 88, 96, 104, 112
803 int i, j, k;
804 mpeg2_decoder_t * decoder = &(mpeg2dec->decoder);
806 if (mpeg2dec->scaled[index] != mpeg2dec->q_scale_type) {
807 mpeg2dec->scaled[index] = mpeg2dec->q_scale_type;
808 for (i = 0; i < 32; i++) {
809 k = mpeg2dec->q_scale_type ? non_linear_scale[i] : (i << 1);
810 decoder->quantizer_scales[i] = k;
811 for (j = 0; j < 64; j++)
812 decoder->quantizer_prescale[index][i][j] =
813 k * mpeg2dec->quantizer_matrix[index][j];
818 mpeg2_state_t mpeg2_header_slice_start (mpeg2dec_t * mpeg2dec)
820 mpeg2_decoder_t * decoder = &(mpeg2dec->decoder);
822 mpeg2dec->info.user_data = NULL; mpeg2dec->info.user_data_len = 0;
823 mpeg2dec->state = ((mpeg2dec->picture->nb_fields > 1 ||
824 mpeg2dec->state == STATE_PICTURE_2ND) ?
825 STATE_SLICE : STATE_SLICE_1ST);
827 if (mpeg2dec->decoder.coding_type != D_TYPE) {
828 prescale (mpeg2dec, 0);
829 if (decoder->chroma_quantizer[0] == decoder->quantizer_prescale[2])
830 prescale (mpeg2dec, 2);
831 if (mpeg2dec->decoder.coding_type != I_TYPE) {
832 prescale (mpeg2dec, 1);
833 if (decoder->chroma_quantizer[1] == decoder->quantizer_prescale[3])
834 prescale (mpeg2dec, 3);
838 if (!(mpeg2dec->nb_decode_slices))
839 mpeg2dec->picture->flags |= PIC_FLAG_SKIP;
840 else if (mpeg2dec->convert_start) {
841 mpeg2dec->convert_start (decoder->convert_id, mpeg2dec->fbuf[0],
842 mpeg2dec->picture, mpeg2dec->info.gop);
844 if (mpeg2dec->decoder.coding_type == B_TYPE)
845 mpeg2_init_fbuf (&(mpeg2dec->decoder), mpeg2dec->yuv_buf[2],
846 mpeg2dec->yuv_buf[mpeg2dec->yuv_index ^ 1],
847 mpeg2dec->yuv_buf[mpeg2dec->yuv_index]);
848 else {
849 mpeg2_init_fbuf (&(mpeg2dec->decoder),
850 mpeg2dec->yuv_buf[mpeg2dec->yuv_index ^ 1],
851 mpeg2dec->yuv_buf[mpeg2dec->yuv_index],
852 mpeg2dec->yuv_buf[mpeg2dec->yuv_index]);
853 if (mpeg2dec->state == STATE_SLICE)
854 mpeg2dec->yuv_index ^= 1;
856 } else {
857 int b_type;
859 b_type = (mpeg2dec->decoder.coding_type == B_TYPE);
860 mpeg2_init_fbuf (&(mpeg2dec->decoder), mpeg2dec->fbuf[0]->buf,
861 mpeg2dec->fbuf[b_type + 1]->buf,
862 mpeg2dec->fbuf[b_type]->buf);
864 mpeg2dec->action = NULL;
865 return (mpeg2_state_t)-1;
868 static mpeg2_state_t seek_sequence (mpeg2dec_t * mpeg2dec)
870 mpeg2_reset_info (&(mpeg2dec->info));
871 mpeg2dec->info.sequence = NULL;
872 mpeg2dec->info.gop = NULL;
873 mpeg2_header_state_init (mpeg2dec);
874 mpeg2dec->action = mpeg2_seek_header;
875 return mpeg2_seek_header (mpeg2dec);
878 mpeg2_state_t mpeg2_header_end (mpeg2dec_t * mpeg2dec)
880 mpeg2_picture_t * picture;
881 int b_type;
883 b_type = (mpeg2dec->decoder.coding_type == B_TYPE);
884 picture = mpeg2dec->pictures;
885 if ((mpeg2dec->picture >= picture + 2) ^ b_type)
886 picture = mpeg2dec->pictures + 2;
888 mpeg2_reset_info (&(mpeg2dec->info));
889 if (!(mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY)) {
890 mpeg2dec->info.display_picture = picture;
891 if (picture->nb_fields == 1)
892 mpeg2dec->info.display_picture_2nd = picture + 1;
893 mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[b_type];
894 if (!mpeg2dec->convert)
895 mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[b_type + 1];
896 } else if (!mpeg2dec->convert)
897 mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[b_type];
898 mpeg2dec->action = seek_sequence;
899 return STATE_END;