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
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
=
20 "MP3 encoder (libmp3lame)",
26 struct hb_work_private_s
31 lame_global_flags
* lame
;
33 int out_discrete_channels
;
34 unsigned long input_samples
;
35 unsigned long output_bytes
;
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
;
51 hb_log( "enclame: opening libmp3lame" );
53 pv
->lame
= lame_init();
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
;
102 /***********************************************************************
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
);
115 w
->private_data
= NULL
;
118 /***********************************************************************
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
;
128 float samples
[2][1152];
132 if( hb_list_bytes( pv
->list
) < pv
->input_samples
* sizeof( float ) )
137 hb_list_getbytes( pv
->list
, pv
->buf
, pv
->input_samples
* sizeof( float ),
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
;
162 /* Encoding was successful but we got no data. Try to encode
164 hb_buffer_close( &buf
);
167 else if( buf
->size
< 0 )
169 hb_log( "enclame: lame_encode_buffer failed" );
170 hb_buffer_close( &buf
);
176 /***********************************************************************
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
;
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
;
203 hb_buffer_close( &buf
);
206 // Add the flushed data
223 hb_list_add( pv
->list
, *buf_in
);
226 *buf_out
= buf
= Encode( w
);
230 buf
->next
= Encode( w
);