00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include <faac.h>
00028
00029 #include "avcodec.h"
00030 #include "audio_frame_queue.h"
00031 #include "internal.h"
00032 #include "libavutil/audioconvert.h"
00033
00034
00035
00036 #define FAAC_DELAY_SAMPLES 1024
00037
00038 typedef struct FaacAudioContext {
00039 faacEncHandle faac_handle;
00040 AudioFrameQueue afq;
00041 } FaacAudioContext;
00042
00043 static av_cold int Faac_encode_close(AVCodecContext *avctx)
00044 {
00045 FaacAudioContext *s = avctx->priv_data;
00046
00047 #if FF_API_OLD_ENCODE_AUDIO
00048 av_freep(&avctx->coded_frame);
00049 #endif
00050 av_freep(&avctx->extradata);
00051 ff_af_queue_close(&s->afq);
00052
00053 if (s->faac_handle)
00054 faacEncClose(s->faac_handle);
00055
00056 return 0;
00057 }
00058
00059 static const int channel_maps[][6] = {
00060 { 2, 0, 1 },
00061 { 2, 0, 1, 3 },
00062 { 2, 0, 1, 3, 4 },
00063 { 2, 0, 1, 4, 5, 3 },
00064 };
00065
00066 static av_cold int Faac_encode_init(AVCodecContext *avctx)
00067 {
00068 FaacAudioContext *s = avctx->priv_data;
00069 faacEncConfigurationPtr faac_cfg;
00070 unsigned long samples_input, max_bytes_output;
00071 int ret;
00072
00073
00074 if (avctx->channels < 1 || avctx->channels > 6) {
00075 av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed\n", avctx->channels);
00076 ret = AVERROR(EINVAL);
00077 goto error;
00078 }
00079
00080 s->faac_handle = faacEncOpen(avctx->sample_rate,
00081 avctx->channels,
00082 &samples_input, &max_bytes_output);
00083 if (!s->faac_handle) {
00084 av_log(avctx, AV_LOG_ERROR, "error in faacEncOpen()\n");
00085 ret = AVERROR_UNKNOWN;
00086 goto error;
00087 }
00088
00089
00090 faac_cfg = faacEncGetCurrentConfiguration(s->faac_handle);
00091 if (faac_cfg->version != FAAC_CFG_VERSION) {
00092 av_log(avctx, AV_LOG_ERROR, "wrong libfaac version (compiled for: %d, using %d)\n", FAAC_CFG_VERSION, faac_cfg->version);
00093 ret = AVERROR(EINVAL);
00094 goto error;
00095 }
00096
00097
00098 switch(avctx->profile) {
00099 case FF_PROFILE_AAC_MAIN:
00100 faac_cfg->aacObjectType = MAIN;
00101 break;
00102 case FF_PROFILE_UNKNOWN:
00103 case FF_PROFILE_AAC_LOW:
00104 faac_cfg->aacObjectType = LOW;
00105 break;
00106 case FF_PROFILE_AAC_SSR:
00107 faac_cfg->aacObjectType = SSR;
00108 break;
00109 case FF_PROFILE_AAC_LTP:
00110 faac_cfg->aacObjectType = LTP;
00111 break;
00112 default:
00113 av_log(avctx, AV_LOG_ERROR, "invalid AAC profile\n");
00114 ret = AVERROR(EINVAL);
00115 goto error;
00116 }
00117 faac_cfg->mpegVersion = MPEG4;
00118 faac_cfg->useTns = 0;
00119 faac_cfg->allowMidside = 1;
00120 faac_cfg->bitRate = avctx->bit_rate / avctx->channels;
00121 faac_cfg->bandWidth = avctx->cutoff;
00122 if(avctx->flags & CODEC_FLAG_QSCALE) {
00123 faac_cfg->bitRate = 0;
00124 faac_cfg->quantqual = avctx->global_quality / FF_QP2LAMBDA;
00125 }
00126 faac_cfg->outputFormat = 1;
00127 faac_cfg->inputFormat = FAAC_INPUT_16BIT;
00128 if (avctx->channels > 2)
00129 memcpy(faac_cfg->channel_map, channel_maps[avctx->channels-3],
00130 avctx->channels * sizeof(int));
00131
00132 avctx->frame_size = samples_input / avctx->channels;
00133
00134 #if FF_API_OLD_ENCODE_AUDIO
00135 avctx->coded_frame= avcodec_alloc_frame();
00136 if (!avctx->coded_frame) {
00137 ret = AVERROR(ENOMEM);
00138 goto error;
00139 }
00140 #endif
00141
00142
00143 avctx->extradata_size = 0;
00144 if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
00145
00146 unsigned char *buffer = NULL;
00147 unsigned long decoder_specific_info_size;
00148
00149 if (!faacEncGetDecoderSpecificInfo(s->faac_handle, &buffer,
00150 &decoder_specific_info_size)) {
00151 avctx->extradata = av_malloc(decoder_specific_info_size + FF_INPUT_BUFFER_PADDING_SIZE);
00152 if (!avctx->extradata) {
00153 ret = AVERROR(ENOMEM);
00154 goto error;
00155 }
00156 avctx->extradata_size = decoder_specific_info_size;
00157 memcpy(avctx->extradata, buffer, avctx->extradata_size);
00158 faac_cfg->outputFormat = 0;
00159 }
00160 #undef free
00161 free(buffer);
00162 #define free please_use_av_free
00163 }
00164
00165 if (!faacEncSetConfiguration(s->faac_handle, faac_cfg)) {
00166 av_log(avctx, AV_LOG_ERROR, "libfaac doesn't support this output format!\n");
00167 ret = AVERROR(EINVAL);
00168 goto error;
00169 }
00170
00171 avctx->delay = FAAC_DELAY_SAMPLES;
00172 ff_af_queue_init(avctx, &s->afq);
00173
00174 return 0;
00175 error:
00176 Faac_encode_close(avctx);
00177 return ret;
00178 }
00179
00180 static int Faac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
00181 const AVFrame *frame, int *got_packet_ptr)
00182 {
00183 FaacAudioContext *s = avctx->priv_data;
00184 int bytes_written, ret;
00185 int num_samples = frame ? frame->nb_samples : 0;
00186 void *samples = frame ? frame->data[0] : NULL;
00187
00188 if ((ret = ff_alloc_packet2(avctx, avpkt, (7 + 768) * avctx->channels))) {
00189 av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
00190 return ret;
00191 }
00192
00193 bytes_written = faacEncEncode(s->faac_handle, samples,
00194 num_samples * avctx->channels,
00195 avpkt->data, avpkt->size);
00196 if (bytes_written < 0) {
00197 av_log(avctx, AV_LOG_ERROR, "faacEncEncode() error\n");
00198 return bytes_written;
00199 }
00200
00201
00202 if (frame) {
00203 if ((ret = ff_af_queue_add(&s->afq, frame) < 0))
00204 return ret;
00205 }
00206
00207 if (!bytes_written)
00208 return 0;
00209
00210
00211 ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
00212 &avpkt->duration);
00213
00214 avpkt->size = bytes_written;
00215 *got_packet_ptr = 1;
00216 return 0;
00217 }
00218
00219 static const AVProfile profiles[] = {
00220 { FF_PROFILE_AAC_MAIN, "Main" },
00221 { FF_PROFILE_AAC_LOW, "LC" },
00222 { FF_PROFILE_AAC_SSR, "SSR" },
00223 { FF_PROFILE_AAC_LTP, "LTP" },
00224 { FF_PROFILE_UNKNOWN },
00225 };
00226
00227 static const uint64_t faac_channel_layouts[] = {
00228 AV_CH_LAYOUT_MONO,
00229 AV_CH_LAYOUT_STEREO,
00230 AV_CH_LAYOUT_SURROUND,
00231 AV_CH_LAYOUT_4POINT0,
00232 AV_CH_LAYOUT_5POINT0_BACK,
00233 AV_CH_LAYOUT_5POINT1_BACK,
00234 0
00235 };
00236
00237 AVCodec ff_libfaac_encoder = {
00238 .name = "libfaac",
00239 .type = AVMEDIA_TYPE_AUDIO,
00240 .id = CODEC_ID_AAC,
00241 .priv_data_size = sizeof(FaacAudioContext),
00242 .init = Faac_encode_init,
00243 .encode2 = Faac_encode_frame,
00244 .close = Faac_encode_close,
00245 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
00246 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
00247 AV_SAMPLE_FMT_NONE },
00248 .long_name = NULL_IF_CONFIG_SMALL("libfaac AAC (Advanced Audio Codec)"),
00249 .profiles = NULL_IF_CONFIG_SMALL(profiles),
00250 .channel_layouts = faac_channel_layouts,
00251 };