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