OSDN Git Service

Fix the release/developer detection in configure.py
[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     lame_set_brate( pv->lame, audio->config.out.bitrate );
52     lame_set_in_samplerate( pv->lame, audio->config.out.samplerate );
53     lame_set_out_samplerate( pv->lame, audio->config.out.samplerate );
54     lame_init_params( pv->lame );
55
56     pv->input_samples = 1152 * 2;
57     pv->output_bytes = LAME_MAXMP3BUFFER;
58     pv->buf  = malloc( pv->input_samples * sizeof( float ) );
59
60     pv->list = hb_list_init();
61     pv->pts  = -1;
62
63     return 0;
64 }
65
66 /***********************************************************************
67  * Close
68  ***********************************************************************
69  *
70  **********************************************************************/
71 void enclameClose( hb_work_object_t * w )
72 {
73     hb_work_private_t * pv = w->private_data;
74
75     lame_close( pv->lame );
76     hb_list_empty( &pv->list );
77     free( pv->buf );
78     free( pv );
79     w->private_data = NULL;
80 }
81
82 /***********************************************************************
83  * Encode
84  ***********************************************************************
85  *
86  **********************************************************************/
87 static hb_buffer_t * Encode( hb_work_object_t * w )
88 {
89     hb_work_private_t * pv = w->private_data;
90     hb_audio_t * audio = w->audio;
91     hb_buffer_t * buf;
92     int16_t samples_s16[1152 * 2];
93     uint64_t pts, pos;
94         int      i;
95
96     if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
97     {
98         return NULL;
99     }
100
101     hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
102                       &pts, &pos);
103
104     for( i = 0; i < 1152 * 2; i++ )
105     {
106         samples_s16[i] = ((float*) pv->buf)[i];
107     }
108
109     buf        = hb_buffer_init( pv->output_bytes );
110     buf->start = pts + 90000 * pos / 2 / sizeof( float ) / audio->config.out.samplerate;
111     buf->stop  = buf->start + 90000 * 1152 / audio->config.out.samplerate;
112     buf->size  = lame_encode_buffer_interleaved( pv->lame, samples_s16,
113             1152, buf->data, LAME_MAXMP3BUFFER );
114     buf->frametype   = HB_FRAME_AUDIO;
115
116     if( !buf->size )
117     {
118         /* Encoding was successful but we got no data. Try to encode
119            more */
120         hb_buffer_close( &buf );
121         return Encode( w );
122     }
123     else if( buf->size < 0 )
124     {
125         hb_log( "enclame: lame_encode_buffer failed" );
126         hb_buffer_close( &buf );
127         return NULL;
128     }
129
130     return buf;
131 }
132
133 /***********************************************************************
134  * Work
135  ***********************************************************************
136  *
137  **********************************************************************/
138 int enclameWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
139                  hb_buffer_t ** buf_out )
140 {
141     hb_work_private_t * pv = w->private_data;
142     hb_buffer_t * in = *buf_in;
143     hb_buffer_t * buf;
144
145     if ( (*buf_in)->size <= 0 )
146     {
147         /* EOF on input - send it downstream & say we're done */
148         if ( pv->done )
149         {
150             *buf_out = *buf_in;
151             *buf_in = NULL;
152             return HB_WORK_DONE;
153         }
154         else
155         {
156             pv->done = 1;
157             hb_fifo_push( w->fifo_in, in);
158             *buf_in = NULL;
159
160             buf = hb_buffer_init( pv->output_bytes );
161             buf->size = lame_encode_flush( pv->lame, buf->data, LAME_MAXMP3BUFFER );
162             if( buf->size <= 0 )
163             {
164                 hb_buffer_close( &buf );
165             }
166             *buf_out = buf;
167             return HB_WORK_OK;
168         }
169     }
170
171     hb_list_add( pv->list, *buf_in );
172     *buf_in = NULL;
173
174     *buf_out = buf = Encode( w );
175
176     while( buf )
177     {
178         buf->next = Encode( w );
179         buf       = buf->next;
180     }
181
182     return HB_WORK_OK;
183 }
184