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