summaryrefslogtreecommitdiff
path: root/apps/plugins/mpegplayer/header.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/mpegplayer/header.c')
-rw-r--r--apps/plugins/mpegplayer/header.c261
1 files changed, 188 insertions, 73 deletions
diff --git a/apps/plugins/mpegplayer/header.c b/apps/plugins/mpegplayer/header.c
index d8d005b692..f5571835c1 100644
--- a/apps/plugins/mpegplayer/header.c
+++ b/apps/plugins/mpegplayer/header.c
@@ -20,6 +20,10 @@
20 * You should have received a copy of the GNU General Public License 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 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 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * $Id$
25 * libmpeg2 sync history:
26 * 2008-07-01 - CVS revision 1.101
23 */ 27 */
24 28
25#include "plugin.h" 29#include "plugin.h"
@@ -325,14 +329,14 @@ static int sequence_display_ext (mpeg2dec_t * mpeg2dec)
325{ 329{
326 uint8_t * buffer = mpeg2dec->chunk_start; 330 uint8_t * buffer = mpeg2dec->chunk_start;
327 mpeg2_sequence_t * sequence = &mpeg2dec->new_sequence; 331 mpeg2_sequence_t * sequence = &mpeg2dec->new_sequence;
328 uint32_t flags; 332 int x;
329 333
330 flags = (sequence->flags & ~SEQ_MASK_VIDEO_FORMAT) | 334 sequence->flags = (sequence->flags & ~SEQ_MASK_VIDEO_FORMAT) |
331 ((buffer[0] << 4) & SEQ_MASK_VIDEO_FORMAT); 335 ((buffer[0] << 4) & SEQ_MASK_VIDEO_FORMAT);
332 336
333 if (buffer[0] & 1) 337 if (buffer[0] & 1)
334 { 338 {
335 flags |= SEQ_FLAG_COLOUR_DESCRIPTION; 339 sequence->flags |= SEQ_FLAG_COLOUR_DESCRIPTION;
336 sequence->colour_primaries = buffer[1]; 340 sequence->colour_primaries = buffer[1];
337 sequence->transfer_characteristics = buffer[2]; 341 sequence->transfer_characteristics = buffer[2];
338 sequence->matrix_coefficients = buffer[3]; 342 sequence->matrix_coefficients = buffer[3];
@@ -342,15 +346,36 @@ static int sequence_display_ext (mpeg2dec_t * mpeg2dec)
342 if (!(buffer[2] & 2)) /* missing marker_bit */ 346 if (!(buffer[2] & 2)) /* missing marker_bit */
343 return 1; 347 return 1;
344 348
345 sequence->display_width = 349 x = (buffer[1] << 6) | (buffer[2] >> 2);
346 (buffer[1] << 6) | (buffer[2] >> 2); 350 if (x)
351 sequence->display_width = x;
347 352
348 sequence->display_height = 353 x = ((buffer[2] & 1) << 13) | (buffer[3] << 5) | (buffer[4] >> 3);
349 ((buffer[2] & 1) << 13) | (buffer[3] << 5) | (buffer[4] >> 3); 354 if (x)
355 sequence->display_height = x;
350 356
351 return 0; 357 return 0;
352} 358}
353 359
360static inline void simplify (unsigned int * u, unsigned int * v)
361{
362 unsigned int a, b, tmp;
363
364 a = *u;
365 b = *v;
366
367 /* find greatest common divisor */
368 while (a)
369 {
370 tmp = a;
371 a = b % tmp;
372 b = tmp;
373 }
374
375 *u /= b;
376 *v /= b;
377}
378
354static inline void finalize_sequence (mpeg2_sequence_t * sequence) 379static inline void finalize_sequence (mpeg2_sequence_t * sequence)
355{ 380{
356 int width; 381 int width;
@@ -411,9 +436,13 @@ static inline void finalize_sequence (mpeg2_sequence_t * sequence)
411 sequence->pixel_width = 32; 436 sequence->pixel_width = 32;
412 sequence->pixel_height = 27; 437 sequence->pixel_height = 27;
413 return; 438 return;
414 case 12: /* 720*480 4:3 */ 439 case 8: /* BT.601 625 lines 4:3 */
415 sequence->pixel_width = 8; 440 sequence->pixel_width = 59;
416 sequence->pixel_height = 9; 441 sequence->pixel_height = 54;
442 return;
443 case 12: /* BT.601 525 lines 4:3 */
444 sequence->pixel_width = 10;
445 sequence->pixel_height = 11;
417 return; 446 return;
418 default: 447 default:
419 height = 88 * sequence->pixel_width + 1171; 448 height = 88 * sequence->pixel_width + 1171;
@@ -424,17 +453,113 @@ static inline void finalize_sequence (mpeg2_sequence_t * sequence)
424 sequence->pixel_width = width; 453 sequence->pixel_width = width;
425 sequence->pixel_height = height; 454 sequence->pixel_height = height;
426 455
427 /* find greatest common divisor */ 456 simplify(&sequence->pixel_width, &sequence->pixel_height);
428 while (width) 457}
458
459int mpeg2_guess_aspect (const mpeg2_sequence_t * sequence,
460 unsigned int * pixel_width,
461 unsigned int * pixel_height)
462{
463 static const struct
464 {
465 unsigned int width, height;
466 } video_modes[] =
467 {
468 {720, 576}, /* 625 lines, 13.5 MHz (D1, DV, DVB, DVD) */
469 {704, 576}, /* 625 lines, 13.5 MHz (1/1 D1, DVB, DVD, 4CIF) */
470 {544, 576}, /* 625 lines, 10.125 MHz (DVB, laserdisc) */
471 {528, 576}, /* 625 lines, 10.125 MHz (3/4 D1, DVB, laserdisc) */
472 {480, 576}, /* 625 lines, 9 MHz (2/3 D1, DVB, SVCD) */
473 {352, 576}, /* 625 lines, 6.75 MHz (D2, 1/2 D1, CVD, DVB, DVD) */
474 {352, 288}, /* 625 lines, 6.75 MHz, 1 field (D4, VCD, DVB, DVD, CIF) */
475 {176, 144}, /* 625 lines, 3.375 MHz, half field (QCIF) */
476 {720, 486}, /* 525 lines, 13.5 MHz (D1) */
477 {704, 486}, /* 525 lines, 13.5 MHz */
478 {720, 480}, /* 525 lines, 13.5 MHz (DV, DSS, DVD) */
479 {704, 480}, /* 525 lines, 13.5 MHz (1/1 D1, ATSC, DVD) */
480 {544, 480}, /* 525 lines. 10.125 MHz (DSS, laserdisc) */
481 {528, 480}, /* 525 lines. 10.125 MHz (3/4 D1, laserdisc) */
482 {480, 480}, /* 525 lines, 9 MHz (2/3 D1, SVCD) */
483 {352, 480}, /* 525 lines, 6.75 MHz (D2, 1/2 D1, CVD, DVD) */
484 {352, 240} /* 525 lines. 6.75 MHz, 1 field (D4, VCD, DSS, DVD) */
485 };
486 unsigned int width, height, pix_width, pix_height, i, DAR_16_9;
487
488 *pixel_width = sequence->pixel_width;
489 *pixel_height = sequence->pixel_height;
490 width = sequence->picture_width;
491 height = sequence->picture_height;
492
493 for (i = 0; i < sizeof (video_modes) / sizeof (video_modes[0]); i++)
494 {
495 if (width == video_modes[i].width && height == video_modes[i].height)
496 break;
497 }
498
499 if (i == ARRAYLEN(video_modes) ||
500 (sequence->pixel_width == 1 && sequence->pixel_height == 1) ||
501 width != sequence->display_width || height != sequence->display_height)
502 {
503 return 0;
504 }
505
506 for (pix_height = 1; height * pix_height < 480; pix_height <<= 1);
507 height *= pix_height;
508
509 for (pix_width = 1; width * pix_width <= 352; pix_width <<= 1);
510 width *= pix_width;
511
512 if (!(sequence->flags & SEQ_FLAG_MPEG2))
429 { 513 {
430 int tmp = width; 514 static unsigned int mpeg1_check[2][2] = {{11, 54}, {27, 45}};
431 width = height % tmp; 515 DAR_16_9 = (sequence->pixel_height == 27 ||
432 height = tmp; 516 sequence->pixel_height == 45);
517 if (width < 704 ||
518 sequence->pixel_height != mpeg1_check[DAR_16_9][height == 576])
519 return 0;
520 }
521 else
522 {
523 DAR_16_9 = (3 * sequence->picture_width * sequence->pixel_width >
524 4 * sequence->picture_height * sequence->pixel_height);
525 switch (width)
526 {
527 case 528:
528 case 544:
529 pix_width *= 4;
530 pix_height *= 3;
531 break;
532 case 480:
533 pix_width *= 3;
534 pix_height *= 2;
535 break;
536 }
433 } 537 }
434 538
435 sequence->pixel_width /= height; 539 if (DAR_16_9)
436 sequence->pixel_height /= height; 540 {
437} 541 pix_width *= 4;
542 pix_height *= 3;
543 }
544
545 if (height == 576)
546 {
547 pix_width *= 59;
548 pix_height *= 54;
549 }
550 else
551 {
552 pix_width *= 10;
553 pix_height *= 11;
554 }
555
556 *pixel_width = pix_width;
557 *pixel_height = pix_height;
558
559 simplify (pixel_width, pixel_height);
560
561 return (height == 576) ? 1 : 2;
562}
438 563
439static void copy_matrix (mpeg2dec_t * mpeg2dec, int index) 564static void copy_matrix (mpeg2dec_t * mpeg2dec, int index)
440{ 565{
@@ -506,36 +631,33 @@ void mpeg2_header_sequence_finalize (mpeg2dec_t * mpeg2dec)
506 631
507 if (mpeg2dec->sequence.width != (unsigned)-1) 632 if (mpeg2dec->sequence.width != (unsigned)-1)
508 { 633 {
509 unsigned int new_byte_rate;
510
511 /* 634 /*
512 * According to 6.1.1.6, repeat sequence headers should be 635 * According to 6.1.1.6, repeat sequence headers should be
513 * identical to the original. However some DVDs dont respect 636 * identical to the original. However some encoders don't
514 * that and have different bitrates in the repeat sequence 637 * respect that and change various fields (including bitrate
515 * headers. So we'll ignore that in the comparison and still 638 * and aspect ratio) in the repeat sequence headers. So we
516 * consider these as repeat sequence headers. 639 * choose to be as conservative as possible and only restart
517 * 640 * the decoder if the width, height, chroma_width,
518 * However, be careful not to alter the current sequence when 641 * chroma_height or low_delay flag are modified.
519 * returning STATE_INVALID_END.
520 */ 642 */
521 new_byte_rate = sequence->byte_rate; 643 if (sequence->width != mpeg2dec->sequence.width ||
522 sequence->byte_rate = mpeg2dec->sequence.byte_rate; 644 sequence->height != mpeg2dec->sequence.height ||
523 645 sequence->chroma_width != mpeg2dec->sequence.chroma_width ||
524 if (rb->memcmp(&mpeg2dec->sequence, sequence, 646 sequence->chroma_height != mpeg2dec->sequence.chroma_height ||
525 sizeof (mpeg2_sequence_t))) 647 ((sequence->flags ^ mpeg2dec->sequence.flags) &
648 SEQ_FLAG_LOW_DELAY))
526 { 649 {
527 decoder->stride_frame = sequence->width; 650 decoder->stride_frame = sequence->width;
528 sequence->byte_rate = new_byte_rate;
529
530 mpeg2_header_end (mpeg2dec); 651 mpeg2_header_end (mpeg2dec);
531
532 mpeg2dec->action = invalid_end_action; 652 mpeg2dec->action = invalid_end_action;
533 mpeg2dec->state = STATE_INVALID_END; 653 mpeg2dec->state = STATE_INVALID_END;
534 return; 654 return;
535 } 655 }
536 656
537 sequence->byte_rate = new_byte_rate; 657 mpeg2dec->state = rb->memcmp(&mpeg2dec->sequence, sequence,
538 mpeg2dec->state = STATE_SEQUENCE_REPEATED; 658 sizeof (mpeg2_sequence_t)) ?
659 STATE_SEQUENCE_MODIFIED :
660 STATE_SEQUENCE_REPEATED;
539 } 661 }
540 else 662 else
541 { 663 {
@@ -601,70 +723,61 @@ void mpeg2_set_fbuf (mpeg2dec_t * mpeg2dec, int b_type)
601 } 723 }
602} 724}
603 725
604mpeg2_state_t mpeg2_header_picture_start (mpeg2dec_t * mpeg2dec) 726int mpeg2_header_picture (mpeg2dec_t * mpeg2dec)
605{ 727{
728 uint8_t * buffer = mpeg2dec->chunk_start;
606 mpeg2_picture_t * picture = &mpeg2dec->new_picture; 729 mpeg2_picture_t * picture = &mpeg2dec->new_picture;
730 mpeg2_decoder_t * decoder = &mpeg2dec->decoder;
731 int type;
607 732
608 mpeg2dec->state = (mpeg2dec->state != STATE_SLICE_1ST) ? 733 mpeg2dec->state = (mpeg2dec->state != STATE_SLICE_1ST) ?
609 STATE_PICTURE : STATE_PICTURE_2ND; 734 STATE_PICTURE : STATE_PICTURE_2ND;
610 picture->flags = 0; 735 mpeg2dec->ext_state = PIC_CODING_EXT;
736
737 picture->temporal_reference = (buffer[0] << 2) | (buffer[1] >> 6);
738
739 type = (buffer [1] >> 3) & 7;
740
741 if (type == PIC_FLAG_CODING_TYPE_P || type == PIC_FLAG_CODING_TYPE_B)
742 {
743 /* forward_f_code and backward_f_code - used in mpeg1 only */
744 decoder->f_motion.f_code[1] = (buffer[3] >> 2) & 1;
745 decoder->f_motion.f_code[0] =
746 (((buffer[3] << 1) | (buffer[4] >> 7)) & 7) - 1;
747 decoder->b_motion.f_code[1] = (buffer[4] >> 6) & 1;
748 decoder->b_motion.f_code[0] = ((buffer[4] >> 3) & 7) - 1;
749 }
750
751 picture->flags = PIC_FLAG_PROGRESSIVE_FRAME | type;
611 picture->tag = picture->tag2 = 0; 752 picture->tag = picture->tag2 = 0;
612 753
613 if (mpeg2dec->num_tags) 754 if (mpeg2dec->num_tags)
614 { 755 {
615 if (mpeg2dec->bytes_since_tag >= 4) 756 if (mpeg2dec->bytes_since_tag >= mpeg2dec->chunk_ptr - buffer + 4)
616 { 757 {
617 mpeg2dec->num_tags = 0; 758 mpeg2dec->num_tags = 0;
618 picture->tag = mpeg2dec->tag_current; 759 picture->tag = mpeg2dec->tag_current;
619 picture->tag2 = mpeg2dec->tag2_current; 760 picture->tag2 = mpeg2dec->tag2_current;
620 picture->flags = PIC_FLAG_TAGS; 761 picture->flags |= PIC_FLAG_TAGS;
621 } 762 }
622 else if (mpeg2dec->num_tags > 1) 763 else if (mpeg2dec->num_tags > 1)
623 { 764 {
624 mpeg2dec->num_tags = 1; 765 mpeg2dec->num_tags = 1;
625 picture->tag = mpeg2dec->tag_previous; 766 picture->tag = mpeg2dec->tag_previous;
626 picture->tag2 = mpeg2dec->tag2_previous; 767 picture->tag2 = mpeg2dec->tag2_previous;
627 picture->flags = PIC_FLAG_TAGS; 768 picture->flags |= PIC_FLAG_TAGS;
628 } 769 }
629 } 770 }
630 771
772 picture->nb_fields = 2;
631 picture->display_offset[0].x = picture->display_offset[1].x = 773 picture->display_offset[0].x = picture->display_offset[1].x =
632 picture->display_offset[2].x = mpeg2dec->display_offset_x; 774 picture->display_offset[2].x = mpeg2dec->display_offset_x;
633 775
634 picture->display_offset[0].y = picture->display_offset[1].y = 776 picture->display_offset[0].y = picture->display_offset[1].y =
635 picture->display_offset[2].y = mpeg2dec->display_offset_y; 777 picture->display_offset[2].y = mpeg2dec->display_offset_y;
636 778
637 return mpeg2_parse_header (mpeg2dec);
638}
639
640int mpeg2_header_picture (mpeg2dec_t * mpeg2dec)
641{
642 uint8_t * buffer = mpeg2dec->chunk_start;
643 mpeg2_picture_t * picture = &mpeg2dec->new_picture;
644 mpeg2_decoder_t * decoder = &mpeg2dec->decoder;
645 int type;
646
647 type = (buffer [1] >> 3) & 7;
648 mpeg2dec->ext_state = PIC_CODING_EXT;
649
650 picture->temporal_reference = (buffer[0] << 2) | (buffer[1] >> 6);
651
652 picture->flags |= type;
653
654 if (type == PIC_FLAG_CODING_TYPE_P || type == PIC_FLAG_CODING_TYPE_B)
655 {
656 /* forward_f_code and backward_f_code - used in mpeg1 only */
657 decoder->f_motion.f_code[1] = (buffer[3] >> 2) & 1;
658 decoder->f_motion.f_code[0] =
659 (((buffer[3] << 1) | (buffer[4] >> 7)) & 7) - 1;
660 decoder->b_motion.f_code[1] = (buffer[4] >> 6) & 1;
661 decoder->b_motion.f_code[0] = ((buffer[4] >> 3) & 7) - 1;
662 }
663
664 /* XXXXXX decode extra_information_picture as well */ 779 /* XXXXXX decode extra_information_picture as well */
665 780
666 picture->nb_fields = 2;
667
668 mpeg2dec->q_scale_type = 0; 781 mpeg2dec->q_scale_type = 0;
669 decoder->intra_dc_precision = 7; 782 decoder->intra_dc_precision = 7;
670 decoder->frame_pred_frame_dct = 1; 783 decoder->frame_pred_frame_dct = 1;
@@ -722,7 +835,9 @@ static int picture_coding_ext (mpeg2dec_t * mpeg2dec)
722 mpeg2dec->q_scale_type = buffer[3] & 16; 835 mpeg2dec->q_scale_type = buffer[3] & 16;
723 decoder->intra_vlc_format = (buffer[3] >> 3) & 1; 836 decoder->intra_vlc_format = (buffer[3] >> 3) & 1;
724 decoder->scan = (buffer[3] & 4) ? mpeg2_scan_alt : mpeg2_scan_norm; 837 decoder->scan = (buffer[3] & 4) ? mpeg2_scan_alt : mpeg2_scan_norm;
725 flags |= (buffer[4] & 0x80) ? PIC_FLAG_PROGRESSIVE_FRAME : 0; 838
839 if (!(buffer[4] & 0x80))
840 flags &= ~PIC_FLAG_PROGRESSIVE_FRAME;
726 841
727 if (buffer[4] & 0x40) 842 if (buffer[4] & 0x40)
728 { 843 {
@@ -1120,7 +1235,7 @@ mpeg2_state_t mpeg2_header_slice_start (mpeg2dec_t * mpeg2dec)
1120 1235
1121 mpeg2dec->action = NULL; 1236 mpeg2dec->action = NULL;
1122 1237
1123 return (mpeg2_state_t)-1; 1238 return STATE_INTERNAL_NORETURN;
1124} 1239}
1125 1240
1126static mpeg2_state_t seek_sequence (mpeg2dec_t * mpeg2dec) 1241static mpeg2_state_t seek_sequence (mpeg2dec_t * mpeg2dec)