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_private_s
16 unsigned long input_samples;
17 unsigned long output_bytes;
24 int encfaacInit( hb_work_object_t *, hb_job_t * );
25 int encfaacWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
26 void encfaacClose( hb_work_object_t * );
28 hb_work_object_t hb_encfaac =
31 "AAC encoder (libfaac)",
37 /***********************************************************************
38 * hb_work_encfaac_init
39 ***********************************************************************
41 **********************************************************************/
42 int encfaacInit( hb_work_object_t * w, hb_job_t * job )
44 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
45 faacEncConfigurationPtr cfg;
53 pv->faac = faacEncOpen( job->arate, 2, &pv->input_samples,
55 pv->buf = malloc( pv->input_samples * sizeof( float ) );
57 cfg = faacEncGetCurrentConfiguration( pv->faac );
58 cfg->mpegVersion = MPEG4;
59 cfg->aacObjectType = LOW;
60 cfg->allowMidside = 1;
63 cfg->bitRate = job->abitrate * 500; /* Per channel */
65 cfg->outputFormat = 0;
66 cfg->inputFormat = FAAC_INPUT_FLOAT;
67 if( !faacEncSetConfiguration( pv->faac, cfg ) )
69 hb_log( "faacEncSetConfiguration failed" );
72 if( faacEncGetDecoderSpecificInfo( pv->faac, &bytes, &length ) < 0 )
74 hb_log( "faacEncGetDecoderSpecificInfo failed" );
76 memcpy( w->config->aac.bytes, bytes, length );
77 w->config->aac.length = length;
80 pv->list = hb_list_init();
86 /***********************************************************************
88 ***********************************************************************
90 **********************************************************************/
91 void encfaacClose( hb_work_object_t * w )
93 hb_work_private_t * pv = w->private_data;
94 faacEncClose( pv->faac );
96 hb_list_empty( &pv->list );
99 /***********************************************************************
101 ***********************************************************************
103 **********************************************************************/
104 static hb_buffer_t * Encode( hb_work_object_t * w )
106 hb_work_private_t * pv = w->private_data;
111 if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
117 hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
120 buf = hb_buffer_init( pv->output_bytes );
121 buf->start = pts + 90000 * pos / 2 / sizeof( float ) / pv->job->arate;
122 buf->stop = buf->start + 90000 * pv->input_samples / pv->job->arate / 2;
123 buf->size = faacEncEncode( pv->faac, (int32_t *) pv->buf,
124 pv->input_samples, buf->data, pv->output_bytes );
129 /* Encoding was successful but we got no data. Try to encode
131 hb_buffer_close( &buf );
134 else if( buf->size < 0 )
136 hb_log( "faacEncEncode failed" );
137 hb_buffer_close( &buf );
144 /***********************************************************************
146 ***********************************************************************
148 **********************************************************************/
149 int encfaacWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
150 hb_buffer_t ** buf_out )
152 hb_work_private_t * pv = w->private_data;
155 hb_list_add( pv->list, *buf_in );
158 *buf_out = buf = Encode( w );
162 buf->next = Encode( w );