OSDN Git Service

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