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_plugin.h>
34 #include <vlc_demux.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( "demux", 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;
85 const uint8_t *p_peek;
87 if( stream_Peek( p_demux->s, &p_peek, 5 ) < 5 ) return VLC_EGENERIC;
88 if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
90 /* In case we had forced this demuxer we try to resynch */
91 if( !p_demux->b_force || ReSynch( p_demux ) )
95 /* Fill p_demux field */
96 p_demux->pf_demux = Demux;
97 p_demux->pf_control = Control;
98 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
100 /* Register one audio and one video stream */
101 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
102 p_sys->p_audio = es_out_Add( p_demux->out, &fmt );
104 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
105 p_sys->p_video = es_out_Add( p_demux->out, &fmt );
112 p_sys->b_pcr_audio = false;
117 /*****************************************************************************
119 *****************************************************************************/
120 static void Close( vlc_object_t *p_this )
122 demux_t *p_demux = (demux_t*)p_this;
123 demux_sys_t *p_sys = p_demux->p_sys;
125 if( p_sys->p_es ) block_Release( p_sys->p_es );
126 if( p_sys->p_pes ) block_Release( p_sys->p_pes );
132 /*****************************************************************************
134 *****************************************************************************/
135 static int Demux( demux_t *p_demux )
137 demux_sys_t *p_sys = p_demux->p_sys;
139 const uint8_t *p_peek;
145 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
147 msg_Warn( p_demux, "eof ?" );
150 if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
152 msg_Warn( p_demux, "lost synchro" );
153 if( ReSynch( p_demux ) )
157 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
159 msg_Warn( p_demux, "eof ?" );
164 i_size = GetWBE( &p_peek[6] );
167 case 0x01: /* VideoStream */
168 if( p_sys->i_vc < 0 )
170 msg_Dbg( p_demux, "first packet for video" );
172 else if( ((p_sys->i_vc + 1)&0xff) != p_peek[3] )
174 msg_Dbg( p_demux, "packet lost (video)" );
177 block_ChainRelease( p_sys->p_es );
181 p_sys->i_vc = p_peek[3];
183 /* read the PTS and potential extra bytes TODO: make it a bit more optimised */
188 int i_pre = p_peek[5]&0x3;
190 if( ( p_frame = stream_Block( p_demux->s, 8 + 4 + i_pre ) ) )
192 i_pts = GetDWBE( &p_frame->p_buffer[8] );
193 if( p_frame->i_buffer > 12 )
195 p_frame->p_buffer += 12;
196 p_frame->i_buffer -= 12;
197 block_ChainAppend( &p_sys->p_es, p_frame );
201 block_Release( p_frame );
207 if( ( p_frame = p_sys->p_es ) )
210 if( p_frame->i_pts > 0 && !p_sys->b_pcr_audio )
212 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_frame->i_pts);
214 es_out_Send( p_demux->out, p_sys->p_video, p_frame );
220 if( ( p_frame = stream_Block( p_demux->s, i_size + i_skip ) ) )
222 p_frame->p_buffer += i_skip;
223 p_frame->i_buffer -= i_skip;
224 if( i_pts > 0 ) p_frame->i_pts = i_pts * 100 / 9;
225 block_ChainAppend( &p_sys->p_es, p_frame );
229 case 0x02: /* MainAudioStream */
230 if( p_sys->i_ac < 0 )
232 msg_Dbg( p_demux, "first packet for audio" );
234 else if( ((p_sys->i_ac + 1)&0xff) != p_peek[3] )
236 msg_Dbg( p_demux, "packet lost (audio)" );
239 block_ChainRelease( p_sys->p_pes );
243 p_sys->i_ac = p_peek[3];
245 if( p_peek[5]&0x10 && p_sys->p_pes )
249 if( ( p_frame = stream_Block( p_demux->s, i_size + 8 ) ) )
251 p_frame->p_buffer += 8;
252 p_frame->i_buffer -= 8;
253 /* XXX this a hack, some streams aren't compliant and
254 * doesn't set pes_start flag */
255 if( p_sys->p_pes && p_frame->i_buffer > 4 &&
256 p_frame->p_buffer[0] == 0x00 &&
257 p_frame->p_buffer[1] == 0x00 &&
258 p_frame->p_buffer[2] == 0x01 )
262 block_ChainAppend( &p_sys->p_pes, p_frame );
267 msg_Warn( p_demux, "unknown id=0x%x", p_peek[2] );
268 stream_Read( p_demux->s, NULL, i_size + 8 );
274 /*****************************************************************************
276 *****************************************************************************/
277 static int Control( demux_t *p_demux, int i_query, va_list args )
279 /* demux_sys_t *p_sys = p_demux->p_sys; */
284 case DEMUX_GET_POSITION:
285 if( ( i64 = stream_Size( p_demux->s ) ) > 0 )
287 pf = (double*) va_arg( args, double* );
288 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
293 case DEMUX_SET_POSITION:
294 f = (double) va_arg( args, double );
295 i64 = stream_Size( p_demux->s );
297 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
298 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
306 pi64 = (int64_t*)va_arg( args, int64_t * );
307 if( p_sys->i_time < 0 )
312 *pi64 = p_sys->i_time;
316 case DEMUX_GET_LENGTH:
317 pi64 = (int64_t*)va_arg( args, int64_t * );
318 if( p_sys->i_mux_rate > 0 )
320 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
328 pf = (double*)va_arg( args, double * );
329 *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
338 /*****************************************************************************
340 *****************************************************************************/
341 static int ReSynch( demux_t *p_demux )
343 const uint8_t *p_peek;
347 while( !p_demux->b_die )
349 if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 1024 ) ) < 8 )
355 while( i_skip < i_peek - 5 )
357 if( p_peek[0] == 'A' && p_peek[1] == 'V' && p_peek[4] == 0x55 )
361 stream_Read( p_demux->s, NULL, i_skip );
369 stream_Read( p_demux->s, NULL, i_skip );
375 static void ParsePES( demux_t *p_demux )
377 demux_sys_t *p_sys = p_demux->p_sys;
378 block_t *p_pes = p_sys->p_pes;
388 /* FIXME find real max size */
389 block_ChainExtract( p_pes, hdr, 30 );
391 if( hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 1 )
393 msg_Warn( p_demux, "invalid hdr [0x%2.2x:%2.2x:%2.2x:%2.2x]",
394 hdr[0], hdr[1],hdr[2],hdr[3] );
395 block_ChainRelease( p_pes );
398 i_pes_size = GetWBE( &hdr[4] );
400 /* we assume mpeg2 PES */
402 if( hdr[7]&0x80 ) /* has pts */
404 i_pts = ((mtime_t)(hdr[ 9]&0x0e ) << 29)|
405 (mtime_t)(hdr[10] << 22)|
406 ((mtime_t)(hdr[11]&0xfe) << 14)|
407 (mtime_t)(hdr[12] << 7)|
408 (mtime_t)(hdr[12] >> 1);
410 if( hdr[7]&0x40 ) /* has dts */
412 i_dts = ((mtime_t)(hdr[14]&0x0e ) << 29)|
413 (mtime_t)(hdr[15] << 22)|
414 ((mtime_t)(hdr[16]&0xfe) << 14)|
415 (mtime_t)(hdr[17] << 7)|
416 (mtime_t)(hdr[18] >> 1);
420 p_pes = block_ChainGather( p_pes );
421 if( p_pes->i_buffer <= i_skip )
423 block_ChainRelease( p_pes );
427 p_pes->i_buffer -= i_skip;
428 p_pes->p_buffer += i_skip;
430 if( i_dts >= 0 ) p_pes->i_dts = i_dts * 100 / 9;
431 if( i_pts >= 0 ) p_pes->i_pts = i_pts * 100 / 9;
434 if( p_pes->i_pts > 0 )
436 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_pes->i_pts);
437 p_sys->b_pcr_audio = true;
439 es_out_Send( p_demux->out, p_sys->p_audio, p_pes );