From c5ccf225e133208126d1b6e958100adeac56033f Mon Sep 17 00:00:00 2001 From: xufuji456 Date: Fri, 15 Jul 2022 15:51:15 +0800 Subject: [PATCH] Feature: support different format to resample --- app/src/main/cpp/audio_resample.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/app/src/main/cpp/audio_resample.cpp b/app/src/main/cpp/audio_resample.cpp index 68795f2..f36f136 100644 --- a/app/src/main/cpp/audio_resample.cpp +++ b/app/src/main/cpp/audio_resample.cpp @@ -114,6 +114,7 @@ static int open_input_file(const char *filename, */ static int open_output_file(const char *filename, int sample_rate, + AVCodecContext *input_codec_context, AVFormatContext **output_format_context, AVCodecContext **output_codec_context) { @@ -153,8 +154,8 @@ static int open_output_file(const char *filename, } /* Find the encoder to be used by its name. */ - if (!(output_codec = avcodec_find_encoder(AV_CODEC_ID_AAC))) { - ALOGE( "Could not find an AAC encoder.\n"); + if (!(output_codec = avcodec_find_encoder(input_codec_context->codec_id))) { + ALOGE( "Could not find encoder=%s\n", input_codec_context->codec->name); goto cleanup; } @@ -665,17 +666,17 @@ static int write_output_file_trailer(AVFormatContext *output_format_context) int resampling(const char *src_file, const char *dst_file, int sampleRate) { - AVFormatContext *input_format_context = nullptr, *output_format_context = nullptr; - AVCodecContext *input_codec_context = nullptr, *output_codec_context = nullptr; - SwrContext *resample_context = nullptr; - AVAudioFifo *fifo = nullptr; int ret = AVERROR_EXIT; + AVAudioFifo *fifo = nullptr; + SwrContext *resample_context = nullptr; + AVCodecContext *input_codec_context = nullptr, *output_codec_context = nullptr; + AVFormatContext *input_format_context = nullptr, *output_format_context = nullptr; /* Open the input file for reading. */ if (open_input_file(src_file, &input_format_context, &input_codec_context)) goto cleanup; /* Open the output file for writing. */ - if (open_output_file(dst_file, sampleRate, &output_format_context, &output_codec_context)) + if (open_output_file(dst_file, sampleRate, input_codec_context, &output_format_context, &output_codec_context)) goto cleanup; /* Initialize the re-sampler to be able to convert audio sample formats. */ if (init_resampler(input_codec_context, output_codec_context,