Feature: change to static method

pull/221/head
xufuji456 3 years ago
parent 5e922d41d1
commit 71d7dbdb5d
  1. 47
      app/src/main/cpp/ff_audio_resample.cpp
  2. 2
      app/src/main/cpp/ff_audio_resample.h

@ -105,16 +105,20 @@ int FFAudioResample::openOutputFile(const char *filename, int sample_rate) {
return 0; return 0;
} }
int FFAudioResample::initResample() { static int initResample(AudioResample **pResample) {
resample->resampleCtx = swr_alloc_set_opts(nullptr, AudioResample *ar = *pResample;
av_get_default_channel_layout(resample->outCodecCtx->channels), SwrContext *context = swr_alloc_set_opts(nullptr,
resample->outCodecCtx->sample_fmt, av_get_default_channel_layout(ar->outCodecCtx->channels),
resample->outCodecCtx->sample_rate, ar->outCodecCtx->sample_fmt,
av_get_default_channel_layout(resample->inCodecCtx->channels), ar->outCodecCtx->sample_rate,
resample->inCodecCtx->sample_fmt, av_get_default_channel_layout(ar->inCodecCtx->channels),
resample->inCodecCtx->sample_rate, ar->inCodecCtx->sample_fmt,
ar->inCodecCtx->sample_rate,
0, nullptr); 0, nullptr);
return swr_init(resample->resampleCtx); int ret = swr_init(context);
ar->resampleCtx = context;
*pResample = ar;
return ret;
} }
int FFAudioResample::decodeAudioFrame(AVFrame *frame, int *data_present, int *finished) { int FFAudioResample::decodeAudioFrame(AVFrame *frame, int *data_present, int *finished) {
@ -227,17 +231,18 @@ cleanup:
return ret; return ret;
} }
static int initOutputFrame(AVFrame **frame, AVCodecContext *output_codec_context) { static int initOutputFrame(AudioResample **pResample) {
*frame = av_frame_alloc(); AudioResample *ar = *pResample;
(*frame)->nb_samples = output_codec_context->frame_size;
(*frame)->channel_layout = output_codec_context->channel_layout;
(*frame)->format = output_codec_context->sample_fmt;
(*frame)->sample_rate = output_codec_context->sample_rate;
int ret = av_frame_get_buffer(*frame, 0); AVFrame *frame = av_frame_alloc();
if (ret < 0) { frame->format = ar->outCodecCtx->sample_fmt;
ALOGE("Could not allocate output frame samples (error:%s)\n", av_err2str(ret)); frame->nb_samples = ar->outCodecCtx->frame_size;
} frame->sample_rate = ar->outCodecCtx->sample_rate;
frame->channel_layout = ar->outCodecCtx->channel_layout;
int ret = av_frame_get_buffer(frame, 0);
ar->outFrame = frame;
*pResample = ar;
return ret; return ret;
} }
@ -313,12 +318,12 @@ int FFAudioResample::resampling(const char *src_file, const char *dst_file, int
if (openOutputFile(dst_file, sampleRate)) if (openOutputFile(dst_file, sampleRate))
goto cleanup; goto cleanup;
/* Initialize the re-sampler to be able to convert audio sample formats. */ /* Initialize the re-sampler to be able to convert audio sample formats. */
if (initResample()) if (initResample(&resample))
goto cleanup; goto cleanup;
/* Initialize the FIFO buffer to store audio samples to be encoded. */ /* Initialize the FIFO buffer to store audio samples to be encoded. */
resample->fifo = av_audio_fifo_alloc(resample->outCodecCtx->sample_fmt, resample->fifo = av_audio_fifo_alloc(resample->outCodecCtx->sample_fmt,
resample->outCodecCtx->channels, 1024 * 10); resample->outCodecCtx->channels, 1024 * 10);
if (initOutputFrame(&resample->outFrame, resample->outCodecCtx)) if (initOutputFrame(&resample))
goto cleanup; goto cleanup;
/* Write the header of the output file container. */ /* Write the header of the output file container. */
if ((ret = avformat_write_header(resample->outFormatCtx, nullptr)) < 0) { if ((ret = avformat_write_header(resample->outFormatCtx, nullptr)) < 0) {

@ -52,8 +52,6 @@ private:
int openOutputFile(const char *filename, int sample_rate); int openOutputFile(const char *filename, int sample_rate);
int initResample();
int decodeAudioFrame(AVFrame *frame, int *data_present, int *finished); int decodeAudioFrame(AVFrame *frame, int *data_present, int *finished);
int initConvertedSamples(uint8_t ***converted_input_samples, int frame_size); int initConvertedSamples(uint8_t ***converted_input_samples, int frame_size);

Loading…
Cancel
Save