Feature: replace fprintf

pull/221/head
xufuji456 2 years ago
parent b897c27c44
commit c1ae76fa6d
  1. 106
      app/src/main/cpp/audio_resample.cpp

@ -35,14 +35,6 @@ extern "C" {
/* Global timestamp for the audio frames. */
static int64_t pts = 0;
static void log_error(const char *functionName, int errorNumber) {
int buffer_len = 1024;
char *buffer = new char [buffer_len];
av_strerror(errorNumber, buffer, buffer_len);
ALOGE("%s: %s", functionName, buffer);
delete []buffer;
}
/**
* Open an input file and the required decoder.
*
@ -58,31 +50,28 @@ static int open_input_file(const char *filename,
/* Open the input file to read from it. */
if ((error = avformat_open_input(input_format_context, filename, nullptr,
nullptr)) < 0) {
fprintf(stderr, "Could not open input file '%s' (error '%s')\n",
filename, av_err2str(error));
ALOGE("Could not open input file:%s (error:%s)\n", filename, av_err2str(error));
*input_format_context = nullptr;
return error;
}
/* Get information on the input file (number of streams etc.). */
if ((error = avformat_find_stream_info(*input_format_context, nullptr)) < 0) {
fprintf(stderr, "Could not open find stream info (error '%s')\n",
av_err2str(error));
ALOGE("Could not open find stream info (error:%s)\n", av_err2str(error));
avformat_close_input(input_format_context);
return error;
}
/* Make sure that there is only one stream in the input file. */
if ((*input_format_context)->nb_streams != 1) {
fprintf(stderr, "Expected one audio input stream, but found %d\n",
(*input_format_context)->nb_streams);
ALOGE("Expected one audio input stream, but found %d\n", (*input_format_context)->nb_streams);
avformat_close_input(input_format_context);
return AVERROR_EXIT;
}
/* Find a decoder for the audio stream. */
if (!(input_codec = avcodec_find_decoder((*input_format_context)->streams[0]->codecpar->codec_id))) {
fprintf(stderr, "Could not find input codec\n");
ALOGE("Could not find input codec\n");
avformat_close_input(input_format_context);
return AVERROR_EXIT;
}
@ -90,7 +79,7 @@ static int open_input_file(const char *filename,
/* Allocate a new decoding context. */
avctx = avcodec_alloc_context3(input_codec);
if (!avctx) {
fprintf(stderr, "Could not allocate a decoding context\n");
ALOGE("Could not allocate a decoding context\n");
avformat_close_input(input_format_context);
return AVERROR(ENOMEM);
}
@ -105,8 +94,7 @@ static int open_input_file(const char *filename,
/* Open the decoder for the audio stream to use it later. */
if ((error = avcodec_open2(avctx, input_codec, nullptr)) < 0) {
fprintf(stderr, "Could not open input codec (error '%s')\n",
av_err2str(error));
ALOGE("Could not open input codec (error:%s)\n", av_err2str(error));
avcodec_free_context(&avctx);
avformat_close_input(input_format_context);
return error;
@ -139,14 +127,13 @@ static int open_output_file(const char *filename,
/* Open the output file to write to it. */
if ((error = avio_open(&output_io_context, filename,
AVIO_FLAG_WRITE)) < 0) {
fprintf(stderr, "Could not open output file '%s' (error '%s')\n",
filename, av_err2str(error));
ALOGE("Could not open output file:%s (error:%s)\n", filename, av_err2str(error));
return error;
}
/* Create a new format context for the output container format. */
if (!(*output_format_context = avformat_alloc_context())) {
fprintf(stderr, "Could not allocate output format context\n");
ALOGE("Could not allocate output format context\n");
return AVERROR(ENOMEM);
}
@ -156,32 +143,32 @@ static int open_output_file(const char *filename,
/* Guess the desired container format based on the file extension. */
if (!((*output_format_context)->oformat = av_guess_format(nullptr, filename,
nullptr))) {
fprintf(stderr, "Could not find output file format\n");
ALOGE("Could not find output file format\n");
goto cleanup;
}
if (!((*output_format_context)->url = av_strdup(filename))) {
fprintf(stderr, "Could not allocate url.\n");
ALOGE("Could not allocate url.\n");
error = AVERROR(ENOMEM);
goto cleanup;
}
/* Find the encoder to be used by its name. */
if (!(output_codec = avcodec_find_encoder(AV_CODEC_ID_AAC))) {
fprintf(stderr, "Could not find an AAC encoder.\n");
ALOGE( "Could not find an AAC encoder.\n");
goto cleanup;
}
/* Create a new audio stream in the output file container. */
if (!(stream = avformat_new_stream(*output_format_context, nullptr))) {
fprintf(stderr, "Could not create new stream\n");
ALOGE("Could not create new stream\n");
error = AVERROR(ENOMEM);
goto cleanup;
}
avctx = avcodec_alloc_context3(output_codec);
if (!avctx) {
fprintf(stderr, "Could not allocate an encoding context\n");
ALOGE("Could not allocate an encoding context\n");
error = AVERROR(ENOMEM);
goto cleanup;
}
@ -208,20 +195,17 @@ static int open_output_file(const char *filename,
/* Open the encoder for the audio stream to use it later. */
if ((error = avcodec_open2(avctx, output_codec, nullptr)) < 0) {
fprintf(stderr, "Could not open output codec (error '%s')\n",
av_err2str(error));
ALOGE("Could not open output codec (error:%s)\n", av_err2str(error));
goto cleanup;
}
error = avcodec_parameters_from_context(stream->codecpar, avctx);
if (error < 0) {
fprintf(stderr, "Could not initialize stream parameters\n");
ALOGE("Could not initialize stream parameters\n");
goto cleanup;
}
/* Save the encoder context for easier access later. */
*output_codec_context = avctx;
return 0;
cleanup:
@ -250,7 +234,7 @@ static void init_packet(AVPacket *packet)
static int init_input_frame(AVFrame **frame)
{
if (!(*frame = av_frame_alloc())) {
fprintf(stderr, "Could not allocate input frame\n");
ALOGE("Could not allocate input frame\n");
return AVERROR(ENOMEM);
}
return 0;
@ -282,7 +266,7 @@ static int init_resampler(AVCodecContext *input_codec_context,
input_codec_context->sample_rate,
0, nullptr);
if (!*resample_context) {
fprintf(stderr, "Could not allocate resample context\n");
ALOGE("Could not allocate resample context\n");
return AVERROR(ENOMEM);
}
/*
@ -294,7 +278,7 @@ static int init_resampler(AVCodecContext *input_codec_context,
/* Open the re-sampler with the specified parameters. */
if ((error = swr_init(*resample_context)) < 0) {
fprintf(stderr, "Could not open resample context\n");
ALOGE("Could not open resample context\n");
swr_free(resample_context);
return error;
}
@ -310,7 +294,7 @@ static int init_fifo(AVAudioFifo **fifo, AVCodecContext *output_codec_context)
/* Create the FIFO buffer based on the specified output sample format. */
if (!(*fifo = av_audio_fifo_alloc(output_codec_context->sample_fmt,
output_codec_context->channels, 1))) {
fprintf(stderr, "Could not allocate FIFO\n");
ALOGE("Could not allocate FIFO\n");
return AVERROR(ENOMEM);
}
return 0;
@ -324,7 +308,7 @@ static int write_output_file_header(AVFormatContext *output_format_context)
{
int error;
if ((error = avformat_write_header(output_format_context, nullptr)) < 0) {
fprintf(stderr, "Could not write output file header (error '%s')\n",
ALOGE("Could not write output file header (error '%s')\n",
av_err2str(error));
return error;
}
@ -351,7 +335,7 @@ static int decode_audio_frame(AVFrame *frame,
if (error == AVERROR_EOF)
*finished = 1;
else {
fprintf(stderr, "Could not read frame (error '%s')\n",
ALOGE("Could not read frame (error:%s)\n",
av_err2str(error));
return error;
}
@ -360,8 +344,7 @@ static int decode_audio_frame(AVFrame *frame,
/* Send the audio frame stored in the temporary packet to the decoder.
* The input audio stream decoder is used to do this. */
if ((error = avcodec_send_packet(input_codec_context, &input_packet)) < 0) {
fprintf(stderr, "Could not send packet for decoding (error '%s')\n",
av_err2str(error));
ALOGE("Could not send packet for decoding (error:%s)\n", av_err2str(error));
return error;
}
@ -378,10 +361,8 @@ static int decode_audio_frame(AVFrame *frame,
error = 0;
goto cleanup;
} else if (error < 0) {
fprintf(stderr, "Could not decode frame (error '%s')\n",
av_err2str(error));
ALOGE("Could not decode frame (error:%s)\n", av_err2str(error));
goto cleanup;
/* Default case: Return decoded data. */
} else {
*data_present = 1;
goto cleanup;
@ -409,7 +390,7 @@ static int init_converted_samples(uint8_t ***converted_input_samples,
*/
if (!(*converted_input_samples = (uint8_t **) calloc(output_codec_context->channels,
sizeof(**converted_input_samples)))) {
fprintf(stderr, "Could not allocate converted input sample pointers\n");
ALOGE("Could not allocate converted input sample pointers\n");
return AVERROR(ENOMEM);
}
@ -419,9 +400,7 @@ static int init_converted_samples(uint8_t ***converted_input_samples,
output_codec_context->channels,
frame_size,
output_codec_context->sample_fmt, 0)) < 0) {
fprintf(stderr,
"Could not allocate converted input samples (error '%s')\n",
av_err2str(error));
ALOGE("Could not allocate converted input samples (error:%s)\n", av_err2str(error));
av_freep(&(*converted_input_samples)[0]);
free(*converted_input_samples);
return error;
@ -445,8 +424,7 @@ static int convert_samples(const uint8_t **input_data,
if ((error = swr_convert(resample_context,
converted_data, frame_size,
input_data , frame_size)) < 0) {
fprintf(stderr, "Could not convert input samples (error '%s')\n",
av_err2str(error));
ALOGE("Could not convert input samples (error:%s)\n", av_err2str(error));
return error;
}
@ -466,14 +444,14 @@ static int add_samples_to_fifo(AVAudioFifo *fifo,
/* Make the FIFO as large as it needs to be to hold both,
* the old and the new samples. */
if ((error = av_audio_fifo_realloc(fifo, av_audio_fifo_size(fifo) + frame_size)) < 0) {
fprintf(stderr, "Could not reallocate FIFO\n");
ALOGE("Could not reallocate FIFO\n");
return error;
}
/* Store the new samples in the FIFO buffer. */
if (av_audio_fifo_write(fifo, (void **)converted_input_samples,
frame_size) < frame_size) {
fprintf(stderr, "Could not write data to FIFO\n");
ALOGE("Could not write data to FIFO\n");
return AVERROR_EXIT;
}
return 0;
@ -556,7 +534,7 @@ static int init_output_frame(AVFrame **frame,
/* Create a new frame to store the audio samples. */
if (!(*frame = av_frame_alloc())) {
fprintf(stderr, "Could not allocate output frame\n");
ALOGE("Could not allocate output frame\n");
return AVERROR_EXIT;
}
@ -573,8 +551,7 @@ static int init_output_frame(AVFrame **frame,
/* Allocate the samples of the created frame. This call will make
* sure that the audio frame can hold as many samples as specified. */
if ((error = av_frame_get_buffer(*frame, 0)) < 0) {
fprintf(stderr, "Could not allocate output frame samples (error '%s')\n",
av_err2str(error));
ALOGE("Could not allocate output frame samples (error:%s)\n", av_err2str(error));
av_frame_free(frame);
return error;
}
@ -584,12 +561,7 @@ static int init_output_frame(AVFrame **frame,
/**
* Encode one frame worth of audio to the output file.
* @param frame Samples to be encoded
* @param output_format_context Format context of the output file
* @param output_codec_context Codec context of the output file
* @param[out] data_present Indicates whether data has been
* encoded
* @return Error code (0 if successful)
*
*/
static int encode_audio_frame(AVFrame *frame,
AVFormatContext *output_format_context,
@ -615,8 +587,7 @@ static int encode_audio_frame(AVFrame *frame,
error = 0;
goto cleanup;
} else if (error < 0) {
fprintf(stderr, "Could not send packet for encoding (error '%s')\n",
av_err2str(error));
ALOGE("Could not send packet for encoding (error:%s)\n", av_err2str(error));
return error;
}
@ -632,8 +603,7 @@ static int encode_audio_frame(AVFrame *frame,
error = 0;
goto cleanup;
} else if (error < 0) {
fprintf(stderr, "Could not encode frame (error '%s')\n",
av_err2str(error));
ALOGE("Could not encode frame (error:%s)\n", av_err2str(error));
goto cleanup;
/* Default case: Return encoded data. */
} else {
@ -643,12 +613,11 @@ static int encode_audio_frame(AVFrame *frame,
/* Write one audio frame from the temporary packet to the output file. */
if (*data_present &&
(error = av_write_frame(output_format_context, &output_packet)) < 0) {
fprintf(stderr, "Could not write frame (error '%s')\n",
av_err2str(error));
ALOGE("Could not write frame (error:%s)\n", av_err2str(error));
goto cleanup;
}
cleanup:
cleanup:
av_packet_unref(&output_packet);
return error;
}
@ -678,7 +647,7 @@ static int load_encode_and_write(AVAudioFifo *fifo,
/* Read as many samples from the FIFO buffer as required to fill the frame.
* The samples are stored in the frame temporarily. */
if (av_audio_fifo_read(fifo, (void **)output_frame->data, frame_size) < frame_size) {
fprintf(stderr, "Could not read data from FIFO\n");
ALOGE("Could not read data from FIFO\n");
av_frame_free(&output_frame);
return AVERROR_EXIT;
}
@ -701,8 +670,7 @@ static int write_output_file_trailer(AVFormatContext *output_format_context)
{
int error;
if ((error = av_write_trailer(output_format_context)) < 0) {
fprintf(stderr, "Could not write output file trailer (error '%s')\n",
av_err2str(error));
ALOGE("Could not write output file trailer (error:%s)\n", av_err2str(error));
return error;
}
return 0;

Loading…
Cancel
Save