OSDN Git Service

LinGui: missed removing a part of target file size code
[handbrake-jp/handbrake-jp-git.git] / libhb / enclame.c
1 /* $Id: enclame.c,v 1.9 2005/03/05 14:27:05 titer Exp $
2
3    This file is part of the HandBrake source code.
4    Homepage: <http://handbrake.fr/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include "hb.h"
8
9 #include "lame/lame.h"
10
11 int  enclameInit( hb_work_object_t *, hb_job_t * );
12 int  enclameWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
13 void enclameClose( hb_work_object_t * );
14
15 hb_work_object_t hb_enclame =
16 {
17     WORK_ENCLAME,
18     "MP3 encoder (libmp3lame)",
19     enclameInit,
20     enclameWork,
21     enclameClose
22 };
23
24 struct hb_work_private_s
25 {
26     hb_job_t   * job;
27
28     /* LAME handle */
29     lame_global_flags * lame;
30
31     int             out_discrete_channels;
32     unsigned long   input_samples;
33     unsigned long   output_bytes;
34     uint8_t       * buf;
35
36     hb_list_t     * list;
37     int64_t         pts;
38 };
39
40 int enclameInit( hb_work_object_t * w, hb_job_t * job )
41 {
42     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
43     hb_audio_t * audio = w->audio;
44
45     w->private_data = pv;
46
47     pv->job   = job;
48
49     hb_log( "enclame: opening libmp3lame" );
50
51     pv->lame = lame_init();
52     // use ABR
53     lame_set_VBR( pv->lame, vbr_abr );
54     lame_set_VBR_mean_bitrate_kbps( pv->lame, audio->config.out.bitrate );
55     lame_set_in_samplerate( pv->lame, audio->config.out.samplerate );
56     lame_set_out_samplerate( pv->lame, audio->config.out.samplerate );
57
58     pv->out_discrete_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
59     // Lame's default encoding mode is JOINT_STEREO.  This subtracts signal
60     // that is "common" to left and right (within some threshold) and encodes
61     // it separately.  This improves quality at low bitrates, but hurts 
62     // imaging (channel separation) at higher bitrates.  So if the bitrate
63     // is suffeciently high, use regular STEREO mode.
64     if ( pv->out_discrete_channels == 1 )
65     {
66         lame_set_mode( pv->lame, MONO );
67         lame_set_num_channels( pv->lame, 1 );
68     }
69     else if ( audio->config.out.bitrate >= 128 )
70     {
71         lame_set_mode( pv->lame, STEREO );
72     }
73     lame_init_params( pv->lame );
74
75     pv->input_samples = 1152 * pv->out_discrete_channels;
76     pv->output_bytes = LAME_MAXMP3BUFFER;
77     pv->buf  = malloc( pv->input_samples * sizeof( float ) );
78
79     pv->list = hb_list_init();
80     pv->pts  = -1;
81
82     return 0;
83 }
84
85 /***********************************************************************
86  * Close
87  ***********************************************************************
88  *
89  **********************************************************************/
90 void enclameClose( hb_work_object_t * w )
91 {
92     hb_work_private_t * pv = w->private_data;
93
94     lame_close( pv->lame );
95     hb_list_empty( &pv->list );
96     free( pv->buf );
97     free( pv );
98     w->private_data = NULL;
99 }
100
101 /***********************************************************************
102  * Encode
103  ***********************************************************************
104  *
105  **********************************************************************/
106 static hb_buffer_t * Encode( hb_work_object_t * w )
107 {
108     hb_work_private_t * pv = w->private_data;
109     hb_audio_t * audio = w->audio;
110     hb_buffer_t * buf;
111     float samples[2][1152];
112     uint64_t pts, pos;
113     int      i, j;
114
115     if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
116     {
117         return NULL;
118     }
119
120     hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
121                       &pts, &pos);
122
123     memset(samples, 0, 2*1152*sizeof(float));
124     for( i = 0; i < 1152; i++ )
125     {
126         for( j = 0; j < pv->out_discrete_channels; j++ )
127         {
128             samples[j][i] = ((float *) pv->buf)[(pv->out_discrete_channels * i + j)];
129         }
130     }
131
132     buf        = hb_buffer_init( pv->output_bytes );
133     buf->start = pts + 90000 * pos / pv->out_discrete_channels / sizeof( float ) / audio->config.out.samplerate;
134     buf->stop  = buf->start + 90000 * 1152 / audio->config.out.samplerate;
135     pv->pts = buf->stop;
136     buf->size  = lame_encode_buffer_float( 
137             pv->lame, samples[0], samples[1],
138             1152, buf->data, LAME_MAXMP3BUFFER );
139
140     buf->frametype   = HB_FRAME_AUDIO;
141
142     if( !buf->size )
143     {
144         /* Encoding was successful but we got no data. Try to encode
145            more */
146         hb_buffer_close( &buf );
147         return Encode( w );
148     }
149     else if( buf->size < 0 )
150     {
151         hb_log( "enclame: lame_encode_buffer failed" );
152         hb_buffer_close( &buf );
153         return NULL;
154     }
155     return buf;
156 }
157
158 /***********************************************************************
159  * Work
160  ***********************************************************************
161  *
162  **********************************************************************/
163 int enclameWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
164                  hb_buffer_t ** buf_out )
165 {
166     hb_work_private_t * pv = w->private_data;
167     hb_audio_t * audio = w->audio;
168     hb_buffer_t * in = *buf_in;
169     hb_buffer_t * buf;
170
171     if ( (*buf_in)->size <= 0 )
172     {
173         /* EOF on input - send it downstream & say we're done */
174
175         buf = hb_buffer_init( pv->output_bytes );
176         buf->size = lame_encode_flush( pv->lame, buf->data, LAME_MAXMP3BUFFER );
177         buf->start = pv->pts;
178         buf->stop  = buf->start + 90000 * 1152 / audio->config.out.samplerate;
179         buf->frametype   = HB_FRAME_AUDIO;
180         if( buf->size <= 0 )
181         {
182             hb_buffer_close( &buf );
183         }
184
185         // Add the flushed data
186         *buf_out = buf;
187
188         // Add the eof
189         if ( buf )
190         {
191             buf->next = in;
192         }
193         else
194         {
195             *buf_out = in;
196         }
197
198         *buf_in = NULL;
199         return HB_WORK_DONE;
200     }
201
202     hb_list_add( pv->list, *buf_in );
203     *buf_in = NULL;
204
205     *buf_out = buf = Encode( w );
206
207     while( buf )
208     {
209         buf->next = Encode( w );
210         buf       = buf->next;
211     }
212
213     return HB_WORK_OK;
214 }
215