Loading...
Searching...
No Matches
transcode_aac.c File Reference
`#include <stdio.h>`
`#include <libavutil/mem.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/channel_layout.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
#define | OUTPUT_CHANNELS 2

## 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.
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 int | 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, AVCodecContext *output_codec_context)
| 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, decode, convert and store 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)

## Variables

static int64_t | pts = 0

## Macro Definition Documentation

## OUTPUT_BIT_RATE

#define OUTPUT_BIT_RATE 96000

Examples
  transcode_aac.c.

Definition at line 48 of file transcode_aac.c.

## OUTPUT_CHANNELS

#define OUTPUT_CHANNELS 2

Examples
  transcode_aac.c.

Definition at line 50 of file transcode_aac.c.

## Function Documentation

## open_input_file()

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.

Parameters

  | filename | File to be opened
  [out] | input_format_context | Format context of opened file
  [out] | input_codec_context | Codec context of opened file

Returns
  Error code (0 if successful)

Definition at line 59 of file transcode_aac.c.

## open_output_file()

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.

Parameters

  | filename | File to be opened
  | input_codec_context | Codec context of input file
  [out] | output_format_context | Format context of output file
  [out] | output_codec_context | Codec context of output file

Returns
  Error code (0 if successful)

Examples
  transcode.c, and transcode_aac.c.

Definition at line 146 of file transcode_aac.c.

Referenced by main().

## init_packet()

static int init_packet | ( | AVPacket ** | packet | ) |

static

Initialize one data packet for reading or writing.

Parameters

  [out] | packet | Packet to be initialized

Returns
  Error code (0 if successful)

Examples
  transcode_aac.c.

Definition at line 254 of file transcode_aac.c.

Referenced by decode_audio_frame(), and encode_audio_frame().

## init_input_frame()

static int init_input_frame | ( | AVFrame ** | frame | ) |

static

Initialize one audio frame for reading from the input file.

Parameters

  [out] | frame | Frame to be initialized

Returns
  Error code (0 if successful)

Examples
  transcode_aac.c.

Definition at line 268 of file transcode_aac.c.

Referenced by read_decode_convert_and_store().

## init_resampler()

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.

Parameters

  | input_codec_context | Codec context of the input file
  | output_codec_context | Codec context of the output file
  [out] | resample_context | Resample context for the required conversion

Returns
  Error code (0 if successful)

Examples
  transcode_aac.c.

Definition at line 286 of file transcode_aac.c.

Referenced by main().

## init_fifo()

static int init_fifo | ( | AVAudioFifo ** | fifo,
|  | AVCodecContext * | output_codec_context
| ) |  |

static

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

Parameters

  [out] | fifo | Sample buffer
  | output_codec_context | Codec context of the output file

Returns
  Error code (0 if successful)

Examples
  transcode_aac.c.

Definition at line 330 of file transcode_aac.c.

Referenced by main().

## write_output_file_header()

static int write_output_file_header | ( | AVFormatContext * | output_format_context | ) |

static

Write the header of the output file container.

Parameters

  output_format_context | Format context of the output file

Returns
  Error code (0 if successful)

Examples
  transcode_aac.c.

Definition at line 346 of file transcode_aac.c.

Referenced by main().

## decode_audio_frame()

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.

Parameters

  | frame | Audio frame to be decoded
  | input_format_context | Format context of the input file
  | input_codec_context | Codec context of the input file
  [out] | data_present | Indicates whether data has been decoded
  [out] | finished | Indicates whether the end of file has been reached and all data has been decoded. If this flag is false, there is more data to be decoded, i.e., this function has to be called again.

Returns
  Error code (0 if successful)

Examples
  transcode_aac.c.

Definition at line 370 of file transcode_aac.c.

Referenced by read_decode_convert_and_store().

## init_converted_samples()

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.

Parameters

  [out] | converted_input_samples | Array of converted samples. The dimensions are reference, channel (for multi-channel audio), sample.
  | output_codec_context | Codec context of the output file
  | frame_size | Number of samples to be converted in each round

Returns
  Error code (0 if successful)

Examples
  transcode_aac.c.

Definition at line 444 of file transcode_aac.c.

Referenced by read_decode_convert_and_store().

## convert_samples()

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.

Parameters

  | input_data | Samples to be decoded. The dimensions are channel (for multi-channel audio), sample.
  [out] | converted_data | Converted samples. The dimensions are channel (for multi-channel audio), sample.
  | frame_size | Number of samples to be converted
  | resample_context | Resample context for the conversion

Returns
  Error code (0 if successful)

Examples
  transcode_aac.c.

Definition at line 479 of file transcode_aac.c.

Referenced by read_decode_convert_and_store().

## add_samples_to_fifo()

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.

Parameters

  fifo | Buffer to add the samples to
  converted_input_samples | Samples to be added. The dimensions are channel (for multi-channel audio), sample.
  frame_size | Number of samples to be converted

Returns
  Error code (0 if successful)

Examples
  transcode_aac.c.

Definition at line 505 of file transcode_aac.c.

Referenced by read_decode_convert_and_store().

## 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, decode, convert and store it in the FIFO buffer.

Parameters

  | fifo | Buffer used for temporary storage
  | input_format_context | Format context of the input file
  | input_codec_context | Codec context of the input file
  | output_codec_context | Codec context of the output file
  | resampler_context | Resample context for the conversion
  [out] | finished | Indicates whether the end of file has been reached and all data has been decoded. If this flag is false, there is more data to be decoded, i.e., this function has to be called again.

Returns
  Error code (0 if successful)

Examples
  transcode_aac.c.

Definition at line 543 of file transcode_aac.c.

Referenced by main().

## init_output_frame()

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.

Parameters

  [out] | frame | Frame to be initialized
  | output_codec_context | Codec context of the output file
  | frame_size | Size of the frame

Returns
  Error code (0 if successful)

Examples
  transcode_aac.c.

Definition at line 609 of file transcode_aac.c.

Referenced by load_encode_and_write().

## encode_audio_frame()

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.

Parameters

  | frame | Samples to be encoded
  | output_format_context | Format context of the output file
  | output_codec_context | Codec context of the output file
  [out] | data_present | Indicates whether data has been encoded

Returns
  Error code (0 if successful)

Examples
  transcode_aac.c.

Definition at line 655 of file transcode_aac.c.

Referenced by load_encode_and_write(), and main().

## load_encode_and_write()

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.

Parameters

  fifo | Buffer used for temporary storage
  output_format_context | Format context of the output file
  output_codec_context | Codec context of the output file

Returns
  Error code (0 if successful)

Examples
  transcode_aac.c.

Definition at line 727 of file transcode_aac.c.

Referenced by main().

## write_output_file_trailer()

static int write_output_file_trailer | ( | AVFormatContext * | output_format_context | ) |

static

Write the trailer of the output file container.

Parameters

  output_format_context | Format context of the output file

Returns
  Error code (0 if successful)

Examples
  transcode_aac.c.

Definition at line 767 of file transcode_aac.c.

Referenced by main().

## main()

int main | ( | int | argc,
|  | char ** | argv
| ) |  |

Definition at line 778 of file transcode_aac.c.

## Variable Documentation

## pts

int64_t pts = 0

static

Examples
  transcode_aac.c.

Definition at line 644 of file transcode_aac.c.

Referenced by encode_audio_frame().