]> git.sesse.net Git - vlc/blob - modules/demux/mpeg/es.c
* all : demuxers *have to* set pf_demux_control. (demux_vaControlDefault
[vlc] / modules / demux / mpeg / es.c
1 /*****************************************************************************
2  * mpeg_es.c : Elementary Stream input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: es.c,v 1.4 2003/09/07 22:48:29 fenrir Exp $
6  *
7  * Authors: Christophe Massiot <massiot@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
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 /*****************************************************************************
34  * Constants
35  *****************************************************************************/
36 #define ES_PACKET_SIZE 65536
37 #define MAX_PACKETS_IN_FIFO 3
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static int  Activate ( vlc_object_t * );
43 static int  Demux ( input_thread_t * );
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48 vlc_module_begin();
49     set_description( _("ISO 13818-1 MPEG Elementary Stream input") );
50     set_capability( "demux", 150 );
51     set_callbacks( Activate, NULL );
52     add_shortcut( "es" );
53 vlc_module_end();
54
55 /*
56  * Data reading functions
57  */
58
59 /*****************************************************************************
60  * Activate: initializes ES structures
61  *****************************************************************************/
62 static int Activate( vlc_object_t * p_this )
63 {
64     input_thread_t *    p_input = (input_thread_t *)p_this;
65     es_descriptor_t *   p_es;
66     byte_t *            p_peek;
67
68     /* Set the demux function */
69     p_input->pf_demux = Demux;
70     p_input->pf_demux_control = demux_vaControlDefault;
71
72     /* Initialize access plug-in structures. */
73     if( p_input->i_mtu == 0 )
74     {
75         /* Improve speed. */
76         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
77     }
78
79     /* Have a peep at the show. */
80     if( input_Peek( p_input, &p_peek, 4 ) < 4 )
81     {
82         /* Stream shorter than 4 bytes... */
83         msg_Err( p_input, "cannot peek()" );
84         return( -1 );
85     }
86
87     if( *p_peek || *(p_peek + 1) || *(p_peek + 2) != 1 )
88     {
89         if( *p_input->psz_demux && !strncmp( p_input->psz_demux, "es", 3 ) )
90         {
91             /* User forced */
92             msg_Err( p_input, "this doesn't look like an MPEG ES stream, continuing" );
93         }
94         else
95         {
96             msg_Warn( p_input, "ES module discarded (no startcode)" );
97             return( -1 );
98         }
99     }
100     else if( *(p_peek + 3) > 0xb9 )
101     {
102         if( *p_input->psz_demux && !strncmp( p_input->psz_demux, "es", 3 ) )
103         {
104             /* User forced */
105             msg_Err( p_input, "this seems to be a system stream (PS plug-in ?), but continuing" );
106         }
107         else
108         {
109             msg_Warn( p_input, "ES module discarded (system startcode)" );
110             return( -1 );
111         }
112     }
113
114     if( input_InitStream( p_input, 0 ) == -1 )
115     {
116         return( -1 );
117     }
118     input_AddProgram( p_input, 0, 0 );
119     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
120     vlc_mutex_lock( &p_input->stream.stream_lock );
121     p_es = input_AddES( p_input, p_input->stream.p_selected_program, 0xE0,
122                         VIDEO_ES, NULL, 0 );
123     p_es->i_stream_id = 0xE0;
124     p_es->i_fourcc = VLC_FOURCC('m','p','g','v');
125     input_SelectES( p_input, p_es );
126     p_input->stream.p_selected_area->i_tell = 0;
127     p_input->stream.p_selected_program->b_is_ok = 1;
128     vlc_mutex_unlock( &p_input->stream.stream_lock );
129
130     return( 0 );
131 }
132
133 /*****************************************************************************
134  * Demux: reads and demuxes data packets
135  *****************************************************************************
136  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
137  *****************************************************************************/
138 static int Demux( input_thread_t * p_input )
139 {
140     ssize_t         i_read;
141     decoder_fifo_t * p_fifo =
142         p_input->stream.p_selected_program->pp_es[0]->p_decoder_fifo;
143     pes_packet_t *  p_pes;
144     data_packet_t * p_data;
145
146     if( p_fifo == NULL )
147     {
148         return -1;
149     }
150
151     i_read = input_SplitBuffer( p_input, &p_data, ES_PACKET_SIZE );
152
153     if ( i_read <= 0 )
154     {
155         return i_read;
156     }
157
158     p_pes = input_NewPES( p_input->p_method_data );
159
160     if( p_pes == NULL )
161     {
162         msg_Err( p_input, "out of memory" );
163         input_DeletePacket( p_input->p_method_data, p_data );
164         return -1;
165     }
166
167     p_pes->i_rate = p_input->stream.control.i_rate;
168     p_pes->p_first = p_pes->p_last = p_data;
169     p_pes->i_nb_data = 1;
170
171     vlc_mutex_lock( &p_fifo->data_lock );
172
173     if( p_fifo->i_depth >= MAX_PACKETS_IN_FIFO )
174     {
175         /* Wait for the decoder. */
176         vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
177     }
178     vlc_mutex_unlock( &p_fifo->data_lock );
179
180     if( (p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT)
181          | (input_ClockManageControl( p_input, 
182                       p_input->stream.p_selected_program,
183                          (mtime_t)0 ) == PAUSE_S) )
184     {
185         msg_Warn( p_input, "synchro reinit" );
186         p_pes->i_pts = mdate() + DEFAULT_PTS_DELAY;
187         p_input->stream.p_selected_program->i_synchro_state = SYNCHRO_OK;
188     }
189
190     input_DecodePES( p_fifo, p_pes );
191
192     return 1;
193 }
194