OSDN Git Service

a3f4a388282233ef5ffb06757b3e69369f7386e2
[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             done;
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     w->private_data = pv;
45
46     pv->job   = job;
47
48     hb_log( "enclame: opening libmp3lame" );
49
50     pv->lame = lame_init();
51     // use ABR
52     lame_set_VBR( pv->lame, vbr_abr );
53     lame_set_VBR_mean_bitrate_kbps( pv->lame, audio->config.out.bitrate );
54     lame_set_in_samplerate( pv->lame, audio->config.out.samplerate );
55     lame_set_out_samplerate( pv->lame, audio->config.out.samplerate );
56     lame_init_params( pv->lame );
57     // Lame's default encoding mode is JOINT_STEREO.  This subtracts signal
58     // that is "common" to left and right (within some threshold) and encodes
59     // it separately.  This improves quality at low bitrates, but hurts 
60     // imaging (channel separation) at higher bitrates.  So if the bitrate
61     // is suffeciently high, use regular STEREO mode.
62     if ( audio->config.out.bitrate >= 128 )
63         lame_set_mode( pv->lame, STEREO );
64
65     pv->input_samples = 1152 * 2;
66     pv->output_bytes = LAME_MAXMP3BUFFER;
67     pv->buf  = malloc( pv->input_samples * sizeof( float ) );
68
69     pv->list = hb_list_init();
70     pv->pts  = -1;
71
72     return 0;
73 }
74
75 /***********************************************************************
76  * Close
77  ***********************************************************************
78  *
79  **********************************************************************/
80 void enclameClose( hb_work_object_t * w )
81 {
82     hb_work_private_t * pv = w->private_data;
83
84     lame_close( pv->lame );
85     hb_list_empty( &pv->list );
86     free( pv->buf );
87     free( pv );
88     w->private_data = NULL;
89 }
90
91 /***********************************************************************
92  * Encode
93  ***********************************************************************
94  *
95  **********************************************************************/
96 static hb_buffer_t * Encode( hb_work_object_t * w )
97 {
98     hb_work_private_t * pv = w->private_data;
99     hb_audio_t * audio = w->audio;
100     hb_buffer_t * buf;
101     int16_t samples_s16[1152 * 2];
102     uint64_t pts, pos;
103         int      i;
104
105     if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
106     {
107         return NULL;
108     }
109
110     hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
111                       &pts, &pos);
112
113     for( i = 0; i < 1152 * 2; i++ )
114     {
115         samples_s16[i] = ((float*) pv->buf)[i];
116     }
117
118     buf        = hb_buffer_init( pv->output_bytes );
119     buf->start = pts + 90000 * pos / 2 / sizeof( float ) / audio->config.out.samplerate;
120     buf->stop  = buf->start + 90000 * 1152 / audio->config.out.samplerate;
121     buf->size  = lame_encode_buffer_interleaved( pv->lame, samples_s16,
122             1152, buf->data, LAME_MAXMP3BUFFER );
123     buf->frametype   = HB_FRAME_AUDIO;
124
125     if( !buf->size )
126     {
127         /* Encoding was successful but we got no data. Try to encode
128            more */
129         hb_buffer_close( &buf );
130         return Encode( w );
131     }
132     else if( buf->size < 0 )
133     {
134         hb_log( "enclame: lame_encode_buffer failed" );
135         hb_buffer_close( &buf );
136         return NULL;
137     }
138
139     return buf;
140 }
141
142 /***********************************************************************
143  * Work
144  ***********************************************************************
145  *
146  **********************************************************************/
147 int enclameWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
148                  hb_buffer_t ** buf_out )
149 {
150     hb_work_private_t * pv = w->private_data;
151     hb_buffer_t * in = *buf_in;
152     hb_buffer_t * buf;
153
154     if ( (*buf_in)->size <= 0 )
155     {
156         /* EOF on input - send it downstream & say we're done */
157         if ( pv->done )
158         {
159             *buf_out = *buf_in;
160             *buf_in = NULL;
161             return HB_WORK_DONE;
162         }
163         else
164         {
165             pv->done = 1;
166             hb_fifo_push( w->fifo_in, in);
167             *buf_in = NULL;
168
169             buf = hb_buffer_init( pv->output_bytes );
170             buf->size = lame_encode_flush( pv->lame, buf->data, LAME_MAXMP3BUFFER );
171             if( buf->size <= 0 )
172             {
173                 hb_buffer_close( &buf );
174             }
175             *buf_out = buf;
176             return HB_WORK_OK;
177         }
178     }
179
180     hb_list_add( pv->list, *buf_in );
181     *buf_in = NULL;
182
183     *buf_out = buf = Encode( w );
184
185     while( buf )
186     {
187         buf->next = Encode( w );
188         buf       = buf->next;
189     }
190
191     return HB_WORK_OK;
192 }
193