FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Macros | Functions | Variables
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   96000
 The output bit rate in kbit/s. More...
 
#define OUTPUT_CHANNELS   2
 The number of output channels. More...
 

Functions

static int open_input_file (const char *filename, AVFormatContext **input_format_context, AVCodecContext **input_codec_context)
 Open an input file and the required decoder. More...
 
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. More...
 
static void init_packet (AVPacket *packet)
 Initialize one data packet for reading or writing. More...
 
static int init_input_frame (AVFrame **frame)
 Initialize one audio frame for reading from the input file. More...
 
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. More...
 
static int init_fifo (AVAudioFifo **fifo, AVCodecContext *output_codec_context)
 Initialize a FIFO buffer for the audio samples to be encoded. More...
 
static int write_output_file_header (AVFormatContext *output_format_context)
 Write the header of the output file container. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
static int init_output_frame (AVFrame **frame, AVCodecContext *output_codec_context, int frame_size)
 Initialize one input frame for writing to the output file. More...
 
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. More...
 
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. More...
 
static int write_output_file_trailer (AVFormatContext *output_format_context)
 Write the trailer of the output file container. More...
 
int main (int argc, char **argv)
 Convert an audio file to an AAC file in an MP4 container. More...
 

Variables

static int64_t pts = 0
 Global timestamp for the audio frames. More...
 

Detailed Description

simple audio converter

Definition in file transcode_aac.c.

Macro Definition Documentation

#define OUTPUT_BIT_RATE   96000

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 open_output_file().

Function Documentation

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.

allocate a new decoding context

initialize the stream parameters with demuxer information

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

Save the decoder context for easier access later.

Examples:
transcode_aac.c.

Definition at line 49 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.

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

Allow the use of the experimental AAC encoder

Set the sample rate for the container.

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.

Save the encoder context for easier access later.

Examples:
transcode_aac.c.

Definition at line 125 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 234 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 243 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 257 of file transcode_aac.c.

Referenced by main().

static int init_fifo ( AVAudioFifo **  fifo,
AVCodecContext output_codec_context 
)
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 299 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 311 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 323 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 374 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 414 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 433 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 461 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 528 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.

Set a timestamp based on the sample rate for the container.

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 570 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 617 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 657 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.

Examples:
transcode_aac.c.

Definition at line 669 of file transcode_aac.c.

Variable Documentation

int64_t pts = 0
static

Global timestamp for the audio frames.

Examples:
transcode_aac.c.

Definition at line 567 of file transcode_aac.c.

Referenced by aal_read_packet(), add_crc_to_array(), adjust_write_index(), ape_read_header(), asf_read_marker(), asf_read_pts(), asf_write_packet(), av_parser_parse2(), build_frame_code(), config_input(), crystalhd_decode_packet(), curves_init(), decode(), decode_frame(), decode_frame_header(), decode_vop_header(), dirac_combine_frame(), dirac_gptopts(), display_end_segment(), do_subtitle_out(), encode_audio_frame(), ff_avfilter_link_set_in_status(), ff_inlink_evaluate_timeline_at_frame(), ff_mediacodec_dec_decode(), ff_subtitles_queue_seek(), ff_update_link_current_pts(), ffm_seek(), filter_frame(), find_next_delta_ts(), flac_read_timestamp(), flush_frame(), flv_read_packet(), frame_list_add_frame(), framesync_advance(), framesync_inject_frame(), func_pts(), get_frame_internal(), get_input_packet(), get_pkt_pts(), guess_correct_pts(), handle_metadata(), interpolate(), ivr_read_packet(), libschroedinger_decode_frame(), load_input_picture(), mkv_write_cues(), mov_read_sidx(), mov_read_trun(), mpeg_mux_write_packet(), mpegps_read_dts(), mpegps_read_packet(), mpegps_read_pes_header(), mpegts_write_packet_internal(), nut_read_timestamp(), ogg_calc_pts(), ogg_get_length(), ogg_gptopts(), ogg_read_packet(), ogg_read_timestamp(), ogg_write_packet_internal(), output_single_frame(), parse_forced_key_frames(), parse_presentation_segment(), parse_psfile(), prepare_packet(), print_report(), queue_picture(), read_frame_internal(), read_packet(), read_trun_duration(), reap_screen(), rm_read_index(), rv34_parse(), scan_file(), scc_write_packet(), set_clock_at(), set_work_frame_pts(), sub2video_push_ref(), sub2video_update(), subtitle_thread(), sup_read_packet(), swr_next_pts(), vc1t_read_packet(), video_thread(), vp8_gptopts(), vtenc_cm_to_avpacket(), and write_pts().