Add explanatory comments to the #endif part of multiple inclusion guards.
[mplayer/greg.git] / libmpdemux / demux_pva.c
blob6a9762b652d2b1a22f32b68ca1f4aeabba3d2ea4
1 /*
2 * demuxer for PVA files, such as the ones produced by software to manage DVB boards
3 * like the Hauppauge WinTV DVBs, for MPlayer.
5 * Uses info from the PVA file specifications found at
6 *
7 * http://www.technotrend.de/download/av_format_v1.pdf
8 *
9 * WARNING: Quite a hack was required in order to get files by MultiDec played back correctly.
10 * If it breaks anything else, just comment out the "#define DEMUX_PVA_MULTIDEC_HACK" below
11 * and it will not be compiled in.
13 * Feedback is appreciated.
15 * written by Matteo Giani
18 #define DEMUX_PVA_MULTIDEC_HACK
19 #define PVA_NEW_PREBYTES_CODE
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include "config.h"
26 #include "mp_msg.h"
27 #include "help_mp.h"
29 #include "stream/stream.h"
30 #include "demuxer.h"
31 #include "stheader.h"
34 * #defines below taken from PVA spec (see URL above)
37 #define PVA_MAX_VIDEO_PACK_LEN 6*1024
39 #define VIDEOSTREAM 0x01
40 #define MAINAUDIOSTREAM 0x02
42 typedef struct {
43 off_t offset;
44 long size;
45 uint8_t type;
46 uint8_t is_packet_start;
47 float pts;
48 } pva_payload_t;
51 typedef struct {
52 float last_audio_pts;
53 float last_video_pts;
54 #ifdef PVA_NEW_PREBYTES_CODE
55 float video_pts_after_prebytes;
56 long video_size_after_prebytes;
57 uint8_t prebytes_delivered;
58 #endif
59 uint8_t just_synced;
60 uint8_t synced_stream_id;
61 } pva_priv_t;
65 int pva_sync(demuxer_t * demuxer)
67 uint8_t buffer[5]={0,0,0,0,0};
68 int count;
69 pva_priv_t * priv = (pva_priv_t *) demuxer->priv;
72 /* This function is used to find the next nearest PVA packet start after a seek, since a PVA file
73 * is not indexed.
74 * The just_synced field is in the priv structure so that pva_get_payload knows pva_sync
75 * has already read (part of) the PVA header. This way we can avoid to seek back and (hopefully)
76 * be able to read from pipes and such.
80 for(count=0 ; count<PVA_MAX_VIDEO_PACK_LEN && !demuxer->stream->eof && !priv->just_synced ; count++)
82 buffer[0]=buffer[1];
83 buffer[1]=buffer[2];
84 buffer[2]=buffer[3];
85 buffer[3]=buffer[4];
86 buffer[4]=stream_read_char(demuxer->stream);
88 * Check for a PVA packet beginning sequence: we check both the "AV" word at the
89 * very beginning and the "0x55" reserved byte (which is unused and set to 0x55 by spec)
91 if(buffer[0]=='A' && buffer[1] == 'V' && buffer[4] == 0x55) priv->just_synced=1;
92 //printf("demux_pva: pva_sync(): current offset= %ld\n",stream_tell(demuxer->stream));
94 if(priv->just_synced)
96 priv->synced_stream_id=buffer[2];
97 return 1;
99 else
101 return 0;
105 static int pva_check_file(demuxer_t * demuxer)
107 uint8_t buffer[5]={0,0,0,0,0};
108 mp_msg(MSGT_DEMUX, MSGL_V, "Checking for PVA\n");
109 stream_read(demuxer->stream,buffer,5);
110 if(buffer[0]=='A' && buffer[1] == 'V' && buffer[4] == 0x55)
112 mp_msg(MSGT_DEMUX,MSGL_DBG2, "Success: PVA\n");
113 return DEMUXER_TYPE_PVA;
115 else
117 mp_msg(MSGT_DEMUX,MSGL_DBG2, "Failed: PVA\n");
118 return 0;
122 static demuxer_t * demux_open_pva (demuxer_t * demuxer)
124 sh_video_t *sh_video = new_sh_video(demuxer,0);
125 sh_audio_t *sh_audio = new_sh_audio(demuxer,0);
128 pva_priv_t * priv;
130 stream_reset(demuxer->stream);
131 stream_seek(demuxer->stream,0);
135 priv=malloc(sizeof(pva_priv_t));
137 if(demuxer->stream->type!=STREAMTYPE_FILE) demuxer->seekable=0;
138 else demuxer->seekable=1;
140 demuxer->priv=priv;
141 memset(demuxer->priv,0,sizeof(pva_priv_t));
143 if(!pva_sync(demuxer))
145 mp_msg(MSGT_DEMUX,MSGL_ERR,"Not a PVA file.\n");
146 return NULL;
149 //printf("priv->just_synced %s after initial sync!\n",priv->just_synced?"set":"UNSET");
151 demuxer->video->sh=sh_video;
153 //printf("demuxer->stream->end_pos= %d\n",demuxer->stream->end_pos);
156 mp_msg(MSGT_DEMUXER,MSGL_INFO,"Opened PVA demuxer...\n");
159 * Audio and Video codecs:
160 * the PVA spec only allows MPEG2 video and MPEG layer II audio. No need to check the formats then.
161 * Moreover, there would be no way to do that since the PVA stream format has no fields to describe
162 * the used codecs.
165 sh_video->format=0x10000002;
166 sh_video->ds=demuxer->video;
169 printf("demuxer->video->id==%d\n",demuxer->video->id);
170 printf("demuxer->audio->id==%d\n",demuxer->audio->id);
173 demuxer->audio->sh=sh_audio;
174 sh_audio->format=0x50;
175 sh_audio->ds=demuxer->audio;
177 demuxer->movi_start=0;
178 demuxer->movi_end=demuxer->stream->end_pos;
180 priv->last_video_pts=-1;
181 priv->last_audio_pts=-1;
183 return demuxer;
186 int pva_get_payload(demuxer_t * d,pva_payload_t * payload);
188 // 0 = EOF or no stream found
189 // 1 = successfully read a packet
190 static int demux_pva_fill_buffer (demuxer_t * demux, demux_stream_t *ds)
192 uint8_t done=0;
193 demux_packet_t * dp;
194 pva_priv_t * priv=demux->priv;
195 pva_payload_t current_payload;
197 while(!done)
199 if(!pva_get_payload(demux,&current_payload)) return 0;
200 switch(current_payload.type)
202 case VIDEOSTREAM:
203 if(demux->video->id==-1) demux->video->id=0;
204 if(!current_payload.is_packet_start && priv->last_video_pts==-1)
206 /* We should only be here at the beginning of a stream, when we have
207 * not yet encountered a valid Video PTS, or after a seek.
208 * So, skip these starting packets in order not to deliver the
209 * player a bogus PTS.
211 done=0;
213 else
216 * In every other condition, we are delivering the payload. Set this
217 * so that the following code knows whether to skip it or read it.
219 done=1;
221 if(demux->video->id!=0) done=0;
222 if(current_payload.is_packet_start)
224 priv->last_video_pts=current_payload.pts;
225 //mp_msg(MSGT_DEMUXER,MSGL_DBG2,"demux_pva: Video PTS=%llu , delivered %f\n",current_payload.pts,priv->last_video_pts);
227 if(done)
229 dp=new_demux_packet(current_payload.size);
230 dp->pts=priv->last_video_pts;
231 stream_read(demux->stream,dp->buffer,current_payload.size);
232 ds_add_packet(demux->video,dp);
234 else
236 //printf("Skipping %u video bytes\n",current_payload.size);
237 stream_skip(demux->stream,current_payload.size);
239 break;
240 case MAINAUDIOSTREAM:
241 if(demux->audio->id==-1) demux->audio->id=0;
242 if(!current_payload.is_packet_start && priv->last_audio_pts==-1)
244 /* Same as above for invalid video PTS, just for audio. */
245 done=0;
247 else
249 done=1;
251 if(current_payload.is_packet_start)
253 priv->last_audio_pts=current_payload.pts;
255 if(demux->audio->id!=0) done=0;
256 if(done)
258 dp=new_demux_packet(current_payload.size);
259 dp->pts=priv->last_audio_pts;
260 if(current_payload.offset != stream_tell(demux->stream))
261 stream_seek(demux->stream,current_payload.offset);
262 stream_read(demux->stream,dp->buffer,current_payload.size);
263 ds_add_packet(demux->audio,dp);
265 else
267 stream_skip(demux->stream,current_payload.size);
269 break;
272 return 1;
275 int pva_get_payload(demuxer_t * d,pva_payload_t * payload)
277 uint8_t flags,pes_head_len;
278 uint16_t pack_size;
279 off_t next_offset,pva_payload_start;
280 unsigned char buffer[256];
281 #ifndef PVA_NEW_PREBYTES_CODE
282 demux_packet_t * dp; //hack to deliver the preBytes (see PVA doc)
283 #endif
284 pva_priv_t * priv;
287 if(d==NULL)
289 mp_msg(MSGT_DEMUX,MSGL_ERR,"demux_pva: pva_get_payload got passed a NULL pointer!\n");
290 return 0;
293 priv = (pva_priv_t *)d->priv;
294 d->filepos=stream_tell(d->stream);
299 if(d->stream->eof)
301 mp_msg(MSGT_DEMUX,MSGL_V,"demux_pva: pva_get_payload() detected stream->eof!!!\n");
302 return 0;
305 //printf("priv->just_synced %s\n",priv->just_synced?"SET":"UNSET");
307 #ifdef PVA_NEW_PREBYTES_CODE
308 if(priv->prebytes_delivered)
309 /* The previous call to this fn has delivered the preBytes. Then we are already inside
310 * the payload. Let's just deliver the video along with its right PTS, the one we stored
311 * in the priv structure and was in the PVA header before the PreBytes.
314 //printf("prebytes_delivered=1. Resetting.\n");
315 payload->size = priv->video_size_after_prebytes;
316 payload->pts = priv->video_pts_after_prebytes;
317 payload->is_packet_start = 1;
318 payload->offset = stream_tell(d->stream);
319 payload->type = VIDEOSTREAM;
320 priv->prebytes_delivered = 0;
321 return 1;
323 #endif
324 if(!priv->just_synced)
326 if(stream_read_word(d->stream) != (('A'<<8)|'V'))
328 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));
329 if(!pva_sync(d))
331 if (!d->stream->eof)
333 mp_msg(MSGT_DEMUX,MSGL_ERR,"demux_pva: couldn't sync! (broken file?)");
335 return 0;
339 if(priv->just_synced)
341 payload->type=priv->synced_stream_id;
342 priv->just_synced=0;
344 else
346 payload->type=stream_read_char(d->stream);
347 stream_skip(d->stream,2); //counter and reserved
349 flags=stream_read_char(d->stream);
350 payload->is_packet_start=flags & 0x10;
351 pack_size=le2me_16(stream_read_word(d->stream));
352 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);
353 pva_payload_start=stream_tell(d->stream);
354 next_offset=pva_payload_start+pack_size;
358 * The code in the #ifdef directive below is a hack needed to get badly formatted PVA files
359 * such as the ones written by MultiDec played back correctly.
360 * Basically, it works like this: if the PVA packet does not signal a PES header, but the
361 * payload looks like one, let's assume it IS one. It has worked for me up to now.
362 * It can be disabled since it's quite an ugly hack and could potentially break things up
363 * if the PVA audio payload happens to start with 0x000001 even without being a non signalled
364 * PES header start.
365 * Though it's quite unlikely, it potentially could (AFAIK).
367 #ifdef DEMUX_PVA_MULTIDEC_HACK
368 if(payload->type==MAINAUDIOSTREAM)
370 stream_read(d->stream,buffer,3);
371 if(buffer[0]==0x00 && buffer[1]==0x00 && buffer[2]==0x01 && !payload->is_packet_start)
373 mp_msg(MSGT_DEMUX,MSGL_V,"demux_pva: suspecting non signaled audio PES packet start. Maybe file by MultiDec?\n");
374 payload->is_packet_start=1;
376 stream_seek(d->stream,stream_tell(d->stream)-3);
378 #endif
381 if(!payload->is_packet_start)
383 payload->offset=stream_tell(d->stream);
384 payload->size=pack_size;
386 else
387 { //here comes the good part...
388 switch(payload->type)
390 case VIDEOSTREAM:
391 payload->pts=(float)(le2me_32(stream_read_dword(d->stream)))/90000;
392 //printf("Video PTS: %f\n",payload->pts);
393 if((flags&0x03)
394 #ifdef PVA_NEW_PREBYTES_CODE
395 && !priv->prebytes_delivered
396 #endif
399 #ifndef PVA_NEW_PREBYTES_CODE
400 dp=new_demux_packet(flags&0x03);
401 stream_read(d->stream,dp->buffer,flags & 0x03); //read PreBytes
402 ds_add_packet(d->video,dp);
403 #else
404 //printf("Delivering prebytes. Setting prebytes_delivered.");
405 payload->offset=stream_tell(d->stream);
406 payload->size = flags & 0x03;
407 priv->video_pts_after_prebytes = payload->pts;
408 priv->video_size_after_prebytes = pack_size - 4 - (flags & 0x03);
409 payload->pts=priv->last_video_pts;
410 payload->is_packet_start=0;
411 priv->prebytes_delivered=1;
412 return 1;
413 #endif
417 //now we are at real beginning of payload.
418 payload->offset=stream_tell(d->stream);
419 //size is pack_size minus PTS size minus PreBytes size.
420 payload->size=pack_size - 4 - (flags & 0x03);
421 break;
422 case MAINAUDIOSTREAM:
423 stream_skip(d->stream,3); //FIXME properly parse PES header.
424 //printf("StreamID in audio PES header: 0x%2X\n",stream_read_char(d->stream));
425 stream_skip(d->stream,4);
427 buffer[255]=stream_read_char(d->stream);
428 pes_head_len=stream_read_char(d->stream);
429 stream_read(d->stream,buffer,pes_head_len);
430 if(!buffer[255]&0x80) //PES header does not contain PTS.
432 mp_msg(MSGT_DEMUX,MSGL_V,"Audio PES packet does not contain PTS. (pes_head_len=%d)\n",pes_head_len);
433 payload->pts=priv->last_audio_pts;
434 break;
436 else //PES header DOES contain PTS
438 if((buffer[0] & 0xf0)!=0x20) // PTS badly formatted
440 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]);
441 payload->pts=priv->last_audio_pts;
442 // return 0;
444 else
446 uint64_t temp_pts;
448 temp_pts=0LL;
449 temp_pts|=((uint64_t)(buffer[0] & 0x0e) << 29);
450 temp_pts|=buffer[1]<<22;
451 temp_pts|=(buffer[2] & 0xfe) << 14;
452 temp_pts|=buffer[3]<<7;
453 temp_pts|=(buffer[4] & 0xfe) >> 1;
455 * PTS parsing is hopefully finished.
457 payload->pts=(float)le2me_64(temp_pts)/90000;
460 payload->offset=stream_tell(d->stream);
461 payload->size=pack_size-stream_tell(d->stream)+pva_payload_start;
462 break;
465 return 1;
468 static void demux_seek_pva(demuxer_t * demuxer,float rel_seek_secs,float audio_delay,int flags)
470 int total_bitrate=0;
471 off_t dest_offset;
472 pva_priv_t * priv=demuxer->priv;
474 total_bitrate=((sh_audio_t *)demuxer->audio->sh)->i_bps + ((sh_video_t *)demuxer->video->sh)->i_bps;
477 * Compute absolute offset inside the stream. Approximate total bitrate with sum of bitrates
478 * reported by the audio and video codecs. The seek is not accurate because, just like
479 * with MPEG streams, the bitrate is not constant. Moreover, we do not take into account
480 * the overhead caused by PVA and PES headers.
481 * If the calculated absolute offset is negative, seek to the beginning of the file.
484 dest_offset=stream_tell(demuxer->stream)+rel_seek_secs*total_bitrate;
485 if(dest_offset<0) dest_offset=0;
487 stream_seek(demuxer->stream,dest_offset);
489 if(!pva_sync(demuxer))
491 mp_msg(MSGT_DEMUX,MSGL_V,"demux_pva: Couldn't seek!\n");
492 return;
496 * Reset the PTS info inside the pva_priv_t structure. This way we don't deliver
497 * data with the wrong PTSs (the ones we had before seeking).
501 priv->last_video_pts=-1;
502 priv->last_audio_pts=-1;
507 static void demux_close_pva(demuxer_t * demuxer)
509 if(demuxer->priv)
511 free(demuxer->priv);
512 demuxer->priv=NULL;
517 demuxer_desc_t demuxer_desc_pva = {
518 "PVA demuxer",
519 "pva",
520 "PVA",
521 "Matteo Giani",
522 "streams from DVB cards",
523 DEMUXER_TYPE_PVA,
524 0, // unsafe autodetect
525 pva_check_file,
526 demux_pva_fill_buffer,
527 demux_open_pva,
528 demux_close_pva,
529 demux_seek_pva,
530 NULL