Commit 9e3d0367 authored by Ilkka Ollakka's avatar Ilkka Ollakka

avcodec: refactor delay buffer handling from EncodeAudio

parent af489315
...@@ -1115,47 +1115,12 @@ static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict ) ...@@ -1115,47 +1115,12 @@ static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
return p_block; return p_block;
} }
/**************************************************************************** static block_t *handle_delay_buffer( encoder_t *p_enc, encoder_sys_t *p_sys, int buffer_delay, block_t *p_aout_buf, int leftover_samples )
* EncodeAudio: the whole thing
****************************************************************************/
static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_aout_buf )
{ {
encoder_sys_t *p_sys = p_enc->p_sys; block_t *p_block,*p_chain = NULL;
block_t *p_block, *p_chain = NULL;
int got_packet,i_out;
size_t buffer_delay = 0, i_samples_left = 0;
//i_bytes_left is amount of bytes we get
i_samples_left = p_aout_buf ? p_aout_buf->i_nb_samples : 0;
buffer_delay = p_sys->i_samples_delay * p_sys->i_sample_bytes * p_sys->p_context->channels;
//p_sys->i_buffer_out = p_sys->i_frame_size * chan * p_sys->i_sample_bytes
//Calculate how many bytes we would need from current buffer to fill frame
size_t leftover_samples = __MAX(0,__MIN((ssize_t)i_samples_left, (ssize_t)(p_sys->i_frame_size - p_sys->i_samples_delay)));
if( ( p_aout_buf && ( p_aout_buf->i_pts > VLC_TS_INVALID ) &&
((p_aout_buf->i_pts - p_sys->i_samples_delay) != date_Get( &p_sys->buffer_date ) ) ) )
{
date_Set( &p_sys->buffer_date, p_aout_buf->i_dts );
/* take back amount we have leftover from previous buffer*/
if( p_sys->i_samples_delay > 0 )
date_Decrement( &p_sys->buffer_date, p_sys->i_samples_delay );
}
// Check if we have enough samples in delay_buffer and current p_aout_buf to fill frame
// Or if we are cleaning up
if( ( buffer_delay > 0 ) &&
( ( p_aout_buf && ( leftover_samples <= p_aout_buf->i_nb_samples ) &&
( (leftover_samples + p_sys->i_samples_delay ) >= p_sys->i_frame_size )
) ||
( !p_aout_buf )
)
)
{
//How much we need to copy from new packet //How much we need to copy from new packet
const int leftover = leftover_samples * p_sys->p_context->channels * p_sys->i_sample_bytes; const int leftover = leftover_samples * p_sys->p_context->channels * p_sys->i_sample_bytes;
int got_packet,i_out;
#if LIBAVUTIL_VERSION_CHECK( 51,27,2,46,100 ) #if LIBAVUTIL_VERSION_CHECK( 51,27,2,46,100 )
const int align = 0; const int align = 0;
...@@ -1167,6 +1132,7 @@ static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_aout_buf ) ...@@ -1167,6 +1132,7 @@ static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_aout_buf )
avcodec_get_frame_defaults( p_sys->frame ); avcodec_get_frame_defaults( p_sys->frame );
p_sys->frame->format = p_sys->p_context->sample_fmt; p_sys->frame->format = p_sys->p_context->sample_fmt;
p_sys->frame->nb_samples = leftover_samples + p_sys->i_samples_delay; p_sys->frame->nb_samples = leftover_samples + p_sys->i_samples_delay;
p_sys->frame->format = p_sys->p_context->sample_fmt;
p_sys->frame->pts = date_Get( &p_sys->buffer_date ); p_sys->frame->pts = date_Get( &p_sys->buffer_date );
...@@ -1210,7 +1176,7 @@ static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_aout_buf ) ...@@ -1210,7 +1176,7 @@ static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_aout_buf )
p_sys->frame->nb_samples = 0; p_sys->frame->nb_samples = 0;
} }
buffer_delay = 0;
p_sys->i_samples_delay = 0; p_sys->i_samples_delay = 0;
p_block = block_Alloc( p_sys->i_buffer_out ); p_block = block_Alloc( p_sys->i_buffer_out );
...@@ -1242,6 +1208,53 @@ static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_aout_buf ) ...@@ -1242,6 +1208,53 @@ static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_aout_buf )
p_block->i_dts = p_block->i_pts = VLC_TS_INVALID; p_block->i_dts = p_block->i_pts = VLC_TS_INVALID;
block_ChainAppend( &p_chain, p_block ); block_ChainAppend( &p_chain, p_block );
return p_chain;
}
/****************************************************************************
* EncodeAudio: the whole thing
****************************************************************************/
static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_aout_buf )
{
encoder_sys_t *p_sys = p_enc->p_sys;
block_t *p_block, *p_chain = NULL;
int got_packet,i_out;
size_t buffer_delay = 0, i_samples_left = 0;
//i_bytes_left is amount of bytes we get
i_samples_left = p_aout_buf ? p_aout_buf->i_nb_samples : 0;
buffer_delay = p_sys->i_samples_delay * p_sys->i_sample_bytes * p_sys->p_context->channels;
//p_sys->i_buffer_out = p_sys->i_frame_size * chan * p_sys->i_sample_bytes
//Calculate how many bytes we would need from current buffer to fill frame
size_t leftover_samples = __MAX(0,__MIN((ssize_t)i_samples_left, (ssize_t)(p_sys->i_frame_size - p_sys->i_samples_delay)));
if( ( p_aout_buf && ( p_aout_buf->i_pts > VLC_TS_INVALID ) &&
((p_aout_buf->i_pts - p_sys->i_samples_delay) != date_Get( &p_sys->buffer_date ) ) ) )
{
date_Set( &p_sys->buffer_date, p_aout_buf->i_dts );
/* take back amount we have leftover from previous buffer*/
if( p_sys->i_samples_delay > 0 )
date_Decrement( &p_sys->buffer_date, p_sys->i_samples_delay );
}
// Check if we have enough samples in delay_buffer and current p_aout_buf to fill frame
// Or if we are cleaning up
if( ( buffer_delay > 0 ) &&
( ( p_aout_buf && ( leftover_samples <= p_aout_buf->i_nb_samples ) &&
( (leftover_samples + p_sys->i_samples_delay ) >= p_sys->i_frame_size )
) ||
( !p_aout_buf )
)
)
{
p_chain = handle_delay_buffer( p_enc, p_sys, buffer_delay, p_aout_buf, leftover_samples );
buffer_delay = 0;
if( unlikely( !p_chain ) )
return NULL;
} }
if( unlikely( !p_aout_buf ) ) if( unlikely( !p_aout_buf ) )
......
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