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