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