OSDN Git Service

fix a crash when a TS has no aspect ratio set in the video stream
[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     int             out_discrete_channels;
33     unsigned long   input_samples;
34     unsigned long   output_bytes;
35     uint8_t       * buf;
36
37     hb_list_t     * list;
38     int64_t         pts;
39 };
40
41 int enclameInit( hb_work_object_t * w, hb_job_t * job )
42 {
43     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
44     hb_audio_t * audio = w->audio;
45
46     w->private_data = pv;
47
48     pv->job   = job;
49
50     hb_log( "enclame: opening libmp3lame" );
51
52     pv->lame = lame_init();
53     // use ABR
54     lame_set_VBR( pv->lame, vbr_abr );
55     lame_set_VBR_mean_bitrate_kbps( pv->lame, audio->config.out.bitrate );
56     lame_set_in_samplerate( pv->lame, audio->config.out.samplerate );
57     lame_set_out_samplerate( pv->lame, audio->config.out.samplerate );
58
59     pv->out_discrete_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
60     // Lame's default encoding mode is JOINT_STEREO.  This subtracts signal
61     // that is "common" to left and right (within some threshold) and encodes
62     // it separately.  This improves quality at low bitrates, but hurts 
63     // imaging (channel separation) at higher bitrates.  So if the bitrate
64     // is suffeciently high, use regular STEREO mode.
65     if ( pv->out_discrete_channels == 1 )
66     {
67         lame_set_mode( pv->lame, MONO );
68         lame_set_num_channels( pv->lame, 1 );
69     }
70     else if ( audio->config.out.bitrate >= 128 )
71     {
72         lame_set_mode( pv->lame, STEREO );
73     }
74     lame_init_params( pv->lame );
75
76     pv->input_samples = 1152 * pv->out_discrete_channels;
77     pv->output_bytes = LAME_MAXMP3BUFFER;
78     pv->buf  = malloc( pv->input_samples * sizeof( float ) );
79
80     pv->list = hb_list_init();
81     pv->pts  = -1;
82
83     return 0;
84 }
85
86 /***********************************************************************
87  * Close
88  ***********************************************************************
89  *
90  **********************************************************************/
91 void enclameClose( hb_work_object_t * w )
92 {
93     hb_work_private_t * pv = w->private_data;
94
95     lame_close( pv->lame );
96     hb_list_empty( &pv->list );
97     free( pv->buf );
98     free( pv );
99     w->private_data = NULL;
100 }
101
102 /***********************************************************************
103  * Encode
104  ***********************************************************************
105  *
106  **********************************************************************/
107 static hb_buffer_t * Encode( hb_work_object_t * w )
108 {
109     hb_work_private_t * pv = w->private_data;
110     hb_audio_t * audio = w->audio;
111     hb_buffer_t * buf;
112     float samples[2][1152];
113     uint64_t pts, pos;
114     int      i, j;
115
116     if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
117     {
118         return NULL;
119     }
120
121     hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
122                       &pts, &pos);
123
124     memset(samples, 0, 2*1152*sizeof(float));
125     for( i = 0; i < 1152; i++ )
126     {
127         for( j = 0; j < pv->out_discrete_channels; j++ )
128         {
129             samples[j][i] = ((float *) pv->buf)[(pv->out_discrete_channels * i + j)];
130         }
131     }
132
133     buf        = hb_buffer_init( pv->output_bytes );
134     buf->start = pts + 90000 * pos / pv->out_discrete_channels / sizeof( float ) / audio->config.out.samplerate;
135     buf->stop  = buf->start + 90000 * 1152 / audio->config.out.samplerate;
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
156     return buf;
157 }
158
159 /***********************************************************************
160  * Work
161  ***********************************************************************
162  *
163  **********************************************************************/
164 int enclameWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
165                  hb_buffer_t ** buf_out )
166 {
167     hb_work_private_t * pv = w->private_data;
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         if ( pv->done )
175         {
176             *buf_out = *buf_in;
177             *buf_in = NULL;
178             return HB_WORK_DONE;
179         }
180         else
181         {
182             pv->done = 1;
183             hb_fifo_push( w->fifo_in, in);
184             *buf_in = NULL;
185
186             buf = hb_buffer_init( pv->output_bytes );
187             buf->size = lame_encode_flush( pv->lame, buf->data, LAME_MAXMP3BUFFER );
188             if( buf->size <= 0 )
189             {
190                 hb_buffer_close( &buf );
191             }
192             *buf_out = buf;
193             return HB_WORK_OK;
194         }
195     }
196
197     hb_list_add( pv->list, *buf_in );
198     *buf_in = NULL;
199
200     *buf_out = buf = Encode( w );
201
202     while( buf )
203     {
204         buf->next = Encode( w );
205         buf       = buf->next;
206     }
207
208     return HB_WORK_OK;
209 }
210