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 #include "libavutil/common.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 #undef free
00162 free(buffer);
00163 #define free please_use_av_free
00164 }
00165
00166 if (!faacEncSetConfiguration(s->faac_handle, faac_cfg)) {
00167 av_log(avctx, AV_LOG_ERROR, "libfaac doesn't support this output format!\n");
00168 ret = AVERROR(EINVAL);
00169 goto error;
00170 }
00171
00172 avctx->delay = FAAC_DELAY_SAMPLES;
00173 ff_af_queue_init(avctx, &s->afq);
00174
00175 return 0;
00176 error:
00177 Faac_encode_close(avctx);
00178 return ret;
00179 }
00180
00181 static int Faac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
00182 const AVFrame *frame, int *got_packet_ptr)
00183 {
00184 FaacAudioContext *s = avctx->priv_data;
00185 int bytes_written, ret;
00186 int num_samples = frame ? frame->nb_samples : 0;
00187 void *samples = frame ? frame->data[0] : NULL;
00188
00189 if ((ret = ff_alloc_packet2(avctx, avpkt, (7 + 768) * avctx->channels))) {
00190 av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
00191 return ret;
00192 }
00193
00194 bytes_written = faacEncEncode(s->faac_handle, samples,
00195 num_samples * avctx->channels,
00196 avpkt->data, avpkt->size);
00197 if (bytes_written < 0) {
00198 av_log(avctx, AV_LOG_ERROR, "faacEncEncode() error\n");
00199 return bytes_written;
00200 }
00201
00202
00203 if (frame) {
00204 if ((ret = ff_af_queue_add(&s->afq, frame) < 0))
00205 return ret;
00206 }
00207
00208 if (!bytes_written)
00209 return 0;
00210
00211
00212 ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
00213 &avpkt->duration);
00214
00215 avpkt->size = bytes_written;
00216 *got_packet_ptr = 1;
00217 return 0;
00218 }
00219
00220 static const AVProfile profiles[] = {
00221 { FF_PROFILE_AAC_MAIN, "Main" },
00222 { FF_PROFILE_AAC_LOW, "LC" },
00223 { FF_PROFILE_AAC_SSR, "SSR" },
00224 { FF_PROFILE_AAC_LTP, "LTP" },
00225 { FF_PROFILE_UNKNOWN },
00226 };
00227
00228 static const uint64_t faac_channel_layouts[] = {
00229 AV_CH_LAYOUT_MONO,
00230 AV_CH_LAYOUT_STEREO,
00231 AV_CH_LAYOUT_SURROUND,
00232 AV_CH_LAYOUT_4POINT0,
00233 AV_CH_LAYOUT_5POINT0_BACK,
00234 AV_CH_LAYOUT_5POINT1_BACK,
00235 0
00236 };
00237
00238 AVCodec ff_libfaac_encoder = {
00239 .name = "libfaac",
00240 .type = AVMEDIA_TYPE_AUDIO,
00241 .id = AV_CODEC_ID_AAC,
00242 .priv_data_size = sizeof(FaacAudioContext),
00243 .init = Faac_encode_init,
00244 .encode2 = Faac_encode_frame,
00245 .close = Faac_encode_close,
00246 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
00247 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
00248 AV_SAMPLE_FMT_NONE },
00249 .long_name = NULL_IF_CONFIG_SMALL("libfaac AAC (Advanced Audio Coding)"),
00250 .profiles = NULL_IF_CONFIG_SMALL(profiles),
00251 .channel_layouts = faac_channel_layouts,
00252 };