]> git.sesse.net Git - vlc/blob - modules/demux/pva.c
Added const wheen needed for stream_Peek (demuxer/access)
[vlc] / modules / demux / pva.c
1 /*****************************************************************************
2  * pva.c: PVA demuxer
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
28 #include <vlc/vlc.h>
29 #include <vlc_demux.h>
30
31 /*****************************************************************************
32  * Module descriptor
33  *****************************************************************************/
34 static int  Open    ( vlc_object_t * );
35 static void Close  ( vlc_object_t * );
36
37 vlc_module_begin();
38     set_description( _("PVA demuxer" ) );
39     set_capability( "demux2", 10 );
40     set_category( CAT_INPUT );
41     set_subcategory( SUBCAT_INPUT_DEMUX );
42     set_callbacks( Open, Close );
43     add_shortcut( "pva" );
44 vlc_module_end();
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49
50 struct demux_sys_t
51 {
52     es_out_id_t *p_video;
53     es_out_id_t *p_audio;
54
55     /* counter */
56     int          i_vc;
57     int          i_ac;
58
59     /* audio/video block */
60     block_t     *p_pes; /* audio */
61     block_t     *p_es;  /* video */
62
63     int64_t     b_pcr_audio;
64 };
65
66 static int  Demux   ( demux_t *p_demux );
67 static int  Control ( demux_t *p_demux, int i_query, va_list args );
68
69 static int  ReSynch ( demux_t * );
70 static void ParsePES( demux_t * );
71
72 /*****************************************************************************
73  * Open
74  *****************************************************************************/
75 static int Open( vlc_object_t *p_this )
76 {
77     demux_t     *p_demux = (demux_t*)p_this;
78     demux_sys_t *p_sys;
79     es_format_t  fmt;
80     const uint8_t *p_peek;
81
82     if( stream_Peek( p_demux->s, &p_peek, 5 ) < 5 ) return VLC_EGENERIC;
83     if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
84     {
85         /* In case we had forced this demuxer we try to resynch */
86         if( !p_demux->b_force || ReSynch( p_demux ) )
87             return VLC_EGENERIC;
88     }
89
90     /* Fill p_demux field */
91     p_demux->pf_demux = Demux;
92     p_demux->pf_control = Control;
93     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
94
95     /* Register one audio and one video stream */
96     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
97     p_sys->p_audio = es_out_Add( p_demux->out, &fmt );
98
99     es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
100     p_sys->p_video = es_out_Add( p_demux->out, &fmt );
101
102     p_sys->i_vc    = -1;
103     p_sys->i_ac    = -1;
104     p_sys->p_pes   = NULL;
105     p_sys->p_es    = NULL;
106
107     p_sys->b_pcr_audio = VLC_FALSE;
108
109     return VLC_SUCCESS;
110 }
111
112 /*****************************************************************************
113  * Close
114  *****************************************************************************/
115 static void Close( vlc_object_t *p_this )
116 {
117     demux_t     *p_demux = (demux_t*)p_this;
118     demux_sys_t *p_sys = p_demux->p_sys;
119
120     if( p_sys->p_es )  block_Release( p_sys->p_es );
121     if( p_sys->p_pes ) block_Release( p_sys->p_pes );
122
123     free( p_sys );
124 }
125
126
127 /*****************************************************************************
128  * Demux:
129  *****************************************************************************/
130 static int Demux( demux_t *p_demux )
131 {
132     demux_sys_t *p_sys = p_demux->p_sys;
133
134     const uint8_t *p_peek;
135     int         i_size;
136     block_t     *p_frame;
137     int64_t     i_pts;
138     int         i_skip;
139
140     if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
141     {
142         msg_Warn( p_demux, "eof ?" );
143         return 0;
144     }
145     if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
146     {
147         msg_Warn( p_demux, "lost synchro" );
148         if( ReSynch( p_demux ) )
149         {
150             return -1;
151         }
152         if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
153         {
154             msg_Warn( p_demux, "eof ?" );
155             return 0;
156         }
157     }
158
159     i_size = GetWBE( &p_peek[6] );
160     switch( p_peek[2] )
161     {
162         case 0x01:  /* VideoStream */
163             if( p_sys->i_vc < 0 )
164             {
165                 msg_Dbg( p_demux, "first packet for video" );
166             }
167             else if( ((p_sys->i_vc + 1)&0xff) != p_peek[3] )
168             {
169                 msg_Dbg( p_demux, "packet lost (video)" );
170                 if( p_sys->p_es )
171                 {
172                     block_ChainRelease( p_sys->p_es );
173                     p_sys->p_es = NULL;
174                 }
175             }
176             p_sys->i_vc = p_peek[3];
177
178             /* read the PTS and potential extra bytes TODO: make it a bit more optimised */
179             i_pts = -1;
180             i_skip = 8;
181             if( p_peek[5]&0x10 )
182             {
183                 int i_pre = p_peek[5]&0x3;
184
185                 if( ( p_frame = stream_Block( p_demux->s, 8 + 4 + i_pre ) ) )
186                 {
187                     i_pts = GetDWBE( &p_frame->p_buffer[8] );
188                     if( p_frame->i_buffer > 12 )
189                     {
190                         p_frame->p_buffer += 12;
191                         p_frame->i_buffer -= 12;
192                         block_ChainAppend( &p_sys->p_es, p_frame );
193                     }
194                     else
195                     {
196                         block_Release( p_frame );
197                     }
198                 }
199                 i_size -= 4 + i_pre;
200                 i_skip  = 0;
201                 /* Set PCR */
202                 if( ( p_frame = p_sys->p_es ) )
203                 {
204
205                     if( p_frame->i_pts > 0 && !p_sys->b_pcr_audio )
206                     {
207                         es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_frame->i_pts);
208                     }
209                     es_out_Send( p_demux->out, p_sys->p_video, p_frame );
210
211                     p_sys->p_es = NULL;
212                 }
213             }
214
215             if( ( p_frame = stream_Block( p_demux->s, i_size + i_skip ) ) )
216             {
217                 p_frame->p_buffer += i_skip;
218                 p_frame->i_buffer -= i_skip;
219                 if( i_pts > 0 ) p_frame->i_pts = i_pts * 100 / 9;
220                 block_ChainAppend( &p_sys->p_es, p_frame );
221             }
222             break;
223
224         case 0x02:  /* MainAudioStream */
225             if( p_sys->i_ac < 0 )
226             {
227                 msg_Dbg( p_demux, "first packet for audio" );
228             }
229             else if( ((p_sys->i_ac + 1)&0xff) != p_peek[3] )
230             {
231                 msg_Dbg( p_demux, "packet lost (audio)" );
232                 if( p_sys->p_pes )
233                 {
234                     block_ChainRelease( p_sys->p_pes );
235                     p_sys->p_pes = NULL;
236                 }
237             }
238             p_sys->i_ac = p_peek[3];
239
240             if( p_peek[5]&0x10 && p_sys->p_pes )
241             {
242                 ParsePES( p_demux );
243             }
244             if( ( p_frame = stream_Block( p_demux->s, i_size + 8 ) ) )
245             {
246                 p_frame->p_buffer += 8;
247                 p_frame->i_buffer -= 8;
248                 /* XXX this a hack, some streams aren't compliant and
249                  * doesn't set pes_start flag */
250                 if( p_sys->p_pes && p_frame->i_buffer > 4 &&
251                     p_frame->p_buffer[0] == 0x00 &&
252                     p_frame->p_buffer[1] == 0x00 &&
253                     p_frame->p_buffer[2] == 0x01 )
254                 {
255                     ParsePES( p_demux );
256                 }
257                 block_ChainAppend( &p_sys->p_pes, p_frame );
258             }
259             break;
260
261         default:
262             msg_Warn( p_demux, "unknown id=0x%x", p_peek[2] );
263             stream_Read( p_demux->s, NULL, i_size + 8 );
264             break;
265     }
266     return 1;
267 }
268
269 /*****************************************************************************
270  * Control:
271  *****************************************************************************/
272 static int Control( demux_t *p_demux, int i_query, va_list args )
273 {
274     /* demux_sys_t *p_sys = p_demux->p_sys; */
275     double f, *pf;
276     int64_t i64;
277     switch( i_query )
278     {
279         case DEMUX_GET_POSITION:
280             if( ( i64 = stream_Size( p_demux->s ) ) > 0 )
281             {
282                 pf = (double*) va_arg( args, double* );
283                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
284                 return VLC_SUCCESS;
285             }
286             return VLC_EGENERIC;
287
288         case DEMUX_SET_POSITION:
289             f = (double) va_arg( args, double );
290             i64 = stream_Size( p_demux->s );
291
292             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
293             if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
294             {
295                 return VLC_EGENERIC;
296             }
297             return VLC_SUCCESS;
298
299 #if 0
300         case DEMUX_GET_TIME:
301             pi64 = (int64_t*)va_arg( args, int64_t * );
302             if( p_sys->i_time < 0 )
303             {
304                 *pi64 = 0;
305                 return VLC_EGENERIC;
306             }
307             *pi64 = p_sys->i_time;
308             return VLC_SUCCESS;
309
310 #if 0
311         case DEMUX_GET_LENGTH:
312             pi64 = (int64_t*)va_arg( args, int64_t * );
313             if( p_sys->i_mux_rate > 0 )
314             {
315                 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
316                 return VLC_SUCCESS;
317             }
318             *pi64 = 0;
319             return VLC_EGENERIC;
320
321 #endif
322         case DEMUX_GET_FPS:
323             pf = (double*)va_arg( args, double * );
324             *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
325             return VLC_SUCCESS;
326 #endif
327         case DEMUX_SET_TIME:
328         default:
329             return VLC_EGENERIC;
330     }
331 }
332
333 /*****************************************************************************
334  * ReSynch:
335  *****************************************************************************/
336 static int ReSynch( demux_t *p_demux )
337 {
338     const uint8_t *p_peek;
339     int      i_skip;
340     int      i_peek;
341
342     while( !p_demux->b_die )
343     {
344         if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 1024 ) ) < 8 )
345         {
346             return VLC_EGENERIC;
347         }
348         i_skip = 0;
349
350         while( i_skip < i_peek - 5 )
351         {
352             if( p_peek[0] == 'A' && p_peek[1] == 'V' && p_peek[4] == 0x55 )
353             {
354                 if( i_skip > 0 )
355                 {
356                     stream_Read( p_demux->s, NULL, i_skip );
357                 }
358                 return VLC_SUCCESS;
359             }
360             p_peek++;
361             i_skip++;
362         }
363
364         stream_Read( p_demux->s, NULL, i_skip );
365     }
366
367     return VLC_EGENERIC;
368 }
369
370 static void ParsePES( demux_t *p_demux )
371 {
372     demux_sys_t *p_sys = p_demux->p_sys;
373     block_t     *p_pes = p_sys->p_pes;
374     uint8_t     hdr[30];
375     int         i_pes_size;
376
377     int         i_skip;
378     mtime_t     i_dts = -1;
379     mtime_t     i_pts = -1;
380
381     p_sys->p_pes = NULL;
382
383     /* FIXME find real max size */
384     block_ChainExtract( p_pes, hdr, 30 );
385
386     if( hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 1 )
387     {
388         msg_Warn( p_demux, "invalid hdr [0x%2.2x:%2.2x:%2.2x:%2.2x]",
389                   hdr[0], hdr[1],hdr[2],hdr[3] );
390         block_ChainRelease( p_pes );
391         return;
392     }
393     i_pes_size = GetWBE( &hdr[4] );
394
395     /* we assume mpeg2 PES */
396     i_skip = hdr[8] + 9;
397     if( hdr[7]&0x80 )    /* has pts */
398     {
399         i_pts = ((mtime_t)(hdr[ 9]&0x0e ) << 29)|
400                  (mtime_t)(hdr[10] << 22)|
401                 ((mtime_t)(hdr[11]&0xfe) << 14)|
402                  (mtime_t)(hdr[12] << 7)|
403                  (mtime_t)(hdr[12] >> 1);
404
405         if( hdr[7]&0x40 )    /* has dts */
406         {
407              i_dts = ((mtime_t)(hdr[14]&0x0e ) << 29)|
408                      (mtime_t)(hdr[15] << 22)|
409                     ((mtime_t)(hdr[16]&0xfe) << 14)|
410                      (mtime_t)(hdr[17] << 7)|
411                      (mtime_t)(hdr[18] >> 1);
412         }
413     }
414
415     p_pes = block_ChainGather( p_pes );
416     if( p_pes->i_buffer <= i_skip )
417     {
418         block_ChainRelease( p_pes );
419         return;
420     }
421
422     p_pes->i_buffer -= i_skip;
423     p_pes->p_buffer += i_skip;
424
425     if( i_dts >= 0 ) p_pes->i_dts = i_dts * 100 / 9;
426     if( i_pts >= 0 ) p_pes->i_pts = i_pts * 100 / 9;
427
428     /* Set PCR */
429     if( p_pes->i_pts > 0 )
430     {
431         es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_pes->i_pts);
432         p_sys->b_pcr_audio = VLC_TRUE;
433     }
434     es_out_Send( p_demux->out, p_sys->p_audio, p_pes );
435 }
436