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