]> git.sesse.net Git - vlc/blob - modules/codec/lpcm.c
lpcm: Fix a few unsigned comparison warnings.
[vlc] / modules / codec / lpcm.c
1 /*****************************************************************************
2  * lpcm.c: lpcm decoder/packetizer module
3  *****************************************************************************
4  * Copyright (C) 1999-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Henri Fallon <henri@videolan.org>
9  *          Christophe Massiot <massiot@via.ecp.fr>
10  *          Gildas Bazin <gbazin@videolan.org>
11  *          Lauren Aimar <fenrir _AT_ videolan _DOT_ org >
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_codec.h>
38 #include <vlc_aout.h>
39 #include <assert.h>
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44 static int  OpenDecoder   ( vlc_object_t * );
45 static int  OpenPacketizer( vlc_object_t * );
46 static void CloseCommon   ( vlc_object_t * );
47
48 vlc_module_begin ()
49
50     set_category( CAT_INPUT )
51     set_subcategory( SUBCAT_INPUT_ACODEC )
52     set_description( N_("Linear PCM audio decoder") )
53     set_capability( "decoder", 100 )
54     set_callbacks( OpenDecoder, CloseCommon )
55
56     add_submodule ()
57     set_description( N_("Linear PCM audio packetizer") )
58     set_capability( "packetizer", 100 )
59     set_callbacks( OpenPacketizer, CloseCommon )
60
61 vlc_module_end ()
62
63
64 /*****************************************************************************
65  * decoder_sys_t : lpcm decoder descriptor
66  *****************************************************************************/
67 struct decoder_sys_t
68 {
69     /* Module mode */
70     bool b_packetizer;
71
72     /*
73      * Output properties
74      */
75     date_t   end_date;
76
77     /* */
78     unsigned i_header_size;
79     int      i_type;
80 };
81
82 /*
83  * LPCM DVD header :
84  * - frame number (8 bits)
85  * - unknown (16 bits) == 0x0003 ?
86  * - unknown (4 bits)
87  * - current frame (4 bits)
88  * - unknown (2 bits)
89  * - frequency (2 bits) 0 == 48 kHz, 1 == 32 kHz, 2 == ?, 3 == ?
90  * - unknown (1 bit)
91  * - number of channels - 1 (3 bits) 1 == 2 channels
92  * - start code (8 bits) == 0x80
93  *
94  * LPCM DVD-A header (http://dvd-audio.sourceforge.net/spec/aob.shtml)
95  * - continuity counter (8 bits, clipped to 0x00-0x1f)
96  * - header size (16 bits)
97  * - byte pointer to start of first audio frame.
98  * - unknown (8bits, 0x10 for stereo, 0x00 for surround)
99  * - sample size (4+4 bits)
100  * - samplerate (4+4 bits)
101  * - unknown (8 bits)
102  * - group assignment (8 bits)
103  * - unknown (8 bits)
104  * - padding(variable)
105  *
106  * LPCM BD header :
107  * - unkown (16 bits)
108  * - number of channels (4 bits)
109  * - frequency (4 bits)
110  * - bits per sample (2 bits)
111  * - unknown (6 bits)
112  */
113
114 #define LPCM_VOB_HEADER_LEN (6)
115 #define LPCM_AOB_HEADER_LEN (11)
116 #define LPCM_BD_HEADER_LEN (4)
117
118 enum
119 {
120     LPCM_VOB,
121     LPCM_AOB,
122     LPCM_BD,
123 };
124
125 typedef struct
126 {
127     unsigned i_channels;
128     bool     b_used;
129     unsigned pi_position[6];
130 } aob_group_t;
131
132 /*****************************************************************************
133  * Local prototypes
134  *****************************************************************************/
135 static void *DecodeFrame  ( decoder_t *, block_t ** );
136
137 /* */
138 static int VobHeader( unsigned *pi_rate,
139                       unsigned *pi_channels, unsigned *pi_original_channels,
140                       unsigned *pi_bits,
141                       const uint8_t *p_header );
142 static void VobExtract( aout_buffer_t *, block_t *, unsigned i_bits );
143 /* */
144 static int AobHeader( unsigned *pi_rate,
145                       unsigned *pi_channels, unsigned *pi_layout,
146                       unsigned *pi_bits,
147                       unsigned *pi_padding,
148                       aob_group_t g[2],
149                       const uint8_t *p_header );
150 static void AobExtract( aout_buffer_t *, block_t *, unsigned i_bits, aob_group_t p_group[2] );
151 /* */
152 static int BdHeader( unsigned *pi_rate,
153                      unsigned *pi_channels, unsigned *pi_original_channels,
154                      unsigned *pi_bits,
155                      const uint8_t *p_header );
156 static void BdExtract( aout_buffer_t *, block_t * );
157
158
159 /*****************************************************************************
160  * OpenCommon:
161  *****************************************************************************/
162 static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
163 {
164     decoder_t *p_dec = (decoder_t*)p_this;
165     decoder_sys_t *p_sys;
166     int i_type;
167     int i_header_size;
168
169     switch( p_dec->fmt_in.i_codec )
170     {
171     /* DVD LPCM */
172     case VLC_CODEC_DVD_LPCM:
173         i_type = LPCM_VOB;
174         i_header_size = LPCM_VOB_HEADER_LEN;
175         break;
176     /* DVD-Audio LPCM */
177     case VLC_CODEC_DVDA_LPCM:
178         i_type = LPCM_AOB;
179         i_header_size = LPCM_AOB_HEADER_LEN;
180         break;
181     /* BD LPCM */
182     case VLC_CODEC_BD_LPCM:
183         i_type = LPCM_BD;
184         i_header_size = LPCM_BD_HEADER_LEN;
185         break;
186     default:
187         return VLC_EGENERIC;
188     }
189
190     /* Allocate the memory needed to store the decoder's structure */
191     if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
192         return VLC_ENOMEM;
193
194     /* Misc init */
195     p_sys->b_packetizer = b_packetizer;
196     date_Set( &p_sys->end_date, 0 );
197     p_sys->i_type = i_type;
198     p_sys->i_header_size = i_header_size;
199
200     /* Set output properties */
201     p_dec->fmt_out.i_cat = AUDIO_ES;
202
203     if( b_packetizer )
204     {
205         switch( i_type )
206         {
207         case LPCM_VOB:
208             p_dec->fmt_out.i_codec = VLC_CODEC_DVD_LPCM;
209             break;
210         case LPCM_AOB:
211             p_dec->fmt_out.i_codec = VLC_CODEC_DVDA_LPCM;
212             break;
213         default:
214             assert(0);
215         case LPCM_BD:
216             p_dec->fmt_out.i_codec = VLC_CODEC_BD_LPCM;
217             break;
218         }
219     }
220     else
221     {
222         switch( p_dec->fmt_out.audio.i_bitspersample )
223         {
224         case 24:
225         case 20:
226             p_dec->fmt_out.i_codec = VLC_CODEC_S24B;
227             p_dec->fmt_out.audio.i_bitspersample = 24;
228             break;
229         default:
230             p_dec->fmt_out.i_codec = VLC_CODEC_S16B;
231             p_dec->fmt_out.audio.i_bitspersample = 16;
232             break;
233         }
234     }
235
236     /* Set callback */
237     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
238         DecodeFrame;
239     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
240         DecodeFrame;
241
242     return VLC_SUCCESS;
243 }
244 static int OpenDecoder( vlc_object_t *p_this )
245 {
246     return OpenCommon( p_this, false );
247 }
248 static int OpenPacketizer( vlc_object_t *p_this )
249 {
250     return OpenCommon( p_this, true );
251 }
252
253 /*****************************************************************************
254  * DecodeFrame: decodes an lpcm frame.
255  ****************************************************************************
256  * Beware, this function must be fed with complete frames (PES packet).
257  *****************************************************************************/
258 static void *DecodeFrame( decoder_t *p_dec, block_t **pp_block )
259 {
260     decoder_sys_t *p_sys = p_dec->p_sys;
261     block_t       *p_block;
262     unsigned int  i_rate = 0, i_original_channels = 0, i_channels = 0, i_bits = 0;
263     int           i_frame_length;
264
265     if( !pp_block || !*pp_block ) return NULL;
266
267     p_block = *pp_block;
268     *pp_block = NULL; /* So the packet doesn't get re-sent */
269
270     /* Date management */
271     if( p_block->i_pts > 0 &&
272         p_block->i_pts != date_Get( &p_sys->end_date ) )
273     {
274         date_Set( &p_sys->end_date, p_block->i_pts );
275     }
276
277     if( !date_Get( &p_sys->end_date ) )
278     {
279         /* We've just started the stream, wait for the first PTS. */
280         block_Release( p_block );
281         return NULL;
282     }
283
284     if( p_block->i_buffer <= p_sys->i_header_size )
285     {
286         msg_Err(p_dec, "frame is too short");
287         block_Release( p_block );
288         return NULL;
289     }
290
291     int i_ret;
292     unsigned i_padding = 0;
293     aob_group_t p_aob_group[2];
294     switch( p_sys->i_type )
295     {
296     case LPCM_VOB:
297         i_ret = VobHeader( &i_rate, &i_channels, &i_original_channels, &i_bits,
298                            p_block->p_buffer );
299         break;
300     case LPCM_AOB:
301         i_ret = AobHeader( &i_rate, &i_channels, &i_original_channels, &i_bits, &i_padding,
302                            p_aob_group,
303                            p_block->p_buffer );
304         break;
305     case LPCM_BD:
306         i_ret = BdHeader( &i_rate, &i_channels, &i_original_channels, &i_bits,
307                           p_block->p_buffer );
308         break;
309     default:
310         assert(0);
311     }
312
313     if( i_ret || p_block->i_buffer <= p_sys->i_header_size + i_padding )
314     {
315         msg_Warn( p_dec, "no frame sync or too small frame" );
316         block_Release( p_block );
317         return NULL;
318     }
319
320     /* Set output properties */
321     if( p_dec->fmt_out.audio.i_rate != i_rate )
322     {
323         date_Init( &p_sys->end_date, i_rate, 1 );
324         date_Set( &p_sys->end_date, p_block->i_pts );
325     }
326     p_dec->fmt_out.audio.i_rate = i_rate;
327     p_dec->fmt_out.audio.i_channels = i_channels;
328     p_dec->fmt_out.audio.i_original_channels = i_original_channels;
329     p_dec->fmt_out.audio.i_physical_channels = i_original_channels & AOUT_CHAN_PHYSMASK;
330
331     i_frame_length = (p_block->i_buffer - p_sys->i_header_size - i_padding) / i_channels * 8 / i_bits;
332
333     if( p_sys->b_packetizer )
334     {
335         p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
336         p_block->i_length =
337             date_Increment( &p_sys->end_date, i_frame_length ) -
338             p_block->i_pts;
339
340         /* Just pass on the incoming frame */
341         return p_block;
342     }
343     else
344     {
345         /* */
346         if( i_bits == 16 )
347         {
348             p_dec->fmt_out.i_codec = VLC_CODEC_S16B;
349             p_dec->fmt_out.audio.i_bitspersample = 16;
350         }
351         else
352         {
353             p_dec->fmt_out.i_codec = VLC_CODEC_S24B;
354             p_dec->fmt_out.audio.i_bitspersample = 24;
355         }
356
357         /* */
358         aout_buffer_t *p_aout_buffer;
359         p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_frame_length );
360         if( !p_aout_buffer )
361             return NULL;
362
363         p_aout_buffer->start_date = date_Get( &p_sys->end_date );
364         p_aout_buffer->end_date =
365             date_Increment( &p_sys->end_date, i_frame_length );
366
367         p_block->p_buffer += p_sys->i_header_size + i_padding;
368         p_block->i_buffer -= p_sys->i_header_size + i_padding;
369
370         switch( p_sys->i_type )
371         {
372         case LPCM_VOB:
373             VobExtract( p_aout_buffer, p_block, i_bits );
374             break;
375         case LPCM_AOB:
376             AobExtract( p_aout_buffer, p_block, i_bits, p_aob_group );
377             break;
378         default:
379             assert(0);
380         case LPCM_BD:
381             BdExtract( p_aout_buffer, p_block );
382             break;
383         }
384
385         block_Release( p_block );
386         return p_aout_buffer;
387     }
388 }
389
390 /*****************************************************************************
391  * CloseCommon : lpcm decoder destruction
392  *****************************************************************************/
393 static void CloseCommon( vlc_object_t *p_this )
394 {
395     decoder_t *p_dec = (decoder_t*)p_this;
396     free( p_dec->p_sys );
397 }
398
399 /*****************************************************************************
400  *
401  *****************************************************************************/
402 static int VobHeader( unsigned *pi_rate,
403                       unsigned *pi_channels, unsigned *pi_original_channels,
404                       unsigned *pi_bits,
405                       const uint8_t *p_header )
406 {
407     const uint8_t i_header = p_header[4];
408
409     switch( (i_header >> 4) & 0x3 )
410     {
411     case 0:
412         *pi_rate = 48000;
413         break;
414     case 1:
415         *pi_rate = 96000;
416         break;
417     case 2:
418         *pi_rate = 44100;
419         break;
420     case 3:
421         *pi_rate = 32000;
422         break;
423     }
424
425     *pi_channels = (i_header & 0x7) + 1;
426     switch( *pi_channels - 1 )
427     {
428     case 0:
429         *pi_original_channels = AOUT_CHAN_CENTER;
430         break;
431     case 1:
432         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
433         break;
434     case 2:
435         /* This is unsure. */
436         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
437         break;
438     case 3:
439         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
440                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
441         break;
442     case 4:
443         /* This is unsure. */
444         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
445                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
446                                | AOUT_CHAN_LFE;
447         break;
448     case 5:
449         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
450                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
451                                | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
452         break;
453     case 6:
454         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
455                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
456                                | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
457                                | AOUT_CHAN_MIDDLERIGHT;
458         break;
459     case 7:
460         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
461                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
462                                | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
463                                | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
464         break;
465     }
466
467     switch( (i_header >> 6) & 0x3 )
468     {
469     case 2:
470         *pi_bits = 24;
471         break;
472     case 1:
473         *pi_bits = 20;
474         break;
475     case 0:
476     default:
477         *pi_bits = 16;
478         break;
479     }
480
481     /* Check frame sync and drop it. */
482     if( p_header[5] != 0x80 )
483         return -1;
484     return 0;
485 }
486
487 static const unsigned p_aob_group1[21][6] = {
488     { AOUT_CHAN_CENTER, 0 },
489     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
490     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
491     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
492     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
493     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
494     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
495     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
496     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
497     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
498     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
499     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
500     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
501     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,   0 },
502     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,   0 },
503     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,   0 },
504     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,   0 },
505     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,   0 },
506     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0  },
507     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0  },
508     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0  },
509 };
510 static const unsigned p_aob_group2[21][6] = {
511     { 0 },
512     { 0 },
513     { AOUT_CHAN_REARCENTER, 0 },
514     { AOUT_CHAN_REARLEFT,   AOUT_CHAN_REARRIGHT,    0 },
515     { AOUT_CHAN_LFE,        0 },
516     { AOUT_CHAN_LFE,        AOUT_CHAN_REARCENTER,   0 },
517     { AOUT_CHAN_LFE,        AOUT_CHAN_REARLEFT,     AOUT_CHAN_REARRIGHT,    0 },
518     { AOUT_CHAN_CENTER,     0 },
519     { AOUT_CHAN_CENTER,     AOUT_CHAN_REARCENTER, 0 },
520     { AOUT_CHAN_CENTER,     AOUT_CHAN_REARLEFT,   AOUT_CHAN_REARRIGHT,    0 },
521     { AOUT_CHAN_CENTER,     AOUT_CHAN_LFE,        0 },
522     { AOUT_CHAN_CENTER,     AOUT_CHAN_LFE,        AOUT_CHAN_REARCENTER,   0 },
523     { AOUT_CHAN_CENTER,     AOUT_CHAN_LFE,        AOUT_CHAN_REARLEFT,     AOUT_CHAN_REARRIGHT,    0 },
524     { AOUT_CHAN_REARCENTER, 0 },
525     { AOUT_CHAN_REARLEFT,   AOUT_CHAN_REARRIGHT,    0 },
526     { AOUT_CHAN_LFE,        0 },
527     { AOUT_CHAN_LFE,        AOUT_CHAN_REARCENTER,   0 },
528     { AOUT_CHAN_LFE,        AOUT_CHAN_REARLEFT,     AOUT_CHAN_REARRIGHT,    0 },
529     { AOUT_CHAN_LFE,        0 },
530     { AOUT_CHAN_CENTER,     0 },
531     { AOUT_CHAN_CENTER,     AOUT_CHAN_LFE,          0 },
532 };
533
534 static int AobHeader( unsigned *pi_rate,
535                       unsigned *pi_channels, unsigned *pi_layout,
536                       unsigned *pi_bits,
537                       unsigned *pi_padding,
538                       aob_group_t g[2],
539                       const uint8_t *p_header )
540 {
541     const unsigned i_header_size = GetWBE( &p_header[1] );
542     if( i_header_size + 3 < LPCM_AOB_HEADER_LEN )
543         return VLC_EGENERIC;
544
545     *pi_padding = 3+i_header_size - LPCM_AOB_HEADER_LEN;
546
547     const int i_index_size_g1 = (p_header[6] >> 4) & 0x0f;
548     const int i_index_size_g2 = (p_header[6]     ) & 0x0f;
549     const int i_index_rate_g1 = (p_header[7] >> 4) & 0x0f;
550     const int i_index_rate_g2 = (p_header[7]     ) & 0x0f;
551     const int i_assignment     = p_header[9];
552
553     /* Validate */
554     if( i_index_size_g1 > 0x02 ||
555         ( i_index_size_g2 != 0x0f && i_index_size_g2 > 0x02 ) )
556         return VLC_EGENERIC;
557     if( (i_index_rate_g1 & 0x07) > 0x02 ||
558         ( i_index_rate_g2 != 0x0f && (i_index_rate_g1 & 0x07) > 0x02 ) )
559         return VLC_EGENERIC;
560     if( i_assignment > 20 )
561         return VLC_EGENERIC;
562
563     /* */
564     *pi_bits = 16 + 4 * i_index_size_g1;
565     if( i_index_rate_g1 & 0x08 )
566         *pi_rate = 44100 << (i_index_rate_g1 & 0x07);
567     else
568         *pi_rate = 48000 << (i_index_rate_g1 & 0x07);
569
570
571     /* Group1 */
572     unsigned i_channels1 = 0;
573     unsigned i_layout1 = 0;
574     for( int i = 0; p_aob_group1[i_assignment][i] != 0; i++ )
575     {
576         i_channels1++;
577         i_layout1 |= p_aob_group1[i_assignment][i];
578     }
579     /* Group2 */
580     unsigned i_channels2 = 0;
581     unsigned i_layout2 = 0;
582     if( i_index_size_g2 != 0x0f && i_index_rate_g2 != 0x0f )
583     {
584         for( int i = 0; p_aob_group2[i_assignment][i] != 0; i++ )
585         {
586             i_channels2++;
587             i_layout2 |= p_aob_group2[i_assignment][i];
588         }
589         assert( (i_layout1 & i_layout2) == 0 );
590     }
591     /* It is enabled only when presents and compatible wih group1 */
592     const bool b_group2_used = i_index_size_g1 == i_index_size_g2 &&
593                                i_index_rate_g1 == i_index_rate_g2;
594
595     /* */
596     *pi_channels = i_channels1 + ( b_group2_used ? i_channels2 : 0 );
597     *pi_layout   = i_layout1   | ( b_group2_used ? i_layout2   : 0 );
598
599     /* */
600     for( unsigned i = 0; i < 2; i++ )
601     {
602         const unsigned *p_aob = i == 0 ? p_aob_group1[i_assignment] :
603                                          p_aob_group2[i_assignment];
604         g[i].i_channels = i == 0 ? i_channels1 :
605                                    i_channels2;
606
607         g[i].b_used = i == 0 || b_group2_used;
608         if( !g[i].b_used )
609             continue;
610         for( unsigned j = 0; j < g[i].i_channels; j++ )
611         {
612             g[i].pi_position[j] = 0;
613             for( int k = 0; pi_vlc_chan_order_wg4[k] != 0; k++ )
614             {
615                 const unsigned i_channel = pi_vlc_chan_order_wg4[k];
616                 if( i_channel == p_aob[j] )
617                     break;
618                 if( (*pi_layout) & i_channel )
619                     g[i].pi_position[j]++;
620             }
621         }
622     }
623     return VLC_SUCCESS;
624 }
625
626 static int BdHeader( unsigned *pi_rate,
627                      unsigned *pi_channels, unsigned *pi_original_channels,
628                      unsigned *pi_bits,
629                      const uint8_t *p_header )
630 {
631     const uint32_t h = GetDWBE( p_header );
632     switch( ( h & 0xf000) >> 12 )
633     {
634     case 1:
635         *pi_channels = 1;
636         *pi_original_channels = AOUT_CHAN_CENTER;
637         break;
638     case 3:
639         *pi_channels = 2;
640         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
641         break;
642     case 4:
643         *pi_channels = 3;
644         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER;
645         break;
646     case 5:
647         *pi_channels = 3;
648         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER;
649         break;
650     case 6:
651         *pi_channels = 4;
652         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
653                                 AOUT_CHAN_REARCENTER;
654         break;
655     case 7:
656         *pi_channels = 4;
657         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
658                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
659         break;
660     case 8:
661         *pi_channels = 5;
662         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
663                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
664         break;
665     case 9:
666         *pi_channels = 6;
667         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
668                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
669                                 AOUT_CHAN_LFE;
670         break;
671     case 10:
672         *pi_channels = 7;
673         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
674                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
675                                 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT;
676         break;
677     case 11:
678         *pi_channels = 8;
679         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
680                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
681                                 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
682                                 AOUT_CHAN_LFE;
683         break;
684
685     default:
686         return -1;
687     }
688     switch( (h >> 6) & 0x03 )
689     {
690     case 1:
691         *pi_bits = 16;
692         break;
693     case 2: /* 20 bits but samples are stored on 24 bits */
694     case 3: /* 24 bits */
695         *pi_bits = 24;
696         break;
697     default:
698         return -1;
699     }
700     switch( (h >> 8) & 0x0f ) 
701     {
702     case 1:
703         *pi_rate = 48000;
704         break;
705     case 4:
706         *pi_rate = 96000;
707         break;
708     case 5:
709         *pi_rate = 192000;
710         break;
711     default:
712         return -1;
713     }
714     return 0;
715 }
716
717 static void VobExtract( aout_buffer_t *p_aout_buffer, block_t *p_block,
718                         unsigned i_bits )
719 {
720     uint8_t *p_out = p_aout_buffer->p_buffer;
721
722     /* 20/24 bits LPCM use special packing */
723     if( i_bits == 24 )
724     {
725         while( p_block->i_buffer / 12 )
726         {
727             /* Sample 1 */
728             p_out[0] = p_block->p_buffer[0];
729             p_out[1] = p_block->p_buffer[1];
730             p_out[2] = p_block->p_buffer[8];
731             /* Sample 2 */
732             p_out[3] = p_block->p_buffer[2];
733             p_out[4] = p_block->p_buffer[3];
734             p_out[5] = p_block->p_buffer[9];
735             /* Sample 3 */
736             p_out[6] = p_block->p_buffer[4];
737             p_out[7] = p_block->p_buffer[5];
738             p_out[8] = p_block->p_buffer[10];
739             /* Sample 4 */
740             p_out[9] = p_block->p_buffer[6];
741             p_out[10] = p_block->p_buffer[7];
742             p_out[11] = p_block->p_buffer[11];
743
744             p_block->i_buffer -= 12;
745             p_block->p_buffer += 12;
746             p_out += 12;
747         }
748     }
749     else if( i_bits == 20 )
750     {
751         while( p_block->i_buffer / 10 )
752         {
753             /* Sample 1 */
754             p_out[0] = p_block->p_buffer[0];
755             p_out[1] = p_block->p_buffer[1];
756             p_out[2] = p_block->p_buffer[8] & 0xF0;
757             /* Sample 2 */
758             p_out[3] = p_block->p_buffer[2];
759             p_out[4] = p_block->p_buffer[3];
760             p_out[5] = p_block->p_buffer[8] << 4;
761             /* Sample 3 */
762             p_out[6] = p_block->p_buffer[4];
763             p_out[7] = p_block->p_buffer[5];
764             p_out[8] = p_block->p_buffer[9] & 0xF0;
765             /* Sample 4 */
766             p_out[9] = p_block->p_buffer[6];
767             p_out[10] = p_block->p_buffer[7];
768             p_out[11] = p_block->p_buffer[9] << 4;
769
770             p_block->i_buffer -= 10;
771             p_block->p_buffer += 10;
772             p_out += 12;
773         }
774     }
775     else
776     {
777         assert( i_bits == 16 );
778         memcpy( p_out, p_block->p_buffer, p_block->i_buffer );
779     }
780 }
781 static void AobExtract( aout_buffer_t *p_aout_buffer,
782                         block_t *p_block, unsigned i_bits, aob_group_t p_group[2] )
783 {
784     const unsigned i_channels = p_group[0].i_channels +
785                                 ( p_group[1].b_used ? p_group[1].i_channels : 0 );
786     uint8_t *p_out = p_aout_buffer->p_buffer;
787
788     while( p_block->i_buffer > 0 )
789     {
790         for( int i = 0; i < 2; i++ )
791         {
792             const aob_group_t *g = &p_group[1-i];
793             const unsigned int i_group_size = 2 * g->i_channels * i_bits / 8;
794
795             if( p_block->i_buffer < i_group_size )
796             {
797                 p_block->i_buffer = 0;
798                 break;
799             }
800             for( unsigned n = 0; n < 2; n++ )
801             {
802                 for( unsigned j = 0; j < g->i_channels && g->b_used; j++ )
803                 {
804                     const int i_src = n * g->i_channels + j;
805                     const int i_dst = n * i_channels + g->pi_position[j];
806
807                     if( i_bits == 24 )
808                     {
809                         p_out[3*i_dst+0] = p_block->p_buffer[2*i_src+0];
810                         p_out[3*i_dst+1] = p_block->p_buffer[2*i_src+1];
811                         p_out[3*i_dst+2] = p_block->p_buffer[4*g->i_channels+i_src];
812                     }
813                     else if( i_bits == 20 )
814                     {
815                         p_out[3*i_dst+0] = p_block->p_buffer[2*i_src+0];
816                         p_out[3*i_dst+1] = p_block->p_buffer[2*i_src+1];
817                         if( n == 0 )
818                             p_out[3*i_dst+2] = (p_block->p_buffer[4*g->i_channels+i_src]     ) & 0xf0;
819                         else
820                             p_out[3*i_dst+2] = (p_block->p_buffer[4*g->i_channels+i_src] << 4) & 0xf0;
821                     }
822                     else
823                     {
824                         assert( i_bits == 16 );
825                         p_out[2*i_dst+0] = p_block->p_buffer[2*i_src+0];
826                         p_out[2*i_dst+1] = p_block->p_buffer[2*i_src+1];
827                     }
828                 }
829             }
830
831             p_block->i_buffer -= i_group_size;
832             p_block->p_buffer += i_group_size;
833         }
834         /* */
835         p_out += (i_bits == 16 ? 2 : 3) * i_channels * 2;
836
837     }
838 }
839 static void BdExtract( aout_buffer_t *p_aout_buffer, block_t *p_block )
840 {
841     memcpy( p_aout_buffer->p_buffer, p_block->p_buffer, p_block->i_buffer );
842 }
843