FFmpeg
|
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 const char * | get_error_text (const int error) |
Convert an error code into a text message. More... | |
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... | |
simple audio converter
Definition in file transcode_aac.c.
#define OUTPUT_BIT_RATE 96000 |
The output bit rate in kbit/s.
Definition at line 44 of file transcode_aac.c.
Referenced by open_output_file().
#define OUTPUT_CHANNELS 2 |
The number of output channels.
Definition at line 46 of file transcode_aac.c.
Referenced by open_output_file().
|
static |
Convert an error code into a text message.
error | Error code to be converted |
Definition at line 53 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 |
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 61 of file transcode_aac.c.
Referenced by main().
|
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 easier access later.
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.
Definition at line 120 of file transcode_aac.c.
Referenced by main().
Initialize one data packet for reading or writing.
Set the packet data and size so that it is recognized as being empty.
Definition at line 214 of file transcode_aac.c.
Referenced by decode_audio_frame(), and encode_audio_frame().
|
static |
Initialize one audio frame for reading from the input file.
Definition at line 223 of file transcode_aac.c.
Referenced by read_decode_convert_and_store().
|
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.
Definition at line 237 of file transcode_aac.c.
Referenced by main().
|
static |
Initialize a FIFO buffer for the audio samples to be encoded.
Create the FIFO buffer based on the specified output sample format.
Definition at line 279 of file transcode_aac.c.
Referenced by main().
|
static |
Write the header of the output file container.
Definition at line 291 of file transcode_aac.c.
Referenced by main().
|
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.
Definition at line 303 of file transcode_aac.c.
Referenced by read_decode_convert_and_store().
|
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.
Definition at line 354 of file transcode_aac.c.
Referenced by read_decode_convert_and_store().
|
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.
Definition at line 394 of file transcode_aac.c.
Referenced by read_decode_convert_and_store().
|
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.
Definition at line 413 of file transcode_aac.c.
Referenced by read_decode_convert_and_store().
|
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.
Definition at line 441 of file transcode_aac.c.
Referenced by main().
|
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.
Definition at line 508 of file transcode_aac.c.
Referenced by load_encode_and_write().
|
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.
Definition at line 550 of file transcode_aac.c.
Referenced by load_encode_and_write(), and main().
|
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.
Definition at line 597 of file transcode_aac.c.
Referenced by main().
|
static |
Write the trailer of the output file container.
Definition at line 637 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 649 of file transcode_aac.c.
|
static |
Global timestamp for the audio frames.
Definition at line 547 of file transcode_aac.c.
Referenced by adjust_write_index(), ape_read_header(), asf_read_pts(), asf_write_packet(), av_parser_parse2(), build_frame_code(), decode(), decode_frame(), decode_frame_header(), decode_thread(), decode_vop_header(), dirac_combine_frame(), dirac_gptopts(), display_end_segment(), do_subtitle_out(), encode_audio_frame(), ff_filter_frame_framed(), ff_subtitles_queue_seek(), 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_input_packet(), guess_correct_pts(), handle_metadata(), init(), libschroedinger_decode_frame(), load_input_picture(), mkv_write_cues(), 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(), parse_forced_key_frames(), parse_presentation_segment(), parse_psfile(), print_report(), queue_picture(), read_frame_internal(), read_packet(), read_trun_duration(), reap_screen(), rm_read_index(), rv34_parse(), scan_file(), set_clock_at(), srt_read_header(), sub2video_push_ref(), sub2video_update(), subtitle_thread(), sup_read_packet(), swr_next_pts(), update_initial_timestamps(), vc1t_read_packet(), video_thread(), vp8_gptopts(), and write_pts().