1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc/input.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close ( vlc_object_t * );
43 set_description( _("PVA demuxer" ) );
44 set_capability( "demux2", 10 );
45 set_category( CAT_INPUT );
46 set_subcategory( SUBCAT_INPUT_DEMUX );
47 set_callbacks( Open, Close );
48 add_shortcut( "pva" );
51 /*****************************************************************************
53 *****************************************************************************/
64 /* audio/video block */
65 block_t *p_pes; /* audio */
66 block_t *p_es; /* video */
71 static int Demux ( demux_t *p_demux );
72 static int Control ( demux_t *p_demux, int i_query, va_list args );
74 static int ReSynch ( demux_t * );
75 static void ParsePES( demux_t * );
77 /*****************************************************************************
79 *****************************************************************************/
80 static int Open( vlc_object_t *p_this )
82 demux_t *p_demux = (demux_t*)p_this;
87 if( stream_Peek( p_demux->s, &p_peek, 5 ) < 5 )
89 msg_Err( p_demux, "cannot peek" );
92 if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
94 /* In case we had forced this demuxer we try to resynch */
95 if( strcasecmp( p_demux->psz_demux, "pva" ) || ReSynch( p_demux ) )
97 msg_Warn( p_demux, "PVA module discarded" );
102 /* Fill p_demux field */
103 p_demux->pf_demux = Demux;
104 p_demux->pf_control = Control;
105 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
107 /* Register one audio and one video stream */
108 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
109 p_sys->p_audio = es_out_Add( p_demux->out, &fmt );
111 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
112 p_sys->p_video = es_out_Add( p_demux->out, &fmt );
119 p_sys->b_pcr_audio = VLC_FALSE;
124 /*****************************************************************************
126 *****************************************************************************/
127 static void Close( vlc_object_t *p_this )
129 demux_t *p_demux = (demux_t*)p_this;
130 demux_sys_t *p_sys = p_demux->p_sys;
132 if( p_sys->p_es ) block_Release( p_sys->p_es );
133 if( p_sys->p_pes ) block_Release( p_sys->p_pes );
139 /*****************************************************************************
141 *****************************************************************************/
142 static int Demux( demux_t *p_demux )
144 demux_sys_t *p_sys = p_demux->p_sys;
152 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
154 msg_Warn( p_demux, "eof ?" );
157 if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
159 msg_Warn( p_demux, "lost synchro" );
160 if( ReSynch( p_demux ) )
164 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
166 msg_Warn( p_demux, "eof ?" );
171 i_size = GetWBE( &p_peek[6] );
174 case 0x01: /* VideoStream */
175 if( p_sys->i_vc < 0 )
177 msg_Dbg( p_demux, "first packet for video" );
179 else if( ((p_sys->i_vc + 1)&0xff) != p_peek[3] )
181 msg_Dbg( p_demux, "packet lost (video)" );
184 block_ChainRelease( p_sys->p_es );
188 p_sys->i_vc = p_peek[3];
190 /* read the PTS and potential extra bytes TODO: make it a bit more optimised */
195 int i_pre = p_peek[5]&0x3;
197 if( ( p_frame = stream_Block( p_demux->s, 8 + 4 + i_pre ) ) )
199 i_pts = GetDWBE( &p_frame->p_buffer[8] );
200 if( p_frame->i_buffer > 12 )
202 p_frame->p_buffer += 12;
203 p_frame->i_buffer -= 12;
204 block_ChainAppend( &p_sys->p_es, p_frame );
208 block_Release( p_frame );
214 if( ( p_frame = p_sys->p_es ) )
217 if( p_frame->i_pts > 0 && !p_sys->b_pcr_audio )
219 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_frame->i_pts);
221 es_out_Send( p_demux->out, p_sys->p_video, p_frame );
227 if( ( p_frame = stream_Block( p_demux->s, i_size + i_skip ) ) )
229 p_frame->p_buffer += i_skip;
230 p_frame->i_buffer -= i_skip;
231 if( i_pts > 0 ) p_frame->i_pts = i_pts * 100 / 9;
232 block_ChainAppend( &p_sys->p_es, p_frame );
236 case 0x02: /* MainAudioStream */
237 if( p_sys->i_ac < 0 )
239 msg_Dbg( p_demux, "first packet for audio" );
241 else if( ((p_sys->i_ac + 1)&0xff) != p_peek[3] )
243 msg_Dbg( p_demux, "packet lost (audio)" );
246 block_ChainRelease( p_sys->p_pes );
250 p_sys->i_ac = p_peek[3];
252 if( p_peek[5]&0x10 && p_sys->p_pes )
256 if( ( p_frame = stream_Block( p_demux->s, i_size + 8 ) ) )
258 p_frame->p_buffer += 8;
259 p_frame->i_buffer -= 8;
260 /* XXX this a hack, some streams aren't compliant and
261 * doesn't set pes_start flag */
262 if( p_sys->p_pes && p_frame->i_buffer > 4 &&
263 p_frame->p_buffer[0] == 0x00 &&
264 p_frame->p_buffer[1] == 0x00 &&
265 p_frame->p_buffer[2] == 0x01 )
269 block_ChainAppend( &p_sys->p_pes, p_frame );
274 msg_Warn( p_demux, "unknown id=0x%x", p_peek[2] );
275 stream_Read( p_demux->s, NULL, i_size + 8 );
281 /*****************************************************************************
283 *****************************************************************************/
284 static int Control( demux_t *p_demux, int i_query, va_list args )
286 /* demux_sys_t *p_sys = p_demux->p_sys; */
291 case DEMUX_GET_POSITION:
292 if( ( i64 = stream_Size( p_demux->s ) ) > 0 )
294 pf = (double*) va_arg( args, double* );
295 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
300 case DEMUX_SET_POSITION:
301 f = (double) va_arg( args, double );
302 i64 = stream_Size( p_demux->s );
304 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
305 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
313 pi64 = (int64_t*)va_arg( args, int64_t * );
314 if( p_sys->i_time < 0 )
319 *pi64 = p_sys->i_time;
323 case DEMUX_GET_LENGTH:
324 pi64 = (int64_t*)va_arg( args, int64_t * );
325 if( p_sys->i_mux_rate > 0 )
327 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
335 pf = (double*)va_arg( args, double * );
336 *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
345 /*****************************************************************************
347 *****************************************************************************/
348 static int ReSynch( demux_t *p_demux )
354 while( !p_demux->b_die )
356 if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 1024 ) ) < 8 )
362 while( i_skip < i_peek - 5 )
364 if( p_peek[0] == 'A' && p_peek[1] == 'V' && p_peek[4] == 0x55 )
368 stream_Read( p_demux->s, NULL, i_skip );
376 stream_Read( p_demux->s, NULL, i_skip );
382 static void ParsePES( demux_t *p_demux )
384 demux_sys_t *p_sys = p_demux->p_sys;
385 block_t *p_pes = p_sys->p_pes;
395 /* FIXME find real max size */
396 block_ChainExtract( p_pes, hdr, 30 );
398 if( hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 1 )
400 msg_Warn( p_demux, "invalid hdr [0x%2.2x:%2.2x:%2.2x:%2.2x]",
401 hdr[0], hdr[1],hdr[2],hdr[3] );
402 block_ChainRelease( p_pes );
405 i_pes_size = GetWBE( &hdr[4] );
407 /* we assume mpeg2 PES */
409 if( hdr[7]&0x80 ) /* has pts */
411 i_pts = ((mtime_t)(hdr[ 9]&0x0e ) << 29)|
412 (mtime_t)(hdr[10] << 22)|
413 ((mtime_t)(hdr[11]&0xfe) << 14)|
414 (mtime_t)(hdr[12] << 7)|
415 (mtime_t)(hdr[12] >> 1);
417 if( hdr[7]&0x40 ) /* has dts */
419 i_dts = ((mtime_t)(hdr[14]&0x0e ) << 29)|
420 (mtime_t)(hdr[15] << 22)|
421 ((mtime_t)(hdr[16]&0xfe) << 14)|
422 (mtime_t)(hdr[17] << 7)|
423 (mtime_t)(hdr[18] >> 1);
427 p_pes = block_ChainGather( p_pes );
428 if( p_pes->i_buffer <= i_skip )
430 block_ChainRelease( p_pes );
434 p_pes->i_buffer -= i_skip;
435 p_pes->p_buffer += i_skip;
437 if( i_dts >= 0 ) p_pes->i_dts = i_dts * 100 / 9;
438 if( i_pts >= 0 ) p_pes->i_pts = i_pts * 100 / 9;
441 if( p_pes->i_pts > 0 )
443 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_pes->i_pts);
444 p_sys->b_pcr_audio = VLC_TRUE;
446 es_out_Send( p_demux->out, p_sys->p_audio, p_pes );