Fixed invalid/useless usage of ES_OUT_RESET_PCR.
[vlc.git] / 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_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
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( N_("PVA demuxer" ) );
44     set_capability( "demux", 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     const uint8_t *p_peek;
86
87     if( stream_Peek( p_demux->s, &p_peek, 5 ) < 5 ) return VLC_EGENERIC;
88     if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
89     {
90         /* In case we had forced this demuxer we try to resynch */
91         if( !p_demux->b_force || ReSynch( p_demux ) )
92             return VLC_EGENERIC;
93     }
94
95     /* Fill p_demux field */
96     p_demux->pf_demux = Demux;
97     p_demux->pf_control = Control;
98     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
99
100     /* Register one audio and one video stream */
101     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
102     p_sys->p_audio = es_out_Add( p_demux->out, &fmt );
103
104     es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
105     p_sys->p_video = es_out_Add( p_demux->out, &fmt );
106
107     p_sys->i_vc    = -1;
108     p_sys->i_ac    = -1;
109     p_sys->p_pes   = NULL;
110     p_sys->p_es    = NULL;
111
112     p_sys->b_pcr_audio = false;
113
114     return VLC_SUCCESS;
115 }
116
117 /*****************************************************************************
118  * Close
119  *****************************************************************************/
120 static void Close( vlc_object_t *p_this )
121 {
122     demux_t     *p_demux = (demux_t*)p_this;
123     demux_sys_t *p_sys = p_demux->p_sys;
124
125     if( p_sys->p_es )  block_Release( p_sys->p_es );
126     if( p_sys->p_pes ) block_Release( p_sys->p_pes );
127
128     free( p_sys );
129 }
130
131
132 /*****************************************************************************
133  * Demux:
134  *****************************************************************************/
135 static int Demux( demux_t *p_demux )
136 {
137     demux_sys_t *p_sys = p_demux->p_sys;
138
139     const uint8_t *p_peek;
140     int         i_size;
141     block_t     *p_frame;
142     int64_t     i_pts;
143     int         i_skip;
144
145     if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
146     {
147         msg_Warn( p_demux, "eof ?" );
148         return 0;
149     }
150     if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
151     {
152         msg_Warn( p_demux, "lost synchro" );
153         if( ReSynch( p_demux ) )
154         {
155             return -1;
156         }
157         if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
158         {
159             msg_Warn( p_demux, "eof ?" );
160             return 0;
161         }
162     }
163
164     i_size = GetWBE( &p_peek[6] );
165     switch( p_peek[2] )
166     {
167         case 0x01:  /* VideoStream */
168             if( p_sys->i_vc < 0 )
169             {
170                 msg_Dbg( p_demux, "first packet for video" );
171             }
172             else if( ((p_sys->i_vc + 1)&0xff) != p_peek[3] )
173             {
174                 msg_Dbg( p_demux, "packet lost (video)" );
175                 if( p_sys->p_es )
176                 {
177                     block_ChainRelease( p_sys->p_es );
178                     p_sys->p_es = NULL;
179                 }
180             }
181             p_sys->i_vc = p_peek[3];
182
183             /* read the PTS and potential extra bytes TODO: make it a bit more optimised */
184             i_pts = -1;
185             i_skip = 8;
186             if( p_peek[5]&0x10 )
187             {
188                 int i_pre = p_peek[5]&0x3;
189
190                 if( ( p_frame = stream_Block( p_demux->s, 8 + 4 + i_pre ) ) )
191                 {
192                     i_pts = GetDWBE( &p_frame->p_buffer[8] );
193                     if( p_frame->i_buffer > 12 )
194                     {
195                         p_frame->p_buffer += 12;
196                         p_frame->i_buffer -= 12;
197                         block_ChainAppend( &p_sys->p_es, p_frame );
198                     }
199                     else
200                     {
201                         block_Release( p_frame );
202                     }
203                 }
204                 i_size -= 4 + i_pre;
205                 i_skip  = 0;
206                 /* Set PCR */
207                 if( ( p_frame = p_sys->p_es ) )
208                 {
209
210                     if( p_frame->i_pts > 0 && !p_sys->b_pcr_audio )
211                     {
212                         es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_frame->i_pts);
213                     }
214                     es_out_Send( p_demux->out, p_sys->p_video, p_frame );
215
216                     p_sys->p_es = NULL;
217                 }
218             }
219
220             if( ( p_frame = stream_Block( p_demux->s, i_size + i_skip ) ) )
221             {
222                 p_frame->p_buffer += i_skip;
223                 p_frame->i_buffer -= i_skip;
224                 if( i_pts > 0 ) p_frame->i_pts = i_pts * 100 / 9;
225                 block_ChainAppend( &p_sys->p_es, p_frame );
226             }
227             break;
228
229         case 0x02:  /* MainAudioStream */
230             if( p_sys->i_ac < 0 )
231             {
232                 msg_Dbg( p_demux, "first packet for audio" );
233             }
234             else if( ((p_sys->i_ac + 1)&0xff) != p_peek[3] )
235             {
236                 msg_Dbg( p_demux, "packet lost (audio)" );
237                 if( p_sys->p_pes )
238                 {
239                     block_ChainRelease( p_sys->p_pes );
240                     p_sys->p_pes = NULL;
241                 }
242             }
243             p_sys->i_ac = p_peek[3];
244
245             if( p_peek[5]&0x10 && p_sys->p_pes )
246             {
247                 ParsePES( p_demux );
248             }
249             if( ( p_frame = stream_Block( p_demux->s, i_size + 8 ) ) )
250             {
251                 p_frame->p_buffer += 8;
252                 p_frame->i_buffer -= 8;
253                 /* XXX this a hack, some streams aren't compliant and
254                  * doesn't set pes_start flag */
255                 if( p_sys->p_pes && p_frame->i_buffer > 4 &&
256                     p_frame->p_buffer[0] == 0x00 &&
257                     p_frame->p_buffer[1] == 0x00 &&
258                     p_frame->p_buffer[2] == 0x01 )
259                 {
260                     ParsePES( p_demux );
261                 }
262                 block_ChainAppend( &p_sys->p_pes, p_frame );
263             }
264             break;
265
266         default:
267             msg_Warn( p_demux, "unknown id=0x%x", p_peek[2] );
268             stream_Read( p_demux->s, NULL, i_size + 8 );
269             break;
270     }
271     return 1;
272 }
273
274 /*****************************************************************************
275  * Control:
276  *****************************************************************************/
277 static int Control( demux_t *p_demux, int i_query, va_list args )
278 {
279     /* demux_sys_t *p_sys = p_demux->p_sys; */
280     double f, *pf;
281     int64_t i64;
282     switch( i_query )
283     {
284         case DEMUX_GET_POSITION:
285             if( ( i64 = stream_Size( p_demux->s ) ) > 0 )
286             {
287                 pf = (double*) va_arg( args, double* );
288                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
289                 return VLC_SUCCESS;
290             }
291             return VLC_EGENERIC;
292
293         case DEMUX_SET_POSITION:
294             f = (double) va_arg( args, double );
295             i64 = stream_Size( p_demux->s );
296
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( vlc_object_alive (p_demux) )
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