1 /*****************************************************************************
2 * flac.c : FLAC demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: flac.c,v 1.7 2003/11/21 12:18:54 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 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 /* Skip stream marker */
103 stream_Read( p_input->s, NULL, 4 );
105 /* We need to read and store the STREAMINFO metadata */
106 i_peek = stream_Peek( p_input->s, &p_peek, 4 );
107 if( p_peek[0] & 0x7F )
109 msg_Err( p_input, "This isn't a STREAMINFO metadata block" );
113 if( ((p_peek[1]<<16)+(p_peek[2]<<8)+p_peek[3]) != (STREAMINFO_SIZE - 4) )
115 msg_Err( p_input, "Invalid size for a STREAMINFO metadata block" );
120 * Load the FLAC packetizer
122 p_sys->p_packetizer = vlc_object_create( p_input, VLC_OBJECT_DECODER );
123 p_sys->p_packetizer->pf_decode = 0;
124 p_sys->p_packetizer->pf_decode_audio = 0;
125 p_sys->p_packetizer->pf_decode_video = 0;
126 p_sys->p_packetizer->pf_decode_sub = 0;
127 p_sys->p_packetizer->pf_packetize = 0;
128 p_sys->p_packetizer->pf_run = 0;
130 /* Initialization of decoder structure */
131 es_format_Init( &p_sys->p_packetizer->fmt_in, AUDIO_ES,
132 VLC_FOURCC( 'f', 'l', 'a', 'c' ) );
134 /* Store STREAMINFO for the decoder and packetizer */
135 p_sys->p_packetizer->fmt_in.i_extra = fmt.i_extra = STREAMINFO_SIZE;
136 p_sys->p_packetizer->fmt_in.p_extra = malloc( STREAMINFO_SIZE );
137 stream_Read( p_input->s, p_sys->p_packetizer->fmt_in.p_extra,
140 /* Fake this as the last metadata block */
141 ((uint8_t*)p_sys->p_packetizer->fmt_in.p_extra)[0] |= 0x80;
142 fmt.p_extra = malloc( STREAMINFO_SIZE );
143 memcpy( fmt.p_extra, p_sys->p_packetizer->fmt_in.p_extra, STREAMINFO_SIZE);
145 p_sys->p_packetizer->p_module =
146 module_Need( p_sys->p_packetizer, "packetizer", NULL );
147 if( !p_sys->p_packetizer->p_module )
149 if( p_sys->p_packetizer->fmt_in.p_extra )
150 free( p_sys->p_packetizer->fmt_in.p_extra );
152 vlc_object_destroy( p_sys->p_packetizer );
153 msg_Err( p_input, "cannot find flac packetizer" );
157 /* Create one program */
158 vlc_mutex_lock( &p_input->stream.stream_lock );
159 if( input_InitStream( p_input, 0 ) == -1 )
161 vlc_mutex_unlock( &p_input->stream.stream_lock );
162 msg_Err( p_input, "cannot init stream" );
165 p_input->stream.i_mux_rate = 0;
166 vlc_mutex_unlock( &p_input->stream.stream_lock );
168 p_sys->p_es = es_out_Add( p_input->p_es_out, &fmt );
173 /*****************************************************************************
174 * Close: frees unused data
175 *****************************************************************************/
176 static void Close( vlc_object_t * p_this )
178 input_thread_t *p_input = (input_thread_t*)p_this;
179 demux_sys_t *p_sys = p_input->p_demux_data;
182 module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
184 if( p_sys->p_packetizer->fmt_in.p_extra )
185 free( p_sys->p_packetizer->fmt_in.p_extra );
187 /* Delete the decoder */
188 vlc_object_destroy( p_sys->p_packetizer );
193 /*****************************************************************************
194 * Demux: reads and demuxes data packets
195 *****************************************************************************
196 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
197 *****************************************************************************/
198 static int Demux( input_thread_t * p_input )
200 demux_sys_t *p_sys = p_input->p_demux_data;
201 block_t *p_block_in, *p_block_out;
203 if( !( p_block_in = stream_Block( p_input->s, FLAC_PACKET_SIZE ) ) )
210 p_block_in->i_pts = p_block_in->i_dts = 1;
211 p_sys->b_start = VLC_FALSE;
215 p_block_in->i_pts = p_block_in->i_dts = 0;
218 while( (p_block_out = p_sys->p_packetizer->pf_packetize(
219 p_sys->p_packetizer, &p_block_in )) )
223 block_t *p_next = p_block_out->p_next;
225 input_ClockManageRef( p_input,
226 p_input->stream.p_selected_program,
227 p_block_out->i_pts * 9 / 100 );
229 p_block_in->b_discontinuity = 0;
230 p_block_out->i_dts = p_block_out->i_pts =
231 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
232 p_block_out->i_pts * 9 / 100 );
234 es_out_Send( p_input->p_es_out, p_sys->p_es, p_block_out );
236 p_block_out = p_next;