Add encoder support for Dirac using the Schroedinger library.
[vlc/asuraparaju-public.git] / modules / demux / pva.c
blobb02f8ddff17820d5a8e270ce64eef2f9ff9d9505
1 /*****************************************************************************
2 * pva.c: PVA demuxer
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
36 /*****************************************************************************
37 * Module descriptor
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close ( vlc_object_t * );
42 vlc_module_begin ()
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 )
48 add_shortcut( "pva" )
49 vlc_module_end ()
51 /*****************************************************************************
52 * Local prototypes
53 *****************************************************************************/
55 struct demux_sys_t
57 es_out_id_t *p_video;
58 es_out_id_t *p_audio;
60 /* counter */
61 int i_vc;
62 int i_ac;
64 /* audio/video block */
65 block_t *p_pes; /* audio */
66 block_t *p_es; /* video */
68 int64_t b_pcr_audio;
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 /*****************************************************************************
78 * Open
79 *****************************************************************************/
80 static int Open( vlc_object_t *p_this )
82 demux_t *p_demux = (demux_t*)p_this;
83 demux_sys_t *p_sys;
84 es_format_t fmt;
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 ) )
92 return VLC_EGENERIC;
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_CODEC_MPGA );
102 fmt.b_packetized = false;
103 p_sys->p_audio = es_out_Add( p_demux->out, &fmt );
105 es_format_Init( &fmt, VIDEO_ES, VLC_CODEC_MPGV );
106 fmt.b_packetized = false;
107 p_sys->p_video = es_out_Add( p_demux->out, &fmt );
109 p_sys->i_vc = -1;
110 p_sys->i_ac = -1;
111 p_sys->p_pes = NULL;
112 p_sys->p_es = NULL;
114 p_sys->b_pcr_audio = false;
116 return VLC_SUCCESS;
119 /*****************************************************************************
120 * Close
121 *****************************************************************************/
122 static void Close( vlc_object_t *p_this )
124 demux_t *p_demux = (demux_t*)p_this;
125 demux_sys_t *p_sys = p_demux->p_sys;
127 if( p_sys->p_es ) block_Release( p_sys->p_es );
128 if( p_sys->p_pes ) block_Release( p_sys->p_pes );
130 free( p_sys );
134 /*****************************************************************************
135 * Demux:
136 *****************************************************************************/
137 static int Demux( demux_t *p_demux )
139 demux_sys_t *p_sys = p_demux->p_sys;
141 const uint8_t *p_peek;
142 int i_size;
143 block_t *p_frame;
144 int64_t i_pts;
145 int i_skip;
147 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
149 msg_Warn( p_demux, "eof ?" );
150 return 0;
152 if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
154 msg_Warn( p_demux, "lost synchro" );
155 if( ReSynch( p_demux ) )
157 return -1;
159 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
161 msg_Warn( p_demux, "eof ?" );
162 return 0;
166 i_size = GetWBE( &p_peek[6] );
167 switch( p_peek[2] )
169 case 0x01: /* VideoStream */
170 if( p_sys->i_vc < 0 )
172 msg_Dbg( p_demux, "first packet for video" );
174 else if( ((p_sys->i_vc + 1)&0xff) != p_peek[3] )
176 msg_Dbg( p_demux, "packet lost (video)" );
177 if( p_sys->p_es )
179 block_ChainRelease( p_sys->p_es );
180 p_sys->p_es = NULL;
183 p_sys->i_vc = p_peek[3];
185 /* read the PTS and potential extra bytes TODO: make it a bit more optimised */
186 i_pts = -1;
187 i_skip = 8;
188 if( p_peek[5]&0x10 )
190 int i_pre = p_peek[5]&0x3;
192 if( ( p_frame = stream_Block( p_demux->s, 8 + 4 + i_pre ) ) )
194 i_pts = GetDWBE( &p_frame->p_buffer[8] );
195 if( p_frame->i_buffer > 12 )
197 p_frame->p_buffer += 12;
198 p_frame->i_buffer -= 12;
199 block_ChainAppend( &p_sys->p_es, p_frame );
201 else
203 block_Release( p_frame );
206 i_size -= 4 + i_pre;
207 i_skip = 0;
208 /* Set PCR */
209 if( ( p_frame = p_sys->p_es ) )
212 if( p_frame->i_pts > VLC_TS_INVALID && !p_sys->b_pcr_audio )
214 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_frame->i_pts);
216 es_out_Send( p_demux->out, p_sys->p_video, p_frame );
218 p_sys->p_es = NULL;
222 if( ( p_frame = stream_Block( p_demux->s, i_size + i_skip ) ) )
224 p_frame->p_buffer += i_skip;
225 p_frame->i_buffer -= i_skip;
226 if( i_pts >= 0 )
227 p_frame->i_pts = VLC_TS_0 + i_pts * 100 / 9;
228 block_ChainAppend( &p_sys->p_es, p_frame );
230 break;
232 case 0x02: /* MainAudioStream */
233 if( p_sys->i_ac < 0 )
235 msg_Dbg( p_demux, "first packet for audio" );
237 else if( ((p_sys->i_ac + 1)&0xff) != p_peek[3] )
239 msg_Dbg( p_demux, "packet lost (audio)" );
240 if( p_sys->p_pes )
242 block_ChainRelease( p_sys->p_pes );
243 p_sys->p_pes = NULL;
246 p_sys->i_ac = p_peek[3];
248 if( p_peek[5]&0x10 && p_sys->p_pes )
250 ParsePES( p_demux );
252 if( ( p_frame = stream_Block( p_demux->s, i_size + 8 ) ) )
254 p_frame->p_buffer += 8;
255 p_frame->i_buffer -= 8;
256 /* XXX this a hack, some streams aren't compliant and
257 * doesn't set pes_start flag */
258 if( p_sys->p_pes && p_frame->i_buffer > 4 &&
259 p_frame->p_buffer[0] == 0x00 &&
260 p_frame->p_buffer[1] == 0x00 &&
261 p_frame->p_buffer[2] == 0x01 )
263 ParsePES( p_demux );
265 block_ChainAppend( &p_sys->p_pes, p_frame );
267 break;
269 default:
270 msg_Warn( p_demux, "unknown id=0x%x", p_peek[2] );
271 stream_Read( p_demux->s, NULL, i_size + 8 );
272 break;
274 return 1;
277 /*****************************************************************************
278 * Control:
279 *****************************************************************************/
280 static int Control( demux_t *p_demux, int i_query, va_list args )
282 /* demux_sys_t *p_sys = p_demux->p_sys; */
283 double f, *pf;
284 int64_t i64;
285 switch( i_query )
287 case DEMUX_GET_POSITION:
288 if( ( i64 = stream_Size( p_demux->s ) ) > 0 )
290 pf = (double*) va_arg( args, double* );
291 double current = stream_Tell( p_demux->s );
292 *pf = current / (double)i64;
293 return VLC_SUCCESS;
295 return VLC_EGENERIC;
297 case DEMUX_SET_POSITION:
298 f = (double) va_arg( args, double );
299 i64 = stream_Size( p_demux->s );
301 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
303 return VLC_EGENERIC;
305 return VLC_SUCCESS;
307 #if 0
308 case DEMUX_GET_TIME:
309 pi64 = (int64_t*)va_arg( args, int64_t * );
310 if( p_sys->i_time < 0 )
312 *pi64 = 0;
313 return VLC_EGENERIC;
315 *pi64 = p_sys->i_time;
316 return VLC_SUCCESS;
318 #if 0
319 case DEMUX_GET_LENGTH:
320 pi64 = (int64_t*)va_arg( args, int64_t * );
321 if( p_sys->i_mux_rate > 0 )
323 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
324 return VLC_SUCCESS;
326 *pi64 = 0;
327 return VLC_EGENERIC;
329 #endif
330 case DEMUX_GET_FPS:
331 pf = (double*)va_arg( args, double * );
332 *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
333 return VLC_SUCCESS;
334 #endif
335 case DEMUX_SET_TIME:
336 default:
337 return VLC_EGENERIC;
341 /*****************************************************************************
342 * ReSynch:
343 *****************************************************************************/
344 static int ReSynch( demux_t *p_demux )
346 const uint8_t *p_peek;
347 int i_skip;
348 int i_peek;
350 while( vlc_object_alive (p_demux) )
352 if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 1024 ) ) < 8 )
354 return VLC_EGENERIC;
356 i_skip = 0;
358 while( i_skip < i_peek - 5 )
360 if( p_peek[0] == 'A' && p_peek[1] == 'V' && p_peek[4] == 0x55 )
362 if( i_skip > 0 )
364 stream_Read( p_demux->s, NULL, i_skip );
366 return VLC_SUCCESS;
368 p_peek++;
369 i_skip++;
372 stream_Read( p_demux->s, NULL, i_skip );
375 return VLC_EGENERIC;
378 static void ParsePES( demux_t *p_demux )
380 demux_sys_t *p_sys = p_demux->p_sys;
381 block_t *p_pes = p_sys->p_pes;
382 uint8_t hdr[30];
383 int i_pes_size;
385 unsigned i_skip;
386 mtime_t i_dts = -1;
387 mtime_t i_pts = -1;
389 p_sys->p_pes = NULL;
391 /* FIXME find real max size */
392 block_ChainExtract( p_pes, hdr, 30 );
394 if( hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 1 )
396 msg_Warn( p_demux, "invalid hdr [0x%2.2x:%2.2x:%2.2x:%2.2x]",
397 hdr[0], hdr[1],hdr[2],hdr[3] );
398 block_ChainRelease( p_pes );
399 return;
401 i_pes_size = GetWBE( &hdr[4] );
403 /* we assume mpeg2 PES */
404 i_skip = hdr[8] + 9;
405 if( hdr[7]&0x80 ) /* has pts */
407 i_pts = ((mtime_t)(hdr[ 9]&0x0e ) << 29)|
408 (mtime_t)(hdr[10] << 22)|
409 ((mtime_t)(hdr[11]&0xfe) << 14)|
410 (mtime_t)(hdr[12] << 7)|
411 (mtime_t)(hdr[12] >> 1);
413 if( hdr[7]&0x40 ) /* has dts */
415 i_dts = ((mtime_t)(hdr[14]&0x0e ) << 29)|
416 (mtime_t)(hdr[15] << 22)|
417 ((mtime_t)(hdr[16]&0xfe) << 14)|
418 (mtime_t)(hdr[17] << 7)|
419 (mtime_t)(hdr[18] >> 1);
423 p_pes = block_ChainGather( p_pes );
424 if( p_pes->i_buffer <= i_skip )
426 block_ChainRelease( p_pes );
427 return;
430 p_pes->i_buffer -= i_skip;
431 p_pes->p_buffer += i_skip;
433 if( i_dts >= 0 )
434 p_pes->i_dts = VLC_TS_0 + i_dts * 100 / 9;
435 if( i_pts >= 0 )
436 p_pes->i_pts = VLC_TS_0 + i_pts * 100 / 9;
438 /* Set PCR */
439 if( p_pes->i_pts > 0 )
441 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_pes->i_pts);
442 p_sys->b_pcr_audio = true;
444 es_out_Send( p_demux->out, p_sys->p_audio, p_pes );