1 #include "audio_encoder.h"
4 #include <libavcodec/avcodec.h>
5 #include <libavformat/avformat.h>
6 #include <libswresample/swresample.h>
7 #include <libavutil/channel_layout.h>
8 #include <libavutil/error.h>
9 #include <libavutil/frame.h>
10 #include <libavutil/mem.h>
11 #include <libavutil/opt.h>
12 #include <libavutil/rational.h>
13 #include <libavutil/samplefmt.h>
25 #include "shared/mux.h"
26 #include "shared/timebase.h"
30 AudioEncoder::AudioEncoder(const string &codec_name, int bit_rate, const AVOutputFormat *oformat)
32 const AVCodec *codec = avcodec_find_encoder_by_name(codec_name.c_str());
33 if (codec == nullptr) {
34 fprintf(stderr, "ERROR: Could not find codec '%s'\n", codec_name.c_str());
38 ctx = avcodec_alloc_context3(codec);
39 ctx->bit_rate = bit_rate;
40 ctx->sample_rate = OUTPUT_FREQUENCY;
41 ctx->sample_fmt = codec->sample_fmts[0];
42 ctx->ch_layout.order = AV_CHANNEL_ORDER_NATIVE;
43 ctx->ch_layout.nb_channels = 2;
44 ctx->ch_layout.u.mask = AV_CH_LAYOUT_STEREO;
45 ctx->time_base = AVRational{1, TIMEBASE};
46 if (oformat->flags & AVFMT_GLOBALHEADER) {
47 ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
49 if (avcodec_open2(ctx, codec, NULL) < 0) {
50 fprintf(stderr, "Could not open codec '%s'\n", codec_name.c_str());
54 resampler = swr_alloc_set_opts(nullptr,
55 /*out_ch_layout=*/AV_CH_LAYOUT_STEREO,
56 /*out_sample_fmt=*/ctx->sample_fmt,
57 /*out_sample_rate=*/OUTPUT_FREQUENCY,
58 /*in_ch_layout=*/AV_CH_LAYOUT_STEREO,
59 /*in_sample_fmt=*/AV_SAMPLE_FMT_FLT,
60 /*in_sample_rate=*/OUTPUT_FREQUENCY,
63 if (resampler == nullptr) {
64 fprintf(stderr, "Allocating resampler failed.\n");
68 if (swr_init(resampler) < 0) {
69 fprintf(stderr, "Could not open resample context.\n");
73 audio_frame = av_frame_alloc();
76 AudioEncoder::~AudioEncoder()
78 av_frame_free(&audio_frame);
80 avcodec_free_context(&ctx);
83 void AudioEncoder::encode_audio(const vector<float> &audio, int64_t audio_pts)
85 if (ctx->frame_size == 0) {
86 // No queueing needed.
87 assert(audio_queue.empty());
88 assert(audio.size() % 2 == 0);
89 encode_audio_one_frame(&audio[0], audio.size() / 2, audio_pts);
93 int64_t sample_offset = audio_queue.size();
95 audio_queue.insert(audio_queue.end(), audio.begin(), audio.end());
98 sample_num + ctx->frame_size * 2 <= audio_queue.size();
99 sample_num += ctx->frame_size * 2) {
100 int64_t adjusted_audio_pts = audio_pts + (int64_t(sample_num) - sample_offset) * TIMEBASE / (OUTPUT_FREQUENCY * 2);
101 encode_audio_one_frame(&audio_queue[sample_num],
105 audio_queue.erase(audio_queue.begin(), audio_queue.begin() + sample_num);
107 last_pts = audio_pts + audio.size() * TIMEBASE / (OUTPUT_FREQUENCY * 2);
110 void AudioEncoder::encode_audio_one_frame(const float *audio, size_t num_samples, int64_t audio_pts)
112 audio_frame->pts = audio_pts;
113 audio_frame->nb_samples = num_samples;
114 audio_frame->ch_layout.order = AV_CHANNEL_ORDER_NATIVE;
115 audio_frame->ch_layout.nb_channels = 2;
116 audio_frame->ch_layout.u.mask = AV_CH_LAYOUT_STEREO;
117 audio_frame->format = ctx->sample_fmt;
118 audio_frame->sample_rate = OUTPUT_FREQUENCY;
120 if (av_samples_alloc(audio_frame->data, nullptr, 2, num_samples, ctx->sample_fmt, 0) < 0) {
121 fprintf(stderr, "Could not allocate %zu samples.\n", num_samples);
125 if (swr_convert(resampler, audio_frame->data, num_samples, reinterpret_cast<const uint8_t **>(&audio), num_samples) < 0) {
126 fprintf(stderr, "Audio conversion failed.\n");
130 int err = avcodec_send_frame(ctx, audio_frame);
132 fprintf(stderr, "avcodec_send_frame() failed with error %d\n", err);
136 for ( ;; ) { // Termination condition within loop.
138 av_init_packet(&pkt);
141 int err = avcodec_receive_packet(ctx, &pkt);
143 pkt.stream_index = 1;
145 for (Mux *mux : muxes) {
146 mux->add_packet(pkt, pkt.pts, pkt.dts);
148 av_packet_unref(&pkt);
149 } else if (err == AVERROR(EAGAIN)) {
152 fprintf(stderr, "avcodec_receive_frame() failed with error %d\n", err);
157 av_freep(&audio_frame->data[0]);
158 av_frame_unref(audio_frame);
161 void AudioEncoder::encode_last_audio()
163 if (!audio_queue.empty()) {
164 // Last frame can be whatever size we want.
165 assert(audio_queue.size() % 2 == 0);
166 encode_audio_one_frame(&audio_queue[0], audio_queue.size() / 2, last_pts);
170 if (ctx->codec->capabilities & AV_CODEC_CAP_DELAY) {
171 // Collect any delayed frames.
174 av_init_packet(&pkt);
177 int err = avcodec_receive_packet(ctx, &pkt);
179 pkt.stream_index = 1;
181 for (Mux *mux : muxes) {
182 mux->add_packet(pkt, pkt.pts, pkt.dts);
184 av_packet_unref(&pkt);
185 } else if (err == AVERROR_EOF) {
188 fprintf(stderr, "avcodec_receive_frame() failed with error %d\n", err);
195 AVCodecParametersWithDeleter AudioEncoder::get_codec_parameters()
197 AVCodecParameters *codecpar = avcodec_parameters_alloc();
198 avcodec_parameters_from_context(codecpar, ctx);
199 return AVCodecParametersWithDeleter(codecpar);