]> git.sesse.net Git - nageru/blobdiff - nageru/audio_encoder.cpp
Fix an audio encoder issue with newer FFmpeg.
[nageru] / nageru / audio_encoder.cpp
index daeb8a81b72eb2e7a13a8f6a7b3c9833003d9805..b236c002e7df44288651c7e6557afbb35844dcc7 100644 (file)
@@ -3,7 +3,7 @@
 extern "C" {
 #include <libavcodec/avcodec.h>
 #include <libavformat/avformat.h>
-#include <libavresample/avresample.h>
+#include <libswresample/swresample.h>
 #include <libavutil/channel_layout.h>
 #include <libavutil/error.h>
 #include <libavutil/frame.h>
@@ -22,7 +22,7 @@ extern "C" {
 #include <vector>
 
 #include "defs.h"
-#include "mux.h"
+#include "shared/mux.h"
 #include "shared/timebase.h"
 
 using namespace std;
@@ -32,7 +32,7 @@ AudioEncoder::AudioEncoder(const string &codec_name, int bit_rate, const AVOutpu
        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);
@@ -47,25 +47,26 @@ AudioEncoder::AudioEncoder(const string &codec_name, int bit_rate, const AVOutpu
        }
        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();
+       resampler = swr_alloc_set_opts(nullptr,
+                                      /*out_ch_layout=*/AV_CH_LAYOUT_STEREO,
+                                      /*out_sample_fmt=*/ctx->sample_fmt,
+                                      /*out_sample_rate=*/OUTPUT_FREQUENCY,
+                                      /*in_ch_layout=*/AV_CH_LAYOUT_STEREO,
+                                      /*in_sample_fmt=*/AV_SAMPLE_FMT_FLT,
+                                      /*in_sample_rate=*/OUTPUT_FREQUENCY,
+                                      /*log_offset=*/0,
+                                      /*log_ctx=*/nullptr);
        if (resampler == nullptr) {
                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 +75,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 +110,25 @@ 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->channels = 2;
        audio_frame->channel_layout = 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<const uint8_t **>(&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 +148,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 +184,7 @@ void AudioEncoder::encode_last_audio()
                                break;
                        } else {
                                fprintf(stderr, "avcodec_receive_frame() failed with error %d\n", err);
-                               exit(1);
+                               abort();
                        }
                }
        }