OSDN Git Service

gitattirubes test 2
[handbrake-jp/handbrake-jp.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     struct ovectl_ratemanage2_arg  ctl_rate_arg;
51
52     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
53     w->private_data = pv;
54     pv->out_discrete_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
55
56     pv->job   = job;
57
58     hb_log( "encvorbis: opening libvorbis" );
59
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)
63     {
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;
68     }
69
70     /* init */
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 ) )
74     {
75         hb_error( "encvorbis: vorbis_encode_setup_managed failed.\n" );
76         *job->die = 1;
77         return 0;
78     }
79
80     if( vorbis_encode_ctl( &pv->vi, OV_ECTL_RATEMANAGE2_GET, &ctl_rate_arg) )
81     {
82         hb_log( "encvorbis: vorbis_encode_ctl( ratemanage2_get ) failed" );
83     }
84
85     ctl_rate_arg.bitrate_average_kbps = audio->config.out.bitrate;
86     ctl_rate_arg.management_active = 1;
87
88     if( vorbis_encode_ctl( &pv->vi, OV_ECTL_RATEMANAGE2_SET, &ctl_rate_arg ) ||
89           vorbis_encode_setup_init( &pv->vi ) )
90     {
91         hb_error( "encvorbis: vorbis_encode_ctl( ratemanage2_set ) OR vorbis_encode_setup_init failed.\n" );
92         *job->die = 1;
93         return 0;
94     }
95
96     /* add a comment */
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);
100
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);
104
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++ )
109     {
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 );
114     }
115
116     pv->input_samples = pv->out_discrete_channels * OGGVORBIS_FRAME_SIZE;
117     pv->buf = malloc( pv->input_samples * sizeof( float ) );
118
119     pv->list = hb_list_init();
120
121     switch (pv->out_discrete_channels) {
122         case 1:
123             pv->channel_map[0] = 0;
124             break;
125         case 6:
126             // Vorbis use the following channels map = L C R Ls Rs Lfe
127             if( audio->config.in.codec == HB_ACODEC_AC3 )
128             {
129                 pv->channel_map[0] = 1;
130                 pv->channel_map[1] = 2;
131                 pv->channel_map[2] = 3;
132                 pv->channel_map[3] = 4;
133                 pv->channel_map[4] = 5;
134                 pv->channel_map[5] = 0;
135             }
136             else
137             {
138                 pv->channel_map[0] = 1;
139                 pv->channel_map[1] = 0;
140                 pv->channel_map[2] = 2;
141                 pv->channel_map[3] = 3;
142                 pv->channel_map[4] = 4;
143                 pv->channel_map[5] = 5;
144             }
145             break;
146         default:
147             hb_log("encvorbis.c: Unable to correctly proccess %d channels, assuming stereo.", pv->out_discrete_channels);
148         case 2:
149             // Assume stereo
150             pv->channel_map[0] = 0;
151             pv->channel_map[1] = 1;
152             break;
153     }
154
155     return 0;
156 }
157
158 /***********************************************************************
159  * Close
160  ***********************************************************************
161  *
162  **********************************************************************/
163 void encvorbisClose( hb_work_object_t * w )
164 {
165     hb_work_private_t * pv = w->private_data;
166
167     vorbis_block_clear( &pv->vb );
168     vorbis_dsp_clear( &pv->vd );
169     vorbis_comment_clear( &pv->vc );
170     vorbis_info_clear( &pv->vi );
171
172     if (pv->list)
173         hb_list_empty( &pv->list );
174
175     free( pv->buf );
176     free( pv );
177     w->private_data = NULL;
178 }
179
180 /***********************************************************************
181  * Flush
182  ***********************************************************************
183  *
184  **********************************************************************/
185 static hb_buffer_t * Flush( hb_work_object_t * w )
186 {
187     hb_work_private_t * pv = w->private_data;
188     hb_buffer_t * buf;
189     int64_t     blocksize = 0;
190
191     if( vorbis_analysis_blockout( &pv->vd, &pv->vb ) == 1 )
192     {
193         ogg_packet op;
194
195         vorbis_analysis( &pv->vb, NULL );
196         vorbis_bitrate_addblock( &pv->vb );
197
198         if( vorbis_bitrate_flushpacket( &pv->vd, &op ) )
199         {
200             buf = hb_buffer_init( sizeof( ogg_packet ) + op.bytes );
201             memcpy( buf->data, &op, sizeof( ogg_packet ) );
202             memcpy( buf->data + sizeof( ogg_packet ), op.packet,
203                     op.bytes );
204             blocksize = vorbis_packet_blocksize(&pv->vi, &op);
205             buf->frametype   = HB_FRAME_AUDIO;
206             buf->start = (int64_t)(vorbis_granule_time(&pv->vd, op.granulepos) * 90000);
207             buf->stop  = (int64_t)(vorbis_granule_time(&pv->vd, (pv->prev_blocksize + blocksize)/4 + op.granulepos) * 90000);
208             /* The stop time isn't accurate for the first ~3 packets, as the actual blocksize depends on the previous _and_ current packets. */
209             pv->prev_blocksize = blocksize;
210             return buf;
211         }
212     }
213
214     return NULL;
215 }
216
217 /***********************************************************************
218  * Encode
219  ***********************************************************************
220  *
221  **********************************************************************/
222 static hb_buffer_t * Encode( hb_work_object_t * w )
223 {
224     hb_work_private_t * pv = w->private_data;
225     hb_buffer_t * buf;
226     float ** buffer;
227     int i, j;
228
229     /* Try to extract more data */
230     if( ( buf = Flush( w ) ) )
231     {
232         return buf;
233     }
234
235     if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
236     {
237         return NULL;
238     }
239
240     /* Process more samples */
241     hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
242                       &pv->pts, NULL );
243     buffer = vorbis_analysis_buffer( &pv->vd, OGGVORBIS_FRAME_SIZE );
244     for( i = 0; i < OGGVORBIS_FRAME_SIZE; i++ )
245     {
246         for( j = 0; j < pv->out_discrete_channels; j++)
247         {
248             buffer[j][i] = ((float *) pv->buf)[(pv->out_discrete_channels * i + pv->channel_map[j])] / 32768.f;
249         }
250     }
251     vorbis_analysis_wrote( &pv->vd, OGGVORBIS_FRAME_SIZE );
252
253     /* Try to extract again */
254     return Flush( w );
255 }
256
257 /***********************************************************************
258  * Work
259  ***********************************************************************
260  *
261  **********************************************************************/
262 int encvorbisWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
263                    hb_buffer_t ** buf_out )
264 {
265     hb_work_private_t * pv = w->private_data;
266     hb_buffer_t * buf;
267
268     if ( (*buf_in)->size <= 0 )
269     {
270         /* EOF on input - send it downstream & say we're done */
271         *buf_out = *buf_in;
272         *buf_in = NULL;
273        return HB_WORK_DONE;
274     }
275
276     hb_list_add( pv->list, *buf_in );
277     *buf_in = NULL;
278
279     *buf_out = buf = Encode( w );
280
281     while( buf )
282     {
283         buf->next = Encode( w );
284         buf       = buf->next;
285     }
286
287     return HB_WORK_OK;
288 }