* ac3_spdif.c: ac3 pass-through to external decoder with enabled soundcard
*****************************************************************************
* Copyright (C) 2001 VideoLAN
- * $Id: ac3_spdif.c,v 1.2 2001/11/14 03:38:11 stef Exp $
+ * $Id: ac3_spdif.c,v 1.32 2002/07/31 20:56:50 sam Exp $
*
* Authors: Stéphane Borel <stef@via.ecp.fr>
* Juha Yrjola <jyrjola@cc.hut.fi>
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/
-#define MODULE_NAME ac3_spdif
-#include "modules_inner.h"
-
/*****************************************************************************
* Preamble
*****************************************************************************/
-#include "defs.h"
-
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* memcpy() */
#include <fcntl.h>
+#include <vlc/vlc.h>
+#include <vlc/decoder.h>
+#include <vlc/aout.h>
+
#ifdef HAVE_UNISTD_H
-#include <unistd.h>
+# include <unistd.h>
#endif
-#include "config.h"
-#include "common.h"
-#include "threads.h"
-#include "mtime.h"
-#include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
-
-#include "audio_output.h"
-
-#include "modules.h"
-#include "modules_export.h"
+#include "ac3_spdif.h"
-#include "stream_control.h"
-#include "input_ext-dec.h"
+/****************************************************************************
+ * Local prototypes
+ ****************************************************************************/
+static int OpenDecoder ( vlc_object_t * );
+static int RunDecoder ( decoder_fifo_t * );
-#include "ac3_spdif.h"
-#include "ac3_iec958.h"
+static int InitThread ( ac3_spdif_thread_t * );
+static void EndThread ( ac3_spdif_thread_t * );
+static void BitstreamCallback ( bit_stream_t *, vlc_bool_t );
-#define FRAME_NB 8
+int ac3_parse_syncinfo ( struct ac3_spdif_thread_s * );
/****************************************************************************
- * Local Prototypes
+ * Local structures and tables
****************************************************************************/
-static int ac3_spdif_Probe ( probedata_t * );
-static int ac3_spdif_Run ( decoder_config_t * );
-static int ac3_spdif_Init ( ac3_spdif_thread_t * );
-static void ac3_spdif_ErrorThread ( ac3_spdif_thread_t * );
-static void ac3_spdif_EndThread ( ac3_spdif_thread_t * );
-static void BitstreamCallback( bit_stream_t *, boolean_t );
-
-/*****************************************************************************
- * Capabilities
- *****************************************************************************/
-void _M( adec_getfunctions )( function_list_t * p_function_list )
+static const frame_size_t p_frame_size_code[64] =
{
- p_function_list->pf_probe = ac3_spdif_Probe;
- p_function_list->functions.dec.pf_RunThread = ac3_spdif_Run;
-}
+ { 32 ,{64 ,69 ,96 } },
+ { 32 ,{64 ,70 ,96 } },
+ { 40 ,{80 ,87 ,120 } },
+ { 40 ,{80 ,88 ,120 } },
+ { 48 ,{96 ,104 ,144 } },
+ { 48 ,{96 ,105 ,144 } },
+ { 56 ,{112 ,121 ,168 } },
+ { 56 ,{112 ,122 ,168 } },
+ { 64 ,{128 ,139 ,192 } },
+ { 64 ,{128 ,140 ,192 } },
+ { 80 ,{160 ,174 ,240 } },
+ { 80 ,{160 ,175 ,240 } },
+ { 96 ,{192 ,208 ,288 } },
+ { 96 ,{192 ,209 ,288 } },
+ { 112 ,{224 ,243 ,336 } },
+ { 112 ,{224 ,244 ,336 } },
+ { 128 ,{256 ,278 ,384 } },
+ { 128 ,{256 ,279 ,384 } },
+ { 160 ,{320 ,348 ,480 } },
+ { 160 ,{320 ,349 ,480 } },
+ { 192 ,{384 ,417 ,576 } },
+ { 192 ,{384 ,418 ,576 } },
+ { 224 ,{448 ,487 ,672 } },
+ { 224 ,{448 ,488 ,672 } },
+ { 256 ,{512 ,557 ,768 } },
+ { 256 ,{512 ,558 ,768 } },
+ { 320 ,{640 ,696 ,960 } },
+ { 320 ,{640 ,697 ,960 } },
+ { 384 ,{768 ,835 ,1152 } },
+ { 384 ,{768 ,836 ,1152 } },
+ { 448 ,{896 ,975 ,1344 } },
+ { 448 ,{896 ,976 ,1344 } },
+ { 512 ,{1024 ,1114 ,1536 } },
+ { 512 ,{1024 ,1115 ,1536 } },
+ { 576 ,{1152 ,1253 ,1728 } },
+ { 576 ,{1152 ,1254 ,1728 } },
+ { 640 ,{1280 ,1393 ,1920 } },
+ { 640 ,{1280 ,1394 ,1920 } }
+};
/*****************************************************************************
- * Build configuration tree.
+ * Module descriptor
*****************************************************************************/
-MODULE_CONFIG_START
-ADD_WINDOW( "Configuration for ac3 spdif decoder module" )
- ADD_COMMENT( "Nothing to configure" )
-MODULE_CONFIG_STOP
-
-MODULE_INIT_START
- p_module->i_capabilities = MODULE_CAPABILITY_DEC;
- p_module->psz_longname = "Ac3 SPDIF decoder for AC3 pass-through";
-MODULE_INIT_STOP
-
-MODULE_ACTIVATE_START
- _M( adec_getfunctions )( &p_module->p_functions->dec );
-MODULE_ACTIVATE_STOP
-
-MODULE_DEACTIVATE_START
-MODULE_DEACTIVATE_STOP
+vlc_module_begin();
+ set_description( _("SPDIF pass-through AC3 decoder") );
+ set_capability( "decoder", 0 );
+ set_callbacks( OpenDecoder, NULL );
+ add_shortcut( "pass_through" );
+ add_shortcut( "pass" );
+vlc_module_end();
/*****************************************************************************
- * ac3_spdif_Probe: probe the decoder and return score
+ * OpenDecoder: probe the decoder and return score
*****************************************************************************
* Tries to launch a decoder and return score so that the interface is able
* to chose.
*****************************************************************************/
-static int ac3_spdif_Probe( probedata_t *p_data )
-{
- if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) &&
- p_data->i_type == AC3_AUDIO_ES )
- return( 100 );
- else
- return( 0 );
-}
+static int OpenDecoder( vlc_object_t *p_this )
+{
+ decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
+
+ if( p_fifo->i_fourcc != VLC_FOURCC('a','5','2',' ') )
+ {
+ return VLC_EGENERIC;
+ }
+ p_fifo->pf_run = RunDecoder;
+ return VLC_SUCCESS;
+}
/****************************************************************************
- * ac3_spdif_Run: the whole thing
+ * RunDecoder: the whole thing
****************************************************************************
* This function is called just after the thread is launched.
****************************************************************************/
-static int ac3_spdif_Run( decoder_config_t * p_config )
+static int RunDecoder( decoder_fifo_t *p_fifo )
{
ac3_spdif_thread_t * p_spdif;
mtime_t i_frame_time;
- boolean_t b_sync;
+ vlc_bool_t b_sync;
/* PTS of the current frame */
mtime_t i_current_pts = 0;
-
- intf_DbgMsg( "spdif debug: ac3_spdif thread created, initializing." );
-
+ u16 i_length;
+
/* Allocate the memory needed to store the thread's structure */
p_spdif = malloc( sizeof(ac3_spdif_thread_t) );
if( p_spdif == NULL )
{
- intf_ErrMsg ( "spdif error: not enough memory "
- "for spdif_CreateThread() to create the new thread");
+ msg_Err( p_fifo, "out of memory" );
+ DecoderError( p_fifo );
return( -1 );
}
- p_spdif->p_config = p_config;
+ p_spdif->p_fifo = p_fifo;
- if (ac3_spdif_Init( p_spdif ) )
+ if (InitThread( p_spdif ) )
{
- intf_ErrMsg( "spdif error: could not initialize thread" );
+
+ if( p_fifo->b_error )
+ {
+ msg_Err( p_fifo, "could not initialize thread" );
+ }
+
+ DecoderError( p_fifo );
+ free( p_spdif );
return( -1 );
}
/* Compute the theorical duration of an ac3 frame */
i_frame_time = 1000000 * AC3_FRAME_SIZE /
p_spdif->ac3_info.i_sample_rate;
+ i_length = p_spdif->ac3_info.i_frame_size;
- intf_DbgMsg( "spdif debug: ac3_spdif thread (%p) initialized", p_spdif );
-
while( !p_spdif->p_fifo->b_die && !p_spdif->p_fifo->b_error )
{
- /* Handle the dates */
+ p_spdif->p_ac3[0] = 0x0b;
+ p_spdif->p_ac3[1] = 0x77;
+
+ /* Handle the dates */
if( p_spdif->i_real_pts )
{
mtime_t i_delta = p_spdif->i_real_pts - i_current_pts -
i_frame_time;
if( i_delta > i_frame_time || i_delta < -i_frame_time )
{
- intf_WarnMsg( 3, "spdif warning: date discontinuity (%d)",
- i_delta );
+ msg_Warn( p_fifo,
+ "date discontinuity (%d)", i_delta );
}
i_current_pts = p_spdif->i_real_pts;
p_spdif->i_real_pts = 0;
i_current_pts += i_frame_time;
}
- /* if we're late here the output won't have to play the frame */
- if( i_current_pts > mdate() )
- {
- p_spdif->p_aout_fifo->date[p_spdif->p_aout_fifo->l_end_frame] =
- i_current_pts;
-
- /* Write in the first free packet of aout fifo */
- p_spdif->p_iec = ((u8*)(p_spdif->p_aout_fifo->buffer) +
- (p_spdif->p_aout_fifo->l_end_frame * SPDIF_FRAME_SIZE ));
-
- /* Build burst to be sent to hardware decoder */
- ac3_iec958_build_burst( p_spdif );
-
- vlc_mutex_lock (&p_spdif->p_aout_fifo->data_lock);
- p_spdif->p_aout_fifo->l_end_frame =
- (p_spdif->p_aout_fifo->l_end_frame + 1 ) & AOUT_FIFO_SIZE;
- vlc_mutex_unlock (&p_spdif->p_aout_fifo->data_lock);
- }
+ // wait a little to avoid an input flood from the a52 input
+ mwait( i_current_pts - 500000 );
+
+ vlc_mutex_lock (&p_spdif->p_aout_fifo->data_lock);
+
+ p_spdif->p_aout_fifo->date[p_spdif->p_aout_fifo->i_end_frame] =
+ i_current_pts;
+
+ p_spdif->p_aout_fifo->i_end_frame =
+ (p_spdif->p_aout_fifo->i_end_frame + 1 ) & AOUT_FIFO_SIZE;
+
+ p_spdif->p_ac3 = ((u8*)(p_spdif->p_aout_fifo->buffer)) +
+ (p_spdif->p_aout_fifo->i_end_frame * i_length );
+
+ vlc_mutex_unlock (&p_spdif->p_aout_fifo->data_lock);
/* Find syncword again in case of stream discontinuity */
/* Here we have p_spdif->i_pts == 0
* Therefore a non-zero value after a call to GetBits() means the PES
* has changed. */
b_sync = 0;
- while( !b_sync )
+ while( !p_spdif->p_fifo->b_die
+ && !p_spdif->p_fifo->b_error
+ && !b_sync )
{
- while( GetBits( &p_spdif->bit_stream, 8 ) != 0x0b );
+ while( !p_spdif->p_fifo->b_die
+ && !p_spdif->p_fifo->b_error
+ && GetBits( &p_spdif->bit_stream, 8 ) != 0x0b );
p_spdif->i_real_pts = p_spdif->i_pts;
p_spdif->i_pts = 0;
b_sync = ( ShowBits( &p_spdif->bit_stream, 8 ) == 0x77 );
RemoveBits( &p_spdif->bit_stream, 8 );
/* Read data from bitstream */
- GetChunk( &p_spdif->bit_stream, p_spdif->p_ac3 + 2,
- p_spdif->ac3_info.i_frame_size - 2 );
+ GetChunk( &p_spdif->bit_stream, p_spdif->p_ac3 + 2, i_length - 2 );
}
/* If b_error is set, the ac3 spdif thread enters the error loop */
if( p_spdif->p_fifo->b_error )
{
- ac3_spdif_ErrorThread( p_spdif );
+ DecoderError( p_spdif->p_fifo );
}
/* End of the ac3 decoder thread */
- ac3_spdif_EndThread( p_spdif );
+ EndThread( p_spdif );
return( 0 );
}
/****************************************************************************
- * ac3_spdif_Init: initialize thread data and create output fifo
+ * InitThread: initialize thread data and create output fifo
****************************************************************************/
-static int ac3_spdif_Init( ac3_spdif_thread_t * p_spdif )
+static int InitThread( ac3_spdif_thread_t * p_spdif )
{
- boolean_t b_sync = 0;
+ vlc_bool_t b_sync = 0;
- /* Temporary buffer to store ac3 frames to be transformed */
+ /* Temporary buffer to store first ac3 frame */
p_spdif->p_ac3 = malloc( SPDIF_FRAME_SIZE );
if( p_spdif->p_ac3 == NULL )
/*
* Initialize the thread properties
*/
- p_spdif->p_fifo = p_spdif->p_config->p_decoder_fifo;
-
- p_spdif->p_config->pf_init_bit_stream(
- &p_spdif->bit_stream,
- p_spdif->p_config->p_decoder_fifo,
- BitstreamCallback, (void*)p_spdif );
-
- /* Creating the audio output fifo */
- p_spdif->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_SPDIF_FIFO, 1, 48000, 0,
- SPDIF_FRAME_SIZE, NULL );
-
- if( p_spdif->p_aout_fifo == NULL )
- {
- return( -1 );
- }
-
- intf_WarnMsg( 3, "spdif: aout fifo #%d created",
- p_spdif->p_aout_fifo->i_fifo );
+ p_spdif->p_fifo = p_spdif->p_fifo;
- /* Sync word */
- p_spdif->p_ac3[0] = 0x0b;
- p_spdif->p_ac3[1] = 0x77;
+ InitBitstream( &p_spdif->bit_stream, p_spdif->p_fifo,
+ BitstreamCallback, (void*)p_spdif );
/* Find syncword */
- while( !b_sync )
+ while( !p_spdif->p_fifo->b_die
+ && !p_spdif->p_fifo->b_error
+ && !b_sync )
{
- while( GetBits( &p_spdif->bit_stream, 8 ) != 0x0b );
+ while( !p_spdif->p_fifo->b_die
+ && !p_spdif->p_fifo->b_error
+ && GetBits( &p_spdif->bit_stream, 8 ) != 0x0b );
p_spdif->i_real_pts = p_spdif->i_pts;
p_spdif->i_pts = 0;
b_sync = ( ShowBits( &p_spdif->bit_stream, 8 ) == 0x77 );
}
+
+ if( p_spdif->p_fifo->b_die || p_spdif->p_fifo->b_error )
+ {
+ return -1;
+ }
+
RemoveBits( &p_spdif->bit_stream, 8 );
/* Check stream properties */
- if( ac3_iec958_parse_syncinfo( p_spdif ) < 0 )
+ if( ac3_parse_syncinfo( p_spdif ) < 0 )
{
- intf_ErrMsg( "spdif error: stream not valid");
+ msg_Err( p_spdif->p_fifo, "stream not valid" );
- aout_DestroyFifo( p_spdif->p_aout_fifo );
return( -1 );
}
* but all rates should be supported by the decoder (32, 44.1, 48) */
if( p_spdif->ac3_info.i_sample_rate != 48000 )
{
- intf_ErrMsg( "spdif error: Only 48000 Hz streams supported");
-
- aout_DestroyFifo( p_spdif->p_aout_fifo );
- return -1;
+ msg_Err( p_spdif->p_fifo,
+ "only 48000 Hz streams tested, expect weird things!" );
}
- p_spdif->p_aout_fifo->l_rate = p_spdif->ac3_info.i_sample_rate;
-
- GetChunk( &p_spdif->bit_stream, p_spdif->p_ac3 + sizeof(sync_frame_t),
- p_spdif->ac3_info.i_frame_size - sizeof(sync_frame_t) );
- return( 0 );
-}
+ /* The audio output need to be ready for an ac3 stream */
+ p_spdif->i_previous_format = config_GetInt( p_spdif->p_fifo,
+ "audio-format" );
+ config_PutInt( p_spdif->p_fifo, "audio-format", 8 );
+
+ /* Creating the audio output fifo */
+ p_spdif->p_aout_fifo =
+ aout_CreateFifo( p_spdif->p_fifo, AOUT_FIFO_SPDIF,
+ 1, p_spdif->ac3_info.i_sample_rate,
+ p_spdif->ac3_info.i_frame_size, NULL );
+ if( p_spdif->p_aout_fifo == NULL )
+ {
+ return( -1 );
+ }
-/*****************************************************************************
- * ac3_spdif_ErrorThread : ac3 spdif's RunThread() error loop
- *****************************************************************************/
-static void ac3_spdif_ErrorThread( ac3_spdif_thread_t * p_spdif )
-{
- /* We take the lock, because we are going to read/write the start/end
- * indexes of the decoder fifo */
- vlc_mutex_lock (&p_spdif->p_fifo->data_lock);
+ msg_Dbg( p_spdif->p_fifo, "aout fifo #%d created",
+ p_spdif->p_aout_fifo->i_fifo );
- /* Wait until a `die' order is sent */
- while( !p_spdif->p_fifo->b_die )
- {
- /* Trash all received PES packets */
- while( !DECODER_FIFO_ISEMPTY( *p_spdif->p_fifo ) )
- {
- p_spdif->p_fifo->pf_delete_pes(p_spdif->p_fifo->p_packets_mgt,
- DECODER_FIFO_START( *p_spdif->p_fifo ) );
- DECODER_FIFO_INCSTART( *p_spdif->p_fifo );
- }
+ /* Put read data into fifo */
+ memcpy( (u8*)(p_spdif->p_aout_fifo->buffer) +
+ (p_spdif->p_aout_fifo->i_end_frame *
+ p_spdif->ac3_info.i_frame_size ),
+ p_spdif->p_ac3, sizeof(sync_frame_t) );
+ free( p_spdif->p_ac3 );
+ p_spdif->p_ac3 = ((u8*)(p_spdif->p_aout_fifo->buffer) +
+ (p_spdif->p_aout_fifo->i_end_frame *
+ p_spdif->ac3_info.i_frame_size ));
- /* Waiting for the input thread to put new PES packets in the fifo */
- vlc_cond_wait( &p_spdif->p_fifo->data_wait,
- &p_spdif->p_fifo->data_lock );
- }
+ GetChunk( &p_spdif->bit_stream, p_spdif->p_ac3 + sizeof(sync_frame_t),
+ p_spdif->ac3_info.i_frame_size - sizeof(sync_frame_t) );
- /* We can release the lock before leaving */
- vlc_mutex_unlock( &p_spdif->p_fifo->data_lock );
+ return( 0 );
}
/*****************************************************************************
- * ac3_spdif_EndThread : ac3 spdif thread destruction
+ * EndThread : ac3 spdif thread destruction
*****************************************************************************/
-static void ac3_spdif_EndThread( ac3_spdif_thread_t * p_spdif )
+static void EndThread( ac3_spdif_thread_t * p_spdif )
{
- intf_DbgMsg( "spdif debug: destroying thread %p", p_spdif );
-
/* If the audio output fifo was created, we destroy it */
if( p_spdif->p_aout_fifo != NULL )
{
}
+ /* restore previous setting for output format */
+ config_PutInt( p_spdif->p_fifo, "audio-format",
+ p_spdif->i_previous_format );
+
/* Destroy descriptor */
- free( p_spdif->p_config );
- free( p_spdif->p_ac3 );
free( p_spdif );
-
- intf_DbgMsg ("spdif debug: thread %p destroyed", p_spdif );
}
/*****************************************************************************
*****************************************************************************
* This function is called by input's NextDataPacket.
*****************************************************************************/
-static void BitstreamCallback ( bit_stream_t * p_bit_stream,
- boolean_t b_new_pes)
+static void BitstreamCallback( bit_stream_t * p_bit_stream,
+ vlc_bool_t b_new_pes )
{
ac3_spdif_thread_t * p_spdif;
{
p_spdif = (ac3_spdif_thread_t *)p_bit_stream->p_callback_arg;
- p_bit_stream->p_byte += 3;
+/* p_bit_stream->p_byte += 3; */
p_spdif->i_pts =
- DECODER_FIFO_START( *p_bit_stream->p_decoder_fifo )->i_pts;
- DECODER_FIFO_START( *p_bit_stream->p_decoder_fifo )->i_pts = 0;
+ p_bit_stream->p_decoder_fifo->p_first->i_pts;
+ p_bit_stream->p_decoder_fifo->p_first->i_pts = 0;
+ }
+}
+
+/****************************************************************************
+ * ac3_parse_syncinfo: parse ac3 sync info
+ ****************************************************************************/
+int ac3_parse_syncinfo( ac3_spdif_thread_t *p_spdif )
+{
+ int p_sample_rates[4] = { 48000, 44100, 32000, -1 };
+ int i_frame_rate_code;
+ int i_frame_size_code;
+ sync_frame_t * p_sync_frame;
+
+ /* Read sync frame */
+ GetChunk( &p_spdif->bit_stream, p_spdif->p_ac3 + 2,
+ sizeof(sync_frame_t) - 2 );
+ if( p_spdif->p_fifo->b_die ) return -1;
+
+ p_sync_frame = (sync_frame_t*)p_spdif->p_ac3;
+
+ /* Compute frame rate */
+ i_frame_rate_code = (p_sync_frame->syncinfo.code >> 6) & 0x03;
+ p_spdif->ac3_info.i_sample_rate = p_sample_rates[i_frame_rate_code];
+ if( p_spdif->ac3_info.i_sample_rate == -1 )
+ {
+ return -1;
+ }
+
+ /* Compute frame size */
+ i_frame_size_code = p_sync_frame->syncinfo.code & 0x3f;
+ p_spdif->ac3_info.i_frame_size = 2 *
+ p_frame_size_code[i_frame_size_code].i_frame_size[i_frame_rate_code];
+ p_spdif->ac3_info.i_bit_rate =
+ p_frame_size_code[i_frame_size_code].i_bit_rate;
+
+ if( ( ( p_sync_frame->bsi.bsidmod >> 3 ) & 0x1f ) != 0x08 )
+ {
+ return -1;
}
+
+ p_spdif->ac3_info.i_bs_mod = p_sync_frame->bsi.bsidmod & 0x7;
+
+ return 0;
}