1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <vlc_demux.h>
31 /*****************************************************************************
33 *****************************************************************************/
34 static int Open ( vlc_object_t * );
35 static void Close ( vlc_object_t * );
38 set_description( _("PVA demuxer" ) );
39 set_capability( "demux2", 10 );
40 set_category( CAT_INPUT );
41 set_subcategory( SUBCAT_INPUT_DEMUX );
42 set_callbacks( Open, Close );
43 add_shortcut( "pva" );
46 /*****************************************************************************
48 *****************************************************************************/
59 /* audio/video block */
60 block_t *p_pes; /* audio */
61 block_t *p_es; /* video */
66 static int Demux ( demux_t *p_demux );
67 static int Control ( demux_t *p_demux, int i_query, va_list args );
69 static int ReSynch ( demux_t * );
70 static void ParsePES( demux_t * );
72 /*****************************************************************************
74 *****************************************************************************/
75 static int Open( vlc_object_t *p_this )
77 demux_t *p_demux = (demux_t*)p_this;
82 if( stream_Peek( p_demux->s, &p_peek, 5 ) < 5 ) return VLC_EGENERIC;
83 if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
85 /* In case we had forced this demuxer we try to resynch */
86 if( strcasecmp( p_demux->psz_demux, "pva" ) || ReSynch( p_demux ) )
92 /* Fill p_demux field */
93 p_demux->pf_demux = Demux;
94 p_demux->pf_control = Control;
95 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
97 /* Register one audio and one video stream */
98 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
99 p_sys->p_audio = es_out_Add( p_demux->out, &fmt );
101 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
102 p_sys->p_video = es_out_Add( p_demux->out, &fmt );
109 p_sys->b_pcr_audio = VLC_FALSE;
114 /*****************************************************************************
116 *****************************************************************************/
117 static void Close( vlc_object_t *p_this )
119 demux_t *p_demux = (demux_t*)p_this;
120 demux_sys_t *p_sys = p_demux->p_sys;
122 if( p_sys->p_es ) block_Release( p_sys->p_es );
123 if( p_sys->p_pes ) block_Release( p_sys->p_pes );
129 /*****************************************************************************
131 *****************************************************************************/
132 static int Demux( demux_t *p_demux )
134 demux_sys_t *p_sys = p_demux->p_sys;
142 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
144 msg_Warn( p_demux, "eof ?" );
147 if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
149 msg_Warn( p_demux, "lost synchro" );
150 if( ReSynch( p_demux ) )
154 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
156 msg_Warn( p_demux, "eof ?" );
161 i_size = GetWBE( &p_peek[6] );
164 case 0x01: /* VideoStream */
165 if( p_sys->i_vc < 0 )
167 msg_Dbg( p_demux, "first packet for video" );
169 else if( ((p_sys->i_vc + 1)&0xff) != p_peek[3] )
171 msg_Dbg( p_demux, "packet lost (video)" );
174 block_ChainRelease( p_sys->p_es );
178 p_sys->i_vc = p_peek[3];
180 /* read the PTS and potential extra bytes TODO: make it a bit more optimised */
185 int i_pre = p_peek[5]&0x3;
187 if( ( p_frame = stream_Block( p_demux->s, 8 + 4 + i_pre ) ) )
189 i_pts = GetDWBE( &p_frame->p_buffer[8] );
190 if( p_frame->i_buffer > 12 )
192 p_frame->p_buffer += 12;
193 p_frame->i_buffer -= 12;
194 block_ChainAppend( &p_sys->p_es, p_frame );
198 block_Release( p_frame );
204 if( ( p_frame = p_sys->p_es ) )
207 if( p_frame->i_pts > 0 && !p_sys->b_pcr_audio )
209 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_frame->i_pts);
211 es_out_Send( p_demux->out, p_sys->p_video, p_frame );
217 if( ( p_frame = stream_Block( p_demux->s, i_size + i_skip ) ) )
219 p_frame->p_buffer += i_skip;
220 p_frame->i_buffer -= i_skip;
221 if( i_pts > 0 ) p_frame->i_pts = i_pts * 100 / 9;
222 block_ChainAppend( &p_sys->p_es, p_frame );
226 case 0x02: /* MainAudioStream */
227 if( p_sys->i_ac < 0 )
229 msg_Dbg( p_demux, "first packet for audio" );
231 else if( ((p_sys->i_ac + 1)&0xff) != p_peek[3] )
233 msg_Dbg( p_demux, "packet lost (audio)" );
236 block_ChainRelease( p_sys->p_pes );
240 p_sys->i_ac = p_peek[3];
242 if( p_peek[5]&0x10 && p_sys->p_pes )
246 if( ( p_frame = stream_Block( p_demux->s, i_size + 8 ) ) )
248 p_frame->p_buffer += 8;
249 p_frame->i_buffer -= 8;
250 /* XXX this a hack, some streams aren't compliant and
251 * doesn't set pes_start flag */
252 if( p_sys->p_pes && p_frame->i_buffer > 4 &&
253 p_frame->p_buffer[0] == 0x00 &&
254 p_frame->p_buffer[1] == 0x00 &&
255 p_frame->p_buffer[2] == 0x01 )
259 block_ChainAppend( &p_sys->p_pes, p_frame );
264 msg_Warn( p_demux, "unknown id=0x%x", p_peek[2] );
265 stream_Read( p_demux->s, NULL, i_size + 8 );
271 /*****************************************************************************
273 *****************************************************************************/
274 static int Control( demux_t *p_demux, int i_query, va_list args )
276 /* demux_sys_t *p_sys = p_demux->p_sys; */
281 case DEMUX_GET_POSITION:
282 if( ( i64 = stream_Size( p_demux->s ) ) > 0 )
284 pf = (double*) va_arg( args, double* );
285 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
290 case DEMUX_SET_POSITION:
291 f = (double) va_arg( args, double );
292 i64 = stream_Size( p_demux->s );
294 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
295 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
303 pi64 = (int64_t*)va_arg( args, int64_t * );
304 if( p_sys->i_time < 0 )
309 *pi64 = p_sys->i_time;
313 case DEMUX_GET_LENGTH:
314 pi64 = (int64_t*)va_arg( args, int64_t * );
315 if( p_sys->i_mux_rate > 0 )
317 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
325 pf = (double*)va_arg( args, double * );
326 *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
335 /*****************************************************************************
337 *****************************************************************************/
338 static int ReSynch( demux_t *p_demux )
344 while( !p_demux->b_die )
346 if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 1024 ) ) < 8 )
352 while( i_skip < i_peek - 5 )
354 if( p_peek[0] == 'A' && p_peek[1] == 'V' && p_peek[4] == 0x55 )
358 stream_Read( p_demux->s, NULL, i_skip );
366 stream_Read( p_demux->s, NULL, i_skip );
372 static void ParsePES( demux_t *p_demux )
374 demux_sys_t *p_sys = p_demux->p_sys;
375 block_t *p_pes = p_sys->p_pes;
385 /* FIXME find real max size */
386 block_ChainExtract( p_pes, hdr, 30 );
388 if( hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 1 )
390 msg_Warn( p_demux, "invalid hdr [0x%2.2x:%2.2x:%2.2x:%2.2x]",
391 hdr[0], hdr[1],hdr[2],hdr[3] );
392 block_ChainRelease( p_pes );
395 i_pes_size = GetWBE( &hdr[4] );
397 /* we assume mpeg2 PES */
399 if( hdr[7]&0x80 ) /* has pts */
401 i_pts = ((mtime_t)(hdr[ 9]&0x0e ) << 29)|
402 (mtime_t)(hdr[10] << 22)|
403 ((mtime_t)(hdr[11]&0xfe) << 14)|
404 (mtime_t)(hdr[12] << 7)|
405 (mtime_t)(hdr[12] >> 1);
407 if( hdr[7]&0x40 ) /* has dts */
409 i_dts = ((mtime_t)(hdr[14]&0x0e ) << 29)|
410 (mtime_t)(hdr[15] << 22)|
411 ((mtime_t)(hdr[16]&0xfe) << 14)|
412 (mtime_t)(hdr[17] << 7)|
413 (mtime_t)(hdr[18] >> 1);
417 p_pes = block_ChainGather( p_pes );
418 if( p_pes->i_buffer <= i_skip )
420 block_ChainRelease( p_pes );
424 p_pes->i_buffer -= i_skip;
425 p_pes->p_buffer += i_skip;
427 if( i_dts >= 0 ) p_pes->i_dts = i_dts * 100 / 9;
428 if( i_pts >= 0 ) p_pes->i_pts = i_pts * 100 / 9;
431 if( p_pes->i_pts > 0 )
433 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_pes->i_pts);
434 p_sys->b_pcr_audio = VLC_TRUE;
436 es_out_Send( p_demux->out, p_sys->p_audio, p_pes );