2006-08-07 22:11:07 +00:00
|
|
|
/*
|
|
|
|
* header.c
|
|
|
|
* Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
|
|
|
|
* Copyright (C) 2003 Regis Duchesne <hpreg@zoy.org>
|
|
|
|
* Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
|
|
|
|
*
|
|
|
|
* This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
|
|
|
|
* See http://libmpeg2.sourceforge.net/ for updates.
|
|
|
|
*
|
|
|
|
* mpeg2dec is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* mpeg2dec is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2008-07-01 10:27:48 +00:00
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
* libmpeg2 sync history:
|
|
|
|
* 2008-07-01 - CVS revision 1.101
|
2006-08-07 22:11:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
|
|
|
|
#include "mpeg2dec_config.h"
|
|
|
|
|
2008-05-13 09:57:56 +00:00
|
|
|
extern const struct plugin_api* rb;
|
2006-08-07 22:11:07 +00:00
|
|
|
|
|
|
|
#include "mpeg2.h"
|
|
|
|
#include "attributes.h"
|
|
|
|
#include "mpeg2_internal.h"
|
|
|
|
|
|
|
|
#define SEQ_EXT 2
|
|
|
|
#define SEQ_DISPLAY_EXT 4
|
|
|
|
#define QUANT_MATRIX_EXT 8
|
|
|
|
#define COPYRIGHT_EXT 0x10
|
|
|
|
#define PIC_DISPLAY_EXT 0x80
|
|
|
|
#define PIC_CODING_EXT 0x100
|
|
|
|
|
|
|
|
/* default intra quant matrix, in zig-zag order */
|
2007-12-07 13:00:31 +00:00
|
|
|
static const uint8_t default_intra_quantizer_matrix[64] =
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
8,
|
|
|
|
16, 16,
|
|
|
|
19, 16, 19,
|
|
|
|
22, 22, 22, 22,
|
|
|
|
22, 22, 26, 24, 26,
|
|
|
|
27, 27, 27, 26, 26, 26,
|
|
|
|
26, 27, 27, 27, 29, 29, 29,
|
|
|
|
34, 34, 34, 29, 29, 29, 27, 27,
|
|
|
|
29, 29, 32, 32, 34, 34, 37,
|
|
|
|
38, 37, 35, 35, 34, 35,
|
|
|
|
38, 38, 40, 40, 40,
|
|
|
|
48, 48, 46, 46,
|
|
|
|
56, 56, 58,
|
|
|
|
69, 69,
|
|
|
|
83
|
|
|
|
};
|
|
|
|
|
2007-12-07 13:00:31 +00:00
|
|
|
const uint8_t default_mpeg2_scan_norm[64] =
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
/* Zig-Zag scan pattern */
|
2007-12-07 13:00:31 +00:00
|
|
|
0, 1, 8, 16, 9, 2, 3, 10,
|
|
|
|
17, 24, 32, 25, 18, 11, 4, 5,
|
|
|
|
12, 19, 26, 33, 40, 48, 41, 34,
|
|
|
|
27, 20, 13, 6, 7, 14, 21, 28,
|
|
|
|
35, 42, 49, 56, 57, 50, 43, 36,
|
|
|
|
29, 22, 15, 23, 30, 37, 44, 51,
|
|
|
|
58, 59, 52, 45, 38, 31, 39, 46,
|
|
|
|
53, 60, 61, 54, 47, 55, 62, 63
|
2006-08-07 22:11:07 +00:00
|
|
|
};
|
|
|
|
|
2007-12-07 13:00:31 +00:00
|
|
|
const uint8_t default_mpeg2_scan_alt[64] =
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
/* Alternate scan pattern */
|
2007-12-07 13:00:31 +00:00
|
|
|
0, 8, 16, 24, 1, 9, 2, 10,
|
|
|
|
17, 25, 32, 40, 48, 56, 57, 49,
|
|
|
|
41, 33, 26, 18, 3, 11, 4, 12,
|
|
|
|
19, 27, 34, 42, 50, 58, 35, 43,
|
|
|
|
51, 59, 20, 28, 5, 13, 6, 14,
|
|
|
|
21, 29, 36, 44, 52, 60, 37, 45,
|
|
|
|
53, 61, 22, 30, 7, 15, 23, 31,
|
|
|
|
38, 46, 54, 62, 39, 47, 55, 63
|
2006-08-07 22:11:07 +00:00
|
|
|
};
|
|
|
|
|
2007-10-09 20:42:20 +00:00
|
|
|
uint8_t mpeg2_scan_norm[64] IDATA_ATTR;
|
|
|
|
uint8_t mpeg2_scan_alt[64] IDATA_ATTR;
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
void mpeg2_header_state_init (mpeg2dec_t * mpeg2dec)
|
|
|
|
{
|
2007-12-07 13:00:31 +00:00
|
|
|
if (mpeg2dec->sequence.width != (unsigned)-1)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->sequence.width = (unsigned)-1;
|
2007-12-29 19:46:35 +00:00
|
|
|
mpeg2_mem_reset(); /* Clean the memory slate */
|
|
|
|
#if 0
|
2006-08-07 22:11:07 +00:00
|
|
|
if (!mpeg2dec->custom_fbuf)
|
2007-12-07 13:00:31 +00:00
|
|
|
{
|
2007-12-29 19:46:35 +00:00
|
|
|
int i;
|
2006-08-07 22:11:07 +00:00
|
|
|
for (i = mpeg2dec->alloc_index_user;
|
2007-12-07 13:00:31 +00:00
|
|
|
i < mpeg2dec->alloc_index; i++)
|
|
|
|
{
|
|
|
|
mpeg2_free(mpeg2dec->fbuf_alloc[i].fbuf.buf[0]);
|
2008-01-16 01:22:56 +00:00
|
|
|
#if MPEG2_COLOR
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2_free(mpeg2dec->fbuf_alloc[i].fbuf.buf[1]);
|
|
|
|
mpeg2_free(mpeg2dec->fbuf_alloc[i].fbuf.buf[2]);
|
2008-01-16 01:22:56 +00:00
|
|
|
#endif
|
2006-08-07 22:11:07 +00:00
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
if (mpeg2dec->convert_start)
|
2007-12-07 13:00:31 +00:00
|
|
|
{
|
2007-12-29 19:46:35 +00:00
|
|
|
int i;
|
2007-12-07 13:00:31 +00:00
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
mpeg2_free(mpeg2dec->yuv_buf[i][0]);
|
2008-01-16 01:22:56 +00:00
|
|
|
#if MPEG2_COLOR
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2_free(mpeg2dec->yuv_buf[i][1]);
|
|
|
|
mpeg2_free(mpeg2dec->yuv_buf[i][2]);
|
2008-01-16 01:22:56 +00:00
|
|
|
#endif
|
2006-08-07 22:11:07 +00:00
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
if (mpeg2dec->decoder.convert_id)
|
2007-12-07 13:00:31 +00:00
|
|
|
{
|
|
|
|
mpeg2_free(mpeg2dec->decoder.convert_id);
|
|
|
|
}
|
2007-12-29 19:46:35 +00:00
|
|
|
#endif
|
2006-08-07 22:11:07 +00:00
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->decoder.coding_type = I_TYPE;
|
|
|
|
mpeg2dec->decoder.convert = NULL;
|
|
|
|
mpeg2dec->decoder.convert_id = NULL;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->picture = mpeg2dec->pictures;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->fbuf[0] = &mpeg2dec->fbuf_alloc[0].fbuf;
|
|
|
|
mpeg2dec->fbuf[1] = &mpeg2dec->fbuf_alloc[1].fbuf;
|
|
|
|
mpeg2dec->fbuf[2] = &mpeg2dec->fbuf_alloc[2].fbuf;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->first = 1;
|
|
|
|
mpeg2dec->alloc_index = 0;
|
|
|
|
mpeg2dec->alloc_index_user = 0;
|
|
|
|
mpeg2dec->first_decode_slice = 1;
|
|
|
|
mpeg2dec->nb_decode_slices = 0xb0 - 1;
|
|
|
|
mpeg2dec->convert = NULL;
|
|
|
|
mpeg2dec->convert_start = NULL;
|
|
|
|
mpeg2dec->custom_fbuf = 0;
|
|
|
|
mpeg2dec->yuv_index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mpeg2_reset_info (mpeg2_info_t * info)
|
|
|
|
{
|
2007-12-07 13:00:31 +00:00
|
|
|
info->current_picture =
|
|
|
|
info->current_picture_2nd = NULL;
|
|
|
|
|
|
|
|
info->display_picture =
|
|
|
|
info->display_picture_2nd = NULL;
|
|
|
|
|
|
|
|
info->current_fbuf =
|
|
|
|
info->display_fbuf =
|
|
|
|
info->discard_fbuf = NULL;
|
2006-08-07 22:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void info_user_data (mpeg2dec_t * mpeg2dec)
|
|
|
|
{
|
2007-12-07 13:00:31 +00:00
|
|
|
if (mpeg2dec->user_data_len)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->info.user_data = mpeg2dec->chunk_buffer;
|
|
|
|
mpeg2dec->info.user_data_len = mpeg2dec->user_data_len - 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int mpeg2_header_sequence (mpeg2dec_t * mpeg2dec)
|
|
|
|
{
|
2007-12-07 13:00:31 +00:00
|
|
|
static const unsigned int frame_period[16] =
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
0, 1126125, 1125000, 1080000, 900900, 900000, 540000, 450450, 450000,
|
|
|
|
/* unofficial: xing 15 fps */
|
|
|
|
1800000,
|
|
|
|
/* unofficial: libmpeg3 "Unofficial economy rates" 5/10/12/15 fps */
|
|
|
|
5400000, 2700000, 2250000, 1800000, 0, 0
|
|
|
|
};
|
2007-12-07 13:00:31 +00:00
|
|
|
|
|
|
|
uint8_t * buffer = mpeg2dec->chunk_start;
|
|
|
|
mpeg2_sequence_t * sequence = &mpeg2dec->new_sequence;
|
2006-08-07 22:11:07 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if ((buffer[6] & 0x20) != 0x20) /* missing marker_bit */
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
i = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2];
|
2007-12-07 13:00:31 +00:00
|
|
|
|
|
|
|
if (!(sequence->display_width = sequence->picture_width = i >> 12))
|
2006-08-07 22:11:07 +00:00
|
|
|
return 1;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
|
|
|
if (!(sequence->display_height = sequence->picture_height = i & 0xfff))
|
2006-08-07 22:11:07 +00:00
|
|
|
return 1;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
sequence->width = (sequence->picture_width + 15) & ~15;
|
|
|
|
sequence->height = (sequence->picture_height + 15) & ~15;
|
|
|
|
sequence->chroma_width = sequence->width >> 1;
|
|
|
|
sequence->chroma_height = sequence->height >> 1;
|
|
|
|
|
2007-12-07 13:00:31 +00:00
|
|
|
sequence->flags = SEQ_FLAG_PROGRESSIVE_SEQUENCE |
|
|
|
|
SEQ_VIDEO_FORMAT_UNSPECIFIED;
|
2006-08-07 22:11:07 +00:00
|
|
|
|
|
|
|
sequence->pixel_width = buffer[3] >> 4; /* aspect ratio */
|
|
|
|
sequence->frame_period = frame_period[buffer[3] & 15];
|
|
|
|
|
|
|
|
sequence->byte_rate = (buffer[4]<<10) | (buffer[5]<<2) | (buffer[6]>>6);
|
|
|
|
|
2007-12-07 13:00:31 +00:00
|
|
|
sequence->vbv_buffer_size = ((buffer[6]<<16) | (buffer[7]<<8)) & 0x1ff800;
|
2006-08-07 22:11:07 +00:00
|
|
|
|
|
|
|
if (buffer[7] & 4)
|
|
|
|
sequence->flags |= SEQ_FLAG_CONSTRAINED_PARAMETERS;
|
|
|
|
|
|
|
|
mpeg2dec->copy_matrix = 3;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
|
|
|
if (buffer[7] & 2)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
for (i = 0; i < 64; i++)
|
2007-12-07 13:00:31 +00:00
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->new_quantizer_matrix[0][mpeg2_scan_norm[i]] =
|
|
|
|
(buffer[i+7] << 7) | (buffer[i+8] >> 1);
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
buffer += 64;
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
for (i = 0; i < 64; i++)
|
2007-12-07 13:00:31 +00:00
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->new_quantizer_matrix[0][mpeg2_scan_norm[i]] =
|
|
|
|
default_intra_quantizer_matrix[i];
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-07 22:11:07 +00:00
|
|
|
|
|
|
|
if (buffer[7] & 1)
|
2007-12-07 13:00:31 +00:00
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
for (i = 0; i < 64; i++)
|
2007-12-07 13:00:31 +00:00
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->new_quantizer_matrix[1][mpeg2_scan_norm[i]] =
|
|
|
|
buffer[i+8];
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-07 22:11:07 +00:00
|
|
|
else
|
2007-12-07 13:00:31 +00:00
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
rb->memset (mpeg2dec->new_quantizer_matrix[1], 16, 64);
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
2006-08-07 22:11:07 +00:00
|
|
|
|
|
|
|
sequence->profile_level_id = 0x80;
|
|
|
|
sequence->colour_primaries = 0;
|
|
|
|
sequence->transfer_characteristics = 0;
|
|
|
|
sequence->matrix_coefficients = 0;
|
|
|
|
|
|
|
|
mpeg2dec->ext_state = SEQ_EXT;
|
|
|
|
mpeg2dec->state = STATE_SEQUENCE;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
|
|
|
mpeg2dec->display_offset_x =
|
|
|
|
mpeg2dec->display_offset_y = 0;
|
2006-08-07 22:11:07 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sequence_ext (mpeg2dec_t * mpeg2dec)
|
|
|
|
{
|
|
|
|
uint8_t * buffer = mpeg2dec->chunk_start;
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2_sequence_t * sequence = &mpeg2dec->new_sequence;
|
2006-08-07 22:11:07 +00:00
|
|
|
uint32_t flags;
|
|
|
|
|
|
|
|
if (!(buffer[3] & 1))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
sequence->profile_level_id = (buffer[0] << 4) | (buffer[1] >> 4);
|
|
|
|
|
2007-12-07 13:00:31 +00:00
|
|
|
sequence->picture_width += ((buffer[1] << 13) | (buffer[2] << 5)) & 0x3000;
|
|
|
|
sequence->display_width = sequence->picture_width;
|
|
|
|
|
|
|
|
sequence->picture_height += (buffer[2] << 7) & 0x3000;
|
|
|
|
sequence->display_height = sequence->picture_height;
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
sequence->width = (sequence->picture_width + 15) & ~15;
|
|
|
|
sequence->height = (sequence->picture_height + 15) & ~15;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
flags = sequence->flags | SEQ_FLAG_MPEG2;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
|
|
|
if (!(buffer[1] & 8))
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
flags &= ~SEQ_FLAG_PROGRESSIVE_SEQUENCE;
|
|
|
|
sequence->height = (sequence->height + 31) & ~31;
|
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
if (buffer[5] & 0x80)
|
|
|
|
flags |= SEQ_FLAG_LOW_DELAY;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
sequence->flags = flags;
|
|
|
|
sequence->chroma_width = sequence->width;
|
|
|
|
sequence->chroma_height = sequence->height;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
|
|
|
switch (buffer[1] & 6)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
case 0: /* invalid */
|
|
|
|
return 1;
|
|
|
|
case 2: /* 4:2:0 */
|
|
|
|
sequence->chroma_height >>= 1;
|
|
|
|
case 4: /* 4:2:2 */
|
|
|
|
sequence->chroma_width >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sequence->byte_rate += ((buffer[2]<<25) | (buffer[3]<<17)) & 0x3ffc0000;
|
|
|
|
|
|
|
|
sequence->vbv_buffer_size |= buffer[4] << 21;
|
|
|
|
|
|
|
|
sequence->frame_period =
|
|
|
|
sequence->frame_period * ((buffer[5]&31)+1) / (((buffer[5]>>2)&3)+1);
|
|
|
|
|
|
|
|
mpeg2dec->ext_state = SEQ_DISPLAY_EXT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sequence_display_ext (mpeg2dec_t * mpeg2dec)
|
|
|
|
{
|
|
|
|
uint8_t * buffer = mpeg2dec->chunk_start;
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2_sequence_t * sequence = &mpeg2dec->new_sequence;
|
2008-07-01 10:27:48 +00:00
|
|
|
int x;
|
2006-08-07 22:11:07 +00:00
|
|
|
|
2008-07-01 10:27:48 +00:00
|
|
|
sequence->flags = (sequence->flags & ~SEQ_MASK_VIDEO_FORMAT) |
|
2007-12-07 13:00:31 +00:00
|
|
|
((buffer[0] << 4) & SEQ_MASK_VIDEO_FORMAT);
|
|
|
|
|
|
|
|
if (buffer[0] & 1)
|
|
|
|
{
|
2008-07-01 10:27:48 +00:00
|
|
|
sequence->flags |= SEQ_FLAG_COLOUR_DESCRIPTION;
|
2006-08-07 22:11:07 +00:00
|
|
|
sequence->colour_primaries = buffer[1];
|
|
|
|
sequence->transfer_characteristics = buffer[2];
|
|
|
|
sequence->matrix_coefficients = buffer[3];
|
|
|
|
buffer += 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(buffer[2] & 2)) /* missing marker_bit */
|
|
|
|
return 1;
|
|
|
|
|
2008-07-01 10:27:48 +00:00
|
|
|
x = (buffer[1] << 6) | (buffer[2] >> 2);
|
|
|
|
if (x)
|
|
|
|
sequence->display_width = x;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2008-07-01 10:27:48 +00:00
|
|
|
x = ((buffer[2] & 1) << 13) | (buffer[3] << 5) | (buffer[4] >> 3);
|
|
|
|
if (x)
|
|
|
|
sequence->display_height = x;
|
2006-08-07 22:11:07 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-01 10:27:48 +00:00
|
|
|
static inline void simplify (unsigned int * u, unsigned int * v)
|
|
|
|
{
|
|
|
|
unsigned int a, b, tmp;
|
|
|
|
|
|
|
|
a = *u;
|
|
|
|
b = *v;
|
|
|
|
|
|
|
|
/* find greatest common divisor */
|
|
|
|
while (a)
|
|
|
|
{
|
|
|
|
tmp = a;
|
|
|
|
a = b % tmp;
|
|
|
|
b = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
*u /= b;
|
|
|
|
*v /= b;
|
|
|
|
}
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
static inline void finalize_sequence (mpeg2_sequence_t * sequence)
|
|
|
|
{
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
|
|
|
|
sequence->byte_rate *= 50;
|
|
|
|
|
2007-12-07 13:00:31 +00:00
|
|
|
if (sequence->flags & SEQ_FLAG_MPEG2)
|
|
|
|
{
|
|
|
|
switch (sequence->pixel_width)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
case 1: /* square pixels */
|
2007-12-07 13:00:31 +00:00
|
|
|
sequence->pixel_width =
|
|
|
|
sequence->pixel_height = 1;
|
|
|
|
return;
|
2006-08-07 22:11:07 +00:00
|
|
|
case 2: /* 4:3 aspect ratio */
|
2007-12-07 13:00:31 +00:00
|
|
|
width = 4;
|
|
|
|
height = 3;
|
|
|
|
break;
|
2006-08-07 22:11:07 +00:00
|
|
|
case 3: /* 16:9 aspect ratio */
|
2007-12-07 13:00:31 +00:00
|
|
|
width = 16;
|
|
|
|
height = 9;
|
|
|
|
break;
|
2006-08-07 22:11:07 +00:00
|
|
|
case 4: /* 2.21:1 aspect ratio */
|
2007-12-07 13:00:31 +00:00
|
|
|
width = 221;
|
|
|
|
height = 100;
|
|
|
|
break;
|
2006-08-07 22:11:07 +00:00
|
|
|
default: /* illegal */
|
2007-12-07 13:00:31 +00:00
|
|
|
sequence->pixel_width =
|
|
|
|
sequence->pixel_height = 0;
|
|
|
|
return;
|
2006-08-07 22:11:07 +00:00
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
width *= sequence->display_height;
|
|
|
|
height *= sequence->display_width;
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (sequence->byte_rate == 50 * 0x3ffff)
|
|
|
|
sequence->byte_rate = 0; /* mpeg-1 VBR */
|
|
|
|
|
|
|
|
switch (sequence->pixel_width)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case 15: /* illegal */
|
|
|
|
sequence->pixel_width =
|
|
|
|
sequence->pixel_height = 0;
|
|
|
|
return;
|
2006-08-07 22:11:07 +00:00
|
|
|
case 1: /* square pixels */
|
2007-12-07 13:00:31 +00:00
|
|
|
sequence->pixel_width =
|
|
|
|
sequence->pixel_height = 1;
|
|
|
|
return;
|
2006-08-07 22:11:07 +00:00
|
|
|
case 3: /* 720x576 16:9 */
|
2007-12-07 13:00:31 +00:00
|
|
|
sequence->pixel_width = 64;
|
|
|
|
sequence->pixel_height = 45;
|
|
|
|
return;
|
2006-08-07 22:11:07 +00:00
|
|
|
case 6: /* 720x480 16:9 */
|
2007-12-07 13:00:31 +00:00
|
|
|
sequence->pixel_width = 32;
|
|
|
|
sequence->pixel_height = 27;
|
|
|
|
return;
|
2008-07-01 10:27:48 +00:00
|
|
|
case 8: /* BT.601 625 lines 4:3 */
|
|
|
|
sequence->pixel_width = 59;
|
|
|
|
sequence->pixel_height = 54;
|
|
|
|
return;
|
|
|
|
case 12: /* BT.601 525 lines 4:3 */
|
|
|
|
sequence->pixel_width = 10;
|
|
|
|
sequence->pixel_height = 11;
|
2007-12-07 13:00:31 +00:00
|
|
|
return;
|
2006-08-07 22:11:07 +00:00
|
|
|
default:
|
|
|
|
height = 88 * sequence->pixel_width + 1171;
|
|
|
|
width = 2000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sequence->pixel_width = width;
|
|
|
|
sequence->pixel_height = height;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2008-07-01 10:27:48 +00:00
|
|
|
simplify(&sequence->pixel_width, &sequence->pixel_height);
|
|
|
|
}
|
|
|
|
|
|
|
|
int mpeg2_guess_aspect (const mpeg2_sequence_t * sequence,
|
|
|
|
unsigned int * pixel_width,
|
|
|
|
unsigned int * pixel_height)
|
|
|
|
{
|
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
unsigned int width, height;
|
|
|
|
} video_modes[] =
|
|
|
|
{
|
|
|
|
{720, 576}, /* 625 lines, 13.5 MHz (D1, DV, DVB, DVD) */
|
|
|
|
{704, 576}, /* 625 lines, 13.5 MHz (1/1 D1, DVB, DVD, 4CIF) */
|
|
|
|
{544, 576}, /* 625 lines, 10.125 MHz (DVB, laserdisc) */
|
|
|
|
{528, 576}, /* 625 lines, 10.125 MHz (3/4 D1, DVB, laserdisc) */
|
|
|
|
{480, 576}, /* 625 lines, 9 MHz (2/3 D1, DVB, SVCD) */
|
|
|
|
{352, 576}, /* 625 lines, 6.75 MHz (D2, 1/2 D1, CVD, DVB, DVD) */
|
|
|
|
{352, 288}, /* 625 lines, 6.75 MHz, 1 field (D4, VCD, DVB, DVD, CIF) */
|
|
|
|
{176, 144}, /* 625 lines, 3.375 MHz, half field (QCIF) */
|
|
|
|
{720, 486}, /* 525 lines, 13.5 MHz (D1) */
|
|
|
|
{704, 486}, /* 525 lines, 13.5 MHz */
|
|
|
|
{720, 480}, /* 525 lines, 13.5 MHz (DV, DSS, DVD) */
|
|
|
|
{704, 480}, /* 525 lines, 13.5 MHz (1/1 D1, ATSC, DVD) */
|
|
|
|
{544, 480}, /* 525 lines. 10.125 MHz (DSS, laserdisc) */
|
|
|
|
{528, 480}, /* 525 lines. 10.125 MHz (3/4 D1, laserdisc) */
|
|
|
|
{480, 480}, /* 525 lines, 9 MHz (2/3 D1, SVCD) */
|
|
|
|
{352, 480}, /* 525 lines, 6.75 MHz (D2, 1/2 D1, CVD, DVD) */
|
|
|
|
{352, 240} /* 525 lines. 6.75 MHz, 1 field (D4, VCD, DSS, DVD) */
|
|
|
|
};
|
|
|
|
unsigned int width, height, pix_width, pix_height, i, DAR_16_9;
|
|
|
|
|
|
|
|
*pixel_width = sequence->pixel_width;
|
|
|
|
*pixel_height = sequence->pixel_height;
|
|
|
|
width = sequence->picture_width;
|
|
|
|
height = sequence->picture_height;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof (video_modes) / sizeof (video_modes[0]); i++)
|
|
|
|
{
|
|
|
|
if (width == video_modes[i].width && height == video_modes[i].height)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == ARRAYLEN(video_modes) ||
|
|
|
|
(sequence->pixel_width == 1 && sequence->pixel_height == 1) ||
|
|
|
|
width != sequence->display_width || height != sequence->display_height)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (pix_height = 1; height * pix_height < 480; pix_height <<= 1);
|
|
|
|
height *= pix_height;
|
|
|
|
|
|
|
|
for (pix_width = 1; width * pix_width <= 352; pix_width <<= 1);
|
|
|
|
width *= pix_width;
|
|
|
|
|
|
|
|
if (!(sequence->flags & SEQ_FLAG_MPEG2))
|
2007-12-07 13:00:31 +00:00
|
|
|
{
|
2008-07-01 10:27:48 +00:00
|
|
|
static unsigned int mpeg1_check[2][2] = {{11, 54}, {27, 45}};
|
|
|
|
DAR_16_9 = (sequence->pixel_height == 27 ||
|
|
|
|
sequence->pixel_height == 45);
|
|
|
|
if (width < 704 ||
|
|
|
|
sequence->pixel_height != mpeg1_check[DAR_16_9][height == 576])
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DAR_16_9 = (3 * sequence->picture_width * sequence->pixel_width >
|
|
|
|
4 * sequence->picture_height * sequence->pixel_height);
|
|
|
|
switch (width)
|
|
|
|
{
|
|
|
|
case 528:
|
|
|
|
case 544:
|
|
|
|
pix_width *= 4;
|
|
|
|
pix_height *= 3;
|
|
|
|
break;
|
|
|
|
case 480:
|
|
|
|
pix_width *= 3;
|
|
|
|
pix_height *= 2;
|
|
|
|
break;
|
|
|
|
}
|
2006-08-07 22:11:07 +00:00
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2008-07-01 10:27:48 +00:00
|
|
|
if (DAR_16_9)
|
|
|
|
{
|
|
|
|
pix_width *= 4;
|
|
|
|
pix_height *= 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (height == 576)
|
|
|
|
{
|
|
|
|
pix_width *= 59;
|
|
|
|
pix_height *= 54;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pix_width *= 10;
|
|
|
|
pix_height *= 11;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pixel_width = pix_width;
|
|
|
|
*pixel_height = pix_height;
|
|
|
|
|
|
|
|
simplify (pixel_width, pixel_height);
|
|
|
|
|
|
|
|
return (height == 576) ? 1 : 2;
|
|
|
|
}
|
2006-08-07 22:11:07 +00:00
|
|
|
|
|
|
|
static void copy_matrix (mpeg2dec_t * mpeg2dec, int index)
|
|
|
|
{
|
|
|
|
if (rb->memcmp (mpeg2dec->quantizer_matrix[index],
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2dec->new_quantizer_matrix[index], 64))
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
rb->memcpy (mpeg2dec->quantizer_matrix[index],
|
|
|
|
mpeg2dec->new_quantizer_matrix[index], 64);
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->scaled[index] = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void finalize_matrix (mpeg2dec_t * mpeg2dec)
|
|
|
|
{
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2_decoder_t *decoder = &mpeg2dec->decoder;
|
2006-08-07 22:11:07 +00:00
|
|
|
int i;
|
|
|
|
|
2007-12-07 13:00:31 +00:00
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
if (mpeg2dec->copy_matrix & (1 << i))
|
|
|
|
copy_matrix (mpeg2dec, i);
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
if ((mpeg2dec->copy_matrix & (4 << i)) &&
|
2007-12-07 13:00:31 +00:00
|
|
|
rb->memcmp(mpeg2dec->quantizer_matrix[i],
|
|
|
|
mpeg2dec->new_quantizer_matrix[i+2], 64))
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
copy_matrix (mpeg2dec, i + 2);
|
|
|
|
decoder->chroma_quantizer[i] = decoder->quantizer_prescale[i+2];
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
else if (mpeg2dec->copy_matrix & (5 << i))
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
decoder->chroma_quantizer[i] = decoder->quantizer_prescale[i];
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
2006-08-07 22:11:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static mpeg2_state_t invalid_end_action (mpeg2dec_t * mpeg2dec)
|
|
|
|
{
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2_reset_info (&mpeg2dec->info);
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->info.gop = NULL;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
info_user_data (mpeg2dec);
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2_header_state_init (mpeg2dec);
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->sequence = mpeg2dec->new_sequence;
|
|
|
|
mpeg2dec->action = mpeg2_seek_header;
|
|
|
|
mpeg2dec->state = STATE_SEQUENCE;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
return STATE_SEQUENCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mpeg2_header_sequence_finalize (mpeg2dec_t * mpeg2dec)
|
|
|
|
{
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2_sequence_t * sequence = &mpeg2dec->new_sequence;
|
|
|
|
mpeg2_decoder_t * decoder = &mpeg2dec->decoder;
|
2006-08-07 22:11:07 +00:00
|
|
|
|
2007-12-07 13:00:31 +00:00
|
|
|
finalize_sequence(sequence);
|
|
|
|
finalize_matrix(mpeg2dec);
|
2006-08-07 22:11:07 +00:00
|
|
|
|
|
|
|
decoder->mpeg1 = !(sequence->flags & SEQ_FLAG_MPEG2);
|
|
|
|
decoder->width = sequence->width;
|
|
|
|
decoder->height = sequence->height;
|
2007-12-07 13:00:31 +00:00
|
|
|
decoder->vertical_position_extension = sequence->picture_height > 2800;
|
|
|
|
decoder->chroma_format = (sequence->chroma_width == sequence->width) +
|
|
|
|
(sequence->chroma_height == sequence->height);
|
2006-08-07 22:11:07 +00:00
|
|
|
|
2007-12-07 13:00:31 +00:00
|
|
|
if (mpeg2dec->sequence.width != (unsigned)-1)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
/*
|
|
|
|
* According to 6.1.1.6, repeat sequence headers should be
|
2008-07-01 10:27:48 +00:00
|
|
|
* identical to the original. However some encoders don't
|
|
|
|
* respect that and change various fields (including bitrate
|
|
|
|
* and aspect ratio) in the repeat sequence headers. So we
|
|
|
|
* choose to be as conservative as possible and only restart
|
|
|
|
* the decoder if the width, height, chroma_width,
|
|
|
|
* chroma_height or low_delay flag are modified.
|
2006-08-07 22:11:07 +00:00
|
|
|
*/
|
2008-07-01 10:27:48 +00:00
|
|
|
if (sequence->width != mpeg2dec->sequence.width ||
|
|
|
|
sequence->height != mpeg2dec->sequence.height ||
|
|
|
|
sequence->chroma_width != mpeg2dec->sequence.chroma_width ||
|
|
|
|
sequence->chroma_height != mpeg2dec->sequence.chroma_height ||
|
|
|
|
((sequence->flags ^ mpeg2dec->sequence.flags) &
|
|
|
|
SEQ_FLAG_LOW_DELAY))
|
2007-12-07 13:00:31 +00:00
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
decoder->stride_frame = sequence->width;
|
|
|
|
mpeg2_header_end (mpeg2dec);
|
|
|
|
mpeg2dec->action = invalid_end_action;
|
|
|
|
mpeg2dec->state = STATE_INVALID_END;
|
|
|
|
return;
|
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2008-07-01 10:27:48 +00:00
|
|
|
mpeg2dec->state = rb->memcmp(&mpeg2dec->sequence, sequence,
|
|
|
|
sizeof (mpeg2_sequence_t)) ?
|
|
|
|
STATE_SEQUENCE_MODIFIED :
|
|
|
|
STATE_SEQUENCE_REPEATED;
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
decoder->stride_frame = sequence->width;
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->sequence = *sequence;
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2_reset_info(&mpeg2dec->info);
|
|
|
|
mpeg2dec->info.sequence = &mpeg2dec->sequence;
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->info.gop = NULL;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
info_user_data (mpeg2dec);
|
|
|
|
}
|
|
|
|
|
|
|
|
int mpeg2_header_gop (mpeg2dec_t * mpeg2dec)
|
|
|
|
{
|
|
|
|
uint8_t * buffer = mpeg2dec->chunk_start;
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2_gop_t * gop = &mpeg2dec->new_gop;
|
2006-08-07 22:11:07 +00:00
|
|
|
|
2007-12-07 13:00:31 +00:00
|
|
|
if (!(buffer[1] & 8))
|
2006-08-07 22:11:07 +00:00
|
|
|
return 1;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
gop->hours = (buffer[0] >> 2) & 31;
|
|
|
|
gop->minutes = ((buffer[0] << 4) | (buffer[1] >> 4)) & 63;
|
|
|
|
gop->seconds = ((buffer[1] << 3) | (buffer[2] >> 5)) & 63;
|
|
|
|
gop->pictures = ((buffer[2] << 1) | (buffer[3] >> 7)) & 63;
|
|
|
|
gop->flags = (buffer[0] >> 7) | ((buffer[3] >> 4) & 6);
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->state = STATE_GOP;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mpeg2_header_gop_finalize (mpeg2dec_t * mpeg2dec)
|
|
|
|
{
|
|
|
|
mpeg2dec->gop = mpeg2dec->new_gop;
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2_reset_info (&mpeg2dec->info);
|
|
|
|
mpeg2dec->info.gop = &mpeg2dec->gop;
|
2006-08-07 22:11:07 +00:00
|
|
|
info_user_data (mpeg2dec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mpeg2_set_fbuf (mpeg2dec_t * mpeg2dec, int b_type)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++)
|
2007-12-07 13:00:31 +00:00
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
if (mpeg2dec->fbuf[1] != &mpeg2dec->fbuf_alloc[i].fbuf &&
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2dec->fbuf[2] != &mpeg2dec->fbuf_alloc[i].fbuf)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->fbuf[0] = &mpeg2dec->fbuf_alloc[i].fbuf;
|
|
|
|
mpeg2dec->info.current_fbuf = mpeg2dec->fbuf[0];
|
2007-12-07 13:00:31 +00:00
|
|
|
|
|
|
|
if (b_type || (mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY))
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
if (b_type || mpeg2dec->convert)
|
|
|
|
mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[0];
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[0];
|
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
2006-08-07 22:11:07 +00:00
|
|
|
}
|
|
|
|
|
2008-07-01 10:27:48 +00:00
|
|
|
int mpeg2_header_picture (mpeg2dec_t * mpeg2dec)
|
2006-08-07 22:11:07 +00:00
|
|
|
{
|
2008-07-01 10:27:48 +00:00
|
|
|
uint8_t * buffer = mpeg2dec->chunk_start;
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2_picture_t * picture = &mpeg2dec->new_picture;
|
2008-07-01 10:27:48 +00:00
|
|
|
mpeg2_decoder_t * decoder = &mpeg2dec->decoder;
|
|
|
|
int type;
|
2006-08-07 22:11:07 +00:00
|
|
|
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2dec->state = (mpeg2dec->state != STATE_SLICE_1ST) ?
|
|
|
|
STATE_PICTURE : STATE_PICTURE_2ND;
|
2008-07-01 10:27:48 +00:00
|
|
|
mpeg2dec->ext_state = PIC_CODING_EXT;
|
|
|
|
|
|
|
|
picture->temporal_reference = (buffer[0] << 2) | (buffer[1] >> 6);
|
|
|
|
|
|
|
|
type = (buffer [1] >> 3) & 7;
|
|
|
|
|
|
|
|
if (type == PIC_FLAG_CODING_TYPE_P || type == PIC_FLAG_CODING_TYPE_B)
|
|
|
|
{
|
|
|
|
/* forward_f_code and backward_f_code - used in mpeg1 only */
|
|
|
|
decoder->f_motion.f_code[1] = (buffer[3] >> 2) & 1;
|
|
|
|
decoder->f_motion.f_code[0] =
|
|
|
|
(((buffer[3] << 1) | (buffer[4] >> 7)) & 7) - 1;
|
|
|
|
decoder->b_motion.f_code[1] = (buffer[4] >> 6) & 1;
|
|
|
|
decoder->b_motion.f_code[0] = ((buffer[4] >> 3) & 7) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
picture->flags = PIC_FLAG_PROGRESSIVE_FRAME | type;
|
2006-08-07 22:11:07 +00:00
|
|
|
picture->tag = picture->tag2 = 0;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
|
|
|
if (mpeg2dec->num_tags)
|
|
|
|
{
|
2008-07-01 10:27:48 +00:00
|
|
|
if (mpeg2dec->bytes_since_tag >= mpeg2dec->chunk_ptr - buffer + 4)
|
2007-12-07 13:00:31 +00:00
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->num_tags = 0;
|
|
|
|
picture->tag = mpeg2dec->tag_current;
|
|
|
|
picture->tag2 = mpeg2dec->tag2_current;
|
2008-07-01 10:27:48 +00:00
|
|
|
picture->flags |= PIC_FLAG_TAGS;
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
else if (mpeg2dec->num_tags > 1)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->num_tags = 1;
|
|
|
|
picture->tag = mpeg2dec->tag_previous;
|
|
|
|
picture->tag2 = mpeg2dec->tag2_previous;
|
2008-07-01 10:27:48 +00:00
|
|
|
picture->flags |= PIC_FLAG_TAGS;
|
2006-08-07 22:11:07 +00:00
|
|
|
}
|
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2008-07-01 10:27:48 +00:00
|
|
|
picture->nb_fields = 2;
|
2006-08-07 22:11:07 +00:00
|
|
|
picture->display_offset[0].x = picture->display_offset[1].x =
|
|
|
|
picture->display_offset[2].x = mpeg2dec->display_offset_x;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
picture->display_offset[0].y = picture->display_offset[1].y =
|
|
|
|
picture->display_offset[2].y = mpeg2dec->display_offset_y;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
/* XXXXXX decode extra_information_picture as well */
|
|
|
|
|
|
|
|
mpeg2dec->q_scale_type = 0;
|
|
|
|
decoder->intra_dc_precision = 7;
|
|
|
|
decoder->frame_pred_frame_dct = 1;
|
|
|
|
decoder->concealment_motion_vectors = 0;
|
|
|
|
decoder->scan = mpeg2_scan_norm;
|
|
|
|
decoder->picture_structure = FRAME_PICTURE;
|
|
|
|
mpeg2dec->copy_matrix = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int picture_coding_ext (mpeg2dec_t * mpeg2dec)
|
|
|
|
{
|
|
|
|
uint8_t * buffer = mpeg2dec->chunk_start;
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2_picture_t * picture = &mpeg2dec->new_picture;
|
|
|
|
mpeg2_decoder_t * decoder = &mpeg2dec->decoder;
|
2006-08-07 22:11:07 +00:00
|
|
|
uint32_t flags;
|
|
|
|
|
|
|
|
/* pre subtract 1 for use later in compute_motion_vector */
|
|
|
|
decoder->f_motion.f_code[0] = (buffer[0] & 15) - 1;
|
|
|
|
decoder->f_motion.f_code[1] = (buffer[1] >> 4) - 1;
|
|
|
|
decoder->b_motion.f_code[0] = (buffer[1] & 15) - 1;
|
|
|
|
decoder->b_motion.f_code[1] = (buffer[2] >> 4) - 1;
|
|
|
|
|
|
|
|
flags = picture->flags;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
decoder->intra_dc_precision = 7 - ((buffer[2] >> 2) & 3);
|
|
|
|
decoder->picture_structure = buffer[2] & 3;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
|
|
|
switch (decoder->picture_structure)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
case TOP_FIELD:
|
|
|
|
flags |= PIC_FLAG_TOP_FIELD_FIRST;
|
|
|
|
case BOTTOM_FIELD:
|
|
|
|
picture->nb_fields = 1;
|
|
|
|
break;
|
|
|
|
case FRAME_PICTURE:
|
2007-12-07 13:00:31 +00:00
|
|
|
if (!(mpeg2dec->sequence.flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE))
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
picture->nb_fields = (buffer[3] & 2) ? 3 : 2;
|
|
|
|
flags |= (buffer[3] & 128) ? PIC_FLAG_TOP_FIELD_FIRST : 0;
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
picture->nb_fields = (buffer[3]&2) ? ((buffer[3]&128) ? 6 : 4) : 2;
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
2006-08-07 22:11:07 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
decoder->top_field_first = buffer[3] >> 7;
|
|
|
|
decoder->frame_pred_frame_dct = (buffer[3] >> 6) & 1;
|
|
|
|
decoder->concealment_motion_vectors = (buffer[3] >> 5) & 1;
|
|
|
|
mpeg2dec->q_scale_type = buffer[3] & 16;
|
|
|
|
decoder->intra_vlc_format = (buffer[3] >> 3) & 1;
|
|
|
|
decoder->scan = (buffer[3] & 4) ? mpeg2_scan_alt : mpeg2_scan_norm;
|
2008-07-01 10:27:48 +00:00
|
|
|
|
|
|
|
if (!(buffer[4] & 0x80))
|
|
|
|
flags &= ~PIC_FLAG_PROGRESSIVE_FRAME;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
if (buffer[4] & 0x40)
|
2007-12-07 13:00:31 +00:00
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
flags |= (((buffer[4]<<26) | (buffer[5]<<18) | (buffer[6]<<10)) &
|
|
|
|
PIC_MASK_COMPOSITE_DISPLAY) | PIC_FLAG_COMPOSITE_DISPLAY;
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
picture->flags = flags;
|
|
|
|
|
|
|
|
mpeg2dec->ext_state = PIC_DISPLAY_EXT | COPYRIGHT_EXT | QUANT_MATRIX_EXT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int picture_display_ext (mpeg2dec_t * mpeg2dec)
|
|
|
|
{
|
|
|
|
uint8_t * buffer = mpeg2dec->chunk_start;
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2_picture_t * picture = &mpeg2dec->new_picture;
|
2006-08-07 22:11:07 +00:00
|
|
|
int i, nb_pos;
|
|
|
|
|
|
|
|
nb_pos = picture->nb_fields;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
if (mpeg2dec->sequence.flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE)
|
|
|
|
nb_pos >>= 1;
|
|
|
|
|
2007-12-07 13:00:31 +00:00
|
|
|
for (i = 0; i < nb_pos; i++)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
int x, y;
|
|
|
|
|
|
|
|
x = ((buffer[4*i] << 24) | (buffer[4*i+1] << 16) |
|
|
|
|
(buffer[4*i+2] << 8) | buffer[4*i+3]) >> (11-2*i);
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
y = ((buffer[4*i+2] << 24) | (buffer[4*i+3] << 16) |
|
|
|
|
(buffer[4*i+4] << 8) | buffer[4*i+5]) >> (10-2*i);
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
if (! (x & y & 1))
|
|
|
|
return 1;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
picture->display_offset[i].x = mpeg2dec->display_offset_x = x >> 1;
|
|
|
|
picture->display_offset[i].y = mpeg2dec->display_offset_y = y >> 1;
|
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
|
|
|
|
for (; i < 3; i++)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
picture->display_offset[i].x = mpeg2dec->display_offset_x;
|
|
|
|
picture->display_offset[i].y = mpeg2dec->display_offset_y;
|
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-14 16:35:44 +00:00
|
|
|
void mpeg2_header_picture_finalize (mpeg2dec_t * mpeg2dec)
|
2006-08-07 22:11:07 +00:00
|
|
|
{
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2_decoder_t * decoder = &mpeg2dec->decoder;
|
|
|
|
int old_type_b = decoder->coding_type == B_TYPE;
|
2006-08-07 22:11:07 +00:00
|
|
|
int low_delay = mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY;
|
|
|
|
|
|
|
|
finalize_matrix (mpeg2dec);
|
|
|
|
decoder->coding_type = mpeg2dec->new_picture.flags & PIC_MASK_CODING_TYPE;
|
|
|
|
|
2007-12-07 13:00:31 +00:00
|
|
|
if (mpeg2dec->state == STATE_PICTURE)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2_picture_t * picture;
|
|
|
|
mpeg2_picture_t * other;
|
|
|
|
|
|
|
|
decoder->second_field = 0;
|
|
|
|
|
|
|
|
picture = other = mpeg2dec->pictures;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
if (old_type_b ^ (mpeg2dec->picture < mpeg2dec->pictures + 2))
|
|
|
|
picture += 2;
|
|
|
|
else
|
|
|
|
other += 2;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->picture = picture;
|
|
|
|
*picture = mpeg2dec->new_picture;
|
|
|
|
|
2007-12-07 13:00:31 +00:00
|
|
|
if (!old_type_b)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->fbuf[2] = mpeg2dec->fbuf[1];
|
|
|
|
mpeg2dec->fbuf[1] = mpeg2dec->fbuf[0];
|
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->fbuf[0] = NULL;
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2_reset_info (&mpeg2dec->info);
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->info.current_picture = picture;
|
|
|
|
mpeg2dec->info.display_picture = picture;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
|
|
|
if (decoder->coding_type != B_TYPE)
|
|
|
|
{
|
|
|
|
if (!low_delay)
|
|
|
|
{
|
|
|
|
if (mpeg2dec->first)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->info.display_picture = NULL;
|
|
|
|
mpeg2dec->first = 0;
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->info.display_picture = other;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
if (other->nb_fields == 1)
|
|
|
|
mpeg2dec->info.display_picture_2nd = other + 1;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[1];
|
|
|
|
}
|
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
if (!low_delay + !mpeg2dec->convert)
|
2007-12-07 13:00:31 +00:00
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->info.discard_fbuf =
|
|
|
|
mpeg2dec->fbuf[!low_delay + !mpeg2dec->convert];
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
2006-08-07 22:11:07 +00:00
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
|
|
|
|
if (mpeg2dec->convert)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2_convert_init_t convert_init;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
|
|
|
if (!mpeg2dec->convert_start)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->decoder.convert_id =
|
|
|
|
mpeg2_malloc (mpeg2dec->convert_id_size,
|
|
|
|
MPEG2_ALLOC_CONVERT_ID);
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->convert (MPEG2_CONVERT_START,
|
|
|
|
mpeg2dec->decoder.convert_id,
|
2007-12-07 13:00:31 +00:00
|
|
|
&mpeg2dec->sequence,
|
2007-04-14 16:35:44 +00:00
|
|
|
mpeg2dec->convert_stride,
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->convert_arg, &convert_init);
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->convert_start = convert_init.start;
|
|
|
|
mpeg2dec->decoder.convert = convert_init.copy;
|
|
|
|
|
2008-01-16 01:22:56 +00:00
|
|
|
int y_size = decoder->stride_frame * mpeg2dec->sequence.height;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->yuv_buf[0][0] =
|
2007-12-07 13:00:31 +00:00
|
|
|
(uint8_t *) mpeg2_malloc(y_size, MPEG2_ALLOC_YUV);
|
2008-01-16 01:22:56 +00:00
|
|
|
#if MPEG2_COLOR
|
|
|
|
int uv_size = y_size >> (2 - mpeg2dec->decoder.chroma_format);
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->yuv_buf[0][1] =
|
2007-12-07 13:00:31 +00:00
|
|
|
(uint8_t *) mpeg2_malloc(uv_size, MPEG2_ALLOC_YUV);
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->yuv_buf[0][2] =
|
2007-12-07 13:00:31 +00:00
|
|
|
(uint8_t *) mpeg2_malloc(uv_size, MPEG2_ALLOC_YUV);
|
2008-01-16 01:22:56 +00:00
|
|
|
#endif
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->yuv_buf[1][0] =
|
2007-12-07 13:00:31 +00:00
|
|
|
(uint8_t *) mpeg2_malloc(y_size, MPEG2_ALLOC_YUV);
|
2008-01-16 01:22:56 +00:00
|
|
|
#if MPEG2_COLOR
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->yuv_buf[1][1] =
|
2007-12-07 13:00:31 +00:00
|
|
|
(uint8_t *) mpeg2_malloc(uv_size, MPEG2_ALLOC_YUV);
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->yuv_buf[1][2] =
|
2007-12-07 13:00:31 +00:00
|
|
|
(uint8_t *) mpeg2_malloc(uv_size, MPEG2_ALLOC_YUV);
|
2008-01-16 01:22:56 +00:00
|
|
|
#endif
|
2006-08-07 22:11:07 +00:00
|
|
|
y_size = decoder->stride_frame * 32;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->yuv_buf[2][0] =
|
2007-12-07 13:00:31 +00:00
|
|
|
(uint8_t *) mpeg2_malloc(y_size, MPEG2_ALLOC_YUV);
|
2008-01-16 01:22:56 +00:00
|
|
|
#if MPEG2_COLOR
|
|
|
|
uv_size = y_size >> (2 - mpeg2dec->decoder.chroma_format);
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->yuv_buf[2][1] =
|
2007-12-07 13:00:31 +00:00
|
|
|
(uint8_t *) mpeg2_malloc(uv_size, MPEG2_ALLOC_YUV);
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->yuv_buf[2][2] =
|
2007-12-07 13:00:31 +00:00
|
|
|
(uint8_t *) mpeg2_malloc(uv_size, MPEG2_ALLOC_YUV);
|
2008-01-16 01:22:56 +00:00
|
|
|
#endif
|
2006-08-07 22:11:07 +00:00
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
|
|
|
|
if (!mpeg2dec->custom_fbuf)
|
|
|
|
{
|
|
|
|
while (mpeg2dec->alloc_index < 3)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2_fbuf_t * fbuf;
|
|
|
|
|
|
|
|
fbuf = &mpeg2dec->fbuf_alloc[mpeg2dec->alloc_index++].fbuf;
|
|
|
|
fbuf->id = NULL;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
fbuf->buf[0] =
|
|
|
|
(uint8_t *) mpeg2_malloc (convert_init.buf_size[0],
|
|
|
|
MPEG2_ALLOC_CONVERTED);
|
2008-01-16 01:22:56 +00:00
|
|
|
#if MPEG2_COLOR
|
2006-08-07 22:11:07 +00:00
|
|
|
fbuf->buf[1] =
|
|
|
|
(uint8_t *) mpeg2_malloc (convert_init.buf_size[1],
|
|
|
|
MPEG2_ALLOC_CONVERTED);
|
|
|
|
fbuf->buf[2] =
|
|
|
|
(uint8_t *) mpeg2_malloc (convert_init.buf_size[2],
|
|
|
|
MPEG2_ALLOC_CONVERTED);
|
2008-01-16 01:22:56 +00:00
|
|
|
#endif
|
2006-08-07 22:11:07 +00:00
|
|
|
}
|
2008-01-16 01:22:56 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2_set_fbuf (mpeg2dec, (decoder->coding_type == B_TYPE));
|
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
else if (!mpeg2dec->custom_fbuf)
|
|
|
|
{
|
|
|
|
while (mpeg2dec->alloc_index < 3)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2_fbuf_t * fbuf;
|
|
|
|
|
2007-12-07 13:00:31 +00:00
|
|
|
fbuf = &mpeg2dec->fbuf_alloc[mpeg2dec->alloc_index++].fbuf;
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
fbuf->id = NULL;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2008-01-16 01:22:56 +00:00
|
|
|
int y_size = decoder->stride_frame * mpeg2dec->sequence.height;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
fbuf->buf[0] = (uint8_t *) mpeg2_malloc (y_size,
|
|
|
|
MPEG2_ALLOC_YUV);
|
2008-01-16 01:22:56 +00:00
|
|
|
#if MPEG2_COLOR
|
|
|
|
int uv_size = y_size >> (2 - decoder->chroma_format);
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
fbuf->buf[1] = (uint8_t *) mpeg2_malloc (uv_size,
|
|
|
|
MPEG2_ALLOC_YUV);
|
|
|
|
fbuf->buf[2] = (uint8_t *) mpeg2_malloc (uv_size,
|
|
|
|
MPEG2_ALLOC_YUV);
|
2008-01-16 01:22:56 +00:00
|
|
|
#endif
|
2006-08-07 22:11:07 +00:00
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2_set_fbuf (mpeg2dec, (decoder->coding_type == B_TYPE));
|
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
decoder->second_field = 1;
|
|
|
|
mpeg2dec->picture++; /* second field picture */
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
*(mpeg2dec->picture) = mpeg2dec->new_picture;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->info.current_picture_2nd = mpeg2dec->picture;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
if (low_delay || decoder->coding_type == B_TYPE)
|
|
|
|
mpeg2dec->info.display_picture_2nd = mpeg2dec->picture;
|
|
|
|
}
|
|
|
|
|
|
|
|
info_user_data (mpeg2dec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int copyright_ext (mpeg2dec_t * mpeg2dec)
|
|
|
|
{
|
|
|
|
(void)mpeg2dec;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int quant_matrix_ext (mpeg2dec_t * mpeg2dec)
|
|
|
|
{
|
|
|
|
uint8_t * buffer = mpeg2dec->chunk_start;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++)
|
2007-12-07 13:00:31 +00:00
|
|
|
{
|
|
|
|
if (buffer[0] & (8 >> i))
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
for (j = 0; j < 64; j++)
|
2007-12-07 13:00:31 +00:00
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->new_quantizer_matrix[i][mpeg2_scan_norm[j]] =
|
|
|
|
(buffer[j] << (i+5)) | (buffer[j+1] >> (3-i));
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->copy_matrix |= 1 << i;
|
|
|
|
buffer += 64;
|
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
2006-08-07 22:11:07 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mpeg2_header_extension (mpeg2dec_t * mpeg2dec)
|
|
|
|
{
|
2007-12-07 13:00:31 +00:00
|
|
|
static int (* const parser[9]) (mpeg2dec_t *) =
|
|
|
|
{
|
|
|
|
NULL,
|
|
|
|
sequence_ext,
|
|
|
|
sequence_display_ext,
|
|
|
|
quant_matrix_ext,
|
|
|
|
copyright_ext,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
picture_display_ext,
|
|
|
|
picture_coding_ext
|
2006-08-07 22:11:07 +00:00
|
|
|
};
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
int ext, ext_bit;
|
|
|
|
|
|
|
|
ext = mpeg2dec->chunk_start[0] >> 4;
|
|
|
|
ext_bit = 1 << ext;
|
|
|
|
|
|
|
|
if (!(mpeg2dec->ext_state & ext_bit))
|
|
|
|
return 0; /* ignore illegal extensions */
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->ext_state &= ~ext_bit;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
return parser[ext] (mpeg2dec);
|
|
|
|
}
|
|
|
|
|
|
|
|
int mpeg2_header_user_data (mpeg2dec_t * mpeg2dec)
|
|
|
|
{
|
|
|
|
mpeg2dec->user_data_len += mpeg2dec->chunk_ptr - 1 - mpeg2dec->chunk_start;
|
|
|
|
mpeg2dec->chunk_start = mpeg2dec->chunk_ptr - 1;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void prescale (mpeg2dec_t * mpeg2dec, int index)
|
|
|
|
{
|
2007-12-07 13:00:31 +00:00
|
|
|
static const int non_linear_scale[32] =
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
0, 1, 2, 3, 4, 5, 6, 7,
|
|
|
|
8, 10, 12, 14, 16, 18, 20, 22,
|
|
|
|
24, 28, 32, 36, 40, 44, 48, 52,
|
|
|
|
56, 64, 72, 80, 88, 96, 104, 112
|
|
|
|
};
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
int i, j, k;
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2_decoder_t * decoder = &mpeg2dec->decoder;
|
2006-08-07 22:11:07 +00:00
|
|
|
|
2007-12-07 13:00:31 +00:00
|
|
|
if (mpeg2dec->scaled[index] != mpeg2dec->q_scale_type)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->scaled[index] = mpeg2dec->q_scale_type;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 32; i++)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
k = mpeg2dec->q_scale_type ? non_linear_scale[i] : (i << 1);
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
for (j = 0; j < 64; j++)
|
2007-12-07 13:00:31 +00:00
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
decoder->quantizer_prescale[index][i][j] =
|
|
|
|
k * mpeg2dec->quantizer_matrix[index][j];
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
2006-08-07 22:11:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mpeg2_state_t mpeg2_header_slice_start (mpeg2dec_t * mpeg2dec)
|
|
|
|
{
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2_decoder_t * decoder = &mpeg2dec->decoder;
|
2006-08-07 22:11:07 +00:00
|
|
|
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2dec->info.user_data = NULL;
|
|
|
|
mpeg2dec->info.user_data_len = 0;
|
|
|
|
mpeg2dec->state = (mpeg2dec->picture->nb_fields > 1 ||
|
|
|
|
mpeg2dec->state == STATE_PICTURE_2ND) ?
|
|
|
|
STATE_SLICE : STATE_SLICE_1ST;
|
2006-08-07 22:11:07 +00:00
|
|
|
|
2007-12-07 13:00:31 +00:00
|
|
|
if (mpeg2dec->decoder.coding_type != D_TYPE)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
prescale (mpeg2dec, 0);
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
if (decoder->chroma_quantizer[0] == decoder->quantizer_prescale[2])
|
|
|
|
prescale (mpeg2dec, 2);
|
2007-12-07 13:00:31 +00:00
|
|
|
|
|
|
|
if (mpeg2dec->decoder.coding_type != I_TYPE)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
prescale (mpeg2dec, 1);
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
if (decoder->chroma_quantizer[1] == decoder->quantizer_prescale[3])
|
|
|
|
prescale (mpeg2dec, 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(mpeg2dec->nb_decode_slices))
|
2007-12-07 13:00:31 +00:00
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->picture->flags |= PIC_FLAG_SKIP;
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
else if (mpeg2dec->convert_start)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->convert_start (decoder->convert_id, mpeg2dec->fbuf[0],
|
|
|
|
mpeg2dec->picture, mpeg2dec->info.gop);
|
|
|
|
|
|
|
|
if (mpeg2dec->decoder.coding_type == B_TYPE)
|
2007-12-07 13:00:31 +00:00
|
|
|
{
|
|
|
|
mpeg2_init_fbuf (&mpeg2dec->decoder, mpeg2dec->yuv_buf[2],
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->yuv_buf[mpeg2dec->yuv_index ^ 1],
|
|
|
|
mpeg2dec->yuv_buf[mpeg2dec->yuv_index]);
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mpeg2_init_fbuf (&mpeg2dec->decoder,
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->yuv_buf[mpeg2dec->yuv_index ^ 1],
|
|
|
|
mpeg2dec->yuv_buf[mpeg2dec->yuv_index],
|
|
|
|
mpeg2dec->yuv_buf[mpeg2dec->yuv_index]);
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
if (mpeg2dec->state == STATE_SLICE)
|
|
|
|
mpeg2dec->yuv_index ^= 1;
|
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
int b_type;
|
|
|
|
|
|
|
|
b_type = (mpeg2dec->decoder.coding_type == B_TYPE);
|
2007-12-07 13:00:31 +00:00
|
|
|
|
|
|
|
mpeg2_init_fbuf (&mpeg2dec->decoder, mpeg2dec->fbuf[0]->buf,
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->fbuf[b_type + 1]->buf,
|
|
|
|
mpeg2dec->fbuf[b_type]->buf);
|
|
|
|
}
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->action = NULL;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2008-07-01 10:27:48 +00:00
|
|
|
return STATE_INTERNAL_NORETURN;
|
2006-08-07 22:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static mpeg2_state_t seek_sequence (mpeg2dec_t * mpeg2dec)
|
|
|
|
{
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2_reset_info (&mpeg2dec->info);
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->info.sequence = NULL;
|
|
|
|
mpeg2dec->info.gop = NULL;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2_header_state_init (mpeg2dec);
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->action = mpeg2_seek_header;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
return mpeg2_seek_header (mpeg2dec);
|
|
|
|
}
|
|
|
|
|
|
|
|
mpeg2_state_t mpeg2_header_end (mpeg2dec_t * mpeg2dec)
|
|
|
|
{
|
|
|
|
mpeg2_picture_t * picture;
|
|
|
|
int b_type;
|
|
|
|
|
|
|
|
b_type = (mpeg2dec->decoder.coding_type == B_TYPE);
|
|
|
|
picture = mpeg2dec->pictures;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
if ((mpeg2dec->picture >= picture + 2) ^ b_type)
|
|
|
|
picture = mpeg2dec->pictures + 2;
|
|
|
|
|
2007-12-07 13:00:31 +00:00
|
|
|
mpeg2_reset_info (&mpeg2dec->info);
|
|
|
|
|
|
|
|
if (!(mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY))
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->info.display_picture = picture;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
if (picture->nb_fields == 1)
|
|
|
|
mpeg2dec->info.display_picture_2nd = picture + 1;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[b_type];
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
if (!mpeg2dec->convert)
|
|
|
|
mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[b_type + 1];
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
else if (!mpeg2dec->convert)
|
|
|
|
{
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[b_type];
|
2007-12-07 13:00:31 +00:00
|
|
|
}
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
mpeg2dec->action = seek_sequence;
|
2007-12-07 13:00:31 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
return STATE_END;
|
|
|
|
}
|