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