2 * Demultiplexer for MPEG2 Transport Streams.
4 * Written by Nico <nsabbi@libero.it>
5 * Kind feedback is appreciated; 'sucks' and alike is not.
6 * Originally based on demux_pva.c written by Matteo Giani and FFmpeg (libavformat) sources
8 * This file is part of MPlayer.
10 * MPlayer is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * MPlayer is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 #include "libmpcodecs/dec_audio.h"
34 #include "stream/stream.h"
42 #define TS_PH_PACKET_SIZE 192
43 #define TS_FEC_PACKET_SIZE 204
44 #define TS_PACKET_SIZE 188
45 #define NB_PID_MAX 8192
47 #define MAX_HEADER_SIZE 6 /* enough for PES header + length */
48 #define MAX_CHECK_SIZE 65535
49 #define NUM_CONSECUTIVE_TS_PACKETS 32
50 #define NUM_CONSECUTIVE_AUDIO_PACKETS 348
51 #define MAX_A52_FRAME_SIZE 3840
54 #define SIZE_MAX ((size_t)-1)
63 int audio_substream_id
= -1;
68 VIDEO_MPEG1
= 0x10000001,
69 VIDEO_MPEG2
= 0x10000002,
70 VIDEO_MPEG4
= 0x10000004,
71 VIDEO_H264
= 0x10000005,
72 VIDEO_AVC
= mmioFOURCC('a', 'v', 'c', '1'),
73 VIDEO_DIRAC
= mmioFOURCC('d', 'r', 'a', 'c'),
74 VIDEO_VC1
= mmioFOURCC('W', 'V', 'C', '1'),
78 AUDIO_LPCM_BE
= 0x10001,
79 AUDIO_AAC
= mmioFOURCC('M', 'P', '4', 'A'),
80 AUDIO_AAC_LATM
= mmioFOURCC('M', 'P', '4', 'L'),
81 AUDIO_TRUEHD
= mmioFOURCC('T', 'R', 'H', 'D'),
84 SPU_TELETEXT
= 0x3000002,
86 PES_PRIVATE1
= 0xBD00000,
87 SL_PES_STREAM
= 0xD000000,
88 SL_SECTION
= 0xD100000,
100 uint16_t payload_size
;
101 es_stream_type_t type
, subtype
;
102 double pts
, last_pts
;
105 int last_cc
; // last cc code (-1 if first packet)
107 ts_section_t section
;
109 int extradata_alloc
, extradata_len
;
111 uint8_t au_start
, au_end
, last_au_end
;
121 typedef struct MpegTSContext
{
122 int packet_size
; // raw packet size, including FEC if present e.g. 188 bytes
123 ES_stream_t
*pids
[NB_PID_MAX
];
124 sh_av_t streams
[NB_PID_MAX
];
130 demux_packet_t
*pack
;
131 int offset
, buffer_size
;
134 #define MAX_EXTRADATA_SIZE 64*1024
136 int32_t object_type
; //aka codec used
137 int32_t stream_type
; //video, audio etc.
138 uint8_t buf
[MAX_EXTRADATA_SIZE
];
141 } mp4_decoder_config_t
;
148 uint8_t random_accesspoint
;
149 uint8_t random_accesspoint_only
;
155 uint32_t ts_resolution
, ocr_resolution
;
156 uint8_t ts_len
, ocr_len
, au_len
, instant_bitrate_len
, degr_len
, au_seqnum_len
, packet_seqnum_len
;
158 uint16_t au_duration
, cts_duration
;
159 uint64_t ocr
, dts
, cts
;
165 mp4_decoder_config_t decoder
;
180 uint16_t section_length
;
182 uint8_t version_number
;
184 uint8_t section_number
;
185 uint8_t last_section_number
;
191 ts_section_t section
;
199 uint16_t section_length
;
200 uint8_t version_number
;
202 uint8_t section_number
;
203 uint8_t last_section_number
;
205 uint16_t prog_descr_length
;
206 ts_section_t section
;
210 uint32_t type
; //it's 8 bit long, but cast to the right type as FOURCC
211 uint16_t descr_length
;
212 uint8_t format_descriptor
[5];
217 mp4_es_descr_t
*mp4es
;
218 int od_cnt
, mp4es_cnt
;
231 av_fifo_t fifo
[3]; //0 for audio, 1 for video, 2 for subs
241 char packet
[TS_FEC_PACKET_SIZE
];
242 TS_stream_info vstr
, astr
;
247 es_stream_type_t type
;
248 ts_section_t section
;
252 static int IS_AUDIO(es_stream_type_t type
)
267 static int IS_VIDEO(es_stream_type_t type
)
282 static int IS_SUB(es_stream_type_t type
)
294 static int ts_parse(demuxer_t
*demuxer
, ES_stream_t
*es
, unsigned char *packet
, int probe
);
296 static uint8_t get_packet_size(const unsigned char *buf
, int size
)
300 if (size
< (TS_FEC_PACKET_SIZE
* NUM_CONSECUTIVE_TS_PACKETS
))
303 for(i
=0; i
<NUM_CONSECUTIVE_TS_PACKETS
; i
++)
305 if (buf
[i
* TS_PACKET_SIZE
] != 0x47)
307 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "GET_PACKET_SIZE, pos %d, char: %2x\n", i
, buf
[i
* TS_PACKET_SIZE
]);
311 return TS_PACKET_SIZE
;
314 for(i
=0; i
<NUM_CONSECUTIVE_TS_PACKETS
; i
++)
316 if (buf
[i
* TS_FEC_PACKET_SIZE
] != 0x47){
317 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "GET_PACKET_SIZE, pos %d, char: %2x\n", i
, buf
[i
* TS_PACKET_SIZE
]);
321 return TS_FEC_PACKET_SIZE
;
324 for(i
=0; i
<NUM_CONSECUTIVE_TS_PACKETS
; i
++)
326 if (buf
[i
* TS_PH_PACKET_SIZE
] != 0x47)
329 return TS_PH_PACKET_SIZE
;
332 static int parse_avc_sps(uint8_t *buf
, int len
, int *w
, int *h
);
333 static inline uint8_t *pid_lang_from_pmt(ts_priv_t
*priv
, int pid
);
335 static void ts_add_stream(demuxer_t
* demuxer
, ES_stream_t
*es
)
338 ts_priv_t
*priv
= (ts_priv_t
*) demuxer
->priv
;
340 if(priv
->ts
.streams
[es
->pid
].sh
)
343 if((IS_AUDIO(es
->type
) || IS_AUDIO(es
->subtype
)) && priv
->last_aid
+1 < MAX_A_STREAMS
)
345 sh_audio_t
*sh
= new_sh_audio_aid(demuxer
, priv
->last_aid
, es
->pid
);
348 const char *lang
= pid_lang_from_pmt(priv
, es
->pid
);
349 sh
->needs_parsing
= 1;
350 sh
->format
= IS_AUDIO(es
->type
) ? es
->type
: es
->subtype
;
351 sh
->ds
= demuxer
->audio
;
353 priv
->ts
.streams
[es
->pid
].id
= priv
->last_aid
;
354 priv
->ts
.streams
[es
->pid
].sh
= sh
;
355 priv
->ts
.streams
[es
->pid
].type
= TYPE_AUDIO
;
356 mp_msg(MSGT_DEMUX
, MSGL_V
, "\r\nADDED AUDIO PID %d, type: %x stream n. %d\r\n", es
->pid
, sh
->format
, priv
->last_aid
);
358 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_AID_%d_LANG=%s\n", es
->pid
, lang
);
362 if(es
->extradata
&& es
->extradata_len
)
364 sh
->wf
= malloc(sizeof(*sh
->wf
) + es
->extradata_len
);
365 sh
->wf
->cbSize
= es
->extradata_len
;
366 memcpy(sh
->wf
+ 1, es
->extradata
, es
->extradata_len
);
370 if((IS_VIDEO(es
->type
) || IS_VIDEO(es
->subtype
)) && priv
->last_vid
+1 < MAX_V_STREAMS
)
372 sh_video_t
*sh
= new_sh_video_vid(demuxer
, priv
->last_vid
, es
->pid
);
375 sh
->format
= IS_VIDEO(es
->type
) ? es
->type
: es
->subtype
;
376 sh
->ds
= demuxer
->video
;
378 priv
->ts
.streams
[es
->pid
].id
= priv
->last_vid
;
379 priv
->ts
.streams
[es
->pid
].sh
= sh
;
380 priv
->ts
.streams
[es
->pid
].type
= TYPE_VIDEO
;
381 mp_msg(MSGT_DEMUX
, MSGL_V
, "\r\nADDED VIDEO PID %d, type: %x stream n. %d\r\n", es
->pid
, sh
->format
, priv
->last_vid
);
385 if(sh
->format
== VIDEO_AVC
&& es
->extradata
&& es
->extradata_len
)
388 sh
->bih
= calloc(1, sizeof(*sh
->bih
) + es
->extradata_len
);
389 sh
->bih
->biSize
= sizeof(*sh
->bih
) + es
->extradata_len
;
390 sh
->bih
->biCompression
= sh
->format
;
391 memcpy(sh
->bih
+ 1, es
->extradata
, es
->extradata_len
);
392 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "EXTRADATA(%d BYTES): \n", es
->extradata_len
);
393 for(i
= 0;i
< es
->extradata_len
; i
++)
394 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "%02x ", (int) es
->extradata
[i
]);
395 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
,"\n");
396 if(parse_avc_sps(es
->extradata
, es
->extradata_len
, &w
, &h
))
398 sh
->bih
->biWidth
= w
;
399 sh
->bih
->biHeight
= h
;
405 if(IS_SUB(es
->type
) && priv
->last_sid
+1 < MAX_S_STREAMS
)
407 sh_sub_t
*sh
= new_sh_sub_sid_lang(demuxer
, priv
->last_sid
, es
->pid
, pid_lang_from_pmt(priv
, es
->pid
));
411 sh
->type
= 'v'; break;
413 sh
->type
= 'p'; break;
415 priv
->ts
.streams
[es
->pid
].id
= priv
->last_aid
;
416 priv
->ts
.streams
[es
->pid
].sh
= sh
;
417 priv
->ts
.streams
[es
->pid
].type
= TYPE_AUDIO
;
423 static int ts_check_file(demuxer_t
* demuxer
)
425 const int buf_size
= (TS_FEC_PACKET_SIZE
* NUM_CONSECUTIVE_TS_PACKETS
);
426 unsigned char buf
[TS_FEC_PACKET_SIZE
* NUM_CONSECUTIVE_TS_PACKETS
], done
= 0, *ptr
;
427 uint32_t _read
, i
, count
= 0, is_ts
;
428 int cc
[NB_PID_MAX
], last_cc
[NB_PID_MAX
], pid
, cc_ok
, c
, good
, bad
;
433 mp_msg(MSGT_DEMUX
, MSGL_V
, "Checking for MPEG-TS...\n");
435 init_pos
= stream_tell(demuxer
->stream
);
442 while(((c
=stream_read_char(demuxer
->stream
)) != 0x47)
444 && (i
< MAX_CHECK_SIZE
)
445 && ! demuxer
->stream
->eof
451 mp_msg(MSGT_DEMUX
, MSGL_V
, "THIS DOESN'T LOOK LIKE AN MPEG-TS FILE!\n");
457 pos
= stream_tell(demuxer
->stream
) - 1;
459 _read
= stream_read(demuxer
->stream
, &buf
[1], buf_size
-1);
461 if(_read
< buf_size
-1)
463 mp_msg(MSGT_DEMUX
, MSGL_V
, "COULDN'T READ ENOUGH DATA, EXITING TS_CHECK\n");
464 stream_reset(demuxer
->stream
);
468 size
= get_packet_size(buf
, buf_size
);
475 if(pos
- init_pos
>= MAX_CHECK_SIZE
)
482 mp_msg(MSGT_DEMUX
, MSGL_V
, "TRIED UP TO POSITION %"PRIu64
", FOUND %x, packet_size= %d, SEEMS A TS? %d\n", (uint64_t) pos
, c
, size
, is_ts
);
483 stream_seek(demuxer
->stream
, pos
);
488 //LET'S CHECK continuity counters
490 for(count
= 0; count
< NB_PID_MAX
; count
++)
492 cc
[count
] = last_cc
[count
] = -1;
495 for(count
= 0; count
< NUM_CONSECUTIVE_TS_PACKETS
; count
++)
497 ptr
= &(buf
[size
* count
]);
498 pid
= ((ptr
[1] & 0x1f) << 8) | ptr
[2];
499 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "BUF: %02x %02x %02x %02x, PID %d, SIZE: %d \n",
500 ptr
[0], ptr
[1], ptr
[2], ptr
[3], pid
, size
);
502 if((pid
== 8191) || (pid
< 16))
505 cc
[pid
] = (ptr
[3] & 0xf);
506 cc_ok
= (last_cc
[pid
] < 0) || ((((last_cc
[pid
] + 1) & 0x0f) == cc
[pid
]));
507 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "PID %d, COMPARE CC %d AND LAST_CC %d\n", pid
, cc
[pid
], last_cc
[pid
]);
514 last_cc
[pid
] = cc
[pid
];
517 mp_msg(MSGT_DEMUX
, MSGL_V
, "GOOD CC: %d, BAD CC: %d\n", good
, bad
);
526 static inline int32_t progid_idx_in_pmt(ts_priv_t
*priv
, uint16_t progid
)
530 if(priv
->pmt
== NULL
)
533 for(x
= 0; x
< priv
->pmt_cnt
; x
++)
535 if(priv
->pmt
[x
].progid
== progid
)
543 static inline int32_t progid_for_pid(ts_priv_t
*priv
, int pid
, int32_t req
) //finds the first program listing a pid
549 if(priv
->pmt
== NULL
)
553 for(i
=0; i
< priv
->pmt_cnt
; i
++)
555 pmt
= &(priv
->pmt
[i
]);
560 for(j
= 0; j
< pmt
->es_cnt
; j
++)
562 if(pmt
->es
[j
].pid
== pid
)
564 if((req
== 0) || (req
== pmt
->progid
))
573 static inline int32_t prog_pcr_pid(ts_priv_t
*priv
, int progid
)
577 if(priv
->pmt
== NULL
)
579 for(i
=0; i
< priv
->pmt_cnt
; i
++)
581 if(priv
->pmt
[i
].progid
== progid
)
582 return priv
->pmt
[i
].PCR_PID
;
588 static inline int pid_match_lang(ts_priv_t
*priv
, uint16_t pid
, char *lang
)
593 if(priv
->pmt
== NULL
)
596 for(i
=0; i
< priv
->pmt_cnt
; i
++)
598 pmt
= &(priv
->pmt
[i
]);
603 for(j
= 0; j
< pmt
->es_cnt
; j
++)
605 if(pmt
->es
[j
].pid
!= pid
)
608 mp_msg(MSGT_DEMUXER
, MSGL_V
, "CMP LANG %s AND %s, pids: %d %d\n",pmt
->es
[j
].lang
, lang
, pmt
->es
[j
].pid
, pid
);
609 if(strncmp(pmt
->es
[j
].lang
, lang
, 3) == 0)
621 int32_t atype
, vtype
, stype
; //types
622 int32_t apid
, vpid
, spid
; //stream ids
623 char alang
[4]; //languages
628 //second stage: returns the count of A52 syncwords found
629 static int a52_check(char *buf
, int len
)
631 int cnt
, frame_length
= 0, ok
, srate
;
639 if(buf
[cnt
] == 0x0B && buf
[cnt
+1] == 0x77)
641 frame_length
= mp_a52_framesize(&buf
[cnt
], &srate
);
642 if(frame_length
>=7 && frame_length
<=3840)
654 mp_msg(MSGT_DEMUXER
, MSGL_V
, "A52_CHECK(%d input bytes), found %d frame syncwords of %d bytes length\n", len
, ok
, frame_length
);
659 static off_t
ts_detect_streams(demuxer_t
*demuxer
, tsdemux_init_t
*param
)
661 int video_found
= 0, audio_found
= 0, sub_found
= 0, i
, num_packets
= 0, req_apid
, req_vpid
, req_spid
;
662 int is_audio
, is_video
, is_sub
, has_tables
;
663 int32_t p
, chosen_pid
= 0;
664 off_t pos
=0, ret
= 0, init_pos
, end_pos
;
666 unsigned char tmp
[TS_FEC_PACKET_SIZE
];
667 ts_priv_t
*priv
= (ts_priv_t
*) demuxer
->priv
;
671 } pes_priv1
[8192], *pptr
;
674 priv
->last_pid
= 8192; //invalid pid
676 req_apid
= param
->apid
;
677 req_vpid
= param
->vpid
;
678 req_spid
= param
->spid
;
681 memset(pes_priv1
, 0, sizeof(pes_priv1
));
682 init_pos
= stream_tell(demuxer
->stream
);
683 mp_msg(MSGT_DEMUXER
, MSGL_V
, "PROBING UP TO %"PRIu64
", PROG: %d\n", (uint64_t) param
->probe
, param
->prog
);
684 end_pos
= init_pos
+ (param
->probe
? param
->probe
: TS_MAX_PROBE_SIZE
);
687 pos
= stream_tell(demuxer
->stream
);
688 if(pos
> end_pos
|| demuxer
->stream
->eof
)
691 if(ts_parse(demuxer
, &es
, tmp
, 1))
693 //Non PES-aligned A52 audio may escape detection if PMT is not present;
694 //in this case we try to find at least 3 A52 syncwords
695 if((es
.type
== PES_PRIVATE1
) && (! audio_found
) && req_apid
> -2)
697 pptr
= &pes_priv1
[es
.pid
];
698 if(pptr
->pos
< 64*1024)
700 tmpbuf
= realloc(pptr
->buf
, pptr
->pos
+ es
.size
);
704 memcpy(&(pptr
->buf
[ pptr
->pos
]), es
.start
, es
.size
);
705 pptr
->pos
+= es
.size
;
706 if(a52_check(pptr
->buf
, pptr
->pos
) > 2)
708 param
->atype
= AUDIO_A52
;
709 param
->apid
= es
.pid
;
716 is_audio
= IS_AUDIO(es
.type
) || ((es
.type
==SL_PES_STREAM
) && IS_AUDIO(es
.subtype
));
717 is_video
= IS_VIDEO(es
.type
) || ((es
.type
==SL_PES_STREAM
) && IS_VIDEO(es
.subtype
));
718 is_sub
= IS_SUB(es
.type
);
721 if((! is_audio
) && (! is_video
) && (! is_sub
))
723 if(is_audio
&& req_apid
==-2)
728 chosen_pid
= (req_vpid
== es
.pid
);
729 if((! chosen_pid
) && (req_vpid
> 0))
736 chosen_pid
= (req_apid
== es
.pid
);
740 else if(param
->alang
[0] > 0 && es
.lang
[0] > 0)
742 if(pid_match_lang(priv
, es
.pid
, param
->alang
) == -1)
746 param
->apid
= req_apid
= es
.pid
;
751 chosen_pid
= (req_spid
== es
.pid
);
752 if((! chosen_pid
) && (req_spid
> 0))
756 if(req_apid
< 0 && (param
->alang
[0] == 0) && req_vpid
< 0 && req_spid
< 0)
759 if((ret
== 0) && chosen_pid
)
761 ret
= stream_tell(demuxer
->stream
);
764 p
= progid_for_pid(priv
, es
.pid
, param
->prog
);
768 if(!param
->prog
&& chosen_pid
)
772 if((param
->prog
> 0) && (param
->prog
!= p
))
776 if(is_video
&& (req_vpid
== es
.pid
))
778 param
->vtype
= IS_VIDEO(es
.type
) ? es
.type
: es
.subtype
;
779 param
->vpid
= es
.pid
;
787 if(is_audio
&& (req_apid
== es
.pid
))
789 param
->atype
= IS_AUDIO(es
.type
) ? es
.type
: es
.subtype
;
790 param
->apid
= es
.pid
;
801 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "TYPE: %x, PID: %d, PROG FOUND: %d\n", es
.type
, es
.pid
, param
->prog
);
806 if((req_vpid
== -1) || (req_vpid
== es
.pid
))
808 param
->vtype
= IS_VIDEO(es
.type
) ? es
.type
: es
.subtype
;
809 param
->vpid
= es
.pid
;
815 if(((req_vpid
== -2) || (num_packets
>= NUM_CONSECUTIVE_AUDIO_PACKETS
)) && audio_found
&& !param
->probe
)
817 //novideo or we have at least 348 audio packets (64 KB) without video (TS with audio only)
824 if((req_spid
== -1) || (req_spid
== es
.pid
))
826 param
->stype
= es
.type
;
827 param
->spid
= es
.pid
;
834 if((req_apid
== -1) || (req_apid
== es
.pid
))
836 param
->atype
= IS_AUDIO(es
.type
) ? es
.type
: es
.subtype
;
837 param
->apid
= es
.pid
;
842 if(audio_found
&& (param
->apid
== es
.pid
) && (! video_found
))
845 if((has_tables
==0) && (video_found
&& audio_found
) && (pos
>= 1000000))
850 for(i
=0; i
<8192; i
++)
852 if(pes_priv1
[i
].buf
!= NULL
)
854 free(pes_priv1
[i
].buf
);
855 pes_priv1
[i
].buf
= NULL
;
856 pes_priv1
[i
].pos
= 0;
862 if(param
->vtype
== VIDEO_MPEG1
)
863 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "VIDEO MPEG1(pid=%d) ", param
->vpid
);
864 else if(param
->vtype
== VIDEO_MPEG2
)
865 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "VIDEO MPEG2(pid=%d) ", param
->vpid
);
866 else if(param
->vtype
== VIDEO_MPEG4
)
867 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "VIDEO MPEG4(pid=%d) ", param
->vpid
);
868 else if(param
->vtype
== VIDEO_H264
)
869 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "VIDEO H264(pid=%d) ", param
->vpid
);
870 else if(param
->vtype
== VIDEO_VC1
)
871 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "VIDEO VC1(pid=%d) ", param
->vpid
);
872 else if(param
->vtype
== VIDEO_AVC
)
873 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "VIDEO AVC(NAL-H264, pid=%d) ", param
->vpid
);
877 param
->vtype
= UNKNOWN
;
878 //WE DIDN'T MATCH ANY VIDEO STREAM
879 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "NO VIDEO! ");
882 if(param
->atype
== AUDIO_MP2
)
883 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "AUDIO MPA(pid=%d)", param
->apid
);
884 else if(param
->atype
== AUDIO_A52
)
885 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "AUDIO A52(pid=%d)", param
->apid
);
886 else if(param
->atype
== AUDIO_DTS
)
887 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "AUDIO DTS(pid=%d)", param
->apid
);
888 else if(param
->atype
== AUDIO_LPCM_BE
)
889 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "AUDIO LPCM(pid=%d)", param
->apid
);
890 else if(param
->atype
== AUDIO_AAC
)
891 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "AUDIO AAC(pid=%d)", param
->apid
);
892 else if(param
->atype
== AUDIO_AAC_LATM
)
893 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "AUDIO AAC LATM(pid=%d)", param
->apid
);
894 else if(param
->atype
== AUDIO_TRUEHD
)
895 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "AUDIO TRUEHD(pid=%d)", param
->apid
);
899 param
->atype
= UNKNOWN
;
900 //WE DIDN'T MATCH ANY AUDIO STREAM, SO WE FORCE THE DEMUXER TO IGNORE AUDIO
901 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "NO AUDIO! ");
904 if(IS_SUB(param
->stype
))
905 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " SUB %s(pid=%d) ", (param
->stype
==SPU_DVD
? "DVD" : param
->stype
==SPU_DVB
? "DVB" : "Teletext"), param
->spid
);
908 param
->stype
= UNKNOWN
;
909 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " NO SUBS (yet)! ");
912 if(video_found
|| audio_found
)
916 p
= progid_for_pid(priv
, video_found
? param
->vpid
: param
->apid
, 0);
921 if(demuxer
->stream
->eof
&& (ret
== 0))
923 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " PROGRAM N. %d\n", param
->prog
);
926 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "\n");
929 for(i
=0; i
<8192; i
++)
931 if(priv
->ts
.pids
[i
] != NULL
)
933 priv
->ts
.pids
[i
]->payload_size
= 0;
934 priv
->ts
.pids
[i
]->pts
= priv
->ts
.pids
[i
]->last_pts
= 0;
935 priv
->ts
.pids
[i
]->last_cc
= -1;
936 priv
->ts
.pids
[i
]->is_synced
= 0;
943 static int parse_avc_sps(uint8_t *buf
, int len
, int *w
, int *h
)
947 mp_mpeg_header_t picture
;
953 sps_len
= (buf
[6] << 8) | buf
[7];
954 if(!sps_len
|| (sps_len
> len
- 8))
957 picture
.display_picture_width
= picture
.display_picture_height
= 0;
958 h264_parse_sps(&picture
, ptr
, len
- 8);
959 if(!picture
.display_picture_width
|| !picture
.display_picture_height
)
961 *w
= picture
.display_picture_width
;
962 *h
= picture
.display_picture_height
;
966 static demuxer_t
*demux_open_ts(demuxer_t
* demuxer
)
970 sh_video_t
*sh_video
;
971 sh_audio_t
*sh_audio
;
973 tsdemux_init_t params
;
974 ts_priv_t
* priv
= demuxer
->priv
;
976 mp_msg(MSGT_DEMUX
, MSGL_V
, "DEMUX OPEN, AUDIO_ID: %d, VIDEO_ID: %d, SUBTITLE_ID: %d,\n",
977 demuxer
->audio
->id
, demuxer
->video
->id
, demuxer
->sub
->id
);
980 demuxer
->type
= DEMUXER_TYPE_MPEG_TS
;
983 stream_reset(demuxer
->stream
);
985 packet_size
= ts_check_file(demuxer
);
989 priv
= calloc(1, sizeof(ts_priv_t
));
992 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "DEMUX_OPEN_TS, couldn't allocate enough memory for ts->priv, exit\n");
996 for(i
=0; i
< 8192; i
++)
998 priv
->ts
.pids
[i
] = NULL
;
999 priv
->ts
.streams
[i
].id
= -3;
1001 priv
->pat
.progs
= NULL
;
1002 priv
->pat
.progs_cnt
= 0;
1003 priv
->pat
.section
.buffer
= NULL
;
1004 priv
->pat
.section
.buffer_len
= 0;
1009 priv
->keep_broken
= ts_keep_broken
;
1010 priv
->ts
.packet_size
= packet_size
;
1013 demuxer
->priv
= priv
;
1014 if(demuxer
->stream
->type
!= STREAMTYPE_FILE
)
1015 demuxer
->seekable
= 1;
1017 demuxer
->seekable
= 1;
1020 params
.atype
= params
.vtype
= params
.stype
= UNKNOWN
;
1021 params
.apid
= demuxer
->audio
->id
;
1022 params
.vpid
= demuxer
->video
->id
;
1023 params
.spid
= demuxer
->sub
->id
;
1024 params
.prog
= ts_prog
;
1025 params
.probe
= ts_probe
;
1027 if(demuxer
->opts
->audio_lang
!= NULL
)
1029 strncpy(params
.alang
, demuxer
->opts
->audio_lang
, 3);
1030 params
.alang
[3] = 0;
1033 memset(params
.alang
, 0, 4);
1035 start_pos
= ts_detect_streams(demuxer
, ¶ms
);
1037 demuxer
->sub
->id
= params
.spid
;
1038 priv
->prog
= params
.prog
;
1040 if(params
.vtype
!= UNKNOWN
)
1042 ts_add_stream(demuxer
, priv
->ts
.pids
[params
.vpid
]);
1043 sh_video
= priv
->ts
.streams
[params
.vpid
].sh
;
1044 demuxer
->video
->id
= priv
->ts
.streams
[params
.vpid
].id
;
1045 sh_video
->ds
= demuxer
->video
;
1046 sh_video
->format
= params
.vtype
;
1047 demuxer
->video
->sh
= sh_video
;
1050 if(params
.atype
!= UNKNOWN
)
1052 ES_stream_t
*es
= priv
->ts
.pids
[params
.apid
];
1054 if(!IS_AUDIO(es
->type
) && !IS_AUDIO(es
->subtype
) && IS_AUDIO(params
.atype
)) es
->subtype
= params
.atype
;
1055 ts_add_stream(demuxer
, priv
->ts
.pids
[params
.apid
]);
1056 sh_audio
= priv
->ts
.streams
[params
.apid
].sh
;
1057 demuxer
->audio
->id
= priv
->ts
.streams
[params
.apid
].id
;
1058 sh_audio
->ds
= demuxer
->audio
;
1059 sh_audio
->format
= params
.atype
;
1060 demuxer
->audio
->sh
= sh_audio
;
1064 mp_msg(MSGT_DEMUXER
,MSGL_V
, "Opened TS demuxer, audio: %x(pid %d), video: %x(pid %d)...POS=%"PRIu64
", PROBE=%"PRIu64
"\n", params
.atype
, demuxer
->audio
->id
, params
.vtype
, demuxer
->video
->id
, (uint64_t) start_pos
, ts_probe
);
1067 start_pos
= (start_pos
<= priv
->ts
.packet_size
? 0 : start_pos
- priv
->ts
.packet_size
);
1068 demuxer
->movi_start
= start_pos
;
1069 demuxer
->reference_clock
= MP_NOPTS_VALUE
;
1070 stream_reset(demuxer
->stream
);
1071 stream_seek(demuxer
->stream
, start_pos
); //IF IT'S FROM A PIPE IT WILL FAIL, BUT WHO CARES?
1074 priv
->last_pid
= 8192; //invalid pid
1076 for(i
= 0; i
< 3; i
++)
1078 priv
->fifo
[i
].pack
= NULL
;
1079 priv
->fifo
[i
].offset
= 0;
1081 priv
->fifo
[0].ds
= demuxer
->audio
;
1082 priv
->fifo
[1].ds
= demuxer
->video
;
1083 priv
->fifo
[2].ds
= demuxer
->sub
;
1085 priv
->fifo
[0].buffer_size
= 1536;
1086 priv
->fifo
[1].buffer_size
= 32767;
1087 priv
->fifo
[2].buffer_size
= 32767;
1089 priv
->pat
.section
.buffer_len
= 0;
1090 for(i
= 0; i
< priv
->pmt_cnt
; i
++)
1091 priv
->pmt
[i
].section
.buffer_len
= 0;
1093 demuxer
->filepos
= stream_tell(demuxer
->stream
);
1097 static void demux_close_ts(demuxer_t
* demuxer
)
1100 ts_priv_t
*priv
= (ts_priv_t
*) demuxer
->priv
;
1104 if(priv
->pat
.section
.buffer
)
1105 free(priv
->pat
.section
.buffer
);
1107 free(priv
->pat
.progs
);
1111 for(i
= 0; i
< priv
->pmt_cnt
; i
++)
1113 if(priv
->pmt
[i
].section
.buffer
)
1114 free(priv
->pmt
[i
].section
.buffer
);
1116 free(priv
->pmt
[i
].es
);
1126 #define getbits mp_getbits
1128 static int mp4_parse_sl_packet(pmt_t
*pmt
, uint8_t *buf
, uint16_t packet_len
, int pid
, ES_stream_t
*pes_es
)
1130 int i
, n
, m
, mp4_es_id
= -1;
1132 uint32_t pl_size
= 0;
1134 mp4_es_descr_t
*es
= NULL
;
1135 mp4_sl_config_t
*sl
= NULL
;
1136 uint8_t au_start
= 0, au_end
= 0, rap_flag
= 0, ocr_flag
= 0, padding
= 0, padding_bits
= 0, idle
= 0;
1138 pes_es
->is_synced
= 0;
1139 mp_msg(MSGT_DEMUXER
,MSGL_V
, "mp4_parse_sl_packet, pid: %d, pmt: %pm, packet_len: %d\n", pid
, pmt
, packet_len
);
1140 if(! pmt
|| !packet_len
)
1143 for(i
= 0; i
< pmt
->es_cnt
; i
++)
1145 if(pmt
->es
[i
].pid
== pid
)
1146 mp4_es_id
= pmt
->es
[i
].mp4_es_id
;
1151 for(i
= 0; i
< pmt
->mp4es_cnt
; i
++)
1153 if(pmt
->mp4es
[i
].id
== mp4_es_id
)
1154 es
= &(pmt
->mp4es
[i
]);
1159 pes_es
->subtype
= es
->decoder
.object_type
;
1165 //now es is the complete es_descriptor of out mp4 ES stream
1166 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "ID: %d, FLAGS: 0x%x, subtype: %x\n", es
->id
, sl
->flags
, pes_es
->subtype
);
1170 pes_es
->sl
.au_start
= au_start
= getbits(buf
, n
++, 1);
1172 pes_es
->sl
.au_start
= (pes_es
->sl
.last_au_end
? 1 : 0);
1174 pes_es
->sl
.au_end
= au_end
= getbits(buf
, n
++, 1);
1176 if(!sl
->au_start
&& !sl
->au_end
)
1178 pes_es
->sl
.au_start
= pes_es
->sl
.au_end
= au_start
= au_end
= 1;
1180 pes_es
->sl
.last_au_end
= pes_es
->sl
.au_end
;
1184 ocr_flag
= getbits(buf
, n
++, 1);
1186 idle
= getbits(buf
, n
++, 1);
1188 padding
= getbits(buf
, n
++, 1);
1191 padding_bits
= getbits(buf
, n
, 3);
1195 if(idle
|| (padding
&& !padding_bits
))
1197 pes_es
->payload_size
= 0;
1201 //(! idle && (!padding || padding_bits != 0)) is true
1202 n
+= sl
->packet_seqnum_len
;
1204 deg_flag
= getbits(buf
, n
++, 1);
1211 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "OCR: %d bits\n", sl
->ocr_len
);
1214 if(packet_len
* 8 <= n
)
1217 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "\nAU_START: %d, AU_END: %d\n", au_start
, au_end
);
1220 int dts_flag
= 0, cts_flag
= 0, ib_flag
= 0;
1222 if(sl
->random_accesspoint
)
1223 rap_flag
= getbits(buf
, n
++, 1);
1225 //check commented because it seems it's rarely used, and we need this flag set in case of au_start
1226 //the decoder will eventually discard the payload if it can't decode it
1227 //if(rap_flag || sl->random_accesspoint_only)
1228 pes_es
->is_synced
= 1;
1230 n
+= sl
->au_seqnum_len
;
1231 if(packet_len
* 8 <= n
+8)
1235 dts_flag
= getbits(buf
, n
++, 1);
1236 cts_flag
= getbits(buf
, n
++, 1);
1238 if(sl
->instant_bitrate_len
)
1239 ib_flag
= getbits(buf
, n
++, 1);
1240 if(packet_len
* 8 <= n
+8)
1242 if(dts_flag
&& (sl
->ts_len
> 0))
1245 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "DTS: %d bits\n", sl
->ts_len
);
1247 if(packet_len
* 8 <= n
+8)
1249 if(cts_flag
&& (sl
->ts_len
> 0))
1253 while(i
< sl
->ts_len
)
1255 m
= FFMIN(8, sl
->ts_len
- i
);
1256 v
|= getbits(buf
, n
, m
);
1257 if(sl
->ts_len
- i
> 8)
1261 if(packet_len
* 8 <= n
+8)
1265 pes_es
->pts
= (double) v
/ (double) sl
->ts_resolution
;
1266 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "CTS: %d bits, value: %"PRIu64
"/%d = %.3f\n", sl
->ts_len
, v
, sl
->ts_resolution
, pes_es
->pts
);
1272 while(i
< sl
->au_len
)
1274 m
= FFMIN(8, sl
->au_len
- i
);
1275 pl_size
|= getbits(buf
, n
, m
);
1276 if(sl
->au_len
- i
> 8)
1280 if(packet_len
* 8 <= n
+8)
1283 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "AU_LEN: %u (%d bits)\n", pl_size
, sl
->au_len
);
1285 n
+= sl
->instant_bitrate_len
;
1289 if(0 < pl_size
&& pl_size
< pes_es
->payload_size
)
1290 pes_es
->payload_size
= pl_size
;
1292 mp_msg(MSGT_DEMUXER
,MSGL_V
, "mp4_parse_sl_packet, n=%d, m=%d, size from pes hdr: %u, sl hdr size: %u, RAP FLAGS: %d/%d\n",
1293 n
, m
, pes_es
->payload_size
, pl_size
, (int) rap_flag
, (int) sl
->random_accesspoint_only
);
1298 //this function parses the extension fields in the PES header and returns the substream_id, or -1 in case of errors
1299 static int parse_pes_extension_fields(unsigned char *p
, int pkt_len
)
1302 unsigned char flags
;
1304 if(!(p
[7] & 0x1)) //no extension_field
1313 if(p
[7] & 0x20) //escr_flag
1315 if(p
[7] & 0x10) //es_rate_flag
1317 if(p
[7] & 0x08)//dsm_trick_mode is unsupported, skip
1319 skip
= 0;//don't let's parse the extension fields
1321 if(p
[7] & 0x04) //additional_copy_info
1323 if(p
[7] & 0x02) //pes_crc_flag
1325 if(skip
>= pkt_len
) //too few bytes
1329 if(flags
& 0x80) //pes_private_data_flag
1333 if(flags
& 0x40) //pack_header_field_flag
1335 unsigned char l
= p
[skip
];
1338 if(flags
& 0x20) //program_packet_sequence_counter
1340 if(flags
& 0x10) //p_std
1344 if(flags
& 0x01) //finally the long desired pes_extension2
1346 unsigned char l
= p
[skip
]; //ext2 flag+len
1348 if((l
== 0x81) && (skip
< pkt_len
))
1351 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "SUBSTREAM_ID=%d (0x%02X)\n", ssid
, ssid
);
1359 static int pes_parse2(unsigned char *buf
, uint16_t packet_len
, ES_stream_t
*es
, int32_t type_from_pmt
, pmt_t
*pmt
, int pid
)
1362 uint32_t header_len
;
1365 uint32_t pkt_len
, pes_is_aligned
;
1367 //Here we are always at the start of a PES packet
1368 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2(%p, %d): \n", buf
, (uint32_t) packet_len
);
1370 if(packet_len
== 0 || packet_len
> 184)
1372 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2, BUFFER LEN IS TOO SMALL OR TOO BIG: %d EXIT\n", packet_len
);
1377 pkt_len
= packet_len
;
1380 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2: HEADER %02x %02x %02x %02x\n", p
[0], p
[1], p
[2], p
[3]);
1381 if (p
[0] || p
[1] || (p
[2] != 1))
1383 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2: error HEADER %02x %02x %02x (should be 0x000001) \n", p
[0], p
[1], p
[2]);
1390 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2: packet too short: %d, exit\n", packet_len
);
1394 es
->payload_size
= (p
[4] << 8 | p
[5]);
1395 pes_is_aligned
= (p
[6] & 4);
1401 { /* pts available */
1402 pts
= (int64_t)(p
[9] & 0x0E) << 29 ;
1403 pts
|= p
[10] << 22 ;
1404 pts
|= (p
[11] & 0xFE) << 14 ;
1406 pts
|= (p
[13] & 0xFE) >> 1 ;
1408 es
->pts
= pts
/ 90000.0;
1417 if (header_len
+ 9 > pkt_len
) //9 are the bytes read up to the header_length field
1419 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "demux_ts: illegal value for PES_header_data_length (0x%02x)\n", header_len
);
1425 int ssid
= parse_pes_extension_fields(p
, pkt_len
);
1426 if((audio_substream_id
!=-1) && (ssid
!= audio_substream_id
))
1428 if(ssid
== 0x72 && type_from_pmt
!= AUDIO_DTS
&& type_from_pmt
!= SPU_PGS
)
1429 es
->type
= type_from_pmt
= AUDIO_TRUEHD
;
1432 p
+= header_len
+ 9;
1433 packet_len
-= header_len
+ 3;
1435 if(es
->payload_size
)
1436 es
->payload_size
-= header_len
+ 3;
1439 es
->is_synced
= 1; //only for SL streams we have to make sure it's really true, see below
1440 if (stream_id
== 0xbd)
1442 mp_msg(MSGT_DEMUX
, MSGL_DBG3
, "pes_parse2: audio buf = %02X %02X %02X %02X %02X %02X %02X %02X, 80: %d\n",
1443 p
[0], p
[1], p
[2], p
[3], p
[4], p
[5], p
[6], p
[7], p
[0] & 0x80);
1447 * we check the descriptor tag first because some stations
1448 * do not include any of the A52 header info in their audio tracks
1449 * these "raw" streams may begin with a byte that looks like a stream type.
1453 if(type_from_pmt
== SPU_PGS
)
1456 es
->size
= packet_len
;
1458 es
->payload_size
-= packet_len
;
1462 (type_from_pmt
== AUDIO_A52
) || /* A52 - raw */
1463 (packet_len
>= 2 && p
[0] == 0x0B && p
[1] == 0x77) /* A52 - syncword */
1466 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "A52 RAW OR SYNCWORD\n");
1468 es
->size
= packet_len
;
1469 es
->type
= AUDIO_A52
;
1470 es
->payload_size
-= packet_len
;
1475 else if(type_from_pmt
== SPU_DVB
||
1476 (packet_len
>= 1 && (p
[0] == 0x20) && pes_is_aligned
)) // && p[1] == 0x00))
1479 es
->size
= packet_len
;
1481 es
->payload_size
-= packet_len
;
1485 else if (pes_is_aligned
&& packet_len
>= 1 && ((p
[0] & 0xE0) == 0x20)) //SPU_DVD
1489 es
->size
= packet_len
-1;
1491 es
->payload_size
-= packet_len
;
1495 else if (pes_is_aligned
&& packet_len
>= 4 && (p
[0] & 0xF8) == 0x80)
1497 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "A52 WITH HEADER\n");
1499 es
->size
= packet_len
- 4;
1500 es
->type
= AUDIO_A52
;
1501 es
->payload_size
-= packet_len
;
1505 else if (pes_is_aligned
&& packet_len
>= 1 && ((p
[0]&0xf0) == 0xa0))
1509 for (pcm_offset
=0; ++pcm_offset
< packet_len
-1 ; )
1511 if (p
[pcm_offset
] == 0x01 && p
[pcm_offset
+1] == 0x80)
1518 es
->start
= p
+ pcm_offset
;
1519 es
->size
= packet_len
- pcm_offset
;
1520 es
->type
= AUDIO_LPCM_BE
;
1521 es
->payload_size
-= packet_len
;
1527 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "PES_PRIVATE1\n");
1529 es
->size
= packet_len
;
1530 es
->type
= (type_from_pmt
== UNKNOWN
? PES_PRIVATE1
: type_from_pmt
);
1531 es
->payload_size
-= packet_len
;
1536 else if(((stream_id
>= 0xe0) && (stream_id
<= 0xef)) || (stream_id
== 0xfd && type_from_pmt
!= UNKNOWN
))
1539 es
->size
= packet_len
;
1540 if(type_from_pmt
!= UNKNOWN
)
1541 es
->type
= type_from_pmt
;
1543 es
->type
= VIDEO_MPEG2
;
1544 if(es
->payload_size
)
1545 es
->payload_size
-= packet_len
;
1547 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2: M2V size %d\n", es
->size
);
1550 else if ((stream_id
== 0xfa))
1555 if(type_from_pmt
!= UNKNOWN
) //MP4 A/V or SL
1558 es
->size
= packet_len
;
1559 es
->type
= type_from_pmt
;
1561 if(type_from_pmt
== SL_PES_STREAM
)
1563 //if(pes_is_aligned)
1565 l
= mp4_parse_sl_packet(pmt
, p
, packet_len
, pid
, es
);
1566 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "L=%d, TYPE=%x\n", l
, type_from_pmt
);
1569 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2: couldn't parse SL header, passing along full PES payload\n");
1578 if(es
->payload_size
)
1579 es
->payload_size
-= packet_len
;
1583 else if ((stream_id
& 0xe0) == 0xc0)
1586 es
->size
= packet_len
;
1588 if(type_from_pmt
!= UNKNOWN
)
1589 es
->type
= type_from_pmt
;
1591 es
->type
= AUDIO_MP2
;
1593 es
->payload_size
-= packet_len
;
1597 else if (type_from_pmt
!= -1) //as a last resort here we trust the PMT, if present
1600 es
->size
= packet_len
;
1601 es
->type
= type_from_pmt
;
1602 es
->payload_size
-= packet_len
;
1608 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2: unknown packet, id: %x\n", stream_id
);
1618 static int ts_sync(stream_t
*stream
)
1620 mp_msg(MSGT_DEMUX
, MSGL_DBG3
, "TS_SYNC \n");
1622 while (!stream
->eof
)
1623 if (stream_read_char(stream
) == 0x47)
1630 static void ts_dump_streams(ts_priv_t
*priv
)
1634 for(i
= 0; i
< 3; i
++)
1636 if((priv
->fifo
[i
].pack
!= NULL
) && (priv
->fifo
[i
].offset
!= 0))
1638 resize_demux_packet(priv
->fifo
[i
].pack
, priv
->fifo
[i
].offset
);
1639 ds_add_packet(priv
->fifo
[i
].ds
, priv
->fifo
[i
].pack
);
1640 priv
->fifo
[i
].offset
= 0;
1641 priv
->fifo
[i
].pack
= NULL
;
1647 static inline int32_t prog_idx_in_pat(ts_priv_t
*priv
, uint16_t progid
)
1651 if(priv
->pat
.progs
== NULL
)
1654 for(x
= 0; x
< priv
->pat
.progs_cnt
; x
++)
1656 if(priv
->pat
.progs
[x
].id
== progid
)
1664 static inline int32_t prog_id_in_pat(ts_priv_t
*priv
, uint16_t pid
)
1668 if(priv
->pat
.progs
== NULL
)
1671 for(x
= 0; x
< priv
->pat
.progs_cnt
; x
++)
1673 if(priv
->pat
.progs
[x
].pmt_pid
== pid
)
1674 return priv
->pat
.progs
[x
].id
;
1680 static int collect_section(ts_section_t
*section
, int is_start
, unsigned char *buff
, int size
)
1686 mp_msg(MSGT_DEMUX
, MSGL_V
, "COLLECT_SECTION, start: %d, size: %d, collected: %d\n", is_start
, size
, section
->buffer_len
);
1687 if(! is_start
&& !section
->buffer_len
)
1692 if(! section
->buffer
)
1694 section
->buffer
= malloc(4096 + 256);
1695 if(section
->buffer
== NULL
)
1698 section
->buffer_len
= 0;
1701 if(size
+ section
->buffer_len
> 4096+256)
1703 mp_msg(MSGT_DEMUX
, MSGL_V
, "COLLECT_SECTION, excessive len: %d + %d\n", section
->buffer_len
, size
);
1707 memcpy(&(section
->buffer
[section
->buffer_len
]), buff
, size
);
1708 section
->buffer_len
+= size
;
1710 if(section
->buffer_len
< 3)
1713 skip
= section
->buffer
[0];
1714 if(skip
+ 4 > section
->buffer_len
)
1717 ptr
= &(section
->buffer
[skip
+ 1]);
1719 tlen
= ((ptr
[1] & 0x0f) << 8) | ptr
[2];
1720 mp_msg(MSGT_DEMUX
, MSGL_V
, "SKIP: %d+1, TID: %d, TLEN: %d, COLLECTED: %d\n", skip
, tid
, tlen
, section
->buffer_len
);
1721 if(section
->buffer_len
< (skip
+1+3+tlen
))
1723 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "DATA IS NOT ENOUGH, NEXT TIME\n");
1730 static int parse_pat(ts_priv_t
* priv
, int is_start
, unsigned char *buff
, int size
)
1734 unsigned char *base
;
1737 struct pat_progs_t
*tmp
;
1738 ts_section_t
*section
;
1740 section
= &(priv
->pat
.section
);
1741 skip
= collect_section(section
, is_start
, buff
, size
);
1745 ptr
= &(section
->buffer
[skip
]);
1747 priv
->pat
.table_id
= ptr
[0];
1748 if(priv
->pat
.table_id
!= 0)
1750 priv
->pat
.ssi
= (ptr
[1] >> 7) & 0x1;
1751 priv
->pat
.curr_next
= ptr
[5] & 0x01;
1752 priv
->pat
.ts_id
= (ptr
[3] << 8 ) | ptr
[4];
1753 priv
->pat
.version_number
= (ptr
[5] >> 1) & 0x1F;
1754 priv
->pat
.section_length
= ((ptr
[1] & 0x03) << 8 ) | ptr
[2];
1755 priv
->pat
.section_number
= ptr
[6];
1756 priv
->pat
.last_section_number
= ptr
[7];
1758 //check_crc32(0xFFFFFFFFL, ptr, priv->pat.buffer_len - 4, &ptr[priv->pat.buffer_len - 4]);
1759 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_PAT: section_len: %d, section %d/%d\n", priv
->pat
.section_length
, priv
->pat
.section_number
, priv
->pat
.last_section_number
);
1761 entries
= (int) (priv
->pat
.section_length
- 9) / 4; //entries per section
1763 for(i
=0; i
< entries
; i
++)
1766 base
= &ptr
[8 + i
*4];
1767 progid
= (base
[0] << 8) | base
[1];
1769 if((idx
= prog_idx_in_pat(priv
, progid
)) == -1)
1771 int sz
= sizeof(struct pat_progs_t
) * (priv
->pat
.progs_cnt
+1);
1772 tmp
= realloc_struct(priv
->pat
.progs
, priv
->pat
.progs_cnt
+1, sizeof(struct pat_progs_t
));
1775 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "PARSE_PAT: COULDN'T REALLOC %d bytes, NEXT\n", sz
);
1778 priv
->pat
.progs
= tmp
;
1779 idx
= priv
->pat
.progs_cnt
;
1780 priv
->pat
.progs_cnt
++;
1783 priv
->pat
.progs
[idx
].id
= progid
;
1784 priv
->pat
.progs
[idx
].pmt_pid
= ((base
[2] & 0x1F) << 8) | base
[3];
1785 mp_msg(MSGT_DEMUX
, MSGL_V
, "PROG: %d (%d-th of %d), PMT: %d\n", priv
->pat
.progs
[idx
].id
, i
+1, entries
, priv
->pat
.progs
[idx
].pmt_pid
);
1786 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "PROGRAM_ID=%d (0x%02X), PMT_PID: %d(0x%02X)\n",
1787 progid
, progid
, priv
->pat
.progs
[idx
].pmt_pid
, priv
->pat
.progs
[idx
].pmt_pid
);
1794 static inline int32_t es_pid_in_pmt(pmt_t
* pmt
, uint16_t pid
)
1804 for(i
= 0; i
< pmt
->es_cnt
; i
++)
1806 if(pmt
->es
[i
].pid
== pid
)
1814 static uint16_t get_mp4_desc_len(uint8_t *buf
, int *len
)
1816 //uint16_t i = 0, size = 0;
1817 int i
= 0, j
, size
= 0;
1819 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "PARSE_MP4_DESC_LEN(%d), bytes: ", *len
);
1823 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, " %x ", buf
[i
]);
1824 size
|= (buf
[i
] & 0x7f);
1825 if(!(buf
[i
] & 0x80))
1830 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, ", SIZE=%d\n", size
);
1837 static uint16_t parse_mp4_slconfig_descriptor(uint8_t *buf
, int len
, void *elem
)
1841 mp4_sl_config_t
*sl
;
1843 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4_SLCONFIG_DESCRIPTOR(%d)\n", len
);
1844 es
= (mp4_es_descr_t
*) elem
;
1847 mp_msg(MSGT_DEMUX
, MSGL_V
, "argh! NULL elem passed, skip\n");
1852 sl
->ts_len
= sl
->ocr_len
= sl
->au_len
= sl
->instant_bitrate_len
= sl
->degr_len
= sl
->au_seqnum_len
= sl
->packet_seqnum_len
= 0;
1853 sl
->ocr
= sl
->dts
= sl
->cts
= 0;
1860 sl
->ts_resolution
= (buf
[i
] << 24) | (buf
[i
+1] << 16) | (buf
[i
+2] << 8) | buf
[i
+3];
1862 sl
->ocr_resolution
= (buf
[i
] << 24) | (buf
[i
+1] << 16) | (buf
[i
+2] << 8) | buf
[i
+3];
1864 sl
->ts_len
= buf
[i
];
1866 sl
->ocr_len
= buf
[i
];
1868 sl
->au_len
= buf
[i
];
1870 sl
->instant_bitrate_len
= buf
[i
];
1872 sl
->degr_len
= (buf
[i
] >> 4) & 0x0f;
1873 sl
->au_seqnum_len
= ((buf
[i
] & 0x0f) << 1) | ((buf
[i
+1] >> 7) & 0x01);
1875 sl
->packet_seqnum_len
= ((buf
[i
] >> 2) & 0x1f);
1879 else if(buf
[0] == 1)
1882 sl
->ts_resolution
= 1000;
1886 else if(buf
[0] == 2)
1897 sl
->au_start
= (sl
->flags
>> 7) & 0x1;
1898 sl
->au_end
= (sl
->flags
>> 6) & 0x1;
1899 sl
->random_accesspoint
= (sl
->flags
>> 5) & 0x1;
1900 sl
->random_accesspoint_only
= (sl
->flags
>> 4) & 0x1;
1901 sl
->padding
= (sl
->flags
>> 3) & 0x1;
1902 sl
->use_ts
= (sl
->flags
>> 2) & 0x1;
1903 sl
->idle
= (sl
->flags
>> 1) & 0x1;
1904 sl
->duration
= sl
->flags
& 0x1;
1908 sl
->timescale
= (buf
[i
] << 24) | (buf
[i
+1] << 16) | (buf
[i
+2] << 8) | buf
[i
+3];
1910 sl
->au_duration
= (buf
[i
] << 8) | buf
[i
+1];
1912 sl
->cts_duration
= (buf
[i
] << 8) | buf
[i
+1];
1915 else //no support for fixed durations atm
1916 sl
->timescale
= sl
->au_duration
= sl
->cts_duration
= 0;
1918 mp_msg(MSGT_DEMUX
, MSGL_V
, "MP4SLCONFIG(len=0x%x), predef: %d, flags: %x, use_ts: %d, tslen: %d, timescale: %d, dts: %"PRIu64
", cts: %"PRIu64
"\n",
1919 len
, buf
[0], sl
->flags
, sl
->use_ts
, sl
->ts_len
, sl
->timescale
, (uint64_t) sl
->dts
, (uint64_t) sl
->cts
);
1924 static int parse_mp4_descriptors(pmt_t
*pmt
, uint8_t *buf
, int len
, void *elem
);
1926 static uint16_t parse_mp4_decoder_config_descriptor(pmt_t
*pmt
, uint8_t *buf
, int len
, void *elem
)
1930 mp4_decoder_config_t
*dec
;
1932 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4_DECODER_CONFIG_DESCRIPTOR(%d)\n", len
);
1933 es
= (mp4_es_descr_t
*) elem
;
1936 mp_msg(MSGT_DEMUX
, MSGL_V
, "argh! NULL elem passed, skip\n");
1939 dec
= (mp4_decoder_config_t
*) &(es
->decoder
);
1941 dec
->object_type
= buf
[i
];
1942 dec
->stream_type
= (buf
[i
+1]>>2) & 0x3f;
1944 if(dec
->object_type
== 1 && dec
->stream_type
== 1)
1946 dec
->object_type
= MP4_OD
;
1947 dec
->stream_type
= MP4_OD
;
1949 else if(dec
->stream_type
== 4)
1951 if(dec
->object_type
== 0x6a)
1952 dec
->object_type
= VIDEO_MPEG1
;
1953 if(dec
->object_type
>= 0x60 && dec
->object_type
<= 0x65)
1954 dec
->object_type
= VIDEO_MPEG2
;
1955 else if(dec
->object_type
== 0x20)
1956 dec
->object_type
= VIDEO_MPEG4
;
1957 else if(dec
->object_type
== 0x21)
1958 dec
->object_type
= VIDEO_AVC
;
1959 /*else if(dec->object_type == 0x22)
1960 fprintf(stderr, "TYPE 0x22\n");*/
1961 else dec
->object_type
= UNKNOWN
;
1963 else if(dec
->stream_type
== 5)
1965 if(dec
->object_type
== 0x40)
1966 dec
->object_type
= AUDIO_AAC
;
1967 else if(dec
->object_type
== 0x6b)
1968 dec
->object_type
= AUDIO_MP2
;
1969 else if(dec
->object_type
>= 0x66 && dec
->object_type
<= 0x69)
1970 dec
->object_type
= AUDIO_MP2
;
1972 dec
->object_type
= UNKNOWN
;
1975 dec
->object_type
= dec
->stream_type
= UNKNOWN
;
1977 if(dec
->object_type
!= UNKNOWN
)
1979 //update the type of the current stream
1980 for(j
= 0; j
< pmt
->es_cnt
; j
++)
1982 if(pmt
->es
[j
].mp4_es_id
== es
->id
)
1984 pmt
->es
[j
].type
= SL_PES_STREAM
;
1990 parse_mp4_descriptors(pmt
, &buf
[13], len
-13, dec
);
1992 mp_msg(MSGT_DEMUX
, MSGL_V
, "MP4DECODER(0x%x), object_type: 0x%x, stream_type: 0x%x\n", len
, dec
->object_type
, dec
->stream_type
);
1997 static uint16_t parse_mp4_decoder_specific_descriptor(uint8_t *buf
, int len
, void *elem
)
2000 mp4_decoder_config_t
*dec
;
2002 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4_DECODER_SPECIFIC_DESCRIPTOR(%d)\n", len
);
2003 dec
= (mp4_decoder_config_t
*) elem
;
2006 mp_msg(MSGT_DEMUX
, MSGL_V
, "argh! NULL elem passed, skip\n");
2010 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "MP4 SPECIFIC INFO BYTES: \n");
2011 for(i
=0; i
<len
; i
++)
2012 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "%02x ", buf
[i
]);
2013 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "\n");
2015 if(len
> MAX_EXTRADATA_SIZE
)
2017 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "DEMUX_TS, EXTRADATA SUSPICIOUSLY BIG: %d, REFUSED\r\n", len
);
2020 memcpy(dec
->buf
, buf
, len
);
2021 dec
->buf_size
= len
;
2026 static uint16_t parse_mp4_es_descriptor(pmt_t
*pmt
, uint8_t *buf
, int len
)
2028 int i
= 0, j
= 0, k
, found
;
2030 mp4_es_descr_t es
, *target_es
= NULL
, *tmp
;
2032 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4ES: len=%d\n", len
);
2033 memset(&es
, 0, sizeof(mp4_es_descr_t
));
2036 es
.id
= (buf
[i
] << 8) | buf
[i
+1];
2037 mp_msg(MSGT_DEMUX
, MSGL_V
, "MP4ES_ID: %d\n", es
.id
);
2045 if(flag
& 0x20) //OCR, maybe we need it
2048 j
= parse_mp4_descriptors(pmt
, &buf
[i
], len
-i
, &es
);
2049 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4ES, types after parse_mp4_descriptors: 0x%x, 0x%x\n", es
.decoder
.object_type
, es
.decoder
.stream_type
);
2050 if(es
.decoder
.object_type
!= UNKNOWN
&& es
.decoder
.stream_type
!= UNKNOWN
)
2053 //search this ES_ID if we already have it
2054 for(k
=0; k
< pmt
->mp4es_cnt
; k
++)
2056 if(pmt
->mp4es
[k
].id
== es
.id
)
2058 target_es
= &(pmt
->mp4es
[k
]);
2065 tmp
= realloc_struct(pmt
->mp4es
, pmt
->mp4es_cnt
+1, sizeof(mp4_es_descr_t
));
2068 fprintf(stderr
, "CAN'T REALLOC MP4_ES_DESCR\n");
2072 target_es
= &(pmt
->mp4es
[pmt
->mp4es_cnt
]);
2075 memcpy(target_es
, &es
, sizeof(mp4_es_descr_t
));
2076 mp_msg(MSGT_DEMUX
, MSGL_V
, "MP4ES_CNT: %d, ID=%d\n", pmt
->mp4es_cnt
, target_es
->id
);
2085 static void parse_mp4_object_descriptor(pmt_t
*pmt
, uint8_t *buf
, int len
, void *elem
)
2090 id
= (buf
[0] << 2) | ((buf
[1] & 0xc0) >> 6);
2091 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4_OBJECT_DESCRIPTOR: len=%d, OD_ID=%d\n", len
, id
);
2094 i
+= buf
[2] + 1; //url
2095 mp_msg(MSGT_DEMUX
, MSGL_V
, "URL\n");
2103 j
= parse_mp4_descriptors(pmt
, &(buf
[i
]), len
-i
, elem
);
2104 mp_msg(MSGT_DEMUX
, MSGL_V
, "OBJD, NOW i = %d, j=%d, LEN=%d\n", i
, j
, len
);
2111 static void parse_mp4_iod(pmt_t
*pmt
, uint8_t *buf
, int len
, void *elem
)
2114 mp4_od_t
*iod
= &(pmt
->iod
);
2116 iod
->id
= (buf
[0] << 2) | ((buf
[1] & 0xc0) >> 6);
2117 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4_IOD: len=%d, IOD_ID=%d\n", len
, iod
->id
);
2121 i
+= buf
[2] + 1; //url
2122 mp_msg(MSGT_DEMUX
, MSGL_V
, "URL\n");
2129 j
= parse_mp4_descriptors(pmt
, &(buf
[i
]), len
-i
, elem
);
2130 mp_msg(MSGT_DEMUX
, MSGL_V
, "IOD, NOW i = %d, j=%d, LEN=%d\n", i
, j
, len
);
2136 static int parse_mp4_descriptors(pmt_t
*pmt
, uint8_t *buf
, int len
, void *elem
)
2138 int tag
, descr_len
, i
= 0, j
= 0;
2140 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4_DESCRIPTORS, len=%d\n", len
);
2148 descr_len
= get_mp4_desc_len(&(buf
[i
+1]), &j
);
2149 mp_msg(MSGT_DEMUX
, MSGL_V
, "TAG=%d (0x%x), DESCR_len=%d, len=%d, j=%d\n", tag
, tag
, descr_len
, len
, j
);
2150 if(descr_len
> len
- j
+1)
2152 mp_msg(MSGT_DEMUX
, MSGL_V
, "descriptor is too long, exit\n");
2160 parse_mp4_object_descriptor(pmt
, &(buf
[i
]), descr_len
, elem
);
2163 parse_mp4_iod(pmt
, &(buf
[i
]), descr_len
, elem
);
2166 parse_mp4_es_descriptor(pmt
, &(buf
[i
]), descr_len
);
2169 parse_mp4_decoder_config_descriptor(pmt
, &buf
[i
], descr_len
, elem
);
2172 parse_mp4_decoder_specific_descriptor(&buf
[i
], descr_len
, elem
);
2175 parse_mp4_slconfig_descriptor(&buf
[i
], descr_len
, elem
);
2178 mp_msg(MSGT_DEMUX
, MSGL_V
, "Unsupported mp4 descriptor 0x%x\n", tag
);
2186 static ES_stream_t
*new_pid(ts_priv_t
*priv
, int pid
)
2190 tss
= malloc(sizeof(ES_stream_t
));
2193 memset(tss
, 0, sizeof(ES_stream_t
));
2196 tss
->type
= UNKNOWN
;
2197 tss
->subtype
= UNKNOWN
;
2199 tss
->extradata
= NULL
;
2200 tss
->extradata_alloc
= tss
->extradata_len
= 0;
2201 priv
->ts
.pids
[pid
] = tss
;
2207 static int parse_program_descriptors(pmt_t
*pmt
, uint8_t *buf
, uint16_t len
)
2209 uint16_t i
= 0, k
, olen
= len
;
2213 mp_msg(MSGT_DEMUX
, MSGL_V
, "PROG DESCR, TAG=%x, LEN=%d(%x)\n", buf
[i
], buf
[i
+1], buf
[i
+1]);
2214 if(buf
[i
+1] > len
-2)
2216 mp_msg(MSGT_DEMUX
, MSGL_V
, "ERROR, descriptor len is too long, skipping\n");
2222 if(buf
[i
+3] == 2) //buggy versions of vlc muxer make this non-standard mess (missing iod_scope)
2225 k
= 4; //this is standard compliant
2226 parse_mp4_descriptors(pmt
, &buf
[i
+k
], (int) buf
[i
+1]-(k
-2), NULL
);
2229 len
-= 2 + buf
[i
+1];
2235 static int parse_descriptors(struct pmt_es_t
*es
, uint8_t *ptr
)
2237 int j
, descr_len
, len
;
2240 len
= es
->descr_length
;
2243 descr_len
= ptr
[j
+1];
2244 mp_msg(MSGT_DEMUX
, MSGL_V
, "...descr id: 0x%x, len=%d\n", ptr
[j
], descr_len
);
2247 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "INVALID DESCR LEN for tag %02x: %d vs %d max, EXIT LOOP\n", ptr
[j
], descr_len
, len
);
2252 if(ptr
[j
] == 0x6a || ptr
[j
] == 0x7a) //A52 Descriptor
2256 es
->type
= AUDIO_A52
;
2257 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "DVB A52 Descriptor\n");
2260 else if(ptr
[j
] == 0x7b) //DVB DTS Descriptor
2264 es
->type
= AUDIO_DTS
;
2265 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "DVB DTS Descriptor\n");
2268 else if(ptr
[j
] == 0x56) // Teletext
2270 if(descr_len
>= 5) {
2271 memcpy(es
->lang
, ptr
+2, 3);
2274 es
->type
= SPU_TELETEXT
;
2276 else if(ptr
[j
] == 0x59) //Subtitling Descriptor
2280 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Subtitling Descriptor\n");
2283 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Descriptor length too short for DVB Subtitle Descriptor: %d, SKIPPING\n", descr_len
);
2287 memcpy(es
->lang
, &ptr
[j
+2], 3);
2291 (subtype
>= 0x10 && subtype
<= 0x13) ||
2292 (subtype
>= 0x20 && subtype
<= 0x23)
2296 //page parameters: compo page 2 bytes, ancillary page 2 bytes
2302 else if(ptr
[j
] == 0x50) //Component Descriptor
2304 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Component Descriptor\n");
2305 memcpy(es
->lang
, &ptr
[j
+5], 3);
2308 else if(ptr
[j
] == 0xa) //Language Descriptor
2310 memcpy(es
->lang
, &ptr
[j
+2], 3);
2312 mp_msg(MSGT_DEMUX
, MSGL_V
, "Language Descriptor: %s\n", es
->lang
);
2314 else if(ptr
[j
] == 0x5) //Registration Descriptor (looks like e fourCC :) )
2316 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Registration Descriptor\n");
2319 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Registration Descriptor length too short: %d, SKIPPING\n", descr_len
);
2324 memcpy(es
->format_descriptor
, &ptr
[j
+2], 4);
2325 es
->format_descriptor
[4] = 0;
2328 if(d
[0] == 'A' && d
[1] == 'C' && d
[2] == '-' && d
[3] == '3')
2330 es
->type
= AUDIO_A52
;
2332 else if(d
[0] == 'D' && d
[1] == 'T' && d
[2] == 'S' && d
[3] == '1')
2334 es
->type
= AUDIO_DTS
;
2336 else if(d
[0] == 'D' && d
[1] == 'T' && d
[2] == 'S' && d
[3] == '2')
2338 es
->type
= AUDIO_DTS
;
2340 else if(d
[0] == 'V' && d
[1] == 'C' && d
[2] == '-' && d
[3] == '1')
2342 es
->type
= VIDEO_VC1
;
2344 else if(d
[0] == 'd' && d
[1] == 'r' && d
[2] == 'a' && d
[3] == 'c')
2346 es
->type
= VIDEO_DIRAC
;
2350 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "FORMAT %s\n", es
->format_descriptor
);
2353 else if(ptr
[j
] == 0x1e)
2355 es
->mp4_es_id
= (ptr
[j
+2] << 8) | ptr
[j
+3];
2356 mp_msg(MSGT_DEMUX
, MSGL_V
, "SL Descriptor: ES_ID: %d(%x), pid: %d\n", es
->mp4_es_id
, es
->mp4_es_id
, es
->pid
);
2359 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Unknown descriptor 0x%x, SKIPPING\n", ptr
[j
]);
2361 len
-= 2 + descr_len
;
2368 static int parse_sl_section(pmt_t
*pmt
, ts_section_t
*section
, int is_start
, unsigned char *buff
, int size
)
2372 skip
= collect_section(section
, is_start
, buff
, size
);
2376 ptr
= &(section
->buffer
[skip
]);
2378 len
= ((ptr
[1] & 0x0f) << 8) | ptr
[2];
2379 mp_msg(MSGT_DEMUX
, MSGL_V
, "TABLEID: %d (av. %d), skip=%d, LEN: %d\n", tid
, section
->buffer_len
, skip
, len
);
2380 if(len
> 4093 || section
->buffer_len
< len
|| tid
!= 5)
2382 mp_msg(MSGT_DEMUX
, MSGL_V
, "SECTION TOO LARGE or wrong section type, EXIT\n");
2389 //8 is the current position, len - 9 is the amount of data available
2390 parse_mp4_descriptors(pmt
, &ptr
[8], len
- 9, NULL
);
2395 static int parse_pmt(ts_priv_t
* priv
, uint16_t progid
, uint16_t pid
, int is_start
, unsigned char *buff
, int size
)
2397 unsigned char *base
, *es_base
;
2399 int32_t idx
, es_count
, section_bytes
;
2403 struct pmt_es_t
*tmp_es
;
2404 ts_section_t
*section
;
2407 idx
= progid_idx_in_pmt(priv
, progid
);
2411 int sz
= (priv
->pmt_cnt
+ 1) * sizeof(pmt_t
);
2412 tmp
= realloc_struct(priv
->pmt
, priv
->pmt_cnt
+ 1, sizeof(pmt_t
));
2415 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "PARSE_PMT: COULDN'T REALLOC %d bytes, NEXT\n", sz
);
2419 idx
= priv
->pmt_cnt
;
2420 memset(&(priv
->pmt
[idx
]), 0, sizeof(pmt_t
));
2422 priv
->pmt
[idx
].progid
= progid
;
2425 pmt
= &(priv
->pmt
[idx
]);
2427 section
= &(pmt
->section
);
2428 skip
= collect_section(section
, is_start
, buff
, size
);
2432 base
= &(section
->buffer
[skip
]);
2434 mp_msg(MSGT_DEMUX
, MSGL_V
, "FILL_PMT(prog=%d), PMT_len: %d, IS_START: %d, TS_PID: %d, SIZE=%d, M=%d, ES_CNT=%d, IDX=%d, PMT_PTR=%p\n",
2435 progid
, pmt
->section
.buffer_len
, is_start
, pid
, size
, m
, pmt
->es_cnt
, idx
, pmt
);
2437 pmt
->table_id
= base
[0];
2438 if(pmt
->table_id
!= 2)
2440 pmt
->ssi
= base
[1] & 0x80;
2441 pmt
->section_length
= (((base
[1] & 0xf) << 8 ) | base
[2]);
2442 pmt
->version_number
= (base
[5] >> 1) & 0x1f;
2443 pmt
->curr_next
= (base
[5] & 1);
2444 pmt
->section_number
= base
[6];
2445 pmt
->last_section_number
= base
[7];
2446 pmt
->PCR_PID
= ((base
[8] & 0x1f) << 8 ) | base
[9];
2447 pmt
->prog_descr_length
= ((base
[10] & 0xf) << 8 ) | base
[11];
2448 if(pmt
->prog_descr_length
> pmt
->section_length
- 9)
2450 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_PMT, INVALID PROG_DESCR LENGTH (%d vs %d)\n", pmt
->prog_descr_length
, pmt
->section_length
- 9);
2454 if(pmt
->prog_descr_length
)
2455 parse_program_descriptors(pmt
, &base
[12], pmt
->prog_descr_length
);
2457 es_base
= &base
[12 + pmt
->prog_descr_length
]; //the beginning of th ES loop
2459 section_bytes
= pmt
->section_length
- 13 - pmt
->prog_descr_length
;
2462 while(section_bytes
>= 5)
2464 int es_pid
, es_type
;
2466 es_type
= es_base
[0];
2467 es_pid
= ((es_base
[1] & 0x1f) << 8) | es_base
[2];
2469 idx
= es_pid_in_pmt(pmt
, es_pid
);
2472 int sz
= sizeof(struct pmt_es_t
) * (pmt
->es_cnt
+ 1);
2473 tmp_es
= realloc_struct(pmt
->es
, pmt
->es_cnt
+ 1, sizeof(struct pmt_es_t
));
2476 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "PARSE_PMT, COULDN'T ALLOCATE %d bytes for PMT_ES\n", sz
);
2481 memset(&(pmt
->es
[idx
]), 0, sizeof(struct pmt_es_t
));
2485 pmt
->es
[idx
].descr_length
= ((es_base
[3] & 0xf) << 8) | es_base
[4];
2488 if(pmt
->es
[idx
].descr_length
> section_bytes
- 5)
2490 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_PMT, ES_DESCR_LENGTH TOO LARGE %d > %d, EXIT\n",
2491 pmt
->es
[idx
].descr_length
, section_bytes
- 5);
2496 pmt
->es
[idx
].pid
= es_pid
;
2498 pmt
->es
[idx
].type
= UNKNOWN
;
2500 pmt
->es
[idx
].type
= es_type
;
2502 parse_descriptors(&pmt
->es
[idx
], &es_base
[5]);
2507 pmt
->es
[idx
].type
= VIDEO_MPEG1
;
2510 pmt
->es
[idx
].type
= VIDEO_MPEG2
;
2514 pmt
->es
[idx
].type
= AUDIO_MP2
;
2517 if(pmt
->es
[idx
].type
== 0x6) //this could have been ovrwritten by parse_descriptors
2518 pmt
->es
[idx
].type
= UNKNOWN
;
2521 pmt
->es
[idx
].type
= VIDEO_MPEG4
;
2524 pmt
->es
[idx
].type
= AUDIO_AAC
;
2527 pmt
->es
[idx
].type
= AUDIO_AAC_LATM
;
2530 pmt
->es
[idx
].type
= VIDEO_H264
;
2533 pmt
->es
[idx
].type
= SL_PES_STREAM
;
2536 pmt
->es
[idx
].type
= SL_SECTION
;
2539 pmt
->es
[idx
].type
= AUDIO_A52
;
2545 pmt
->es
[idx
].type
= AUDIO_DTS
;
2548 pmt
->es
[idx
].type
= SPU_PGS
;
2551 pmt
->es
[idx
].type
= VIDEO_DIRAC
;
2554 pmt
->es
[idx
].type
= VIDEO_VC1
;
2557 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "UNKNOWN ES TYPE=0x%x\n", es_type
);
2558 pmt
->es
[idx
].type
= UNKNOWN
;
2561 tss
= priv
->ts
.pids
[es_pid
]; //an ES stream
2564 tss
= new_pid(priv
, es_pid
);
2566 tss
->type
= pmt
->es
[idx
].type
;
2569 section_bytes
-= 5 + pmt
->es
[idx
].descr_length
;
2570 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_PMT(%d INDEX %d), STREAM: %d, FOUND pid=0x%x (%d), type=0x%x, ES_DESCR_LENGTH: %d, bytes left: %d\n",
2571 progid
, idx
, es_count
, pmt
->es
[idx
].pid
, pmt
->es
[idx
].pid
, pmt
->es
[idx
].type
, pmt
->es
[idx
].descr_length
, section_bytes
);
2574 es_base
+= 5 + pmt
->es
[idx
].descr_length
;
2579 mp_msg(MSGT_DEMUX
, MSGL_V
, "----------------------------\n");
2583 static pmt_t
* pmt_of_pid(ts_priv_t
*priv
, int pid
, mp4_decoder_config_t
**mp4_dec
)
2589 for(i
= 0; i
< priv
->pmt_cnt
; i
++)
2591 if(priv
->pmt
[i
].es
&& priv
->pmt
[i
].es_cnt
)
2593 for(j
= 0; j
< priv
->pmt
[i
].es_cnt
; j
++)
2595 if(priv
->pmt
[i
].es
[j
].pid
== pid
)
2598 if(priv
->pmt
[i
].es
[j
].mp4_es_id
)
2600 for(k
= 0; k
< priv
->pmt
[i
].mp4es_cnt
; k
++)
2602 if(priv
->pmt
[i
].mp4es
[k
].id
== priv
->pmt
[i
].es
[j
].mp4_es_id
)
2604 *mp4_dec
= &(priv
->pmt
[i
].mp4es
[k
].decoder
);
2610 return &(priv
->pmt
[i
]);
2621 static inline int32_t pid_type_from_pmt(ts_priv_t
*priv
, int pid
)
2623 int32_t pmt_idx
, pid_idx
, i
, j
;
2625 pmt_idx
= progid_idx_in_pmt(priv
, priv
->prog
);
2629 pid_idx
= es_pid_in_pmt(&(priv
->pmt
[pmt_idx
]), pid
);
2631 return priv
->pmt
[pmt_idx
].es
[pid_idx
].type
;
2635 for(i
= 0; i
< priv
->pmt_cnt
; i
++)
2637 pmt_t
*pmt
= &(priv
->pmt
[i
]);
2638 for(j
= 0; j
< pmt
->es_cnt
; j
++)
2639 if(pmt
->es
[j
].pid
== pid
)
2640 return pmt
->es
[j
].type
;
2648 static inline uint8_t *pid_lang_from_pmt(ts_priv_t
*priv
, int pid
)
2650 int32_t pmt_idx
, pid_idx
, i
, j
;
2652 pmt_idx
= progid_idx_in_pmt(priv
, priv
->prog
);
2656 pid_idx
= es_pid_in_pmt(&(priv
->pmt
[pmt_idx
]), pid
);
2658 return priv
->pmt
[pmt_idx
].es
[pid_idx
].lang
;
2662 for(i
= 0; i
< priv
->pmt_cnt
; i
++)
2664 pmt_t
*pmt
= &(priv
->pmt
[i
]);
2665 for(j
= 0; j
< pmt
->es_cnt
; j
++)
2666 if(pmt
->es
[j
].pid
== pid
)
2667 return pmt
->es
[j
].lang
;
2675 static int fill_packet(demuxer_t
*demuxer
, demux_stream_t
*ds
, demux_packet_t
**dp
, int *dp_offset
, TS_stream_info
*si
)
2679 if((*dp
!= NULL
) && (*dp_offset
> 0))
2682 resize_demux_packet(*dp
, ret
); //shrinked to the right size
2683 ds_add_packet(ds
, *dp
);
2684 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "ADDED %d bytes to %s fifo, PTS=%.3f\n", ret
, (ds
== demuxer
->audio
? "audio" : (ds
== demuxer
->video
? "video" : "sub")), (*dp
)->pts
);
2687 float diff
= (*dp
)->pts
- si
->last_pts
;
2690 if(abs(diff
) > 1) //1 second, there's a discontinuity
2692 si
->duration
+= si
->last_pts
- si
->first_pts
;
2693 si
->first_pts
= si
->last_pts
= (*dp
)->pts
;
2697 si
->last_pts
= (*dp
)->pts
;
2700 dur
= si
->duration
+ (si
->last_pts
- si
->first_pts
);
2702 if(dur
> 0 && ds
== demuxer
->video
)
2704 ts_priv_t
* priv
= (ts_priv_t
*) demuxer
->priv
;
2705 if(dur
> 1) //otherwise it may be unreliable
2706 priv
->vbitrate
= (uint32_t) ((float) si
->size
/ dur
);
2717 static int fill_extradata(mp4_decoder_config_t
* mp4_dec
, ES_stream_t
*tss
)
2721 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "MP4_dec: %p, pid: %d\n", mp4_dec
, tss
->pid
);
2723 if(mp4_dec
->buf_size
> tss
->extradata_alloc
)
2725 tmp
= realloc(tss
->extradata
, mp4_dec
->buf_size
);
2728 tss
->extradata
= tmp
;
2729 tss
->extradata_alloc
= mp4_dec
->buf_size
;
2731 memcpy(tss
->extradata
, mp4_dec
->buf
, mp4_dec
->buf_size
);
2732 tss
->extradata_len
= mp4_dec
->buf_size
;
2733 mp_msg(MSGT_DEMUX
, MSGL_V
, "EXTRADATA: %p, alloc=%d, len=%d\n", tss
->extradata
, tss
->extradata_alloc
, tss
->extradata_len
);
2735 return tss
->extradata_len
;
2738 // 0 = EOF or no stream found
2739 // else = [-] number of bytes written to the packet
2740 static int ts_parse(demuxer_t
*demuxer
, ES_stream_t
*es
, unsigned char *packet
, int probe
)
2743 int buf_size
, is_start
, pid
, base
;
2744 int len
, cc
, cc_ok
, afc
, retv
= 0, is_video
, is_audio
, is_sub
;
2745 ts_priv_t
* priv
= (ts_priv_t
*) demuxer
->priv
;
2746 stream_t
*stream
= demuxer
->stream
;
2748 demux_stream_t
*ds
= NULL
;
2749 demux_packet_t
**dp
= NULL
;
2750 int *dp_offset
= 0, *buffer_size
= 0;
2751 int32_t progid
, pid_type
, bad
, ts_error
;
2752 int junk
= 0, rap_flag
= 0;
2754 mp4_decoder_config_t
*mp4_dec
;
2763 dp_offset
= buffer_size
= NULL
;
2770 junk
= priv
->ts
.packet_size
- TS_PACKET_SIZE
;
2771 buf_size
= priv
->ts
.packet_size
- junk
;
2773 if(stream_eof(stream
))
2777 ts_dump_streams(priv
);
2778 demuxer
->filepos
= stream_tell(demuxer
->stream
);
2785 if(! ts_sync(stream
))
2787 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "TS_PARSE: COULDN'T SYNC\n");
2791 len
= stream_read(stream
, &packet
[1], 3);
2796 if((packet
[1] >> 7) & 0x01) //transport error
2800 is_start
= packet
[1] & 0x40;
2801 pid
= ((packet
[1] & 0x1f) << 8) | packet
[2];
2803 tss
= priv
->ts
.pids
[pid
]; //an ES stream
2806 tss
= new_pid(priv
, pid
);
2811 cc
= (packet
[3] & 0xf);
2812 cc_ok
= (tss
->last_cc
< 0) || ((((tss
->last_cc
+ 1) & 0x0f) == cc
));
2815 bad
= ts_error
; // || (! cc_ok);
2818 if(priv
->keep_broken
== 0)
2820 stream_skip(stream
, buf_size
-1+junk
);
2824 is_start
= 0; //queued to the packet data
2830 if((!is_start
&& !tss
->is_synced
) || ((pid
> 1) && (pid
< 16)) || (pid
== 8191)) //invalid pid
2832 stream_skip(stream
, buf_size
-1+junk
);
2837 afc
= (packet
[3] >> 4) & 3;
2838 if(! (afc
% 2)) //no payload in this TS packet
2840 stream_skip(stream
, buf_size
-1+junk
);
2847 c
= stream_read_char(stream
);
2849 if(c
< 0 || c
> 183) //broken from the stream layer or invalid
2851 stream_skip(stream
, buf_size
-1+junk
);
2858 uint8_t pcrbuf
[188];
2859 int flags
= stream_read_char(stream
);
2861 rap_flag
= (flags
& 0x40) >> 6;
2862 has_pcr
= flags
& 0x10;
2866 stream_read(stream
, pcrbuf
, c
);
2870 int pcr_pid
= prog_pcr_pid(priv
, priv
->prog
);
2873 uint64_t pcr
, pcr_ext
;
2875 pcr
= (int64_t)(pcrbuf
[0]) << 25;
2876 pcr
|= pcrbuf
[1] << 17 ;
2877 pcr
|= (pcrbuf
[2]) << 9;
2878 pcr
|= pcrbuf
[3] << 1 ;
2879 pcr
|= (pcrbuf
[4] & 0x80) >> 7;
2881 pcr_ext
= (pcrbuf
[4] & 0x01) << 8;
2882 pcr_ext
|= pcrbuf
[5];
2884 pcr
= pcr
* 300 + pcr_ext
;
2886 demuxer
->reference_clock
= (double)pcr
/(double)27000000.0;
2896 //find the program that the pid belongs to; if (it's the right one or -1) && pid_type==SL_SECTION
2897 //call parse_sl_section()
2898 pmt
= pmt_of_pid(priv
, pid
, &mp4_dec
);
2901 fill_extradata(mp4_dec
, tss
);
2902 if(IS_VIDEO(mp4_dec
->object_type
) || IS_AUDIO(mp4_dec
->object_type
))
2904 tss
->type
= SL_PES_STREAM
;
2905 tss
->subtype
= mp4_dec
->object_type
;
2912 base
= priv
->ts
.packet_size
- buf_size
;
2914 priv
->last_pid
= pid
;
2916 is_video
= IS_VIDEO(tss
->type
) || (tss
->type
==SL_PES_STREAM
&& IS_VIDEO(tss
->subtype
));
2917 is_audio
= IS_AUDIO(tss
->type
) || (tss
->type
==SL_PES_STREAM
&& IS_AUDIO(tss
->subtype
)) || (tss
->type
== PES_PRIVATE1
);
2918 is_sub
= IS_SUB(tss
->type
);
2919 pid_type
= pid_type_from_pmt(priv
, pid
);
2921 // PES CONTENT STARTS HERE
2924 if((is_video
|| is_audio
|| is_sub
) && is_start
)
2925 ts_add_stream(demuxer
, tss
);
2927 if(is_video
&& (demuxer
->video
->id
== priv
->ts
.streams
[pid
].id
))
2929 ds
= demuxer
->video
;
2931 dp
= &priv
->fifo
[1].pack
;
2932 dp_offset
= &priv
->fifo
[1].offset
;
2933 buffer_size
= &priv
->fifo
[1].buffer_size
;
2936 else if(is_audio
&& (demuxer
->audio
->id
== priv
->ts
.streams
[pid
].id
))
2938 ds
= demuxer
->audio
;
2940 dp
= &priv
->fifo
[0].pack
;
2941 dp_offset
= &priv
->fifo
[0].offset
;
2942 buffer_size
= &priv
->fifo
[0].buffer_size
;
2947 sh_sub_t
*sh_sub
= demuxer
->sub
->sh
;
2949 if(sh_sub
&& sh_sub
->sid
== tss
->pid
)
2953 dp
= &priv
->fifo
[2].pack
;
2954 dp_offset
= &priv
->fifo
[2].offset
;
2955 buffer_size
= &priv
->fifo
[2].buffer_size
;
2959 stream_skip(stream
, buf_size
+junk
);
2964 //IS IT TIME TO QUEUE DATA to the dp_packet?
2965 if(is_start
&& (dp
!= NULL
))
2967 retv
= fill_packet(demuxer
, ds
, dp
, dp_offset
, si
);
2971 if(dp
&& *dp
== NULL
)
2973 if(*buffer_size
> MAX_PACK_BYTES
)
2974 *buffer_size
= MAX_PACK_BYTES
;
2975 *dp
= new_demux_packet(*buffer_size
); //es->size
2979 fprintf(stderr
, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", *buffer_size
);
2982 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "CREATED DP(%d)\n", *buffer_size
);
2987 if(probe
|| !dp
) //dp is NULL for tables and sections
2993 if(*dp_offset
+ buf_size
> *buffer_size
)
2995 *buffer_size
= *dp_offset
+ buf_size
+ TS_FEC_PACKET_SIZE
;
2996 resize_demux_packet(*dp
, *buffer_size
);
2998 p
= &((*dp
)->buffer
[*dp_offset
]);
3001 len
= stream_read(stream
, p
, buf_size
);
3004 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "\r\nts_parse() couldn't read enough data: %d < %d\r\n", len
, buf_size
);
3007 stream_skip(stream
, junk
);
3011 parse_pat(priv
, is_start
, p
, buf_size
);
3014 else if((tss
->type
== SL_SECTION
) && pmt
)
3016 int k
, mp4_es_id
= -1;
3017 ts_section_t
*section
;
3018 for(k
= 0; k
< pmt
->mp4es_cnt
; k
++)
3020 if(pmt
->mp4es
[k
].decoder
.object_type
== MP4_OD
&& pmt
->mp4es
[k
].decoder
.stream_type
== MP4_OD
)
3021 mp4_es_id
= pmt
->mp4es
[k
].id
;
3023 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "MP4ESID: %d\n", mp4_es_id
);
3024 for(k
= 0; k
< pmt
->es_cnt
; k
++)
3026 if(pmt
->es
[k
].mp4_es_id
== mp4_es_id
)
3028 section
= &(tss
->section
);
3029 parse_sl_section(pmt
, section
, is_start
, &packet
[base
], buf_size
);
3036 progid
= prog_id_in_pat(priv
, pid
);
3039 if(pid
!= demuxer
->video
->id
&& pid
!= demuxer
->audio
->id
&& pid
!= demuxer
->sub
->id
)
3041 parse_pmt(priv
, progid
, pid
, is_start
, &packet
[base
], buf_size
);
3045 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "Argh! Data pid %d used in the PMT, Skipping PMT parsing!\n", pid
);
3054 uint8_t *lang
= NULL
;
3056 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "IS_START\n");
3058 len
= pes_parse2(p
, buf_size
, es
, pid_type
, pmt
, pid
);
3065 tss
->is_synced
|= es
->is_synced
|| rap_flag
;
3066 tss
->payload_size
= es
->payload_size
;
3068 if((is_sub
|| is_audio
) && (lang
= pid_lang_from_pmt(priv
, es
->pid
)))
3070 memcpy(es
->lang
, lang
, 3);
3078 if(es
->type
== UNKNOWN
)
3081 tss
->type
= es
->type
;
3082 tss
->subtype
= es
->subtype
;
3089 es
->pts
= tss
->pts
= tss
->last_pts
;
3091 tss
->pts
= tss
->last_pts
= es
->pts
;
3093 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "ts_parse, NEW pid=%d, PSIZE: %u, type=%X, start=%p, len=%d\n",
3094 es
->pid
, es
->payload_size
, es
->type
, es
->start
, es
->size
);
3096 demuxer
->filepos
= stream_tell(demuxer
->stream
) - es
->size
;
3098 if(es
->size
< 0 || es
->size
> buf_size
) {
3099 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "Broken ES packet size\n");
3102 memmove(p
, es
->start
, es
->size
);
3103 *dp_offset
+= es
->size
;
3105 (*dp
)->pos
= stream_tell(demuxer
->stream
);
3106 (*dp
)->pts
= es
->pts
;
3119 es
->type
= tss
->type
;
3120 es
->subtype
= tss
->subtype
;
3121 es
->pts
= tss
->pts
= tss
->last_pts
;
3122 es
->start
= &packet
[base
];
3125 if(tss
->payload_size
> 0)
3127 sz
= FFMIN(tss
->payload_size
, buf_size
);
3128 tss
->payload_size
-= sz
;
3135 sz
= es
->size
= buf_size
;
3148 if(*dp_offset
>= MAX_PACK_BYTES
)
3150 (*dp
)->pts
= tss
->last_pts
;
3151 retv
= fill_packet(demuxer
, ds
, dp
, dp_offset
, si
);
3159 memcpy(es
->start
, p
, sz
);
3173 static void reset_fifos(demuxer_t
*demuxer
, int a
, int v
, int s
)
3175 ts_priv_t
* priv
= demuxer
->priv
;
3178 if(priv
->fifo
[0].pack
!= NULL
)
3180 free_demux_packet(priv
->fifo
[0].pack
);
3181 priv
->fifo
[0].pack
= NULL
;
3183 priv
->fifo
[0].offset
= 0;
3188 if(priv
->fifo
[1].pack
!= NULL
)
3190 free_demux_packet(priv
->fifo
[1].pack
);
3191 priv
->fifo
[1].pack
= NULL
;
3193 priv
->fifo
[1].offset
= 0;
3198 if(priv
->fifo
[2].pack
!= NULL
)
3200 free_demux_packet(priv
->fifo
[2].pack
);
3201 priv
->fifo
[2].pack
= NULL
;
3203 priv
->fifo
[2].offset
= 0;
3205 demuxer
->reference_clock
= MP_NOPTS_VALUE
;
3209 static void demux_seek_ts(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
, int flags
)
3211 demux_stream_t
*d_audio
=demuxer
->audio
;
3212 demux_stream_t
*d_video
=demuxer
->video
;
3213 sh_audio_t
*sh_audio
=d_audio
->sh
;
3214 sh_video_t
*sh_video
=d_video
->sh
;
3215 ts_priv_t
* priv
= (ts_priv_t
*) demuxer
->priv
;
3219 //================= seek in MPEG-TS ==========================
3221 ts_dump_streams(demuxer
->priv
);
3222 reset_fifos(demuxer
, sh_audio
!= NULL
, sh_video
!= NULL
, demuxer
->sub
->id
> 0);
3224 demux_flush(demuxer
);
3228 video_stats
= (sh_video
!= NULL
);
3231 mp_msg(MSGT_DEMUX
, MSGL_V
, "IBPS: %d, vb: %d\r\n", sh_video
->i_bps
, priv
->vbitrate
);
3233 video_stats
= priv
->vbitrate
;
3235 video_stats
= sh_video
->i_bps
;
3238 newpos
= (flags
& SEEK_ABSOLUTE
) ? demuxer
->movi_start
: demuxer
->filepos
;
3239 if(flags
& SEEK_FACTOR
) // float seek 0..1
3240 newpos
+=(demuxer
->movi_end
-demuxer
->movi_start
)*rel_seek_secs
;
3244 if(! video_stats
) // unspecified or VBR
3245 newpos
+= 2324*75*rel_seek_secs
; // 174.3 kbyte/sec
3247 newpos
+= video_stats
*rel_seek_secs
;
3251 if(newpos
< demuxer
->movi_start
)
3252 newpos
= demuxer
->movi_start
; //begininng of stream
3254 stream_seek(demuxer
->stream
, newpos
);
3255 for(i
= 0; i
< 8192; i
++)
3256 if(priv
->ts
.pids
[i
] != NULL
)
3257 priv
->ts
.pids
[i
]->is_synced
= 0;
3259 videobuf_code_len
= 0;
3261 if(sh_video
!= NULL
)
3262 ds_fill_buffer(d_video
);
3264 if(sh_audio
!= NULL
)
3266 ds_fill_buffer(d_audio
);
3269 while(sh_video
!= NULL
)
3271 if(sh_audio
&& !d_audio
->eof
&& d_video
->pts
&& d_audio
->pts
)
3273 double a_pts
=d_audio
->pts
;
3274 a_pts
+=(ds_tell_pts(d_audio
)-sh_audio
->a_in_buffer_len
)/(double)sh_audio
->i_bps
;
3275 if(d_video
->pts
> a_pts
)
3277 skip_audio_frame(sh_audio
); // sync audio
3283 i
= sync_video_packet(d_video
);
3284 if((sh_video
->format
== VIDEO_MPEG1
) || (sh_video
->format
== VIDEO_MPEG2
))
3286 if(i
==0x1B3 || i
==0x1B8) break; // found it!
3288 else if((sh_video
->format
== VIDEO_MPEG4
) && (i
==0x1B6))
3290 else if(sh_video
->format
== VIDEO_VC1
&& (i
==0x10E || i
==0x10F))
3294 if((i
& ~0x60) == 0x105 || (i
& ~0x60) == 0x107) break;
3297 if(!i
|| !skip_video_packet(d_video
)) break; // EOF?
3302 static int demux_ts_fill_buffer(demuxer_t
* demuxer
, demux_stream_t
*ds
)
3305 ts_priv_t
*priv
= (ts_priv_t
*)demuxer
->priv
;
3307 return -ts_parse(demuxer
, &es
, priv
->packet
, 0);
3311 static int ts_check_file_dmx(demuxer_t
*demuxer
)
3313 return ts_check_file(demuxer
) ? DEMUXER_TYPE_MPEG_TS
: 0;
3316 static int is_usable_program(ts_priv_t
*priv
, pmt_t
*pmt
)
3320 for(j
= 0; j
< pmt
->es_cnt
; j
++)
3322 if(priv
->ts
.pids
[pmt
->es
[j
].pid
] == NULL
|| priv
->ts
.streams
[pmt
->es
[j
].pid
].sh
== NULL
)
3325 priv
->ts
.streams
[pmt
->es
[j
].pid
].type
== TYPE_VIDEO
||
3326 priv
->ts
.streams
[pmt
->es
[j
].pid
].type
== TYPE_AUDIO
3334 static int demux_ts_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
3336 ts_priv_t
* priv
= (ts_priv_t
*)demuxer
->priv
;
3340 case DEMUXER_CTRL_SWITCH_AUDIO
:
3341 case DEMUXER_CTRL_SWITCH_VIDEO
:
3345 int reftype
, areset
= 0, vreset
= 0;
3348 if(cmd
== DEMUXER_CTRL_SWITCH_VIDEO
)
3350 reftype
= TYPE_VIDEO
;
3351 ds
= demuxer
->video
;
3356 reftype
= TYPE_AUDIO
;
3357 ds
= demuxer
->audio
;
3363 reset_fifos(demuxer
, areset
, vreset
, 0);
3367 *((int*)arg
) = ds
->id
;
3368 return DEMUXER_CTRL_OK
;
3373 for(i
= 0; i
< 8192; i
++)
3375 if(priv
->ts
.streams
[i
].id
== ds
->id
&& priv
->ts
.streams
[i
].type
== reftype
)
3382 if(priv
->ts
.streams
[i
].type
== reftype
)
3384 if(priv
->ts
.streams
[i
].id
== ds
->id
) //we made a complete loop
3386 sh
= priv
->ts
.streams
[i
].sh
;
3390 else //audio track <n>
3392 if (n
>= 8192 || priv
->ts
.streams
[n
].type
!= reftype
) return DEMUXER_CTRL_NOTIMPL
;
3394 sh
= priv
->ts
.streams
[i
].sh
;
3399 if(ds
->id
!= priv
->ts
.streams
[i
].id
)
3400 reset_fifos(demuxer
, areset
, vreset
, 0);
3401 ds
->id
= priv
->ts
.streams
[i
].id
;
3404 mp_msg(MSGT_DEMUX
, MSGL_V
, "\r\ndemux_ts, switched to audio pid %d, id: %d, sh: %p\r\n", i
, ds
->id
, sh
);
3407 *((int*)arg
) = ds
->id
;
3408 return DEMUXER_CTRL_OK
;
3411 case DEMUXER_CTRL_IDENTIFY_PROGRAM
: //returns in prog->{aid,vid} the new ids that comprise a program
3414 int vid_done
=0, aid_done
=0;
3416 demux_program_t
*prog
= arg
;
3418 if(priv
->pmt_cnt
< 2)
3419 return DEMUXER_CTRL_NOTIMPL
;
3421 if(prog
->progid
== -1)
3423 int cur_pmt_idx
= 0;
3425 for(i
= 0; i
< priv
->pmt_cnt
; i
++)
3426 if(priv
->pmt
[i
].progid
== priv
->prog
)
3432 i
= (cur_pmt_idx
+ 1) % priv
->pmt_cnt
;
3433 while(i
!= cur_pmt_idx
)
3435 pmt
= &priv
->pmt
[i
];
3436 cnt
= is_usable_program(priv
, pmt
);
3439 i
= (i
+ 1) % priv
->pmt_cnt
;
3444 for(i
= 0; i
< priv
->pmt_cnt
; i
++)
3445 if(priv
->pmt
[i
].progid
== prog
->progid
)
3447 pmt
= &priv
->pmt
[i
]; //required program
3448 cnt
= is_usable_program(priv
, pmt
);
3453 return DEMUXER_CTRL_NOTIMPL
;
3456 prog
->aid
= prog
->vid
= -2; //no audio and no video by default
3457 for(j
= 0; j
< pmt
->es_cnt
; j
++)
3459 if(priv
->ts
.pids
[pmt
->es
[j
].pid
] == NULL
|| priv
->ts
.streams
[pmt
->es
[j
].pid
].sh
== NULL
)
3462 if(!vid_done
&& priv
->ts
.streams
[pmt
->es
[j
].pid
].type
== TYPE_VIDEO
)
3465 prog
->vid
= pmt
->es
[j
].pid
;
3467 else if(!aid_done
&& priv
->ts
.streams
[pmt
->es
[j
].pid
].type
== TYPE_AUDIO
)
3470 prog
->aid
= pmt
->es
[j
].pid
;
3474 priv
->prog
= prog
->progid
= pmt
->progid
;
3475 return DEMUXER_CTRL_OK
;
3479 return DEMUXER_CTRL_NOTIMPL
;
3484 const demuxer_desc_t demuxer_desc_mpeg_ts
= {
3490 DEMUXER_TYPE_MPEG_TS
,
3491 0, // unsafe autodetect
3493 demux_ts_fill_buffer
,