]> git.sesse.net Git - vlc/blob - modules/demux/a52.c
* a52.c aac.c au.c dts.c flac.c wav.c: Converted all audio only demuxers
[vlc] / modules / demux / a52.c
1 /*****************************************************************************
2  * a52.c : raw A/52 stream input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: a52.c,v 1.7 2004/03/03 11:40:19 fenrir 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 #ifdef HAVE_UNISTD_H
32 #   include <unistd.h>
33 #endif
34
35 /*****************************************************************************
36  * Module descriptor
37  *****************************************************************************/
38 static int  Open  ( vlc_object_t * );
39 static void Close ( vlc_object_t * );
40
41 vlc_module_begin();
42     set_description( _("Raw A/52 demuxer") );
43     set_capability( "demux2", 145 );
44     set_callbacks( Open, Close );
45     add_shortcut( "a52" );
46 vlc_module_end();
47
48 /*****************************************************************************
49  * Local prototypes
50  *****************************************************************************/
51 static int Demux  ( demux_t * );
52 static int Control( demux_t *, int, va_list );
53
54 struct demux_sys_t
55 {
56     vlc_bool_t  b_start;
57     es_out_id_t *p_es;
58
59     /* Packetizer */
60     decoder_t *p_packetizer;
61
62     int i_mux_rate;
63     vlc_bool_t b_big_endian;
64 };
65
66 static int CheckSync( uint8_t *p_peek, vlc_bool_t *p_big_endian );
67
68 #define PCM_FRAME_SIZE (1536 * 4)
69 #define A52_PACKET_SIZE (4 * PCM_FRAME_SIZE)
70 #define A52_MAX_HEADER_SIZE 10
71
72
73 /*****************************************************************************
74  * Open: initializes ES structures
75  *****************************************************************************/
76 static int Open( vlc_object_t * p_this )
77 {
78     demux_t     *p_demux = (demux_t*)p_this;
79     demux_sys_t *p_sys;
80     byte_t      *p_peek;
81     int         i_peek = 0;
82     vlc_bool_t  b_big_endian;
83
84     /* Check if we are dealing with a WAV file */
85     if( stream_Peek( p_demux->s, &p_peek, 12 ) == 12 &&
86         !strncmp( p_peek, "RIFF", 4 ) && !strncmp( &p_peek[8], "WAVE", 4 ) )
87     {
88         int i_size;
89
90         /* Skip the wave header */
91         i_peek = 12 + 8;
92         while( stream_Peek( p_demux->s, &p_peek, i_peek ) == i_peek &&
93                strncmp( p_peek + i_peek - 8, "data", 4 ) )
94         {
95             i_peek += GetDWLE( p_peek + i_peek - 4 ) + 8;
96         }
97
98         /* TODO: should check wave format and sample_rate */
99
100         /* Some A52 wav files don't begin with a sync code so we do a more
101          * extensive search */
102         i_size = stream_Peek( p_demux->s, &p_peek, i_peek + A52_PACKET_SIZE * 2);
103         i_size -= (PCM_FRAME_SIZE + A52_MAX_HEADER_SIZE);
104
105         while( i_peek < i_size )
106         {
107             if( CheckSync( p_peek + i_peek, &b_big_endian ) != VLC_SUCCESS )
108                 /* The data is stored in 16 bits words */
109                 i_peek += 2;
110             else
111             {
112                 /* Check following sync code */
113                 if( CheckSync( p_peek + i_peek + PCM_FRAME_SIZE,
114                                &b_big_endian ) != VLC_SUCCESS )
115                 {
116                     i_peek += 2;
117                     continue;
118                 }
119
120                 break;
121             }
122         }
123     }
124
125     /* Have a peep at the show. */
126     if( stream_Peek( p_demux->s, &p_peek, i_peek + A52_MAX_HEADER_SIZE * 2 ) <
127         i_peek + A52_MAX_HEADER_SIZE * 2 )
128     {
129         /* Stream too short */
130         msg_Warn( p_demux, "cannot peek()" );
131         return VLC_EGENERIC;
132     }
133
134     if( CheckSync( p_peek + i_peek, &b_big_endian ) != VLC_SUCCESS )
135     {
136         if( strncmp( p_demux->psz_demux, "a52", 3 ) )
137         {
138             return VLC_EGENERIC;
139         }
140
141         /* User forced */
142         msg_Err( p_demux, "this doesn't look like a A52 audio stream, "
143                  "continuing anyway" );
144     }
145
146     /* Fill p_demux fields */
147     p_demux->pf_demux = Demux;
148     p_demux->pf_control = Control;
149     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
150     p_sys->b_start = VLC_TRUE;
151     p_sys->i_mux_rate = 0;
152     p_sys->b_big_endian = b_big_endian;
153
154     /*
155      * Load the A52 packetizer
156      */
157     p_sys->p_packetizer = vlc_object_create( p_demux, VLC_OBJECT_DECODER );
158     p_sys->p_packetizer->pf_decode_audio = 0;
159     p_sys->p_packetizer->pf_decode_video = 0;
160     p_sys->p_packetizer->pf_decode_sub = 0;
161     p_sys->p_packetizer->pf_packetize = 0;
162
163     /* Initialization of decoder structure */
164     es_format_Init( &p_sys->p_packetizer->fmt_in, AUDIO_ES,
165                     VLC_FOURCC( 'a', '5', '2', ' ' ) );
166
167     p_sys->p_packetizer->p_module =
168         module_Need( p_sys->p_packetizer, "packetizer", NULL );
169     if( !p_sys->p_packetizer->p_module )
170     {
171         msg_Err( p_demux, "cannot find A52 packetizer" );
172         return VLC_EGENERIC;
173     }
174
175     /* Create one program */
176     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_in );
177
178     return VLC_SUCCESS;
179 }
180
181 /*****************************************************************************
182  * Close: frees unused data
183  *****************************************************************************/
184 static void Close( vlc_object_t * p_this )
185 {
186     demux_t        *p_demux = (demux_t*)p_this;
187     demux_sys_t    *p_sys = p_demux->p_sys;
188
189     /* Unneed module */
190     module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
191
192     /* Delete the decoder */
193     vlc_object_destroy( p_sys->p_packetizer );
194
195     free( p_sys );
196 }
197
198 /*****************************************************************************
199  * Demux: reads and demuxes data packets
200  *****************************************************************************
201  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
202  *****************************************************************************/
203 static int Demux( demux_t *p_demux )
204 {
205     demux_sys_t *p_sys = p_demux->p_sys;
206     block_t     *p_block_in, *p_block_out;
207
208      /* Align stream */
209     int64_t i_pos = stream_Tell( p_demux->s );
210     if( i_pos % 2 ) stream_Read( p_demux->s, NULL, 1 );
211
212     if( !( p_block_in = stream_Block( p_demux->s, A52_PACKET_SIZE ) ) )
213     {
214         return 0;
215     }
216
217     if( !p_sys->b_big_endian && p_block_in->i_buffer )
218     {
219         /* Convert to big endian */
220
221 #ifdef HAVE_SWAB
222         swab(p_block_in->p_buffer, p_block_in->p_buffer, p_block_in->i_buffer);
223
224 #else
225         int i;
226         byte_t *p_tmp, tmp;
227         p_tmp = p_block_in->p_buffer;
228         for( i = p_block_in->i_buffer / 2 ; i-- ; )
229         {
230             tmp = p_tmp[0];
231             p_tmp[0] = p_tmp[1];
232             p_tmp[1] = tmp;
233             p_tmp += 2;
234         }
235 #endif
236     }
237
238     if( p_sys->b_start )
239         p_block_in->i_pts = p_block_in->i_dts = 1;
240     else
241         p_block_in->i_pts = p_block_in->i_dts = 0;
242
243     while( (p_block_out = p_sys->p_packetizer->pf_packetize(
244                 p_sys->p_packetizer, &p_block_in )) )
245     {
246         p_sys->b_start = VLC_FALSE;
247
248         while( p_block_out )
249         {
250             block_t *p_next = p_block_out->p_next;
251
252             /* We assume a constant bitrate */
253             if( p_block_out->i_length )
254             {
255                 p_sys->i_mux_rate =
256                     p_block_out->i_buffer * I64C(1000000)/p_block_out->i_length;
257             }
258
259             /* set PCR */
260             es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
261
262             es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
263
264             p_block_out = p_next;
265         }
266     }
267
268     return 1;
269 }
270
271 /*****************************************************************************
272  * Control:
273  *****************************************************************************/
274 static int Control( demux_t *p_demux, int i_query, va_list args )
275 {
276     demux_sys_t *p_sys  = p_demux->p_sys;
277     return demux2_vaControlHelper( p_demux->s,
278                                    0, -1,
279                                    8*p_sys->i_mux_rate, 1, i_query, args );
280 }
281
282 /*****************************************************************************
283  * CheckSync: Check if buffer starts with an A52 sync code
284  *****************************************************************************/
285 static int CheckSync( uint8_t *p_peek, vlc_bool_t *p_big_endian )
286 {
287     /* Little endian version of the bitstream */
288     if( p_peek[0] == 0x77 && p_peek[1] == 0x0b &&
289         p_peek[4] < 0x60 /* bsid < 12 */ )
290     {
291         *p_big_endian = VLC_FALSE;
292         return VLC_SUCCESS;
293     }
294     /* Big endian version of the bitstream */
295     else if( p_peek[0] == 0x0b && p_peek[1] == 0x77 &&
296              p_peek[5] < 0x60 /* bsid < 12 */ )
297     {
298         *p_big_endian = VLC_TRUE;
299         return VLC_SUCCESS;
300     }
301
302     return VLC_EGENERIC;
303 }
304
305