OSDN Git Service

Merge the 0.8.0_mpeg4ip branch into the trunk
[handbrake-jp/handbrake-jp-git.git] / libmediafork / 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 "mediafork.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 };
41
42 int encvorbisInit( hb_work_object_t * w, hb_job_t * job )
43 {
44     int i;
45     ogg_packet header[3];
46
47     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
48     w->private_data = pv;
49
50     pv->job   = job;
51
52     hb_log( "encvorbis: opening libvorbis" );
53
54     /* init */
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 ) )
60     {
61         hb_log( "encvorbis: vorbis_encode_setup_managed failed" );
62     }
63
64     /* add a comment */
65     vorbis_comment_init( &pv->vc );
66     vorbis_comment_add_tag( &pv->vc, "Encoder", "HandBrake");
67
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);
71
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++ )
76     {
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 );
81     }
82
83     pv->input_samples = 2 * OGGVORBIS_FRAME_SIZE;
84     pv->buf = malloc( pv->input_samples * sizeof( float ) );
85
86     pv->list = hb_list_init();
87
88     return 0;
89 }
90
91 /***********************************************************************
92  * Close
93  ***********************************************************************
94  *
95  **********************************************************************/
96 void encvorbisClose( hb_work_object_t * w )
97 {
98 }
99
100 /***********************************************************************
101  * Flush
102  ***********************************************************************
103  *
104  **********************************************************************/
105 static hb_buffer_t * Flush( hb_work_object_t * w )
106 {
107     hb_work_private_t * pv = w->private_data;
108     hb_buffer_t * buf;
109
110     if( vorbis_analysis_blockout( &pv->vd, &pv->vb ) == 1 )
111     {
112         ogg_packet op;
113
114         vorbis_analysis( &pv->vb, NULL );
115         vorbis_bitrate_addblock( &pv->vb );
116
117         if( vorbis_bitrate_flushpacket( &pv->vd, &op ) )
118         {
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,
122                     op.bytes );
123             buf->key   = 1;
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;
128
129             return buf;
130         }
131     }
132
133     return NULL;
134 }
135
136 /***********************************************************************
137  * Encode
138  ***********************************************************************
139  *
140  **********************************************************************/
141 static hb_buffer_t * Encode( hb_work_object_t * w )
142 {
143     hb_work_private_t * pv = w->private_data;
144     hb_buffer_t * buf;
145     float ** buffer;
146     int i;
147
148     /* Try to extract more data */
149     if( ( buf = Flush( w ) ) )
150     {
151         return buf;
152     }
153
154     if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
155     {
156         return NULL;
157     }
158
159     /* Process more samples */
160     hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
161                       &pv->pts, NULL );
162     buffer = vorbis_analysis_buffer( &pv->vd, OGGVORBIS_FRAME_SIZE );
163     for( i = 0; i < OGGVORBIS_FRAME_SIZE; i++ )
164     {
165         buffer[0][i] = ((float *) pv->buf)[2*i]   / 32768.f;
166         buffer[1][i] = ((float *) pv->buf)[2*i+1] / 32768.f;
167     }
168     vorbis_analysis_wrote( &pv->vd, OGGVORBIS_FRAME_SIZE );
169
170     /* Try to extract again */
171     return Flush( w );
172 }
173
174 /***********************************************************************
175  * Work
176  ***********************************************************************
177  *
178  **********************************************************************/
179 int encvorbisWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
180                    hb_buffer_t ** buf_out )
181 {
182     hb_work_private_t * pv = w->private_data;
183     hb_buffer_t * buf;
184
185     hb_list_add( pv->list, *buf_in );
186     *buf_in = NULL;
187
188     *buf_out = buf = Encode( w );
189
190     while( buf )
191     {
192         buf->next = Encode( w );
193         buf       = buf->next;
194     }
195
196     return HB_WORK_OK;
197 }