OSDN Git Service

5c919c6b0c0921d8cb1e941822c329a776ab5d03
[handbrake-jp/handbrake-jp-git.git] / libhb / encvorbis.c
1 /* $Id: encvorbis.c,v 1.6 2005/03/05 15:08:32 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 "vorbis/vorbisenc.h"
10
11 #define OGGVORBIS_FRAME_SIZE 1024
12
13 int  encvorbisInit( hb_work_object_t *, hb_job_t * );
14 int  encvorbisWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
15 void encvorbisClose( hb_work_object_t * );
16
17 hb_work_object_t hb_encvorbis =
18 {
19     WORK_ENCVORBIS,
20     "Vorbis encoder (libvorbis)",
21     encvorbisInit,
22     encvorbisWork,
23     encvorbisClose
24 };
25
26 struct hb_work_private_s
27 {
28     hb_job_t   * job;
29
30     vorbis_info        vi;
31     vorbis_comment     vc;
32     vorbis_dsp_state   vd;
33     vorbis_block       vb;
34
35     unsigned long   input_samples;
36     uint8_t       * buf;
37     uint64_t        pts;
38
39     hb_list_t     * list;
40     int           out_discrete_channels;
41     int           channel_map[6];
42 };
43
44 int encvorbisInit( hb_work_object_t * w, hb_job_t * job )
45 {
46     int i;
47     ogg_packet header[3];
48     struct ovectl_ratemanage2_arg  ctl_rate_arg;
49
50     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
51     w->private_data = pv;
52     pv->out_discrete_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(w->amixdown);
53
54     pv->job   = job;
55
56     hb_log( "encvorbis: opening libvorbis" );
57
58     /* init */
59     vorbis_info_init( &pv->vi );
60     if( vorbis_encode_setup_managed( &pv->vi, pv->out_discrete_channels,
61           job->arate, -1, 1000 * job->abitrate, -1 ) )
62     {
63         hb_log( "encvorbis: vorbis_encode_setup_managed failed" );
64         *job->die = 1;
65         return 0;
66     }
67
68     if( vorbis_encode_ctl( &pv->vi, OV_ECTL_RATEMANAGE2_GET, &ctl_rate_arg) )
69     {
70         hb_log( "encvorbis: vorbis_encode_ctl( ratemanage2_get ) failed" );
71     }
72
73     ctl_rate_arg.bitrate_average_kbps = 1000 * job->abitrate;
74     ctl_rate_arg.managed = 1;
75
76     if( vorbis_encode_ctl( &pv->vi, OV_ECTL_RATEMANAGE2_SET, &ctl_rate_arg ) ||
77           vorbis_encode_setup_init( &pv->vi ) )
78     {
79         hb_log( "encvorbis: vorbis_encode_ctl( ratemanage2_set ) OR vorbis_encode_setup_init failed" );
80         *job->die = 1;
81         return 0;
82     }
83
84     /* add a comment */
85     vorbis_comment_init( &pv->vc );
86     vorbis_comment_add_tag( &pv->vc, "Encoder", "HandBrake");
87     vorbis_comment_add_tag( &pv->vc, "LANGUAGE", w->config->vorbis.language);
88
89     /* set up the analysis state and auxiliary encoding storage */
90     vorbis_analysis_init( &pv->vd, &pv->vi);
91     vorbis_block_init( &pv->vd, &pv->vb);
92
93     /* get the 3 headers */
94     vorbis_analysis_headerout( &pv->vd, &pv->vc,
95                                &header[0], &header[1], &header[2] );
96     for( i = 0; i < 3; i++ )
97     {
98         memcpy( w->config->vorbis.headers[i], &header[i],
99                 sizeof( ogg_packet ) );
100         memcpy( w->config->vorbis.headers[i] + sizeof( ogg_packet ),
101                 header[i].packet, header[i].bytes );
102     }
103
104     pv->input_samples = pv->out_discrete_channels * OGGVORBIS_FRAME_SIZE;
105     pv->buf = malloc( pv->input_samples * sizeof( float ) );
106
107     pv->list = hb_list_init();
108
109     switch (pv->out_discrete_channels) {
110         case 1:
111             pv->channel_map[0] = 0;
112             break;
113         case 6:
114             pv->channel_map[0] = 0;
115             pv->channel_map[1] = 2;
116             pv->channel_map[2] = 1;
117             pv->channel_map[3] = 4;
118             pv->channel_map[4] = 5;
119             pv->channel_map[5] = 3;
120             break;
121         default:
122             hb_log("encvorbis.c: Unable to correctly proccess %d channels, assuming stereo.", pv->out_discrete_channels);
123         case 2:
124             // Assume stereo
125             pv->channel_map[0] = 0;
126             pv->channel_map[1] = 1;
127             break;
128     }
129
130     return 0;
131 }
132
133 /***********************************************************************
134  * Close
135  ***********************************************************************
136  *
137  **********************************************************************/
138 void encvorbisClose( hb_work_object_t * w )
139 {
140     hb_work_private_t * pv = w->private_data;
141     
142     vorbis_block_clear( &pv->vb );
143     vorbis_dsp_clear( &pv->vd );
144     vorbis_comment_clear( &pv->vc );
145     vorbis_info_clear( &pv->vi );
146     
147     hb_list_empty( &pv->list );
148     
149     free( pv->buf );
150     free( pv );
151     w->private_data = NULL;
152 }
153
154 /***********************************************************************
155  * Flush
156  ***********************************************************************
157  *
158  **********************************************************************/
159 static hb_buffer_t * Flush( hb_work_object_t * w )
160 {
161     hb_work_private_t * pv = w->private_data;
162     hb_buffer_t * buf;
163
164     if( vorbis_analysis_blockout( &pv->vd, &pv->vb ) == 1 )
165     {
166         ogg_packet op;
167
168         vorbis_analysis( &pv->vb, NULL );
169         vorbis_bitrate_addblock( &pv->vb );
170
171         if( vorbis_bitrate_flushpacket( &pv->vd, &op ) )
172         {
173             buf = hb_buffer_init( sizeof( ogg_packet ) + op.bytes );
174             memcpy( buf->data, &op, sizeof( ogg_packet ) );
175             memcpy( buf->data + sizeof( ogg_packet ), op.packet,
176                     op.bytes );
177             buf->frametype   = HB_FRAME_AUDIO;
178             buf->start = pv->pts; /* No exact, but who cares - the OGM
179                                     muxer doesn't use it */
180             buf->stop  = buf->start +
181                 90000 * OGGVORBIS_FRAME_SIZE + pv->job->arate;
182
183             return buf;
184         }
185     }
186
187     return NULL;
188 }
189
190 /***********************************************************************
191  * Encode
192  ***********************************************************************
193  *
194  **********************************************************************/
195 static hb_buffer_t * Encode( hb_work_object_t * w )
196 {
197     hb_work_private_t * pv = w->private_data;
198     hb_buffer_t * buf;
199     float ** buffer;
200     int i, j;
201
202     /* Try to extract more data */
203     if( ( buf = Flush( w ) ) )
204     {
205         return buf;
206     }
207
208     if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
209     {
210         return NULL;
211     }
212
213     /* Process more samples */
214     hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
215                       &pv->pts, NULL );
216     buffer = vorbis_analysis_buffer( &pv->vd, OGGVORBIS_FRAME_SIZE );
217     for( i = 0; i < OGGVORBIS_FRAME_SIZE; i++ )
218     {
219         for( j = 0; j < pv->out_discrete_channels; j++)
220         {
221             buffer[j][i] = ((float *) pv->buf)[(pv->out_discrete_channels * i + pv->channel_map[j])] / 32768.f;
222         }
223     }
224     vorbis_analysis_wrote( &pv->vd, OGGVORBIS_FRAME_SIZE );
225
226     /* Try to extract again */
227     return Flush( w );
228 }
229
230 /***********************************************************************
231  * Work
232  ***********************************************************************
233  *
234  **********************************************************************/
235 int encvorbisWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
236                    hb_buffer_t ** buf_out )
237 {
238     hb_work_private_t * pv = w->private_data;
239     hb_buffer_t * buf;
240
241     hb_list_add( pv->list, *buf_in );
242     *buf_in = NULL;
243
244     *buf_out = buf = Encode( w );
245
246     while( buf )
247     {
248         buf->next = Encode( w );
249         buf       = buf->next;
250     }
251
252     return HB_WORK_OK;
253 }