]> git.sesse.net Git - vlc/blob - modules/demux/pva.c
MKV: code factorisation around extra_data filling
[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_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_CODEC_MPGA );
102     p_sys->p_audio = es_out_Add( p_demux->out, &fmt );
103
104     es_format_Init( &fmt, VIDEO_ES, VLC_CODEC_MPGV );
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 > VLC_TS_INVALID && !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 )
225                     p_frame->i_pts = VLC_TS_0 + i_pts * 100 / 9;
226                 block_ChainAppend( &p_sys->p_es, p_frame );
227             }
228             break;
229
230         case 0x02:  /* MainAudioStream */
231             if( p_sys->i_ac < 0 )
232             {
233                 msg_Dbg( p_demux, "first packet for audio" );
234             }
235             else if( ((p_sys->i_ac + 1)&0xff) != p_peek[3] )
236             {
237                 msg_Dbg( p_demux, "packet lost (audio)" );
238                 if( p_sys->p_pes )
239                 {
240                     block_ChainRelease( p_sys->p_pes );
241                     p_sys->p_pes = NULL;
242                 }
243             }
244             p_sys->i_ac = p_peek[3];
245
246             if( p_peek[5]&0x10 && p_sys->p_pes )
247             {
248                 ParsePES( p_demux );
249             }
250             if( ( p_frame = stream_Block( p_demux->s, i_size + 8 ) ) )
251             {
252                 p_frame->p_buffer += 8;
253                 p_frame->i_buffer -= 8;
254                 /* XXX this a hack, some streams aren't compliant and
255                  * doesn't set pes_start flag */
256                 if( p_sys->p_pes && p_frame->i_buffer > 4 &&
257                     p_frame->p_buffer[0] == 0x00 &&
258                     p_frame->p_buffer[1] == 0x00 &&
259                     p_frame->p_buffer[2] == 0x01 )
260                 {
261                     ParsePES( p_demux );
262                 }
263                 block_ChainAppend( &p_sys->p_pes, p_frame );
264             }
265             break;
266
267         default:
268             msg_Warn( p_demux, "unknown id=0x%x", p_peek[2] );
269             stream_Read( p_demux->s, NULL, i_size + 8 );
270             break;
271     }
272     return 1;
273 }
274
275 /*****************************************************************************
276  * Control:
277  *****************************************************************************/
278 static int Control( demux_t *p_demux, int i_query, va_list args )
279 {
280     /* demux_sys_t *p_sys = p_demux->p_sys; */
281     double f, *pf;
282     int64_t i64;
283     switch( i_query )
284     {
285         case DEMUX_GET_POSITION:
286             if( ( i64 = stream_Size( p_demux->s ) ) > 0 )
287             {
288                 pf = (double*) va_arg( args, double* );
289                 double current = stream_Tell( p_demux->s );
290                 *pf = current / (double)i64;
291                 return VLC_SUCCESS;
292             }
293             return VLC_EGENERIC;
294
295         case DEMUX_SET_POSITION:
296             f = (double) va_arg( args, double );
297             i64 = stream_Size( p_demux->s );
298
299             if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
300             {
301                 return VLC_EGENERIC;
302             }
303             return VLC_SUCCESS;
304
305 #if 0
306         case DEMUX_GET_TIME:
307             pi64 = (int64_t*)va_arg( args, int64_t * );
308             if( p_sys->i_time < 0 )
309             {
310                 *pi64 = 0;
311                 return VLC_EGENERIC;
312             }
313             *pi64 = p_sys->i_time;
314             return VLC_SUCCESS;
315
316 #if 0
317         case DEMUX_GET_LENGTH:
318             pi64 = (int64_t*)va_arg( args, int64_t * );
319             if( p_sys->i_mux_rate > 0 )
320             {
321                 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
322                 return VLC_SUCCESS;
323             }
324             *pi64 = 0;
325             return VLC_EGENERIC;
326
327 #endif
328         case DEMUX_GET_FPS:
329             pf = (double*)va_arg( args, double * );
330             *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
331             return VLC_SUCCESS;
332 #endif
333         case DEMUX_SET_TIME:
334         default:
335             return VLC_EGENERIC;
336     }
337 }
338
339 /*****************************************************************************
340  * ReSynch:
341  *****************************************************************************/
342 static int ReSynch( demux_t *p_demux )
343 {
344     const uint8_t *p_peek;
345     int      i_skip;
346     int      i_peek;
347
348     while( vlc_object_alive (p_demux) )
349     {
350         if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 1024 ) ) < 8 )
351         {
352             return VLC_EGENERIC;
353         }
354         i_skip = 0;
355
356         while( i_skip < i_peek - 5 )
357         {
358             if( p_peek[0] == 'A' && p_peek[1] == 'V' && p_peek[4] == 0x55 )
359             {
360                 if( i_skip > 0 )
361                 {
362                     stream_Read( p_demux->s, NULL, i_skip );
363                 }
364                 return VLC_SUCCESS;
365             }
366             p_peek++;
367             i_skip++;
368         }
369
370         stream_Read( p_demux->s, NULL, i_skip );
371     }
372
373     return VLC_EGENERIC;
374 }
375
376 static void ParsePES( demux_t *p_demux )
377 {
378     demux_sys_t *p_sys = p_demux->p_sys;
379     block_t     *p_pes = p_sys->p_pes;
380     uint8_t     hdr[30];
381     int         i_pes_size;
382
383     unsigned    i_skip;
384     mtime_t     i_dts = -1;
385     mtime_t     i_pts = -1;
386
387     p_sys->p_pes = NULL;
388
389     /* FIXME find real max size */
390     block_ChainExtract( p_pes, hdr, 30 );
391
392     if( hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 1 )
393     {
394         msg_Warn( p_demux, "invalid hdr [0x%2.2x:%2.2x:%2.2x:%2.2x]",
395                   hdr[0], hdr[1],hdr[2],hdr[3] );
396         block_ChainRelease( p_pes );
397         return;
398     }
399     i_pes_size = GetWBE( &hdr[4] );
400
401     /* we assume mpeg2 PES */
402     i_skip = hdr[8] + 9;
403     if( hdr[7]&0x80 )    /* has pts */
404     {
405         i_pts = ((mtime_t)(hdr[ 9]&0x0e ) << 29)|
406                  (mtime_t)(hdr[10] << 22)|
407                 ((mtime_t)(hdr[11]&0xfe) << 14)|
408                  (mtime_t)(hdr[12] << 7)|
409                  (mtime_t)(hdr[12] >> 1);
410
411         if( hdr[7]&0x40 )    /* has dts */
412         {
413              i_dts = ((mtime_t)(hdr[14]&0x0e ) << 29)|
414                      (mtime_t)(hdr[15] << 22)|
415                     ((mtime_t)(hdr[16]&0xfe) << 14)|
416                      (mtime_t)(hdr[17] << 7)|
417                      (mtime_t)(hdr[18] >> 1);
418         }
419     }
420
421     p_pes = block_ChainGather( p_pes );
422     if( p_pes->i_buffer <= i_skip )
423     {
424         block_ChainRelease( p_pes );
425         return;
426     }
427
428     p_pes->i_buffer -= i_skip;
429     p_pes->p_buffer += i_skip;
430
431     if( i_dts >= 0 )
432         p_pes->i_dts = VLC_TS_0 + i_dts * 100 / 9;
433     if( i_pts >= 0 )
434         p_pes->i_pts = VLC_TS_0 + i_pts * 100 / 9;
435
436     /* Set PCR */
437     if( p_pes->i_pts > 0 )
438     {
439         es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_pes->i_pts);
440         p_sys->b_pcr_audio = true;
441     }
442     es_out_Send( p_demux->out, p_sys->p_audio, p_pes );
443 }
444