44 #define OUTPUT_BIT_RATE 48000
46 #define OUTPUT_CHANNELS 2
48 #define OUTPUT_SAMPLE_FORMAT AV_SAMPLE_FMT_S16
57 static char error_buffer[255];
58 av_strerror(error, error_buffer,
sizeof(error_buffer));
73 fprintf(stderr,
"Could not open input file '%s' (error '%s')\n",
75 *input_format_context = NULL;
81 fprintf(stderr,
"Could not open find stream info (error '%s')\n",
88 if ((*input_format_context)->nb_streams != 1) {
89 fprintf(stderr,
"Expected one audio input stream, but found %d\n",
90 (*input_format_context)->nb_streams);
96 if (!(input_codec =
avcodec_find_decoder((*input_format_context)->streams[0]->codec->codec_id))) {
97 fprintf(stderr,
"Could not find input codec\n");
103 if ((error =
avcodec_open2((*input_format_context)->streams[0]->codec,
104 input_codec, NULL)) < 0) {
105 fprintf(stderr,
"Could not open input codec (error '%s')\n",
112 *input_codec_context = (*input_format_context)->streams[0]->
codec;
133 if ((error =
avio_open(&output_io_context, filename,
135 fprintf(stderr,
"Could not open output file '%s' (error '%s')\n",
142 fprintf(stderr,
"Could not allocate output format context\n");
147 (*output_format_context)->pb = output_io_context;
150 if (!((*output_format_context)->oformat =
av_guess_format(NULL, filename,
152 fprintf(stderr,
"Could not find output file format\n");
156 av_strlcpy((*output_format_context)->filename, filename,
157 sizeof((*output_format_context)->filename));
161 fprintf(stderr,
"Could not find an AAC encoder.\n");
167 fprintf(stderr,
"Could not create new stream\n");
173 *output_codec_context = stream->
codec;
181 (*output_codec_context)->sample_rate = input_codec_context->
sample_rate;
193 if ((error =
avcodec_open2(*output_codec_context, output_codec, NULL)) < 0) {
194 fprintf(stderr,
"Could not open output codec (error '%s')\n",
204 *output_format_context = NULL;
221 fprintf(stderr,
"Could not allocate input frame\n");
253 if (!*resample_context) {
254 fprintf(stderr,
"Could not allocate resample context\n");
265 if ((error =
swr_init(*resample_context)) < 0) {
266 fprintf(stderr,
"Could not open resample context\n");
278 fprintf(stderr,
"Could not allocate FIFO\n");
289 fprintf(stderr,
"Could not write output file header (error '%s')\n",
300 int *data_present,
int *finished)
308 if ((error =
av_read_frame(input_format_context, &input_packet)) < 0) {
313 fprintf(stderr,
"Could not read frame (error '%s')\n",
326 data_present, &input_packet)) < 0) {
327 fprintf(stderr,
"Could not decode frame (error '%s')\n",
337 if (*finished && *data_present)
359 if (!(*converted_input_samples = calloc(output_codec_context->
channels,
360 sizeof(**converted_input_samples)))) {
361 fprintf(stderr,
"Could not allocate converted input sample pointers\n");
374 "Could not allocate converted input samples (error '%s')\n",
376 av_freep(&(*converted_input_samples)[0]);
377 free(*converted_input_samples);
396 converted_data, frame_size,
397 input_data , frame_size)) < 0) {
398 fprintf(stderr,
"Could not convert input samples (error '%s')\n",
408 uint8_t **converted_input_samples,
418 fprintf(stderr,
"Could not reallocate FIFO\n");
424 frame_size) < frame_size) {
425 fprintf(stderr,
"Could not write data to FIFO\n");
445 uint8_t **converted_input_samples = NULL;
454 input_codec_context, &data_present, finished))
461 if (*finished && !data_present) {
489 if (converted_input_samples) {
490 av_freep(&converted_input_samples[0]);
491 free(converted_input_samples);
510 fprintf(stderr,
"Could not allocate output frame\n");
523 (*frame)->format = output_codec_context->
sample_fmt;
524 (*frame)->sample_rate = output_codec_context->
sample_rate;
531 fprintf(stderr,
"Could allocate output frame samples (error '%s')\n",
556 frame, data_present)) < 0) {
557 fprintf(stderr,
"Could not encode frame (error '%s')\n",
565 if ((error =
av_write_frame(output_format_context, &output_packet)) < 0) {
566 fprintf(stderr,
"Could not write frame (error '%s')\n",
606 fprintf(stderr,
"Could not read data from FIFO\n");
613 output_codec_context, &data_written)) {
626 fprintf(stderr,
"Could not write output file trailer (error '%s')\n",
634 int main(
int argc,
char **argv)
636 AVFormatContext *input_format_context = NULL, *output_format_context = NULL;
637 AVCodecContext *input_codec_context = NULL, *output_codec_context = NULL;
643 fprintf(stderr,
"Usage: %s <input file> <output file>\n", argv[0]);
651 &input_codec_context))
655 &output_format_context, &output_codec_context))
674 const int output_frame_size = output_codec_context->frame_size;
691 output_codec_context,
692 resample_context, &finished))
715 output_codec_context))
727 output_codec_context, &data_written))
729 }
while (data_written);
743 if (output_codec_context)
745 if (output_format_context) {
749 if (input_codec_context)
751 if (input_format_context)