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.m0k.org/>.
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 int deca52Init( hb_work_object_t *, hb_job_t * );
42 int deca52Work( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
43 void deca52Close( hb_work_object_t * );
45 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 int deca52Init( hb_work_object_t * w, hb_job_t * job )
90 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
95 pv->list = hb_list_init();
96 pv->state = a52_init( 0 );
98 /* Decide what format we want out of a52dec
99 work.c has already done some of this deduction for us in do_job() */
101 pv->flags_out = HB_AMIXDOWN_GET_A52_FORMAT(w->amixdown);
103 /* pass the number of channels used into the private work data */
104 /* will only be actually used if we're not doing AC3 passthru */
105 pv->out_discrete_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(w->amixdown);
108 pv->dynamic_range_compression = job->dynamic_range_compression;
110 pv->next_expected_pts = 0;
116 /***********************************************************************
118 ***********************************************************************
120 **********************************************************************/
121 void deca52Close( hb_work_object_t * w )
123 hb_work_private_t * pv = w->private_data;
124 a52_free( pv->state );
125 hb_list_empty( &pv->list );
127 w->private_data = NULL;
130 /***********************************************************************
132 ***********************************************************************
133 * Add the given buffer to the data we already have, and decode as much
135 **********************************************************************/
136 int deca52Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
137 hb_buffer_t ** buf_out )
139 hb_work_private_t * pv = w->private_data;
142 if( buf_in && *buf_in )
144 pv->sequence = (*buf_in)->sequence;
147 hb_list_add( pv->list, *buf_in );
150 /* If we got more than a frame, chain raw buffers */
151 *buf_out = buf = Decode( w );
154 buf->sequence = pv->sequence;
155 buf->next = Decode( w );
162 /***********************************************************************
164 ***********************************************************************
166 **********************************************************************/
167 static hb_buffer_t * Decode( hb_work_object_t * w )
169 hb_work_private_t * pv = w->private_data;
174 /* Get a frame header if don't have one yet */
177 while( hb_list_bytes( pv->list ) >= 7 )
179 /* We have 7 bytes, check if this is a correct header */
180 hb_list_seebytes( pv->list, pv->frame, 7 );
181 pv->size = a52_syncinfo( pv->frame, &pv->flags_in, &pv->rate,
188 hb_log( "a52_syncinfo ok" );
198 hb_log( "a52_syncinfo failed" );
202 /* Try one byte later */
203 hb_list_getbytes( pv->list, pv->frame, 1, NULL, NULL );
208 hb_list_bytes( pv->list ) < pv->size )
214 /* Get the whole frame */
215 hb_list_getbytes( pv->list, pv->frame, pv->size, &pts, &pos );
217 /* AC3 passthrough: don't decode the AC3 frame */
218 if( pv->job->acodec & HB_ACODEC_AC3 ||
219 w->amixdown == HB_AMIXDOWN_AC3 )
221 buf = hb_buffer_init( pv->size );
222 memcpy( buf->data, pv->frame, pv->size );
223 buf->start = pts + ( pos / pv->size ) * 6 * 256 * 90000 / pv->rate;
224 buf->stop = buf->start + 6 * 256 * 90000 / pv->rate;
230 a52_frame( pv->state, pv->frame, &pv->flags_out, &pv->level, 0 );
232 if ( pv->dynamic_range_compression > 1.0 )
234 a52_dynrng( pv->state, dynrng_call, &pv->dynamic_range_compression);
237 /* 6 blocks per frame, 256 samples per block, channelsused channels */
238 buf = hb_buffer_init( 6 * 256 * pv->out_discrete_channels * sizeof( float ) );
241 pts = pv->next_expected_pts;
243 buf->start = pts + ( pos / pv->size ) * 6 * 256 * 90000 / pv->rate;
244 buf->stop = buf->start + 6 * 256 * 90000 / pv->rate;
247 * To track AC3 PTS add this back in again.
248 *hb_log("AC3: pts is %lld, buf->start %lld buf->stop %lld", pts, buf->start, buf->stop);
251 pv->next_expected_pts = buf->stop;
253 for( i = 0; i < 6; i++ )
255 sample_t * samples_in;
258 a52_block( pv->state );
259 samples_in = a52_samples( pv->state );
260 samples_out = ((float *) buf->data) + 256 * pv->out_discrete_channels * i;
263 for( j = 0; j < 256; j++ )
265 for ( k = 0; k < pv->out_discrete_channels; k++ )
267 samples_out[(pv->out_discrete_channels*j)+k] = samples_in[(256*k)+j];