FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Macros | Functions
transcode_aac.c File Reference

simple audio converter More...

#include <stdio.h>
#include "libavformat/avformat.h"
#include "libavformat/avio.h"
#include "libavcodec/avcodec.h"
#include "libavutil/audio_fifo.h"
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
#include "libavutil/frame.h"
#include "libavutil/opt.h"
#include "libswresample/swresample.h"

Go to the source code of this file.

Macros

#define OUTPUT_BIT_RATE   48000
 The output bit rate in kbit/s.
 
#define OUTPUT_CHANNELS   2
 The number of output channels.
 
#define OUTPUT_SAMPLE_FORMAT   AV_SAMPLE_FMT_S16
 The audio sample output format.
 

Functions

static const char * get_error_text (const int error)
 Convert an error code into a text message.
 
static int open_input_file (const char *filename, AVFormatContext **input_format_context, AVCodecContext **input_codec_context)
 Open an input file and the required decoder.
 
static int open_output_file (const char *filename, AVCodecContext *input_codec_context, AVFormatContext **output_format_context, AVCodecContext **output_codec_context)
 Open an output file and the required encoder.
 
static void init_packet (AVPacket *packet)
 Initialize one data packet for reading or writing.
 
static int init_input_frame (AVFrame **frame)
 Initialize one audio frame for reading from the input file.
 
static int init_resampler (AVCodecContext *input_codec_context, AVCodecContext *output_codec_context, SwrContext **resample_context)
 Initialize the audio resampler based on the input and output codec settings.
 
static int init_fifo (AVAudioFifo **fifo)
 Initialize a FIFO buffer for the audio samples to be encoded.
 
static int write_output_file_header (AVFormatContext *output_format_context)
 Write the header of the output file container.
 
static int decode_audio_frame (AVFrame *frame, AVFormatContext *input_format_context, AVCodecContext *input_codec_context, int *data_present, int *finished)
 Decode one audio frame from the input file.
 
static int init_converted_samples (uint8_t ***converted_input_samples, AVCodecContext *output_codec_context, int frame_size)
 Initialize a temporary storage for the specified number of audio samples.
 
static int convert_samples (const uint8_t **input_data, uint8_t **converted_data, const int frame_size, SwrContext *resample_context)
 Convert the input audio samples into the output sample format.
 
static int add_samples_to_fifo (AVAudioFifo *fifo, uint8_t **converted_input_samples, const int frame_size)
 Add converted input audio samples to the FIFO buffer for later processing.
 
static int read_decode_convert_and_store (AVAudioFifo *fifo, AVFormatContext *input_format_context, AVCodecContext *input_codec_context, AVCodecContext *output_codec_context, SwrContext *resampler_context, int *finished)
 Read one audio frame from the input file, decodes, converts and stores it in the FIFO buffer.
 
static int init_output_frame (AVFrame **frame, AVCodecContext *output_codec_context, int frame_size)
 Initialize one input frame for writing to the output file.
 
static int encode_audio_frame (AVFrame *frame, AVFormatContext *output_format_context, AVCodecContext *output_codec_context, int *data_present)
 Encode one frame worth of audio to the output file.
 
static int load_encode_and_write (AVAudioFifo *fifo, AVFormatContext *output_format_context, AVCodecContext *output_codec_context)
 Load one audio frame from the FIFO buffer, encode and write it to the output file.
 
static int write_output_file_trailer (AVFormatContext *output_format_context)
 Write the trailer of the output file container.
 
int main (int argc, char **argv)
 Convert an audio file to an AAC file in an MP4 container.
 

Detailed Description

simple audio converter

Definition in file transcode_aac.c.

Macro Definition Documentation

#define OUTPUT_BIT_RATE   48000

The output bit rate in kbit/s.

Examples:
transcode_aac.c.

Definition at line 44 of file transcode_aac.c.

Referenced by open_output_file().

#define OUTPUT_CHANNELS   2

The number of output channels.

Examples:
transcode_aac.c.

Definition at line 46 of file transcode_aac.c.

Referenced by init_fifo(), and open_output_file().

#define OUTPUT_SAMPLE_FORMAT   AV_SAMPLE_FMT_S16

The audio sample output format.

Examples:
transcode_aac.c.

Definition at line 48 of file transcode_aac.c.

Referenced by init_fifo().

Function Documentation

static const char* get_error_text ( const int  error)
static

Convert an error code into a text message.

Parameters
errorError code to be converted
Returns
Corresponding error text (not thread-safe)
Examples:
transcode_aac.c.

Definition at line 55 of file transcode_aac.c.

Referenced by convert_samples(), decode_audio_frame(), encode_audio_frame(), init_converted_samples(), init_output_frame(), open_input_file(), open_output_file(), write_output_file_header(), and write_output_file_trailer().

