OSDN Git Service

ETSI TS 102 366 V1.2.1 compliant AC3 in MP4, replaces older hacked version that was...
[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)->size <= 0 )
148     {
149         /* EOF on input stream - send it downstream & say that we're done */
150         *buf_out = *buf_in;
151         *buf_in = NULL;
152         return HB_WORK_DONE;
153     }
154
155     pv->sequence = (*buf_in)->sequence;
156
157     hb_list_add( pv->list, *buf_in );
158     *buf_in = NULL;
159
160     /* If we got more than a frame, chain raw buffers */
161     *buf_out = buf = Decode( w );
162     while( buf )
163     {
164         buf->sequence = pv->sequence;
165         buf->next = Decode( w );
166         buf       = buf->next;
167     }
168
169     return HB_WORK_OK;
170 }
171
172 /***********************************************************************
173  * Decode
174  ***********************************************************************
175  *
176  **********************************************************************/
177 static hb_buffer_t * Decode( hb_work_object_t * w )
178 {
179     hb_work_private_t * pv = w->private_data;
180     hb_buffer_t * buf;
181     hb_audio_t  * audio = w->audio;
182     int           i, j, k;
183     uint64_t      pts, pos;
184
185     /* Get a frame header if don't have one yet */
186     if( !pv->sync )
187     {
188         while( hb_list_bytes( pv->list ) >= 7 )
189         {
190             /* We have 7 bytes, check if this is a correct header */
191             hb_list_seebytes( pv->list, pv->frame, 7 );
192             pv->size = a52_syncinfo( pv->frame, &pv->flags_in, &pv->rate,
193                                     &pv->bitrate );
194             if( pv->size )
195             {
196                 /* It is. W00t. */
197                 if( pv->error )
198                 {
199                     hb_log( "a52_syncinfo ok" );
200                 }
201                 pv->error = 0;
202                 pv->sync  = 1;
203                 break;
204             }
205
206             /* It is not */
207             if( !pv->error )
208             {
209                 hb_log( "a52_syncinfo failed" );
210                 pv->error = 1;
211             }
212
213             /* Try one byte later */
214             hb_list_getbytes( pv->list, pv->frame, 1, NULL, NULL );
215         }
216     }
217
218     if( !pv->sync ||
219         hb_list_bytes( pv->list ) < pv->size )
220     {
221         /* Need more data */
222         return NULL;
223     }
224
225     /* Get the whole frame */
226     hb_list_getbytes( pv->list, pv->frame, pv->size, &pts, &pos );
227     if (pts == -1)
228     {
229         pts = pv->next_expected_pts;
230     }
231
232     /* AC3 passthrough: don't decode the AC3 frame */
233     if( audio->config.out.codec == HB_ACODEC_AC3 )
234     {
235         buf = hb_buffer_init( pv->size );
236         memcpy( buf->data, pv->frame, pv->size );
237         buf->start = pts + ( pos / pv->size ) * 6 * 256 * 90000 / pv->rate;
238         buf->stop  = buf->start + 6 * 256 * 90000 / pv->rate;
239         pv->next_expected_pts = buf->stop;
240         pv->sync = 0;
241         return buf;
242     }
243
244     /* Feed liba52 */
245     a52_frame( pv->state, pv->frame, &pv->flags_out, &pv->level, 0 );
246
247     if ( pv->dynamic_range_compression > 1.0 )
248     {
249         a52_dynrng( pv->state, dynrng_call, &pv->dynamic_range_compression);
250     }
251
252     /* 6 blocks per frame, 256 samples per block, channelsused channels */
253     buf        = hb_buffer_init( 6 * 256 * pv->out_discrete_channels * sizeof( float ) );
254     buf->start = pts + ( pos / pv->size ) * 6 * 256 * 90000 / pv->rate;
255     buf->stop  = buf->start + 6 * 256 * 90000 / pv->rate;
256
257     /*
258        * To track AC3 PTS add this back in again.
259         *hb_log("AC3: pts is %lld, buf->start %lld buf->stop %lld", pts, buf->start, buf->stop);
260         */
261
262     pv->next_expected_pts = buf->stop;
263
264     for( i = 0; i < 6; i++ )
265     {
266         sample_t * samples_in;
267         float    * samples_out;
268
269         a52_block( pv->state );
270         samples_in  = a52_samples( pv->state );
271         samples_out = ((float *) buf->data) + 256 * pv->out_discrete_channels * i;
272
273         /* Interleave */
274         for( j = 0; j < 256; j++ )
275         {
276                         for ( k = 0; k < pv->out_discrete_channels; k++ )
277                         {
278                                 samples_out[(pv->out_discrete_channels*j)+k]   = samples_in[(256*k)+j];
279                         }
280         }
281
282     }
283
284     pv->sync = 0;
285     return buf;
286 }
287
288 static int deca52BSInfo( hb_work_object_t *w, const hb_buffer_t *b,
289                          hb_work_info_t *info )
290 {
291     int i;
292     int rate = 0, bitrate = 0, flags = 0;
293     int old_rate = 0, old_bitrate = 0;
294     uint8_t raw;
295
296     memset( info, 0, sizeof(*info) );
297
298     /* since AC3 frames don't line up with MPEG ES frames scan the
299      * entire frame for an AC3 sync pattern.  */
300     for ( i = 0; i < b->size - 7; ++i )
301     {
302         if( a52_syncinfo( &b->data[i], &flags, &rate, &bitrate ) != 0 )
303         {
304             /*
305              * Got sync apparently, save these values and check that they
306              * also match when we get sync again.
307              */
308             if( old_rate ) 
309             {
310                 if( rate == old_rate && bitrate == old_bitrate )
311                 {
312                     break;
313                 } 
314             } 
315             
316             old_rate = rate;
317             old_bitrate = bitrate;
318             raw = b->data[i+5];
319         }
320     }
321     if ( rate == 0 || bitrate == 0 )
322     {
323         /* didn't find AC3 sync */
324         return 0;
325     }
326
327     /*
328      * bsid | bsmod | acmod | cmixlev | surmixlev | dsurmod | lfeon | dialnorm | compre
329      *    5       3       3         2           2         2       1          5        1
330      * [    byte1  ][         byte2                    ][   byte3                     ]
331      */
332
333
334     info->name = "AC-3";
335     info->rate = rate;
336     info->rate_base = 1;
337     info->bitrate = bitrate;
338     info->flags = flags;
339     info->version = raw >> 3;    /* bsid is the first 5 bits */
340     info->mode = raw & 0x7;      /* bsmod is the following 3 bits */
341
342     if ( (flags & A52_CHANNEL_MASK) == A52_DOLBY )
343     {
344         info->flags |= AUDIO_F_DOLBY;
345     }
346
347     switch( flags & A52_CHANNEL_MASK )
348     {
349         /* mono sources */
350         case A52_MONO:
351         case A52_CHANNEL1:
352         case A52_CHANNEL2:
353             info->channel_layout = HB_INPUT_CH_LAYOUT_MONO;
354             break;
355         /* stereo input */
356         case A52_CHANNEL:
357         case A52_STEREO:
358             info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
359             break;
360         /* dolby (DPL1 aka Dolby Surround = 4.0 matrix-encoded) input */
361         case A52_DOLBY:
362             info->channel_layout = HB_INPUT_CH_LAYOUT_DOLBY;
363             break;
364         /* 3F/2R input */
365         case A52_3F2R:
366             info->channel_layout = HB_INPUT_CH_LAYOUT_3F2R;
367             break;
368         /* 3F/1R input */
369         case A52_3F1R:
370             info->channel_layout = HB_INPUT_CH_LAYOUT_3F1R;
371             break;
372         /* other inputs */
373         case A52_3F:
374             info->channel_layout = HB_INPUT_CH_LAYOUT_3F;
375             break;
376         case A52_2F1R:
377             info->channel_layout = HB_INPUT_CH_LAYOUT_2F1R;
378             break;
379         case A52_2F2R:
380             info->channel_layout = HB_INPUT_CH_LAYOUT_2F2R;
381             break;
382         /* unknown */
383         default:
384             info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
385     }
386
387     if (flags & A52_LFE)
388     {
389         info->channel_layout |= HB_INPUT_CH_LAYOUT_HAS_LFE;
390     }
391
392     return 1;
393 }