+ int i_ret;
+ unsigned i_channels_padding = 0;
+ unsigned i_padding = 0;
+ aob_group_t p_aob_group[2];
+ switch( p_sys->i_type )
+ {
+ case LPCM_VOB:
+ i_ret = VobHeader( &i_rate, &i_channels, &i_original_channels, &i_bits,
+ p_block->p_buffer );
+ break;
+ case LPCM_AOB:
+ i_ret = AobHeader( &i_rate, &i_channels, &i_original_channels, &i_bits, &i_padding,
+ p_aob_group,
+ p_block->p_buffer );
+ break;
+ case LPCM_BD:
+ i_ret = BdHeader( &i_rate, &i_channels, &i_channels_padding, &i_original_channels, &i_bits,
+ p_block->p_buffer );
+ break;
+ default:
+ abort();
+ }
+
+ if( i_ret || p_block->i_buffer <= p_sys->i_header_size + i_padding )
+ {
+ msg_Warn( p_dec, "no frame sync or too small frame" );
+ block_Release( p_block );
+ return NULL;
+ }
+
+ /* Set output properties */
+ if( p_dec->fmt_out.audio.i_rate != i_rate )
+ {
+ date_Init( &p_sys->end_date, i_rate, 1 );
+ date_Set( &p_sys->end_date, p_block->i_pts );
+ }
+ p_dec->fmt_out.audio.i_rate = i_rate;
+ p_dec->fmt_out.audio.i_channels = i_channels;
+ p_dec->fmt_out.audio.i_original_channels = i_original_channels;
+ p_dec->fmt_out.audio.i_physical_channels = i_original_channels & AOUT_CHAN_PHYSMASK;
+
+ i_frame_length = (p_block->i_buffer - p_sys->i_header_size - i_padding) /
+ (i_channels + i_channels_padding) * 8 / i_bits;
+
+ if( p_sys->b_packetizer )
+ {
+ p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
+ p_block->i_length =
+ date_Increment( &p_sys->end_date, i_frame_length ) -
+ p_block->i_pts;
+
+ /* Just pass on the incoming frame */
+ return p_block;
+ }
+ else
+ {
+ /* */
+ if( i_bits == 16 )
+ {
+ p_dec->fmt_out.i_codec = VLC_CODEC_S16B;
+ p_dec->fmt_out.audio.i_bitspersample = 16;
+ }
+ else
+ {
+ p_dec->fmt_out.i_codec = VLC_CODEC_S24B;
+ p_dec->fmt_out.audio.i_bitspersample = 24;
+ }
+
+ /* */
+ aout_buffer_t *p_aout_buffer;
+ p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_frame_length );
+ if( !p_aout_buffer )
+ return NULL;
+
+ p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
+ p_aout_buffer->i_length =
+ date_Increment( &p_sys->end_date, i_frame_length )
+ - p_aout_buffer->i_pts;
+
+ p_block->p_buffer += p_sys->i_header_size + i_padding;
+ p_block->i_buffer -= p_sys->i_header_size + i_padding;
+
+ switch( p_sys->i_type )
+ {
+ case LPCM_VOB:
+ VobExtract( p_aout_buffer, p_block, i_bits );
+ break;
+ case LPCM_AOB:
+ AobExtract( p_aout_buffer, p_block, i_bits, p_aob_group );
+ break;
+ default:
+ assert(0);
+ case LPCM_BD:
+ BdExtract( p_aout_buffer, p_block, i_frame_length, i_channels, i_channels_padding, i_bits );
+ break;
+ }
+
+ block_Release( p_block );
+ return p_aout_buffer;
+ }
+}
+
+/*****************************************************************************
+ * CloseCommon : lpcm decoder destruction
+ *****************************************************************************/
+static void CloseCommon( vlc_object_t *p_this )
+{
+ decoder_t *p_dec = (decoder_t*)p_this;
+ free( p_dec->p_sys );
+}
+
+#ifdef ENABLE_SOUT
+/*****************************************************************************
+ * OpenEncoder: lpcm encoder construction
+ *****************************************************************************/
+static int OpenEncoder( vlc_object_t *p_this )
+{
+ encoder_t *p_enc = (encoder_t *)p_this;
+ encoder_sys_t *p_sys;
+
+ /* We only support DVD LPCM yet. */
+ if( p_enc->fmt_out.i_codec != VLC_CODEC_DVD_LPCM )
+ return VLC_EGENERIC;
+
+ if( p_enc->fmt_in.audio.i_rate != 48000 &&
+ p_enc->fmt_in.audio.i_rate != 96000 &&
+ p_enc->fmt_in.audio.i_rate != 44100 &&
+ p_enc->fmt_in.audio.i_rate != 32000 )
+ {
+ msg_Err( p_enc, "DVD LPCM supports only sample rates of 48, 96, 44.1 or 32 kHz" );
+ return VLC_EGENERIC;
+ }
+
+ if( p_enc->fmt_in.audio.i_channels > 8 )
+ {
+ msg_Err( p_enc, "DVD LPCM supports a maximum of eight channels" );
+ return VLC_EGENERIC;
+ }
+
+ /* Allocate the memory needed to store the encoder's structure */
+ if( ( p_enc->p_sys = p_sys =
+ (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
+ return VLC_ENOMEM;
+
+ /* In DVD LCPM, a frame is always 150 PTS ticks. */
+ p_sys->i_frame_samples = p_enc->fmt_in.audio.i_rate * 150 / 90000;
+ p_sys->p_buffer = (uint8_t *)malloc(
+ p_sys->i_frame_samples *
+ p_enc->fmt_in.audio.i_channels *
+ p_enc->fmt_in.audio.i_bitspersample);
+ p_sys->i_buffer_used = 0;
+ p_sys->i_frame_num = 0;
+
+ p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
+ p_sys->i_rate = p_enc->fmt_in.audio.i_rate;
+
+ p_enc->pf_encode_audio = EncodeFrames;
+ p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
+
+ p_enc->fmt_in.audio.i_bitspersample = 16;
+ p_enc->fmt_in.i_codec = VLC_CODEC_S16B;
+
+ p_enc->fmt_out.i_bitrate =
+ p_enc->fmt_in.audio.i_channels *
+ p_enc->fmt_in.audio.i_rate *
+ p_enc->fmt_in.audio.i_bitspersample *
+ (p_sys->i_frame_samples + LPCM_VOB_HEADER_LEN) /
+ p_sys->i_frame_samples;
+
+ return VLC_SUCCESS;
+}
+
+/*****************************************************************************
+ * CloseEncoder: lpcm encoder destruction
+ *****************************************************************************/
+static void CloseEncoder ( vlc_object_t *p_this )
+{
+ encoder_t *p_enc = (encoder_t *)p_this;
+ encoder_sys_t *p_sys = p_enc->p_sys;
+
+ free( p_sys->p_buffer );
+ free( p_sys );
+}
+
+/*****************************************************************************
+ * EncodeFrames: encode zero or more LCPM audio packets
+ *****************************************************************************/
+static block_t *EncodeFrames( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
+{
+ encoder_sys_t *p_sys = p_enc->p_sys;
+ block_t *p_first_block = NULL, *p_last_block = NULL;
+
+ if( !p_aout_buf || !p_aout_buf->i_buffer ) return NULL;
+
+ const int i_num_frames = ( p_sys->i_buffer_used + p_aout_buf->i_nb_samples ) /
+ p_sys->i_frame_samples;
+ const int i_leftover_samples = ( p_sys->i_buffer_used + p_aout_buf->i_nb_samples ) %
+ p_sys->i_frame_samples;
+ const int i_frame_size = p_sys->i_frame_samples * p_sys->i_channels * 2 + LPCM_VOB_HEADER_LEN;
+ const int i_start_offset = -p_sys->i_buffer_used;
+
+ uint8_t i_freq_code = 0;
+
+ switch( p_sys->i_rate ) {
+ case 48000:
+ i_freq_code = 0;
+ break;
+ case 96000:
+ i_freq_code = 1;
+ break;
+ case 44100:
+ i_freq_code = 2;
+ break;
+ case 32000:
+ i_freq_code = 3;
+ break;
+ default:
+ assert(0);
+ }
+
+ int i_bytes_consumed = 0;
+
+ for ( int i = 0; i < i_num_frames; ++i )
+ {
+ block_t *p_block = block_New( p_enc, i_frame_size );
+ if( !p_block )
+ return NULL;
+
+ uint8_t *frame = (uint8_t *)p_block->p_buffer;
+ frame[0] = 1; /* one frame in packet */
+ frame[1] = 0;
+ frame[2] = 0; /* no first access unit */
+ frame[3] = (p_sys->i_frame_num + i) & 0x1f; /* no emphasis, no mute */
+ frame[4] = (i_freq_code << 4) | (p_sys->i_channels - 1);
+ frame[5] = 0x80; /* neutral dynamic range */
+
+ const int i_consume_samples = p_sys->i_frame_samples - p_sys->i_buffer_used;
+ const int i_kept_bytes = p_sys->i_buffer_used * p_sys->i_channels * 2;
+ const int i_consume_bytes = i_consume_samples * p_sys->i_channels * 2;
+
+ memcpy( frame + 6, p_sys->p_buffer, i_kept_bytes );
+ memcpy( frame + 6 + i_kept_bytes, p_aout_buf->p_buffer + i_bytes_consumed, i_consume_bytes );
+
+ p_sys->i_frame_num++;
+ p_sys->i_buffer_used = 0;
+ i_bytes_consumed += i_consume_bytes;
+
+ /* We need to find i_length by means of next_pts due to possible roundoff errors. */
+ mtime_t this_pts = p_aout_buf->i_pts +
+ (i * p_sys->i_frame_samples + i_start_offset) * CLOCK_FREQ / p_sys->i_rate;
+ mtime_t next_pts = p_aout_buf->i_pts +
+ ((i + 1) * p_sys->i_frame_samples + i_start_offset) * CLOCK_FREQ / p_sys->i_rate;
+
+ p_block->i_pts = p_block->i_dts = this_pts;
+ p_block->i_length = next_pts - this_pts;
+
+ if( !p_first_block )
+ p_first_block = p_last_block = p_block;
+ else
+ p_last_block = p_last_block->p_next = p_block;
+ }
+
+ memcpy( p_sys->p_buffer,
+ p_aout_buf->p_buffer + i_bytes_consumed,
+ i_leftover_samples * p_sys->i_channels * 2 );
+ p_sys->i_buffer_used = i_leftover_samples;
+
+ return p_first_block;
+}
+#endif
+
+/*****************************************************************************
+ *
+ *****************************************************************************/
+static int VobHeader( unsigned *pi_rate,
+ unsigned *pi_channels, unsigned *pi_original_channels,
+ unsigned *pi_bits,
+ const uint8_t *p_header )
+{
+ const uint8_t i_header = p_header[4];
+
+ switch( (i_header >> 4) & 0x3 )