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.m0k.org/>.
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;
42 int encvorbisInit( hb_work_object_t * w, hb_job_t * job )
47 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
52 hb_log( "encvorbis: opening libvorbis" );
55 vorbis_info_init( &pv->vi );
56 if( vorbis_encode_setup_managed( &pv->vi, 2,
57 job->arate, -1, 1000 * job->abitrate, -1 ) ||
58 vorbis_encode_ctl( &pv->vi, OV_ECTL_RATEMANAGE_AVG, NULL ) ||
59 vorbis_encode_setup_init( &pv->vi ) )
61 hb_log( "encvorbis: vorbis_encode_setup_managed failed" );
65 vorbis_comment_init( &pv->vc );
66 vorbis_comment_add_tag( &pv->vc, "Encoder", "HandBrake");
68 /* set up the analysis state and auxiliary encoding storage */
69 vorbis_analysis_init( &pv->vd, &pv->vi);
70 vorbis_block_init( &pv->vd, &pv->vb);
72 /* get the 3 headers */
73 vorbis_analysis_headerout( &pv->vd, &pv->vc,
74 &header[0], &header[1], &header[2] );
75 for( i = 0; i < 3; i++ )
77 memcpy( w->config->vorbis.headers[i], &header[i],
78 sizeof( ogg_packet ) );
79 memcpy( w->config->vorbis.headers[i] + sizeof( ogg_packet ),
80 header[i].packet, header[i].bytes );
83 pv->input_samples = 2 * OGGVORBIS_FRAME_SIZE;
84 pv->buf = malloc( pv->input_samples * sizeof( float ) );
86 pv->list = hb_list_init();
91 /***********************************************************************
93 ***********************************************************************
95 **********************************************************************/
96 void encvorbisClose( hb_work_object_t * w )
100 /***********************************************************************
102 ***********************************************************************
104 **********************************************************************/
105 static hb_buffer_t * Flush( hb_work_object_t * w )
107 hb_work_private_t * pv = w->private_data;
110 if( vorbis_analysis_blockout( &pv->vd, &pv->vb ) == 1 )
114 vorbis_analysis( &pv->vb, NULL );
115 vorbis_bitrate_addblock( &pv->vb );
117 if( vorbis_bitrate_flushpacket( &pv->vd, &op ) )
119 buf = hb_buffer_init( sizeof( ogg_packet ) + op.bytes );
120 memcpy( buf->data, &op, sizeof( ogg_packet ) );
121 memcpy( buf->data + sizeof( ogg_packet ), op.packet,
124 buf->start = pv->pts; /* No exact, but who cares - the OGM
125 muxer doesn't use it */
126 buf->stop = buf->start +
127 90000 * OGGVORBIS_FRAME_SIZE + pv->job->arate;
136 /***********************************************************************
138 ***********************************************************************
140 **********************************************************************/
141 static hb_buffer_t * Encode( hb_work_object_t * w )
143 hb_work_private_t * pv = w->private_data;
148 /* Try to extract more data */
149 if( ( buf = Flush( w ) ) )
154 if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
159 /* Process more samples */
160 hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
162 buffer = vorbis_analysis_buffer( &pv->vd, OGGVORBIS_FRAME_SIZE );
163 for( i = 0; i < OGGVORBIS_FRAME_SIZE; i++ )
165 buffer[0][i] = ((float *) pv->buf)[2*i] / 32768.f;
166 buffer[1][i] = ((float *) pv->buf)[2*i+1] / 32768.f;
168 vorbis_analysis_wrote( &pv->vd, OGGVORBIS_FRAME_SIZE );
170 /* Try to extract again */
174 /***********************************************************************
176 ***********************************************************************
178 **********************************************************************/
179 int encvorbisWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
180 hb_buffer_t ** buf_out )
182 hb_work_private_t * pv = w->private_data;
185 hb_list_add( pv->list, *buf_in );
188 *buf_out = buf = Encode( w );
192 buf->next = Encode( w );