1 /* $Id: deca52.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. */
9 #include "a52dec/a52.h"
11 struct hb_work_private_s
23 float dynamic_range_compression;
29 int64_t next_expected_pts;
37 int out_discrete_channels;
41 static int deca52Init( hb_work_object_t *, hb_job_t * );
42 static int deca52Work( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
43 static void deca52Close( hb_work_object_t * );
44 static int deca52BSInfo( hb_work_object_t * , const hb_buffer_t *,
47 hb_work_object_t hb_deca52 =
58 /***********************************************************************
60 **********************************************************************/
61 static hb_buffer_t * Decode( hb_work_object_t * w );
63 /***********************************************************************
65 ***********************************************************************
66 * Boosts soft audio -- taken from gbooker's work in A52Decoder, comment and all..
68 * 1) The user requested a compression of 1 or less, return the typical power rule
69 * 2) The user requested a compression of more than 1 (decompression):
70 * If the stream's requested compression is less than 1.0 (loud sound), return the normal compression
71 * If the stream's requested compression is more than 1.0 (soft sound), use power rule (which will make
72 * it louder in this case).
74 **********************************************************************/
75 static sample_t dynrng_call (sample_t c, void *data)
77 float *level = (float *)data;
78 float levelToUse = (float)*level;
79 if(c > 1.0 || levelToUse <= 1.0)
81 return powf(c, levelToUse);
87 /***********************************************************************
89 ***********************************************************************
90 * Allocate the work object, initialize liba52
91 **********************************************************************/
92 static int deca52Init( hb_work_object_t * w, hb_job_t * job )
94 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
95 hb_audio_t * audio = w->audio;
100 pv->list = hb_list_init();
101 pv->state = a52_init( 0 );
103 /* Decide what format we want out of a52dec
104 work.c has already done some of this deduction for us in do_job() */
106 pv->flags_out = HB_AMIXDOWN_GET_A52_FORMAT(audio->config.out.mixdown);
108 /* pass the number of channels used into the private work data */
109 /* will only be actually used if we're not doing AC3 passthru */
110 pv->out_discrete_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
113 pv->dynamic_range_compression = audio->config.out.dynamic_range_compression;
115 pv->next_expected_pts = 0;
121 /***********************************************************************
123 ***********************************************************************
125 **********************************************************************/
126 static void deca52Close( hb_work_object_t * w )
128 hb_work_private_t * pv = w->private_data;
129 a52_free( pv->state );
130 hb_list_empty( &pv->list );
132 w->private_data = NULL;
135 /***********************************************************************
137 ***********************************************************************
138 * Add the given buffer to the data we already have, and decode as much
140 **********************************************************************/
141 static int deca52Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
142 hb_buffer_t ** buf_out )
144 hb_work_private_t * pv = w->private_data;
147 if ( (*buf_in)->size <= 0 )
149 /* EOF on input stream - send it downstream & say that we're done */
155 pv->sequence = (*buf_in)->sequence;
157 hb_list_add( pv->list, *buf_in );
160 /* If we got more than a frame, chain raw buffers */
161 *buf_out = buf = Decode( w );
164 buf->sequence = pv->sequence;
165 buf->next = Decode( w );
172 /***********************************************************************
174 ***********************************************************************
176 **********************************************************************/
177 static hb_buffer_t * Decode( hb_work_object_t * w )
179 hb_work_private_t * pv = w->private_data;
181 hb_audio_t * audio = w->audio;
185 /* Get a frame header if don't have one yet */
188 while( hb_list_bytes( pv->list ) >= 7 )
190 /* We have 7 bytes, check if this is a correct header */
191 hb_list_seebytes( pv->list, pv->frame, 7 );
192 pv->size = a52_syncinfo( pv->frame, &pv->flags_in, &pv->rate,
199 hb_log( "a52_syncinfo ok" );
209 hb_log( "a52_syncinfo failed" );
213 /* Try one byte later */
214 hb_list_getbytes( pv->list, pv->frame, 1, NULL, NULL );
219 hb_list_bytes( pv->list ) < pv->size )
225 /* Get the whole frame */
226 hb_list_getbytes( pv->list, pv->frame, pv->size, &pts, &pos );
229 pts = pv->next_expected_pts;
232 /* AC3 passthrough: don't decode the AC3 frame */
233 if( audio->config.out.codec == HB_ACODEC_AC3 )
235 buf = hb_buffer_init( pv->size );
236 memcpy( buf->data, pv->frame, pv->size );
237 buf->start = pts + ( pos / pv->size ) * 6 * 256 * 90000 / pv->rate;
238 buf->stop = buf->start + 6 * 256 * 90000 / pv->rate;
239 pv->next_expected_pts = buf->stop;
245 a52_frame( pv->state, pv->frame, &pv->flags_out, &pv->level, 0 );
247 if ( pv->dynamic_range_compression > 1.0 )
249 a52_dynrng( pv->state, dynrng_call, &pv->dynamic_range_compression);
252 /* 6 blocks per frame, 256 samples per block, channelsused channels */
253 buf = hb_buffer_init( 6 * 256 * pv->out_discrete_channels * sizeof( float ) );
254 buf->start = pts + ( pos / pv->size ) * 6 * 256 * 90000 / pv->rate;
255 buf->stop = buf->start + 6 * 256 * 90000 / pv->rate;
258 * To track AC3 PTS add this back in again.
259 *hb_log("AC3: pts is %lld, buf->start %lld buf->stop %lld", pts, buf->start, buf->stop);
262 pv->next_expected_pts = buf->stop;
264 for( i = 0; i < 6; i++ )
266 sample_t * samples_in;
269 a52_block( pv->state );
270 samples_in = a52_samples( pv->state );
271 samples_out = ((float *) buf->data) + 256 * pv->out_discrete_channels * i;
274 for( j = 0; j < 256; j++ )
276 for ( k = 0; k < pv->out_discrete_channels; k++ )
278 samples_out[(pv->out_discrete_channels*j)+k] = samples_in[(256*k)+j];
288 static int deca52BSInfo( hb_work_object_t *w, const hb_buffer_t *b,
289 hb_work_info_t *info )
292 int rate = 0, bitrate = 0, flags = 0;
293 int old_rate = 0, old_bitrate = 0;
296 memset( info, 0, sizeof(*info) );
298 /* since AC3 frames don't line up with MPEG ES frames scan the
299 * entire frame for an AC3 sync pattern. */
300 for ( i = 0; i < b->size - 7; ++i )
302 if( a52_syncinfo( &b->data[i], &flags, &rate, &bitrate ) != 0 )
305 * Got sync apparently, save these values and check that they
306 * also match when we get sync again.
310 if( rate == old_rate && bitrate == old_bitrate )
317 old_bitrate = bitrate;
321 if ( rate == 0 || bitrate == 0 )
323 /* didn't find AC3 sync */
328 * bsid | bsmod | acmod | cmixlev | surmixlev | dsurmod | lfeon | dialnorm | compre
330 * [ byte1 ][ byte2 ][ byte3 ]
337 info->bitrate = bitrate;
339 info->version = raw >> 3; /* bsid is the first 5 bits */
340 info->mode = raw & 0x7; /* bsmod is the following 3 bits */
342 if ( (flags & A52_CHANNEL_MASK) == A52_DOLBY )
344 info->flags |= AUDIO_F_DOLBY;
347 switch( flags & A52_CHANNEL_MASK )
353 info->channel_layout = HB_INPUT_CH_LAYOUT_MONO;
358 info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
360 /* dolby (DPL1 aka Dolby Surround = 4.0 matrix-encoded) input */
362 info->channel_layout = HB_INPUT_CH_LAYOUT_DOLBY;
366 info->channel_layout = HB_INPUT_CH_LAYOUT_3F2R;
370 info->channel_layout = HB_INPUT_CH_LAYOUT_3F1R;
374 info->channel_layout = HB_INPUT_CH_LAYOUT_3F;
377 info->channel_layout = HB_INPUT_CH_LAYOUT_2F1R;
380 info->channel_layout = HB_INPUT_CH_LAYOUT_2F2R;
384 info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
389 info->channel_layout |= HB_INPUT_CH_LAYOUT_HAS_LFE;