add an audio_fifo to handle input audio < 1024 samples per frame
This commit is contained in:
parent
4e463c9354
commit
96906734b8
|
@ -210,6 +210,9 @@ VideoStore::VideoStore(
|
|||
out_frame = NULL;
|
||||
#if defined(HAVE_LIBSWRESAMPLE) || defined(HAVE_LIBAVRESAMPLE)
|
||||
resample_ctx = NULL;
|
||||
#if defined(HAVE_LIBSWRESAMPLE)
|
||||
fifo = NULL;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if ( audio_in_stream ) {
|
||||
|
@ -415,7 +418,7 @@ VideoStore::~VideoStore() {
|
|||
|
||||
Debug(1,"Writing trailer");
|
||||
/* Write the trailer before close */
|
||||
if (int rc = av_write_trailer(oc)) {
|
||||
if ( int rc = av_write_trailer(oc) ) {
|
||||
Error("Error writing trailer %s", av_err2str(rc));
|
||||
} else {
|
||||
Debug(3, "Success Writing trailer");
|
||||
|
@ -471,6 +474,10 @@ VideoStore::~VideoStore() {
|
|||
#if defined(HAVE_LIBAVRESAMPLE) || defined(HAVE_LIBSWRESAMPLE)
|
||||
if ( resample_ctx ) {
|
||||
#if defined(HAVE_LIBSWRESAMPLE)
|
||||
if ( fifo ) {
|
||||
av_audio_fifo_free(fifo);
|
||||
fifo = NULL;
|
||||
}
|
||||
swr_free(&resample_ctx);
|
||||
#else
|
||||
#if defined(HAVE_LIBAVRESAMPLE)
|
||||
|
@ -618,6 +625,12 @@ bool VideoStore::setup_resampler() {
|
|||
}
|
||||
#endif
|
||||
|
||||
Debug(1,
|
||||
"Audio in bit_rate (%d) sample_rate(%d) channels(%d) fmt(%d) "
|
||||
"layout(%d) frame_size(%d)",
|
||||
audio_in_ctx->bit_rate, audio_in_ctx->sample_rate,
|
||||
audio_in_ctx->channels, audio_in_ctx->sample_fmt,
|
||||
audio_in_ctx->channel_layout, audio_in_ctx->frame_size);
|
||||
Debug(1,
|
||||
"Audio out bit_rate (%d) sample_rate(%d) channels(%d) fmt(%d) "
|
||||
"layout(%d) frame_size(%d)",
|
||||
|
@ -639,6 +652,12 @@ bool VideoStore::setup_resampler() {
|
|||
}
|
||||
|
||||
#if defined(HAVE_LIBSWRESAMPLE)
|
||||
if (!(fifo = av_audio_fifo_alloc(
|
||||
audio_out_ctx->sample_fmt,
|
||||
audio_out_ctx->channels, 1))) {
|
||||
Error("Could not allocate FIFO");
|
||||
return false;
|
||||
}
|
||||
resample_ctx = swr_alloc_set_opts(NULL,
|
||||
audio_out_ctx->channel_layout,
|
||||
audio_out_ctx->sample_fmt,
|
||||
|
@ -933,7 +952,7 @@ int VideoStore::writeAudioFramePacket(AVPacket *ipkt) {
|
|||
return 0;
|
||||
}
|
||||
#endif
|
||||
int frame_size = out_frame->nb_samples;
|
||||
int frame_size = in_frame->nb_samples;
|
||||
|
||||
// Resample the in into the audioSampleBuffer until we proceed the whole
|
||||
// decoded data
|
||||
|
@ -947,23 +966,49 @@ int VideoStore::writeAudioFramePacket(AVPacket *ipkt) {
|
|||
);
|
||||
#else
|
||||
ret = swr_convert_frame(resample_ctx, out_frame, in_frame);
|
||||
|
||||
#endif
|
||||
#else
|
||||
#if defined(HAVE_LIBAVRESAMPLE)
|
||||
(ret = avresample_convert(resample_ctx, NULL, 0, 0, in_frame->data,
|
||||
0, in_frame->nb_samples))
|
||||
#endif
|
||||
#endif
|
||||
//out_frame->pts = in_frame->pts;
|
||||
av_frame_unref(in_frame);
|
||||
if ( ret < 0 ) {
|
||||
Error("Could not resample frame (error '%s')",
|
||||
av_make_error_string(ret).c_str());
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
if ((ret = av_audio_fifo_realloc(fifo, av_audio_fifo_size(fifo) + out_frame->nb_samples)) < 0) {
|
||||
Error("Could not reallocate FIFO");
|
||||
return 0;
|
||||
}
|
||||
/** Store the new samples in the FIFO buffer. */
|
||||
ret = av_audio_fifo_write(fifo, (void **)out_frame->data, out_frame->nb_samples);
|
||||
if ( ret < frame_size ) {
|
||||
Error("Could not write data to FIFO on %d written", ret);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Reset frame_size to output_frame_size
|
||||
frame_size = audio_out_ctx->frame_size;
|
||||
|
||||
// AAC requires 1024 samples per encode. Our input tends to be 160, so need to buffer them.
|
||||
if ( frame_size > av_audio_fifo_size(fifo) ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ( av_audio_fifo_read(fifo, (void **)out_frame->data, frame_size) < frame_size ) {
|
||||
Error("Could not read data from FIFO");
|
||||
return 0;
|
||||
}
|
||||
out_frame->nb_samples = frame_size;
|
||||
/// FIXME this is not the correct pts
|
||||
out_frame->pts = in_frame->pts;
|
||||
#else
|
||||
#if defined(HAVE_LIBAVRESAMPLE)
|
||||
(ret = avresample_convert(resample_ctx, NULL, 0, 0, in_frame->data,
|
||||
0, in_frame->nb_samples))
|
||||
av_frame_unref(in_frame);
|
||||
if ( ret < 0 ) {
|
||||
Error("Could not resample frame (error '%s')",
|
||||
av_make_error_string(ret).c_str());
|
||||
return 0;
|
||||
}
|
||||
int samples_available = avresample_available(resample_ctx);
|
||||
|
||||
if ( samples_available < frame_size ) {
|
||||
|
@ -979,6 +1024,7 @@ int VideoStore::writeAudioFramePacket(AVPacket *ipkt) {
|
|||
return 0;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
Debug(2,
|
||||
"Out Frame: samples(%d), format(%d), sample_rate(%d), "
|
||||
"channels(%d) channel layout(%d) pts(%" PRId64 ")",
|
||||
|
@ -1003,7 +1049,7 @@ int VideoStore::writeAudioFramePacket(AVPacket *ipkt) {
|
|||
|
||||
if ( (ret = avcodec_receive_packet(audio_out_ctx, &opkt)) < 0 ) {
|
||||
if ( AVERROR(EAGAIN) == ret ) {
|
||||
// THe codec may need more samples than it has, perfectly valid
|
||||
// The codec may need more samples than it has, perfectly valid
|
||||
Debug(3, "Could not recieve packet (error '%s')",
|
||||
av_make_error_string(ret).c_str());
|
||||
} else {
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
extern "C" {
|
||||
#ifdef HAVE_LIBSWRESAMPLE
|
||||
#include "libswresample/swresample.h"
|
||||
#include "libavutil/audio_fifo.h"
|
||||
#else
|
||||
#ifdef HAVE_LIBAVRESAMPLE
|
||||
#include "libavresample/avresample.h"
|
||||
|
@ -44,6 +45,7 @@ private:
|
|||
AVCodecContext *audio_out_ctx;
|
||||
#ifdef HAVE_LIBSWRESAMPLE
|
||||
SwrContext *resample_ctx;
|
||||
AVAudioFifo *fifo;
|
||||
#else
|
||||
#ifdef HAVE_LIBAVRESAMPLE
|
||||
AVAudioResampleContext* resample_ctx;
|
||||
|
|
Loading…
Reference in New Issue