]> git.sesse.net Git - vlc/blob - modules/demux/mpeg/mpgv.c
* ALL: changed the prototype of module_Need() to accept a "strict" boolean argument.
[vlc] / modules / demux / mpeg / mpgv.c
1 /*****************************************************************************
2  * mpgv.c : MPEG-I/II Video demuxer
3  *****************************************************************************
4  * Copyright (C) 2001-2003 VideoLAN
5  * $Id: mpgv.c,v 1.6 2004/03/03 20:39:52 gbazin 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 #include "vlc_codec.h"
32
33 /*****************************************************************************
34  * Module descriptor
35  *****************************************************************************/
36 static int  Open    ( vlc_object_t * );
37 static void Close  ( vlc_object_t * );
38
39 vlc_module_begin();
40     set_description( _("MPEG-I/II video demuxer" ) );
41     set_capability( "demux", 100 );
42     set_callbacks( Open, Close );
43     add_shortcut( "mpgv" );
44 vlc_module_end();
45
46 /* TODO:
47  * - free bitrate
48  */
49
50 /*****************************************************************************
51  * Local prototypes
52  *****************************************************************************/
53 static int  Demux       ( input_thread_t * );
54
55 struct demux_sys_t
56 {
57     vlc_bool_t  b_start;
58
59     es_out_id_t *p_es;
60
61     decoder_t *p_packetizer;
62 };
63
64 #define MPGV_PACKET_SIZE 4096
65
66 /*****************************************************************************
67  * Open: initializes demux structures
68  *****************************************************************************/
69 static int Open( vlc_object_t * p_this )
70 {
71     input_thread_t *p_input = (input_thread_t *)p_this;
72     demux_sys_t    *p_sys;
73     vlc_bool_t     b_forced = VLC_FALSE;
74
75     uint8_t        *p_peek;
76
77     es_format_t    fmt;
78     char psz_description[50];
79
80     if( stream_Peek( p_input->s, &p_peek, 4 ) < 4 )
81     {
82         msg_Err( p_input, "cannot peek" );
83         return VLC_EGENERIC;
84     }
85
86     if( p_input->psz_demux && !strncmp( p_input->psz_demux, "mpgv", 4 ) )
87     {
88         b_forced = VLC_TRUE;
89     }
90
91     if( p_peek[0] != 0x00 || p_peek[1] != 0x00 || p_peek[2] != 0x01 )
92     {
93         if( !b_forced )
94         {
95             msg_Warn( p_input, "ES module discarded (no startcode)" );
96             return VLC_EGENERIC;
97         }
98
99         msg_Err( p_input, "this doesn't look like an MPEG ES stream, continuing" );
100     }
101
102     if( p_peek[3] > 0xb9 )
103     {
104         if( !b_forced )
105         {
106             msg_Warn( p_input, "ES module discarded (system startcode)" );
107             return VLC_EGENERIC;
108         }
109         msg_Err( p_input, "this seems to be a system stream (PS plug-in ?), but continuing" );
110     }
111
112     vlc_mutex_lock( &p_input->stream.stream_lock );
113     if( input_InitStream( p_input, 0 ) == -1)
114     {
115         vlc_mutex_unlock( &p_input->stream.stream_lock );
116         msg_Err( p_input, "cannot init stream" );
117         return VLC_EGENERIC;
118     }
119     p_input->stream.i_mux_rate = 0 / 50;
120     vlc_mutex_unlock( &p_input->stream.stream_lock );
121
122     p_input->pf_demux  = Demux;
123     p_input->pf_rewind = NULL;
124     p_input->pf_demux_control = demux_vaControlDefault;
125
126     p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
127     p_sys->b_start = VLC_TRUE;
128     p_sys->p_es    = NULL;
129
130     /*
131      * Load the mpegvideo packetizer
132      */
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,
139                     VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
140     es_format_Init( &p_sys->p_packetizer->fmt_out, UNKNOWN_ES, 0 );
141     p_sys->p_packetizer->p_module =
142         module_Need( p_sys->p_packetizer, "packetizer", NULL, 0 );
143
144     if( p_sys->p_packetizer->p_module == NULL)
145     {
146         vlc_object_destroy( p_sys->p_packetizer );
147         msg_Err( p_input, "cannot find mpgv packetizer" );
148         free( p_sys );
149         return VLC_EGENERIC;
150     }
151
152     /*
153      * create the output
154      */
155     es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
156     sprintf( psz_description, "MPEG-I/II Video" );
157     fmt.psz_description = strdup( psz_description );
158     p_sys->p_es = es_out_Add( p_input->p_es_out, &fmt );
159
160     return VLC_SUCCESS;
161 }
162
163 /*****************************************************************************
164  * Close: frees unused data
165  *****************************************************************************/
166 static void Close( vlc_object_t * p_this )
167 {
168     input_thread_t *p_input = (input_thread_t*)p_this;
169     demux_sys_t    *p_sys = p_input->p_demux_data;
170
171     module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
172     vlc_object_destroy( p_sys->p_packetizer );
173
174     free( p_sys );
175 }
176
177 /*****************************************************************************
178  * Demux: reads and demuxes data packets
179  *****************************************************************************
180  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
181  *****************************************************************************/
182 static int Demux( input_thread_t * p_input )
183 {
184     demux_sys_t  *p_sys = p_input->p_demux_data;
185     block_t *p_block_in, *p_block_out;
186
187     if( ( p_block_in = stream_Block( p_input->s, MPGV_PACKET_SIZE ) ) == NULL )
188     {
189         return 0;
190     }
191
192     if( p_sys->b_start )
193     {
194         p_block_in->i_pts =
195         p_block_in->i_dts = 1;
196     }
197     else
198     {
199         p_block_in->i_pts =
200         p_block_in->i_dts = 0;
201     }
202
203     while( (p_block_out = p_sys->p_packetizer->pf_packetize( p_sys->p_packetizer, &p_block_in )) )
204     {
205         p_sys->b_start = VLC_FALSE;
206
207         while( p_block_out )
208         {
209             block_t *p_next = p_block_out->p_next;
210
211             input_ClockManageRef( p_input,
212                                   p_input->stream.p_selected_program,
213                                   p_block_out->i_dts * 9 / 100 );
214
215             p_block_out->i_dts =
216                 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
217                                   p_block_out->i_dts * 9 / 100 );
218             if( p_block_out->i_pts > 0 )
219             {
220                 p_block_out->i_pts =
221                     input_ClockGetTS( p_input, p_input->stream.p_selected_program,
222                                       p_block_out->i_pts * 9 / 100 );
223             }
224             else
225             {
226                 p_block_out->i_pts = 0;
227             }
228
229             p_block_out->p_next = NULL;
230             es_out_Send( p_input->p_es_out, p_sys->p_es, p_block_out );
231
232             p_block_out = p_next;
233         }
234     }
235     return 1;
236 }
237