1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
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( N_("PVA demuxer" ) )
44 set_capability( "demux", 10 )
45 set_category( CAT_INPUT
)
46 set_subcategory( SUBCAT_INPUT_DEMUX
)
47 set_callbacks( Open
, Close
)
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( vlc_stream_Peek( p_demux
->s
, &p_peek
, 8 ) < 8 ) 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
->obj
.force
|| ReSynch( p_demux
) )
95 p_sys
= malloc( sizeof( demux_sys_t
) );
96 if( unlikely(p_sys
== NULL
) )
99 /* Fill p_demux field */
100 p_demux
->pf_demux
= Demux
;
101 p_demux
->pf_control
= Control
;
102 p_demux
->p_sys
= p_sys
;
104 /* Register one audio and one video stream */
105 es_format_Init( &fmt
, AUDIO_ES
, VLC_CODEC_MPGA
);
106 fmt
.b_packetized
= false;
107 p_sys
->p_audio
= es_out_Add( p_demux
->out
, &fmt
);
109 es_format_Init( &fmt
, VIDEO_ES
, VLC_CODEC_MPGV
);
110 fmt
.b_packetized
= false;
111 p_sys
->p_video
= es_out_Add( p_demux
->out
, &fmt
);
118 p_sys
->b_pcr_audio
= false;
123 /*****************************************************************************
125 *****************************************************************************/
126 static void Close( vlc_object_t
*p_this
)
128 demux_t
*p_demux
= (demux_t
*)p_this
;
129 demux_sys_t
*p_sys
= p_demux
->p_sys
;
131 block_ChainRelease( p_sys
->p_es
);
132 block_ChainRelease( p_sys
->p_pes
);
138 /*****************************************************************************
140 *****************************************************************************
141 * See http://multimedia.cx/mirror/av_format_v1.pdf
142 *****************************************************************************/
143 static int Demux( demux_t
*p_demux
)
145 demux_sys_t
*p_sys
= p_demux
->p_sys
;
147 const uint8_t *p_peek
;
153 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 8 ) < 8 )
155 msg_Warn( p_demux
, "eof ?" );
156 return VLC_DEMUXER_EOF
;
158 if( p_peek
[0] != 'A' || p_peek
[1] != 'V' || p_peek
[4] != 0x55 )
160 msg_Warn( p_demux
, "lost synchro" );
161 if( ReSynch( p_demux
) )
163 return VLC_DEMUXER_EGENERIC
;
165 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 8 ) < 8 )
167 msg_Warn( p_demux
, "eof ?" );
168 return VLC_DEMUXER_EOF
;
172 i_size
= GetWBE( &p_peek
[6] );
175 case 0x01: /* VideoStream */
176 if( p_sys
->i_vc
< 0 )
178 msg_Dbg( p_demux
, "first packet for video" );
180 else if( ((p_sys
->i_vc
+ 1)&0xff) != p_peek
[3] )
182 msg_Dbg( p_demux
, "packet lost (video)" );
185 block_ChainRelease( p_sys
->p_es
);
189 p_sys
->i_vc
= p_peek
[3];
191 /* read the PTS and potential extra bytes TODO: make it a bit more optimised */
196 int i_pre
= p_peek
[5]&0x3;
198 if( ( p_frame
= vlc_stream_Block( p_demux
->s
, 8 + 4 + i_pre
) ) )
200 i_pts
= GetDWBE( &p_frame
->p_buffer
[8] );
201 if( p_frame
->i_buffer
> 12 )
203 p_frame
->p_buffer
+= 12;
204 p_frame
->i_buffer
-= 12;
205 block_ChainAppend( &p_sys
->p_es
, p_frame
);
209 block_Release( p_frame
);
215 if( ( p_frame
= p_sys
->p_es
) )
218 if( p_frame
->i_pts
!= VLC_TICK_INVALID
&& !p_sys
->b_pcr_audio
)
220 es_out_SetPCR( p_demux
->out
, p_frame
->i_pts
);
223 p_frame
= block_ChainGather( p_frame
);
224 if( likely(p_frame
) )
225 es_out_Send( p_demux
->out
, p_sys
->p_video
, p_frame
);
231 if( ( p_frame
= vlc_stream_Block( p_demux
->s
, i_size
+ i_skip
) ) )
233 p_frame
->p_buffer
+= i_skip
;
234 p_frame
->i_buffer
-= i_skip
;
236 p_frame
->i_pts
= VLC_TICK_0
+ i_pts
* 100 / 9;
237 block_ChainAppend( &p_sys
->p_es
, p_frame
);
241 case 0x02: /* MainAudioStream */
242 if( p_sys
->i_ac
< 0 )
244 msg_Dbg( p_demux
, "first packet for audio" );
246 else if( ((p_sys
->i_ac
+ 1)&0xff) != p_peek
[3] )
248 msg_Dbg( p_demux
, "packet lost (audio)" );
251 block_ChainRelease( p_sys
->p_pes
);
255 p_sys
->i_ac
= p_peek
[3];
257 if( p_peek
[5]&0x10 && p_sys
->p_pes
)
261 if( ( p_frame
= vlc_stream_Block( p_demux
->s
, i_size
+ 8 ) ) )
263 p_frame
->p_buffer
+= 8;
264 p_frame
->i_buffer
-= 8;
265 /* XXX this a hack, some streams aren't compliant and
266 * don't set pes_start flag */
267 if( p_sys
->p_pes
&& p_frame
->i_buffer
> 4 &&
268 p_frame
->p_buffer
[0] == 0x00 &&
269 p_frame
->p_buffer
[1] == 0x00 &&
270 p_frame
->p_buffer
[2] == 0x01 )
274 block_ChainAppend( &p_sys
->p_pes
, p_frame
);
279 msg_Warn( p_demux
, "unknown id=0x%x", p_peek
[2] );
280 if( vlc_stream_Read( p_demux
->s
, NULL
, i_size
+ 8 ) < i_size
+ 8 )
281 return VLC_DEMUXER_EOF
;
284 return VLC_DEMUXER_SUCCESS
;
287 /*****************************************************************************
289 *****************************************************************************/
290 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
292 /* demux_sys_t *p_sys = p_demux->p_sys; */
298 return vlc_stream_vaControl( p_demux
->s
, i_query
, args
);
300 case DEMUX_GET_POSITION
:
301 if( ( i64
= stream_Size( p_demux
->s
) ) > 0 )
303 pf
= va_arg( args
, double * );
304 double current
= vlc_stream_Tell( p_demux
->s
);
305 *pf
= current
/ (double)i64
;
310 case DEMUX_SET_POSITION
:
311 f
= va_arg( args
, double );
312 i64
= stream_Size( p_demux
->s
);
314 if( vlc_stream_Seek( p_demux
->s
, (int64_t)(i64
* f
) ) || ReSynch( p_demux
) )
322 pi64
= va_arg( args
, int64_t * );
323 if( p_sys
->i_time
< 0 )
328 *pi64
= p_sys
->i_time
;
332 case DEMUX_GET_LENGTH
:
333 pi64
= va_arg( args
, int64_t * );
334 if( p_sys
->i_mux_rate
> 0 )
336 *pi64
= CLOCK_FREQ
* ( stream_Size( p_demux
->s
) / 50 ) / p_sys
->i_mux_rate
;
344 pf
= va_arg( args
, double * );
345 *pf
= (double)1000000.0 / (double)p_sys
->i_pcr_inc
;
348 case DEMUX_CAN_PAUSE
:
349 case DEMUX_SET_PAUSE_STATE
:
350 case DEMUX_CAN_CONTROL_PACE
:
351 case DEMUX_GET_PTS_DELAY
:
352 return demux_vaControlHelper( p_demux
->s
, 0, -1, 0, 1, i_query
, args
);
360 /*****************************************************************************
362 *****************************************************************************/
363 static int ReSynch( demux_t
*p_demux
)
367 const uint8_t *p_peek
;
368 int i_peek
= vlc_stream_Peek( p_demux
->s
, &p_peek
, 1024 );
374 while( i_skip
< i_peek
- 5 )
376 if( p_peek
[0] == 'A' && p_peek
[1] == 'V' && p_peek
[4] == 0x55 )
379 && vlc_stream_Read( p_demux
->s
, NULL
, i_skip
) < i_skip
)
387 if( vlc_stream_Read( p_demux
->s
, NULL
, i_skip
) < i_skip
)
394 static void ParsePES( demux_t
*p_demux
)
396 demux_sys_t
*p_sys
= p_demux
->p_sys
;
397 block_t
*p_pes
= p_sys
->p_pes
;
401 vlc_tick_t i_dts
= -1;
402 vlc_tick_t i_pts
= -1;
406 /* FIXME find real max size */
407 block_ChainExtract( p_pes
, hdr
, 30 );
409 /* See ยง2.4.3.6 of ISO 13818-1 */
410 if( hdr
[0] != 0 || hdr
[1] != 0 || hdr
[2] != 1 )
412 msg_Warn( p_demux
, "invalid hdr [0x%2.2x:%2.2x:%2.2x:%2.2x]",
413 hdr
[0], hdr
[1],hdr
[2],hdr
[3] );
414 block_ChainRelease( p_pes
);
417 // hdr[4] i_pes_size, 2 bytes
418 // hdr[6] Marker -> original_or_copy
420 /* we assume mpeg2 PES */
422 if( hdr
[7]&0x80 ) /* has pts */
424 i_pts
= ((vlc_tick_t
)(hdr
[ 9]&0x0e ) << 29)|
425 (vlc_tick_t
)(hdr
[10] << 22)|
426 ((vlc_tick_t
)(hdr
[11]&0xfe) << 14)|
427 (vlc_tick_t
)(hdr
[12] << 7)|
428 (vlc_tick_t
)(hdr
[12] >> 1);
430 if( hdr
[7]&0x40 ) /* has dts */
432 i_dts
= ((vlc_tick_t
)(hdr
[14]&0x0e ) << 29)|
433 (vlc_tick_t
)(hdr
[15] << 22)|
434 ((vlc_tick_t
)(hdr
[16]&0xfe) << 14)|
435 (vlc_tick_t
)(hdr
[17] << 7)|
436 (vlc_tick_t
)(hdr
[18] >> 1);
440 p_pes
= block_ChainGather( p_pes
);
441 if( unlikely(p_pes
== NULL
) )
443 if( p_pes
->i_buffer
<= i_skip
)
445 block_ChainRelease( p_pes
);
449 p_pes
->i_buffer
-= i_skip
;
450 p_pes
->p_buffer
+= i_skip
;
453 p_pes
->i_dts
= VLC_TICK_0
+ i_dts
* 100 / 9;
455 p_pes
->i_pts
= VLC_TICK_0
+ i_pts
* 100 / 9;
458 if( p_pes
->i_pts
> 0 )
460 es_out_SetPCR( p_demux
->out
, p_pes
->i_pts
);
461 p_sys
->b_pcr_audio
= true;
463 es_out_Send( p_demux
->out
, p_sys
->p_audio
, p_pes
);