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 *****************************************************************************/
33 #include <vlc_demux.h>
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Open ( vlc_object_t * );
39 static void Close ( vlc_object_t * );
42 set_description( _("PVA demuxer" ) );
43 set_capability( "demux2", 10 );
44 set_category( CAT_INPUT );
45 set_subcategory( SUBCAT_INPUT_DEMUX );
46 set_callbacks( Open, Close );
47 add_shortcut( "pva" );
50 /*****************************************************************************
52 *****************************************************************************/
63 /* audio/video block */
64 block_t *p_pes; /* audio */
65 block_t *p_es; /* video */
70 static int Demux ( demux_t *p_demux );
71 static int Control ( demux_t *p_demux, int i_query, va_list args );
73 static int ReSynch ( demux_t * );
74 static void ParsePES( demux_t * );
76 /*****************************************************************************
78 *****************************************************************************/
79 static int Open( vlc_object_t *p_this )
81 demux_t *p_demux = (demux_t*)p_this;
84 const uint8_t *p_peek;
86 if( stream_Peek( p_demux->s, &p_peek, 5 ) < 5 ) return VLC_EGENERIC;
87 if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
89 /* In case we had forced this demuxer we try to resynch */
90 if( !p_demux->b_force || ReSynch( p_demux ) )
94 /* Fill p_demux field */
95 p_demux->pf_demux = Demux;
96 p_demux->pf_control = Control;
97 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
99 /* Register one audio and one video stream */
100 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
101 p_sys->p_audio = es_out_Add( p_demux->out, &fmt );
103 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
104 p_sys->p_video = es_out_Add( p_demux->out, &fmt );
111 p_sys->b_pcr_audio = false;
116 /*****************************************************************************
118 *****************************************************************************/
119 static void Close( vlc_object_t *p_this )
121 demux_t *p_demux = (demux_t*)p_this;
122 demux_sys_t *p_sys = p_demux->p_sys;
124 if( p_sys->p_es ) block_Release( p_sys->p_es );
125 if( p_sys->p_pes ) block_Release( p_sys->p_pes );
131 /*****************************************************************************
133 *****************************************************************************/
134 static int Demux( demux_t *p_demux )
136 demux_sys_t *p_sys = p_demux->p_sys;
138 const uint8_t *p_peek;
144 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
146 msg_Warn( p_demux, "eof ?" );
149 if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
151 msg_Warn( p_demux, "lost synchro" );
152 if( ReSynch( p_demux ) )
156 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
158 msg_Warn( p_demux, "eof ?" );
163 i_size = GetWBE( &p_peek[6] );
166 case 0x01: /* VideoStream */
167 if( p_sys->i_vc < 0 )
169 msg_Dbg( p_demux, "first packet for video" );
171 else if( ((p_sys->i_vc + 1)&0xff) != p_peek[3] )
173 msg_Dbg( p_demux, "packet lost (video)" );
176 block_ChainRelease( p_sys->p_es );
180 p_sys->i_vc = p_peek[3];
182 /* read the PTS and potential extra bytes TODO: make it a bit more optimised */
187 int i_pre = p_peek[5]&0x3;
189 if( ( p_frame = stream_Block( p_demux->s, 8 + 4 + i_pre ) ) )
191 i_pts = GetDWBE( &p_frame->p_buffer[8] );
192 if( p_frame->i_buffer > 12 )
194 p_frame->p_buffer += 12;
195 p_frame->i_buffer -= 12;
196 block_ChainAppend( &p_sys->p_es, p_frame );
200 block_Release( p_frame );
206 if( ( p_frame = p_sys->p_es ) )
209 if( p_frame->i_pts > 0 && !p_sys->b_pcr_audio )
211 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_frame->i_pts);
213 es_out_Send( p_demux->out, p_sys->p_video, p_frame );
219 if( ( p_frame = stream_Block( p_demux->s, i_size + i_skip ) ) )
221 p_frame->p_buffer += i_skip;
222 p_frame->i_buffer -= i_skip;
223 if( i_pts > 0 ) p_frame->i_pts = i_pts * 100 / 9;
224 block_ChainAppend( &p_sys->p_es, p_frame );
228 case 0x02: /* MainAudioStream */
229 if( p_sys->i_ac < 0 )
231 msg_Dbg( p_demux, "first packet for audio" );
233 else if( ((p_sys->i_ac + 1)&0xff) != p_peek[3] )
235 msg_Dbg( p_demux, "packet lost (audio)" );
238 block_ChainRelease( p_sys->p_pes );
242 p_sys->i_ac = p_peek[3];
244 if( p_peek[5]&0x10 && p_sys->p_pes )
248 if( ( p_frame = stream_Block( p_demux->s, i_size + 8 ) ) )
250 p_frame->p_buffer += 8;
251 p_frame->i_buffer -= 8;
252 /* XXX this a hack, some streams aren't compliant and
253 * doesn't set pes_start flag */
254 if( p_sys->p_pes && p_frame->i_buffer > 4 &&
255 p_frame->p_buffer[0] == 0x00 &&
256 p_frame->p_buffer[1] == 0x00 &&
257 p_frame->p_buffer[2] == 0x01 )
261 block_ChainAppend( &p_sys->p_pes, p_frame );
266 msg_Warn( p_demux, "unknown id=0x%x", p_peek[2] );
267 stream_Read( p_demux->s, NULL, i_size + 8 );
273 /*****************************************************************************
275 *****************************************************************************/
276 static int Control( demux_t *p_demux, int i_query, va_list args )
278 /* demux_sys_t *p_sys = p_demux->p_sys; */
283 case DEMUX_GET_POSITION:
284 if( ( i64 = stream_Size( p_demux->s ) ) > 0 )
286 pf = (double*) va_arg( args, double* );
287 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
292 case DEMUX_SET_POSITION:
293 f = (double) va_arg( args, double );
294 i64 = stream_Size( p_demux->s );
296 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
297 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
305 pi64 = (int64_t*)va_arg( args, int64_t * );
306 if( p_sys->i_time < 0 )
311 *pi64 = p_sys->i_time;
315 case DEMUX_GET_LENGTH:
316 pi64 = (int64_t*)va_arg( args, int64_t * );
317 if( p_sys->i_mux_rate > 0 )
319 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
327 pf = (double*)va_arg( args, double * );
328 *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
337 /*****************************************************************************
339 *****************************************************************************/
340 static int ReSynch( demux_t *p_demux )
342 const uint8_t *p_peek;
346 while( !p_demux->b_die )
348 if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 1024 ) ) < 8 )
354 while( i_skip < i_peek - 5 )
356 if( p_peek[0] == 'A' && p_peek[1] == 'V' && p_peek[4] == 0x55 )
360 stream_Read( p_demux->s, NULL, i_skip );
368 stream_Read( p_demux->s, NULL, i_skip );
374 static void ParsePES( demux_t *p_demux )
376 demux_sys_t *p_sys = p_demux->p_sys;
377 block_t *p_pes = p_sys->p_pes;
387 /* FIXME find real max size */
388 block_ChainExtract( p_pes, hdr, 30 );
390 if( hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 1 )
392 msg_Warn( p_demux, "invalid hdr [0x%2.2x:%2.2x:%2.2x:%2.2x]",
393 hdr[0], hdr[1],hdr[2],hdr[3] );
394 block_ChainRelease( p_pes );
397 i_pes_size = GetWBE( &hdr[4] );
399 /* we assume mpeg2 PES */
401 if( hdr[7]&0x80 ) /* has pts */
403 i_pts = ((mtime_t)(hdr[ 9]&0x0e ) << 29)|
404 (mtime_t)(hdr[10] << 22)|
405 ((mtime_t)(hdr[11]&0xfe) << 14)|
406 (mtime_t)(hdr[12] << 7)|
407 (mtime_t)(hdr[12] >> 1);
409 if( hdr[7]&0x40 ) /* has dts */
411 i_dts = ((mtime_t)(hdr[14]&0x0e ) << 29)|
412 (mtime_t)(hdr[15] << 22)|
413 ((mtime_t)(hdr[16]&0xfe) << 14)|
414 (mtime_t)(hdr[17] << 7)|
415 (mtime_t)(hdr[18] >> 1);
419 p_pes = block_ChainGather( p_pes );
420 if( p_pes->i_buffer <= i_skip )
422 block_ChainRelease( p_pes );
426 p_pes->i_buffer -= i_skip;
427 p_pes->p_buffer += i_skip;
429 if( i_dts >= 0 ) p_pes->i_dts = i_dts * 100 / 9;
430 if( i_pts >= 0 ) p_pes->i_pts = i_pts * 100 / 9;
433 if( p_pes->i_pts > 0 )
435 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_pes->i_pts);
436 p_sys->b_pcr_audio = true;
438 es_out_Send( p_demux->out, p_sys->p_audio, p_pes );