1 /*****************************************************************************
2 * dts.c : raw DTS stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: dts.c,v 1.2 2004/01/21 17:56:05 gbazin Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <vlc/input.h>
29 #include <vlc_codec.h>
31 #define DTS_PACKET_SIZE 16384
32 #define DTS_MAX_HEADER_SIZE 11
34 /*****************************************************************************
36 *****************************************************************************/
37 static int Open ( vlc_object_t * );
38 static void Close ( vlc_object_t * );
39 static int Demux ( input_thread_t * );
47 decoder_t *p_packetizer;
50 /*****************************************************************************
52 *****************************************************************************/
54 set_description( _("Raw DTS demuxer") );
55 set_capability( "demux", 155 );
56 set_callbacks( Open, Close );
57 add_shortcut( "dts" );
60 /*****************************************************************************
61 * Open: initializes ES structures
62 *****************************************************************************/
63 static int Open( vlc_object_t * p_this )
65 input_thread_t *p_input = (input_thread_t *)p_this;
70 p_input->pf_demux = Demux;
71 p_input->pf_demux_control = demux_vaControlDefault;
72 p_input->pf_rewind = NULL;
74 /* Check if we are dealing with a WAV file */
75 if( input_Peek( p_input, &p_peek, 12 ) == 12 &&
76 !strncmp( p_peek, "RIFF", 4 ) && !strncmp( &p_peek[8], "WAVE", 4 ) )
78 /* Skip the wave header */
80 while( input_Peek( p_input, &p_peek, i_peek ) == i_peek &&
81 strncmp( p_peek + i_peek - 8, "data", 4 ) )
83 i_peek += GetDWLE( p_peek + i_peek - 4 ) + 8;
87 /* Have a peep at the show. */
88 if( input_Peek( p_input, &p_peek, i_peek + DTS_MAX_HEADER_SIZE ) <
89 i_peek + DTS_MAX_HEADER_SIZE )
91 /* Stream too short */
92 msg_Err( p_input, "cannot peek()" );
96 /* 14 bits, little endian version of the bitstream */
97 if( p_peek[i_peek + 0] == 0xff && p_peek[i_peek + 1] == 0x1f &&
98 p_peek[i_peek + 2] == 0x00 && p_peek[i_peek + 3] == 0xe8 &&
99 (p_peek[i_peek + 4] & 0xf0) == 0xf0 && p_peek[i_peek + 5] == 0x07 )
102 /* 16 bits, big endian version of the bitstream */
103 else if( p_peek[i_peek + 0] == 0x7f && p_peek[i_peek + 1] == 0xfe &&
104 p_peek[i_peek + 2] == 0x80 && p_peek[i_peek + 3] == 0x01 )
109 if( p_input->psz_demux && !strncmp( p_input->psz_demux, "dts", 3 ) )
112 msg_Err( p_input, "this doesn't look like a DTS audio stream, "
113 "continuing anyway" );
121 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
122 p_sys->b_start = VLC_TRUE;
125 * Load the DTS packetizer
127 p_sys->p_packetizer = vlc_object_create( p_input, VLC_OBJECT_DECODER );
128 p_sys->p_packetizer->pf_decode_audio = 0;
129 p_sys->p_packetizer->pf_decode_video = 0;
130 p_sys->p_packetizer->pf_decode_sub = 0;
131 p_sys->p_packetizer->pf_packetize = 0;
133 /* Initialization of decoder structure */
134 es_format_Init( &p_sys->p_packetizer->fmt_in, AUDIO_ES,
135 VLC_FOURCC( 'd', 't', 's', ' ' ) );
137 p_sys->p_packetizer->p_module =
138 module_Need( p_sys->p_packetizer, "packetizer", NULL );
139 if( !p_sys->p_packetizer->p_module )
141 msg_Err( p_input, "cannot find DTS packetizer" );
145 /* Create one program */
146 vlc_mutex_lock( &p_input->stream.stream_lock );
147 if( input_InitStream( p_input, 0 ) == -1 )
149 vlc_mutex_unlock( &p_input->stream.stream_lock );
150 msg_Err( p_input, "cannot init stream" );
153 p_input->stream.i_mux_rate = 0;
154 vlc_mutex_unlock( &p_input->stream.stream_lock );
157 es_out_Add( p_input->p_es_out, &p_sys->p_packetizer->fmt_in );
162 /*****************************************************************************
163 * Close: frees unused data
164 *****************************************************************************/
165 static void Close( vlc_object_t * p_this )
167 input_thread_t *p_input = (input_thread_t*)p_this;
168 demux_sys_t *p_sys = p_input->p_demux_data;
171 module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
173 /* Delete the decoder */
174 vlc_object_destroy( p_sys->p_packetizer );
179 /*****************************************************************************
180 * Demux: reads and demuxes data packets
181 *****************************************************************************
182 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
183 *****************************************************************************/
184 static int Demux( input_thread_t * p_input )
186 demux_sys_t *p_sys = p_input->p_demux_data;
187 block_t *p_block_in, *p_block_out;
189 if( !( p_block_in = stream_Block( p_input->s, DTS_PACKET_SIZE ) ) )
196 p_block_in->i_pts = p_block_in->i_dts = 1;
197 p_sys->b_start = VLC_FALSE;
201 p_block_in->i_pts = p_block_in->i_dts = 0;
204 while( (p_block_out = p_sys->p_packetizer->pf_packetize(
205 p_sys->p_packetizer, &p_block_in )) )
209 block_t *p_next = p_block_out->p_next;
211 input_ClockManageRef( p_input,
212 p_input->stream.p_selected_program,
213 p_block_out->i_pts * 9 / 100 );
215 p_block_in->b_discontinuity = 0;
216 p_block_out->i_dts = p_block_out->i_pts =
217 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
218 p_block_out->i_pts * 9 / 100 );
220 es_out_Send( p_input->p_es_out, p_sys->p_es, p_block_out );
222 p_block_out = p_next;