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