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