]> git.sesse.net Git - vlc/blob - modules/demux/flac.c
A bit of headers cleanup
[vlc] / modules / demux / flac.c
1 /*****************************************************************************
2  * flac.c : FLAC demux module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2003 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc_demux.h>
29 #include <vlc_meta.h>
30 #include <vlc_codec.h>
31
32 /*****************************************************************************
33  * Module descriptor
34  *****************************************************************************/
35 static int  Open  ( vlc_object_t * );
36 static void Close ( vlc_object_t * );
37
38 vlc_module_begin();
39     set_description( _("FLAC demuxer") );
40     set_capability( "demux2", 155 );
41     set_category( CAT_INPUT );
42     set_subcategory( SUBCAT_INPUT_DEMUX );
43     set_callbacks( Open, Close );
44     add_shortcut( "flac" );
45 vlc_module_end();
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50 static int Demux  ( demux_t * );
51 static int Control( demux_t *, int, va_list );
52
53 struct demux_sys_t
54 {
55     vlc_bool_t  b_start;
56     es_out_id_t *p_es;
57
58     /* Packetizer */
59     decoder_t *p_packetizer;
60     vlc_meta_t *p_meta;
61 };
62
63 #define STREAMINFO_SIZE 38
64 #define FLAC_PACKET_SIZE 16384
65
66 /*****************************************************************************
67  * Open: initializes ES structures
68  *****************************************************************************/
69 static int Open( vlc_object_t * p_this )
70 {
71     demux_t     *p_demux = (demux_t*)p_this;
72     module_t    *p_id3;
73     demux_sys_t *p_sys;
74     int          i_peek;
75     byte_t      *p_peek;
76     es_format_t  fmt;
77
78     /* Have a peep at the show. */
79     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
80
81     if( p_peek[0]!='f' || p_peek[1]!='L' || p_peek[2]!='a' || p_peek[3]!='C' )
82     {
83         if( !p_demux->b_force ) return VLC_EGENERIC;
84
85         /* User forced */
86         msg_Err( p_demux, "this doesn't look like a flac stream, "
87                  "continuing anyway" );
88     }
89
90     p_demux->pf_demux   = Demux;
91     p_demux->pf_control = Control;
92     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
93     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'f', 'l', 'a', 'c' ) );
94     p_sys->b_start = VLC_TRUE;
95     p_sys->p_meta = NULL;
96
97     /* We need to read and store the STREAMINFO metadata */
98     i_peek = stream_Peek( p_demux->s, &p_peek, 8 );
99     if( p_peek[4] & 0x7F )
100     {
101         msg_Err( p_demux, "this isn't a STREAMINFO metadata block" );
102         return VLC_EGENERIC;
103     }
104
105     if( ((p_peek[5]<<16)+(p_peek[6]<<8)+p_peek[7]) != (STREAMINFO_SIZE - 4) )
106     {
107         msg_Err( p_demux, "invalid size for a STREAMINFO metadata block" );
108         return VLC_EGENERIC;
109     }
110
111     /* Load the FLAC packetizer */
112     INIT_APACKETIZER( p_sys->p_packetizer, 'f', 'l', 'a', 'c' );
113
114     /* Store STREAMINFO for the decoder and packetizer */
115     p_sys->p_packetizer->fmt_in.i_extra = fmt.i_extra = STREAMINFO_SIZE + 4;
116     p_sys->p_packetizer->fmt_in.p_extra = malloc( STREAMINFO_SIZE + 4 );
117     stream_Read( p_demux->s, p_sys->p_packetizer->fmt_in.p_extra,
118                  STREAMINFO_SIZE + 4 );
119
120     /* Fake this as the last metadata block */
121     ((uint8_t*)p_sys->p_packetizer->fmt_in.p_extra)[4] |= 0x80;
122     fmt.p_extra = malloc( STREAMINFO_SIZE + 4 );
123     memcpy( fmt.p_extra, p_sys->p_packetizer->fmt_in.p_extra,
124             STREAMINFO_SIZE + 4 );
125
126     p_sys->p_packetizer->p_module =
127         module_Need( p_sys->p_packetizer, "packetizer", NULL, 0 );
128     if( !p_sys->p_packetizer->p_module )
129     {
130         if( p_sys->p_packetizer->fmt_in.p_extra )
131             free( p_sys->p_packetizer->fmt_in.p_extra );
132         vlc_object_destroy( p_sys->p_packetizer );
133
134         msg_Err( p_demux, "cannot find flac packetizer" );
135         return VLC_EGENERIC;
136     }
137
138     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
139
140     /* Parse possible id3 header */
141     if( ( p_id3 = module_Need( p_demux, "meta reader", NULL, 0 ) ) )
142     {
143         p_sys->p_meta = (vlc_meta_t *)p_demux->p_private;
144         p_demux->p_private = NULL;
145         module_Unneed( p_demux, p_id3 );
146     }
147
148     return VLC_SUCCESS;
149 }
150
151 /*****************************************************************************
152  * Close: frees unused data
153  *****************************************************************************/
154 static void Close( vlc_object_t * p_this )
155 {
156     demux_t     *p_demux = (demux_t*)p_this;
157     demux_sys_t *p_sys = p_demux->p_sys;
158
159     /* Unneed module */
160     module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
161
162     if( p_sys->p_packetizer->fmt_in.p_extra )
163         free( p_sys->p_packetizer->fmt_in.p_extra );
164
165     /* Delete the decoder */
166     vlc_object_destroy( p_sys->p_packetizer );
167     if( p_sys->p_meta ) vlc_meta_Delete( p_sys->p_meta );
168     free( p_sys );
169 }
170
171 /*****************************************************************************
172  * Demux: reads and demuxes data packets
173  *****************************************************************************
174  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
175  *****************************************************************************/
176 static int Demux( demux_t *p_demux )
177 {
178     demux_sys_t *p_sys = p_demux->p_sys;
179     block_t     *p_block_in, *p_block_out;
180
181     if( !( p_block_in = stream_Block( p_demux->s, FLAC_PACKET_SIZE ) ) )
182     {
183         return 0;
184     }
185
186     if( p_sys->b_start )
187     {
188         p_block_in->i_pts = p_block_in->i_dts = 1;
189         p_sys->b_start = VLC_FALSE;
190     }
191     else
192     {
193         p_block_in->i_pts = p_block_in->i_dts = 0;
194     }
195
196     while( (p_block_out = p_sys->p_packetizer->pf_packetize(
197                 p_sys->p_packetizer, &p_block_in )) )
198     {
199         while( p_block_out )
200         {
201             block_t *p_next = p_block_out->p_next;
202
203             /* set PCR */
204             es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
205
206             es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
207
208             p_block_out = p_next;
209         }
210     }
211
212     return 1;
213 }
214
215 /*****************************************************************************
216  * Control:
217  *****************************************************************************/
218 static int Control( demux_t *p_demux, int i_query, va_list args )
219 {
220     /* demux_sys_t *p_sys  = p_demux->p_sys; */
221     /* FIXME bitrate */
222     if( i_query == DEMUX_SET_TIME ) return VLC_EGENERIC;
223     else if( i_query == DEMUX_GET_META )
224     {
225         vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
226         if( p_demux->p_sys->p_meta )
227             vlc_meta_Merge( p_meta, p_demux->p_sys->p_meta );
228         return VLC_SUCCESS;
229     }
230     else return demux2_vaControlHelper( p_demux->s, 0, -1,
231                                         8*0, 1, i_query, args );
232 }
233