]> git.sesse.net Git - vlc/blob - modules/demux/ps.c
* Fake the PCR/SCR on MPEG PES streams (VDR for instance).
[vlc] / modules / demux / ps.c
1 /*****************************************************************************
2  * ps.c: Program Stream demux module for VLC.
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 #include "ps.h"
33
34 /* TODO:
35  *  - re-add pre-scanning.
36  *  - ...
37  */
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 static int  Open   ( vlc_object_t * );
43 static int  OpenAlt( vlc_object_t * );
44 static void Close  ( vlc_object_t * );
45
46 vlc_module_begin();
47     set_description( _("PS demuxer") );
48     set_category( CAT_INPUT );
49     set_subcategory( SUBCAT_INPUT_DEMUX );
50     set_capability( "demux2", 1 );
51     set_callbacks( Open, Close );
52     add_shortcut( "ps" );
53
54     add_submodule();
55     set_description( _("PS demuxer") );
56     set_capability( "demux2", 9 );
57     set_callbacks( OpenAlt, Close );
58 vlc_module_end();
59
60 /*****************************************************************************
61  * Local prototypes
62  *****************************************************************************/
63
64 struct demux_sys_t
65 {
66     ps_psm_t    psm;
67     ps_track_t  tk[PS_TK_COUNT];
68
69     int64_t     i_scr;
70     int         i_mux_rate;
71
72     vlc_bool_t  b_lost_sync;
73     vlc_bool_t  b_have_pack;
74 };
75
76 static int Demux  ( demux_t *p_demux );
77 static int Control( demux_t *p_demux, int i_query, va_list args );
78
79 static int      ps_pkt_resynch( stream_t *, uint32_t *pi_code );
80 static block_t *ps_pkt_read   ( stream_t *, uint32_t i_code );
81
82 /*****************************************************************************
83  * Open
84  *****************************************************************************/
85 static int Open( vlc_object_t *p_this )
86 {
87     demux_t     *p_demux = (demux_t*)p_this;
88     demux_sys_t *p_sys;
89
90     uint8_t     *p_peek;
91
92     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
93     {
94         msg_Err( p_demux, "cannot peek" );
95         return VLC_EGENERIC;
96     }
97
98     if( p_peek[0] != 0 || p_peek[1] != 0 ||
99         p_peek[2] != 1 || p_peek[3] < 0xb9 )
100     {
101         msg_Warn( p_demux, "this does not look like an MPEG PS stream, "
102                   "continuing anyway" );
103     }
104
105     /* Fill p_demux field */
106     p_demux->pf_demux = Demux;
107     p_demux->pf_control = Control;
108     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
109
110     /* Init p_sys */
111     p_sys->i_mux_rate = 0;
112     p_sys->i_scr      = -1;
113     p_sys->b_lost_sync = VLC_FALSE;
114     p_sys->b_have_pack = VLC_FALSE;
115
116     ps_psm_init( &p_sys->psm );
117     ps_track_init( p_sys->tk );
118
119     /* TODO prescanning of ES */
120
121     return VLC_SUCCESS;
122 }
123
124 static int OpenAlt( vlc_object_t *p_this )
125 {
126     demux_t *p_demux = (demux_t*)p_this;
127     uint8_t *p_peek;
128
129     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
130     {
131         msg_Err( p_demux, "cannot peek" );
132         return VLC_EGENERIC;
133     }
134
135     if( p_peek[0] != 0 || p_peek[1] != 0 ||
136         p_peek[2] != 1 || p_peek[3] < 0xb9 )
137     {
138         if( !p_demux->b_force ) return VLC_EGENERIC;
139     }
140
141     return Open( p_this );
142 }
143
144 /*****************************************************************************
145  * Close
146  *****************************************************************************/
147 static void Close( vlc_object_t *p_this )
148 {
149     demux_t     *p_demux = (demux_t*)p_this;
150     demux_sys_t *p_sys = p_demux->p_sys;
151     int i;
152
153     for( i = 0; i < PS_TK_COUNT; i++ )
154     {
155         ps_track_t *tk = &p_sys->tk[i];
156         if( tk->b_seen )
157         {
158             es_format_Clean( &tk->fmt );
159             if( tk->es ) es_out_Del( p_demux->out, tk->es );
160         }
161     }
162
163     ps_psm_destroy( &p_sys->psm );
164
165     free( p_sys );
166 }
167
168 /*****************************************************************************
169  * Demux:
170  *****************************************************************************/
171 static int Demux( demux_t *p_demux )
172 {
173     demux_sys_t *p_sys = p_demux->p_sys;
174     int i_ret, i_id, i_mux_rate;
175     uint32_t i_code;
176     block_t *p_pkt;
177
178     i_ret = ps_pkt_resynch( p_demux->s, &i_code );
179     if( i_ret < 0 )
180     {
181         return 0;
182     }
183     else if( i_ret == 0 )
184     {
185         if( !p_sys->b_lost_sync )
186             msg_Warn( p_demux, "garbage at input, trying to resync..." );
187
188         p_sys->b_lost_sync = VLC_TRUE;
189         return 1;
190     }
191
192     if( p_sys->b_lost_sync ) msg_Warn( p_demux, "found sync code" );
193     p_sys->b_lost_sync = VLC_FALSE;
194
195     if( ( p_pkt = ps_pkt_read( p_demux->s, i_code ) ) == NULL )
196     {
197         return 0;
198     }
199
200     switch( i_code )
201     {
202     case 0x1b9:
203         block_Release( p_pkt );
204         break;
205
206     case 0x1ba:
207         if( !ps_pkt_parse_pack( p_pkt, &p_sys->i_scr, &i_mux_rate ) )
208         {
209             if( !p_sys->b_have_pack ) p_sys->b_have_pack = VLC_TRUE;
210             /* done later on to work around bad vcd/svcd streams */
211             /* es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_scr ); */
212             if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
213         }
214         block_Release( p_pkt );
215         break;
216
217     case 0x1bb:
218         if( !ps_pkt_parse_system( p_pkt, &p_sys->psm, p_sys->tk ) )
219         {
220             int i;
221             for( i = 0; i < PS_TK_COUNT; i++ )
222             {
223                 ps_track_t *tk = &p_sys->tk[i];
224
225                 if( tk->b_seen && !tk->es && tk->fmt.i_cat != UNKNOWN_ES )
226                 {
227                     tk->es = es_out_Add( p_demux->out, &tk->fmt );
228                 }
229             }
230         }
231         block_Release( p_pkt );
232         break;
233
234     case 0x1bc:
235         if( p_sys->psm.i_version == 0xFFFF )
236             msg_Dbg( p_demux, "contains a PSM");
237
238         ps_psm_fill( &p_sys->psm, p_pkt, p_sys->tk, p_demux->out );
239         block_Release( p_pkt );
240         break;
241
242     default:
243         if( (i_id = ps_pkt_id( p_pkt )) >= 0xc0 )
244         {
245             vlc_bool_t b_new = VLC_FALSE;
246             ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
247
248             if( !tk->b_seen )
249             {
250                 if( !ps_track_fill( tk, &p_sys->psm, i_id ) )
251                 {
252                     tk->es = es_out_Add( p_demux->out, &tk->fmt );
253                     b_new = VLC_TRUE;
254                 }
255                 else
256                 {
257                     msg_Dbg( p_demux, "es id=0x%x format unknown", i_id );
258                 }
259                 tk->b_seen = VLC_TRUE;
260             }
261
262             /* The popular VCD/SVCD subtitling WinSubMux does not
263              * renumber the SCRs when merging subtitles into the PES */
264             if( tk->b_seen &&
265                 ( tk->fmt.i_codec == VLC_FOURCC('o','g','t',' ') ||
266                   tk->fmt.i_codec == VLC_FOURCC('c','v','d',' ') ) )
267             {
268                 p_sys->i_scr = -1;
269             }
270
271             if( p_sys->i_scr > 0 )
272                 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_scr );
273
274             p_sys->i_scr = -1;
275
276             if( tk->b_seen && tk->es &&
277                 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
278             {
279                 if( !b_new && !p_sys->b_have_pack && tk->fmt.i_cat == AUDIO_ES && p_pkt->i_pts > 0 )
280                 {
281                     /* A hack to sync the A/V on PES files. */
282                     msg_Dbg( p_demux, "force SCR: %lld", p_pkt->i_pts );
283                     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_pkt->i_pts );
284                 }
285
286                 es_out_Send( p_demux->out, tk->es, p_pkt );
287             }
288             else
289             {
290                 block_Release( p_pkt );
291             }
292         }
293         else
294         {
295             block_Release( p_pkt );
296         }
297         break;
298     }
299
300     return 1;
301 }
302
303 /*****************************************************************************
304  * Control:
305  *****************************************************************************/
306 static int Control( demux_t *p_demux, int i_query, va_list args )
307 {
308     demux_sys_t *p_sys = p_demux->p_sys;
309     double f, *pf;
310     int64_t i64, *pi64;
311
312     switch( i_query )
313     {
314         case DEMUX_GET_POSITION:
315             pf = (double*) va_arg( args, double* );
316             i64 = stream_Size( p_demux->s );
317             if( i64 > 0 )
318             {
319                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
320             }
321             else
322             {
323                 *pf = 0.0;
324             }
325             return VLC_SUCCESS;
326
327         case DEMUX_SET_POSITION:
328             f = (double) va_arg( args, double );
329             i64 = stream_Size( p_demux->s );
330
331             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
332
333             return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
334
335         case DEMUX_GET_TIME:
336             pi64 = (int64_t*)va_arg( args, int64_t * );
337             if( p_sys->i_mux_rate > 0 )
338             {
339                 *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) /
340                     p_sys->i_mux_rate;
341                 return VLC_SUCCESS;
342             }
343             *pi64 = 0;
344             return VLC_EGENERIC;
345
346         case DEMUX_GET_LENGTH:
347             pi64 = (int64_t*)va_arg( args, int64_t * );
348             if( p_sys->i_mux_rate > 0 )
349             {
350                 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) /
351                     p_sys->i_mux_rate;
352                 return VLC_SUCCESS;
353             }
354             *pi64 = 0;
355             return VLC_EGENERIC;
356
357         case DEMUX_SET_TIME:
358         case DEMUX_GET_FPS:
359         default:
360             return VLC_EGENERIC;
361     }
362 }
363
364 /*****************************************************************************
365  * Divers:
366  *****************************************************************************/
367
368 /* PSResynch: resynch on a system starcode
369  *  It doesn't skip more than 512 bytes
370  *  -1 -> error, 0 -> not synch, 1 -> ok
371  */
372 static int ps_pkt_resynch( stream_t *s, uint32_t *pi_code )
373 {
374     uint8_t *p_peek;
375     int     i_peek;
376     int     i_skip;
377
378     if( stream_Peek( s, &p_peek, 4 ) < 4 )
379     {
380         return -1;
381     }
382     if( p_peek[0] == 0 && p_peek[1] == 0 && p_peek[2] == 1 &&
383         p_peek[3] >= 0xb9 )
384     {
385         *pi_code = 0x100 | p_peek[3];
386         return 1;
387     }
388
389     if( ( i_peek = stream_Peek( s, &p_peek, 512 ) ) < 4 )
390     {
391         return -1;
392     }
393     i_skip = 0;
394
395     for( ;; )
396     {
397         if( i_peek < 4 )
398         {
399             break;
400         }
401         if( p_peek[0] == 0 && p_peek[1] == 0 && p_peek[2] == 1 &&
402             p_peek[3] >= 0xb9 )
403         {
404             *pi_code = 0x100 | p_peek[3];
405             return stream_Read( s, NULL, i_skip ) == i_skip ? 1 : -1;
406         }
407
408         p_peek++;
409         i_peek--;
410         i_skip++;
411     }
412     return stream_Read( s, NULL, i_skip ) == i_skip ? 0 : -1;
413 }
414
415 static block_t *ps_pkt_read( stream_t *s, uint32_t i_code )
416 {
417     uint8_t *p_peek;
418     int      i_peek = stream_Peek( s, &p_peek, 14 );
419     int      i_size = ps_pkt_size( p_peek, i_peek );
420
421     if( i_size <= 6 && p_peek[3] > 0xba )
422     {
423         /* Special case, search the next start code */
424         i_size = 6;
425         for( ;; )
426         {
427             i_peek = stream_Peek( s, &p_peek, i_size + 1024 );
428             if( i_peek <= i_size + 4 )
429             {
430                 return NULL;
431             }
432             while( i_size <= i_peek - 4 )
433             {
434                 if( p_peek[i_size] == 0x00 && p_peek[i_size+1] == 0x00 &&
435                     p_peek[i_size+2] == 0x01 && p_peek[i_size+3] >= 0xb9 )
436                 {
437                     return stream_Block( s, i_size );
438                 }
439                 i_size++;
440             }
441         }
442     }
443     else
444     {
445         /* Normal case */
446         return stream_Block( s, i_size );
447     }
448
449     return NULL;
450 }