Rearange menu of mpegplayer. Add new menu with "settings" and "quit", and remove...
[kugel-rb.git] / apps / plugins / mpegplayer / header.c
blob001cafe7d50617ba538db0b3a41b11b3594fe66c
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 * $Id$
25 * libmpeg2 sync history:
26 * 2008-07-01 - CVS revision 1.101
29 #include "plugin.h"
31 #include "mpeg2dec_config.h"
33 #include "mpeg2.h"
34 #include "attributes.h"
35 #include "mpeg2_internal.h"
37 #define SEQ_EXT 2
38 #define SEQ_DISPLAY_EXT 4
39 #define QUANT_MATRIX_EXT 8
40 #define COPYRIGHT_EXT 0x10
41 #define PIC_DISPLAY_EXT 0x80
42 #define PIC_CODING_EXT 0x100
44 /* default intra quant matrix, in zig-zag order */
45 static const uint8_t default_intra_quantizer_matrix[64] =
48 16, 16,
49 19, 16, 19,
50 22, 22, 22, 22,
51 22, 22, 26, 24, 26,
52 27, 27, 27, 26, 26, 26,
53 26, 27, 27, 27, 29, 29, 29,
54 34, 34, 34, 29, 29, 29, 27, 27,
55 29, 29, 32, 32, 34, 34, 37,
56 38, 37, 35, 35, 34, 35,
57 38, 38, 40, 40, 40,
58 48, 48, 46, 46,
59 56, 56, 58,
60 69, 69,
64 const uint8_t default_mpeg2_scan_norm[64] =
66 /* Zig-Zag scan pattern */
67 0, 1, 8, 16, 9, 2, 3, 10,
68 17, 24, 32, 25, 18, 11, 4, 5,
69 12, 19, 26, 33, 40, 48, 41, 34,
70 27, 20, 13, 6, 7, 14, 21, 28,
71 35, 42, 49, 56, 57, 50, 43, 36,
72 29, 22, 15, 23, 30, 37, 44, 51,
73 58, 59, 52, 45, 38, 31, 39, 46,
74 53, 60, 61, 54, 47, 55, 62, 63
77 const uint8_t default_mpeg2_scan_alt[64] =
79 /* Alternate scan pattern */
80 0, 8, 16, 24, 1, 9, 2, 10,
81 17, 25, 32, 40, 48, 56, 57, 49,
82 41, 33, 26, 18, 3, 11, 4, 12,
83 19, 27, 34, 42, 50, 58, 35, 43,
84 51, 59, 20, 28, 5, 13, 6, 14,
85 21, 29, 36, 44, 52, 60, 37, 45,
86 53, 61, 22, 30, 7, 15, 23, 31,
87 38, 46, 54, 62, 39, 47, 55, 63
90 uint8_t mpeg2_scan_norm[64] IDATA_ATTR;
91 uint8_t mpeg2_scan_alt[64] IDATA_ATTR;
93 void mpeg2_header_state_init (mpeg2dec_t * mpeg2dec)
95 if (mpeg2dec->sequence.width != (unsigned)-1)
97 mpeg2dec->sequence.width = (unsigned)-1;
98 mpeg2_mem_reset(); /* Clean the memory slate */
99 #if 0
100 if (!mpeg2dec->custom_fbuf)
102 int i;
103 for (i = mpeg2dec->alloc_index_user;
104 i < mpeg2dec->alloc_index; i++)
106 mpeg2_free(mpeg2dec->fbuf_alloc[i].fbuf.buf[0]);
107 #if MPEG2_COLOR
108 mpeg2_free(mpeg2dec->fbuf_alloc[i].fbuf.buf[1]);
109 mpeg2_free(mpeg2dec->fbuf_alloc[i].fbuf.buf[2]);
110 #endif
114 if (mpeg2dec->convert_start)
116 int i;
117 for (i = 0; i < 3; i++)
119 mpeg2_free(mpeg2dec->yuv_buf[i][0]);
120 #if MPEG2_COLOR
121 mpeg2_free(mpeg2dec->yuv_buf[i][1]);
122 mpeg2_free(mpeg2dec->yuv_buf[i][2]);
123 #endif
127 if (mpeg2dec->decoder.convert_id)
129 mpeg2_free(mpeg2dec->decoder.convert_id);
131 #endif
134 mpeg2dec->decoder.coding_type = I_TYPE;
135 mpeg2dec->decoder.convert = NULL;
136 mpeg2dec->decoder.convert_id = NULL;
138 mpeg2dec->picture = mpeg2dec->pictures;
140 mpeg2dec->fbuf[0] = &mpeg2dec->fbuf_alloc[0].fbuf;
141 mpeg2dec->fbuf[1] = &mpeg2dec->fbuf_alloc[1].fbuf;
142 mpeg2dec->fbuf[2] = &mpeg2dec->fbuf_alloc[2].fbuf;
144 mpeg2dec->first = 1;
145 mpeg2dec->alloc_index = 0;
146 mpeg2dec->alloc_index_user = 0;
147 mpeg2dec->first_decode_slice = 1;
148 mpeg2dec->nb_decode_slices = 0xb0 - 1;
149 mpeg2dec->convert = NULL;
150 mpeg2dec->convert_start = NULL;
151 mpeg2dec->custom_fbuf = 0;
152 mpeg2dec->yuv_index = 0;
155 void mpeg2_reset_info (mpeg2_info_t * info)
157 info->current_picture =
158 info->current_picture_2nd = NULL;
160 info->display_picture =
161 info->display_picture_2nd = NULL;
163 info->current_fbuf =
164 info->display_fbuf =
165 info->discard_fbuf = NULL;
168 static void info_user_data (mpeg2dec_t * mpeg2dec)
170 if (mpeg2dec->user_data_len)
172 mpeg2dec->info.user_data = mpeg2dec->chunk_buffer;
173 mpeg2dec->info.user_data_len = mpeg2dec->user_data_len - 3;
177 int mpeg2_header_sequence (mpeg2dec_t * mpeg2dec)
179 static const unsigned int frame_period[16] =
181 0, 1126125, 1125000, 1080000, 900900, 900000, 540000, 450450, 450000,
182 /* unofficial: xing 15 fps */
183 1800000,
184 /* unofficial: libmpeg3 "Unofficial economy rates" 5/10/12/15 fps */
185 5400000, 2700000, 2250000, 1800000, 0, 0
188 uint8_t * buffer = mpeg2dec->chunk_start;
189 mpeg2_sequence_t * sequence = &mpeg2dec->new_sequence;
190 int i;
192 if ((buffer[6] & 0x20) != 0x20) /* missing marker_bit */
193 return 1;
195 i = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2];
197 if (!(sequence->display_width = sequence->picture_width = i >> 12))
198 return 1;
200 if (!(sequence->display_height = sequence->picture_height = i & 0xfff))
201 return 1;
203 sequence->width = (sequence->picture_width + 15) & ~15;
204 sequence->height = (sequence->picture_height + 15) & ~15;
205 sequence->chroma_width = sequence->width >> 1;
206 sequence->chroma_height = sequence->height >> 1;
208 sequence->flags = SEQ_FLAG_PROGRESSIVE_SEQUENCE |
209 SEQ_VIDEO_FORMAT_UNSPECIFIED;
211 sequence->pixel_width = buffer[3] >> 4; /* aspect ratio */
212 sequence->frame_period = frame_period[buffer[3] & 15];
214 sequence->byte_rate = (buffer[4]<<10) | (buffer[5]<<2) | (buffer[6]>>6);
216 sequence->vbv_buffer_size = ((buffer[6]<<16) | (buffer[7]<<8)) & 0x1ff800;
218 if (buffer[7] & 4)
219 sequence->flags |= SEQ_FLAG_CONSTRAINED_PARAMETERS;
221 mpeg2dec->copy_matrix = 3;
223 if (buffer[7] & 2)
225 for (i = 0; i < 64; i++)
227 mpeg2dec->new_quantizer_matrix[0][mpeg2_scan_norm[i]] =
228 (buffer[i+7] << 7) | (buffer[i+8] >> 1);
231 buffer += 64;
233 else
235 for (i = 0; i < 64; i++)
237 mpeg2dec->new_quantizer_matrix[0][mpeg2_scan_norm[i]] =
238 default_intra_quantizer_matrix[i];
242 if (buffer[7] & 1)
244 for (i = 0; i < 64; i++)
246 mpeg2dec->new_quantizer_matrix[1][mpeg2_scan_norm[i]] =
247 buffer[i+8];
250 else
252 rb->memset (mpeg2dec->new_quantizer_matrix[1], 16, 64);
255 sequence->profile_level_id = 0x80;
256 sequence->colour_primaries = 0;
257 sequence->transfer_characteristics = 0;
258 sequence->matrix_coefficients = 0;
260 mpeg2dec->ext_state = SEQ_EXT;
261 mpeg2dec->state = STATE_SEQUENCE;
263 mpeg2dec->display_offset_x =
264 mpeg2dec->display_offset_y = 0;
266 return 0;
269 static int sequence_ext (mpeg2dec_t * mpeg2dec)
271 uint8_t * buffer = mpeg2dec->chunk_start;
272 mpeg2_sequence_t * sequence = &mpeg2dec->new_sequence;
273 uint32_t flags;
275 if (!(buffer[3] & 1))
276 return 1;
278 sequence->profile_level_id = (buffer[0] << 4) | (buffer[1] >> 4);
280 sequence->picture_width += ((buffer[1] << 13) | (buffer[2] << 5)) & 0x3000;
281 sequence->display_width = sequence->picture_width;
283 sequence->picture_height += (buffer[2] << 7) & 0x3000;
284 sequence->display_height = sequence->picture_height;
286 sequence->width = (sequence->picture_width + 15) & ~15;
287 sequence->height = (sequence->picture_height + 15) & ~15;
289 flags = sequence->flags | SEQ_FLAG_MPEG2;
291 if (!(buffer[1] & 8))
293 flags &= ~SEQ_FLAG_PROGRESSIVE_SEQUENCE;
294 sequence->height = (sequence->height + 31) & ~31;
297 if (buffer[5] & 0x80)
298 flags |= SEQ_FLAG_LOW_DELAY;
300 sequence->flags = flags;
301 sequence->chroma_width = sequence->width;
302 sequence->chroma_height = sequence->height;
304 switch (buffer[1] & 6)
306 case 0: /* invalid */
307 return 1;
308 case 2: /* 4:2:0 */
309 sequence->chroma_height >>= 1;
310 case 4: /* 4:2:2 */
311 sequence->chroma_width >>= 1;
314 sequence->byte_rate += ((buffer[2]<<25) | (buffer[3]<<17)) & 0x3ffc0000;
316 sequence->vbv_buffer_size |= buffer[4] << 21;
318 sequence->frame_period =
319 sequence->frame_period * ((buffer[5]&31)+1) / (((buffer[5]>>2)&3)+1);
321 mpeg2dec->ext_state = SEQ_DISPLAY_EXT;
323 return 0;
326 static int sequence_display_ext (mpeg2dec_t * mpeg2dec)
328 uint8_t * buffer = mpeg2dec->chunk_start;
329 mpeg2_sequence_t * sequence = &mpeg2dec->new_sequence;
330 int x;
332 sequence->flags = (sequence->flags & ~SEQ_MASK_VIDEO_FORMAT) |
333 ((buffer[0] << 4) & SEQ_MASK_VIDEO_FORMAT);
335 if (buffer[0] & 1)
337 sequence->flags |= SEQ_FLAG_COLOUR_DESCRIPTION;
338 sequence->colour_primaries = buffer[1];
339 sequence->transfer_characteristics = buffer[2];
340 sequence->matrix_coefficients = buffer[3];
341 buffer += 3;
344 if (!(buffer[2] & 2)) /* missing marker_bit */
345 return 1;
347 x = (buffer[1] << 6) | (buffer[2] >> 2);
348 if (x)
349 sequence->display_width = x;
351 x = ((buffer[2] & 1) << 13) | (buffer[3] << 5) | (buffer[4] >> 3);
352 if (x)
353 sequence->display_height = x;
355 return 0;
358 static inline void simplify (unsigned int * u, unsigned int * v)
360 unsigned int a, b, tmp;
362 a = *u;
363 b = *v;
365 /* find greatest common divisor */
366 while (a)
368 tmp = a;
369 a = b % tmp;
370 b = tmp;
373 *u /= b;
374 *v /= b;
377 static inline void finalize_sequence (mpeg2_sequence_t * sequence)
379 int width;
380 int height;
382 sequence->byte_rate *= 50;
384 if (sequence->flags & SEQ_FLAG_MPEG2)
386 switch (sequence->pixel_width)
388 case 1: /* square pixels */
389 sequence->pixel_width =
390 sequence->pixel_height = 1;
391 return;
392 case 2: /* 4:3 aspect ratio */
393 width = 4;
394 height = 3;
395 break;
396 case 3: /* 16:9 aspect ratio */
397 width = 16;
398 height = 9;
399 break;
400 case 4: /* 2.21:1 aspect ratio */
401 width = 221;
402 height = 100;
403 break;
404 default: /* illegal */
405 sequence->pixel_width =
406 sequence->pixel_height = 0;
407 return;
410 width *= sequence->display_height;
411 height *= sequence->display_width;
413 else
415 if (sequence->byte_rate == 50 * 0x3ffff)
416 sequence->byte_rate = 0; /* mpeg-1 VBR */
418 switch (sequence->pixel_width)
420 case 0:
421 case 15: /* illegal */
422 sequence->pixel_width =
423 sequence->pixel_height = 0;
424 return;
425 case 1: /* square pixels */
426 sequence->pixel_width =
427 sequence->pixel_height = 1;
428 return;
429 case 3: /* 720x576 16:9 */
430 sequence->pixel_width = 64;
431 sequence->pixel_height = 45;
432 return;
433 case 6: /* 720x480 16:9 */
434 sequence->pixel_width = 32;
435 sequence->pixel_height = 27;
436 return;
437 case 8: /* BT.601 625 lines 4:3 */
438 sequence->pixel_width = 59;
439 sequence->pixel_height = 54;
440 return;
441 case 12: /* BT.601 525 lines 4:3 */
442 sequence->pixel_width = 10;
443 sequence->pixel_height = 11;
444 return;
445 default:
446 height = 88 * sequence->pixel_width + 1171;
447 width = 2000;
451 sequence->pixel_width = width;
452 sequence->pixel_height = height;
454 simplify(&sequence->pixel_width, &sequence->pixel_height);
457 int mpeg2_guess_aspect (const mpeg2_sequence_t * sequence,
458 unsigned int * pixel_width,
459 unsigned int * pixel_height)
461 static const struct
463 unsigned int width, height;
464 } video_modes[] =
466 {720, 576}, /* 625 lines, 13.5 MHz (D1, DV, DVB, DVD) */
467 {704, 576}, /* 625 lines, 13.5 MHz (1/1 D1, DVB, DVD, 4CIF) */
468 {544, 576}, /* 625 lines, 10.125 MHz (DVB, laserdisc) */
469 {528, 576}, /* 625 lines, 10.125 MHz (3/4 D1, DVB, laserdisc) */
470 {480, 576}, /* 625 lines, 9 MHz (2/3 D1, DVB, SVCD) */
471 {352, 576}, /* 625 lines, 6.75 MHz (D2, 1/2 D1, CVD, DVB, DVD) */
472 {352, 288}, /* 625 lines, 6.75 MHz, 1 field (D4, VCD, DVB, DVD, CIF) */
473 {176, 144}, /* 625 lines, 3.375 MHz, half field (QCIF) */
474 {720, 486}, /* 525 lines, 13.5 MHz (D1) */
475 {704, 486}, /* 525 lines, 13.5 MHz */
476 {720, 480}, /* 525 lines, 13.5 MHz (DV, DSS, DVD) */
477 {704, 480}, /* 525 lines, 13.5 MHz (1/1 D1, ATSC, DVD) */
478 {544, 480}, /* 525 lines. 10.125 MHz (DSS, laserdisc) */
479 {528, 480}, /* 525 lines. 10.125 MHz (3/4 D1, laserdisc) */
480 {480, 480}, /* 525 lines, 9 MHz (2/3 D1, SVCD) */
481 {352, 480}, /* 525 lines, 6.75 MHz (D2, 1/2 D1, CVD, DVD) */
482 {352, 240} /* 525 lines. 6.75 MHz, 1 field (D4, VCD, DSS, DVD) */
484 unsigned int width, height, pix_width, pix_height, i, DAR_16_9;
486 *pixel_width = sequence->pixel_width;
487 *pixel_height = sequence->pixel_height;
488 width = sequence->picture_width;
489 height = sequence->picture_height;
491 for (i = 0; i < sizeof (video_modes) / sizeof (video_modes[0]); i++)
493 if (width == video_modes[i].width && height == video_modes[i].height)
494 break;
497 if (i == ARRAYLEN(video_modes) ||
498 (sequence->pixel_width == 1 && sequence->pixel_height == 1) ||
499 width != sequence->display_width || height != sequence->display_height)
501 return 0;
504 for (pix_height = 1; height * pix_height < 480; pix_height <<= 1);
505 height *= pix_height;
507 for (pix_width = 1; width * pix_width <= 352; pix_width <<= 1);
508 width *= pix_width;
510 if (!(sequence->flags & SEQ_FLAG_MPEG2))
512 static unsigned int mpeg1_check[2][2] = {{11, 54}, {27, 45}};
513 DAR_16_9 = (sequence->pixel_height == 27 ||
514 sequence->pixel_height == 45);
515 if (width < 704 ||
516 sequence->pixel_height != mpeg1_check[DAR_16_9][height == 576])
517 return 0;
519 else
521 DAR_16_9 = (3 * sequence->picture_width * sequence->pixel_width >
522 4 * sequence->picture_height * sequence->pixel_height);
523 switch (width)
525 case 528:
526 case 544:
527 pix_width *= 4;
528 pix_height *= 3;
529 break;
530 case 480:
531 pix_width *= 3;
532 pix_height *= 2;
533 break;
537 if (DAR_16_9)
539 pix_width *= 4;
540 pix_height *= 3;
543 if (height == 576)
545 pix_width *= 59;
546 pix_height *= 54;
548 else
550 pix_width *= 10;
551 pix_height *= 11;
554 *pixel_width = pix_width;
555 *pixel_height = pix_height;
557 simplify (pixel_width, pixel_height);
559 return (height == 576) ? 1 : 2;
562 static void copy_matrix (mpeg2dec_t * mpeg2dec, int index)
564 if (rb->memcmp (mpeg2dec->quantizer_matrix[index],
565 mpeg2dec->new_quantizer_matrix[index], 64))
567 rb->memcpy (mpeg2dec->quantizer_matrix[index],
568 mpeg2dec->new_quantizer_matrix[index], 64);
570 mpeg2dec->scaled[index] = -1;
574 static void finalize_matrix (mpeg2dec_t * mpeg2dec)
576 mpeg2_decoder_t *decoder = &mpeg2dec->decoder;
577 int i;
579 for (i = 0; i < 2; i++)
581 if (mpeg2dec->copy_matrix & (1 << i))
582 copy_matrix (mpeg2dec, i);
584 if ((mpeg2dec->copy_matrix & (4 << i)) &&
585 rb->memcmp(mpeg2dec->quantizer_matrix[i],
586 mpeg2dec->new_quantizer_matrix[i+2], 64))
588 copy_matrix (mpeg2dec, i + 2);
589 decoder->chroma_quantizer[i] = decoder->quantizer_prescale[i+2];
591 else if (mpeg2dec->copy_matrix & (5 << i))
593 decoder->chroma_quantizer[i] = decoder->quantizer_prescale[i];
598 static mpeg2_state_t invalid_end_action (mpeg2dec_t * mpeg2dec)
600 mpeg2_reset_info (&mpeg2dec->info);
602 mpeg2dec->info.gop = NULL;
604 info_user_data (mpeg2dec);
606 mpeg2_header_state_init (mpeg2dec);
608 mpeg2dec->sequence = mpeg2dec->new_sequence;
609 mpeg2dec->action = mpeg2_seek_header;
610 mpeg2dec->state = STATE_SEQUENCE;
612 return STATE_SEQUENCE;
615 void mpeg2_header_sequence_finalize (mpeg2dec_t * mpeg2dec)
617 mpeg2_sequence_t * sequence = &mpeg2dec->new_sequence;
618 mpeg2_decoder_t * decoder = &mpeg2dec->decoder;
620 finalize_sequence(sequence);
621 finalize_matrix(mpeg2dec);
623 decoder->mpeg1 = !(sequence->flags & SEQ_FLAG_MPEG2);
624 decoder->width = sequence->width;
625 decoder->height = sequence->height;
626 decoder->vertical_position_extension = sequence->picture_height > 2800;
627 decoder->chroma_format = (sequence->chroma_width == sequence->width) +
628 (sequence->chroma_height == sequence->height);
630 if (mpeg2dec->sequence.width != (unsigned)-1)
633 * According to 6.1.1.6, repeat sequence headers should be
634 * identical to the original. However some encoders don't
635 * respect that and change various fields (including bitrate
636 * and aspect ratio) in the repeat sequence headers. So we
637 * choose to be as conservative as possible and only restart
638 * the decoder if the width, height, chroma_width,
639 * chroma_height or low_delay flag are modified.
641 if (sequence->width != mpeg2dec->sequence.width ||
642 sequence->height != mpeg2dec->sequence.height ||
643 sequence->chroma_width != mpeg2dec->sequence.chroma_width ||
644 sequence->chroma_height != mpeg2dec->sequence.chroma_height ||
645 ((sequence->flags ^ mpeg2dec->sequence.flags) &
646 SEQ_FLAG_LOW_DELAY))
648 decoder->stride_frame = sequence->width;
649 mpeg2_header_end (mpeg2dec);
650 mpeg2dec->action = invalid_end_action;
651 mpeg2dec->state = STATE_INVALID_END;
652 return;
655 mpeg2dec->state = rb->memcmp(&mpeg2dec->sequence, sequence,
656 sizeof (mpeg2_sequence_t)) ?
657 STATE_SEQUENCE_MODIFIED :
658 STATE_SEQUENCE_REPEATED;
660 else
662 decoder->stride_frame = sequence->width;
665 mpeg2dec->sequence = *sequence;
666 mpeg2_reset_info(&mpeg2dec->info);
667 mpeg2dec->info.sequence = &mpeg2dec->sequence;
668 mpeg2dec->info.gop = NULL;
670 info_user_data (mpeg2dec);
673 int mpeg2_header_gop (mpeg2dec_t * mpeg2dec)
675 uint8_t * buffer = mpeg2dec->chunk_start;
676 mpeg2_gop_t * gop = &mpeg2dec->new_gop;
678 if (!(buffer[1] & 8))
679 return 1;
681 gop->hours = (buffer[0] >> 2) & 31;
682 gop->minutes = ((buffer[0] << 4) | (buffer[1] >> 4)) & 63;
683 gop->seconds = ((buffer[1] << 3) | (buffer[2] >> 5)) & 63;
684 gop->pictures = ((buffer[2] << 1) | (buffer[3] >> 7)) & 63;
685 gop->flags = (buffer[0] >> 7) | ((buffer[3] >> 4) & 6);
687 mpeg2dec->state = STATE_GOP;
688 return 0;
691 void mpeg2_header_gop_finalize (mpeg2dec_t * mpeg2dec)
693 mpeg2dec->gop = mpeg2dec->new_gop;
694 mpeg2_reset_info (&mpeg2dec->info);
695 mpeg2dec->info.gop = &mpeg2dec->gop;
696 info_user_data (mpeg2dec);
699 void mpeg2_set_fbuf (mpeg2dec_t * mpeg2dec, int b_type)
701 int i;
703 for (i = 0; i < 3; i++)
705 if (mpeg2dec->fbuf[1] != &mpeg2dec->fbuf_alloc[i].fbuf &&
706 mpeg2dec->fbuf[2] != &mpeg2dec->fbuf_alloc[i].fbuf)
708 mpeg2dec->fbuf[0] = &mpeg2dec->fbuf_alloc[i].fbuf;
709 mpeg2dec->info.current_fbuf = mpeg2dec->fbuf[0];
711 if (b_type || (mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY))
713 if (b_type || mpeg2dec->convert)
714 mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[0];
716 mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[0];
719 break;
724 int mpeg2_header_picture (mpeg2dec_t * mpeg2dec)
726 uint8_t * buffer = mpeg2dec->chunk_start;
727 mpeg2_picture_t * picture = &mpeg2dec->new_picture;
728 mpeg2_decoder_t * decoder = &mpeg2dec->decoder;
729 int type;
731 mpeg2dec->state = (mpeg2dec->state != STATE_SLICE_1ST) ?
732 STATE_PICTURE : STATE_PICTURE_2ND;
733 mpeg2dec->ext_state = PIC_CODING_EXT;
735 picture->temporal_reference = (buffer[0] << 2) | (buffer[1] >> 6);
737 type = (buffer [1] >> 3) & 7;
739 if (type == PIC_FLAG_CODING_TYPE_P || type == PIC_FLAG_CODING_TYPE_B)
741 /* forward_f_code and backward_f_code - used in mpeg1 only */
742 decoder->f_motion.f_code[1] = (buffer[3] >> 2) & 1;
743 decoder->f_motion.f_code[0] =
744 (((buffer[3] << 1) | (buffer[4] >> 7)) & 7) - 1;
745 decoder->b_motion.f_code[1] = (buffer[4] >> 6) & 1;
746 decoder->b_motion.f_code[0] = ((buffer[4] >> 3) & 7) - 1;
749 picture->flags = PIC_FLAG_PROGRESSIVE_FRAME | type;
750 picture->tag = picture->tag2 = 0;
752 if (mpeg2dec->num_tags)
754 if (mpeg2dec->bytes_since_tag >= mpeg2dec->chunk_ptr - buffer + 4)
756 mpeg2dec->num_tags = 0;
757 picture->tag = mpeg2dec->tag_current;
758 picture->tag2 = mpeg2dec->tag2_current;
759 picture->flags |= PIC_FLAG_TAGS;
761 else if (mpeg2dec->num_tags > 1)
763 mpeg2dec->num_tags = 1;
764 picture->tag = mpeg2dec->tag_previous;
765 picture->tag2 = mpeg2dec->tag2_previous;
766 picture->flags |= PIC_FLAG_TAGS;
770 picture->nb_fields = 2;
771 picture->display_offset[0].x = picture->display_offset[1].x =
772 picture->display_offset[2].x = mpeg2dec->display_offset_x;
774 picture->display_offset[0].y = picture->display_offset[1].y =
775 picture->display_offset[2].y = mpeg2dec->display_offset_y;
777 /* XXXXXX decode extra_information_picture as well */
779 mpeg2dec->q_scale_type = 0;
780 decoder->intra_dc_precision = 7;
781 decoder->frame_pred_frame_dct = 1;
782 decoder->concealment_motion_vectors = 0;
783 decoder->scan = mpeg2_scan_norm;
784 decoder->picture_structure = FRAME_PICTURE;
785 mpeg2dec->copy_matrix = 0;
787 return 0;
790 static int picture_coding_ext (mpeg2dec_t * mpeg2dec)
792 uint8_t * buffer = mpeg2dec->chunk_start;
793 mpeg2_picture_t * picture = &mpeg2dec->new_picture;
794 mpeg2_decoder_t * decoder = &mpeg2dec->decoder;
795 uint32_t flags;
797 /* pre subtract 1 for use later in compute_motion_vector */
798 decoder->f_motion.f_code[0] = (buffer[0] & 15) - 1;
799 decoder->f_motion.f_code[1] = (buffer[1] >> 4) - 1;
800 decoder->b_motion.f_code[0] = (buffer[1] & 15) - 1;
801 decoder->b_motion.f_code[1] = (buffer[2] >> 4) - 1;
803 flags = picture->flags;
805 decoder->intra_dc_precision = 7 - ((buffer[2] >> 2) & 3);
806 decoder->picture_structure = buffer[2] & 3;
808 switch (decoder->picture_structure)
810 case TOP_FIELD:
811 flags |= PIC_FLAG_TOP_FIELD_FIRST;
812 case BOTTOM_FIELD:
813 picture->nb_fields = 1;
814 break;
815 case FRAME_PICTURE:
816 if (!(mpeg2dec->sequence.flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE))
818 picture->nb_fields = (buffer[3] & 2) ? 3 : 2;
819 flags |= (buffer[3] & 128) ? PIC_FLAG_TOP_FIELD_FIRST : 0;
821 else
823 picture->nb_fields = (buffer[3]&2) ? ((buffer[3]&128) ? 6 : 4) : 2;
825 break;
826 default:
827 return 1;
830 decoder->top_field_first = buffer[3] >> 7;
831 decoder->frame_pred_frame_dct = (buffer[3] >> 6) & 1;
832 decoder->concealment_motion_vectors = (buffer[3] >> 5) & 1;
833 mpeg2dec->q_scale_type = buffer[3] & 16;
834 decoder->intra_vlc_format = (buffer[3] >> 3) & 1;
835 decoder->scan = (buffer[3] & 4) ? mpeg2_scan_alt : mpeg2_scan_norm;
837 if (!(buffer[4] & 0x80))
838 flags &= ~PIC_FLAG_PROGRESSIVE_FRAME;
840 if (buffer[4] & 0x40)
842 flags |= (((buffer[4]<<26) | (buffer[5]<<18) | (buffer[6]<<10)) &
843 PIC_MASK_COMPOSITE_DISPLAY) | PIC_FLAG_COMPOSITE_DISPLAY;
846 picture->flags = flags;
848 mpeg2dec->ext_state = PIC_DISPLAY_EXT | COPYRIGHT_EXT | QUANT_MATRIX_EXT;
850 return 0;
853 static int picture_display_ext (mpeg2dec_t * mpeg2dec)
855 uint8_t * buffer = mpeg2dec->chunk_start;
856 mpeg2_picture_t * picture = &mpeg2dec->new_picture;
857 int i, nb_pos;
859 nb_pos = picture->nb_fields;
861 if (mpeg2dec->sequence.flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE)
862 nb_pos >>= 1;
864 for (i = 0; i < nb_pos; i++)
866 int x, y;
868 x = ((buffer[4*i] << 24) | (buffer[4*i+1] << 16) |
869 (buffer[4*i+2] << 8) | buffer[4*i+3]) >> (11-2*i);
871 y = ((buffer[4*i+2] << 24) | (buffer[4*i+3] << 16) |
872 (buffer[4*i+4] << 8) | buffer[4*i+5]) >> (10-2*i);
874 if (! (x & y & 1))
875 return 1;
877 picture->display_offset[i].x = mpeg2dec->display_offset_x = x >> 1;
878 picture->display_offset[i].y = mpeg2dec->display_offset_y = y >> 1;
881 for (; i < 3; i++)
883 picture->display_offset[i].x = mpeg2dec->display_offset_x;
884 picture->display_offset[i].y = mpeg2dec->display_offset_y;
887 return 0;
890 void mpeg2_header_picture_finalize (mpeg2dec_t * mpeg2dec)
892 mpeg2_decoder_t * decoder = &mpeg2dec->decoder;
893 int old_type_b = decoder->coding_type == B_TYPE;
894 int low_delay = mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY;
896 finalize_matrix (mpeg2dec);
897 decoder->coding_type = mpeg2dec->new_picture.flags & PIC_MASK_CODING_TYPE;
899 if (mpeg2dec->state == STATE_PICTURE)
901 mpeg2_picture_t * picture;
902 mpeg2_picture_t * other;
904 decoder->second_field = 0;
906 picture = other = mpeg2dec->pictures;
908 if (old_type_b ^ (mpeg2dec->picture < mpeg2dec->pictures + 2))
909 picture += 2;
910 else
911 other += 2;
913 mpeg2dec->picture = picture;
914 *picture = mpeg2dec->new_picture;
916 if (!old_type_b)
918 mpeg2dec->fbuf[2] = mpeg2dec->fbuf[1];
919 mpeg2dec->fbuf[1] = mpeg2dec->fbuf[0];
922 mpeg2dec->fbuf[0] = NULL;
923 mpeg2_reset_info (&mpeg2dec->info);
924 mpeg2dec->info.current_picture = picture;
925 mpeg2dec->info.display_picture = picture;
927 if (decoder->coding_type != B_TYPE)
929 if (!low_delay)
931 if (mpeg2dec->first)
933 mpeg2dec->info.display_picture = NULL;
934 mpeg2dec->first = 0;
936 else
938 mpeg2dec->info.display_picture = other;
940 if (other->nb_fields == 1)
941 mpeg2dec->info.display_picture_2nd = other + 1;
943 mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[1];
947 if (!low_delay + !mpeg2dec->convert)
949 mpeg2dec->info.discard_fbuf =
950 mpeg2dec->fbuf[!low_delay + !mpeg2dec->convert];
954 if (mpeg2dec->convert)
956 mpeg2_convert_init_t convert_init;
958 if (!mpeg2dec->convert_start)
960 mpeg2dec->decoder.convert_id =
961 mpeg2_malloc (mpeg2dec->convert_id_size,
962 MPEG2_ALLOC_CONVERT_ID);
964 mpeg2dec->convert (MPEG2_CONVERT_START,
965 mpeg2dec->decoder.convert_id,
966 &mpeg2dec->sequence,
967 mpeg2dec->convert_stride,
968 mpeg2dec->convert_arg, &convert_init);
970 mpeg2dec->convert_start = convert_init.start;
971 mpeg2dec->decoder.convert = convert_init.copy;
973 int y_size = decoder->stride_frame * mpeg2dec->sequence.height;
975 mpeg2dec->yuv_buf[0][0] =
976 (uint8_t *) mpeg2_malloc(y_size, MPEG2_ALLOC_YUV);
977 #if MPEG2_COLOR
978 int uv_size = y_size >> (2 - mpeg2dec->decoder.chroma_format);
980 mpeg2dec->yuv_buf[0][1] =
981 (uint8_t *) mpeg2_malloc(uv_size, MPEG2_ALLOC_YUV);
982 mpeg2dec->yuv_buf[0][2] =
983 (uint8_t *) mpeg2_malloc(uv_size, MPEG2_ALLOC_YUV);
984 #endif
986 mpeg2dec->yuv_buf[1][0] =
987 (uint8_t *) mpeg2_malloc(y_size, MPEG2_ALLOC_YUV);
988 #if MPEG2_COLOR
989 mpeg2dec->yuv_buf[1][1] =
990 (uint8_t *) mpeg2_malloc(uv_size, MPEG2_ALLOC_YUV);
991 mpeg2dec->yuv_buf[1][2] =
992 (uint8_t *) mpeg2_malloc(uv_size, MPEG2_ALLOC_YUV);
993 #endif
994 y_size = decoder->stride_frame * 32;
996 mpeg2dec->yuv_buf[2][0] =
997 (uint8_t *) mpeg2_malloc(y_size, MPEG2_ALLOC_YUV);
998 #if MPEG2_COLOR
999 uv_size = y_size >> (2 - mpeg2dec->decoder.chroma_format);
1001 mpeg2dec->yuv_buf[2][1] =
1002 (uint8_t *) mpeg2_malloc(uv_size, MPEG2_ALLOC_YUV);
1003 mpeg2dec->yuv_buf[2][2] =
1004 (uint8_t *) mpeg2_malloc(uv_size, MPEG2_ALLOC_YUV);
1005 #endif
1008 if (!mpeg2dec->custom_fbuf)
1010 while (mpeg2dec->alloc_index < 3)
1012 mpeg2_fbuf_t * fbuf;
1014 fbuf = &mpeg2dec->fbuf_alloc[mpeg2dec->alloc_index++].fbuf;
1015 fbuf->id = NULL;
1017 fbuf->buf[0] =
1018 (uint8_t *) mpeg2_malloc (convert_init.buf_size[0],
1019 MPEG2_ALLOC_CONVERTED);
1020 #if MPEG2_COLOR
1021 fbuf->buf[1] =
1022 (uint8_t *) mpeg2_malloc (convert_init.buf_size[1],
1023 MPEG2_ALLOC_CONVERTED);
1024 fbuf->buf[2] =
1025 (uint8_t *) mpeg2_malloc (convert_init.buf_size[2],
1026 MPEG2_ALLOC_CONVERTED);
1027 #endif
1030 mpeg2_set_fbuf (mpeg2dec, (decoder->coding_type == B_TYPE));
1033 else if (!mpeg2dec->custom_fbuf)
1035 while (mpeg2dec->alloc_index < 3)
1037 mpeg2_fbuf_t * fbuf;
1039 fbuf = &mpeg2dec->fbuf_alloc[mpeg2dec->alloc_index++].fbuf;
1041 fbuf->id = NULL;
1043 int y_size = decoder->stride_frame * mpeg2dec->sequence.height;
1045 fbuf->buf[0] = (uint8_t *) mpeg2_malloc (y_size,
1046 MPEG2_ALLOC_YUV);
1047 #if MPEG2_COLOR
1048 int uv_size = y_size >> (2 - decoder->chroma_format);
1050 fbuf->buf[1] = (uint8_t *) mpeg2_malloc (uv_size,
1051 MPEG2_ALLOC_YUV);
1052 fbuf->buf[2] = (uint8_t *) mpeg2_malloc (uv_size,
1053 MPEG2_ALLOC_YUV);
1054 #endif
1057 mpeg2_set_fbuf (mpeg2dec, (decoder->coding_type == B_TYPE));
1060 else
1062 decoder->second_field = 1;
1063 mpeg2dec->picture++; /* second field picture */
1065 *(mpeg2dec->picture) = mpeg2dec->new_picture;
1067 mpeg2dec->info.current_picture_2nd = mpeg2dec->picture;
1069 if (low_delay || decoder->coding_type == B_TYPE)
1070 mpeg2dec->info.display_picture_2nd = mpeg2dec->picture;
1073 info_user_data (mpeg2dec);
1076 static int copyright_ext (mpeg2dec_t * mpeg2dec)
1078 (void)mpeg2dec;
1079 return 0;
1082 static int quant_matrix_ext (mpeg2dec_t * mpeg2dec)
1084 uint8_t * buffer = mpeg2dec->chunk_start;
1085 int i, j;
1087 for (i = 0; i < 4; i++)
1089 if (buffer[0] & (8 >> i))
1091 for (j = 0; j < 64; j++)
1093 mpeg2dec->new_quantizer_matrix[i][mpeg2_scan_norm[j]] =
1094 (buffer[j] << (i+5)) | (buffer[j+1] >> (3-i));
1097 mpeg2dec->copy_matrix |= 1 << i;
1098 buffer += 64;
1102 return 0;
1105 int mpeg2_header_extension (mpeg2dec_t * mpeg2dec)
1107 static int (* const parser[9]) (mpeg2dec_t *) =
1109 NULL,
1110 sequence_ext,
1111 sequence_display_ext,
1112 quant_matrix_ext,
1113 copyright_ext,
1114 NULL,
1115 NULL,
1116 picture_display_ext,
1117 picture_coding_ext
1120 int ext, ext_bit;
1122 ext = mpeg2dec->chunk_start[0] >> 4;
1123 ext_bit = 1 << ext;
1125 if (!(mpeg2dec->ext_state & ext_bit))
1126 return 0; /* ignore illegal extensions */
1128 mpeg2dec->ext_state &= ~ext_bit;
1130 return parser[ext] (mpeg2dec);
1133 int mpeg2_header_user_data (mpeg2dec_t * mpeg2dec)
1135 mpeg2dec->user_data_len += mpeg2dec->chunk_ptr - 1 - mpeg2dec->chunk_start;
1136 mpeg2dec->chunk_start = mpeg2dec->chunk_ptr - 1;
1138 return 0;
1141 static void prescale (mpeg2dec_t * mpeg2dec, int index)
1143 static const int non_linear_scale[32] =
1145 0, 1, 2, 3, 4, 5, 6, 7,
1146 8, 10, 12, 14, 16, 18, 20, 22,
1147 24, 28, 32, 36, 40, 44, 48, 52,
1148 56, 64, 72, 80, 88, 96, 104, 112
1151 int i, j, k;
1152 mpeg2_decoder_t * decoder = &mpeg2dec->decoder;
1154 if (mpeg2dec->scaled[index] != mpeg2dec->q_scale_type)
1156 mpeg2dec->scaled[index] = mpeg2dec->q_scale_type;
1158 for (i = 0; i < 32; i++)
1160 k = mpeg2dec->q_scale_type ? non_linear_scale[i] : (i << 1);
1162 for (j = 0; j < 64; j++)
1164 decoder->quantizer_prescale[index][i][j] =
1165 k * mpeg2dec->quantizer_matrix[index][j];
1171 mpeg2_state_t mpeg2_header_slice_start (mpeg2dec_t * mpeg2dec)
1173 mpeg2_decoder_t * decoder = &mpeg2dec->decoder;
1175 mpeg2dec->info.user_data = NULL;
1176 mpeg2dec->info.user_data_len = 0;
1177 mpeg2dec->state = (mpeg2dec->picture->nb_fields > 1 ||
1178 mpeg2dec->state == STATE_PICTURE_2ND) ?
1179 STATE_SLICE : STATE_SLICE_1ST;
1181 if (mpeg2dec->decoder.coding_type != D_TYPE)
1183 prescale (mpeg2dec, 0);
1185 if (decoder->chroma_quantizer[0] == decoder->quantizer_prescale[2])
1186 prescale (mpeg2dec, 2);
1188 if (mpeg2dec->decoder.coding_type != I_TYPE)
1190 prescale (mpeg2dec, 1);
1192 if (decoder->chroma_quantizer[1] == decoder->quantizer_prescale[3])
1193 prescale (mpeg2dec, 3);
1197 if (!(mpeg2dec->nb_decode_slices))
1199 mpeg2dec->picture->flags |= PIC_FLAG_SKIP;
1201 else if (mpeg2dec->convert_start)
1203 mpeg2dec->convert_start (decoder->convert_id, mpeg2dec->fbuf[0],
1204 mpeg2dec->picture, mpeg2dec->info.gop);
1206 if (mpeg2dec->decoder.coding_type == B_TYPE)
1208 mpeg2_init_fbuf (&mpeg2dec->decoder, mpeg2dec->yuv_buf[2],
1209 mpeg2dec->yuv_buf[mpeg2dec->yuv_index ^ 1],
1210 mpeg2dec->yuv_buf[mpeg2dec->yuv_index]);
1212 else
1214 mpeg2_init_fbuf (&mpeg2dec->decoder,
1215 mpeg2dec->yuv_buf[mpeg2dec->yuv_index ^ 1],
1216 mpeg2dec->yuv_buf[mpeg2dec->yuv_index],
1217 mpeg2dec->yuv_buf[mpeg2dec->yuv_index]);
1219 if (mpeg2dec->state == STATE_SLICE)
1220 mpeg2dec->yuv_index ^= 1;
1223 else
1225 int b_type;
1227 b_type = (mpeg2dec->decoder.coding_type == B_TYPE);
1229 mpeg2_init_fbuf (&mpeg2dec->decoder, mpeg2dec->fbuf[0]->buf,
1230 mpeg2dec->fbuf[b_type + 1]->buf,
1231 mpeg2dec->fbuf[b_type]->buf);
1234 mpeg2dec->action = NULL;
1236 return STATE_INTERNAL_NORETURN;
1239 static mpeg2_state_t seek_sequence (mpeg2dec_t * mpeg2dec)
1241 mpeg2_reset_info (&mpeg2dec->info);
1243 mpeg2dec->info.sequence = NULL;
1244 mpeg2dec->info.gop = NULL;
1246 mpeg2_header_state_init (mpeg2dec);
1248 mpeg2dec->action = mpeg2_seek_header;
1250 return mpeg2_seek_header (mpeg2dec);
1253 mpeg2_state_t mpeg2_header_end (mpeg2dec_t * mpeg2dec)
1255 mpeg2_picture_t * picture;
1256 int b_type;
1258 b_type = (mpeg2dec->decoder.coding_type == B_TYPE);
1259 picture = mpeg2dec->pictures;
1261 if ((mpeg2dec->picture >= picture + 2) ^ b_type)
1262 picture = mpeg2dec->pictures + 2;
1264 mpeg2_reset_info (&mpeg2dec->info);
1266 if (!(mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY))
1268 mpeg2dec->info.display_picture = picture;
1270 if (picture->nb_fields == 1)
1271 mpeg2dec->info.display_picture_2nd = picture + 1;
1273 mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[b_type];
1275 if (!mpeg2dec->convert)
1276 mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[b_type + 1];
1278 else if (!mpeg2dec->convert)
1280 mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[b_type];
1283 mpeg2dec->action = seek_sequence;
1285 return STATE_END;