Index: configure
===================================================================
--- configure	(revision 23548)
+++ configure	(working copy)
@@ -164,6 +164,7 @@
 External library support:
   --enable-avisynth        enable reading of AVISynth script files [no]
   --enable-bzlib           enable bzlib [autodetect]
+  --enable-libaacplus      enable AAC+ support via libaacplus [no]
   --enable-libopencore-amrnb enable AMR-NB de/encoding via libopencore-amrnb [no]
   --enable-libopencore-amrwb enable AMR-WB decoding via libopencore-amrwb [no]
   --enable-libdc1394       enable IIDC-1394 grabbing using libdc1394
@@ -915,6 +916,7 @@
     libdc1394
     libdirac
     libfaac
+    libaacplus
     libfaad
     libfaadbin
     libgsm
@@ -1335,6 +1337,7 @@
 libdirac_decoder_deps="libdirac !libschroedinger"
 libdirac_encoder_deps="libdirac"
 libfaac_encoder_deps="libfaac"
+libaacplus_encoder_deps="libaacplus"
 libfaad_decoder_deps="libfaad"
 libfaadbin_extralibs='$ldl'
 libgsm_decoder_deps="libgsm"
@@ -2610,6 +2613,8 @@
                       require  libdirac libdirac_decoder/dirac_parser.h dirac_decoder_init $(pkg-config --libs dirac) &&
                       require  libdirac libdirac_encoder/dirac_encoder.h dirac_encoder_init $(pkg-config --libs dirac)
 enabled libfaac    && require2 libfaac "stdint.h faac.h" faacEncGetVersion -lfaac
+enabled libaacplus && add_cflags $(pkg-config --cflags aacplus) &&
+                      require libaacplus libaacplus/aacenc.h AacInitDefaultConfig $(pkg-config --libs aacplus)
 enabled libfaad    && require2 libfaad faad.h faacDecOpen -lfaad
 enabled libgsm     && require  libgsm gsm/gsm.h gsm_create -lgsm
 enabled libmp3lame && require  libmp3lame lame/lame.h lame_init -lmp3lame -lm
@@ -2877,6 +2882,7 @@
 echo "libdc1394 support         ${libdc1394-no}"
 echo "libdirac enabled          ${libdirac-no}"
 echo "libfaac enabled           ${libfaac-no}"
+echo "libaacplus enabled        ${libaacplus-no}"
 echo "libfaad enabled           ${libfaad-no}"
 echo "libfaad dlopened          ${libfaadbin-no}"
 echo "libgsm enabled            ${libgsm-no}"
