]> git.sesse.net Git - vlc/blob - modules/demux/flac.c
Improvements to preferences
[vlc] / modules / demux / flac.c
1 /*****************************************************************************
2  * flac.c : FLAC demux module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2003 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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 };
60
61 #define STREAMINFO_SIZE 38
62 #define FLAC_PACKET_SIZE 16384
63
64 /*****************************************************************************
65  * Open: initializes ES structures
66  *****************************************************************************/
67 static int Open( vlc_object_t * p_this )
68 {
69     demux_t     *p_demux = (demux_t*)p_this;
70     demux_sys_t *p_sys;
71     int          i_peek;
72     byte_t      *p_peek;
73     es_format_t  fmt;
74
75     /* Have a peep at the show. */
76     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
77     {
78         /* Stream shorter than 4 bytes... */
79         msg_Err( p_demux, "cannot peek()" );
80         return VLC_EGENERIC;
81     }
82
83     if( p_peek[0]!='f' || p_peek[1]!='L' || p_peek[2]!='a' || p_peek[3]!='C' )
84     {
85         if( strncmp( p_demux->psz_demux, "flac", 4 ) )
86         {
87             msg_Warn( p_demux, "flac module discarded (no startcode)" );
88             return VLC_EGENERIC;
89         }
90         /* User forced */
91         msg_Err( p_demux, "this doesn't look like a flac stream, "
92                  "continuing anyway" );
93     }
94
95     p_demux->pf_demux   = Demux;
96     p_demux->pf_control = Control;
97     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
98     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'f', 'l', 'a', 'c' ) );
99     p_sys->b_start = VLC_TRUE;
100
101     /* We need to read and store the STREAMINFO metadata */
102     i_peek = stream_Peek( p_demux->s, &p_peek, 8 );
103     if( p_peek[4] & 0x7F )
104     {
105         msg_Err( p_demux, "this isn't a STREAMINFO metadata block" );
106         return VLC_EGENERIC;
107     }
108
109     if( ((p_peek[5]<<16)+(p_peek[6]<<8)+p_peek[7]) != (STREAMINFO_SIZE - 4) )
110     {
111         msg_Err( p_demux, "invalid size for a STREAMINFO metadata block" );
112         return VLC_EGENERIC;
113     }
114
115     /*
116      * Load the FLAC packetizer
117      */
118     p_sys->p_packetizer = vlc_object_create( p_demux, VLC_OBJECT_DECODER );
119     p_sys->p_packetizer->pf_decode_audio = 0;
120     p_sys->p_packetizer->pf_decode_video = 0;
121     p_sys->p_packetizer->pf_decode_sub = 0;
122     p_sys->p_packetizer->pf_packetize = 0;
123
124     /* Initialization of decoder structure */
125     es_format_Init( &p_sys->p_packetizer->fmt_in, AUDIO_ES,
126                     VLC_FOURCC( 'f', 'l', 'a', 'c' ) );
127
128     /* Store STREAMINFO for the decoder and packetizer */
129     p_sys->p_packetizer->fmt_in.i_extra = fmt.i_extra = STREAMINFO_SIZE + 4;
130     p_sys->p_packetizer->fmt_in.p_extra = malloc( STREAMINFO_SIZE + 4 );
131     stream_Read( p_demux->s, p_sys->p_packetizer->fmt_in.p_extra,
132                  STREAMINFO_SIZE + 4 );
133
134     /* Fake this as the last metadata block */
135     ((uint8_t*)p_sys->p_packetizer->fmt_in.p_extra)[4] |= 0x80;
136     fmt.p_extra = malloc( STREAMINFO_SIZE + 4 );
137     memcpy( fmt.p_extra, p_sys->p_packetizer->fmt_in.p_extra,
138             STREAMINFO_SIZE + 4 );
139
140     p_sys->p_packetizer->p_module =
141         module_Need( p_sys->p_packetizer, "packetizer", NULL, 0 );
142     if( !p_sys->p_packetizer->p_module )
143     {
144         if( p_sys->p_packetizer->fmt_in.p_extra )
145             free( p_sys->p_packetizer->fmt_in.p_extra );
146
147         vlc_object_destroy( p_sys->p_packetizer );
148         msg_Err( p_demux, "cannot find flac packetizer" );
149         return VLC_EGENERIC;
150     }
151
152     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
153
154     return VLC_SUCCESS;
155 }
156
157 /*****************************************************************************
158  * Close: frees unused data
159  *****************************************************************************/
160 static void Close( vlc_object_t * p_this )
161 {
162     demux_t     *p_demux = (demux_t*)p_this;
163     demux_sys_t *p_sys = p_demux->p_sys;
164
165     /* Unneed module */
166     module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
167
168     if( p_sys->p_packetizer->fmt_in.p_extra )
169         free( p_sys->p_packetizer->fmt_in.p_extra );
170
171     /* Delete the decoder */
172     vlc_object_destroy( p_sys->p_packetizer );
173
174     free( p_sys );
175 }
176
177 /*****************************************************************************
178  * Demux: reads and demuxes data packets
179  *****************************************************************************
180  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
181  *****************************************************************************/
182 static int Demux( demux_t *p_demux )
183 {
184     demux_sys_t *p_sys = p_demux->p_sys;
185     block_t     *p_block_in, *p_block_out;
186
187     if( !( p_block_in = stream_Block( p_demux->s, FLAC_PACKET_SIZE ) ) )
188     {
189         return 0;
190     }
191
192     if( p_sys->b_start )
193     {
194         p_block_in->i_pts = p_block_in->i_dts = 1;
195         p_sys->b_start = VLC_FALSE;
196     }
197     else
198     {
199         p_block_in->i_pts = p_block_in->i_dts = 0;
200     }
201
202     while( (p_block_out = p_sys->p_packetizer->pf_packetize(
203                 p_sys->p_packetizer, &p_block_in )) )
204     {
205         while( p_block_out )
206         {
207             block_t *p_next = p_block_out->p_next;
208
209             /* set PCR */
210             es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
211
212             es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
213
214             p_block_out = p_next;
215         }
216     }
217
218     return 1;
219 }
220
221 /*****************************************************************************
222  * Control:
223  *****************************************************************************/
224 static int Control( demux_t *p_demux, int i_query, va_list args )
225 {
226     /* demux_sys_t *p_sys  = p_demux->p_sys; */
227     /* FIXME bitrate */
228     if( i_query == DEMUX_SET_TIME )
229         return VLC_EGENERIC;
230     else
231         return demux2_vaControlHelper( p_demux->s,
232                                        0, -1,
233                                        8*0, 1, i_query, args );
234 }
235