This commit is contained in:
Isaac Connor 2019-02-15 17:52:51 -05:00
commit 9656032906
2 changed files with 87 additions and 44 deletions

View File

@ -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 ) {
@ -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)
@ -602,7 +609,7 @@ bool VideoStore::setup_resampler() {
ret = avcodec_open2(audio_out_ctx, audio_out_codec, &opts);
av_dict_free(&opts);
if ( ret < 0 ) {
Error("could not open codec (%d) (%s)\n", ret, av_make_error_string(ret).c_str());
Error("could not open codec (%d) (%s)", ret, av_make_error_string(ret).c_str());
audio_out_codec = NULL;
audio_out_ctx = NULL;
audio_out_stream = NULL;
@ -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,
@ -749,9 +768,7 @@ int VideoStore::writeVideoFramePacket(AVPacket *ipkt) {
av_rescale_q(ipkt->pts - video_last_pts, video_in_stream->time_base,
video_out_stream->time_base);
Debug(1, "duration calc: pts(%" PRId64 ") - last_pts(% " PRId64 ") = (%" PRId64 ")",
ipkt->pts,
video_last_pts,
duration);
ipkt->pts, video_last_pts, duration);
if ( duration <= 0 ) {
duration = ipkt->duration ? ipkt->duration : av_rescale_q(1,video_in_stream->time_base, video_out_stream->time_base);
}
@ -834,10 +851,8 @@ int VideoStore::writeVideoFramePacket(AVPacket *ipkt) {
opkt.flags = ipkt->flags;
opkt.pos = -1;
opkt.data = ipkt->data;
opkt.size = ipkt->size;
opkt.stream_index = video_out_stream->index;
AVPacket safepkt;
@ -864,7 +879,6 @@ int VideoStore::writeVideoFramePacket(AVPacket *ipkt) {
// and get on with the next
Warning(
"%s:%d: Writing frame [av_interleaved_write_frame()] failed: %s(%d)"
" ",
__FILE__, __LINE__, av_make_error_string(ret).c_str(), ret);
dumpPacket(&safepkt);
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
@ -932,36 +946,63 @@ int VideoStore::writeAudioFramePacket(AVPacket *ipkt) {
return 0;
}
#endif
int frame_size = in_frame->nb_samples;
// Resample the in into the audioSampleBuffer until we proceed the whole
// decoded data
zm_dump_frame(in_frame, "In frame");
zm_dump_frame(out_frame, "Out frame before resample");
Debug(2, "Converting %d to %d samples", in_frame->nb_samples, out_frame->nb_samples);
#if defined(HAVE_LIBSWRESAMPLE)
#if 0
(ret = swr_convert(resample_ctx,
ret = swr_convert(resample_ctx,
out_frame->data, frame_size,
(const uint8_t**)in_frame->data,
in_frame->nb_samples
))
);
#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 ) {
@ -976,10 +1017,10 @@ int VideoStore::writeAudioFramePacket(AVPacket *ipkt) {
return 0;
}
#endif
#endif
zm_dump_frame(out_frame, "Out frame after resample");
av_init_packet(&opkt);
Debug(5, "after init packet");
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
if ( (ret = avcodec_send_frame(audio_out_ctx, out_frame)) < 0 ) {
@ -993,7 +1034,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 {
@ -1093,7 +1134,7 @@ int VideoStore::writeAudioFramePacket(AVPacket *ipkt) {
// I wonder if we could just use duration instead of all the hoop jumping
// above?
//
if ( out_frame ) {
opkt.duration = out_frame->nb_samples;
} else {
@ -1101,8 +1142,8 @@ int VideoStore::writeAudioFramePacket(AVPacket *ipkt) {
}
// opkt.duration = av_rescale_q(ipkt->duration, audio_in_stream->time_base,
// audio_out_stream->time_base);
Debug(2, "opkt.pts (%d), opkt.dts(%d) opkt.duration = (%d)", opkt.pts,
opkt.dts, opkt.duration);
Debug(2, "opkt.pts (%d), opkt.dts(%d) opkt.duration = (%d)",
opkt.pts, opkt.dts, opkt.duration);
// pkt.pos: byte position in stream, -1 if unknown
opkt.pos = -1;
@ -1114,7 +1155,7 @@ int VideoStore::writeAudioFramePacket(AVPacket *ipkt) {
memcpy(&safepkt, &opkt, sizeof(AVPacket));
ret = av_interleaved_write_frame(oc, &opkt);
if ( ret != 0 ) {
Error("Error writing audio frame packet: %s\n",
Error("Error writing audio frame packet: %s",
av_make_error_string(ret).c_str());
dumpPacket(&safepkt);
} else {

View File

@ -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;