static int open_input_file ( const char *  filename,
AVFormatContext **  input_format_context,
AVCodecContext **  input_codec_context 
)
static

Open an input file and the required decoder.

Open the input file to read from it.

Get information on the input file (number of streams etc.).

Make sure that there is only one stream in the input file.

Find a decoder for the audio stream.

Open the decoder for the audio stream to use it later.

Save the decoder context for easier access later.

Definition at line 63 of file transcode_aac.c.

Referenced by main().

static int open_output_file ( const char *  filename,
AVCodecContext input_codec_context,
AVFormatContext **  output_format_context,
AVCodecContext **  output_codec_context 
)
static

Open an output file and the required encoder.

Also set some basic encoder parameters. Some of these parameters are based on the input file's parameters.

Open the output file to write to it.

Create a new format context for the output container format.

Associate the output file (pointer) with the container format context.

Guess the desired container format based on the file extension.

Find the encoder to be used by its name.

Create a new audio stream in the output file container.

Save the encoder context for easiert access later.

Set the basic encoder parameters. The input file's sample rate is used to avoid a sample rate conversion.

Some container formats (like MP4) require global headers to be present Mark the encoder so that it behaves accordingly.

Open the encoder for the audio stream to use it later.

Examples:
transcode_aac.c, and transcoding.c.

Definition at line 122 of file transcode_aac.c.

Referenced by main().

static void init_packet ( AVPacket packet)
static

Initialize one data packet for reading or writing.

Set the packet data and size so that it is recognized as being empty.

Examples:
transcode_aac.c.

Definition at line 209 of file transcode_aac.c.

Referenced by decode_audio_frame(), and encode_audio_frame().

static int init_input_frame ( AVFrame **  frame)
static

Initialize one audio frame for reading from the input file.

Examples:
transcode_aac.c.

Definition at line 218 of file transcode_aac.c.

Referenced by read_decode_convert_and_store().

static int init_resampler ( AVCodecContext input_codec_context,
AVCodecContext output_codec_context,
SwrContext **  resample_context 
)
static

Initialize the audio resampler based on the input and output codec settings.

If the input and output sample formats differ, a conversion is required libswresample takes care of this, but requires initialization.

Create a resampler context for the conversion. Set the conversion parameters. Default channel layouts based on the number of channels are assumed for simplicity (they are sometimes not detected properly by the demuxer and/or decoder).

Perform a sanity check so that the number of converted samples is not greater than the number of samples to be converted. If the sample rates differ, this case has to be handled differently

Open the resampler with the specified parameters.

Examples:
transcode_aac.c.

Definition at line 232 of file transcode_aac.c.

Referenced by main().

static int init_fifo ( AVAudioFifo **  fifo)
static

Initialize a FIFO buffer for the audio samples to be encoded.

Create the FIFO buffer based on the specified output sample format.

Examples:
transcode_aac.c.

Definition at line 274 of file transcode_aac.c.

Referenced by main().

static int write_output_file_header ( AVFormatContext output_format_context)
static

Write the header of the output file container.

Examples:
transcode_aac.c.

Definition at line 285 of file transcode_aac.c.

Referenced by main().

static int decode_audio_frame ( AVFrame frame,
AVFormatContext input_format_context,
AVCodecContext input_codec_context,
int *  data_present,
int *  finished 
)
static

Decode one audio frame from the input file.

Packet used for temporary storage.

Read one audio frame from the input file into a temporary packet.

If we are at the end of the file, flush the decoder below.

Decode the audio frame stored in the temporary packet. The input audio stream decoder is used to do this. If we are at the end of the file, pass an empty packet to the decoder to flush it.

If the decoder has not been flushed completely, we are not finished, so that this function has to be called again.

Examples:
transcode_aac.c.

Definition at line 297 of file transcode_aac.c.

Referenced by read_decode_convert_and_store().

static int init_converted_samples ( uint8_t ***  converted_input_samples,
AVCodecContext output_codec_context,
int  frame_size 
)
static

Initialize a temporary storage for the specified number of audio samples.

The conversion requires temporary storage due to the different format. The number of audio samples to be allocated is specified in frame_size.

Allocate as many pointers as there are audio channels. Each pointer will later point to the audio samples of the corresponding channels (although it may be NULL for interleaved formats).

Allocate memory for the samples of all channels in one consecutive block for convenience.

Examples:
transcode_aac.c.

Definition at line 348 of file transcode_aac.c.

Referenced by read_decode_convert_and_store().

static int convert_samples ( const uint8_t **  input_data,
uint8_t **  converted_data,
const int  frame_size,
SwrContext resample_context 
)
static

Convert the input audio samples into the output sample format.

The conversion happens on a per-frame basis, the size of which is specified by frame_size.

Convert the samples using the resampler.

Examples:
transcode_aac.c.

Definition at line 388 of file transcode_aac.c.

