1 /*****************************************************************************
2 * flac.c : FLAC demuc module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: flac.c,v 1.5 2003/09/07 22:48:29 fenrir Exp $
7 * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <string.h> /* strdup() */
32 #include <vlc/input.h>
34 #include <sys/types.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 #define FLAC_PACKET_SIZE 16384
40 #define MAX_PACKETS_IN_FIFO 1
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Init ( vlc_object_t * );
46 static int Demux ( input_thread_t * );
48 /*****************************************************************************
50 *****************************************************************************/
52 set_description( _("flac demuxer") );
53 set_capability( "demux", 155 );
54 set_callbacks( Init, NULL );
55 add_shortcut( "flac" );
58 /*****************************************************************************
59 * Init: initializes ES structures
60 *****************************************************************************/
61 static int Init( vlc_object_t * p_this )
63 input_thread_t * p_input = (input_thread_t *)p_this;
64 es_descriptor_t * p_es;
67 /* Initialize access plug-in structures. */
68 if( p_input->i_mtu == 0 )
71 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
74 p_input->pf_demux = Demux;
75 p_input->pf_demux_control = demux_vaControlDefault;
76 p_input->pf_rewind = NULL;
78 /* Have a peep at the show. */
79 if( input_Peek( p_input, &p_peek, 4 ) < 4 )
81 /* Stream shorter than 4 bytes... */
82 msg_Err( p_input, "cannot peek()" );
86 if( *p_peek != 'f' || *(p_peek + 1) != 'L' || *(p_peek +2) != 'a'
87 || *(p_peek+3) != 'C')
89 if( *p_input->psz_demux && !strncmp( p_input->psz_demux, "flac", 4 ) )
92 msg_Err( p_input, "this doesn't look like an flac stream, "
93 "continuing anyway" );
97 msg_Warn( p_input, "flac module discarded (no startcode)" );
102 if( input_InitStream( p_input, 0 ) == -1 )
106 input_AddProgram( p_input, 0, 0 );
107 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
108 vlc_mutex_lock( &p_input->stream.stream_lock );
109 p_es = input_AddES( p_input, p_input->stream.p_selected_program, 1,
111 p_es->i_stream_id = 1;
112 p_es->i_fourcc = VLC_FOURCC('f','l','a','c');
113 input_SelectES( p_input, p_es );
114 p_input->stream.p_selected_program->b_is_ok = 1;
115 vlc_mutex_unlock( &p_input->stream.stream_lock );
120 /*****************************************************************************
121 * Demux: reads and demuxes data packets
122 *****************************************************************************
123 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
124 *****************************************************************************/
125 static int Demux( input_thread_t * p_input )
128 decoder_fifo_t * p_fifo =
129 p_input->stream.p_selected_program->pp_es[0]->p_decoder_fifo;
130 pes_packet_t * p_pes;
131 data_packet_t * p_data;
138 i_read = input_SplitBuffer( p_input, &p_data, FLAC_PACKET_SIZE );
145 p_pes = input_NewPES( p_input->p_method_data );
149 msg_Err( p_input, "out of memory" );
150 input_DeletePacket( p_input->p_method_data, p_data );
154 p_pes->i_rate = p_input->stream.control.i_rate;
155 p_pes->p_first = p_pes->p_last = p_data;
156 p_pes->i_pes_size = i_read;
157 p_pes->i_nb_data = 1;
159 vlc_mutex_lock( &p_fifo->data_lock );
160 if( p_fifo->i_depth >= MAX_PACKETS_IN_FIFO )
162 /* Wait for the decoder. */
163 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
165 vlc_mutex_unlock( &p_fifo->data_lock );
167 if( (p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT)
168 |(p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_START)
169 | (input_ClockManageControl( p_input,
170 p_input->stream.p_selected_program,
171 (mtime_t)0 ) == PAUSE_S) )
173 msg_Warn( p_input, "synchro reinit" );
174 p_pes->i_pts = mdate() + DEFAULT_PTS_DELAY;
175 p_input->stream.p_selected_program->i_synchro_state = SYNCHRO_OK;
178 input_DecodePES( p_fifo, p_pes );