1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
5 * $Id: pva.c,v 1.1 2004/01/23 17:42:10 fenrir Exp $
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_callbacks( Open, Close );
46 add_shortcut( "pva" );
49 /*****************************************************************************
51 *****************************************************************************/
62 /* audio/video block */
63 block_t *p_pes; /* audio */
64 block_t *p_es; /* video */
69 static int Demux ( demux_t *p_demux );
70 static int Control ( demux_t *p_demux, int i_query, va_list args );
72 static int ReSynch ( demux_t * );
73 static void ParsePES( demux_t * );
75 /*****************************************************************************
77 *****************************************************************************/
78 static int Open( vlc_object_t *p_this )
80 demux_t *p_demux = (demux_t*)p_this;
85 if( stream_Peek( p_demux->s, &p_peek, 5 ) < 5 )
87 msg_Err( p_demux, "cannot peek" );
90 if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
92 /* In case we had forced this demuxer we try to resynch */
93 if( strcasecmp( p_demux->psz_demux, "pva" ) || ReSynch( p_demux ) )
95 msg_Warn( p_demux, "PVA module discarded" );
100 /* Fill p_demux field */
101 p_demux->pf_demux = Demux;
102 p_demux->pf_control = Control;
103 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
105 /* Register one audio and one video stream */
106 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
107 p_sys->p_audio = es_out_Add( p_demux->out, &fmt );
109 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
110 p_sys->p_video = es_out_Add( p_demux->out, &fmt );
117 p_sys->b_pcr_audio = VLC_FALSE;
122 /*****************************************************************************
124 *****************************************************************************/
125 static void Close( vlc_object_t *p_this )
127 demux_t *p_demux = (demux_t*)p_this;
128 demux_sys_t *p_sys = p_demux->p_sys;
130 if( p_sys->p_es ) block_Release( p_sys->p_es );
131 if( p_sys->p_pes ) block_Release( p_sys->p_pes );
137 /*****************************************************************************
139 *****************************************************************************/
140 static int Demux( demux_t *p_demux )
142 demux_sys_t *p_sys = p_demux->p_sys;
150 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
152 msg_Warn( p_demux, "eof ?" );
155 if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
157 msg_Warn( p_demux, "lost synchro" );
158 if( ReSynch( p_demux ) )
162 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
164 msg_Warn( p_demux, "eof ?" );
169 i_size = GetWBE( &p_peek[6] );
172 case 0x01: /* VideoStream */
173 if( p_sys->i_vc < 0 )
175 msg_Dbg( p_demux, "first packet for video" );
177 else if( ((p_sys->i_vc + 1)&0xff) != p_peek[3] )
179 msg_Dbg( p_demux, "packet lost (video)" );
182 block_ChainRelease( p_sys->p_es );
186 p_sys->i_vc = p_peek[3];
188 /* read the PTS and potential extra bytes TODO: make it a bit more optimised */
193 int i_pre = p_peek[5]&0x3;
195 if( ( p_frame = stream_Block( p_demux->s, 8 + 4 + i_pre ) ) )
197 i_pts = GetDWBE( &p_frame->p_buffer[8] );
198 if( p_frame->i_buffer > 12 )
200 p_frame->p_buffer += 12;
201 p_frame->i_buffer -= 12;
202 block_ChainAppend( &p_sys->p_es, p_frame );
206 block_Release( p_frame );
212 if( ( p_frame = p_sys->p_es ) )
215 if( p_frame->i_pts > 0 && !p_sys->b_pcr_audio )
217 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_frame->i_pts);
219 es_out_Send( p_demux->out, p_sys->p_video, p_frame );
225 if( ( p_frame = stream_Block( p_demux->s, i_size + i_skip ) ) )
227 p_frame->p_buffer += i_skip;
228 p_frame->i_buffer -= i_skip;
229 if( i_pts > 0 ) p_frame->i_pts = i_pts * 100 / 9;
230 block_ChainAppend( &p_sys->p_es, p_frame );
234 case 0x02: /* MainAudioStream */
235 if( p_sys->i_ac < 0 )
237 msg_Dbg( p_demux, "first packet for audio" );
239 else if( ((p_sys->i_ac + 1)&0xff) != p_peek[3] )
241 msg_Dbg( p_demux, "packet lost (audio)" );
244 block_ChainRelease( p_sys->p_pes );
248 p_sys->i_ac = p_peek[3];
250 if( p_peek[5]&0x10 && p_sys->p_pes )
254 if( ( p_frame = stream_Block( p_demux->s, i_size + 8 ) ) )
256 p_frame->p_buffer += 8;
257 p_frame->i_buffer -= 8;
258 /* XXX this a hack, some streams aren't compliant and
259 * doesn'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 )
267 block_ChainAppend( &p_sys->p_pes, p_frame );
272 msg_Warn( p_demux, "unknown id=0x%x", p_peek[2] );
273 stream_Read( p_demux->s, NULL, i_size + 8 );
279 /*****************************************************************************
281 *****************************************************************************/
282 static int Control( demux_t *p_demux, int i_query, va_list args )
284 /* demux_sys_t *p_sys = p_demux->p_sys; */
289 case DEMUX_GET_POSITION:
290 if( ( i64 = stream_Size( p_demux->s ) ) > 0 )
292 pf = (double*) va_arg( args, double* );
293 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
298 case DEMUX_SET_POSITION:
299 f = (double) va_arg( args, double );
300 i64 = stream_Size( p_demux->s );
302 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
303 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
311 pi64 = (int64_t*)va_arg( args, int64_t * );
312 if( p_sys->i_time < 0 )
317 *pi64 = p_sys->i_time;
321 case DEMUX_GET_LENGTH:
322 pi64 = (int64_t*)va_arg( args, int64_t * );
323 if( p_sys->i_mux_rate > 0 )
325 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
334 pf = (double*)va_arg( args, double * );
335 *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
343 /*****************************************************************************
345 *****************************************************************************/
346 static int ReSynch( demux_t *p_demux )
352 while( !p_demux->b_die )
354 if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 1024 ) ) < 8 )
360 while( i_skip < i_peek - 5 )
362 if( p_peek[0] == 'A' && p_peek[1] == 'V' && p_peek[4] == 0x55 )
366 stream_Read( p_demux->s, NULL, i_skip );
374 stream_Read( p_demux->s, NULL, i_skip );
380 static void ParsePES( demux_t *p_demux )
382 demux_sys_t *p_sys = p_demux->p_sys;
383 block_t *p_pes = p_sys->p_pes;
393 /* FIXME find real max size */
394 block_ChainExtract( p_pes, hdr, 30 );
396 if( hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 1 )
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 );
403 i_pes_size = GetWBE( &hdr[4] );
405 /* we assume mpeg2 PES */
407 if( hdr[7]&0x80 ) /* has pts */
409 i_pts = ((mtime_t)(hdr[ 9]&0x0e ) << 29)|
410 (mtime_t)(hdr[10] << 22)|
411 ((mtime_t)(hdr[11]&0xfe) << 14)|
412 (mtime_t)(hdr[12] << 7)|
413 (mtime_t)(hdr[12] >> 1);
415 if( hdr[7]&0x40 ) /* has dts */
417 i_dts = ((mtime_t)(hdr[14]&0x0e ) << 29)|
418 (mtime_t)(hdr[15] << 22)|
419 ((mtime_t)(hdr[16]&0xfe) << 14)|
420 (mtime_t)(hdr[17] << 7)|
421 (mtime_t)(hdr[18] >> 1);
425 p_pes = block_ChainGather( p_pes );
426 if( p_pes->i_buffer <= i_skip )
428 block_ChainRelease( p_pes );
432 p_pes->i_buffer -= i_skip;
433 p_pes->p_buffer += i_skip;
435 if( i_dts >= 0 ) p_pes->i_dts = i_dts * 100 / 9;
436 if( i_pts >= 0 ) p_pes->i_pts = i_pts * 100 / 9;
439 if( p_pes->i_pts > 0 )
441 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_pes->i_pts);
442 p_sys->b_pcr_audio = VLC_TRUE;
444 es_out_Send( p_demux->out, p_sys->p_audio, p_pes );