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;
80 const uint8_t *p_peek;
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( !p_demux->b_force || ReSynch( p_demux ) )
90 /* Fill p_demux field */
91 p_demux->pf_demux = Demux;
92 p_demux->pf_control = Control;
93 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
95 /* Register one audio and one video stream */
96 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
97 p_sys->p_audio = es_out_Add( p_demux->out, &fmt );
99 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
100 p_sys->p_video = es_out_Add( p_demux->out, &fmt );
107 p_sys->b_pcr_audio = VLC_FALSE;
112 /*****************************************************************************
114 *****************************************************************************/
115 static void Close( vlc_object_t *p_this )
117 demux_t *p_demux = (demux_t*)p_this;
118 demux_sys_t *p_sys = p_demux->p_sys;
120 if( p_sys->p_es ) block_Release( p_sys->p_es );
121 if( p_sys->p_pes ) block_Release( p_sys->p_pes );
127 /*****************************************************************************
129 *****************************************************************************/
130 static int Demux( demux_t *p_demux )
132 demux_sys_t *p_sys = p_demux->p_sys;
134 const uint8_t *p_peek;
140 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
142 msg_Warn( p_demux, "eof ?" );
145 if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
147 msg_Warn( p_demux, "lost synchro" );
148 if( ReSynch( p_demux ) )
152 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
154 msg_Warn( p_demux, "eof ?" );
159 i_size = GetWBE( &p_peek[6] );
162 case 0x01: /* VideoStream */
163 if( p_sys->i_vc < 0 )
165 msg_Dbg( p_demux, "first packet for video" );
167 else if( ((p_sys->i_vc + 1)&0xff) != p_peek[3] )
169 msg_Dbg( p_demux, "packet lost (video)" );
172 block_ChainRelease( p_sys->p_es );
176 p_sys->i_vc = p_peek[3];
178 /* read the PTS and potential extra bytes TODO: make it a bit more optimised */
183 int i_pre = p_peek[5]&0x3;
185 if( ( p_frame = stream_Block( p_demux->s, 8 + 4 + i_pre ) ) )
187 i_pts = GetDWBE( &p_frame->p_buffer[8] );
188 if( p_frame->i_buffer > 12 )
190 p_frame->p_buffer += 12;
191 p_frame->i_buffer -= 12;
192 block_ChainAppend( &p_sys->p_es, p_frame );
196 block_Release( p_frame );
202 if( ( p_frame = p_sys->p_es ) )
205 if( p_frame->i_pts > 0 && !p_sys->b_pcr_audio )
207 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_frame->i_pts);
209 es_out_Send( p_demux->out, p_sys->p_video, p_frame );
215 if( ( p_frame = stream_Block( p_demux->s, i_size + i_skip ) ) )
217 p_frame->p_buffer += i_skip;
218 p_frame->i_buffer -= i_skip;
219 if( i_pts > 0 ) p_frame->i_pts = i_pts * 100 / 9;
220 block_ChainAppend( &p_sys->p_es, p_frame );
224 case 0x02: /* MainAudioStream */
225 if( p_sys->i_ac < 0 )
227 msg_Dbg( p_demux, "first packet for audio" );
229 else if( ((p_sys->i_ac + 1)&0xff) != p_peek[3] )
231 msg_Dbg( p_demux, "packet lost (audio)" );
234 block_ChainRelease( p_sys->p_pes );
238 p_sys->i_ac = p_peek[3];
240 if( p_peek[5]&0x10 && p_sys->p_pes )
244 if( ( p_frame = stream_Block( p_demux->s, i_size + 8 ) ) )
246 p_frame->p_buffer += 8;
247 p_frame->i_buffer -= 8;
248 /* XXX this a hack, some streams aren't compliant and
249 * doesn't set pes_start flag */
250 if( p_sys->p_pes && p_frame->i_buffer > 4 &&
251 p_frame->p_buffer[0] == 0x00 &&
252 p_frame->p_buffer[1] == 0x00 &&
253 p_frame->p_buffer[2] == 0x01 )
257 block_ChainAppend( &p_sys->p_pes, p_frame );
262 msg_Warn( p_demux, "unknown id=0x%x", p_peek[2] );
263 stream_Read( p_demux->s, NULL, i_size + 8 );
269 /*****************************************************************************
271 *****************************************************************************/
272 static int Control( demux_t *p_demux, int i_query, va_list args )
274 /* demux_sys_t *p_sys = p_demux->p_sys; */
279 case DEMUX_GET_POSITION:
280 if( ( i64 = stream_Size( p_demux->s ) ) > 0 )
282 pf = (double*) va_arg( args, double* );
283 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
288 case DEMUX_SET_POSITION:
289 f = (double) va_arg( args, double );
290 i64 = stream_Size( p_demux->s );
292 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
293 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
301 pi64 = (int64_t*)va_arg( args, int64_t * );
302 if( p_sys->i_time < 0 )
307 *pi64 = p_sys->i_time;
311 case DEMUX_GET_LENGTH:
312 pi64 = (int64_t*)va_arg( args, int64_t * );
313 if( p_sys->i_mux_rate > 0 )
315 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
323 pf = (double*)va_arg( args, double * );
324 *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
333 /*****************************************************************************
335 *****************************************************************************/
336 static int ReSynch( demux_t *p_demux )
338 const uint8_t *p_peek;
342 while( !p_demux->b_die )
344 if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 1024 ) ) < 8 )
350 while( i_skip < i_peek - 5 )
352 if( p_peek[0] == 'A' && p_peek[1] == 'V' && p_peek[4] == 0x55 )
356 stream_Read( p_demux->s, NULL, i_skip );
364 stream_Read( p_demux->s, NULL, i_skip );
370 static void ParsePES( demux_t *p_demux )
372 demux_sys_t *p_sys = p_demux->p_sys;
373 block_t *p_pes = p_sys->p_pes;
383 /* FIXME find real max size */
384 block_ChainExtract( p_pes, hdr, 30 );
386 if( hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 1 )
388 msg_Warn( p_demux, "invalid hdr [0x%2.2x:%2.2x:%2.2x:%2.2x]",
389 hdr[0], hdr[1],hdr[2],hdr[3] );
390 block_ChainRelease( p_pes );
393 i_pes_size = GetWBE( &hdr[4] );
395 /* we assume mpeg2 PES */
397 if( hdr[7]&0x80 ) /* has pts */
399 i_pts = ((mtime_t)(hdr[ 9]&0x0e ) << 29)|
400 (mtime_t)(hdr[10] << 22)|
401 ((mtime_t)(hdr[11]&0xfe) << 14)|
402 (mtime_t)(hdr[12] << 7)|
403 (mtime_t)(hdr[12] >> 1);
405 if( hdr[7]&0x40 ) /* has dts */
407 i_dts = ((mtime_t)(hdr[14]&0x0e ) << 29)|
408 (mtime_t)(hdr[15] << 22)|
409 ((mtime_t)(hdr[16]&0xfe) << 14)|
410 (mtime_t)(hdr[17] << 7)|
411 (mtime_t)(hdr[18] >> 1);
415 p_pes = block_ChainGather( p_pes );
416 if( p_pes->i_buffer <= i_skip )
418 block_ChainRelease( p_pes );
422 p_pes->i_buffer -= i_skip;
423 p_pes->p_buffer += i_skip;
425 if( i_dts >= 0 ) p_pes->i_dts = i_dts * 100 / 9;
426 if( i_pts >= 0 ) p_pes->i_pts = i_pts * 100 / 9;
429 if( p_pes->i_pts > 0 )
431 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_pes->i_pts);
432 p_sys->b_pcr_audio = VLC_TRUE;
434 es_out_Send( p_demux->out, p_sys->p_audio, p_pes );