1 /*****************************************************************************
2 * lpcm.c: lpcm decoder/packetizer module
3 *****************************************************************************
4 * Copyright (C) 1999-2008 VLC authors and VideoLAN
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>
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.
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.
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 *****************************************************************************/
29 /*****************************************************************************
31 *****************************************************************************/
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_codec.h>
43 /*****************************************************************************
45 *****************************************************************************/
46 static int OpenDecoder ( vlc_object_t * );
47 static int OpenPacketizer( vlc_object_t * );
48 static void CloseCommon ( vlc_object_t * );
51 static int OpenEncoder ( vlc_object_t * );
52 static void CloseEncoder ( vlc_object_t * );
53 static block_t *EncodeFrames( encoder_t *, block_t * );
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 )
65 set_description( N_("Linear PCM audio packetizer") )
66 set_capability( "packetizer", 100 )
67 set_callbacks( OpenPacketizer, CloseCommon )
71 set_description( N_("Linear PCM audio encoder") )
72 set_capability( "encoder", 100 )
73 set_callbacks( OpenEncoder, CloseEncoder )
74 add_shortcut( "lpcm" )
80 /*****************************************************************************
81 * decoder_sys_t : lpcm decoder descriptor
82 *****************************************************************************/
94 unsigned i_header_size;
96 uint8_t i_chans_to_reorder;
97 uint8_t pi_chan_table[AOUT_CHAN_MAX];
115 * - number of frames in this packet (8 bits)
116 * - first access unit (16 bits) == 0x0003 ?
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
124 * - number of channels - 1 (3 bits) 1 == 2 channels
125 * - dynamic range (8 bits) 0x80 == neutral
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)
135 * - group assignment (8 bits)
137 * - padding(variable)
140 * - unknown (16 bits)
141 * - number of channels (4 bits)
142 * - frequency (4 bits)
143 * - bits per sample (2 bits)
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)
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)
175 unsigned pi_position[6];
178 /*****************************************************************************
180 *****************************************************************************/
181 static block_t *DecodeFrame ( decoder_t *, block_t ** );
184 static int VobHeader( unsigned *pi_rate,
185 unsigned *pi_channels, unsigned *pi_original_channels,
187 const uint8_t *p_header );
188 static void VobExtract( block_t *, block_t *, unsigned i_bits );
190 static int AobHeader( unsigned *pi_rate,
191 unsigned *pi_channels, unsigned *pi_layout,
193 unsigned *pi_padding,
195 const uint8_t *p_header );
196 static void AobExtract( block_t *, block_t *, unsigned i_bits, aob_group_t p_group[2] );
198 static int BdHeader( decoder_sys_t *p_sys,
200 unsigned *pi_channels,
201 unsigned *pi_channels_padding,
202 unsigned *pi_original_channels,
204 const uint8_t *p_header );
205 static void BdExtract( block_t *, block_t *, unsigned, unsigned, unsigned, unsigned );
207 static int WidiHeader( unsigned *pi_rate,
208 unsigned *pi_channels, unsigned *pi_original_channels,
210 const uint8_t *p_header );
212 /*****************************************************************************
214 *****************************************************************************/
215 static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
217 decoder_t *p_dec = (decoder_t*)p_this;
218 decoder_sys_t *p_sys;
222 switch( p_dec->fmt_in.i_codec )
225 case VLC_CODEC_DVD_LPCM:
227 i_header_size = LPCM_VOB_HEADER_LEN;
230 case VLC_CODEC_DVDA_LPCM:
232 i_header_size = LPCM_AOB_HEADER_LEN;
235 case VLC_CODEC_BD_LPCM:
237 i_header_size = LPCM_BD_HEADER_LEN;
240 case VLC_CODEC_WIDI_LPCM:
242 i_header_size = LPCM_WIDI_HEADER_LEN;
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 )
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;
259 /* Set output properties */
260 p_dec->fmt_out.i_cat = AUDIO_ES;
267 p_dec->fmt_out.i_codec = VLC_CODEC_DVD_LPCM;
270 p_dec->fmt_out.i_codec = VLC_CODEC_DVDA_LPCM;
273 p_dec->fmt_out.i_codec = VLC_CODEC_WIDI_LPCM;
278 p_dec->fmt_out.i_codec = VLC_CODEC_BD_LPCM;
284 switch( p_dec->fmt_out.audio.i_bitspersample )
288 p_dec->fmt_out.i_codec = VLC_CODEC_S32N;
289 p_dec->fmt_out.audio.i_bitspersample = 32;
292 p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
293 p_dec->fmt_out.audio.i_bitspersample = 16;
299 p_dec->pf_decode_audio = DecodeFrame;
300 p_dec->pf_packetize = DecodeFrame;
304 static int OpenDecoder( vlc_object_t *p_this )
306 return OpenCommon( p_this, false );
308 static int OpenPacketizer( vlc_object_t *p_this )
310 return OpenCommon( p_this, true );
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 )
320 decoder_sys_t *p_sys = p_dec->p_sys;
322 unsigned int i_rate = 0, i_original_channels = 0, i_channels = 0, i_bits = 0;
325 if( !pp_block || !*pp_block ) return NULL;
328 *pp_block = NULL; /* So the packet doesn't get re-sent */
330 /* Date management */
331 if( p_block->i_pts > VLC_TS_INVALID &&
332 p_block->i_pts != date_Get( &p_sys->end_date ) )
334 date_Set( &p_sys->end_date, p_block->i_pts );
337 if( !date_Get( &p_sys->end_date ) )
339 /* We've just started the stream, wait for the first PTS. */
340 block_Release( p_block );
344 if( p_block->i_buffer <= p_sys->i_header_size )
346 msg_Err(p_dec, "frame is too short");
347 block_Release( p_block );
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 )
358 i_ret = VobHeader( &i_rate, &i_channels, &i_original_channels, &i_bits,
362 i_ret = AobHeader( &i_rate, &i_channels, &i_original_channels, &i_bits, &i_padding,
367 i_ret = BdHeader( p_sys, &i_rate, &i_channels, &i_channels_padding, &i_original_channels, &i_bits,
371 i_ret = WidiHeader( &i_rate, &i_channels, &i_original_channels, &i_bits,
378 if( i_ret || p_block->i_buffer <= p_sys->i_header_size + i_padding )
380 msg_Warn( p_dec, "no frame sync or too small frame" );
381 block_Release( p_block );
385 /* Set output properties */
386 if( p_dec->fmt_out.audio.i_rate != i_rate )
388 date_Init( &p_sys->end_date, i_rate, 1 );
389 date_Set( &p_sys->end_date, p_block->i_pts );
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;
396 i_frame_length = (p_block->i_buffer - p_sys->i_header_size - i_padding) /
397 (i_channels + i_channels_padding) * 8 / i_bits;
399 if( p_sys->b_packetizer )
401 p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
403 date_Increment( &p_sys->end_date, i_frame_length ) -
406 /* Just pass on the incoming frame */
414 p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
415 p_dec->fmt_out.audio.i_bitspersample = 16;
419 p_dec->fmt_out.i_codec = VLC_CODEC_S32N;
420 p_dec->fmt_out.audio.i_bitspersample = 32;
424 block_t *p_aout_buffer;
425 p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_frame_length );
429 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
430 p_aout_buffer->i_length =
431 date_Increment( &p_sys->end_date, i_frame_length )
432 - p_aout_buffer->i_pts;
434 p_block->p_buffer += p_sys->i_header_size + i_padding;
435 p_block->i_buffer -= p_sys->i_header_size + i_padding;
437 if( p_sys->i_chans_to_reorder )
439 aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer,
440 p_sys->i_chans_to_reorder, p_sys->pi_chan_table,
441 p_dec->fmt_out.i_codec );
444 switch( p_sys->i_type )
448 VobExtract( p_aout_buffer, p_block, i_bits );
451 AobExtract( p_aout_buffer, p_block, i_bits, p_aob_group );
456 BdExtract( p_aout_buffer, p_block, i_frame_length, i_channels, i_channels_padding, i_bits );
460 block_Release( p_block );
461 return p_aout_buffer;
465 /*****************************************************************************
466 * CloseCommon : lpcm decoder destruction
467 *****************************************************************************/
468 static void CloseCommon( vlc_object_t *p_this )
470 decoder_t *p_dec = (decoder_t*)p_this;
471 free( p_dec->p_sys );
475 /*****************************************************************************
476 * OpenEncoder: lpcm encoder construction
477 *****************************************************************************/
478 static int OpenEncoder( vlc_object_t *p_this )
480 encoder_t *p_enc = (encoder_t *)p_this;
481 encoder_sys_t *p_sys;
483 /* We only support DVD LPCM yet. */
484 if( p_enc->fmt_out.i_codec != VLC_CODEC_DVD_LPCM )
487 if( p_enc->fmt_in.audio.i_rate != 48000 &&
488 p_enc->fmt_in.audio.i_rate != 96000 &&
489 p_enc->fmt_in.audio.i_rate != 44100 &&
490 p_enc->fmt_in.audio.i_rate != 32000 )
492 msg_Err( p_enc, "DVD LPCM supports only sample rates of 48, 96, 44.1 or 32 kHz" );
496 if( p_enc->fmt_in.audio.i_channels > 8 )
498 msg_Err( p_enc, "DVD LPCM supports a maximum of eight channels" );
502 /* Allocate the memory needed to store the encoder's structure */
503 if( ( p_enc->p_sys = p_sys =
504 (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
507 /* In DVD LCPM, a frame is always 150 PTS ticks. */
508 p_sys->i_frame_samples = p_enc->fmt_in.audio.i_rate * 150 / 90000;
509 p_sys->p_buffer = xmalloc(p_sys->i_frame_samples
510 * p_enc->fmt_in.audio.i_channels * 16);
511 p_sys->i_buffer_used = 0;
512 p_sys->i_frame_num = 0;
514 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
515 p_sys->i_rate = p_enc->fmt_in.audio.i_rate;
517 p_enc->pf_encode_audio = EncodeFrames;
518 p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
520 p_enc->fmt_in.audio.i_bitspersample = 16;
521 p_enc->fmt_in.i_codec = VLC_CODEC_S16N;
523 p_enc->fmt_out.i_bitrate =
524 p_enc->fmt_in.audio.i_channels *
525 p_enc->fmt_in.audio.i_rate *
526 p_enc->fmt_in.audio.i_bitspersample *
527 (p_sys->i_frame_samples + LPCM_VOB_HEADER_LEN) /
528 p_sys->i_frame_samples;
533 /*****************************************************************************
534 * CloseEncoder: lpcm encoder destruction
535 *****************************************************************************/
536 static void CloseEncoder ( vlc_object_t *p_this )
538 encoder_t *p_enc = (encoder_t *)p_this;
539 encoder_sys_t *p_sys = p_enc->p_sys;
541 free( p_sys->p_buffer );
545 /*****************************************************************************
546 * EncodeFrames: encode zero or more LCPM audio packets
547 *****************************************************************************/
548 static block_t *EncodeFrames( encoder_t *p_enc, block_t *p_aout_buf )
550 encoder_sys_t *p_sys = p_enc->p_sys;
551 block_t *p_first_block = NULL, *p_last_block = NULL;
553 if( !p_aout_buf || !p_aout_buf->i_buffer ) return NULL;
555 const int i_num_frames = ( p_sys->i_buffer_used + p_aout_buf->i_nb_samples ) /
556 p_sys->i_frame_samples;
557 const int i_leftover_samples = ( p_sys->i_buffer_used + p_aout_buf->i_nb_samples ) %
558 p_sys->i_frame_samples;
559 const int i_frame_size = p_sys->i_frame_samples * p_sys->i_channels * 2 + LPCM_VOB_HEADER_LEN;
560 const int i_start_offset = -p_sys->i_buffer_used;
562 uint8_t i_freq_code = 0;
564 switch( p_sys->i_rate ) {
581 int i_bytes_consumed = 0;
583 for ( int i = 0; i < i_num_frames; ++i )
585 block_t *p_block = block_Alloc( i_frame_size );
589 uint8_t *frame = (uint8_t *)p_block->p_buffer;
590 frame[0] = 1; /* one frame in packet */
592 frame[2] = 0; /* no first access unit */
593 frame[3] = (p_sys->i_frame_num + i) & 0x1f; /* no emphasis, no mute */
594 frame[4] = (i_freq_code << 4) | (p_sys->i_channels - 1);
595 frame[5] = 0x80; /* neutral dynamic range */
597 const int i_consume_samples = p_sys->i_frame_samples - p_sys->i_buffer_used;
598 const int i_kept_bytes = p_sys->i_buffer_used * p_sys->i_channels * 2;
599 const int i_consume_bytes = i_consume_samples * p_sys->i_channels * 2;
601 #ifdef WORDS_BIGENDIAN
602 memcpy( frame + 6, p_sys->p_buffer, i_kept_bytes );
603 memcpy( frame + 6 + i_kept_bytes, p_aout_buf->p_buffer + i_bytes_consumed,
606 swab( p_sys->p_buffer, frame + 6, i_kept_bytes );
607 swab( p_aout_buf->p_buffer + i_bytes_consumed, frame + 6 + i_kept_bytes,
611 p_sys->i_frame_num++;
612 p_sys->i_buffer_used = 0;
613 i_bytes_consumed += i_consume_bytes;
615 /* We need to find i_length by means of next_pts due to possible roundoff errors. */
616 mtime_t this_pts = p_aout_buf->i_pts +
617 (i * p_sys->i_frame_samples + i_start_offset) * CLOCK_FREQ / p_sys->i_rate;
618 mtime_t next_pts = p_aout_buf->i_pts +
619 ((i + 1) * p_sys->i_frame_samples + i_start_offset) * CLOCK_FREQ / p_sys->i_rate;
621 p_block->i_pts = p_block->i_dts = this_pts;
622 p_block->i_length = next_pts - this_pts;
625 p_first_block = p_last_block = p_block;
627 p_last_block = p_last_block->p_next = p_block;
630 memcpy( p_sys->p_buffer,
631 p_aout_buf->p_buffer + i_bytes_consumed,
632 i_leftover_samples * p_sys->i_channels * 2 );
633 p_sys->i_buffer_used = i_leftover_samples;
635 return p_first_block;
639 /*****************************************************************************
641 *****************************************************************************/
642 static int VobHeader( unsigned *pi_rate,
643 unsigned *pi_channels, unsigned *pi_original_channels,
645 const uint8_t *p_header )
647 const uint8_t i_header = p_header[4];
649 switch( (i_header >> 4) & 0x3 )
665 *pi_channels = (i_header & 0x7) + 1;
666 switch( *pi_channels - 1 )
669 *pi_original_channels = AOUT_CHAN_CENTER;
672 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
675 /* This is unsure. */
676 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
679 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
680 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
683 /* This is unsure. */
684 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
685 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
689 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
690 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
691 | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
694 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
695 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
696 | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
697 | AOUT_CHAN_MIDDLERIGHT;
700 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
701 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
702 | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
703 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
707 switch( (i_header >> 6) & 0x3 )
721 /* Check frame sync and drop it. */
722 if( p_header[5] != 0x80 )
727 static const unsigned p_aob_group1[21][6] = {
728 { AOUT_CHAN_CENTER, 0 },
729 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
730 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
731 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
732 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
733 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
734 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
735 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
736 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
737 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
738 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
739 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
740 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
741 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, 0 },
742 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, 0 },
743 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, 0 },
744 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, 0 },
745 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, 0 },
746 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
747 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
748 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
750 static const unsigned p_aob_group2[21][6] = {
753 { AOUT_CHAN_REARCENTER, 0 },
754 { AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
755 { AOUT_CHAN_LFE, 0 },
756 { AOUT_CHAN_LFE, AOUT_CHAN_REARCENTER, 0 },
757 { AOUT_CHAN_LFE, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
758 { AOUT_CHAN_CENTER, 0 },
759 { AOUT_CHAN_CENTER, AOUT_CHAN_REARCENTER, 0 },
760 { AOUT_CHAN_CENTER, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
761 { AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 },
762 { AOUT_CHAN_CENTER, AOUT_CHAN_LFE, AOUT_CHAN_REARCENTER, 0 },
763 { AOUT_CHAN_CENTER, AOUT_CHAN_LFE, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 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_LFE, 0 },
770 { AOUT_CHAN_CENTER, 0 },
771 { AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 },
774 static int AobHeader( unsigned *pi_rate,
775 unsigned *pi_channels, unsigned *pi_layout,
777 unsigned *pi_padding,
779 const uint8_t *p_header )
781 const unsigned i_header_size = GetWBE( &p_header[1] );
782 if( i_header_size + 3 < LPCM_AOB_HEADER_LEN )
785 *pi_padding = 3+i_header_size - LPCM_AOB_HEADER_LEN;
787 const int i_index_size_g1 = (p_header[6] >> 4) & 0x0f;
788 const int i_index_size_g2 = (p_header[6] ) & 0x0f;
789 const int i_index_rate_g1 = (p_header[7] >> 4) & 0x0f;
790 const int i_index_rate_g2 = (p_header[7] ) & 0x0f;
791 const int i_assignment = p_header[9];
794 if( i_index_size_g1 > 0x02 ||
795 ( i_index_size_g2 != 0x0f && i_index_size_g2 > 0x02 ) )
797 if( (i_index_rate_g1 & 0x07) > 0x02 ||
798 ( i_index_rate_g2 != 0x0f && (i_index_rate_g1 & 0x07) > 0x02 ) )
800 if( i_assignment > 20 )
804 *pi_bits = 16 + 4 * i_index_size_g1;
805 if( i_index_rate_g1 & 0x08 )
806 *pi_rate = 44100 << (i_index_rate_g1 & 0x07);
808 *pi_rate = 48000 << (i_index_rate_g1 & 0x07);
812 unsigned i_channels1 = 0;
813 unsigned i_layout1 = 0;
814 for( int i = 0; p_aob_group1[i_assignment][i] != 0; i++ )
817 i_layout1 |= p_aob_group1[i_assignment][i];
820 unsigned i_channels2 = 0;
821 unsigned i_layout2 = 0;
822 if( i_index_size_g2 != 0x0f && i_index_rate_g2 != 0x0f )
824 for( int i = 0; p_aob_group2[i_assignment][i] != 0; i++ )
827 i_layout2 |= p_aob_group2[i_assignment][i];
829 assert( (i_layout1 & i_layout2) == 0 );
831 /* It is enabled only when presents and compatible wih group1 */
832 const bool b_group2_used = i_index_size_g1 == i_index_size_g2 &&
833 i_index_rate_g1 == i_index_rate_g2;
836 *pi_channels = i_channels1 + ( b_group2_used ? i_channels2 : 0 );
837 *pi_layout = i_layout1 | ( b_group2_used ? i_layout2 : 0 );
840 for( unsigned i = 0; i < 2; i++ )
842 const unsigned *p_aob = i == 0 ? p_aob_group1[i_assignment] :
843 p_aob_group2[i_assignment];
844 g[i].i_channels = i == 0 ? i_channels1 :
847 g[i].b_used = i == 0 || b_group2_used;
850 for( unsigned j = 0; j < g[i].i_channels; j++ )
852 g[i].pi_position[j] = 0;
853 for( int k = 0; pi_vlc_chan_order_wg4[k] != 0; k++ )
855 const unsigned i_channel = pi_vlc_chan_order_wg4[k];
856 if( i_channel == p_aob[j] )
858 if( (*pi_layout) & i_channel )
859 g[i].pi_position[j]++;
866 static const uint32_t pi_8channels_in[] =
867 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,
868 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
869 AOUT_CHAN_MIDDLERIGHT, AOUT_CHAN_LFE, 0 };
871 static const uint32_t pi_7channels_in[] =
872 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,
873 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
874 AOUT_CHAN_MIDDLERIGHT, 0 };
876 static const uint32_t pi_6channels_in[] =
877 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,
878 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, AOUT_CHAN_LFE, 0 };
880 static const uint32_t pi_5channels_in[] =
881 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,
882 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, 0 };
884 static const uint32_t pi_4channels_in[] =
885 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
886 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 };
888 static const uint32_t pi_3channels_in[] =
889 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
890 AOUT_CHAN_CENTER, 0 };
893 static int BdHeader( decoder_sys_t *p_sys,
895 unsigned *pi_channels,
896 unsigned *pi_channels_padding,
897 unsigned *pi_original_channels,
899 const uint8_t *p_header )
901 const uint32_t h = GetDWBE( p_header );
902 const uint32_t *pi_channels_in = NULL;
903 switch( ( h & 0xf000) >> 12 )
907 *pi_original_channels = AOUT_CHAN_CENTER;
911 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
915 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER;
916 pi_channels_in = pi_3channels_in;
920 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER;
924 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
925 AOUT_CHAN_REARCENTER;
929 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
930 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
931 pi_channels_in = pi_4channels_in;
935 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
936 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
937 pi_channels_in = pi_5channels_in;
941 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
942 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
944 pi_channels_in = pi_6channels_in;
948 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
949 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
950 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT;
951 pi_channels_in = pi_7channels_in;
955 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
956 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
957 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
959 pi_channels_in = pi_8channels_in;
965 *pi_channels_padding = *pi_channels & 1;
967 switch( (h >> 6) & 0x03 )
972 case 2: /* 20 bits but samples are stored on 24 bits */
973 case 3: /* 24 bits */
979 switch( (h >> 8) & 0x0f )
996 p_sys->i_chans_to_reorder =
997 aout_CheckChannelReorder( pi_channels_in, NULL,
998 *pi_original_channels,
999 p_sys->pi_chan_table );
1005 static int WidiHeader( unsigned *pi_rate,
1006 unsigned *pi_channels, unsigned *pi_original_channels,
1008 const uint8_t *p_header )
1010 if ( p_header[0] != 0xa0 || p_header[1] != 0x06 )
1013 switch( ( p_header[3] & 0x38 ) >> 3 )
1025 if( p_header[3] >> 6 != 0 )
1030 *pi_channels = (p_header[3] & 0x7) + 1;
1032 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
1037 static void VobExtract( block_t *p_aout_buffer, block_t *p_block,
1040 /* 20/24 bits LPCM use special packing */
1043 uint32_t *p_out = (uint32_t *)p_aout_buffer->p_buffer;
1045 while( p_block->i_buffer / 12 )
1048 *(p_out++) = (p_block->p_buffer[ 0] << 24)
1049 | (p_block->p_buffer[ 1] << 16)
1050 | (p_block->p_buffer[ 8] << 8);
1052 *(p_out++) = (p_block->p_buffer[ 2] << 24)
1053 | (p_block->p_buffer[ 3] << 16)
1054 | (p_block->p_buffer[ 9] << 8);
1056 *(p_out++) = (p_block->p_buffer[ 4] << 24)
1057 | (p_block->p_buffer[ 5] << 16)
1058 | (p_block->p_buffer[10] << 8);
1060 *(p_out++) = (p_block->p_buffer[ 6] << 24)
1061 | (p_block->p_buffer[ 7] << 16)
1062 | (p_block->p_buffer[11] << 8);
1064 p_block->i_buffer -= 12;
1065 p_block->p_buffer += 12;
1068 else if( i_bits == 20 )
1070 uint32_t *p_out = (uint32_t *)p_aout_buffer->p_buffer;
1072 while( p_block->i_buffer / 10 )
1075 *(p_out++) = ( p_block->p_buffer[0] << 24)
1076 | ( p_block->p_buffer[1] << 16)
1077 | ((p_block->p_buffer[8] & 0xF0) << 8);
1079 *(p_out++) = ( p_block->p_buffer[2] << 24)
1080 | ( p_block->p_buffer[3] << 16)
1081 | ((p_block->p_buffer[8] & 0x0F) << 12);
1083 *(p_out++) = ( p_block->p_buffer[4] << 24)
1084 | ( p_block->p_buffer[5] << 16)
1085 | ((p_block->p_buffer[9] & 0xF0) << 8);
1087 *(p_out++) = ( p_block->p_buffer[6] << 24)
1088 | ( p_block->p_buffer[7] << 16)
1089 | ((p_block->p_buffer[9] & 0x0F) << 12);
1091 p_block->i_buffer -= 10;
1092 p_block->p_buffer += 10;
1097 assert( i_bits == 16 );
1098 #ifdef WORDS_BIGENDIAN
1099 memcpy( p_aout_buffer->p_buffer, p_block->p_buffer, p_block->i_buffer );
1101 swab( p_block->p_buffer, p_aout_buffer->p_buffer, p_block->i_buffer );
1105 static void AobExtract( block_t *p_aout_buffer,
1106 block_t *p_block, unsigned i_bits, aob_group_t p_group[2] )
1108 const unsigned i_channels = p_group[0].i_channels +
1109 ( p_group[1].b_used ? p_group[1].i_channels : 0 );
1110 uint8_t *p_out = p_aout_buffer->p_buffer;
1112 while( p_block->i_buffer > 0 )
1114 for( int i = 0; i < 2; i++ )
1116 const aob_group_t *g = &p_group[1-i];
1117 const unsigned int i_group_size = 2 * g->i_channels * i_bits / 8;
1119 if( p_block->i_buffer < i_group_size )
1121 p_block->i_buffer = 0;
1124 for( unsigned n = 0; n < 2; n++ )
1126 for( unsigned j = 0; j < g->i_channels && g->b_used; j++ )
1128 const int i_src = n * g->i_channels + j;
1129 const int i_dst = n * i_channels + g->pi_position[j];
1133 p_out[3*i_dst+0] = p_block->p_buffer[2*i_src+0];
1134 p_out[3*i_dst+1] = p_block->p_buffer[2*i_src+1];
1135 p_out[3*i_dst+2] = p_block->p_buffer[4*g->i_channels+i_src];
1137 else if( i_bits == 20 )
1139 p_out[3*i_dst+0] = p_block->p_buffer[2*i_src+0];
1140 p_out[3*i_dst+1] = p_block->p_buffer[2*i_src+1];
1142 p_out[3*i_dst+2] = (p_block->p_buffer[4*g->i_channels+i_src] ) & 0xf0;
1144 p_out[3*i_dst+2] = (p_block->p_buffer[4*g->i_channels+i_src] << 4) & 0xf0;
1148 assert( i_bits == 16 );
1149 p_out[2*i_dst+0] = p_block->p_buffer[2*i_src+0];
1150 p_out[2*i_dst+1] = p_block->p_buffer[2*i_src+1];
1155 p_block->i_buffer -= i_group_size;
1156 p_block->p_buffer += i_group_size;
1159 p_out += (i_bits == 16 ? 2 : 3) * i_channels * 2;
1163 static void BdExtract( block_t *p_aout_buffer, block_t *p_block,
1164 unsigned i_frame_length,
1165 unsigned i_channels, unsigned i_channels_padding,
1168 if( i_bits != 16 || i_channels_padding > 0 )
1170 uint8_t *p_src = p_block->p_buffer;
1171 uint8_t *p_dst = p_aout_buffer->p_buffer;
1172 int dst_inc = ((i_bits == 16) ? 2 : 4) * i_channels;
1174 while( i_frame_length > 0 )
1176 #ifdef WORDS_BIGENDIAN
1177 memcpy( p_dst, p_src, i_channels * i_bits / 8 );
1180 swab( p_src, p_dst, (i_channels + i_channels_padding) * i_bits / 8 );
1182 for (unsigned i = 0; i < i_channels; ++i) {
1184 p_dst[1 + (i * 4)] = p_src[2 + (i * 3)];
1185 p_dst[2 + (i * 4)] = p_src[1 + (i * 3)];
1186 p_dst[3 + (i * 4)] = p_src[i * 3];
1190 p_src += (i_channels + i_channels_padding) * i_bits / 8;
1197 #ifdef WORDS_BIGENDIAN
1198 memcpy( p_aout_buffer->p_buffer, p_block->p_buffer, p_block->i_buffer );
1200 swab( p_block->p_buffer, p_aout_buffer->p_buffer, p_block->i_buffer );