]> git.sesse.net Git - vlc/blob - modules/demux/flac.c
* modules/codec/flac.c: complete rewrite of the flac decoder + new packetizer.
[vlc] / modules / demux / flac.c
1 /*****************************************************************************
2  * flac.c : FLAC demux module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: flac.c,v 1.6 2003/11/21 01:45:48 gbazin 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 #define STREAMINFO_SIZE 38
32 #define FLAC_PACKET_SIZE 16384
33
34 /*****************************************************************************
35  * Local prototypes
36  *****************************************************************************/
37 static int  Open  ( vlc_object_t * );
38 static void Close ( vlc_object_t * );
39 static int  Demux ( input_thread_t * );
40
41 struct demux_sys_t
42 {
43     vlc_bool_t  b_start;
44     es_out_id_t *p_es;
45
46     /* Packetizer */
47     decoder_t *p_packetizer;
48 };
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 vlc_module_begin();                                      
54     set_description( _("flac demuxer") );                       
55     set_capability( "demux", 155 );
56     set_callbacks( Open, Close );
57     add_shortcut( "flac" );
58 vlc_module_end();
59
60 /*****************************************************************************
61  * Open: initializes ES structures
62  *****************************************************************************/
63 static int Open( vlc_object_t * p_this )
64 {
65     input_thread_t *p_input = (input_thread_t *)p_this;
66     demux_sys_t    *p_sys;
67     int            i_peek;
68     byte_t *       p_peek;
69     es_format_t    fmt;
70
71     p_input->pf_demux = Demux;
72     p_input->pf_demux_control = demux_vaControlDefault;
73     p_input->pf_rewind = NULL;
74
75     /* Have a peep at the show. */
76     if( input_Peek( p_input, &p_peek, 4 ) < 4 )
77     {
78         /* Stream shorter than 4 bytes... */
79         msg_Err( p_input, "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( p_input->psz_demux && !strncmp( p_input->psz_demux, "flac", 4 ) )
86         {
87             /* User forced */
88             msg_Err( p_input, "this doesn't look like a flac stream, "
89                      "continuing anyway" );
90         }
91         else
92         {
93             msg_Warn( p_input, "flac module discarded (no startcode)" );
94             return VLC_EGENERIC;
95         }
96     }
97
98     p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
99     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'f', 'l', 'a', 'c' ) );
100     p_sys->b_start = VLC_TRUE;
101
102     /* Skip stream marker */
103     stream_Read( p_input->s, NULL, 4 );
104
105     /* We need to read and store the STREAMINFO metadata */
106     i_peek = stream_Peek( p_input->s, &p_peek, 4 );
107     if( p_peek[0] & 0x7F )
108     {
109         msg_Err( p_input, "This isn't a STREAMINFO metadata block" );
110         return VLC_EGENERIC;
111     }
112
113     if( ((p_peek[1]<<16)+(p_peek[2]<<8)+p_peek[3]) != (STREAMINFO_SIZE - 4) )
114     {
115         msg_Err( p_input, "Invalid size for a STREAMINFO metadata block" );
116         return VLC_EGENERIC;
117     }
118
119     /*
120      * Load the FLAC packetizer
121      */
122     p_sys->p_packetizer = vlc_object_create( p_input, VLC_OBJECT_DECODER );
123     p_sys->p_packetizer->pf_decode = 0;
124     p_sys->p_packetizer->pf_decode_audio = 0;
125     p_sys->p_packetizer->pf_decode_video = 0;
126     p_sys->p_packetizer->pf_decode_sub = 0;
127     p_sys->p_packetizer->pf_packetize = 0;
128     p_sys->p_packetizer->pf_run = 0;
129
130     /* Initialization of decoder structure */
131     es_format_Init( &p_sys->p_packetizer->fmt_in, AUDIO_ES,
132                     VLC_FOURCC( 'f', 'l', 'a', 'c' ) );
133
134     /* Store STREAMINFO for the decoder an packetizer */
135     p_sys->p_packetizer->fmt_in.i_extra = fmt.i_extra = STREAMINFO_SIZE;
136     p_sys->p_packetizer->fmt_in.p_extra = malloc( STREAMINFO_SIZE );
137     stream_Read( p_input->s, p_sys->p_packetizer->fmt_in.p_extra,
138                  STREAMINFO_SIZE );
139
140     /* Fake this a the last metadata block */
141     ((uint8_t*)p_sys->p_packetizer->fmt_in.p_extra)[0] |= 0x80;
142     fmt.p_extra = malloc( STREAMINFO_SIZE );
143     memcpy( fmt.p_extra, p_sys->p_packetizer->fmt_in.p_extra, STREAMINFO_SIZE);
144
145     p_sys->p_packetizer->p_module =
146         module_Need( p_sys->p_packetizer, "packetizer", NULL );
147     if( !p_sys->p_packetizer->p_module )
148     {
149         if( p_sys->p_packetizer->fmt_in.p_extra )
150             free( p_sys->p_packetizer->fmt_in.p_extra );
151
152         vlc_object_destroy( p_sys->p_packetizer );
153         msg_Err( p_input, "cannot find flac packetizer" );
154         return VLC_EGENERIC;
155     }
156
157     /* Create one program */
158     vlc_mutex_lock( &p_input->stream.stream_lock );
159     if( input_InitStream( p_input, 0 ) == -1 )
160     {
161         vlc_mutex_unlock( &p_input->stream.stream_lock );
162         msg_Err( p_input, "cannot init stream" );
163         return VLC_EGENERIC;
164     }
165     p_input->stream.i_mux_rate = 0;
166     vlc_mutex_unlock( &p_input->stream.stream_lock );
167
168     p_sys->p_es = es_out_Add( p_input->p_es_out, &fmt );
169
170     return VLC_SUCCESS;
171 }
172
173 /*****************************************************************************
174  * Close: frees unused data
175  *****************************************************************************/
176 static void Close( vlc_object_t * p_this )
177 {
178     input_thread_t *p_input = (input_thread_t*)p_this;
179     demux_sys_t    *p_sys = p_input->p_demux_data;
180
181     /* Unneed module */
182     module_Unneed( p_input, p_sys->p_packetizer->p_module );
183
184     if( p_sys->p_packetizer->fmt_in.p_extra )
185         free( p_sys->p_packetizer->fmt_in.p_extra );
186
187     /* Delete the decoder */
188     vlc_object_destroy( p_sys->p_packetizer );
189
190     free( p_sys );
191 }
192
193 /*****************************************************************************
194  * Demux: reads and demuxes data packets
195  *****************************************************************************
196  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
197  *****************************************************************************/
198 static int Demux( input_thread_t * p_input )
199 {
200     demux_sys_t  *p_sys = p_input->p_demux_data;
201     block_t *p_block_in, *p_block_out;
202
203     if( !( p_block_in = stream_Block( p_input->s, FLAC_PACKET_SIZE ) ) )
204     {
205         return 0;
206     }
207
208     if( p_sys->b_start )
209     {
210         p_block_in->i_pts = p_block_in->i_dts = 1;
211         p_sys->b_start = VLC_FALSE;
212     }
213     else
214     {
215         p_block_in->i_pts = p_block_in->i_dts = 0;
216     }
217
218     while( (p_block_out = p_sys->p_packetizer->pf_packetize(
219                 p_sys->p_packetizer, &p_block_in )) )
220     {
221         while( p_block_out )
222         {
223             block_t *p_next = p_block_out->p_next;
224
225             input_ClockManageRef( p_input,
226                                   p_input->stream.p_selected_program,
227                                   p_block_out->i_pts * 9 / 100 );
228
229             p_block_in->b_discontinuity = 0;
230             p_block_out->i_dts = p_block_out->i_pts =
231                 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
232                                   p_block_out->i_pts * 9 / 100 );
233
234             es_out_Send( p_input->p_es_out, p_sys->p_es, p_block_out );
235
236             p_block_out = p_next;
237         }
238     }
239
240     return 1;
241 }