X-Git-Url: https://git.sesse.net/?p=c64tapwav;a=blobdiff_plain;f=audioreader.cpp;h=1f95f131e495d6020efc7ecd3a0c391a896d952d;hp=f0d07d8c08741acde3d13197f50503128a5bcdc9;hb=6d341e3e890484f23ce2b4ab057f344f13e32811;hpb=16e71fe97173f6d8c7c1c4836eea04d460174912 diff --git a/audioreader.cpp b/audioreader.cpp index f0d07d8..1f95f13 100644 --- a/audioreader.cpp +++ b/audioreader.cpp @@ -2,9 +2,12 @@ extern "C" { +#define __STDC_CONSTANT_MACROS + #include #include #include +#include } @@ -22,7 +25,8 @@ struct AVFormatCloserAndDeleter { struct AVCodecContextDeleter { void operator() (AVCodecContext *ctx) { - avcodec_free_context(&ctx); + avcodec_close(ctx); + av_freep(&ctx); } }; @@ -38,11 +42,13 @@ struct AVPacketDeleter { } }; +#if (LIBAVCODEC_VERSION_MAJOR >= 55) struct AVFrameDeleter { void operator() (AVFrame *frame) { av_frame_free(&frame); } }; +#endif struct AVSampleDeleter { void operator() (uint8_t *data) { @@ -50,25 +56,25 @@ struct AVSampleDeleter { } }; -void convert_samples(SwrContext *swr, int sample_rate, const uint8_t **data, int nb_samples, std::vector *samples) +void convert_samples(SwrContext *swr, int sample_rate, const uint8_t **data, int nb_samples, std::vector *samples) { int max_out_samples = nb_samples + swr_get_delay(swr, sample_rate); if (max_out_samples == 0) { return; } uint8_t *output; - av_samples_alloc(&output, nullptr, 1, max_out_samples, AV_SAMPLE_FMT_S16, 0); + av_samples_alloc(&output, nullptr, 1, max_out_samples, AV_SAMPLE_FMT_FLT, 0); std::unique_ptr output_deleter(output); int out_samples = swr_convert(swr, &output, max_out_samples, data, nb_samples); if (out_samples > 0) { - const int16_t* start = reinterpret_cast(output); - const int16_t* end = start + out_samples; + const float* start = reinterpret_cast(output); + const float* end = start + out_samples; samples->insert(samples->end(), start, end); } } -int decode_packet(const char *filename, AVCodecContext *codec_ctx, SwrContext *swr, AVFrame *audio_frame, AVPacket *packet, int *got_frame, std::vector *samples) +int decode_packet(const char *filename, AVCodecContext *codec_ctx, SwrContext *swr, AVFrame *audio_frame, AVPacket *packet, int *got_frame, std::vector *samples) { *got_frame = 0; int len1 = avcodec_decode_audio4(codec_ctx, audio_frame, got_frame, packet); @@ -88,7 +94,7 @@ int decode_packet(const char *filename, AVCodecContext *codec_ctx, SwrContext *s } // namespace -bool read_audio_file(const char *filename, std::vector *samples, int *sample_rate) +bool read_audio_file(const char *filename, std::vector *samples, int *sample_rate) { av_register_all(); @@ -141,7 +147,7 @@ bool read_audio_file(const char *filename, std::vector *samples, int *s } SwrContext *swr = swr_alloc_set_opts( nullptr, - AV_CH_LAYOUT_MONO, AV_SAMPLE_FMT_S16, codec_ctx->sample_rate, + AV_CH_LAYOUT_MONO, AV_SAMPLE_FMT_FLT, codec_ctx->sample_rate, codec_ctx->channel_layout, codec_ctx->sample_fmt, codec_ctx->sample_rate, 0, nullptr); std::unique_ptr swr_deleter(swr); @@ -151,8 +157,13 @@ bool read_audio_file(const char *filename, std::vector *samples, int *s } AVPacket packet; - AVFrame* audio_frame = av_frame_alloc(); +#if (LIBAVCODEC_VERSION_MAJOR >= 55) + AVFrame *audio_frame = av_frame_alloc(); std::unique_ptr audio_frame_deleter(audio_frame); +#else + AVFrame frame_holder {}; + AVFrame *audio_frame = &frame_holder; +#endif while (av_read_frame(format_ctx, &packet) >= 0) { std::unique_ptr av_packet_deleter(&packet);