00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <fdk-aac/aacenc_lib.h>
00023
00024 #include "avcodec.h"
00025 #include "audio_frame_queue.h"
00026 #include "internal.h"
00027 #include "libavutil/audioconvert.h"
00028 #include "libavutil/common.h"
00029 #include "libavutil/opt.h"
00030
00031 typedef struct AACContext {
00032 const AVClass *class;
00033 HANDLE_AACENCODER handle;
00034 int afterburner;
00035 int eld_sbr;
00036 int signaling;
00037 int latm;
00038 int header_period;
00039 int vbr;
00040
00041 AudioFrameQueue afq;
00042 } AACContext;
00043
00044 static const AVOption aac_enc_options[] = {
00045 { "afterburner", "Afterburner (improved quality)", offsetof(AACContext, afterburner), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
00046 { "eld_sbr", "Enable SBR for ELD (for SBR in other configurations, use the -profile parameter)", offsetof(AACContext, eld_sbr), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
00047 { "signaling", "SBR/PS signaling style", offsetof(AACContext, signaling), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 2, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, "signaling" },
00048 { "default", "Choose signaling implicitly (explicit hierarchical by default, implicit if global header is disabled)", 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, 0, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, "signaling" },
00049 { "implicit", "Implicit backwards compatible signaling", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, "signaling" },
00050 { "explicit_sbr", "Explicit SBR, implicit PS signaling", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, "signaling" },
00051 { "explicit_hierarchical", "Explicit hierarchical signaling", 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, 0, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, "signaling" },
00052 { "latm", "Output LATM/LOAS encapsulated data", offsetof(AACContext, latm), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
00053 { "header_period", "StreamMuxConfig and PCE repetition period (in frames)", offsetof(AACContext, header_period), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 0xffff, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
00054 { "vbr", "VBR mode (1-5)", offsetof(AACContext, vbr), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 5, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
00055 { NULL }
00056 };
00057
00058 static const AVClass aac_enc_class = {
00059 "libfdk_aac", av_default_item_name, aac_enc_options, LIBAVUTIL_VERSION_INT
00060 };
00061
00062 static const char *aac_get_error(AACENC_ERROR err)
00063 {
00064 switch (err) {
00065 case AACENC_OK:
00066 return "No error";
00067 case AACENC_INVALID_HANDLE:
00068 return "Invalid handle";
00069 case AACENC_MEMORY_ERROR:
00070 return "Memory allocation error";
00071 case AACENC_UNSUPPORTED_PARAMETER:
00072 return "Unsupported parameter";
00073 case AACENC_INVALID_CONFIG:
00074 return "Invalid config";
00075 case AACENC_INIT_ERROR:
00076 return "Initialization error";
00077 case AACENC_INIT_AAC_ERROR:
00078 return "AAC library initialization error";
00079 case AACENC_INIT_SBR_ERROR:
00080 return "SBR library initialization error";
00081 case AACENC_INIT_TP_ERROR:
00082 return "Transport library initialization error";
00083 case AACENC_INIT_META_ERROR:
00084 return "Metadata library initialization error";
00085 case AACENC_ENCODE_ERROR:
00086 return "Encoding error";
00087 case AACENC_ENCODE_EOF:
00088 return "End of file";
00089 default:
00090 return "Unknown error";
00091 }
00092 }
00093
00094 static int aac_encode_close(AVCodecContext *avctx)
00095 {
00096 AACContext *s = avctx->priv_data;
00097
00098 if (s->handle)
00099 aacEncClose(&s->handle);
00100 #if FF_API_OLD_ENCODE_AUDIO
00101 av_freep(&avctx->coded_frame);
00102 #endif
00103 av_freep(&avctx->extradata);
00104 ff_af_queue_close(&s->afq);
00105
00106 return 0;
00107 }
00108
00109 static av_cold int aac_encode_init(AVCodecContext *avctx)
00110 {
00111 AACContext *s = avctx->priv_data;
00112 int ret = AVERROR(EINVAL);
00113 AACENC_InfoStruct info = { 0 };
00114 CHANNEL_MODE mode;
00115 AACENC_ERROR err;
00116 int aot = FF_PROFILE_AAC_LOW + 1;
00117 int sce = 0, cpe = 0;
00118
00119 if ((err = aacEncOpen(&s->handle, 0, avctx->channels)) != AACENC_OK) {
00120 av_log(avctx, AV_LOG_ERROR, "Unable to open the encoder: %s\n",
00121 aac_get_error(err));
00122 goto error;
00123 }
00124
00125 if (avctx->profile != FF_PROFILE_UNKNOWN)
00126 aot = avctx->profile + 1;
00127
00128 if ((err = aacEncoder_SetParam(s->handle, AACENC_AOT, aot)) != AACENC_OK) {
00129 av_log(avctx, AV_LOG_ERROR, "Unable to set the AOT %d: %s\n",
00130 aot, aac_get_error(err));
00131 goto error;
00132 }
00133
00134 if (aot == FF_PROFILE_AAC_ELD + 1 && s->eld_sbr) {
00135 if ((err = aacEncoder_SetParam(s->handle, AACENC_SBR_MODE,
00136 1)) != AACENC_OK) {
00137 av_log(avctx, AV_LOG_ERROR, "Unable to enable SBR for ELD: %s\n",
00138 aac_get_error(err));
00139 goto error;
00140 }
00141 }
00142
00143 if ((err = aacEncoder_SetParam(s->handle, AACENC_SAMPLERATE,
00144 avctx->sample_rate)) != AACENC_OK) {
00145 av_log(avctx, AV_LOG_ERROR, "Unable to set the sample rate %d: %s\n",
00146 avctx->sample_rate, aac_get_error(err));
00147 goto error;
00148 }
00149
00150 switch (avctx->channels) {
00151 case 1: mode = MODE_1; sce = 1; cpe = 0; break;
00152 case 2: mode = MODE_2; sce = 0; cpe = 1; break;
00153 case 3: mode = MODE_1_2; sce = 1; cpe = 1; break;
00154 case 4: mode = MODE_1_2_1; sce = 2; cpe = 1; break;
00155 case 5: mode = MODE_1_2_2; sce = 1; cpe = 2; break;
00156 case 6: mode = MODE_1_2_2_1; sce = 2; cpe = 2; break;
00157 default:
00158 av_log(avctx, AV_LOG_ERROR,
00159 "Unsupported number of channels %d\n", avctx->channels);
00160 goto error;
00161 }
00162
00163 if ((err = aacEncoder_SetParam(s->handle, AACENC_CHANNELMODE,
00164 mode)) != AACENC_OK) {
00165 av_log(avctx, AV_LOG_ERROR,
00166 "Unable to set channel mode %d: %s\n", mode, aac_get_error(err));
00167 goto error;
00168 }
00169
00170 if ((err = aacEncoder_SetParam(s->handle, AACENC_CHANNELORDER,
00171 1)) != AACENC_OK) {
00172 av_log(avctx, AV_LOG_ERROR,
00173 "Unable to set wav channel order %d: %s\n",
00174 mode, aac_get_error(err));
00175 goto error;
00176 }
00177
00178 if (avctx->flags & CODEC_FLAG_QSCALE || s->vbr) {
00179 int mode = s->vbr ? s->vbr : avctx->global_quality;
00180 if (mode < 1 || mode > 5) {
00181 av_log(avctx, AV_LOG_WARNING,
00182 "VBR quality %d out of range, should be 1-5\n", mode);
00183 mode = av_clip(mode, 1, 5);
00184 }
00185 av_log(avctx, AV_LOG_WARNING,
00186 "Note, the VBR setting is unsupported and only works with "
00187 "some parameter combinations\n");
00188 if ((err = aacEncoder_SetParam(s->handle, AACENC_BITRATEMODE,
00189 mode)) != AACENC_OK) {
00190 av_log(avctx, AV_LOG_ERROR, "Unable to set the VBR bitrate mode %d: %s\n",
00191 mode, aac_get_error(err));
00192 goto error;
00193 }
00194 } else {
00195 if (avctx->bit_rate <= 0) {
00196 if (avctx->profile == FF_PROFILE_AAC_HE_V2) {
00197 sce = 1;
00198 cpe = 0;
00199 }
00200 avctx->bit_rate = (96*sce + 128*cpe) * avctx->sample_rate / 44;
00201 if (avctx->profile == FF_PROFILE_AAC_HE ||
00202 avctx->profile == FF_PROFILE_AAC_HE_V2 ||
00203 s->eld_sbr)
00204 avctx->bit_rate /= 2;
00205 }
00206 if ((err = aacEncoder_SetParam(s->handle, AACENC_BITRATE,
00207 avctx->bit_rate)) != AACENC_OK) {
00208 av_log(avctx, AV_LOG_ERROR, "Unable to set the bitrate %d: %s\n",
00209 avctx->bit_rate, aac_get_error(err));
00210 goto error;
00211 }
00212 }
00213
00214
00215
00216 if ((err = aacEncoder_SetParam(s->handle, AACENC_TRANSMUX,
00217 avctx->flags & CODEC_FLAG_GLOBAL_HEADER ? 0 : s->latm ? 10 : 2)) != AACENC_OK) {
00218 av_log(avctx, AV_LOG_ERROR, "Unable to set the transmux format: %s\n",
00219 aac_get_error(err));
00220 goto error;
00221 }
00222
00223 if (s->latm && s->header_period) {
00224 if ((err = aacEncoder_SetParam(s->handle, AACENC_HEADER_PERIOD,
00225 s->header_period)) != AACENC_OK) {
00226 av_log(avctx, AV_LOG_ERROR, "Unable to set header period: %s\n",
00227 aac_get_error(err));
00228 goto error;
00229 }
00230 }
00231
00232
00233
00234
00235 if (s->signaling < 0)
00236 s->signaling = avctx->flags & CODEC_FLAG_GLOBAL_HEADER ? 2 : 0;
00237
00238 if ((err = aacEncoder_SetParam(s->handle, AACENC_SIGNALING_MODE,
00239 s->signaling)) != AACENC_OK) {
00240 av_log(avctx, AV_LOG_ERROR, "Unable to set signaling mode %d: %s\n",
00241 s->signaling, aac_get_error(err));
00242 goto error;
00243 }
00244
00245 if ((err = aacEncoder_SetParam(s->handle, AACENC_AFTERBURNER,
00246 s->afterburner)) != AACENC_OK) {
00247 av_log(avctx, AV_LOG_ERROR, "Unable to set afterburner to %d: %s\n",
00248 s->afterburner, aac_get_error(err));
00249 goto error;
00250 }
00251
00252 if (avctx->cutoff > 0) {
00253 if (avctx->cutoff < (avctx->sample_rate + 255) >> 8) {
00254 av_log(avctx, AV_LOG_ERROR, "cutoff valid range is %d-20000\n",
00255 (avctx->sample_rate + 255) >> 8);
00256 goto error;
00257 }
00258 if ((err = aacEncoder_SetParam(s->handle, AACENC_BANDWIDTH,
00259 avctx->cutoff)) != AACENC_OK) {
00260 av_log(avctx, AV_LOG_ERROR, "Unable to set the encoder bandwith to %d: %s\n",
00261 avctx->cutoff, aac_get_error(err));
00262 goto error;
00263 }
00264 }
00265
00266 if ((err = aacEncEncode(s->handle, NULL, NULL, NULL, NULL)) != AACENC_OK) {
00267 av_log(avctx, AV_LOG_ERROR, "Unable to initialize the encoder: %s\n",
00268 aac_get_error(err));
00269 return AVERROR(EINVAL);
00270 }
00271
00272 if ((err = aacEncInfo(s->handle, &info)) != AACENC_OK) {
00273 av_log(avctx, AV_LOG_ERROR, "Unable to get encoder info: %s\n",
00274 aac_get_error(err));
00275 goto error;
00276 }
00277
00278 #if FF_API_OLD_ENCODE_AUDIO
00279 avctx->coded_frame = avcodec_alloc_frame();
00280 if (!avctx->coded_frame) {
00281 ret = AVERROR(ENOMEM);
00282 goto error;
00283 }
00284 #endif
00285 avctx->frame_size = info.frameLength;
00286 avctx->delay = info.encoderDelay;
00287 ff_af_queue_init(avctx, &s->afq);
00288
00289 if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
00290 avctx->extradata_size = info.confSize;
00291 avctx->extradata = av_mallocz(avctx->extradata_size +
00292 FF_INPUT_BUFFER_PADDING_SIZE);
00293 if (!avctx->extradata) {
00294 ret = AVERROR(ENOMEM);
00295 goto error;
00296 }
00297
00298 memcpy(avctx->extradata, info.confBuf, info.confSize);
00299 }
00300 return 0;
00301 error:
00302 aac_encode_close(avctx);
00303 return ret;
00304 }
00305
00306 static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
00307 const AVFrame *frame, int *got_packet_ptr)
00308 {
00309 AACContext *s = avctx->priv_data;
00310 AACENC_BufDesc in_buf = { 0 }, out_buf = { 0 };
00311 AACENC_InArgs in_args = { 0 };
00312 AACENC_OutArgs out_args = { 0 };
00313 int in_buffer_identifier = IN_AUDIO_DATA;
00314 int in_buffer_size, in_buffer_element_size;
00315 int out_buffer_identifier = OUT_BITSTREAM_DATA;
00316 int out_buffer_size, out_buffer_element_size;
00317 void *in_ptr, *out_ptr;
00318 int ret;
00319 AACENC_ERROR err;
00320
00321
00322 if (!frame) {
00323 in_args.numInSamples = -1;
00324 } else {
00325 in_ptr = frame->data[0];
00326 in_buffer_size = 2 * avctx->channels * frame->nb_samples;
00327 in_buffer_element_size = 2;
00328
00329 in_args.numInSamples = avctx->channels * frame->nb_samples;
00330 in_buf.numBufs = 1;
00331 in_buf.bufs = &in_ptr;
00332 in_buf.bufferIdentifiers = &in_buffer_identifier;
00333 in_buf.bufSizes = &in_buffer_size;
00334 in_buf.bufElSizes = &in_buffer_element_size;
00335
00336
00337 if ((ret = ff_af_queue_add(&s->afq, frame) < 0))
00338 return ret;
00339 }
00340
00341
00342 if ((ret = ff_alloc_packet2(avctx, avpkt, FFMAX(8192, 768 * avctx->channels))))
00343 return ret;
00344
00345 out_ptr = avpkt->data;
00346 out_buffer_size = avpkt->size;
00347 out_buffer_element_size = 1;
00348 out_buf.numBufs = 1;
00349 out_buf.bufs = &out_ptr;
00350 out_buf.bufferIdentifiers = &out_buffer_identifier;
00351 out_buf.bufSizes = &out_buffer_size;
00352 out_buf.bufElSizes = &out_buffer_element_size;
00353
00354 if ((err = aacEncEncode(s->handle, &in_buf, &out_buf, &in_args,
00355 &out_args)) != AACENC_OK) {
00356 if (!frame && err == AACENC_ENCODE_EOF)
00357 return 0;
00358 av_log(avctx, AV_LOG_ERROR, "Unable to encode frame: %s\n",
00359 aac_get_error(err));
00360 return AVERROR(EINVAL);
00361 }
00362
00363 if (!out_args.numOutBytes)
00364 return 0;
00365
00366
00367 ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
00368 &avpkt->duration);
00369
00370 avpkt->size = out_args.numOutBytes;
00371 *got_packet_ptr = 1;
00372 return 0;
00373 }
00374
00375 static const AVProfile profiles[] = {
00376 { FF_PROFILE_AAC_LOW, "LC" },
00377 { FF_PROFILE_AAC_HE, "HE-AAC" },
00378 { FF_PROFILE_AAC_HE_V2, "HE-AACv2" },
00379 { FF_PROFILE_AAC_LD, "LD" },
00380 { FF_PROFILE_AAC_ELD, "ELD" },
00381 { FF_PROFILE_UNKNOWN },
00382 };
00383
00384 static const AVCodecDefault aac_encode_defaults[] = {
00385 { "b", "0" },
00386 { NULL }
00387 };
00388
00389 static const uint64_t aac_channel_layout[] = {
00390 AV_CH_LAYOUT_MONO,
00391 AV_CH_LAYOUT_STEREO,
00392 AV_CH_LAYOUT_SURROUND,
00393 AV_CH_LAYOUT_4POINT0,
00394 AV_CH_LAYOUT_5POINT0_BACK,
00395 AV_CH_LAYOUT_5POINT1_BACK,
00396 0,
00397 };
00398
00399 static const int aac_sample_rates[] = {
00400 96000, 88200, 64000, 48000, 44100, 32000,
00401 24000, 22050, 16000, 12000, 11025, 8000, 0
00402 };
00403
00404 AVCodec ff_libfdk_aac_encoder = {
00405 .name = "libfdk_aac",
00406 .type = AVMEDIA_TYPE_AUDIO,
00407 .id = AV_CODEC_ID_AAC,
00408 .priv_data_size = sizeof(AACContext),
00409 .init = aac_encode_init,
00410 .encode2 = aac_encode_frame,
00411 .close = aac_encode_close,
00412 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
00413 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
00414 AV_SAMPLE_FMT_NONE },
00415 .long_name = NULL_IF_CONFIG_SMALL("Fraunhofer FDK AAC"),
00416 .priv_class = &aac_enc_class,
00417 .defaults = aac_encode_defaults,
00418 .profiles = profiles,
00419 .supported_samplerates = aac_sample_rates,
00420 .channel_layouts = aac_channel_layout,
00421 };