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