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
32 #include "stream/stream.h"
40 #define TS_PH_PACKET_SIZE 192
41 #define TS_FEC_PACKET_SIZE 204
42 #define TS_PACKET_SIZE 188
43 #define NB_PID_MAX 8192
45 #define MAX_HEADER_SIZE 6 /* enough for PES header + length */
46 #define MAX_CHECK_SIZE 65535
47 #define TS_MAX_PROBE_SIZE 2000000 /* do not forget to change this in cfg-common-opts.h, too */
48 #define NUM_CONSECUTIVE_TS_PACKETS 32
49 #define NUM_CONSECUTIVE_AUDIO_PACKETS 348
50 #define MAX_A52_FRAME_SIZE 3840
53 #define SIZE_MAX ((size_t)-1)
62 extern char *dvdsub_lang
, *audio_lang
; //for -alang
67 VIDEO_MPEG1
= 0x10000001,
68 VIDEO_MPEG2
= 0x10000002,
69 VIDEO_MPEG4
= 0x10000004,
70 VIDEO_H264
= 0x10000005,
71 VIDEO_AVC
= mmioFOURCC('a', 'v', 'c', '1'),
72 VIDEO_VC1
= mmioFOURCC('W', 'V', 'C', '1'),
76 AUDIO_LPCM_BE
= 0x10001,
77 AUDIO_AAC
= mmioFOURCC('M', 'P', '4', 'A'),
80 PES_PRIVATE1
= 0xBD00000,
81 SL_PES_STREAM
= 0xD000000,
82 SL_SECTION
= 0xD100000,
94 uint16_t payload_size
;
95 es_stream_type_t type
, subtype
;
99 int last_cc
; // last cc code (-1 if first packet)
101 ts_section_t section
;
103 int extradata_alloc
, extradata_len
;
105 uint8_t au_start
, au_end
, last_au_end
;
115 typedef struct MpegTSContext
{
116 int packet_size
; // raw packet size, including FEC if present e.g. 188 bytes
117 ES_stream_t
*pids
[NB_PID_MAX
];
118 sh_av_t streams
[NB_PID_MAX
];
124 demux_packet_t
*pack
;
125 int offset
, buffer_size
;
128 #define MAX_EXTRADATA_SIZE 64*1024
130 int32_t object_type
; //aka codec used
131 int32_t stream_type
; //video, audio etc.
132 uint8_t buf
[MAX_EXTRADATA_SIZE
];
135 } mp4_decoder_config_t
;
142 uint8_t random_accesspoint
;
143 uint8_t random_accesspoint_only
;
149 uint32_t ts_resolution
, ocr_resolution
;
150 uint8_t ts_len
, ocr_len
, au_len
, instant_bitrate_len
, degr_len
, au_seqnum_len
, packet_seqnum_len
;
152 uint16_t au_duration
, cts_duration
;
153 uint64_t ocr
, dts
, cts
;
159 mp4_decoder_config_t decoder
;
174 uint16_t section_length
;
176 uint8_t version_number
;
178 uint8_t section_number
;
179 uint8_t last_section_number
;
185 ts_section_t section
;
193 uint16_t section_length
;
194 uint8_t version_number
;
196 uint8_t section_number
;
197 uint8_t last_section_number
;
199 uint16_t prog_descr_length
;
200 ts_section_t section
;
204 uint32_t type
; //it's 8 bit long, but cast to the right type as FOURCC
205 uint16_t descr_length
;
206 uint8_t format_descriptor
[5];
211 mp4_es_descr_t
*mp4es
;
212 int od_cnt
, mp4es_cnt
;
225 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) || ((x) == VIDEO_VC1))
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
;
286 static int parse_avc_sps(uint8_t *buf
, int len
, int *w
, int *h
);
288 static void ts_add_stream(demuxer_t
* demuxer
, ES_stream_t
*es
)
291 ts_priv_t
*priv
= (ts_priv_t
*) demuxer
->priv
;
293 if(priv
->ts
.streams
[es
->pid
].sh
)
296 if((IS_AUDIO(es
->type
) || IS_AUDIO(es
->subtype
)) && priv
->last_aid
+1 < MAX_A_STREAMS
)
298 sh_audio_t
*sh
= new_sh_audio_aid(demuxer
, priv
->last_aid
, es
->pid
);
301 sh
->format
= IS_AUDIO(es
->type
) ? es
->type
: es
->subtype
;
302 sh
->ds
= demuxer
->audio
;
304 priv
->ts
.streams
[es
->pid
].id
= priv
->last_aid
;
305 priv
->ts
.streams
[es
->pid
].sh
= sh
;
306 priv
->ts
.streams
[es
->pid
].type
= TYPE_AUDIO
;
307 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
);
311 if(es
->extradata
&& es
->extradata_len
)
313 sh
->wf
= (WAVEFORMATEX
*) malloc(sizeof (WAVEFORMATEX
) + es
->extradata_len
);
314 sh
->wf
->cbSize
= es
->extradata_len
;
315 memcpy(sh
->wf
+ 1, es
->extradata
, es
->extradata_len
);
319 if((IS_VIDEO(es
->type
) || IS_VIDEO(es
->subtype
)) && priv
->last_vid
+1 < MAX_V_STREAMS
)
321 sh_video_t
*sh
= new_sh_video_vid(demuxer
, priv
->last_vid
, es
->pid
);
324 sh
->format
= IS_VIDEO(es
->type
) ? es
->type
: es
->subtype
;;
325 sh
->ds
= demuxer
->video
;
327 priv
->ts
.streams
[es
->pid
].id
= priv
->last_vid
;
328 priv
->ts
.streams
[es
->pid
].sh
= sh
;
329 priv
->ts
.streams
[es
->pid
].type
= TYPE_VIDEO
;
330 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
);
334 if(sh
->format
== VIDEO_AVC
&& es
->extradata
&& es
->extradata_len
)
337 sh
->bih
= (BITMAPINFOHEADER
*) calloc(1, sizeof(BITMAPINFOHEADER
) + es
->extradata_len
);
338 sh
->bih
->biSize
= sizeof(BITMAPINFOHEADER
) + es
->extradata_len
;
339 sh
->bih
->biCompression
= sh
->format
;
340 memcpy(sh
->bih
+ 1, es
->extradata
, es
->extradata_len
);
341 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "EXTRADATA(%d BYTES): \n", es
->extradata_len
);
342 for(i
= 0;i
< es
->extradata_len
; i
++)
343 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "%02x ", (int) es
->extradata
[i
]);
344 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
,"\n");
345 if(parse_avc_sps(es
->extradata
, es
->extradata_len
, &w
, &h
))
347 sh
->bih
->biWidth
= w
;
348 sh
->bih
->biHeight
= h
;
355 static int ts_check_file(demuxer_t
* demuxer
)
357 const int buf_size
= (TS_FEC_PACKET_SIZE
* NUM_CONSECUTIVE_TS_PACKETS
);
358 unsigned char buf
[TS_FEC_PACKET_SIZE
* NUM_CONSECUTIVE_TS_PACKETS
], done
= 0, *ptr
;
359 uint32_t _read
, i
, count
= 0, is_ts
;
360 int cc
[NB_PID_MAX
], last_cc
[NB_PID_MAX
], pid
, cc_ok
, c
, good
, bad
;
365 mp_msg(MSGT_DEMUX
, MSGL_V
, "Checking for MPEG-TS...\n");
367 init_pos
= stream_tell(demuxer
->stream
);
374 while(((c
=stream_read_char(demuxer
->stream
)) != 0x47)
376 && (i
< MAX_CHECK_SIZE
)
377 && ! demuxer
->stream
->eof
383 mp_msg(MSGT_DEMUX
, MSGL_V
, "THIS DOESN'T LOOK LIKE AN MPEG-TS FILE!\n");
389 pos
= stream_tell(demuxer
->stream
) - 1;
391 _read
= stream_read(demuxer
->stream
, &buf
[1], buf_size
-1);
393 if(_read
< buf_size
-1)
395 mp_msg(MSGT_DEMUX
, MSGL_V
, "COULDN'T READ ENOUGH DATA, EXITING TS_CHECK\n");
396 stream_reset(demuxer
->stream
);
400 size
= get_packet_size(buf
, buf_size
);
407 if(pos
- init_pos
>= MAX_CHECK_SIZE
)
414 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
);
415 stream_seek(demuxer
->stream
, pos
);
420 //LET'S CHECK continuity counters
422 for(count
= 0; count
< NB_PID_MAX
; count
++)
424 cc
[count
] = last_cc
[count
] = -1;
427 for(count
= 0; count
< NUM_CONSECUTIVE_TS_PACKETS
; count
++)
429 ptr
= &(buf
[size
* count
]);
430 pid
= ((ptr
[1] & 0x1f) << 8) | ptr
[2];
431 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "BUF: %02x %02x %02x %02x, PID %d, SIZE: %d \n",
432 ptr
[0], ptr
[1], ptr
[2], ptr
[3], pid
, size
);
434 if((pid
== 8191) || (pid
< 16))
437 cc
[pid
] = (ptr
[3] & 0xf);
438 cc_ok
= (last_cc
[pid
] < 0) || ((((last_cc
[pid
] + 1) & 0x0f) == cc
[pid
]));
439 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "PID %d, COMPARE CC %d AND LAST_CC %d\n", pid
, cc
[pid
], last_cc
[pid
]);
446 last_cc
[pid
] = cc
[pid
];
449 mp_msg(MSGT_DEMUX
, MSGL_V
, "GOOD CC: %d, BAD CC: %d\n", good
, bad
);
458 static inline int32_t progid_idx_in_pmt(ts_priv_t
*priv
, uint16_t progid
)
462 if(priv
->pmt
== NULL
)
465 for(x
= 0; x
< priv
->pmt_cnt
; x
++)
467 if(priv
->pmt
[x
].progid
== progid
)
475 static inline int32_t progid_for_pid(ts_priv_t
*priv
, int pid
, int32_t req
) //finds the first program listing a pid
481 if(priv
->pmt
== NULL
)
485 for(i
=0; i
< priv
->pmt_cnt
; i
++)
487 pmt
= &(priv
->pmt
[i
]);
492 for(j
= 0; j
< pmt
->es_cnt
; j
++)
494 if(pmt
->es
[j
].pid
== pid
)
496 if((req
== 0) || (req
== pmt
->progid
))
505 static inline int32_t prog_pcr_pid(ts_priv_t
*priv
, int progid
)
509 if(priv
->pmt
== NULL
)
511 for(i
=0; i
< priv
->pmt_cnt
; i
++)
513 if(priv
->pmt
[i
].progid
== progid
)
514 return priv
->pmt
[i
].PCR_PID
;
520 static inline int pid_match_lang(ts_priv_t
*priv
, uint16_t pid
, char *lang
)
525 if(priv
->pmt
== NULL
)
528 for(i
=0; i
< priv
->pmt_cnt
; i
++)
530 pmt
= &(priv
->pmt
[i
]);
535 for(j
= 0; j
< pmt
->es_cnt
; j
++)
537 if(pmt
->es
[j
].pid
!= pid
)
540 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
);
541 if(strncmp(pmt
->es
[j
].lang
, lang
, 3) == 0)
553 int32_t atype
, vtype
, stype
; //types
554 int32_t apid
, vpid
, spid
; //stream ids
555 char slang
[4], alang
[4]; //languages
560 //stripped down version of a52_syncinfo() from liba52
561 //copyright belongs to Michel Lespinasse <walken@zoy.org> and Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
562 int mp_a52_framesize(uint8_t * buf
, int *srate
)
564 int rate
[] = { 32, 40, 48, 56, 64, 80, 96, 112,
565 128, 160, 192, 224, 256, 320, 384, 448,
568 uint8_t halfrate
[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
569 int frmsizecod
, bitrate
, half
;
571 if((buf
[0] != 0x0b) || (buf
[1] != 0x77)) /* syncword */
574 if(buf
[5] >= 0x60) /* bsid >= 12 */
577 half
= halfrate
[buf
[5] >> 3];
579 frmsizecod
= buf
[4] & 63;
583 bitrate
= rate
[frmsizecod
>> 1];
585 switch(buf
[4] & 0xc0)
588 *srate
= 48000 >> half
;
590 case 0x40: /* 44.1 KHz */
591 *srate
= 44100 >> half
;
592 return 2 * (320 * bitrate
/ 147 + (frmsizecod
& 1));
593 case 0x80: /* 32 KHz */
594 *srate
= 32000 >> half
;
601 //second stage: returns the count of A52 syncwords found
602 static int a52_check(char *buf
, int len
)
604 int cnt
, frame_length
, ok
, srate
;
612 if(buf
[cnt
] == 0x0B && buf
[cnt
+1] == 0x77)
614 frame_length
= mp_a52_framesize(&buf
[cnt
], &srate
);
615 if(frame_length
>=7 && frame_length
<=3840)
627 mp_msg(MSGT_DEMUXER
, MSGL_V
, "A52_CHECK(%d input bytes), found %d frame syncwords of %d bytes length\n", len
, ok
, frame_length
);
632 static off_t
ts_detect_streams(demuxer_t
*demuxer
, tsdemux_init_t
*param
)
634 int video_found
= 0, audio_found
= 0, sub_found
= 0, i
, num_packets
= 0, req_apid
, req_vpid
, req_spid
;
635 int is_audio
, is_video
, is_sub
, has_tables
;
636 int32_t p
, chosen_pid
= 0;
637 off_t pos
=0, ret
= 0, init_pos
, end_pos
;
639 unsigned char tmp
[TS_FEC_PACKET_SIZE
];
640 ts_priv_t
*priv
= (ts_priv_t
*) demuxer
->priv
;
644 } pes_priv1
[8192], *pptr
;
647 priv
->last_pid
= 8192; //invalid pid
649 req_apid
= param
->apid
;
650 req_vpid
= param
->vpid
;
651 req_spid
= param
->spid
;
654 memset(pes_priv1
, 0, sizeof(pes_priv1
));
655 init_pos
= stream_tell(demuxer
->stream
);
656 mp_msg(MSGT_DEMUXER
, MSGL_V
, "PROBING UP TO %"PRIu64
", PROG: %d\n", (uint64_t) param
->probe
, param
->prog
);
657 end_pos
= init_pos
+ (param
->probe
? param
->probe
: TS_MAX_PROBE_SIZE
);
660 pos
= stream_tell(demuxer
->stream
);
661 if(pos
> end_pos
|| demuxer
->stream
->eof
)
664 if(ts_parse(demuxer
, &es
, tmp
, 1))
666 //Non PES-aligned A52 audio may escape detection if PMT is not present;
667 //in this case we try to find at least 3 A52 syncwords
668 if((es
.type
== PES_PRIVATE1
) && (! audio_found
) && req_apid
> -2)
670 pptr
= &pes_priv1
[es
.pid
];
671 if(pptr
->pos
< 64*1024)
673 tmpbuf
= (char*) realloc(pptr
->buf
, pptr
->pos
+ es
.size
);
677 memcpy(&(pptr
->buf
[ pptr
->pos
]), es
.start
, es
.size
);
678 pptr
->pos
+= es
.size
;
679 if(a52_check(pptr
->buf
, pptr
->pos
) > 2)
681 param
->atype
= AUDIO_A52
;
682 param
->apid
= es
.pid
;
689 is_audio
= IS_AUDIO(es
.type
) || ((es
.type
==SL_PES_STREAM
) && IS_AUDIO(es
.subtype
));
690 is_video
= IS_VIDEO(es
.type
) || ((es
.type
==SL_PES_STREAM
) && IS_VIDEO(es
.subtype
));
691 is_sub
= ((es
.type
== SPU_DVD
) || (es
.type
== SPU_DVB
));
694 if((! is_audio
) && (! is_video
) && (! is_sub
))
696 if(is_audio
&& req_apid
==-2)
701 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_VIDEO_ID=%d\n", es
.pid
);
702 chosen_pid
= (req_vpid
== es
.pid
);
703 if((! chosen_pid
) && (req_vpid
> 0))
708 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_AUDIO_ID=%d\n", es
.pid
);
710 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_AID_%d_LANG=%s\n", es
.pid
, es
.lang
);
713 chosen_pid
= (req_apid
== es
.pid
);
717 else if(param
->alang
[0] > 0 && es
.lang
[0] > 0)
719 if(pid_match_lang(priv
, es
.pid
, param
->alang
) == -1)
723 param
->apid
= req_apid
= es
.pid
;
728 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_SUBTITLE_ID=%d\n", es
.pid
);
730 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_SID_%d_LANG=%s\n", es
.pid
, es
.lang
);
731 chosen_pid
= (req_spid
== es
.pid
);
732 if((! chosen_pid
) && (req_spid
> 0))
736 if(req_apid
< 0 && (param
->alang
[0] == 0) && req_vpid
< 0 && req_spid
< 0)
739 if((ret
== 0) && chosen_pid
)
741 ret
= stream_tell(demuxer
->stream
);
744 p
= progid_for_pid(priv
, es
.pid
, param
->prog
);
748 if(!param
->prog
&& chosen_pid
)
752 if((param
->prog
> 0) && (param
->prog
!= p
))
756 if(is_video
&& (req_vpid
== es
.pid
))
758 param
->vtype
= IS_VIDEO(es
.type
) ? es
.type
: es
.subtype
;
759 param
->vpid
= es
.pid
;
767 if(is_audio
&& (req_apid
== es
.pid
))
769 param
->atype
= IS_AUDIO(es
.type
) ? es
.type
: es
.subtype
;
770 param
->apid
= es
.pid
;
781 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "TYPE: %x, PID: %d, PROG FOUND: %d\n", es
.type
, es
.pid
, param
->prog
);
786 if((req_vpid
== -1) || (req_vpid
== es
.pid
))
788 param
->vtype
= IS_VIDEO(es
.type
) ? es
.type
: es
.subtype
;
789 param
->vpid
= es
.pid
;
795 if(((req_vpid
== -2) || (num_packets
>= NUM_CONSECUTIVE_AUDIO_PACKETS
)) && audio_found
&& !param
->probe
)
797 //novideo or we have at least 348 audio packets (64 KB) without video (TS with audio only)
804 if((req_spid
== -1) || (req_spid
== es
.pid
))
806 param
->stype
= es
.type
;
807 param
->spid
= es
.pid
;
814 if((req_apid
== -1) || (req_apid
== es
.pid
))
816 param
->atype
= IS_AUDIO(es
.type
) ? es
.type
: es
.subtype
;
817 param
->apid
= es
.pid
;
822 if(audio_found
&& (param
->apid
== es
.pid
) && (! video_found
))
825 if((has_tables
==0) && (video_found
&& audio_found
) && (pos
>= 1000000))
830 for(i
=0; i
<8192; i
++)
832 if(pes_priv1
[i
].buf
!= NULL
)
834 free(pes_priv1
[i
].buf
);
835 pes_priv1
[i
].buf
= NULL
;
836 pes_priv1
[i
].pos
= 0;
842 if(param
->vtype
== VIDEO_MPEG1
)
843 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "VIDEO MPEG1(pid=%d) ", param
->vpid
);
844 else if(param
->vtype
== VIDEO_MPEG2
)
845 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "VIDEO MPEG2(pid=%d) ", param
->vpid
);
846 else if(param
->vtype
== VIDEO_MPEG4
)
847 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "VIDEO MPEG4(pid=%d) ", param
->vpid
);
848 else if(param
->vtype
== VIDEO_H264
)
849 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "VIDEO H264(pid=%d) ", param
->vpid
);
850 else if(param
->vtype
== VIDEO_VC1
)
851 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "VIDEO VC1(pid=%d) ", param
->vpid
);
852 else if(param
->vtype
== VIDEO_AVC
)
853 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "VIDEO AVC(NAL-H264, pid=%d) ", param
->vpid
);
857 param
->vtype
= UNKNOWN
;
858 //WE DIDN'T MATCH ANY VIDEO STREAM
859 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "NO VIDEO! ");
862 if(param
->atype
== AUDIO_MP2
)
863 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "AUDIO MPA(pid=%d)", param
->apid
);
864 else if(param
->atype
== AUDIO_A52
)
865 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "AUDIO A52(pid=%d)", param
->apid
);
866 else if(param
->atype
== AUDIO_DTS
)
867 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "AUDIO DTS(pid=%d)", param
->apid
);
868 else if(param
->atype
== AUDIO_LPCM_BE
)
869 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "AUDIO LPCM(pid=%d)", param
->apid
);
870 else if(param
->atype
== AUDIO_AAC
)
871 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "AUDIO AAC(pid=%d)", param
->apid
);
875 param
->atype
= UNKNOWN
;
876 //WE DIDN'T MATCH ANY AUDIO STREAM, SO WE FORCE THE DEMUXER TO IGNORE AUDIO
877 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "NO AUDIO! ");
880 if(param
->stype
== SPU_DVD
|| param
->stype
== SPU_DVB
)
881 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " SUB %s(pid=%d) ", (param
->stype
==SPU_DVD
? "DVD" : "DVB"), param
->spid
);
884 param
->stype
= UNKNOWN
;
885 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " NO SUBS (yet)! ");
888 if(video_found
|| audio_found
)
892 p
= progid_for_pid(priv
, video_found
? param
->vpid
: param
->apid
, 0);
897 if(demuxer
->stream
->eof
&& (ret
== 0))
899 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " PROGRAM N. %d\n", param
->prog
);
902 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "\n");
905 for(i
=0; i
<8192; i
++)
907 if(priv
->ts
.pids
[i
] != NULL
)
909 priv
->ts
.pids
[i
]->payload_size
= 0;
910 priv
->ts
.pids
[i
]->pts
= priv
->ts
.pids
[i
]->last_pts
= 0;
911 priv
->ts
.pids
[i
]->last_cc
= -1;
912 priv
->ts
.pids
[i
]->is_synced
= 0;
919 static int parse_avc_sps(uint8_t *buf
, int len
, int *w
, int *h
)
923 mp_mpeg_header_t picture
;
929 sps_len
= (buf
[6] << 8) | buf
[7];
930 if(!sps_len
|| (sps_len
> len
- 8))
933 picture
.display_picture_width
= picture
.display_picture_height
= 0;
934 h264_parse_sps(&picture
, ptr
, len
- 8);
935 if(!picture
.display_picture_width
|| !picture
.display_picture_height
)
937 *w
= picture
.display_picture_width
;
938 *h
= picture
.display_picture_height
;
942 static demuxer_t
*demux_open_ts(demuxer_t
* demuxer
)
946 sh_video_t
*sh_video
;
947 sh_audio_t
*sh_audio
;
949 tsdemux_init_t params
;
950 ts_priv_t
* priv
= demuxer
->priv
;
952 mp_msg(MSGT_DEMUX
, MSGL_V
, "DEMUX OPEN, AUDIO_ID: %d, VIDEO_ID: %d, SUBTITLE_ID: %d,\n",
953 demuxer
->audio
->id
, demuxer
->video
->id
, demuxer
->sub
->id
);
956 demuxer
->type
= DEMUXER_TYPE_MPEG_TS
;
959 stream_reset(demuxer
->stream
);
961 packet_size
= ts_check_file(demuxer
);
965 priv
= calloc(1, sizeof(ts_priv_t
));
968 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "DEMUX_OPEN_TS, couldn't allocate enough memory for ts->priv, exit\n");
972 for(i
=0; i
< 8192; i
++)
974 priv
->ts
.pids
[i
] = NULL
;
975 priv
->ts
.streams
[i
].id
= -3;
977 priv
->pat
.progs
= NULL
;
978 priv
->pat
.progs_cnt
= 0;
979 priv
->pat
.section
.buffer
= NULL
;
980 priv
->pat
.section
.buffer_len
= 0;
985 priv
->keep_broken
= ts_keep_broken
;
986 priv
->ts
.packet_size
= packet_size
;
989 demuxer
->priv
= priv
;
990 if(demuxer
->stream
->type
!= STREAMTYPE_FILE
)
991 demuxer
->seekable
= 1;
993 demuxer
->seekable
= 1;
996 params
.atype
= params
.vtype
= params
.stype
= UNKNOWN
;
997 params
.apid
= demuxer
->audio
->id
;
998 params
.vpid
= demuxer
->video
->id
;
999 params
.spid
= demuxer
->sub
->id
;
1000 params
.prog
= ts_prog
;
1001 params
.probe
= ts_probe
;
1003 if(dvdsub_lang
!= NULL
)
1005 strncpy(params
.slang
, dvdsub_lang
, 3);
1006 params
.slang
[3] = 0;
1009 memset(params
.slang
, 0, 4);
1011 if(audio_lang
!= NULL
)
1013 strncpy(params
.alang
, audio_lang
, 3);
1014 params
.alang
[3] = 0;
1017 memset(params
.alang
, 0, 4);
1019 start_pos
= ts_detect_streams(demuxer
, ¶ms
);
1021 demuxer
->sub
->id
= params
.spid
;
1022 priv
->prog
= params
.prog
;
1024 if(params
.vtype
!= UNKNOWN
)
1026 ts_add_stream(demuxer
, priv
->ts
.pids
[params
.vpid
]);
1027 sh_video
= priv
->ts
.streams
[params
.vpid
].sh
;
1028 demuxer
->video
->id
= priv
->ts
.streams
[params
.vpid
].id
;
1029 sh_video
->ds
= demuxer
->video
;
1030 sh_video
->format
= params
.vtype
;
1031 demuxer
->video
->sh
= sh_video
;
1034 if(params
.atype
!= UNKNOWN
)
1036 ES_stream_t
*es
= priv
->ts
.pids
[params
.apid
];
1038 if(!IS_AUDIO(es
->type
) && !IS_AUDIO(es
->subtype
) && IS_AUDIO(params
.atype
)) es
->subtype
= params
.atype
;
1039 ts_add_stream(demuxer
, priv
->ts
.pids
[params
.apid
]);
1040 sh_audio
= priv
->ts
.streams
[params
.apid
].sh
;
1041 demuxer
->audio
->id
= priv
->ts
.streams
[params
.apid
].id
;
1042 sh_audio
->ds
= demuxer
->audio
;
1043 sh_audio
->format
= params
.atype
;
1044 demuxer
->audio
->sh
= sh_audio
;
1048 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
);
1051 start_pos
= (start_pos
<= priv
->ts
.packet_size
? 0 : start_pos
- priv
->ts
.packet_size
);
1052 demuxer
->movi_start
= start_pos
;
1053 demuxer
->reference_clock
= MP_NOPTS_VALUE
;
1054 stream_reset(demuxer
->stream
);
1055 stream_seek(demuxer
->stream
, start_pos
); //IF IT'S FROM A PIPE IT WILL FAIL, BUT WHO CARES?
1058 priv
->last_pid
= 8192; //invalid pid
1060 for(i
= 0; i
< 3; i
++)
1062 priv
->fifo
[i
].pack
= NULL
;
1063 priv
->fifo
[i
].offset
= 0;
1065 priv
->fifo
[0].ds
= demuxer
->audio
;
1066 priv
->fifo
[1].ds
= demuxer
->video
;
1067 priv
->fifo
[2].ds
= demuxer
->sub
;
1069 priv
->fifo
[0].buffer_size
= 1536;
1070 priv
->fifo
[1].buffer_size
= 32767;
1071 priv
->fifo
[2].buffer_size
= 32767;
1073 priv
->pat
.section
.buffer_len
= 0;
1074 for(i
= 0; i
< priv
->pmt_cnt
; i
++)
1075 priv
->pmt
[i
].section
.buffer_len
= 0;
1077 demuxer
->filepos
= stream_tell(demuxer
->stream
);
1081 static void demux_close_ts(demuxer_t
* demuxer
)
1084 ts_priv_t
*priv
= (ts_priv_t
*) demuxer
->priv
;
1088 if(priv
->pat
.section
.buffer
)
1089 free(priv
->pat
.section
.buffer
);
1091 free(priv
->pat
.progs
);
1095 for(i
= 0; i
< priv
->pmt_cnt
; i
++)
1097 if(priv
->pmt
[i
].section
.buffer
)
1098 free(priv
->pmt
[i
].section
.buffer
);
1100 free(priv
->pmt
[i
].es
);
1110 extern unsigned char mp_getbits(unsigned char*, unsigned int, unsigned char);
1111 #define getbits mp_getbits
1113 static int mp4_parse_sl_packet(pmt_t
*pmt
, uint8_t *buf
, uint16_t packet_len
, int pid
, ES_stream_t
*pes_es
)
1115 int i
, n
, m
, mp4_es_id
= -1;
1117 uint32_t pl_size
= 0;
1119 mp4_es_descr_t
*es
= NULL
;
1120 mp4_sl_config_t
*sl
= NULL
;
1121 uint8_t au_start
= 0, au_end
= 0, rap_flag
= 0, ocr_flag
= 0, padding
= 0, padding_bits
= 0, idle
= 0;
1123 pes_es
->is_synced
= 0;
1124 mp_msg(MSGT_DEMUXER
,MSGL_V
, "mp4_parse_sl_packet, pid: %d, pmt: %pm, packet_len: %d\n", pid
, pmt
, packet_len
);
1125 if(! pmt
|| !packet_len
)
1128 for(i
= 0; i
< pmt
->es_cnt
; i
++)
1130 if(pmt
->es
[i
].pid
== pid
)
1131 mp4_es_id
= pmt
->es
[i
].mp4_es_id
;
1136 for(i
= 0; i
< pmt
->mp4es_cnt
; i
++)
1138 if(pmt
->mp4es
[i
].id
== mp4_es_id
)
1139 es
= &(pmt
->mp4es
[i
]);
1144 pes_es
->subtype
= es
->decoder
.object_type
;
1150 //now es is the complete es_descriptor of out mp4 ES stream
1151 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "ID: %d, FLAGS: 0x%x, subtype: %x\n", es
->id
, sl
->flags
, pes_es
->subtype
);
1155 pes_es
->sl
.au_start
= au_start
= getbits(buf
, n
++, 1);
1157 pes_es
->sl
.au_start
= (pes_es
->sl
.last_au_end
? 1 : 0);
1159 pes_es
->sl
.au_end
= au_end
= getbits(buf
, n
++, 1);
1161 if(!sl
->au_start
&& !sl
->au_end
)
1163 pes_es
->sl
.au_start
= pes_es
->sl
.au_end
= au_start
= au_end
= 1;
1165 pes_es
->sl
.last_au_end
= pes_es
->sl
.au_end
;
1169 ocr_flag
= getbits(buf
, n
++, 1);
1171 idle
= getbits(buf
, n
++, 1);
1173 padding
= getbits(buf
, n
++, 1);
1176 padding_bits
= getbits(buf
, n
, 3);
1180 if(idle
|| (padding
&& !padding_bits
))
1182 pes_es
->payload_size
= 0;
1186 //(! idle && (!padding || padding_bits != 0)) is true
1187 n
+= sl
->packet_seqnum_len
;
1189 deg_flag
= getbits(buf
, n
++, 1);
1196 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "OCR: %d bits\n", sl
->ocr_len
);
1199 if(packet_len
* 8 <= n
)
1202 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "\nAU_START: %d, AU_END: %d\n", au_start
, au_end
);
1205 int dts_flag
= 0, cts_flag
= 0, ib_flag
= 0;
1207 if(sl
->random_accesspoint
)
1208 rap_flag
= getbits(buf
, n
++, 1);
1210 //check commented because it seems it's rarely used, and we need this flag set in case of au_start
1211 //the decoder will eventually discard the payload if it can't decode it
1212 //if(rap_flag || sl->random_accesspoint_only)
1213 pes_es
->is_synced
= 1;
1215 n
+= sl
->au_seqnum_len
;
1216 if(packet_len
* 8 <= n
+8)
1220 dts_flag
= getbits(buf
, n
++, 1);
1221 cts_flag
= getbits(buf
, n
++, 1);
1223 if(sl
->instant_bitrate_len
)
1224 ib_flag
= getbits(buf
, n
++, 1);
1225 if(packet_len
* 8 <= n
+8)
1227 if(dts_flag
&& (sl
->ts_len
> 0))
1230 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "DTS: %d bits\n", sl
->ts_len
);
1232 if(packet_len
* 8 <= n
+8)
1234 if(cts_flag
&& (sl
->ts_len
> 0))
1238 while(i
< sl
->ts_len
)
1240 m
= FFMIN(8, sl
->ts_len
- i
);
1241 v
|= getbits(buf
, n
, m
);
1242 if(sl
->ts_len
- i
> 8)
1246 if(packet_len
* 8 <= n
+8)
1250 pes_es
->pts
= (float) v
/ (float) sl
->ts_resolution
;
1251 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "CTS: %d bits, value: %"PRIu64
"/%d = %.3f\n", sl
->ts_len
, v
, sl
->ts_resolution
, pes_es
->pts
);
1257 while(i
< sl
->au_len
)
1259 m
= FFMIN(8, sl
->au_len
- i
);
1260 pl_size
|= getbits(buf
, n
, m
);
1261 if(sl
->au_len
- i
> 8)
1265 if(packet_len
* 8 <= n
+8)
1268 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
, "AU_LEN: %u (%d bits)\n", pl_size
, sl
->au_len
);
1270 n
+= sl
->instant_bitrate_len
;
1274 if(0 < pl_size
&& pl_size
< pes_es
->payload_size
)
1275 pes_es
->payload_size
= pl_size
;
1277 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",
1278 n
, m
, pes_es
->payload_size
, pl_size
, (int) rap_flag
, (int) sl
->random_accesspoint_only
);
1283 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
)
1286 uint32_t header_len
;
1289 uint32_t pkt_len
, pes_is_aligned
;
1291 //Here we are always at the start of a PES packet
1292 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2(%p, %d): \n", buf
, (uint32_t) packet_len
);
1294 if(packet_len
== 0 || packet_len
> 184)
1296 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2, BUFFER LEN IS TOO SMALL OR TOO BIG: %d EXIT\n", packet_len
);
1301 pkt_len
= packet_len
;
1304 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2: HEADER %02x %02x %02x %02x\n", p
[0], p
[1], p
[2], p
[3]);
1305 if (p
[0] || p
[1] || (p
[2] != 1))
1307 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2: error HEADER %02x %02x %02x (should be 0x000001) \n", p
[0], p
[1], p
[2]);
1314 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2: packet too short: %d, exit\n", packet_len
);
1318 es
->payload_size
= (p
[4] << 8 | p
[5]);
1319 pes_is_aligned
= (p
[6] & 4);
1325 { /* pts available */
1326 pts
= (int64_t)(p
[9] & 0x0E) << 29 ;
1327 pts
|= p
[10] << 22 ;
1328 pts
|= (p
[11] & 0xFE) << 14 ;
1330 pts
|= (p
[13] & 0xFE) >> 1 ;
1332 es
->pts
= pts
/ 90000.0f
;
1341 if (header_len
+ 9 > pkt_len
) //9 are the bytes read up to the header_length field
1343 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "demux_ts: illegal value for PES_header_data_length (0x%02x)\n", header_len
);
1347 p
+= header_len
+ 9;
1348 packet_len
-= header_len
+ 3;
1350 if(es
->payload_size
)
1351 es
->payload_size
-= header_len
+ 3;
1354 es
->is_synced
= 1; //only for SL streams we have to make sure it's really true, see below
1355 if (stream_id
== 0xbd)
1357 mp_msg(MSGT_DEMUX
, MSGL_DBG3
, "pes_parse2: audio buf = %02X %02X %02X %02X %02X %02X %02X %02X, 80: %d\n",
1358 p
[0], p
[1], p
[2], p
[3], p
[4], p
[5], p
[6], p
[7], p
[0] & 0x80);
1362 * we check the descriptor tag first because some stations
1363 * do not include any of the A52 header info in their audio tracks
1364 * these "raw" streams may begin with a byte that looks like a stream type.
1369 (type_from_pmt
== AUDIO_A52
) || /* A52 - raw */
1370 (p
[0] == 0x0B && p
[1] == 0x77) /* A52 - syncword */
1373 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "A52 RAW OR SYNCWORD\n");
1375 es
->size
= packet_len
;
1376 es
->type
= AUDIO_A52
;
1377 es
->payload_size
-= packet_len
;
1382 else if(type_from_pmt
== SPU_DVB
||
1383 ((p
[0] == 0x20) && pes_is_aligned
)) // && p[1] == 0x00))
1386 es
->size
= packet_len
;
1388 es
->payload_size
-= packet_len
;
1392 else if (pes_is_aligned
&& ((p
[0] & 0xE0) == 0x20)) //SPU_DVD
1396 es
->size
= packet_len
-1;
1398 es
->payload_size
-= packet_len
;
1402 else if (pes_is_aligned
&& (p
[0] & 0xF8) == 0x80)
1404 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "A52 WITH HEADER\n");
1406 es
->size
= packet_len
- 4;
1407 es
->type
= AUDIO_A52
;
1408 es
->payload_size
-= packet_len
;
1412 else if (pes_is_aligned
&& ((p
[0]&0xf0) == 0xa0))
1416 for (pcm_offset
=0; ++pcm_offset
< packet_len
-1 ; )
1418 if (p
[pcm_offset
] == 0x01 && p
[pcm_offset
+1] == 0x80)
1425 es
->start
= p
+ pcm_offset
;
1426 es
->size
= packet_len
- pcm_offset
;
1427 es
->type
= AUDIO_LPCM_BE
;
1428 es
->payload_size
-= packet_len
;
1434 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "PES_PRIVATE1\n");
1436 es
->size
= packet_len
;
1437 es
->type
= (type_from_pmt
== UNKNOWN
? PES_PRIVATE1
: type_from_pmt
);
1438 es
->payload_size
-= packet_len
;
1443 else if(((stream_id
>= 0xe0) && (stream_id
<= 0xef)) || (stream_id
== 0xfd && type_from_pmt
!= UNKNOWN
))
1446 es
->size
= packet_len
;
1447 if(type_from_pmt
!= UNKNOWN
)
1448 es
->type
= type_from_pmt
;
1450 es
->type
= VIDEO_MPEG2
;
1451 if(es
->payload_size
)
1452 es
->payload_size
-= packet_len
;
1454 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2: M2V size %d\n", es
->size
);
1457 else if ((stream_id
== 0xfa))
1462 if(type_from_pmt
!= UNKNOWN
) //MP4 A/V or SL
1465 es
->size
= packet_len
;
1466 es
->type
= type_from_pmt
;
1468 if(type_from_pmt
== SL_PES_STREAM
)
1470 //if(pes_is_aligned)
1472 l
= mp4_parse_sl_packet(pmt
, p
, packet_len
, pid
, es
);
1473 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "L=%d, TYPE=%x\n", l
, type_from_pmt
);
1476 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2: couldn't parse SL header, passing along full PES payload\n");
1485 if(es
->payload_size
)
1486 es
->payload_size
-= packet_len
;
1490 else if ((stream_id
& 0xe0) == 0xc0)
1493 es
->size
= packet_len
;
1495 if(type_from_pmt
!= UNKNOWN
)
1496 es
->type
= type_from_pmt
;
1498 es
->type
= AUDIO_MP2
;
1500 es
->payload_size
-= packet_len
;
1504 else if (type_from_pmt
!= -1) //as a last resort here we trust the PMT, if present
1507 es
->size
= packet_len
;
1508 es
->type
= type_from_pmt
;
1509 es
->payload_size
-= packet_len
;
1515 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "pes_parse2: unknown packet, id: %x\n", stream_id
);
1525 static int ts_sync(stream_t
*stream
)
1529 mp_msg(MSGT_DEMUX
, MSGL_DBG3
, "TS_SYNC \n");
1531 while(((c
=stream_read_char(stream
)) != 0x47) && ! stream
->eof
);
1540 static void ts_dump_streams(ts_priv_t
*priv
)
1544 for(i
= 0; i
< 3; i
++)
1546 if((priv
->fifo
[i
].pack
!= NULL
) && (priv
->fifo
[i
].offset
!= 0))
1548 resize_demux_packet(priv
->fifo
[i
].pack
, priv
->fifo
[i
].offset
);
1549 ds_add_packet(priv
->fifo
[i
].ds
, priv
->fifo
[i
].pack
);
1550 priv
->fifo
[i
].offset
= 0;
1551 priv
->fifo
[i
].pack
= NULL
;
1557 static inline int32_t prog_idx_in_pat(ts_priv_t
*priv
, uint16_t progid
)
1561 if(priv
->pat
.progs
== NULL
)
1564 for(x
= 0; x
< priv
->pat
.progs_cnt
; x
++)
1566 if(priv
->pat
.progs
[x
].id
== progid
)
1574 static inline int32_t prog_id_in_pat(ts_priv_t
*priv
, uint16_t pid
)
1578 if(priv
->pat
.progs
== NULL
)
1581 for(x
= 0; x
< priv
->pat
.progs_cnt
; x
++)
1583 if(priv
->pat
.progs
[x
].pmt_pid
== pid
)
1584 return priv
->pat
.progs
[x
].id
;
1590 static int collect_section(ts_section_t
*section
, int is_start
, unsigned char *buff
, int size
)
1596 mp_msg(MSGT_DEMUX
, MSGL_V
, "COLLECT_SECTION, start: %d, size: %d, collected: %d\n", is_start
, size
, section
->buffer_len
);
1597 if(! is_start
&& !section
->buffer_len
)
1602 if(! section
->buffer
)
1604 section
->buffer
= (uint8_t*) malloc(4096+256);
1605 if(section
->buffer
== NULL
)
1608 section
->buffer_len
= 0;
1611 if(size
+ section
->buffer_len
> 4096+256)
1613 mp_msg(MSGT_DEMUX
, MSGL_V
, "COLLECT_SECTION, excessive len: %d + %d\n", section
->buffer_len
, size
);
1617 memcpy(&(section
->buffer
[section
->buffer_len
]), buff
, size
);
1618 section
->buffer_len
+= size
;
1620 if(section
->buffer_len
< 3)
1623 skip
= section
->buffer
[0];
1624 if(skip
+ 4 > section
->buffer_len
)
1627 ptr
= &(section
->buffer
[skip
+ 1]);
1629 tlen
= ((ptr
[1] & 0x0f) << 8) | ptr
[2];
1630 mp_msg(MSGT_DEMUX
, MSGL_V
, "SKIP: %d+1, TID: %d, TLEN: %d, COLLECTED: %d\n", skip
, tid
, tlen
, section
->buffer_len
);
1631 if(section
->buffer_len
< (skip
+1+3+tlen
))
1633 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "DATA IS NOT ENOUGH, NEXT TIME\n");
1640 static int parse_pat(ts_priv_t
* priv
, int is_start
, unsigned char *buff
, int size
)
1644 unsigned char *base
;
1647 struct pat_progs_t
*tmp
;
1648 ts_section_t
*section
;
1650 section
= &(priv
->pat
.section
);
1651 skip
= collect_section(section
, is_start
, buff
, size
);
1655 ptr
= &(section
->buffer
[skip
]);
1657 priv
->pat
.table_id
= ptr
[0];
1658 if(priv
->pat
.table_id
!= 0)
1660 priv
->pat
.ssi
= (ptr
[1] >> 7) & 0x1;
1661 priv
->pat
.curr_next
= ptr
[5] & 0x01;
1662 priv
->pat
.ts_id
= (ptr
[3] << 8 ) | ptr
[4];
1663 priv
->pat
.version_number
= (ptr
[5] >> 1) & 0x1F;
1664 priv
->pat
.section_length
= ((ptr
[1] & 0x03) << 8 ) | ptr
[2];
1665 priv
->pat
.section_number
= ptr
[6];
1666 priv
->pat
.last_section_number
= ptr
[7];
1668 //check_crc32(0xFFFFFFFFL, ptr, priv->pat.buffer_len - 4, &ptr[priv->pat.buffer_len - 4]);
1669 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
);
1671 entries
= (int) (priv
->pat
.section_length
- 9) / 4; //entries per section
1673 for(i
=0; i
< entries
; i
++)
1676 base
= &ptr
[8 + i
*4];
1677 progid
= (base
[0] << 8) | base
[1];
1679 if((idx
= prog_idx_in_pat(priv
, progid
)) == -1)
1681 int sz
= sizeof(struct pat_progs_t
) * (priv
->pat
.progs_cnt
+1);
1682 tmp
= realloc_struct(priv
->pat
.progs
, priv
->pat
.progs_cnt
+1, sizeof(struct pat_progs_t
));
1685 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "PARSE_PAT: COULDN'T REALLOC %d bytes, NEXT\n", sz
);
1688 priv
->pat
.progs
= tmp
;
1689 idx
= priv
->pat
.progs_cnt
;
1690 priv
->pat
.progs_cnt
++;
1693 priv
->pat
.progs
[idx
].id
= progid
;
1694 priv
->pat
.progs
[idx
].pmt_pid
= ((base
[2] & 0x1F) << 8) | base
[3];
1695 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
);
1702 static inline int32_t es_pid_in_pmt(pmt_t
* pmt
, uint16_t pid
)
1712 for(i
= 0; i
< pmt
->es_cnt
; i
++)
1714 if(pmt
->es
[i
].pid
== pid
)
1722 static uint16_t get_mp4_desc_len(uint8_t *buf
, int *len
)
1724 //uint16_t i = 0, size = 0;
1725 int i
= 0, j
, size
= 0;
1727 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "PARSE_MP4_DESC_LEN(%d), bytes: ", *len
);
1731 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, " %x ", buf
[i
]);
1732 size
|= (buf
[i
] & 0x7f);
1733 if(!(buf
[i
] & 0x80))
1738 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, ", SIZE=%d\n", size
);
1745 static uint16_t parse_mp4_slconfig_descriptor(uint8_t *buf
, int len
, void *elem
)
1749 mp4_sl_config_t
*sl
;
1751 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4_SLCONFIG_DESCRIPTOR(%d)\n", len
);
1752 es
= (mp4_es_descr_t
*) elem
;
1755 mp_msg(MSGT_DEMUX
, MSGL_V
, "argh! NULL elem passed, skip\n");
1760 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;
1761 sl
->ocr
= sl
->dts
= sl
->cts
= 0;
1768 sl
->ts_resolution
= (buf
[i
] << 24) | (buf
[i
+1] << 16) | (buf
[i
+2] << 8) | buf
[i
+3];
1770 sl
->ocr_resolution
= (buf
[i
] << 24) | (buf
[i
+1] << 16) | (buf
[i
+2] << 8) | buf
[i
+3];
1772 sl
->ts_len
= buf
[i
];
1774 sl
->ocr_len
= buf
[i
];
1776 sl
->au_len
= buf
[i
];
1778 sl
->instant_bitrate_len
= buf
[i
];
1780 sl
->degr_len
= (buf
[i
] >> 4) & 0x0f;
1781 sl
->au_seqnum_len
= ((buf
[i
] & 0x0f) << 1) | ((buf
[i
+1] >> 7) & 0x01);
1783 sl
->packet_seqnum_len
= ((buf
[i
] >> 2) & 0x1f);
1787 else if(buf
[0] == 1)
1790 sl
->ts_resolution
= 1000;
1794 else if(buf
[0] == 2)
1805 sl
->au_start
= (sl
->flags
>> 7) & 0x1;
1806 sl
->au_end
= (sl
->flags
>> 6) & 0x1;
1807 sl
->random_accesspoint
= (sl
->flags
>> 5) & 0x1;
1808 sl
->random_accesspoint_only
= (sl
->flags
>> 4) & 0x1;
1809 sl
->padding
= (sl
->flags
>> 3) & 0x1;
1810 sl
->use_ts
= (sl
->flags
>> 2) & 0x1;
1811 sl
->idle
= (sl
->flags
>> 1) & 0x1;
1812 sl
->duration
= sl
->flags
& 0x1;
1816 sl
->timescale
= (buf
[i
] << 24) | (buf
[i
+1] << 16) | (buf
[i
+2] << 8) | buf
[i
+3];
1818 sl
->au_duration
= (buf
[i
] << 8) | buf
[i
+1];
1820 sl
->cts_duration
= (buf
[i
] << 8) | buf
[i
+1];
1823 else //no support for fixed durations atm
1824 sl
->timescale
= sl
->au_duration
= sl
->cts_duration
= 0;
1826 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",
1827 len
, buf
[0], sl
->flags
, sl
->use_ts
, sl
->ts_len
, sl
->timescale
, (uint64_t) sl
->dts
, (uint64_t) sl
->cts
);
1832 static int parse_mp4_descriptors(pmt_t
*pmt
, uint8_t *buf
, int len
, void *elem
);
1834 static uint16_t parse_mp4_decoder_config_descriptor(pmt_t
*pmt
, uint8_t *buf
, int len
, void *elem
)
1838 mp4_decoder_config_t
*dec
;
1840 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4_DECODER_CONFIG_DESCRIPTOR(%d)\n", len
);
1841 es
= (mp4_es_descr_t
*) elem
;
1844 mp_msg(MSGT_DEMUX
, MSGL_V
, "argh! NULL elem passed, skip\n");
1847 dec
= (mp4_decoder_config_t
*) &(es
->decoder
);
1849 dec
->object_type
= buf
[i
];
1850 dec
->stream_type
= (buf
[i
+1]>>2) & 0x3f;
1852 if(dec
->object_type
== 1 && dec
->stream_type
== 1)
1854 dec
->object_type
= MP4_OD
;
1855 dec
->stream_type
= MP4_OD
;
1857 else if(dec
->stream_type
== 4)
1859 if(dec
->object_type
== 0x6a)
1860 dec
->object_type
= VIDEO_MPEG1
;
1861 if(dec
->object_type
>= 0x60 && dec
->object_type
<= 0x65)
1862 dec
->object_type
= VIDEO_MPEG2
;
1863 else if(dec
->object_type
== 0x20)
1864 dec
->object_type
= VIDEO_MPEG4
;
1865 else if(dec
->object_type
== 0x21)
1866 dec
->object_type
= VIDEO_AVC
;
1867 /*else if(dec->object_type == 0x22)
1868 fprintf(stderr, "TYPE 0x22\n");*/
1869 else dec
->object_type
= UNKNOWN
;
1871 else if(dec
->stream_type
== 5)
1873 if(dec
->object_type
== 0x40)
1874 dec
->object_type
= AUDIO_AAC
;
1875 else if(dec
->object_type
== 0x6b)
1876 dec
->object_type
= AUDIO_MP2
;
1877 else if(dec
->object_type
>= 0x66 && dec
->object_type
<= 0x69)
1878 dec
->object_type
= AUDIO_MP2
;
1880 dec
->object_type
= UNKNOWN
;
1883 dec
->object_type
= dec
->stream_type
= UNKNOWN
;
1885 if(dec
->object_type
!= UNKNOWN
)
1887 //update the type of the current stream
1888 for(j
= 0; j
< pmt
->es_cnt
; j
++)
1890 if(pmt
->es
[j
].mp4_es_id
== es
->id
)
1892 pmt
->es
[j
].type
= SL_PES_STREAM
;
1898 parse_mp4_descriptors(pmt
, &buf
[13], len
-13, dec
);
1900 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
);
1905 static uint16_t parse_mp4_decoder_specific_descriptor(uint8_t *buf
, int len
, void *elem
)
1908 mp4_decoder_config_t
*dec
;
1910 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4_DECODER_SPECIFIC_DESCRIPTOR(%d)\n", len
);
1911 dec
= (mp4_decoder_config_t
*) elem
;
1914 mp_msg(MSGT_DEMUX
, MSGL_V
, "argh! NULL elem passed, skip\n");
1918 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "MP4 SPECIFIC INFO BYTES: \n");
1919 for(i
=0; i
<len
; i
++)
1920 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "%02x ", buf
[i
]);
1921 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "\n");
1923 if(len
> MAX_EXTRADATA_SIZE
)
1925 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "DEMUX_TS, EXTRADATA SUSPICIOUSLY BIG: %d, REFUSED\r\n", len
);
1928 memcpy(dec
->buf
, buf
, len
);
1929 dec
->buf_size
= len
;
1934 static uint16_t parse_mp4_es_descriptor(pmt_t
*pmt
, uint8_t *buf
, int len
)
1936 int i
= 0, j
= 0, k
, found
;
1938 mp4_es_descr_t es
, *target_es
= NULL
, *tmp
;
1940 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4ES: len=%d\n", len
);
1941 memset(&es
, 0, sizeof(mp4_es_descr_t
));
1944 es
.id
= (buf
[i
] << 8) | buf
[i
+1];
1945 mp_msg(MSGT_DEMUX
, MSGL_V
, "MP4ES_ID: %d\n", es
.id
);
1953 if(flag
& 0x20) //OCR, maybe we need it
1956 j
= parse_mp4_descriptors(pmt
, &buf
[i
], len
-i
, &es
);
1957 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
);
1958 if(es
.decoder
.object_type
!= UNKNOWN
&& es
.decoder
.stream_type
!= UNKNOWN
)
1961 //search this ES_ID if we already have it
1962 for(k
=0; k
< pmt
->mp4es_cnt
; k
++)
1964 if(pmt
->mp4es
[k
].id
== es
.id
)
1966 target_es
= &(pmt
->mp4es
[k
]);
1973 tmp
= realloc_struct(pmt
->mp4es
, pmt
->mp4es_cnt
+1, sizeof(mp4_es_descr_t
));
1976 fprintf(stderr
, "CAN'T REALLOC MP4_ES_DESCR\n");
1980 target_es
= &(pmt
->mp4es
[pmt
->mp4es_cnt
]);
1983 memcpy(target_es
, &es
, sizeof(mp4_es_descr_t
));
1984 mp_msg(MSGT_DEMUX
, MSGL_V
, "MP4ES_CNT: %d, ID=%d\n", pmt
->mp4es_cnt
, target_es
->id
);
1993 static void parse_mp4_object_descriptor(pmt_t
*pmt
, uint8_t *buf
, int len
, void *elem
)
1998 id
= (buf
[0] << 2) | ((buf
[1] & 0xc0) >> 6);
1999 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4_OBJECT_DESCRIPTOR: len=%d, OD_ID=%d\n", len
, id
);
2002 i
+= buf
[2] + 1; //url
2003 mp_msg(MSGT_DEMUX
, MSGL_V
, "URL\n");
2011 j
= parse_mp4_descriptors(pmt
, &(buf
[i
]), len
-i
, elem
);
2012 mp_msg(MSGT_DEMUX
, MSGL_V
, "OBJD, NOW i = %d, j=%d, LEN=%d\n", i
, j
, len
);
2019 static void parse_mp4_iod(pmt_t
*pmt
, uint8_t *buf
, int len
, void *elem
)
2022 mp4_od_t
*iod
= &(pmt
->iod
);
2024 iod
->id
= (buf
[0] << 2) | ((buf
[1] & 0xc0) >> 6);
2025 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4_IOD: len=%d, IOD_ID=%d\n", len
, iod
->id
);
2029 i
+= buf
[2] + 1; //url
2030 mp_msg(MSGT_DEMUX
, MSGL_V
, "URL\n");
2037 j
= parse_mp4_descriptors(pmt
, &(buf
[i
]), len
-i
, elem
);
2038 mp_msg(MSGT_DEMUX
, MSGL_V
, "IOD, NOW i = %d, j=%d, LEN=%d\n", i
, j
, len
);
2044 static int parse_mp4_descriptors(pmt_t
*pmt
, uint8_t *buf
, int len
, void *elem
)
2046 int tag
, descr_len
, i
= 0, j
= 0;
2048 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_MP4_DESCRIPTORS, len=%d\n", len
);
2056 descr_len
= get_mp4_desc_len(&(buf
[i
+1]), &j
);
2057 mp_msg(MSGT_DEMUX
, MSGL_V
, "TAG=%d (0x%x), DESCR_len=%d, len=%d, j=%d\n", tag
, tag
, descr_len
, len
, j
);
2058 if(descr_len
> len
- j
+1)
2060 mp_msg(MSGT_DEMUX
, MSGL_V
, "descriptor is too long, exit\n");
2068 parse_mp4_object_descriptor(pmt
, &(buf
[i
]), descr_len
, elem
);
2071 parse_mp4_iod(pmt
, &(buf
[i
]), descr_len
, elem
);
2074 parse_mp4_es_descriptor(pmt
, &(buf
[i
]), descr_len
);
2077 parse_mp4_decoder_config_descriptor(pmt
, &buf
[i
], descr_len
, elem
);
2080 parse_mp4_decoder_specific_descriptor(&buf
[i
], descr_len
, elem
);
2083 parse_mp4_slconfig_descriptor(&buf
[i
], descr_len
, elem
);
2086 mp_msg(MSGT_DEMUX
, MSGL_V
, "Unsupported mp4 descriptor 0x%x\n", tag
);
2094 static ES_stream_t
*new_pid(ts_priv_t
*priv
, int pid
)
2098 tss
= malloc(sizeof(ES_stream_t
));
2101 memset(tss
, 0, sizeof(ES_stream_t
));
2104 tss
->type
= UNKNOWN
;
2105 tss
->subtype
= UNKNOWN
;
2107 tss
->extradata
= NULL
;
2108 tss
->extradata_alloc
= tss
->extradata_len
= 0;
2109 priv
->ts
.pids
[pid
] = tss
;
2115 static int parse_program_descriptors(pmt_t
*pmt
, uint8_t *buf
, uint16_t len
)
2117 uint16_t i
= 0, k
, olen
= len
;
2121 mp_msg(MSGT_DEMUX
, MSGL_V
, "PROG DESCR, TAG=%x, LEN=%d(%x)\n", buf
[i
], buf
[i
+1], buf
[i
+1]);
2122 if(buf
[i
+1] > len
-2)
2124 mp_msg(MSGT_DEMUX
, MSGL_V
, "ERROR, descriptor len is too long, skipping\n");
2130 if(buf
[i
+3] == 2) //buggy versions of vlc muxer make this non-standard mess (missing iod_scope)
2133 k
= 4; //this is standard compliant
2134 parse_mp4_descriptors(pmt
, &buf
[i
+k
], (int) buf
[i
+1]-(k
-2), NULL
);
2137 len
-= 2 + buf
[i
+1];
2143 static int parse_descriptors(struct pmt_es_t
*es
, uint8_t *ptr
)
2145 int j
, descr_len
, len
;
2148 len
= es
->descr_length
;
2151 descr_len
= ptr
[j
+1];
2152 mp_msg(MSGT_DEMUX
, MSGL_V
, "...descr id: 0x%x, len=%d\n", ptr
[j
], descr_len
);
2155 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "INVALID DESCR LEN for tag %02x: %d vs %d max, EXIT LOOP\n", ptr
[j
], descr_len
, len
);
2160 if(ptr
[j
] == 0x6a || ptr
[j
] == 0x7a) //A52 Descriptor
2164 es
->type
= AUDIO_A52
;
2165 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "DVB A52 Descriptor\n");
2168 else if(ptr
[j
] == 0x7b) //DVB DTS Descriptor
2172 es
->type
= AUDIO_DTS
;
2173 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "DVB DTS Descriptor\n");
2176 else if(ptr
[j
] == 0x59) //Subtitling Descriptor
2180 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Subtitling Descriptor\n");
2183 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Descriptor length too short for DVB Subtitle Descriptor: %d, SKIPPING\n", descr_len
);
2187 memcpy(es
->lang
, &ptr
[j
+2], 3);
2191 (subtype
>= 0x10 && subtype
<= 0x13) ||
2192 (subtype
>= 0x20 && subtype
<= 0x23)
2196 //page parameters: compo page 2 bytes, ancillary page 2 bytes
2202 else if(ptr
[j
] == 0x50) //Component Descriptor
2204 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Component Descriptor\n");
2205 memcpy(es
->lang
, &ptr
[j
+5], 3);
2208 else if(ptr
[j
] == 0xa) //Language Descriptor
2210 memcpy(es
->lang
, &ptr
[j
+2], 3);
2212 mp_msg(MSGT_DEMUX
, MSGL_V
, "Language Descriptor: %s\n", es
->lang
);
2214 else if(ptr
[j
] == 0x5) //Registration Descriptor (looks like e fourCC :) )
2216 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Registration Descriptor\n");
2219 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Registration Descriptor length too short: %d, SKIPPING\n", descr_len
);
2224 memcpy(es
->format_descriptor
, &ptr
[j
+2], 4);
2225 es
->format_descriptor
[4] = 0;
2228 if(d
[0] == 'A' && d
[1] == 'C' && d
[2] == '-' && d
[3] == '3')
2230 es
->type
= AUDIO_A52
;
2232 else if(d
[0] == 'D' && d
[1] == 'T' && d
[2] == 'S' && d
[3] == '1')
2234 es
->type
= AUDIO_DTS
;
2236 else if(d
[0] == 'D' && d
[1] == 'T' && d
[2] == 'S' && d
[3] == '2')
2238 es
->type
= AUDIO_DTS
;
2240 else if(d
[0] == 'V' && d
[1] == 'C' && d
[2] == '-' && d
[3] == '1')
2242 es
->type
= VIDEO_VC1
;
2246 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "FORMAT %s\n", es
->format_descriptor
);
2249 else if(ptr
[j
] == 0x1e)
2251 es
->mp4_es_id
= (ptr
[j
+2] << 8) | ptr
[j
+3];
2252 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
);
2255 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Unknown descriptor 0x%x, SKIPPING\n", ptr
[j
]);
2257 len
-= 2 + descr_len
;
2264 static int parse_sl_section(pmt_t
*pmt
, ts_section_t
*section
, int is_start
, unsigned char *buff
, int size
)
2268 skip
= collect_section(section
, is_start
, buff
, size
);
2272 ptr
= &(section
->buffer
[skip
]);
2274 len
= ((ptr
[1] & 0x0f) << 8) | ptr
[2];
2275 mp_msg(MSGT_DEMUX
, MSGL_V
, "TABLEID: %d (av. %d), skip=%d, LEN: %d\n", tid
, section
->buffer_len
, skip
, len
);
2276 if(len
> 4093 || section
->buffer_len
< len
|| tid
!= 5)
2278 mp_msg(MSGT_DEMUX
, MSGL_V
, "SECTION TOO LARGE or wrong section type, EXIT\n");
2285 //8 is the current position, len - 9 is the amount of data available
2286 parse_mp4_descriptors(pmt
, &ptr
[8], len
- 9, NULL
);
2291 static int parse_pmt(ts_priv_t
* priv
, uint16_t progid
, uint16_t pid
, int is_start
, unsigned char *buff
, int size
)
2293 unsigned char *base
, *es_base
;
2295 int32_t idx
, es_count
, section_bytes
;
2299 struct pmt_es_t
*tmp_es
;
2300 ts_section_t
*section
;
2303 idx
= progid_idx_in_pmt(priv
, progid
);
2307 int sz
= (priv
->pmt_cnt
+ 1) * sizeof(pmt_t
);
2308 tmp
= realloc_struct(priv
->pmt
, priv
->pmt_cnt
+ 1, sizeof(pmt_t
));
2311 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "PARSE_PMT: COULDN'T REALLOC %d bytes, NEXT\n", sz
);
2315 idx
= priv
->pmt_cnt
;
2316 memset(&(priv
->pmt
[idx
]), 0, sizeof(pmt_t
));
2318 priv
->pmt
[idx
].progid
= progid
;
2321 pmt
= &(priv
->pmt
[idx
]);
2323 section
= &(pmt
->section
);
2324 skip
= collect_section(section
, is_start
, buff
, size
);
2328 base
= &(section
->buffer
[skip
]);
2330 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",
2331 progid
, pmt
->section
.buffer_len
, is_start
, pid
, size
, m
, pmt
->es_cnt
, idx
, pmt
);
2333 pmt
->table_id
= base
[0];
2334 if(pmt
->table_id
!= 2)
2336 pmt
->ssi
= base
[1] & 0x80;
2337 pmt
->section_length
= (((base
[1] & 0xf) << 8 ) | base
[2]);
2338 pmt
->version_number
= (base
[5] >> 1) & 0x1f;
2339 pmt
->curr_next
= (base
[5] & 1);
2340 pmt
->section_number
= base
[6];
2341 pmt
->last_section_number
= base
[7];
2342 pmt
->PCR_PID
= ((base
[8] & 0x1f) << 8 ) | base
[9];
2343 pmt
->prog_descr_length
= ((base
[10] & 0xf) << 8 ) | base
[11];
2344 if(pmt
->prog_descr_length
> pmt
->section_length
- 9)
2346 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_PMT, INVALID PROG_DESCR LENGTH (%d vs %d)\n", pmt
->prog_descr_length
, pmt
->section_length
- 9);
2350 if(pmt
->prog_descr_length
)
2351 parse_program_descriptors(pmt
, &base
[12], pmt
->prog_descr_length
);
2353 es_base
= &base
[12 + pmt
->prog_descr_length
]; //the beginning of th ES loop
2355 section_bytes
= pmt
->section_length
- 13 - pmt
->prog_descr_length
;
2358 while(section_bytes
>= 5)
2360 int es_pid
, es_type
;
2362 es_type
= es_base
[0];
2363 es_pid
= ((es_base
[1] & 0x1f) << 8) | es_base
[2];
2365 idx
= es_pid_in_pmt(pmt
, es_pid
);
2368 int sz
= sizeof(struct pmt_es_t
) * (pmt
->es_cnt
+ 1);
2369 tmp_es
= realloc_struct(pmt
->es
, pmt
->es_cnt
+ 1, sizeof(struct pmt_es_t
));
2372 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "PARSE_PMT, COULDN'T ALLOCATE %d bytes for PMT_ES\n", sz
);
2377 memset(&(pmt
->es
[idx
]), 0, sizeof(struct pmt_es_t
));
2381 pmt
->es
[idx
].descr_length
= ((es_base
[3] & 0xf) << 8) | es_base
[4];
2384 if(pmt
->es
[idx
].descr_length
> section_bytes
- 5)
2386 mp_msg(MSGT_DEMUX
, MSGL_V
, "PARSE_PMT, ES_DESCR_LENGTH TOO LARGE %d > %d, EXIT\n",
2387 pmt
->es
[idx
].descr_length
, section_bytes
- 5);
2392 pmt
->es
[idx
].pid
= es_pid
;
2394 pmt
->es
[idx
].type
= UNKNOWN
;
2396 pmt
->es
[idx
].type
= es_type
;
2398 parse_descriptors(&pmt
->es
[idx
], &es_base
[5]);
2403 pmt
->es
[idx
].type
= VIDEO_MPEG1
;
2406 pmt
->es
[idx
].type
= VIDEO_MPEG2
;
2410 pmt
->es
[idx
].type
= AUDIO_MP2
;
2413 if(pmt
->es
[idx
].type
== 0x6) //this could have been ovrwritten by parse_descriptors
2414 pmt
->es
[idx
].type
= UNKNOWN
;
2417 pmt
->es
[idx
].type
= VIDEO_MPEG4
;
2421 pmt
->es
[idx
].type
= AUDIO_AAC
;
2424 pmt
->es
[idx
].type
= VIDEO_H264
;
2427 pmt
->es
[idx
].type
= SL_PES_STREAM
;
2430 pmt
->es
[idx
].type
= SL_SECTION
;
2433 pmt
->es
[idx
].type
= AUDIO_A52
;
2438 pmt
->es
[idx
].type
= AUDIO_DTS
;
2441 pmt
->es
[idx
].type
= VIDEO_VC1
;
2444 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "UNKNOWN ES TYPE=0x%x\n", es_type
);
2445 pmt
->es
[idx
].type
= UNKNOWN
;
2448 tss
= priv
->ts
.pids
[es_pid
]; //an ES stream
2451 tss
= new_pid(priv
, es_pid
);
2453 tss
->type
= pmt
->es
[idx
].type
;
2456 section_bytes
-= 5 + pmt
->es
[idx
].descr_length
;
2457 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",
2458 progid
, idx
, es_count
, pmt
->es
[idx
].pid
, pmt
->es
[idx
].pid
, pmt
->es
[idx
].type
, pmt
->es
[idx
].descr_length
, section_bytes
);
2461 es_base
+= 5 + pmt
->es
[idx
].descr_length
;
2466 mp_msg(MSGT_DEMUX
, MSGL_V
, "----------------------------\n");
2470 static pmt_t
* pmt_of_pid(ts_priv_t
*priv
, int pid
, mp4_decoder_config_t
**mp4_dec
)
2476 for(i
= 0; i
< priv
->pmt_cnt
; i
++)
2478 if(priv
->pmt
[i
].es
&& priv
->pmt
[i
].es_cnt
)
2480 for(j
= 0; j
< priv
->pmt
[i
].es_cnt
; j
++)
2482 if(priv
->pmt
[i
].es
[j
].pid
== pid
)
2485 if(priv
->pmt
[i
].es
[j
].mp4_es_id
)
2487 for(k
= 0; k
< priv
->pmt
[i
].mp4es_cnt
; k
++)
2489 if(priv
->pmt
[i
].mp4es
[k
].id
== priv
->pmt
[i
].es
[j
].mp4_es_id
)
2491 *mp4_dec
= &(priv
->pmt
[i
].mp4es
[k
].decoder
);
2497 return &(priv
->pmt
[i
]);
2508 static inline int32_t pid_type_from_pmt(ts_priv_t
*priv
, int pid
)
2510 int32_t pmt_idx
, pid_idx
, i
, j
;
2512 pmt_idx
= progid_idx_in_pmt(priv
, priv
->prog
);
2516 pid_idx
= es_pid_in_pmt(&(priv
->pmt
[pmt_idx
]), pid
);
2518 return priv
->pmt
[pmt_idx
].es
[pid_idx
].type
;
2522 for(i
= 0; i
< priv
->pmt_cnt
; i
++)
2524 pmt_t
*pmt
= &(priv
->pmt
[i
]);
2525 for(j
= 0; j
< pmt
->es_cnt
; j
++)
2526 if(pmt
->es
[j
].pid
== pid
)
2527 return pmt
->es
[j
].type
;
2535 static inline uint8_t *pid_lang_from_pmt(ts_priv_t
*priv
, int pid
)
2537 int32_t pmt_idx
, pid_idx
, i
, j
;
2539 pmt_idx
= progid_idx_in_pmt(priv
, priv
->prog
);
2543 pid_idx
= es_pid_in_pmt(&(priv
->pmt
[pmt_idx
]), pid
);
2545 return priv
->pmt
[pmt_idx
].es
[pid_idx
].lang
;
2549 for(i
= 0; i
< priv
->pmt_cnt
; i
++)
2551 pmt_t
*pmt
= &(priv
->pmt
[i
]);
2552 for(j
= 0; j
< pmt
->es_cnt
; j
++)
2553 if(pmt
->es
[j
].pid
== pid
)
2554 return pmt
->es
[j
].lang
;
2562 static int fill_packet(demuxer_t
*demuxer
, demux_stream_t
*ds
, demux_packet_t
**dp
, int *dp_offset
, TS_stream_info
*si
)
2566 if((*dp
!= NULL
) && (*dp_offset
> 0))
2569 resize_demux_packet(*dp
, ret
); //shrinked to the right size
2570 ds_add_packet(ds
, *dp
);
2571 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
);
2574 float diff
= (*dp
)->pts
- si
->last_pts
;
2577 if(abs(diff
) > 1) //1 second, there's a discontinuity
2579 si
->duration
+= si
->last_pts
- si
->first_pts
;
2580 si
->first_pts
= si
->last_pts
= (*dp
)->pts
;
2584 si
->last_pts
= (*dp
)->pts
;
2587 dur
= si
->duration
+ (si
->last_pts
- si
->first_pts
);
2589 if(dur
> 0 && ds
== demuxer
->video
)
2591 ts_priv_t
* priv
= (ts_priv_t
*) demuxer
->priv
;
2592 if(dur
> 1) //otherwise it may be unreliable
2593 priv
->vbitrate
= (uint32_t) ((float) si
->size
/ dur
);
2604 static int fill_extradata(mp4_decoder_config_t
* mp4_dec
, ES_stream_t
*tss
)
2608 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "MP4_dec: %p, pid: %d\n", mp4_dec
, tss
->pid
);
2610 if(mp4_dec
->buf_size
> tss
->extradata_alloc
)
2612 tmp
= (uint8_t *) realloc(tss
->extradata
, mp4_dec
->buf_size
);
2615 tss
->extradata
= tmp
;
2616 tss
->extradata_alloc
= mp4_dec
->buf_size
;
2618 memcpy(tss
->extradata
, mp4_dec
->buf
, mp4_dec
->buf_size
);
2619 tss
->extradata_len
= mp4_dec
->buf_size
;
2620 mp_msg(MSGT_DEMUX
, MSGL_V
, "EXTRADATA: %p, alloc=%d, len=%d\n", tss
->extradata
, tss
->extradata_alloc
, tss
->extradata_len
);
2622 return tss
->extradata_len
;
2625 // 0 = EOF or no stream found
2626 // else = [-] number of bytes written to the packet
2627 static int ts_parse(demuxer_t
*demuxer
, ES_stream_t
*es
, unsigned char *packet
, int probe
)
2631 int buf_size
, is_start
, pid
, base
;
2632 int len
, cc
, cc_ok
, afc
, retv
= 0, is_video
, is_audio
, is_sub
;
2633 ts_priv_t
* priv
= (ts_priv_t
*) demuxer
->priv
;
2634 stream_t
*stream
= demuxer
->stream
;
2636 demux_stream_t
*ds
= NULL
;
2637 demux_packet_t
**dp
= NULL
;
2638 int *dp_offset
= 0, *buffer_size
= 0;
2639 int32_t progid
, pid_type
, bad
, ts_error
;
2640 int junk
= 0, rap_flag
= 0;
2642 mp4_decoder_config_t
*mp4_dec
;
2649 ds
= (demux_stream_t
*) NULL
;
2650 dp
= (demux_packet_t
**) NULL
;
2651 dp_offset
= buffer_size
= NULL
;
2657 junk
= priv
->ts
.packet_size
- TS_PACKET_SIZE
;
2658 buf_size
= priv
->ts
.packet_size
- junk
;
2660 if(stream_eof(stream
))
2664 ts_dump_streams(priv
);
2665 demuxer
->filepos
= stream_tell(demuxer
->stream
);
2672 if(! ts_sync(stream
))
2674 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "TS_PARSE: COULDN'T SYNC\n");
2678 len
= stream_read(stream
, &packet
[1], 3);
2683 if((packet
[1] >> 7) & 0x01) //transport error
2687 is_start
= packet
[1] & 0x40;
2688 pid
= ((packet
[1] & 0x1f) << 8) | packet
[2];
2690 tss
= priv
->ts
.pids
[pid
]; //an ES stream
2693 tss
= new_pid(priv
, pid
);
2698 cc
= (packet
[3] & 0xf);
2699 cc_ok
= (tss
->last_cc
< 0) || ((((tss
->last_cc
+ 1) & 0x0f) == cc
));
2702 bad
= ts_error
; // || (! cc_ok);
2705 if(priv
->keep_broken
== 0)
2707 stream_skip(stream
, buf_size
-1+junk
);
2711 is_start
= 0; //queued to the packet data
2717 if((!is_start
&& !tss
->is_synced
) || ((pid
> 1) && (pid
< 16)) || (pid
== 8191)) //invalid pid
2719 stream_skip(stream
, buf_size
-1+junk
);
2724 afc
= (packet
[3] >> 4) & 3;
2725 if(! (afc
% 2)) //no payload in this TS packet
2727 stream_skip(stream
, buf_size
-1+junk
);
2734 c
= stream_read_char(stream
);
2736 if(c
< 0 || c
> 183) //broken from the stream layer or invalid
2738 stream_skip(stream
, buf_size
-1+junk
);
2745 uint8_t pcrbuf
[188];
2746 int flags
= stream_read_char(stream
);
2748 rap_flag
= (flags
& 0x40) >> 6;
2749 has_pcr
= flags
& 0x10;
2753 stream_read(stream
, pcrbuf
, c
);
2757 int pcr_pid
= prog_pcr_pid(priv
, priv
->prog
);
2760 uint64_t pcr
, pcr_ext
;
2762 pcr
= (int64_t)(pcrbuf
[0]) << 25;
2763 pcr
|= pcrbuf
[1] << 17 ;
2764 pcr
|= (pcrbuf
[2]) << 9;
2765 pcr
|= pcrbuf
[3] << 1 ;
2766 pcr
|= (pcrbuf
[4] & 0x80) >> 7;
2768 pcr_ext
= (pcrbuf
[4] & 0x01) << 8;
2769 pcr_ext
|= pcrbuf
[5];
2771 pcr
= pcr
* 300 + pcr_ext
;
2773 demuxer
->reference_clock
= (double)pcr
/(double)27000000.0;
2783 //find the program that the pid belongs to; if (it's the right one or -1) && pid_type==SL_SECTION
2784 //call parse_sl_section()
2785 pmt
= pmt_of_pid(priv
, pid
, &mp4_dec
);
2788 fill_extradata(mp4_dec
, tss
);
2789 if(IS_VIDEO(mp4_dec
->object_type
) || IS_AUDIO(mp4_dec
->object_type
))
2791 tss
->type
= SL_PES_STREAM
;
2792 tss
->subtype
= mp4_dec
->object_type
;
2799 base
= priv
->ts
.packet_size
- buf_size
;
2801 priv
->last_pid
= pid
;
2803 is_video
= IS_VIDEO(tss
->type
) || (tss
->type
==SL_PES_STREAM
&& IS_VIDEO(tss
->subtype
));
2804 is_audio
= IS_AUDIO(tss
->type
) || (tss
->type
==SL_PES_STREAM
&& IS_AUDIO(tss
->subtype
)) || (tss
->type
== PES_PRIVATE1
);
2805 is_sub
= ((tss
->type
== SPU_DVD
) || (tss
->type
== SPU_DVB
));
2806 pid_type
= pid_type_from_pmt(priv
, pid
);
2808 // PES CONTENT STARTS HERE
2811 if((is_video
|| is_audio
) && is_start
&& !priv
->ts
.streams
[pid
].sh
)
2812 ts_add_stream(demuxer
, tss
);
2814 if((pid
== demuxer
->sub
->id
)) //or the lang is right
2819 if(is_video
&& (demuxer
->video
->id
== priv
->ts
.streams
[pid
].id
))
2821 ds
= demuxer
->video
;
2823 dp
= &priv
->fifo
[1].pack
;
2824 dp_offset
= &priv
->fifo
[1].offset
;
2825 buffer_size
= &priv
->fifo
[1].buffer_size
;
2828 else if(is_audio
&& (demuxer
->audio
->id
== priv
->ts
.streams
[pid
].id
))
2830 ds
= demuxer
->audio
;
2832 dp
= &priv
->fifo
[0].pack
;
2833 dp_offset
= &priv
->fifo
[0].offset
;
2834 buffer_size
= &priv
->fifo
[0].buffer_size
;
2838 || (pid_type
== SPU_DVD
) || (pid_type
== SPU_DVB
))
2840 //SUBS are infrequent, so the initial detection may fail
2841 // and we may need to add them at play-time
2842 if(demuxer
->sub
->id
== -1)
2845 p
= progid_for_pid(priv
, tss
->pid
, priv
->prog
);
2854 if ((lang
= pid_lang_from_pmt(priv
, pid
)))
2855 asgn
= (strncmp(lang
, dvdsub_lang
, 3) == 0);
2857 else //no language specified with -slang
2862 demuxer
->sub
->id
= tss
->pid
;
2863 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "CHOSEN SUBs pid 0x%x (%d) FROM PROG %d\n", tss
->pid
, tss
->pid
, priv
->prog
);
2868 if(demuxer
->sub
->id
== tss
->pid
)
2872 dp
= &priv
->fifo
[2].pack
;
2873 dp_offset
= &priv
->fifo
[2].offset
;
2874 buffer_size
= &priv
->fifo
[2].buffer_size
;
2878 stream_skip(stream
, buf_size
+junk
);
2883 //IS IT TIME TO QUEUE DATA to the dp_packet?
2884 if(is_start
&& (dp
!= NULL
))
2886 retv
= fill_packet(demuxer
, ds
, dp
, dp_offset
, si
);
2890 if(dp
&& *dp
== NULL
)
2892 if(*buffer_size
> MAX_PACK_BYTES
)
2893 *buffer_size
= MAX_PACK_BYTES
;
2894 *dp
= new_demux_packet(*buffer_size
); //es->size
2898 fprintf(stderr
, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", *buffer_size
);
2901 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "CREATED DP(%d)\n", *buffer_size
);
2906 if(probe
|| !dp
) //dp is NULL for tables and sections
2912 if(*dp_offset
+ buf_size
> *buffer_size
)
2914 *buffer_size
= *dp_offset
+ buf_size
+ TS_FEC_PACKET_SIZE
;
2915 resize_demux_packet(*dp
, *buffer_size
);
2917 p
= &((*dp
)->buffer
[*dp_offset
]);
2920 len
= stream_read(stream
, p
, buf_size
);
2923 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "\r\nts_parse() couldn't read enough data: %d < %d\r\n", len
, buf_size
);
2926 stream_skip(stream
, junk
);
2930 parse_pat(priv
, is_start
, p
, buf_size
);
2933 else if((tss
->type
== SL_SECTION
) && pmt
)
2935 int k
, mp4_es_id
= -1;
2936 ts_section_t
*section
;
2937 for(k
= 0; k
< pmt
->mp4es_cnt
; k
++)
2939 if(pmt
->mp4es
[k
].decoder
.object_type
== MP4_OD
&& pmt
->mp4es
[k
].decoder
.stream_type
== MP4_OD
)
2940 mp4_es_id
= pmt
->mp4es
[k
].id
;
2942 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "MP4ESID: %d\n", mp4_es_id
);
2943 for(k
= 0; k
< pmt
->es_cnt
; k
++)
2945 if(pmt
->es
[k
].mp4_es_id
== mp4_es_id
)
2947 section
= &(tss
->section
);
2948 parse_sl_section(pmt
, section
, is_start
, &packet
[base
], buf_size
);
2955 progid
= prog_id_in_pat(priv
, pid
);
2958 if(pid
!= demuxer
->video
->id
&& pid
!= demuxer
->audio
->id
&& pid
!= demuxer
->sub
->id
)
2960 parse_pmt(priv
, progid
, pid
, is_start
, &packet
[base
], buf_size
);
2964 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "Argh! Data pid %d used in the PMT, Skipping PMT parsing!\n", pid
);
2973 uint8_t *lang
= NULL
;
2975 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "IS_START\n");
2977 len
= pes_parse2(p
, buf_size
, es
, pid_type
, pmt
, pid
);
2984 tss
->is_synced
|= es
->is_synced
|| rap_flag
;
2985 tss
->payload_size
= es
->payload_size
;
2987 if(is_audio
&& (lang
= pid_lang_from_pmt(priv
, es
->pid
)))
2989 memcpy(es
->lang
, lang
, 3);
2997 if(es
->type
== UNKNOWN
)
3000 tss
->type
= es
->type
;
3001 tss
->subtype
= es
->subtype
;
3008 es
->pts
= tss
->pts
= tss
->last_pts
;
3010 tss
->pts
= tss
->last_pts
= es
->pts
;
3012 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "ts_parse, NEW pid=%d, PSIZE: %u, type=%X, start=%p, len=%d\n",
3013 es
->pid
, es
->payload_size
, es
->type
, es
->start
, es
->size
);
3015 demuxer
->filepos
= stream_tell(demuxer
->stream
) - es
->size
;
3017 memmove(p
, es
->start
, es
->size
);
3018 *dp_offset
+= es
->size
;
3020 (*dp
)->pos
= stream_tell(demuxer
->stream
);
3021 (*dp
)->pts
= es
->pts
;
3034 es
->type
= tss
->type
;
3035 es
->subtype
= tss
->subtype
;
3036 es
->pts
= tss
->pts
= tss
->last_pts
;
3037 es
->start
= &packet
[base
];
3040 if(tss
->payload_size
> 0)
3042 sz
= FFMIN(tss
->payload_size
, buf_size
);
3043 tss
->payload_size
-= sz
;
3050 sz
= es
->size
= buf_size
;
3063 if(*dp_offset
>= MAX_PACK_BYTES
)
3065 (*dp
)->pts
= tss
->last_pts
;
3066 retv
= fill_packet(demuxer
, ds
, dp
, dp_offset
, si
);
3074 memcpy(es
->start
, p
, sz
);
3088 extern void skip_audio_frame(sh_audio_t
*sh_audio
);
3090 static void reset_fifos(demuxer_t
*demuxer
, int a
, int v
, int s
)
3092 ts_priv_t
* priv
= demuxer
->priv
;
3095 if(priv
->fifo
[0].pack
!= NULL
)
3097 free_demux_packet(priv
->fifo
[0].pack
);
3098 priv
->fifo
[0].pack
= NULL
;
3100 priv
->fifo
[0].offset
= 0;
3105 if(priv
->fifo
[1].pack
!= NULL
)
3107 free_demux_packet(priv
->fifo
[1].pack
);
3108 priv
->fifo
[1].pack
= NULL
;
3110 priv
->fifo
[1].offset
= 0;
3115 if(priv
->fifo
[2].pack
!= NULL
)
3117 free_demux_packet(priv
->fifo
[2].pack
);
3118 priv
->fifo
[2].pack
= NULL
;
3120 priv
->fifo
[2].offset
= 0;
3122 demuxer
->reference_clock
= MP_NOPTS_VALUE
;
3126 static void demux_seek_ts(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
, int flags
)
3128 demux_stream_t
*d_audio
=demuxer
->audio
;
3129 demux_stream_t
*d_video
=demuxer
->video
;
3130 sh_audio_t
*sh_audio
=d_audio
->sh
;
3131 sh_video_t
*sh_video
=d_video
->sh
;
3132 ts_priv_t
* priv
= (ts_priv_t
*) demuxer
->priv
;
3136 //================= seek in MPEG-TS ==========================
3138 ts_dump_streams(demuxer
->priv
);
3139 reset_fifos(demuxer
, sh_audio
!= NULL
, sh_video
!= NULL
, demuxer
->sub
->id
> 0);
3141 demux_flush(demuxer
);
3145 video_stats
= (sh_video
!= NULL
);
3148 mp_msg(MSGT_DEMUX
, MSGL_V
, "IBPS: %d, vb: %d\r\n", sh_video
->i_bps
, priv
->vbitrate
);
3150 video_stats
= priv
->vbitrate
;
3152 video_stats
= sh_video
->i_bps
;
3155 newpos
= (flags
& SEEK_ABSOLUTE
) ? demuxer
->movi_start
: demuxer
->filepos
;
3156 if(flags
& SEEK_FACTOR
) // float seek 0..1
3157 newpos
+=(demuxer
->movi_end
-demuxer
->movi_start
)*rel_seek_secs
;
3161 if(! video_stats
) // unspecified or VBR
3162 newpos
+= 2324*75*rel_seek_secs
; // 174.3 kbyte/sec
3164 newpos
+= video_stats
*rel_seek_secs
;
3168 if(newpos
< demuxer
->movi_start
)
3169 newpos
= demuxer
->movi_start
; //begininng of stream
3171 stream_seek(demuxer
->stream
, newpos
);
3172 for(i
= 0; i
< 8192; i
++)
3173 if(priv
->ts
.pids
[i
] != NULL
)
3174 priv
->ts
.pids
[i
]->is_synced
= 0;
3176 videobuf_code_len
= 0;
3178 if(sh_video
!= NULL
)
3179 ds_fill_buffer(d_video
);
3181 if(sh_audio
!= NULL
)
3183 ds_fill_buffer(d_audio
);
3186 while(sh_video
!= NULL
)
3188 if(sh_audio
&& !d_audio
->eof
&& d_video
->pts
&& d_audio
->pts
)
3190 float a_pts
=d_audio
->pts
;
3191 a_pts
+=(ds_tell_pts(d_audio
)-sh_audio
->a_in_buffer_len
)/(float)sh_audio
->i_bps
;
3192 if(d_video
->pts
> a_pts
)
3194 skip_audio_frame(sh_audio
); // sync audio
3200 i
= sync_video_packet(d_video
);
3201 if((sh_video
->format
== VIDEO_MPEG1
) || (sh_video
->format
== VIDEO_MPEG2
))
3203 if(i
==0x1B3 || i
==0x1B8) break; // found it!
3205 else if((sh_video
->format
== VIDEO_MPEG4
) && (i
==0x1B6))
3207 else if(sh_video
->format
== VIDEO_VC1
&& (i
==0x10E || i
==0x10F))
3211 if((i
& ~0x60) == 0x105 || (i
& ~0x60) == 0x107) break;
3214 if(!i
|| !skip_video_packet(d_video
)) break; // EOF?
3219 static int demux_ts_fill_buffer(demuxer_t
* demuxer
, demux_stream_t
*ds
)
3222 ts_priv_t
*priv
= (ts_priv_t
*)demuxer
->priv
;
3224 return -ts_parse(demuxer
, &es
, priv
->packet
, 0);
3228 static int ts_check_file_dmx(demuxer_t
*demuxer
)
3230 return ts_check_file(demuxer
) ? DEMUXER_TYPE_MPEG_TS
: 0;
3233 static int is_usable_program(ts_priv_t
*priv
, pmt_t
*pmt
)
3237 for(j
= 0; j
< pmt
->es_cnt
; j
++)
3239 if(priv
->ts
.pids
[pmt
->es
[j
].pid
] == NULL
|| priv
->ts
.streams
[pmt
->es
[j
].pid
].sh
== NULL
)
3242 priv
->ts
.streams
[pmt
->es
[j
].pid
].type
== TYPE_VIDEO
||
3243 priv
->ts
.streams
[pmt
->es
[j
].pid
].type
== TYPE_AUDIO
3251 static int demux_ts_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
3253 ts_priv_t
* priv
= (ts_priv_t
*)demuxer
->priv
;
3257 case DEMUXER_CTRL_SWITCH_AUDIO
:
3258 case DEMUXER_CTRL_SWITCH_VIDEO
:
3262 int reftype
, areset
= 0, vreset
= 0;
3265 if(cmd
== DEMUXER_CTRL_SWITCH_VIDEO
)
3267 reftype
= TYPE_VIDEO
;
3268 ds
= demuxer
->video
;
3273 reftype
= TYPE_AUDIO
;
3274 ds
= demuxer
->audio
;
3280 reset_fifos(demuxer
, areset
, vreset
, 0);
3284 *((int*)arg
) = ds
->id
;
3285 return DEMUXER_CTRL_OK
;
3290 for(i
= 0; i
< 8192; i
++)
3292 if(priv
->ts
.streams
[i
].id
== ds
->id
&& priv
->ts
.streams
[i
].type
== reftype
)
3299 if(priv
->ts
.streams
[i
].type
== reftype
)
3301 if(priv
->ts
.streams
[i
].id
== ds
->id
) //we made a complete loop
3303 sh
= priv
->ts
.streams
[i
].sh
;
3307 else //audio track <n>
3309 for(i
= 0; i
< 8192; i
++)
3311 if(priv
->ts
.streams
[i
].id
== n
&& priv
->ts
.streams
[i
].type
== reftype
)
3313 sh
= priv
->ts
.streams
[i
].sh
;
3321 if(ds
->id
!= priv
->ts
.streams
[i
].id
)
3322 reset_fifos(demuxer
, areset
, vreset
, 0);
3323 ds
->id
= priv
->ts
.streams
[i
].id
;
3326 mp_msg(MSGT_DEMUX
, MSGL_V
, "\r\ndemux_ts, switched to audio pid %d, id: %d, sh: %p\r\n", i
, ds
->id
, sh
);
3329 *((int*)arg
) = ds
->id
;
3330 return DEMUXER_CTRL_OK
;
3333 case DEMUXER_CTRL_IDENTIFY_PROGRAM
: //returns in prog->{aid,vid} the new ids that comprise a program
3336 int vid_done
=0, aid_done
=0;
3338 demux_program_t
*prog
= arg
;
3340 if(priv
->pmt_cnt
< 2)
3341 return DEMUXER_CTRL_NOTIMPL
;
3343 if(prog
->progid
== -1)
3345 int cur_pmt_idx
= 0;
3347 for(i
= 0; i
< priv
->pmt_cnt
; i
++)
3348 if(priv
->pmt
[i
].progid
== priv
->prog
)
3354 i
= (cur_pmt_idx
+ 1) % priv
->pmt_cnt
;
3355 while(i
!= cur_pmt_idx
)
3357 pmt
= &priv
->pmt
[i
];
3358 cnt
= is_usable_program(priv
, pmt
);
3361 i
= (i
+ 1) % priv
->pmt_cnt
;
3366 for(i
= 0; i
< priv
->pmt_cnt
; i
++)
3367 if(priv
->pmt
[i
].progid
== prog
->progid
)
3369 pmt
= &priv
->pmt
[i
]; //required program
3370 cnt
= is_usable_program(priv
, pmt
);
3375 return DEMUXER_CTRL_NOTIMPL
;
3378 prog
->aid
= prog
->vid
= -2; //no audio and no video by default
3379 for(j
= 0; j
< pmt
->es_cnt
; j
++)
3381 if(priv
->ts
.pids
[pmt
->es
[j
].pid
] == NULL
|| priv
->ts
.streams
[pmt
->es
[j
].pid
].sh
== NULL
)
3384 if(!vid_done
&& priv
->ts
.streams
[pmt
->es
[j
].pid
].type
== TYPE_VIDEO
)
3387 prog
->vid
= priv
->ts
.streams
[pmt
->es
[j
].pid
].id
;
3389 else if(!aid_done
&& priv
->ts
.streams
[pmt
->es
[j
].pid
].type
== TYPE_AUDIO
)
3392 prog
->aid
= priv
->ts
.streams
[pmt
->es
[j
].pid
].id
;
3396 priv
->prog
= prog
->progid
= pmt
->progid
;
3397 return DEMUXER_CTRL_OK
;
3401 return DEMUXER_CTRL_NOTIMPL
;
3406 const demuxer_desc_t demuxer_desc_mpeg_ts
= {
3412 DEMUXER_TYPE_MPEG_TS
,
3413 0, // unsafe autodetect
3415 demux_ts_fill_buffer
,