WinGui: Fix another instance of the Caliburn vs Json.net sillyness where objects...
[HandBrake.git] / libhb / enclame.c
blob0efe219060fcd195b54213d93293c524ccee834d
1 /* enclame.c
3 Copyright (c) 2003-2015 HandBrake Team
4 This file is part of the HandBrake source code
5 Homepage: <http://handbrake.fr/>.
6 It may be used under the terms of the GNU General Public License v2.
7 For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
8 */
9 #include "hb.h"
11 #include "lame/lame.h"
13 int enclameInit( hb_work_object_t *, hb_job_t * );
14 int enclameWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
15 void enclameClose( hb_work_object_t * );
17 hb_work_object_t hb_enclame =
19 WORK_ENCLAME,
20 "MP3 encoder (libmp3lame)",
21 enclameInit,
22 enclameWork,
23 enclameClose
26 struct hb_work_private_s
28 hb_job_t * job;
30 /* LAME handle */
31 lame_global_flags * lame;
33 int out_discrete_channels;
34 unsigned long input_samples;
35 unsigned long output_bytes;
36 uint8_t * buf;
38 hb_list_t * list;
39 int64_t pts;
42 int enclameInit( hb_work_object_t * w, hb_job_t * job )
44 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
45 hb_audio_t * audio = w->audio;
47 w->private_data = pv;
49 pv->job = job;
51 hb_log( "enclame: opening libmp3lame" );
53 pv->lame = lame_init();
54 // use ABR
55 lame_set_scale( pv->lame, 32768.0 );
56 if( audio->config.out.compression_level >= 0 )
58 lame_set_quality( pv->lame, audio->config.out.compression_level );
60 if( audio->config.out.bitrate > 0 )
62 lame_set_VBR( pv->lame, vbr_abr );
63 lame_set_VBR_mean_bitrate_kbps( pv->lame, audio->config.out.bitrate );
65 else if( audio->config.out.quality >= 0 )
67 lame_set_brate( pv->lame, 0 );
68 lame_set_VBR( pv->lame, vbr_default );
69 lame_set_VBR_quality( pv->lame, audio->config.out.quality );
71 lame_set_in_samplerate( pv->lame, audio->config.out.samplerate );
72 lame_set_out_samplerate( pv->lame, audio->config.out.samplerate );
74 pv->out_discrete_channels = hb_mixdown_get_discrete_channel_count( audio->config.out.mixdown );
75 // Lame's default encoding mode is JOINT_STEREO. This subtracts signal
76 // that is "common" to left and right (within some threshold) and encodes
77 // it separately. This improves quality at low bitrates, but hurts
78 // imaging (channel separation) at higher bitrates. So if the bitrate
79 // is suffeciently high, use regular STEREO mode.
80 if ( pv->out_discrete_channels == 1 )
82 lame_set_mode( pv->lame, MONO );
83 lame_set_num_channels( pv->lame, 1 );
85 else if ( audio->config.out.bitrate >= 128 )
87 lame_set_mode( pv->lame, STEREO );
89 lame_init_params( pv->lame );
91 pv->input_samples = 1152 * pv->out_discrete_channels;
92 pv->output_bytes = LAME_MAXMP3BUFFER;
93 pv->buf = malloc( pv->input_samples * sizeof( float ) );
94 audio->config.out.samples_per_frame = 1152;
96 pv->list = hb_list_init();
97 pv->pts = AV_NOPTS_VALUE;
99 return 0;
102 /***********************************************************************
103 * Close
104 ***********************************************************************
106 **********************************************************************/
107 void enclameClose( hb_work_object_t * w )
109 hb_work_private_t * pv = w->private_data;
111 lame_close( pv->lame );
112 hb_list_empty( &pv->list );
113 free( pv->buf );
114 free( pv );
115 w->private_data = NULL;
118 /***********************************************************************
119 * Encode
120 ***********************************************************************
122 **********************************************************************/
123 static hb_buffer_t * Encode( hb_work_object_t * w )
125 hb_work_private_t * pv = w->private_data;
126 hb_audio_t * audio = w->audio;
127 hb_buffer_t * buf;
128 float samples[2][1152];
129 uint64_t pts, pos;
130 int i, j;
132 if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
134 return NULL;
137 hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
138 &pts, &pos);
140 for( i = 0; i < 1152; i++ )
142 for( j = 0; j < pv->out_discrete_channels; j++ )
144 samples[j][i] = ((float *) pv->buf)[(pv->out_discrete_channels * i + j)];
148 buf = hb_buffer_init( pv->output_bytes );
149 buf->s.start = pts + 90000 * pos / pv->out_discrete_channels / sizeof( float ) / audio->config.out.samplerate;
150 buf->s.duration = (double)90000 * 1152 / audio->config.out.samplerate;
151 buf->s.stop = buf->s.start + buf->s.duration;
152 pv->pts = buf->s.stop;
153 buf->size = lame_encode_buffer_float(
154 pv->lame, samples[0], samples[1],
155 1152, buf->data, LAME_MAXMP3BUFFER );
157 buf->s.type = AUDIO_BUF;
158 buf->s.frametype = HB_FRAME_AUDIO;
160 if( !buf->size )
162 /* Encoding was successful but we got no data. Try to encode
163 more */
164 hb_buffer_close( &buf );
165 return Encode( w );
167 else if( buf->size < 0 )
169 hb_log( "enclame: lame_encode_buffer failed" );
170 hb_buffer_close( &buf );
171 return NULL;
173 return buf;
176 /***********************************************************************
177 * Work
178 ***********************************************************************
180 **********************************************************************/
181 int enclameWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
182 hb_buffer_t ** buf_out )
184 hb_work_private_t * pv = w->private_data;
185 hb_audio_t * audio = w->audio;
186 hb_buffer_t * in = *buf_in;
187 hb_buffer_t * buf;
189 if (in->s.flags & HB_BUF_FLAG_EOF)
191 /* EOF on input - send it downstream & say we're done */
193 buf = hb_buffer_init( pv->output_bytes );
194 buf->size = lame_encode_flush( pv->lame, buf->data, LAME_MAXMP3BUFFER );
195 buf->s.start = pv->pts;
196 buf->s.stop = buf->s.start + 90000 * 1152 / audio->config.out.samplerate;
198 buf->s.type = AUDIO_BUF;
199 buf->s.frametype = HB_FRAME_AUDIO;
201 if( buf->size <= 0 )
203 hb_buffer_close( &buf );
206 // Add the flushed data
207 *buf_out = buf;
209 // Add the eof
210 if ( buf )
212 buf->next = in;
214 else
216 *buf_out = in;
219 *buf_in = NULL;
220 return HB_WORK_DONE;
223 hb_list_add( pv->list, *buf_in );
224 *buf_in = NULL;
226 *buf_out = buf = Encode( w );
228 while( buf )
230 buf->next = Encode( w );
231 buf = buf->next;
234 return HB_WORK_OK;