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 av_cold
int Faac_encode_init(AVCodecContext
*avctx
)
36 FaacAudioContext
*s
= avctx
->priv_data
;
37 faacEncConfigurationPtr faac_cfg
;
38 unsigned long samples_input
, max_bytes_output
;
40 /* number of channels */
41 if (avctx
->channels
< 1 || avctx
->channels
> 6)
44 s
->faac_handle
= faacEncOpen(avctx
->sample_rate
,
46 &samples_input
, &max_bytes_output
);
48 /* check faac version */
49 faac_cfg
= faacEncGetCurrentConfiguration(s
->faac_handle
);
50 if (faac_cfg
->version
!= FAAC_CFG_VERSION
) {
51 av_log(avctx
, AV_LOG_ERROR
, "wrong libfaac version (compiled for: %d, using %d)\n", FAAC_CFG_VERSION
, faac_cfg
->version
);
52 faacEncClose(s
->faac_handle
);
56 /* put the options in the configuration struct */
57 switch(avctx
->profile
) {
58 case FF_PROFILE_AAC_MAIN
:
59 faac_cfg
->aacObjectType
= MAIN
;
61 case FF_PROFILE_UNKNOWN
:
62 case FF_PROFILE_AAC_LOW
:
63 faac_cfg
->aacObjectType
= LOW
;
65 case FF_PROFILE_AAC_SSR
:
66 faac_cfg
->aacObjectType
= SSR
;
68 case FF_PROFILE_AAC_LTP
:
69 faac_cfg
->aacObjectType
= LTP
;
72 av_log(avctx
, AV_LOG_ERROR
, "invalid AAC profile\n");
73 faacEncClose(s
->faac_handle
);
76 faac_cfg
->mpegVersion
= MPEG4
;
78 faac_cfg
->allowMidside
= 1;
79 faac_cfg
->bitRate
= avctx
->bit_rate
/ avctx
->channels
;
80 faac_cfg
->bandWidth
= avctx
->cutoff
;
81 if(avctx
->flags
& CODEC_FLAG_QSCALE
) {
82 faac_cfg
->bitRate
= 0;
83 faac_cfg
->quantqual
= avctx
->global_quality
/ FF_QP2LAMBDA
;
85 faac_cfg
->outputFormat
= 1;
86 faac_cfg
->inputFormat
= FAAC_INPUT_16BIT
;
88 avctx
->frame_size
= samples_input
/ avctx
->channels
;
90 avctx
->coded_frame
= avcodec_alloc_frame();
91 avctx
->coded_frame
->key_frame
= 1;
93 /* Set decoder specific info */
94 avctx
->extradata_size
= 0;
95 if (avctx
->flags
& CODEC_FLAG_GLOBAL_HEADER
) {
97 unsigned char *buffer
= NULL
;
98 unsigned long decoder_specific_info_size
;
100 if (!faacEncGetDecoderSpecificInfo(s
->faac_handle
, &buffer
,
101 &decoder_specific_info_size
)) {
102 avctx
->extradata
= av_malloc(decoder_specific_info_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
103 avctx
->extradata_size
= decoder_specific_info_size
;
104 memcpy(avctx
->extradata
, buffer
, avctx
->extradata_size
);
105 faac_cfg
->outputFormat
= 0;
109 #define free please_use_av_free
112 if (!faacEncSetConfiguration(s
->faac_handle
, faac_cfg
)) {
113 av_log(avctx
, AV_LOG_ERROR
, "libfaac doesn't support this output format!\n");
120 static int Faac_encode_frame(AVCodecContext
*avctx
,
121 unsigned char *frame
, int buf_size
, void *data
)
123 FaacAudioContext
*s
= avctx
->priv_data
;
126 bytes_written
= faacEncEncode(s
->faac_handle
,
128 avctx
->frame_size
* avctx
->channels
,
132 return bytes_written
;
135 static av_cold
int Faac_encode_close(AVCodecContext
*avctx
)
137 FaacAudioContext
*s
= avctx
->priv_data
;
139 av_freep(&avctx
->coded_frame
);
140 av_freep(&avctx
->extradata
);
142 faacEncClose(s
->faac_handle
);
146 AVCodec libfaac_encoder
= {
150 sizeof(FaacAudioContext
),
154 .long_name
= "libfaac AAC (Advanced Audio Codec)",