1 /*****************************************************************************
2 * a52.c : raw A/52 stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_demux.h>
34 #include <vlc_codec.h>
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close ( vlc_object_t * );
47 set_category( CAT_INPUT );
48 set_subcategory( SUBCAT_INPUT_DEMUX );
49 set_description( N_("Raw A/52 demuxer") );
50 set_capability( "demux", 145 );
51 set_callbacks( Open, Close );
52 add_shortcut( "a52" );
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Demux ( demux_t * );
59 static int Control( demux_t *, int, va_list );
67 decoder_t *p_packetizer;
73 static int CheckSync( const uint8_t *p_peek, bool *p_big_endian );
75 #define PCM_FRAME_SIZE (1536 * 4)
76 #define A52_PACKET_SIZE (4 * PCM_FRAME_SIZE)
77 #define A52_PROBE_SIZE (512*1024)
78 #define A52_MAX_HEADER_SIZE 10
80 /*****************************************************************************
81 * Open: initializes ES structures
82 *****************************************************************************/
83 static int Open( vlc_object_t * p_this )
85 demux_t *p_demux = (demux_t*)p_this;
87 const uint8_t *p_peek;
89 bool b_big_endian = 0; /* Arbitrary initialisation */
91 /* Check if we are dealing with a WAV file */
92 if( stream_Peek( p_demux->s, &p_peek, 12+8 ) == 12+8 &&
93 !memcmp( p_peek, "RIFF", 4 ) && !memcmp( &p_peek[8], "WAVE", 4 ) )
95 /* Skip the wave header */
97 while( memcmp( p_peek + i_peek - 8, "data", 4 ) )
99 uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
100 if( i_len > A52_PROBE_SIZE || i_peek + i_len > A52_PROBE_SIZE )
104 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
108 /* TODO: should check wave format and sample_rate */
110 /* Some A52 wav files don't begin with a sync code so we do a more
111 * extensive search */
112 int i_size = stream_Peek( p_demux->s, &p_peek, i_peek + A52_PACKET_SIZE * 2);
113 i_size -= (PCM_FRAME_SIZE + A52_MAX_HEADER_SIZE);
115 while( i_peek < i_size )
117 if( CheckSync( p_peek + i_peek, &b_big_endian ) != VLC_SUCCESS )
118 /* The data is stored in 16 bits words */
122 /* Check following sync code */
123 if( CheckSync( p_peek + i_peek + PCM_FRAME_SIZE,
124 &b_big_endian ) != VLC_SUCCESS )
135 /* Have a peep at the show. */
136 CHECK_PEEK( p_peek, i_peek + A52_MAX_HEADER_SIZE * 2 );
138 if( CheckSync( p_peek + i_peek, &b_big_endian ) != VLC_SUCCESS )
140 if( !p_demux->b_force )
144 msg_Err( p_demux, "this doesn't look like a A52 audio stream, "
145 "continuing anyway" );
148 /* Fill p_demux fields */
149 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
150 p_sys->b_start = true;
151 p_sys->i_mux_rate = 0;
152 p_sys->b_big_endian = b_big_endian;
154 /* Load the A52 packetizer */
155 INIT_APACKETIZER( p_sys->p_packetizer, 'a', '5', '2', ' ' );
156 LOAD_PACKETIZER_OR_FAIL( p_sys->p_packetizer, "A52" );
158 /* Create one program */
159 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_in );
164 /*****************************************************************************
165 * Close: frees unused data
166 *****************************************************************************/
167 static void Close( vlc_object_t * p_this )
169 demux_t *p_demux = (demux_t*)p_this;
170 demux_sys_t *p_sys = p_demux->p_sys;
172 DESTROY_PACKETIZER( p_sys->p_packetizer );
176 /*****************************************************************************
177 * Demux: reads and demuxes data packets
178 *****************************************************************************
179 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
180 *****************************************************************************/
181 static int Demux( demux_t *p_demux )
183 demux_sys_t *p_sys = p_demux->p_sys;
184 block_t *p_block_in, *p_block_out;
187 int64_t i_pos = stream_Tell( p_demux->s );
188 if( i_pos % 2 ) stream_Read( p_demux->s, NULL, 1 );
190 if( !( p_block_in = stream_Block( p_demux->s, A52_PACKET_SIZE ) ) )
195 if( !p_sys->b_big_endian && p_block_in->i_buffer )
197 /* Convert to big endian */
200 swab(p_block_in->p_buffer, p_block_in->p_buffer, p_block_in->i_buffer);
205 p_tmp = p_block_in->p_buffer;
206 for( i = p_block_in->i_buffer / 2 ; i-- ; )
217 p_block_in->i_pts = p_block_in->i_dts = 1;
219 p_block_in->i_pts = p_block_in->i_dts = 0;
221 while( (p_block_out = p_sys->p_packetizer->pf_packetize(
222 p_sys->p_packetizer, &p_block_in )) )
224 p_sys->b_start = false;
228 block_t *p_next = p_block_out->p_next;
230 /* We assume a constant bitrate */
231 if( p_block_out->i_length )
234 p_block_out->i_buffer * INT64_C(1000000)/p_block_out->i_length;
238 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
240 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
242 p_block_out = p_next;
249 /*****************************************************************************
251 *****************************************************************************/
252 static int Control( demux_t *p_demux, int i_query, va_list args )
254 demux_sys_t *p_sys = p_demux->p_sys;
255 if( i_query == DEMUX_SET_TIME )
259 else if( i_query == DEMUX_HAS_UNSUPPORTED_META )
261 bool *pb_bool = (bool*)va_arg( args, bool* );
267 return demux_vaControlHelper( p_demux->s,
269 8*p_sys->i_mux_rate, 1, i_query, args );
273 /*****************************************************************************
274 * CheckSync: Check if buffer starts with an A52 sync code
275 *****************************************************************************/
276 static int CheckSync( const uint8_t *p_peek, bool *p_big_endian )
278 /* Little endian version of the bitstream */
279 if( p_peek[0] == 0x77 && p_peek[1] == 0x0b &&
280 p_peek[4] < 0x60 /* bsid < 12 */ )
282 *p_big_endian = false;
285 /* Big endian version of the bitstream */
286 else if( p_peek[0] == 0x0b && p_peek[1] == 0x77 &&
287 p_peek[5] < 0x60 /* bsid < 12 */ )
289 *p_big_endian = true;