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