7364
|
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.
|
|
4 *
|
|
5 * Uses info from the PVA file specifications found at
|
|
6 *
|
|
7 * http://www.technotrend.de/download/av_format_v1.pdf
|
|
8 *
|
7663
|
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.
|
7364
|
12 *
|
7663
|
13 * Feedback is appreciated.
|
7364
|
14 *
|
|
15 * written by Matteo Giani
|
|
16 */
|
|
17
|
7663
|
18 #define DEMUX_PVA_MULTIDEC_HACK
|
|
19 #define PVA_NEW_PREBYTES_CODE
|
7364
|
20
|
7472
|
21 #include <stdio.h>
|
|
22 #include <stdlib.h>
|
|
23 #include <string.h>
|
7364
|
24
|
|
25 #include "config.h"
|
|
26 #include "mp_msg.h"
|
|
27 #include "help_mp.h"
|
|
28
|
|
29 #include "stream.h"
|
|
30 #include "demuxer.h"
|
|
31 #include "stheader.h"
|
|
32
|
|
33 #include "bswap.h"
|
|
34
|
|
35 /*
|
|
36 * #defines below taken from PVA spec (see URL above)
|
|
37 */
|
|
38
|
|
39 #define PVA_MAX_VIDEO_PACK_LEN 6*1024
|
|
40
|
|
41 #define VIDEOSTREAM 0x01
|
|
42 #define MAINAUDIOSTREAM 0x02
|
|
43
|
|
44 typedef struct {
|
7663
|
45 off_t offset;
|
7364
|
46 long size;
|
|
47 uint8_t type;
|
|
48 uint8_t is_packet_start;
|
7663
|
49 float pts;
|
7364
|
50 } pva_payload_t;
|
|
51
|
|
52
|
|
53 typedef struct {
|
|
54 float last_audio_pts;
|
|
55 float last_video_pts;
|
7663
|
56 #ifdef PVA_NEW_PREBYTES_CODE
|
|
57 float video_pts_after_prebytes;
|
|
58 long video_size_after_prebytes;
|
|
59 uint8_t prebytes_delivered;
|
|
60 #endif
|
7364
|
61 uint8_t just_synced;
|
|
62 uint8_t synced_stream_id;
|
|
63 } pva_priv_t;
|
|
64
|
|
65
|
7663
|
66
|
7364
|
67 int pva_sync(demuxer_t * demuxer)
|
|
68 {
|
|
69 uint8_t buffer[5]={0,0,0,0,0};
|
|
70 int count;
|
|
71 pva_priv_t * priv = (pva_priv_t *) demuxer->priv;
|
|
72
|
|
73
|
7663
|
74 /* This function is used to find the next nearest PVA packet start after a seek, since a PVA file
|
|
75 * is not indexed.
|
|
76 * The just_synced field is in the priv structure so that pva_get_payload knows pva_sync
|
|
77 * has already read (part of) the PVA header. This way we can avoid to seek back and (hopefully)
|
|
78 * be able to read from pipes and such.
|
|
79 */
|
|
80
|
|
81
|
|
82 for(count=0 ; count<PVA_MAX_VIDEO_PACK_LEN && !demuxer->stream->eof && !priv->just_synced ; count++)
|
7364
|
83 {
|
|
84 buffer[0]=buffer[1];
|
|
85 buffer[1]=buffer[2];
|
|
86 buffer[2]=buffer[3];
|
|
87 buffer[3]=buffer[4];
|
|
88 buffer[4]=stream_read_char(demuxer->stream);
|
|
89 /*
|
|
90 * Check for a PVA packet beginning sequence: we check both the "AV" word at the
|
|
91 * very beginning and the "0x55" reserved byte (which is unused and set to 0x55 by spec)
|
|
92 */
|
7663
|
93 if(buffer[0]=='A' && buffer[1] == 'V' && buffer[4] == 0x55) priv->just_synced=1;
|
|
94 //printf("demux_pva: pva_sync(): current offset= %ld\n",stream_tell(demuxer->stream));
|
7364
|
95 }
|
7663
|
96 if(priv->just_synced)
|
7364
|
97 {
|
|
98 if(priv!=NULL) priv->synced_stream_id=buffer[2];
|
|
99 return 1;
|
|
100 }
|
|
101 else
|
|
102 {
|
|
103 return 0;
|
|
104 }
|
|
105 }
|
|
106
|
|
107 int pva_check_file(demuxer_t * demuxer)
|
|
108 {
|
|
109 uint8_t buffer[5]={0,0,0,0,0};
|
|
110 mp_msg(MSGT_DEMUX, MSGL_V, "Checking for PVA\n");
|
|
111 stream_read(demuxer->stream,buffer,5);
|
|
112 if(buffer[0]=='A' && buffer[1] == 'V' && buffer[4] == 0x55)
|
|
113 {
|
|
114 mp_msg(MSGT_DEMUX,MSGL_DBG2, "Success: PVA\n");
|
|
115 return 1;
|
|
116 }
|
|
117 else
|
|
118 {
|
|
119 mp_msg(MSGT_DEMUX,MSGL_DBG2, "Failed: PVA\n");
|
|
120 return 0;
|
|
121 }
|
|
122 }
|
|
123
|
|
124 demuxer_t * demux_open_pva (demuxer_t * demuxer)
|
|
125 {
|
|
126 sh_video_t *sh_video = new_sh_video(demuxer,0);
|
7663
|
127 sh_audio_t *sh_audio = new_sh_audio(demuxer,0);
|
|
128
|
|
129
|
7364
|
130 pva_priv_t * priv;
|
|
131
|
|
132 stream_reset(demuxer->stream);
|
|
133 stream_seek(demuxer->stream,0);
|
|
134
|
|
135
|
|
136
|
|
137 priv=malloc(sizeof(pva_priv_t));
|
|
138
|
|
139 if(demuxer->stream->type!=STREAMTYPE_FILE) demuxer->seekable=0;
|
|
140 else demuxer->seekable=1;
|
|
141
|
|
142 demuxer->priv=priv;
|
|
143 memset(demuxer->priv,0,sizeof(pva_priv_t));
|
|
144
|
|
145 if(!pva_sync(demuxer))
|
|
146 {
|
|
147 mp_msg(MSGT_DEMUX,MSGL_ERR,"Not a PVA file.\n");
|
|
148 return NULL;
|
|
149 }
|
|
150
|
|
151 //printf("priv->just_synced %s after initial sync!\n",priv->just_synced?"set":"UNSET");
|
|
152
|
|
153 demuxer->video->sh=sh_video;
|
7663
|
154
|
|
155 //printf("demuxer->stream->end_pos= %d\n",demuxer->stream->end_pos);
|
|
156
|
|
157
|
7364
|
158 mp_msg(MSGT_DEMUXER,MSGL_INFO,"Opened PVA demuxer...\n");
|
|
159
|
|
160 /*
|
|
161 * Audio and Video codecs:
|
|
162 * the PVA spec only allows MPEG2 video and MPEG layer II audio. No need to check the formats then.
|
|
163 * Moreover, there would be no way to do that since the PVA stream format has no fields to describe
|
|
164 * the used codecs.
|
|
165 */
|
|
166
|
|
167 sh_video->format=0x10000002;
|
|
168 sh_video->ds=demuxer->video;
|
|
169
|
7663
|
170 /*
|
|
171 printf("demuxer->video->id==%d\n",demuxer->video->id);
|
|
172 printf("demuxer->audio->id==%d\n",demuxer->audio->id);
|
|
173 */
|
|
174
|
|
175 demuxer->audio->sh=sh_audio;
|
7364
|
176 sh_audio->format=0x50;
|
|
177 sh_audio->ds=demuxer->audio;
|
7663
|
178
|
|
179 demuxer->movi_start=0;
|
|
180 demuxer->movi_end=demuxer->stream->end_pos;
|
|
181
|
7364
|
182 priv->last_video_pts=-1;
|
|
183 priv->last_audio_pts=-1;
|
|
184
|
|
185 return demuxer;
|
|
186 }
|
|
187
|
7472
|
188 int pva_get_payload(demuxer_t * d,pva_payload_t * payload);
|
|
189
|
7364
|
190 // 0 = EOF or no stream found
|
|
191 // 1 = successfully read a packet
|
|
192 int demux_pva_fill_buffer (demuxer_t * demux)
|
|
193 {
|
|
194 uint8_t done=0;
|
|
195 demux_packet_t * dp;
|
|
196 pva_priv_t * priv=demux->priv;
|
|
197 pva_payload_t current_payload;
|
|
198
|
|
199 while(!done)
|
|
200 {
|
|
201 if(!pva_get_payload(demux,¤t_payload)) return 0;
|
|
202 switch(current_payload.type)
|
|
203 {
|
|
204 case VIDEOSTREAM:
|
7663
|
205 if(demux->video->id==-1) demux->video->id=0;
|
7364
|
206 if(!current_payload.is_packet_start && priv->last_video_pts==-1)
|
|
207 {
|
|
208 /* We should only be here at the beginning of a stream, when we have
|
|
209 * not yet encountered a valid Video PTS, or after a seek.
|
|
210 * So, skip these starting packets in order not to deliver the
|
|
211 * player a bogus PTS.
|
|
212 */
|
|
213 done=0;
|
|
214 }
|
|
215 else
|
|
216 {
|
|
217 /*
|
|
218 * In every other condition, we are delivering the payload. Set this
|
|
219 * so that the following code knows whether to skip it or read it.
|
|
220 */
|
|
221 done=1;
|
|
222 }
|
7663
|
223 if(demux->video->id!=0) done=0;
|
7364
|
224 if(current_payload.is_packet_start)
|
|
225 {
|
7663
|
226 priv->last_video_pts=current_payload.pts;
|
7364
|
227 //mp_msg(MSGT_DEMUXER,MSGL_DBG2,"demux_pva: Video PTS=%llu , delivered %f\n",current_payload.pts,priv->last_video_pts);
|
|
228 }
|
|
229 if(done)
|
|
230 {
|
|
231 dp=new_demux_packet(current_payload.size);
|
|
232 dp->pts=priv->last_video_pts;
|
|
233 stream_read(demux->stream,dp->buffer,current_payload.size);
|
|
234 ds_add_packet(demux->video,dp);
|
|
235 }
|
|
236 else
|
|
237 {
|
|
238 //printf("Skipping %u video bytes\n",current_payload.size);
|
|
239 stream_skip(demux->stream,current_payload.size);
|
|
240 }
|
|
241 break;
|
|
242 case MAINAUDIOSTREAM:
|
7663
|
243 if(demux->audio->id==-1) demux->audio->id=0;
|
7364
|
244 if(!current_payload.is_packet_start && priv->last_audio_pts==-1)
|
|
245 {
|
|
246 /* Same as above for invalid video PTS, just for audio. */
|
|
247 done=0;
|
|
248 }
|
|
249 else
|
|
250 {
|
|
251 done=1;
|
|
252 }
|
|
253 if(current_payload.is_packet_start)
|
|
254 {
|
7663
|
255 priv->last_audio_pts=current_payload.pts;
|
7364
|
256 }
|
7663
|
257 if(demux->audio->id!=0) done=0;
|
7364
|
258 if(done)
|
|
259 {
|
|
260 dp=new_demux_packet(current_payload.size);
|
|
261 dp->pts=priv->last_audio_pts;
|
7663
|
262 if(current_payload.offset != stream_tell(demux->stream))
|
|
263 stream_seek(demux->stream,current_payload.offset);
|
7364
|
264 stream_read(demux->stream,dp->buffer,current_payload.size);
|
|
265 ds_add_packet(demux->audio,dp);
|
|
266 }
|
|
267 else
|
|
268 {
|
|
269 stream_skip(demux->stream,current_payload.size);
|
|
270 }
|
|
271 break;
|
|
272 }
|
|
273 }
|
|
274 return 1;
|
|
275 }
|
|
276
|
|
277 int pva_get_payload(demuxer_t * d,pva_payload_t * payload)
|
|
278 {
|
|
279 uint8_t flags,pes_head_len;
|
|
280 uint16_t pack_size;
|
7663
|
281 off_t next_offset,pva_payload_start;
|
7364
|
282 unsigned char buffer[256];
|
8254
|
283 #ifndef PVA_NEW_PREBYTES_CODE
|
7364
|
284 demux_packet_t * dp; //hack to deliver the preBytes (see PVA doc)
|
8254
|
285 #endif
|
7364
|
286 pva_priv_t * priv=(pva_priv_t *) d->priv;
|
|
287
|
|
288
|
|
289 if(d==NULL)
|
|
290 {
|
7663
|
291 mp_msg(MSGT_DEMUX,MSGL_ERR,"demux_pva: pva_get_payload got passed a NULL pointer!\n");
|
7364
|
292 return 0;
|
|
293 }
|
|
294
|
7663
|
295 d->filepos=stream_tell(d->stream);
|
|
296
|
|
297
|
|
298
|
7364
|
299
|
|
300 if(d->stream->eof)
|
|
301 {
|
|
302 mp_msg(MSGT_DEMUX,MSGL_V,"demux_pva: pva_get_payload() detected stream->eof!!!\n");
|
|
303 return 0;
|
|
304 }
|
|
305
|
|
306 //printf("priv->just_synced %s\n",priv->just_synced?"SET":"UNSET");
|
7663
|
307
|
|
308 #ifdef PVA_NEW_PREBYTES_CODE
|
|
309 if(priv->prebytes_delivered)
|
|
310 /* The previous call to this fn has delivered the preBytes. Then we are already inside
|
|
311 * the payload. Let's just deliver the video along with its right PTS, the one we stored
|
|
312 * in the priv structure and was in the PVA header before the PreBytes.
|
|
313 */
|
|
314 {
|
|
315 //printf("prebytes_delivered=1. Resetting.\n");
|
|
316 payload->size = priv->video_size_after_prebytes;
|
|
317 payload->pts = priv->video_pts_after_prebytes;
|
|
318 payload->is_packet_start = 1;
|
|
319 payload->offset = stream_tell(d->stream);
|
|
320 payload->type = VIDEOSTREAM;
|
|
321 priv->prebytes_delivered = 0;
|
|
322 return 1;
|
|
323 }
|
|
324 #endif
|
7364
|
325 if(!priv->just_synced)
|
|
326 {
|
|
327 if(stream_read_word(d->stream) != (('A'<<8)|'V'))
|
|
328 {
|
|
329 mp_msg(MSGT_DEMUX,MSGL_V,"demux_pva: pva_get_payload() missed a SyncWord at %ld!! Trying to sync...\n",stream_tell(d->stream));
|
7663
|
330 if(!pva_sync(d))
|
|
331 {
|
|
332 if (!d->stream->eof)
|
|
333 {
|
|
334 mp_msg(MSGT_DEMUX,MSGL_ERR,"demux_pva: couldn't sync! (broken file?)");
|
|
335 }
|
|
336 return 0;
|
|
337 }
|
7364
|
338 }
|
|
339 }
|
|
340 if(priv->just_synced)
|
|
341 {
|
|
342 payload->type=priv->synced_stream_id;
|
|
343 priv->just_synced=0;
|
|
344 }
|
|
345 else
|
|
346 {
|
|
347 payload->type=stream_read_char(d->stream);
|
|
348 stream_skip(d->stream,2); //counter and reserved
|
|
349 }
|
|
350 flags=stream_read_char(d->stream);
|
|
351 payload->is_packet_start=flags & 0x10;
|
|
352 pack_size=le2me_16(stream_read_word(d->stream));
|
|
353 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_pva::pva_get_payload(): pack_size=%u field read at offset %lu\n",pack_size,stream_tell(d->stream)-2);
|
|
354 pva_payload_start=stream_tell(d->stream);
|
|
355 next_offset=pva_payload_start+pack_size;
|
7663
|
356
|
|
357
|
|
358 /*
|
|
359 * The code in the #ifdef directive below is a hack needed to get badly formatted PVA files
|
|
360 * such as the ones written by MultiDec played back correctly.
|
|
361 * Basically, it works like this: if the PVA packet does not signal a PES header, but the
|
|
362 * payload looks like one, let's assume it IS one. It has worked for me up to now.
|
|
363 * It can be disabled since it's quite an ugly hack and could potentially break things up
|
|
364 * if the PVA audio payload happens to start with 0x000001 even without being a non signalled
|
|
365 * PES header start.
|
|
366 * Though it's quite unlikely, it potentially could (AFAIK).
|
|
367 */
|
|
368 #ifdef DEMUX_PVA_MULTIDEC_HACK
|
|
369 if(payload->type==MAINAUDIOSTREAM)
|
|
370 {
|
|
371 stream_read(d->stream,buffer,3);
|
|
372 if(buffer[0]==0x00 && buffer[1]==0x00 && buffer[2]==0x01 && !payload->is_packet_start)
|
|
373 {
|
|
374 mp_msg(MSGT_DEMUX,MSGL_V,"demux_pva: suspecting non signaled audio PES packet start. Maybe file by MultiDec?\n");
|
|
375 payload->is_packet_start=1;
|
|
376 }
|
|
377 stream_seek(d->stream,stream_tell(d->stream)-3);
|
|
378 }
|
|
379 #endif
|
|
380
|
|
381
|
7364
|
382 if(!payload->is_packet_start)
|
|
383 {
|
|
384 payload->offset=stream_tell(d->stream);
|
|
385 payload->size=pack_size;
|
|
386 }
|
|
387 else
|
|
388 { //here comes the good part...
|
|
389 switch(payload->type)
|
|
390 {
|
|
391 case VIDEOSTREAM:
|
7663
|
392 payload->pts=(float)(le2me_32(stream_read_dword(d->stream)))/90000;
|
|
393 //printf("Video PTS: %f\n",payload->pts);
|
|
394 if((flags&0x03)
|
|
395 #ifdef PVA_NEW_PREBYTES_CODE
|
|
396 && !priv->prebytes_delivered
|
|
397 #endif
|
|
398 )
|
7364
|
399 {
|
7663
|
400 #ifndef PVA_NEW_PREBYTES_CODE
|
7364
|
401 dp=new_demux_packet(flags&0x03);
|
|
402 stream_read(d->stream,dp->buffer,flags & 0x03); //read PreBytes
|
|
403 ds_add_packet(d->video,dp);
|
7663
|
404 #else
|
|
405 //printf("Delivering prebytes. Setting prebytes_delivered.");
|
|
406 payload->offset=stream_tell(d->stream);
|
|
407 payload->size = flags & 0x03;
|
|
408 priv->video_pts_after_prebytes = payload->pts;
|
|
409 priv->video_size_after_prebytes = pack_size - 4 - (flags & 0x03);
|
|
410 payload->pts=priv->last_video_pts;
|
|
411 payload->is_packet_start=0;
|
|
412 priv->prebytes_delivered=1;
|
|
413 return 1;
|
|
414 #endif
|
7364
|
415 }
|
7663
|
416
|
7364
|
417
|
|
418 //now we are at real beginning of payload.
|
|
419 payload->offset=stream_tell(d->stream);
|
7663
|
420 //size is pack_size minus PTS size minus PreBytes size.
|
7364
|
421 payload->size=pack_size - 4 - (flags & 0x03);
|
|
422 break;
|
|
423 case MAINAUDIOSTREAM:
|
7663
|
424 stream_skip(d->stream,3); //FIXME properly parse PES header.
|
|
425 //printf("StreamID in audio PES header: 0x%2X\n",stream_read_char(d->stream));
|
|
426 stream_skip(d->stream,4);
|
|
427
|
|
428 buffer[255]=stream_read_char(d->stream);
|
7364
|
429 pes_head_len=stream_read_char(d->stream);
|
|
430 stream_read(d->stream,buffer,pes_head_len);
|
7663
|
431 if(!buffer[255]&0x80) //PES header does not contain PTS.
|
|
432 {
|
|
433 mp_msg(MSGT_DEMUX,MSGL_V,"Audio PES packet does not contain PTS. (pes_head_len=%d)\n",pes_head_len);
|
|
434 payload->pts=priv->last_audio_pts;
|
|
435 break;
|
|
436 }
|
|
437 else //PES header DOES contain PTS
|
7364
|
438 {
|
7663
|
439 if((buffer[0] & 0xf0)!=0x20) // PTS badly formatted
|
|
440 {
|
|
441 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]);
|
|
442 payload->pts=priv->last_audio_pts;
|
|
443 // return 0;
|
|
444 }
|
|
445 else
|
|
446 {
|
|
447 uint64_t temp_pts;
|
|
448
|
|
449 temp_pts=0LL;
|
|
450 temp_pts|=((uint64_t)(buffer[0] & 0x0e) << 29);
|
|
451 temp_pts|=buffer[1]<<22;
|
|
452 temp_pts|=(buffer[2] & 0xfe) << 14;
|
|
453 temp_pts|=buffer[3]<<7;
|
|
454 temp_pts|=(buffer[4] & 0xfe) >> 1;
|
|
455 /*
|
|
456 * PTS parsing is hopefully finished.
|
|
457 */
|
|
458 payload->pts=(float)le2me_64(temp_pts)/90000;
|
|
459 }
|
7364
|
460 }
|
|
461 payload->offset=stream_tell(d->stream);
|
|
462 payload->size=pack_size-stream_tell(d->stream)+pva_payload_start;
|
|
463 break;
|
|
464 }
|
|
465 }
|
7472
|
466 return 1;
|
7364
|
467 }
|
|
468
|
|
469 int demux_seek_pva(demuxer_t * demuxer,float rel_seek_secs,int flags)
|
|
470 {
|
|
471 int total_bitrate=0;
|
7663
|
472 off_t dest_offset;
|
7364
|
473 pva_priv_t * priv=demuxer->priv;
|
|
474
|
|
475 total_bitrate=((sh_audio_t *)demuxer->audio->sh)->i_bps + ((sh_video_t *)demuxer->video->sh)->i_bps;
|
|
476
|
|
477 /*
|
|
478 * Compute absolute offset inside the stream. Approximate total bitrate with sum of bitrates
|
|
479 * reported by the audio and video codecs. The seek is not accurate because, just like
|
|
480 * with MPEG streams, the bitrate is not constant. Moreover, we do not take into account
|
|
481 * the overhead caused by PVA and PES headers.
|
|
482 * If the calculated absolute offset is negative, seek to the beginning of the file.
|
|
483 */
|
|
484
|
|
485 dest_offset=stream_tell(demuxer->stream)+rel_seek_secs*total_bitrate;
|
|
486 if(dest_offset<0) dest_offset=0;
|
|
487
|
|
488 stream_seek(demuxer->stream,dest_offset);
|
|
489
|
|
490 if(!pva_sync(demuxer))
|
|
491 {
|
|
492 mp_msg(MSGT_DEMUX,MSGL_V,"demux_pva: Couldn't seek!\n");
|
|
493 return 0;
|
|
494 }
|
|
495
|
|
496 /*
|
|
497 * Reset the PTS info inside the pva_priv_t structure. This way we don't deliver
|
|
498 * data with the wrong PTSs (the ones we had before seeking).
|
|
499 *
|
|
500 */
|
|
501
|
|
502 priv->last_video_pts=-1;
|
|
503 priv->last_audio_pts=-1;
|
|
504
|
|
505 return 1;
|
|
506 }
|
|
507
|
|
508
|
|
509
|
|
510 void demux_close_pva(demuxer_t * demuxer)
|
|
511 {
|
|
512 if(demuxer->priv)
|
|
513 {
|
|
514 free(demuxer->priv);
|
|
515 demuxer->priv=NULL;
|
|
516 }
|
|
517 }
|
|
518
|