OSDN Git Service

Put the correct subtitle language in the Queue for the Mac GUI.
[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.m0k.org/>.
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     w->private_data = pv;
43
44     pv->job   = job;
45
46     hb_log( "enclame: opening libmp3lame" );
47
48     pv->lame = lame_init();
49     lame_set_brate( pv->lame, job->abitrate );
50     lame_set_in_samplerate( pv->lame, job->arate );
51     lame_set_out_samplerate( pv->lame, job->arate );
52     lame_init_params( pv->lame );
53     
54     pv->input_samples = 1152 * 2;
55     pv->output_bytes = LAME_MAXMP3BUFFER;
56     pv->buf  = malloc( pv->input_samples * sizeof( float ) );
57
58     pv->list = hb_list_init();
59     pv->pts  = -1;
60
61     return 0;
62 }
63
64 /***********************************************************************
65  * Close
66  ***********************************************************************
67  *
68  **********************************************************************/
69 void enclameClose( hb_work_object_t * w )
70 {
71     hb_work_private_t * pv = w->private_data;
72     
73     lame_close( pv->lame );
74     hb_list_empty( &pv->list );
75     free( pv->buf );
76     free( pv );
77     w->private_data = NULL;
78 }
79
80 /***********************************************************************
81  * Encode
82  ***********************************************************************
83  *
84  **********************************************************************/
85 static hb_buffer_t * Encode( hb_work_object_t * w )
86 {
87     hb_work_private_t * pv = w->private_data;
88     hb_buffer_t * buf;
89     int16_t samples_s16[1152 * 2];
90     uint64_t pts, pos;
91         int      i;
92
93     if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
94     {
95         return NULL;
96     }
97
98     hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
99                       &pts, &pos);
100
101     for( i = 0; i < 1152 * 2; i++ )
102     {
103         samples_s16[i] = ((float*) pv->buf)[i];
104     }
105
106     buf        = hb_buffer_init( pv->output_bytes );
107     buf->start = pts + 90000 * pos / 2 / sizeof( float ) / pv->job->arate;
108     buf->stop  = buf->start + 90000 * 1152 / pv->job->arate;
109     buf->size  = lame_encode_buffer_interleaved( pv->lame, samples_s16,
110             1152, buf->data, LAME_MAXMP3BUFFER );
111     buf->frametype   = HB_FRAME_AUDIO;
112
113     if( !buf->size )
114     {
115         /* Encoding was successful but we got no data. Try to encode
116            more */
117         hb_buffer_close( &buf );
118         return Encode( w );
119     }
120     else if( buf->size < 0 )
121     {
122         hb_log( "enclame: lame_encode_buffer failed" );
123         hb_buffer_close( &buf );
124         return NULL;
125     }
126
127     return buf;
128 }
129
130 /***********************************************************************
131  * Work
132  ***********************************************************************
133  *
134  **********************************************************************/
135 int enclameWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
136                  hb_buffer_t ** buf_out )
137 {
138     hb_work_private_t * pv = w->private_data;
139     hb_buffer_t * buf;
140
141     hb_list_add( pv->list, *buf_in );
142     *buf_in = NULL;
143
144     *buf_out = buf = Encode( w );
145
146     while( buf )
147     {
148         buf->next = Encode( w );
149         buf       = buf->next;
150     }
151
152     return HB_WORK_OK;
153 }
154