1 // MPG/VOB file parser for DEMUXER v2.5 by A'rpi/ESP-team
12 #include "stream/stream.h"
18 //#define MAX_PS_PACKETSIZE 2048
19 #define MAX_PS_PACKETSIZE (224*1024)
22 #define VIDEO_MPEG1 0x10000001
23 #define VIDEO_MPEG2 0x10000002
24 #define VIDEO_MPEG4 0x10000004
25 #define VIDEO_H264 0x10000005
26 #define AUDIO_MP2 0x50
27 #define AUDIO_A52 0x2000
28 #define AUDIO_LPCM_BE 0x10001
29 #define AUDIO_AAC mmioFOURCC('M', 'P', '4', 'A')
31 typedef struct mpg_demuxer
{
33 float first_pts
; // first pts found in stream
34 float first_to_final_pts_len
; // difference between final pts and first pts
35 int has_valid_timestamps
; // !=0 iff time stamps look linear
36 // (not necessarily starting with 0)
37 unsigned int es_map
[0x40]; //es map of stream types (associated to the pes id) from 0xb0 to 0xef
39 int a_stream_ids
[MAX_A_STREAMS
];
42 extern char* dvdsub_lang
;
43 static int mpeg_pts_error
=0;
46 static int parse_psm(demuxer_t
*demux
, int len
) {
47 unsigned char c
, id
, type
;
48 unsigned int plen
, prog_len
, es_map_len
;
49 mpg_demuxer_t
*priv
= (mpg_demuxer_t
*) demux
->priv
;
51 mp_dbg(MSGT_DEMUX
,MSGL_V
, "PARSE_PSM, len=%d\n", len
);
52 if(! len
|| len
> 1018)
55 c
= stream_read_char(demux
->stream
);
57 stream_skip(demux
->stream
, len
- 1); //not yet valid, discard
60 stream_skip(demux
->stream
, 1);
61 prog_len
= stream_read_word(demux
->stream
); //length of program descriptors
62 stream_skip(demux
->stream
, prog_len
); //.. that we ignore
63 es_map_len
= stream_read_word(demux
->stream
); //length of elementary streams map
64 es_map_len
= FFMIN(es_map_len
, len
- prog_len
- 8); //sanity check
65 while(es_map_len
> 0) {
66 type
= stream_read_char(demux
->stream
);
67 id
= stream_read_char(demux
->stream
);
68 if(id
>= 0xB0 && id
<= 0xEF && priv
) {
69 int idoffset
= id
- 0xB0;
72 priv
->es_map
[idoffset
] = VIDEO_MPEG1
;
75 priv
->es_map
[idoffset
] = VIDEO_MPEG2
;
79 priv
->es_map
[idoffset
] = AUDIO_MP2
;
83 priv
->es_map
[idoffset
] = AUDIO_AAC
;
86 priv
->es_map
[idoffset
] = VIDEO_MPEG4
;
89 priv
->es_map
[idoffset
] = VIDEO_H264
;
92 priv
->es_map
[idoffset
] = AUDIO_A52
;
95 mp_dbg(MSGT_DEMUX
,MSGL_V
, "PSM ES, id=0x%x, type=%x, stype: %x\n", id
, type
, priv
->es_map
[idoffset
]);
97 plen
= stream_read_word(demux
->stream
); //length of elementary stream descriptors
98 plen
= FFMIN(plen
, es_map_len
); //sanity check
99 stream_skip(demux
->stream
, plen
); //skip descriptors for now
100 es_map_len
-= 4 + plen
;
102 stream_skip(demux
->stream
, 4); //skip crc32
106 // 500000 is a wild guess
107 #define TIMESTAMP_PROBE_LEN 500000
109 //MAX_PTS_DIFF_FOR_CONSECUTIVE denotes the maximum difference
110 //between two pts to consider them consecutive
111 //1.0 is a wild guess
112 #define MAX_PTS_DIFF_FOR_CONSECUTIVE 1.0
114 //returns the first pts found within TIME_STAMP_PROBE_LEN bytes after stream_pos in demuxer's stream.
115 //if no pts is found or an error occurs, -1.0 is returned.
117 static float read_first_mpeg_pts_at_position(demuxer_t
* demuxer
, off_t stream_pos
)
119 stream_t
*s
= demuxer
->stream
;
120 mpg_demuxer_t
*mpg_d
= demuxer
->priv
;
121 float pts
= -1.0; //the pts to return;
122 float found_pts1
; //the most recently found pts
123 float found_pts2
; //the pts found before found_pts1
124 float found_pts3
; //the pts found before found_pts2
127 if(!mpg_d
|| stream_pos
< 0)
130 found_pts3
= found_pts2
= found_pts1
= mpg_d
->last_pts
;
131 stream_seek(s
, stream_pos
);
134 //However, we do not stop at the first found one, as timestamps may reset
135 //Therefore, we seek until we found three consecutive
136 //pts within MAX_PTS_DIFF_FOR_CONSECUTIVE.
138 while(found
<3 && !s
->eof
139 && (fabsf(found_pts2
-found_pts1
) < MAX_PTS_DIFF_FOR_CONSECUTIVE
)
140 && (fabsf(found_pts3
-found_pts2
) < MAX_PTS_DIFF_FOR_CONSECUTIVE
)
141 && (stream_tell(s
) < stream_pos
+ TIMESTAMP_PROBE_LEN
)
142 && ds_fill_buffer(demuxer
->video
))
144 if(mpg_d
->last_pts
!= found_pts1
)
147 found_pts3
= found_pts2
= found_pts1
= mpg_d
->last_pts
; //the most recently found pts
150 found_pts3
= found_pts2
;
151 found_pts2
= found_pts1
;
152 found_pts1
= mpg_d
->last_pts
;
158 if(found
== 3) pts
= found_pts3
;
160 //clean up from searching of first pts;
161 demux_flush(demuxer
);
166 /// Open an mpg physical stream
167 static demuxer_t
* demux_mpg_open(demuxer_t
* demuxer
) {
168 stream_t
*s
= demuxer
->stream
;
169 mpg_demuxer_t
* mpg_d
;
171 if (!ds_fill_buffer(demuxer
->video
)) return 0;
172 mpg_d
= calloc(1,sizeof(mpg_demuxer_t
));
175 demuxer
->priv
= mpg_d
;
176 mpg_d
->last_pts
= -1.0;
177 mpg_d
->first_pts
= -1.0;
179 //if seeking is allowed set has_valid_timestamps if appropriate
181 && (demuxer
->stream
->type
== STREAMTYPE_FILE
182 || demuxer
->stream
->type
== STREAMTYPE_VCD
)
183 && demuxer
->movi_start
!= demuxer
-> movi_end
186 //We seek to the beginning of the stream, to somewhere in the
187 //middle, and to the end of the stream, while remembering the pts
188 //at each of the three positions. With these pts, we check whether
189 //or not the pts are "linear enough" to justify seeking by the pts
192 //The position where the stream is now
193 off_t pos
= stream_tell(s
);
194 float first_pts
= read_first_mpeg_pts_at_position(demuxer
, demuxer
->movi_start
);
195 if(first_pts
!= -1.0)
197 float middle_pts
= read_first_mpeg_pts_at_position(demuxer
, (demuxer
->movi_end
+ demuxer
->movi_start
)/2);
198 if(middle_pts
!= -1.0)
200 float final_pts
= read_first_mpeg_pts_at_position(demuxer
, demuxer
->movi_end
- TIMESTAMP_PROBE_LEN
);
201 if(final_pts
!= -1.0)
203 // found proper first, middle, and final pts.
204 float proportion
= (middle_pts
-first_pts
==0) ? -1 : (final_pts
-middle_pts
)/(middle_pts
-first_pts
);
205 // if they are linear enough set has_valid_timestamps
206 if((0.5 < proportion
) && (proportion
< 2))
208 mpg_d
->first_pts
= first_pts
;
209 mpg_d
->first_to_final_pts_len
= final_pts
- first_pts
;
210 mpg_d
->has_valid_timestamps
= 1;
216 //Cleaning up from seeking in stream
217 demuxer
->stream
->eof
=0;
218 demuxer
->video
->eof
=0;
219 demuxer
->audio
->eof
=0;
222 ds_fill_buffer(demuxer
->video
);
223 } // if ( demuxer->seekable )
228 static void demux_close_mpg(demuxer_t
* demuxer
) {
229 mpg_demuxer_t
* mpg_d
= demuxer
->priv
;
230 if (mpg_d
) free(mpg_d
);
234 static unsigned long long read_mpeg_timestamp(stream_t
*s
,int c
){
236 unsigned long long pts
;
237 d
=stream_read_word(s
);
238 e
=stream_read_word(s
);
239 if( ((c
&1)!=1) || ((d
&1)!=1) || ((e
&1)!=1) ){
241 return 0; // invalid pts
243 pts
=(((uint64_t)((c
>>1)&7))<<30)|((d
>>1)<<15)|(e
>>1);
244 mp_dbg(MSGT_DEMUX
,MSGL_DBG3
," pts {%"PRIu64
"}",pts
);
248 static void new_audio_stream(demuxer_t
*demux
, int aid
){
249 if(!demux
->a_streams
[aid
]){
250 mpg_demuxer_t
*mpg_d
=(mpg_demuxer_t
*)demux
->priv
;
252 new_sh_audio(demux
,aid
);
253 sh_a
= (sh_audio_t
*)demux
->a_streams
[aid
];
254 switch(aid
& 0xE0){ // 1110 0000 b (high 3 bit: type low 5: id)
255 case 0x00: sh_a
->format
=0x50;break; // mpeg
256 case 0xA0: sh_a
->format
=0x10001;break; // dvd pcm
257 case 0x80: if((aid
& 0xF8) == 0x88) sh_a
->format
=0x2001;//dts
258 else sh_a
->format
=0x2000;break; // ac3
261 if((aid
& 0xC0) == 0xC0) sh_a
->format
=0x2000;
262 else if(aid
>= 0x98 && aid
<= 0x9f) sh_a
->format
=0x2001;
263 if (mpg_d
) mpg_d
->a_stream_ids
[mpg_d
->num_a_streams
++] = aid
;
265 if(demux
->audio
->id
==-1) demux
->audio
->id
=aid
;
268 static int demux_mpg_read_packet(demuxer_t
*demux
,int id
){
271 int set_pts
=0; // !=0 iff pts has been set to a proper value
273 unsigned long long pts
=0;
274 unsigned long long dts
=0;
276 int pes_ext2_subid
=-1;
277 double stream_pts
= MP_NOPTS_VALUE
;
278 demux_stream_t
*ds
=NULL
;
280 mpg_demuxer_t
*priv
= (mpg_demuxer_t
*) demux
->priv
;
282 mp_dbg(MSGT_DEMUX
,MSGL_DBG3
,"demux_read_packet: %X\n",id
);
285 // demux->synced=0; // force resync after 0x1F0
289 // if(id==0x1BA) packet_start_pos=stream_tell(demux->stream);
290 if((id
<0x1BC || id
>=0x1F0) && id
!= 0x1FD) return -1;
291 if(id
==0x1BE) return -1; // padding stream
292 if(id
==0x1BF) return -1; // private2
294 len
=stream_read_word(demux
->stream
);
295 mp_dbg(MSGT_DEMUX
,MSGL_DBG3
,"PACKET len=%d",len
);
296 // if(len==62480){ demux->synced=0;return -1;} /* :) */
297 if(len
==0 || len
>MAX_PS_PACKETSIZE
){
298 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
,"Invalid PS packet len: %d\n",len
);
299 return -2; // invalid packet !!!!!!
305 parse_psm(demux
, len
);
309 while(len
>0){ // Skip stuFFing bytes
310 c
=stream_read_char(demux
->stream
);
314 if((c
>>6)==1){ // Read (skip) STD scale & size value
315 // printf(" STD_scale=%d",(c>>5)&1);
316 d
=((c
&0x1F)<<8)|stream_read_char(demux
->stream
);
318 // printf(" STD_size=%d",d);
319 c
=stream_read_char(demux
->stream
);
321 // Read System-1 stream timestamps:
323 pts
=read_mpeg_timestamp(demux
->stream
,c
);
328 pts
=read_mpeg_timestamp(demux
->stream
,c
);
329 c
=stream_read_char(demux
->stream
);
330 if((c
>>4)!=1) pts
=0; //printf("{ERROR4}");
332 dts
=read_mpeg_timestamp(demux
->stream
,c
);
339 // System-2 (.VOB) stream:
340 c
=stream_read_char(demux
->stream
);
342 parse_ext2
= (id
== 0x1FD) && ((c
& 0x3F) == 1);
343 c
=stream_read_char(demux
->stream
);
346 mp_dbg(MSGT_DEMUX
,MSGL_DBG3
," hdrlen=%d (len=%d)",hdrlen
,len
);
347 if(hdrlen
>len
){ mp_msg(MSGT_DEMUX
,MSGL_V
,"demux_mpg: invalid header length \n"); return -1;}
348 if(pts_flags
==2 && hdrlen
>=5){
349 c
=stream_read_char(demux
->stream
);
350 pts
=read_mpeg_timestamp(demux
->stream
,c
);
354 if(pts_flags
==3 && hdrlen
>=10){
355 c
=stream_read_char(demux
->stream
);
356 pts
=read_mpeg_timestamp(demux
->stream
,c
);
358 c
=stream_read_char(demux
->stream
);
359 dts
=read_mpeg_timestamp(demux
->stream
,c
);
363 if(parse_ext2
&& hdrlen
>=3) {
364 c
=stream_read_char(demux
->stream
);
367 if((c
& 0x0F) != 0x0F) {
368 mp_msg(MSGT_DEMUX
,MSGL_V
,"demux_mpg: pes_extension_flag2 not set, discarding pes packet\n");
371 if(c
& 0x80) { //pes_private_data_flag
373 mp_msg(MSGT_DEMUX
,MSGL_V
,"demux_mpg: not enough pes_private_data bytes: %d < 16, discarding pes packet\n", hdrlen
);
376 stream_skip(demux
->stream
, 16);
379 if(c
& 0x40) { //pack_header_field_flag
380 int l
= stream_read_char(demux
->stream
);
381 if(l
< 0) //couldn't read from the stream?
384 if(l
< 0 || hdrlen
< l
) {
385 mp_msg(MSGT_DEMUX
,MSGL_V
,"demux_mpg: not enough pack_header bytes: hdrlen: %d < skip: %d, discarding pes packet\n",
389 stream_skip(demux
->stream
, l
);
392 if(c
& 0x20) { //program_packet_sequence_counter_flag
394 mp_msg(MSGT_DEMUX
,MSGL_V
,"demux_mpg: not enough program_packet bytes: hdrlen: %d, discarding pes packet\n", hdrlen
);
397 stream_skip(demux
->stream
, 2);
402 stream_skip(demux
->stream
, 2);
405 c
=stream_read_char(demux
->stream
); //pes_extension2 flag
407 if(c
!=0x81) { mp_msg(MSGT_DEMUX
,MSGL_V
,"demux_mpg: unknown pes_extension2 format, len is > 1 \n"); return -1;}
408 c
=stream_read_char(demux
->stream
); //pes_extension2 payload === substream id
410 if(c
<0x55 || c
>0x5F) { mp_msg(MSGT_DEMUX
,MSGL_V
,"demux_mpg: unknown vc1 substream_id: 0x%x \n", c
); return -1;}
414 stream_skip(demux
->stream
,hdrlen
); // skip header and stuffing bytes
416 if(id
==0x1FD && pes_ext2_subid
!=-1) {
417 //==== EVO VC1 STREAMS ===//
418 if(!demux
->v_streams
[pes_ext2_subid
]) new_sh_video(demux
,pes_ext2_subid
);
419 if(demux
->video
->id
==-1) demux
->video
->id
=pes_ext2_subid
;
420 if(demux
->video
->id
==pes_ext2_subid
){
422 if(!ds
->sh
) ds
->sh
=demux
->v_streams
[pes_ext2_subid
];
424 sh_video_t
*sh
= (sh_video_t
*)ds
->sh
;
425 sh
->format
= mmioFOURCC('W', 'V', 'C', '1');
429 //============== DVD Audio sub-stream ======================
435 tmppos
= stream_tell(demux
->stream
);
436 tmp
= stream_read_word(demux
->stream
);
437 stream_seek(demux
->stream
, tmppos
);
438 /// vdr stores A52 without the 4 header bytes, so we have to check this condition first
444 aid
=stream_read_char(demux
->stream
);--len
;
445 if(len
<3) return -1; // invalid audio packet
449 // 0x20..0x3F subtitle
450 // 0x80..0x87 and 0xC0..0xCF AC3 audio
451 // 0x88..0x8F and 0x98..0x9F DTS audio
452 // 0xA0..0xBF PCM audio
454 if((aid
& 0xE0) == 0x20){
458 if(!demux
->s_streams
[aid
]){
459 sh_sub_t
*sh
= new_sh_sub(demux
, aid
);
460 if (sh
) sh
->type
= 'v';
461 mp_msg(MSGT_DEMUX
,MSGL_V
,"==> Found subtitle: %d\n",aid
);
464 if(demux
->sub
->id
> -1)
465 demux
->sub
->id
&= 0x1F;
466 if(!dvdsub_lang
&& demux
->sub
->id
== -1)
467 demux
->sub
->id
= aid
;
468 if(demux
->sub
->id
==aid
){
471 } else if((aid
>= 0x80 && aid
<= 0x8F) || (aid
>= 0x98 && aid
<= 0xAF) || (aid
>= 0xC0 && aid
<= 0xCF)) {
473 // aid=128+(aid&0x7F);
475 new_audio_stream(demux
, aid
);
476 if(demux
->audio
->id
==aid
){
479 if(!ds
->sh
) ds
->sh
=demux
->a_streams
[aid
];
480 // READ Packet: Skip additional audio header data:
482 c
=stream_read_char(demux
->stream
);//num of frames
483 type
=stream_read_char(demux
->stream
);//startpos hi
484 type
=(type
<<8)|stream_read_char(demux
->stream
);//startpos lo
485 // printf("\r[%02X][%04X]",c,type);
488 if((aid
&0xE0)==0xA0 && len
>=3){
490 // save audio header as codecdata!
491 if(!((sh_audio_t
*)(ds
->sh
))->codecdata_len
){
492 ((sh_audio_t
*)(ds
->sh
))->codecdata
=malloc(3);
493 ((sh_audio_t
*)(ds
->sh
))->codecdata_len
=3;
495 hdr
=((sh_audio_t
*)(ds
->sh
))->codecdata
;
497 // emphasis[1], mute[1], rvd[1], frame number[5]:
498 hdr
[0]=stream_read_char(demux
->stream
);
499 // printf(" [%01X:%02d]",c>>5,c&31);
500 // quantization[2],freq[2],rvd[1],channels[3]
501 hdr
[1]=stream_read_char(demux
->stream
);
502 // printf("[%01X:%01X] ",c>>4,c&15);
503 // dynamic range control (0x80=off):
504 hdr
[2]=stream_read_char(demux
->stream
);
505 // printf("[%02X] ",c);
507 if(len
<=0) mp_msg(MSGT_DEMUX
,MSGL_V
,"End of packet while searching for PCM header\n");
510 } // if(demux->audio->id==aid)
512 } else mp_msg(MSGT_DEMUX
,MSGL_V
,"Unknown 0x1BD substream: 0x%02X \n",aid
);
516 mp_msg(MSGT_DEMUX
,MSGL_V
," {ERROR5,c=%d} \n",c
);
517 return -1; // invalid packet !!!!!!
520 if(mpeg_pts_error
) mp_msg(MSGT_DEMUX
,MSGL_V
," {PTS_err:%d} \n",mpeg_pts_error
);
521 mp_dbg(MSGT_DEMUX
,MSGL_DBG3
," => len=%d\n",len
);
523 // if(len<=0 || len>MAX_PS_PACKETSIZE) return -1; // Invalid packet size
524 if(len
<=0 || len
>MAX_PS_PACKETSIZE
){
525 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
,"Invalid PS data len: %d\n",len
);
526 return -1; // invalid packet !!!!!!
529 if(id
>=0x1C0 && id
<=0x1DF){
532 new_audio_stream(demux
, aid
);
533 if(demux
->audio
->id
==aid
){
535 if(!ds
->sh
) ds
->sh
=demux
->a_streams
[aid
];
537 sh_audio_t
*sh
= (sh_audio_t
*)ds
->sh
;
538 if(priv
->es_map
[id
- 0x1B0])
539 sh
->format
= priv
->es_map
[id
- 0x1B0];
540 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
,"ASSIGNED TO STREAM %d CODEC %x\n", id
, priv
->es_map
[id
- 0x1B0]);
544 if(id
>=0x1E0 && id
<=0x1EF){
547 if(!demux
->v_streams
[aid
]) new_sh_video(demux
,aid
);
548 if(demux
->video
->id
==-1) demux
->video
->id
=aid
;
549 if(demux
->video
->id
==aid
){
551 if(!ds
->sh
) ds
->sh
=demux
->v_streams
[aid
];
553 sh_video_t
*sh
= (sh_video_t
*)ds
->sh
;
554 if(priv
->es_map
[id
- 0x1B0]) {
555 sh
->format
= priv
->es_map
[id
- 0x1B0];
556 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
,"ASSIGNED TO STREAM %d CODEC %x\n", id
, priv
->es_map
[id
- 0x1B0]);
563 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
,"DEMUX_MPG: Read %d data bytes from packet %04X\n",len
,id
);
564 // printf("packet start = 0x%X \n",stream_tell(demux->stream)-packet_start_pos);
566 dp
=new_demux_packet(len
);
568 mp_dbg(MSGT_DEMUX
,MSGL_ERR
,"DEMUX_MPG ERROR: couldn't create demux_packet(%d bytes)\n",len
);
569 stream_skip(demux
->stream
,len
);
572 l
= stream_read(demux
->stream
,dp
->buffer
,len
);
574 resize_demux_packet(dp
, l
);
577 dp
->pts
=pts
/90000.0f
;
578 dp
->pos
=demux
->filepos
;
581 set dp->stream_pts only when feeding the video stream, or strangely interleaved files
582 (such as SWIII) will show strange alternations in the stream time, wildly going
585 if(ds
== demux
->video
&& stream_control(demux
->stream
, STREAM_CTRL_GET_CURRENT_TIME
,(void *)&stream_pts
)!=STREAM_UNSUPPORTED
)
586 dp
->stream_pts
= stream_pts
;
587 ds_add_packet(ds
,dp
);
588 if (demux
->priv
&& set_pts
) ((mpg_demuxer_t
*)demux
->priv
)->last_pts
= pts
/90000.0f
;
589 // if(ds==demux->sub) parse_dvdsub(ds->last->buffer,ds->last->len);
592 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
,"DEMUX_MPG: Skipping %d data bytes from packet %04X\n",len
,id
);
593 if(len
<=2356) stream_skip(demux
->stream
,len
);
597 static int num_elementary_packets100
=0;
598 static int num_elementary_packets101
=0;
599 static int num_elementary_packets12x
=0;
600 static int num_elementary_packets1B6
=0;
601 static int num_elementary_packetsPES
=0;
602 static int num_mpeg12_startcode
=0;
603 static int num_h264_slice
=0; //combined slice
604 static int num_h264_dpa
=0; //DPA Slice
605 static int num_h264_dpb
=0; //DPB Slice
606 static int num_h264_dpc
=0; //DPC Slice
607 static int num_h264_idr
=0; //IDR Slice
608 static int num_h264_sps
=0;
609 static int num_h264_pps
=0;
611 static int num_mp3audio_packets
=0;
613 static void clear_stats(void)
615 num_elementary_packets100
=0;
616 num_elementary_packets101
=0;
617 num_elementary_packets1B6
=0;
618 num_elementary_packets12x
=0;
619 num_elementary_packetsPES
=0;
620 num_mpeg12_startcode
=0;
621 num_h264_slice
=0; //combined slice
622 num_h264_dpa
=0; //DPA Slice
623 num_h264_dpb
=0; //DPB Slice
624 num_h264_dpc
=0; //DPC Slice
625 num_h264_idr
=0; //IDR Slice
628 num_mp3audio_packets
=0;
631 //assumes demuxer->synced < 2
632 static inline void update_stats(int head
)
634 if(head
==0x1B6) ++num_elementary_packets1B6
;
635 else if(head
==0x1B3 || head
==0x1B8) ++num_mpeg12_startcode
;
636 else if(head
==0x100) ++num_elementary_packets100
;
637 else if(head
==0x101) ++num_elementary_packets101
;
638 else if(head
==0x1BD || (0x1C0<=head
&& head
<=0x1EF))
639 num_elementary_packetsPES
++;
640 else if(head
>=0x120 && head
<=0x12F) ++num_elementary_packets12x
;
641 if(head
>=0x100 && head
<0x1B0)
643 if((head
&~0x60) == 0x101) ++num_h264_slice
;
644 else if((head
&~0x60) == 0x102) ++num_h264_dpa
;
645 else if((head
&~0x60) == 0x103) ++num_h264_dpb
;
646 else if((head
&~0x60) == 0x104) ++num_h264_dpc
;
647 else if((head
&~0x60) == 0x105 && head
!= 0x105) ++num_h264_idr
;
648 else if((head
&~0x60) == 0x107 && head
!= 0x107) ++num_h264_sps
;
649 else if((head
&~0x60) == 0x108 && head
!= 0x108) ++num_h264_pps
;
653 static int demux_mpg_probe(demuxer_t
*demuxer
) {
657 int file_format
= DEMUXER_TYPE_UNKNOWN
;
659 tmppos
=stream_tell(demuxer
->stream
);
660 tmp
=stream_read_dword(demuxer
->stream
);
661 if(tmp
==0x1E0 || tmp
==0x1C0) {
662 tmp
=stream_read_word(demuxer
->stream
);
663 if(tmp
>1 && tmp
<=2048) pes
=0; // demuxer->synced=3; // PES...
665 stream_seek(demuxer
->stream
,tmppos
);
669 if(demux_mpg_open(demuxer
))
670 file_format
=DEMUXER_TYPE_MPEG_PS
;
672 mp_msg(MSGT_DEMUX
,MSGL_V
,"MPEG packet stats: p100: %d p101: %d p1B6: %d p12x: %d sli: %d a: %d b: %d c: %d idr: %d sps: %d pps: %d PES: %d MP3: %d, synced: %d\n",
673 num_elementary_packets100
,num_elementary_packets101
,
674 num_elementary_packets1B6
,num_elementary_packets12x
,
675 num_h264_slice
, num_h264_dpa
,
676 num_h264_dpb
, num_h264_dpc
=0,
677 num_h264_idr
, num_h264_sps
=0,
679 num_elementary_packetsPES
,num_mp3audio_packets
, demuxer
->synced
);
681 //MPEG packet stats: p100: 458 p101: 458 PES: 0 MP3: 1103 (.m2v)
682 if(num_mp3audio_packets
>50 && num_mp3audio_packets
>2*num_elementary_packets100
683 && abs(num_elementary_packets100
-num_elementary_packets101
)>2)
686 // some hack to get meaningfull error messages to our unhappy users:
687 if(num_mpeg12_startcode
>=2 && num_elementary_packets100
>=2 && num_elementary_packets101
>=2 &&
688 abs(num_elementary_packets101
+8-num_elementary_packets100
)<16) {
689 if(num_elementary_packetsPES
>=4 && num_elementary_packetsPES
>=num_elementary_packets100
-4) {
692 file_format
=DEMUXER_TYPE_MPEG_ES
; // <-- hack is here :)
694 // fuzzy mpeg4-es detection. do NOT enable without heavy testing of mpeg formats detection!
695 if(num_elementary_packets1B6
>3 && num_elementary_packets12x
>=1 &&
696 num_elementary_packetsPES
==0 && num_elementary_packets100
<=num_elementary_packets12x
&&
698 file_format
=DEMUXER_TYPE_MPEG4_ES
;
700 // fuzzy h264-es detection. do NOT enable without heavy testing of mpeg formats detection!
701 if((num_h264_slice
>3 || (num_h264_dpa
>3 && num_h264_dpb
>3 && num_h264_dpc
>3)) &&
702 /* FIXME num_h264_sps>=1 && */ num_h264_pps
>=1 && num_h264_idr
>=1 &&
703 num_elementary_packets1B6
==0 && num_elementary_packetsPES
==0 &&
705 file_format
=DEMUXER_TYPE_H264_ES
;
708 if(demuxer
->synced
==2)
709 mp_msg(MSGT_DEMUXER
,MSGL_ERR
,"MPEG: " MSGTR_MissingVideoStreamBug
);
711 mp_msg(MSGT_DEMUXER
,MSGL_V
,MSGTR_NotSystemStream
);
714 //FIXME this shouldn't be necessary
715 stream_seek(demuxer
->stream
,tmppos
);
719 static int demux_mpg_es_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
){
720 // Elementary video stream
721 if(demux
->stream
->eof
) return 0;
722 demux
->filepos
=stream_tell(demux
->stream
);
723 ds_read_packet(demux
->video
,demux
->stream
,STREAM_BUFFER_SIZE
,0,demux
->filepos
,0);
728 * \brief discard until 0x100 header and return a filled buffer
729 * \param b buffer-end pointer
730 * \param pos current pos in stream, negative since b points to end of buffer
731 * \param s stream to read from
732 * \return new position, differs from original pos when eof hit and thus
733 * b was modified to point to the new end of buffer
735 static int find_end(unsigned char **b
, int pos
, stream_t
*s
) {
736 register int state
= 0xffffffff;
737 unsigned char *buf
= *b
;
740 // search already read part
741 while (state
!= 0x100 && pos
) {
742 state
= state
<< 8 | buf
[pos
++];
744 // continue search in stream
745 while (state
!= 0x100) {
746 register int c
= stream_read_char(s
);
748 state
= state
<< 8 | c
;
750 // modify previous header (from 0x1bc or 0x1bf to 0x100)
752 // copy remaining buffer part to current pos
753 memmove(&buf
[start
], &buf
[pos
], -pos
);
754 unused
= start
+ -pos
; // -unused bytes in buffer
755 read
= stream_read(s
, &buf
[unused
], -unused
);
757 // fix buffer so it ends at pos == 0 (eof case)
764 * This format usually uses an insane bitrate, which makes this function
765 * performance-critical!
766 * Be sure to benchmark any changes with different compiler versions.
768 static int demux_mpg_gxf_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
) {
769 demux_packet_t
*pack
;
771 demux
->filepos
= stream_tell(demux
->stream
);
772 pack
= new_demux_packet(STREAM_BUFFER_SIZE
);
773 len
= stream_read(demux
->stream
, pack
->buffer
, STREAM_BUFFER_SIZE
);
776 free_demux_packet(pack
);
780 register uint32_t state
= (uint32_t)demux
->priv
;
781 register int pos
= -len
;
782 unsigned char *buf
= &pack
->buffer
[len
];
784 state
= state
<< 8 | buf
[pos
];
785 if (unlikely((state
| 3) == 0x1bf))
786 pos
= find_end(&buf
, pos
, demux
->stream
);
788 demux
->priv
= (void *)state
;
789 len
= buf
- pack
->buffer
;
791 if (len
< STREAM_BUFFER_SIZE
)
792 resize_demux_packet(pack
, len
);
793 ds_add_packet(ds
, pack
);
797 static int demux_mpg_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
){
800 int max_packs
=256; // 512kbyte
805 demux
->filepos
=stream_tell(demux
->stream
);
807 //lame workaround: this is needed to show the progress bar when playing dvdnav://
808 //(ths poor guy doesn't know teh length of the stream at startup)
809 demux
->movi_end
= demux
->stream
->end_pos
;
811 head
=stream_read_dword(demux
->stream
);
812 if((head
&0xFFFFFF00)!=0x100){
814 demux
->filepos
-=skipped
;
816 int c
=stream_read_char(demux
->stream
);
821 if(mp_check_mp3_header(head
)) ++num_mp3audio_packets
;
822 ++skipped
; //++demux->filepos;
828 demux
->filepos
+=skipped
;
830 if(stream_eof(demux
->stream
)) break;
831 // sure: head=0x000001XX
832 mp_dbg(MSGT_DEMUX
,MSGL_DBG4
,"*** head=0x%X\n",head
);
833 if(demux
->synced
==0){
834 if(head
==0x1BA) demux
->synced
=1; //else
835 // if(head==0x1BD || (head>=0x1C0 && head<=0x1EF)) demux->synced=3; // PES?
837 if(demux
->synced
==1){
838 if(head
==0x1BB || head
==0x1BD || (head
>=0x1C0 && head
<=0x1EF)){
840 mp_msg(MSGT_DEMUX
,MSGL_V
,"system stream synced at 0x%"PRIX64
" (%"PRId64
")!\n",(int64_t)demux
->filepos
,(int64_t)demux
->filepos
);
841 num_elementary_packets100
=0; // requires for re-sync!
842 num_elementary_packets101
=0; // requires for re-sync!
843 } else demux
->synced
=0;
845 if(demux
->synced
>=2){
846 ret
=demux_mpg_read_packet(demux
,head
);
849 demux
->stream
->eof
=1;
850 mp_msg(MSGT_DEMUX
,MSGL_ERR
,MSGTR_DoesntContainSelectedStream
);
853 if(demux
->synced
==3) demux
->synced
=(ret
==1)?2:0; // PES detect
856 if(head
>=0x100 && head
<0x1B0)
857 mp_msg(MSGT_DEMUX
,MSGL_DBG3
,"Opps... elementary video packet found: %03X\n",head
);
858 else if((head
>=0x1C0 && head
<0x1F0) || head
==0x1BD)
859 mp_msg(MSGT_DEMUX
,MSGL_DBG3
,"Opps... PES packet found: %03X\n",head
);
861 if(((num_elementary_packets100
>50 && num_elementary_packets101
>50) ||
862 (num_elementary_packetsPES
>50)) && skipped
>4000000){
863 mp_msg(MSGT_DEMUX
,MSGL_V
,"sync_mpeg_ps: seems to be ES/PES stream...\n");
864 demux
->stream
->eof
=1;
867 if(num_mp3audio_packets
>100 && num_elementary_packets100
<10){
868 mp_msg(MSGT_DEMUX
,MSGL_V
,"sync_mpeg_ps: seems to be MP3 stream...\n");
869 demux
->stream
->eof
=1;
874 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
,"demux: %d bad bytes skipped\n",skipped
);
875 if(demux
->stream
->eof
){
876 mp_msg(MSGT_DEMUX
,MSGL_V
,"MPEG Stream reached EOF\n");
882 extern void skip_audio_frame(sh_audio_t
*sh_audio
);
884 static void demux_seek_mpg(demuxer_t
*demuxer
,float rel_seek_secs
,float audio_delay
, int flags
){
885 demux_stream_t
*d_audio
=demuxer
->audio
;
886 demux_stream_t
*d_video
=demuxer
->video
;
887 sh_audio_t
*sh_audio
=d_audio
->sh
;
888 sh_video_t
*sh_video
=d_video
->sh
;
889 mpg_demuxer_t
*mpg_d
=(mpg_demuxer_t
*)demuxer
->priv
;
892 off_t oldpos
= demuxer
->filepos
;
894 off_t newpos
= (flags
& SEEK_ABSOLUTE
) ? demuxer
->movi_start
: oldpos
;
897 oldpts
= mpg_d
->last_pts
;
898 newpts
= (flags
& SEEK_ABSOLUTE
) ? 0.0 : oldpts
;
899 //================= seek in MPEG ==========================
900 //calculate the pts to seek to
901 if(flags
& SEEK_FACTOR
) {
902 if (mpg_d
&& mpg_d
->first_to_final_pts_len
> 0.0)
903 newpts
+= mpg_d
->first_to_final_pts_len
* rel_seek_secs
;
905 newpts
+= rel_seek_secs
* (demuxer
->movi_end
- demuxer
->movi_start
) * oldpts
/ oldpos
;
907 newpts
+= rel_seek_secs
;
908 if (newpts
< 0) newpts
= 0;
910 if(flags
&SEEK_FACTOR
){
912 newpos
+=(demuxer
->movi_end
-demuxer
->movi_start
)*rel_seek_secs
;
915 if (mpg_d
&& mpg_d
->has_valid_timestamps
) {
916 if (mpg_d
->first_to_final_pts_len
> 0.0)
917 newpos
+= rel_seek_secs
* (demuxer
->movi_end
- demuxer
->movi_start
) / mpg_d
->first_to_final_pts_len
;
918 else if (oldpts
> 0.0)
919 newpos
+= rel_seek_secs
* (oldpos
- demuxer
->movi_start
) / oldpts
;
920 } else if(!sh_video
|| !sh_video
->i_bps
) // unspecified or VBR
921 newpos
+=2324*75*rel_seek_secs
; // 174.3 kbyte/sec
923 newpos
+=sh_video
->i_bps
*rel_seek_secs
;
927 if(newpos
<demuxer
->movi_start
){
928 if(demuxer
->stream
->type
!=STREAMTYPE_VCD
) demuxer
->movi_start
=0; // for VCD
929 if(newpos
<demuxer
->movi_start
) newpos
=demuxer
->movi_start
;
932 stream_seek(demuxer
->stream
,newpos
);
935 videobuf_code_len
=0; // reset ES stream buffer
937 ds_fill_buffer(d_video
);
939 ds_fill_buffer(d_audio
);
944 if(sh_audio
&& !d_audio
->eof
&& d_video
->pts
&& d_audio
->pts
){
945 float a_pts
=d_audio
->pts
;
946 a_pts
+=(ds_tell_pts(d_audio
)-sh_audio
->a_in_buffer_len
)/(float)sh_audio
->i_bps
;
947 if(d_video
->pts
>a_pts
){
948 skip_audio_frame(sh_audio
); // sync audio
953 i
=sync_video_packet(d_video
);
954 if(sh_video
->format
== mmioFOURCC('W', 'V', 'C', '1')) {
955 if(i
==0x10E || i
==0x10F) //entry point or sequence header
958 if(sh_video
->format
== 0x10000004) { //mpeg4
959 if(i
==0x1B6) { //vop (frame) startcode
960 int pos
= videobuf_len
;
961 if(!read_video_packet(d_video
)) break; // EOF
962 if((videobuffer
[pos
+4] & 0x3F) == 0) break; //I-frame
964 } else if(sh_video
->format
== 0x10000005){ //h264
965 if((i
& ~0x60) == 0x105) break;
966 } else { //default mpeg1/2
967 if(i
==0x1B3 || i
==0x1B8) break; // found it!
969 if(!i
|| !skip_video_packet(d_video
)) break; // EOF?
973 if (!precision
|| abs(newpts
- mpg_d
->last_pts
) < 0.5 || (mpg_d
->last_pts
== oldpts
)) break;
974 if ((newpos
- oldpos
) * (mpg_d
->last_pts
- oldpts
) < 0) { // invalid timestamps
975 mpg_d
->has_valid_timestamps
= 0;
979 //prepare another seek because we are off by more than 0.5s
981 newpos
+= (newpts
- mpg_d
->last_pts
) * (newpos
- oldpos
) / (mpg_d
->last_pts
- oldpts
);
982 demux_flush(demuxer
);
983 demuxer
->stream
->eof
=0; // clear eof flag
990 static int demux_mpg_control(demuxer_t
*demuxer
,int cmd
, void *arg
){
991 mpg_demuxer_t
*mpg_d
=(mpg_demuxer_t
*)demuxer
->priv
;
994 case DEMUXER_CTRL_GET_TIME_LENGTH
:
995 if(stream_control(demuxer
->stream
, STREAM_CTRL_GET_TIME_LENGTH
, arg
) != STREAM_UNSUPPORTED
) {
996 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
,"\r\nDEMUX_MPG_CTRL, (%.3lf)\r\n", *((double*)arg
));
997 return DEMUXER_CTRL_GUESS
;
999 if (mpg_d
&& mpg_d
->has_valid_timestamps
) {
1000 *((double *)arg
)=(double)mpg_d
->first_to_final_pts_len
;
1001 return DEMUXER_CTRL_OK
;
1003 return DEMUXER_CTRL_DONTKNOW
;
1005 case DEMUXER_CTRL_GET_PERCENT_POS
:
1006 if (mpg_d
&& mpg_d
->has_valid_timestamps
&& mpg_d
->first_to_final_pts_len
> 0.0) {
1007 *((int *)arg
)=(int)(100 * (mpg_d
->last_pts
-mpg_d
->first_pts
) / mpg_d
->first_to_final_pts_len
);
1008 return DEMUXER_CTRL_OK
;
1010 return DEMUXER_CTRL_DONTKNOW
;
1012 case DEMUXER_CTRL_SWITCH_AUDIO
:
1013 if(! (mpg_d
&& mpg_d
->num_a_streams
> 1 && demuxer
->audio
&& demuxer
->audio
->sh
))
1014 return DEMUXER_CTRL_NOTIMPL
;
1016 demux_stream_t
*d_audio
= demuxer
->audio
;
1017 sh_audio_t
*sh_audio
= d_audio
->sh
;
1018 sh_audio_t
*sh_a
= sh_audio
;
1021 return DEMUXER_CTRL_NOTIMPL
;
1022 if (*((int*)arg
) < 0)
1024 for (i
= 0; i
< mpg_d
->num_a_streams
; i
++) {
1025 if (d_audio
->id
== mpg_d
->a_stream_ids
[i
]) break;
1027 i
= (i
+1) % mpg_d
->num_a_streams
;
1028 sh_a
= (sh_audio_t
*)demuxer
->a_streams
[mpg_d
->a_stream_ids
[i
]];
1031 for (i
= 0; i
< mpg_d
->num_a_streams
; i
++)
1032 if (*((int*)arg
) == mpg_d
->a_stream_ids
[i
]) break;
1033 if (i
< mpg_d
->num_a_streams
)
1034 sh_a
= (sh_audio_t
*)demuxer
->a_streams
[*((int*)arg
)];
1036 if (i
< mpg_d
->num_a_streams
&& d_audio
->id
!= mpg_d
->a_stream_ids
[i
]) {
1037 d_audio
->id
= mpg_d
->a_stream_ids
[i
];
1039 ds_free_packs(d_audio
);
1042 *((int*)arg
) = demuxer
->audio
->id
;
1043 return DEMUXER_CTRL_OK
;
1046 return DEMUXER_CTRL_NOTIMPL
;
1051 static int demux_mpg_pes_probe(demuxer_t
*demuxer
) {
1052 demuxer
->synced
= 3;
1053 return (demux_mpg_probe(demuxer
) == DEMUXER_TYPE_MPEG_PS
) ? DEMUXER_TYPE_MPEG_PES
: 0;
1057 static demuxer_t
* demux_mpg_es_open(demuxer_t
* demuxer
)
1059 sh_video_t
*sh_video
=NULL
;
1061 demuxer
->audio
->sh
= NULL
; // ES streams has no audio channel
1062 demuxer
->video
->sh
= new_sh_video(demuxer
,0); // create dummy video stream header, id=0
1063 sh_video
=demuxer
->video
->sh
;sh_video
->ds
=demuxer
->video
;
1068 static demuxer_t
*demux_mpg_gxf_open(demuxer_t
*demuxer
) {
1069 demuxer
->audio
->sh
= NULL
;
1070 demuxer
->video
->sh
= new_sh_video(demuxer
,0);
1071 ((sh_video_t
*)demuxer
->video
->sh
)->ds
= demuxer
->video
;
1072 demuxer
->priv
= (void *) 0xffffffff;
1076 static demuxer_t
* demux_mpg_ps_open(demuxer_t
* demuxer
)
1078 sh_audio_t
*sh_audio
=NULL
;
1079 sh_video_t
*sh_video
=NULL
;
1081 sh_video
=demuxer
->video
->sh
;sh_video
->ds
=demuxer
->video
;
1083 if(demuxer
->audio
->id
!=-2) {
1084 if(!ds_fill_buffer(demuxer
->audio
)){
1085 mp_msg(MSGT_DEMUXER
,MSGL_INFO
,"MPEG: " MSGTR_MissingAudioStream
);
1086 demuxer
->audio
->sh
=NULL
;
1088 sh_audio
=demuxer
->audio
->sh
;sh_audio
->ds
=demuxer
->audio
;
1092 if(!sh_video
->format
&& ps_probe
> 0) {
1094 off_t pos
= stream_tell(demuxer
->stream
);
1098 head
=sync_video_packet(demuxer
->video
);
1101 skip_video_packet(demuxer
->video
);
1102 } while(stream_tell(demuxer
->stream
) < pos
+ ps_probe
&& !demuxer
->stream
->eof
);
1104 ds_free_packs(demuxer
->video
);
1105 demuxer
->stream
->eof
=0;
1106 stream_seek(demuxer
->stream
, pos
);
1107 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"MPEG packet stats: p100: %d p101: %d p1B6: %d p12x: %d sli: %d a: %d b: %d c: %d idr: %d sps: %d pps: %d\n",
1108 num_elementary_packets100
, num_elementary_packets101
,
1109 num_elementary_packets1B6
, num_elementary_packets12x
,
1110 num_h264_slice
, num_h264_dpa
, num_h264_dpb
, num_h264_dpc
,
1111 num_h264_idr
, num_h264_sps
, num_h264_pps
);
1113 if(num_elementary_packets1B6
>3 && num_elementary_packets12x
>=1 &&
1114 num_elementary_packets100
<=num_elementary_packets12x
)
1115 sh_video
->format
= 0x10000004;
1116 else if((num_h264_slice
>3 || (num_h264_dpa
>3 && num_h264_dpb
>3 && num_h264_dpc
>3)) &&
1117 num_h264_sps
>=1 && num_h264_pps
>=1 && num_h264_idr
>=1 &&
1118 num_elementary_packets1B6
==0)
1119 sh_video
->format
= 0x10000005;
1120 else sh_video
->format
= 0x10000002;
1127 const demuxer_desc_t demuxer_desc_mpeg_ps
= {
1133 DEMUXER_TYPE_MPEG_PS
,
1134 0, // unsafe autodetect
1136 demux_mpg_fill_buffer
,
1144 const demuxer_desc_t demuxer_desc_mpeg_pes
= {
1150 DEMUXER_TYPE_MPEG_PES
,
1151 0, // unsafe autodetect
1152 demux_mpg_pes_probe
,
1153 demux_mpg_fill_buffer
,
1161 const demuxer_desc_t demuxer_desc_mpeg_gxf
= {
1162 "MPEG ES in GXF demuxer",
1165 "Reimar Doeffinger",
1167 DEMUXER_TYPE_MPEG_GXF
,
1168 0, // hack autodetection
1170 demux_mpg_gxf_fill_buffer
,
1177 const demuxer_desc_t demuxer_desc_mpeg_es
= {
1183 DEMUXER_TYPE_MPEG_ES
,
1184 0, // hack autodetection
1186 demux_mpg_es_fill_buffer
,
1194 const demuxer_desc_t demuxer_desc_mpeg4_es
= {
1200 DEMUXER_TYPE_MPEG4_ES
,
1201 0, // hack autodetection
1203 demux_mpg_es_fill_buffer
,
1211 const demuxer_desc_t demuxer_desc_h264_es
= {
1217 DEMUXER_TYPE_H264_ES
,
1218 0, // hack autodetection
1220 demux_mpg_es_fill_buffer
,