OSDN Git Service

fix audio clipping when downmixing and output codec is lame
[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 #include "libavutil/crc.h"
11
12 struct hb_work_private_s
13 {
14     hb_job_t    * job;
15
16     /* liba52 handle */
17     a52_state_t * state;
18
19     int           flags_in;
20     int           flags_out;
21     int           rate;
22     int           bitrate;
23     int           out_discrete_channels;
24     int           error;
25     int           frames;                   // number of good frames decoded
26     int           crc_errors;               // number of frames with crc errors
27     int           bytes_dropped;            // total bytes dropped while resyncing
28     float         level;
29     float         dynamic_range_compression;
30     double        next_expected_pts;
31     int64_t       last_buf_pts;
32     hb_list_t    *list;
33     const AVCRC  *crc_table;
34     uint8_t       frame[3840];
35 };
36
37 static int  deca52Init( hb_work_object_t *, hb_job_t * );
38 static int  deca52Work( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
39 static void deca52Close( hb_work_object_t * );
40 static int deca52BSInfo( hb_work_object_t * , const hb_buffer_t *,
41                          hb_work_info_t * );
42
43 hb_work_object_t hb_deca52 =
44 {
45     WORK_DECA52,
46     "AC3 decoder",
47     deca52Init,
48     deca52Work,
49     deca52Close,
50     0,
51     deca52BSInfo
52 };
53
54 /***********************************************************************
55  * Local prototypes
56  **********************************************************************/
57 static hb_buffer_t * Decode( hb_work_object_t * w );
58
59 /***********************************************************************
60  * dynrng_call
61  ***********************************************************************
62  * Boosts soft audio -- taken from gbooker's work in A52Decoder, comment and all..
63  * Two cases
64  * 1) The user requested a compression of 1 or less, return the typical power rule
65  * 2) The user requested a compression of more than 1 (decompression):
66  *    If the stream's requested compression is less than 1.0 (loud sound), return the normal compression
67  *    If the stream's requested compression is more than 1.0 (soft sound), use power rule (which will make
68  *   it louder in this case).
69  *
70  **********************************************************************/
71 static sample_t dynrng_call (sample_t c, void *data)
72 {
73         float *level = (float *)data;
74         float levelToUse = (float)*level;
75         if(c > 1.0 || levelToUse <= 1.0)
76         {
77             return powf(c, levelToUse);
78         }
79         else
80                 return c;
81 }
82
83 /***********************************************************************
84  * hb_work_deca52_init
85  ***********************************************************************
86  * Allocate the work object, initialize liba52
87  **********************************************************************/
88 static int deca52Init( hb_work_object_t * w, hb_job_t * job )
89 {
90     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
91     hb_audio_t * audio = w->audio;
92     w->private_data = pv;
93
94     pv->job   = job;
95
96     pv->crc_table = av_crc_get_table( AV_CRC_16_ANSI );
97     pv->list      = hb_list_init();
98     pv->state     = a52_init( 0 );
99
100     /* Decide what format we want out of a52dec
101     work.c has already done some of this deduction for us in do_job() */
102
103     pv->flags_out = HB_AMIXDOWN_GET_A52_FORMAT(audio->config.out.mixdown);
104     if ( audio->config.out.codec == HB_ACODEC_LAME )
105         pv->flags_out |= A52_ADJUST_LEVEL;
106
107     /* pass the number of channels used into the private work data */
108     /* will only be actually used if we're not doing AC3 passthru */
109     pv->out_discrete_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
110
111     pv->level     = 32768.0;
112     pv->dynamic_range_compression = audio->config.out.dynamic_range_compression;
113
114     return 0;
115 }
116
117 /***********************************************************************
118  * Close
119  ***********************************************************************
120  * Free memory
121  **********************************************************************/
122 static void deca52Close( hb_work_object_t * w )
123 {
124     hb_work_private_t * pv = w->private_data;
125
126     if ( pv->crc_errors )
127     {
128         hb_log( "deca52: %d frames decoded, %d crc errors, %d bytes dropped",
129                 pv->frames, pv->crc_errors, pv->bytes_dropped );
130     }
131     a52_free( pv->state );
132     hb_list_empty( &pv->list );
133     free( pv );
134     w->private_data = NULL;
135 }
136
137 /***********************************************************************
138  * Work
139  ***********************************************************************
140  * Add the given buffer to the data we already have, and decode as much
141  * as we can
142  **********************************************************************/
143 static int deca52Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
144                 hb_buffer_t ** buf_out )
145 {
146     hb_work_private_t * pv = w->private_data;
147     hb_buffer_t * buf;
148
149     if ( (*buf_in)->size <= 0 )
150     {
151         /* EOF on input stream - send it downstream & say that we're done */
152         *buf_out = *buf_in;
153         *buf_in = NULL;
154         return HB_WORK_DONE;
155     }
156
157     if ( (*buf_in)->start < -1 && pv->next_expected_pts == 0 )
158     {
159         // discard buffers that start before video time 0
160         *buf_out = NULL;
161         return HB_WORK_OK;
162     }
163
164     hb_list_add( pv->list, *buf_in );
165     *buf_in = NULL;
166
167     /* If we got more than a frame, chain raw buffers */
168     *buf_out = buf = Decode( w );
169     while( buf )
170     {
171         buf->next = Decode( w );
172         buf       = buf->next;
173     }
174
175     return HB_WORK_OK;
176 }
177
178 /***********************************************************************
179  * Decode
180  ***********************************************************************
181  *
182  **********************************************************************/
183 static hb_buffer_t * Decode( hb_work_object_t * w )
184 {
185     hb_work_private_t * pv = w->private_data;
186     hb_buffer_t * buf;
187     hb_audio_t  * audio = w->audio;
188     int           i, j, k;
189     int           size = 0;
190
191     // check that we're at the start of a valid frame and align to the
192     // start of a valid frame if we're not.
193     // we have to check the header & crc so we need at least
194     // 7 (the header size) + 128 (the minimum frame size) bytes
195     while( hb_list_bytes( pv->list ) >= 7+128 )
196     {
197         /* check if this is a valid header */
198         hb_list_seebytes( pv->list, pv->frame, 7 );
199         size = a52_syncinfo( pv->frame, &pv->flags_in, &pv->rate, &pv->bitrate );
200         if ( size > 0 )
201         {
202             // header looks valid - check the crc1
203             if( size > hb_list_bytes( pv->list ) )
204             {
205                 // don't have all the frame's data yet
206                 return NULL;
207             }
208             int crc1size = (size >> 1) + (size >> 3);
209             hb_list_seebytes( pv->list, pv->frame, crc1size );
210             if ( av_crc( pv->crc_table, 0, pv->frame + 2, crc1size - 2 ) == 0 )
211             {
212                 // crc1 is ok - say we have valid frame sync
213                 if( pv->error )
214                 {
215                     hb_log( "output track %d: ac3 in sync after skipping %d bytes",
216                             audio->config.out.track, pv->error );
217                     pv->bytes_dropped += pv->error;
218                     pv->error = 0;
219                 }
220                 break;
221             }
222         }
223         // no sync - discard one byte then try again
224         hb_list_getbytes( pv->list, pv->frame, 1, NULL, NULL );
225         ++pv->error;
226     }
227
228     // we exit the above loop either in error state (we didn't find sync
229     // or don't have enough data yet to validate sync) or in sync. If we're
230     // not in sync we need more data so just return.
231     if( pv->error || size <= 0 || hb_list_bytes( pv->list ) < size )
232     {
233         /* Need more data */
234         return NULL;
235     }
236
237     // Get the whole frame and check its CRC. If the CRC is wrong
238     // discard the frame - we'll resync on the next call.
239
240     uint64_t ipts;
241     hb_list_getbytes( pv->list, pv->frame, size, &ipts, NULL );
242     if ( av_crc( pv->crc_table, 0, pv->frame + 2, size - 2 ) != 0 )
243     {
244         ++pv->crc_errors;
245         return NULL;
246     }
247     ++pv->frames;
248     if ( ipts != pv->last_buf_pts )
249     {
250         pv->last_buf_pts = ipts;
251     }
252     else
253     {
254         // spec says that the PTS is the start time of the first frame
255         // that starts in the PES frame so we only use the PTS once then
256         // get the following frames' PTS from the frame length.
257         ipts = -1;
258     }
259
260     double pts = ( ipts != -1 ) ? ipts : pv->next_expected_pts;
261     double frame_dur = (6. * 256. * 90000.) / pv->rate;
262
263     /* AC3 passthrough: don't decode the AC3 frame */
264     if( audio->config.out.codec == HB_ACODEC_AC3 )
265     {
266         buf = hb_buffer_init( size );
267         memcpy( buf->data, pv->frame, size );
268         buf->start = pts;
269         pts += frame_dur;
270         buf->stop  = pts;
271         pv->next_expected_pts = pts;
272         return buf;
273     }
274
275     /* Feed liba52 */
276     a52_frame( pv->state, pv->frame, &pv->flags_out, &pv->level, 0 );
277
278     /* If a user specifies strong dynamic range compression (>1), adjust it.
279        If a user specifies default dynamic range compression (1), leave it alone.
280        If a user specifies no dynamic range compression (0), call a null function. */
281     if( pv->dynamic_range_compression > 1.0 )
282     {
283         a52_dynrng( pv->state, dynrng_call, &pv->dynamic_range_compression );
284     }
285     else if( !pv->dynamic_range_compression )
286     {
287         a52_dynrng( pv->state, NULL, NULL );
288     }
289
290     /* 6 blocks per frame, 256 samples per block, channelsused channels */
291     buf        = hb_buffer_init( 6 * 256 * pv->out_discrete_channels * sizeof( float ) );
292     buf->start = pts;
293     pts += frame_dur;
294     buf->stop  = pts;
295     pv->next_expected_pts = pts;
296
297     for( i = 0; i < 6; i++ )
298     {
299         sample_t * samples_in;
300         float    * samples_out;
301
302         a52_block( pv->state );
303         samples_in  = a52_samples( pv->state );
304         samples_out = ((float *) buf->data) + 256 * pv->out_discrete_channels * i;
305
306         /* Interleave */
307         for( j = 0; j < 256; j++ )
308         {
309             for ( k = 0; k < pv->out_discrete_channels; k++ )
310             {
311                 samples_out[(pv->out_discrete_channels*j)+k]   = samples_in[(256*k)+j];
312             }
313         }
314
315     }
316     return buf;
317 }
318
319 static int find_sync( const uint8_t *buf, int len )
320 {
321     int i;
322
323     // since AC3 frames don't line up with MPEG ES frames scan the
324     // frame for an AC3 sync pattern.
325     for ( i = 0; i < len - 16; ++i )
326     {
327         int rate, bitrate, flags;
328         int size = a52_syncinfo( (uint8_t *)buf + i, &flags, &rate, &bitrate );
329         if( size > 0 )
330         {
331             // we have a plausible sync header - see if crc1 checks
332             int crc1size = (size >> 1) + (size >> 3); 
333             if ( i + crc1size > len )
334             {
335                 // don't have enough data to check crc1
336                 break;
337             }
338             if ( av_crc( av_crc_get_table( AV_CRC_16_ANSI ), 0,
339                          buf + i + 2, crc1size - 2 ) == 0 )
340             {
341                 // crc checks - we've got sync
342                 return i;
343             }
344         }
345     }
346     return -1;
347 }
348
349 static int deca52BSInfo( hb_work_object_t *w, const hb_buffer_t *b,
350                          hb_work_info_t *info )
351 {
352     memset( info, 0, sizeof(*info) );
353
354     // We don't know if the way that AC3 frames are fragmented into whatever
355     // packetization the container uses will give us enough bytes per fragment
356     // to check the CRC (we need at least 5/8 of the the frame). So we
357     // copy the fragment we got into an accumulation buffer in the audio object
358     // then look for sync over all the frags we've accumulated so far.
359     uint8_t *buf = w->audio->priv.config.a52.buf;
360     int len = w->audio->priv.config.a52.len, blen = b->size;
361     if ( len + blen > sizeof(w->audio->priv.config.a52.buf) )
362     {
363         // we don't have enough empty space in the accumulation buffer to
364         // hold the new frag - make room for it by discarding the oldest data.
365         if ( blen >= sizeof(w->audio->priv.config.a52.buf) )
366         {
367             // the frag is bigger than our accumulation buffer - copy all
368             // that will fit (the excess doesn't matter since the buffer
369             // is many times the size of a max length ac3 frame).
370             blen = sizeof(w->audio->priv.config.a52.buf);
371             len = 0;
372         }
373         else
374         {
375             // discard enough bytes from the front of the buffer to make
376             // room for the new stuff
377             int newlen = sizeof(w->audio->priv.config.a52.buf) - blen;
378             memcpy( buf, buf + len - newlen, newlen );
379             len = newlen;
380         }
381     }
382     // add the new frag to the buffer
383     memcpy( buf+len, b->data, blen );
384     len += blen;
385
386     int i;
387     if ( ( i = find_sync( buf, len ) ) < 0 )
388     {
389         // didn't find sync - wait for more data
390         w->audio->priv.config.a52.len = len;
391         return 0;
392     }
393
394     // got sync - extract and canoncalize the bitstream parameters
395     int rate = 0, bitrate = 0, flags = 0;
396     uint8_t raw = buf[i + 5];
397     a52_syncinfo( buf + i, &flags, &rate, &bitrate );
398
399     if ( rate == 0 || bitrate == 0 )
400     {
401         // invalid AC-3 parameters - toss what we have so we'll start over
402         // with the next buf otherwise we'll keep syncing on this junk.
403         w->audio->priv.config.a52.len = 0;
404         return 0;
405     }
406
407     // bsid | bsmod | acmod | cmixlv | surmixlv | dsurmod | lfeon | dialnorm | compre
408     //   5       3      3        2         2         2        1          5        1
409     //      byte1   |          byte2                 |    byte3  
410
411     info->name = "AC-3";
412     info->rate = rate;
413     info->rate_base = 1;
414     info->bitrate = bitrate;
415     info->flags = flags;
416     info->version = raw >> 3;    /* bsid is the first 5 bits */
417     info->mode = raw & 0x7;      /* bsmod is the following 3 bits */
418
419     if ( (flags & A52_CHANNEL_MASK) == A52_DOLBY )
420     {
421         info->flags |= AUDIO_F_DOLBY;
422     }
423
424     switch( flags & A52_CHANNEL_MASK )
425     {
426         /* mono sources */
427         case A52_MONO:
428         case A52_CHANNEL1:
429         case A52_CHANNEL2:
430             info->channel_layout = HB_INPUT_CH_LAYOUT_MONO;
431             break;
432         /* stereo input */
433         case A52_CHANNEL:
434         case A52_STEREO:
435             info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
436             break;
437         /* dolby (DPL1 aka Dolby Surround = 4.0 matrix-encoded) input */
438         case A52_DOLBY:
439             info->channel_layout = HB_INPUT_CH_LAYOUT_DOLBY;
440             break;
441         /* 3F/2R input */
442         case A52_3F2R:
443             info->channel_layout = HB_INPUT_CH_LAYOUT_3F2R;
444             break;
445         /* 3F/1R input */
446         case A52_3F1R:
447             info->channel_layout = HB_INPUT_CH_LAYOUT_3F1R;
448             break;
449         /* other inputs */
450         case A52_3F:
451             info->channel_layout = HB_INPUT_CH_LAYOUT_3F;
452             break;
453         case A52_2F1R:
454             info->channel_layout = HB_INPUT_CH_LAYOUT_2F1R;
455             break;
456         case A52_2F2R:
457             info->channel_layout = HB_INPUT_CH_LAYOUT_2F2R;
458             break;
459         /* unknown */
460         default:
461             info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
462     }
463
464     if (flags & A52_LFE)
465     {
466         info->channel_layout |= HB_INPUT_CH_LAYOUT_HAS_LFE;
467     }
468
469     return 1;
470 }