Commit e3576569 authored by Laurent Aimar's avatar Laurent Aimar

* h264: added avc1 -> h264 annexe B stream.

parent 2f0dfb7c
...@@ -52,6 +52,7 @@ vlc_module_end(); ...@@ -52,6 +52,7 @@ vlc_module_end();
* Local prototypes * Local prototypes
****************************************************************************/ ****************************************************************************/
static block_t *Packetize( decoder_t *, block_t ** ); static block_t *Packetize( decoder_t *, block_t ** );
static block_t *PacketizeAVC1( decoder_t *, block_t ** );
struct decoder_sys_t struct decoder_sys_t
{ {
...@@ -69,6 +70,9 @@ struct decoder_sys_t ...@@ -69,6 +70,9 @@ struct decoder_sys_t
unsigned int i_flags; unsigned int i_flags;
vlc_bool_t b_sps; vlc_bool_t b_sps;
/* avcC data */
int i_avcC_length_size;
}; };
enum enum
...@@ -101,6 +105,8 @@ enum nal_priority_e ...@@ -101,6 +105,8 @@ enum nal_priority_e
static block_t *ParseNALBlock( decoder_t *, block_t * ); static block_t *ParseNALBlock( decoder_t *, block_t * );
static block_t *nal_get_encoded( decoder_t *, uint8_t *p, int );
/***************************************************************************** /*****************************************************************************
* Open: probe the packetizer and return score * Open: probe the packetizer and return score
*****************************************************************************/ *****************************************************************************/
...@@ -110,7 +116,8 @@ static int Open( vlc_object_t *p_this ) ...@@ -110,7 +116,8 @@ static int Open( vlc_object_t *p_this )
decoder_sys_t *p_sys; decoder_sys_t *p_sys;
if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'h', '2', '6', '4') && if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'h', '2', '6', '4') &&
p_dec->fmt_in.i_codec != VLC_FOURCC( 'H', '2', '6', '4') ) p_dec->fmt_in.i_codec != VLC_FOURCC( 'H', '2', '6', '4') &&
( p_dec->fmt_in.i_codec != VLC_FOURCC( 'a', 'v', 'c', '1') || p_dec->fmt_in.i_extra < 7 ) )
{ {
return VLC_EGENERIC; return VLC_EGENERIC;
} }
...@@ -139,29 +146,47 @@ static int Open( vlc_object_t *p_this ) ...@@ -139,29 +146,47 @@ static int Open( vlc_object_t *p_this )
es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in ); es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
p_dec->fmt_out.i_codec = VLC_FOURCC( 'h', '2', '6', '4' ); p_dec->fmt_out.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
#if 0 if( p_dec->fmt_in.i_codec == VLC_FOURCC( 'a', 'v', 'c', '1' ) )
if( p_dec->fmt_in.i_extra )
{ {
/* We have a vol */ uint8_t *p = &((uint8_t*)p_dec->fmt_in.p_extra)[4];
p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra; int i_sps, i_pps;
p_dec->fmt_out.p_extra = malloc( p_dec->fmt_in.i_extra ); int i;
memcpy( p_dec->fmt_out.p_extra, p_dec->fmt_in.p_extra,
p_dec->fmt_in.i_extra ); /* Parse avcC */
p_sys->i_avcC_length_size = 1 + ((*p++)&0x03);
msg_Dbg( p_dec, "opening with vol size:%d", p_dec->fmt_in.i_extra );
m4v_VOLParse( &p_dec->fmt_out, /* Read SPS */
p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra ); i_sps = (*p++)&0x1f;
for( i = 0; i < i_sps; i++ )
{
int i_length = GetWBE( p );
block_t *p_sps = nal_get_encoded( p_dec, p+2, i_length );
ParseNALBlock( p_dec, p_sps );
p += 2 + i_length;
}
/* Read PPS */
i_pps = *p++;
for( i = 0; i < i_pps; i++ )
{
int i_length = GetWBE( p );
block_t *p_pps = nal_get_encoded( p_dec, p+2, i_length );
ParseNALBlock( p_dec, p_pps );
p += 2 + i_length;
}
msg_Dbg( p_dec, "avcC length size=%d sps=%d pps=%d",
p_sys->i_avcC_length_size, i_sps, i_pps );
/* Set callback */
p_dec->pf_packetize = PacketizeAVC1;
} }
else else
{ {
/* No vol, we'll have to look for one later on */ /* Set callback */
p_dec->fmt_out.i_extra = 0; p_dec->pf_packetize = Packetize;
p_dec->fmt_out.p_extra = 0;
} }
#endif
/* Set callback */
p_dec->pf_packetize = Packetize;
return VLC_SUCCESS; return VLC_SUCCESS;
} }
...@@ -252,6 +277,115 @@ static block_t *Packetize( decoder_t *p_dec, block_t **pp_block ) ...@@ -252,6 +277,115 @@ static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
} }
} }
/****************************************************************************
* PacketizeAVC1: the whole thing
****************************************************************************/
static block_t *PacketizeAVC1( decoder_t *p_dec, block_t **pp_block )
{
decoder_sys_t *p_sys = p_dec->p_sys;
block_t *p_block;
block_t *p_ret = NULL;
uint8_t *p;
if( !pp_block || !*pp_block ) return NULL;
p_block = *pp_block;
*pp_block = NULL;
for( p = p_block->p_buffer; p < &p_block->p_buffer[p_block->i_buffer]; )
{
block_t *p_pic;
int i_size = 0;
int i;
for( i = 0; i < p_sys->i_avcC_length_size; i++ )
{
i_size = (i_size << 8) | (*p++);
}
if( i_size > 0 )
{
block_t *p_part = nal_get_encoded( p_dec, p, i_size );
p_part->i_dts = p_block->i_dts;
p_part->i_pts = p_block->i_pts;
/* Parse the NAL */
if( ( p_pic = ParseNALBlock( p_dec, p_part ) ) )
{
block_ChainAppend( &p_ret, p_pic );
}
}
p += i_size;
}
return p_ret;
}
static block_t *nal_get_encoded( decoder_t *p_dec, uint8_t *p, int i_size )
{
block_t *p_nal;
int i_nal_size = 5;
uint8_t *src = &p[1];
uint8_t *end = &p[i_size];
uint8_t *dst;
int i_count = 0;
/* 1: compute real size */
while( src < end )
{
if( i_count == 2 && *src <= 0x03 )
{
i_nal_size++;
i_count = 0;
}
if( *src == 0 )
{
i_count++;
}
else
{
i_count = 0;
}
i_nal_size++;
src++;
}
/* 2: encode it */
p_nal = block_New( p_dec, i_nal_size );
i_count = 0;
src = p;
dst = p_nal->p_buffer;
/* add start code */
*dst++ = 0x00;
*dst++ = 0x00;
*dst++ = 0x00;
*dst++ = 0x01;
/* nal type */
*dst++ = *src++;
while( src < end )
{
if( i_count == 2 && *src <= 0x03 )
{
*dst++ = 0x03;
i_count = 0;
}
if( *src == 0 )
{
i_count++;
}
else
{
i_count = 0;
}
*dst++ = *src++;
}
return p_nal;
}
static void nal_get_decoded( uint8_t **pp_ret, int *pi_ret, uint8_t *src, int i_src ) static void nal_get_decoded( uint8_t **pp_ret, int *pi_ret, uint8_t *src, int i_src )
{ {
uint8_t *end = &src[i_src]; uint8_t *end = &src[i_src];
...@@ -285,7 +419,7 @@ static inline int bs_read_ue( bs_t *s ) ...@@ -285,7 +419,7 @@ static inline int bs_read_ue( bs_t *s )
} }
return( ( 1 << i) - 1 + bs_read( s, i ) ); return( ( 1 << i) - 1 + bs_read( s, i ) );
} }
static inline int bs_read_se( bs_t *s ) static inline int bs_read_se( bs_t *s )
{ {
int val = bs_read_ue( s ); int val = bs_read_ue( s );
...@@ -316,6 +450,7 @@ static block_t *ParseNALBlock( decoder_t *p_dec, block_t *p_frag ) ...@@ -316,6 +450,7 @@ static block_t *ParseNALBlock( decoder_t *p_dec, block_t *p_frag )
else else
{ {
block_ChainRelease( p_sys->p_frame ); block_ChainRelease( p_sys->p_frame );
msg_Warn( p_dec, "waiting SPS" );
} }
/* reset context */ /* reset context */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment