OSDN Git Service

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