2 * Interface to libfaac for aac encoding
3 * Copyright (c) 2002 Gildas Bazin <gbazin@netcourrier.com>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Interface to libfaac for aac encoding.
30 typedef struct FaacAudioContext {
31 faacEncHandle faac_handle;
34 static const int channel_maps[][6] = {
35 { 2, 0, 1 }, //< C L R
36 { 2, 0, 1, 3 }, //< C L R Cs
37 { 2, 0, 1, 3, 4 }, //< C L R Ls Rs
38 { 2, 0, 1, 4, 5, 3 }, //< C L R Ls Rs LFE
41 static av_cold int Faac_encode_init(AVCodecContext *avctx)
43 FaacAudioContext *s = avctx->priv_data;
44 faacEncConfigurationPtr faac_cfg;
45 unsigned long samples_input, max_bytes_output;
47 /* number of channels */
48 if (avctx->channels < 1 || avctx->channels > 6) {
49 av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed\n", avctx->channels);
53 s->faac_handle = faacEncOpen(avctx->sample_rate,
55 &samples_input, &max_bytes_output);
57 /* check faac version */
58 faac_cfg = faacEncGetCurrentConfiguration(s->faac_handle);
59 if (faac_cfg->version != FAAC_CFG_VERSION) {
60 av_log(avctx, AV_LOG_ERROR, "wrong libfaac version (compiled for: %d, using %d)\n", FAAC_CFG_VERSION, faac_cfg->version);
61 faacEncClose(s->faac_handle);
65 /* put the options in the configuration struct */
66 switch(avctx->profile) {
67 case FF_PROFILE_AAC_MAIN:
68 faac_cfg->aacObjectType = MAIN;
70 case FF_PROFILE_UNKNOWN:
71 case FF_PROFILE_AAC_LOW:
72 faac_cfg->aacObjectType = LOW;
74 case FF_PROFILE_AAC_SSR:
75 faac_cfg->aacObjectType = SSR;
77 case FF_PROFILE_AAC_LTP:
78 faac_cfg->aacObjectType = LTP;
81 av_log(avctx, AV_LOG_ERROR, "invalid AAC profile\n");
82 faacEncClose(s->faac_handle);
85 faac_cfg->mpegVersion = MPEG4;
87 faac_cfg->allowMidside = 1;
88 faac_cfg->bitRate = avctx->bit_rate / avctx->channels;
89 faac_cfg->bandWidth = avctx->cutoff;
90 if(avctx->flags & CODEC_FLAG_QSCALE) {
91 faac_cfg->bitRate = 0;
92 faac_cfg->quantqual = avctx->global_quality / FF_QP2LAMBDA;
94 faac_cfg->outputFormat = 1;
95 faac_cfg->inputFormat = FAAC_INPUT_16BIT;
96 if (avctx->channels > 2)
97 memcpy(faac_cfg->channel_map, channel_maps[avctx->channels-3],
98 avctx->channels * sizeof(int));
100 avctx->frame_size = samples_input / avctx->channels;
102 avctx->coded_frame= avcodec_alloc_frame();
103 avctx->coded_frame->key_frame= 1;
105 /* Set decoder specific info */
106 avctx->extradata_size = 0;
107 if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
109 unsigned char *buffer = NULL;
110 unsigned long decoder_specific_info_size;
112 if (!faacEncGetDecoderSpecificInfo(s->faac_handle, &buffer,
113 &decoder_specific_info_size)) {
114 avctx->extradata = av_malloc(decoder_specific_info_size + FF_INPUT_BUFFER_PADDING_SIZE);
115 avctx->extradata_size = decoder_specific_info_size;
116 memcpy(avctx->extradata, buffer, avctx->extradata_size);
117 faac_cfg->outputFormat = 0;
121 #define free please_use_av_free
124 if (!faacEncSetConfiguration(s->faac_handle, faac_cfg)) {
125 av_log(avctx, AV_LOG_ERROR, "libfaac doesn't support this output format!\n");
132 static int Faac_encode_frame(AVCodecContext *avctx,
133 unsigned char *frame, int buf_size, void *data)
135 FaacAudioContext *s = avctx->priv_data;
137 int num_samples = data ? avctx->frame_size : 0;
139 bytes_written = faacEncEncode(s->faac_handle,
141 num_samples * avctx->channels,
145 return bytes_written;
148 static av_cold int Faac_encode_close(AVCodecContext *avctx)
150 FaacAudioContext *s = avctx->priv_data;
152 av_freep(&avctx->coded_frame);
153 av_freep(&avctx->extradata);
155 faacEncClose(s->faac_handle);
159 static const AVProfile profiles[] = {
160 { FF_PROFILE_AAC_MAIN, "Main" },
161 { FF_PROFILE_AAC_LOW, "LC" },
162 { FF_PROFILE_AAC_SSR, "SSR" },
163 { FF_PROFILE_AAC_LTP, "LTP" },
164 { FF_PROFILE_UNKNOWN },
167 AVCodec ff_libfaac_encoder = {
169 .type = AVMEDIA_TYPE_AUDIO,
171 .priv_data_size = sizeof(FaacAudioContext),
172 .init = Faac_encode_init,
173 .encode = Faac_encode_frame,
174 .close = Faac_encode_close,
175 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
176 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
177 .long_name = NULL_IF_CONFIG_SMALL("libfaac AAC (Advanced Audio Codec)"),
178 .profiles = NULL_IF_CONFIG_SMALL(profiles),