1 /* $Id: decdca.c,v 1.14 2005/03/03 17:21:57 titer Exp $
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. */
10 struct hb_work_private_s
29 /* max frame size of the 16 bits version is 16384 */
30 /* max frame size of the 14 bits version is 18726 */
35 int out_discrete_channels;
39 static int decdcaInit( hb_work_object_t *, hb_job_t * );
40 static int decdcaWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
41 static void decdcaClose( hb_work_object_t * );
42 static int decdcaBSInfo( hb_work_object_t *, const hb_buffer_t *,
45 hb_work_object_t hb_decdca =
56 /***********************************************************************
58 **********************************************************************/
59 static hb_buffer_t * Decode( hb_work_object_t * w );
61 /***********************************************************************
63 ***********************************************************************
64 * Allocate the work object, initialize libdca
65 **********************************************************************/
66 static int decdcaInit( hb_work_object_t * w, hb_job_t * job )
68 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
69 hb_audio_t * audio = w->audio;
74 pv->list = hb_list_init();
75 pv->state = dca_init( 0 );
77 /* Decide what format we want out of libdca
78 work.c has already done some of this deduction for us in do_job() */
80 pv->flags_out = HB_AMIXDOWN_GET_DCA_FORMAT(audio->config.out.mixdown);
82 /* pass the number of channels used into the private work data */
83 /* will only be actually used if we're not doing AC3 passthru */
84 pv->out_discrete_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
91 /***********************************************************************
93 ***********************************************************************
95 **********************************************************************/
96 static void decdcaClose( hb_work_object_t * w )
98 hb_work_private_t * pv = w->private_data;
99 dca_free( pv->state );
100 hb_list_empty( &pv->list );
102 w->private_data = NULL;
105 /***********************************************************************
107 ***********************************************************************
108 * Add the given buffer to the data we already have, and decode as much
110 **********************************************************************/
111 static int decdcaWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
112 hb_buffer_t ** buf_out )
114 hb_work_private_t * pv = w->private_data;
117 if ( (*buf_in)->size <= 0 )
119 /* EOF on input stream - send it downstream & say that we're done */
125 hb_list_add( pv->list, *buf_in );
128 /* If we got more than a frame, chain raw buffers */
129 *buf_out = buf = Decode( w );
132 buf->next = Decode( w );
139 /***********************************************************************
141 ***********************************************************************
143 **********************************************************************/
144 static hb_buffer_t * Decode( hb_work_object_t * w )
146 hb_work_private_t * pv = w->private_data;
152 /* Get a frame header if don't have one yet */
155 while( hb_list_bytes( pv->list ) >= 14 )
157 /* We have 14 bytes, check if this is a correct header */
158 hb_list_seebytes( pv->list, pv->frame, 14 );
159 pv->size = dca_syncinfo( pv->state, pv->frame, &pv->flags_in, &pv->rate,
160 &pv->bitrate, &pv->frame_length );
166 hb_log( "dca_syncinfo ok" );
176 hb_log( "dca_syncinfo failed" );
180 /* Try one byte later */
181 hb_list_getbytes( pv->list, pv->frame, 1, NULL, NULL );
185 if( !pv->sync || hb_list_bytes( pv->list ) < pv->size )
191 /* Get the whole frame */
192 hb_list_getbytes( pv->list, pv->frame, pv->size, &pts, &pos );
195 dca_frame( pv->state, pv->frame, &pv->flags_out, &pv->level, 0 );
197 /* find out how many blocks are in this frame */
198 num_blocks = dca_blocks_num( pv->state );
200 /* num_blocks blocks per frame, 256 samples per block, channelsused channels */
201 int nsamp = num_blocks * 256;
202 buf = hb_buffer_init( nsamp * pv->out_discrete_channels * sizeof( float ) );
204 // mkv files typically use a 1ms timebase which results in a lot of
205 // truncation error in their timestamps. Also, TSMuxer or something
206 // in the m2ts-to-mkv toolchain seems to take a very casual attitude
207 // about time - timestamps seem to randomly offset by ~40ms for a few
208 // seconds then recover. So, if the pts we got is within 50ms of the
209 // pts computed from the data stream, use the data stream pts.
210 if ( pts < 0 || fabs( (double)pts - pv->next_pts ) < 50.*90. )
215 pv->next_pts += (double)nsamp / (double)pv->rate * 90000.;
216 buf->stop = pv->next_pts;
218 for( i = 0; i < num_blocks; i++ )
220 dca_sample_t * samples_in;
223 dca_block( pv->state );
224 samples_in = dca_samples( pv->state );
225 samples_out = ((float *) buf->data) + 256 * pv->out_discrete_channels * i;
228 for( j = 0; j < 256; j++ )
230 for ( k = 0; k < pv->out_discrete_channels; k++ )
232 samples_out[(pv->out_discrete_channels*j)+k] = samples_in[(256*k)+j] * 16384;
243 static int decdcaBSInfo( hb_work_object_t *w, const hb_buffer_t *b,
244 hb_work_info_t *info )
246 int i, flags, rate, bitrate, frame_length;
247 dca_state_t * state = dca_init( 0 );
249 memset( info, 0, sizeof(*info) );
251 /* since DCA frames don't line up with MPEG ES frames scan the
252 * entire frame for an DCA sync pattern. */
253 for ( i = 0; i < b->size - 7; ++i )
255 if( dca_syncinfo( state, &b->data[i], &flags, &rate, &bitrate,
261 if ( i >= b->size - 7 )
263 /* didn't find DCA sync */
270 info->bitrate = bitrate;
273 if ( ( flags & DCA_CHANNEL_MASK) == DCA_DOLBY )
275 info->flags |= AUDIO_F_DOLBY;
278 switch( flags & DCA_CHANNEL_MASK )
282 info->channel_layout = HB_INPUT_CH_LAYOUT_MONO;
287 case DCA_STEREO_SUMDIFF:
288 case DCA_STEREO_TOTAL:
289 info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
293 info->channel_layout = HB_INPUT_CH_LAYOUT_3F2R;
297 info->channel_layout = HB_INPUT_CH_LAYOUT_3F1R;
301 info->channel_layout = HB_INPUT_CH_LAYOUT_3F;
304 info->channel_layout = HB_INPUT_CH_LAYOUT_2F1R;
307 info->channel_layout = HB_INPUT_CH_LAYOUT_2F2R;
310 info->channel_layout = HB_INPUT_CH_LAYOUT_4F2R;
314 info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
319 info->channel_layout |= HB_INPUT_CH_LAYOUT_HAS_LFE;