2 * Interface to libaacplus for aac+ (sbr+ps) encoding
3 * Copyright (c) 2010 tipok <piratfm@gmail.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 libaacplus for aac+ (sbr+ps) encoding.
30 typedef struct aacPlusAudioContext {
31 aacplusEncHandle aacplus_handle;
32 } aacPlusAudioContext;
34 static av_cold int aacPlus_encode_init(AVCodecContext *avctx)
36 aacPlusAudioContext *s = avctx->priv_data;
37 aacplusEncConfiguration *aacplus_cfg;
38 unsigned long samples_input, max_bytes_output;
40 /* number of channels */
41 if (avctx->channels < 1 || avctx->channels > 2) {
42 av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed\n", avctx->channels);
46 s->aacplus_handle = aacplusEncOpen(avctx->sample_rate,
48 &samples_input, &max_bytes_output);
49 if(!s->aacplus_handle) {
50 av_log(avctx, AV_LOG_ERROR, "can't open encoder\n");
54 /* check aacplus version */
55 aacplus_cfg = aacplusEncGetCurrentConfiguration(s->aacplus_handle);
57 /* put the options in the configuration struct */
58 if(avctx->profile != FF_PROFILE_AAC_LOW && avctx->profile != FF_PROFILE_UNKNOWN) {
59 av_log(avctx, AV_LOG_ERROR, "invalid AAC profile: %d, only LC supported\n", avctx->profile);
60 aacplusEncClose(s->aacplus_handle);
64 aacplus_cfg->bitRate = avctx->bit_rate;
65 aacplus_cfg->bandWidth = avctx->cutoff;
66 if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
67 aacplus_cfg->outputFormat = 0; //raw aac
69 aacplus_cfg->inputFormat = AACPLUS_INPUT_16BIT;
70 if (!aacplusEncSetConfiguration(s->aacplus_handle, aacplus_cfg)) {
71 av_log(avctx, AV_LOG_ERROR, "libaacplus doesn't support this output format!\n");
75 avctx->frame_size = samples_input / avctx->channels;
77 avctx->coded_frame= avcodec_alloc_frame();
78 avctx->coded_frame->key_frame= 1;
80 /* Set decoder specific info */
81 avctx->extradata_size = 0;
82 if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
84 unsigned char *buffer = NULL;
85 unsigned long decoder_specific_info_size;
87 if (aacplusEncGetDecoderSpecificInfo(s->aacplus_handle, &buffer,
88 &decoder_specific_info_size) == 1) {
89 avctx->extradata = av_malloc(decoder_specific_info_size + FF_INPUT_BUFFER_PADDING_SIZE);
90 avctx->extradata_size = decoder_specific_info_size;
91 memcpy(avctx->extradata, buffer, avctx->extradata_size);
95 #define free please_use_av_free
100 static int aacPlus_encode_frame(AVCodecContext *avctx,
101 unsigned char *frame, int buf_size, void *data)
103 aacPlusAudioContext *s = avctx->priv_data;
106 bytes_written = aacplusEncEncode(s->aacplus_handle,
108 avctx->frame_size * avctx->channels,
112 return bytes_written;
115 static av_cold int aacPlus_encode_close(AVCodecContext *avctx)
117 aacPlusAudioContext *s = avctx->priv_data;
119 av_freep(&avctx->coded_frame);
120 av_freep(&avctx->extradata);
122 aacplusEncClose(s->aacplus_handle);
126 AVCodec ff_libaacplus_encoder = {
130 sizeof(aacPlusAudioContext),
132 aacPlus_encode_frame,
133 aacPlus_encode_close,
134 .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
135 .long_name = NULL_IF_CONFIG_SMALL("libaacplus AAC+ (Advanced Audio Codec with SBR+PS)"),