OSDN Git Service

import original 0.9.5 release
[handbrake-jp/handbrake-jp.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.fr/>.
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     int64_t       prev_blocksize;
43 };
44
45 int encvorbisInit( hb_work_object_t * w, hb_job_t * job )
46 {
47     hb_audio_t * audio = w->audio;
48     int i;
49     ogg_packet header[3];
50     struct ovectl_ratemanage2_arg  ctl_rate_arg;
51
52     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
53     w->private_data = pv;
54     pv->out_discrete_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
55
56     pv->job   = job;
57
58     hb_log( "encvorbis: opening libvorbis" );
59
60     /* 28kbps/channel seems to be the minimum for 6ch vorbis. */
61     int min_bitrate = 28 * pv->out_discrete_channels;
62     if (pv->out_discrete_channels > 2 && audio->config.out.bitrate < min_bitrate)
63     {
64         hb_log( "encvorbis: Selected bitrate (%d kbps) too low for %d channel audio.", audio->config.out.bitrate, pv->out_discrete_channels);
65         hb_log( "encvorbis: Resetting bitrate to %d kbps", min_bitrate);
66         /* Naughty! We shouldn't modify the audio from here. */
67         audio->config.out.bitrate = min_bitrate;
68     }
69
70     /* init */
71     for( i = 0; i < 3; i++ )
72     {
73         // Zero vorbis headers so that we don't crash in mk_laceXiph
74         // when vorbis_encode_setup_managed fails.
75         memset( w->config->vorbis.headers[i], 0, sizeof( ogg_packet ) );
76     }
77     vorbis_info_init( &pv->vi );
78     if( vorbis_encode_setup_managed( &pv->vi, pv->out_discrete_channels,
79           audio->config.out.samplerate, -1, 1000 * audio->config.out.bitrate, -1 ) )
80     {
81         hb_error( "encvorbis: vorbis_encode_setup_managed failed.\n" );
82         *job->die = 1;
83         return 0;
84     }
85
86     if( vorbis_encode_ctl( &pv->vi, OV_ECTL_RATEMANAGE2_GET, &ctl_rate_arg) )
87     {
88         hb_log( "encvorbis: vorbis_encode_ctl( ratemanage2_get ) failed" );
89     }
90
91     ctl_rate_arg.bitrate_average_kbps = audio->config.out.bitrate;
92     ctl_rate_arg.management_active = 1;
93
94     if( vorbis_encode_ctl( &pv->vi, OV_ECTL_RATEMANAGE2_SET, &ctl_rate_arg ) ||
95           vorbis_encode_setup_init( &pv->vi ) )
96     {
97         hb_error( "encvorbis: vorbis_encode_ctl( ratemanage2_set ) OR vorbis_encode_setup_init failed.\n" );
98         *job->die = 1;
99         return 0;
100     }
101
102     /* add a comment */
103     vorbis_comment_init( &pv->vc );
104     vorbis_comment_add_tag( &pv->vc, "Encoder", "HandBrake");
105     vorbis_comment_add_tag( &pv->vc, "LANGUAGE", w->config->vorbis.language);
106
107     /* set up the analysis state and auxiliary encoding storage */
108     vorbis_analysis_init( &pv->vd, &pv->vi);
109     vorbis_block_init( &pv->vd, &pv->vb);
110
111     /* get the 3 headers */
112     vorbis_analysis_headerout( &pv->vd, &pv->vc,
113                                &header[0], &header[1], &header[2] );
114     for( i = 0; i < 3; i++ )
115     {
116         memcpy( w->config->vorbis.headers[i], &header[i],
117                 sizeof( ogg_packet ) );
118         memcpy( w->config->vorbis.headers[i] + sizeof( ogg_packet ),
119                 header[i].packet, header[i].bytes );
120     }
121
122     pv->input_samples = pv->out_discrete_channels * OGGVORBIS_FRAME_SIZE;
123     pv->buf = malloc( pv->input_samples * sizeof( float ) );
124
125     pv->list = hb_list_init();
126
127     switch (pv->out_discrete_channels) {
128         case 1:
129             pv->channel_map[0] = 0;
130             break;
131         case 6:
132             // Vorbis use the following channels map = L C R Ls Rs Lfe
133             if( audio->config.in.codec == HB_ACODEC_AC3 )
134             {
135                 pv->channel_map[0] = 1;
136                 pv->channel_map[1] = 2;
137                 pv->channel_map[2] = 3;
138                 pv->channel_map[3] = 4;
139                 pv->channel_map[4] = 5;
140                 pv->channel_map[5] = 0;
141             }
142             else
143             {
144                 pv->channel_map[0] = 1;
145                 pv->channel_map[1] = 0;
146                 pv->channel_map[2] = 2;
147                 pv->channel_map[3] = 3;
148                 pv->channel_map[4] = 4;
149                 pv->channel_map[5] = 5;
150             }
151             break;
152         default:
153             hb_log("encvorbis.c: Unable to correctly proccess %d channels, assuming stereo.", pv->out_discrete_channels);
154         case 2:
155             // Assume stereo
156             pv->channel_map[0] = 0;
157             pv->channel_map[1] = 1;
158             break;
159     }
160
161     return 0;
162 }
163
164 /***********************************************************************
165  * Close
166  ***********************************************************************
167  *
168  **********************************************************************/
169 void encvorbisClose( hb_work_object_t * w )
170 {
171     hb_work_private_t * pv = w->private_data;
172
173     vorbis_block_clear( &pv->vb );
174     vorbis_dsp_clear( &pv->vd );
175     vorbis_comment_clear( &pv->vc );
176     vorbis_info_clear( &pv->vi );
177
178     if (pv->list)
179         hb_list_empty( &pv->list );
180
181     free( pv->buf );
182     free( pv );
183     w->private_data = NULL;
184 }
185
186 /***********************************************************************
187  * Flush
188  ***********************************************************************
189  *
190  **********************************************************************/
191 static hb_buffer_t * Flush( hb_work_object_t * w )
192 {
193     hb_work_private_t * pv = w->private_data;
194     hb_buffer_t * buf;
195     int64_t     blocksize = 0;
196
197     if( vorbis_analysis_blockout( &pv->vd, &pv->vb ) == 1 )
198     {
199         ogg_packet op;
200
201         vorbis_analysis( &pv->vb, NULL );
202         vorbis_bitrate_addblock( &pv->vb );
203
204         if( vorbis_bitrate_flushpacket( &pv->vd, &op ) )
205         {
206             buf = hb_buffer_init( sizeof( ogg_packet ) + op.bytes );
207             memcpy( buf->data, &op, sizeof( ogg_packet ) );
208             memcpy( buf->data + sizeof( ogg_packet ), op.packet,
209                     op.bytes );
210             blocksize = vorbis_packet_blocksize(&pv->vi, &op);
211             buf->frametype   = HB_FRAME_AUDIO;
212             buf->start = (int64_t)(vorbis_granule_time(&pv->vd, op.granulepos) * 90000);
213             buf->stop  = (int64_t)(vorbis_granule_time(&pv->vd, (pv->prev_blocksize + blocksize)/4 + op.granulepos) * 90000);
214             /* The stop time isn't accurate for the first ~3 packets, as the actual blocksize depends on the previous _and_ current packets. */
215             pv->prev_blocksize = blocksize;
216             return buf;
217         }
218     }
219
220     return NULL;
221 }
222
223 /***********************************************************************
224  * Encode
225  ***********************************************************************
226  *
227  **********************************************************************/
228 static hb_buffer_t * Encode( hb_work_object_t * w )
229 {
230     hb_work_private_t * pv = w->private_data;
231     hb_buffer_t * buf;
232     float ** buffer;
233     int i, j;
234
235     /* Try to extract more data */
236     if( ( buf = Flush( w ) ) )
237     {
238         return buf;
239     }
240
241     if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
242     {
243         return NULL;
244     }
245
246     /* Process more samples */
247     hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
248                       &pv->pts, NULL );
249     buffer = vorbis_analysis_buffer( &pv->vd, OGGVORBIS_FRAME_SIZE );
250     for( i = 0; i < OGGVORBIS_FRAME_SIZE; i++ )
251     {
252         for( j = 0; j < pv->out_discrete_channels; j++)
253         {
254             buffer[j][i] = ((float *) pv->buf)[(pv->out_discrete_channels * i + pv->channel_map[j])] / 32768.f;
255         }
256     }
257     vorbis_analysis_wrote( &pv->vd, OGGVORBIS_FRAME_SIZE );
258
259     /* Try to extract again */
260     return Flush( w );
261 }
262
263 /***********************************************************************
264  * Work
265  ***********************************************************************
266  *
267  **********************************************************************/
268 int encvorbisWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
269                    hb_buffer_t ** buf_out )
270 {
271     hb_work_private_t * pv = w->private_data;
272     hb_buffer_t * buf;
273
274     if ( (*buf_in)->size <= 0 )
275     {
276         /* EOF on input - send it downstream & say we're done */
277         *buf_out = *buf_in;
278         *buf_in = NULL;
279        return HB_WORK_DONE;
280     }
281
282     hb_list_add( pv->list, *buf_in );
283     *buf_in = NULL;
284
285     *buf_out = buf = Encode( w );
286
287     while( buf )
288     {
289         buf->next = Encode( w );
290         buf       = buf->next;
291     }
292
293     return HB_WORK_OK;
294 }