1 /*****************************************************************************
2 * aac.c: FDK-AAC Encoder plugin for vlc.
3 *****************************************************************************
4 * Copyright (C) 2012 Sergio Ammirata
6 * Authors: Sergio Ammirata <sergio@ammirata.net>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * Alternatively you can redistribute this file under the terms of the
23 * BSD license as stated below:
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
28 * 1. Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in
32 * the documentation and/or other materials provided with the
35 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
41 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
42 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
43 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 *****************************************************************************/
49 /*****************************************************************************
51 *****************************************************************************/
56 #include <fdk-aac/aacenc_lib.h>
58 #include <vlc_common.h>
59 #include <vlc_plugin.h>
60 #include <vlc_codec.h>
62 static int OpenEncoder(vlc_object_t
*);
63 static void CloseEncoder(vlc_object_t
*);
65 #define ENC_CFG_PREFIX "sout-fdkaac-"
67 #define AOT_TEXT N_("Encoder Profile")
68 #define AOT_LONGTEXT N_("Encoder Algorithm to use")
70 #define SIDEBAND_TEXT N_("Enable spectral band replication")
71 #define SIDEBAND_LONGTEXT N_("This is an optional feature only for the AAC-ELD profile")
73 #define VBR_QUALITY_TEXT N_("VBR Quality")
74 #define VBR_QUALITY_LONGTEXT N_("Quality of the VBR Encoding (0=cbr, 1-5 constant quality vbr, 5 is best")
76 #define AFTERBURNER_TEXT N_("Enable afterburner library")
77 #define AFTERBURNER_LONGTEXT N_("This library will produce higher quality audio at the expense of additional CPU usage (default is enabled)")
79 #define SIGNALING_TEXT N_("Signaling mode of the extension AOT")
80 #define SIGNALING_LONGTEXT N_("1 is explicit for SBR and implicit for PS (default), 2 is explicit hierarchical")
82 #define CH_ORDER_MPEG 0 /*!< MPEG channel ordering (e. g. 5.1: C, L, R, SL, SR, LFE) */
83 #define CH_ORDER_WAV 1 /*!< WAV fileformat channel ordering (e. g. 5.1: L, R, C, LFE, SL, SR) */
84 #define CH_ORDER_WG4 2 /*!< WG4 fileformat channel ordering (e. g. 5.1: L, R, SL, SR, C, LFE) */
86 #define PROFILE_AAC_LC 2
87 #define PROFILE_AAC_HE 5
88 #define PROFILE_AAC_HE_v2 29
89 #define PROFILE_AAC_LD 23
90 #define PROFILE_AAC_ELD 39
92 #define SIGNALING_COMPATIBLE 1
93 #define SIGNALING_HIERARCHICAL 2
95 static const int pi_aot_values
[] = { PROFILE_AAC_LC
, PROFILE_AAC_HE
, PROFILE_AAC_HE_v2
, PROFILE_AAC_LD
, PROFILE_AAC_ELD
};
96 static const char *const ppsz_aot_descriptions
[] =
97 { N_("AAC-LC"), N_("HE-AAC"), N_("HE-AAC-v2"), N_("AAC-LD"), N_("AAC-ELD") };
100 set_shortname(N_("FDKAAC"))
101 set_description(N_("FDK-AAC Audio encoder"))
102 set_capability("encoder", 150)
103 set_callbacks(OpenEncoder
, CloseEncoder
)
104 add_shortcut("fdkaac")
105 set_category(CAT_INPUT
)
106 set_subcategory(SUBCAT_INPUT_ACODEC
)
107 add_integer(ENC_CFG_PREFIX
"profile", PROFILE_AAC_LC
, AOT_TEXT
,
109 change_integer_list(pi_aot_values
, ppsz_aot_descriptions
);
110 add_bool(ENC_CFG_PREFIX
"sbr", false, SIDEBAND_TEXT
,
111 SIDEBAND_LONGTEXT
, false)
112 add_integer(ENC_CFG_PREFIX
"vbr", 0, VBR_QUALITY_TEXT
,
113 VBR_QUALITY_LONGTEXT
, false)
114 change_integer_range (0, 5)
115 add_bool(ENC_CFG_PREFIX
"afterburner", true, AFTERBURNER_TEXT
,
116 AFTERBURNER_LONGTEXT
, true)
117 add_integer(ENC_CFG_PREFIX
"signaling", SIGNALING_COMPATIBLE
, SIGNALING_TEXT
,
118 SIGNALING_LONGTEXT
, true)
119 change_integer_range (0, 2)
122 /*****************************************************************************
124 *****************************************************************************/
125 static block_t
*EncodeAudio(encoder_t
*p_enc
, block_t
*p_buf
);
127 static const char *const ppsz_enc_options
[] = {
128 "profile", "sbr", "vbr", "afterburner", "signaling", NULL
131 /*****************************************************************************
132 * encoder_sys_t : aac encoder descriptor
133 *****************************************************************************/
136 double d_compression_ratio
;
138 int i_encoderdelay
; /* Samples delay introduced by the profile */
140 int i_maxoutputsize
; /* Maximum buffer size for encoded output */
141 HANDLE_AACENCODER handle
;
144 static const char *aac_get_errorstring(AACENC_ERROR erraac
)
149 case AACENC_INVALID_HANDLE
:
150 return "Invalid handle";
151 case AACENC_MEMORY_ERROR
:
152 return "Memory allocation error";
153 case AACENC_UNSUPPORTED_PARAMETER
:
154 return "Unsupported parameter";
155 case AACENC_INVALID_CONFIG
:
156 return "Invalid config";
157 case AACENC_INIT_ERROR
:
158 return "Initialization error";
159 case AACENC_INIT_AAC_ERROR
:
160 return "AAC library initialization error";
161 case AACENC_INIT_SBR_ERROR
:
162 return "SBR library initialization error";
163 case AACENC_INIT_TP_ERROR
:
164 return "Transport library initialization error";
165 case AACENC_INIT_META_ERROR
:
166 return "Metadata library initialization error";
167 case AACENC_ENCODE_ERROR
:
168 return "Encoding error";
169 case AACENC_ENCODE_EOF
:
170 return "End of file";
172 return "Unknown error";
176 /*****************************************************************************
177 * OpenDecoder: open the encoder.
178 *****************************************************************************/
179 static int OpenEncoder(vlc_object_t
*p_this
)
181 encoder_t
*p_enc
= (encoder_t
*)p_this
;
183 config_ChainParse(p_enc
, ENC_CFG_PREFIX
, ppsz_enc_options
, p_enc
->p_cfg
);
186 switch (p_enc
->fmt_out
.i_codec
) {
188 i_aot
= var_InheritInteger(p_enc
, ENC_CFG_PREFIX
"profile");
190 case VLC_FOURCC('l', 'a', 'a', 'c'):
191 i_aot
= PROFILE_AAC_LC
;
193 case VLC_FOURCC('h', 'a', 'a', 'c'):
194 i_aot
= PROFILE_AAC_HE
;
196 case VLC_FOURCC('s', 'a', 'a', 'c'):
197 i_aot
= PROFILE_AAC_HE_v2
;
203 if (p_enc
->fmt_in
.audio
.i_channels
!= 2)
204 if (i_aot
== PROFILE_AAC_HE_v2
|| i_aot
== PROFILE_AAC_ELD
) {
205 msg_Err(p_enc
, "Selected profile %d can only be used with stereo", i_aot
);
209 uint16_t channel_config
;
211 switch (p_enc
->fmt_in
.audio
.i_channels
) {
212 case 1: mode
= MODE_1
; channel_config
= AOUT_CHAN_CENTER
; break;
213 case 2: mode
= MODE_2
; channel_config
= AOUT_CHANS_STEREO
; break;
214 case 3: mode
= MODE_1_2
; channel_config
= AOUT_CHANS_3_0
; break;
215 case 4: mode
= MODE_1_2_1
; channel_config
= AOUT_CHANS_4_CENTER_REAR
; break;
216 case 5: mode
= MODE_1_2_2
; channel_config
= AOUT_CHANS_5_0
; break;
217 case 6: mode
= MODE_1_2_2_1
; channel_config
= AOUT_CHANS_5_1
; break;
218 case 8: mode
= MODE_1_2_2_2_1
; channel_config
= AOUT_CHANS_7_1
; break;
220 msg_Err(p_enc
, "we do not support > 8 input channels, this input has %i",
221 p_enc
->fmt_in
.audio
.i_channels
);
225 p_enc
->fmt_in
.audio
.i_physical_channels
= channel_config
;
227 msg_Info(p_enc
, "Initializing AAC Encoder, %i channels", p_enc
->fmt_in
.audio
.i_channels
);
229 /* Allocate the memory needed to store the encoder's structure */
230 encoder_sys_t
*p_sys
= (encoder_sys_t
*)malloc(sizeof(encoder_sys_t
));
231 if (unlikely(!p_sys
))
233 p_enc
->p_sys
= p_sys
;
234 p_enc
->fmt_in
.i_codec
= VLC_CODEC_S16N
;
235 p_enc
->fmt_out
.i_cat
= AUDIO_ES
;
236 p_enc
->fmt_out
.i_codec
= VLC_CODEC_MP4A
;
238 p_sys
->i_pts_last
= 0;
241 erraac
= aacEncOpen(&p_sys
->handle
, 0, p_enc
->fmt_in
.audio
.i_channels
);
242 if (erraac
!= AACENC_OK
) {
243 msg_Err(p_enc
, "Unable to open encoder: %s", aac_get_errorstring(erraac
));
248 #define SET_PARAM(P, V) do { \
249 AACENC_ERROR err = aacEncoder_SetParam(p_sys->handle, AACENC_ ## P, V); \
250 if (err != AACENC_OK) { \
251 msg_Err(p_enc, "Couldn't set " #P " to value %d: %s", V, aac_get_errorstring(err)); \
256 SET_PARAM(AOT
, i_aot
);
257 bool b_eld_sbr
= var_InheritBool(p_enc
, ENC_CFG_PREFIX
"sbr");
258 if (i_aot
== PROFILE_AAC_ELD
&& b_eld_sbr
)
259 SET_PARAM(SBR_MODE
, 1);
260 SET_PARAM(SAMPLERATE
, p_enc
->fmt_out
.audio
.i_rate
);
261 SET_PARAM(CHANNELMODE
, mode
);
262 SET_PARAM(CHANNELORDER
, CH_ORDER_WG4
);
264 int i_vbr
= var_InheritInteger(p_enc
, ENC_CFG_PREFIX
"vbr");
266 if ((i_aot
== PROFILE_AAC_HE
|| i_aot
== PROFILE_AAC_HE_v2
) && i_vbr
> 3) {
267 msg_Warn(p_enc
, "Maximum VBR quality for this profile is 3, setting vbr=3");
270 SET_PARAM(BITRATEMODE
, i_vbr
);
272 int i_bitrate
= p_enc
->fmt_out
.i_bitrate
;
273 if (i_bitrate
== 0) {
274 i_bitrate
= 96 * p_enc
->fmt_in
.audio
.i_channels
* p_enc
->fmt_out
.audio
.i_rate
/ 44;
275 if (i_aot
== PROFILE_AAC_HE
|| i_aot
== PROFILE_AAC_HE_v2
|| b_eld_sbr
)
277 p_enc
->fmt_out
.i_bitrate
= i_bitrate
;
278 msg_Info(p_enc
, "Setting optimal bitrate of %i", i_bitrate
);
280 SET_PARAM(BITRATE
, i_bitrate
);
282 SET_PARAM(TRANSMUX
, 0);
283 SET_PARAM(SIGNALING_MODE
, (int)var_InheritInteger(p_enc
, ENC_CFG_PREFIX
"signaling"));
284 SET_PARAM(AFTERBURNER
, !!var_InheritBool(p_enc
, ENC_CFG_PREFIX
"afterburner"));
287 erraac
= aacEncEncode(p_sys
->handle
, NULL
, NULL
, NULL
, NULL
);
288 if (erraac
!= AACENC_OK
) {
289 msg_Err(p_enc
, "Unable to initialize the encoder: %s", aac_get_errorstring(erraac
));
293 AACENC_InfoStruct info
= { 0 };
294 erraac
= aacEncInfo(p_sys
->handle
, &info
);
295 if (erraac
!= AACENC_OK
) {
296 msg_Err(p_enc
, "Unable to get the encoder info: %s", aac_get_errorstring(erraac
));
300 /* The maximum packet size is 6144 bits aka 768 bytes per channel. */
301 p_sys
->i_maxoutputsize
= 768*p_enc
->fmt_in
.audio
.i_channels
;
302 p_enc
->fmt_in
.audio
.i_bitspersample
= 16;
303 p_sys
->i_frame_size
= info
.frameLength
;
304 p_sys
->i_encoderdelay
= info
.encoderDelay
;
306 p_enc
->fmt_out
.i_extra
= info
.confSize
;
307 if (p_enc
->fmt_out
.i_extra
) {
308 p_enc
->fmt_out
.p_extra
= malloc(p_enc
->fmt_out
.i_extra
);
309 if (p_enc
->fmt_out
.p_extra
== NULL
) {
310 msg_Err(p_enc
, "Unable to allocate fmt_out.p_extra");
313 memcpy(p_enc
->fmt_out
.p_extra
, info
.confBuf
, p_enc
->fmt_out
.i_extra
);
316 p_enc
->pf_encode_audio
= EncodeAudio
;
319 // TODO: Add more debug info to this config printout
320 msg_Dbg(p_enc
, "fmt_out.p_extra = %i", p_enc
->fmt_out
.i_extra
);
326 CloseEncoder(p_this
);
330 /****************************************************************************
331 * EncodeAudio: the whole thing
332 ****************************************************************************/
333 static block_t
*EncodeAudio(encoder_t
*p_enc
, block_t
*p_aout_buf
)
339 encoder_sys_t
*p_sys
= p_enc
->p_sys
;
341 if (likely(p_aout_buf
)) {
342 p_buffer
= (int16_t *)p_aout_buf
->p_buffer
;
343 i_samples
= p_aout_buf
->i_nb_samples
;
344 i_pts_out
= p_aout_buf
->i_pts
- (mtime_t
)((double)CLOCK_FREQ
*
345 (double)p_sys
->i_encoderdelay
/
346 (double)p_enc
->fmt_out
.audio
.i_rate
);
347 if (p_sys
->i_pts_last
== 0)
348 p_sys
->i_pts_last
= i_pts_out
- (mtime_t
)((double)CLOCK_FREQ
*
349 (double)(p_sys
->i_frame_size
) /
350 (double)p_enc
->fmt_out
.audio
.i_rate
);
353 i_pts_out
= p_sys
->i_pts_last
;
356 int i_samples_left
= i_samples
;
357 int i_loop_count
= 0;
359 block_t
*p_chain
= NULL
;
360 while (i_samples_left
>= 0) {
361 AACENC_BufDesc in_buf
= { 0 }, out_buf
= { 0 };
362 AACENC_InArgs in_args
= { 0 };
363 AACENC_OutArgs out_args
= { 0 };
364 int in_identifier
= IN_AUDIO_DATA
;
365 int in_size
, in_elem_size
;
366 int out_identifier
= OUT_BITSTREAM_DATA
;
367 int out_size
, out_elem_size
;
368 void *in_ptr
, *out_ptr
;
370 if (unlikely(i_samples
== 0)) {
371 // this forces the encoder to purge whatever is left in the internal buffer
372 in_args
.numInSamples
= -1;
374 in_ptr
= p_buffer
+ (i_samples
- i_samples_left
)*p_enc
->fmt_in
.audio
.i_channels
;
375 in_size
= 2*p_enc
->fmt_in
.audio
.i_channels
*i_samples_left
;
377 in_args
.numInSamples
= p_enc
->fmt_in
.audio
.i_channels
*i_samples_left
;
379 in_buf
.bufs
= &in_ptr
;
380 in_buf
.bufferIdentifiers
= &in_identifier
;
381 in_buf
.bufSizes
= &in_size
;
382 in_buf
.bufElSizes
= &in_elem_size
;
385 p_block
= block_Alloc(p_sys
->i_maxoutputsize
);
386 p_block
->i_buffer
= p_sys
->i_maxoutputsize
;
387 out_ptr
= p_block
->p_buffer
;
388 out_size
= p_block
->i_buffer
;
391 out_buf
.bufs
= &out_ptr
;
392 out_buf
.bufferIdentifiers
= &out_identifier
;
393 out_buf
.bufSizes
= &out_size
;
394 out_buf
.bufElSizes
= &out_elem_size
;
397 if ((erraac
= aacEncEncode(p_sys
->handle
, &in_buf
, &out_buf
, &in_args
, &out_args
)) != AACENC_OK
) {
398 if (erraac
== AACENC_ENCODE_EOF
) {
399 msg_Info(p_enc
, "Encoding final bytes (EOF)");
401 msg_Err(p_enc
, "Encoding failed: %s", aac_get_errorstring(erraac
));
402 block_Release(p_block
);
406 if (out_args
.numOutBytes
> 0) {
407 p_block
->i_buffer
= out_args
.numOutBytes
;
408 if (unlikely(i_samples
== 0)) {
409 // I only have the numOutBytes so approximate based on compression factor
410 double d_samples_forward
= p_sys
->d_compression_ratio
*(double)out_args
.numOutBytes
;
411 i_pts_out
+= (mtime_t
)d_samples_forward
;
412 p_block
->i_length
= (mtime_t
)d_samples_forward
;
413 // TODO: It would be more precise (a few microseconds) to use d_samples_forward =
414 // (mtime_t)CLOCK_FREQ * (mtime_t)p_sys->i_frame_size/(mtime_t)p_enc->fmt_out.audio.i_rate
415 // but I am not sure if the lib always outputs a full frame when
416 // emptying the internal buffer in the EOF scenario
418 if (i_loop_count
== 0) {
419 // There can be an implicit delay in the first loop cycle because leftover bytes
420 // in the library buffer from the prior block
421 double d_samples_delay
= (double)p_sys
->i_frame_size
- (double)out_args
.numInSamples
/
422 (double)p_enc
->fmt_in
.audio
.i_channels
;
423 i_pts_out
-= (mtime_t
)((double)CLOCK_FREQ
* d_samples_delay
/
424 (double)p_enc
->fmt_out
.audio
.i_rate
);
425 p_block
->i_length
= (mtime_t
)((double)CLOCK_FREQ
* (double)p_sys
->i_frame_size
/
426 (double)p_enc
->fmt_out
.audio
.i_rate
);
427 p_block
->i_nb_samples
= d_samples_delay
;
428 //p_block->i_length = i_pts_out - p_sys->i_pts_last;
430 double d_samples_forward
= (double)out_args
.numInSamples
/(double)p_enc
->fmt_in
.audio
.i_channels
;
431 double d_length
= ((double)CLOCK_FREQ
* d_samples_forward
/
432 (double)p_enc
->fmt_out
.audio
.i_rate
);
433 i_pts_out
+= (mtime_t
) d_length
;
434 p_block
->i_length
= (mtime_t
) d_length
;
435 p_block
->i_nb_samples
= d_samples_forward
;
438 p_block
->i_dts
= p_block
->i_pts
= i_pts_out
;
439 block_ChainAppend(&p_chain
, p_block
);
441 msg_Dbg(p_enc
, "dts %"PRId64
", length %"PRId64
", " "pts_last "
442 "%"PRId64
" numOutBytes = %i, numInSamples = %i, "
443 "i_samples %i, i_loop_count %i",
444 p_block
->i_dts
, p_block
->i_length
,
445 p_sys
->i_pts_last
, out_args
.numOutBytes
,
446 out_args
.numInSamples
, i_samples
, i_loop_count
);
448 if (likely(i_samples
> 0)) {
449 p_sys
->d_compression_ratio
= (double)p_block
->i_length
/ (double)out_args
.numOutBytes
;
450 i_samples_left
-= out_args
.numInSamples
/p_enc
->fmt_in
.audio
.i_channels
;
451 p_sys
->i_pts_last
= i_pts_out
;
454 block_Release(p_block
);
455 //msg_Dbg(p_enc, "aac_encode_audio: not enough data yet");
458 if (unlikely(i_loop_count
++ > 100)) {
459 msg_Err(p_enc
, "Loop count greater than 100!!!, something must be wrong with the encoder library");
467 /*****************************************************************************
468 * CloseDecoder: decoder destruction
469 *****************************************************************************/
470 static void CloseEncoder(vlc_object_t
*p_this
)
472 encoder_t
*p_enc
= (encoder_t
*)p_this
;
473 encoder_sys_t
*p_sys
= p_enc
->p_sys
;
475 aacEncClose(&p_sys
->handle
);