1 /* $Id: encfaac.c,v 1.13 2005/03/03 17:21:57 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.m0k.org/>.
5 It may be used under the terms of the GNU General Public License. */
11 struct hb_work_object_s
19 unsigned long input_samples;
20 unsigned long output_bytes;
27 /***********************************************************************
29 **********************************************************************/
30 static void Close( hb_work_object_t ** _w );
31 static int Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
32 hb_buffer_t ** buf_out );
34 /***********************************************************************
35 * hb_work_encfaac_init
36 ***********************************************************************
38 **********************************************************************/
39 hb_work_object_t * hb_work_encfaac_init( hb_job_t * job, hb_audio_t * audio )
41 hb_work_object_t * w = calloc( sizeof( hb_work_object_t ), 1 );
42 faacEncConfigurationPtr cfg;
43 w->name = strdup( "AAC encoder (libfaac)" );
50 w->faac = faacEncOpen( job->arate, 2, &w->input_samples,
52 w->buf = malloc( w->input_samples * sizeof( float ) );
54 cfg = faacEncGetCurrentConfiguration( w->faac );
55 cfg->mpegVersion = MPEG4;
56 cfg->aacObjectType = LOW;
57 cfg->allowMidside = 1;
60 cfg->bitRate = job->abitrate * 500; /* Per channel */
62 cfg->outputFormat = 0;
63 cfg->inputFormat = FAAC_INPUT_FLOAT;
64 if( !faacEncSetConfiguration( w->faac, cfg ) )
66 hb_log( "faacEncSetConfiguration failed" );
68 if( faacEncGetDecoderSpecificInfo( w->faac, &audio->config.faac.decinfo,
69 &audio->config.faac.size ) < 0 )
71 hb_log( "faacEncGetDecoderSpecificInfo failed" );
74 w->list = hb_list_init();
80 /***********************************************************************
82 ***********************************************************************
84 **********************************************************************/
85 static void Close( hb_work_object_t ** _w )
87 hb_work_object_t * w = *_w;
89 faacEncClose( w->faac );
91 hb_list_empty( &w->list );
92 free( w->audio->config.faac.decinfo );
99 /***********************************************************************
101 ***********************************************************************
103 **********************************************************************/
104 static hb_buffer_t * Encode( hb_work_object_t * w )
110 if( hb_list_bytes( w->list ) < w->input_samples * sizeof( float ) )
116 hb_list_getbytes( w->list, w->buf, w->input_samples * sizeof( float ),
119 buf = hb_buffer_init( w->output_bytes );
120 buf->start = pts + 90000 * pos / 2 / sizeof( float ) / w->job->arate;
121 buf->stop = buf->start + 90000 * w->input_samples / w->job->arate / 2;
122 buf->size = faacEncEncode( w->faac, (int32_t *) w->buf,
123 w->input_samples, buf->data, w->output_bytes );
128 /* Encoding was successful but we got no data. Try to encode
130 hb_buffer_close( &buf );
133 else if( buf->size < 0 )
135 hb_log( "faacEncEncode failed" );
136 hb_buffer_close( &buf );
143 /***********************************************************************
145 ***********************************************************************
147 **********************************************************************/
148 static int Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
149 hb_buffer_t ** buf_out )
153 hb_list_add( w->list, *buf_in );
156 *buf_out = buf = Encode( w );
160 buf->next = Encode( w );