/***************************************************************************** * SplitBuffer: Needed because aout really doesn't like big audio chunk and * wma produces easily > 30000 samples... *****************************************************************************/ static aout_buffer_t *SplitBuffer( decoder_t *p_dec ) { decoder_sys_t *p_sys = p_dec->p_sys; int i_samples = __MIN( p_sys->i_samples, 4096 ); aout_buffer_t *p_buffer; if( i_samples == 0 ) return NULL; if( ( p_buffer = decoder_NewAudioBuffer( p_dec, i_samples ) ) == NULL ) return NULL; p_buffer->i_pts = date_Get( &p_sys->end_date ); p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) - p_buffer->i_pts; if( p_sys->b_extract ) aout_ChannelExtract( p_buffer->p_buffer, p_dec->fmt_out.audio.i_channels, p_sys->p_samples, p_sys->p_context->channels, i_samples, p_sys->pi_extraction, p_dec->fmt_out.audio.i_bitspersample ); else memcpy( p_buffer->p_buffer, p_sys->p_samples, p_buffer->i_buffer ); p_sys->p_samples += i_samples * p_sys->p_context->channels * ( p_dec->fmt_out.audio.i_bitspersample / 8 ); p_sys->i_samples -= i_samples; return p_buffer; }
static block_t *Extract( filter_t *p_filter, block_t *p_in_buf ) { size_t i_out_channels = aout_FormatNbChannels( &p_filter->fmt_out.audio ); size_t i_out_size = p_in_buf->i_nb_samples * p_filter->fmt_out.audio.i_bitspersample * i_out_channels / 8; block_t *p_out_buf = block_Alloc( i_out_size ); if( unlikely(p_out_buf == NULL) ) { block_Release( p_in_buf ); return NULL; } p_out_buf->i_nb_samples = p_in_buf->i_nb_samples; p_out_buf->i_dts = p_in_buf->i_dts; p_out_buf->i_pts = p_in_buf->i_pts; p_out_buf->i_length = p_in_buf->i_length; static const int pi_selections[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, }; static_assert(sizeof(pi_selections)/sizeof(int) == AOUT_CHAN_MAX, "channel max size mismatch!"); aout_ChannelExtract( p_out_buf->p_buffer, i_out_channels, p_in_buf->p_buffer, p_filter->fmt_in.audio.i_channels, p_in_buf->i_nb_samples, pi_selections, p_filter->fmt_out.audio.i_bitspersample ); return p_out_buf; }
static block_t * ConvertAVFrame( decoder_t *p_dec, AVFrame *frame ) { decoder_sys_t *p_sys = p_dec->p_sys; AVCodecContext *ctx = p_sys->p_context; block_t *p_block; /* Interleave audio if required */ if( av_sample_fmt_is_planar( ctx->sample_fmt ) ) { p_block = block_Alloc(frame->linesize[0] * ctx->channels); if ( likely(p_block) ) { const void *planes[ctx->channels]; for (int i = 0; i < ctx->channels; i++) planes[i] = frame->extended_data[i]; aout_Interleave(p_block->p_buffer, planes, frame->nb_samples, ctx->channels, p_dec->fmt_out.audio.i_format); p_block->i_nb_samples = frame->nb_samples; } av_frame_free(&frame); } else { p_block = vlc_av_frame_Wrap(frame); frame = NULL; } if (p_sys->b_extract && p_block) { /* TODO: do not drop channels... at least not here */ block_t *p_buffer = block_Alloc( p_dec->fmt_out.audio.i_bytes_per_frame * p_block->i_nb_samples ); if( likely(p_buffer) ) { aout_ChannelExtract( p_buffer->p_buffer, p_dec->fmt_out.audio.i_channels, p_block->p_buffer, ctx->channels, p_block->i_nb_samples, p_sys->pi_extraction, p_dec->fmt_out.audio.i_bitspersample ); p_buffer->i_nb_samples = p_block->i_nb_samples; } block_Release( p_block ); p_block = p_buffer; } return p_block; }
/***************************************************************************** * DecodeAudio: Called to decode one frame *****************************************************************************/ block_t * DecodeAudio ( decoder_t *p_dec, block_t **pp_block ) { decoder_sys_t *p_sys = p_dec->p_sys; AVCodecContext *ctx = p_sys->p_context; if( !pp_block || !*pp_block ) return NULL; block_t *p_block = *pp_block; if( !ctx->extradata_size && p_dec->fmt_in.i_extra && p_sys->b_delayed_open) { InitDecoderConfig( p_dec, ctx ); if( ffmpeg_OpenCodec( p_dec ) ) msg_Err( p_dec, "Cannot open decoder %s", p_sys->psz_namecodec ); } if( p_sys->b_delayed_open ) goto end; if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) { avcodec_flush_buffers( ctx ); date_Set( &p_sys->end_date, 0 ); if( p_sys->i_codec_id == AV_CODEC_ID_MP2 || p_sys->i_codec_id == AV_CODEC_ID_MP3 ) p_sys->i_reject_count = 3; goto end; } /* We've just started the stream, wait for the first PTS. */ if( !date_Get( &p_sys->end_date ) && p_block->i_pts <= VLC_TS_INVALID ) goto end; if( p_block->i_buffer <= 0 ) goto end; if( (p_block->i_flags & BLOCK_FLAG_PRIVATE_REALLOCATED) == 0 ) { p_block = block_Realloc( p_block, 0, p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE ); if( !p_block ) return NULL; p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE; memset( &p_block->p_buffer[p_block->i_buffer], 0, FF_INPUT_BUFFER_PADDING_SIZE ); p_block->i_flags |= BLOCK_FLAG_PRIVATE_REALLOCATED; } AVFrame frame; memset( &frame, 0, sizeof( frame ) ); for( int got_frame = 0; !got_frame; ) { if( p_block->i_buffer == 0 ) goto end; AVPacket pkt; av_init_packet( &pkt ); pkt.data = p_block->p_buffer; pkt.size = p_block->i_buffer; int used = avcodec_decode_audio4( ctx, &frame, &got_frame, &pkt ); if( used < 0 ) { msg_Warn( p_dec, "cannot decode one frame (%zu bytes)", p_block->i_buffer ); goto end; } assert( p_block->i_buffer >= (unsigned)used ); p_block->p_buffer += used; p_block->i_buffer -= used; } if( ctx->channels <= 0 || ctx->channels > 8 || ctx->sample_rate <= 0 ) { msg_Warn( p_dec, "invalid audio properties channels count %d, sample rate %d", ctx->channels, ctx->sample_rate ); goto end; } if( p_dec->fmt_out.audio.i_rate != (unsigned int)ctx->sample_rate ) date_Init( &p_sys->end_date, ctx->sample_rate, 1 ); if( p_block->i_pts > VLC_TS_INVALID && p_block->i_pts > date_Get( &p_sys->end_date ) ) { date_Set( &p_sys->end_date, p_block->i_pts ); } if( p_block->i_buffer == 0 ) { /* Done with this buffer */ block_Release( p_block ); *pp_block = NULL; } /* NOTE WELL: Beyond this point, p_block now refers to the DECODED block */ p_block = frame.opaque; SetupOutputFormat( p_dec, true ); /* Silent unwanted samples */ if( p_sys->i_reject_count > 0 ) { memset( p_block->p_buffer, 0, p_block->i_buffer ); p_sys->i_reject_count--; } block_t *p_buffer = decoder_NewAudioBuffer( p_dec, p_block->i_nb_samples ); if (!p_buffer) return NULL; assert( p_block->i_nb_samples >= (unsigned)frame.nb_samples ); assert( p_block->i_nb_samples == p_buffer->i_nb_samples ); p_block->i_buffer = p_buffer->i_buffer; /* drop buffer padding */ /* Interleave audio if required */ if( av_sample_fmt_is_planar( ctx->sample_fmt ) ) { aout_Interleave( p_buffer->p_buffer, p_block->p_buffer, p_block->i_nb_samples, ctx->channels, p_dec->fmt_out.audio.i_format ); if( ctx->channels > AV_NUM_DATA_POINTERS ) free( frame.extended_data ); block_Release( p_block ); p_block = p_buffer; } else /* FIXME: improve decoder_NewAudioBuffer(), avoid useless buffer... */ block_Release( p_buffer ); if (p_sys->b_extract) { /* TODO: do not drop channels... at least not here */ p_buffer = block_Alloc( p_dec->fmt_out.audio.i_bytes_per_frame * frame.nb_samples ); if( unlikely(p_buffer == NULL) ) { block_Release( p_block ); return NULL; } aout_ChannelExtract( p_buffer->p_buffer, p_dec->fmt_out.audio.i_channels, p_block->p_buffer, ctx->channels, frame.nb_samples, p_sys->pi_extraction, p_dec->fmt_out.audio.i_bitspersample ); block_Release( p_block ); p_block = p_buffer; } p_block->i_nb_samples = frame.nb_samples; p_block->i_buffer = frame.nb_samples * p_dec->fmt_out.audio.i_bytes_per_frame; p_block->i_pts = date_Get( &p_sys->end_date ); p_block->i_length = date_Increment( &p_sys->end_date, frame.nb_samples ) - p_block->i_pts; return p_block; end: block_Release(p_block); *pp_block = NULL; return NULL; }
static int Audio_GetOutput(decoder_t *p_dec, picture_t **pp_out_pic, block_t **pp_out_block, bool *p_abort, mtime_t i_timeout) { decoder_sys_t *p_sys = p_dec->p_sys; mc_api_out out; int i_ret; (void) p_abort; assert(!pp_out_pic && pp_out_block); i_ret = p_sys->api->get_out(p_sys->api, &out, i_timeout); if (i_ret != 1) return i_ret; if (out.type == MC_OUT_TYPE_BUF) { block_t *p_block = NULL; if (!p_sys->b_has_format) { msg_Warn(p_dec, "Buffers returned before output format is set, dropping frame"); return p_sys->api->release_out(p_sys->api, out.u.buf.i_index, false); } p_block = block_Alloc(out.u.buf.i_size); if (!p_block) return -1; p_block->i_nb_samples = out.u.buf.i_size / p_dec->fmt_out.audio.i_bytes_per_frame; if (p_sys->u.audio.b_extract) { aout_ChannelExtract(p_block->p_buffer, p_dec->fmt_out.audio.i_channels, out.u.buf.p_ptr, p_sys->u.audio.i_channels, p_block->i_nb_samples, p_sys->u.audio.pi_extraction, p_dec->fmt_out.audio.i_bitspersample); } else memcpy(p_block->p_buffer, out.u.buf.p_ptr, out.u.buf.i_size); if (out.u.buf.i_ts != 0 && out.u.buf.i_ts != date_Get(&p_sys->u.audio.i_end_date)) date_Set(&p_sys->u.audio.i_end_date, out.u.buf.i_ts); p_block->i_pts = date_Get(&p_sys->u.audio.i_end_date); p_block->i_length = date_Increment(&p_sys->u.audio.i_end_date, p_block->i_nb_samples) - p_block->i_pts; if (p_sys->api->release_out(p_sys->api, out.u.buf.i_index, false)) { block_Release(p_block); return -1; } *pp_out_block = p_block; return 1; } else { uint32_t i_layout_dst; int i_channels_dst; assert(out.type == MC_OUT_TYPE_CONF); if (out.u.conf.audio.channel_count <= 0 || out.u.conf.audio.channel_count > 8 || out.u.conf.audio.sample_rate <= 0) { msg_Warn( p_dec, "invalid audio properties channels count %d, sample rate %d", out.u.conf.audio.channel_count, out.u.conf.audio.sample_rate); return -1; } msg_Err(p_dec, "output: channel_count: %d, channel_mask: 0x%X, rate: %d", out.u.conf.audio.channel_count, out.u.conf.audio.channel_mask, out.u.conf.audio.sample_rate); p_dec->fmt_out.i_codec = VLC_CODEC_S16N; p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec; p_dec->fmt_out.audio.i_rate = out.u.conf.audio.sample_rate; date_Init(&p_sys->u.audio.i_end_date, out.u.conf.audio.sample_rate, 1 ); p_sys->u.audio.i_channels = out.u.conf.audio.channel_count; p_sys->u.audio.b_extract = aout_CheckChannelExtraction(p_sys->u.audio.pi_extraction, &i_layout_dst, &i_channels_dst, NULL, pi_audio_order_src, p_sys->u.audio.i_channels); if (p_sys->u.audio.b_extract) msg_Warn(p_dec, "need channel extraction: %d -> %d", p_sys->u.audio.i_channels, i_channels_dst); p_dec->fmt_out.audio.i_original_channels = p_dec->fmt_out.audio.i_physical_channels = i_layout_dst; aout_FormatPrepare(&p_dec->fmt_out.audio); if (decoder_UpdateAudioFormat(p_dec)) return -1; p_sys->b_has_format = true; return 0; } }
/***************************************************************************** * DecodeAudio: Called to decode one frame *****************************************************************************/ static block_t *DecodeAudio( decoder_t *p_dec, block_t **pp_block ) { decoder_sys_t *p_sys = p_dec->p_sys; AVCodecContext *ctx = p_sys->p_context; AVFrame *frame = NULL; if( !pp_block || !*pp_block ) return NULL; block_t *p_block = *pp_block; if( !ctx->extradata_size && p_dec->fmt_in.i_extra && p_sys->b_delayed_open) { InitDecoderConfig( p_dec, ctx ); OpenAudioCodec( p_dec ); } if( p_sys->b_delayed_open ) goto end; if( p_block->i_flags & BLOCK_FLAG_CORRUPTED ) { Flush( p_dec ); goto end; } if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY ) { date_Set( &p_sys->end_date, VLC_TS_INVALID ); } /* We've just started the stream, wait for the first PTS. */ if( !date_Get( &p_sys->end_date ) && p_block->i_pts <= VLC_TS_INVALID ) goto end; if( p_block->i_buffer <= 0 ) goto end; if( (p_block->i_flags & BLOCK_FLAG_PRIVATE_REALLOCATED) == 0 ) { p_block = block_Realloc( p_block, 0, p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE ); if( !p_block ) return NULL; *pp_block = p_block; p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE; memset( &p_block->p_buffer[p_block->i_buffer], 0, FF_INPUT_BUFFER_PADDING_SIZE ); p_block->i_flags |= BLOCK_FLAG_PRIVATE_REALLOCATED; } frame = av_frame_alloc(); if (unlikely(frame == NULL)) goto end; for( int got_frame = 0; !got_frame; ) { if( p_block->i_buffer == 0 ) goto end; AVPacket pkt; av_init_packet( &pkt ); pkt.data = p_block->p_buffer; pkt.size = p_block->i_buffer; int ret = avcodec_send_packet( ctx, &pkt ); if( ret != 0 && ret != AVERROR(EAGAIN) ) { msg_Warn( p_dec, "cannot decode one frame (%zu bytes)", p_block->i_buffer ); goto end; } int used = ret != AVERROR(EAGAIN) ? pkt.size : 0; ret = avcodec_receive_frame( ctx, frame ); if( ret != 0 && ret != AVERROR(EAGAIN) ) { msg_Warn( p_dec, "cannot decode one frame (%zu bytes)", p_block->i_buffer ); goto end; } got_frame = ret == 0; p_block->p_buffer += used; p_block->i_buffer -= used; } if( ctx->channels <= 0 || ctx->channels > 8 || ctx->sample_rate <= 0 ) { msg_Warn( p_dec, "invalid audio properties channels count %d, sample rate %d", ctx->channels, ctx->sample_rate ); goto end; } if( p_dec->fmt_out.audio.i_rate != (unsigned int)ctx->sample_rate ) date_Init( &p_sys->end_date, ctx->sample_rate, 1 ); if( p_block->i_pts > date_Get( &p_sys->end_date ) ) { date_Set( &p_sys->end_date, p_block->i_pts ); } if( p_block->i_buffer == 0 ) { /* Done with this buffer */ block_Release( p_block ); p_block = NULL; *pp_block = NULL; } /* NOTE WELL: Beyond this point, p_block refers to the DECODED block! */ SetupOutputFormat( p_dec, true ); if( decoder_UpdateAudioFormat( p_dec ) ) goto drop; /* Interleave audio if required */ if( av_sample_fmt_is_planar( ctx->sample_fmt ) ) { p_block = block_Alloc(frame->linesize[0] * ctx->channels); if (unlikely(p_block == NULL)) goto drop; const void *planes[ctx->channels]; for (int i = 0; i < ctx->channels; i++) planes[i] = frame->extended_data[i]; aout_Interleave(p_block->p_buffer, planes, frame->nb_samples, ctx->channels, p_dec->fmt_out.audio.i_format); p_block->i_nb_samples = frame->nb_samples; av_frame_free(&frame); } else { p_block = vlc_av_frame_Wrap(frame); if (unlikely(p_block == NULL)) goto drop; frame = NULL; } if (p_sys->b_extract) { /* TODO: do not drop channels... at least not here */ block_t *p_buffer = block_Alloc( p_dec->fmt_out.audio.i_bytes_per_frame * p_block->i_nb_samples ); if( unlikely(p_buffer == NULL) ) goto drop; aout_ChannelExtract( p_buffer->p_buffer, p_dec->fmt_out.audio.i_channels, p_block->p_buffer, ctx->channels, p_block->i_nb_samples, p_sys->pi_extraction, p_dec->fmt_out.audio.i_bitspersample ); p_buffer->i_nb_samples = p_block->i_nb_samples; block_Release( p_block ); p_block = p_buffer; } /* Silent unwanted samples */ if( p_sys->i_reject_count > 0 ) { memset( p_block->p_buffer, 0, p_block->i_buffer ); p_sys->i_reject_count--; } p_block->i_buffer = p_block->i_nb_samples * p_dec->fmt_out.audio.i_bytes_per_frame; p_block->i_pts = date_Get( &p_sys->end_date ); p_block->i_length = date_Increment( &p_sys->end_date, p_block->i_nb_samples ) - p_block->i_pts; return p_block; end: *pp_block = NULL; drop: av_frame_free(&frame); if( p_block != NULL ) block_Release(p_block); return NULL; }