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 )
68 set_description( N_("Linear PCM audio encoder") )
69 set_capability( "encoder", 100 )
70 set_callbacks( OpenEncoder, CloseEncoder )
71 add_shortcut( "lpcm" )
76 /*****************************************************************************
77 * decoder_sys_t : lpcm decoder descriptor
78 *****************************************************************************/
90 unsigned i_header_size;
107 * - number of frames in this packet (8 bits)
108 * - first access unit (16 bits) == 0x0003 ?
112 * - current frame (5 bits)
113 * - quantisation (2 bits) 0 == 16bps, 1 == 20bps, 2 == 24bps, 3 == illegal
114 * - frequency (2 bits) 0 == 48 kHz, 1 == 96 kHz, 2 == 44.1 kHz, 3 == 32 kHz
116 * - number of channels - 1 (3 bits) 1 == 2 channels
117 * - dynamic range (8 bits) 0x80 == neutral
119 * LPCM DVD-A header (http://dvd-audio.sourceforge.net/spec/aob.shtml)
120 * - continuity counter (8 bits, clipped to 0x00-0x1f)
121 * - header size (16 bits)
122 * - byte pointer to start of first audio frame.
123 * - unknown (8bits, 0x10 for stereo, 0x00 for surround)
124 * - sample size (4+4 bits)
125 * - samplerate (4+4 bits)
127 * - group assignment (8 bits)
129 * - padding(variable)
133 * - number of channels (4 bits)
134 * - frequency (4 bits)
135 * - bits per sample (2 bits)
139 #define LPCM_VOB_HEADER_LEN (6)
140 #define LPCM_AOB_HEADER_LEN (11)
141 #define LPCM_BD_HEADER_LEN (4)
154 unsigned pi_position[6];
157 /*****************************************************************************
159 *****************************************************************************/
160 static void *DecodeFrame ( decoder_t *, block_t ** );
163 static int VobHeader( unsigned *pi_rate,
164 unsigned *pi_channels, unsigned *pi_original_channels,
166 const uint8_t *p_header );
167 static void VobExtract( aout_buffer_t *, block_t *, unsigned i_bits );
169 static int AobHeader( unsigned *pi_rate,
170 unsigned *pi_channels, unsigned *pi_layout,
172 unsigned *pi_padding,
174 const uint8_t *p_header );
175 static void AobExtract( aout_buffer_t *, block_t *, unsigned i_bits, aob_group_t p_group[2] );
177 static int BdHeader( unsigned *pi_rate,
178 unsigned *pi_channels, unsigned *pi_original_channels,
180 const uint8_t *p_header );
181 static void BdExtract( aout_buffer_t *, block_t * );
184 /*****************************************************************************
186 *****************************************************************************/
187 static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
189 decoder_t *p_dec = (decoder_t*)p_this;
190 decoder_sys_t *p_sys;
194 switch( p_dec->fmt_in.i_codec )
197 case VLC_CODEC_DVD_LPCM:
199 i_header_size = LPCM_VOB_HEADER_LEN;
202 case VLC_CODEC_DVDA_LPCM:
204 i_header_size = LPCM_AOB_HEADER_LEN;
207 case VLC_CODEC_BD_LPCM:
209 i_header_size = LPCM_BD_HEADER_LEN;
215 /* Allocate the memory needed to store the decoder's structure */
216 if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
220 p_sys->b_packetizer = b_packetizer;
221 date_Set( &p_sys->end_date, 0 );
222 p_sys->i_type = i_type;
223 p_sys->i_header_size = i_header_size;
225 /* Set output properties */
226 p_dec->fmt_out.i_cat = AUDIO_ES;
233 p_dec->fmt_out.i_codec = VLC_CODEC_DVD_LPCM;
236 p_dec->fmt_out.i_codec = VLC_CODEC_DVDA_LPCM;
241 p_dec->fmt_out.i_codec = VLC_CODEC_BD_LPCM;
247 switch( p_dec->fmt_out.audio.i_bitspersample )
251 p_dec->fmt_out.i_codec = VLC_CODEC_S24B;
252 p_dec->fmt_out.audio.i_bitspersample = 24;
255 p_dec->fmt_out.i_codec = VLC_CODEC_S16B;
256 p_dec->fmt_out.audio.i_bitspersample = 16;
262 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
264 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
269 static int OpenDecoder( vlc_object_t *p_this )
271 return OpenCommon( p_this, false );
273 static int OpenPacketizer( vlc_object_t *p_this )
275 return OpenCommon( p_this, true );
278 /*****************************************************************************
279 * DecodeFrame: decodes an lpcm frame.
280 ****************************************************************************
281 * Beware, this function must be fed with complete frames (PES packet).
282 *****************************************************************************/
283 static void *DecodeFrame( decoder_t *p_dec, block_t **pp_block )
285 decoder_sys_t *p_sys = p_dec->p_sys;
287 unsigned int i_rate = 0, i_original_channels = 0, i_channels = 0, i_bits = 0;
290 if( !pp_block || !*pp_block ) return NULL;
293 *pp_block = NULL; /* So the packet doesn't get re-sent */
295 /* Date management */
296 if( p_block->i_pts > VLC_TS_INVALID &&
297 p_block->i_pts != date_Get( &p_sys->end_date ) )
299 date_Set( &p_sys->end_date, p_block->i_pts );
302 if( !date_Get( &p_sys->end_date ) )
304 /* We've just started the stream, wait for the first PTS. */
305 block_Release( p_block );
309 if( p_block->i_buffer <= p_sys->i_header_size )
311 msg_Err(p_dec, "frame is too short");
312 block_Release( p_block );
317 unsigned i_padding = 0;
318 aob_group_t p_aob_group[2];
319 switch( p_sys->i_type )
322 i_ret = VobHeader( &i_rate, &i_channels, &i_original_channels, &i_bits,
326 i_ret = AobHeader( &i_rate, &i_channels, &i_original_channels, &i_bits, &i_padding,
331 i_ret = BdHeader( &i_rate, &i_channels, &i_original_channels, &i_bits,
338 if( i_ret || p_block->i_buffer <= p_sys->i_header_size + i_padding )
340 msg_Warn( p_dec, "no frame sync or too small frame" );
341 block_Release( p_block );
345 /* Set output properties */
346 if( p_dec->fmt_out.audio.i_rate != i_rate )
348 date_Init( &p_sys->end_date, i_rate, 1 );
349 date_Set( &p_sys->end_date, p_block->i_pts );
351 p_dec->fmt_out.audio.i_rate = i_rate;
352 p_dec->fmt_out.audio.i_channels = i_channels;
353 p_dec->fmt_out.audio.i_original_channels = i_original_channels;
354 p_dec->fmt_out.audio.i_physical_channels = i_original_channels & AOUT_CHAN_PHYSMASK;
356 i_frame_length = (p_block->i_buffer - p_sys->i_header_size - i_padding) / i_channels * 8 / i_bits;
358 if( p_sys->b_packetizer )
360 p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
362 date_Increment( &p_sys->end_date, i_frame_length ) -
365 /* Just pass on the incoming frame */
373 p_dec->fmt_out.i_codec = VLC_CODEC_S16B;
374 p_dec->fmt_out.audio.i_bitspersample = 16;
378 p_dec->fmt_out.i_codec = VLC_CODEC_S24B;
379 p_dec->fmt_out.audio.i_bitspersample = 24;
383 aout_buffer_t *p_aout_buffer;
384 p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_frame_length );
388 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
389 p_aout_buffer->i_length =
390 date_Increment( &p_sys->end_date, i_frame_length )
391 - p_aout_buffer->i_pts;
393 p_block->p_buffer += p_sys->i_header_size + i_padding;
394 p_block->i_buffer -= p_sys->i_header_size + i_padding;
396 switch( p_sys->i_type )
399 VobExtract( p_aout_buffer, p_block, i_bits );
402 AobExtract( p_aout_buffer, p_block, i_bits, p_aob_group );
407 BdExtract( p_aout_buffer, p_block );
411 block_Release( p_block );
412 return p_aout_buffer;
416 /*****************************************************************************
417 * CloseCommon : lpcm decoder destruction
418 *****************************************************************************/
419 static void CloseCommon( vlc_object_t *p_this )
421 decoder_t *p_dec = (decoder_t*)p_this;
422 free( p_dec->p_sys );
425 /*****************************************************************************
426 * OpenEncoder: lpcm encoder construction
427 *****************************************************************************/
428 static int OpenEncoder( vlc_object_t *p_this )
430 encoder_t *p_enc = (encoder_t *)p_this;
431 encoder_sys_t *p_sys;
433 /* We only support DVD LPCM yet. */
434 if( p_enc->fmt_out.i_codec != VLC_CODEC_DVD_LPCM ||
435 ( p_enc->fmt_in.audio.i_rate != 48000 &&
436 p_enc->fmt_in.audio.i_rate != 96000 &&
437 p_enc->fmt_in.audio.i_rate != 44100 &&
438 p_enc->fmt_in.audio.i_rate != 32000 ) ||
439 p_enc->fmt_in.audio.i_channels > 8 )
442 /* Allocate the memory needed to store the encoder's structure */
443 if( ( p_enc->p_sys = p_sys =
444 (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
447 p_enc->pf_encode_audio = EncodeFrames;
448 p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
450 p_enc->fmt_in.audio.i_bitspersample = 16;
451 p_enc->fmt_in.i_codec = VLC_CODEC_S16B;
453 p_enc->fmt_out.i_bitrate =
454 p_enc->fmt_in.audio.i_channels *
455 p_enc->fmt_in.audio.i_rate *
456 p_enc->fmt_in.audio.i_bitspersample;
458 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
459 p_sys->i_rate = p_enc->fmt_in.audio.i_rate;
461 /* In DVD LCPM, a frame is always 150 PTS ticks. */
462 p_sys->i_frame_samples = p_enc->fmt_in.audio.i_rate * 150 / 90000;
463 p_sys->p_buffer = (uint8_t *)malloc(
464 p_sys->i_frame_samples *
465 p_enc->fmt_in.audio.i_channels *
466 p_enc->fmt_in.audio.i_bitspersample);
467 p_sys->i_buffer_used = 0;
468 p_sys->i_frame_num = 0;
473 /*****************************************************************************
474 * CloseEncoder: lpcm encoder destruction
475 *****************************************************************************/
476 static void CloseEncoder ( vlc_object_t *p_this )
481 /*****************************************************************************
482 * EncodeFrames: encode zero or more LCPM audio packets
483 *****************************************************************************/
484 static block_t *EncodeFrames( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
486 encoder_sys_t *p_sys = p_enc->p_sys;
487 block_t *p_first_block = NULL, *p_last_block = NULL;
489 if( !p_aout_buf || !p_aout_buf->i_buffer ) return NULL;
491 const int i_num_frames = ( p_sys->i_buffer_used + p_aout_buf->i_nb_samples ) /
492 p_sys->i_frame_samples;
493 const int i_leftover_samples = ( p_sys->i_buffer_used + p_aout_buf->i_nb_samples ) %
494 p_sys->i_frame_samples;
495 const int i_frame_size = p_sys->i_frame_samples * p_sys->i_channels * 2 + LPCM_VOB_HEADER_LEN;
497 uint8_t i_freq_code = 0;
499 switch( p_sys->i_rate ) {
514 int i_bytes_consumed = 0;
516 for ( int i = 0; i < i_num_frames; ++i )
518 block_t *p_block = block_New( p_enc, i_frame_size );
522 uint8_t *frame = (uint8_t *)p_block->p_buffer;
523 frame[0] = 1; /* one frame in packet */
525 frame[2] = 0; /* no first access unit */
526 frame[3] = (p_sys->i_frame_num + i) & 0x1f; /* no emphasis, no mute */
527 frame[4] = (i_freq_code << 4) | (p_sys->i_channels - 1);
528 frame[5] = 0x80; /* neutral dynamic range */
530 const int i_consume_samples = p_sys->i_frame_samples - p_sys->i_buffer_used;
531 const int i_kept_bytes = p_sys->i_buffer_used * p_sys->i_channels * 2;
532 const int i_consume_bytes = i_consume_samples * p_sys->i_channels * 2;
534 memcpy( frame + 6, p_sys->p_buffer, i_kept_bytes );
535 memcpy( frame + 6 + i_kept_bytes, p_aout_buf->p_buffer + i_bytes_consumed, i_consume_bytes );
537 p_sys->i_frame_num++;
538 p_sys->i_buffer_used = 0;
539 i_bytes_consumed += i_consume_bytes;
542 * The pts is strictly not correct if we have samples kept from
543 * a previous buffer, but the frames are so small it should be OK.
545 p_block->i_dts = p_block->i_pts = p_aout_buf->i_pts + i * p_sys->i_frame_samples * CLOCK_FREQ / p_sys->i_rate;
546 p_block->i_length = p_sys->i_frame_samples * CLOCK_FREQ / p_sys->i_rate;
549 p_first_block = p_last_block = p_block;
551 block_ChainLastAppend( &p_last_block, p_block );
554 memcpy( p_sys->p_buffer,
555 p_aout_buf->p_buffer + i_bytes_consumed,
556 i_leftover_samples * p_sys->i_channels * 2 );
557 p_sys->i_buffer_used = i_leftover_samples;
559 return p_first_block;
562 /*****************************************************************************
564 *****************************************************************************/
565 static int VobHeader( unsigned *pi_rate,
566 unsigned *pi_channels, unsigned *pi_original_channels,
568 const uint8_t *p_header )
570 const uint8_t i_header = p_header[4];
572 switch( (i_header >> 4) & 0x3 )
588 *pi_channels = (i_header & 0x7) + 1;
589 switch( *pi_channels - 1 )
592 *pi_original_channels = AOUT_CHAN_CENTER;
595 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
598 /* This is unsure. */
599 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
602 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
603 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
606 /* This is unsure. */
607 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
608 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
612 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
613 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
614 | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
617 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
618 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
619 | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
620 | AOUT_CHAN_MIDDLERIGHT;
623 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
624 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
625 | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
626 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
630 switch( (i_header >> 6) & 0x3 )
644 /* Check frame sync and drop it. */
645 if( p_header[5] != 0x80 )
650 static const unsigned p_aob_group1[21][6] = {
651 { AOUT_CHAN_CENTER, 0 },
652 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
653 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 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, AOUT_CHAN_CENTER, 0 },
665 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, 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_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
670 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
671 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
673 static const unsigned p_aob_group2[21][6] = {
676 { AOUT_CHAN_REARCENTER, 0 },
677 { AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
678 { AOUT_CHAN_LFE, 0 },
679 { AOUT_CHAN_LFE, AOUT_CHAN_REARCENTER, 0 },
680 { AOUT_CHAN_LFE, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
681 { AOUT_CHAN_CENTER, 0 },
682 { AOUT_CHAN_CENTER, AOUT_CHAN_REARCENTER, 0 },
683 { AOUT_CHAN_CENTER, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
684 { AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 },
685 { AOUT_CHAN_CENTER, AOUT_CHAN_LFE, AOUT_CHAN_REARCENTER, 0 },
686 { AOUT_CHAN_CENTER, AOUT_CHAN_LFE, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
687 { AOUT_CHAN_REARCENTER, 0 },
688 { AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
689 { AOUT_CHAN_LFE, 0 },
690 { AOUT_CHAN_LFE, AOUT_CHAN_REARCENTER, 0 },
691 { AOUT_CHAN_LFE, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
692 { AOUT_CHAN_LFE, 0 },
693 { AOUT_CHAN_CENTER, 0 },
694 { AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 },
697 static int AobHeader( unsigned *pi_rate,
698 unsigned *pi_channels, unsigned *pi_layout,
700 unsigned *pi_padding,
702 const uint8_t *p_header )
704 const unsigned i_header_size = GetWBE( &p_header[1] );
705 if( i_header_size + 3 < LPCM_AOB_HEADER_LEN )
708 *pi_padding = 3+i_header_size - LPCM_AOB_HEADER_LEN;
710 const int i_index_size_g1 = (p_header[6] >> 4) & 0x0f;
711 const int i_index_size_g2 = (p_header[6] ) & 0x0f;
712 const int i_index_rate_g1 = (p_header[7] >> 4) & 0x0f;
713 const int i_index_rate_g2 = (p_header[7] ) & 0x0f;
714 const int i_assignment = p_header[9];
717 if( i_index_size_g1 > 0x02 ||
718 ( i_index_size_g2 != 0x0f && i_index_size_g2 > 0x02 ) )
720 if( (i_index_rate_g1 & 0x07) > 0x02 ||
721 ( i_index_rate_g2 != 0x0f && (i_index_rate_g1 & 0x07) > 0x02 ) )
723 if( i_assignment > 20 )
727 *pi_bits = 16 + 4 * i_index_size_g1;
728 if( i_index_rate_g1 & 0x08 )
729 *pi_rate = 44100 << (i_index_rate_g1 & 0x07);
731 *pi_rate = 48000 << (i_index_rate_g1 & 0x07);
735 unsigned i_channels1 = 0;
736 unsigned i_layout1 = 0;
737 for( int i = 0; p_aob_group1[i_assignment][i] != 0; i++ )
740 i_layout1 |= p_aob_group1[i_assignment][i];
743 unsigned i_channels2 = 0;
744 unsigned i_layout2 = 0;
745 if( i_index_size_g2 != 0x0f && i_index_rate_g2 != 0x0f )
747 for( int i = 0; p_aob_group2[i_assignment][i] != 0; i++ )
750 i_layout2 |= p_aob_group2[i_assignment][i];
752 assert( (i_layout1 & i_layout2) == 0 );
754 /* It is enabled only when presents and compatible wih group1 */
755 const bool b_group2_used = i_index_size_g1 == i_index_size_g2 &&
756 i_index_rate_g1 == i_index_rate_g2;
759 *pi_channels = i_channels1 + ( b_group2_used ? i_channels2 : 0 );
760 *pi_layout = i_layout1 | ( b_group2_used ? i_layout2 : 0 );
763 for( unsigned i = 0; i < 2; i++ )
765 const unsigned *p_aob = i == 0 ? p_aob_group1[i_assignment] :
766 p_aob_group2[i_assignment];
767 g[i].i_channels = i == 0 ? i_channels1 :
770 g[i].b_used = i == 0 || b_group2_used;
773 for( unsigned j = 0; j < g[i].i_channels; j++ )
775 g[i].pi_position[j] = 0;
776 for( int k = 0; pi_vlc_chan_order_wg4[k] != 0; k++ )
778 const unsigned i_channel = pi_vlc_chan_order_wg4[k];
779 if( i_channel == p_aob[j] )
781 if( (*pi_layout) & i_channel )
782 g[i].pi_position[j]++;
789 static int BdHeader( unsigned *pi_rate,
790 unsigned *pi_channels, unsigned *pi_original_channels,
792 const uint8_t *p_header )
794 const uint32_t h = GetDWBE( p_header );
795 switch( ( h & 0xf000) >> 12 )
799 *pi_original_channels = AOUT_CHAN_CENTER;
803 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
807 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER;
811 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER;
815 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
816 AOUT_CHAN_REARCENTER;
820 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
821 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
825 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
826 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
830 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
831 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
836 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
837 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
838 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT;
842 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
843 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
844 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
851 switch( (h >> 6) & 0x03 )
856 case 2: /* 20 bits but samples are stored on 24 bits */
857 case 3: /* 24 bits */
863 switch( (h >> 8) & 0x0f )
880 static void VobExtract( aout_buffer_t *p_aout_buffer, block_t *p_block,
883 uint8_t *p_out = p_aout_buffer->p_buffer;
885 /* 20/24 bits LPCM use special packing */
888 while( p_block->i_buffer / 12 )
891 p_out[0] = p_block->p_buffer[0];
892 p_out[1] = p_block->p_buffer[1];
893 p_out[2] = p_block->p_buffer[8];
895 p_out[3] = p_block->p_buffer[2];
896 p_out[4] = p_block->p_buffer[3];
897 p_out[5] = p_block->p_buffer[9];
899 p_out[6] = p_block->p_buffer[4];
900 p_out[7] = p_block->p_buffer[5];
901 p_out[8] = p_block->p_buffer[10];
903 p_out[9] = p_block->p_buffer[6];
904 p_out[10] = p_block->p_buffer[7];
905 p_out[11] = p_block->p_buffer[11];
907 p_block->i_buffer -= 12;
908 p_block->p_buffer += 12;
912 else if( i_bits == 20 )
914 while( p_block->i_buffer / 10 )
917 p_out[0] = p_block->p_buffer[0];
918 p_out[1] = p_block->p_buffer[1];
919 p_out[2] = p_block->p_buffer[8] & 0xF0;
921 p_out[3] = p_block->p_buffer[2];
922 p_out[4] = p_block->p_buffer[3];
923 p_out[5] = p_block->p_buffer[8] << 4;
925 p_out[6] = p_block->p_buffer[4];
926 p_out[7] = p_block->p_buffer[5];
927 p_out[8] = p_block->p_buffer[9] & 0xF0;
929 p_out[9] = p_block->p_buffer[6];
930 p_out[10] = p_block->p_buffer[7];
931 p_out[11] = p_block->p_buffer[9] << 4;
933 p_block->i_buffer -= 10;
934 p_block->p_buffer += 10;
940 assert( i_bits == 16 );
941 memcpy( p_out, p_block->p_buffer, p_block->i_buffer );
944 static void AobExtract( aout_buffer_t *p_aout_buffer,
945 block_t *p_block, unsigned i_bits, aob_group_t p_group[2] )
947 const unsigned i_channels = p_group[0].i_channels +
948 ( p_group[1].b_used ? p_group[1].i_channels : 0 );
949 uint8_t *p_out = p_aout_buffer->p_buffer;
951 while( p_block->i_buffer > 0 )
953 for( int i = 0; i < 2; i++ )
955 const aob_group_t *g = &p_group[1-i];
956 const unsigned int i_group_size = 2 * g->i_channels * i_bits / 8;
958 if( p_block->i_buffer < i_group_size )
960 p_block->i_buffer = 0;
963 for( unsigned n = 0; n < 2; n++ )
965 for( unsigned j = 0; j < g->i_channels && g->b_used; j++ )
967 const int i_src = n * g->i_channels + j;
968 const int i_dst = n * i_channels + g->pi_position[j];
972 p_out[3*i_dst+0] = p_block->p_buffer[2*i_src+0];
973 p_out[3*i_dst+1] = p_block->p_buffer[2*i_src+1];
974 p_out[3*i_dst+2] = p_block->p_buffer[4*g->i_channels+i_src];
976 else if( i_bits == 20 )
978 p_out[3*i_dst+0] = p_block->p_buffer[2*i_src+0];
979 p_out[3*i_dst+1] = p_block->p_buffer[2*i_src+1];
981 p_out[3*i_dst+2] = (p_block->p_buffer[4*g->i_channels+i_src] ) & 0xf0;
983 p_out[3*i_dst+2] = (p_block->p_buffer[4*g->i_channels+i_src] << 4) & 0xf0;
987 assert( i_bits == 16 );
988 p_out[2*i_dst+0] = p_block->p_buffer[2*i_src+0];
989 p_out[2*i_dst+1] = p_block->p_buffer[2*i_src+1];
994 p_block->i_buffer -= i_group_size;
995 p_block->p_buffer += i_group_size;
998 p_out += (i_bits == 16 ? 2 : 3) * i_channels * 2;
1002 static void BdExtract( aout_buffer_t *p_aout_buffer, block_t *p_block )
1004 memcpy( p_aout_buffer->p_buffer, p_block->p_buffer, p_block->i_buffer );