]> git.sesse.net Git - vlc/blob - modules/codec/fdkaac.c
fdkaac: cosmetics
[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  *  Alternatively you can redistribute this file under the terms of the
23  *  BSD license as stated below:
24  *
25  *  Redistribution and use in source and binary forms, with or without
26  *  modification, are permitted provided that the following conditions
27  *  are met:
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
33  *     distribution.
34  *
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.
46  *
47  *****************************************************************************/
48
49 /*****************************************************************************
50  * Preamble
51  *****************************************************************************/
52 #ifdef HAVE_CONFIG_H
53 # include "config.h"
54 #endif
55
56 #include <fdk-aac/aacenc_lib.h>
57
58 #include <vlc_common.h>
59 #include <vlc_plugin.h>
60 #include <vlc_codec.h>
61
62 static int OpenEncoder( vlc_object_t * );
63 static void CloseEncoder( vlc_object_t * );
64
65 #define ENC_CFG_PREFIX "sout-fdkaac-"
66
67 #define AOT_TEXT N_("Encoder Profile")
68 #define AOT_LONGTEXT N_( "Encoder Algorithm to use" )
69
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" )
72
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" )
75
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)" )
78
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" )
81
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) */
85
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
91
92 #define SIGNALING_COMPATIBLE 1
93 #define SIGNALING_HIERARCHICAL 2
94
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") };
98
99 vlc_module_begin ()
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,
108              AOT_LONGTEXT, false )
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)
120 vlc_module_end ()
121
122 /*****************************************************************************
123  * Local prototypes
124  *****************************************************************************/
125 static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_buf );
126
127 static const char *const ppsz_enc_options[] = {
128     "profile", "sbr", "vbr", "afterburner", "signaling", NULL
129 };
130
131 /*****************************************************************************
132  * encoder_sys_t : aac encoder descriptor
133  *****************************************************************************/
134 struct encoder_sys_t
135 {
136     double d_compression_ratio;
137     mtime_t i_pts_last;
138     int i_aot; /* This stores the aac profile chosen */
139     int i_vbr; /* cbr or vbr-quality value chosen */
140     int i_signaling; /* Library feature for backwards compatibility */
141     int i_encoderdelay; /* Samples delay introduced by the profile */
142     int i_frame_size;
143     int i_maxoutputsize; /* Maximum buffer size for encoded output */
144     HANDLE_AACENCODER handle;
145     bool b_afterburner; /* Library feature for additional quality */
146     bool b_eld_sbr; /* Spectral band replication option for ELD profile */
147 };
148
149 static const char *aac_get_errorstring(AACENC_ERROR erraac)
150 {
151     switch (erraac) {
152     case AACENC_OK:
153         return "No error";
154     case AACENC_INVALID_HANDLE:
155         return "Invalid handle";
156     case AACENC_MEMORY_ERROR:
157         return "Memory allocation error";
158     case AACENC_UNSUPPORTED_PARAMETER:
159         return "Unsupported parameter";
160     case AACENC_INVALID_CONFIG:
161         return "Invalid config";
162     case AACENC_INIT_ERROR:
163         return "Initialization error";
164     case AACENC_INIT_AAC_ERROR:
165         return "AAC library initialization error";
166     case AACENC_INIT_SBR_ERROR:
167         return "SBR library initialization error";
168     case AACENC_INIT_TP_ERROR:
169         return "Transport library initialization error";
170     case AACENC_INIT_META_ERROR:
171         return "Metadata library initialization error";
172     case AACENC_ENCODE_ERROR:
173         return "Encoding error";
174     case AACENC_ENCODE_EOF:
175         return "End of file";
176     default:
177         return "Unknown error";
178     }
179 }
180
181 /*****************************************************************************
182  * OpenDecoder: open the encoder.
183  *****************************************************************************/
184 static int OpenEncoder( vlc_object_t *p_this )
185 {
186     encoder_t *p_enc;
187     encoder_sys_t *p_sys;
188     CHANNEL_MODE mode;
189     AACENC_ERROR erraac;
190     bool b_profile_selected;
191     int sce;
192     int cpe;
193     int i_profile;
194     int i_bitrate;
195
196     p_enc = (encoder_t *)p_this;
197     b_profile_selected = false;
198     sce = 0;
199     cpe = 0;
200
201     if( p_enc->fmt_out.i_codec != VLC_FOURCC( 'l', 'a', 'a', 'c' ) &&
202         p_enc->fmt_out.i_codec != VLC_FOURCC( 'h', 'a', 'a', 'c' ) &&
203         p_enc->fmt_out.i_codec != VLC_FOURCC( 's', 'a', 'a', 'c' ) &&
204         p_enc->fmt_out.i_codec != VLC_CODEC_MP4A )
205     {
206         return VLC_EGENERIC;
207     }
208     else if ( p_enc->fmt_out.i_codec == VLC_FOURCC( 'l', 'a', 'a', 'c' ) )
209     {
210         b_profile_selected = true;
211         i_profile = PROFILE_AAC_LC;
212     }
213     else if ( p_enc->fmt_out.i_codec == VLC_FOURCC( 'h', 'a', 'a', 'c' ) )
214     {
215         b_profile_selected = true;
216         i_profile = PROFILE_AAC_HE;
217     }
218     else if ( p_enc->fmt_out.i_codec == VLC_FOURCC( 's', 'a', 'a', 'c' ) )
219     {
220         b_profile_selected = true;
221         i_profile = PROFILE_AAC_HE_v2;
222     }
223
224     uint16_t channel_config;
225     switch (p_enc->fmt_in.audio.i_channels) {
226     case 1: mode = MODE_1;       sce = 1; cpe = 0;
227          channel_config = AOUT_CHAN_CENTER; break;
228     case 2: mode = MODE_2;       sce = 0; cpe = 1;
229          channel_config = AOUT_CHANS_STEREO; break;
230     case 3: mode = MODE_1_2;     sce = 1; cpe = 1;
231          channel_config = AOUT_CHANS_3_0; break;
232     case 4: mode = MODE_1_2_1;   sce = 2; cpe = 1;
233          channel_config = AOUT_CHANS_4_CENTER_REAR; break;
234     case 5: mode = MODE_1_2_2;   sce = 1; cpe = 2;
235          channel_config = AOUT_CHANS_5_0; break;
236     case 6: mode = MODE_1_2_2_1; sce = 2; cpe = 2;
237          channel_config = AOUT_CHANS_5_1; break;
238     case 8: mode = MODE_1_2_2_2_1; sce = 2; cpe = 3;
239          channel_config = AOUT_CHANS_7_1; break;
240     default:
241         msg_Err( p_enc, "we do not support > 8 input channels, this input has %i",
242                         p_enc->fmt_in.audio.i_channels );
243         return VLC_EGENERIC;
244     }
245
246     p_enc->fmt_in.audio.i_physical_channels = channel_config;
247
248     msg_Info(p_enc, "Initializing AAC Encoder, %i channels", p_enc->fmt_in.audio.i_channels);
249
250     /* Allocate the memory needed to store the encoder's structure */
251     p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t));
252     if( unlikely( !p_sys ) )
253         return VLC_ENOMEM;
254     p_enc->p_sys = p_sys;
255     p_enc->fmt_in.i_codec = VLC_CODEC_S16N;
256     p_enc->fmt_out.i_cat = AUDIO_ES;
257     p_enc->fmt_out.i_codec = VLC_CODEC_MP4A;
258
259     config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
260
261     if ( b_profile_selected == false )
262         p_sys->i_aot = var_InheritInteger( p_enc, ENC_CFG_PREFIX "profile" );
263     else
264         p_sys->i_aot = i_profile;
265     p_sys->b_eld_sbr = var_InheritBool( p_enc, ENC_CFG_PREFIX "sbr" );
266     p_sys->i_vbr = var_InheritInteger( p_enc, ENC_CFG_PREFIX "vbr" );
267     p_sys->b_afterburner = var_InheritBool( p_enc, ENC_CFG_PREFIX "afterburner" );
268     p_sys->i_signaling = var_InheritInteger( p_enc, ENC_CFG_PREFIX "signaling" );
269     p_sys->i_pts_last = 0;
270
271     if ((p_sys->i_aot == PROFILE_AAC_HE || p_sys->i_aot == PROFILE_AAC_HE_v2) && p_sys->i_vbr > 3)
272     {
273         msg_Warn(p_enc, "Maximum VBR quality for this profile is 3, setting vbr=3");
274         p_sys->i_vbr = 3;
275     }
276     if ((erraac = aacEncOpen(&p_sys->handle, 0, p_enc->fmt_in.audio.i_channels)) != AACENC_OK) {
277         msg_Err(p_enc, "Unable to open encoder: %s", aac_get_errorstring(erraac));
278         free( p_sys );
279         return VLC_EGENERIC;
280     }
281     if ( p_sys->i_aot == PROFILE_AAC_HE_v2 && p_enc->fmt_in.audio.i_channels != 2 )
282     {
283         msg_Err(p_enc, "The HE-AAC-v2 profile can only be used with stereo sources");
284         goto error;
285     }
286     if ( p_sys->i_aot == PROFILE_AAC_ELD && p_enc->fmt_in.audio.i_channels != 2 )
287     {
288         msg_Err(p_enc, "The ELD-AAC profile can only be used with stereo sources");
289         goto error;
290     }
291     if ((erraac = aacEncoder_SetParam(p_sys->handle, AACENC_AOT, p_sys->i_aot)) != AACENC_OK) {
292         msg_Err(p_enc, "Unable to set the Profile %i: %s", p_sys->i_aot, aac_get_errorstring(erraac));
293         goto error;
294     }
295     if (p_sys->i_aot == PROFILE_AAC_ELD && p_sys->b_eld_sbr) {
296         if ((erraac = aacEncoder_SetParam(p_sys->handle, AACENC_SBR_MODE, 1)) != AACENC_OK) {
297             msg_Err(p_enc, "Unable to set SBR mode for ELD: %s", aac_get_errorstring(erraac));
298         goto error;
299         }
300     }
301     if ((erraac = aacEncoder_SetParam(p_sys->handle, AACENC_SAMPLERATE,
302                     p_enc->fmt_out.audio.i_rate)) != AACENC_OK) {
303         msg_Err(p_enc, "Unable to set the sample rate %i: %s",p_enc->fmt_out.audio.i_rate,
304                         aac_get_errorstring(erraac));
305         goto error;
306     }
307     if ((erraac = aacEncoder_SetParam(p_sys->handle, AACENC_CHANNELMODE, mode)) != AACENC_OK) {
308         msg_Err(p_enc, "Unable to set the channel mode: %s", aac_get_errorstring(erraac));
309         goto error;
310     }
311     if ((erraac = aacEncoder_SetParam(p_sys->handle, AACENC_CHANNELORDER, CH_ORDER_WG4)) != AACENC_OK) {
312         msg_Err(p_enc, "Unable to set the sound channel order: %s", aac_get_errorstring(erraac));
313         goto error;
314     }
315     if (p_sys->i_vbr != 0) {
316         if ((erraac = aacEncoder_SetParam(p_sys->handle,
317                          AACENC_BITRATEMODE, p_sys->i_vbr)) != AACENC_OK) {
318             msg_Err(p_enc, "Unable to set the VBR bitrate mode: %s", aac_get_errorstring(erraac));
319             goto error;
320         }
321     } else {
322         if (p_enc->fmt_out.i_bitrate == 0) {
323             if (p_sys->i_aot == PROFILE_AAC_HE_v2) {
324                 sce = 1;
325                 cpe = 0;
326             }
327             i_bitrate = (96*sce + 128*cpe) * p_enc->fmt_out.audio.i_rate / 44;
328             if (p_sys->i_aot == PROFILE_AAC_HE ||
329                 p_sys->i_aot == PROFILE_AAC_HE_v2 ||
330                 p_sys->b_eld_sbr)
331                 i_bitrate /= 2;
332             p_enc->fmt_out.i_bitrate = i_bitrate;
333             msg_Info(p_enc, "Setting optimal bitrate of %i", i_bitrate);
334         }
335         else
336         {
337             i_bitrate = p_enc->fmt_out.i_bitrate;
338         }
339         if ((erraac = aacEncoder_SetParam(p_sys->handle, AACENC_BITRATE,
340                                          i_bitrate)) != AACENC_OK) {
341             msg_Err(p_enc, "Unable to set the bitrate %i: %s", i_bitrate,
342                     aac_get_errorstring(erraac));
343             goto error;
344         }
345     }
346     if ((erraac = aacEncoder_SetParam(p_sys->handle, AACENC_TRANSMUX, 0)) != AACENC_OK) {
347         msg_Err(p_enc, "Unable to set the ADTS transmux: %s", aac_get_errorstring(erraac));
348         goto error;
349     }
350     if ((erraac = aacEncoder_SetParam(p_sys->handle, AACENC_SIGNALING_MODE,
351                                  (int)p_sys->i_signaling)) != AACENC_OK) {
352       /* use explicit backward compatible =1 */
353       /* use explicit hierarchical signaling =2 */
354         msg_Err(p_enc, "Unable to set signaling mode: %s", aac_get_errorstring(erraac));
355         goto error;
356     }
357     if ((erraac = aacEncoder_SetParam(p_sys->handle, AACENC_AFTERBURNER,
358                                (int)p_sys->b_afterburner)) != AACENC_OK) {
359         msg_Err(p_enc, "Unable to set the afterburner mode: %s", aac_get_errorstring(erraac));
360         goto error;
361     }
362     if ((erraac = aacEncEncode(p_sys->handle, NULL, NULL, NULL, NULL)) != AACENC_OK) {
363         msg_Err(p_enc, "Unable to initialize the encoder: %s", aac_get_errorstring(erraac));
364         goto error;
365     }
366     AACENC_InfoStruct info = { 0 };
367     if ((erraac = aacEncInfo(p_sys->handle, &info)) != AACENC_OK) {
368         msg_Err(p_enc, "Unable to get the encoder info: %s", aac_get_errorstring(erraac));
369         goto error;
370     }
371
372     /* The maximum packet size is 6144 bits aka 768 bytes per channel. */
373     p_sys->i_maxoutputsize = 768*p_enc->fmt_in.audio.i_channels;
374     p_enc->fmt_in.audio.i_bitspersample = 16;
375     p_sys->i_frame_size = info.frameLength;
376     p_sys->i_encoderdelay = info.encoderDelay;
377
378     p_enc->fmt_out.i_extra = info.confSize;
379     if( p_enc->fmt_out.i_extra )
380     {
381         p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
382         if ( p_enc->fmt_out.p_extra == NULL )
383         {
384             msg_Err(p_enc, "Unable to allocate fmt_out.p_extra");
385             goto error;
386         }
387         memcpy( p_enc->fmt_out.p_extra, info.confBuf,
388                 p_enc->fmt_out.i_extra );
389     }
390
391     p_enc->pf_encode_audio = EncodeAudio;
392
393 #ifndef NDEBUG
394     // TODO: Add more debug info to this config printout
395     msg_Dbg(p_enc, "fmt_out.p_extra = %i", p_enc->fmt_out.i_extra);
396 #endif
397
398     return VLC_SUCCESS;
399
400 error:
401     aacEncClose(&p_sys->handle);
402     free( p_sys );
403     return VLC_EGENERIC;
404 }
405
406 /****************************************************************************
407  * EncodeAudio: the whole thing
408  ****************************************************************************/
409 static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_aout_buf )
410 {
411     encoder_sys_t *p_sys;
412     int16_t *p_buffer;
413     int i_samples;
414     int i_loop_count;
415     int i_samples_left;
416     mtime_t i_pts_out;
417     block_t *p_chain;
418     AACENC_ERROR erraac;
419
420     p_sys = p_enc->p_sys;
421     p_chain = NULL;
422
423     if ( likely( p_aout_buf ) )
424     {
425         p_buffer = (int16_t *)p_aout_buf->p_buffer;
426         i_samples = p_aout_buf->i_nb_samples;
427         i_pts_out = p_aout_buf->i_pts - (mtime_t)((double)CLOCK_FREQ *
428                (double)p_sys->i_encoderdelay /
429                (double)p_enc->fmt_out.audio.i_rate);
430         if (p_sys->i_pts_last == 0)
431             p_sys->i_pts_last = i_pts_out - (mtime_t)((double)CLOCK_FREQ *
432                (double)(p_sys->i_frame_size) /
433                (double)p_enc->fmt_out.audio.i_rate);
434     }
435     else
436     {
437         i_samples = 0;
438         i_pts_out = p_sys->i_pts_last;
439     }
440
441     i_samples_left = i_samples;
442     i_loop_count = 0;
443
444     while ( i_samples_left >= 0 )
445     {
446         AACENC_BufDesc in_buf = { 0 }, out_buf = { 0 };
447         AACENC_InArgs in_args = { 0 };
448         AACENC_OutArgs out_args = { 0 };
449         int in_identifier = IN_AUDIO_DATA;
450         int in_size, in_elem_size;
451         int out_identifier = OUT_BITSTREAM_DATA;
452         int out_size, out_elem_size;
453         void *in_ptr, *out_ptr;
454
455         if ( unlikely(i_samples == 0) ) {
456             // this forces the encoder to purge whatever is left in the internal buffer
457             in_args.numInSamples = -1;
458         } else {
459             in_ptr = p_buffer + (i_samples - i_samples_left)*p_enc->fmt_in.audio.i_channels;
460             in_size = 2*p_enc->fmt_in.audio.i_channels*i_samples_left;
461             in_elem_size = 2;
462             in_args.numInSamples = p_enc->fmt_in.audio.i_channels*i_samples_left;
463             in_buf.numBufs = 1;
464             in_buf.bufs = &in_ptr;
465             in_buf.bufferIdentifiers = &in_identifier;
466             in_buf.bufSizes = &in_size;
467             in_buf.bufElSizes = &in_elem_size;
468         }
469         block_t *p_block;
470         p_block = block_Alloc( p_sys->i_maxoutputsize );
471         p_block->i_buffer = p_sys->i_maxoutputsize;
472         out_ptr = p_block->p_buffer;
473         out_size = p_block->i_buffer;
474         out_elem_size = 1;
475         out_buf.numBufs = 1;
476         out_buf.bufs = &out_ptr;
477         out_buf.bufferIdentifiers = &out_identifier;
478         out_buf.bufSizes = &out_size;
479         out_buf.bufElSizes = &out_elem_size;
480
481         if ((erraac = aacEncEncode(p_sys->handle, &in_buf, &out_buf, &in_args, &out_args)) != AACENC_OK) {
482             if (erraac == AACENC_ENCODE_EOF) {
483                 msg_Info( p_enc, "Encoding final bytes (EOF)");
484             }
485             else
486             {
487                 msg_Err( p_enc, "Encoding failed: %s", aac_get_errorstring(erraac));
488                 block_Release(p_block);
489                 break;
490             }
491         }
492         if ( out_args.numOutBytes > 0 )
493         {
494             p_block->i_buffer = out_args.numOutBytes;
495             if ( unlikely(i_samples == 0) )
496             {
497                 // I only have the numOutBytes so approximate based on compression factor
498                 double d_samples_forward = p_sys->d_compression_ratio*(double)out_args.numOutBytes;
499                 i_pts_out += (mtime_t)d_samples_forward;
500                 p_block->i_length = (mtime_t)d_samples_forward;
501                 // TODO: It would be more precise (a few microseconds) to use d_samples_forward =
502                 // (mtime_t)CLOCK_FREQ * (mtime_t)p_sys->i_frame_size/(mtime_t)p_enc->fmt_out.audio.i_rate
503                 // but I am not sure if the lib always outputs a full frame when
504                 // emptying the internal buffer in the EOF scenario
505             }
506             else
507             {
508                 if ( i_loop_count == 0 )
509                 {
510                     // There can be an implicit delay in the first loop cycle because leftover bytes
511                     // in the library buffer from the prior block
512                     double d_samples_delay = (double)p_sys->i_frame_size - (double)out_args.numInSamples /
513                                              (double)p_enc->fmt_in.audio.i_channels;
514                     i_pts_out -= (mtime_t)((double)CLOCK_FREQ * d_samples_delay /
515                                            (double)p_enc->fmt_out.audio.i_rate);
516                     p_block->i_length = (mtime_t)((double)CLOCK_FREQ * (double)p_sys->i_frame_size /
517                         (double)p_enc->fmt_out.audio.i_rate);
518                     p_block->i_nb_samples = d_samples_delay;
519                     //p_block->i_length = i_pts_out - p_sys->i_pts_last;
520                 }
521                 else
522                 {
523                     double d_samples_forward = (double)out_args.numInSamples/(double)p_enc->fmt_in.audio.i_channels;
524                     double d_length = ((double)CLOCK_FREQ * d_samples_forward /
525                                             (double)p_enc->fmt_out.audio.i_rate);
526                     i_pts_out += (mtime_t) d_length;
527                     p_block->i_length = (mtime_t) d_length;
528                     p_block->i_nb_samples = d_samples_forward;
529                 }
530             }
531             p_block->i_dts = p_block->i_pts = i_pts_out;
532             block_ChainAppend( &p_chain, p_block );
533 #if 0
534             msg_Dbg( p_enc, "dts %"PRId64", length %"PRId64", " "pts_last "
535                             "%"PRId64" numOutBytes = %i, numInSamples = %i, "
536                             "i_samples %i, i_loop_count %i",
537                               p_block->i_dts, p_block->i_length,
538                               p_sys->i_pts_last, out_args.numOutBytes,
539                               out_args.numInSamples, i_samples, i_loop_count);
540 #endif
541             if ( likely(i_samples > 0) )
542             {
543                 p_sys->d_compression_ratio = (double)p_block->i_length / (double)out_args.numOutBytes;
544                 i_samples_left -= out_args.numInSamples/p_enc->fmt_in.audio.i_channels;
545                 p_sys->i_pts_last = i_pts_out;
546             }
547         }
548         else
549         {
550             block_Release(p_block);
551             //msg_Dbg( p_enc, "aac_encode_audio: not enough data yet");
552             break;
553         }
554         if ( unlikely(i_loop_count++ > 100) )
555         {
556             msg_Err( p_enc, "Loop count greater than 100!!!, something must be wrong with the encoder library");
557             break;
558         }
559     }
560
561     return p_chain;
562
563 }
564
565 /*****************************************************************************
566  * CloseDecoder: decoder destruction
567  *****************************************************************************/
568 static void CloseEncoder( vlc_object_t *p_this )
569 {
570     encoder_t *p_enc = (encoder_t *)p_this;
571     encoder_sys_t *p_sys = p_enc->p_sys;
572
573     aacEncClose(&p_sys->handle);
574
575     free( p_sys );
576 }