]> git.sesse.net Git - vlc/blob - modules/demux/mpeg/m4v.c
* all : demuxers *have to* set pf_demux_control. (demux_vaControlDefault
[vlc] / modules / demux / mpeg / m4v.c
1 /*****************************************************************************
2  * m4v.c : MPEG-4 video Stream input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: m4v.c,v 1.7 2003/09/07 22:48:29 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@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>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 /*****************************************************************************
34  * Local prototypes
35  *****************************************************************************/
36 static int  Activate ( vlc_object_t * );
37 static int  Demux ( input_thread_t * );
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 vlc_module_begin();
43     set_description( _("MPEG-4 video elementary stream demuxer" ) );
44     set_capability( "demux", 0 );
45     set_callbacks( Activate, NULL );
46     add_shortcut( "m4v" );
47 vlc_module_end();
48
49 /*****************************************************************************
50  * Definitions of structures  and functions used by this plugins 
51  *****************************************************************************/
52
53 struct demux_sys_t
54 {
55     mtime_t i_dts;
56
57     es_descriptor_t *p_es;
58 };
59
60
61 /*****************************************************************************
62  * Activate: initializes MPEGaudio structures
63  *****************************************************************************/
64 static int Activate( vlc_object_t * p_this )
65 {
66     input_thread_t * p_input = (input_thread_t *)p_this;
67     demux_sys_t * p_demux;
68     input_info_category_t * p_category;
69
70     uint8_t *p_peek;
71
72     /* Set the demux function */
73     p_input->pf_demux = Demux;
74     p_input->pf_demux_control = demux_vaControlDefault;
75
76     /* Initialize access plug-in structures. */
77     if( p_input->i_mtu == 0 )
78     {
79         /* Improve speed. */
80         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
81     }
82
83     /* Have a peep at the show. */
84     if( input_Peek( p_input, &p_peek, 4 ) < 4 )
85     {
86         /* Stream shorter than 4 bytes... */
87         msg_Err( p_input, "cannot peek()" );
88         return( -1 );
89     }
90
91     if( p_peek[0] != 0x00 || p_peek[1] != 0x00 || p_peek[2] != 0x01 || p_peek[3] > 0x2f )
92     {
93         if( *p_input->psz_demux && !strncmp( p_input->psz_demux, "m4v", 3 ) )
94         {
95             /* user forced */
96             msg_Warn( p_input, "this doesn't look like an MPEG-4 ES stream, continuing" );
97         }
98         else
99         {
100             msg_Warn( p_input, "m4v module discarded (invalid startcode)" );
101             return( -1 );
102         }
103     }
104
105     /* create p_demux and init it */
106     if( !( p_demux = p_input->p_demux_data = malloc( sizeof(demux_sys_t) ) ) )
107     {
108         msg_Err( p_input, "out of memory" );
109         return( -1 );
110     }
111     memset( p_demux, 0, sizeof(demux_sys_t) );
112     p_demux->i_dts = 0;
113
114     vlc_mutex_lock( &p_input->stream.stream_lock );
115     if( input_InitStream( p_input, 0 ) == -1)
116     {
117         msg_Err( p_input, "cannot init stream" );
118         free( p_input->p_demux_data );
119         return( -1 );
120     }
121     if( input_AddProgram( p_input, 0, 0) == NULL )
122     {
123         msg_Err( p_input, "cannot add program" );
124         free( p_input->p_demux_data );
125         return( -1 );
126     }
127     p_input->stream.pp_programs[0]->b_is_ok = 0;
128     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
129
130     /* create our ES */
131     p_demux->p_es = input_AddES( p_input,
132                                  p_input->stream.p_selected_program,
133                                  1 /* id */, VIDEO_ES, NULL, 0 );
134     if( !p_demux->p_es )
135     {
136         vlc_mutex_unlock( &p_input->stream.stream_lock );
137         msg_Err( p_input, "out of memory" );
138         free( p_input->p_demux_data );
139         return( -1 );
140     }
141     p_demux->p_es->i_stream_id = 1;
142     p_demux->p_es->i_fourcc = VLC_FOURCC('m','p','4','v');
143
144     input_SelectES( p_input, p_demux->p_es );
145
146     p_input->stream.p_selected_program->b_is_ok = 1;
147     vlc_mutex_unlock( &p_input->stream.stream_lock );
148
149     vlc_mutex_lock( &p_input->stream.stream_lock );
150     p_category = input_InfoCategory( p_input, _("mpeg") );
151     input_AddInfo( p_category, _("Input Type"), _("Video MPEG-4 (raw ES)") );
152     vlc_mutex_unlock( &p_input->stream.stream_lock );
153
154     return( 0 );
155 }
156
157 /*****************************************************************************
158  * Demux: reads and demuxes data packets
159  *****************************************************************************
160  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
161  *****************************************************************************/
162 static int FindStartCode( uint8_t *p_data, int i_size, uint8_t i_startcode, uint8_t i_mask )
163 {
164     int i_pos = 0;
165
166     for( i_pos = 0; i_size >= 4; i_pos++,i_size--,p_data++ )
167     {
168         if( p_data[0] == 0 && p_data[1] == 0 && 
169             p_data[2] == 1 && ( p_data[3]&i_mask) == i_startcode )
170         {
171             return( i_pos );
172         }
173     }
174     return( i_pos );
175 }
176
177 static void PESAddDataPacket( pes_packet_t *p_pes, data_packet_t *p_data )
178 {
179     if( !p_pes->p_first )
180     {
181         p_pes->p_first = p_data;
182         p_pes->i_nb_data = 1;
183         p_pes->i_pes_size = p_data->p_payload_end - p_data->p_payload_start;
184     }
185     else
186     {
187         p_pes->p_last->p_next  = p_data;
188         p_pes->i_nb_data++;
189         p_pes->i_pes_size += p_data->p_payload_end - p_data->p_payload_start;
190     }
191     p_pes->p_last  = p_data;
192 }
193
194 static int Demux( input_thread_t * p_input )
195 {
196     demux_sys_t  *p_demux = p_input->p_demux_data;
197     pes_packet_t *p_pes;
198     data_packet_t *p_data;
199     uint8_t *p_peek;
200     int     i_peek;
201     int     i_size;
202     int     i_read;
203     int     b_vop;
204
205     input_ClockManageRef( p_input,
206                           p_input->stream.p_selected_program,
207                           p_demux->i_dts );
208
209     if( (p_pes = input_NewPES( p_input->p_method_data ) ) == NULL )
210     {
211         msg_Err( p_input, "cannot allocate new PES" );
212         return( -1 );
213     }
214
215     /* we will read data into this pes until we found a vop header */
216     for( ; ; )
217     {
218         /* Have a peep at the show. */
219         if( ( i_peek = input_Peek( p_input, &p_peek, 512 ) ) < 5 )
220         {
221             /* Stream shorter than 4 bytes... */
222             msg_Warn( p_input, "cannot peek()" );
223             return( 0 );
224         }
225
226         /* vop startcode */
227         if( ( i_size = FindStartCode( p_peek, i_peek, 0xb6, 0xff ) ) == 0 )
228         {
229             break;
230         }
231
232         if( ( i_read = input_SplitBuffer( p_input,
233                                           &p_data,
234                                           i_size ) ) <= 0 )
235         {
236             msg_Warn( p_input, "error while reading data" );
237             break;
238         }
239         PESAddDataPacket( p_pes, p_data );
240     }
241     b_vop = 1;
242     for( ; ; )
243     {
244         /* Have a peep at the show. */
245         if( ( i_peek = input_Peek( p_input, &p_peek, 512 ) ) < 5 )
246         {
247             /* Stream shorter than 4 bytes... */
248             msg_Warn( p_input, "cannot peek()" );
249             return( 0 );
250         }
251
252         /* vop startcode */
253         if( b_vop )
254             i_size = FindStartCode( p_peek + 1, i_peek - 1, 0x00, 0x00 ) + 1;
255         else
256             i_size = FindStartCode( p_peek, i_peek, 0x00, 0x00 );
257         b_vop = 0;
258
259         if( i_size == 0 )
260         {
261             break;
262         }
263
264         if( ( i_read = input_SplitBuffer( p_input,
265                                           &p_data,
266                                           i_size ) ) <= 0 )
267         {
268             msg_Warn( p_input, "error while reading data" );
269             break;
270         }
271         PESAddDataPacket( p_pes, p_data );
272     }
273
274     p_pes->i_dts =
275         p_pes->i_pts = input_ClockGetTS( p_input,
276                                          p_input->stream.p_selected_program,
277                                          p_demux->i_dts );
278
279     if( !p_demux->p_es->p_decoder_fifo )
280     {
281         msg_Err( p_input, "no video decoder" );
282         input_DeletePES( p_input->p_method_data, p_pes );
283         return( -1 );
284     }
285     else
286     {
287         input_DecodePES( p_demux->p_es->p_decoder_fifo, p_pes );
288     }
289     /* FIXME FIXME FIXME FIXME */
290     p_demux->i_dts += (mtime_t)90000 / 25;
291     /* FIXME FIXME FIXME FIXME */
292
293     return( 1 );
294 }
295
296