Index: libavcodec/libaacplus.c
===================================================================
--- libavcodec/libaacplus.c	(revision 0)
+++ libavcodec/libaacplus.c	(revision 0)
@@ -0,0 +1,265 @@
+/*
+ * Interface to libaacplus for aac+ encoding
+ * Copyright (c) 2002 The FFmpeg Team
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file libavcodec/libaacplus.c
+ * Interface to libaacplus for aac+ encoding.
+ */
+
+#include "avcodec.h"
+#include "put_bits.h"
+#include "mpeg4audio.h"
+#include <libaacplus/cfftn.h>
+#include <libaacplus/FloatFR.h>
+#include <libaacplus/resampler.h>
+#include <libaacplus/aacenc.h>
+#include <libaacplus/adts.h>
+#include <libaacplus/sbr_main.h>
+#include <libaacplus/aac_ram.h>
+#include <libaacplus/aac_rom.h>
+
+#define CORE_DELAY   (1600)
+/* ((1600 (core codec)*2 (multi rate) + 6*64 (sbr dec delay) - 2048 (sbr enc delay) + magic */
+#define INPUT_DELAY  ((CORE_DELAY)*2 +6*64-2048+1)     
+/* the additional max resampler filter delay (source fs) */
+#define MAX_DS_FILTER_DELAY 16
+/* (96-64) makes AAC still some 64 core samples too early wrt SBR ... maybe -32 would be even more correct, 
+ * but 1024-32 would need additional SBR bitstream delay by one frame */
+#define CORE_INPUT_OFFSET_PS (0)  
+
+typedef struct AacpAudioContext {
+    struct AAC_ENCODER *aacEnc;
+    HANDLE_SBR_ENCODER hEnvEnc;
+
+    AACENC_CONFIG     config;
+    sbrConfiguration sbrConfig;
+    
+    IIR21_RESAMPLER IIR21_reSampler[MAX_CHANNELS];
+    float inputBuffer[(AACENC_BLOCKSIZE*2 + MAX_DS_FILTER_DELAY + INPUT_DELAY)*MAX_CHANNELS];
+    
+    int nChannelsAAC, nChannelsSBR;
+    unsigned int sampleRateAAC;
+    int samplerate_index;
+
+    unsigned int numAncDataBytes;
+    int useParametricStereo;
+    int coreWriteOffset;
+    int envReadOffset;
+    int writeOffset;
+    
+    unsigned char ancDataBytes[MAX_PAYLOAD_SIZE];
+    unsigned char adtsDataBytes[ADTS_HEADER_SIZE];
+    int adtsOffset;
+} AacpAudioContext;
+
+/**
+ * Make AAC audio config object.
+ * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
+ */
+static void put_audio_specific_config(AVCodecContext *avctx)
+{
+    PutBitContext pb;
+    AacpAudioContext *s = avctx->priv_data;
+
+    init_put_bits(&pb, avctx->extradata, avctx->extradata_size*8);
+    put_bits(&pb, 5, FF_PROFILE_AAC_LOW+1);		/* we have HE-AAC, but it's not compatible with mp3 container */						    
+    put_bits(&pb, 4, s->samplerate_index); 		/* downsampled sample rate index */
+    put_bits(&pb, 4, s->nChannelsAAC);
+    
+    put_bits(&pb, 1, 0); //frame length - 1024 samples
+    put_bits(&pb, 1, 0); //depend on core coder
+    put_bits(&pb, 1, 0); //is not extension
+    flush_put_bits(&pb);	    
+}
+					       
+
+static av_cold int Aacp_encode_init(AVCodecContext *avctx)
+{
+    AacpAudioContext *s = avctx->priv_data;
+    int i;
+    
+    s->coreWriteOffset = 0;
+    s->envReadOffset = 0;
+    s->writeOffset=INPUT_DELAY*MAX_CHANNELS;
+
+    s->useParametricStereo = 0;
+    s->numAncDataBytes=0;
+    s->adtsOffset=ADTS_HEADER_SIZE;
+
+
+    /* number of channels */
+    if (avctx->channels < 1 || avctx->channels > 2) {
+        av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed\n", avctx->channels);
+        return -1;
+    }
+    
+    AacInitDefaultConfig(&s->config);
+    s->nChannelsAAC = s->nChannelsSBR = avctx->channels;
+    s->sampleRateAAC = avctx->sample_rate;
+    
+    /* ps or sbr? */
+    if ( (avctx->channels == 2) && (avctx->bit_rate >= 16000) && (avctx->bit_rate < 44001) ) {
+	s->useParametricStereo = 1;
+	s->nChannelsAAC = 1;
+	
+	s->envReadOffset = (MAX_DS_FILTER_DELAY + INPUT_DELAY)*MAX_CHANNELS;
+	s->coreWriteOffset = CORE_INPUT_OFFSET_PS;
+	s->writeOffset = s->envReadOffset;
+    } else {
+	/* set up 2:1 downsampling */
+	InitIIR21_Resampler(&(s->IIR21_reSampler[0]));
+	if (avctx->channels == 2) InitIIR21_Resampler(&(s->IIR21_reSampler[1]));
+	if (s->IIR21_reSampler[0].delay > MAX_DS_FILTER_DELAY) {
+	    av_log(avctx, AV_LOG_ERROR, "Initialisation of AAC+ encoder failed (IIR21_reSampler size too big)!\n");
+	    AacEncClose(s->aacEnc);
+	    return -1;
+	}
+	s->writeOffset += s->IIR21_reSampler[0].delay * avctx->channels; //MAX_CHANNELS;
+    }
+    
+    
+    s->config.bitRate = avctx->bit_rate;
+    s->config.nChannelsIn=avctx->channels;
+    s->config.nChannelsOut=s->nChannelsAAC;
+    s->config.bandWidth=avctx->cutoff;
+    
+    /* set up SBR configuration	*/
+    if(!IsSbrSettingAvail (avctx->bit_rate, s->nChannelsAAC, s->sampleRateAAC, &s->sampleRateAAC)) {
+	av_log(avctx, AV_LOG_ERROR, "No valid AAC+ SBR configuration found: br=%d, nChAAC=%d, srAAC=%d\n",
+		avctx->bit_rate, s->nChannelsAAC, s->sampleRateAAC );
+            return -1;
+    }
+
+    InitializeSbrDefaults (&s->sbrConfig);
+    s->sbrConfig.usePs = s->useParametricStereo;
+    
+    AdjustSbrSettings(&s->sbrConfig, avctx->bit_rate, s->nChannelsAAC, s->sampleRateAAC, AACENC_TRANS_FAC, 24000);
+    EnvOpen(&s->hEnvEnc, s->inputBuffer + s->coreWriteOffset, &s->sbrConfig, &s->config.bandWidth);
+    
+    /* set up AAC encoder, now that samling rate is known */
+    s->config.sampleRate = s->sampleRateAAC;
+    if (AacEncOpen(&s->aacEnc, s->config) != 0){
+	av_log(avctx, AV_LOG_ERROR, "Initialisation of AAC+ encoder failed!\n");
+	AacEncClose(s->aacEnc);
+	return -1;
+    }
+    
+    init_plans();
+    
+    /* set up input samples block size feed */
+    avctx->frame_size = AACENC_BLOCKSIZE * 2;
+    avctx->coded_frame= avcodec_alloc_frame();
+    avctx->coded_frame->key_frame= 1;
+    
+    avctx->extradata_size = 0;
+    /* Set decoder specific info */
+    if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
+
+        for(i = 0; i < 16; i++)
+            if(s->sampleRateAAC == ff_mpeg4audio_sample_rates[i])
+                break;
+        if(i == 16){
+            av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n", avctx->sample_rate);
+            return -1;
+        }
+        s->samplerate_index=i;
+
+        /* extradata init */
+        avctx->extradata = av_malloc(2);
+        avctx->extradata_size = 2;
+        put_audio_specific_config(avctx);
+        s->adtsOffset=0;
+    } else {
+	/* ADTS header needed */
+	adts_hdr(s->adtsDataBytes, &s->config);
+   }
+  
+    return 0;
+}
+
+
+static int Aacp_encode_frame(AVCodecContext *avctx,
+                             unsigned char *frame, int buf_size, void *data)
+{
+    AacpAudioContext *s = avctx->priv_data;
+    short *TimeDataPcm = data;
+    int i, ch, outSamples, bytes_written;
+
+    for (i=0; i<AACENC_BLOCKSIZE*2*avctx->channels; i++)
+	    s->inputBuffer[i+s->writeOffset] = (float) TimeDataPcm[i];
+
+    /* encode one SBR frame */
+    EnvEncodeFrame( s->hEnvEnc, s->inputBuffer + s->envReadOffset, 
+		    s->inputBuffer + s->coreWriteOffset, avctx->channels, 
+		    &s->numAncDataBytes, s->ancDataBytes);
+    
+    /* 2:1 downsampling for AAC core */
+    if (!s->useParametricStereo) {
+	for( ch=0; ch < avctx->channels; ch++ )
+	    IIR21_Downsample( &(s->IIR21_reSampler[ch] ), s->inputBuffer + s->writeOffset+ch, 
+			    AACENC_BLOCKSIZE * 2, avctx->channels, s->inputBuffer + ch, &outSamples, avctx->channels);
+    }
+    
+    /* encode one AAC frame */
+    AacEncEncode(	s->aacEnc, s->inputBuffer, 
+			s->useParametricStereo ? 1 : avctx->channels, /* stride (step) */ s->ancDataBytes, 
+		        &s->numAncDataBytes, (unsigned *) (frame + s->adtsOffset), &bytes_written);
+    
+    /* write ADTS header (if needed) */
+    if(s->adtsOffset){
+	memcpy(frame, s->adtsDataBytes, s->adtsOffset);
+	adts_hdr_up(frame, bytes_written);
+        bytes_written += s->adtsOffset;
+    }
+    
+    if (s->useParametricStereo){
+        memcpy( s->inputBuffer,s->inputBuffer+AACENC_BLOCKSIZE,CORE_INPUT_OFFSET_PS*sizeof(float));
+    } else {
+        memmove( s->inputBuffer,s->inputBuffer+AACENC_BLOCKSIZE*2*avctx->channels,s->writeOffset*sizeof(float));
+    }
+
+    return bytes_written;
+}
+
+static av_cold int Aacp_encode_close(AVCodecContext *avctx)
+{
+    AacpAudioContext *s = avctx->priv_data;
+
+    av_freep(&avctx->coded_frame);
+    av_freep(&avctx->extradata);
+
+    AacEncClose(s->aacEnc);
+    destroy_plans();
+    EnvClose(s->hEnvEnc);
+    return 0;
+}
+
+AVCodec libaacplus_encoder = {
+    "libaacplus",
+    CODEC_TYPE_AUDIO,
+    CODEC_ID_AAC,
+    sizeof(AacpAudioContext),
+    Aacp_encode_init,
+    Aacp_encode_frame,
+    Aacp_encode_close,
+    .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
+    .long_name = NULL_IF_CONFIG_SMALL("libaacplus AAC+ (he-aac+, he-aac+ v2)"),
+};
Index: libavcodec/allcodecs.c
===================================================================
--- libavcodec/allcodecs.c	(revision 23548)
+++ libavcodec/allcodecs.c	(working copy)
@@ -339,6 +339,7 @@
     /* external libraries */
     REGISTER_ENCDEC  (LIBDIRAC, libdirac);
     REGISTER_ENCODER (LIBFAAC, libfaac);
