X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=nageru%2Faudio_encoder.cpp;h=126e0e2dce19ef40f0406bea7a3d4abed120c731;hb=e4037149c9a6fbbd6b3747cdbd0909c0ca3b6382;hp=f9faf42c349fdd926eb9f54263eb2f04d81240c7;hpb=131a051c4cd3719a9be415386fdf0f4e15da7c66;p=nageru diff --git a/nageru/audio_encoder.cpp b/nageru/audio_encoder.cpp index f9faf42..126e0e2 100644 --- a/nageru/audio_encoder.cpp +++ b/nageru/audio_encoder.cpp @@ -3,7 +3,7 @@ extern "C" { #include #include -#include +#include #include #include #include @@ -29,43 +29,46 @@ using namespace std; AudioEncoder::AudioEncoder(const string &codec_name, int bit_rate, const AVOutputFormat *oformat) { - AVCodec *codec = avcodec_find_encoder_by_name(codec_name.c_str()); + const AVCodec *codec = avcodec_find_encoder_by_name(codec_name.c_str()); if (codec == nullptr) { fprintf(stderr, "ERROR: Could not find codec '%s'\n", codec_name.c_str()); - exit(1); + abort(); } ctx = avcodec_alloc_context3(codec); ctx->bit_rate = bit_rate; ctx->sample_rate = OUTPUT_FREQUENCY; ctx->sample_fmt = codec->sample_fmts[0]; - ctx->channels = 2; - ctx->channel_layout = AV_CH_LAYOUT_STEREO; + ctx->ch_layout.order = AV_CHANNEL_ORDER_NATIVE; + ctx->ch_layout.nb_channels = 2; + ctx->ch_layout.u.mask = AV_CH_LAYOUT_STEREO; ctx->time_base = AVRational{1, TIMEBASE}; if (oformat->flags & AVFMT_GLOBALHEADER) { ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; } if (avcodec_open2(ctx, codec, NULL) < 0) { fprintf(stderr, "Could not open codec '%s'\n", codec_name.c_str()); - exit(1); + abort(); } - resampler = avresample_alloc_context(); - if (resampler == nullptr) { + resampler = nullptr; + int ok = swr_alloc_set_opts2(&resampler, + /*out_ch_layout=*/&ctx->ch_layout, + /*out_sample_fmt=*/ctx->sample_fmt, + /*out_sample_rate=*/OUTPUT_FREQUENCY, + /*in_ch_layout=*/&ctx->ch_layout, + /*in_sample_fmt=*/AV_SAMPLE_FMT_FLT, + /*in_sample_rate=*/OUTPUT_FREQUENCY, + /*log_offset=*/0, + /*log_ctx=*/nullptr); + if (ok != 0) { fprintf(stderr, "Allocating resampler failed.\n"); - exit(1); + abort(); } - av_opt_set_int(resampler, "in_channel_layout", AV_CH_LAYOUT_STEREO, 0); - av_opt_set_int(resampler, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0); - av_opt_set_int(resampler, "in_sample_rate", OUTPUT_FREQUENCY, 0); - av_opt_set_int(resampler, "out_sample_rate", OUTPUT_FREQUENCY, 0); - av_opt_set_int(resampler, "in_sample_fmt", AV_SAMPLE_FMT_FLT, 0); - av_opt_set_int(resampler, "out_sample_fmt", ctx->sample_fmt, 0); - - if (avresample_open(resampler) < 0) { + if (swr_init(resampler) < 0) { fprintf(stderr, "Could not open resample context.\n"); - exit(1); + abort(); } audio_frame = av_frame_alloc(); @@ -74,7 +77,7 @@ AudioEncoder::AudioEncoder(const string &codec_name, int bit_rate, const AVOutpu AudioEncoder::~AudioEncoder() { av_frame_free(&audio_frame); - avresample_free(&resampler); + swr_free(&resampler); avcodec_free_context(&ctx); } @@ -109,25 +112,26 @@ void AudioEncoder::encode_audio_one_frame(const float *audio, size_t num_samples { audio_frame->pts = audio_pts; audio_frame->nb_samples = num_samples; - audio_frame->channel_layout = AV_CH_LAYOUT_STEREO; + audio_frame->ch_layout.order = AV_CHANNEL_ORDER_NATIVE; + audio_frame->ch_layout.nb_channels = 2; + audio_frame->ch_layout.u.mask = AV_CH_LAYOUT_STEREO; audio_frame->format = ctx->sample_fmt; audio_frame->sample_rate = OUTPUT_FREQUENCY; if (av_samples_alloc(audio_frame->data, nullptr, 2, num_samples, ctx->sample_fmt, 0) < 0) { - fprintf(stderr, "Could not allocate %ld samples.\n", num_samples); - exit(1); + fprintf(stderr, "Could not allocate %zu samples.\n", num_samples); + abort(); } - if (avresample_convert(resampler, audio_frame->data, 0, num_samples, - (uint8_t **)&audio, 0, num_samples) < 0) { + if (swr_convert(resampler, audio_frame->data, num_samples, reinterpret_cast(&audio), num_samples) < 0) { fprintf(stderr, "Audio conversion failed.\n"); - exit(1); + abort(); } int err = avcodec_send_frame(ctx, audio_frame); if (err < 0) { fprintf(stderr, "avcodec_send_frame() failed with error %d\n", err); - exit(1); + abort(); } for ( ;; ) { // Termination condition within loop. @@ -147,7 +151,7 @@ void AudioEncoder::encode_audio_one_frame(const float *audio, size_t num_samples break; } else { fprintf(stderr, "avcodec_receive_frame() failed with error %d\n", err); - exit(1); + abort(); } } @@ -183,7 +187,7 @@ void AudioEncoder::encode_last_audio() break; } else { fprintf(stderr, "avcodec_receive_frame() failed with error %d\n", err); - exit(1); + abort(); } } }