core: fix audio-only + framestep weird behavior
[mplayer/glamo.git] / libmpdemux / demux_pva.c
blobdc25d29662298c9aaad89e5afe5dcc218aa30b8d
1 /*
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
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
36 #include "config.h"
37 #include "mp_msg.h"
39 #include "stream/stream.h"
40 #include "demuxer.h"
41 #include "stheader.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
52 typedef struct {
53 off_t offset;
54 long size;
55 uint8_t type;
56 uint8_t is_packet_start;
57 float pts;
58 } pva_payload_t;
61 typedef struct {
62 float last_audio_pts;
63 float last_video_pts;
64 #ifdef PVA_NEW_PREBYTES_CODE
65 float video_pts_after_prebytes;
66 long video_size_after_prebytes;
67 uint8_t prebytes_delivered;
68 #endif
69 uint8_t just_synced;
70 uint8_t synced_stream_id;
71 } pva_priv_t;
75 static int pva_sync(demuxer_t * demuxer)
77 uint8_t buffer[5]={0,0,0,0,0};
78 int count;
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
83 * is not indexed.
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++)
92 buffer[0]=buffer[1];
93 buffer[1]=buffer[2];
94 buffer[2]=buffer[3];
95 buffer[3]=buffer[4];
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];
107 return 1;
109 else
111 return 0;
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;
125 else
127 mp_msg(MSGT_DEMUX,MSGL_DBG2, "Failed: PVA\n");
128 return 0;
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);
138 pva_priv_t * priv;
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;
150 demuxer->priv=priv;
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");
156 return NULL;
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
172 * the used codecs.
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;
194 return demuxer;
197 static int pva_get_payload(demuxer_t *d, pva_payload_t *payload)
199 uint8_t flags,pes_head_len;
200 uint16_t pack_size;
201 off_t next_offset,pva_payload_start;
202 unsigned char buffer[256];
203 #ifndef PVA_NEW_PREBYTES_CODE
204 demux_packet_t * dp; //hack to deliver the preBytes (see PVA doc)
205 #endif
206 pva_priv_t * priv;
209 if(d==NULL)
211 mp_msg(MSGT_DEMUX,MSGL_ERR,"demux_pva: pva_get_payload got passed a NULL pointer!\n");
212 return 0;
215 priv = (pva_priv_t *)d->priv;
216 d->filepos=stream_tell(d->stream);
221 if(d->stream->eof)
223 mp_msg(MSGT_DEMUX,MSGL_V,"demux_pva: pva_get_payload() detected stream->eof!!!\n");
224 return 0;
227 //printf("priv->just_synced %s\n",priv->just_synced?"SET":"UNSET");
229 #ifdef PVA_NEW_PREBYTES_CODE
230 if(priv->prebytes_delivered)
231 /* The previous call to this fn has delivered the preBytes. Then we are already inside
232 * the payload. Let's just deliver the video along with its right PTS, the one we stored
233 * in the priv structure and was in the PVA header before the PreBytes.
236 //printf("prebytes_delivered=1. Resetting.\n");
237 payload->size = priv->video_size_after_prebytes;
238 payload->pts = priv->video_pts_after_prebytes;
239 payload->is_packet_start = 1;
240 payload->offset = stream_tell(d->stream);
241 payload->type = VIDEOSTREAM;
242 priv->prebytes_delivered = 0;
243 return 1;
245 #endif
246 if(!priv->just_synced)
248 if(stream_read_word(d->stream) != (('A'<<8)|'V'))
250 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));
251 if(!pva_sync(d))
253 if (!d->stream->eof)
255 mp_msg(MSGT_DEMUX,MSGL_ERR,"demux_pva: couldn't sync! (broken file?)");
257 return 0;
261 if(priv->just_synced)
263 payload->type=priv->synced_stream_id;
264 priv->just_synced=0;
266 else
268 payload->type=stream_read_char(d->stream);
269 stream_skip(d->stream,2); //counter and reserved
271 flags=stream_read_char(d->stream);
272 payload->is_packet_start=flags & 0x10;
273 pack_size=stream_read_word(d->stream);
274 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);
275 pva_payload_start=stream_tell(d->stream);
276 next_offset=pva_payload_start+pack_size;
280 * The code in the #ifdef directive below is a hack needed to get badly formatted PVA files
281 * such as the ones written by MultiDec played back correctly.
282 * Basically, it works like this: if the PVA packet does not signal a PES header, but the
283 * payload looks like one, let's assume it IS one. It has worked for me up to now.
284 * It can be disabled since it's quite an ugly hack and could potentially break things up
285 * if the PVA audio payload happens to start with 0x000001 even without being a non signalled
286 * PES header start.
287 * Though it's quite unlikely, it potentially could (AFAIK).
289 #ifdef DEMUX_PVA_MULTIDEC_HACK
290 if(payload->type==MAINAUDIOSTREAM)
292 stream_read(d->stream,buffer,3);
293 if(buffer[0]==0x00 && buffer[1]==0x00 && buffer[2]==0x01 && !payload->is_packet_start)
295 mp_msg(MSGT_DEMUX,MSGL_V,"demux_pva: suspecting non signaled audio PES packet start. Maybe file by MultiDec?\n");
296 payload->is_packet_start=1;
298 stream_seek(d->stream,stream_tell(d->stream)-3);
300 #endif
303 if(!payload->is_packet_start)
305 payload->offset=stream_tell(d->stream);
306 payload->size=pack_size;
308 else
309 { //here comes the good part...
310 switch(payload->type)
312 case VIDEOSTREAM:
313 payload->pts=(float)(stream_read_dword(d->stream))/90000;
314 //printf("Video PTS: %f\n",payload->pts);
315 if((flags&0x03)
316 #ifdef PVA_NEW_PREBYTES_CODE
317 && !priv->prebytes_delivered
318 #endif
321 #ifndef PVA_NEW_PREBYTES_CODE
322 dp=new_demux_packet(flags&0x03);
323 stream_read(d->stream,dp->buffer,flags & 0x03); //read PreBytes
324 ds_add_packet(d->video,dp);
325 #else
326 //printf("Delivering prebytes. Setting prebytes_delivered.");
327 payload->offset=stream_tell(d->stream);
328 payload->size = flags & 0x03;
329 priv->video_pts_after_prebytes = payload->pts;
330 priv->video_size_after_prebytes = pack_size - 4 - (flags & 0x03);
331 payload->pts=priv->last_video_pts;
332 payload->is_packet_start=0;
333 priv->prebytes_delivered=1;
334 return 1;
335 #endif
339 //now we are at real beginning of payload.
340 payload->offset=stream_tell(d->stream);
341 //size is pack_size minus PTS size minus PreBytes size.
342 payload->size=pack_size - 4 - (flags & 0x03);
343 break;
344 case MAINAUDIOSTREAM:
345 stream_skip(d->stream,3); //FIXME properly parse PES header.
346 //printf("StreamID in audio PES header: 0x%2X\n",stream_read_char(d->stream));
347 stream_skip(d->stream,4);
349 buffer[255]=stream_read_char(d->stream);
350 pes_head_len=stream_read_char(d->stream);
351 stream_read(d->stream,buffer,pes_head_len);
352 if(!(buffer[255]&0x80)) //PES header does not contain PTS.
354 mp_msg(MSGT_DEMUX,MSGL_V,"Audio PES packet does not contain PTS. (pes_head_len=%d)\n",pes_head_len);
355 payload->pts=priv->last_audio_pts;
356 break;
358 else //PES header DOES contain PTS
360 if((buffer[0] & 0xf0)!=0x20) // PTS badly formatted
362 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]);
363 payload->pts=priv->last_audio_pts;
364 // return 0;
366 else
368 uint64_t temp_pts;
370 temp_pts=0LL;
371 temp_pts|=((uint64_t)(buffer[0] & 0x0e) << 29);
372 temp_pts|=buffer[1]<<22;
373 temp_pts|=(buffer[2] & 0xfe) << 14;
374 temp_pts|=buffer[3]<<7;
375 temp_pts|=(buffer[4] & 0xfe) >> 1;
377 * PTS parsing is hopefully finished.
379 payload->pts=(float)temp_pts/90000;
382 payload->offset=stream_tell(d->stream);
383 payload->size=pack_size-stream_tell(d->stream)+pva_payload_start;
384 break;
387 return 1;
390 // 0 = EOF or no stream found
391 // 1 = successfully read a packet
392 static int demux_pva_fill_buffer (demuxer_t * demux, demux_stream_t *ds)
394 uint8_t done=0;
395 demux_packet_t * dp;
396 pva_priv_t * priv=demux->priv;
397 pva_payload_t current_payload;
399 while(!done)
401 if(!pva_get_payload(demux,&current_payload)) return 0;
402 switch(current_payload.type)
404 case VIDEOSTREAM:
405 if(demux->video->id==-1) demux->video->id=0;
406 if(!current_payload.is_packet_start && priv->last_video_pts==-1)
408 /* We should only be here at the beginning of a stream, when we have
409 * not yet encountered a valid Video PTS, or after a seek.
410 * So, skip these starting packets in order not to deliver the
411 * player a bogus PTS.
413 done=0;
415 else
418 * In every other condition, we are delivering the payload. Set this
419 * so that the following code knows whether to skip it or read it.
421 done=1;
423 if(demux->video->id!=0) done=0;
424 if(current_payload.is_packet_start)
426 priv->last_video_pts=current_payload.pts;
427 //mp_msg(MSGT_DEMUXER,MSGL_DBG2,"demux_pva: Video PTS=%llu , delivered %f\n",current_payload.pts,priv->last_video_pts);
429 if(done)
431 dp=new_demux_packet(current_payload.size);
432 dp->pts=priv->last_video_pts;
433 stream_read(demux->stream,dp->buffer,current_payload.size);
434 ds_add_packet(demux->video,dp);
436 else
438 //printf("Skipping %u video bytes\n",current_payload.size);
439 stream_skip(demux->stream,current_payload.size);
441 break;
442 case MAINAUDIOSTREAM:
443 if(demux->audio->id==-1) demux->audio->id=0;
444 if(!current_payload.is_packet_start && priv->last_audio_pts==-1)
446 /* Same as above for invalid video PTS, just for audio. */
447 done=0;
449 else
451 done=1;
453 if(current_payload.is_packet_start)
455 priv->last_audio_pts=current_payload.pts;
457 if(demux->audio->id!=0) done=0;
458 if(done)
460 dp=new_demux_packet(current_payload.size);
461 dp->pts=priv->last_audio_pts;
462 if(current_payload.offset != stream_tell(demux->stream))
463 stream_seek(demux->stream,current_payload.offset);
464 stream_read(demux->stream,dp->buffer,current_payload.size);
465 ds_add_packet(demux->audio,dp);
467 else
469 stream_skip(demux->stream,current_payload.size);
471 break;
474 return 1;
477 static void demux_seek_pva(demuxer_t * demuxer,float rel_seek_secs,float audio_delay,int flags)
479 int total_bitrate=0;
480 off_t dest_offset;
481 pva_priv_t * priv=demuxer->priv;
483 total_bitrate=((sh_audio_t *)demuxer->audio->sh)->i_bps + ((sh_video_t *)demuxer->video->sh)->i_bps;
486 * Compute absolute offset inside the stream. Approximate total bitrate with sum of bitrates
487 * reported by the audio and video codecs. The seek is not accurate because, just like
488 * with MPEG streams, the bitrate is not constant. Moreover, we do not take into account
489 * the overhead caused by PVA and PES headers.
490 * If the calculated absolute offset is negative, seek to the beginning of the file.
493 dest_offset=stream_tell(demuxer->stream)+rel_seek_secs*total_bitrate;
494 if(dest_offset<0) dest_offset=0;
496 stream_seek(demuxer->stream,dest_offset);
498 if(!pva_sync(demuxer))
500 mp_msg(MSGT_DEMUX,MSGL_V,"demux_pva: Couldn't seek!\n");
501 return;
505 * Reset the PTS info inside the pva_priv_t structure. This way we don't deliver
506 * data with the wrong PTSs (the ones we had before seeking).
510 priv->last_video_pts=-1;
511 priv->last_audio_pts=-1;
516 static void demux_close_pva(demuxer_t * demuxer)
518 free(demuxer->priv);
519 demuxer->priv = NULL;
523 const demuxer_desc_t demuxer_desc_pva = {
524 "PVA demuxer",
525 "pva",
526 "PVA",
527 "Matteo Giani",
528 "streams from DVB cards",
529 DEMUXER_TYPE_PVA,
530 0, // unsafe autodetect
531 pva_check_file,
532 demux_pva_fill_buffer,
533 demux_open_pva,
534 demux_close_pva,
535 demux_seek_pva,
536 NULL