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
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
30 #include <fdk-aac/aacenc_lib.h>
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
36 static int OpenEncoder( vlc_object_t
* );
37 static void CloseEncoder( vlc_object_t
* );
39 #define ENC_CFG_PREFIX "sout-fdkaac-"
41 #define AOT_TEXT N_("Encoder Profile")
42 #define AOT_LONGTEXT N_( "Encoder Algorithm to use" )
44 #define SIDEBAND_TEXT N_("Enable spectral band replication")
45 #define SIDEBAND_LONGTEXT N_( "This is an optional feature only for the AAC-ELD profile" )
47 #define VBR_QUALITY_TEXT N_("VBR Quality")
48 #define VBR_QUALITY_LONGTEXT N_( "Quality of the VBR Encoding (0=cbr, 1-5 constant quality vbr, 5 is best" )
50 #define AFTERBURNER_TEXT N_("Enable afterburner library")
51 #define AFTERBURNER_LONGTEXT N_( "This library will produce higher quality audio at the expense of additional CPU usage (default is enabled)" )
53 #define BITRATE_TEXT N_("CBR Bitrate")
54 #define BITRATE_LONGTEXT N_( "Bitrate of desired stream (in bps)" )
56 #define SIGNALING_TEXT N_("Signaling mode of the extension AOT")
57 #define SIGNALING_LONGTEXT N_( "1 is explicit for SBR and implicit for PS (default), 2 is explicit hierarchical" )
59 #define CH_ORDER_MPEG 0 /*!< MPEG channel ordering (e. g. 5.1: C, L, R, SL, SR, LFE) */
60 #define CH_ORDER_WAV 1 /*!< WAV fileformat channel ordering (e. g. 5.1: L, R, C, LFE, SL, SR) */
61 #define CH_ORDER_WG4 2 /*!< WG4 fileformat channel ordering (e. g. 5.1: L, R, SL, SR, C, LFE) */
63 #define PROFILE_AAC_LC 2
64 #define PROFILE_AAC_HE 5
65 #define PROFILE_AAC_HE_v2 29
66 #define PROFILE_AAC_LD 23
67 #define PROFILE_AAC_ELD 39
69 #define SIGNALING_COMPATIBLE 1
70 #define SIGNALING_HIERARCHICAL 2
72 static const int pi_aot_values
[] = { PROFILE_AAC_LC
, PROFILE_AAC_HE
, PROFILE_AAC_HE_v2
, PROFILE_AAC_LD
, PROFILE_AAC_ELD
};
73 static const char *const ppsz_aot_descriptions
[] =
74 { N_("AAC-LC"), N_("HE-AAC"), N_("HE-AAC-v2"), N_("AAC-LD"), N_("AAC-ELD") };
77 set_shortname( N_("FDKAAC") )
78 set_description( N_("FDK-AAC Audio encoder") )
79 set_capability( "encoder", 50 )
80 set_callbacks( OpenEncoder
, CloseEncoder
)
81 add_shortcut( "fdkaac" )
82 set_category( CAT_INPUT
)
83 set_subcategory( SUBCAT_INPUT_ACODEC
)
84 add_integer( ENC_CFG_PREFIX
"profile", PROFILE_AAC_LC
, AOT_TEXT
,
86 change_integer_list( pi_aot_values
, ppsz_aot_descriptions
);
87 add_bool( ENC_CFG_PREFIX
"sbr", false, SIDEBAND_TEXT
,
88 SIDEBAND_LONGTEXT
, false )
89 add_integer( ENC_CFG_PREFIX
"vbr", 0, VBR_QUALITY_TEXT
,
90 VBR_QUALITY_LONGTEXT
, false )
91 change_integer_range (0, 5)
92 add_bool( ENC_CFG_PREFIX
"afterburner", true, AFTERBURNER_TEXT
,
93 AFTERBURNER_LONGTEXT
, true )
94 add_integer( ENC_CFG_PREFIX
"signaling", SIGNALING_COMPATIBLE
, SIGNALING_TEXT
,
95 SIGNALING_LONGTEXT
, true )
96 change_integer_range (0, 2)
99 /*****************************************************************************
101 *****************************************************************************/
102 static block_t
*EncodeAudio( encoder_t
*p_enc
, block_t
*p_buf
);
104 static const char *const ppsz_enc_options
[] = {
105 "profile", "sbr", "vbr", "afterburner", "signaling", NULL
108 /*****************************************************************************
109 * encoder_sys_t : aac encoder descriptor
110 *****************************************************************************/
113 double d_compression_ratio
;
115 int i_aot
; /* This stores the aac profile chosen */
116 int i_vbr
; /* cbr or vbr-quality value chosen */
117 int i_signaling
; /* Library feature for backwards compatibility */
118 int i_encoderdelay
; /* Samples delay introduced by the profile */
120 int i_maxoutputsize
; /* Maximum buffer size for encoded output */
121 HANDLE_AACENCODER handle
;
122 bool b_afterburner
; /* Library feature for additional quality */
123 bool b_eld_sbr
; /* Spectral band replication option for ELD profile */
126 static const char *aac_get_errorstring(AACENC_ERROR erraac
)
131 case AACENC_INVALID_HANDLE
:
132 return "Invalid handle";
133 case AACENC_MEMORY_ERROR
:
134 return "Memory allocation error";
135 case AACENC_UNSUPPORTED_PARAMETER
:
136 return "Unsupported parameter";
137 case AACENC_INVALID_CONFIG
:
138 return "Invalid config";
139 case AACENC_INIT_ERROR
:
140 return "Initialization error";
141 case AACENC_INIT_AAC_ERROR
:
142 return "AAC library initialization error";
143 case AACENC_INIT_SBR_ERROR
:
144 return "SBR library initialization error";
145 case AACENC_INIT_TP_ERROR
:
146 return "Transport library initialization error";
147 case AACENC_INIT_META_ERROR
:
148 return "Metadata library initialization error";
149 case AACENC_ENCODE_ERROR
:
150 return "Encoding error";
151 case AACENC_ENCODE_EOF
:
152 return "End of file";
154 return "Unknown error";
158 /*****************************************************************************
159 * OpenDecoder: open the encoder.
160 *****************************************************************************/
161 static int OpenEncoder( vlc_object_t
*p_this
)
164 encoder_sys_t
*p_sys
;
167 bool b_profile_selected
;
173 p_enc
= (encoder_t
*)p_this
;
174 b_profile_selected
= false;
178 if( p_enc
->fmt_out
.i_codec
!= VLC_FOURCC( 'l', 'a', 'a', 'c' ) &&
179 p_enc
->fmt_out
.i_codec
!= VLC_FOURCC( 'h', 'a', 'a', 'c' ) &&
180 p_enc
->fmt_out
.i_codec
!= VLC_FOURCC( 's', 'a', 'a', 'c' ) &&
181 p_enc
->fmt_out
.i_codec
!= VLC_CODEC_MP4A
)
185 else if ( p_enc
->fmt_out
.i_codec
== VLC_FOURCC( 'l', 'a', 'a', 'c' ) )
187 b_profile_selected
= true;
188 i_profile
= PROFILE_AAC_LC
;
190 else if ( p_enc
->fmt_out
.i_codec
== VLC_FOURCC( 'h', 'a', 'a', 'c' ) )
192 b_profile_selected
= true;
193 i_profile
= PROFILE_AAC_HE
;
195 else if ( p_enc
->fmt_out
.i_codec
== VLC_FOURCC( 's', 'a', 'a', 'c' ) )
197 b_profile_selected
= true;
198 i_profile
= PROFILE_AAC_HE_v2
;
201 switch (p_enc
->fmt_in
.audio
.i_channels
) {
202 case 1: mode
= MODE_1
; sce
= 1; cpe
= 0; break;
203 case 2: mode
= MODE_2
; sce
= 0; cpe
= 1; break;
204 case 3: mode
= MODE_1_2
; sce
= 1; cpe
= 1; break;
205 case 4: mode
= MODE_1_2_1
; sce
= 2; cpe
= 1; break;
206 case 5: mode
= MODE_1_2_2
; sce
= 1; cpe
= 2; break;
207 case 6: mode
= MODE_1_2_2_1
; sce
= 2; cpe
= 2; break;
208 case 8: mode
= MODE_1_2_2_2_1
; sce
= 2; cpe
= 3; break;
210 msg_Err( p_enc
, "we do not support > 8 input channels, this input has %i",
211 p_enc
->fmt_in
.audio
.i_channels
);
215 msg_Info(p_enc
, "Initializing AAC Encoder, %i channels", p_enc
->fmt_in
.audio
.i_channels
);
217 /* Allocate the memory needed to store the encoder's structure */
218 p_sys
= (encoder_sys_t
*)malloc(sizeof(encoder_sys_t
));
219 if( unlikely( !p_sys
) )
221 p_enc
->p_sys
= p_sys
;
222 p_enc
->fmt_in
.i_codec
= VLC_CODEC_S16N
;
223 p_enc
->fmt_out
.i_cat
= AUDIO_ES
;
224 p_enc
->fmt_out
.i_codec
= VLC_CODEC_MP4A
;
226 config_ChainParse( p_enc
, ENC_CFG_PREFIX
, ppsz_enc_options
, p_enc
->p_cfg
);
228 if ( b_profile_selected
== false )
229 p_sys
->i_aot
= var_InheritInteger( p_enc
, ENC_CFG_PREFIX
"profile" );
231 p_sys
->i_aot
= i_profile
;
232 p_sys
->b_eld_sbr
= var_InheritBool( p_enc
, ENC_CFG_PREFIX
"sbr" );
233 p_sys
->i_vbr
= var_InheritInteger( p_enc
, ENC_CFG_PREFIX
"vbr" );
234 p_sys
->b_afterburner
= var_InheritBool( p_enc
, ENC_CFG_PREFIX
"afterburner" );
235 p_sys
->i_signaling
= var_InheritInteger( p_enc
, ENC_CFG_PREFIX
"signaling" );
236 p_sys
->i_pts_last
= 0;
238 if ((p_sys
->i_aot
== PROFILE_AAC_HE
|| p_sys
->i_aot
== PROFILE_AAC_HE_v2
) && p_sys
->i_vbr
> 3)
240 msg_Warn(p_enc
, "Maximum VBR quality for this profile is 3, setting vbr=3");
243 if ((erraac
= aacEncOpen(&p_sys
->handle
, 0, p_enc
->fmt_in
.audio
.i_channels
)) != AACENC_OK
) {
244 msg_Err(p_enc
, "Unable to open encoder: %s", aac_get_errorstring(erraac
));
248 if ( p_sys
->i_aot
== PROFILE_AAC_HE_v2
&& p_enc
->fmt_in
.audio
.i_channels
!= 2 )
250 msg_Err(p_enc
, "The HE-AAC-v2 profile can only be used with stereo sources");
253 if ( p_sys
->i_aot
== PROFILE_AAC_ELD
&& p_enc
->fmt_in
.audio
.i_channels
!= 2 )
255 msg_Err(p_enc
, "The ELD-AAC profile can only be used with stereo sources");
258 if ((erraac
= aacEncoder_SetParam(p_sys
->handle
, AACENC_AOT
, p_sys
->i_aot
)) != AACENC_OK
) {
259 msg_Err(p_enc
, "Unable to set the Profile %i: %s", p_sys
->i_aot
, aac_get_errorstring(erraac
));
262 if (p_sys
->i_aot
== PROFILE_AAC_ELD
&& p_sys
->b_eld_sbr
) {
263 if ((erraac
= aacEncoder_SetParam(p_sys
->handle
, AACENC_SBR_MODE
, 1)) != AACENC_OK
) {
264 msg_Err(p_enc
, "Unable to set SBR mode for ELD: %s", aac_get_errorstring(erraac
));
268 if ((erraac
= aacEncoder_SetParam(p_sys
->handle
, AACENC_SAMPLERATE
,
269 p_enc
->fmt_out
.audio
.i_rate
)) != AACENC_OK
) {
270 msg_Err(p_enc
, "Unable to set the sample rate %i: %s",p_enc
->fmt_out
.audio
.i_rate
,
271 aac_get_errorstring(erraac
));
274 if ((erraac
= aacEncoder_SetParam(p_sys
->handle
, AACENC_CHANNELMODE
, mode
)) != AACENC_OK
) {
275 msg_Err(p_enc
, "Unable to set the channel mode: %s", aac_get_errorstring(erraac
));
278 if ((erraac
= aacEncoder_SetParam(p_sys
->handle
, AACENC_CHANNELORDER
, CH_ORDER_WG4
)) != AACENC_OK
) {
279 msg_Err(p_enc
, "Unable to set the sound channel order: %s", aac_get_errorstring(erraac
));
282 if (p_sys
->i_vbr
!= 0) {
283 if ((erraac
= aacEncoder_SetParam(p_sys
->handle
,
284 AACENC_BITRATEMODE
, p_sys
->i_vbr
)) != AACENC_OK
) {
285 msg_Err(p_enc
, "Unable to set the VBR bitrate mode: %s", aac_get_errorstring(erraac
));
289 if (p_enc
->fmt_out
.i_bitrate
== 0) {
290 if (p_sys
->i_aot
== PROFILE_AAC_HE_v2
) {
294 i_bitrate
= (96*sce
+ 128*cpe
) * p_enc
->fmt_out
.audio
.i_rate
/ 44;
295 if (p_sys
->i_aot
== PROFILE_AAC_HE
||
296 p_sys
->i_aot
== PROFILE_AAC_HE_v2
||
299 p_enc
->fmt_out
.i_bitrate
= i_bitrate
;
300 msg_Info(p_enc
, "Setting optimal bitrate of %i", i_bitrate
);
304 i_bitrate
= p_enc
->fmt_out
.i_bitrate
;
306 if ((erraac
= aacEncoder_SetParam(p_sys
->handle
, AACENC_BITRATE
,
307 i_bitrate
)) != AACENC_OK
) {
308 msg_Err(p_enc
, "Unable to set the bitrate %i: %s", i_bitrate
,
309 aac_get_errorstring(erraac
));
313 if ((erraac
= aacEncoder_SetParam(p_sys
->handle
, AACENC_TRANSMUX
, 0)) != AACENC_OK
) {
314 msg_Err(p_enc
, "Unable to set the ADTS transmux: %s", aac_get_errorstring(erraac
));
317 if ((erraac
= aacEncoder_SetParam(p_sys
->handle
, AACENC_SIGNALING_MODE
,
318 (int)p_sys
->i_signaling
)) != AACENC_OK
) {
319 /* use explicit backward compatible =1 */
320 /* use explicit hierarchical signaling =2 */
321 msg_Err(p_enc
, "Unable to set signaling mode: %s", aac_get_errorstring(erraac
));
324 if ((erraac
= aacEncoder_SetParam(p_sys
->handle
, AACENC_AFTERBURNER
,
325 (int)p_sys
->b_afterburner
)) != AACENC_OK
) {
326 msg_Err(p_enc
, "Unable to set the afterburner mode: %s", aac_get_errorstring(erraac
));
329 if ((erraac
= aacEncEncode(p_sys
->handle
, NULL
, NULL
, NULL
, NULL
)) != AACENC_OK
) {
330 msg_Err(p_enc
, "Unable to initialize the encoder: %s", aac_get_errorstring(erraac
));
333 AACENC_InfoStruct info
= { 0 };
334 if ((erraac
= aacEncInfo(p_sys
->handle
, &info
)) != AACENC_OK
) {
335 msg_Err(p_enc
, "Unable to get the encoder info: %s", aac_get_errorstring(erraac
));
339 /* The maximum packet size is 6144 bits aka 768 bytes per channel. */
340 p_sys
->i_maxoutputsize
= 768*p_enc
->fmt_in
.audio
.i_channels
;
341 p_enc
->fmt_in
.audio
.i_bitspersample
= 16;
342 p_sys
->i_frame_size
= info
.frameLength
;
343 p_sys
->i_encoderdelay
= info
.encoderDelay
;
345 p_enc
->fmt_out
.i_extra
= info
.confSize
;
346 if( p_enc
->fmt_out
.i_extra
)
348 p_enc
->fmt_out
.p_extra
= malloc( p_enc
->fmt_out
.i_extra
);
349 if ( p_enc
->fmt_out
.p_extra
== NULL
)
351 msg_Err(p_enc
, "Unable to allocate fmt_out.p_extra");
354 memcpy( p_enc
->fmt_out
.p_extra
, info
.confBuf
,
355 p_enc
->fmt_out
.i_extra
);
358 p_enc
->pf_encode_audio
= EncodeAudio
;
361 // TODO: Add more debug info to this config printout
362 msg_Dbg(p_enc
, "fmt_out.p_extra = %i", p_enc
->fmt_out
.i_extra
);
368 aacEncClose(&p_sys
->handle
);
373 /****************************************************************************
374 * EncodeAudio: the whole thing
375 ****************************************************************************/
376 static block_t
*EncodeAudio( encoder_t
*p_enc
, block_t
*p_aout_buf
)
378 encoder_sys_t
*p_sys
;
387 p_sys
= p_enc
->p_sys
;
390 if ( likely( p_aout_buf
) )
392 p_buffer
= (int16_t *)p_aout_buf
->p_buffer
;
393 i_samples
= p_aout_buf
->i_nb_samples
;
394 i_pts_out
= p_aout_buf
->i_pts
- (mtime_t
)((double)CLOCK_FREQ
*
395 (double)p_sys
->i_encoderdelay
/
396 (double)p_enc
->fmt_out
.audio
.i_rate
);
397 if (p_sys
->i_pts_last
== 0)
398 p_sys
->i_pts_last
= i_pts_out
- (mtime_t
)((double)CLOCK_FREQ
*
399 (double)(p_sys
->i_frame_size
) /
400 (double)p_enc
->fmt_out
.audio
.i_rate
);
405 i_pts_out
= p_sys
->i_pts_last
;
408 i_samples_left
= i_samples
;
411 while ( i_samples_left
>= 0 )
413 AACENC_BufDesc in_buf
= { 0 }, out_buf
= { 0 };
414 AACENC_InArgs in_args
= { 0 };
415 AACENC_OutArgs out_args
= { 0 };
416 int in_identifier
= IN_AUDIO_DATA
;
417 int in_size
, in_elem_size
;
418 int out_identifier
= OUT_BITSTREAM_DATA
;
419 int out_size
, out_elem_size
;
420 void *in_ptr
, *out_ptr
;
422 if ( unlikely(i_samples
== 0) ) {
423 // this forces the encoder to purge whatever is left in the internal buffer
424 in_args
.numInSamples
= -1;
426 in_ptr
= p_buffer
+ (i_samples
- i_samples_left
)*p_enc
->fmt_in
.audio
.i_channels
;
427 in_size
= 2*p_enc
->fmt_in
.audio
.i_channels
*i_samples_left
;
429 in_args
.numInSamples
= p_enc
->fmt_in
.audio
.i_channels
*i_samples_left
;
431 in_buf
.bufs
= &in_ptr
;
432 in_buf
.bufferIdentifiers
= &in_identifier
;
433 in_buf
.bufSizes
= &in_size
;
434 in_buf
.bufElSizes
= &in_elem_size
;
437 p_block
= block_Alloc( p_sys
->i_maxoutputsize
);
438 p_block
->i_buffer
= p_sys
->i_maxoutputsize
;
439 out_ptr
= p_block
->p_buffer
;
440 out_size
= p_block
->i_buffer
;
443 out_buf
.bufs
= &out_ptr
;
444 out_buf
.bufferIdentifiers
= &out_identifier
;
445 out_buf
.bufSizes
= &out_size
;
446 out_buf
.bufElSizes
= &out_elem_size
;
448 if ((erraac
= aacEncEncode(p_sys
->handle
, &in_buf
, &out_buf
, &in_args
, &out_args
)) != AACENC_OK
) {
449 if (erraac
== AACENC_ENCODE_EOF
) {
450 msg_Info( p_enc
, "Encoding final bytes (EOF)");
454 msg_Err( p_enc
, "Encoding failed: %s", aac_get_errorstring(erraac
));
455 block_Release(p_block
);
459 if ( out_args
.numOutBytes
> 0 )
461 p_block
->i_buffer
= out_args
.numOutBytes
;
462 if ( unlikely(i_samples
== 0) )
464 // I only have the numOutBytes so approximate based on compression factor
465 double d_samples_forward
= p_sys
->d_compression_ratio
*(double)out_args
.numOutBytes
;
466 i_pts_out
+= (mtime_t
)d_samples_forward
;
467 p_block
->i_length
= (mtime_t
)d_samples_forward
;
468 // TODO: It would be more precise (a few microseconds) to use d_samples_forward =
469 // (mtime_t)CLOCK_FREQ * (mtime_t)p_sys->i_frame_size/(mtime_t)p_enc->fmt_out.audio.i_rate
470 // but I am not sure if the lib always outputs a full frame when
471 // emptying the internal buffer in the EOF scenario
475 if ( i_loop_count
== 0 )
477 // There can be an implicit delay in the first loop cycle because leftover bytes
478 // in the library buffer from the prior block
479 double d_samples_delay
= (double)p_sys
->i_frame_size
- (double)out_args
.numInSamples
/
480 (double)p_enc
->fmt_in
.audio
.i_channels
;
481 i_pts_out
-= (mtime_t
)((double)CLOCK_FREQ
* d_samples_delay
/
482 (double)p_enc
->fmt_out
.audio
.i_rate
);
483 //p_block->i_length = (mtime_t)((double)CLOCK_FREQ * (double)p_sys->i_frame_size /
484 // (double)p_enc->fmt_out.audio.i_rate);
485 p_block
->i_length
= i_pts_out
- p_sys
->i_pts_last
;
489 double d_samples_forward
= (double)out_args
.numInSamples
/(double)p_enc
->fmt_in
.audio
.i_channels
;
490 double d_length
= ((double)CLOCK_FREQ
* d_samples_forward
/
491 (double)p_enc
->fmt_out
.audio
.i_rate
);
492 i_pts_out
+= (mtime_t
) d_length
;
493 p_block
->i_length
= (mtime_t
) d_length
;
496 p_block
->i_dts
= p_block
->i_pts
= i_pts_out
;
497 block_ChainAppend( &p_chain
, p_block
);
498 //msg_Dbg( p_enc, "p_block->i_dts %llu, p_block->i_length %llu, p_sys->i_pts_last %llu\n\
499 // out_args.numOutBytes = %i, out_args.numInSamples = %i, i_samples %i, i_loop_count %i",
500 // p_block->i_dts , p_block->i_length,p_sys->i_pts_last,
501 // out_args.numOutBytes, out_args.numInSamples, i_samples, i_loop_count);
502 if ( likely(i_samples
> 0) )
504 p_sys
->d_compression_ratio
= (double)p_block
->i_length
/ (double)out_args
.numOutBytes
;
505 i_samples_left
-= out_args
.numInSamples
/p_enc
->fmt_in
.audio
.i_channels
;
506 p_sys
->i_pts_last
= i_pts_out
;
511 block_Release(p_block
);
512 //msg_Dbg( p_enc, "aac_encode_audio: not enough data yet");
515 if ( unlikely(i_loop_count
++ > 100) )
517 msg_Err( p_enc
, "Loop count greater than 100!!!, something must be wrong with the encoder library");
526 /*****************************************************************************
527 * CloseDecoder: decoder destruction
528 *****************************************************************************/
529 static void CloseEncoder( vlc_object_t
*p_this
)
531 encoder_t
*p_enc
= (encoder_t
*)p_this
;
532 encoder_sys_t
*p_sys
= p_enc
->p_sys
;
534 aacEncClose(&p_sys
->handle
);