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
33 int deca52Init( hb_work_object_t *, hb_job_t * );
34 int deca52Work( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
35 void deca52Close( hb_work_object_t * );
37 hb_work_object_t hb_deca52 =
46 /***********************************************************************
48 **********************************************************************/
49 static hb_buffer_t * Decode( hb_work_object_t * w );
51 /***********************************************************************
53 ***********************************************************************
54 * Allocate the work object, initialize liba52
55 **********************************************************************/
56 int deca52Init( hb_work_object_t * w, hb_job_t * job )
58 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
63 pv->list = hb_list_init();
64 pv->state = a52_init( 0 );
65 pv->flags_out = A52_STEREO;
71 /***********************************************************************
73 ***********************************************************************
75 **********************************************************************/
76 void deca52Close( hb_work_object_t * w )
78 hb_work_private_t * pv = w->private_data;
79 a52_free( pv->state );
82 /***********************************************************************
84 ***********************************************************************
85 * Add the given buffer to the data we already have, and decode as much
87 **********************************************************************/
88 int deca52Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
89 hb_buffer_t ** buf_out )
91 hb_work_private_t * pv = w->private_data;
94 hb_list_add( pv->list, *buf_in );
97 /* If we got more than a frame, chain raw buffers */
98 *buf_out = buf = Decode( w );
101 buf->next = Decode( w );
108 /***********************************************************************
110 ***********************************************************************
112 **********************************************************************/
113 static hb_buffer_t * Decode( hb_work_object_t * w )
115 hb_work_private_t * pv = w->private_data;
121 /* Get a frame header if don't have one yet */
124 while( hb_list_bytes( pv->list ) >= 7 )
126 /* We have 7 bytes, check if this is a correct header */
127 hb_list_seebytes( pv->list, pv->frame, 7 );
128 pv->size = a52_syncinfo( pv->frame, &pv->flags_in, &pv->rate,
135 hb_log( "a52_syncinfo ok" );
145 hb_log( "a52_syncinfo failed" );
149 /* Try one byte later */
150 hb_list_getbytes( pv->list, pv->frame, 1, NULL, NULL );
155 hb_list_bytes( pv->list ) < pv->size )
161 /* Get the whole frame */
162 hb_list_getbytes( pv->list, pv->frame, pv->size, &pts, &pos );
164 /* AC3 passthrough: don't decode the AC3 frame */
165 if( pv->job->acodec & HB_ACODEC_AC3 )
167 buf = hb_buffer_init( pv->size );
168 memcpy( buf->data, pv->frame, pv->size );
169 buf->start = pts + ( pos / pv->size ) * 6 * 256 * 90000 / pv->rate;
170 buf->stop = buf->start + 6 * 256 * 90000 / pv->rate;
176 a52_frame( pv->state, pv->frame, &pv->flags_out, &pv->level, 0 );
178 /* 6 blocks per frame, 256 samples per block, 2 channels */
179 buf = hb_buffer_init( 3072 * sizeof( float ) );
180 buf->start = pts + ( pos / pv->size ) * 6 * 256 * 90000 / pv->rate;
181 buf->stop = buf->start + 6 * 256 * 90000 / pv->rate;
183 for( i = 0; i < 6; i++ )
185 sample_t * samples_in;
188 a52_block( pv->state );
189 samples_in = a52_samples( pv->state );
190 samples_out = ((float *) buf->data) + 512 * i;
193 for( j = 0; j < 256; j++ )
195 samples_out[2*j] = samples_in[j];
196 samples_out[2*j+1] = samples_in[256+j];