1 /*****************************************************************************
2 * flac.c : FLAC demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: flac.c,v 1.11 2004/02/25 17:48:52 fenrir 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 STREAMINFO_SIZE 38
32 #define FLAC_PACKET_SIZE 16384
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( _("FLAC demuxer") );
55 set_capability( "demux", 155 );
56 set_callbacks( Open, Close );
57 add_shortcut( "flac" );
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;
71 p_input->pf_demux = Demux;
72 p_input->pf_demux_control = demux_vaControlDefault;
73 p_input->pf_rewind = NULL;
75 /* Have a peep at the show. */
76 if( input_Peek( p_input, &p_peek, 4 ) < 4 )
78 /* Stream shorter than 4 bytes... */
79 msg_Err( p_input, "cannot peek()" );
83 if( p_peek[0]!='f' || p_peek[1]!='L' || p_peek[2]!='a' || p_peek[3]!='C' )
85 if( p_input->psz_demux && !strncmp( p_input->psz_demux, "flac", 4 ) )
88 msg_Err( p_input, "this doesn't look like a flac stream, "
89 "continuing anyway" );
93 msg_Warn( p_input, "flac module discarded (no startcode)" );
98 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
99 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'f', 'l', 'a', 'c' ) );
100 p_sys->b_start = VLC_TRUE;
102 /* We need to read and store the STREAMINFO metadata */
103 i_peek = stream_Peek( p_input->s, &p_peek, 8 );
104 if( p_peek[4] & 0x7F )
106 msg_Err( p_input, "this isn't a STREAMINFO metadata block" );
110 if( ((p_peek[5]<<16)+(p_peek[6]<<8)+p_peek[7]) != (STREAMINFO_SIZE - 4) )
112 msg_Err( p_input, "invalid size for a STREAMINFO metadata block" );
117 * Load the FLAC packetizer
119 p_sys->p_packetizer = vlc_object_create( p_input, VLC_OBJECT_DECODER );
120 p_sys->p_packetizer->pf_decode_audio = 0;
121 p_sys->p_packetizer->pf_decode_video = 0;
122 p_sys->p_packetizer->pf_decode_sub = 0;
123 p_sys->p_packetizer->pf_packetize = 0;
125 /* Initialization of decoder structure */
126 es_format_Init( &p_sys->p_packetizer->fmt_in, AUDIO_ES,
127 VLC_FOURCC( 'f', 'l', 'a', 'c' ) );
129 /* Store STREAMINFO for the decoder and packetizer */
130 p_sys->p_packetizer->fmt_in.i_extra = fmt.i_extra = STREAMINFO_SIZE + 4;
131 p_sys->p_packetizer->fmt_in.p_extra = malloc( STREAMINFO_SIZE + 4 );
132 stream_Read( p_input->s, p_sys->p_packetizer->fmt_in.p_extra,
133 STREAMINFO_SIZE + 4 );
135 /* Fake this as the last metadata block */
136 ((uint8_t*)p_sys->p_packetizer->fmt_in.p_extra)[4] |= 0x80;
137 fmt.p_extra = malloc( STREAMINFO_SIZE + 4 );
138 memcpy( fmt.p_extra, p_sys->p_packetizer->fmt_in.p_extra,
139 STREAMINFO_SIZE + 4 );
141 p_sys->p_packetizer->p_module =
142 module_Need( p_sys->p_packetizer, "packetizer", NULL );
143 if( !p_sys->p_packetizer->p_module )
145 if( p_sys->p_packetizer->fmt_in.p_extra )
146 free( p_sys->p_packetizer->fmt_in.p_extra );
148 vlc_object_destroy( p_sys->p_packetizer );
149 msg_Err( p_input, "cannot find flac packetizer" );
153 /* Create one program */
154 vlc_mutex_lock( &p_input->stream.stream_lock );
155 if( input_InitStream( p_input, 0 ) == -1 )
157 vlc_mutex_unlock( &p_input->stream.stream_lock );
158 msg_Err( p_input, "cannot init stream" );
161 p_input->stream.i_mux_rate = 0;
162 vlc_mutex_unlock( &p_input->stream.stream_lock );
164 p_sys->p_es = es_out_Add( p_input->p_es_out, &fmt );
169 /*****************************************************************************
170 * Close: frees unused data
171 *****************************************************************************/
172 static void Close( vlc_object_t * p_this )
174 input_thread_t *p_input = (input_thread_t*)p_this;
175 demux_sys_t *p_sys = p_input->p_demux_data;
178 module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
180 if( p_sys->p_packetizer->fmt_in.p_extra )
181 free( p_sys->p_packetizer->fmt_in.p_extra );
183 /* Delete the decoder */
184 vlc_object_destroy( p_sys->p_packetizer );
189 /*****************************************************************************
190 * Demux: reads and demuxes data packets
191 *****************************************************************************
192 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
193 *****************************************************************************/
194 static int Demux( input_thread_t * p_input )
196 demux_sys_t *p_sys = p_input->p_demux_data;
197 block_t *p_block_in, *p_block_out;
199 if( !( p_block_in = stream_Block( p_input->s, FLAC_PACKET_SIZE ) ) )
206 p_block_in->i_pts = p_block_in->i_dts = 1;
207 p_sys->b_start = VLC_FALSE;
211 p_block_in->i_pts = p_block_in->i_dts = 0;
214 while( (p_block_out = p_sys->p_packetizer->pf_packetize(
215 p_sys->p_packetizer, &p_block_in )) )
219 block_t *p_next = p_block_out->p_next;
221 input_ClockManageRef( p_input,
222 p_input->stream.p_selected_program,
223 p_block_out->i_pts * 9 / 100 );
225 p_block_out->i_dts = p_block_out->i_pts =
226 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
227 p_block_out->i_pts * 9 / 100 );
229 es_out_Send( p_input->p_es_out, p_sys->p_es, p_block_out );
231 p_block_out = p_next;