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