OSDN Git Service

Don't drop subtitles when crossing PTS discontinuities by using buffer sequence numbe...
[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.m0k.org/>.
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     int i;
48     ogg_packet header[3];
49     struct ovectl_ratemanage2_arg  ctl_rate_arg;
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(w->amixdown);
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 && job->abitrate < min_bitrate)
62     {
63         hb_log( "encvorbis: Selected bitrate (%d kbps) too low for %d channel audio.", job->abitrate, pv->out_discrete_channels);
64         hb_log( "encvorbis: Resetting bitrate to %d kbps", min_bitrate);
65         job->abitrate = min_bitrate;
66     }
67
68     /* init */
69     vorbis_info_init( &pv->vi );
70     if( vorbis_encode_setup_managed( &pv->vi, pv->out_discrete_channels,
71           job->arate, -1, 1000 * job->abitrate, -1 ) )
72     {
73         hb_error( "encvorbis: vorbis_encode_setup_managed failed.\n" );
74         *job->die = 1;
75         return 0;
76     }
77
78     if( vorbis_encode_ctl( &pv->vi, OV_ECTL_RATEMANAGE2_GET, &ctl_rate_arg) )
79     {
80         hb_log( "encvorbis: vorbis_encode_ctl( ratemanage2_get ) failed" );
81     }
82
83     ctl_rate_arg.bitrate_average_kbps = 1000 * job->abitrate;
84     ctl_rate_arg.management_active = 1;
85
86     if( vorbis_encode_ctl( &pv->vi, OV_ECTL_RATEMANAGE2_SET, &ctl_rate_arg ) ||
87           vorbis_encode_setup_init( &pv->vi ) )
88     {
89         hb_error( "encvorbis: vorbis_encode_ctl( ratemanage2_set ) OR vorbis_encode_setup_init failed.\n" );
90         *job->die = 1;
91         return 0;
92     }
93
94     /* add a comment */
95     vorbis_comment_init( &pv->vc );
96     vorbis_comment_add_tag( &pv->vc, "Encoder", "HandBrake");
97     vorbis_comment_add_tag( &pv->vc, "LANGUAGE", w->config->vorbis.language);
98
99     /* set up the analysis state and auxiliary encoding storage */
100     vorbis_analysis_init( &pv->vd, &pv->vi);
101     vorbis_block_init( &pv->vd, &pv->vb);
102
103     /* get the 3 headers */
104     vorbis_analysis_headerout( &pv->vd, &pv->vc,
105                                &header[0], &header[1], &header[2] );
106     for( i = 0; i < 3; i++ )
107     {
108         memcpy( w->config->vorbis.headers[i], &header[i],
109                 sizeof( ogg_packet ) );
110         memcpy( w->config->vorbis.headers[i] + sizeof( ogg_packet ),
111                 header[i].packet, header[i].bytes );
112     }
113
114     pv->input_samples = pv->out_discrete_channels * OGGVORBIS_FRAME_SIZE;
115     pv->buf = malloc( pv->input_samples * sizeof( float ) );
116
117     pv->list = hb_list_init();
118
119     switch (pv->out_discrete_channels) {
120         case 1:
121             pv->channel_map[0] = 0;
122             break;
123         case 6:
124             pv->channel_map[0] = 0;
125             pv->channel_map[1] = 2;
126             pv->channel_map[2] = 1;
127             pv->channel_map[3] = 4;
128             pv->channel_map[4] = 5;
129             pv->channel_map[5] = 3;
130             break;
131         default:
132             hb_log("encvorbis.c: Unable to correctly proccess %d channels, assuming stereo.", pv->out_discrete_channels);
133         case 2:
134             // Assume stereo
135             pv->channel_map[0] = 0;
136             pv->channel_map[1] = 1;
137             break;
138     }
139
140     return 0;
141 }
142
143 /***********************************************************************
144  * Close
145  ***********************************************************************
146  *
147  **********************************************************************/
148 void encvorbisClose( hb_work_object_t * w )
149 {
150     hb_work_private_t * pv = w->private_data;
151     
152     vorbis_block_clear( &pv->vb );
153     vorbis_dsp_clear( &pv->vd );
154     vorbis_comment_clear( &pv->vc );
155     vorbis_info_clear( &pv->vi );
156
157     if (pv->list)
158         hb_list_empty( &pv->list );
159     
160     free( pv->buf );
161     free( pv );
162     w->private_data = NULL;
163 }
164
165 /***********************************************************************
166  * Flush
167  ***********************************************************************
168  *
169  **********************************************************************/
170 static hb_buffer_t * Flush( hb_work_object_t * w )
171 {
172     hb_work_private_t * pv = w->private_data;
173     hb_buffer_t * buf;
174     int64_t     blocksize = 0;
175
176     if( vorbis_analysis_blockout( &pv->vd, &pv->vb ) == 1 )
177     {
178         ogg_packet op;
179
180         vorbis_analysis( &pv->vb, NULL );
181         vorbis_bitrate_addblock( &pv->vb );
182
183         if( vorbis_bitrate_flushpacket( &pv->vd, &op ) )
184         {
185             buf = hb_buffer_init( sizeof( ogg_packet ) + op.bytes );
186             memcpy( buf->data, &op, sizeof( ogg_packet ) );
187             memcpy( buf->data + sizeof( ogg_packet ), op.packet,
188                     op.bytes );
189             blocksize = vorbis_packet_blocksize(&pv->vi, &op);
190             buf->frametype   = HB_FRAME_AUDIO;
191             buf->start = (int64_t)(vorbis_granule_time(&pv->vd, op.granulepos) * 90000);
192             buf->stop  = (int64_t)(vorbis_granule_time(&pv->vd, (pv->prev_blocksize + blocksize)/4 + op.granulepos) * 90000);
193             /* The stop time isn't accurate for the first ~3 packets, as the actual blocksize depends on the previous _and_ current packets. */
194             pv->prev_blocksize = blocksize;
195             return buf;
196         }
197     }
198
199     return NULL;
200 }
201
202 /***********************************************************************
203  * Encode
204  ***********************************************************************
205  *
206  **********************************************************************/
207 static hb_buffer_t * Encode( hb_work_object_t * w )
208 {
209     hb_work_private_t * pv = w->private_data;
210     hb_buffer_t * buf;
211     float ** buffer;
212     int i, j;
213
214     /* Try to extract more data */
215     if( ( buf = Flush( w ) ) )
216     {
217         return buf;
218     }
219
220     if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
221     {
222         return NULL;
223     }
224
225     /* Process more samples */
226     hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
227                       &pv->pts, NULL );
228     buffer = vorbis_analysis_buffer( &pv->vd, OGGVORBIS_FRAME_SIZE );
229     for( i = 0; i < OGGVORBIS_FRAME_SIZE; i++ )
230     {
231         for( j = 0; j < pv->out_discrete_channels; j++)
232         {
233             buffer[j][i] = ((float *) pv->buf)[(pv->out_discrete_channels * i + pv->channel_map[j])] / 32768.f;
234         }
235     }
236     vorbis_analysis_wrote( &pv->vd, OGGVORBIS_FRAME_SIZE );
237
238     /* Try to extract again */
239     return Flush( w );
240 }
241
242 /***********************************************************************
243  * Work
244  ***********************************************************************
245  *
246  **********************************************************************/
247 int encvorbisWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
248                    hb_buffer_t ** buf_out )
249 {
250     hb_work_private_t * pv = w->private_data;
251     hb_buffer_t * buf;
252
253     hb_list_add( pv->list, *buf_in );
254     *buf_in = NULL;
255
256     *buf_out = buf = Encode( w );
257
258     while( buf )
259     {
260         buf->next = Encode( w );
261         buf       = buf->next;
262     }
263
264     return HB_WORK_OK;
265 }