fix ffmpeg warnings. We have to copy parameters from the context to the stream AFTER we open the codec

This commit is contained in:
Isaac Connor 2018-12-03 10:33:53 -05:00
parent dbb51e9857
commit d837b42836
1 changed files with 48 additions and 76 deletions

View File

@ -238,7 +238,7 @@ VideoStore::VideoStore(const char *filename_in, const char *format_in,
avformat_new_stream(oc, (AVCodec *)audio_in_ctx->codec); avformat_new_stream(oc, (AVCodec *)audio_in_ctx->codec);
#endif #endif
if ( !audio_out_stream ) { if ( !audio_out_stream ) {
Error("Unable to create audio out stream\n"); Error("Unable to create audio out stream");
audio_out_stream = NULL; audio_out_stream = NULL;
} else { } else {
Debug(2, "setting parameters"); Debug(2, "setting parameters");
@ -265,7 +265,6 @@ VideoStore::VideoStore(const char *filename_in, const char *format_in,
Debug(2, "Setting audio codec tag to %d", Debug(2, "Setting audio codec tag to %d",
audio_out_ctx->codec_tag); audio_out_ctx->codec_tag);
} }
#else #else
audio_out_ctx = audio_out_stream->codec; audio_out_ctx = audio_out_stream->codec;
ret = avcodec_copy_context(audio_out_ctx, audio_in_ctx); ret = avcodec_copy_context(audio_out_ctx, audio_in_ctx);
@ -297,7 +296,6 @@ VideoStore::VideoStore(const char *filename_in, const char *format_in,
} }
} // end if audio_in_stream } // end if audio_in_stream
video_last_pts = 0; video_last_pts = 0;
video_last_dts = 0; video_last_dts = 0;
audio_last_pts = 0; audio_last_pts = 0;
@ -315,7 +313,6 @@ bool VideoStore::open() {
if ( ret < 0 ) { if ( ret < 0 ) {
Error("Could not open out file '%s': %s\n", filename, Error("Could not open out file '%s': %s\n", filename,
av_make_error_string(ret).c_str()); av_make_error_string(ret).c_str());
return false; return false;
} }
} }
@ -341,7 +338,7 @@ bool VideoStore::open() {
} }
if ( opts ) av_dict_free(&opts); if ( opts ) av_dict_free(&opts);
if ( ret < 0 ) { if ( ret < 0 ) {
Error("Error occurred when writing out file header to %s: %s\n", Error("Error occurred when writing out file header to %s: %s",
filename, av_make_error_string(ret).c_str()); filename, av_make_error_string(ret).c_str());
/* free the stream */ /* free the stream */
avio_closep(&oc->pb); avio_closep(&oc->pb);
@ -521,7 +518,6 @@ bool VideoStore::setup_resampler() {
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0) #if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
// audio_out_ctx = audio_out_stream->codec; // audio_out_ctx = audio_out_stream->codec;
audio_out_ctx = avcodec_alloc_context3(audio_out_codec); audio_out_ctx = avcodec_alloc_context3(audio_out_codec);
if ( !audio_out_ctx ) { if ( !audio_out_ctx ) {
Error("could not allocate codec ctx for AAC"); Error("could not allocate codec ctx for AAC");
audio_out_stream = NULL; audio_out_stream = NULL;
@ -530,7 +526,7 @@ bool VideoStore::setup_resampler() {
Debug(2, "Have audio_out_ctx"); Debug(2, "Have audio_out_ctx");
// Now copy them to the out stream // Now copy them to the out stream
audio_out_stream = avformat_new_stream(oc, NULL); audio_out_stream = avformat_new_stream(oc, audio_out_codec);
#else #else
audio_out_stream = avformat_new_stream(oc, NULL); audio_out_stream = avformat_new_stream(oc, NULL);
audio_out_ctx = audio_out_stream->codec; audio_out_ctx = audio_out_stream->codec;
@ -556,8 +552,7 @@ bool VideoStore::setup_resampler() {
if ( audio_out_codec->supported_samplerates ) { if ( audio_out_codec->supported_samplerates ) {
int found = 0; int found = 0;
for (unsigned int i = 0; audio_out_codec->supported_samplerates[i]; for ( unsigned int i = 0; audio_out_codec->supported_samplerates[i]; i++) {
i++) {
if ( audio_out_ctx->sample_rate == if ( audio_out_ctx->sample_rate ==
audio_out_codec->supported_samplerates[i] ) { audio_out_codec->supported_samplerates[i] ) {
found = 1; found = 1;
@ -569,7 +564,7 @@ bool VideoStore::setup_resampler() {
} else { } else {
audio_out_ctx->sample_rate = audio_out_ctx->sample_rate =
audio_out_codec->supported_samplerates[0]; audio_out_codec->supported_samplerates[0];
Debug(1, "Sampel rate is no good, setting to (%d)", Debug(1, "Sample rate is no good, setting to (%d)",
audio_out_codec->supported_samplerates[0]); audio_out_codec->supported_samplerates[0]);
} }
} }
@ -584,16 +579,6 @@ bool VideoStore::setup_resampler() {
audio_out_ctx->time_base = audio_out_ctx->time_base =
(AVRational){1, audio_out_ctx->sample_rate}; (AVRational){1, audio_out_ctx->sample_rate};
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
ret = avcodec_parameters_from_context(audio_out_stream->codecpar,
audio_out_ctx);
if (ret < 0) {
Error("Could not initialize stream parameteres");
return false;
}
#endif
AVDictionary *opts = NULL; AVDictionary *opts = NULL;
if ( (ret = av_dict_set(&opts, "strict", "experimental", 0)) < 0 ) { if ( (ret = av_dict_set(&opts, "strict", "experimental", 0)) < 0 ) {
Error("Couldn't set experimental"); Error("Couldn't set experimental");
@ -608,6 +593,15 @@ bool VideoStore::setup_resampler() {
return false; return false;
} }
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
ret = avcodec_parameters_from_context(
audio_out_stream->codecpar, audio_out_ctx);
if ( ret < 0 ) {
Error("Could not initialize stream parameteres");
return false;
}
#endif
Debug(1, Debug(1,
"Audio out bit_rate (%d) sample_rate(%d) channels(%d) fmt(%d) " "Audio out bit_rate (%d) sample_rate(%d) channels(%d) fmt(%d) "
"layout(%d) frame_size(%d)", "layout(%d) frame_size(%d)",
@ -631,19 +625,19 @@ bool VideoStore::setup_resampler() {
// Setup the audio resampler // Setup the audio resampler
resample_ctx = avresample_alloc_context(); resample_ctx = avresample_alloc_context();
if ( !resample_ctx ) { if ( !resample_ctx ) {
Error("Could not allocate resample ctx\n"); Error("Could not allocate resample ctx");
return false; return false;
} }
// Some formats (i.e. WAV) do not produce the proper channel layout // Some formats (i.e. WAV) do not produce the proper channel layout
if (audio_in_ctx->channel_layout == 0) {
uint64_t layout = av_get_channel_layout("mono"); uint64_t layout = av_get_channel_layout("mono");
av_opt_set_int(resample_ctx, "in_channel_layout", if ( audio_in_ctx->channel_layout == 0 ) {
av_get_channel_layout("mono"), 0); av_opt_set_int(resample_ctx, "in_channel_layout", layout, 0);
Debug(1, "Bad channel layout. Need to set it to mono (%d).", layout); Debug(1, "Bad in channel layout. Need to set it to mono (%d).", layout);
} else { } else {
av_opt_set_int(resample_ctx, "in_channel_layout", av_opt_set_int(resample_ctx, "in_channel_layout",
audio_in_ctx->channel_layout, 0); audio_in_ctx->channel_layout, 0);
layout = audio_in_ctx->channel_layout;
} }
av_opt_set_int(resample_ctx, "in_sample_fmt", av_opt_set_int(resample_ctx, "in_sample_fmt",
@ -655,7 +649,7 @@ bool VideoStore::setup_resampler() {
// av_opt_set_int( resample_ctx, "out_channel_layout", // av_opt_set_int( resample_ctx, "out_channel_layout",
// audio_out_ctx->channel_layout, 0); // audio_out_ctx->channel_layout, 0);
av_opt_set_int(resample_ctx, "out_channel_layout", av_opt_set_int(resample_ctx, "out_channel_layout",
av_get_channel_layout("mono"), 0); layout, 0);
av_opt_set_int(resample_ctx, "out_sample_fmt", av_opt_set_int(resample_ctx, "out_sample_fmt",
audio_out_ctx->sample_fmt, 0); audio_out_ctx->sample_fmt, 0);
av_opt_set_int(resample_ctx, "out_sample_rate", av_opt_set_int(resample_ctx, "out_sample_rate",
@ -665,37 +659,12 @@ bool VideoStore::setup_resampler() {
ret = avresample_open(resample_ctx); ret = avresample_open(resample_ctx);
if ( ret < 0 ) { if ( ret < 0 ) {
Error("Could not open resample ctx\n"); Error("Could not open resample ctx");
return false; return false;
} else {
Debug(2, "Success opening resampler");
} }
#if 0
/**
* Allocate as many pointers as there are audio channels.
* Each pointer will later point to the audio samples of the corresponding
* channels (although it may be NULL for interleaved formats).
*/
if (!( converted_in_samples = reinterpret_cast<uint8_t *>calloc( audio_out_ctx->channels, sizeof(*converted_in_samples))) ) {
Error("Could not allocate converted in sample pointers\n");
return;
}
/**
* Allocate memory for the samples of all channels in one consecutive
* block for convenience.
*/
if ( (ret = av_samples_alloc( &converted_in_samples, NULL,
audio_out_ctx->channels,
audio_out_ctx->frame_size,
audio_out_ctx->sample_fmt, 0)) < 0 ) {
Error("Could not allocate converted in samples (error '%s')\n",
av_make_error_string(ret).c_str());
av_freep(converted_in_samples);
free(converted_in_samples);
return;
}
#endif
out_frame->nb_samples = audio_out_ctx->frame_size; out_frame->nb_samples = audio_out_ctx->frame_size;
out_frame->format = audio_out_ctx->sample_fmt; out_frame->format = audio_out_ctx->sample_fmt;
out_frame->channel_layout = audio_out_ctx->channel_layout; out_frame->channel_layout = audio_out_ctx->channel_layout;
@ -703,13 +672,16 @@ bool VideoStore::setup_resampler() {
// The codec gives us the frame size, in samples, we calculate the size of the // The codec gives us the frame size, in samples, we calculate the size of the
// samples buffer in bytes // samples buffer in bytes
unsigned int audioSampleBuffer_size = av_samples_get_buffer_size( unsigned int audioSampleBuffer_size = av_samples_get_buffer_size(
NULL, audio_out_ctx->channels, audio_out_ctx->frame_size, NULL, audio_out_ctx->channels,
audio_out_ctx->frame_size,
audio_out_ctx->sample_fmt, 0); audio_out_ctx->sample_fmt, 0);
converted_in_samples = (uint8_t *)av_malloc(audioSampleBuffer_size); converted_in_samples = (uint8_t *)av_malloc(audioSampleBuffer_size);
if ( !converted_in_samples ) { if ( !converted_in_samples ) {
Error("Could not allocate converted in sample pointers\n"); Error("Could not allocate converted in sample pointers");
return false; return false;
} else {
Debug(2, "Frame Size %d, sample buffer size %d", audio_out_ctx->frame_size, audioSampleBuffer_size);
} }
// Setup the data pointers in the AVFrame // Setup the data pointers in the AVFrame
@ -717,7 +689,7 @@ bool VideoStore::setup_resampler() {
audio_out_ctx->sample_fmt, audio_out_ctx->sample_fmt,
(const uint8_t *)converted_in_samples, (const uint8_t *)converted_in_samples,
audioSampleBuffer_size, 0) < 0) { audioSampleBuffer_size, 0) < 0) {
Error("Could not allocate converted in sample pointers\n"); Error("Could not allocate converted in sample pointers");
return false; return false;
} }