Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

dts.c

00001 /*****************************************************************************
00002  * dts.c : raw DTS stream input module for vlc
00003  *****************************************************************************
00004  * Copyright (C) 2001 the VideoLAN team
00005  * $Id: dts.c 11676 2005-07-10 08:13:48Z courmisch $
00006  *
00007  * Authors: Gildas Bazin <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 /*****************************************************************************
00025  * Preamble
00026  *****************************************************************************/
00027 #include <vlc/vlc.h>
00028 #include <vlc/input.h>
00029 #include <vlc_codec.h>
00030 
00031 /*****************************************************************************
00032  * Module descriptor
00033  *****************************************************************************/
00034 static int  Open  ( vlc_object_t * );
00035 static void Close ( vlc_object_t * );
00036 
00037 vlc_module_begin();
00038     set_category( CAT_INPUT );
00039     set_subcategory( SUBCAT_INPUT_DEMUX );
00040     set_description( _("Raw DTS demuxer") );
00041     set_capability( "demux2", 155 );
00042     set_callbacks( Open, Close );
00043     add_shortcut( "dts" );
00044 vlc_module_end();
00045 
00046 /*****************************************************************************
00047  * Local prototypes
00048  *****************************************************************************/
00049 static int Demux  ( demux_t * );
00050 static int Control( demux_t *, int, va_list );
00051 
00052 struct demux_sys_t
00053 {
00054     vlc_bool_t  b_start;
00055     es_out_id_t *p_es;
00056 
00057     /* Packetizer */
00058     decoder_t *p_packetizer;
00059 
00060     int i_mux_rate;
00061 };
00062 
00063 static int CheckSync( uint8_t *p_peek );
00064 
00065 #define DTS_PACKET_SIZE 16384
00066 #define DTS_PROBE_SIZE (DTS_PACKET_SIZE * 4)
00067 #define DTS_MAX_HEADER_SIZE 11
00068 
00069 /*****************************************************************************
00070  * Open: initializes ES structures
00071  *****************************************************************************/
00072 static int Open( vlc_object_t * p_this )
00073 {
00074     demux_t     *p_demux = (demux_t*)p_this;
00075     demux_sys_t *p_sys;
00076     byte_t *     p_peek;
00077     int          i_peek = 0;
00078 
00079     /* Check if we are dealing with a WAV file */
00080     if( stream_Peek( p_demux->s, &p_peek, 20 ) == 20 &&
00081         !memcmp( p_peek, "RIFF", 4 ) && !memcmp( &p_peek[8], "WAVE", 4 ) )
00082     {
00083         int i_size;
00084 
00085         /* Find the wave format header */
00086         i_peek = 20;
00087         while( memcmp( p_peek + i_peek - 8, "fmt ", 4 ) )
00088         {
00089             i_size = GetDWLE( p_peek + i_peek - 4 );
00090             if( i_size + i_peek > DTS_PROBE_SIZE ) return VLC_EGENERIC;
00091             i_peek += i_size + 8;
00092 
00093             if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
00094                 return VLC_EGENERIC;
00095         }
00096 
00097         /* Sanity check the wave format header */
00098         i_size = GetDWLE( p_peek + i_peek - 4 );
00099         if( i_size + i_peek > DTS_PROBE_SIZE ) return VLC_EGENERIC;
00100         i_peek += i_size + 8;
00101         if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
00102             return VLC_EGENERIC;
00103         if( GetWLE( p_peek + i_peek - i_size - 8 /* wFormatTag */ ) !=
00104             1 /* WAVE_FORMAT_PCM */ )
00105             return VLC_EGENERIC;
00106         if( GetWLE( p_peek + i_peek - i_size - 6 /* nChannels */ ) != 2 )
00107             return VLC_EGENERIC;
00108         if( GetDWLE( p_peek + i_peek - i_size - 4 /* nSamplesPerSec */ ) !=
00109             44100 )
00110             return VLC_EGENERIC;
00111 
00112         /* Skip the wave header */
00113         while( memcmp( p_peek + i_peek - 8, "data", 4 ) )
00114         {
00115             i_size = GetDWLE( p_peek + i_peek - 4 );
00116             if( i_size + i_peek > DTS_PROBE_SIZE ) return VLC_EGENERIC;
00117             i_peek += i_size + 8;
00118 
00119             if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
00120                 return VLC_EGENERIC;
00121         }
00122 
00123         /* Some DTS wav files don't begin with a sync code so we do a more
00124          * extensive search */
00125         i_size = stream_Peek( p_demux->s, &p_peek, DTS_PROBE_SIZE );
00126         i_size -= DTS_MAX_HEADER_SIZE;
00127 
00128         while( i_peek < i_size )
00129         {
00130             if( CheckSync( p_peek + i_peek ) != VLC_SUCCESS )
00131                 /* The data is stored in 16 bits words */
00132                 i_peek += 2;
00133             else
00134                 break;
00135         }
00136     }
00137 
00138     /* Have a peep at the show. */
00139     if( stream_Peek( p_demux->s, &p_peek, i_peek + DTS_MAX_HEADER_SIZE * 2 ) <
00140         i_peek + DTS_MAX_HEADER_SIZE * 2 )
00141     {
00142         /* Stream too short */
00143         msg_Warn( p_demux, "cannot peek()" );
00144         return VLC_EGENERIC;
00145     }
00146 
00147     if( CheckSync( p_peek + i_peek ) != VLC_SUCCESS )
00148     {
00149         if( strncmp( p_demux->psz_demux, "dts", 3 ) )
00150         {
00151             return VLC_EGENERIC;
00152         }
00153         /* User forced */
00154         msg_Err( p_demux, "this doesn't look like a DTS audio stream, "
00155                  "continuing anyway" );
00156     }
00157 
00158     p_demux->pf_demux = Demux;
00159     p_demux->pf_control = Control;
00160     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
00161     p_sys->b_start = VLC_TRUE;
00162     p_sys->i_mux_rate = 0;
00163 
00164     /*
00165      * Load the DTS packetizer
00166      */
00167     p_sys->p_packetizer = vlc_object_create( p_demux, VLC_OBJECT_DECODER );
00168     p_sys->p_packetizer->pf_decode_audio = 0;
00169     p_sys->p_packetizer->pf_decode_video = 0;
00170     p_sys->p_packetizer->pf_decode_sub = 0;
00171     p_sys->p_packetizer->pf_packetize = 0;
00172 
00173     /* Initialization of decoder structure */
00174     es_format_Init( &p_sys->p_packetizer->fmt_in, AUDIO_ES,
00175                     VLC_FOURCC( 'd', 't', 's', ' ' ) );
00176 
00177     p_sys->p_packetizer->p_module =
00178         module_Need( p_sys->p_packetizer, "packetizer", NULL, 0 );
00179     if( !p_sys->p_packetizer->p_module )
00180     {
00181         msg_Err( p_demux, "cannot find DTS packetizer" );
00182         return VLC_EGENERIC;
00183     }
00184 
00185     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_in );
00186 
00187     return VLC_SUCCESS;
00188 }
00189 
00190 /*****************************************************************************
00191  * Close: frees unused data
00192  *****************************************************************************/
00193 static void Close( vlc_object_t *p_this )
00194 {
00195     demux_t     *p_demux = (demux_t*)p_this;
00196     demux_sys_t *p_sys = p_demux->p_sys;
00197 
00198     /* Unneed module */
00199     module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
00200 
00201     /* Delete the decoder */
00202     vlc_object_destroy( p_sys->p_packetizer );
00203 
00204     free( p_sys );
00205 }
00206 
00207 /*****************************************************************************
00208  * Demux: reads and demuxes data packets
00209  *****************************************************************************
00210  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
00211  *****************************************************************************/
00212 static int Demux( demux_t *p_demux )
00213 {
00214     demux_sys_t *p_sys = p_demux->p_sys;
00215     block_t *p_block_in, *p_block_out;
00216 
00217     if( !( p_block_in = stream_Block( p_demux->s, DTS_PACKET_SIZE ) ) )
00218     {
00219         return 0;
00220     }
00221 
00222     if( p_sys->b_start )
00223         p_block_in->i_pts = p_block_in->i_dts = 1;
00224     else
00225         p_block_in->i_pts = p_block_in->i_dts = 0;
00226 
00227     while( (p_block_out = p_sys->p_packetizer->pf_packetize(
00228                 p_sys->p_packetizer, &p_block_in )) )
00229     {
00230         p_sys->b_start = VLC_FALSE;
00231 
00232         while( p_block_out )
00233         {
00234             block_t *p_next = p_block_out->p_next;
00235 
00236             /* We assume a constant bitrate */
00237             if( p_block_out->i_length )
00238             {
00239                 p_sys->i_mux_rate =
00240                     p_block_out->i_buffer * I64C(1000000) / p_block_out->i_length;
00241             }
00242 
00243             /* set PCR */
00244             es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
00245 
00246             es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
00247 
00248             p_block_out = p_next;
00249         }
00250     }
00251 
00252     return 1;
00253 }
00254 
00255 /*****************************************************************************
00256  * Control:
00257  *****************************************************************************/
00258 static int Control( demux_t *p_demux, int i_query, va_list args )
00259 {
00260     demux_sys_t *p_sys  = p_demux->p_sys;
00261     if( i_query == DEMUX_SET_TIME )
00262         return VLC_EGENERIC;
00263     else
00264         return demux2_vaControlHelper( p_demux->s,
00265                                        0, -1,
00266                                        8*p_sys->i_mux_rate, 1, i_query, args );
00267 }
00268 
00269 /*****************************************************************************
00270  * CheckSync: Check if buffer starts with a DTS sync code
00271  *****************************************************************************/
00272 static int CheckSync( uint8_t *p_peek )
00273 {
00274     /* 14 bits, little endian version of the bitstream */
00275     if( p_peek[0] == 0xff && p_peek[1] == 0x1f &&
00276         p_peek[2] == 0x00 && p_peek[3] == 0xe8 &&
00277         (p_peek[4] & 0xf0) == 0xf0 && p_peek[5] == 0x07 )
00278     {
00279         return VLC_SUCCESS;
00280     }
00281     /* 14 bits, big endian version of the bitstream */
00282     else if( p_peek[0] == 0x1f && p_peek[1] == 0xff &&
00283              p_peek[2] == 0xe8 && p_peek[3] == 0x00 &&
00284              p_peek[4] == 0x07 && (p_peek[5] & 0xf0) == 0xf0)
00285     {
00286         return VLC_SUCCESS;
00287     }
00288     /* 16 bits, big endian version of the bitstream */
00289     else if( p_peek[0] == 0x7f && p_peek[1] == 0xfe &&
00290              p_peek[2] == 0x80 && p_peek[3] == 0x01 )
00291     {
00292         return VLC_SUCCESS;
00293     }
00294     /* 16 bits, little endian version of the bitstream */
00295     else if( p_peek[0] == 0xfe && p_peek[1] == 0x7f &&
00296              p_peek[2] == 0x01 && p_peek[3] == 0x80 )
00297     {
00298         return VLC_SUCCESS;
00299     }
00300 
00301     return VLC_EGENERIC;
00302 }
00303 

Generated on Tue Dec 20 10:14:29 2005 for vlc-0.8.4a by  doxygen 1.4.2