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
36 int deca52Init( hb_work_object_t *, hb_job_t * );
37 int deca52Work( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
38 void deca52Close( hb_work_object_t * );
40 hb_work_object_t hb_deca52 =
49 /***********************************************************************
51 **********************************************************************/
52 static hb_buffer_t * Decode( hb_work_object_t * w );
54 /***********************************************************************
56 ***********************************************************************
57 * Allocate the work object, initialize liba52
58 **********************************************************************/
59 int deca52Init( hb_work_object_t * w, hb_job_t * job )
61 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
66 pv->list = hb_list_init();
67 pv->state = a52_init( 0 );
69 /* Decide what format we want out of a52dec
70 work.c has already done some of this deduction for us in do_job() */
72 if (w->config->a52.channelsused == 6) {
73 /* we're going to be encoding to AAC,
74 and have turned on the "preserve 5.1" flag */
75 pv->flags_out = A52_3F2R | A52_LFE;
76 } else if (w->config->a52.channelsused == 1) {
77 /* we're going to be encoding to AAC, */
78 /* and want to keep the mono-ness of the source audio */
79 pv->flags_out = A52_MONO;
80 } else if (w->config->a52.channelsused == 2 && w->config->a52.channels == 5 && w->config->a52.lfechannels == 1) {
81 /* we are mixing a 5.1 source down to stereo, so use dolby surround */
82 pv->flags_out = A52_DOLBY;
83 } else if (w->config->a52.channelsused == 2 && ((w->config->a52.ac3flags & A52_CHANNEL_MASK) == A52_DOLBY)) {
84 /* we have a dolby stereo surround source, so preserve it */
85 pv->flags_out = A52_DOLBY;
87 /* mix everything else down to plain stereo */
88 pv->flags_out = A52_STEREO;
91 /* pass the number of channels used into the private work data */
92 pv->channelsused = w->config->a52.channelsused;
99 /***********************************************************************
101 ***********************************************************************
103 **********************************************************************/
104 void deca52Close( hb_work_object_t * w )
106 hb_work_private_t * pv = w->private_data;
107 a52_free( pv->state );
110 /***********************************************************************
112 ***********************************************************************
113 * Add the given buffer to the data we already have, and decode as much
115 **********************************************************************/
116 int deca52Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
117 hb_buffer_t ** buf_out )
119 hb_work_private_t * pv = w->private_data;
122 hb_list_add( pv->list, *buf_in );
125 /* If we got more than a frame, chain raw buffers */
126 *buf_out = buf = Decode( w );
129 buf->next = Decode( w );
136 /***********************************************************************
138 ***********************************************************************
140 **********************************************************************/
141 static hb_buffer_t * Decode( hb_work_object_t * w )
143 hb_work_private_t * pv = w->private_data;
148 /* Get a frame header if don't have one yet */
151 while( hb_list_bytes( pv->list ) >= 7 )
153 /* We have 7 bytes, check if this is a correct header */
154 hb_list_seebytes( pv->list, pv->frame, 7 );
155 pv->size = a52_syncinfo( pv->frame, &pv->flags_in, &pv->rate,
162 hb_log( "a52_syncinfo ok" );
172 hb_log( "a52_syncinfo failed" );
176 /* Try one byte later */
177 hb_list_getbytes( pv->list, pv->frame, 1, NULL, NULL );
182 hb_list_bytes( pv->list ) < pv->size )
188 /* Get the whole frame */
189 hb_list_getbytes( pv->list, pv->frame, pv->size, &pts, &pos );
191 /* AC3 passthrough: don't decode the AC3 frame */
192 if( pv->job->acodec & HB_ACODEC_AC3 )
194 buf = hb_buffer_init( pv->size );
195 memcpy( buf->data, pv->frame, pv->size );
196 buf->start = pts + ( pos / pv->size ) * 6 * 256 * 90000 / pv->rate;
197 buf->stop = buf->start + 6 * 256 * 90000 / pv->rate;
203 a52_frame( pv->state, pv->frame, &pv->flags_out, &pv->level, 0 );
205 /* 6 blocks per frame, 256 samples per block, channelsused channels */
206 buf = hb_buffer_init( 6 * 256 * pv->channelsused * sizeof( float ) );
207 buf->start = pts + ( pos / pv->size ) * 6 * 256 * 90000 / pv->rate;
208 buf->stop = buf->start + 6 * 256 * 90000 / pv->rate;
210 for( i = 0; i < 6; i++ )
212 sample_t * samples_in;
215 a52_block( pv->state );
216 samples_in = a52_samples( pv->state );
217 samples_out = ((float *) buf->data) + 256 * pv->channelsused * i;
220 for( j = 0; j < 256; j++ )
222 for ( k = 0; k < pv->channelsused; k++ )
224 samples_out[(pv->channelsused*j)+k] = samples_in[(256*k)+j]; // DJA