OSDN Git Service

import 0.9.3
[handbrake-jp/handbrake-jp.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     unsigned long   input_samples;
32     unsigned long   output_bytes;
33     uint8_t       * buf;
34
35     hb_list_t     * list;
36     int64_t         pts;
37 };
38
39 int enclameInit( hb_work_object_t * w, hb_job_t * job )
40 {
41     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
42     hb_audio_t * audio = w->audio;
43     w->private_data = pv;
44
45     pv->job   = job;
46
47     hb_log( "enclame: opening libmp3lame" );
48
49     pv->lame = lame_init();
50     lame_set_brate( pv->lame, audio->config.out.bitrate );
51     lame_set_in_samplerate( pv->lame, audio->config.out.samplerate );
52     lame_set_out_samplerate( pv->lame, audio->config.out.samplerate );
53     lame_init_params( pv->lame );
54
55     pv->input_samples = 1152 * 2;
56     pv->output_bytes = LAME_MAXMP3BUFFER;
57     pv->buf  = malloc( pv->input_samples * sizeof( float ) );
58
59     pv->list = hb_list_init();
60     pv->pts  = -1;
61
62     return 0;
63 }
64
65 /***********************************************************************
66  * Close
67  ***********************************************************************
68  *
69  **********************************************************************/
70 void enclameClose( hb_work_object_t * w )
71 {
72     hb_work_private_t * pv = w->private_data;
73
74     lame_close( pv->lame );
75     hb_list_empty( &pv->list );
76     free( pv->buf );
77     free( pv );
78     w->private_data = NULL;
79 }
80
81 /***********************************************************************
82  * Encode
83  ***********************************************************************
84  *
85  **********************************************************************/
86 static hb_buffer_t * Encode( hb_work_object_t * w )
87 {
88     hb_work_private_t * pv = w->private_data;
89     hb_audio_t * audio = w->audio;
90     hb_buffer_t * buf;
91     int16_t samples_s16[1152 * 2];
92     uint64_t pts, pos;
93         int      i;
94
95     if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
96     {
97         return NULL;
98     }
99
100     hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
101                       &pts, &pos);
102
103     for( i = 0; i < 1152 * 2; i++ )
104     {
105         samples_s16[i] = ((float*) pv->buf)[i];
106     }
107
108     buf        = hb_buffer_init( pv->output_bytes );
109     buf->start = pts + 90000 * pos / 2 / sizeof( float ) / audio->config.out.samplerate;
110     buf->stop  = buf->start + 90000 * 1152 / audio->config.out.samplerate;
111     buf->size  = lame_encode_buffer_interleaved( pv->lame, samples_s16,
112             1152, buf->data, LAME_MAXMP3BUFFER );
113     buf->frametype   = HB_FRAME_AUDIO;
114
115     if( !buf->size )
116     {
117         /* Encoding was successful but we got no data. Try to encode
118            more */
119         hb_buffer_close( &buf );
120         return Encode( w );
121     }
122     else if( buf->size < 0 )
123     {
124         hb_log( "enclame: lame_encode_buffer failed" );
125         hb_buffer_close( &buf );
126         return NULL;
127     }
128
129     return buf;
130 }
131
132 /***********************************************************************
133  * Work
134  ***********************************************************************
135  *
136  **********************************************************************/
137 int enclameWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
138                  hb_buffer_t ** buf_out )
139 {
140     hb_work_private_t * pv = w->private_data;
141     hb_buffer_t * buf;
142
143     if ( (*buf_in)->size <= 0 )
144     {
145         /* EOF on input - send it downstream & say we're done */
146         *buf_out = *buf_in;
147         *buf_in = NULL;
148        return HB_WORK_DONE;
149     }
150
151     hb_list_add( pv->list, *buf_in );
152     *buf_in = NULL;
153
154     *buf_out = buf = Encode( w );
155
156     while( buf )
157     {
158         buf->next = Encode( w );
159         buf       = buf->next;
160     }
161
162     return HB_WORK_OK;
163 }
164