1 /*****************************************************************************
2 * m4v.c : MPEG-4 video Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: m4v.c,v 1.2 2003/01/20 13:06:34 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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() */
31 #include <vlc/input.h>
33 /*****************************************************************************
35 *****************************************************************************/
36 static int Activate ( vlc_object_t * );
37 static int Demux ( input_thread_t * );
39 /*****************************************************************************
41 *****************************************************************************/
43 set_description( _("MPEG-4 video elementary stream demux" ) );
44 set_capability( "demux", 0 );
45 set_callbacks( Activate, NULL );
46 add_shortcut( "m4v" );
49 /*****************************************************************************
50 * Definitions of structures and functions used by this plugins
51 *****************************************************************************/
57 es_descriptor_t *p_es;
61 /*****************************************************************************
62 * Activate: initializes MPEGaudio structures
63 *****************************************************************************/
64 static int Activate( vlc_object_t * p_this )
66 input_thread_t * p_input = (input_thread_t *)p_this;
67 demux_sys_t * p_demux;
68 input_info_category_t * p_category;
72 /* Set the demux function */
73 p_input->pf_demux = Demux;
75 /* Initialize access plug-in structures. */
76 if( p_input->i_mtu == 0 )
79 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
82 /* Have a peep at the show. */
83 if( input_Peek( p_input, &p_peek, 4 ) < 4 )
85 /* Stream shorter than 4 bytes... */
86 msg_Err( p_input, "cannot peek()" );
90 if( p_peek[0] != 0x00 || p_peek[1] != 0x00 || p_peek[2] != 0x01 || p_peek[3] > 0x2f )
92 if( *p_input->psz_demux && !strncmp( p_input->psz_demux, "m4v", 3 ) )
95 msg_Warn( p_input, "this doesn't look like an MPEG-4 ES stream, continuing" );
99 msg_Warn( p_input, "m4v module discarded (invalid startcode)" );
104 /* create p_demux and init it */
105 if( !( p_demux = p_input->p_demux_data = malloc( sizeof(demux_sys_t) ) ) )
107 msg_Err( p_input, "out of memory" );
110 memset( p_demux, 0, sizeof(demux_sys_t) );
113 vlc_mutex_lock( &p_input->stream.stream_lock );
114 if( input_InitStream( p_input, 0 ) == -1)
116 msg_Err( p_input, "cannot init stream" );
117 free( p_input->p_demux_data );
120 if( input_AddProgram( p_input, 0, 0) == NULL )
122 msg_Err( p_input, "cannot add program" );
123 free( p_input->p_demux_data );
126 p_input->stream.pp_programs[0]->b_is_ok = 0;
127 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
130 p_demux->p_es = input_AddES( p_input,
131 p_input->stream.p_selected_program,
136 vlc_mutex_unlock( &p_input->stream.stream_lock );
137 msg_Err( p_input, "out of memory" );
138 free( p_input->p_demux_data );
141 p_demux->p_es->i_stream_id = 1;
142 p_demux->p_es->i_fourcc = VLC_FOURCC('m','p','4','v');
143 p_demux->p_es->i_cat = VIDEO_ES;
145 input_SelectES( p_input, p_demux->p_es );
147 p_input->stream.p_selected_program->b_is_ok = 1;
148 vlc_mutex_unlock( &p_input->stream.stream_lock );
150 vlc_mutex_lock( &p_input->stream.stream_lock );
151 p_category = input_InfoCategory( p_input, "mpeg" );
152 input_AddInfo( p_category, "input type", "video MPEG-4 (raw ES)" );
153 vlc_mutex_unlock( &p_input->stream.stream_lock );
158 /*****************************************************************************
159 * Demux: reads and demuxes data packets
160 *****************************************************************************
161 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
162 *****************************************************************************/
163 static int FindStartCode( uint8_t *p_data, int i_size, uint8_t i_startcode, uint8_t i_mask )
167 for( i_pos = 0; i_size >= 4; i_pos++,i_size--,p_data++ )
169 if( p_data[0] == 0 && p_data[1] == 0 &&
170 p_data[2] == 1 && ( p_data[3]&i_mask) == i_startcode )
178 static void PESAddDataPacket( pes_packet_t *p_pes, data_packet_t *p_data )
180 if( !p_pes->p_first )
182 p_pes->p_first = p_data;
183 p_pes->i_nb_data = 1;
184 p_pes->i_pes_size = p_data->p_payload_end - p_data->p_payload_start;
188 p_pes->p_last->p_next = p_data;
190 p_pes->i_pes_size += p_data->p_payload_end - p_data->p_payload_start;
192 p_pes->p_last = p_data;
195 static int Demux( input_thread_t * p_input )
197 demux_sys_t *p_demux = p_input->p_demux_data;
199 data_packet_t *p_data;
206 input_ClockManageRef( p_input,
207 p_input->stream.p_selected_program,
210 if( (p_pes = input_NewPES( p_input->p_method_data ) ) == NULL )
212 msg_Err( p_input, "cannot allocate new PES" );
216 /* we will read data into this pes until we found a vop header */
219 /* Have a peep at the show. */
220 if( ( i_peek = input_Peek( p_input, &p_peek, 512 ) ) < 5 )
222 /* Stream shorter than 4 bytes... */
223 msg_Warn( p_input, "cannot peek()" );
228 if( ( i_size = FindStartCode( p_peek, i_peek, 0xb6, 0xff ) ) == 0 )
233 if( ( i_read = input_SplitBuffer( p_input,
237 msg_Warn( p_input, "error while reading data" );
240 PESAddDataPacket( p_pes, p_data );
245 /* Have a peep at the show. */
246 if( ( i_peek = input_Peek( p_input, &p_peek, 512 ) ) < 5 )
248 /* Stream shorter than 4 bytes... */
249 msg_Warn( p_input, "cannot peek()" );
255 i_size = FindStartCode( p_peek + 1, i_peek - 1, 0x00, 0x00 ) + 1;
257 i_size = FindStartCode( p_peek, i_peek, 0x00, 0x00 );
265 if( ( i_read = input_SplitBuffer( p_input,
269 msg_Warn( p_input, "error while reading data" );
272 PESAddDataPacket( p_pes, p_data );
276 p_pes->i_pts = input_ClockGetTS( p_input,
277 p_input->stream.p_selected_program,
280 if( !p_demux->p_es->p_decoder_fifo )
282 msg_Err( p_input, "no video decoder" );
283 input_DeletePES( p_input->p_method_data, p_pes );
288 input_DecodePES( p_demux->p_es->p_decoder_fifo, p_pes );
290 /* FIXME FIXME FIXME FIXME */
291 p_demux->i_dts += (mtime_t)90000 / 25;
292 /* FIXME FIXME FIXME FIXME */