]> git.sesse.net Git - vlc/blob - modules/demux/pva.c
Use gettext_noop() consistently
[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_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             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
298             if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
299             {
300                 return VLC_EGENERIC;
301             }
302             return VLC_SUCCESS;
303
304 #if 0
305         case DEMUX_GET_TIME:
306             pi64 = (int64_t*)va_arg( args, int64_t * );
307             if( p_sys->i_time < 0 )
308             {
309                 *pi64 = 0;
310                 return VLC_EGENERIC;
311             }
312             *pi64 = p_sys->i_time;
313             return VLC_SUCCESS;
314
315 #if 0
316         case DEMUX_GET_LENGTH:
317             pi64 = (int64_t*)va_arg( args, int64_t * );
318             if( p_sys->i_mux_rate > 0 )
319             {
320                 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
321                 return VLC_SUCCESS;
322             }
323             *pi64 = 0;
324             return VLC_EGENERIC;
325
326 #endif
327         case DEMUX_GET_FPS:
328             pf = (double*)va_arg( args, double * );
329             *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
330             return VLC_SUCCESS;
331 #endif
332         case DEMUX_SET_TIME:
333         default:
334             return VLC_EGENERIC;
335     }
336 }
337
338 /*****************************************************************************
339  * ReSynch:
340  *****************************************************************************/
341 static int ReSynch( demux_t *p_demux )
342 {
343     const uint8_t *p_peek;
344     int      i_skip;
345     int      i_peek;
346
347     while( !p_demux->b_die )
348     {
349         if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 1024 ) ) < 8 )
350         {
351             return VLC_EGENERIC;
352         }
353         i_skip = 0;
354
355         while( i_skip < i_peek - 5 )
356         {
357             if( p_peek[0] == 'A' && p_peek[1] == 'V' && p_peek[4] == 0x55 )
358             {
359                 if( i_skip > 0 )
360                 {
361                     stream_Read( p_demux->s, NULL, i_skip );
362                 }
363                 return VLC_SUCCESS;
364             }
365             p_peek++;
366             i_skip++;
367         }
368
369         stream_Read( p_demux->s, NULL, i_skip );
370     }
371
372     return VLC_EGENERIC;
373 }
374
375 static void ParsePES( demux_t *p_demux )
376 {
377     demux_sys_t *p_sys = p_demux->p_sys;
378     block_t     *p_pes = p_sys->p_pes;
379     uint8_t     hdr[30];
380     int         i_pes_size;
381
382     int         i_skip;
383     mtime_t     i_dts = -1;
384     mtime_t     i_pts = -1;
385
386     p_sys->p_pes = NULL;
387
388     /* FIXME find real max size */
389     block_ChainExtract( p_pes, hdr, 30 );
390
391     if( hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 1 )
392     {
393         msg_Warn( p_demux, "invalid hdr [0x%2.2x:%2.2x:%2.2x:%2.2x]",
394                   hdr[0], hdr[1],hdr[2],hdr[3] );
395         block_ChainRelease( p_pes );
396         return;
397     }
398     i_pes_size = GetWBE( &hdr[4] );
399
400     /* we assume mpeg2 PES */
401     i_skip = hdr[8] + 9;
402     if( hdr[7]&0x80 )    /* has pts */
403     {
404         i_pts = ((mtime_t)(hdr[ 9]&0x0e ) << 29)|
405                  (mtime_t)(hdr[10] << 22)|
406                 ((mtime_t)(hdr[11]&0xfe) << 14)|
407                  (mtime_t)(hdr[12] << 7)|
408                  (mtime_t)(hdr[12] >> 1);
409
410         if( hdr[7]&0x40 )    /* has dts */
411         {
412              i_dts = ((mtime_t)(hdr[14]&0x0e ) << 29)|
413                      (mtime_t)(hdr[15] << 22)|
414                     ((mtime_t)(hdr[16]&0xfe) << 14)|
415                      (mtime_t)(hdr[17] << 7)|
416                      (mtime_t)(hdr[18] >> 1);
417         }
418     }
419
420     p_pes = block_ChainGather( p_pes );
421     if( p_pes->i_buffer <= i_skip )
422     {
423         block_ChainRelease( p_pes );
424         return;
425     }
426
427     p_pes->i_buffer -= i_skip;
428     p_pes->p_buffer += i_skip;
429
430     if( i_dts >= 0 ) p_pes->i_dts = i_dts * 100 / 9;
431     if( i_pts >= 0 ) p_pes->i_pts = i_pts * 100 / 9;
432
433     /* Set PCR */
434     if( p_pes->i_pts > 0 )
435     {
436         es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_pes->i_pts);
437         p_sys->b_pcr_audio = true;
438     }
439     es_out_Send( p_demux->out, p_sys->p_audio, p_pes );
440 }
441