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"
10 #include "libavutil/crc.h"
12 struct hb_work_private_s
23 int out_discrete_channels;
25 int frames; // number of good frames decoded
26 int crc_errors; // number of frames with crc errors
27 int bytes_dropped; // total bytes dropped while resyncing
29 float dynamic_range_compression;
30 double next_expected_pts;
33 const AVCRC *crc_table;
37 static int deca52Init( hb_work_object_t *, hb_job_t * );
38 static int deca52Work( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
39 static void deca52Close( hb_work_object_t * );
40 static int deca52BSInfo( hb_work_object_t * , const hb_buffer_t *,
43 hb_work_object_t hb_deca52 =
54 /***********************************************************************
56 **********************************************************************/
57 static hb_buffer_t * Decode( hb_work_object_t * w );
59 /***********************************************************************
61 ***********************************************************************
62 * Boosts soft audio -- taken from gbooker's work in A52Decoder, comment and all..
64 * 1) The user requested a compression of 1 or less, return the typical power rule
65 * 2) The user requested a compression of more than 1 (decompression):
66 * If the stream's requested compression is less than 1.0 (loud sound), return the normal compression
67 * If the stream's requested compression is more than 1.0 (soft sound), use power rule (which will make
68 * it louder in this case).
70 **********************************************************************/
71 static sample_t dynrng_call (sample_t c, void *data)
73 float *level = (float *)data;
74 float levelToUse = (float)*level;
75 if(c > 1.0 || levelToUse <= 1.0)
77 return powf(c, levelToUse);
83 /***********************************************************************
85 ***********************************************************************
86 * Allocate the work object, initialize liba52
87 **********************************************************************/
88 static int deca52Init( hb_work_object_t * w, hb_job_t * job )
90 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
91 hb_audio_t * audio = w->audio;
96 pv->crc_table = av_crc_get_table( AV_CRC_16_ANSI );
97 pv->list = hb_list_init();
98 pv->state = a52_init( 0 );
100 /* Decide what format we want out of a52dec
101 work.c has already done some of this deduction for us in do_job() */
103 pv->flags_out = HB_AMIXDOWN_GET_A52_FORMAT(audio->config.out.mixdown);
104 if ( audio->config.out.codec == HB_ACODEC_LAME )
105 pv->flags_out |= A52_ADJUST_LEVEL;
107 /* pass the number of channels used into the private work data */
108 /* will only be actually used if we're not doing AC3 passthru */
109 pv->out_discrete_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
112 pv->dynamic_range_compression = audio->config.out.dynamic_range_compression;
117 /***********************************************************************
119 ***********************************************************************
121 **********************************************************************/
122 static void deca52Close( hb_work_object_t * w )
124 hb_work_private_t * pv = w->private_data;
126 if ( pv->crc_errors )
128 hb_log( "deca52: %d frames decoded, %d crc errors, %d bytes dropped",
129 pv->frames, pv->crc_errors, pv->bytes_dropped );
131 a52_free( pv->state );
132 hb_list_empty( &pv->list );
134 w->private_data = NULL;
137 /***********************************************************************
139 ***********************************************************************
140 * Add the given buffer to the data we already have, and decode as much
142 **********************************************************************/
143 static int deca52Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
144 hb_buffer_t ** buf_out )
146 hb_work_private_t * pv = w->private_data;
149 if ( (*buf_in)->size <= 0 )
151 /* EOF on input stream - send it downstream & say that we're done */
157 if ( (*buf_in)->start < -1 && pv->next_expected_pts == 0 )
159 // discard buffers that start before video time 0
164 hb_list_add( pv->list, *buf_in );
167 /* If we got more than a frame, chain raw buffers */
168 *buf_out = buf = Decode( w );
171 buf->next = Decode( w );
178 /***********************************************************************
180 ***********************************************************************
182 **********************************************************************/
183 static hb_buffer_t * Decode( hb_work_object_t * w )
185 hb_work_private_t * pv = w->private_data;
187 hb_audio_t * audio = w->audio;
191 // check that we're at the start of a valid frame and align to the
192 // start of a valid frame if we're not.
193 // we have to check the header & crc so we need at least
194 // 7 (the header size) + 128 (the minimum frame size) bytes
195 while( hb_list_bytes( pv->list ) >= 7+128 )
197 /* check if this is a valid header */
198 hb_list_seebytes( pv->list, pv->frame, 7 );
199 size = a52_syncinfo( pv->frame, &pv->flags_in, &pv->rate, &pv->bitrate );
202 // header looks valid - check the crc1
203 if( size > hb_list_bytes( pv->list ) )
205 // don't have all the frame's data yet
208 int crc1size = (size >> 1) + (size >> 3);
209 hb_list_seebytes( pv->list, pv->frame, crc1size );
210 if ( av_crc( pv->crc_table, 0, pv->frame + 2, crc1size - 2 ) == 0 )
212 // crc1 is ok - say we have valid frame sync
215 hb_log( "output track %d: ac3 in sync after skipping %d bytes",
216 audio->config.out.track, pv->error );
217 pv->bytes_dropped += pv->error;
223 // no sync - discard one byte then try again
224 hb_list_getbytes( pv->list, pv->frame, 1, NULL, NULL );
228 // we exit the above loop either in error state (we didn't find sync
229 // or don't have enough data yet to validate sync) or in sync. If we're
230 // not in sync we need more data so just return.
231 if( pv->error || size <= 0 || hb_list_bytes( pv->list ) < size )
237 // Get the whole frame and check its CRC. If the CRC is wrong
238 // discard the frame - we'll resync on the next call.
241 hb_list_getbytes( pv->list, pv->frame, size, &ipts, NULL );
242 if ( av_crc( pv->crc_table, 0, pv->frame + 2, size - 2 ) != 0 )
248 if ( ipts != pv->last_buf_pts )
250 pv->last_buf_pts = ipts;
254 // spec says that the PTS is the start time of the first frame
255 // that starts in the PES frame so we only use the PTS once then
256 // get the following frames' PTS from the frame length.
260 double pts = ( ipts != -1 ) ? ipts : pv->next_expected_pts;
261 double frame_dur = (6. * 256. * 90000.) / pv->rate;
263 /* AC3 passthrough: don't decode the AC3 frame */
264 if( audio->config.out.codec == HB_ACODEC_AC3 )
266 buf = hb_buffer_init( size );
267 memcpy( buf->data, pv->frame, size );
271 pv->next_expected_pts = pts;
276 a52_frame( pv->state, pv->frame, &pv->flags_out, &pv->level, 0 );
278 /* If a user specifies strong dynamic range compression (>1), adjust it.
279 If a user specifies default dynamic range compression (1), leave it alone.
280 If a user specifies no dynamic range compression (0), call a null function. */
281 if( pv->dynamic_range_compression > 1.0 )
283 a52_dynrng( pv->state, dynrng_call, &pv->dynamic_range_compression );
285 else if( !pv->dynamic_range_compression )
287 a52_dynrng( pv->state, NULL, NULL );
290 /* 6 blocks per frame, 256 samples per block, channelsused channels */
291 buf = hb_buffer_init( 6 * 256 * pv->out_discrete_channels * sizeof( float ) );
295 pv->next_expected_pts = pts;
297 for( i = 0; i < 6; i++ )
299 sample_t * samples_in;
302 a52_block( pv->state );
303 samples_in = a52_samples( pv->state );
304 samples_out = ((float *) buf->data) + 256 * pv->out_discrete_channels * i;
307 for( j = 0; j < 256; j++ )
309 for ( k = 0; k < pv->out_discrete_channels; k++ )
311 samples_out[(pv->out_discrete_channels*j)+k] = samples_in[(256*k)+j];
319 static int find_sync( const uint8_t *buf, int len )
323 // since AC3 frames don't line up with MPEG ES frames scan the
324 // frame for an AC3 sync pattern.
325 for ( i = 0; i < len - 16; ++i )
327 int rate, bitrate, flags;
328 int size = a52_syncinfo( (uint8_t *)buf + i, &flags, &rate, &bitrate );
331 // we have a plausible sync header - see if crc1 checks
332 int crc1size = (size >> 1) + (size >> 3);
333 if ( i + crc1size > len )
335 // don't have enough data to check crc1
338 if ( av_crc( av_crc_get_table( AV_CRC_16_ANSI ), 0,
339 buf + i + 2, crc1size - 2 ) == 0 )
341 // crc checks - we've got sync
349 static int deca52BSInfo( hb_work_object_t *w, const hb_buffer_t *b,
350 hb_work_info_t *info )
352 memset( info, 0, sizeof(*info) );
354 // We don't know if the way that AC3 frames are fragmented into whatever
355 // packetization the container uses will give us enough bytes per fragment
356 // to check the CRC (we need at least 5/8 of the the frame). So we
357 // copy the fragment we got into an accumulation buffer in the audio object
358 // then look for sync over all the frags we've accumulated so far.
359 uint8_t *buf = w->audio->priv.config.a52.buf;
360 int len = w->audio->priv.config.a52.len, blen = b->size;
361 if ( len + blen > sizeof(w->audio->priv.config.a52.buf) )
363 // we don't have enough empty space in the accumulation buffer to
364 // hold the new frag - make room for it by discarding the oldest data.
365 if ( blen >= sizeof(w->audio->priv.config.a52.buf) )
367 // the frag is bigger than our accumulation buffer - copy all
368 // that will fit (the excess doesn't matter since the buffer
369 // is many times the size of a max length ac3 frame).
370 blen = sizeof(w->audio->priv.config.a52.buf);
375 // discard enough bytes from the front of the buffer to make
376 // room for the new stuff
377 int newlen = sizeof(w->audio->priv.config.a52.buf) - blen;
378 memcpy( buf, buf + len - newlen, newlen );
382 // add the new frag to the buffer
383 memcpy( buf+len, b->data, blen );
387 if ( ( i = find_sync( buf, len ) ) < 0 )
389 // didn't find sync - wait for more data
390 w->audio->priv.config.a52.len = len;
394 // got sync - extract and canoncalize the bitstream parameters
395 int rate = 0, bitrate = 0, flags = 0;
396 uint8_t raw = buf[i + 5];
397 a52_syncinfo( buf + i, &flags, &rate, &bitrate );
399 if ( rate == 0 || bitrate == 0 )
401 // invalid AC-3 parameters - toss what we have so we'll start over
402 // with the next buf otherwise we'll keep syncing on this junk.
403 w->audio->priv.config.a52.len = 0;
407 // bsid | bsmod | acmod | cmixlv | surmixlv | dsurmod | lfeon | dialnorm | compre
409 // byte1 | byte2 | byte3
414 info->bitrate = bitrate;
416 info->version = raw >> 3; /* bsid is the first 5 bits */
417 info->mode = raw & 0x7; /* bsmod is the following 3 bits */
419 if ( (flags & A52_CHANNEL_MASK) == A52_DOLBY )
421 info->flags |= AUDIO_F_DOLBY;
424 switch( flags & A52_CHANNEL_MASK )
430 info->channel_layout = HB_INPUT_CH_LAYOUT_MONO;
435 info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
437 /* dolby (DPL1 aka Dolby Surround = 4.0 matrix-encoded) input */
439 info->channel_layout = HB_INPUT_CH_LAYOUT_DOLBY;
443 info->channel_layout = HB_INPUT_CH_LAYOUT_3F2R;
447 info->channel_layout = HB_INPUT_CH_LAYOUT_3F1R;
451 info->channel_layout = HB_INPUT_CH_LAYOUT_3F;
454 info->channel_layout = HB_INPUT_CH_LAYOUT_2F1R;
457 info->channel_layout = HB_INPUT_CH_LAYOUT_2F2R;
461 info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
466 info->channel_layout |= HB_INPUT_CH_LAYOUT_HAS_LFE;