]> git.sesse.net Git - vlc/blob - modules/demux/dts.c
* ALL: fixed DTS audio support.
[vlc] / modules / demux / dts.c
1 /*****************************************************************************
2  * dts.c : raw DTS stream input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: dts.c,v 1.1 2003/12/01 23:39:11 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/input.h>
29 #include <vlc_codec.h>
30
31 #define DTS_PACKET_SIZE 16384
32
33 /*****************************************************************************
34  * Local prototypes
35  *****************************************************************************/
36 static int  Open  ( vlc_object_t * );
37 static void Close ( vlc_object_t * );
38 static int  Demux ( input_thread_t * );
39
40 struct demux_sys_t
41 {
42     vlc_bool_t  b_start;
43     es_out_id_t *p_es;
44
45     /* Packetizer */
46     decoder_t *p_packetizer;
47 };
48
49 /*****************************************************************************
50  * Module descriptor
51  *****************************************************************************/
52 vlc_module_begin();
53     set_description( _("Raw DTS demuxer") );
54     set_capability( "demux", 155 );
55     set_callbacks( Open, Close );
56     add_shortcut( "dts" );
57 vlc_module_end();
58
59 /*****************************************************************************
60  * Open: initializes ES structures
61  *****************************************************************************/
62 static int Open( vlc_object_t * p_this )
63 {
64     input_thread_t *p_input = (input_thread_t *)p_this;
65     demux_sys_t    *p_sys;
66     byte_t *       p_peek;
67
68     p_input->pf_demux = Demux;
69     p_input->pf_demux_control = demux_vaControlDefault;
70     p_input->pf_rewind = NULL;
71
72     /* Have a peep at the show. */
73     if( input_Peek( p_input, &p_peek, 4 ) < 4 )
74     {
75         /* Stream shorter than 4 bytes... */
76         msg_Err( p_input, "cannot peek()" );
77         return VLC_EGENERIC;
78     }
79
80     if( p_peek[0] != 0x7f || p_peek[1] != 0xfe ||
81         p_peek[2] != 0x80 || p_peek[3] != 0x01 )
82     {
83         if( p_input->psz_demux && !strncmp( p_input->psz_demux, "dts", 3 ) )
84         {
85             /* User forced */
86             msg_Err( p_input, "this doesn't look like a DTS audio stream, "
87                      "continuing anyway" );
88         }
89         else
90         {
91             return VLC_EGENERIC;
92         }
93     }
94
95     p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
96     p_sys->b_start = VLC_TRUE;
97
98     /*
99      * Load the DTS packetizer
100      */
101     p_sys->p_packetizer = vlc_object_create( p_input, VLC_OBJECT_DECODER );
102     p_sys->p_packetizer->pf_decode_audio = 0;
103     p_sys->p_packetizer->pf_decode_video = 0;
104     p_sys->p_packetizer->pf_decode_sub = 0;
105     p_sys->p_packetizer->pf_packetize = 0;
106
107     /* Initialization of decoder structure */
108     es_format_Init( &p_sys->p_packetizer->fmt_in, AUDIO_ES,
109                     VLC_FOURCC( 'd', 't', 's', ' ' ) );
110
111     p_sys->p_packetizer->p_module =
112         module_Need( p_sys->p_packetizer, "packetizer", NULL );
113     if( !p_sys->p_packetizer->p_module )
114     {
115         msg_Err( p_input, "cannot find DTS packetizer" );
116         return VLC_EGENERIC;
117     }
118
119     /* Create one program */
120     vlc_mutex_lock( &p_input->stream.stream_lock );
121     if( input_InitStream( p_input, 0 ) == -1 )
122     {
123         vlc_mutex_unlock( &p_input->stream.stream_lock );
124         msg_Err( p_input, "cannot init stream" );
125         return VLC_EGENERIC;
126     }
127     p_input->stream.i_mux_rate = 0;
128     vlc_mutex_unlock( &p_input->stream.stream_lock );
129
130     p_sys->p_es =
131         es_out_Add( p_input->p_es_out, &p_sys->p_packetizer->fmt_in );
132
133     return VLC_SUCCESS;
134 }
135
136 /*****************************************************************************
137  * Close: frees unused data
138  *****************************************************************************/
139 static void Close( vlc_object_t * p_this )
140 {
141     input_thread_t *p_input = (input_thread_t*)p_this;
142     demux_sys_t    *p_sys = p_input->p_demux_data;
143
144     /* Unneed module */
145     module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
146
147     /* Delete the decoder */
148     vlc_object_destroy( p_sys->p_packetizer );
149
150     free( p_sys );
151 }
152
153 /*****************************************************************************
154  * Demux: reads and demuxes data packets
155  *****************************************************************************
156  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
157  *****************************************************************************/
158 static int Demux( input_thread_t * p_input )
159 {
160     demux_sys_t  *p_sys = p_input->p_demux_data;
161     block_t *p_block_in, *p_block_out;
162
163     if( !( p_block_in = stream_Block( p_input->s, DTS_PACKET_SIZE ) ) )
164     {
165         return 0;
166     }
167
168     if( p_sys->b_start )
169     {
170         p_block_in->i_pts = p_block_in->i_dts = 1;
171         p_sys->b_start = VLC_FALSE;
172     }
173     else
174     {
175         p_block_in->i_pts = p_block_in->i_dts = 0;
176     }
177
178     while( (p_block_out = p_sys->p_packetizer->pf_packetize(
179                 p_sys->p_packetizer, &p_block_in )) )
180     {
181         while( p_block_out )
182         {
183             block_t *p_next = p_block_out->p_next;
184
185             input_ClockManageRef( p_input,
186                                   p_input->stream.p_selected_program,
187                                   p_block_out->i_pts * 9 / 100 );
188
189             p_block_in->b_discontinuity = 0;
190             p_block_out->i_dts = p_block_out->i_pts =
191                 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
192                                   p_block_out->i_pts * 9 / 100 );
193
194             es_out_Send( p_input->p_es_out, p_sys->p_es, p_block_out );
195
196             p_block_out = p_next;
197         }
198     }
199
200     return 1;
201 }