Refer to transitions in the presence-or-lack-thereof of progressive flags on MPEG...
[HandBrake.git] / libhb / decdca.c
blob669112f5b1d5dfdb86b9dfca3afe4f81431acb49
1 /* $Id: decdca.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. */
7 #include "hb.h"
8 #include "dca.h"
10 struct hb_work_private_s
12 hb_job_t * job;
14 /* libdca handle */
15 dca_state_t * state;
17 int flags_in;
18 int flags_out;
19 int rate;
20 int bitrate;
21 int frame_length;
22 float level;
24 int error;
25 int sync;
26 int size;
28 /* max frame size of the 16 bits version is 16384 */
29 /* max frame size of the 14 bits version is 18726 */
30 uint8_t frame[18726];
32 hb_list_t * list;
34 int out_discrete_channels;
38 int decdcaInit( hb_work_object_t *, hb_job_t * );
39 int decdcaWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
40 void decdcaClose( hb_work_object_t * );
42 hb_work_object_t hb_decdca =
44 WORK_DECDCA,
45 "DCA decoder",
46 decdcaInit,
47 decdcaWork,
48 decdcaClose
51 /***********************************************************************
52 * Local prototypes
53 **********************************************************************/
54 static hb_buffer_t * Decode( hb_work_object_t * w );
56 /***********************************************************************
57 * hb_work_decdca_init
58 ***********************************************************************
59 * Allocate the work object, initialize libdca
60 **********************************************************************/
61 int decdcaInit( hb_work_object_t * w, hb_job_t * job )
63 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
64 w->private_data = pv;
66 pv->job = job;
68 pv->list = hb_list_init();
69 pv->state = dca_init( 0 );
71 /* Decide what format we want out of libdca
72 work.c has already done some of this deduction for us in do_job() */
74 pv->flags_out = HB_AMIXDOWN_GET_DCA_FORMAT(w->amixdown);
76 /* pass the number of channels used into the private work data */
77 /* will only be actually used if we're not doing AC3 passthru */
78 pv->out_discrete_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(w->amixdown);
80 pv->level = 32768.0;
82 return 0;
85 /***********************************************************************
86 * Close
87 ***********************************************************************
88 * Free memory
89 **********************************************************************/
90 void decdcaClose( hb_work_object_t * w )
92 hb_work_private_t * pv = w->private_data;
93 dca_free( pv->state );
94 hb_list_empty( &pv->list );
95 free( pv );
96 w->private_data = NULL;
99 /***********************************************************************
100 * Work
101 ***********************************************************************
102 * Add the given buffer to the data we already have, and decode as much
103 * as we can
104 **********************************************************************/
105 int decdcaWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
106 hb_buffer_t ** buf_out )
108 hb_work_private_t * pv = w->private_data;
109 hb_buffer_t * buf;
111 hb_list_add( pv->list, *buf_in );
112 *buf_in = NULL;
114 /* If we got more than a frame, chain raw buffers */
115 *buf_out = buf = Decode( w );
116 while( buf )
118 buf->next = Decode( w );
119 buf = buf->next;
122 return HB_WORK_OK;
125 /***********************************************************************
126 * Decode
127 ***********************************************************************
129 **********************************************************************/
130 static hb_buffer_t * Decode( hb_work_object_t * w )
132 hb_work_private_t * pv = w->private_data;
133 hb_buffer_t * buf;
134 int i, j, k;
135 uint64_t pts, pos;
136 int num_blocks;
138 /* Get a frame header if don't have one yet */
139 if( !pv->sync )
141 while( hb_list_bytes( pv->list ) >= 14 )
143 /* We have 14 bytes, check if this is a correct header */
144 hb_list_seebytes( pv->list, pv->frame, 14 );
145 pv->size = dca_syncinfo( pv->state, pv->frame, &pv->flags_in, &pv->rate,
146 &pv->bitrate, &pv->frame_length );
147 if( pv->size )
149 /* It is. W00t. */
150 if( pv->error )
152 hb_log( "dca_syncinfo ok" );
154 pv->error = 0;
155 pv->sync = 1;
156 break;
159 /* It is not */
160 if( !pv->error )
162 hb_log( "dca_syncinfo failed" );
163 pv->error = 1;
166 /* Try one byte later */
167 hb_list_getbytes( pv->list, pv->frame, 1, NULL, NULL );
171 if( !pv->sync ||
172 hb_list_bytes( pv->list ) < pv->size )
174 /* Need more data */
175 return NULL;
178 /* Get the whole frame */
179 hb_list_getbytes( pv->list, pv->frame, pv->size, &pts, &pos );
181 /* Feed libdca */
182 dca_frame( pv->state, pv->frame, &pv->flags_out, &pv->level, 0 );
184 /* find out how many blocks are in this frame */
185 num_blocks = dca_blocks_num( pv->state );
187 /* num_blocks blocks per frame, 256 samples per block, channelsused channels */
188 buf = hb_buffer_init( num_blocks * 256 * pv->out_discrete_channels * sizeof( float ) );
189 buf->start = pts + ( pos / pv->size ) * num_blocks * 256 * 90000 / pv->rate;
190 buf->stop = buf->start + num_blocks * 256 * 90000 / pv->rate;
192 for( i = 0; i < num_blocks; i++ )
194 dca_sample_t * samples_in;
195 float * samples_out;
197 dca_block( pv->state );
198 samples_in = dca_samples( pv->state );
199 samples_out = ((float *) buf->data) + 256 * pv->out_discrete_channels * i;
201 /* Interleave */
202 for( j = 0; j < 256; j++ )
204 for ( k = 0; k < pv->out_discrete_channels; k++ )
206 samples_out[(pv->out_discrete_channels*j)+k] = samples_in[(256*k)+j] * 16384;
212 pv->sync = 0;
213 return buf;