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_object_s
36 /***********************************************************************
38 **********************************************************************/
39 static void Close( hb_work_object_t ** _w );
40 static int Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
41 hb_buffer_t ** buf_out );
42 static hb_buffer_t * Decode( hb_work_object_t * w );
44 /***********************************************************************
46 ***********************************************************************
47 * Allocate the work object, initialize liba52
48 **********************************************************************/
49 hb_work_object_t * hb_work_deca52_init( hb_job_t * job, hb_audio_t * audio )
51 hb_work_object_t * w = calloc( sizeof( hb_work_object_t ), 1 );
52 w->name = strdup( "AC3 decoder" );
59 w->list = hb_list_init();
60 w->state = a52_init( 0 );
61 w->flags_out = A52_STEREO;
67 /***********************************************************************
69 ***********************************************************************
71 **********************************************************************/
72 static void Close( hb_work_object_t ** _w )
74 hb_work_object_t * w = *_w;
81 /***********************************************************************
83 ***********************************************************************
84 * Add the given buffer to the data we already have, and decode as much
86 **********************************************************************/
87 static int Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
88 hb_buffer_t ** buf_out )
92 hb_list_add( w->list, *buf_in );
95 /* If we got more than a frame, chain raw buffers */
96 *buf_out = buf = Decode( w );
99 buf->next = Decode( w );
106 /***********************************************************************
108 ***********************************************************************
110 **********************************************************************/
111 static hb_buffer_t * Decode( hb_work_object_t * w )
118 /* Get a frame header if don't have one yet */
121 while( hb_list_bytes( w->list ) >= 7 )
123 /* We have 7 bytes, check if this is a correct header */
124 hb_list_seebytes( w->list, w->frame, 7 );
125 w->size = a52_syncinfo( w->frame, &w->flags_in, &w->rate,
132 hb_log( "a52_syncinfo ok" );
142 hb_log( "a52_syncinfo failed" );
146 /* Try one byte later */
147 hb_list_getbytes( w->list, w->frame, 1, NULL, NULL );
152 hb_list_bytes( w->list ) < w->size )
158 /* Get the whole frame */
159 hb_list_getbytes( w->list, w->frame, w->size, &pts, &pos );
161 /* AC3 passthrough: don't decode the AC3 frame */
162 if( w->job->acodec & HB_ACODEC_AC3 )
164 buf = hb_buffer_init( w->size );
165 memcpy( buf->data, w->frame, w->size );
166 buf->start = pts + ( pos / w->size ) * 6 * 256 * 90000 / w->rate;
167 buf->stop = buf->start + 6 * 256 * 90000 / w->rate;
173 a52_frame( w->state, w->frame, &w->flags_out, &w->level, 0 );
175 /* 6 blocks per frame, 256 samples per block, 2 channels */
176 buf = hb_buffer_init( 3072 * sizeof( float ) );
177 buf->start = pts + ( pos / w->size ) * 6 * 256 * 90000 / w->rate;
178 buf->stop = buf->start + 6 * 256 * 90000 / w->rate;
180 for( i = 0; i < 6; i++ )
182 sample_t * samples_in;
185 a52_block( w->state );
186 samples_in = a52_samples( w->state );
187 samples_out = ((float *) buf->data) + 512 * i;
190 for( j = 0; j < 256; j++ )
192 samples_out[2*j] = samples_in[j];
193 samples_out[2*j+1] = samples_in[256+j];