OSDN Git Service

CLI: Add support for naming audio tracks to the HandBrakeCLI, thanks to LePetomane...
[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, rate, bitrate, flags;
292
293     memset( info, 0, sizeof(*info) );
294
295     /* since AC3 frames don't line up with MPEG ES frames scan the
296      * entire frame for an AC3 sync pattern.  */
297     for ( i = 0; i < b->size - 7; ++i )
298     {
299         if( a52_syncinfo( &b->data[i], &flags, &rate, &bitrate ) != 0 )
300         {
301             break;
302         }
303     }
304     if ( i >= b->size - 7 )
305     {
306         /* didn't find AC3 sync */
307         return 0;
308     }
309
310     info->name = "AC-3";
311     info->rate = rate;
312     info->rate_base = 1;
313     info->bitrate = bitrate;
314     info->flags = flags;
315     if ( (flags & A52_CHANNEL_MASK) == A52_DOLBY )
316     {
317         info->flags |= AUDIO_F_DOLBY;
318     }
319
320     switch( flags & A52_CHANNEL_MASK )
321     {
322         /* mono sources */
323         case A52_MONO:
324         case A52_CHANNEL1:
325         case A52_CHANNEL2:
326             info->channel_layout = HB_INPUT_CH_LAYOUT_MONO;
327             break;
328         /* stereo input */
329         case A52_CHANNEL:
330         case A52_STEREO:
331             info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
332             break;
333         /* dolby (DPL1 aka Dolby Surround = 4.0 matrix-encoded) input */
334         case A52_DOLBY:
335             info->channel_layout = HB_INPUT_CH_LAYOUT_DOLBY;
336             break;
337         /* 3F/2R input */
338         case A52_3F2R:
339             info->channel_layout = HB_INPUT_CH_LAYOUT_3F2R;
340             break;
341         /* 3F/1R input */
342         case A52_3F1R:
343             info->channel_layout = HB_INPUT_CH_LAYOUT_3F1R;
344             break;
345         /* other inputs */
346         case A52_3F:
347             info->channel_layout = HB_INPUT_CH_LAYOUT_3F;
348             break;
349         case A52_2F1R:
350             info->channel_layout = HB_INPUT_CH_LAYOUT_2F1R;
351             break;
352         case A52_2F2R:
353             info->channel_layout = HB_INPUT_CH_LAYOUT_2F2R;
354             break;
355         /* unknown */
356         default:
357             info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
358     }
359
360     if (flags & A52_LFE)
361     {
362         info->channel_layout |= HB_INPUT_CH_LAYOUT_HAS_LFE;
363     }
364
365     return 1;
366 }