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 free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This file is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 TS_MAX_PROBE_SIZE 2000000 /* dont forget to change this in cfg-common.h too */
50 #define NUM_CONSECUTIVE_TS_PACKETS 32
51 #define NUM_CONSECUTIVE_AUDIO_PACKETS 348
52 #define MAX_A52_FRAME_SIZE 3840
55 #define SIZE_MAX ((size_t)-1)
63 off_t ts_probe
= TS_MAX_PROBE_SIZE
;
64 extern char *dvdsub_lang
, *audio_lang
; //for -alang
69 VIDEO_MPEG1
= 0x10000001,
70 VIDEO_MPEG2
= 0x10000002,
71 VIDEO_MPEG4
= 0x10000004,
72 VIDEO_H264
= 0x10000005,
73 VIDEO_AVC
= mmioFOURCC('a', 'v', 'c', '1'),
77 AUDIO_LPCM_BE
= 0x10001,
78 AUDIO_AAC
= mmioFOURCC('M', 'P', '4', 'A'),
81 PES_PRIVATE1
= 0xBD00000,
82 SL_PES_STREAM
= 0xD000000,
83 SL_SECTION
= 0xD100000,
95 uint16_t payload_size
;
96 es_stream_type_t type
, subtype
;
100 int last_cc
; // last cc code (-1 if first packet)
102 ts_section_t section
;
104 int extradata_alloc
, extradata_len
;
106 uint8_t au_start
, au_end
, last_au_end
;
116 typedef struct MpegTSContext
{
117 int packet_size
; // raw packet size, including FEC if present e.g. 188 bytes
118 ES_stream_t
*pids
[NB_PID_MAX
];
119 sh_av_t streams
[NB_PID_MAX
];
125 demux_packet_t
*pack
;
126 int offset
, buffer_size
;
129 #define MAX_EXTRADATA_SIZE 64*1024
131 int32_t object_type
; //aka codec used
132 int32_t stream_type
; //video, audio etc.
133 uint8_t buf
[MAX_EXTRADATA_SIZE
];
136 } mp4_decoder_config_t
;
143 uint8_t random_accesspoint
;
144 uint8_t random_accesspoint_only
;
150 uint32_t ts_resolution
, ocr_resolution
;
151 uint8_t ts_len
, ocr_len
, au_len
, instant_bitrate_len
, degr_len
, au_seqnum_len
, packet_seqnum_len
;
153 uint16_t au_duration
, cts_duration
;
154 uint64_t ocr
, dts
, cts
;
160 mp4_decoder_config_t decoder
;
175 uint16_t section_length
;
177 uint8_t version_number
;
179 uint8_t section_number
;
180 uint8_t last_section_number
;
186 ts_section_t section
;
194 uint16_t section_length
;
195 uint8_t version_number
;
197 uint8_t section_number
;
198 uint8_t last_section_number
;
200 uint16_t prog_descr_length
;
201 ts_section_t section
;
205 uint32_t type
; //it's 8 bit long, but cast to the right type as FOURCC
206 uint16_t descr_length
;
207 uint8_t format_descriptor
[5];
212 mp4_es_descr_t
*mp4es
;
213 int od_cnt
, mp4es_cnt
;
226 av_fifo_t fifo
[3]; //0 for audio, 1 for video, 2 for subs
234 char packet
[TS_FEC_PACKET_SIZE
];
235 TS_stream_info vstr
, astr
;
240 es_stream_type_t type
;
241 ts_section_t section
;
245 #define IS_AUDIO(x) (((x) == AUDIO_MP2) || ((x) == AUDIO_A52) || ((x) == AUDIO_LPCM_BE) || ((x) == AUDIO_AAC) || ((x) == AUDIO_DTS))
246 #define IS_VIDEO(x) (((x) == VIDEO_MPEG1) || ((x) == VIDEO_MPEG2) || ((x) == VIDEO_MPEG4) || ((x) == VIDEO_H264) || ((x) == VIDEO_AVC))
248 static int ts_parse(demuxer_t
*demuxer
, ES_stream_t
*es
, unsigned char *packet
, int probe
);
250 static uint8_t get_packet_size(const unsigned char *buf
, int size
)
254 if (size
< (TS_FEC_PACKET_SIZE
* NUM_CONSECUTIVE_TS_PACKETS
))
257 for(i
=0; i
<NUM_CONSECUTIVE_TS_PACKETS
; i
++)
259 if (buf
[i
* TS_PACKET_SIZE
] != 0x47)
261 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "GET_PACKET_SIZE, pos %d, char: %2x\n", i
, buf
[i
* TS_PACKET_SIZE
]);
265 return TS_PACKET_SIZE
;
268 for(i
=0; i
<NUM_CONSECUTIVE_TS_PACKETS
; i
++)
270 if (buf
[i
* TS_FEC_PACKET_SIZE
] != 0x47){
271 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "GET_PACKET_SIZE, pos %d, char: %2x\n", i
, buf
[i
* TS_PACKET_SIZE
]);
275 return TS_FEC_PACKET_SIZE
;
278 for(i
=0; i
<NUM_CONSECUTIVE_TS_PACKETS
; i
++)
280 if (buf
[i
* TS_PH_PACKET_SIZE
] != 0x47)
283 return TS_PH_PACKET_SIZE
;
288 static int ts_check_file(demuxer_t
* demuxer
)
290 const int buf_size
= (TS_FEC_PACKET_SIZE
* NUM_CONSECUTIVE_TS_PACKETS
);
291 unsigned char buf
[TS_FEC_PACKET_SIZE
* NUM_CONSECUTIVE_TS_PACKETS
], done
= 0, *ptr
;
292 uint32_t _read
, i
, count
= 0, is_ts
;
293 int cc
[NB_PID_MAX
], last_cc
[NB_PID_MAX
], pid
, cc_ok
, c
, good
, bad
;
298 mp_msg(MSGT_DEMUX
, MSGL_V
, "Checking for MPEG-TS...\n");
300 init_pos
= stream_tell(demuxer
->stream
);
307 while(((c
=stream_read_char(demuxer
->stream
)) != 0x47)
309 && (i
< MAX_CHECK_SIZE
)
310 && ! demuxer
->stream
->eof
316 mp_msg(MSGT_DEMUX
, MSGL_V
, "THIS DOESN'T LOOK LIKE AN MPEG-TS FILE!\n");
322 pos
= stream_tell(demuxer
->stream
) - 1;
324 _read
= stream_read(demuxer
->stream
, &buf
[1], buf_size
-1);
326 if(_read
< buf_size
-1)
328 mp_msg(MSGT_DEMUX
, MSGL_V
, "COULDN'T READ ENOUGH DATA, EXITING TS_CHECK\n");
329 stream_reset(demuxer
->stream
);
333 size
= get_packet_size(buf
, buf_size
);
340 if(pos
- init_pos
>= MAX_CHECK_SIZE
)
347 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
);
348 stream_seek(demuxer
->stream
, pos
);
353 //LET'S CHECK continuity counters
355 for(count
= 0; count
< NB_PID_MAX
; count
++)
357 cc
[count
] = last_cc
[count
] = -1;
360 for(count
= 0; count
< NUM_CONSECUTIVE_TS_PACKETS
; count
++)
362 ptr
= &(buf
[size
* count
]);
363 pid
= ((ptr
[1] & 0x1f) << 8) | ptr
[2];
364 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "BUF: %02x %02x %02x %02x, PID %d, SIZE: %d \n",
365 ptr
[0], ptr
[1], ptr
[2], ptr
[3], pid
, size
);
367 if((pid
== 8191) || (pid
< 16))
370 cc
[pid
] = (ptr
[3] & 0xf);
371 cc_ok
= (last_cc
[pid
] < 0) || ((((last_cc
[pid
] + 1) & 0x0f) == cc
[pid
]));
372 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "PID %d, COMPARE CC %d AND LAST_CC %d\n", pid
, cc
[pid
], last_cc
[pid
]);
379 last_cc
[pid
] = cc
[pid
];
382 mp_msg(MSGT_DEMUX
, MSGL_V
, "GOOD CC: %d, BAD CC: %d\n", good
, bad
);
391 static inline int32_t progid_idx_in_pmt(ts_priv_t
*priv
, uint16_t progid
)
395 if(priv
->pmt
== NULL
)
398 for(x
= 0; x
< priv
->pmt_cnt
; x
++)
400 if(priv
->pmt
[x
].progid
== progid
)
408 static inline int32_t progid_for_pid(ts_priv_t
*priv
, int pid
, int32_t req
) //finds the first program listing a pid
414 if(priv
->pmt
== NULL
)
418 for(i
=0; i
< priv
->pmt_cnt
; i
++)
420 pmt
= &(priv
->pmt
[i
]);
425 for(j
= 0; j
< pmt
->es_cnt
; j
++)
427 if(pmt
->es
[j
].pid
== pid
)
429 if((req
== 0) || (req
== pmt
->progid
))
439 static inline int pid_match_lang(ts_priv_t
*priv
, uint16_t pid
, char *lang
)
444 if(priv
->pmt
== NULL
)
447 for(i
=0; i
< priv
->pmt_cnt
; i
++)
449 pmt
= &(priv
->pmt
[i
]);
454 for(j
= 0; j
< pmt
->es_cnt
; j
++)
456 if(pmt
->es
[j
].pid
!= pid
)
459 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
);
460 if(strncmp(pmt
->es
[j
].lang
, lang
, 3) == 0)
472 int32_t atype
, vtype
, stype
; //types
473 int32_t apid
, vpid
, spid
; //stream ids
474 char slang
[4], alang
[4]; //languages
479 //stripped down version of a52_syncinfo() from liba52
480 //copyright belongs to Michel Lespinasse <walken@zoy.org> and Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
481 int mp_a52_framesize(uint8_t * buf
, int *srate
)
483 int rate
[] = { 32, 40, 48, 56, 64, 80, 96, 112,
484 128, 160, 192, 224, 256, 320, 384, 448,
487 uint8_t halfrate
[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
488 int frmsizecod
, bitrate
, half
;
490 if((buf
[0] != 0x0b) || (buf
[1] != 0x77)) /* syncword */
493 if(buf
[5] >= 0x60) /* bsid >= 12 */
496 half
= halfrate
[buf
[5] >> 3];
498 frmsizecod
= buf
[4] & 63;
502 bitrate
= rate
[frmsizecod
>> 1];
504 switch(buf
[4] & 0xc0)
507 *srate
= 48000 >> half
;
509 case 0x40: /* 44.1 KHz */
510 *srate
= 44100 >> half
;
511 return 2 * (320 * bitrate
/ 147 + (frmsizecod
& 1));
512 case 0x80: /* 32 KHz */
513 *srate
= 32000 >> half
;
520 //second stage: returns the count of A52 syncwords found
521 static int a52_check(char *buf
, int len
)
523 int cnt
, frame_length
, ok
, srate
;
531 if(buf
[cnt
] == 0x0B && buf
[cnt
+1] == 0x77)
533 frame_length
= mp_a52_framesize(&buf
[cnt
], &srate
);
534 if(frame_length
>=7 && frame_length
<=3840)
546 mp_msg(MSGT_DEMUXER
, MSGL_V
, "A52_CHECK(%d input bytes), found %d frame syncwords of %d bytes length\n", len
, ok
, frame_length
);
551 static off_t
ts_detect_streams(demuxer_t
*demuxer
, tsdemux_init_t
*param
)
553 int video_found
= 0, audio_found
= 0, sub_found
= 0, i
, num_packets
= 0, req_apid
, req_vpid
, req_spid
;
554 int is_audio
, is_video
, is_sub
, has_tables
;
555 int32_t p
, chosen_pid
= 0;
556 off_t pos
=0, ret
= 0, init_pos
;
558 unsigned char tmp
[TS_FEC_PACKET_SIZE
];
559 ts_priv_t
*priv
= (ts_priv_t
*) demuxer
->priv
;
563 } pes_priv1
[8192], *pptr
;
566 priv
->last_pid
= 8192; //invalid pid
568 req_apid
= param
->apid
;
569 req_vpid
= param
->vpid
;
570 req_spid
= param
->spid
;
573 memset(pes_priv1
, 0, sizeof(pes_priv1
));
574 init_pos
= stream_tell(demuxer
->stream
);
575 mp_msg(MSGT_DEMUXER
, MSGL_V
, "PROBING UP TO %"PRIu64
", PROG: %d\n", (uint64_t) param
->probe
, param
->prog
);
576 while((pos
<= init_pos
+ param
->probe
) && (! demuxer
->stream
->eof
))
578 pos
= stream_tell(demuxer
->stream
);
579 if(ts_parse(demuxer
, &es
, tmp
, 1))
581 //Non PES-aligned A52 audio may escape detection if PMT is not present;
582 //in this case we try to find at least 3 A52 syncwords
583 if((es
.type
== PES_PRIVATE1
) && (! audio_found
))
585 pptr
= &pes_priv1
[es
.pid
];
586 if(pptr
->pos
< 64*1024)
588 tmpbuf
= (char*) realloc(pptr
->buf
, pptr
->pos
+ es
.size
);
592 memcpy(&(pptr
->buf
[ pptr
->pos
]), es
.start
, es
.size
);
593 pptr
->pos
+= es
.size
;
594 if(a52_check(pptr
->buf
, pptr
->pos
) > 2)
596 param
->atype
= AUDIO_A52
;
597 param
->apid
= es
.pid
;
604 is_audio
= IS_AUDIO(es
.type
) || ((es
.type
==SL_PES_STREAM
) && IS_AUDIO(es
.subtype
));
605 is_video
= IS_VIDEO(es
.type
) || ((es
.type
==SL_PES_STREAM
) && IS_VIDEO(es
.subtype
));
606 is_sub
= ((es
.type
== SPU_DVD
) || (es
.type
== SPU_DVB
));
609 if((! is_audio
) && (! is_video
) && (! is_sub
))
614 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_VIDEO_ID=%d\n", es
.pid
);
615 chosen_pid
= (req_vpid
== es
.pid
);
616 if((! chosen_pid
) && (req_vpid
> 0))
621 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_AUDIO_ID=%d\n", es
.pid
);
623 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_AID_%d_LANG=%s\n", es
.pid
, es
.lang
);
626 chosen_pid
= (req_apid
== es
.pid
);
630 else if(param
->alang
[0] > 0)
632 if(pid_match_lang(priv
, es
.pid
, param
->alang
) == -1)
636 param
->apid
= req_apid
= es
.pid
;
641 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_SUBTITLE_ID=%d\n", es
.pid
);
643 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_SID_%d_LANG=%s\n", es
.pid
, es
.lang
);
644 chosen_pid
= (req_spid
== es
.pid
);
645 if((! chosen_pid
) && (req_spid
> 0))
649 if(req_apid
< 0 && (param
->alang
[0] == 0) && req_vpid
< 0 && req_spid
< 0)
652 if((ret
== 0) && chosen_pid
)
654 ret
= stream_tell(demuxer
->stream
);
657 p
= progid_for_pid(priv
, es
.pid
, param
->prog
);
661 if((param
->prog
== 0) && (p
!= -1))
667 if((param
->prog
> 0) && (param
->prog
!= p
))
671 if(is_video
&& (req_vpid
== es
.pid
))
673 param
->vtype
= IS_VIDEO(es
.type
) ? es
.type
: es
.subtype
;
674 param
->vpid
= es
.pid
;
682 if(is_audio
&& (req_apid
== es
.pid
))
684 param
->atype
= IS_AUDIO(es
.type
) ? es
.type
: es
.subtype
;
685 param
->apid
= es
.pid
;
696 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "TYPE: %x, PID: %d, PROG FOUND: %d\n", es
.type
, es
.pid
, param
->prog
);
701 if((req_vpid
== -1) || (req_vpid
== es
.pid
))
703 param
->vtype
= IS_VIDEO(es
.type
) ? es
.type
: es
.subtype
;
704 param
->vpid
= es
.pid
;
710 if(((req_vpid
== -2) || (num_packets
>= NUM_CONSECUTIVE_AUDIO_PACKETS
)) && audio_found
)
712 //novideo or we have at least 348 audio packets (64 KB) without video (TS with audio only)
719 if((req_spid
== -1) || (req_spid
== es
.pid
))
721 param
->stype
= es
.type
;
722 param
->spid
= es
.pid
;
729 if((req_apid
== -1) || (req_apid
== es
.pid
))
731 param
->atype
= IS_AUDIO(es
.type
) ? es
.type
: es
.subtype
;
732 param
->apid
= es
.pid
;
737 if(audio_found
&& (param
->apid
== es
.pid
) && (! video_found
))
740 if((req_apid
== -2) && video_found
)
746 if((has_tables
==0) && (video_found
&& audio_found
) && (pos
>= 1000000))
751 for(i
=0; i
<8192; i
++)
753 if(pes_priv1
[i
].buf
!= NULL
)
755 free(pes_priv1
[i
].buf
);
756 pes_priv1
[i
].buf
= NULL
;
757 pes_priv1
[i
].pos
= 0;
763 if(param
->vtype
== VIDEO_MPEG1
)
764 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "VIDEO MPEG1(pid=%d) ", param
->vpid
);
765 else if(param
->vtype
== VIDEO_MPEG2
)
766 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "VIDEO MPEG2(pid=%d) ", param
->vpid
);
767 else if(param
->vtype
== VIDEO_MPEG4
)
768 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "VIDEO MPEG4(pid=%d) ", param
->vpid
);
769 else if(param
->vtype
== VIDEO_H264
)
770 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "VIDEO H264(pid=%d) ", param
->vpid
);
771 else if(param
->vtype
== VIDEO_AVC
)
772 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "VIDEO AVC(NAL-H264, pid=%d) ", param
->vpid
);
776 param
->vtype
= UNKNOWN
;
777 //WE DIDN'T MATCH ANY VIDEO STREAM
778 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "NO VIDEO! ");
781 if(param
->atype
== AUDIO_MP2
)
782 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "AUDIO MPA(pid=%d)", param
->apid
);
783 else if(param
->atype
== AUDIO_A52
)
784 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "AUDIO A52(pid=%d)", param
->apid
);
785 else if(param
->atype
== AUDIO_DTS
)
786 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "AUDIO DTS(pid=%d)", param
->apid
);
787 else if(param
->atype
== AUDIO_LPCM_BE
)
788 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "AUDIO LPCM(pid=%d)", param
->apid
);
789 else if(param
->atype
== AUDIO_AAC
)
790 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "AUDIO AAC(pid=%d)", param
->apid
);
794 param
->atype
= UNKNOWN
;
795 //WE DIDN'T MATCH ANY AUDIO STREAM, SO WE FORCE THE DEMUXER TO IGNORE AUDIO
796 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "NO AUDIO! ");
799 if(param
->stype
== SPU_DVD
|| param
->stype
== SPU_DVB
)
800 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " SUB %s(pid=%d) ", (param
->stype
==SPU_DVD
? "DVD" : "DVB"), param
->spid
);
803 param
->stype
= UNKNOWN
;
804 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " NO SUBS (yet)! ");
807 if(video_found
|| audio_found
)
809 if(demuxer
->stream
->eof
&& (ret
== 0))
811 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " PROGRAM N. %d\n", param
->prog
);
814 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "\n");
817 for(i
=0; i
<8192; i
++)
819 if(priv
->ts
.pids
[i
] != NULL
)
821 priv
->ts
.pids
[i
]->payload_size
= 0;
822 priv
->ts
.pids
[i
]->pts
= priv
->ts
.pids
[i
]->last_pts
= 0;
823 priv
->ts
.pids
[i
]->last_cc
= -1;
824 priv
->ts
.pids
[i
]->is_synced
= 0;
831 static int parse_avc_sps(uint8_t *buf
, int len
, int *w
, int *h
)
835 mp_mpeg_header_t picture
;
841 sps_len
= (buf
[6] << 8) | buf
[7];
842 if(!sps_len
|| (sps_len
> len
- 8))
845 picture
.display_picture_width
= picture
.display_picture_height
= 0;
846 h264_parse_sps(&picture
, ptr
, len
- 8);
847 if(!picture
.display_picture_width
|| !picture
.display_picture_height
)
849 *w
= picture
.display_picture_width
;
850 *h
= picture
.display_picture_height
;
854 static demuxer_t
*demux_open_ts(demuxer_t
* demuxer
)
858 sh_video_t
*sh_video
;
859 sh_audio_t
*sh_audio
;
861 tsdemux_init_t params
;
862 ts_priv_t
* priv
= (ts_priv_t
*) demuxer
->priv
;
864 mp_msg(MSGT_DEMUX
, MSGL_V
, "DEMUX OPEN, AUDIO_ID: %d, VIDEO_ID: %d, SUBTITLE_ID: %d,\n",
865 demuxer
->audio
->id
, demuxer
->video
->id
, demuxer
->sub
->id
);
868 demuxer
->type
= DEMUXER_TYPE_MPEG_TS
;
871 stream_reset(demuxer
->stream
);
873 packet_size
= ts_check_file(demuxer
);
877 priv
= calloc(1, sizeof(ts_priv_t
));
880 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "DEMUX_OPEN_TS, couldn't allocate enough memory for ts->priv, exit\n");
884 for(i
=0; i
< 8192; i
++)
886 priv
->ts
.pids
[i
] = NULL
;
887 priv
->ts
.streams
[i
].id
= -3;
889 priv
->pat
.progs
= NULL
;
890 priv
->pat
.progs_cnt
= 0;
891 priv
->pat
.section
.buffer
= NULL
;
892 priv
->pat
.section
.buffer_len
= 0;
897 priv
->keep_broken
= ts_keep_broken
;
898 priv
->ts
.packet_size
= packet_size
;
901 demuxer
->priv
= priv
;
902 if(demuxer
->stream
->type
!= STREAMTYPE_FILE
)
903 demuxer
->seekable
= 1;
905 demuxer
->seekable
= 1;
908 params
.atype
= params
.vtype
= params
.stype
= UNKNOWN
;
909 params
.apid
= demuxer
->audio
->id
;
910 params
.vpid
= demuxer
->video
->id
;
911 params
.spid
= demuxer
->sub
->id
;
912 params
.prog
= ts_prog
;
913 params
.probe
= ts_probe
;
915 if(dvdsub_lang
!= NULL
)
917 strncpy(params
.slang
, dvdsub_lang
, 3);
921 memset(params
.slang
, 0, 4);
923 if(audio_lang
!= NULL
)
925 strncpy(params
.alang
, audio_lang
, 3);
929 memset(params
.alang
, 0, 4);
931 start_pos
= ts_detect_streams(demuxer
, ¶ms
);
933 demuxer
->video
->id
= params
.vpid
;
934 demuxer
->sub
->id
= params
.spid
;
935 priv
->prog
= params
.prog
;
937 if(params
.vtype
!= UNKNOWN
)
939 ES_stream_t
*es
= priv
->ts
.pids
[params
.vpid
];
940 sh_video
= new_sh_video_vid(demuxer
, 0, es
->pid
);
941 if(params
.vtype
== VIDEO_AVC
&& es
->extradata
&& es
->extradata_len
)
944 sh_video
->bih
= (BITMAPINFOHEADER
*) calloc(1, sizeof(BITMAPINFOHEADER
) + es
->extradata_len
);
945 sh_video
->bih
->biSize
= sizeof(BITMAPINFOHEADER
) + es
->extradata_len
;
946 sh_video
->bih
->biCompression
= params
.vtype
;
947 memcpy(sh_video
->bih
+ 1, es
->extradata
, es
->extradata_len
);
948 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "EXTRADATA(%d BYTES): \n", es
->extradata_len
);
949 for(i
= 0;i
< es
->extradata_len
; i
++)
950 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "%02x ", (int) es
->extradata
[i
]);
951 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
,"\n");
952 if(parse_avc_sps(es
->extradata
, es
->extradata_len
, &w
, &h
))
954 sh_video
->bih
->biWidth
= w
;
955 sh_video
->bih
->biHeight
= h
;
958 sh_video
->ds
= demuxer
->video
;
959 sh_video
->format
= params
.vtype
;
960 demuxer
->video
->sh
= sh_video
;
963 if(params
.atype
!= UNKNOWN
)
965 ES_stream_t
*es
= priv
->ts
.pids
[params
.apid
];
966 sh_audio
= new_sh_audio_aid(demuxer
, 0, es
->pid
);
967 priv
->ts
.streams
[params
.apid
].id
= 0;
968 priv
->ts
.streams
[params
.apid
].sh
= sh_audio
;
969 priv
->ts
.streams
[params
.apid
].type
= TYPE_AUDIO
;
971 demuxer
->audio
->id
= 0;
972 sh_audio
->ds
= demuxer
->audio
;
973 sh_audio
->format
= params
.atype
;
974 demuxer
->audio
->sh
= sh_audio
;
975 if(es
->extradata
&& es
->extradata_len
)
977 sh_audio
->wf
= (WAVEFORMATEX
*) malloc(sizeof (WAVEFORMATEX
) + es
->extradata_len
);
978 sh_audio
->wf
->cbSize
= es
->extradata_len
;
979 memcpy(sh_audio
->wf
+ 1, es
->extradata
, es
->extradata_len
);
984 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
);
987 start_pos
= (start_pos
<= priv
->ts
.packet_size
? 0 : start_pos
- priv
->ts
.packet_size
);
988 demuxer
->movi_start
= start_pos
;
989 stream_reset(demuxer
->stream
);
990 stream_seek(demuxer
->stream
, start_pos
); //IF IT'S FROM A PIPE IT WILL FAIL, BUT WHO CARES?
993 priv
->last_pid
= 8192; //invalid pid
995 for(i
= 0; i
< 3; i
++)
997 priv
->fifo
[i
].pack
= NULL
;
998 priv
->fifo
[i
].offset
= 0;
1000 priv
->fifo
[0].ds
= demuxer
->audio
;
1001 priv
->fifo
[1].ds
= demuxer
->video
;
1002 priv
->fifo
[2].ds
= demuxer
->sub
;
1004 priv
->fifo
[0].buffer_size
= 1536;
1005 priv
->fifo
[1].buffer_size
= 32767;
1006 priv
->fifo
[2].buffer_size
= 32767;
1008 priv
->pat
.section
.buffer_len
= 0;
1009 for(i
= 0; i
< priv
->pmt_cnt
; i
++)
1010 priv
->pmt
[i
].section
.buffer_len
= 0;
1012 demuxer
->filepos
= stream_tell(demuxer
->stream
);
1016 static void demux_close_ts(demuxer_t
* demuxer
)
1019 ts_priv_t
*priv
= (ts_priv_t
*) demuxer
->priv
;
1023 if(priv
->pat
.section
.buffer
)
1024 free(priv
->pat
.section
.buffer
);
1026 free(priv
->pat
.progs
);
1030 for(i
= 0; i
< priv
->pmt_cnt
; i
++)
1032 if(priv
->pmt
[i
].section
.buffer
)
1033 free(priv
->pmt
[i
].section
.buffer
);
1035 free(priv
->pmt
[i
].es
);
1045 extern unsigned char mp_getbits(unsigned char*, unsigned int, unsigned char);
1046 #define getbits mp_getbits
1048 static int mp4_parse_sl_packet(pmt_t
*pmt
, uint8_t *buf
, uint16_t packet_len
, int pid
, ES_stream_t
*pes_es
)
1050 int i
, n
, m
, mp4_es_id
= -1;
1052 uint32_t pl_size
= 0;
1054 mp4_es_descr_t
*es
= NULL
;
1055 mp4_sl_config_t
*sl
= NULL
;
1056 uint8_t au_start
= 0, au_end
= 0, rap_flag
= 0, ocr_flag
= 0, padding
= 0, padding_bits
= 0, idle
= 0;
1058 pes_es
->is_synced
= 0;
1059 mp_msg(MSGT_DEMUXER
,MSGL_V
, "mp4_parse_sl_packet, pid: %d, pmt: %pm, packet_len: %d\n", pid
, pmt
, packet_len
);
1060 if(! pmt
|| !packet_len
)
1063 for(i
= 0; i
< pmt
->es_cnt
; i
++)
1065 if(pmt
->es
[i
].pid
== pid
)
1066 mp4_es_id
= pmt
->es
[i
].mp4_es_id
;
1071 for(i
= 0; i
< pmt
->mp4es_cnt
; i
++)
1073 if(pmt
->mp4es
[i
].id
== mp4_es_id
)
1074 es
= &(pmt
->mp4es
[i
]);
1079 pes_es
->subtype
= es
->decoder
.object_type
;
1085 //now es is the complete es_descriptor of out mp4 ES stream
1086 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "ID: %d, FLAGS: 0x%x, subtype: %x\n", es
->id
, sl
->flags
, pes_es
->subtype
);
1090 pes_es
->sl
.au_start
= au_start
= getbits(buf
, n
++, 1);
1092 pes_es
->sl
.au_start
= (pes_es
->sl
.last_au_end
? 1 : 0);
1094 pes_es
->sl
.au_end
= au_end
= getbits(buf
, n
++, 1);
1096 if(!sl
->au_start
&& !sl
->au_end
)
1098 pes_es
->sl
.au_start
= pes_es
->sl
.au_end
= au_start
= au_end
= 1;
1100 pes_es
->sl
.last_au_end
= pes_es
->sl
.au_end
;
1104 ocr_flag
= getbits(buf
, n
++, 1);
1106 idle
= getbits(buf
, n
++, 1);
1108 padding
= getbits(buf
, n
++, 1);
1111 padding_bits
= getbits(buf
, n
, 3);
1115 if(idle
|| (padding
&& !padding_bits
))
1117 pes_es
->payload_size
= 0;
1121 //(! idle && (!padding || padding_bits != 0)) is true
1122 n
+= sl
->packet_seqnum_len
;
1124 deg_flag
= getbits(buf
, n
++, 1);
1131 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "OCR: %d bits\n", sl
->ocr_len
);
1134 if(packet_len
* 8 <= n
)
1137 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "\nAU_START: %d, AU_END: %d\n", au_start
, au_end
);
1140 int dts_flag
= 0, cts_flag
= 0, ib_flag
= 0;
1142 if(sl
->random_accesspoint
)
1143 rap_flag
= getbits(buf
, n
++, 1);
1145 //check commented because it seems it's rarely used, and we need this flag set in case of au_start
1146 //the decoder will eventually discard the payload if it can't decode it
1147 //if(rap_flag || sl->random_accesspoint_only)
1148 pes_es
->is_synced
= 1;
1150 n
+= sl
->au_seqnum_len
;
1151 if(packet_len
* 8 <= n
+8)
1155 dts_flag
= getbits(buf
, n
++, 1);
1156 cts_flag
= getbits(buf
, n
++, 1);
1158 if(sl
->instant_bitrate_len
)
1159 ib_flag
= getbits(buf
, n
++, 1);
1160 if(packet_len
* 8 <= n
+8)
1162 if(dts_flag
&& (sl
->ts_len
> 0))
1165 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "DTS: %d bits\n", sl
->ts_len
);
1167 if(packet_len
* 8 <= n
+8)
1169 if(cts_flag
&& (sl
->ts_len
> 0))
1173 while(i
< sl
->ts_len
)
1175 m
= min(8, sl
->ts_len
- i
);
1176 v
|= getbits(buf
, n
, m
);
1177 if(sl
->ts_len
- i
> 8)
1181 if(packet_len
* 8 <= n
+8)
1185 pes_es
->pts
= (float) v
/ (float) sl
->ts_resolution
;
1186 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "CTS: %d bits, value: %"PRIu64
"/%d = %.3f\n", sl
->ts_len
, v
, sl
->ts_resolution
, pes_es
->pts
);
1192 while(i
< sl
->au_len
)
1194 m
= min(8, sl
->au_len
- i
);
1195 pl_size
|= getbits(buf
, n
, m
);
1196 if(sl
->au_len
- i
> 8)
1200 if(packet_len
* 8 <= n
+8)
1203 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "AU_LEN: %u (%d bits)\n", pl_size
, sl
->au_len
);
1205 n
+= sl
->instant_bitrate_len
;
1209 if(0 < pl_size
&& pl_size
< pes_es
->payload_size
)
1210 pes_es
->payload_size
= pl_size
;
1212 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",
1213 n
, m
, pes_es
->payload_size
, pl_size
, (int) rap_flag
, (int) sl
->random_accesspoint_only
);
1218 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
)
1221 uint32_t header_len
;
1224 uint32_t pkt_len
, pes_is_aligned
;
1226 //Here we are always at the start of a PES packet
1227 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2(%p, %d): \n", buf
, (uint32_t) packet_len
);
1229 if(packet_len
== 0 || packet_len
> 184)
1231 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2, BUFFER LEN IS TOO SMALL OR TOO BIG: %d EXIT\n", packet_len
);
1236 pkt_len
= packet_len
;
1239 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2: HEADER %02x %02x %02x %02x\n", p
[0], p
[1], p
[2], p
[3]);
1240 if (p
[0] || p
[1] || (p
[2] != 1))
1242 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2: error HEADER %02x %02x %02x (should be 0x000001) \n", p
[0], p
[1], p
[2]);
1249 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2: packet too short: %d, exit\n", packet_len
);
1253 es
->payload_size
= (p
[4] << 8 | p
[5]);
1254 pes_is_aligned
= (p
[6] & 4);
1260 { /* pts available */
1261 pts
= (int64_t)(p
[9] & 0x0E) << 29 ;
1262 pts
|= p
[10] << 22 ;
1263 pts
|= (p
[11] & 0xFE) << 14 ;
1265 pts
|= (p
[13] & 0xFE) >> 1 ;
1267 es
->pts
= pts
/ 90000.0f
;
1276 if (header_len
+ 9 > pkt_len
) //9 are the bytes read up to the header_length field
1278 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "demux_ts: illegal value for PES_header_data_length (0x%02x)\n", header_len
);
1282 p
+= header_len
+ 9;
1283 packet_len
-= header_len
+ 3;
1285 if(es
->payload_size
)
1286 es
->payload_size
-= header_len
+ 3;
1289 if (stream_id
== 0xbd)
1291 mp_msg(MSGT_DEMUX
, MSGL_DBG3
, "pes_parse2: audio buf = %02X %02X %02X %02X %02X %02X %02X %02X, 80: %d\n",
1292 p
[0], p
[1], p
[2], p
[3], p
[4], p
[5], p
[6], p
[7], p
[0] & 0x80);
1296 * we check the descriptor tag first because some stations
1297 * do not include any of the A52 header info in their audio tracks
1298 * these "raw" streams may begin with a byte that looks like a stream type.
1303 (type_from_pmt
== AUDIO_A52
) || /* A52 - raw */
1304 (p
[0] == 0x0B && p
[1] == 0x77) /* A52 - syncword */
1307 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "A52 RAW OR SYNCWORD\n");
1309 es
->size
= packet_len
;
1310 es
->type
= AUDIO_A52
;
1311 es
->payload_size
-= packet_len
;
1316 else if(type_from_pmt
== SPU_DVB
||
1317 ((p
[0] == 0x20) && pes_is_aligned
)) // && p[1] == 0x00))
1320 es
->size
= packet_len
;
1322 es
->payload_size
-= packet_len
;
1326 else if (pes_is_aligned
&& ((p
[0] & 0xE0) == 0x20)) //SPU_DVD
1330 es
->size
= packet_len
-1;
1332 es
->payload_size
-= packet_len
;
1336 else if (pes_is_aligned
&& (p
[0] & 0xF8) == 0x80)
1338 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "A52 WITH HEADER\n");
1340 es
->size
= packet_len
- 4;
1341 es
->type
= AUDIO_A52
;
1342 es
->payload_size
-= packet_len
;
1346 else if (pes_is_aligned
&& ((p
[0]&0xf0) == 0xa0))
1350 for (pcm_offset
=0; ++pcm_offset
< packet_len
-1 ; )
1352 if (p
[pcm_offset
] == 0x01 && p
[pcm_offset
+1] == 0x80)
1359 es
->start
= p
+ pcm_offset
;
1360 es
->size
= packet_len
- pcm_offset
;
1361 es
->type
= AUDIO_LPCM_BE
;
1362 es
->payload_size
-= packet_len
;
1368 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "PES_PRIVATE1\n");
1370 es
->size
= packet_len
;
1371 es
->type
= (type_from_pmt
== UNKNOWN
? PES_PRIVATE1
: type_from_pmt
);
1372 es
->payload_size
-= packet_len
;
1377 else if((stream_id
>= 0xe0) && (stream_id
<= 0xef))
1380 es
->size
= packet_len
;
1381 if(type_from_pmt
!= UNKNOWN
)
1382 es
->type
= type_from_pmt
;
1384 es
->type
= VIDEO_MPEG2
;
1385 if(es
->payload_size
)
1386 es
->payload_size
-= packet_len
;
1388 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2: M2V size %d\n", es
->size
);
1391 else if ((stream_id
== 0xfa))
1395 if(type_from_pmt
!= UNKNOWN
) //MP4 A/V or SL
1398 es
->size
= packet_len
;
1399 es
->type
= type_from_pmt
;
1401 if(type_from_pmt
== SL_PES_STREAM
)
1403 //if(pes_is_aligned)
1405 l
= mp4_parse_sl_packet(pmt
, p
, packet_len
, pid
, es
);
1406 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "L=%d, TYPE=%x\n", l
, type_from_pmt
);
1409 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2: couldn't parse SL header, passing along full PES payload\n");
1418 if(es
->payload_size
)
1419 es
->payload_size
-= packet_len
;
1423 else if ((stream_id
& 0xe0) == 0xc0)
1426 es
->size
= packet_len
;
1428 if(type_from_pmt
!= UNKNOWN
)
1429 es
->type
= type_from_pmt
;
1431 es
->type
= AUDIO_MP2
;
1433 es
->payload_size
-= packet_len
;
1437 else if (type_from_pmt
!= -1) //as a last resort here we trust the PMT, if present
1440 es
->size
= packet_len
;
1441 es
->type
= type_from_pmt
;
1442 es
->payload_size
-= packet_len
;
1448 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2: unknown packet, id: %x\n", stream_id
);
1457 static int ts_sync(stream_t
*stream
)
1461 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "TS_SYNC \n");
1463 while(((c
=stream_read_char(stream
)) != 0x47) && ! stream
->eof
);
1472 static void ts_dump_streams(ts_priv_t
*priv
)
1476 for(i
= 0; i
< 3; i
++)
1478 if((priv
->fifo
[i
].pack
!= NULL
) && (priv
->fifo
[i
].offset
!= 0))
1480 resize_demux_packet(priv
->fifo
[i
].pack
, priv
->fifo
[i
].offset
);
1481 ds_add_packet(priv
->fifo
[i
].ds
, priv
->fifo
[i
].pack
);
1482 priv
->fifo
[i
].offset
= 0;
1483 priv
->fifo
[i
].pack
= NULL
;
1489 static inline int32_t prog_idx_in_pat(ts_priv_t
*priv
, uint16_t progid
)
1493 if(priv
->pat
.progs
== NULL
)
1496 for(x
= 0; x
< priv
->pat
.progs_cnt
; x
++)
1498 if(priv
->pat
.progs
[x
].id
== progid
)
1506 static inline int32_t prog_id_in_pat(ts_priv_t
*priv
, uint16_t pid
)
1510 if(priv
->pat
.progs
== NULL
)
1513 for(x
= 0; x
< priv
->pat
.progs_cnt
; x
++)
1515 if(priv
->pat
.progs
[x
].pmt_pid
== pid
)
1516 return priv
->pat
.progs
[x
].id
;
1523 DON'T REMOVE, I'LL NEED IT IN THE (NEAR) FUTURE)
1524 static int check_crc32(uint32_t val, uint8_t *ptr, uint16_t len, uint8_t *vbase)
1526 uint32_t tab_crc32, calc_crc32;
1528 calc_crc32 = CalcCRC32(val, ptr, len);
1530 tab_crc32 = (vbase[0] << 24) | (vbase[1] << 16) | (vbase[2] << 8) | vbase[3];
1532 printf("CRC32, TAB: %x, CALC: %x, eq: %x\n", tab_crc32, calc_crc32, tab_crc32 == calc_crc32);
1533 return (tab_crc32 == calc_crc32);
1538 static int collect_section(ts_section_t
*section
, int is_start
, unsigned char *buff
, int size
)
1544 mp_msg(MSGT_DEMUX
, MSGL_V
, "COLLECT_SECTION, start: %d, size: %d, collected: %d\n", is_start
, size
, section
->buffer_len
);
1545 if(! is_start
&& !section
->buffer_len
)
1550 if(! section
->buffer
)
1552 section
->buffer
= (uint8_t*) malloc(4096+256);
1553 if(section
->buffer
== NULL
)
1556 section
->buffer_len
= 0;
1559 if(size
+ section
->buffer_len
> 4096+256)
1561 mp_msg(MSGT_DEMUX
, MSGL_V
, "COLLECT_SECTION, excessive len: %d + %d\n", section
->buffer_len
, size
);
1565 memcpy(&(section
->buffer
[section
->buffer_len
]), buff
, size
);
1566 section
->buffer_len
+= size
;
1568 if(section
->buffer_len
< 3)
1571 skip
= section
->buffer
[0];
1572 if(skip
+ 4 > section
->buffer_len
)
1575 ptr
= &(section
->buffer
[skip
+ 1]);
1577 tlen
= ((ptr
[1] & 0x0f) << 8) | ptr
[2];
1578 mp_msg(MSGT_DEMUX
, MSGL_V
, "SKIP: %d+1, TID: %d, TLEN: %d, COLLECTED: %d\n", skip
, tid
, tlen
, section
->buffer_len
);
1579 if(section
->buffer_len
< (skip
+1+3+tlen
))
1581 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "DATA IS NOT ENOUGH, NEXT TIME\n");
1588 static int parse_pat(ts_priv_t
* priv
, int is_start
, unsigned char *buff
, int size
)
1592 unsigned char *base
;
1595 struct pat_progs_t
*tmp
;
1596 ts_section_t
*section
;
1598 section
= &(priv
->pat
.section
);
1599 skip
= collect_section(section
, is_start
, buff
, size
);
1603 ptr
= &(section
->buffer
[skip
]);
1605 priv
->pat
.table_id
= ptr
[0];
1606 if(priv
->pat
.table_id
!= 0)
1608 priv
->pat
.ssi
= (ptr
[1] >> 7) & 0x1;
1609 priv
->pat
.curr_next
= ptr
[5] & 0x01;
1610 priv
->pat
.ts_id
= (ptr
[3] << 8 ) | ptr
[4];
1611 priv
->pat
.version_number
= (ptr
[5] >> 1) & 0x1F;
1612 priv
->pat
.section_length
= ((ptr
[1] & 0x03) << 8 ) | ptr
[2];
1613 priv
->pat
.section_number
= ptr
[6];
1614 priv
->pat
.last_section_number
= ptr
[7];
1616 //check_crc32(0xFFFFFFFFL, ptr, priv->pat.buffer_len - 4, &ptr[priv->pat.buffer_len - 4]);
1617 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
);
1619 entries
= (int) (priv
->pat
.section_length
- 9) / 4; //entries per section
1621 for(i
=0; i
< entries
; i
++)
1624 base
= &ptr
[8 + i
*4];
1625 progid
= (base
[0] << 8) | base
[1];
1627 if((idx
= prog_idx_in_pat(priv
, progid
)) == -1)
1629 int sz
= sizeof(struct pat_progs_t
) * (priv
->pat
.progs_cnt
+1);
1630 tmp
= realloc_struct(priv
->pat
.progs
, priv
->pat
.progs_cnt
+1, sizeof(struct pat_progs_t
));
1633 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "PARSE_PAT: COULDN'T REALLOC %d bytes, NEXT\n", sz
);
1636 priv
->pat
.progs
= tmp
;
1637 idx
= priv
->pat
.progs_cnt
;
1638 priv
->pat
.progs_cnt
++;
1641 priv
->pat
.progs
[idx
].id
= progid
;
1642 priv
->pat
.progs
[idx
].pmt_pid
= ((base
[2] & 0x1F) << 8) | base
[3];
1643 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
);
1650 static inline int32_t es_pid_in_pmt(pmt_t
* pmt
, uint16_t pid
)
1660 for(i
= 0; i
< pmt
->es_cnt
; i
++)
1662 if(pmt
->es
[i
].pid
== pid
)
1670 static uint16_t get_mp4_desc_len(uint8_t *buf
, int *len
)
1672 //uint16_t i = 0, size = 0;
1673 int i
= 0, j
, size
= 0;
1675 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "PARSE_MP4_DESC_LEN(%d), bytes: ", *len
);
1679 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, " %x ", buf
[i
]);
1680 size
|= (buf
[i
] & 0x7f);
1681 if(!(buf
[i
] & 0x80))
1686 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, ", SIZE=%d\n", size
);
1693 static uint16_t parse_mp4_slconfig_descriptor(pmt_t
*pmt
, uint8_t *buf
, int len
, void *elem
)
1697 mp4_sl_config_t
*sl
;
1699 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4_SLCONFIG_DESCRIPTOR(%d)\n", len
);
1700 es
= (mp4_es_descr_t
*) elem
;
1703 mp_msg(MSGT_DEMUX
, MSGL_V
, "argh! NULL elem passed, skip\n");
1708 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;
1709 sl
->ocr
= sl
->dts
= sl
->cts
= 0;
1716 sl
->ts_resolution
= (buf
[i
] << 24) | (buf
[i
+1] << 16) | (buf
[i
+2] << 8) | buf
[i
+3];
1718 sl
->ocr_resolution
= (buf
[i
] << 24) | (buf
[i
+1] << 16) | (buf
[i
+2] << 8) | buf
[i
+3];
1720 sl
->ts_len
= buf
[i
];
1722 sl
->ocr_len
= buf
[i
];
1724 sl
->au_len
= buf
[i
];
1726 sl
->instant_bitrate_len
= buf
[i
];
1728 sl
->degr_len
= (buf
[i
] >> 4) & 0x0f;
1729 sl
->au_seqnum_len
= ((buf
[i
] & 0x0f) << 1) | ((buf
[i
+1] >> 7) & 0x01);
1731 sl
->packet_seqnum_len
= ((buf
[i
] >> 2) & 0x1f);
1735 else if(buf
[0] == 1)
1738 sl
->ts_resolution
= 1000;
1742 else if(buf
[0] == 2)
1753 sl
->au_start
= (sl
->flags
>> 7) & 0x1;
1754 sl
->au_end
= (sl
->flags
>> 6) & 0x1;
1755 sl
->random_accesspoint
= (sl
->flags
>> 5) & 0x1;
1756 sl
->random_accesspoint_only
= (sl
->flags
>> 4) & 0x1;
1757 sl
->padding
= (sl
->flags
>> 3) & 0x1;
1758 sl
->use_ts
= (sl
->flags
>> 2) & 0x1;
1759 sl
->idle
= (sl
->flags
>> 1) & 0x1;
1760 sl
->duration
= sl
->flags
& 0x1;
1764 sl
->timescale
= (buf
[i
] << 24) | (buf
[i
+1] << 16) | (buf
[i
+2] << 8) | buf
[i
+3];
1766 sl
->au_duration
= (buf
[i
] << 8) | buf
[i
+1];
1768 sl
->cts_duration
= (buf
[i
] << 8) | buf
[i
+1];
1771 else //no support for fixed durations atm
1772 sl
->timescale
= sl
->au_duration
= sl
->cts_duration
= 0;
1774 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",
1775 len
, buf
[0], sl
->flags
, sl
->use_ts
, sl
->ts_len
, sl
->timescale
, (uint64_t) sl
->dts
, (uint64_t) sl
->cts
);
1780 static int parse_mp4_descriptors(pmt_t
*pmt
, uint8_t *buf
, int len
, void *elem
);
1782 static uint16_t parse_mp4_decoder_config_descriptor(pmt_t
*pmt
, uint8_t *buf
, int len
, void *elem
)
1786 mp4_decoder_config_t
*dec
;
1788 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4_DECODER_CONFIG_DESCRIPTOR(%d)\n", len
);
1789 es
= (mp4_es_descr_t
*) elem
;
1792 mp_msg(MSGT_DEMUX
, MSGL_V
, "argh! NULL elem passed, skip\n");
1795 dec
= (mp4_decoder_config_t
*) &(es
->decoder
);
1797 dec
->object_type
= buf
[i
];
1798 dec
->stream_type
= (buf
[i
+1]>>2) & 0x3f;
1800 if(dec
->object_type
== 1 && dec
->stream_type
== 1)
1802 dec
->object_type
= MP4_OD
;
1803 dec
->stream_type
= MP4_OD
;
1805 else if(dec
->stream_type
== 4)
1807 if(dec
->object_type
== 0x6a)
1808 dec
->object_type
= VIDEO_MPEG1
;
1809 if(dec
->object_type
>= 0x60 && dec
->object_type
<= 0x65)
1810 dec
->object_type
= VIDEO_MPEG2
;
1811 else if(dec
->object_type
== 0x20)
1812 dec
->object_type
= VIDEO_MPEG4
;
1813 else if(dec
->object_type
== 0x21)
1814 dec
->object_type
= VIDEO_AVC
;
1815 /*else if(dec->object_type == 0x22)
1816 fprintf(stderr, "TYPE 0x22\n");*/
1817 else dec
->object_type
= UNKNOWN
;
1819 else if(dec
->stream_type
== 5)
1821 if(dec
->object_type
== 0x40)
1822 dec
->object_type
= AUDIO_AAC
;
1823 else if(dec
->object_type
== 0x6b)
1824 dec
->object_type
= AUDIO_MP2
;
1825 else if(dec
->object_type
>= 0x66 && dec
->object_type
<= 0x69)
1826 dec
->object_type
= AUDIO_MP2
;
1828 dec
->object_type
= UNKNOWN
;
1831 dec
->object_type
= dec
->stream_type
= UNKNOWN
;
1833 if(dec
->object_type
!= UNKNOWN
)
1835 //update the type of the current stream
1836 for(j
= 0; j
< pmt
->es_cnt
; j
++)
1838 if(pmt
->es
[j
].mp4_es_id
== es
->id
)
1840 pmt
->es
[j
].type
= SL_PES_STREAM
;
1846 parse_mp4_descriptors(pmt
, &buf
[13], len
-13, dec
);
1848 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
);
1853 static uint16_t parse_mp4_decoder_specific_descriptor(pmt_t
*pmt
, uint8_t *buf
, int len
, void *elem
)
1856 mp4_decoder_config_t
*dec
;
1858 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4_DECODER_SPECIFIC_DESCRIPTOR(%d)\n", len
);
1859 dec
= (mp4_decoder_config_t
*) elem
;
1862 mp_msg(MSGT_DEMUX
, MSGL_V
, "argh! NULL elem passed, skip\n");
1866 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "MP4 SPECIFIC INFO BYTES: \n");
1867 for(i
=0; i
<len
; i
++)
1868 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "%02x ", buf
[i
]);
1869 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "\n");
1871 if(len
> MAX_EXTRADATA_SIZE
)
1873 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "DEMUX_TS, EXTRADATA SUSPICIOUSLY BIG: %d, REFUSED\r\n", len
);
1876 memcpy(dec
->buf
, buf
, len
);
1877 dec
->buf_size
= len
;
1882 static uint16_t parse_mp4_es_descriptor(pmt_t
*pmt
, uint8_t *buf
, int len
, void *elem
)
1884 int i
= 0, j
= 0, k
, found
;
1886 mp4_es_descr_t es
, *target_es
= NULL
, *tmp
;
1888 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4ES: len=%d\n", len
);
1889 memset(&es
, 0, sizeof(mp4_es_descr_t
));
1892 es
.id
= (buf
[i
] << 8) | buf
[i
+1];
1893 mp_msg(MSGT_DEMUX
, MSGL_V
, "MP4ES_ID: %d\n", es
.id
);
1901 if(flag
& 0x20) //OCR, maybe we need it
1904 j
= parse_mp4_descriptors(pmt
, &buf
[i
], len
-i
, &es
);
1905 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
);
1906 if(es
.decoder
.object_type
!= UNKNOWN
&& es
.decoder
.stream_type
!= UNKNOWN
)
1909 //search this ES_ID if we already have it
1910 for(k
=0; k
< pmt
->mp4es_cnt
; k
++)
1912 if(pmt
->mp4es
[k
].id
== es
.id
)
1914 target_es
= &(pmt
->mp4es
[k
]);
1921 tmp
= realloc_struct(pmt
->mp4es
, pmt
->mp4es_cnt
+1, sizeof(mp4_es_descr_t
));
1924 fprintf(stderr
, "CAN'T REALLOC MP4_ES_DESCR\n");
1928 target_es
= &(pmt
->mp4es
[pmt
->mp4es_cnt
]);
1931 memcpy(target_es
, &es
, sizeof(mp4_es_descr_t
));
1932 mp_msg(MSGT_DEMUX
, MSGL_V
, "MP4ES_CNT: %d, ID=%d\n", pmt
->mp4es_cnt
, target_es
->id
);
1941 static void parse_mp4_object_descriptor(pmt_t
*pmt
, uint8_t *buf
, int len
, void *elem
)
1946 id
= (buf
[0] << 2) | ((buf
[1] & 0xc0) >> 6);
1947 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4_OBJECT_DESCRIPTOR: len=%d, OD_ID=%d\n", len
, id
);
1950 i
+= buf
[2] + 1; //url
1951 mp_msg(MSGT_DEMUX
, MSGL_V
, "URL\n");
1959 j
= parse_mp4_descriptors(pmt
, &(buf
[i
]), len
-i
, elem
);
1960 mp_msg(MSGT_DEMUX
, MSGL_V
, "OBJD, NOW i = %d, j=%d, LEN=%d\n", i
, j
, len
);
1967 static void parse_mp4_iod(pmt_t
*pmt
, uint8_t *buf
, int len
, void *elem
)
1970 mp4_od_t
*iod
= &(pmt
->iod
);
1972 iod
->id
= (buf
[0] << 2) | ((buf
[1] & 0xc0) >> 6);
1973 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4_IOD: len=%d, IOD_ID=%d\n", len
, iod
->id
);
1977 i
+= buf
[2] + 1; //url
1978 mp_msg(MSGT_DEMUX
, MSGL_V
, "URL\n");
1985 j
= parse_mp4_descriptors(pmt
, &(buf
[i
]), len
-i
, elem
);
1986 mp_msg(MSGT_DEMUX
, MSGL_V
, "IOD, NOW i = %d, j=%d, LEN=%d\n", i
, j
, len
);
1992 static int parse_mp4_descriptors(pmt_t
*pmt
, uint8_t *buf
, int len
, void *elem
)
1994 int tag
, descr_len
, i
= 0, j
= 0;
1996 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4_DESCRIPTORS, len=%d\n", len
);
2004 descr_len
= get_mp4_desc_len(&(buf
[i
+1]), &j
);
2005 mp_msg(MSGT_DEMUX
, MSGL_V
, "TAG=%d (0x%x), DESCR_len=%d, len=%d, j=%d\n", tag
, tag
, descr_len
, len
, j
);
2006 if(descr_len
> len
- j
+1)
2008 mp_msg(MSGT_DEMUX
, MSGL_V
, "descriptor is too long, exit\n");
2016 parse_mp4_object_descriptor(pmt
, &(buf
[i
]), descr_len
, elem
);
2019 parse_mp4_iod(pmt
, &(buf
[i
]), descr_len
, elem
);
2022 parse_mp4_es_descriptor(pmt
, &(buf
[i
]), descr_len
, elem
);
2025 parse_mp4_decoder_config_descriptor(pmt
, &buf
[i
], descr_len
, elem
);
2028 parse_mp4_decoder_specific_descriptor(pmt
, &buf
[i
], descr_len
, elem
);
2031 parse_mp4_slconfig_descriptor(pmt
, &buf
[i
], descr_len
, elem
);
2034 mp_msg(MSGT_DEMUX
, MSGL_V
, "Unsupported mp4 descriptor 0x%x\n", tag
);
2042 static ES_stream_t
*new_pid(ts_priv_t
*priv
, int pid
)
2046 tss
= malloc(sizeof(ES_stream_t
));
2049 memset(tss
, 0, sizeof(ES_stream_t
));
2052 tss
->type
= UNKNOWN
;
2053 tss
->subtype
= UNKNOWN
;
2055 tss
->extradata
= NULL
;
2056 tss
->extradata_alloc
= tss
->extradata_len
= 0;
2057 priv
->ts
.pids
[pid
] = tss
;
2063 static int parse_program_descriptors(pmt_t
*pmt
, uint8_t *buf
, uint16_t len
)
2065 uint16_t i
= 0, k
, olen
= len
;
2069 mp_msg(MSGT_DEMUX
, MSGL_V
, "PROG DESCR, TAG=%x, LEN=%d(%x)\n", buf
[i
], buf
[i
+1], buf
[i
+1]);
2070 if(buf
[i
+1] > len
-2)
2072 mp_msg(MSGT_DEMUX
, MSGL_V
, "ERROR, descriptor len is too long, skipping\n");
2078 if(buf
[i
+3] == 2) //buggy versions of vlc muxer make this non-standard mess (missing iod_scope)
2081 k
= 4; //this is standard compliant
2082 parse_mp4_descriptors(pmt
, &buf
[i
+k
], (int) buf
[i
+1]-(k
-2), NULL
);
2085 len
-= 2 + buf
[i
+1];
2091 static int parse_descriptors(struct pmt_es_t
*es
, uint8_t *ptr
)
2093 int j
, descr_len
, len
;
2096 len
= es
->descr_length
;
2099 descr_len
= ptr
[j
+1];
2100 mp_msg(MSGT_DEMUX
, MSGL_V
, "...descr id: 0x%x, len=%d\n", ptr
[j
], descr_len
);
2103 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "INVALID DESCR LEN for tag %02x: %d vs %d max, EXIT LOOP\n", ptr
[j
], descr_len
, len
);
2108 if(ptr
[j
] == 0x6a || ptr
[j
] == 0x7a) //A52 Descriptor
2112 es
->type
= AUDIO_A52
;
2113 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "DVB A52 Descriptor\n");
2116 else if(ptr
[j
] == 0x59) //Subtitling Descriptor
2120 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Subtitling Descriptor\n");
2123 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Descriptor length too short for DVB Subtitle Descriptor: %d, SKIPPING\n", descr_len
);
2127 memcpy(es
->lang
, &ptr
[j
+2], 3);
2131 (subtype
>= 0x10 && subtype
<= 0x13) ||
2132 (subtype
>= 0x20 && subtype
<= 0x23)
2136 //page parameters: compo page 2 bytes, ancillary page 2 bytes
2142 else if(ptr
[j
] == 0x50) //Component Descriptor
2144 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Component Descriptor\n");
2145 memcpy(es
->lang
, &ptr
[j
+5], 3);
2148 else if(ptr
[j
] == 0xa) //Language Descriptor
2150 memcpy(es
->lang
, &ptr
[j
+2], 3);
2152 mp_msg(MSGT_DEMUX
, MSGL_V
, "Language Descriptor: %s\n", es
->lang
);
2154 else if(ptr
[j
] == 0x5) //Registration Descriptor (looks like e fourCC :) )
2156 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Registration Descriptor\n");
2159 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Registration Descriptor length too short: %d, SKIPPING\n", descr_len
);
2164 memcpy(es
->format_descriptor
, &ptr
[j
+2], 4);
2165 es
->format_descriptor
[4] = 0;
2168 if(d
[0] == 'A' && d
[1] == 'C' && d
[2] == '-' && d
[3] == '3')
2170 es
->type
= AUDIO_A52
;
2172 else if(d
[0] == 'D' && d
[1] == 'T' && d
[2] == 'S' && d
[3] == '2')
2174 es
->type
= AUDIO_DTS
;
2178 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "FORMAT %s\n", es
->format_descriptor
);
2181 else if(ptr
[j
] == 0x1e)
2183 es
->mp4_es_id
= (ptr
[j
+2] << 8) | ptr
[j
+3];
2184 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
);
2187 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Unknown descriptor 0x%x, SKIPPING\n", ptr
[j
]);
2189 len
-= 2 + descr_len
;
2196 static int parse_sl_section(pmt_t
*pmt
, ts_section_t
*section
, uint16_t progid
, uint16_t pid
, int is_start
, unsigned char *buff
, int size
)
2200 skip
= collect_section(section
, is_start
, buff
, size
);
2204 ptr
= &(section
->buffer
[skip
]);
2206 len
= ((ptr
[1] & 0x0f) << 8) | ptr
[2];
2207 mp_msg(MSGT_DEMUX
, MSGL_V
, "TABLEID: %d (av. %d), skip=%d, LEN: %d\n", tid
, section
->buffer_len
, skip
, len
);
2208 if(len
> 4093 || section
->buffer_len
< len
|| tid
!= 5)
2210 mp_msg(MSGT_DEMUX
, MSGL_V
, "SECTION TOO LARGE or wrong section type, EXIT\n");
2217 //8 is the current position, len - 9 is the amount of data available
2218 parse_mp4_descriptors(pmt
, &ptr
[8], len
- 9, NULL
);
2223 static int parse_pmt(ts_priv_t
* priv
, uint16_t progid
, uint16_t pid
, int is_start
, unsigned char *buff
, int size
)
2225 unsigned char *base
, *es_base
;
2227 int32_t idx
, es_count
, section_bytes
;
2231 struct pmt_es_t
*tmp_es
;
2232 ts_section_t
*section
;
2235 idx
= progid_idx_in_pmt(priv
, progid
);
2239 int sz
= (priv
->pmt_cnt
+ 1) * sizeof(pmt_t
);
2240 tmp
= realloc_struct(priv
->pmt
, priv
->pmt_cnt
+ 1, sizeof(pmt_t
));
2243 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "PARSE_PMT: COULDN'T REALLOC %d bytes, NEXT\n", sz
);
2247 idx
= priv
->pmt_cnt
;
2248 memset(&(priv
->pmt
[idx
]), 0, sizeof(pmt_t
));
2250 priv
->pmt
[idx
].progid
= progid
;
2253 pmt
= &(priv
->pmt
[idx
]);
2255 section
= &(pmt
->section
);
2256 skip
= collect_section(section
, is_start
, buff
, size
);
2260 base
= &(section
->buffer
[skip
]);
2262 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",
2263 progid
, pmt
->section
.buffer_len
, is_start
, pid
, size
, m
, pmt
->es_cnt
, idx
, pmt
);
2265 pmt
->table_id
= base
[0];
2266 if(pmt
->table_id
!= 2)
2268 pmt
->ssi
= base
[1] & 0x80;
2269 pmt
->section_length
= (((base
[1] & 0xf) << 8 ) | base
[2]);
2270 pmt
->version_number
= (base
[5] >> 1) & 0x1f;
2271 pmt
->curr_next
= (base
[5] & 1);
2272 pmt
->section_number
= base
[6];
2273 pmt
->last_section_number
= base
[7];
2274 pmt
->PCR_PID
= ((base
[8] & 0x1f) << 8 ) | base
[9];
2275 pmt
->prog_descr_length
= ((base
[10] & 0xf) << 8 ) | base
[11];
2276 if(pmt
->prog_descr_length
> pmt
->section_length
- 9)
2278 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_PMT, INVALID PROG_DESCR LENGTH (%d vs %d)\n", pmt
->prog_descr_length
, pmt
->section_length
- 9);
2282 if(pmt
->prog_descr_length
)
2283 parse_program_descriptors(pmt
, &base
[12], pmt
->prog_descr_length
);
2285 es_base
= &base
[12 + pmt
->prog_descr_length
]; //the beginning of th ES loop
2287 section_bytes
= pmt
->section_length
- 13 - pmt
->prog_descr_length
;
2290 while(section_bytes
>= 5)
2292 int es_pid
, es_type
;
2294 es_type
= es_base
[0];
2295 es_pid
= ((es_base
[1] & 0x1f) << 8) | es_base
[2];
2297 idx
= es_pid_in_pmt(pmt
, es_pid
);
2300 int sz
= sizeof(struct pmt_es_t
) * (pmt
->es_cnt
+ 1);
2301 tmp_es
= realloc_struct(pmt
->es
, pmt
->es_cnt
+ 1, sizeof(struct pmt_es_t
));
2304 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "PARSE_PMT, COULDN'T ALLOCATE %d bytes for PMT_ES\n", sz
);
2309 memset(&(pmt
->es
[idx
]), 0, sizeof(struct pmt_es_t
));
2313 pmt
->es
[idx
].descr_length
= ((es_base
[3] & 0xf) << 8) | es_base
[4];
2316 if(pmt
->es
[idx
].descr_length
> section_bytes
- 5)
2318 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_PMT, ES_DESCR_LENGTH TOO LARGE %d > %d, EXIT\n",
2319 pmt
->es
[idx
].descr_length
, section_bytes
- 5);
2324 pmt
->es
[idx
].pid
= es_pid
;
2326 pmt
->es
[idx
].type
= UNKNOWN
;
2328 pmt
->es
[idx
].type
= es_type
;
2330 parse_descriptors(&pmt
->es
[idx
], &es_base
[5]);
2335 pmt
->es
[idx
].type
= VIDEO_MPEG1
;
2338 pmt
->es
[idx
].type
= VIDEO_MPEG2
;
2342 pmt
->es
[idx
].type
= AUDIO_MP2
;
2345 if(pmt
->es
[idx
].type
== 0x6) //this could have been ovrwritten by parse_descriptors
2346 pmt
->es
[idx
].type
= UNKNOWN
;
2349 pmt
->es
[idx
].type
= VIDEO_MPEG4
;
2353 pmt
->es
[idx
].type
= AUDIO_AAC
;
2356 pmt
->es
[idx
].type
= VIDEO_H264
;
2359 pmt
->es
[idx
].type
= SL_PES_STREAM
;
2362 pmt
->es
[idx
].type
= SL_SECTION
;
2365 pmt
->es
[idx
].type
= AUDIO_A52
;
2368 pmt
->es
[idx
].type
= AUDIO_DTS
;
2371 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "UNKNOWN ES TYPE=0x%x\n", es_type
);
2372 pmt
->es
[idx
].type
= UNKNOWN
;
2375 tss
= priv
->ts
.pids
[es_pid
]; //an ES stream
2378 tss
= new_pid(priv
, es_pid
);
2380 tss
->type
= pmt
->es
[idx
].type
;
2383 section_bytes
-= 5 + pmt
->es
[idx
].descr_length
;
2384 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",
2385 progid
, idx
, es_count
, pmt
->es
[idx
].pid
, pmt
->es
[idx
].pid
, pmt
->es
[idx
].type
, pmt
->es
[idx
].descr_length
, section_bytes
);
2388 es_base
+= 5 + pmt
->es
[idx
].descr_length
;
2393 mp_msg(MSGT_DEMUX
, MSGL_V
, "----------------------------\n");
2397 static pmt_t
* pmt_of_pid(ts_priv_t
*priv
, int pid
, mp4_decoder_config_t
**mp4_dec
)
2403 for(i
= 0; i
< priv
->pmt_cnt
; i
++)
2405 if(priv
->pmt
[i
].es
&& priv
->pmt
[i
].es_cnt
)
2407 for(j
= 0; j
< priv
->pmt
[i
].es_cnt
; j
++)
2409 if(priv
->pmt
[i
].es
[j
].pid
== pid
)
2412 if(priv
->pmt
[i
].es
[j
].mp4_es_id
)
2414 for(k
= 0; k
< priv
->pmt
[i
].mp4es_cnt
; k
++)
2416 if(priv
->pmt
[i
].mp4es
[k
].id
== priv
->pmt
[i
].es
[j
].mp4_es_id
)
2418 *mp4_dec
= &(priv
->pmt
[i
].mp4es
[k
].decoder
);
2424 return &(priv
->pmt
[i
]);
2435 static inline int32_t pid_type_from_pmt(ts_priv_t
*priv
, int pid
)
2437 int32_t pmt_idx
, pid_idx
, i
, j
;
2439 pmt_idx
= progid_idx_in_pmt(priv
, priv
->prog
);
2443 pid_idx
= es_pid_in_pmt(&(priv
->pmt
[pmt_idx
]), pid
);
2445 return priv
->pmt
[pmt_idx
].es
[pid_idx
].type
;
2449 for(i
= 0; i
< priv
->pmt_cnt
; i
++)
2451 pmt_t
*pmt
= &(priv
->pmt
[i
]);
2452 for(j
= 0; j
< pmt
->es_cnt
; j
++)
2453 if(pmt
->es
[j
].pid
== pid
)
2454 return pmt
->es
[j
].type
;
2462 static inline uint8_t *pid_lang_from_pmt(ts_priv_t
*priv
, int pid
)
2464 int32_t pmt_idx
, pid_idx
, i
, j
;
2466 pmt_idx
= progid_idx_in_pmt(priv
, priv
->prog
);
2470 pid_idx
= es_pid_in_pmt(&(priv
->pmt
[pmt_idx
]), pid
);
2472 return priv
->pmt
[pmt_idx
].es
[pid_idx
].lang
;
2476 for(i
= 0; i
< priv
->pmt_cnt
; i
++)
2478 pmt_t
*pmt
= &(priv
->pmt
[i
]);
2479 for(j
= 0; j
< pmt
->es_cnt
; j
++)
2480 if(pmt
->es
[j
].pid
== pid
)
2481 return pmt
->es
[j
].lang
;
2489 static int fill_packet(demuxer_t
*demuxer
, demux_stream_t
*ds
, demux_packet_t
**dp
, int *dp_offset
, TS_stream_info
*si
)
2493 if((*dp
!= NULL
) && (*dp_offset
> 0))
2496 resize_demux_packet(*dp
, ret
); //shrinked to the right size
2497 ds_add_packet(ds
, *dp
);
2498 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
);
2501 float diff
= (*dp
)->pts
- si
->last_pts
;
2504 if(abs(diff
) > 1) //1 second, there's a discontinuity
2506 si
->duration
+= si
->last_pts
- si
->first_pts
;
2507 si
->first_pts
= si
->last_pts
= (*dp
)->pts
;
2511 si
->last_pts
= (*dp
)->pts
;
2514 dur
= si
->duration
+ (si
->last_pts
- si
->first_pts
);
2516 if(dur
> 0 && ds
== demuxer
->video
)
2518 ts_priv_t
* priv
= (ts_priv_t
*) demuxer
->priv
;
2519 if(dur
> 1) //otherwise it may be unreliable
2520 priv
->vbitrate
= (uint32_t) ((float) si
->size
/ dur
);
2531 static int fill_extradata(mp4_decoder_config_t
* mp4_dec
, ES_stream_t
*tss
)
2535 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "MP4_dec: %p, pid: %d\n", mp4_dec
, tss
->pid
);
2537 if(mp4_dec
->buf_size
> tss
->extradata_alloc
)
2539 tmp
= (uint8_t *) realloc(tss
->extradata
, mp4_dec
->buf_size
);
2542 tss
->extradata
= tmp
;
2543 tss
->extradata_alloc
= mp4_dec
->buf_size
;
2545 memcpy(tss
->extradata
, mp4_dec
->buf
, mp4_dec
->buf_size
);
2546 tss
->extradata_len
= mp4_dec
->buf_size
;
2547 mp_msg(MSGT_DEMUX
, MSGL_V
, "EXTRADATA: %p, alloc=%d, len=%d\n", tss
->extradata
, tss
->extradata_alloc
, tss
->extradata_len
);
2549 return tss
->extradata_len
;
2552 // 0 = EOF or no stream found
2553 // else = [-] number of bytes written to the packet
2554 static int ts_parse(demuxer_t
*demuxer
, ES_stream_t
*es
, unsigned char *packet
, int probe
)
2558 int buf_size
, is_start
, pid
, base
;
2559 int len
, cc
, cc_ok
, afc
, retv
= 0, is_video
, is_audio
, is_sub
;
2560 ts_priv_t
* priv
= (ts_priv_t
*) demuxer
->priv
;
2561 stream_t
*stream
= demuxer
->stream
;
2563 demux_stream_t
*ds
= NULL
;
2564 demux_packet_t
**dp
= NULL
;
2565 int *dp_offset
= 0, *buffer_size
= 0;
2566 int32_t progid
, pid_type
, bad
, ts_error
;
2567 int junk
= 0, rap_flag
= 0;
2569 mp4_decoder_config_t
*mp4_dec
;
2576 ds
= (demux_stream_t
*) NULL
;
2577 dp
= (demux_packet_t
**) NULL
;
2578 dp_offset
= buffer_size
= NULL
;
2584 junk
= priv
->ts
.packet_size
- TS_PACKET_SIZE
;
2585 buf_size
= priv
->ts
.packet_size
- junk
;
2587 if(stream_eof(stream
))
2591 ts_dump_streams(priv
);
2592 demuxer
->filepos
= stream_tell(demuxer
->stream
);
2599 if(! ts_sync(stream
))
2601 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "TS_PARSE: COULDN'T SYNC\n");
2605 len
= stream_read(stream
, &packet
[1], 3);
2609 if((packet
[1] >> 7) & 0x01) //transport error
2614 is_start
= packet
[1] & 0x40;
2615 pid
= ((packet
[1] & 0x1f) << 8) | packet
[2];
2617 tss
= priv
->ts
.pids
[pid
]; //an ES stream
2620 tss
= new_pid(priv
, pid
);
2627 if(((pid
> 1) && (pid
< 16)) || (pid
== 8191)) //invalid pid
2629 stream_skip(stream
, buf_size
-1+junk
);
2633 cc
= (packet
[3] & 0xf);
2634 cc_ok
= (tss
->last_cc
< 0) || ((((tss
->last_cc
+ 1) & 0x0f) == cc
));
2637 bad
= ts_error
; // || (! cc_ok);
2639 afc
= (packet
[3] >> 4) & 3;
2643 c
= stream_read_char(stream
);
2645 rap_flag
= (stream_read_char(stream
) & 0x40) >> 6;
2648 c
= min(c
-1, buf_size
);
2649 stream_skip(stream
, c
);
2655 if(! (afc
% 2)) //no payload in this TS packet
2657 stream_skip(stream
, buf_size
-1+junk
);
2663 // logically this packet should be dropped, but if I do it
2664 // certain streams play corrupted. Maybe the decoders know
2665 // how to deal with it, but at least I consider the packet
2667 mp_msg(MSGT_DEMUX
, MSGL_V
, "ts_parse: PID=%d, Transport error: %d, CC_OK: %s\n\n", tss
->pid
, ts_error
, (cc_ok
? "yes" : "no"));
2669 if(priv
->keep_broken
== 0)
2671 stream_skip(stream
, buf_size
-1+junk
);
2675 is_start
= 0; //queued to the packet data
2678 //find the program that the pid belongs to; if (it's the right one or -1) && pid_type==SL_SECTION
2679 //call parse_sl_section()
2680 pmt
= pmt_of_pid(priv
, pid
, &mp4_dec
);
2683 fill_extradata(mp4_dec
, tss
);
2684 if(IS_VIDEO(mp4_dec
->object_type
) || IS_AUDIO(mp4_dec
->object_type
))
2686 tss
->type
= SL_PES_STREAM
;
2687 tss
->subtype
= mp4_dec
->object_type
;
2694 base
= priv
->ts
.packet_size
- buf_size
;
2697 stream_read(stream
,&packet
[base
], buf_size
);
2698 stream_skip(stream
, junk
);
2699 parse_pat(priv
, is_start
, &packet
[base
], buf_size
);
2702 else if((tss
->type
== SL_SECTION
) && pmt
)
2704 int k
, ok
=0, mp4_es_id
= -1;
2705 ts_section_t
*section
;
2706 for(k
= 0; k
< pmt
->mp4es_cnt
; k
++)
2708 if(pmt
->mp4es
[k
].decoder
.object_type
== MP4_OD
&& pmt
->mp4es
[k
].decoder
.stream_type
== MP4_OD
)
2709 mp4_es_id
= pmt
->mp4es
[k
].id
;
2711 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "MP4ESID: %d\n", mp4_es_id
);
2712 for(k
= 0; k
< pmt
->es_cnt
; k
++)
2714 if(pmt
->es
[k
].mp4_es_id
== mp4_es_id
)
2717 stream_read(stream
,&packet
[base
], buf_size
);
2718 stream_skip(stream
, junk
);
2721 section
= &(tss
->section
);
2722 parse_sl_section(pmt
, section
, progid
, pid
, is_start
, &packet
[base
], buf_size
);
2728 progid
= prog_id_in_pat(priv
, pid
);
2731 if(pid
!= demuxer
->video
->id
&& pid
!= demuxer
->audio
->id
&& pid
!= demuxer
->sub
->id
)
2733 stream_read(stream
,&packet
[base
], buf_size
);
2734 stream_skip(stream
, junk
);
2735 parse_pmt(priv
, progid
, pid
, is_start
, &packet
[base
], buf_size
);
2739 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "Argh! Data pid %d used in the PMT, Skipping PMT parsing!\n", pid
);
2744 priv
->last_pid
= pid
;
2746 is_video
= IS_VIDEO(tss
->type
) || (tss
->type
==SL_PES_STREAM
&& IS_VIDEO(tss
->subtype
));
2747 is_audio
= IS_AUDIO(tss
->type
) || (tss
->type
==SL_PES_STREAM
&& IS_AUDIO(tss
->subtype
)) || (tss
->type
== PES_PRIVATE1
);
2748 is_sub
= ((tss
->type
== SPU_DVD
) || (tss
->type
== SPU_DVB
));
2749 pid_type
= pid_type_from_pmt(priv
, pid
);
2751 // PES CONTENT STARTS HERE
2754 if((IS_AUDIO(tss
->type
) || IS_AUDIO(tss
->subtype
)) && is_start
&& !priv
->ts
.streams
[pid
].sh
&& priv
->last_aid
+1 < MAX_A_STREAMS
)
2756 sh_audio_t
*sh
= new_sh_audio_aid(demuxer
, priv
->last_aid
+1, pid
);
2759 sh
->format
= IS_AUDIO(tss
->type
) ? tss
->type
: tss
->subtype
;
2760 sh
->ds
= demuxer
->audio
;
2763 priv
->ts
.streams
[pid
].id
= priv
->last_aid
;
2764 priv
->ts
.streams
[pid
].sh
= sh
;
2765 priv
->ts
.streams
[pid
].type
= TYPE_AUDIO
;
2766 mp_msg(MSGT_DEMUX
, MSGL_V
, "\r\nADDED AUDIO PID %d, type: %x stream n. %d\r\n", pid
, sh
->format
, priv
->last_aid
);
2770 if((pid
== demuxer
->sub
->id
)) //or the lang is right
2775 if(is_video
&& (demuxer
->video
->id
== tss
->pid
))
2777 ds
= demuxer
->video
;
2779 dp
= &priv
->fifo
[1].pack
;
2780 dp_offset
= &priv
->fifo
[1].offset
;
2781 buffer_size
= &priv
->fifo
[1].buffer_size
;
2784 else if(is_audio
&& (demuxer
->audio
->id
== priv
->ts
.streams
[pid
].id
))
2786 ds
= demuxer
->audio
;
2788 dp
= &priv
->fifo
[0].pack
;
2789 dp_offset
= &priv
->fifo
[0].offset
;
2790 buffer_size
= &priv
->fifo
[0].buffer_size
;
2794 || (pid_type
== SPU_DVD
) || (pid_type
== SPU_DVB
))
2796 //SUBS are infrequent, so the initial detection may fail
2797 // and we may need to add them at play-time
2798 if(demuxer
->sub
->id
== -1)
2801 p
= progid_for_pid(priv
, tss
->pid
, priv
->prog
);
2810 if(!strcmp(dvdsub_lang
, ""))
2814 lang
= pid_lang_from_pmt(priv
, pid
);
2816 asgn
= (strncmp(lang
, dvdsub_lang
, 3) == 0);
2824 demuxer
->sub
->id
= tss
->pid
;
2825 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "CHOSEN SUBs pid 0x%x (%d) FROM PROG %d\n", tss
->pid
, tss
->pid
, priv
->prog
);
2830 mp_msg(MSGT_DEMUX
, MSGL_V
, "DISCARDED SUBs pid 0x%x (%d) NOT CHOSEN OR NOT IN PROG %d\n", tss
->pid
, tss
->pid
, priv
->prog
);
2834 if(demuxer
->sub
->id
== tss
->pid
)
2838 dp
= &priv
->fifo
[2].pack
;
2839 dp_offset
= &priv
->fifo
[2].offset
;
2840 buffer_size
= &priv
->fifo
[2].buffer_size
;
2844 stream_skip(stream
, buf_size
+junk
);
2850 stream_skip(stream
, buf_size
+junk
);
2854 //IS IT TIME TO QUEUE DATA to the dp_packet?
2855 if(is_start
&& (dp
!= NULL
))
2857 retv
= fill_packet(demuxer
, ds
, dp
, dp_offset
, si
);
2863 if(*buffer_size
> MAX_PACK_BYTES
)
2864 *buffer_size
= MAX_PACK_BYTES
;
2865 *dp
= new_demux_packet(*buffer_size
); //es->size
2869 fprintf(stderr
, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", *buffer_size
);
2872 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "CREATED DP(%d)\n", *buffer_size
);
2875 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "NOW PACKET_SIZE = %d, DP_OFFSET = %d\n", *buffer_size
, *dp_offset
);
2882 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "IS_START\n");
2885 stream_read(stream
, p
, buf_size
);
2886 stream_skip(stream
, junk
);
2888 len
= pes_parse2(p
, buf_size
, es
, pid_type
, pmt
, pid
);
2890 tss
->is_synced
|= es
->is_synced
|| rap_flag
;
2892 if(es
->type
==SL_PES_STREAM
&& !tss
->is_synced
)
2902 if(es
->type
== UNKNOWN
)
2905 tss
->payload_size
= es
->payload_size
;
2908 if(tss
->type
!= UNKNOWN
)
2910 es
->size
= buf_size
;
2917 uint8_t *lang
= NULL
;
2918 tss
->type
= es
->type
;
2919 tss
->subtype
= es
->subtype
;
2922 lang
= pid_lang_from_pmt(priv
, es
->pid
);
2925 memcpy(es
->lang
, lang
, 3);
2937 if(tss
->type
!= UNKNOWN
)
2939 len
= es
->size
= buf_size
; //push the whole packet to the fifo
2940 //(we already learned what it is during the probe phase)
2948 es
->pts
= tss
->pts
= tss
->last_pts
;
2950 tss
->pts
= tss
->last_pts
= es
->pts
;
2952 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "ts_parse, NEW pid=%d, PSIZE: %u, type=%X, start=%p, len=%d\n",
2953 es
->pid
, es
->payload_size
, es
->type
, es
->start
, es
->size
);
2955 tss
->payload_size
= es
->payload_size
;
2957 demuxer
->filepos
= stream_tell(demuxer
->stream
) - es
->size
;
2959 if(*dp_offset
+ es
->size
> *buffer_size
)
2961 *buffer_size
= *dp_offset
+ es
->size
+ TS_FEC_PACKET_SIZE
;
2962 resize_demux_packet(*dp
, *buffer_size
);
2963 //we'll skip at least one RESIZE() in the next iteration of ts_parse()
2964 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "RESIZE DP TO %d\n", *buffer_size
);
2966 memcpy(&((*dp
)->buffer
[*dp_offset
]), es
->start
, es
->size
);
2967 *dp_offset
+= es
->size
;
2969 (*dp
)->pos
= stream_tell(demuxer
->stream
);
2970 (*dp
)->pts
= es
->pts
;
2972 if(*dp_offset
>= MAX_PACK_BYTES
)
2974 retv
= fill_packet(demuxer
, ds
, dp
, dp_offset
, si
);
2988 if((tss
->type
== UNKNOWN
) || (tss
->type
==SL_PES_STREAM
&& !tss
->is_synced
))
2990 stream_skip(stream
, buf_size
+junk
);
2992 return (is_video
|| is_audio
|| is_sub
);
2999 es
->type
= tss
->type
;
3000 es
->subtype
= tss
->subtype
;
3001 es
->pts
= tss
->pts
= tss
->last_pts
;
3002 es
->start
= &packet
[base
];
3005 if(tss
->payload_size
> 0)
3007 sz
= min(tss
->payload_size
, buf_size
);
3008 tss
->payload_size
-= sz
;
3015 sz
= es
->size
= buf_size
;
3019 stream_skip(stream
, buf_size
+junk
);
3027 if(*dp_offset
+ sz
> *buffer_size
)
3029 *buffer_size
= *dp_offset
+ sz
+ TS_FEC_PACKET_SIZE
;
3030 resize_demux_packet(*dp
, *buffer_size
);
3031 //we'll skip at least one RESIZE() in the next iteration of ts_parse()
3032 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "RESIZE DP TO %d\n", *buffer_size
);
3035 stream_read(stream
, &((*dp
)->buffer
[*dp_offset
]), sz
);
3038 if(buf_size
- sz
> 0)
3040 stream_skip(stream
, buf_size
- sz
);
3042 stream_skip(stream
, junk
);
3044 if(*dp_offset
>= MAX_PACK_BYTES
)
3046 (*dp
)->pts
= tss
->last_pts
;
3047 retv
= fill_packet(demuxer
, ds
, dp
, dp_offset
, si
);
3055 stream_read(stream
, es
->start
, sz
);
3056 if(buf_size
- sz
) stream_skip(stream
, buf_size
-sz
);
3057 stream_skip(stream
, junk
);
3071 extern void skip_audio_frame(sh_audio_t
*sh_audio
);
3073 static void reset_fifos(ts_priv_t
* priv
, int a
, int v
, int s
)
3077 if(priv
->fifo
[0].pack
!= NULL
)
3079 free_demux_packet(priv
->fifo
[0].pack
);
3080 priv
->fifo
[0].pack
= NULL
;
3082 priv
->fifo
[0].offset
= 0;
3087 if(priv
->fifo
[1].pack
!= NULL
)
3089 free_demux_packet(priv
->fifo
[1].pack
);
3090 priv
->fifo
[1].pack
= NULL
;
3092 priv
->fifo
[1].offset
= 0;
3097 if(priv
->fifo
[2].pack
!= NULL
)
3099 free_demux_packet(priv
->fifo
[2].pack
);
3100 priv
->fifo
[2].pack
= NULL
;
3102 priv
->fifo
[2].offset
= 0;
3106 extern int videobuf_code_len
;
3107 extern int sync_video_packet(demux_stream_t
*);
3108 extern int skip_video_packet(demux_stream_t
*);
3110 static void demux_seek_ts(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
, int flags
)
3112 demux_stream_t
*d_audio
=demuxer
->audio
;
3113 demux_stream_t
*d_video
=demuxer
->video
;
3114 demux_stream_t
*d_sub
=demuxer
->sub
;
3115 sh_audio_t
*sh_audio
=d_audio
->sh
;
3116 sh_video_t
*sh_video
=d_video
->sh
;
3117 ts_priv_t
* priv
= (ts_priv_t
*) demuxer
->priv
;
3121 //================= seek in MPEG-TS ==========================
3123 ts_dump_streams(demuxer
->priv
);
3124 reset_fifos(priv
, sh_audio
!= NULL
, sh_video
!= NULL
, demuxer
->sub
->id
> 0);
3127 if(sh_audio
!= NULL
)
3128 ds_free_packs(d_audio
);
3129 if(sh_video
!= NULL
)
3130 ds_free_packs(d_video
);
3131 if(demuxer
->sub
->id
> 0)
3132 ds_free_packs(d_sub
);
3135 video_stats
= (sh_video
!= NULL
);
3138 mp_msg(MSGT_DEMUX
, MSGL_V
, "IBPS: %d, vb: %d\r\n", sh_video
->i_bps
, priv
->vbitrate
);
3140 video_stats
= priv
->vbitrate
;
3142 video_stats
= sh_video
->i_bps
;
3145 newpos
= (flags
& 1) ? demuxer
->movi_start
: demuxer
->filepos
;
3146 if(flags
& 2) // float seek 0..1
3147 newpos
+=(demuxer
->movi_end
-demuxer
->movi_start
)*rel_seek_secs
;
3152 if(! video_stats
) // unspecified or VBR
3153 newpos
+= 2324*75*rel_seek_secs
; // 174.3 kbyte/sec
3155 newpos
+= video_stats
*rel_seek_secs
;
3159 if(newpos
< demuxer
->movi_start
)
3160 newpos
= demuxer
->movi_start
; //begininng of stream
3162 #ifdef _LARGEFILE_SOURCE
3163 newpos
&= ~((long long) (STREAM_BUFFER_SIZE
- 1)); /* sector boundary */
3165 newpos
&= ~(STREAM_BUFFER_SIZE
- 1); /* sector boundary */
3168 stream_seek(demuxer
->stream
, newpos
);
3169 for(i
= 0; i
< 8192; i
++)
3170 if(priv
->ts
.pids
[i
] != NULL
)
3171 priv
->ts
.pids
[i
]->is_synced
= 0;
3173 videobuf_code_len
= 0;
3175 if(sh_video
!= NULL
)
3176 ds_fill_buffer(d_video
);
3178 if(sh_audio
!= NULL
)
3180 ds_fill_buffer(d_audio
);
3183 while(sh_video
!= NULL
)
3185 if(sh_audio
&& !d_audio
->eof
&& d_video
->pts
&& d_audio
->pts
)
3187 float a_pts
=d_audio
->pts
;
3188 a_pts
+=(ds_tell_pts(d_audio
)-sh_audio
->a_in_buffer_len
)/(float)sh_audio
->i_bps
;
3189 if(d_video
->pts
> a_pts
)
3191 skip_audio_frame(sh_audio
); // sync audio
3197 i
= sync_video_packet(d_video
);
3198 if((sh_video
->format
== VIDEO_MPEG1
) || (sh_video
->format
== VIDEO_MPEG2
))
3200 if(i
==0x1B3 || i
==0x1B8) break; // found it!
3202 else if(sh_video
->format
== VIDEO_MPEG4
)
3204 if(i
==0x1B6) break; // found it!
3208 if((i
& ~0x60) == 0x101 || (i
& ~0x60) == 0x102 || (i
& ~0x60) == 0x105) break;
3211 if(!i
|| !skip_video_packet(d_video
)) break; // EOF?
3216 static int demux_ts_fill_buffer(demuxer_t
* demuxer
, demux_stream_t
*ds
)
3219 ts_priv_t
*priv
= (ts_priv_t
*)demuxer
->priv
;
3221 return -ts_parse(demuxer
, &es
, priv
->packet
, 0);
3225 static int ts_check_file_dmx(demuxer_t
*demuxer
)
3227 return ts_check_file(demuxer
) ? DEMUXER_TYPE_MPEG_TS
: 0;
3230 static int demux_ts_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
3232 ts_priv_t
* priv
= (ts_priv_t
*)demuxer
->priv
;
3236 case DEMUXER_CTRL_SWITCH_AUDIO
:
3238 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
3239 sh_audio_t
*sh_a
= NULL
;
3242 return DEMUXER_CTRL_NOTIMPL
;
3247 for(i
= 0; i
< 8192; i
++)
3249 if(priv
->ts
.streams
[i
].id
== demuxer
->audio
->id
&& priv
->ts
.streams
[i
].type
== TYPE_AUDIO
)
3256 if(priv
->ts
.streams
[i
].id
== demuxer
->audio
->id
) //we made a complete loop
3258 if(priv
->ts
.streams
[i
].type
== TYPE_AUDIO
)
3259 sh_a
= (sh_audio_t
*)priv
->ts
.streams
[i
].sh
;
3262 else if(n
<= priv
->last_aid
)
3264 for(i
= 0; i
< 8192; i
++)
3266 if(priv
->ts
.streams
[i
].id
== n
&& priv
->ts
.streams
[i
].type
== TYPE_AUDIO
)
3268 sh_a
= (sh_audio_t
*)priv
->ts
.streams
[i
].sh
;
3276 demuxer
->audio
->id
= priv
->ts
.streams
[i
].id
;
3277 demuxer
->audio
->sh
= sh_a
;
3278 ds_free_packs(demuxer
->audio
);
3279 mp_msg(MSGT_DEMUX
, MSGL_V
, "\r\ndemux_ts, switched to audio pid %d, id: %d, sh: %p\r\n", i
, demuxer
->audio
->id
, sh_a
);
3282 *((int*)arg
) = demuxer
->audio
->id
;
3283 return DEMUXER_CTRL_OK
;
3288 return DEMUXER_CTRL_NOTIMPL
;
3293 demuxer_desc_t demuxer_desc_mpeg_ts
= {
3299 DEMUXER_TYPE_MPEG_TS
,
3300 0, // unsafe autodetect
3302 demux_ts_fill_buffer
,