2 * demuxer for PVA files, such as the ones produced by software to manage
3 * DVB boards like the Hauppauge WinTV DVBs
4 * copyright (c) 2002 Matteo Giani
6 * Uses info from the PVA file specifications found at
7 * http://www.technotrend.de/download/av_format_v1.pdf
9 * This file is part of MPlayer.
11 * MPlayer is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * MPlayer is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 /* WARNING: Quite a hack was required in order to get files by MultiDec
27 * played back correctly. If it breaks anything else, just comment out
28 * the #define below and it will not be compiled in. */
29 #define DEMUX_PVA_MULTIDEC_HACK
30 #define PVA_NEW_PREBYTES_CODE
39 #include "stream/stream.h"
44 * #defines below taken from PVA spec (see URL above)
47 #define PVA_MAX_VIDEO_PACK_LEN 6*1024
49 #define VIDEOSTREAM 0x01
50 #define MAINAUDIOSTREAM 0x02
56 uint8_t is_packet_start
;
64 #ifdef PVA_NEW_PREBYTES_CODE
65 float video_pts_after_prebytes
;
66 long video_size_after_prebytes
;
67 uint8_t prebytes_delivered
;
70 uint8_t synced_stream_id
;
75 static int pva_sync(demuxer_t
* demuxer
)
77 uint8_t buffer
[5]={0,0,0,0,0};
79 pva_priv_t
* priv
= (pva_priv_t
*) demuxer
->priv
;
82 /* This function is used to find the next nearest PVA packet start after a seek, since a PVA file
84 * The just_synced field is in the priv structure so that pva_get_payload knows pva_sync
85 * has already read (part of) the PVA header. This way we can avoid to seek back and (hopefully)
86 * be able to read from pipes and such.
90 for(count
=0 ; count
<PVA_MAX_VIDEO_PACK_LEN
&& !demuxer
->stream
->eof
&& !priv
->just_synced
; count
++)
96 buffer
[4]=stream_read_char(demuxer
->stream
);
98 * Check for a PVA packet beginning sequence: we check both the "AV" word at the
99 * very beginning and the "0x55" reserved byte (which is unused and set to 0x55 by spec)
101 if(buffer
[0]=='A' && buffer
[1] == 'V' && buffer
[4] == 0x55) priv
->just_synced
=1;
102 //printf("demux_pva: pva_sync(): current offset= %ld\n",stream_tell(demuxer->stream));
104 if(priv
->just_synced
)
106 priv
->synced_stream_id
=buffer
[2];
115 static int pva_check_file(demuxer_t
* demuxer
)
117 uint8_t buffer
[5]={0,0,0,0,0};
118 mp_msg(MSGT_DEMUX
, MSGL_V
, "Checking for PVA\n");
119 stream_read(demuxer
->stream
,buffer
,5);
120 if(buffer
[0]=='A' && buffer
[1] == 'V' && buffer
[4] == 0x55)
122 mp_msg(MSGT_DEMUX
,MSGL_DBG2
, "Success: PVA\n");
123 return DEMUXER_TYPE_PVA
;
127 mp_msg(MSGT_DEMUX
,MSGL_DBG2
, "Failed: PVA\n");
132 static demuxer_t
* demux_open_pva (demuxer_t
* demuxer
)
134 sh_video_t
*sh_video
= new_sh_video(demuxer
,0);
135 sh_audio_t
*sh_audio
= new_sh_audio(demuxer
,0);
140 stream_reset(demuxer
->stream
);
141 stream_seek(demuxer
->stream
,0);
145 priv
=malloc(sizeof(pva_priv_t
));
147 if(demuxer
->stream
->type
!=STREAMTYPE_FILE
) demuxer
->seekable
=0;
148 else demuxer
->seekable
=1;
151 memset(demuxer
->priv
,0,sizeof(pva_priv_t
));
153 if(!pva_sync(demuxer
))
155 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"Not a PVA file.\n");
159 //printf("priv->just_synced %s after initial sync!\n",priv->just_synced?"set":"UNSET");
161 demuxer
->video
->sh
=sh_video
;
163 //printf("demuxer->stream->end_pos= %d\n",demuxer->stream->end_pos);
166 mp_msg(MSGT_DEMUXER
,MSGL_INFO
,"Opened PVA demuxer...\n");
169 * Audio and Video codecs:
170 * the PVA spec only allows MPEG2 video and MPEG layer II audio. No need to check the formats then.
171 * Moreover, there would be no way to do that since the PVA stream format has no fields to describe
175 sh_video
->format
=0x10000002;
176 sh_video
->ds
=demuxer
->video
;
179 printf("demuxer->video->id==%d\n",demuxer->video->id);
180 printf("demuxer->audio->id==%d\n",demuxer->audio->id);
183 demuxer
->audio
->id
= 0;
184 demuxer
->audio
->sh
=sh_audio
;
185 sh_audio
->format
=0x50;
186 sh_audio
->ds
=demuxer
->audio
;
188 demuxer
->movi_start
=0;
189 demuxer
->movi_end
=demuxer
->stream
->end_pos
;
191 priv
->last_video_pts
=-1;
192 priv
->last_audio_pts
=-1;
197 int pva_get_payload(demuxer_t
* d
,pva_payload_t
* payload
);
199 // 0 = EOF or no stream found
200 // 1 = successfully read a packet
201 static int demux_pva_fill_buffer (demuxer_t
* demux
, demux_stream_t
*ds
)
205 pva_priv_t
* priv
=demux
->priv
;
206 pva_payload_t current_payload
;
210 if(!pva_get_payload(demux
,¤t_payload
)) return 0;
211 switch(current_payload
.type
)
214 if(demux
->video
->id
==-1) demux
->video
->id
=0;
215 if(!current_payload
.is_packet_start
&& priv
->last_video_pts
==-1)
217 /* We should only be here at the beginning of a stream, when we have
218 * not yet encountered a valid Video PTS, or after a seek.
219 * So, skip these starting packets in order not to deliver the
220 * player a bogus PTS.
227 * In every other condition, we are delivering the payload. Set this
228 * so that the following code knows whether to skip it or read it.
232 if(demux
->video
->id
!=0) done
=0;
233 if(current_payload
.is_packet_start
)
235 priv
->last_video_pts
=current_payload
.pts
;
236 //mp_msg(MSGT_DEMUXER,MSGL_DBG2,"demux_pva: Video PTS=%llu , delivered %f\n",current_payload.pts,priv->last_video_pts);
240 dp
=new_demux_packet(current_payload
.size
);
241 dp
->pts
=priv
->last_video_pts
;
242 stream_read(demux
->stream
,dp
->buffer
,current_payload
.size
);
243 ds_add_packet(demux
->video
,dp
);
247 //printf("Skipping %u video bytes\n",current_payload.size);
248 stream_skip(demux
->stream
,current_payload
.size
);
251 case MAINAUDIOSTREAM
:
252 if(demux
->audio
->id
==-1) demux
->audio
->id
=0;
253 if(!current_payload
.is_packet_start
&& priv
->last_audio_pts
==-1)
255 /* Same as above for invalid video PTS, just for audio. */
262 if(current_payload
.is_packet_start
)
264 priv
->last_audio_pts
=current_payload
.pts
;
266 if(demux
->audio
->id
!=0) done
=0;
269 dp
=new_demux_packet(current_payload
.size
);
270 dp
->pts
=priv
->last_audio_pts
;
271 if(current_payload
.offset
!= stream_tell(demux
->stream
))
272 stream_seek(demux
->stream
,current_payload
.offset
);
273 stream_read(demux
->stream
,dp
->buffer
,current_payload
.size
);
274 ds_add_packet(demux
->audio
,dp
);
278 stream_skip(demux
->stream
,current_payload
.size
);
286 int pva_get_payload(demuxer_t
* d
,pva_payload_t
* payload
)
288 uint8_t flags
,pes_head_len
;
290 off_t next_offset
,pva_payload_start
;
291 unsigned char buffer
[256];
292 #ifndef PVA_NEW_PREBYTES_CODE
293 demux_packet_t
* dp
; //hack to deliver the preBytes (see PVA doc)
300 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"demux_pva: pva_get_payload got passed a NULL pointer!\n");
304 priv
= (pva_priv_t
*)d
->priv
;
305 d
->filepos
=stream_tell(d
->stream
);
312 mp_msg(MSGT_DEMUX
,MSGL_V
,"demux_pva: pva_get_payload() detected stream->eof!!!\n");
316 //printf("priv->just_synced %s\n",priv->just_synced?"SET":"UNSET");
318 #ifdef PVA_NEW_PREBYTES_CODE
319 if(priv
->prebytes_delivered
)
320 /* The previous call to this fn has delivered the preBytes. Then we are already inside
321 * the payload. Let's just deliver the video along with its right PTS, the one we stored
322 * in the priv structure and was in the PVA header before the PreBytes.
325 //printf("prebytes_delivered=1. Resetting.\n");
326 payload
->size
= priv
->video_size_after_prebytes
;
327 payload
->pts
= priv
->video_pts_after_prebytes
;
328 payload
->is_packet_start
= 1;
329 payload
->offset
= stream_tell(d
->stream
);
330 payload
->type
= VIDEOSTREAM
;
331 priv
->prebytes_delivered
= 0;
335 if(!priv
->just_synced
)
337 if(stream_read_word(d
->stream
) != (('A'<<8)|'V'))
339 mp_msg(MSGT_DEMUX
,MSGL_V
,"demux_pva: pva_get_payload() missed a SyncWord at %"PRId64
"!! Trying to sync...\n",(int64_t)stream_tell(d
->stream
));
344 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"demux_pva: couldn't sync! (broken file?)");
350 if(priv
->just_synced
)
352 payload
->type
=priv
->synced_stream_id
;
357 payload
->type
=stream_read_char(d
->stream
);
358 stream_skip(d
->stream
,2); //counter and reserved
360 flags
=stream_read_char(d
->stream
);
361 payload
->is_packet_start
=flags
& 0x10;
362 pack_size
=le2me_16(stream_read_word(d
->stream
));
363 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"demux_pva::pva_get_payload(): pack_size=%u field read at offset %"PRIu64
"\n",pack_size
,(int64_t)stream_tell(d
->stream
)-2);
364 pva_payload_start
=stream_tell(d
->stream
);
365 next_offset
=pva_payload_start
+pack_size
;
369 * The code in the #ifdef directive below is a hack needed to get badly formatted PVA files
370 * such as the ones written by MultiDec played back correctly.
371 * Basically, it works like this: if the PVA packet does not signal a PES header, but the
372 * payload looks like one, let's assume it IS one. It has worked for me up to now.
373 * It can be disabled since it's quite an ugly hack and could potentially break things up
374 * if the PVA audio payload happens to start with 0x000001 even without being a non signalled
376 * Though it's quite unlikely, it potentially could (AFAIK).
378 #ifdef DEMUX_PVA_MULTIDEC_HACK
379 if(payload
->type
==MAINAUDIOSTREAM
)
381 stream_read(d
->stream
,buffer
,3);
382 if(buffer
[0]==0x00 && buffer
[1]==0x00 && buffer
[2]==0x01 && !payload
->is_packet_start
)
384 mp_msg(MSGT_DEMUX
,MSGL_V
,"demux_pva: suspecting non signaled audio PES packet start. Maybe file by MultiDec?\n");
385 payload
->is_packet_start
=1;
387 stream_seek(d
->stream
,stream_tell(d
->stream
)-3);
392 if(!payload
->is_packet_start
)
394 payload
->offset
=stream_tell(d
->stream
);
395 payload
->size
=pack_size
;
398 { //here comes the good part...
399 switch(payload
->type
)
402 payload
->pts
=(float)(le2me_32(stream_read_dword(d
->stream
)))/90000;
403 //printf("Video PTS: %f\n",payload->pts);
405 #ifdef PVA_NEW_PREBYTES_CODE
406 && !priv
->prebytes_delivered
410 #ifndef PVA_NEW_PREBYTES_CODE
411 dp
=new_demux_packet(flags
&0x03);
412 stream_read(d
->stream
,dp
->buffer
,flags
& 0x03); //read PreBytes
413 ds_add_packet(d
->video
,dp
);
415 //printf("Delivering prebytes. Setting prebytes_delivered.");
416 payload
->offset
=stream_tell(d
->stream
);
417 payload
->size
= flags
& 0x03;
418 priv
->video_pts_after_prebytes
= payload
->pts
;
419 priv
->video_size_after_prebytes
= pack_size
- 4 - (flags
& 0x03);
420 payload
->pts
=priv
->last_video_pts
;
421 payload
->is_packet_start
=0;
422 priv
->prebytes_delivered
=1;
428 //now we are at real beginning of payload.
429 payload
->offset
=stream_tell(d
->stream
);
430 //size is pack_size minus PTS size minus PreBytes size.
431 payload
->size
=pack_size
- 4 - (flags
& 0x03);
433 case MAINAUDIOSTREAM
:
434 stream_skip(d
->stream
,3); //FIXME properly parse PES header.
435 //printf("StreamID in audio PES header: 0x%2X\n",stream_read_char(d->stream));
436 stream_skip(d
->stream
,4);
438 buffer
[255]=stream_read_char(d
->stream
);
439 pes_head_len
=stream_read_char(d
->stream
);
440 stream_read(d
->stream
,buffer
,pes_head_len
);
441 if(!(buffer
[255]&0x80)) //PES header does not contain PTS.
443 mp_msg(MSGT_DEMUX
,MSGL_V
,"Audio PES packet does not contain PTS. (pes_head_len=%d)\n",pes_head_len
);
444 payload
->pts
=priv
->last_audio_pts
;
447 else //PES header DOES contain PTS
449 if((buffer
[0] & 0xf0)!=0x20) // PTS badly formatted
451 mp_msg(MSGT_DEMUX
,MSGL_V
,"demux_pva: expected audio PTS but badly formatted... (read 0x%02X). Falling back to previous PTS (hack).\n",buffer
[0]);
452 payload
->pts
=priv
->last_audio_pts
;
460 temp_pts
|=((uint64_t)(buffer
[0] & 0x0e) << 29);
461 temp_pts
|=buffer
[1]<<22;
462 temp_pts
|=(buffer
[2] & 0xfe) << 14;
463 temp_pts
|=buffer
[3]<<7;
464 temp_pts
|=(buffer
[4] & 0xfe) >> 1;
466 * PTS parsing is hopefully finished.
468 payload
->pts
=(float)le2me_64(temp_pts
)/90000;
471 payload
->offset
=stream_tell(d
->stream
);
472 payload
->size
=pack_size
-stream_tell(d
->stream
)+pva_payload_start
;
479 static void demux_seek_pva(demuxer_t
* demuxer
,float rel_seek_secs
,float audio_delay
,int flags
)
483 pva_priv_t
* priv
=demuxer
->priv
;
485 total_bitrate
=((sh_audio_t
*)demuxer
->audio
->sh
)->i_bps
+ ((sh_video_t
*)demuxer
->video
->sh
)->i_bps
;
488 * Compute absolute offset inside the stream. Approximate total bitrate with sum of bitrates
489 * reported by the audio and video codecs. The seek is not accurate because, just like
490 * with MPEG streams, the bitrate is not constant. Moreover, we do not take into account
491 * the overhead caused by PVA and PES headers.
492 * If the calculated absolute offset is negative, seek to the beginning of the file.
495 dest_offset
=stream_tell(demuxer
->stream
)+rel_seek_secs
*total_bitrate
;
496 if(dest_offset
<0) dest_offset
=0;
498 stream_seek(demuxer
->stream
,dest_offset
);
500 if(!pva_sync(demuxer
))
502 mp_msg(MSGT_DEMUX
,MSGL_V
,"demux_pva: Couldn't seek!\n");
507 * Reset the PTS info inside the pva_priv_t structure. This way we don't deliver
508 * data with the wrong PTSs (the ones we had before seeking).
512 priv
->last_video_pts
=-1;
513 priv
->last_audio_pts
=-1;
518 static void demux_close_pva(demuxer_t
* demuxer
)
528 const demuxer_desc_t demuxer_desc_pva
= {
533 "streams from DVB cards",
535 0, // unsafe autodetect
537 demux_pva_fill_buffer
,