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>
#include <vector>
#include "defs.h"
-#include "mux.h"
-#include "timebase.h"
+#include "shared/mux.h"
+#include "shared/timebase.h"
using namespace std;
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);
}
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();
AudioEncoder::~AudioEncoder()
{
av_frame_free(&audio_frame);
- avresample_free(&resampler);
+ swr_free(&resampler);
avcodec_free_context(&ctx);
}
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.
break;
} else {
fprintf(stderr, "avcodec_receive_frame() failed with error %d\n", err);
- exit(1);
+ abort();
}
}
break;
} else {
fprintf(stderr, "avcodec_receive_frame() failed with error %d\n", err);
- exit(1);
+ abort();
}
}
}