1 /*****************************************************************************
2 * mpgv.c : MPEG-I/II Video demuxer
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: mpgv.c,v 1.5 2004/01/25 20:05:28 hartman 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() */
30 #include <vlc/input.h>
31 #include "vlc_codec.h"
33 /*****************************************************************************
35 *****************************************************************************/
36 static int Open ( vlc_object_t * );
37 static void Close ( vlc_object_t * );
40 set_description( _("MPEG-I/II video demuxer" ) );
41 set_capability( "demux", 100 );
42 set_callbacks( Open, Close );
43 add_shortcut( "mpgv" );
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Demux ( input_thread_t * );
61 decoder_t *p_packetizer;
64 #define MPGV_PACKET_SIZE 4096
66 /*****************************************************************************
67 * Open: initializes demux structures
68 *****************************************************************************/
69 static int Open( vlc_object_t * p_this )
71 input_thread_t *p_input = (input_thread_t *)p_this;
73 vlc_bool_t b_forced = VLC_FALSE;
78 char psz_description[50];
80 if( stream_Peek( p_input->s, &p_peek, 4 ) < 4 )
82 msg_Err( p_input, "cannot peek" );
86 if( p_input->psz_demux && !strncmp( p_input->psz_demux, "mpgv", 4 ) )
91 if( p_peek[0] != 0x00 || p_peek[1] != 0x00 || p_peek[2] != 0x01 )
95 msg_Warn( p_input, "ES module discarded (no startcode)" );
99 msg_Err( p_input, "this doesn't look like an MPEG ES stream, continuing" );
102 if( p_peek[3] > 0xb9 )
106 msg_Warn( p_input, "ES module discarded (system startcode)" );
109 msg_Err( p_input, "this seems to be a system stream (PS plug-in ?), but continuing" );
112 vlc_mutex_lock( &p_input->stream.stream_lock );
113 if( input_InitStream( p_input, 0 ) == -1)
115 vlc_mutex_unlock( &p_input->stream.stream_lock );
116 msg_Err( p_input, "cannot init stream" );
119 p_input->stream.i_mux_rate = 0 / 50;
120 vlc_mutex_unlock( &p_input->stream.stream_lock );
122 p_input->pf_demux = Demux;
123 p_input->pf_rewind = NULL;
124 p_input->pf_demux_control = demux_vaControlDefault;
126 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
127 p_sys->b_start = VLC_TRUE;
131 * Load the mpegvideo packetizer
133 p_sys->p_packetizer = vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
134 p_sys->p_packetizer->pf_decode_audio = NULL;
135 p_sys->p_packetizer->pf_decode_video = NULL;
136 p_sys->p_packetizer->pf_decode_sub = NULL;
137 p_sys->p_packetizer->pf_packetize = NULL;
138 es_format_Init( &p_sys->p_packetizer->fmt_in, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
139 es_format_Init( &p_sys->p_packetizer->fmt_out, UNKNOWN_ES, 0 );
140 p_sys->p_packetizer->p_module =
141 module_Need( p_sys->p_packetizer, "packetizer", NULL );
143 if( p_sys->p_packetizer->p_module == NULL)
145 vlc_object_destroy( p_sys->p_packetizer );
146 msg_Err( p_input, "cannot find mpgv packetizer" );
154 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
155 sprintf( psz_description, "MPEG-I/II Video" );
156 fmt.psz_description = strdup( psz_description );
157 p_sys->p_es = es_out_Add( p_input->p_es_out, &fmt );
162 /*****************************************************************************
163 * Close: frees unused data
164 *****************************************************************************/
165 static void Close( vlc_object_t * p_this )
167 input_thread_t *p_input = (input_thread_t*)p_this;
168 demux_sys_t *p_sys = p_input->p_demux_data;
170 module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
171 vlc_object_destroy( p_sys->p_packetizer );
176 /*****************************************************************************
177 * Demux: reads and demuxes data packets
178 *****************************************************************************
179 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
180 *****************************************************************************/
181 static int Demux( input_thread_t * p_input )
183 demux_sys_t *p_sys = p_input->p_demux_data;
184 block_t *p_block_in, *p_block_out;
186 if( ( p_block_in = stream_Block( p_input->s, MPGV_PACKET_SIZE ) ) == NULL )
194 p_block_in->i_dts = 1;
199 p_block_in->i_dts = 0;
202 while( (p_block_out = p_sys->p_packetizer->pf_packetize( p_sys->p_packetizer, &p_block_in )) )
204 p_sys->b_start = VLC_FALSE;
208 block_t *p_next = p_block_out->p_next;
210 input_ClockManageRef( p_input,
211 p_input->stream.p_selected_program,
212 p_block_out->i_dts * 9 / 100 );
215 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
216 p_block_out->i_dts * 9 / 100 );
217 if( p_block_out->i_pts > 0 )
220 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
221 p_block_out->i_pts * 9 / 100 );
225 p_block_out->i_pts = 0;
228 p_block_out->p_next = NULL;
229 es_out_Send( p_input->p_es_out, p_sys->p_es, p_block_out );
231 p_block_out = p_next;