]> git.sesse.net Git - vlc/blob - modules/codec/fdkaac.c
rename configuration page title for x264 options
[vlc] / modules / codec / fdkaac.c
1 /*****************************************************************************
2  * aac.c: FDK-AAC Encoder plugin for vlc.
3  *****************************************************************************
4  * Copyright (C) 2012 Sergio Ammirata
5  *
6  * Authors: Sergio Ammirata <sergio@ammirata.net>
7  *
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.
12  * 
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.
17  * 
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  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29  
30 #include <fdk-aac/aacenc_lib.h>
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
35
36 static int OpenEncoder( vlc_object_t * );
37 static void CloseEncoder( vlc_object_t * );
38
39 #define ENC_CFG_PREFIX "sout-fdkaac-"
40
41 #define AOT_TEXT N_("Encoder Profile")
42 #define AOT_LONGTEXT N_( "Encoder Algorithm to use" )
43
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" )
46
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" )
49
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)" )
52
53 #define BITRATE_TEXT N_("CBR Bitrate")
54 #define BITRATE_LONGTEXT N_( "Bitrate of desired stream (in bps)" )
55
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" )
58
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) */
62
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
68
69 #define SIGNALING_COMPATIBLE 1
70 #define SIGNALING_HIERARCHICAL 2
71
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") };
75
76 vlc_module_begin ()
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,
85              AOT_LONGTEXT, false )
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)
97 vlc_module_end ()
98
99 /*****************************************************************************
100  * Local prototypes
101  *****************************************************************************/
102 static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_buf );
103
104 static const char *const ppsz_enc_options[] = {
105     "profile", "sbr", "vbr", "afterburner", "signaling", NULL
106 };
107
108 /*****************************************************************************
109  * encoder_sys_t : aac encoder descriptor
110  *****************************************************************************/
111 struct encoder_sys_t
112 {
113     double d_compression_ratio;
114     mtime_t i_pts_last;
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 */
119     int i_frame_size;
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 */
124 };
125
126 static const char *aac_get_errorstring(AACENC_ERROR erraac)
127 {
128     switch (erraac) {
129     case AACENC_OK:
130         return "No error";
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";
153     default:
154         return "Unknown error";
155     }
156 }
157
158 /*****************************************************************************
159  * OpenDecoder: open the encoder.
160  *****************************************************************************/
161 static int OpenEncoder( vlc_object_t *p_this )
162 {
163     encoder_t *p_enc;
164     encoder_sys_t *p_sys;
165     CHANNEL_MODE mode;
166     AACENC_ERROR erraac;
167     bool b_profile_selected;
168     int sce;
169     int cpe;
170     int i_profile;
171     int i_bitrate;
172
173     p_enc = (encoder_t *)p_this;
174     b_profile_selected = false;
175     sce = 0;
176     cpe = 0;
177
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 )
182     {
183         return VLC_EGENERIC;
184     }
185     else if ( p_enc->fmt_out.i_codec == VLC_FOURCC( 'l', 'a', 'a', 'c' ) )
186     {
187         b_profile_selected = true;
188         i_profile = PROFILE_AAC_LC;
189     }
190     else if ( p_enc->fmt_out.i_codec == VLC_FOURCC( 'h', 'a', 'a', 'c' ) )
191     {
192         b_profile_selected = true;
193         i_profile = PROFILE_AAC_HE;
194     }
195     else if ( p_enc->fmt_out.i_codec == VLC_FOURCC( 's', 'a', 'a', 'c' ) )
196     {
197         b_profile_selected = true;
198         i_profile = PROFILE_AAC_HE_v2;
199     }
200
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;
209     default:
210         msg_Err( p_enc, "we do not support > 8 input channels, this input has %i", 
211                         p_enc->fmt_in.audio.i_channels );
212         return VLC_EGENERIC;
213     }
214
215     msg_Info(p_enc, "Initializing AAC Encoder, %i channels", p_enc->fmt_in.audio.i_channels);
216
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 ) )
220         return VLC_ENOMEM;
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;
225
226     config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
227
228     if ( b_profile_selected == false )
229         p_sys->i_aot = var_InheritInteger( p_enc, ENC_CFG_PREFIX "profile" );
230     else
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;
237
238     if ((p_sys->i_aot == PROFILE_AAC_HE || p_sys->i_aot == PROFILE_AAC_HE_v2) && p_sys->i_vbr > 3)
239     {
240         msg_Warn(p_enc, "Maximum VBR quality for this profile is 3, setting vbr=3");
241         p_sys->i_vbr = 3;
242     }
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));
245         free( p_sys );
246         return VLC_EGENERIC;
247     }
248     if ( p_sys->i_aot == PROFILE_AAC_HE_v2 && p_enc->fmt_in.audio.i_channels != 2 )
249     {
250         msg_Err(p_enc, "The HE-AAC-v2 profile can only be used with stereo sources");
251         goto error;
252     }
253     if ( p_sys->i_aot == PROFILE_AAC_ELD && p_enc->fmt_in.audio.i_channels != 2 )
254     {
255         msg_Err(p_enc, "The ELD-AAC profile can only be used with stereo sources");
256         goto error;
257     }
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));
260         goto error;
261     }
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));
265         goto error;
266         }
267     }
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));
272         goto error;
273     }
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));
276         goto error;
277     }
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));
280         goto error;
281     }
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));
286             goto error;
287         }
288     } else {
289         if (p_enc->fmt_out.i_bitrate == 0) {
290             if (p_sys->i_aot == PROFILE_AAC_HE_v2) {
291                 sce = 1;
292                 cpe = 0;
293             }
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 ||
297                 p_sys->b_eld_sbr)
298                 i_bitrate /= 2;
299             p_enc->fmt_out.i_bitrate = i_bitrate;
300             msg_Info(p_enc, "Setting optimal bitrate of %i", i_bitrate);
301         }
302         else
303         {
304             i_bitrate = p_enc->fmt_out.i_bitrate;
305         }
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));
310             goto error;
311         }
312     }
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));
315         goto error;
316     }
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));
322         goto error;
323     }
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));
327         goto error;
328     }
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));
331         goto error;
332     }
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));
336         goto error;
337     }
338
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;
344
345     p_enc->fmt_out.i_extra = info.confSize;
346     if( p_enc->fmt_out.i_extra )
347     {
348         p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
349         if ( p_enc->fmt_out.p_extra == NULL )
350         {
351             msg_Err(p_enc, "Unable to allocate fmt_out.p_extra");
352             goto error;
353         }
354         memcpy( p_enc->fmt_out.p_extra, info.confBuf,
355                 p_enc->fmt_out.i_extra );
356     }
357
358     p_enc->pf_encode_audio = EncodeAudio;
359
360 #ifndef NDEBUG
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);
363 #endif
364
365     return VLC_SUCCESS;
366
367 error:
368     aacEncClose(&p_sys->handle);
369     free( p_sys );
370     return VLC_EGENERIC;
371 }
372
373 /****************************************************************************
374  * EncodeAudio: the whole thing
375  ****************************************************************************/
376 static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_aout_buf )
377 {
378     encoder_sys_t *p_sys;
379     int16_t *p_buffer;
380     int i_samples;
381     int i_loop_count;
382     int i_samples_left;
383     mtime_t i_pts_out;
384     block_t *p_chain;
385     AACENC_ERROR erraac;
386
387     p_sys = p_enc->p_sys;
388     p_chain = NULL;
389
390     if ( likely( p_aout_buf ) )
391     {
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);
401     }
402     else
403     {
404         i_samples = 0;
405         i_pts_out = p_sys->i_pts_last;
406     }
407
408     i_samples_left = i_samples;
409     i_loop_count = 0;
410
411     while ( i_samples_left >= 0 )
412     {
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;
421
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;
425         } else {
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;
428             in_elem_size = 2;
429             in_args.numInSamples = p_enc->fmt_in.audio.i_channels*i_samples_left;
430             in_buf.numBufs = 1;
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;
435         }
436         block_t *p_block;
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;
441         out_elem_size = 1;
442         out_buf.numBufs = 1;
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;
447
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)");
451             }
452             else
453             {
454                 msg_Err( p_enc, "Encoding failed: %s", aac_get_errorstring(erraac));
455                 block_Release(p_block);
456                 break;
457             }
458         }
459         if ( out_args.numOutBytes > 0 )
460         {
461             p_block->i_buffer = out_args.numOutBytes;
462             if ( unlikely(i_samples == 0) )
463             {
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
472             }
473             else
474             {
475                 if ( i_loop_count == 0 )
476                 {
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;
486                 }
487                 else
488                 {
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;
494                 }
495             }
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) )
503             {
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;
507             }
508         }
509         else
510         {
511             block_Release(p_block);
512             //msg_Dbg( p_enc, "aac_encode_audio: not enough data yet");
513             break;
514         }
515         if ( unlikely(i_loop_count++ > 100) )
516         {
517             msg_Err( p_enc, "Loop count greater than 100!!!, something must be wrong with the encoder library");
518             break;
519         }
520     }
521
522     return p_chain;
523
524 }
525
526 /*****************************************************************************
527  * CloseDecoder: decoder destruction
528  *****************************************************************************/
529 static void CloseEncoder( vlc_object_t *p_this )
530 {
531     encoder_t *p_enc = (encoder_t *)p_this;
532     encoder_sys_t *p_sys = p_enc->p_sys;
533
534     aacEncClose(&p_sys->handle);
535
536     free( p_sys );
537 }