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