]> git.sesse.net Git - vlc/blob - modules/demux/flac.c
* all: removed decoder_fifo_t.
[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.9 2003/11/24 00:39:01 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 #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     /* We need to read and store the STREAMINFO metadata */
103     i_peek = stream_Peek( p_input->s, &p_peek, 8 );
104     if( p_peek[4] & 0x7F )
105     {
106         msg_Err( p_input, "This isn't a STREAMINFO metadata block" );
107         return VLC_EGENERIC;
108     }
109
110     if( ((p_peek[5]<<16)+(p_peek[6]<<8)+p_peek[7]) != (STREAMINFO_SIZE - 4) )
111     {
112         msg_Err( p_input, "Invalid size for a STREAMINFO metadata block" );
113         return VLC_EGENERIC;
114     }
115
116     /*
117      * Load the FLAC packetizer
118      */
119     p_sys->p_packetizer = vlc_object_create( p_input, VLC_OBJECT_DECODER );
120     p_sys->p_packetizer->pf_decode_audio = 0;
121     p_sys->p_packetizer->pf_decode_video = 0;
122     p_sys->p_packetizer->pf_decode_sub = 0;
123     p_sys->p_packetizer->pf_packetize = 0;
124
125     /* Initialization of decoder structure */
126     es_format_Init( &p_sys->p_packetizer->fmt_in, AUDIO_ES,
127                     VLC_FOURCC( 'f', 'l', 'a', 'c' ) );
128
129     /* Store STREAMINFO for the decoder and packetizer */
130     p_sys->p_packetizer->fmt_in.i_extra = fmt.i_extra = STREAMINFO_SIZE + 4;
131     p_sys->p_packetizer->fmt_in.p_extra = malloc( STREAMINFO_SIZE + 4 );
132     stream_Read( p_input->s, p_sys->p_packetizer->fmt_in.p_extra,
133                  STREAMINFO_SIZE + 4 );
134
135     /* Fake this as the last metadata block */
136     ((uint8_t*)p_sys->p_packetizer->fmt_in.p_extra)[4] |= 0x80;
137     fmt.p_extra = malloc( STREAMINFO_SIZE + 4 );
138     memcpy( fmt.p_extra, p_sys->p_packetizer->fmt_in.p_extra,
139             STREAMINFO_SIZE + 4 );
140
141     p_sys->p_packetizer->p_module =
142         module_Need( p_sys->p_packetizer, "packetizer", NULL );
143     if( !p_sys->p_packetizer->p_module )
144     {
145         if( p_sys->p_packetizer->fmt_in.p_extra )
146             free( p_sys->p_packetizer->fmt_in.p_extra );
147
148         vlc_object_destroy( p_sys->p_packetizer );
149         msg_Err( p_input, "cannot find flac packetizer" );
150         return VLC_EGENERIC;
151     }
152
153     /* Create one program */
154     vlc_mutex_lock( &p_input->stream.stream_lock );
155     if( input_InitStream( p_input, 0 ) == -1 )
156     {
157         vlc_mutex_unlock( &p_input->stream.stream_lock );
158         msg_Err( p_input, "cannot init stream" );
159         return VLC_EGENERIC;
160     }
161     p_input->stream.i_mux_rate = 0;
162     vlc_mutex_unlock( &p_input->stream.stream_lock );
163
164     p_sys->p_es = es_out_Add( p_input->p_es_out, &fmt );
165
166     return VLC_SUCCESS;
167 }
168
169 /*****************************************************************************
170  * Close: frees unused data
171  *****************************************************************************/
172 static void Close( vlc_object_t * p_this )
173 {
174     input_thread_t *p_input = (input_thread_t*)p_this;
175     demux_sys_t    *p_sys = p_input->p_demux_data;
176
177     /* Unneed module */
178     module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
179
180     if( p_sys->p_packetizer->fmt_in.p_extra )
181         free( p_sys->p_packetizer->fmt_in.p_extra );
182
183     /* Delete the decoder */
184     vlc_object_destroy( p_sys->p_packetizer );
185
186     free( p_sys );
187 }
188
189 /*****************************************************************************
190  * Demux: reads and demuxes data packets
191  *****************************************************************************
192  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
193  *****************************************************************************/
194 static int Demux( input_thread_t * p_input )
195 {
196     demux_sys_t  *p_sys = p_input->p_demux_data;
197     block_t *p_block_in, *p_block_out;
198
199     if( !( p_block_in = stream_Block( p_input->s, FLAC_PACKET_SIZE ) ) )
200     {
201         return 0;
202     }
203
204     if( p_sys->b_start )
205     {
206         p_block_in->i_pts = p_block_in->i_dts = 1;
207         p_sys->b_start = VLC_FALSE;
208     }
209     else
210     {
211         p_block_in->i_pts = p_block_in->i_dts = 0;
212     }
213
214     while( (p_block_out = p_sys->p_packetizer->pf_packetize(
215                 p_sys->p_packetizer, &p_block_in )) )
216     {
217         while( p_block_out )
218         {
219             block_t *p_next = p_block_out->p_next;
220
221             input_ClockManageRef( p_input,
222                                   p_input->stream.p_selected_program,
223                                   p_block_out->i_pts * 9 / 100 );
224
225             p_block_in->b_discontinuity = 0;
226             p_block_out->i_dts = p_block_out->i_pts =
227                 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
228                                   p_block_out->i_pts * 9 / 100 );
229
230             es_out_Send( p_input->p_es_out, p_sys->p_es, p_block_out );
231
232             p_block_out = p_next;
233         }
234     }
235
236     return 1;
237 }