Referenced by read_decode_convert_and_store().

static int add_samples_to_fifo ( AVAudioFifo fifo,
uint8_t **  converted_input_samples,
const int  frame_size 
)
static

Add converted input audio samples to the FIFO buffer for later processing.

Make the FIFO as large as it needs to be to hold both, the old and the new samples.

Store the new samples in the FIFO buffer.

Examples:
transcode_aac.c.

Definition at line 407 of file transcode_aac.c.

Referenced by read_decode_convert_and_store().

static int read_decode_convert_and_store ( AVAudioFifo fifo,
AVFormatContext input_format_context,
AVCodecContext input_codec_context,
AVCodecContext output_codec_context,
SwrContext resampler_context,
int *  finished 
)
static

Read one audio frame from the input file, decodes, converts and stores it in the FIFO buffer.

Temporary storage of the input samples of the frame read from the file.

Temporary storage for the converted input samples.

Initialize temporary storage for one input frame.

Decode one frame worth of audio samples.

If we are at the end of the file and there are no more samples in the decoder which are delayed, we are actually finished. This must not be treated as an error.

If there is decoded data, convert and store it

Initialize the temporary storage for the converted input samples.

Convert the input samples to the desired output sample format. This requires a temporary storage provided by converted_input_samples.

Add the converted input samples to the FIFO buffer for later processing.

Examples:
transcode_aac.c.

Definition at line 435 of file transcode_aac.c.

Referenced by main().

static int init_output_frame ( AVFrame **  frame,
AVCodecContext output_codec_context,
int  frame_size 
)
static

Initialize one input frame for writing to the output file.

The frame will be exactly frame_size samples large.

Create a new frame to store the audio samples.

Set the frame's parameters, especially its size and format. av_frame_get_buffer needs this to allocate memory for the audio samples of the frame. Default channel layouts based on the number of channels are assumed for simplicity.

Allocate the samples of the created frame. This call will make sure that the audio frame can hold as many samples as specified.

Examples:
transcode_aac.c.

Definition at line 502 of file transcode_aac.c.

Referenced by load_encode_and_write().

static int encode_audio_frame ( AVFrame frame,
AVFormatContext output_format_context,
AVCodecContext output_codec_context,
int *  data_present 
)
static

Encode one frame worth of audio to the output file.

Packet used for temporary storage.

Encode the audio frame and store it in the temporary packet. The output audio stream encoder is used to do this.

Write one audio frame from the temporary packet to the output file.

Examples:
transcode_aac.c.

Definition at line 541 of file transcode_aac.c.

Referenced by load_encode_and_write(), and main().

static int load_encode_and_write ( AVAudioFifo fifo,
AVFormatContext output_format_context,
AVCodecContext output_codec_context 
)
static

Load one audio frame from the FIFO buffer, encode and write it to the output file.

Temporary storage of the output samples of the frame written to the file.

Use the maximum number of possible samples per frame. If there is less than the maximum possible frame size in the FIFO buffer use this number. Otherwise, use the maximum possible frame size

Initialize temporary storage for one output frame.

Read as many samples from the FIFO buffer as required to fill the frame. The samples are stored in the frame temporarily.

Encode one frame worth of audio samples.

Examples:
transcode_aac.c.

Definition at line 582 of file transcode_aac.c.

Referenced by main().

static int write_output_file_trailer ( AVFormatContext output_format_context)
static

Write the trailer of the output file container.

Examples:
transcode_aac.c.

Definition at line 622 of file transcode_aac.c.

Referenced by main().

int main ( int  argc,
char **  argv 
)

Convert an audio file to an AAC file in an MP4 container.

Register all codecs and formats so that they can be used.

Open the input file for reading.

Open the output file for writing.

Initialize the resampler to be able to convert audio sample formats.

Initialize the FIFO buffer to store audio samples to be encoded.

Write the header of the output file container.

Loop as long as we have input samples to read or output samples to write; abort as soon as we have neither.

Use the encoder's desired frame size for processing.

Make sure that there is one frame worth of samples in the FIFO buffer so that the encoder can do its work. Since the decoder's and the encoder's frame size may differ, we need to FIFO buffer to store as many frames worth of input samples that they make up at least one frame worth of output samples.

Decode one frame worth of audio samples, convert it to the output sample format and put it into the FIFO buffer.

If we are at the end of the input file, we continue encoding the remaining audio samples to the output file.

If we have enough samples for the encoder, we encode them. At the end of the file, we pass the remaining samples to the encoder.

Take one frame worth of audio samples from the FIFO buffer, encode it and write it to the output file.

If we are at the end of the input file and have encoded all remaining samples, we can exit this loop and finish.

Flush the encoder as it may have delayed frames.

Write the trailer of the output file container.

Definition at line 634 of file transcode_aac.c.