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