]> git.sesse.net Git - vlc/blob - modules/demux/a52sys.c
* ALL: i18n updates and fixes.
[vlc] / modules / demux / a52sys.c
1 /*****************************************************************************
2  * a52sys.c : A/52 input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: a52sys.c,v 1.2 2003/02/27 13:19:43 gbazin Exp $
6  *
7  * Authors: Arnaud de Bossoreille de Ribou <bozo@via.ecp.fr>
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 <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>                                              /* strdup() */
29 #include <errno.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/input.h>
33
34 #include <sys/types.h>
35
36 /*****************************************************************************
37  * Constants
38  *****************************************************************************/
39 #define A52_PACKET_SIZE 16384
40 #define MAX_PACKETS_IN_FIFO 3
41
42 /*****************************************************************************
43  * Local prototypes
44  *****************************************************************************/
45 static int  Init  ( vlc_object_t * );
46 static int  Demux ( input_thread_t * );
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 vlc_module_begin();                                      
52     set_description( _("A52 demuxer") );                       
53     set_capability( "demux", 155 );
54     set_callbacks( Init, NULL );
55     add_shortcut( "a52" );
56 vlc_module_end();
57
58 /*****************************************************************************
59  * Init: initializes ES structures
60  *****************************************************************************/
61 static int Init( vlc_object_t * p_this )
62 {
63     input_thread_t *    p_input = (input_thread_t *)p_this;
64     es_descriptor_t *   p_es;
65     byte_t *            p_peek;
66
67     /* Initialize access plug-in structures. */
68     if( p_input->i_mtu == 0 )
69     {
70         /* Improve speed. */
71         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
72     }
73
74     p_input->pf_demux = Demux;
75     p_input->pf_rewind = NULL;
76
77     /* Have a peep at the show. */
78     if( input_Peek( p_input, &p_peek, 2 ) < 2 )
79     {
80         /* Stream shorter than 4 bytes... */
81         msg_Err( p_input, "cannot peek()" );
82         return( -1 );
83     }
84
85     if( *p_peek != 0x0b || *(p_peek + 1) != 0x77 )
86     {
87         if( *p_input->psz_demux && !strncmp( p_input->psz_demux, "a52", 3 ) )
88         {
89             /* User forced */
90             msg_Err( p_input, "this doesn't look like an a52 stream, continuing" );
91         }
92         else
93         {
94             msg_Warn( p_input, "a52 module discarded (no startcode)" );
95             return( -1 );
96         }
97     }
98
99     if( input_InitStream( p_input, 0 ) == -1 )
100     {
101         return( -1 );
102     }
103     input_AddProgram( p_input, 0, 0 );
104     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
105     vlc_mutex_lock( &p_input->stream.stream_lock );
106     p_es = input_AddES( p_input, p_input->stream.p_selected_program, 0xBD, 0 );
107     p_es->i_stream_id = 0xBD;
108     p_es->i_fourcc = VLC_FOURCC('a','5','2',' ');
109     p_es->i_cat = AUDIO_ES;
110     input_SelectES( p_input, p_es );
111     p_input->stream.p_selected_area->i_tell = 0;
112     p_input->stream.p_selected_program->b_is_ok = 1;
113     vlc_mutex_unlock( &p_input->stream.stream_lock );
114
115     return( 0 );
116 }
117
118 /*****************************************************************************
119  * Demux: reads and demuxes data packets
120  *****************************************************************************
121  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
122  *****************************************************************************/
123 static int Demux( input_thread_t * p_input )
124 {
125     ssize_t         i_read;
126     decoder_fifo_t * p_fifo =
127         p_input->stream.p_selected_program->pp_es[0]->p_decoder_fifo;
128     pes_packet_t *  p_pes;
129     data_packet_t * p_data;
130
131     if( p_fifo == NULL )
132     {
133         return -1;
134     }
135
136     i_read = input_SplitBuffer( p_input, &p_data, A52_PACKET_SIZE );
137
138     if ( i_read <= 0 )
139     {
140         return i_read;
141     }
142
143     p_pes = input_NewPES( p_input->p_method_data );
144
145     if( p_pes == NULL )
146     {
147         msg_Err( p_input, "out of memory" );
148         input_DeletePacket( p_input->p_method_data, p_data );
149         return( -1 );
150     }
151
152     p_pes->i_rate = p_input->stream.control.i_rate;
153     p_pes->p_first = p_pes->p_last = p_data;
154     p_pes->i_nb_data = 1;
155
156     vlc_mutex_lock( &p_fifo->data_lock );
157     if( p_fifo->i_depth >= MAX_PACKETS_IN_FIFO )
158     {
159         /* Wait for the decoder. */
160         vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
161     }
162     vlc_mutex_unlock( &p_fifo->data_lock );
163
164     if( (p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT)
165        |(p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_START)
166          | (input_ClockManageControl( p_input, 
167                       p_input->stream.p_selected_program,
168                          (mtime_t)0 ) == PAUSE_S) )
169     {
170         msg_Warn( p_input, "synchro reinit" );
171         p_pes->i_pts = mdate() + DEFAULT_PTS_DELAY;
172         p_input->stream.p_selected_program->i_synchro_state = SYNCHRO_OK;
173     }
174
175     input_DecodePES( p_fifo, p_pes );
176
177     return 1;
178 }
179