5 #include <libavcodec/avcodec.h>
6 #include <libavformat/avformat.h>
7 #include <libswresample/swresample.h>
8 #include <libavutil/avutil.h>
17 struct AVFormatCloserAndDeleter {
18 void operator() (AVFormatContext *ctx) {
19 avformat_close_input(&ctx);
20 avformat_free_context(ctx);
24 struct AVCodecContextDeleter {
25 void operator() (AVCodecContext *ctx) {
31 struct SwrContextDeleter {
32 void operator() (SwrContext *swr) {
37 struct AVPacketDeleter {
38 void operator() (AVPacket *pkt) {
43 struct AVFrameDeleter {
44 void operator() (AVFrame *frame) {
45 av_frame_free(&frame);
49 struct AVSampleDeleter {
50 void operator() (uint8_t *data) {
55 void convert_samples(SwrContext *swr, int sample_rate, const uint8_t **data, int nb_samples, std::vector<float> *samples)
57 int max_out_samples = nb_samples + swr_get_delay(swr, sample_rate);
58 if (max_out_samples == 0) {
62 av_samples_alloc(&output, nullptr, 1, max_out_samples, AV_SAMPLE_FMT_FLT, 0);
63 std::unique_ptr<uint8_t, AVSampleDeleter> output_deleter(output);
65 int out_samples = swr_convert(swr, &output, max_out_samples, data, nb_samples);
66 if (out_samples > 0) {
67 const float* start = reinterpret_cast<const float *>(output);
68 const float* end = start + out_samples;
69 samples->insert(samples->end(), start, end);
73 int decode_packet(const char *filename, AVCodecContext *codec_ctx, SwrContext *swr, AVFrame *audio_frame, AVPacket *packet, int *got_frame, std::vector<float> *samples)
76 int len1 = avcodec_decode_audio4(codec_ctx, audio_frame, got_frame, packet);
77 if (len1 < 0 || !*got_frame) {
81 if (audio_frame->channel_layout != codec_ctx->channel_layout ||
82 audio_frame->sample_rate != codec_ctx->sample_rate) {
83 fprintf(stderr, "%s: Channel layout or sample rate changed mid-file\n", filename);
87 convert_samples(swr, codec_ctx->sample_rate, (const uint8_t **)audio_frame->data, audio_frame->nb_samples, samples);
93 bool read_audio_file(const char *filename, std::vector<float> *samples, int *sample_rate)
97 AVFormatContext *format_ctx = nullptr;
98 if (avformat_open_input(&format_ctx, filename, nullptr, nullptr) != 0) {
99 fprintf(stderr, "Couldn't open %s\n", filename);
102 std::unique_ptr<AVFormatContext, AVFormatCloserAndDeleter> format_ctx_closer(format_ctx);
104 if (avformat_find_stream_info(format_ctx, nullptr) < 0) {
105 fprintf(stderr, "%s: Couldn't find stream information\n", filename);
109 // Find the first audio stream.
110 int audio_stream_index = -1;
111 for (unsigned i = 0; i < format_ctx->nb_streams; ++i) {
112 if (format_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
113 audio_stream_index = i;
117 if (audio_stream_index == -1) {
118 fprintf(stderr, "%s: Couldn't find an audio stream\n", filename);
122 AVCodec *codec = avcodec_find_decoder(format_ctx->streams[audio_stream_index]->codec->codec_id);
123 if (codec == nullptr) {
124 fprintf(stderr, "%s: Unsupported codec\n", filename);
128 AVCodecContext *codec_ctx = avcodec_alloc_context3(codec);
129 std::unique_ptr<AVCodecContext, AVCodecContextDeleter> codec_ctx_deleter(codec_ctx);
130 if (avcodec_copy_context(codec_ctx, format_ctx->streams[audio_stream_index]->codec) != 0) {
131 fprintf(stderr, "%s: Couldn't copy codec context\n", filename);
135 if (avcodec_open2(codec_ctx, codec, nullptr) < 0) {
136 fprintf(stderr, "%s: Couldn't open codec\n", filename);
140 // Init resampler (to downmix to mono and convert to s16).
141 if (codec_ctx->channel_layout == 0) {
142 codec_ctx->channel_layout = av_get_default_channel_layout(codec_ctx->channels);
144 SwrContext *swr = swr_alloc_set_opts(
146 AV_CH_LAYOUT_MONO, AV_SAMPLE_FMT_FLT, codec_ctx->sample_rate,
147 codec_ctx->channel_layout, codec_ctx->sample_fmt, codec_ctx->sample_rate,
149 std::unique_ptr<SwrContext, SwrContextDeleter> swr_deleter(swr);
150 if (swr_init(swr) < 0) {
151 fprintf(stderr, "%s: Couldn't initialize resampler\n", filename);
156 AVFrame* audio_frame = av_frame_alloc();
157 std::unique_ptr<AVFrame, AVFrameDeleter> audio_frame_deleter(audio_frame);
158 while (av_read_frame(format_ctx, &packet) >= 0) {
159 std::unique_ptr<AVPacket, AVPacketDeleter> av_packet_deleter(&packet);
161 if (packet.stream_index != audio_stream_index) {
165 while (packet.size > 0) {
167 int len1 = decode_packet(filename, codec_ctx, swr, audio_frame, &packet, &got_frame, samples);
169 fprintf(stderr, "%s: Couldn't decode audio\n", filename);
180 // Flush any delayed data from the end.
181 packet.data = nullptr;
185 int len1 = decode_packet(filename, codec_ctx, swr, audio_frame, &packet, &got_frame, samples);
187 fprintf(stderr, "%s: Couldn't decode audio\n", filename);
192 // Convert any leftover samples from the converter.
193 convert_samples(swr, codec_ctx->sample_rate, nullptr, 0, samples);
195 *sample_rate = codec_ctx->sample_rate;