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