]> git.sesse.net Git - vlc/blob - modules/codec/lpcm.c
lpcm bd: fix 24 bit stereo decoding
[vlc] / modules / codec / lpcm.c
1 /*****************************************************************************
2  * lpcm.c: lpcm decoder/packetizer module
3  *****************************************************************************
4  * Copyright (C) 1999-2008 VLC authors and VideoLAN
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  *          Steinar H. Gunderson <steinar+vlc@gunderson.no>
13  *
14  * This program is free software; you can redistribute it and/or modify it
15  * under the terms of the GNU Lesser General Public License as published by
16  * the Free Software Foundation; either version 2.1 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with this program; if not, write to the Free Software Foundation,
26  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27  *****************************************************************************/
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_codec.h>
39 #include <vlc_aout.h>
40 #include <unistd.h>
41 #include <assert.h>
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 static int  OpenDecoder   ( vlc_object_t * );
47 static int  OpenPacketizer( vlc_object_t * );
48 static void CloseCommon   ( vlc_object_t * );
49
50 #ifdef ENABLE_SOUT
51 static int  OpenEncoder   ( vlc_object_t * );
52 static void CloseEncoder  ( vlc_object_t * );
53 static block_t *EncodeFrames( encoder_t *, block_t * );
54 #endif
55
56 vlc_module_begin ()
57
58     set_category( CAT_INPUT )
59     set_subcategory( SUBCAT_INPUT_ACODEC )
60     set_description( N_("Linear PCM audio decoder") )
61     set_capability( "decoder", 100 )
62     set_callbacks( OpenDecoder, CloseCommon )
63
64     add_submodule ()
65     set_description( N_("Linear PCM audio packetizer") )
66     set_capability( "packetizer", 100 )
67     set_callbacks( OpenPacketizer, CloseCommon )
68
69 #ifdef ENABLE_SOUT
70     add_submodule ()
71     set_description( N_("Linear PCM audio encoder") )
72     set_capability( "encoder", 100 )
73     set_callbacks( OpenEncoder, CloseEncoder )
74     add_shortcut( "lpcm" )
75 #endif
76
77 vlc_module_end ()
78
79
80 /*****************************************************************************
81  * decoder_sys_t : lpcm decoder descriptor
82  *****************************************************************************/
83 struct decoder_sys_t
84 {
85     /* Module mode */
86     bool b_packetizer;
87
88     /*
89      * Output properties
90      */
91     date_t   end_date;
92
93     /* */
94     unsigned i_header_size;
95     int      i_type;
96     uint8_t  i_chans_to_reorder;
97     uint8_t  pi_chan_table[AOUT_CHAN_MAX];
98 };
99
100 #ifdef ENABLE_SOUT
101 struct encoder_sys_t
102 {
103     int     i_channels;
104     int     i_rate;
105
106     int     i_frame_samples;
107     uint8_t *p_buffer;
108     int     i_buffer_used;
109     int     i_frame_num;
110 };
111 #endif
112
113 /*
114  * LPCM DVD header :
115  * - number of frames in this packet (8 bits)
116  * - first access unit (16 bits) == 0x0003 ?
117  * - emphasis (1 bit)
118  * - mute (1 bit)
119  * - reserved (1 bit)
120  * - current frame (5 bits)
121  * - quantisation (2 bits) 0 == 16bps, 1 == 20bps, 2 == 24bps, 3 == illegal
122  * - frequency (2 bits) 0 == 48 kHz, 1 == 96 kHz, 2 == 44.1 kHz, 3 == 32 kHz
123  * - reserved (1 bit)
124  * - number of channels - 1 (3 bits) 1 == 2 channels
125  * - dynamic range (8 bits) 0x80 == neutral
126  *
127  * LPCM DVD-A header (http://dvd-audio.sourceforge.net/spec/aob.shtml)
128  * - continuity counter (8 bits, clipped to 0x00-0x1f)
129  * - header size (16 bits)
130  * - byte pointer to start of first audio frame.
131  * - unknown (8bits, 0x10 for stereo, 0x00 for surround)
132  * - sample size (4+4 bits)
133  * - samplerate (4+4 bits)
134  * - unknown (8 bits)
135  * - group assignment (8 bits)
136  * - unknown (8 bits)
137  * - padding(variable)
138  *
139  * LPCM BD header :
140  * - unknown (16 bits)
141  * - number of channels (4 bits)
142  * - frequency (4 bits)
143  * - bits per sample (2 bits)
144  * - unknown (6 bits)
145  */
146
147 #define LPCM_VOB_HEADER_LEN (6)
148 #define LPCM_AOB_HEADER_LEN (11)
149 #define LPCM_BD_HEADER_LEN (4)
150
151 enum
152 {
153     LPCM_VOB,
154     LPCM_AOB,
155     LPCM_BD,
156 };
157
158 typedef struct
159 {
160     unsigned i_channels;
161     bool     b_used;
162     unsigned pi_position[6];
163 } aob_group_t;
164
165 /*****************************************************************************
166  * Local prototypes
167  *****************************************************************************/
168 static block_t *DecodeFrame  ( decoder_t *, block_t ** );
169
170 /* */
171 static int VobHeader( unsigned *pi_rate,
172                       unsigned *pi_channels, unsigned *pi_original_channels,
173                       unsigned *pi_bits,
174                       const uint8_t *p_header );
175 static void VobExtract( block_t *, block_t *, unsigned i_bits );
176 /* */
177 static int AobHeader( unsigned *pi_rate,
178                       unsigned *pi_channels, unsigned *pi_layout,
179                       unsigned *pi_bits,
180                       unsigned *pi_padding,
181                       aob_group_t g[2],
182                       const uint8_t *p_header );
183 static void AobExtract( block_t *, block_t *, unsigned i_bits, aob_group_t p_group[2] );
184 /* */
185 static int BdHeader( decoder_sys_t *p_sys,
186                      unsigned *pi_rate,
187                      unsigned *pi_channels,
188                      unsigned *pi_channels_padding,
189                      unsigned *pi_original_channels,
190                      unsigned *pi_bits,
191                      const uint8_t *p_header );
192 static void BdExtract( block_t *, block_t *, unsigned, unsigned, unsigned, unsigned );
193
194
195 /*****************************************************************************
196  * OpenCommon:
197  *****************************************************************************/
198 static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
199 {
200     decoder_t *p_dec = (decoder_t*)p_this;
201     decoder_sys_t *p_sys;
202     int i_type;
203     int i_header_size;
204
205     switch( p_dec->fmt_in.i_codec )
206     {
207     /* DVD LPCM */
208     case VLC_CODEC_DVD_LPCM:
209         i_type = LPCM_VOB;
210         i_header_size = LPCM_VOB_HEADER_LEN;
211         break;
212     /* DVD-Audio LPCM */
213     case VLC_CODEC_DVDA_LPCM:
214         i_type = LPCM_AOB;
215         i_header_size = LPCM_AOB_HEADER_LEN;
216         break;
217     /* BD LPCM */
218     case VLC_CODEC_BD_LPCM:
219         i_type = LPCM_BD;
220         i_header_size = LPCM_BD_HEADER_LEN;
221         break;
222     default:
223         return VLC_EGENERIC;
224     }
225
226     /* Allocate the memory needed to store the decoder's structure */
227     if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
228         return VLC_ENOMEM;
229
230     /* Misc init */
231     p_sys->b_packetizer = b_packetizer;
232     date_Set( &p_sys->end_date, 0 );
233     p_sys->i_type = i_type;
234     p_sys->i_header_size = i_header_size;
235     p_sys->i_chans_to_reorder = 0;
236
237     /* Set output properties */
238     p_dec->fmt_out.i_cat = AUDIO_ES;
239
240     if( b_packetizer )
241     {
242         switch( i_type )
243         {
244         case LPCM_VOB:
245             p_dec->fmt_out.i_codec = VLC_CODEC_DVD_LPCM;
246             break;
247         case LPCM_AOB:
248             p_dec->fmt_out.i_codec = VLC_CODEC_DVDA_LPCM;
249             break;
250         default:
251             assert(0);
252         case LPCM_BD:
253             p_dec->fmt_out.i_codec = VLC_CODEC_BD_LPCM;
254             break;
255         }
256     }
257     else
258     {
259         switch( p_dec->fmt_out.audio.i_bitspersample )
260         {
261         case 24:
262         case 20:
263             p_dec->fmt_out.i_codec = VLC_CODEC_S32N;
264             p_dec->fmt_out.audio.i_bitspersample = 32;
265             break;
266         default:
267             p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
268             p_dec->fmt_out.audio.i_bitspersample = 16;
269             break;
270         }
271     }
272
273     /* Set callback */
274     p_dec->pf_decode_audio = DecodeFrame;
275     p_dec->pf_packetize    = DecodeFrame;
276
277     return VLC_SUCCESS;
278 }
279 static int OpenDecoder( vlc_object_t *p_this )
280 {
281     return OpenCommon( p_this, false );
282 }
283 static int OpenPacketizer( vlc_object_t *p_this )
284 {
285     return OpenCommon( p_this, true );
286 }
287
288 /*****************************************************************************
289  * DecodeFrame: decodes an lpcm frame.
290  ****************************************************************************
291  * Beware, this function must be fed with complete frames (PES packet).
292  *****************************************************************************/
293 static block_t *DecodeFrame( decoder_t *p_dec, block_t **pp_block )
294 {
295     decoder_sys_t *p_sys = p_dec->p_sys;
296     block_t       *p_block;
297     unsigned int  i_rate = 0, i_original_channels = 0, i_channels = 0, i_bits = 0;
298     int           i_frame_length;
299
300     if( !pp_block || !*pp_block ) return NULL;
301
302     p_block = *pp_block;
303     *pp_block = NULL; /* So the packet doesn't get re-sent */
304
305     /* Date management */
306     if( p_block->i_pts > VLC_TS_INVALID &&
307         p_block->i_pts != date_Get( &p_sys->end_date ) )
308     {
309         date_Set( &p_sys->end_date, p_block->i_pts );
310     }
311
312     if( !date_Get( &p_sys->end_date ) )
313     {
314         /* We've just started the stream, wait for the first PTS. */
315         block_Release( p_block );
316         return NULL;
317     }
318
319     if( p_block->i_buffer <= p_sys->i_header_size )
320     {
321         msg_Err(p_dec, "frame is too short");
322         block_Release( p_block );
323         return NULL;
324     }
325
326     int i_ret;
327     unsigned i_channels_padding = 0;
328     unsigned i_padding = 0;
329     aob_group_t p_aob_group[2];
330     switch( p_sys->i_type )
331     {
332     case LPCM_VOB:
333         i_ret = VobHeader( &i_rate, &i_channels, &i_original_channels, &i_bits,
334                            p_block->p_buffer );
335         break;
336     case LPCM_AOB:
337         i_ret = AobHeader( &i_rate, &i_channels, &i_original_channels, &i_bits, &i_padding,
338                            p_aob_group,
339                            p_block->p_buffer );
340         break;
341     case LPCM_BD:
342         i_ret = BdHeader( p_sys, &i_rate, &i_channels, &i_channels_padding, &i_original_channels, &i_bits,
343                           p_block->p_buffer );
344         break;
345     default:
346         abort();
347     }
348
349     if( i_ret || p_block->i_buffer <= p_sys->i_header_size + i_padding )
350     {
351         msg_Warn( p_dec, "no frame sync or too small frame" );
352         block_Release( p_block );
353         return NULL;
354     }
355
356     /* Set output properties */
357     if( p_dec->fmt_out.audio.i_rate != i_rate )
358     {
359         date_Init( &p_sys->end_date, i_rate, 1 );
360         date_Set( &p_sys->end_date, p_block->i_pts );
361     }
362     p_dec->fmt_out.audio.i_rate = i_rate;
363     p_dec->fmt_out.audio.i_channels = i_channels;
364     p_dec->fmt_out.audio.i_original_channels = i_original_channels;
365     p_dec->fmt_out.audio.i_physical_channels = i_original_channels;
366
367     i_frame_length = (p_block->i_buffer - p_sys->i_header_size - i_padding) /
368                      (i_channels + i_channels_padding) * 8 / i_bits;
369
370     if( p_sys->b_packetizer )
371     {
372         p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
373         p_block->i_length =
374             date_Increment( &p_sys->end_date, i_frame_length ) -
375             p_block->i_pts;
376
377         /* Just pass on the incoming frame */
378         return p_block;
379     }
380     else
381     {
382         /* */
383         if( i_bits == 16 )
384         {
385             p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
386             p_dec->fmt_out.audio.i_bitspersample = 16;
387         }
388         else
389         {
390             p_dec->fmt_out.i_codec = VLC_CODEC_S32N;
391             p_dec->fmt_out.audio.i_bitspersample = 32;
392         }
393
394         /* */
395         block_t *p_aout_buffer;
396         p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_frame_length );
397         if( !p_aout_buffer )
398             return NULL;
399
400         p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
401         p_aout_buffer->i_length =
402             date_Increment( &p_sys->end_date, i_frame_length )
403             - p_aout_buffer->i_pts;
404
405         p_block->p_buffer += p_sys->i_header_size + i_padding;
406         p_block->i_buffer -= p_sys->i_header_size + i_padding;
407
408         if( p_sys->i_chans_to_reorder )
409         {
410             aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer,
411                                  p_sys->i_chans_to_reorder, p_sys->pi_chan_table,
412                                  p_dec->fmt_out.i_codec );
413         }
414
415         switch( p_sys->i_type )
416         {
417         case LPCM_VOB:
418             VobExtract( p_aout_buffer, p_block, i_bits );
419             break;
420         case LPCM_AOB:
421             AobExtract( p_aout_buffer, p_block, i_bits, p_aob_group );
422             break;
423         default:
424             assert(0);
425         case LPCM_BD:
426             BdExtract( p_aout_buffer, p_block, i_frame_length, i_channels, i_channels_padding, i_bits );
427             break;
428         }
429
430         block_Release( p_block );
431         return p_aout_buffer;
432     }
433 }
434
435 /*****************************************************************************
436  * CloseCommon : lpcm decoder destruction
437  *****************************************************************************/
438 static void CloseCommon( vlc_object_t *p_this )
439 {
440     decoder_t *p_dec = (decoder_t*)p_this;
441     free( p_dec->p_sys );
442 }
443
444 #ifdef ENABLE_SOUT
445 /*****************************************************************************
446  * OpenEncoder: lpcm encoder construction
447  *****************************************************************************/
448 static int OpenEncoder( vlc_object_t *p_this )
449 {
450     encoder_t *p_enc = (encoder_t *)p_this;
451     encoder_sys_t *p_sys;
452
453     /* We only support DVD LPCM yet. */
454     if( p_enc->fmt_out.i_codec != VLC_CODEC_DVD_LPCM )
455         return VLC_EGENERIC;
456
457     if( p_enc->fmt_in.audio.i_rate != 48000 &&
458         p_enc->fmt_in.audio.i_rate != 96000 &&
459         p_enc->fmt_in.audio.i_rate != 44100 &&
460         p_enc->fmt_in.audio.i_rate != 32000 )
461     {
462         msg_Err( p_enc, "DVD LPCM supports only sample rates of 48, 96, 44.1 or 32 kHz" );
463         return VLC_EGENERIC;
464     }
465
466     if( p_enc->fmt_in.audio.i_channels > 8 )
467     {
468         msg_Err( p_enc, "DVD LPCM supports a maximum of eight channels" );
469         return VLC_EGENERIC;
470     }
471
472     /* Allocate the memory needed to store the encoder's structure */
473     if( ( p_enc->p_sys = p_sys =
474           (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
475         return VLC_ENOMEM;
476
477     /* In DVD LCPM, a frame is always 150 PTS ticks. */
478     p_sys->i_frame_samples = p_enc->fmt_in.audio.i_rate * 150 / 90000;
479     p_sys->p_buffer = xmalloc(p_sys->i_frame_samples
480                             * p_enc->fmt_in.audio.i_channels * 16);
481     p_sys->i_buffer_used = 0;
482     p_sys->i_frame_num = 0;
483
484     p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
485     p_sys->i_rate = p_enc->fmt_in.audio.i_rate;
486
487     p_enc->pf_encode_audio = EncodeFrames;
488     p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
489
490     p_enc->fmt_in.audio.i_bitspersample = 16;
491     p_enc->fmt_in.i_codec = VLC_CODEC_S16N;
492
493     p_enc->fmt_out.i_bitrate =
494         p_enc->fmt_in.audio.i_channels *
495         p_enc->fmt_in.audio.i_rate *
496         p_enc->fmt_in.audio.i_bitspersample *
497         (p_sys->i_frame_samples + LPCM_VOB_HEADER_LEN) /
498         p_sys->i_frame_samples;
499
500     return VLC_SUCCESS;
501 }
502
503 /*****************************************************************************
504  * CloseEncoder: lpcm encoder destruction
505  *****************************************************************************/
506 static void CloseEncoder ( vlc_object_t *p_this )
507 {
508     encoder_t     *p_enc = (encoder_t *)p_this;
509     encoder_sys_t *p_sys = p_enc->p_sys;
510
511     free( p_sys->p_buffer );
512     free( p_sys );
513 }
514
515 /*****************************************************************************
516  * EncodeFrames: encode zero or more LCPM audio packets
517  *****************************************************************************/
518 static block_t *EncodeFrames( encoder_t *p_enc, block_t *p_aout_buf )
519 {
520     encoder_sys_t *p_sys = p_enc->p_sys;
521     block_t *p_first_block = NULL, *p_last_block = NULL;
522
523     if( !p_aout_buf || !p_aout_buf->i_buffer ) return NULL;
524
525     const int i_num_frames = ( p_sys->i_buffer_used + p_aout_buf->i_nb_samples ) /
526         p_sys->i_frame_samples;
527     const int i_leftover_samples = ( p_sys->i_buffer_used + p_aout_buf->i_nb_samples ) %
528         p_sys->i_frame_samples;
529     const int i_frame_size = p_sys->i_frame_samples * p_sys->i_channels * 2 + LPCM_VOB_HEADER_LEN;
530     const int i_start_offset = -p_sys->i_buffer_used;
531
532     uint8_t i_freq_code = 0;
533
534     switch( p_sys->i_rate ) {
535     case 48000:
536         i_freq_code = 0;
537         break;
538     case 96000:
539         i_freq_code = 1;
540         break;
541     case 44100:
542         i_freq_code = 2;
543         break;
544     case 32000:
545         i_freq_code = 3;
546         break;
547     default:
548         assert(0);
549     }
550
551     int i_bytes_consumed = 0;
552
553     for ( int i = 0; i < i_num_frames; ++i )
554     {
555         block_t *p_block = block_Alloc( i_frame_size );
556         if( !p_block )
557             return NULL;
558
559         uint8_t *frame = (uint8_t *)p_block->p_buffer;
560         frame[0] = 1;  /* one frame in packet */
561         frame[1] = 0;
562         frame[2] = 0;  /* no first access unit */
563         frame[3] = (p_sys->i_frame_num + i) & 0x1f;  /* no emphasis, no mute */
564         frame[4] = (i_freq_code << 4) | (p_sys->i_channels - 1);
565         frame[5] = 0x80;  /* neutral dynamic range */
566
567         const int i_consume_samples = p_sys->i_frame_samples - p_sys->i_buffer_used;
568         const int i_kept_bytes = p_sys->i_buffer_used * p_sys->i_channels * 2;
569         const int i_consume_bytes = i_consume_samples * p_sys->i_channels * 2;
570
571 #ifdef WORDS_BIGENDIAN
572         memcpy( frame + 6, p_sys->p_buffer, i_kept_bytes );
573         memcpy( frame + 6 + i_kept_bytes, p_aout_buf->p_buffer + i_bytes_consumed,
574                 i_consume_bytes );
575 #else
576         swab( p_sys->p_buffer, frame + 6, i_kept_bytes );
577         swab( p_aout_buf->p_buffer + i_bytes_consumed, frame + 6 + i_kept_bytes,
578               i_consume_bytes );
579 #endif
580
581         p_sys->i_frame_num++;
582         p_sys->i_buffer_used = 0;
583         i_bytes_consumed += i_consume_bytes;
584
585         /* We need to find i_length by means of next_pts due to possible roundoff errors. */
586         mtime_t this_pts = p_aout_buf->i_pts +
587             (i * p_sys->i_frame_samples + i_start_offset) * CLOCK_FREQ / p_sys->i_rate;
588         mtime_t next_pts = p_aout_buf->i_pts +
589             ((i + 1) * p_sys->i_frame_samples + i_start_offset) * CLOCK_FREQ / p_sys->i_rate;
590
591         p_block->i_pts = p_block->i_dts = this_pts;
592         p_block->i_length = next_pts - this_pts;
593
594         if( !p_first_block )
595             p_first_block = p_last_block = p_block;
596         else
597             p_last_block = p_last_block->p_next = p_block;
598     }
599
600     memcpy( p_sys->p_buffer,
601             p_aout_buf->p_buffer + i_bytes_consumed,
602             i_leftover_samples * p_sys->i_channels * 2 );
603     p_sys->i_buffer_used = i_leftover_samples;
604
605     return p_first_block;
606 }
607 #endif
608
609 /*****************************************************************************
610  *
611  *****************************************************************************/
612 static int VobHeader( unsigned *pi_rate,
613                       unsigned *pi_channels, unsigned *pi_original_channels,
614                       unsigned *pi_bits,
615                       const uint8_t *p_header )
616 {
617     const uint8_t i_header = p_header[4];
618
619     switch( (i_header >> 4) & 0x3 )
620     {
621     case 0:
622         *pi_rate = 48000;
623         break;
624     case 1:
625         *pi_rate = 96000;
626         break;
627     case 2:
628         *pi_rate = 44100;
629         break;
630     case 3:
631         *pi_rate = 32000;
632         break;
633     }
634
635     *pi_channels = (i_header & 0x7) + 1;
636     switch( *pi_channels - 1 )
637     {
638     case 0:
639         *pi_original_channels = AOUT_CHAN_CENTER;
640         break;
641     case 1:
642         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
643         break;
644     case 2:
645         /* This is unsure. */
646         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
647         break;
648     case 3:
649         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
650                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
651         break;
652     case 4:
653         /* This is unsure. */
654         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
655                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
656                                | AOUT_CHAN_LFE;
657         break;
658     case 5:
659         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
660                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
661                                | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
662         break;
663     case 6:
664         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
665                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
666                                | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
667                                | AOUT_CHAN_MIDDLERIGHT;
668         break;
669     case 7:
670         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
671                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
672                                | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
673                                | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
674         break;
675     }
676
677     switch( (i_header >> 6) & 0x3 )
678     {
679     case 2:
680         *pi_bits = 24;
681         break;
682     case 1:
683         *pi_bits = 20;
684         break;
685     case 0:
686     default:
687         *pi_bits = 16;
688         break;
689     }
690
691     /* Check frame sync and drop it. */
692     if( p_header[5] != 0x80 )
693         return -1;
694     return 0;
695 }
696
697 static const unsigned p_aob_group1[21][6] = {
698     { AOUT_CHAN_CENTER, 0 },
699     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
700     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
701     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
702     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
703     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
704     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
705     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
706     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
707     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
708     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
709     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
710     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, 0 },
711     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,   0 },
712     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,   0 },
713     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,   0 },
714     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,   0 },
715     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,   0 },
716     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0  },
717     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0  },
718     { AOUT_CHAN_LEFT,   AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0  },
719 };
720 static const unsigned p_aob_group2[21][6] = {
721     { 0 },
722     { 0 },
723     { AOUT_CHAN_REARCENTER, 0 },
724     { AOUT_CHAN_REARLEFT,   AOUT_CHAN_REARRIGHT,    0 },
725     { AOUT_CHAN_LFE,        0 },
726     { AOUT_CHAN_LFE,        AOUT_CHAN_REARCENTER,   0 },
727     { AOUT_CHAN_LFE,        AOUT_CHAN_REARLEFT,     AOUT_CHAN_REARRIGHT,    0 },
728     { AOUT_CHAN_CENTER,     0 },
729     { AOUT_CHAN_CENTER,     AOUT_CHAN_REARCENTER, 0 },
730     { AOUT_CHAN_CENTER,     AOUT_CHAN_REARLEFT,   AOUT_CHAN_REARRIGHT,    0 },
731     { AOUT_CHAN_CENTER,     AOUT_CHAN_LFE,        0 },
732     { AOUT_CHAN_CENTER,     AOUT_CHAN_LFE,        AOUT_CHAN_REARCENTER,   0 },
733     { AOUT_CHAN_CENTER,     AOUT_CHAN_LFE,        AOUT_CHAN_REARLEFT,     AOUT_CHAN_REARRIGHT,    0 },
734     { AOUT_CHAN_REARCENTER, 0 },
735     { AOUT_CHAN_REARLEFT,   AOUT_CHAN_REARRIGHT,    0 },
736     { AOUT_CHAN_LFE,        0 },
737     { AOUT_CHAN_LFE,        AOUT_CHAN_REARCENTER,   0 },
738     { AOUT_CHAN_LFE,        AOUT_CHAN_REARLEFT,     AOUT_CHAN_REARRIGHT,    0 },
739     { AOUT_CHAN_LFE,        0 },
740     { AOUT_CHAN_CENTER,     0 },
741     { AOUT_CHAN_CENTER,     AOUT_CHAN_LFE,          0 },
742 };
743
744 static int AobHeader( unsigned *pi_rate,
745                       unsigned *pi_channels, unsigned *pi_layout,
746                       unsigned *pi_bits,
747                       unsigned *pi_padding,
748                       aob_group_t g[2],
749                       const uint8_t *p_header )
750 {
751     const unsigned i_header_size = GetWBE( &p_header[1] );
752     if( i_header_size + 3 < LPCM_AOB_HEADER_LEN )
753         return VLC_EGENERIC;
754
755     *pi_padding = 3+i_header_size - LPCM_AOB_HEADER_LEN;
756
757     const int i_index_size_g1 = (p_header[6] >> 4) & 0x0f;
758     const int i_index_size_g2 = (p_header[6]     ) & 0x0f;
759     const int i_index_rate_g1 = (p_header[7] >> 4) & 0x0f;
760     const int i_index_rate_g2 = (p_header[7]     ) & 0x0f;
761     const int i_assignment     = p_header[9];
762
763     /* Validate */
764     if( i_index_size_g1 > 0x02 ||
765         ( i_index_size_g2 != 0x0f && i_index_size_g2 > 0x02 ) )
766         return VLC_EGENERIC;
767     if( (i_index_rate_g1 & 0x07) > 0x02 ||
768         ( i_index_rate_g2 != 0x0f && (i_index_rate_g1 & 0x07) > 0x02 ) )
769         return VLC_EGENERIC;
770     if( i_assignment > 20 )
771         return VLC_EGENERIC;
772
773     /* */
774     *pi_bits = 16 + 4 * i_index_size_g1;
775     if( i_index_rate_g1 & 0x08 )
776         *pi_rate = 44100 << (i_index_rate_g1 & 0x07);
777     else
778         *pi_rate = 48000 << (i_index_rate_g1 & 0x07);
779
780
781     /* Group1 */
782     unsigned i_channels1 = 0;
783     unsigned i_layout1 = 0;
784     for( int i = 0; p_aob_group1[i_assignment][i] != 0; i++ )
785     {
786         i_channels1++;
787         i_layout1 |= p_aob_group1[i_assignment][i];
788     }
789     /* Group2 */
790     unsigned i_channels2 = 0;
791     unsigned i_layout2 = 0;
792     if( i_index_size_g2 != 0x0f && i_index_rate_g2 != 0x0f )
793     {
794         for( int i = 0; p_aob_group2[i_assignment][i] != 0; i++ )
795         {
796             i_channels2++;
797             i_layout2 |= p_aob_group2[i_assignment][i];
798         }
799         assert( (i_layout1 & i_layout2) == 0 );
800     }
801     /* It is enabled only when presents and compatible wih group1 */
802     const bool b_group2_used = i_index_size_g1 == i_index_size_g2 &&
803                                i_index_rate_g1 == i_index_rate_g2;
804
805     /* */
806     *pi_channels = i_channels1 + ( b_group2_used ? i_channels2 : 0 );
807     *pi_layout   = i_layout1   | ( b_group2_used ? i_layout2   : 0 );
808
809     /* */
810     for( unsigned i = 0; i < 2; i++ )
811     {
812         const unsigned *p_aob = i == 0 ? p_aob_group1[i_assignment] :
813                                          p_aob_group2[i_assignment];
814         g[i].i_channels = i == 0 ? i_channels1 :
815                                    i_channels2;
816
817         g[i].b_used = i == 0 || b_group2_used;
818         if( !g[i].b_used )
819             continue;
820         for( unsigned j = 0; j < g[i].i_channels; j++ )
821         {
822             g[i].pi_position[j] = 0;
823             for( int k = 0; pi_vlc_chan_order_wg4[k] != 0; k++ )
824             {
825                 const unsigned i_channel = pi_vlc_chan_order_wg4[k];
826                 if( i_channel == p_aob[j] )
827                     break;
828                 if( (*pi_layout) & i_channel )
829                     g[i].pi_position[j]++;
830             }
831         }
832     }
833     return VLC_SUCCESS;
834 }
835
836 static const uint32_t pi_8channels_in[] =
837 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,
838   AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
839   AOUT_CHAN_MIDDLERIGHT, AOUT_CHAN_LFE, 0 };
840
841 static const uint32_t pi_7channels_in[] =
842 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,
843   AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
844   AOUT_CHAN_MIDDLERIGHT, 0 };
845
846 static const uint32_t pi_6channels_in[] =
847 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,
848   AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, AOUT_CHAN_LFE, 0 };
849
850 static const uint32_t pi_5channels_in[] =
851 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,
852   AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, 0 };
853
854 static const uint32_t pi_4channels_in[] =
855 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
856   AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 };
857
858 static const uint32_t pi_3channels_in[] =
859 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
860   AOUT_CHAN_CENTER, 0 };
861
862
863 static int BdHeader( decoder_sys_t *p_sys,
864                      unsigned *pi_rate,
865                      unsigned *pi_channels,
866                      unsigned *pi_channels_padding,
867                      unsigned *pi_original_channels,
868                      unsigned *pi_bits,
869                      const uint8_t *p_header )
870 {
871     const uint32_t h = GetDWBE( p_header );
872     const uint32_t *pi_channels_in = NULL;
873     switch( ( h & 0xf000) >> 12 )
874     {
875     case 1:
876         *pi_channels = 1;
877         *pi_original_channels = AOUT_CHAN_CENTER;
878         break;
879     case 3:
880         *pi_channels = 2;
881         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
882         break;
883     case 4:
884         *pi_channels = 3;
885         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER;
886         pi_channels_in = pi_3channels_in;
887         break;
888     case 5:
889         *pi_channels = 3;
890         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER;
891         break;
892     case 6:
893         *pi_channels = 4;
894         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
895                                 AOUT_CHAN_REARCENTER;
896         break;
897     case 7:
898         *pi_channels = 4;
899         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
900                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
901         pi_channels_in = pi_4channels_in;
902         break;
903     case 8:
904         *pi_channels = 5;
905         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
906                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
907         pi_channels_in = pi_5channels_in;
908         break;
909     case 9:
910         *pi_channels = 6;
911         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
912                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
913                                 AOUT_CHAN_LFE;
914         pi_channels_in = pi_6channels_in;
915         break;
916     case 10:
917         *pi_channels = 7;
918         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
919                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
920                                 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT;
921         pi_channels_in = pi_7channels_in;
922         break;
923     case 11:
924         *pi_channels = 8;
925         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
926                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
927                                 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
928                                 AOUT_CHAN_LFE;
929         pi_channels_in = pi_8channels_in;
930         break;
931
932     default:
933         return -1;
934     }
935     *pi_channels_padding = *pi_channels & 1;
936
937     switch( (h >> 6) & 0x03 )
938     {
939     case 1:
940         *pi_bits = 16;
941         break;
942     case 2: /* 20 bits but samples are stored on 24 bits */
943     case 3: /* 24 bits */
944         *pi_bits = 24;
945         break;
946     default:
947         return -1;
948     }
949     switch( (h >> 8) & 0x0f ) 
950     {
951     case 1:
952         *pi_rate = 48000;
953         break;
954     case 4:
955         *pi_rate = 96000;
956         break;
957     case 5:
958         *pi_rate = 192000;
959         break;
960     default:
961         return -1;
962     }
963
964     if( pi_channels_in )
965     {
966         p_sys->i_chans_to_reorder =
967             aout_CheckChannelReorder( pi_channels_in, NULL,
968                                       *pi_original_channels,
969                                       p_sys->pi_chan_table );
970     }
971
972     return 0;
973 }
974
975 static void VobExtract( block_t *p_aout_buffer, block_t *p_block,
976                         unsigned i_bits )
977 {
978     /* 20/24 bits LPCM use special packing */
979     if( i_bits == 24 )
980     {
981         uint32_t *p_out = (uint32_t *)p_aout_buffer->p_buffer;
982
983         while( p_block->i_buffer / 12 )
984         {
985             /* Sample 1 */
986             *(p_out++) = (p_block->p_buffer[ 0] << 24)
987                        | (p_block->p_buffer[ 1] << 16)
988                        | (p_block->p_buffer[ 8] <<  8);
989             /* Sample 2 */
990             *(p_out++) = (p_block->p_buffer[ 2] << 24)
991                        | (p_block->p_buffer[ 3] << 16)
992                        | (p_block->p_buffer[ 9] <<  8);
993             /* Sample 3 */
994             *(p_out++) = (p_block->p_buffer[ 4] << 24)
995                        | (p_block->p_buffer[ 5] << 16)
996                        | (p_block->p_buffer[10] <<  8);
997             /* Sample 4 */
998             *(p_out++) = (p_block->p_buffer[ 6] << 24)
999                        | (p_block->p_buffer[ 7] << 16)
1000                        | (p_block->p_buffer[11] <<  8);
1001
1002             p_block->i_buffer -= 12;
1003             p_block->p_buffer += 12;
1004         }
1005     }
1006     else if( i_bits == 20 )
1007     {
1008         uint32_t *p_out = (uint32_t *)p_aout_buffer->p_buffer;
1009
1010         while( p_block->i_buffer / 10 )
1011         {
1012             /* Sample 1 */
1013             *(p_out++) = ( p_block->p_buffer[0]         << 24)
1014                        | ( p_block->p_buffer[1]         << 16)
1015                        | ((p_block->p_buffer[8] & 0xF0) <<  8);
1016             /* Sample 2 */
1017             *(p_out++) = ( p_block->p_buffer[2]         << 24)
1018                        | ( p_block->p_buffer[3]         << 16)
1019                        | ((p_block->p_buffer[8] & 0x0F) << 12);
1020             /* Sample 3 */
1021             *(p_out++) = ( p_block->p_buffer[4]         << 24)
1022                        | ( p_block->p_buffer[5]         << 16)
1023                        | ((p_block->p_buffer[9] & 0xF0) <<  8);
1024             /* Sample 4 */
1025             *(p_out++) = ( p_block->p_buffer[6]         << 24)
1026                        | ( p_block->p_buffer[7]         << 16)
1027                        | ((p_block->p_buffer[9] & 0x0F) << 12);
1028
1029             p_block->i_buffer -= 10;
1030             p_block->p_buffer += 10;
1031         }
1032     }
1033     else
1034     {
1035         assert( i_bits == 16 );
1036 #ifdef WORDS_BIGENDIAN
1037         memcpy( p_aout_buffer->p_buffer, p_block->p_buffer, p_block->i_buffer );
1038 #else
1039         swab( p_block->p_buffer, p_aout_buffer->p_buffer, p_block->i_buffer );
1040 #endif
1041     }
1042 }
1043 static void AobExtract( block_t *p_aout_buffer,
1044                         block_t *p_block, unsigned i_bits, aob_group_t p_group[2] )
1045 {
1046     const unsigned i_channels = p_group[0].i_channels +
1047                                 ( p_group[1].b_used ? p_group[1].i_channels : 0 );
1048     uint8_t *p_out = p_aout_buffer->p_buffer;
1049
1050     while( p_block->i_buffer > 0 )
1051     {
1052         for( int i = 0; i < 2; i++ )
1053         {
1054             const aob_group_t *g = &p_group[1-i];
1055             const unsigned int i_group_size = 2 * g->i_channels * i_bits / 8;
1056
1057             if( p_block->i_buffer < i_group_size )
1058             {
1059                 p_block->i_buffer = 0;
1060                 break;
1061             }
1062             for( unsigned n = 0; n < 2; n++ )
1063             {
1064                 for( unsigned j = 0; j < g->i_channels && g->b_used; j++ )
1065                 {
1066                     const int i_src = n * g->i_channels + j;
1067                     const int i_dst = n * i_channels + g->pi_position[j];
1068
1069                     if( i_bits == 24 )
1070                     {
1071                         p_out[3*i_dst+0] = p_block->p_buffer[2*i_src+0];
1072                         p_out[3*i_dst+1] = p_block->p_buffer[2*i_src+1];
1073                         p_out[3*i_dst+2] = p_block->p_buffer[4*g->i_channels+i_src];
1074                     }
1075                     else if( i_bits == 20 )
1076                     {
1077                         p_out[3*i_dst+0] = p_block->p_buffer[2*i_src+0];
1078                         p_out[3*i_dst+1] = p_block->p_buffer[2*i_src+1];
1079                         if( n == 0 )
1080                             p_out[3*i_dst+2] = (p_block->p_buffer[4*g->i_channels+i_src]     ) & 0xf0;
1081                         else
1082                             p_out[3*i_dst+2] = (p_block->p_buffer[4*g->i_channels+i_src] << 4) & 0xf0;
1083                     }
1084                     else
1085                     {
1086                         assert( i_bits == 16 );
1087                         p_out[2*i_dst+0] = p_block->p_buffer[2*i_src+0];
1088                         p_out[2*i_dst+1] = p_block->p_buffer[2*i_src+1];
1089                     }
1090                 }
1091             }
1092
1093             p_block->i_buffer -= i_group_size;
1094             p_block->p_buffer += i_group_size;
1095         }
1096         /* */
1097         p_out += (i_bits == 16 ? 2 : 3) * i_channels * 2;
1098
1099     }
1100 }
1101 static void BdExtract( block_t *p_aout_buffer, block_t *p_block,
1102                        unsigned i_frame_length,
1103                        unsigned i_channels, unsigned i_channels_padding,
1104                        unsigned i_bits )
1105 {
1106     if( i_bits != 16 || i_channels_padding > 0 )
1107     {
1108         uint8_t *p_src = p_block->p_buffer;
1109         uint8_t *p_dst = p_aout_buffer->p_buffer;
1110         int dst_inc = ((i_bits == 16) ? 2 : 4) * i_channels;
1111
1112         while( i_frame_length > 0 )
1113         {
1114 #ifdef WORDS_BIGENDIAN
1115             memcpy( p_dst, p_src, i_channels * i_bits / 8 );
1116 #else
1117             if (i_bits == 16) {
1118                 swab( p_src, p_dst, (i_channels + i_channels_padding) * i_bits / 8 );
1119             } else {
1120                 for (unsigned i = 0; i < i_channels; ++i) {
1121                     p_dst[i * 4] = 0;
1122                     p_dst[1 + (i * 4)] = p_src[2 + (i * 3)];
1123                     p_dst[2 + (i * 4)] = p_src[1 + (i * 3)];
1124                     p_dst[3 + (i * 4)] = p_src[i * 3];
1125                 }
1126             }
1127 #endif
1128             p_src += (i_channels + i_channels_padding) * i_bits / 8;
1129             p_dst += dst_inc;
1130             i_frame_length--;
1131         }
1132     }
1133     else
1134     {
1135 #ifdef WORDS_BIGENDIAN
1136         memcpy( p_aout_buffer->p_buffer, p_block->p_buffer, p_block->i_buffer );
1137 #else
1138         swab( p_block->p_buffer, p_aout_buffer->p_buffer, p_block->i_buffer );
1139 #endif
1140     }
1141 }
1142