]> git.sesse.net Git - vlc/blob - modules/demux/mpeg/mpgv.c
2b962466697d25fe41d341e8d635a66944f1ed33
[vlc] / modules / demux / mpeg / mpgv.c
1 /*****************************************************************************
2  * mpgv.c : MPEG-I/II Video demuxer
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: mpgv.c,v 1.4 2003/12/22 02:24:52 sam 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,   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 );
142
143     if( p_sys->p_packetizer->p_module == NULL)
144     {
145         vlc_object_destroy( p_sys->p_packetizer );
146         msg_Err( p_input, "cannot find mpgv packetizer" );
147         free( p_sys );
148         return VLC_EGENERIC;
149     }
150
151     /*
152      * create the output
153      */
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 );
158
159     return VLC_SUCCESS;
160 }
161
162 /*****************************************************************************
163  * Close: frees unused data
164  *****************************************************************************/
165 static void Close( vlc_object_t * p_this )
166 {
167     input_thread_t *p_input = (input_thread_t*)p_this;
168     demux_sys_t    *p_sys = p_input->p_demux_data;
169
170     module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
171     vlc_object_destroy( p_sys->p_packetizer );
172
173     free( p_sys );
174 }
175
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 )
182 {
183     demux_sys_t  *p_sys = p_input->p_demux_data;
184     block_t *p_block_in, *p_block_out;
185
186     if( ( p_block_in = stream_Block( p_input->s, MPGV_PACKET_SIZE ) ) == NULL )
187     {
188         return 0;
189     }
190
191     if( p_sys->b_start )
192     {
193         p_block_in->i_pts =
194         p_block_in->i_dts = 1;
195     }
196     else
197     {
198         p_block_in->i_pts =
199         p_block_in->i_dts = 0;
200     }
201
202     while( (p_block_out = p_sys->p_packetizer->pf_packetize( p_sys->p_packetizer, &p_block_in )) )
203     {
204         p_sys->b_start = VLC_FALSE;
205
206         while( p_block_out )
207         {
208             block_t *p_next = p_block_out->p_next;
209
210             input_ClockManageRef( p_input,
211                                   p_input->stream.p_selected_program,
212                                   p_block_out->i_dts * 9 / 100 );
213
214             p_block_out->i_dts =
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 )
218             {
219                 p_block_out->i_pts =
220                     input_ClockGetTS( p_input, p_input->stream.p_selected_program,
221                                       p_block_out->i_pts * 9 / 100 );
222             }
223             else
224             {
225                 p_block_out->i_pts = 0;
226             }
227
228             p_block_out->p_next = NULL;
229             es_out_Send( p_input->p_es_out, p_sys->p_es, p_block_out );
230
231             p_block_out = p_next;
232         }
233     }
234     return 1;
235 }
236