OSDN Git Service

- support blu-ray, avchd & dvb x264
[handbrake-jp/handbrake-jp-git.git] / libhb / deca52.c
1 /* $Id: deca52.c,v 1.14 2005/03/03 17:21:57 titer Exp $
2
3    This file is part of the HandBrake source code.
4    Homepage: <http://handbrake.fr/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include "hb.h"
8
9 #include "a52dec/a52.h"
10
11 struct hb_work_private_s
12 {
13     hb_job_t    * job;
14
15     /* liba52 handle */
16     a52_state_t * state;
17
18     int           flags_in;
19     int           flags_out;
20     int           rate;
21     int           bitrate;
22     float         level;
23     float         dynamic_range_compression;
24
25     int           error;
26     int           sync;
27     int           size;
28
29     int64_t       next_expected_pts;
30
31     int64_t       sequence;
32
33     uint8_t       frame[3840];
34
35     hb_list_t   * list;
36
37         int           out_discrete_channels;
38
39 };
40
41 static int  deca52Init( hb_work_object_t *, hb_job_t * );
42 static int  deca52Work( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
43 static void deca52Close( hb_work_object_t * );
44 static int deca52BSInfo( hb_work_object_t * , const hb_buffer_t *,
45                          hb_work_info_t * );
46
47 hb_work_object_t hb_deca52 =
48 {
49     WORK_DECA52,
50     "AC3 decoder",
51     deca52Init,
52     deca52Work,
53     deca52Close,
54     0,
55     deca52BSInfo
56 };
57
58 /***********************************************************************
59  * Local prototypes
60  **********************************************************************/
61 static hb_buffer_t * Decode( hb_work_object_t * w );
62
63 /***********************************************************************
64  * dynrng_call
65  ***********************************************************************
66  * Boosts soft audio -- taken from gbooker's work in A52Decoder, comment and all..
67  * Two cases
68  * 1) The user requested a compression of 1 or less, return the typical power rule
69  * 2) The user requested a compression of more than 1 (decompression):
70  *    If the stream's requested compression is less than 1.0 (loud sound), return the normal compression
71  *    If the stream's requested compression is more than 1.0 (soft sound), use power rule (which will make
72  *   it louder in this case).
73  *
74  **********************************************************************/
75 static sample_t dynrng_call (sample_t c, void *data)
76 {
77         float *level = (float *)data;
78         float levelToUse = (float)*level;
79         if(c > 1.0 || levelToUse <= 1.0)
80         {
81             return powf(c, levelToUse);
82         }
83         else
84                 return c;
85 }
86
87 /***********************************************************************
88  * hb_work_deca52_init
89  ***********************************************************************
90  * Allocate the work object, initialize liba52
91  **********************************************************************/
92 static int deca52Init( hb_work_object_t * w, hb_job_t * job )
93 {
94     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
95     hb_audio_t * audio = w->audio;
96     w->private_data = pv;
97
98     pv->job   = job;
99
100     pv->list      = hb_list_init();
101     pv->state     = a52_init( 0 );
102
103         /* Decide what format we want out of a52dec
104         work.c has already done some of this deduction for us in do_job() */
105
106         pv->flags_out = HB_AMIXDOWN_GET_A52_FORMAT(audio->config.out.mixdown);
107
108         /* pass the number of channels used into the private work data */
109         /* will only be actually used if we're not doing AC3 passthru */
110     pv->out_discrete_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
111
112     pv->level     = 32768.0;
113     pv->dynamic_range_compression = audio->config.out.dynamic_range_compression;
114
115     pv->next_expected_pts = 0;
116     pv->sequence = 0;
117
118     return 0;
119 }
120
121 /***********************************************************************
122  * Close
123  ***********************************************************************
124  * Free memory
125  **********************************************************************/
126 static void deca52Close( hb_work_object_t * w )
127 {
128     hb_work_private_t * pv = w->private_data;
129     a52_free( pv->state );
130     hb_list_empty( &pv->list );
131     free( pv );
132     w->private_data = NULL;
133 }
134
135 /***********************************************************************
136  * Work
137  ***********************************************************************
138  * Add the given buffer to the data we already have, and decode as much
139  * as we can
140  **********************************************************************/
141 static int deca52Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
142                 hb_buffer_t ** buf_out )
143 {
144     hb_work_private_t * pv = w->private_data;
145     hb_buffer_t * buf;
146
147     if( buf_in && *buf_in )
148     {
149         pv->sequence = (*buf_in)->sequence;
150     }
151
152     hb_list_add( pv->list, *buf_in );
153     *buf_in = NULL;
154
155     /* If we got more than a frame, chain raw buffers */
156     *buf_out = buf = Decode( w );
157     while( buf )
158     {
159         buf->sequence = pv->sequence;
160         buf->next = Decode( w );
161         buf       = buf->next;
162     }
163
164     return HB_WORK_OK;
165 }
166
167 /***********************************************************************
168  * Decode
169  ***********************************************************************
170  *
171  **********************************************************************/
172 static hb_buffer_t * Decode( hb_work_object_t * w )
173 {
174     hb_work_private_t * pv = w->private_data;
175     hb_buffer_t * buf;
176     hb_audio_t  * audio = w->audio;
177     int           i, j, k;
178     uint64_t      pts, pos;
179
180     /* Get a frame header if don't have one yet */
181     if( !pv->sync )
182     {
183         while( hb_list_bytes( pv->list ) >= 7 )
184         {
185             /* We have 7 bytes, check if this is a correct header */
186             hb_list_seebytes( pv->list, pv->frame, 7 );
187             pv->size = a52_syncinfo( pv->frame, &pv->flags_in, &pv->rate,
188                                     &pv->bitrate );
189             if( pv->size )
190             {
191                 /* It is. W00t. */
192                 if( pv->error )
193                 {
194                     hb_log( "a52_syncinfo ok" );
195                 }
196                 pv->error = 0;
197                 pv->sync  = 1;
198                 break;
199             }
200
201             /* It is not */
202             if( !pv->error )
203             {
204                 hb_log( "a52_syncinfo failed" );
205                 pv->error = 1;
206             }
207
208             /* Try one byte later */
209             hb_list_getbytes( pv->list, pv->frame, 1, NULL, NULL );
210         }
211     }
212
213     if( !pv->sync ||
214         hb_list_bytes( pv->list ) < pv->size )
215     {
216         /* Need more data */
217         return NULL;
218     }
219
220     /* Get the whole frame */
221     hb_list_getbytes( pv->list, pv->frame, pv->size, &pts, &pos );
222     if (pts == -1)
223     {
224         pts = pv->next_expected_pts;
225     }
226
227     /* AC3 passthrough: don't decode the AC3 frame */
228     if( audio->config.out.codec == HB_ACODEC_AC3 )
229     {
230         buf = hb_buffer_init( pv->size );
231         memcpy( buf->data, pv->frame, pv->size );
232         buf->start = pts + ( pos / pv->size ) * 6 * 256 * 90000 / pv->rate;
233         buf->stop  = buf->start + 6 * 256 * 90000 / pv->rate;
234         pv->next_expected_pts = buf->stop;
235         pv->sync = 0;
236         return buf;
237     }
238
239     /* Feed liba52 */
240     a52_frame( pv->state, pv->frame, &pv->flags_out, &pv->level, 0 );
241
242     if ( pv->dynamic_range_compression > 1.0 )
243     {
244         a52_dynrng( pv->state, dynrng_call, &pv->dynamic_range_compression);
245     }
246
247     /* 6 blocks per frame, 256 samples per block, channelsused channels */
248     buf        = hb_buffer_init( 6 * 256 * pv->out_discrete_channels * sizeof( float ) );
249     buf->start = pts + ( pos / pv->size ) * 6 * 256 * 90000 / pv->rate;
250     buf->stop  = buf->start + 6 * 256 * 90000 / pv->rate;
251
252     /*
253        * To track AC3 PTS add this back in again.
254         *hb_log("AC3: pts is %lld, buf->start %lld buf->stop %lld", pts, buf->start, buf->stop);
255         */
256
257     pv->next_expected_pts = buf->stop;
258
259     for( i = 0; i < 6; i++ )
260     {
261         sample_t * samples_in;
262         float    * samples_out;
263
264         a52_block( pv->state );
265         samples_in  = a52_samples( pv->state );
266         samples_out = ((float *) buf->data) + 256 * pv->out_discrete_channels * i;
267
268         /* Interleave */
269         for( j = 0; j < 256; j++ )
270         {
271                         for ( k = 0; k < pv->out_discrete_channels; k++ )
272                         {
273                                 samples_out[(pv->out_discrete_channels*j)+k]   = samples_in[(256*k)+j];
274                         }
275         }
276
277     }
278
279     pv->sync = 0;
280     return buf;
281 }
282
283 static int deca52BSInfo( hb_work_object_t *w, const hb_buffer_t *b,
284                          hb_work_info_t *info )
285 {
286     int i, rate, bitrate, flags;
287
288     memset( info, 0, sizeof(*info) );
289
290     /* since AC3 frames don't line up with MPEG ES frames scan the
291      * entire frame for an AC3 sync pattern.  */
292     for ( i = 0; i < b->size - 7; ++i )
293     {
294         if( a52_syncinfo( &b->data[i], &flags, &rate, &bitrate ) != 0 )
295         {
296             break;
297         }
298     }
299     if ( i >= b->size - 7 )
300     {
301         /* didn't find AC3 sync */
302         return 0;
303     }
304
305     info->name = "AC-3";
306     info->rate = rate;
307     info->rate_base = 1;
308     info->bitrate = bitrate;
309     info->flags = flags;
310     if ( (flags & A52_CHANNEL_MASK) == A52_DOLBY )
311     {
312         info->flags |= AUDIO_F_DOLBY;
313     }
314
315     switch( flags & A52_CHANNEL_MASK )
316     {
317         /* mono sources */
318         case A52_MONO:
319         case A52_CHANNEL1:
320         case A52_CHANNEL2:
321             info->channel_layout = HB_INPUT_CH_LAYOUT_MONO;
322             break;
323         /* stereo input */
324         case A52_CHANNEL:
325         case A52_STEREO:
326             info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
327             break;
328         /* dolby (DPL1 aka Dolby Surround = 4.0 matrix-encoded) input */
329         case A52_DOLBY:
330             info->channel_layout = HB_INPUT_CH_LAYOUT_DOLBY;
331             break;
332         /* 3F/2R input */
333         case A52_3F2R:
334             info->channel_layout = HB_INPUT_CH_LAYOUT_3F2R;
335             break;
336         /* 3F/1R input */
337         case A52_3F1R:
338             info->channel_layout = HB_INPUT_CH_LAYOUT_3F1R;
339             break;
340         /* other inputs */
341         case A52_3F:
342             info->channel_layout = HB_INPUT_CH_LAYOUT_3F;
343             break;
344         case A52_2F1R:
345             info->channel_layout = HB_INPUT_CH_LAYOUT_2F1R;
346             break;
347         case A52_2F2R:
348             info->channel_layout = HB_INPUT_CH_LAYOUT_2F2R;
349             break;
350         /* unknown */
351         default:
352             info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
353     }
354
355     if (flags & A52_LFE)
356     {
357         info->channel_layout |= HB_INPUT_CH_LAYOUT_HAS_LFE;
358     }
359
360     return 1;
361 }