1 /*****************************************************************************
2 * flac.c : FLAC demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
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 /*****************************************************************************
33 *****************************************************************************/
34 static int Open ( vlc_object_t * );
35 static void Close ( vlc_object_t * );
38 set_description( _("FLAC demuxer") );
39 set_capability( "demux2", 155 );
40 set_category( CAT_INPUT );
41 set_subcategory( SUBCAT_INPUT_DEMUX );
42 set_callbacks( Open, Close );
43 add_shortcut( "flac" );
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Demux ( demux_t * );
50 static int Control( demux_t *, int, va_list );
58 decoder_t *p_packetizer;
62 #define STREAMINFO_SIZE 38
63 #define FLAC_PACKET_SIZE 16384
65 /*****************************************************************************
66 * Open: initializes ES structures
67 *****************************************************************************/
68 static int Open( vlc_object_t * p_this )
70 demux_t *p_demux = (demux_t*)p_this;
76 vlc_meta_t *p_meta = NULL;
78 /* Skip/parse possible id3 header */
79 if( ( p_id3 = module_Need( p_demux, "id3", NULL, 0 ) ) )
81 p_meta = (vlc_meta_t *)p_demux->p_private;
82 p_demux->p_private = NULL;
83 module_Unneed( p_demux, p_id3 );
86 /* Have a peep at the show. */
87 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
89 if( p_meta ) vlc_meta_Delete( p_meta );
93 if( p_peek[0]!='f' || p_peek[1]!='L' || p_peek[2]!='a' || p_peek[3]!='C' )
95 if( strncmp( p_demux->psz_demux, "flac", 4 ) )
97 if( p_meta ) vlc_meta_Delete( p_meta );
101 msg_Err( p_demux, "this doesn't look like a flac stream, "
102 "continuing anyway" );
105 p_demux->pf_demux = Demux;
106 p_demux->pf_control = Control;
107 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
108 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'f', 'l', 'a', 'c' ) );
109 p_sys->b_start = VLC_TRUE;
110 p_sys->p_meta = p_meta;
112 /* We need to read and store the STREAMINFO metadata */
113 i_peek = stream_Peek( p_demux->s, &p_peek, 8 );
114 if( p_peek[4] & 0x7F )
116 msg_Err( p_demux, "this isn't a STREAMINFO metadata block" );
117 if( p_meta ) vlc_meta_Delete( p_meta );
121 if( ((p_peek[5]<<16)+(p_peek[6]<<8)+p_peek[7]) != (STREAMINFO_SIZE - 4) )
123 msg_Err( p_demux, "invalid size for a STREAMINFO metadata block" );
124 if( p_meta ) vlc_meta_Delete( p_meta );
129 * Load the FLAC packetizer
131 p_sys->p_packetizer = vlc_object_create( p_demux, VLC_OBJECT_DECODER );
132 p_sys->p_packetizer->pf_decode_audio = 0;
133 p_sys->p_packetizer->pf_decode_video = 0;
134 p_sys->p_packetizer->pf_decode_sub = 0;
135 p_sys->p_packetizer->pf_packetize = 0;
137 /* Initialization of decoder structure */
138 es_format_Init( &p_sys->p_packetizer->fmt_in, AUDIO_ES,
139 VLC_FOURCC( 'f', 'l', 'a', 'c' ) );
141 /* Store STREAMINFO for the decoder and packetizer */
142 p_sys->p_packetizer->fmt_in.i_extra = fmt.i_extra = STREAMINFO_SIZE + 4;
143 p_sys->p_packetizer->fmt_in.p_extra = malloc( STREAMINFO_SIZE + 4 );
144 stream_Read( p_demux->s, p_sys->p_packetizer->fmt_in.p_extra,
145 STREAMINFO_SIZE + 4 );
147 /* Fake this as the last metadata block */
148 ((uint8_t*)p_sys->p_packetizer->fmt_in.p_extra)[4] |= 0x80;
149 fmt.p_extra = malloc( STREAMINFO_SIZE + 4 );
150 memcpy( fmt.p_extra, p_sys->p_packetizer->fmt_in.p_extra,
151 STREAMINFO_SIZE + 4 );
153 p_sys->p_packetizer->p_module =
154 module_Need( p_sys->p_packetizer, "packetizer", NULL, 0 );
155 if( !p_sys->p_packetizer->p_module )
157 if( p_sys->p_packetizer->fmt_in.p_extra )
158 free( p_sys->p_packetizer->fmt_in.p_extra );
160 vlc_object_destroy( p_sys->p_packetizer );
161 msg_Err( p_demux, "cannot find flac packetizer" );
162 if( p_meta ) vlc_meta_Delete( p_meta );
166 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
171 /*****************************************************************************
172 * Close: frees unused data
173 *****************************************************************************/
174 static void Close( vlc_object_t * p_this )
176 demux_t *p_demux = (demux_t*)p_this;
177 demux_sys_t *p_sys = p_demux->p_sys;
180 module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
182 if( p_sys->p_packetizer->fmt_in.p_extra )
183 free( p_sys->p_packetizer->fmt_in.p_extra );
185 /* Delete the decoder */
186 vlc_object_destroy( p_sys->p_packetizer );
187 if( p_sys->p_meta ) vlc_meta_Delete( p_sys->p_meta );
191 /*****************************************************************************
192 * Demux: reads and demuxes data packets
193 *****************************************************************************
194 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
195 *****************************************************************************/
196 static int Demux( demux_t *p_demux )
198 demux_sys_t *p_sys = p_demux->p_sys;
199 block_t *p_block_in, *p_block_out;
201 if( !( p_block_in = stream_Block( p_demux->s, FLAC_PACKET_SIZE ) ) )
208 p_block_in->i_pts = p_block_in->i_dts = 1;
209 p_sys->b_start = VLC_FALSE;
213 p_block_in->i_pts = p_block_in->i_dts = 0;
216 while( (p_block_out = p_sys->p_packetizer->pf_packetize(
217 p_sys->p_packetizer, &p_block_in )) )
221 block_t *p_next = p_block_out->p_next;
224 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
226 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
228 p_block_out = p_next;
235 /*****************************************************************************
237 *****************************************************************************/
238 static int Control( demux_t *p_demux, int i_query, va_list args )
240 /* demux_sys_t *p_sys = p_demux->p_sys; */
242 if( i_query == DEMUX_SET_TIME )
244 else if( i_query == DEMUX_GET_META )
246 vlc_meta_t **pp_meta = (vlc_meta_t **)va_arg( args, vlc_meta_t** );
247 if( p_demux->p_sys->p_meta ) *pp_meta = vlc_meta_Duplicate( p_demux->p_sys->p_meta );
248 else *pp_meta = NULL;
252 return demux2_vaControlHelper( p_demux->s,
254 8*0, 1, i_query, args );