1 /*****************************************************************************
2 * lpcm.c: lpcm decoder/packetizer module
3 *****************************************************************************
4 * Copyright (C) 1999-2008 the VideoLAN team
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 >
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
28 /*****************************************************************************
30 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_codec.h>
41 /*****************************************************************************
43 *****************************************************************************/
44 static int OpenDecoder ( vlc_object_t * );
45 static int OpenPacketizer( vlc_object_t * );
46 static void CloseCommon ( vlc_object_t * );
49 static int OpenEncoder ( vlc_object_t * );
50 static void CloseEncoder ( vlc_object_t * );
51 static block_t *EncodeFrames( encoder_t *, aout_buffer_t * );
56 set_category( CAT_INPUT )
57 set_subcategory( SUBCAT_INPUT_ACODEC )
58 set_description( N_("Linear PCM audio decoder") )
59 set_capability( "decoder", 100 )
60 set_callbacks( OpenDecoder, CloseCommon )
63 set_description( N_("Linear PCM audio packetizer") )
64 set_capability( "packetizer", 100 )
65 set_callbacks( OpenPacketizer, CloseCommon )
69 set_description( N_("Linear PCM audio encoder") )
70 set_capability( "encoder", 100 )
71 set_callbacks( OpenEncoder, CloseEncoder )
72 add_shortcut( "lpcm" )
78 /*****************************************************************************
79 * decoder_sys_t : lpcm decoder descriptor
80 *****************************************************************************/
92 unsigned i_header_size;
109 * - number of frames in this packet (8 bits)
110 * - first access unit (16 bits) == 0x0003 ?
114 * - current frame (5 bits)
115 * - quantisation (2 bits) 0 == 16bps, 1 == 20bps, 2 == 24bps, 3 == illegal
116 * - frequency (2 bits) 0 == 48 kHz, 1 == 96 kHz, 2 == 44.1 kHz, 3 == 32 kHz
118 * - number of channels - 1 (3 bits) 1 == 2 channels
119 * - dynamic range (8 bits) 0x80 == neutral
121 * LPCM DVD-A header (http://dvd-audio.sourceforge.net/spec/aob.shtml)
122 * - continuity counter (8 bits, clipped to 0x00-0x1f)
123 * - header size (16 bits)
124 * - byte pointer to start of first audio frame.
125 * - unknown (8bits, 0x10 for stereo, 0x00 for surround)
126 * - sample size (4+4 bits)
127 * - samplerate (4+4 bits)
129 * - group assignment (8 bits)
131 * - padding(variable)
135 * - number of channels (4 bits)
136 * - frequency (4 bits)
137 * - bits per sample (2 bits)
141 #define LPCM_VOB_HEADER_LEN (6)
142 #define LPCM_AOB_HEADER_LEN (11)
143 #define LPCM_BD_HEADER_LEN (4)
156 unsigned pi_position[6];
159 /*****************************************************************************
161 *****************************************************************************/
162 static void *DecodeFrame ( decoder_t *, block_t ** );
165 static int VobHeader( unsigned *pi_rate,
166 unsigned *pi_channels, unsigned *pi_original_channels,
168 const uint8_t *p_header );
169 static void VobExtract( aout_buffer_t *, block_t *, unsigned i_bits );
171 static int AobHeader( unsigned *pi_rate,
172 unsigned *pi_channels, unsigned *pi_layout,
174 unsigned *pi_padding,
176 const uint8_t *p_header );
177 static void AobExtract( aout_buffer_t *, block_t *, unsigned i_bits, aob_group_t p_group[2] );
179 static int BdHeader( unsigned *pi_rate,
180 unsigned *pi_channels, unsigned *pi_original_channels,
182 const uint8_t *p_header );
183 static void BdExtract( aout_buffer_t *, block_t * );
186 /*****************************************************************************
188 *****************************************************************************/
189 static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
191 decoder_t *p_dec = (decoder_t*)p_this;
192 decoder_sys_t *p_sys;
196 switch( p_dec->fmt_in.i_codec )
199 case VLC_CODEC_DVD_LPCM:
201 i_header_size = LPCM_VOB_HEADER_LEN;
204 case VLC_CODEC_DVDA_LPCM:
206 i_header_size = LPCM_AOB_HEADER_LEN;
209 case VLC_CODEC_BD_LPCM:
211 i_header_size = LPCM_BD_HEADER_LEN;
217 /* Allocate the memory needed to store the decoder's structure */
218 if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
222 p_sys->b_packetizer = b_packetizer;
223 date_Set( &p_sys->end_date, 0 );
224 p_sys->i_type = i_type;
225 p_sys->i_header_size = i_header_size;
227 /* Set output properties */
228 p_dec->fmt_out.i_cat = AUDIO_ES;
235 p_dec->fmt_out.i_codec = VLC_CODEC_DVD_LPCM;
238 p_dec->fmt_out.i_codec = VLC_CODEC_DVDA_LPCM;
243 p_dec->fmt_out.i_codec = VLC_CODEC_BD_LPCM;
249 switch( p_dec->fmt_out.audio.i_bitspersample )
253 p_dec->fmt_out.i_codec = VLC_CODEC_S24B;
254 p_dec->fmt_out.audio.i_bitspersample = 24;
257 p_dec->fmt_out.i_codec = VLC_CODEC_S16B;
258 p_dec->fmt_out.audio.i_bitspersample = 16;
264 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
266 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
271 static int OpenDecoder( vlc_object_t *p_this )
273 return OpenCommon( p_this, false );
275 static int OpenPacketizer( vlc_object_t *p_this )
277 return OpenCommon( p_this, true );
280 /*****************************************************************************
281 * DecodeFrame: decodes an lpcm frame.
282 ****************************************************************************
283 * Beware, this function must be fed with complete frames (PES packet).
284 *****************************************************************************/
285 static void *DecodeFrame( decoder_t *p_dec, block_t **pp_block )
287 decoder_sys_t *p_sys = p_dec->p_sys;
289 unsigned int i_rate = 0, i_original_channels = 0, i_channels = 0, i_bits = 0;
292 if( !pp_block || !*pp_block ) return NULL;
295 *pp_block = NULL; /* So the packet doesn't get re-sent */
297 /* Date management */
298 if( p_block->i_pts > VLC_TS_INVALID &&
299 p_block->i_pts != date_Get( &p_sys->end_date ) )
301 date_Set( &p_sys->end_date, p_block->i_pts );
304 if( !date_Get( &p_sys->end_date ) )
306 /* We've just started the stream, wait for the first PTS. */
307 block_Release( p_block );
311 if( p_block->i_buffer <= p_sys->i_header_size )
313 msg_Err(p_dec, "frame is too short");
314 block_Release( p_block );
319 unsigned i_padding = 0;
320 aob_group_t p_aob_group[2];
321 switch( p_sys->i_type )
324 i_ret = VobHeader( &i_rate, &i_channels, &i_original_channels, &i_bits,
328 i_ret = AobHeader( &i_rate, &i_channels, &i_original_channels, &i_bits, &i_padding,
333 i_ret = BdHeader( &i_rate, &i_channels, &i_original_channels, &i_bits,
340 if( i_ret || p_block->i_buffer <= p_sys->i_header_size + i_padding )
342 msg_Warn( p_dec, "no frame sync or too small frame" );
343 block_Release( p_block );
347 /* Set output properties */
348 if( p_dec->fmt_out.audio.i_rate != i_rate )
350 date_Init( &p_sys->end_date, i_rate, 1 );
351 date_Set( &p_sys->end_date, p_block->i_pts );
353 p_dec->fmt_out.audio.i_rate = i_rate;
354 p_dec->fmt_out.audio.i_channels = i_channels;
355 p_dec->fmt_out.audio.i_original_channels = i_original_channels;
356 p_dec->fmt_out.audio.i_physical_channels = i_original_channels & AOUT_CHAN_PHYSMASK;
358 i_frame_length = (p_block->i_buffer - p_sys->i_header_size - i_padding) / i_channels * 8 / i_bits;
360 if( p_sys->b_packetizer )
362 p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
364 date_Increment( &p_sys->end_date, i_frame_length ) -
367 /* Just pass on the incoming frame */
375 p_dec->fmt_out.i_codec = VLC_CODEC_S16B;
376 p_dec->fmt_out.audio.i_bitspersample = 16;
380 p_dec->fmt_out.i_codec = VLC_CODEC_S24B;
381 p_dec->fmt_out.audio.i_bitspersample = 24;
385 aout_buffer_t *p_aout_buffer;
386 p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_frame_length );
390 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
391 p_aout_buffer->i_length =
392 date_Increment( &p_sys->end_date, i_frame_length )
393 - p_aout_buffer->i_pts;
395 p_block->p_buffer += p_sys->i_header_size + i_padding;
396 p_block->i_buffer -= p_sys->i_header_size + i_padding;
398 switch( p_sys->i_type )
401 VobExtract( p_aout_buffer, p_block, i_bits );
404 AobExtract( p_aout_buffer, p_block, i_bits, p_aob_group );
409 BdExtract( p_aout_buffer, p_block );
413 block_Release( p_block );
414 return p_aout_buffer;
418 /*****************************************************************************
419 * CloseCommon : lpcm decoder destruction
420 *****************************************************************************/
421 static void CloseCommon( vlc_object_t *p_this )
423 decoder_t *p_dec = (decoder_t*)p_this;
424 free( p_dec->p_sys );
428 /*****************************************************************************
429 * OpenEncoder: lpcm encoder construction
430 *****************************************************************************/
431 static int OpenEncoder( vlc_object_t *p_this )
433 encoder_t *p_enc = (encoder_t *)p_this;
434 encoder_sys_t *p_sys;
436 /* We only support DVD LPCM yet. */
437 if( p_enc->fmt_out.i_codec != VLC_CODEC_DVD_LPCM ||
438 ( p_enc->fmt_in.audio.i_rate != 48000 &&
439 p_enc->fmt_in.audio.i_rate != 96000 &&
440 p_enc->fmt_in.audio.i_rate != 44100 &&
441 p_enc->fmt_in.audio.i_rate != 32000 ) ||
442 p_enc->fmt_in.audio.i_channels > 8 )
445 /* Allocate the memory needed to store the encoder's structure */
446 if( ( p_enc->p_sys = p_sys =
447 (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
450 p_enc->pf_encode_audio = EncodeFrames;
451 p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
453 p_enc->fmt_in.audio.i_bitspersample = 16;
454 p_enc->fmt_in.i_codec = VLC_CODEC_S16B;
456 p_enc->fmt_out.i_bitrate =
457 p_enc->fmt_in.audio.i_channels *
458 p_enc->fmt_in.audio.i_rate *
459 p_enc->fmt_in.audio.i_bitspersample;
461 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
462 p_sys->i_rate = p_enc->fmt_in.audio.i_rate;
464 /* In DVD LCPM, a frame is always 150 PTS ticks. */
465 p_sys->i_frame_samples = p_enc->fmt_in.audio.i_rate * 150 / 90000;
466 p_sys->p_buffer = (uint8_t *)malloc(
467 p_sys->i_frame_samples *
468 p_enc->fmt_in.audio.i_channels *
469 p_enc->fmt_in.audio.i_bitspersample);
470 p_sys->i_buffer_used = 0;
471 p_sys->i_frame_num = 0;
476 /*****************************************************************************
477 * CloseEncoder: lpcm encoder destruction
478 *****************************************************************************/
479 static void CloseEncoder ( vlc_object_t *p_this )
484 /*****************************************************************************
485 * EncodeFrames: encode zero or more LCPM audio packets
486 *****************************************************************************/
487 static block_t *EncodeFrames( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
489 encoder_sys_t *p_sys = p_enc->p_sys;
490 block_t *p_first_block = NULL, *p_last_block = NULL;
492 if( !p_aout_buf || !p_aout_buf->i_buffer ) return NULL;
494 const int i_num_frames = ( p_sys->i_buffer_used + p_aout_buf->i_nb_samples ) /
495 p_sys->i_frame_samples;
496 const int i_leftover_samples = ( p_sys->i_buffer_used + p_aout_buf->i_nb_samples ) %
497 p_sys->i_frame_samples;
498 const int i_frame_size = p_sys->i_frame_samples * p_sys->i_channels * 2 + LPCM_VOB_HEADER_LEN;
499 const int i_start_offset = -p_sys->i_buffer_used;
501 uint8_t i_freq_code = 0;
503 switch( p_sys->i_rate ) {
518 int i_bytes_consumed = 0;
520 for ( int i = 0; i < i_num_frames; ++i )
522 block_t *p_block = block_New( p_enc, i_frame_size );
526 uint8_t *frame = (uint8_t *)p_block->p_buffer;
527 frame[0] = 1; /* one frame in packet */
529 frame[2] = 0; /* no first access unit */
530 frame[3] = (p_sys->i_frame_num + i) & 0x1f; /* no emphasis, no mute */
531 frame[4] = (i_freq_code << 4) | (p_sys->i_channels - 1);
532 frame[5] = 0x80; /* neutral dynamic range */
534 const int i_consume_samples = p_sys->i_frame_samples - p_sys->i_buffer_used;
535 const int i_kept_bytes = p_sys->i_buffer_used * p_sys->i_channels * 2;
536 const int i_consume_bytes = i_consume_samples * p_sys->i_channels * 2;
538 memcpy( frame + 6, p_sys->p_buffer, i_kept_bytes );
539 memcpy( frame + 6 + i_kept_bytes, p_aout_buf->p_buffer + i_bytes_consumed, i_consume_bytes );
541 p_sys->i_frame_num++;
542 p_sys->i_buffer_used = 0;
543 i_bytes_consumed += i_consume_bytes;
545 p_block->i_dts = p_block->i_pts = p_aout_buf->i_pts +
546 (i * p_sys->i_frame_samples + i_start_offset) * CLOCK_FREQ / p_sys->i_rate;
547 p_block->i_length = p_sys->i_frame_samples * CLOCK_FREQ / p_sys->i_rate;
550 p_first_block = p_last_block = p_block;
552 p_last_block = p_last_block->p_next = p_block;
555 memcpy( p_sys->p_buffer,
556 p_aout_buf->p_buffer + i_bytes_consumed,
557 i_leftover_samples * p_sys->i_channels * 2 );
558 p_sys->i_buffer_used = i_leftover_samples;
560 return p_first_block;
564 /*****************************************************************************
566 *****************************************************************************/
567 static int VobHeader( unsigned *pi_rate,
568 unsigned *pi_channels, unsigned *pi_original_channels,
570 const uint8_t *p_header )
572 const uint8_t i_header = p_header[4];
574 switch( (i_header >> 4) & 0x3 )
590 *pi_channels = (i_header & 0x7) + 1;
591 switch( *pi_channels - 1 )
594 *pi_original_channels = AOUT_CHAN_CENTER;
597 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
600 /* This is unsure. */
601 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
604 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
605 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
608 /* This is unsure. */
609 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
610 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
614 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
615 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
616 | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
619 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
620 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
621 | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
622 | AOUT_CHAN_MIDDLERIGHT;
625 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
626 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
627 | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
628 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
632 switch( (i_header >> 6) & 0x3 )
646 /* Check frame sync and drop it. */
647 if( p_header[5] != 0x80 )
652 static const unsigned p_aob_group1[21][6] = {
653 { AOUT_CHAN_CENTER, 0 },
654 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
655 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
656 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
657 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
658 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
659 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
660 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
661 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
662 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
663 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
664 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
665 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
666 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, 0 },
667 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, 0 },
668 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, 0 },
669 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, 0 },
670 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, 0 },
671 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
672 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
673 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
675 static const unsigned p_aob_group2[21][6] = {
678 { AOUT_CHAN_REARCENTER, 0 },
679 { AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
680 { AOUT_CHAN_LFE, 0 },
681 { AOUT_CHAN_LFE, AOUT_CHAN_REARCENTER, 0 },
682 { AOUT_CHAN_LFE, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
683 { AOUT_CHAN_CENTER, 0 },
684 { AOUT_CHAN_CENTER, AOUT_CHAN_REARCENTER, 0 },
685 { AOUT_CHAN_CENTER, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
686 { AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 },
687 { AOUT_CHAN_CENTER, AOUT_CHAN_LFE, AOUT_CHAN_REARCENTER, 0 },
688 { AOUT_CHAN_CENTER, AOUT_CHAN_LFE, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
689 { AOUT_CHAN_REARCENTER, 0 },
690 { AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
691 { AOUT_CHAN_LFE, 0 },
692 { AOUT_CHAN_LFE, AOUT_CHAN_REARCENTER, 0 },
693 { AOUT_CHAN_LFE, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
694 { AOUT_CHAN_LFE, 0 },
695 { AOUT_CHAN_CENTER, 0 },
696 { AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 },
699 static int AobHeader( unsigned *pi_rate,
700 unsigned *pi_channels, unsigned *pi_layout,
702 unsigned *pi_padding,
704 const uint8_t *p_header )
706 const unsigned i_header_size = GetWBE( &p_header[1] );
707 if( i_header_size + 3 < LPCM_AOB_HEADER_LEN )
710 *pi_padding = 3+i_header_size - LPCM_AOB_HEADER_LEN;
712 const int i_index_size_g1 = (p_header[6] >> 4) & 0x0f;
713 const int i_index_size_g2 = (p_header[6] ) & 0x0f;
714 const int i_index_rate_g1 = (p_header[7] >> 4) & 0x0f;
715 const int i_index_rate_g2 = (p_header[7] ) & 0x0f;
716 const int i_assignment = p_header[9];
719 if( i_index_size_g1 > 0x02 ||
720 ( i_index_size_g2 != 0x0f && i_index_size_g2 > 0x02 ) )
722 if( (i_index_rate_g1 & 0x07) > 0x02 ||
723 ( i_index_rate_g2 != 0x0f && (i_index_rate_g1 & 0x07) > 0x02 ) )
725 if( i_assignment > 20 )
729 *pi_bits = 16 + 4 * i_index_size_g1;
730 if( i_index_rate_g1 & 0x08 )
731 *pi_rate = 44100 << (i_index_rate_g1 & 0x07);
733 *pi_rate = 48000 << (i_index_rate_g1 & 0x07);
737 unsigned i_channels1 = 0;
738 unsigned i_layout1 = 0;
739 for( int i = 0; p_aob_group1[i_assignment][i] != 0; i++ )
742 i_layout1 |= p_aob_group1[i_assignment][i];
745 unsigned i_channels2 = 0;
746 unsigned i_layout2 = 0;
747 if( i_index_size_g2 != 0x0f && i_index_rate_g2 != 0x0f )
749 for( int i = 0; p_aob_group2[i_assignment][i] != 0; i++ )
752 i_layout2 |= p_aob_group2[i_assignment][i];
754 assert( (i_layout1 & i_layout2) == 0 );
756 /* It is enabled only when presents and compatible wih group1 */
757 const bool b_group2_used = i_index_size_g1 == i_index_size_g2 &&
758 i_index_rate_g1 == i_index_rate_g2;
761 *pi_channels = i_channels1 + ( b_group2_used ? i_channels2 : 0 );
762 *pi_layout = i_layout1 | ( b_group2_used ? i_layout2 : 0 );
765 for( unsigned i = 0; i < 2; i++ )
767 const unsigned *p_aob = i == 0 ? p_aob_group1[i_assignment] :
768 p_aob_group2[i_assignment];
769 g[i].i_channels = i == 0 ? i_channels1 :
772 g[i].b_used = i == 0 || b_group2_used;
775 for( unsigned j = 0; j < g[i].i_channels; j++ )
777 g[i].pi_position[j] = 0;
778 for( int k = 0; pi_vlc_chan_order_wg4[k] != 0; k++ )
780 const unsigned i_channel = pi_vlc_chan_order_wg4[k];
781 if( i_channel == p_aob[j] )
783 if( (*pi_layout) & i_channel )
784 g[i].pi_position[j]++;
791 static int BdHeader( unsigned *pi_rate,
792 unsigned *pi_channels, unsigned *pi_original_channels,
794 const uint8_t *p_header )
796 const uint32_t h = GetDWBE( p_header );
797 switch( ( h & 0xf000) >> 12 )
801 *pi_original_channels = AOUT_CHAN_CENTER;
805 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
809 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER;
813 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER;
817 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
818 AOUT_CHAN_REARCENTER;
822 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
823 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
827 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
828 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
832 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
833 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
838 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
839 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
840 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT;
844 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
845 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
846 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
853 switch( (h >> 6) & 0x03 )
858 case 2: /* 20 bits but samples are stored on 24 bits */
859 case 3: /* 24 bits */
865 switch( (h >> 8) & 0x0f )
882 static void VobExtract( aout_buffer_t *p_aout_buffer, block_t *p_block,
885 uint8_t *p_out = p_aout_buffer->p_buffer;
887 /* 20/24 bits LPCM use special packing */
890 while( p_block->i_buffer / 12 )
893 p_out[0] = p_block->p_buffer[0];
894 p_out[1] = p_block->p_buffer[1];
895 p_out[2] = p_block->p_buffer[8];
897 p_out[3] = p_block->p_buffer[2];
898 p_out[4] = p_block->p_buffer[3];
899 p_out[5] = p_block->p_buffer[9];
901 p_out[6] = p_block->p_buffer[4];
902 p_out[7] = p_block->p_buffer[5];
903 p_out[8] = p_block->p_buffer[10];
905 p_out[9] = p_block->p_buffer[6];
906 p_out[10] = p_block->p_buffer[7];
907 p_out[11] = p_block->p_buffer[11];
909 p_block->i_buffer -= 12;
910 p_block->p_buffer += 12;
914 else if( i_bits == 20 )
916 while( p_block->i_buffer / 10 )
919 p_out[0] = p_block->p_buffer[0];
920 p_out[1] = p_block->p_buffer[1];
921 p_out[2] = p_block->p_buffer[8] & 0xF0;
923 p_out[3] = p_block->p_buffer[2];
924 p_out[4] = p_block->p_buffer[3];
925 p_out[5] = p_block->p_buffer[8] << 4;
927 p_out[6] = p_block->p_buffer[4];
928 p_out[7] = p_block->p_buffer[5];
929 p_out[8] = p_block->p_buffer[9] & 0xF0;
931 p_out[9] = p_block->p_buffer[6];
932 p_out[10] = p_block->p_buffer[7];
933 p_out[11] = p_block->p_buffer[9] << 4;
935 p_block->i_buffer -= 10;
936 p_block->p_buffer += 10;
942 assert( i_bits == 16 );
943 memcpy( p_out, p_block->p_buffer, p_block->i_buffer );
946 static void AobExtract( aout_buffer_t *p_aout_buffer,
947 block_t *p_block, unsigned i_bits, aob_group_t p_group[2] )
949 const unsigned i_channels = p_group[0].i_channels +
950 ( p_group[1].b_used ? p_group[1].i_channels : 0 );
951 uint8_t *p_out = p_aout_buffer->p_buffer;
953 while( p_block->i_buffer > 0 )
955 for( int i = 0; i < 2; i++ )
957 const aob_group_t *g = &p_group[1-i];
958 const unsigned int i_group_size = 2 * g->i_channels * i_bits / 8;
960 if( p_block->i_buffer < i_group_size )
962 p_block->i_buffer = 0;
965 for( unsigned n = 0; n < 2; n++ )
967 for( unsigned j = 0; j < g->i_channels && g->b_used; j++ )
969 const int i_src = n * g->i_channels + j;
970 const int i_dst = n * i_channels + g->pi_position[j];
974 p_out[3*i_dst+0] = p_block->p_buffer[2*i_src+0];
975 p_out[3*i_dst+1] = p_block->p_buffer[2*i_src+1];
976 p_out[3*i_dst+2] = p_block->p_buffer[4*g->i_channels+i_src];
978 else if( i_bits == 20 )
980 p_out[3*i_dst+0] = p_block->p_buffer[2*i_src+0];
981 p_out[3*i_dst+1] = p_block->p_buffer[2*i_src+1];
983 p_out[3*i_dst+2] = (p_block->p_buffer[4*g->i_channels+i_src] ) & 0xf0;
985 p_out[3*i_dst+2] = (p_block->p_buffer[4*g->i_channels+i_src] << 4) & 0xf0;
989 assert( i_bits == 16 );
990 p_out[2*i_dst+0] = p_block->p_buffer[2*i_src+0];
991 p_out[2*i_dst+1] = p_block->p_buffer[2*i_src+1];
996 p_block->i_buffer -= i_group_size;
997 p_block->p_buffer += i_group_size;
1000 p_out += (i_bits == 16 ? 2 : 3) * i_channels * 2;
1004 static void BdExtract( aout_buffer_t *p_aout_buffer, block_t *p_block )
1006 memcpy( p_aout_buffer->p_buffer, p_block->p_buffer, p_block->i_buffer );