+    REGISTER_ENCODER (LIBAACPLUS, libaacplus);
     REGISTER_DECODER (LIBFAAD, libfaad);
     REGISTER_ENCDEC  (LIBGSM, libgsm);
     REGISTER_ENCDEC  (LIBGSM_MS, libgsm_ms);
Index: libavcodec/Makefile
===================================================================
--- libavcodec/Makefile	(revision 23548)
+++ libavcodec/Makefile	(working copy)
@@ -516,6 +516,7 @@
                                           flacdec.o flacdata.o flac.o
 
 # external codec libraries
+OBJS-$(CONFIG_LIBAACPLUS_ENCODER)         += libaacplus.o
 OBJS-$(CONFIG_LIBDIRAC_DECODER)           += libdiracdec.o
 OBJS-$(CONFIG_LIBDIRAC_ENCODER)           += libdiracenc.o libdirac_libschro.o
 OBJS-$(CONFIG_LIBFAAC_ENCODER)            += libfaac.o
Index: doc/general.texi
===================================================================
--- doc/general.texi	(revision 23548)
+++ doc/general.texi	(working copy)
@@ -516,6 +516,8 @@
 @item 8SVX audio             @tab     @tab  X
 @item AAC                    @tab  E  @tab  X
     @tab encoding supported through external library libfaac
+@item AACP                   @tab  E  @tab  X
+    @tab encoding supported through external library libaacplus
 @item AC-3                   @tab IX  @tab  X
 @item ADPCM 4X Movie         @tab     @tab  X
 @item ADPCM CDROM XA         @tab     @tab  X
@@ -1030,7 +1032,7 @@
 (@url{http://sourceware.org/cygwinports/}) :
 
 @example
-yasm, libSDL-devel, libdirac-devel, libfaac-devel, libfaad-devel, libgsm-devel,
+yasm, libSDL-devel, libdirac-devel, libfaac-devel, libaacplus-devel, libfaad-devel, libgsm-devel,
 libmp3lame-devel, libschroedinger1.0-devel, speex-devel, libtheora-devel,
 libxvidcore-devel
 @end example
