1 /* $Id: encvorbis.c,v 1.6 2005/03/05 15:08:32 titer Exp $
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. */
9 #include "vorbis/vorbisenc.h"
11 #define OGGVORBIS_FRAME_SIZE 1024
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 * );
17 hb_work_object_t hb_encvorbis =
20 "Vorbis encoder (libvorbis)",
26 struct hb_work_private_s
35 unsigned long input_samples;
40 int out_discrete_channels;
42 int64_t prev_blocksize;
45 int encvorbisInit( hb_work_object_t * w, hb_job_t * job )
47 hb_audio_t * audio = w->audio;
50 struct ovectl_ratemanage2_arg ctl_rate_arg;
52 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
54 pv->out_discrete_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
58 hb_log( "encvorbis: opening libvorbis" );
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)
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;
71 vorbis_info_init( &pv->vi );
72 if( vorbis_encode_setup_managed( &pv->vi, pv->out_discrete_channels,
73 audio->config.out.samplerate, -1, 1000 * audio->config.out.bitrate, -1 ) )
75 hb_error( "encvorbis: vorbis_encode_setup_managed failed.\n" );
80 if( vorbis_encode_ctl( &pv->vi, OV_ECTL_RATEMANAGE2_GET, &ctl_rate_arg) )
82 hb_log( "encvorbis: vorbis_encode_ctl( ratemanage2_get ) failed" );
85 ctl_rate_arg.bitrate_average_kbps = audio->config.out.bitrate;
86 ctl_rate_arg.management_active = 1;
88 if( vorbis_encode_ctl( &pv->vi, OV_ECTL_RATEMANAGE2_SET, &ctl_rate_arg ) ||
89 vorbis_encode_setup_init( &pv->vi ) )
91 hb_error( "encvorbis: vorbis_encode_ctl( ratemanage2_set ) OR vorbis_encode_setup_init failed.\n" );
97 vorbis_comment_init( &pv->vc );
98 vorbis_comment_add_tag( &pv->vc, "Encoder", "HandBrake");
99 vorbis_comment_add_tag( &pv->vc, "LANGUAGE", w->config->vorbis.language);
101 /* set up the analysis state and auxiliary encoding storage */
102 vorbis_analysis_init( &pv->vd, &pv->vi);
103 vorbis_block_init( &pv->vd, &pv->vb);
105 /* get the 3 headers */
106 vorbis_analysis_headerout( &pv->vd, &pv->vc,
107 &header[0], &header[1], &header[2] );
108 for( i = 0; i < 3; i++ )
110 memcpy( w->config->vorbis.headers[i], &header[i],
111 sizeof( ogg_packet ) );
112 memcpy( w->config->vorbis.headers[i] + sizeof( ogg_packet ),
113 header[i].packet, header[i].bytes );
116 pv->input_samples = pv->out_discrete_channels * OGGVORBIS_FRAME_SIZE;
117 pv->buf = malloc( pv->input_samples * sizeof( float ) );
119 pv->list = hb_list_init();
121 switch (pv->out_discrete_channels) {
123 pv->channel_map[0] = 0;
126 pv->channel_map[0] = 0;
127 pv->channel_map[1] = 2;
128 pv->channel_map[2] = 1;
129 pv->channel_map[3] = 4;
130 pv->channel_map[4] = 5;
131 pv->channel_map[5] = 3;
134 hb_log("encvorbis.c: Unable to correctly proccess %d channels, assuming stereo.", pv->out_discrete_channels);
137 pv->channel_map[0] = 0;
138 pv->channel_map[1] = 1;
145 /***********************************************************************
147 ***********************************************************************
149 **********************************************************************/
150 void encvorbisClose( hb_work_object_t * w )
152 hb_work_private_t * pv = w->private_data;
154 vorbis_block_clear( &pv->vb );
155 vorbis_dsp_clear( &pv->vd );
156 vorbis_comment_clear( &pv->vc );
157 vorbis_info_clear( &pv->vi );
160 hb_list_empty( &pv->list );
164 w->private_data = NULL;
167 /***********************************************************************
169 ***********************************************************************
171 **********************************************************************/
172 static hb_buffer_t * Flush( hb_work_object_t * w )
174 hb_work_private_t * pv = w->private_data;
176 int64_t blocksize = 0;
178 if( vorbis_analysis_blockout( &pv->vd, &pv->vb ) == 1 )
182 vorbis_analysis( &pv->vb, NULL );
183 vorbis_bitrate_addblock( &pv->vb );
185 if( vorbis_bitrate_flushpacket( &pv->vd, &op ) )
187 buf = hb_buffer_init( sizeof( ogg_packet ) + op.bytes );
188 memcpy( buf->data, &op, sizeof( ogg_packet ) );
189 memcpy( buf->data + sizeof( ogg_packet ), op.packet,
191 blocksize = vorbis_packet_blocksize(&pv->vi, &op);
192 buf->frametype = HB_FRAME_AUDIO;
193 buf->start = (int64_t)(vorbis_granule_time(&pv->vd, op.granulepos) * 90000);
194 buf->stop = (int64_t)(vorbis_granule_time(&pv->vd, (pv->prev_blocksize + blocksize)/4 + op.granulepos) * 90000);
195 /* The stop time isn't accurate for the first ~3 packets, as the actual blocksize depends on the previous _and_ current packets. */
196 pv->prev_blocksize = blocksize;
204 /***********************************************************************
206 ***********************************************************************
208 **********************************************************************/
209 static hb_buffer_t * Encode( hb_work_object_t * w )
211 hb_work_private_t * pv = w->private_data;
216 /* Try to extract more data */
217 if( ( buf = Flush( w ) ) )
222 if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
227 /* Process more samples */
228 hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
230 buffer = vorbis_analysis_buffer( &pv->vd, OGGVORBIS_FRAME_SIZE );
231 for( i = 0; i < OGGVORBIS_FRAME_SIZE; i++ )
233 for( j = 0; j < pv->out_discrete_channels; j++)
235 buffer[j][i] = ((float *) pv->buf)[(pv->out_discrete_channels * i + pv->channel_map[j])] / 32768.f;
238 vorbis_analysis_wrote( &pv->vd, OGGVORBIS_FRAME_SIZE );
240 /* Try to extract again */
244 /***********************************************************************
246 ***********************************************************************
248 **********************************************************************/
249 int encvorbisWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
250 hb_buffer_t ** buf_out )
252 hb_work_private_t * pv = w->private_data;
255 if ( (*buf_in)->size <= 0 )
257 /* EOF on input - send it downstream & say we're done */
263 hb_list_add( pv->list, *buf_in );
266 *buf_out = buf = Encode( w );
270 buf->next = Encode( w );