yading@10: /* yading@10: * Interface to libaacplus for aac+ (sbr+ps) encoding yading@10: * Copyright (c) 2010 tipok yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * Interface to libaacplus for aac+ (sbr+ps) encoding. yading@10: */ yading@10: yading@10: #include yading@10: yading@10: #include "avcodec.h" yading@10: #include "internal.h" yading@10: yading@10: typedef struct aacPlusAudioContext { yading@10: aacplusEncHandle aacplus_handle; yading@10: unsigned long max_output_bytes; yading@10: unsigned long samples_input; yading@10: } aacPlusAudioContext; yading@10: yading@10: static av_cold int aacPlus_encode_init(AVCodecContext *avctx) yading@10: { yading@10: aacPlusAudioContext *s = avctx->priv_data; yading@10: aacplusEncConfiguration *aacplus_cfg; yading@10: yading@10: /* number of channels */ yading@10: if (avctx->channels < 1 || avctx->channels > 2) { yading@10: av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed\n", avctx->channels); yading@10: return -1; yading@10: } yading@10: yading@10: s->aacplus_handle = aacplusEncOpen(avctx->sample_rate, avctx->channels, yading@10: &s->samples_input, &s->max_output_bytes); yading@10: if(!s->aacplus_handle) { yading@10: av_log(avctx, AV_LOG_ERROR, "can't open encoder\n"); yading@10: return -1; yading@10: } yading@10: yading@10: /* check aacplus version */ yading@10: aacplus_cfg = aacplusEncGetCurrentConfiguration(s->aacplus_handle); yading@10: yading@10: /* put the options in the configuration struct */ yading@10: if(avctx->profile != FF_PROFILE_AAC_LOW && avctx->profile != FF_PROFILE_UNKNOWN) { yading@10: av_log(avctx, AV_LOG_ERROR, "invalid AAC profile: %d, only LC supported\n", avctx->profile); yading@10: aacplusEncClose(s->aacplus_handle); yading@10: return -1; yading@10: } yading@10: yading@10: aacplus_cfg->bitRate = avctx->bit_rate; yading@10: aacplus_cfg->bandWidth = avctx->cutoff; yading@10: aacplus_cfg->outputFormat = !(avctx->flags & CODEC_FLAG_GLOBAL_HEADER); yading@10: aacplus_cfg->inputFormat = avctx->sample_fmt == AV_SAMPLE_FMT_FLT ? AACPLUS_INPUT_FLOAT : AACPLUS_INPUT_16BIT; yading@10: if (!aacplusEncSetConfiguration(s->aacplus_handle, aacplus_cfg)) { yading@10: av_log(avctx, AV_LOG_ERROR, "libaacplus doesn't support this output format!\n"); yading@10: return -1; yading@10: } yading@10: yading@10: avctx->frame_size = s->samples_input / avctx->channels; yading@10: yading@10: /* Set decoder specific info */ yading@10: avctx->extradata_size = 0; yading@10: if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) { yading@10: yading@10: unsigned char *buffer = NULL; yading@10: unsigned long decoder_specific_info_size; yading@10: yading@10: if (aacplusEncGetDecoderSpecificInfo(s->aacplus_handle, &buffer, yading@10: &decoder_specific_info_size) == 1) { yading@10: avctx->extradata = av_malloc(decoder_specific_info_size + FF_INPUT_BUFFER_PADDING_SIZE); yading@10: avctx->extradata_size = decoder_specific_info_size; yading@10: memcpy(avctx->extradata, buffer, avctx->extradata_size); yading@10: } yading@10: free(buffer); yading@10: } yading@10: return 0; yading@10: } yading@10: yading@10: static int aacPlus_encode_frame(AVCodecContext *avctx, AVPacket *pkt, yading@10: const AVFrame *frame, int *got_packet) yading@10: { yading@10: aacPlusAudioContext *s = avctx->priv_data; yading@10: int32_t *input_buffer = (int32_t *)frame->data[0]; yading@10: int ret; yading@10: yading@10: if ((ret = ff_alloc_packet2(avctx, pkt, s->max_output_bytes)) < 0) yading@10: return ret; yading@10: yading@10: pkt->size = aacplusEncEncode(s->aacplus_handle, input_buffer, yading@10: s->samples_input, pkt->data, pkt->size); yading@10: *got_packet = 1; yading@10: pkt->pts = frame->pts; yading@10: return 0; yading@10: } yading@10: yading@10: static av_cold int aacPlus_encode_close(AVCodecContext *avctx) yading@10: { yading@10: aacPlusAudioContext *s = avctx->priv_data; yading@10: yading@10: av_freep(&avctx->extradata); yading@10: yading@10: aacplusEncClose(s->aacplus_handle); yading@10: return 0; yading@10: } yading@10: yading@10: static const AVProfile profiles[] = { yading@10: { FF_PROFILE_AAC_LOW, "LC" }, yading@10: { FF_PROFILE_UNKNOWN }, yading@10: }; yading@10: yading@10: AVCodec ff_libaacplus_encoder = { yading@10: .name = "libaacplus", yading@10: .type = AVMEDIA_TYPE_AUDIO, yading@10: .id = AV_CODEC_ID_AAC, yading@10: .priv_data_size = sizeof(aacPlusAudioContext), yading@10: .init = aacPlus_encode_init, yading@10: .encode2 = aacPlus_encode_frame, yading@10: .close = aacPlus_encode_close, yading@10: .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16, yading@10: AV_SAMPLE_FMT_FLT, yading@10: AV_SAMPLE_FMT_NONE }, yading@10: .long_name = NULL_IF_CONFIG_SMALL("libaacplus AAC+ (Advanced Audio Codec with SBR+PS)"), yading@10: .profiles = profiles, yading@10: .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO, yading@10: AV_CH_LAYOUT_STEREO, yading@10: 0 }, yading@10: };