/*****************************************************************************
* lpcm.c: lpcm decoder/packetizer module
*****************************************************************************
- * Copyright (C) 1999-2008 the VideoLAN team
+ * Copyright (C) 1999-2008 VLC authors and VideoLAN
* $Id$
*
* Authors: Samuel Hocevar <sam@zoy.org>
* Christophe Massiot <massiot@via.ecp.fr>
* Gildas Bazin <gbazin@videolan.org>
* Lauren Aimar <fenrir _AT_ videolan _DOT_ org >
+ * Steinar H. Gunderson <steinar+vlc@gunderson.no>
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
#include <vlc_plugin.h>
#include <vlc_codec.h>
#include <vlc_aout.h>
+#include <unistd.h>
#include <assert.h>
/*****************************************************************************
static int OpenPacketizer( vlc_object_t * );
static void CloseCommon ( vlc_object_t * );
+#ifdef ENABLE_SOUT
+static int OpenEncoder ( vlc_object_t * );
+static void CloseEncoder ( vlc_object_t * );
+static block_t *EncodeFrames( encoder_t *, block_t * );
+#endif
+
vlc_module_begin ()
set_category( CAT_INPUT )
set_capability( "packetizer", 100 )
set_callbacks( OpenPacketizer, CloseCommon )
+#ifdef ENABLE_SOUT
+ add_submodule ()
+ set_description( N_("Linear PCM audio encoder") )
+ set_capability( "encoder", 100 )
+ set_callbacks( OpenEncoder, CloseEncoder )
+ add_shortcut( "lpcm" )
+#endif
+
vlc_module_end ()
int i_type;
};
+#ifdef ENABLE_SOUT
+struct encoder_sys_t
+{
+ int i_channels;
+ int i_rate;
+
+ int i_frame_samples;
+ uint8_t *p_buffer;
+ int i_buffer_used;
+ int i_frame_num;
+};
+#endif
+
/*
* LPCM DVD header :
* - number of frames in this packet (8 bits)
* - padding(variable)
*
* LPCM BD header :
- * - unkown (16 bits)
+ * - unknown (16 bits)
* - number of channels (4 bits)
* - frequency (4 bits)
* - bits per sample (2 bits)
/*****************************************************************************
* Local prototypes
*****************************************************************************/
-static void *DecodeFrame ( decoder_t *, block_t ** );
+static block_t *DecodeFrame ( decoder_t *, block_t ** );
/* */
static int VobHeader( unsigned *pi_rate,
unsigned *pi_channels, unsigned *pi_original_channels,
unsigned *pi_bits,
const uint8_t *p_header );
-static void VobExtract( aout_buffer_t *, block_t *, unsigned i_bits );
+static void VobExtract( block_t *, block_t *, unsigned i_bits );
/* */
static int AobHeader( unsigned *pi_rate,
unsigned *pi_channels, unsigned *pi_layout,
unsigned *pi_padding,
aob_group_t g[2],
const uint8_t *p_header );
-static void AobExtract( aout_buffer_t *, block_t *, unsigned i_bits, aob_group_t p_group[2] );
+static void AobExtract( block_t *, block_t *, unsigned i_bits, aob_group_t p_group[2] );
/* */
static int BdHeader( unsigned *pi_rate,
- unsigned *pi_channels, unsigned *pi_original_channels,
+ unsigned *pi_channels,
+ unsigned *pi_channels_padding,
+ unsigned *pi_original_channels,
unsigned *pi_bits,
const uint8_t *p_header );
-static void BdExtract( aout_buffer_t *, block_t * );
+static void BdExtract( block_t *, block_t *, unsigned, unsigned, unsigned, unsigned );
/*****************************************************************************
{
case 24:
case 20:
- p_dec->fmt_out.i_codec = VLC_CODEC_S24B;
- p_dec->fmt_out.audio.i_bitspersample = 24;
+ p_dec->fmt_out.i_codec = VLC_CODEC_S32N;
+ p_dec->fmt_out.audio.i_bitspersample = 32;
break;
default:
- p_dec->fmt_out.i_codec = VLC_CODEC_S16B;
+ p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
p_dec->fmt_out.audio.i_bitspersample = 16;
break;
}
}
/* Set callback */
- p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
- DecodeFrame;
- p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
- DecodeFrame;
+ p_dec->pf_decode_audio = DecodeFrame;
+ p_dec->pf_packetize = DecodeFrame;
return VLC_SUCCESS;
}
****************************************************************************
* Beware, this function must be fed with complete frames (PES packet).
*****************************************************************************/
-static void *DecodeFrame( decoder_t *p_dec, block_t **pp_block )
+static block_t *DecodeFrame( decoder_t *p_dec, block_t **pp_block )
{
decoder_sys_t *p_sys = p_dec->p_sys;
block_t *p_block;
}
int i_ret;
+ unsigned i_channels_padding = 0;
unsigned i_padding = 0;
aob_group_t p_aob_group[2];
switch( p_sys->i_type )
p_block->p_buffer );
break;
case LPCM_BD:
- i_ret = BdHeader( &i_rate, &i_channels, &i_original_channels, &i_bits,
+ i_ret = BdHeader( &i_rate, &i_channels, &i_channels_padding, &i_original_channels, &i_bits,
p_block->p_buffer );
break;
default:
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;
+ p_dec->fmt_out.audio.i_physical_channels = i_original_channels;
- i_frame_length = (p_block->i_buffer - p_sys->i_header_size - i_padding) / i_channels * 8 / i_bits;
+ 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 )
{
/* */
if( i_bits == 16 )
{
- p_dec->fmt_out.i_codec = VLC_CODEC_S16B;
+ p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
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;
+ p_dec->fmt_out.i_codec = VLC_CODEC_S32N;
+ p_dec->fmt_out.audio.i_bitspersample = 32;
}
/* */
- aout_buffer_t *p_aout_buffer;
+ block_t *p_aout_buffer;
p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_frame_length );
if( !p_aout_buffer )
return NULL;
default:
assert(0);
case LPCM_BD:
- BdExtract( p_aout_buffer, p_block );
+ BdExtract( p_aout_buffer, p_block, i_frame_length, i_channels, i_channels_padding, i_bits );
break;
}
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 = xmalloc(p_sys->i_frame_samples
+ * p_enc->fmt_in.audio.i_channels * 16);
+ 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_S16N;
+
+ 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, block_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_Alloc( 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;
+
+#ifdef WORDS_BIGENDIAN
+ 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 );
+#else
+ swab( p_sys->p_buffer, frame + 6, i_kept_bytes );
+ swab( p_aout_buf->p_buffer + i_bytes_consumed, frame + 6 + i_kept_bytes,
+ i_consume_bytes );
+#endif
+
+ 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 BdHeader( unsigned *pi_rate,
- unsigned *pi_channels, unsigned *pi_original_channels,
+ unsigned *pi_channels,
+ unsigned *pi_channels_padding,
+ unsigned *pi_original_channels,
unsigned *pi_bits,
const uint8_t *p_header )
{
default:
return -1;
}
+ *pi_channels_padding = *pi_channels & 1;
+
switch( (h >> 6) & 0x03 )
{
case 1:
return 0;
}
-static void VobExtract( aout_buffer_t *p_aout_buffer, block_t *p_block,
+static void VobExtract( block_t *p_aout_buffer, block_t *p_block,
unsigned i_bits )
{
- uint8_t *p_out = p_aout_buffer->p_buffer;
-
/* 20/24 bits LPCM use special packing */
if( i_bits == 24 )
{
+ uint32_t *p_out = (uint32_t *)p_aout_buffer->p_buffer;
+
while( p_block->i_buffer / 12 )
{
/* Sample 1 */
- p_out[0] = p_block->p_buffer[0];
- p_out[1] = p_block->p_buffer[1];
- p_out[2] = p_block->p_buffer[8];
+ *(p_out++) = (p_block->p_buffer[ 0] << 24)
+ | (p_block->p_buffer[ 1] << 16)
+ | (p_block->p_buffer[ 8] << 8);
/* Sample 2 */
- p_out[3] = p_block->p_buffer[2];
- p_out[4] = p_block->p_buffer[3];
- p_out[5] = p_block->p_buffer[9];
+ *(p_out++) = (p_block->p_buffer[ 2] << 24)
+ | (p_block->p_buffer[ 3] << 16)
+ | (p_block->p_buffer[ 9] << 8);
/* Sample 3 */
- p_out[6] = p_block->p_buffer[4];
- p_out[7] = p_block->p_buffer[5];
- p_out[8] = p_block->p_buffer[10];
+ *(p_out++) = (p_block->p_buffer[ 4] << 24)
+ | (p_block->p_buffer[ 5] << 16)
+ | (p_block->p_buffer[10] << 8);
/* Sample 4 */
- p_out[9] = p_block->p_buffer[6];
- p_out[10] = p_block->p_buffer[7];
- p_out[11] = p_block->p_buffer[11];
+ *(p_out++) = (p_block->p_buffer[ 6] << 24)
+ | (p_block->p_buffer[ 7] << 16)
+ | (p_block->p_buffer[11] << 8);
p_block->i_buffer -= 12;
p_block->p_buffer += 12;
- p_out += 12;
}
}
else if( i_bits == 20 )
{
+ uint32_t *p_out = (uint32_t *)p_aout_buffer->p_buffer;
+
while( p_block->i_buffer / 10 )
{
/* Sample 1 */
- p_out[0] = p_block->p_buffer[0];
- p_out[1] = p_block->p_buffer[1];
- p_out[2] = p_block->p_buffer[8] & 0xF0;
+ *(p_out++) = ( p_block->p_buffer[0] << 24)
+ | ( p_block->p_buffer[1] << 16)
+ | ((p_block->p_buffer[8] & 0xF0) << 8);
/* Sample 2 */
- p_out[3] = p_block->p_buffer[2];
- p_out[4] = p_block->p_buffer[3];
- p_out[5] = p_block->p_buffer[8] << 4;
+ *(p_out++) = ( p_block->p_buffer[2] << 24)
+ | ( p_block->p_buffer[3] << 16)
+ | ((p_block->p_buffer[8] & 0x0F) << 12);
/* Sample 3 */
- p_out[6] = p_block->p_buffer[4];
- p_out[7] = p_block->p_buffer[5];
- p_out[8] = p_block->p_buffer[9] & 0xF0;
+ *(p_out++) = ( p_block->p_buffer[4] << 24)
+ | ( p_block->p_buffer[5] << 16)
+ | ((p_block->p_buffer[9] & 0xF0) << 8);
/* Sample 4 */
- p_out[9] = p_block->p_buffer[6];
- p_out[10] = p_block->p_buffer[7];
- p_out[11] = p_block->p_buffer[9] << 4;
+ *(p_out++) = ( p_block->p_buffer[6] << 24)
+ | ( p_block->p_buffer[7] << 16)
+ | ((p_block->p_buffer[9] & 0x0F) << 12);
p_block->i_buffer -= 10;
p_block->p_buffer += 10;
- p_out += 12;
}
}
else
{
assert( i_bits == 16 );
- memcpy( p_out, p_block->p_buffer, p_block->i_buffer );
+#ifdef WORDS_BIGENDIAN
+ memcpy( p_aout_buffer->p_buffer, p_block->p_buffer, p_block->i_buffer );
+#else
+ swab( p_block->p_buffer, p_aout_buffer->p_buffer, p_block->i_buffer );
+#endif
}
}
-static void AobExtract( aout_buffer_t *p_aout_buffer,
+static void AobExtract( block_t *p_aout_buffer,
block_t *p_block, unsigned i_bits, aob_group_t p_group[2] )
{
const unsigned i_channels = p_group[0].i_channels +
}
}
-static void BdExtract( aout_buffer_t *p_aout_buffer, block_t *p_block )
+static void BdExtract( block_t *p_aout_buffer, block_t *p_block,
+ unsigned i_frame_length,
+ unsigned i_channels, unsigned i_channels_padding,
+ unsigned i_bits )
{
- memcpy( p_aout_buffer->p_buffer, p_block->p_buffer, p_block->i_buffer );
+ if( i_channels_padding > 0 )
+ {
+ uint8_t *p_src = p_block->p_buffer;
+ uint8_t *p_dst = p_aout_buffer->p_buffer;
+ while( i_frame_length > 0 )
+ {
+ memcpy( p_dst, p_src, i_channels * i_bits / 8 );
+ p_src += (i_channels + i_channels_padding) * i_bits / 8;
+ p_dst += (i_channels + 0) * i_bits / 8;
+ i_frame_length--;
+ }
+ }
+ else
+ {
+ memcpy( p_aout_buffer->p_buffer, p_block->p_buffer, p_block->i_buffer );
+ }
}