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