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