]> git.sesse.net Git - vlc/blob - modules/codec/lpcm.c
lpcm: kill a warning
[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 > VLC_TS_INVALID &&
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         i_ret = VLC_EGENERIC;
312     }
313
314     if( i_ret || p_block->i_buffer <= p_sys->i_header_size + i_padding )
315     {
316         msg_Warn( p_dec, "no frame sync or too small frame" );
317         block_Release( p_block );
318         return NULL;
319     }
320
321     /* Set output properties */
322     if( p_dec->fmt_out.audio.i_rate != i_rate )
323     {
324         date_Init( &p_sys->end_date, i_rate, 1 );
325         date_Set( &p_sys->end_date, p_block->i_pts );
326     }
327     p_dec->fmt_out.audio.i_rate = i_rate;
328     p_dec->fmt_out.audio.i_channels = i_channels;
329     p_dec->fmt_out.audio.i_original_channels = i_original_channels;
330     p_dec->fmt_out.audio.i_physical_channels = i_original_channels & AOUT_CHAN_PHYSMASK;
331
332     i_frame_length = (p_block->i_buffer - p_sys->i_header_size - i_padding) / i_channels * 8 / i_bits;
333
334     if( p_sys->b_packetizer )
335     {
336         p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
337         p_block->i_length =
338             date_Increment( &p_sys->end_date, i_frame_length ) -
339             p_block->i_pts;
340
341         /* Just pass on the incoming frame */
342         return p_block;
343     }
344     else
345     {
346         /* */
347         if( i_bits == 16 )
348         {
349             p_dec->fmt_out.i_codec = VLC_CODEC_S16B;
350             p_dec->fmt_out.audio.i_bitspersample = 16;
351         }
352         else
353         {
354             p_dec->fmt_out.i_codec = VLC_CODEC_S24B;
355             p_dec->fmt_out.audio.i_bitspersample = 24;
356         }
357
358         /* */
359         aout_buffer_t *p_aout_buffer;
360         p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_frame_length );
361         if( !p_aout_buffer )
362             return NULL;
363
364         p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
365         p_aout_buffer->i_length =
366             date_Increment( &p_sys->end_date, i_frame_length )
367             - p_aout_buffer->i_pts;
368
369         p_block->p_buffer += p_sys->i_header_size + i_padding;
370         p_block->i_buffer -= p_sys->i_header_size + i_padding;
371
372         switch( p_sys->i_type )
373         {
374         case LPCM_VOB:
375             VobExtract( p_aout_buffer, p_block, i_bits );
376             break;
377         case LPCM_AOB:
378             AobExtract( p_aout_buffer, p_block, i_bits, p_aob_group );
379             break;
380         default:
381             assert(0);
382         case LPCM_BD:
383             BdExtract( p_aout_buffer, p_block );
384             break;
385         }
386
387         block_Release( p_block );
388         return p_aout_buffer;
389     }
390 }
391
392 /*****************************************************************************
393  * CloseCommon : lpcm decoder destruction
394  *****************************************************************************/
395 static void CloseCommon( vlc_object_t *p_this )
396 {
397     decoder_t *p_dec = (decoder_t*)p_this;
398     free( p_dec->p_sys );
399 }
400
401 /*****************************************************************************
402  *
403  *****************************************************************************/
404 static int VobHeader( unsigned *pi_rate,
405                       unsigned *pi_channels, unsigned *pi_original_channels,
406                       unsigned *pi_bits,
407                       const uint8_t *p_header )
408 {
409     const uint8_t i_header = p_header[4];
410
411     switch( (i_header >> 4) & 0x3 )
412     {
413     case 0:
414         *pi_rate = 48000;
415         break;
416     case 1:
417         *pi_rate = 96000;
418         break;
419     case 2:
420         *pi_rate = 44100;
421         break;
422     case 3:
423         *pi_rate = 32000;
424         break;
425     }
426
427     *pi_channels = (i_header & 0x7) + 1;
428     switch( *pi_channels - 1 )
429     {
430     case 0:
431         *pi_original_channels = AOUT_CHAN_CENTER;
432         break;
433     case 1:
434         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
435         break;
436     case 2:
437         /* This is unsure. */
438         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
439         break;
440     case 3:
441         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
442                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
443         break;
444     case 4:
445         /* This is unsure. */
446         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
447                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
448                                | AOUT_CHAN_LFE;
449         break;
450     case 5:
451         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
452                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
453                                | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
454         break;
455     case 6:
456         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
457                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
458                                | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
459                                | AOUT_CHAN_MIDDLERIGHT;
460         break;
461     case 7:
462         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
463                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
464                                | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
465                                | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
466         break;
467     }
468
469     switch( (i_header >> 6) & 0x3 )
470     {
471     case 2:
472         *pi_bits = 24;
473         break;
474     case 1:
475         *pi_bits = 20;
476         break;
477     case 0:
478     default:
479         *pi_bits = 16;
480         break;
481     }
482
483     /* Check frame sync and drop it. */
484     if( p_header[5] != 0x80 )
485         return -1;
486     return 0;
487 }
488
489 static const unsigned p_aob_group1[21][6] = {
490     { AOUT_CHAN_CENTER, 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, 0 },
502     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 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_CENTER,   0 },
507     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,   0 },
508     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0  },
509     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0  },
510     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0  },
511 };
512 static const unsigned p_aob_group2[21][6] = {
513     { 0 },
514     { 0 },
515     { AOUT_CHAN_REARCENTER, 0 },
516     { AOUT_CHAN_REARLEFT,   AOUT_CHAN_REARRIGHT,    0 },
517     { AOUT_CHAN_LFE,        0 },
518     { AOUT_CHAN_LFE,        AOUT_CHAN_REARCENTER,   0 },
519     { AOUT_CHAN_LFE,        AOUT_CHAN_REARLEFT,     AOUT_CHAN_REARRIGHT,    0 },
520     { AOUT_CHAN_CENTER,     0 },
521     { AOUT_CHAN_CENTER,     AOUT_CHAN_REARCENTER, 0 },
522     { AOUT_CHAN_CENTER,     AOUT_CHAN_REARLEFT,   AOUT_CHAN_REARRIGHT,    0 },
523     { AOUT_CHAN_CENTER,     AOUT_CHAN_LFE,        0 },
524     { AOUT_CHAN_CENTER,     AOUT_CHAN_LFE,        AOUT_CHAN_REARCENTER,   0 },
525     { AOUT_CHAN_CENTER,     AOUT_CHAN_LFE,        AOUT_CHAN_REARLEFT,     AOUT_CHAN_REARRIGHT,    0 },
526     { AOUT_CHAN_REARCENTER, 0 },
527     { AOUT_CHAN_REARLEFT,   AOUT_CHAN_REARRIGHT,    0 },
528     { AOUT_CHAN_LFE,        0 },
529     { AOUT_CHAN_LFE,        AOUT_CHAN_REARCENTER,   0 },
530     { AOUT_CHAN_LFE,        AOUT_CHAN_REARLEFT,     AOUT_CHAN_REARRIGHT,    0 },
531     { AOUT_CHAN_LFE,        0 },
532     { AOUT_CHAN_CENTER,     0 },
533     { AOUT_CHAN_CENTER,     AOUT_CHAN_LFE,          0 },
534 };
535
536 static int AobHeader( unsigned *pi_rate,
537                       unsigned *pi_channels, unsigned *pi_layout,
538                       unsigned *pi_bits,
539                       unsigned *pi_padding,
540                       aob_group_t g[2],
541                       const uint8_t *p_header )
542 {
543     const unsigned i_header_size = GetWBE( &p_header[1] );
544     if( i_header_size + 3 < LPCM_AOB_HEADER_LEN )
545         return VLC_EGENERIC;
546
547     *pi_padding = 3+i_header_size - LPCM_AOB_HEADER_LEN;
548
549     const int i_index_size_g1 = (p_header[6] >> 4) & 0x0f;
550     const int i_index_size_g2 = (p_header[6]     ) & 0x0f;
551     const int i_index_rate_g1 = (p_header[7] >> 4) & 0x0f;
552     const int i_index_rate_g2 = (p_header[7]     ) & 0x0f;
553     const int i_assignment     = p_header[9];
554
555     /* Validate */
556     if( i_index_size_g1 > 0x02 ||
557         ( i_index_size_g2 != 0x0f && i_index_size_g2 > 0x02 ) )
558         return VLC_EGENERIC;
559     if( (i_index_rate_g1 & 0x07) > 0x02 ||
560         ( i_index_rate_g2 != 0x0f && (i_index_rate_g1 & 0x07) > 0x02 ) )
561         return VLC_EGENERIC;
562     if( i_assignment > 20 )
563         return VLC_EGENERIC;
564
565     /* */
566     *pi_bits = 16 + 4 * i_index_size_g1;
567     if( i_index_rate_g1 & 0x08 )
568         *pi_rate = 44100 << (i_index_rate_g1 & 0x07);
569     else
570         *pi_rate = 48000 << (i_index_rate_g1 & 0x07);
571
572
573     /* Group1 */
574     unsigned i_channels1 = 0;
575     unsigned i_layout1 = 0;
576     for( int i = 0; p_aob_group1[i_assignment][i] != 0; i++ )
577     {
578         i_channels1++;
579         i_layout1 |= p_aob_group1[i_assignment][i];
580     }
581     /* Group2 */
582     unsigned i_channels2 = 0;
583     unsigned i_layout2 = 0;
584     if( i_index_size_g2 != 0x0f && i_index_rate_g2 != 0x0f )
585     {
586         for( int i = 0; p_aob_group2[i_assignment][i] != 0; i++ )
587         {
588             i_channels2++;
589             i_layout2 |= p_aob_group2[i_assignment][i];
590         }
591         assert( (i_layout1 & i_layout2) == 0 );
592     }
593     /* It is enabled only when presents and compatible wih group1 */
594     const bool b_group2_used = i_index_size_g1 == i_index_size_g2 &&
595                                i_index_rate_g1 == i_index_rate_g2;
596
597     /* */
598     *pi_channels = i_channels1 + ( b_group2_used ? i_channels2 : 0 );
599     *pi_layout   = i_layout1   | ( b_group2_used ? i_layout2   : 0 );
600
601     /* */
602     for( unsigned i = 0; i < 2; i++ )
603     {
604         const unsigned *p_aob = i == 0 ? p_aob_group1[i_assignment] :
605                                          p_aob_group2[i_assignment];
606         g[i].i_channels = i == 0 ? i_channels1 :
607                                    i_channels2;
608
609         g[i].b_used = i == 0 || b_group2_used;
610         if( !g[i].b_used )
611             continue;
612         for( unsigned j = 0; j < g[i].i_channels; j++ )
613         {
614             g[i].pi_position[j] = 0;
615             for( int k = 0; pi_vlc_chan_order_wg4[k] != 0; k++ )
616             {
617                 const unsigned i_channel = pi_vlc_chan_order_wg4[k];
618                 if( i_channel == p_aob[j] )
619                     break;
620                 if( (*pi_layout) & i_channel )
621                     g[i].pi_position[j]++;
622             }
623         }
624     }
625     return VLC_SUCCESS;
626 }
627
628 static int BdHeader( unsigned *pi_rate,
629                      unsigned *pi_channels, unsigned *pi_original_channels,
630                      unsigned *pi_bits,
631                      const uint8_t *p_header )
632 {
633     const uint32_t h = GetDWBE( p_header );
634     switch( ( h & 0xf000) >> 12 )
635     {
636     case 1:
637         *pi_channels = 1;
638         *pi_original_channels = AOUT_CHAN_CENTER;
639         break;
640     case 3:
641         *pi_channels = 2;
642         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
643         break;
644     case 4:
645         *pi_channels = 3;
646         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER;
647         break;
648     case 5:
649         *pi_channels = 3;
650         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER;
651         break;
652     case 6:
653         *pi_channels = 4;
654         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
655                                 AOUT_CHAN_REARCENTER;
656         break;
657     case 7:
658         *pi_channels = 4;
659         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
660                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
661         break;
662     case 8:
663         *pi_channels = 5;
664         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
665                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
666         break;
667     case 9:
668         *pi_channels = 6;
669         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
670                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
671                                 AOUT_CHAN_LFE;
672         break;
673     case 10:
674         *pi_channels = 7;
675         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
676                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
677                                 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT;
678         break;
679     case 11:
680         *pi_channels = 8;
681         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
682                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
683                                 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
684                                 AOUT_CHAN_LFE;
685         break;
686
687     default:
688         return -1;
689     }
690     switch( (h >> 6) & 0x03 )
691     {
692     case 1:
693         *pi_bits = 16;
694         break;
695     case 2: /* 20 bits but samples are stored on 24 bits */
696     case 3: /* 24 bits */
697         *pi_bits = 24;
698         break;
699     default:
700         return -1;
701     }
702     switch( (h >> 8) & 0x0f ) 
703     {
704     case 1:
705         *pi_rate = 48000;
706         break;
707     case 4:
708         *pi_rate = 96000;
709         break;
710     case 5:
711         *pi_rate = 192000;
712         break;
713     default:
714         return -1;
715     }
716     return 0;
717 }
718
719 static void VobExtract( aout_buffer_t *p_aout_buffer, block_t *p_block,
720                         unsigned i_bits )
721 {
722     uint8_t *p_out = p_aout_buffer->p_buffer;
723
724     /* 20/24 bits LPCM use special packing */
725     if( i_bits == 24 )
726     {
727         while( p_block->i_buffer / 12 )
728         {
729             /* Sample 1 */
730             p_out[0] = p_block->p_buffer[0];
731             p_out[1] = p_block->p_buffer[1];
732             p_out[2] = p_block->p_buffer[8];
733             /* Sample 2 */
734             p_out[3] = p_block->p_buffer[2];
735             p_out[4] = p_block->p_buffer[3];
736             p_out[5] = p_block->p_buffer[9];
737             /* Sample 3 */
738             p_out[6] = p_block->p_buffer[4];
739             p_out[7] = p_block->p_buffer[5];
740             p_out[8] = p_block->p_buffer[10];
741             /* Sample 4 */
742             p_out[9] = p_block->p_buffer[6];
743             p_out[10] = p_block->p_buffer[7];
744             p_out[11] = p_block->p_buffer[11];
745
746             p_block->i_buffer -= 12;
747             p_block->p_buffer += 12;
748             p_out += 12;
749         }
750     }
751     else if( i_bits == 20 )
752     {
753         while( p_block->i_buffer / 10 )
754         {
755             /* Sample 1 */
756             p_out[0] = p_block->p_buffer[0];
757             p_out[1] = p_block->p_buffer[1];
758             p_out[2] = p_block->p_buffer[8] & 0xF0;
759             /* Sample 2 */
760             p_out[3] = p_block->p_buffer[2];
761             p_out[4] = p_block->p_buffer[3];
762             p_out[5] = p_block->p_buffer[8] << 4;
763             /* Sample 3 */
764             p_out[6] = p_block->p_buffer[4];
765             p_out[7] = p_block->p_buffer[5];
766             p_out[8] = p_block->p_buffer[9] & 0xF0;
767             /* Sample 4 */
768             p_out[9] = p_block->p_buffer[6];
769             p_out[10] = p_block->p_buffer[7];
770             p_out[11] = p_block->p_buffer[9] << 4;
771
772             p_block->i_buffer -= 10;
773             p_block->p_buffer += 10;
774             p_out += 12;
775         }
776     }
777     else
778     {
779         assert( i_bits == 16 );
780         memcpy( p_out, p_block->p_buffer, p_block->i_buffer );
781     }
782 }
783 static void AobExtract( aout_buffer_t *p_aout_buffer,
784                         block_t *p_block, unsigned i_bits, aob_group_t p_group[2] )
785 {
786     const unsigned i_channels = p_group[0].i_channels +
787                                 ( p_group[1].b_used ? p_group[1].i_channels : 0 );
788     uint8_t *p_out = p_aout_buffer->p_buffer;
789
790     while( p_block->i_buffer > 0 )
791     {
792         for( int i = 0; i < 2; i++ )
793         {
794             const aob_group_t *g = &p_group[1-i];
795             const unsigned int i_group_size = 2 * g->i_channels * i_bits / 8;
796
797             if( p_block->i_buffer < i_group_size )
798             {
799                 p_block->i_buffer = 0;
800                 break;
801             }
802             for( unsigned n = 0; n < 2; n++ )
803             {
804                 for( unsigned j = 0; j < g->i_channels && g->b_used; j++ )
805                 {
806                     const int i_src = n * g->i_channels + j;
807                     const int i_dst = n * i_channels + g->pi_position[j];
808
809                     if( i_bits == 24 )
810                     {
811                         p_out[3*i_dst+0] = p_block->p_buffer[2*i_src+0];
812                         p_out[3*i_dst+1] = p_block->p_buffer[2*i_src+1];
813                         p_out[3*i_dst+2] = p_block->p_buffer[4*g->i_channels+i_src];
814                     }
815                     else if( i_bits == 20 )
816                     {
817                         p_out[3*i_dst+0] = p_block->p_buffer[2*i_src+0];
818                         p_out[3*i_dst+1] = p_block->p_buffer[2*i_src+1];
819                         if( n == 0 )
820                             p_out[3*i_dst+2] = (p_block->p_buffer[4*g->i_channels+i_src]     ) & 0xf0;
821                         else
822                             p_out[3*i_dst+2] = (p_block->p_buffer[4*g->i_channels+i_src] << 4) & 0xf0;
823                     }
824                     else
825                     {
826                         assert( i_bits == 16 );
827                         p_out[2*i_dst+0] = p_block->p_buffer[2*i_src+0];
828                         p_out[2*i_dst+1] = p_block->p_buffer[2*i_src+1];
829                     }
830                 }
831             }
832
833             p_block->i_buffer -= i_group_size;
834             p_block->p_buffer += i_group_size;
835         }
836         /* */
837         p_out += (i_bits == 16 ? 2 : 3) * i_channels * 2;
838
839     }
840 }
841 static void BdExtract( aout_buffer_t *p_aout_buffer, block_t *p_block )
842 {
843     memcpy( p_aout_buffer->p_buffer, p_block->p_buffer, p_block->i_buffer );
844 }
845