subassconvert: do not escape likely ASS override tags
[mplayer.git] / libmpdemux / demux_ts.c
blobdbf12b745676b7712224dda1f5613875522c405a
1 /*
2 * Demultiplexer for MPEG2 Transport Streams.
4 * Written by Nico <nsabbi@libero.it>
5 * Kind feedback is appreciated; 'sucks' and alike is not.
6 * Originally based on demux_pva.c written by Matteo Giani and FFmpeg (libavformat) sources
8 * This file is part of MPlayer.
10 * MPlayer is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * MPlayer is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "config.h"
30 #include "mp_msg.h"
31 #include "options.h"
33 #include "libmpcodecs/dec_audio.h"
34 #include "stream/stream.h"
35 #include "demuxer.h"
36 #include "parse_es.h"
37 #include "stheader.h"
38 #include "ms_hdr.h"
39 #include "mpeg_hdr.h"
40 #include "demux_ts.h"
42 #define TS_PH_PACKET_SIZE 192
43 #define TS_FEC_PACKET_SIZE 204
44 #define TS_PACKET_SIZE 188
45 #define NB_PID_MAX 8192
47 #define MAX_HEADER_SIZE 6 /* enough for PES header + length */
48 #define MAX_CHECK_SIZE 65535
49 #define NUM_CONSECUTIVE_TS_PACKETS 32
50 #define NUM_CONSECUTIVE_AUDIO_PACKETS 348
51 #define MAX_A52_FRAME_SIZE 3840
53 #ifndef SIZE_MAX
54 #define SIZE_MAX ((size_t)-1)
55 #endif
57 #define TYPE_AUDIO 1
58 #define TYPE_VIDEO 2
59 #define TYPE_SUB 3
61 int ts_prog;
62 int ts_keep_broken=0;
63 off_t ts_probe = 0;
64 int audio_substream_id = -1;
66 typedef enum
68 UNKNOWN = -1,
69 VIDEO_MPEG1 = 0x10000001,
70 VIDEO_MPEG2 = 0x10000002,
71 VIDEO_MPEG4 = 0x10000004,
72 VIDEO_H264 = 0x10000005,
73 VIDEO_AVC = mmioFOURCC('a', 'v', 'c', '1'),
74 VIDEO_DIRAC = mmioFOURCC('d', 'r', 'a', 'c'),
75 VIDEO_VC1 = mmioFOURCC('W', 'V', 'C', '1'),
76 AUDIO_MP2 = 0x50,
77 AUDIO_A52 = 0x2000,
78 AUDIO_DTS = 0x2001,
79 AUDIO_LPCM_BE = 0x10001,
80 AUDIO_AAC = mmioFOURCC('M', 'P', '4', 'A'),
81 AUDIO_AAC_LATM = mmioFOURCC('M', 'P', '4', 'L'),
82 AUDIO_TRUEHD = mmioFOURCC('T', 'R', 'H', 'D'),
83 AUDIO_S302M = mmioFOURCC('B', 'S', 'S', 'D'),
84 SPU_DVD = 0x3000000,
85 SPU_DVB = 0x3000001,
86 SPU_TELETEXT = 0x3000002,
87 SPU_PGS = 0x3000003,
88 PES_PRIVATE1 = 0xBD00000,
89 SL_PES_STREAM = 0xD000000,
90 SL_SECTION = 0xD100000,
91 MP4_OD = 0xD200000,
92 } es_stream_type_t;
94 typedef struct {
95 uint8_t *buffer;
96 uint16_t buffer_len;
97 } ts_section_t;
99 typedef struct {
100 int size;
101 unsigned char *start;
102 uint16_t payload_size;
103 es_stream_type_t type, subtype;
104 double pts, last_pts;
105 int pid;
106 char lang[4];
107 int last_cc; // last cc code (-1 if first packet)
108 int is_synced;
109 ts_section_t section;
110 uint8_t *extradata;
111 int extradata_alloc, extradata_len;
112 struct {
113 uint8_t au_start, au_end, last_au_end;
114 } sl;
115 } ES_stream_t;
117 typedef struct {
118 void *sh;
119 int id;
120 int type;
121 } sh_av_t;
123 typedef struct MpegTSContext {
124 int packet_size; // raw packet size, including FEC if present e.g. 188 bytes
125 ES_stream_t *pids[NB_PID_MAX];
126 sh_av_t streams[NB_PID_MAX];
127 } MpegTSContext;
130 typedef struct {
131 demux_stream_t *ds;
132 demux_packet_t *pack;
133 int offset, buffer_size;
134 } av_fifo_t;
136 #define MAX_EXTRADATA_SIZE 64*1024
137 typedef struct {
138 int32_t object_type; //aka codec used
139 int32_t stream_type; //video, audio etc.
140 uint8_t buf[MAX_EXTRADATA_SIZE];
141 uint16_t buf_size;
142 uint8_t szm1;
143 } mp4_decoder_config_t;
145 typedef struct {
146 //flags
147 uint8_t flags;
148 uint8_t au_start;
149 uint8_t au_end;
150 uint8_t random_accesspoint;
151 uint8_t random_accesspoint_only;
152 uint8_t padding;
153 uint8_t use_ts;
154 uint8_t idle;
155 uint8_t duration;
157 uint32_t ts_resolution, ocr_resolution;
158 uint8_t ts_len, ocr_len, au_len, instant_bitrate_len, degr_len, au_seqnum_len, packet_seqnum_len;
159 uint32_t timescale;
160 uint16_t au_duration, cts_duration;
161 uint64_t ocr, dts, cts;
162 } mp4_sl_config_t;
164 typedef struct {
165 uint16_t id;
166 uint8_t flags;
167 mp4_decoder_config_t decoder;
168 mp4_sl_config_t sl;
169 } mp4_es_descr_t;
171 typedef struct {
172 uint16_t id;
173 uint8_t flags;
174 mp4_es_descr_t *es;
175 uint16_t es_cnt;
176 } mp4_od_t;
178 typedef struct {
179 uint8_t skip;
180 uint8_t table_id;
181 uint8_t ssi;
182 uint16_t section_length;
183 uint16_t ts_id;
184 uint8_t version_number;
185 uint8_t curr_next;
186 uint8_t section_number;
187 uint8_t last_section_number;
188 struct pat_progs_t {
189 uint16_t id;
190 uint16_t pmt_pid;
191 } *progs;
192 uint16_t progs_cnt;
193 ts_section_t section;
194 } pat_t;
196 typedef struct {
197 uint16_t progid;
198 uint8_t skip;
199 uint8_t table_id;
200 uint8_t ssi;
201 uint16_t section_length;
202 uint8_t version_number;
203 uint8_t curr_next;
204 uint8_t section_number;
205 uint8_t last_section_number;
206 uint16_t PCR_PID;
207 uint16_t prog_descr_length;
208 ts_section_t section;
209 uint16_t es_cnt;
210 struct pmt_es_t {
211 uint16_t pid;
212 uint32_t type; //it's 8 bit long, but cast to the right type as FOURCC
213 uint16_t descr_length;
214 uint8_t format_descriptor[5];
215 uint8_t lang[4];
216 uint16_t mp4_es_id;
217 } *es;
218 mp4_od_t iod, *od;
219 mp4_es_descr_t *mp4es;
220 int od_cnt, mp4es_cnt;
221 } pmt_t;
223 typedef struct {
224 uint64_t size;
225 float duration;
226 double first_pts;
227 double last_pts;
228 } TS_stream_info;
230 typedef struct {
231 MpegTSContext ts;
232 int last_pid;
233 av_fifo_t fifo[3]; //0 for audio, 1 for video, 2 for subs
234 pat_t pat;
235 pmt_t *pmt;
236 uint16_t pmt_cnt;
237 uint32_t prog;
238 uint32_t vbitrate;
239 int keep_broken;
240 int last_aid;
241 int last_vid;
242 int last_sid;
243 char packet[TS_FEC_PACKET_SIZE];
244 TS_stream_info vstr, astr;
245 } ts_priv_t;
248 typedef struct {
249 es_stream_type_t type;
250 ts_section_t section;
251 } TS_pids_t;
254 static int IS_AUDIO(es_stream_type_t type)
256 switch (type) {
257 case AUDIO_MP2:
258 case AUDIO_A52:
259 case AUDIO_LPCM_BE:
260 case AUDIO_AAC:
261 case AUDIO_AAC_LATM:
262 case AUDIO_DTS:
263 case AUDIO_TRUEHD:
264 case AUDIO_S302M:
265 return 1;
267 return 0;
270 static int IS_VIDEO(es_stream_type_t type)
272 switch (type) {
273 case VIDEO_MPEG1:
274 case VIDEO_MPEG2:
275 case VIDEO_MPEG4:
276 case VIDEO_H264:
277 case VIDEO_AVC:
278 case VIDEO_DIRAC:
279 case VIDEO_VC1:
280 return 1;
282 return 0;
285 static int IS_SUB(es_stream_type_t type)
287 switch (type) {
288 case SPU_DVD:
289 case SPU_DVB:
290 case SPU_PGS:
291 case SPU_TELETEXT:
292 return 1;
294 return 0;
297 static int ts_parse(demuxer_t *demuxer, ES_stream_t *es, unsigned char *packet, int probe);
299 static uint8_t get_packet_size(const unsigned char *buf, int size)
301 int i;
303 if (size < (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS))
304 return 0;
306 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
308 if (buf[i * TS_PACKET_SIZE] != 0x47)
310 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
311 goto try_fec;
314 return TS_PACKET_SIZE;
316 try_fec:
317 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
319 if (buf[i * TS_FEC_PACKET_SIZE] != 0x47){
320 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
321 goto try_philips;
324 return TS_FEC_PACKET_SIZE;
326 try_philips:
327 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
329 if (buf[i * TS_PH_PACKET_SIZE] != 0x47)
330 return 0;
332 return TS_PH_PACKET_SIZE;
335 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h);
336 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid);
338 static void ts_add_stream(demuxer_t * demuxer, ES_stream_t *es)
340 int i;
341 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
343 if(priv->ts.streams[es->pid].sh)
344 return;
346 if((IS_AUDIO(es->type) || IS_AUDIO(es->subtype)) && priv->last_aid+1 < MAX_A_STREAMS)
348 sh_audio_t *sh = new_sh_audio_aid(demuxer, priv->last_aid, es->pid);
349 if(sh)
351 const char *lang = pid_lang_from_pmt(priv, es->pid);
352 sh->needs_parsing = 1;
353 sh->format = IS_AUDIO(es->type) ? es->type : es->subtype;
354 sh->ds = demuxer->audio;
356 priv->ts.streams[es->pid].id = priv->last_aid;
357 priv->ts.streams[es->pid].sh = sh;
358 priv->ts.streams[es->pid].type = TYPE_AUDIO;
359 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);
360 if (lang && lang[0])
361 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_AID_%d_LANG=%s\n", es->pid, lang);
362 priv->last_aid++;
365 if(es->extradata && es->extradata_len)
367 sh->wf = malloc(sizeof(*sh->wf) + es->extradata_len);
368 sh->wf->cbSize = es->extradata_len;
369 memcpy(sh->wf + 1, es->extradata, es->extradata_len);
373 if((IS_VIDEO(es->type) || IS_VIDEO(es->subtype)) && priv->last_vid+1 < MAX_V_STREAMS)
375 sh_video_t *sh = new_sh_video_vid(demuxer, priv->last_vid, es->pid);
376 if(sh)
378 sh->format = IS_VIDEO(es->type) ? es->type : es->subtype;
379 sh->ds = demuxer->video;
381 priv->ts.streams[es->pid].id = priv->last_vid;
382 priv->ts.streams[es->pid].sh = sh;
383 priv->ts.streams[es->pid].type = TYPE_VIDEO;
384 mp_msg(MSGT_DEMUX, MSGL_V, "\r\nADDED VIDEO PID %d, type: %x stream n. %d\r\n", es->pid, sh->format, priv->last_vid);
385 priv->last_vid++;
388 if(sh->format == VIDEO_AVC && es->extradata && es->extradata_len)
390 int w = 0, h = 0;
391 sh->bih = calloc(1, sizeof(*sh->bih) + es->extradata_len);
392 sh->bih->biSize= sizeof(*sh->bih) + es->extradata_len;
393 sh->bih->biCompression = sh->format;
394 memcpy(sh->bih + 1, es->extradata, es->extradata_len);
395 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "EXTRADATA(%d BYTES): \n", es->extradata_len);
396 for(i = 0;i < es->extradata_len; i++)
397 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "%02x ", (int) es->extradata[i]);
398 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"\n");
399 if(parse_avc_sps(es->extradata, es->extradata_len, &w, &h))
401 sh->bih->biWidth = w;
402 sh->bih->biHeight = h;
408 if(IS_SUB(es->type) && priv->last_sid+1 < MAX_S_STREAMS)
410 sh_sub_t *sh = new_sh_sub_sid_lang(demuxer, priv->last_sid, es->pid, pid_lang_from_pmt(priv, es->pid));
411 if (sh) {
412 switch (es->type) {
413 case SPU_DVB:
414 sh->type = 'b'; break;
415 case SPU_DVD:
416 sh->type = 'v'; break;
417 case SPU_PGS:
418 sh->type = 'p'; break;
420 priv->ts.streams[es->pid].id = priv->last_sid;
421 priv->ts.streams[es->pid].sh = sh;
422 priv->ts.streams[es->pid].type = TYPE_SUB;
423 priv->last_sid++;
428 static int ts_check_file(demuxer_t * demuxer)
430 const int buf_size = (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS);
431 unsigned char buf[TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS], done = 0, *ptr;
432 uint32_t _read, i, count = 0, is_ts;
433 int cc[NB_PID_MAX], last_cc[NB_PID_MAX], pid, cc_ok, c, good, bad;
434 uint8_t size = 0;
435 off_t pos = 0;
436 off_t init_pos;
438 mp_msg(MSGT_DEMUX, MSGL_V, "Checking for MPEG-TS...\n");
440 init_pos = stream_tell(demuxer->stream);
441 is_ts = 0;
442 while(! done)
444 i = 1;
445 c = 0;
447 while(((c=stream_read_char(demuxer->stream)) != 0x47)
448 && (c >= 0)
449 && (i < MAX_CHECK_SIZE)
450 && ! demuxer->stream->eof
451 ) i++;
454 if(c != 0x47)
456 mp_msg(MSGT_DEMUX, MSGL_V, "THIS DOESN'T LOOK LIKE AN MPEG-TS FILE!\n");
457 is_ts = 0;
458 done = 1;
459 continue;
462 pos = stream_tell(demuxer->stream) - 1;
463 buf[0] = c;
464 _read = stream_read(demuxer->stream, &buf[1], buf_size-1);
466 if(_read < buf_size-1)
468 mp_msg(MSGT_DEMUX, MSGL_V, "COULDN'T READ ENOUGH DATA, EXITING TS_CHECK\n");
469 stream_reset(demuxer->stream);
470 return 0;
473 size = get_packet_size(buf, buf_size);
474 if(size)
476 done = 1;
477 is_ts = 1;
480 if(pos - init_pos >= MAX_CHECK_SIZE)
482 done = 1;
483 is_ts = 0;
487 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);
488 stream_seek(demuxer->stream, pos);
490 if(! is_ts)
491 return 0;
493 //LET'S CHECK continuity counters
494 good = bad = 0;
495 for(count = 0; count < NB_PID_MAX; count++)
497 cc[count] = last_cc[count] = -1;
500 for(count = 0; count < NUM_CONSECUTIVE_TS_PACKETS; count++)
502 ptr = &(buf[size * count]);
503 pid = ((ptr[1] & 0x1f) << 8) | ptr[2];
504 mp_msg(MSGT_DEMUX, MSGL_DBG2, "BUF: %02x %02x %02x %02x, PID %d, SIZE: %d \n",
505 ptr[0], ptr[1], ptr[2], ptr[3], pid, size);
507 if((pid == 8191) || (pid < 16))
508 continue;
510 cc[pid] = (ptr[3] & 0xf);
511 cc_ok = (last_cc[pid] < 0) || ((((last_cc[pid] + 1) & 0x0f) == cc[pid]));
512 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PID %d, COMPARE CC %d AND LAST_CC %d\n", pid, cc[pid], last_cc[pid]);
513 if(! cc_ok)
514 //return 0;
515 bad++;
516 else
517 good++;
519 last_cc[pid] = cc[pid];
522 mp_msg(MSGT_DEMUX, MSGL_V, "GOOD CC: %d, BAD CC: %d\n", good, bad);
524 if(good >= bad)
525 return size;
526 else
527 return 0;
531 static inline int32_t progid_idx_in_pmt(ts_priv_t *priv, uint16_t progid)
533 int x;
535 if(priv->pmt == NULL)
536 return -1;
538 for(x = 0; x < priv->pmt_cnt; x++)
540 if(priv->pmt[x].progid == progid)
541 return x;
544 return -1;
548 static inline int32_t progid_for_pid(ts_priv_t *priv, int pid, int32_t req) //finds the first program listing a pid
550 int i, j;
551 pmt_t *pmt;
554 if(priv->pmt == NULL)
555 return -1;
558 for(i=0; i < priv->pmt_cnt; i++)
560 pmt = &(priv->pmt[i]);
562 if(pmt->es == NULL)
563 return -1;
565 for(j = 0; j < pmt->es_cnt; j++)
567 if(pmt->es[j].pid == pid)
569 if((req == 0) || (req == pmt->progid))
570 return pmt->progid;
575 return -1;
578 static inline int32_t prog_pcr_pid(ts_priv_t *priv, int progid)
580 int i;
582 if(priv->pmt == NULL)
583 return -1;
584 for(i=0; i < priv->pmt_cnt; i++)
586 if(priv->pmt[i].progid == progid)
587 return priv->pmt[i].PCR_PID;
589 return -1;
593 static inline int pid_match_lang(ts_priv_t *priv, uint16_t pid, char *lang)
595 uint16_t i, j;
596 pmt_t *pmt;
598 if(priv->pmt == NULL)
599 return -1;
601 for(i=0; i < priv->pmt_cnt; i++)
603 pmt = &(priv->pmt[i]);
605 if(pmt->es == NULL)
606 return -1;
608 for(j = 0; j < pmt->es_cnt; j++)
610 if(pmt->es[j].pid != pid)
611 continue;
613 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);
614 if(strncmp(pmt->es[j].lang, lang, 3) == 0)
616 return 1;
622 return -1;
625 typedef struct {
626 int32_t atype, vtype, stype; //types
627 int32_t apid, vpid, spid; //stream ids
628 char alang[4]; //languages
629 uint16_t prog;
630 off_t probe;
631 } tsdemux_init_t;
633 //second stage: returns the count of A52 syncwords found
634 static int a52_check(char *buf, int len)
636 int cnt, frame_length = 0, ok, srate;
638 cnt = ok = 0;
639 if(len < 8)
640 return 0;
642 while(cnt < len - 7)
644 if(buf[cnt] == 0x0B && buf[cnt+1] == 0x77)
646 frame_length = mp_a52_framesize(&buf[cnt], &srate);
647 if(frame_length>=7 && frame_length<=3840)
649 cnt += frame_length;
650 ok++;
652 else
653 cnt++;
655 else
656 cnt++;
659 mp_msg(MSGT_DEMUXER, MSGL_V, "A52_CHECK(%d input bytes), found %d frame syncwords of %d bytes length\n", len, ok, frame_length);
660 return ok;
664 static off_t ts_detect_streams(demuxer_t *demuxer, tsdemux_init_t *param)
666 int video_found = 0, audio_found = 0, i, num_packets = 0, req_apid, req_vpid, req_spid;
667 int is_audio, is_video, is_sub, has_tables;
668 int32_t p, chosen_pid = 0;
669 off_t pos=0, ret = 0, init_pos, end_pos;
670 ES_stream_t es;
671 unsigned char tmp[TS_FEC_PACKET_SIZE];
672 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
673 struct {
674 char *buf;
675 int pos;
676 } pes_priv1[8192], *pptr;
677 char *tmpbuf;
679 priv->last_pid = 8192; //invalid pid
681 req_apid = param->apid;
682 req_vpid = param->vpid;
683 req_spid = param->spid;
685 has_tables = 0;
686 memset(pes_priv1, 0, sizeof(pes_priv1));
687 init_pos = stream_tell(demuxer->stream);
688 mp_msg(MSGT_DEMUXER, MSGL_V, "PROBING UP TO %"PRIu64", PROG: %d\n", (uint64_t) param->probe, param->prog);
689 end_pos = init_pos + (param->probe ? param->probe : TS_MAX_PROBE_SIZE);
690 while(1)
692 pos = stream_tell(demuxer->stream);
693 if(pos > end_pos || demuxer->stream->eof)
694 break;
696 if(ts_parse(demuxer, &es, tmp, 1))
698 //Non PES-aligned A52 audio may escape detection if PMT is not present;
699 //in this case we try to find at least 3 A52 syncwords
700 if((es.type == PES_PRIVATE1) && (! audio_found) && req_apid > -2)
702 pptr = &pes_priv1[es.pid];
703 if(pptr->pos < 64*1024)
705 tmpbuf = realloc(pptr->buf, pptr->pos + es.size);
706 if(tmpbuf != NULL)
708 pptr->buf = tmpbuf;
709 memcpy(&(pptr->buf[ pptr->pos ]), es.start, es.size);
710 pptr->pos += es.size;
711 if(a52_check(pptr->buf, pptr->pos) > 2)
713 param->atype = AUDIO_A52;
714 param->apid = es.pid;
715 es.type = AUDIO_A52;
721 is_audio = IS_AUDIO(es.type) || ((es.type==SL_PES_STREAM) && IS_AUDIO(es.subtype));
722 is_video = IS_VIDEO(es.type) || ((es.type==SL_PES_STREAM) && IS_VIDEO(es.subtype));
723 is_sub = IS_SUB(es.type);
726 if((! is_audio) && (! is_video) && (! is_sub))
727 continue;
728 if(is_audio && req_apid==-2)
729 continue;
731 if(is_video)
733 chosen_pid = (req_vpid == es.pid);
734 if((! chosen_pid) && (req_vpid > 0))
735 continue;
737 else if(is_audio)
739 if(req_apid > 0)
741 chosen_pid = (req_apid == es.pid);
742 if(! chosen_pid)
743 continue;
745 else if(param->alang[0] > 0 && es.lang[0] > 0)
747 if(pid_match_lang(priv, es.pid, param->alang) == -1)
748 continue;
750 chosen_pid = 1;
751 param->apid = req_apid = es.pid;
754 else if(is_sub)
756 chosen_pid = (req_spid == es.pid);
757 if((! chosen_pid) && (req_spid > 0))
758 continue;
761 if(req_apid < 0 && (param->alang[0] == 0) && req_vpid < 0 && req_spid < 0)
762 chosen_pid = 1;
764 if((ret == 0) && chosen_pid)
766 ret = stream_tell(demuxer->stream);
769 p = progid_for_pid(priv, es.pid, param->prog);
770 if(p != -1)
772 has_tables++;
773 if(!param->prog && chosen_pid)
774 param->prog = p;
777 if((param->prog > 0) && (param->prog != p))
779 if(audio_found)
781 if(is_video && (req_vpid == es.pid))
783 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
784 param->vpid = es.pid;
785 video_found = 1;
786 break;
790 if(video_found)
792 if(is_audio && (req_apid == es.pid))
794 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
795 param->apid = es.pid;
796 audio_found = 1;
797 break;
802 continue;
806 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "TYPE: %x, PID: %d, PROG FOUND: %d\n", es.type, es.pid, param->prog);
809 if(is_video)
811 if((req_vpid == -1) || (req_vpid == es.pid))
813 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
814 param->vpid = es.pid;
815 video_found = 1;
820 if(((req_vpid == -2) || (num_packets >= NUM_CONSECUTIVE_AUDIO_PACKETS)) && audio_found && !param->probe)
822 //novideo or we have at least 348 audio packets (64 KB) without video (TS with audio only)
823 param->vtype = 0;
824 break;
827 if(is_sub)
829 if((req_spid == -1) || (req_spid == es.pid))
831 param->stype = es.type;
832 param->spid = es.pid;
836 if(is_audio)
838 if((req_apid == -1) || (req_apid == es.pid))
840 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
841 param->apid = es.pid;
842 audio_found = 1;
846 if(audio_found && (param->apid == es.pid) && (! video_found))
847 num_packets++;
849 if((has_tables==0) && (video_found && audio_found) && (pos >= 1000000))
850 break;
854 for(i=0; i<8192; i++)
856 if(pes_priv1[i].buf != NULL)
858 free(pes_priv1[i].buf);
859 pes_priv1[i].buf = NULL;
860 pes_priv1[i].pos = 0;
864 if(video_found)
866 if(param->vtype == VIDEO_MPEG1)
867 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG1(pid=%d) ", param->vpid);
868 else if(param->vtype == VIDEO_MPEG2)
869 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG2(pid=%d) ", param->vpid);
870 else if(param->vtype == VIDEO_MPEG4)
871 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG4(pid=%d) ", param->vpid);
872 else if(param->vtype == VIDEO_H264)
873 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO H264(pid=%d) ", param->vpid);
874 else if(param->vtype == VIDEO_VC1)
875 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO VC1(pid=%d) ", param->vpid);
876 else if(param->vtype == VIDEO_AVC)
877 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO AVC(NAL-H264, pid=%d) ", param->vpid);
879 else
881 param->vtype = UNKNOWN;
882 //WE DIDN'T MATCH ANY VIDEO STREAM
883 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO VIDEO! ");
886 if(param->atype == AUDIO_MP2)
887 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO MPA(pid=%d)", param->apid);
888 else if(param->atype == AUDIO_A52)
889 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO A52(pid=%d)", param->apid);
890 else if(param->atype == AUDIO_DTS)
891 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO DTS(pid=%d)", param->apid);
892 else if(param->atype == AUDIO_LPCM_BE)
893 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO LPCM(pid=%d)", param->apid);
894 else if(param->atype == AUDIO_AAC)
895 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO AAC(pid=%d)", param->apid);
896 else if(param->atype == AUDIO_AAC_LATM)
897 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO AAC LATM(pid=%d)", param->apid);
898 else if(param->atype == AUDIO_TRUEHD)
899 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO TRUEHD(pid=%d)", param->apid);
900 else if(param->atype == AUDIO_S302M)
901 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO S302M(pid=%d)", param->apid);
902 else
904 audio_found = 0;
905 param->atype = UNKNOWN;
906 //WE DIDN'T MATCH ANY AUDIO STREAM, SO WE FORCE THE DEMUXER TO IGNORE AUDIO
907 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO AUDIO! (try increasing -tsprobe)");
910 if(IS_SUB(param->stype))
911 mp_msg(MSGT_DEMUXER, MSGL_INFO, " SUB %s(pid=%d) ", (param->stype==SPU_DVD ? "DVD" : param->stype==SPU_DVB ? "DVB" : "Teletext"), param->spid);
912 else
914 param->stype = UNKNOWN;
915 mp_msg(MSGT_DEMUXER, MSGL_INFO, " NO SUBS (yet)! ");
918 if(video_found || audio_found)
920 if(!param->prog)
922 p = progid_for_pid(priv, video_found ? param->vpid : param->apid, 0);
923 if(p != -1)
924 param->prog = p;
927 if(demuxer->stream->eof && (ret == 0))
928 ret = init_pos;
929 mp_msg(MSGT_DEMUXER, MSGL_INFO, " PROGRAM N. %d\n", param->prog);
931 else
932 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\n");
935 for(i=0; i<NB_PID_MAX; i++)
937 if(priv->ts.pids[i] != NULL)
939 priv->ts.pids[i]->payload_size = 0;
940 priv->ts.pids[i]->pts = priv->ts.pids[i]->last_pts = 0;
941 priv->ts.pids[i]->last_cc = -1;
942 priv->ts.pids[i]->is_synced = 0;
946 return ret;
949 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h)
951 int sps, sps_len;
952 unsigned char *ptr;
953 mp_mpeg_header_t picture;
954 if(len < 6)
955 return 0;
956 sps = buf[5] & 0x1f;
957 if(!sps)
958 return 0;
959 sps_len = (buf[6] << 8) | buf[7];
960 if(!sps_len || (sps_len > len - 8))
961 return 0;
962 ptr = &(buf[8]);
963 picture.display_picture_width = picture.display_picture_height = 0;
964 h264_parse_sps(&picture, ptr, len - 8);
965 if(!picture.display_picture_width || !picture.display_picture_height)
966 return 0;
967 *w = picture.display_picture_width;
968 *h = picture.display_picture_height;
969 return 1;
972 static demuxer_t *demux_open_ts(demuxer_t * demuxer)
974 int i;
975 uint8_t packet_size;
976 sh_video_t *sh_video;
977 sh_audio_t *sh_audio;
978 off_t start_pos;
979 tsdemux_init_t params;
980 ts_priv_t * priv = demuxer->priv;
982 mp_msg(MSGT_DEMUX, MSGL_V, "DEMUX OPEN, AUDIO_ID: %d, VIDEO_ID: %d, SUBTITLE_ID: %d,\n",
983 demuxer->audio->id, demuxer->video->id, demuxer->sub->id);
986 demuxer->type= DEMUXER_TYPE_MPEG_TS;
989 stream_reset(demuxer->stream);
991 packet_size = ts_check_file(demuxer);
992 if(!packet_size)
993 return NULL;
995 priv = calloc(1, sizeof(ts_priv_t));
996 if(priv == NULL)
998 mp_msg(MSGT_DEMUX, MSGL_FATAL, "DEMUX_OPEN_TS, couldn't allocate enough memory for ts->priv, exit\n");
999 return NULL;
1002 for(i=0; i < NB_PID_MAX; i++)
1004 priv->ts.pids[i] = NULL;
1005 priv->ts.streams[i].id = -3;
1007 priv->pat.progs = NULL;
1008 priv->pat.progs_cnt = 0;
1009 priv->pat.section.buffer = NULL;
1010 priv->pat.section.buffer_len = 0;
1012 priv->pmt = NULL;
1013 priv->pmt_cnt = 0;
1015 priv->keep_broken = ts_keep_broken;
1016 priv->ts.packet_size = packet_size;
1019 demuxer->priv = priv;
1020 if(demuxer->stream->type != STREAMTYPE_FILE)
1021 demuxer->seekable = 1;
1022 else
1023 demuxer->seekable = 1;
1026 params.atype = params.vtype = params.stype = UNKNOWN;
1027 params.apid = demuxer->audio->id;
1028 params.vpid = demuxer->video->id;
1029 params.spid = demuxer->sub->id;
1030 params.prog = ts_prog;
1031 params.probe = ts_probe;
1033 if(demuxer->opts->audio_lang != NULL)
1035 strncpy(params.alang, demuxer->opts->audio_lang[0], 3);
1036 params.alang[3] = 0;
1038 else
1039 memset(params.alang, 0, 4);
1041 start_pos = ts_detect_streams(demuxer, &params);
1043 demuxer->sub->id = params.spid;
1044 priv->prog = params.prog;
1046 if(params.vtype != UNKNOWN)
1048 ts_add_stream(demuxer, priv->ts.pids[params.vpid]);
1049 sh_video = priv->ts.streams[params.vpid].sh;
1050 demuxer->video->id = priv->ts.streams[params.vpid].id;
1051 sh_video->ds = demuxer->video;
1052 sh_video->format = params.vtype;
1053 demuxer->video->sh = sh_video;
1056 if(params.atype != UNKNOWN)
1058 ES_stream_t *es = priv->ts.pids[params.apid];
1060 if(!IS_AUDIO(es->type) && !IS_AUDIO(es->subtype) && IS_AUDIO(params.atype)) es->subtype = params.atype;
1061 ts_add_stream(demuxer, priv->ts.pids[params.apid]);
1062 sh_audio = priv->ts.streams[params.apid].sh;
1063 demuxer->audio->id = priv->ts.streams[params.apid].id;
1064 sh_audio->ds = demuxer->audio;
1065 sh_audio->format = params.atype;
1066 demuxer->audio->sh = sh_audio;
1070 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);
1073 start_pos = start_pos <= priv->ts.packet_size ?
1074 demuxer->stream->start_pos :
1075 start_pos - priv->ts.packet_size;
1076 demuxer->movi_start = start_pos;
1077 demuxer->reference_clock = MP_NOPTS_VALUE;
1078 stream_reset(demuxer->stream);
1079 stream_seek(demuxer->stream, start_pos); //IF IT'S FROM A PIPE IT WILL FAIL, BUT WHO CARES?
1082 priv->last_pid = 8192; //invalid pid
1084 for(i = 0; i < 3; i++)
1086 priv->fifo[i].pack = NULL;
1087 priv->fifo[i].offset = 0;
1089 priv->fifo[0].ds = demuxer->audio;
1090 priv->fifo[1].ds = demuxer->video;
1091 priv->fifo[2].ds = demuxer->sub;
1093 priv->fifo[0].buffer_size = 1536;
1094 priv->fifo[1].buffer_size = 32767;
1095 priv->fifo[2].buffer_size = 32767;
1097 priv->pat.section.buffer_len = 0;
1098 for(i = 0; i < priv->pmt_cnt; i++)
1099 priv->pmt[i].section.buffer_len = 0;
1101 demuxer->filepos = stream_tell(demuxer->stream);
1102 return demuxer;
1105 static void demux_close_ts(demuxer_t * demuxer)
1107 uint16_t i;
1108 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
1110 if(priv)
1112 free(priv->pat.section.buffer);
1113 free(priv->pat.progs);
1115 if(priv->pmt)
1117 for(i = 0; i < priv->pmt_cnt; i++)
1119 free(priv->pmt[i].section.buffer);
1120 free(priv->pmt[i].es);
1122 free(priv->pmt);
1124 for (i = 0; i < NB_PID_MAX; i++)
1126 free(priv->ts.pids[i]);
1127 priv->ts.pids[i] = NULL;
1129 for (i = 0; i < 3; i++)
1131 if (priv->fifo[i].pack)
1132 free_demux_packet(priv->fifo[i].pack);
1133 priv->fifo[i].pack = NULL;
1135 free(priv);
1137 demuxer->priv=NULL;
1141 #define getbits mp_getbits
1143 static int mp4_parse_sl_packet(pmt_t *pmt, uint8_t *buf, uint16_t packet_len, int pid, ES_stream_t *pes_es)
1145 int i, n, m, mp4_es_id = -1;
1146 uint64_t v = 0;
1147 uint32_t pl_size = 0;
1148 int deg_flag = 0;
1149 mp4_es_descr_t *es = NULL;
1150 mp4_sl_config_t *sl = NULL;
1151 uint8_t au_start = 0, au_end = 0, rap_flag = 0, ocr_flag = 0, padding = 0, padding_bits = 0, idle = 0;
1153 pes_es->is_synced = 0;
1154 mp_msg(MSGT_DEMUXER,MSGL_V, "mp4_parse_sl_packet, pid: %d, pmt: %pm, packet_len: %d\n", pid, pmt, packet_len);
1155 if(! pmt || !packet_len)
1156 return 0;
1158 for(i = 0; i < pmt->es_cnt; i++)
1160 if(pmt->es[i].pid == pid)
1161 mp4_es_id = pmt->es[i].mp4_es_id;
1163 if(mp4_es_id < 0)
1164 return -1;
1166 for(i = 0; i < pmt->mp4es_cnt; i++)
1168 if(pmt->mp4es[i].id == mp4_es_id)
1169 es = &(pmt->mp4es[i]);
1171 if(! es)
1172 return -1;
1174 pes_es->subtype = es->decoder.object_type;
1176 sl = &(es->sl);
1177 if(!sl)
1178 return -1;
1180 //now es is the complete es_descriptor of out mp4 ES stream
1181 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "ID: %d, FLAGS: 0x%x, subtype: %x\n", es->id, sl->flags, pes_es->subtype);
1183 n = 0;
1184 if(sl->au_start)
1185 pes_es->sl.au_start = au_start = getbits(buf, n++, 1);
1186 else
1187 pes_es->sl.au_start = (pes_es->sl.last_au_end ? 1 : 0);
1188 if(sl->au_end)
1189 pes_es->sl.au_end = au_end = getbits(buf, n++, 1);
1191 if(!sl->au_start && !sl->au_end)
1193 pes_es->sl.au_start = pes_es->sl.au_end = au_start = au_end = 1;
1195 pes_es->sl.last_au_end = pes_es->sl.au_end;
1198 if(sl->ocr_len > 0)
1199 ocr_flag = getbits(buf, n++, 1);
1200 if(sl->idle)
1201 idle = getbits(buf, n++, 1);
1202 if(sl->padding)
1203 padding = getbits(buf, n++, 1);
1204 if(padding)
1206 padding_bits = getbits(buf, n, 3);
1207 n += 3;
1210 if(idle || (padding && !padding_bits))
1212 pes_es->payload_size = 0;
1213 return -1;
1216 //(! idle && (!padding || padding_bits != 0)) is true
1217 n += sl->packet_seqnum_len;
1218 if(sl->degr_len)
1219 deg_flag = getbits(buf, n++, 1);
1220 if(deg_flag)
1221 n += sl->degr_len;
1223 if(ocr_flag)
1225 n += sl->ocr_len;
1226 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "OCR: %d bits\n", sl->ocr_len);
1229 if(packet_len * 8 <= n)
1230 return -1;
1232 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "\nAU_START: %d, AU_END: %d\n", au_start, au_end);
1233 if(au_start)
1235 int dts_flag = 0, cts_flag = 0, ib_flag = 0;
1237 if(sl->random_accesspoint)
1238 rap_flag = getbits(buf, n++, 1);
1240 //check commented because it seems it's rarely used, and we need this flag set in case of au_start
1241 //the decoder will eventually discard the payload if it can't decode it
1242 //if(rap_flag || sl->random_accesspoint_only)
1243 pes_es->is_synced = 1;
1245 n += sl->au_seqnum_len;
1246 if(packet_len * 8 <= n+8)
1247 return -1;
1248 if(sl->use_ts)
1250 dts_flag = getbits(buf, n++, 1);
1251 cts_flag = getbits(buf, n++, 1);
1253 if(sl->instant_bitrate_len)
1254 ib_flag = getbits(buf, n++, 1);
1255 if(packet_len * 8 <= n+8)
1256 return -1;
1257 if(dts_flag && (sl->ts_len > 0))
1259 n += sl->ts_len;
1260 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "DTS: %d bits\n", sl->ts_len);
1262 if(packet_len * 8 <= n+8)
1263 return -1;
1264 if(cts_flag && (sl->ts_len > 0))
1266 int i = 0, m;
1268 while(i < sl->ts_len)
1270 m = FFMIN(8, sl->ts_len - i);
1271 v |= getbits(buf, n, m);
1272 if(sl->ts_len - i > 8)
1273 v <<= 8;
1274 i += m;
1275 n += m;
1276 if(packet_len * 8 <= n+8)
1277 return -1;
1280 pes_es->pts = (double) v / (double) sl->ts_resolution;
1281 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "CTS: %d bits, value: %"PRIu64"/%d = %.3f\n", sl->ts_len, v, sl->ts_resolution, pes_es->pts);
1285 i = 0;
1286 pl_size = 0;
1287 while(i < sl->au_len)
1289 m = FFMIN(8, sl->au_len - i);
1290 pl_size |= getbits(buf, n, m);
1291 if(sl->au_len - i > 8)
1292 pl_size <<= 8;
1293 i += m;
1294 n += m;
1295 if(packet_len * 8 <= n+8)
1296 return -1;
1298 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "AU_LEN: %u (%d bits)\n", pl_size, sl->au_len);
1299 if(ib_flag)
1300 n += sl->instant_bitrate_len;
1303 m = (n+7)/8;
1304 if(0 < pl_size && pl_size < pes_es->payload_size)
1305 pes_es->payload_size = pl_size;
1307 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",
1308 n, m, pes_es->payload_size, pl_size, (int) rap_flag, (int) sl->random_accesspoint_only);
1310 return m;
1313 //this function parses the extension fields in the PES header and returns the substream_id, or -1 in case of errors
1314 static int parse_pes_extension_fields(unsigned char *p, int pkt_len)
1316 int skip;
1317 unsigned char flags;
1319 if(!(p[7] & 0x1)) //no extension_field
1320 return -1;
1321 skip = 9;
1322 if(p[7] & 0x80)
1324 skip += 5;
1325 if(p[7] & 0x40)
1326 skip += 5;
1328 if(p[7] & 0x20) //escr_flag
1329 skip += 6;
1330 if(p[7] & 0x10) //es_rate_flag
1331 skip += 3;
1332 if(p[7] & 0x08)//dsm_trick_mode is unsupported, skip
1334 skip = 0;//don't let's parse the extension fields
1336 if(p[7] & 0x04) //additional_copy_info
1337 skip += 1;
1338 if(p[7] & 0x02) //pes_crc_flag
1339 skip += 2;
1340 if(skip >= pkt_len) //too few bytes
1341 return -1;
1342 flags = p[skip];
1343 skip++;
1344 if(flags & 0x80) //pes_private_data_flag
1345 skip += 16;
1346 if(skip >= pkt_len)
1347 return -1;
1348 if(flags & 0x40) //pack_header_field_flag
1350 unsigned char l = p[skip];
1351 skip += l;
1353 if(flags & 0x20) //program_packet_sequence_counter
1354 skip += 2;
1355 if(flags & 0x10) //p_std
1356 skip += 2;
1357 if(skip >= pkt_len)
1358 return -1;
1359 if(flags & 0x01) //finally the long desired pes_extension2
1361 unsigned char l = p[skip]; //ext2 flag+len
1362 skip++;
1363 if((l == 0x81) && (skip < pkt_len))
1365 int ssid = p[skip];
1366 mp_msg(MSGT_IDENTIFY, MSGL_V, "SUBSTREAM_ID=%d (0x%02X)\n", ssid, ssid);
1367 return ssid;
1371 return -1;
1374 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)
1376 unsigned char *p;
1377 uint32_t header_len;
1378 int64_t pts;
1379 uint32_t stream_id;
1380 uint32_t pkt_len, pes_is_aligned;
1382 //Here we are always at the start of a PES packet
1383 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2(%p, %d): \n", buf, (uint32_t) packet_len);
1385 if(packet_len == 0 || packet_len > 184)
1387 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2, BUFFER LEN IS TOO SMALL OR TOO BIG: %d EXIT\n", packet_len);
1388 return 0;
1391 p = buf;
1392 pkt_len = packet_len;
1395 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: HEADER %02x %02x %02x %02x\n", p[0], p[1], p[2], p[3]);
1396 if (p[0] || p[1] || (p[2] != 1))
1398 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: error HEADER %02x %02x %02x (should be 0x000001) \n", p[0], p[1], p[2]);
1399 return 0 ;
1402 packet_len -= 6;
1403 if(packet_len==0)
1405 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: packet too short: %d, exit\n", packet_len);
1406 return 0;
1409 es->payload_size = (p[4] << 8 | p[5]);
1410 pes_is_aligned = (p[6] & 4);
1412 stream_id = p[3];
1415 if (p[7] & 0x80)
1416 { /* pts available */
1417 pts = (int64_t)(p[9] & 0x0E) << 29 ;
1418 pts |= p[10] << 22 ;
1419 pts |= (p[11] & 0xFE) << 14 ;
1420 pts |= p[12] << 7 ;
1421 pts |= (p[13] & 0xFE) >> 1 ;
1423 es->pts = pts / 90000.0;
1425 else
1426 es->pts = 0.0;
1429 header_len = p[8];
1432 if (header_len + 9 > pkt_len) //9 are the bytes read up to the header_length field
1434 mp_msg(MSGT_DEMUX, MSGL_DBG2, "demux_ts: illegal value for PES_header_data_length (0x%02x)\n", header_len);
1435 return 0;
1438 if(stream_id==0xfd)
1440 int ssid = parse_pes_extension_fields(p, pkt_len);
1441 if((audio_substream_id!=-1) && (ssid != audio_substream_id))
1442 return 0;
1443 if(ssid == 0x72 && type_from_pmt != AUDIO_DTS && type_from_pmt != SPU_PGS)
1444 es->type = type_from_pmt = AUDIO_TRUEHD;
1447 p += header_len + 9;
1448 packet_len -= header_len + 3;
1450 if(es->payload_size)
1451 es->payload_size -= header_len + 3;
1454 es->is_synced = 1; //only for SL streams we have to make sure it's really true, see below
1455 if (stream_id == 0xbd)
1457 mp_msg(MSGT_DEMUX, MSGL_DBG3, "pes_parse2: audio buf = %02X %02X %02X %02X %02X %02X %02X %02X, 80: %d\n",
1458 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[0] & 0x80);
1462 * we check the descriptor tag first because some stations
1463 * do not include any of the A52 header info in their audio tracks
1464 * these "raw" streams may begin with a byte that looks like a stream type.
1468 if(type_from_pmt == SPU_PGS)
1470 es->start = p;
1471 es->size = packet_len;
1472 es->type = SPU_PGS;
1473 es->payload_size -= packet_len;
1474 return 1;
1477 (type_from_pmt == AUDIO_A52) || /* A52 - raw */
1478 (packet_len >= 2 && p[0] == 0x0B && p[1] == 0x77) /* A52 - syncword */
1481 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 RAW OR SYNCWORD\n");
1482 es->start = p;
1483 es->size = packet_len;
1484 es->type = AUDIO_A52;
1485 es->payload_size -= packet_len;
1487 return 1;
1489 /* SPU SUBS */
1490 else if(type_from_pmt == SPU_DVB ||
1491 (packet_len >= 2 && (p[0] == 0x20) && pes_is_aligned)) // && p[1] == 0x00))
1493 // offset/length fiddling to make decoding with lavc possible
1494 es->start = p + 2;
1495 es->size = packet_len - 2;
1496 es->type = SPU_DVB;
1497 es->payload_size -= packet_len;
1499 return 1;
1501 else if (pes_is_aligned && packet_len >= 1 && ((p[0] & 0xE0) == 0x20)) //SPU_DVD
1503 //DVD SUBS
1504 es->start = p+1;
1505 es->size = packet_len-1;
1506 es->type = SPU_DVD;
1507 es->payload_size -= packet_len;
1509 return 1;
1511 else if (pes_is_aligned && packet_len >= 4 && (p[0] & 0xF8) == 0x80)
1513 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 WITH HEADER\n");
1514 es->start = p+4;
1515 es->size = packet_len - 4;
1516 es->type = AUDIO_A52;
1517 es->payload_size -= packet_len;
1519 return 1;
1521 else if (pes_is_aligned && packet_len >= 1 && ((p[0]&0xf0) == 0xa0))
1523 int pcm_offset;
1525 for (pcm_offset=0; ++pcm_offset < packet_len-1 ; )
1527 if (p[pcm_offset] == 0x01 && p[pcm_offset+1] == 0x80)
1528 { /* START */
1529 pcm_offset += 2;
1530 break;
1534 es->start = p + pcm_offset;
1535 es->size = packet_len - pcm_offset;
1536 es->type = AUDIO_LPCM_BE;
1537 es->payload_size -= packet_len;
1539 return 1;
1541 else
1543 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PES_PRIVATE1\n");
1544 es->start = p;
1545 es->size = packet_len;
1546 es->type = (type_from_pmt == UNKNOWN ? PES_PRIVATE1 : type_from_pmt);
1547 es->payload_size -= packet_len;
1549 return 1;
1552 else if((stream_id >= 0xe0 && stream_id <= 0xef) || (stream_id == 0xfd && type_from_pmt != UNKNOWN))
1554 es->start = p;
1555 es->size = packet_len;
1556 if(type_from_pmt != UNKNOWN)
1557 es->type = type_from_pmt;
1558 else
1559 es->type = VIDEO_MPEG2;
1560 if(es->payload_size)
1561 es->payload_size -= packet_len;
1563 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: M2V size %d\n", es->size);
1564 return 1;
1566 else if (stream_id == 0xfa)
1568 int l;
1570 es->is_synced = 0;
1571 if(type_from_pmt != UNKNOWN) //MP4 A/V or SL
1573 es->start = p;
1574 es->size = packet_len;
1575 es->type = type_from_pmt;
1577 if(type_from_pmt == SL_PES_STREAM)
1579 //if(pes_is_aligned)
1581 l = mp4_parse_sl_packet(pmt, p, packet_len, pid, es);
1582 mp_msg(MSGT_DEMUX, MSGL_DBG2, "L=%d, TYPE=%x\n", l, type_from_pmt);
1583 if(l < 0)
1585 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: couldn't parse SL header, passing along full PES payload\n");
1586 l = 0;
1590 es->start += l;
1591 es->size -= l;
1594 if(es->payload_size)
1595 es->payload_size -= packet_len;
1596 return 1;
1599 else if ((stream_id & 0xe0) == 0xc0)
1601 es->start = p;
1602 es->size = packet_len;
1604 if(type_from_pmt != UNKNOWN)
1605 es->type = type_from_pmt;
1606 else
1607 es->type = AUDIO_MP2;
1609 es->payload_size -= packet_len;
1611 return 1;
1613 else if (type_from_pmt != -1) //as a last resort here we trust the PMT, if present
1615 es->start = p;
1616 es->size = packet_len;
1617 es->type = type_from_pmt;
1618 es->payload_size -= packet_len;
1620 return 1;
1622 else
1624 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: unknown packet, id: %x\n", stream_id);
1627 es->is_synced = 0;
1628 return 0;
1634 static int ts_sync(stream_t *stream)
1636 mp_msg(MSGT_DEMUX, MSGL_DBG3, "TS_SYNC \n");
1638 while (!stream->eof)
1639 if (stream_read_char(stream) == 0x47)
1640 return 1;
1642 return 0;
1646 static void ts_dump_streams(ts_priv_t *priv)
1648 int i;
1650 for(i = 0; i < 3; i++)
1652 if((priv->fifo[i].pack != NULL) && (priv->fifo[i].offset != 0))
1654 resize_demux_packet(priv->fifo[i].pack, priv->fifo[i].offset);
1655 ds_add_packet(priv->fifo[i].ds, priv->fifo[i].pack);
1656 priv->fifo[i].offset = 0;
1657 priv->fifo[i].pack = NULL;
1663 static inline int32_t prog_idx_in_pat(ts_priv_t *priv, uint16_t progid)
1665 int x;
1667 if(priv->pat.progs == NULL)
1668 return -1;
1670 for(x = 0; x < priv->pat.progs_cnt; x++)
1672 if(priv->pat.progs[x].id == progid)
1673 return x;
1676 return -1;
1680 static inline int32_t prog_id_in_pat(ts_priv_t *priv, uint16_t pid)
1682 int x;
1684 if(priv->pat.progs == NULL)
1685 return -1;
1687 for(x = 0; x < priv->pat.progs_cnt; x++)
1689 if(priv->pat.progs[x].pmt_pid == pid)
1690 return priv->pat.progs[x].id;
1693 return -1;
1696 static int collect_section(ts_section_t *section, int is_start, unsigned char *buff, int size)
1698 uint8_t *ptr;
1699 uint16_t tlen;
1700 int skip, tid;
1702 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, start: %d, size: %d, collected: %d\n", is_start, size, section->buffer_len);
1703 if(! is_start && !section->buffer_len)
1704 return 0;
1706 if(is_start)
1708 if(! section->buffer)
1710 section->buffer = malloc(4096 + 256);
1711 if(section->buffer == NULL)
1712 return 0;
1714 section->buffer_len = 0;
1717 if(size + section->buffer_len > 4096+256)
1719 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, excessive len: %d + %d\n", section->buffer_len, size);
1720 return 0;
1723 memcpy(&(section->buffer[section->buffer_len]), buff, size);
1724 section->buffer_len += size;
1726 if(section->buffer_len < 3)
1727 return 0;
1729 skip = section->buffer[0];
1730 if(skip + 4 > section->buffer_len)
1731 return 0;
1733 ptr = &(section->buffer[skip + 1]);
1734 tid = ptr[0];
1735 tlen = ((ptr[1] & 0x0f) << 8) | ptr[2];
1736 mp_msg(MSGT_DEMUX, MSGL_V, "SKIP: %d+1, TID: %d, TLEN: %d, COLLECTED: %d\n", skip, tid, tlen, section->buffer_len);
1737 if(section->buffer_len < (skip+1+3+tlen))
1739 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DATA IS NOT ENOUGH, NEXT TIME\n");
1740 return 0;
1743 return skip+1;
1746 static int parse_pat(ts_priv_t * priv, int is_start, unsigned char *buff, int size)
1748 int skip;
1749 unsigned char *ptr;
1750 unsigned char *base;
1751 int entries, i;
1752 uint16_t progid;
1753 struct pat_progs_t *tmp;
1754 ts_section_t *section;
1756 section = &(priv->pat.section);
1757 skip = collect_section(section, is_start, buff, size);
1758 if(! skip)
1759 return 0;
1761 ptr = &(section->buffer[skip]);
1762 //PARSING
1763 priv->pat.table_id = ptr[0];
1764 if(priv->pat.table_id != 0)
1765 return 0;
1766 priv->pat.ssi = (ptr[1] >> 7) & 0x1;
1767 priv->pat.curr_next = ptr[5] & 0x01;
1768 priv->pat.ts_id = (ptr[3] << 8 ) | ptr[4];
1769 priv->pat.version_number = (ptr[5] >> 1) & 0x1F;
1770 priv->pat.section_length = ((ptr[1] & 0x03) << 8 ) | ptr[2];
1771 priv->pat.section_number = ptr[6];
1772 priv->pat.last_section_number = ptr[7];
1774 //check_crc32(0xFFFFFFFFL, ptr, priv->pat.buffer_len - 4, &ptr[priv->pat.buffer_len - 4]);
1775 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);
1777 entries = (int) (priv->pat.section_length - 9) / 4; //entries per section
1779 for(i=0; i < entries; i++)
1781 int32_t idx;
1782 base = &ptr[8 + i*4];
1783 progid = (base[0] << 8) | base[1];
1785 if((idx = prog_idx_in_pat(priv, progid)) == -1)
1787 int sz = sizeof(struct pat_progs_t) * (priv->pat.progs_cnt+1);
1788 tmp = realloc_struct(priv->pat.progs, priv->pat.progs_cnt+1, sizeof(struct pat_progs_t));
1789 if(tmp == NULL)
1791 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PAT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
1792 break;
1794 priv->pat.progs = tmp;
1795 idx = priv->pat.progs_cnt;
1796 priv->pat.progs_cnt++;
1799 priv->pat.progs[idx].id = progid;
1800 priv->pat.progs[idx].pmt_pid = ((base[2] & 0x1F) << 8) | base[3];
1801 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);
1802 mp_msg(MSGT_IDENTIFY, MSGL_V, "PROGRAM_ID=%d (0x%02X), PMT_PID: %d(0x%02X)\n",
1803 progid, progid, priv->pat.progs[idx].pmt_pid, priv->pat.progs[idx].pmt_pid);
1806 return 1;
1810 static inline int32_t es_pid_in_pmt(pmt_t * pmt, uint16_t pid)
1812 uint16_t i;
1814 if(pmt == NULL)
1815 return -1;
1817 if(pmt->es == NULL)
1818 return -1;
1820 for(i = 0; i < pmt->es_cnt; i++)
1822 if(pmt->es[i].pid == pid)
1823 return (int32_t) i;
1826 return -1;
1830 static uint16_t get_mp4_desc_len(uint8_t *buf, int *len)
1832 //uint16_t i = 0, size = 0;
1833 int i = 0, j, size = 0;
1835 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PARSE_MP4_DESC_LEN(%d), bytes: ", *len);
1836 j = FFMIN(*len, 4);
1837 while(i < j)
1839 mp_msg(MSGT_DEMUX, MSGL_DBG2, " %x ", buf[i]);
1840 size |= (buf[i] & 0x7f);
1841 if(!(buf[i] & 0x80))
1842 break;
1843 size <<= 7;
1844 i++;
1846 mp_msg(MSGT_DEMUX, MSGL_DBG2, ", SIZE=%d\n", size);
1848 *len = i+1;
1849 return size;
1853 static uint16_t parse_mp4_slconfig_descriptor(uint8_t *buf, int len, void *elem)
1855 int i = 0;
1856 mp4_es_descr_t *es;
1857 mp4_sl_config_t *sl;
1859 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_SLCONFIG_DESCRIPTOR(%d)\n", len);
1860 es = (mp4_es_descr_t *) elem;
1861 if(!es)
1863 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1864 return len;
1866 sl = &(es->sl);
1868 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;
1869 sl->ocr = sl->dts = sl->cts = 0;
1871 if(buf[0] == 0)
1873 i++;
1874 sl->flags = buf[i];
1875 i++;
1876 sl->ts_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1877 i += 4;
1878 sl->ocr_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1879 i += 4;
1880 sl->ts_len = buf[i];
1881 i++;
1882 sl->ocr_len = buf[i];
1883 i++;
1884 sl->au_len = buf[i];
1885 i++;
1886 sl->instant_bitrate_len = buf[i];
1887 i++;
1888 sl->degr_len = (buf[i] >> 4) & 0x0f;
1889 sl->au_seqnum_len = ((buf[i] & 0x0f) << 1) | ((buf[i+1] >> 7) & 0x01);
1890 i++;
1891 sl->packet_seqnum_len = ((buf[i] >> 2) & 0x1f);
1892 i++;
1895 else if(buf[0] == 1)
1897 sl->flags = 0;
1898 sl->ts_resolution = 1000;
1899 sl->ts_len = 32;
1900 i++;
1902 else if(buf[0] == 2)
1904 sl->flags = 4;
1905 i++;
1907 else
1909 sl->flags = 0;
1910 i++;
1913 sl->au_start = (sl->flags >> 7) & 0x1;
1914 sl->au_end = (sl->flags >> 6) & 0x1;
1915 sl->random_accesspoint = (sl->flags >> 5) & 0x1;
1916 sl->random_accesspoint_only = (sl->flags >> 4) & 0x1;
1917 sl->padding = (sl->flags >> 3) & 0x1;
1918 sl->use_ts = (sl->flags >> 2) & 0x1;
1919 sl->idle = (sl->flags >> 1) & 0x1;
1920 sl->duration = sl->flags & 0x1;
1922 if(sl->duration)
1924 sl->timescale = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1925 i += 4;
1926 sl->au_duration = (buf[i] << 8) | buf[i+1];
1927 i += 2;
1928 sl->cts_duration = (buf[i] << 8) | buf[i+1];
1929 i += 2;
1931 else //no support for fixed durations atm
1932 sl->timescale = sl->au_duration = sl->cts_duration = 0;
1934 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",
1935 len, buf[0], sl->flags, sl->use_ts, sl->ts_len, sl->timescale, (uint64_t) sl->dts, (uint64_t) sl->cts);
1937 return len;
1940 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem);
1942 static uint16_t parse_mp4_decoder_config_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
1944 int i = 0, j;
1945 mp4_es_descr_t *es;
1946 mp4_decoder_config_t *dec;
1948 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_CONFIG_DESCRIPTOR(%d)\n", len);
1949 es = (mp4_es_descr_t *) elem;
1950 if(!es)
1952 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1953 return len;
1955 dec = (mp4_decoder_config_t*) &(es->decoder);
1957 dec->object_type = buf[i];
1958 dec->stream_type = (buf[i+1]>>2) & 0x3f;
1960 if(dec->object_type == 1 && dec->stream_type == 1)
1962 dec->object_type = MP4_OD;
1963 dec->stream_type = MP4_OD;
1965 else if(dec->stream_type == 4)
1967 if(dec->object_type == 0x6a)
1968 dec->object_type = VIDEO_MPEG1;
1969 if(dec->object_type >= 0x60 && dec->object_type <= 0x65)
1970 dec->object_type = VIDEO_MPEG2;
1971 else if(dec->object_type == 0x20)
1972 dec->object_type = VIDEO_MPEG4;
1973 else if(dec->object_type == 0x21)
1974 dec->object_type = VIDEO_AVC;
1975 /*else if(dec->object_type == 0x22)
1976 fprintf(stderr, "TYPE 0x22\n");*/
1977 else dec->object_type = UNKNOWN;
1979 else if(dec->stream_type == 5)
1981 if(dec->object_type == 0x40)
1982 dec->object_type = AUDIO_AAC;
1983 else if(dec->object_type == 0x6b)
1984 dec->object_type = AUDIO_MP2;
1985 else if(dec->object_type >= 0x66 && dec->object_type <= 0x69)
1986 dec->object_type = AUDIO_MP2;
1987 else
1988 dec->object_type = UNKNOWN;
1990 else
1991 dec->object_type = dec->stream_type = UNKNOWN;
1993 if(dec->object_type != UNKNOWN)
1995 //update the type of the current stream
1996 for(j = 0; j < pmt->es_cnt; j++)
1998 if(pmt->es[j].mp4_es_id == es->id)
2000 pmt->es[j].type = SL_PES_STREAM;
2005 if(len > 13)
2006 parse_mp4_descriptors(pmt, &buf[13], len-13, dec);
2008 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);
2010 return len;
2013 static uint16_t parse_mp4_decoder_specific_descriptor(uint8_t *buf, int len, void *elem)
2015 int i;
2016 mp4_decoder_config_t *dec;
2018 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_SPECIFIC_DESCRIPTOR(%d)\n", len);
2019 dec = (mp4_decoder_config_t *) elem;
2020 if(!dec)
2022 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
2023 return len;
2026 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4 SPECIFIC INFO BYTES: \n");
2027 for(i=0; i<len; i++)
2028 mp_msg(MSGT_DEMUX, MSGL_DBG2, "%02x ", buf[i]);
2029 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\n");
2031 if(len > MAX_EXTRADATA_SIZE)
2033 mp_msg(MSGT_DEMUX, MSGL_ERR, "DEMUX_TS, EXTRADATA SUSPICIOUSLY BIG: %d, REFUSED\r\n", len);
2034 return len;
2036 memcpy(dec->buf, buf, len);
2037 dec->buf_size = len;
2039 return len;
2042 static uint16_t parse_mp4_es_descriptor(pmt_t *pmt, uint8_t *buf, int len)
2044 int i = 0, j = 0, k, found;
2045 uint8_t flag;
2046 mp4_es_descr_t es, *target_es = NULL, *tmp;
2048 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4ES: len=%d\n", len);
2049 memset(&es, 0, sizeof(mp4_es_descr_t));
2050 while(i < len)
2052 es.id = (buf[i] << 8) | buf[i+1];
2053 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_ID: %d\n", es.id);
2054 i += 2;
2055 flag = buf[i];
2056 i++;
2057 if(flag & 0x80)
2058 i += 2;
2059 if(flag & 0x40)
2060 i += buf[i]+1;
2061 if(flag & 0x20) //OCR, maybe we need it
2062 i += 2;
2064 j = parse_mp4_descriptors(pmt, &buf[i], len-i, &es);
2065 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);
2066 if(es.decoder.object_type != UNKNOWN && es.decoder.stream_type != UNKNOWN)
2068 found = 0;
2069 //search this ES_ID if we already have it
2070 for(k=0; k < pmt->mp4es_cnt; k++)
2072 if(pmt->mp4es[k].id == es.id)
2074 target_es = &(pmt->mp4es[k]);
2075 found = 1;
2079 if(! found)
2081 tmp = realloc_struct(pmt->mp4es, pmt->mp4es_cnt+1, sizeof(mp4_es_descr_t));
2082 if(tmp == NULL)
2084 fprintf(stderr, "CAN'T REALLOC MP4_ES_DESCR\n");
2085 continue;
2087 pmt->mp4es = tmp;
2088 target_es = &(pmt->mp4es[pmt->mp4es_cnt]);
2089 pmt->mp4es_cnt++;
2091 memcpy(target_es, &es, sizeof(mp4_es_descr_t));
2092 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_CNT: %d, ID=%d\n", pmt->mp4es_cnt, target_es->id);
2095 i += j;
2098 return len;
2101 static void parse_mp4_object_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2103 int i, j = 0, id;
2105 i=0;
2106 id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2107 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_OBJECT_DESCRIPTOR: len=%d, OD_ID=%d\n", len, id);
2108 if(buf[1] & 0x20)
2110 i += buf[2] + 1; //url
2111 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2113 else
2115 i = 2;
2117 while(i < len)
2119 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2120 mp_msg(MSGT_DEMUX, MSGL_V, "OBJD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2121 i += j;
2127 static void parse_mp4_iod(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2129 int i, j = 0;
2130 mp4_od_t *iod = &(pmt->iod);
2132 iod->id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2133 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_IOD: len=%d, IOD_ID=%d\n", len, iod->id);
2134 i = 2;
2135 if(buf[1] & 0x20)
2137 i += buf[2] + 1; //url
2138 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2140 else
2142 i = 7;
2143 while(i < len)
2145 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2146 mp_msg(MSGT_DEMUX, MSGL_V, "IOD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2147 i += j;
2152 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2154 int tag, descr_len, i = 0, j = 0;
2156 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DESCRIPTORS, len=%d\n", len);
2157 if(! len)
2158 return len;
2160 while(i < len)
2162 tag = buf[i];
2163 j = len - i -1;
2164 descr_len = get_mp4_desc_len(&(buf[i+1]), &j);
2165 mp_msg(MSGT_DEMUX, MSGL_V, "TAG=%d (0x%x), DESCR_len=%d, len=%d, j=%d\n", tag, tag, descr_len, len, j);
2166 if(descr_len > len - j+1)
2168 mp_msg(MSGT_DEMUX, MSGL_V, "descriptor is too long, exit\n");
2169 return len;
2171 i += j+1;
2173 switch(tag)
2175 case 0x1:
2176 parse_mp4_object_descriptor(pmt, &(buf[i]), descr_len, elem);
2177 break;
2178 case 0x2:
2179 parse_mp4_iod(pmt, &(buf[i]), descr_len, elem);
2180 break;
2181 case 0x3:
2182 parse_mp4_es_descriptor(pmt, &(buf[i]), descr_len);
2183 break;
2184 case 0x4:
2185 parse_mp4_decoder_config_descriptor(pmt, &buf[i], descr_len, elem);
2186 break;
2187 case 0x05:
2188 parse_mp4_decoder_specific_descriptor(&buf[i], descr_len, elem);
2189 break;
2190 case 0x6:
2191 parse_mp4_slconfig_descriptor(&buf[i], descr_len, elem);
2192 break;
2193 default:
2194 mp_msg(MSGT_DEMUX, MSGL_V, "Unsupported mp4 descriptor 0x%x\n", tag);
2196 i += descr_len;
2199 return len;
2202 static ES_stream_t *new_pid(ts_priv_t *priv, int pid)
2204 ES_stream_t *tss;
2206 tss = calloc(sizeof(*tss), 1);
2207 if(! tss)
2208 return NULL;
2209 tss->pid = pid;
2210 tss->last_cc = -1;
2211 tss->type = UNKNOWN;
2212 tss->subtype = UNKNOWN;
2213 tss->is_synced = 0;
2214 tss->extradata = NULL;
2215 tss->extradata_alloc = tss->extradata_len = 0;
2216 priv->ts.pids[pid] = tss;
2218 return tss;
2222 static int parse_program_descriptors(pmt_t *pmt, uint8_t *buf, uint16_t len)
2224 uint16_t i = 0, k, olen = len;
2226 while(len > 0)
2228 mp_msg(MSGT_DEMUX, MSGL_V, "PROG DESCR, TAG=%x, LEN=%d(%x)\n", buf[i], buf[i+1], buf[i+1]);
2229 if(buf[i+1] > len-2)
2231 mp_msg(MSGT_DEMUX, MSGL_V, "ERROR, descriptor len is too long, skipping\n");
2232 return olen;
2235 if(buf[i] == 0x1d)
2237 if(buf[i+3] == 2) //buggy versions of vlc muxer make this non-standard mess (missing iod_scope)
2238 k = 3;
2239 else
2240 k = 4; //this is standard compliant
2241 parse_mp4_descriptors(pmt, &buf[i+k], (int) buf[i+1]-(k-2), NULL);
2244 len -= 2 + buf[i+1];
2247 return olen;
2250 static int parse_descriptors(struct pmt_es_t *es, uint8_t *ptr)
2252 int j, descr_len, len;
2254 j = 0;
2255 len = es->descr_length;
2256 while(len > 2)
2258 descr_len = ptr[j+1];
2259 mp_msg(MSGT_DEMUX, MSGL_V, "...descr id: 0x%x, len=%d\n", ptr[j], descr_len);
2260 if(descr_len > len)
2262 mp_msg(MSGT_DEMUX, MSGL_ERR, "INVALID DESCR LEN for tag %02x: %d vs %d max, EXIT LOOP\n", ptr[j], descr_len, len);
2263 return -1;
2267 if(ptr[j] == 0x6a || ptr[j] == 0x7a) //A52 Descriptor
2269 if(es->type == 0x6)
2271 es->type = AUDIO_A52;
2272 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB A52 Descriptor\n");
2275 else if(ptr[j] == 0x7b) //DVB DTS Descriptor
2277 if(es->type == 0x6)
2279 es->type = AUDIO_DTS;
2280 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB DTS Descriptor\n");
2283 else if(ptr[j] == 0x56) // Teletext
2285 if(descr_len >= 5) {
2286 memcpy(es->lang, ptr+2, 3);
2287 es->lang[3] = 0;
2289 es->type = SPU_TELETEXT;
2291 else if(ptr[j] == 0x59) //Subtitling Descriptor
2293 uint8_t subtype;
2295 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Subtitling Descriptor\n");
2296 if(descr_len < 8)
2298 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Descriptor length too short for DVB Subtitle Descriptor: %d, SKIPPING\n", descr_len);
2300 else
2302 memcpy(es->lang, &ptr[j+2], 3);
2303 es->lang[3] = 0;
2304 subtype = ptr[j+5];
2306 (subtype >= 0x10 && subtype <= 0x13) ||
2307 (subtype >= 0x20 && subtype <= 0x23)
2310 es->type = SPU_DVB;
2311 //page parameters: compo page 2 bytes, ancillary page 2 bytes
2313 else
2314 es->type = UNKNOWN;
2317 else if(ptr[j] == 0x50) //Component Descriptor
2319 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Component Descriptor\n");
2320 memcpy(es->lang, &ptr[j+5], 3);
2321 es->lang[3] = 0;
2323 else if(ptr[j] == 0xa) //Language Descriptor
2325 memcpy(es->lang, &ptr[j+2], 3);
2326 es->lang[3] = 0;
2327 mp_msg(MSGT_DEMUX, MSGL_V, "Language Descriptor: %s\n", es->lang);
2329 else if(ptr[j] == 0x5) //Registration Descriptor (looks like e fourCC :) )
2331 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor\n");
2332 if(descr_len < 4)
2334 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor length too short: %d, SKIPPING\n", descr_len);
2336 else
2338 char *d;
2339 memcpy(es->format_descriptor, &ptr[j+2], 4);
2340 es->format_descriptor[4] = 0;
2342 d = &ptr[j+2];
2343 if(d[0] == 'A' && d[1] == 'C' && d[2] == '-' && d[3] == '3')
2345 es->type = AUDIO_A52;
2347 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '1')
2349 es->type = AUDIO_DTS;
2351 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '2')
2353 es->type = AUDIO_DTS;
2355 else if(d[0] == 'V' && d[1] == 'C' && d[2] == '-' && d[3] == '1')
2357 es->type = VIDEO_VC1;
2359 else if(d[0] == 'd' && d[1] == 'r' && d[2] == 'a' && d[3] == 'c')
2361 es->type = VIDEO_DIRAC;
2363 else if(d[0] == 'B' && d[1] == 'S' && d[2] == 'S' && d[3] == 'D')
2365 es->type = AUDIO_S302M;
2367 else
2368 es->type = UNKNOWN;
2369 mp_msg(MSGT_DEMUX, MSGL_DBG2, "FORMAT %s\n", es->format_descriptor);
2372 else if(ptr[j] == 0x1e || ptr[j] == 0x1f)
2374 // 0x1f is FMC, but currently it is easiest to handle them the same way
2375 es->mp4_es_id = (ptr[j+2] << 8) | ptr[j+3];
2376 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);
2378 else
2379 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Unknown descriptor 0x%x, SKIPPING\n", ptr[j]);
2381 len -= 2 + descr_len;
2382 j += 2 + descr_len;
2385 return 1;
2388 static int parse_sl_section(pmt_t *pmt, ts_section_t *section, int is_start, unsigned char *buff, int size)
2390 int tid, len, skip;
2391 uint8_t *ptr;
2392 skip = collect_section(section, is_start, buff, size);
2393 if(! skip)
2394 return 0;
2396 ptr = &(section->buffer[skip]);
2397 tid = ptr[0];
2398 len = ((ptr[1] & 0x0f) << 8) | ptr[2];
2399 mp_msg(MSGT_DEMUX, MSGL_V, "TABLEID: %d (av. %d), skip=%d, LEN: %d\n", tid, section->buffer_len, skip, len);
2400 if(len > 4093 || section->buffer_len < len || tid != 5)
2402 mp_msg(MSGT_DEMUX, MSGL_V, "SECTION TOO LARGE or wrong section type, EXIT\n");
2403 return 0;
2406 if(! (ptr[5] & 1))
2407 return 0;
2409 //8 is the current position, len - 9 is the amount of data available
2410 parse_mp4_descriptors(pmt, &ptr[8], len - 9, NULL);
2412 return 1;
2415 static int parse_pmt(ts_priv_t * priv, uint16_t progid, uint16_t pid, int is_start, unsigned char *buff, int size)
2417 unsigned char *base, *es_base;
2418 pmt_t *pmt;
2419 int32_t idx, es_count, section_bytes;
2420 uint8_t m=0;
2421 int skip;
2422 pmt_t *tmp;
2423 struct pmt_es_t *tmp_es;
2424 ts_section_t *section;
2425 ES_stream_t *tss;
2426 int i;
2428 idx = progid_idx_in_pmt(priv, progid);
2430 if(idx == -1)
2432 int sz = (priv->pmt_cnt + 1) * sizeof(pmt_t);
2433 tmp = realloc_struct(priv->pmt, priv->pmt_cnt + 1, sizeof(pmt_t));
2434 if(tmp == NULL)
2436 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
2437 return 0;
2439 priv->pmt = tmp;
2440 idx = priv->pmt_cnt;
2441 memset(&(priv->pmt[idx]), 0, sizeof(pmt_t));
2442 priv->pmt_cnt++;
2443 priv->pmt[idx].progid = progid;
2446 pmt = &(priv->pmt[idx]);
2448 section = &(pmt->section);
2449 skip = collect_section(section, is_start, buff, size);
2450 if(! skip)
2451 return 0;
2453 base = &(section->buffer[skip]);
2455 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",
2456 progid, pmt->section.buffer_len, is_start, pid, size, m, pmt->es_cnt, idx, pmt);
2458 pmt->table_id = base[0];
2459 if(pmt->table_id != 2)
2460 return -1;
2461 pmt->ssi = base[1] & 0x80;
2462 pmt->section_length = (((base[1] & 0xf) << 8 ) | base[2]);
2463 pmt->version_number = (base[5] >> 1) & 0x1f;
2464 pmt->curr_next = (base[5] & 1);
2465 pmt->section_number = base[6];
2466 pmt->last_section_number = base[7];
2467 pmt->PCR_PID = ((base[8] & 0x1f) << 8 ) | base[9];
2468 pmt->prog_descr_length = ((base[10] & 0xf) << 8 ) | base[11];
2469 if(pmt->prog_descr_length > pmt->section_length - 9)
2471 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, INVALID PROG_DESCR LENGTH (%d vs %d)\n", pmt->prog_descr_length, pmt->section_length - 9);
2472 return -1;
2475 if(pmt->prog_descr_length)
2476 parse_program_descriptors(pmt, &base[12], pmt->prog_descr_length);
2478 es_base = &base[12 + pmt->prog_descr_length]; //the beginning of th ES loop
2480 section_bytes= pmt->section_length - 13 - pmt->prog_descr_length;
2481 es_count = 0;
2483 while(section_bytes >= 5)
2485 int es_pid, es_type;
2487 es_type = es_base[0];
2488 es_pid = ((es_base[1] & 0x1f) << 8) | es_base[2];
2490 idx = es_pid_in_pmt(pmt, es_pid);
2491 if(idx == -1)
2493 int sz = sizeof(struct pmt_es_t) * (pmt->es_cnt + 1);
2494 tmp_es = realloc_struct(pmt->es, pmt->es_cnt + 1, sizeof(struct pmt_es_t));
2495 if(tmp_es == NULL)
2497 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT, COULDN'T ALLOCATE %d bytes for PMT_ES\n", sz);
2498 continue;
2500 pmt->es = tmp_es;
2501 idx = pmt->es_cnt;
2502 memset(&(pmt->es[idx]), 0, sizeof(struct pmt_es_t));
2503 pmt->es_cnt++;
2506 pmt->es[idx].descr_length = ((es_base[3] & 0xf) << 8) | es_base[4];
2509 if(pmt->es[idx].descr_length > section_bytes - 5)
2511 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, ES_DESCR_LENGTH TOO LARGE %d > %d, EXIT\n",
2512 pmt->es[idx].descr_length, section_bytes - 5);
2513 return -1;
2517 pmt->es[idx].pid = es_pid;
2518 if(es_type != 0x6)
2519 pmt->es[idx].type = UNKNOWN;
2520 else
2521 pmt->es[idx].type = es_type;
2523 parse_descriptors(&pmt->es[idx], &es_base[5]);
2525 switch(es_type)
2527 case 1:
2528 pmt->es[idx].type = VIDEO_MPEG1;
2529 break;
2530 case 2:
2531 pmt->es[idx].type = VIDEO_MPEG2;
2532 break;
2533 case 3:
2534 case 4:
2535 pmt->es[idx].type = AUDIO_MP2;
2536 break;
2537 case 6:
2538 if(pmt->es[idx].type == 0x6) //this could have been ovrwritten by parse_descriptors
2539 pmt->es[idx].type = UNKNOWN;
2540 break;
2541 case 0x10:
2542 pmt->es[idx].type = VIDEO_MPEG4;
2543 break;
2544 case 0x0f:
2545 pmt->es[idx].type = AUDIO_AAC;
2546 break;
2547 case 0x11:
2548 pmt->es[idx].type = AUDIO_AAC_LATM;
2549 for (i = 0; i < pmt->mp4es_cnt; i++)
2550 if (pmt->mp4es[i].id == pmt->es[idx].mp4_es_id &&
2551 pmt->mp4es[i].decoder.object_type == AUDIO_AAC)
2552 pmt->es[idx].type = AUDIO_AAC;
2553 break;
2554 case 0x1b:
2555 pmt->es[idx].type = VIDEO_H264;
2556 break;
2557 case 0x12:
2558 pmt->es[idx].type = SL_PES_STREAM;
2559 break;
2560 case 0x13:
2561 pmt->es[idx].type = SL_SECTION;
2562 break;
2563 case 0x81:
2564 pmt->es[idx].type = AUDIO_A52;
2565 break;
2566 case 0x8A:
2567 case 0x82:
2568 case 0x85:
2569 case 0x86:
2570 pmt->es[idx].type = AUDIO_DTS;
2571 break;
2572 case 0x90:
2573 pmt->es[idx].type = SPU_PGS;
2574 break;
2575 case 0xD1:
2576 pmt->es[idx].type = VIDEO_DIRAC;
2577 break;
2578 case 0xEA:
2579 pmt->es[idx].type = VIDEO_VC1;
2580 break;
2581 default:
2582 mp_msg(MSGT_DEMUX, MSGL_DBG2, "UNKNOWN ES TYPE=0x%x\n", es_type);
2583 pmt->es[idx].type = UNKNOWN;
2586 tss = priv->ts.pids[es_pid]; //an ES stream
2587 if(tss == NULL)
2589 tss = new_pid(priv, es_pid);
2590 if(tss)
2591 tss->type = pmt->es[idx].type;
2594 section_bytes -= 5 + pmt->es[idx].descr_length;
2595 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",
2596 progid, idx, es_count, pmt->es[idx].pid, pmt->es[idx].pid, pmt->es[idx].type, pmt->es[idx].descr_length, section_bytes);
2599 es_base += 5 + pmt->es[idx].descr_length;
2601 es_count++;
2604 mp_msg(MSGT_DEMUX, MSGL_V, "----------------------------\n");
2605 return 1;
2608 static pmt_t* pmt_of_pid(ts_priv_t *priv, int pid, mp4_decoder_config_t **mp4_dec)
2610 int32_t i, j, k;
2612 if(priv->pmt)
2614 for(i = 0; i < priv->pmt_cnt; i++)
2616 if(priv->pmt[i].es && priv->pmt[i].es_cnt)
2618 for(j = 0; j < priv->pmt[i].es_cnt; j++)
2620 if(priv->pmt[i].es[j].pid == pid)
2622 //search mp4_es_id
2623 if(priv->pmt[i].es[j].mp4_es_id)
2625 for(k = 0; k < priv->pmt[i].mp4es_cnt; k++)
2627 if(priv->pmt[i].mp4es[k].id == priv->pmt[i].es[j].mp4_es_id)
2629 *mp4_dec = &(priv->pmt[i].mp4es[k].decoder);
2630 break;
2635 return &(priv->pmt[i]);
2642 return NULL;
2646 static inline int32_t pid_type_from_pmt(ts_priv_t *priv, int pid)
2648 int32_t pmt_idx, pid_idx, i, j;
2650 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2652 if(pmt_idx != -1)
2654 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2655 if(pid_idx != -1)
2656 return priv->pmt[pmt_idx].es[pid_idx].type;
2658 //else
2660 for(i = 0; i < priv->pmt_cnt; i++)
2662 pmt_t *pmt = &(priv->pmt[i]);
2663 for(j = 0; j < pmt->es_cnt; j++)
2664 if(pmt->es[j].pid == pid)
2665 return pmt->es[j].type;
2669 return UNKNOWN;
2673 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid)
2675 int32_t pmt_idx, pid_idx, i, j;
2677 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2679 if(pmt_idx != -1)
2681 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2682 if(pid_idx != -1)
2683 return priv->pmt[pmt_idx].es[pid_idx].lang;
2685 else
2687 for(i = 0; i < priv->pmt_cnt; i++)
2689 pmt_t *pmt = &(priv->pmt[i]);
2690 for(j = 0; j < pmt->es_cnt; j++)
2691 if(pmt->es[j].pid == pid)
2692 return pmt->es[j].lang;
2696 return NULL;
2700 static int fill_packet(demuxer_t *demuxer, demux_stream_t *ds, demux_packet_t **dp, int *dp_offset, TS_stream_info *si)
2702 int ret = 0;
2704 if(*dp && *dp_offset <= 0)
2706 free_demux_packet(*dp);
2707 *dp = NULL;
2709 if(*dp)
2711 ret = *dp_offset;
2712 resize_demux_packet(*dp, ret); //shrinked to the right size
2713 ds_add_packet(ds, *dp);
2714 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);
2715 if(si)
2717 float diff = (*dp)->pts - si->last_pts;
2718 float dur;
2720 if(abs(diff) > 1) //1 second, there's a discontinuity
2722 si->duration += si->last_pts - si->first_pts;
2723 si->first_pts = si->last_pts = (*dp)->pts;
2725 else
2727 si->last_pts = (*dp)->pts;
2729 si->size += ret;
2730 dur = si->duration + (si->last_pts - si->first_pts);
2732 if(dur > 0 && ds == demuxer->video)
2734 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2735 if(dur > 1) //otherwise it may be unreliable
2736 priv->vbitrate = (uint32_t) ((float) si->size / dur);
2741 *dp = NULL;
2742 *dp_offset = 0;
2744 return ret;
2747 static int fill_extradata(mp4_decoder_config_t * mp4_dec, ES_stream_t *tss)
2749 uint8_t *tmp;
2751 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4_dec: %p, pid: %d\n", mp4_dec, tss->pid);
2753 if(mp4_dec->buf_size > tss->extradata_alloc)
2755 tmp = realloc(tss->extradata, mp4_dec->buf_size);
2756 if(!tmp)
2757 return 0;
2758 tss->extradata = tmp;
2759 tss->extradata_alloc = mp4_dec->buf_size;
2761 memcpy(tss->extradata, mp4_dec->buf, mp4_dec->buf_size);
2762 tss->extradata_len = mp4_dec->buf_size;
2763 mp_msg(MSGT_DEMUX, MSGL_V, "EXTRADATA: %p, alloc=%d, len=%d\n", tss->extradata, tss->extradata_alloc, tss->extradata_len);
2765 return tss->extradata_len;
2768 // 0 = EOF or no stream found
2769 // else = [-] number of bytes written to the packet
2770 static int ts_parse(demuxer_t *demuxer , ES_stream_t *es, unsigned char *packet, int probe)
2772 ES_stream_t *tss;
2773 int buf_size, is_start, pid, base;
2774 int len, cc, cc_ok av_unused, afc, retv = 0, is_video, is_audio, is_sub;
2775 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2776 stream_t *stream = demuxer->stream;
2777 char *p;
2778 demux_stream_t *ds = NULL;
2779 demux_packet_t **dp = NULL;
2780 int *dp_offset = 0, *buffer_size = 0;
2781 int32_t progid, pid_type, bad, ts_error;
2782 int junk = 0, rap_flag = 0;
2783 pmt_t *pmt;
2784 mp4_decoder_config_t *mp4_dec;
2785 TS_stream_info *si;
2788 memset(es, 0, sizeof(*es));
2789 while(1)
2791 bad = ts_error = 0;
2792 ds = NULL;
2793 dp = NULL;
2794 dp_offset = buffer_size = NULL;
2795 rap_flag = 0;
2796 mp4_dec = NULL;
2797 es->is_synced = 0;
2798 es->lang[0] = 0;
2799 si = NULL;
2801 junk = priv->ts.packet_size - TS_PACKET_SIZE;
2802 buf_size = priv->ts.packet_size - junk;
2804 if(stream_eof(stream))
2806 if(! probe)
2808 ts_dump_streams(priv);
2809 demuxer->filepos = stream_tell(demuxer->stream);
2812 return 0;
2816 if(! ts_sync(stream))
2818 mp_msg(MSGT_DEMUX, MSGL_INFO, "TS_PARSE: COULDN'T SYNC\n");
2819 return 0;
2822 len = stream_read(stream, &packet[1], 3);
2823 if (len != 3)
2824 return 0;
2825 buf_size -= 4;
2827 if((packet[1] >> 7) & 0x01) //transport error
2828 ts_error = 1;
2831 is_start = packet[1] & 0x40;
2832 pid = ((packet[1] & 0x1f) << 8) | packet[2];
2834 tss = priv->ts.pids[pid]; //an ES stream
2835 if(tss == NULL)
2837 tss = new_pid(priv, pid);
2838 if(tss == NULL)
2839 continue;
2842 cc = (packet[3] & 0xf);
2843 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
2844 tss->last_cc = cc;
2846 bad = ts_error; // || (! cc_ok);
2847 if(bad)
2849 if(priv->keep_broken == 0)
2851 stream_skip(stream, buf_size-1+junk);
2852 continue;
2855 is_start = 0; //queued to the packet data
2858 if(is_start)
2859 tss->is_synced = 1;
2861 if((!is_start && !tss->is_synced) || ((pid > 1) && (pid < 16)) || (pid == 8191)) //invalid pid
2863 stream_skip(stream, buf_size-1+junk);
2864 continue;
2868 afc = (packet[3] >> 4) & 3;
2869 if(! (afc % 2)) //no payload in this TS packet
2871 stream_skip(stream, buf_size-1+junk);
2872 continue;
2875 if(afc > 1)
2877 int c;
2878 c = stream_read_char(stream);
2879 buf_size--;
2880 if(c < 0 || c > 183) //broken from the stream layer or invalid
2882 stream_skip(stream, buf_size-1+junk);
2883 continue;
2886 //c==0 is allowed!
2887 if(c > 0)
2889 uint8_t pcrbuf[188];
2890 int flags = stream_read_char(stream);
2891 int has_pcr;
2892 rap_flag = (flags & 0x40) >> 6;
2893 has_pcr = flags & 0x10;
2895 buf_size--;
2896 c--;
2897 stream_read(stream, pcrbuf, c);
2899 if(has_pcr)
2901 int pcr_pid = prog_pcr_pid(priv, priv->prog);
2902 if(pcr_pid == pid)
2904 uint64_t pcr, pcr_ext;
2906 pcr = (int64_t)(pcrbuf[0]) << 25;
2907 pcr |= pcrbuf[1] << 17 ;
2908 pcr |= (pcrbuf[2]) << 9;
2909 pcr |= pcrbuf[3] << 1 ;
2910 pcr |= (pcrbuf[4] & 0x80) >> 7;
2912 pcr_ext = (pcrbuf[4] & 0x01) << 8;
2913 pcr_ext |= pcrbuf[5];
2915 pcr = pcr * 300 + pcr_ext;
2917 demuxer->reference_clock = (double)pcr/(double)27000000.0;
2921 buf_size -= c;
2922 if(buf_size == 0)
2923 continue;
2927 //find the program that the pid belongs to; if (it's the right one or -1) && pid_type==SL_SECTION
2928 //call parse_sl_section()
2929 pmt = pmt_of_pid(priv, pid, &mp4_dec);
2930 if(mp4_dec)
2932 fill_extradata(mp4_dec, tss);
2933 if(IS_VIDEO(mp4_dec->object_type) || IS_AUDIO(mp4_dec->object_type))
2935 tss->type = SL_PES_STREAM;
2936 tss->subtype = mp4_dec->object_type;
2941 //TABLE PARSING
2943 base = priv->ts.packet_size - buf_size;
2945 priv->last_pid = pid;
2947 is_video = IS_VIDEO(tss->type) || (tss->type==SL_PES_STREAM && IS_VIDEO(tss->subtype));
2948 is_audio = IS_AUDIO(tss->type) || (tss->type==SL_PES_STREAM && IS_AUDIO(tss->subtype)) || (tss->type == PES_PRIVATE1);
2949 is_sub = IS_SUB(tss->type);
2950 pid_type = pid_type_from_pmt(priv, pid);
2952 // PES CONTENT STARTS HERE
2953 if(! probe)
2955 if((is_video || is_audio || is_sub) && is_start)
2956 ts_add_stream(demuxer, tss);
2958 if(is_video && (demuxer->video->id == priv->ts.streams[pid].id))
2960 ds = demuxer->video;
2962 dp = &priv->fifo[1].pack;
2963 dp_offset = &priv->fifo[1].offset;
2964 buffer_size = &priv->fifo[1].buffer_size;
2965 si = &priv->vstr;
2967 else if(is_audio && (demuxer->audio->id == priv->ts.streams[pid].id))
2969 ds = demuxer->audio;
2971 dp = &priv->fifo[0].pack;
2972 dp_offset = &priv->fifo[0].offset;
2973 buffer_size = &priv->fifo[0].buffer_size;
2974 si = &priv->astr;
2976 else if(is_sub)
2978 sh_sub_t *sh_sub = demuxer->sub->sh;
2980 if(sh_sub && sh_sub->sid == tss->pid)
2982 ds = demuxer->sub;
2984 dp = &priv->fifo[2].pack;
2985 dp_offset = &priv->fifo[2].offset;
2986 buffer_size = &priv->fifo[2].buffer_size;
2988 else
2990 stream_skip(stream, buf_size+junk);
2991 continue;
2995 //IS IT TIME TO QUEUE DATA to the dp_packet?
2996 if(is_start && (dp != NULL))
2998 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
3002 if(dp && *dp == NULL)
3004 if(*buffer_size > MAX_PACK_BYTES)
3005 *buffer_size = MAX_PACK_BYTES;
3006 *dp = new_demux_packet(*buffer_size); //es->size
3007 *dp_offset = 0;
3008 if(! *dp)
3010 fprintf(stderr, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", *buffer_size);
3011 continue;
3013 mp_msg(MSGT_DEMUX, MSGL_DBG2, "CREATED DP(%d)\n", *buffer_size);
3018 if(probe || !dp) //dp is NULL for tables and sections
3020 p = &packet[base];
3022 else //feeding
3024 if(*dp_offset + buf_size > *buffer_size)
3026 *buffer_size = *dp_offset + buf_size + TS_FEC_PACKET_SIZE;
3027 resize_demux_packet(*dp, *buffer_size);
3029 p = &((*dp)->buffer[*dp_offset]);
3032 len = stream_read(stream, p, buf_size);
3033 if(len < buf_size)
3035 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\r\nts_parse() couldn't read enough data: %d < %d\r\n", len, buf_size);
3036 continue;
3038 stream_skip(stream, junk);
3040 if(pid == 0)
3042 parse_pat(priv, is_start, p, buf_size);
3043 continue;
3045 else if((tss->type == SL_SECTION) && pmt)
3047 int k, mp4_es_id = -1;
3048 ts_section_t *section;
3049 for(k = 0; k < pmt->mp4es_cnt; k++)
3051 if(pmt->mp4es[k].decoder.object_type == MP4_OD && pmt->mp4es[k].decoder.stream_type == MP4_OD)
3052 mp4_es_id = pmt->mp4es[k].id;
3054 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4ESID: %d\n", mp4_es_id);
3055 for(k = 0; k < pmt->es_cnt; k++)
3057 if(pmt->es[k].mp4_es_id == mp4_es_id)
3059 section = &(tss->section);
3060 parse_sl_section(pmt, section, is_start, &packet[base], buf_size);
3063 continue;
3065 else
3067 progid = prog_id_in_pat(priv, pid);
3068 if(progid != -1)
3070 if(pid != demuxer->video->id && pid != demuxer->audio->id && pid != demuxer->sub->id)
3072 parse_pmt(priv, progid, pid, is_start, &packet[base], buf_size);
3073 continue;
3075 else
3076 mp_msg(MSGT_DEMUX, MSGL_ERR, "Argh! Data pid %d used in the PMT, Skipping PMT parsing!\n", pid);
3080 if(!probe && !dp)
3081 continue;
3083 if(is_start)
3085 uint8_t *lang = NULL;
3087 mp_msg(MSGT_DEMUX, MSGL_DBG2, "IS_START\n");
3089 len = pes_parse2(p, buf_size, es, pid_type, pmt, pid);
3090 if(! len)
3092 tss->is_synced = 0;
3093 continue;
3095 es->pid = tss->pid;
3096 tss->is_synced |= es->is_synced || rap_flag;
3097 tss->payload_size = es->payload_size;
3099 if((is_sub || is_audio) && (lang = pid_lang_from_pmt(priv, es->pid)))
3101 memcpy(es->lang, lang, 3);
3102 es->lang[3] = 0;
3104 else
3105 es->lang[0] = 0;
3107 if(probe)
3109 if(es->type == UNKNOWN)
3110 return 0;
3112 tss->type = es->type;
3113 tss->subtype = es->subtype;
3115 return 1;
3117 else
3119 if(es->pts == 0.0)
3120 es->pts = tss->pts = tss->last_pts;
3121 else
3122 tss->pts = tss->last_pts = es->pts;
3124 mp_msg(MSGT_DEMUX, MSGL_DBG2, "ts_parse, NEW pid=%d, PSIZE: %u, type=%X, start=%p, len=%d\n",
3125 es->pid, es->payload_size, es->type, es->start, es->size);
3127 demuxer->filepos = stream_tell(demuxer->stream) - es->size;
3129 if(es->size < 0 || es->size > buf_size) {
3130 mp_msg(MSGT_DEMUX, MSGL_ERR, "Broken ES packet size\n");
3131 es->size = 0;
3133 memmove(p, es->start, es->size);
3134 *dp_offset += es->size;
3135 (*dp)->keyframe = 0;
3136 (*dp)->pos = stream_tell(demuxer->stream);
3137 (*dp)->pts = es->pts;
3138 // subtitle packets must be returned immediately if possible
3139 if (is_sub && !tss->payload_size)
3140 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
3142 if(retv > 0)
3143 return retv;
3144 else
3145 continue;
3148 else
3150 uint16_t sz;
3152 es->pid = tss->pid;
3153 es->type = tss->type;
3154 es->subtype = tss->subtype;
3155 es->pts = tss->pts = tss->last_pts;
3156 es->start = &packet[base];
3159 if(tss->payload_size > 0)
3161 sz = FFMIN(tss->payload_size, buf_size);
3162 tss->payload_size -= sz;
3163 es->size = sz;
3165 else
3167 if(is_video)
3169 sz = es->size = buf_size;
3171 else
3173 continue;
3178 if(! probe)
3180 *dp_offset += sz;
3182 // subtitle packets must be returned immediately if possible
3183 if(*dp_offset >= MAX_PACK_BYTES || (is_sub && !tss->payload_size))
3185 (*dp)->pts = tss->last_pts;
3186 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
3187 return 1;
3190 continue;
3192 else
3194 memmove(es->start, p, sz);
3196 if(es->size)
3197 return es->size;
3198 else
3199 continue;
3204 return 0;
3208 static void reset_fifos(demuxer_t *demuxer, int a, int v, int s)
3210 ts_priv_t* priv = demuxer->priv;
3211 if(a)
3213 if(priv->fifo[0].pack != NULL)
3215 free_demux_packet(priv->fifo[0].pack);
3216 priv->fifo[0].pack = NULL;
3218 priv->fifo[0].offset = 0;
3221 if(v)
3223 if(priv->fifo[1].pack != NULL)
3225 free_demux_packet(priv->fifo[1].pack);
3226 priv->fifo[1].pack = NULL;
3228 priv->fifo[1].offset = 0;
3231 if(s)
3233 if(priv->fifo[2].pack != NULL)
3235 free_demux_packet(priv->fifo[2].pack);
3236 priv->fifo[2].pack = NULL;
3238 priv->fifo[2].offset = 0;
3240 demuxer->reference_clock = MP_NOPTS_VALUE;
3244 static void demux_seek_ts(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
3246 demux_stream_t *d_audio=demuxer->audio;
3247 demux_stream_t *d_video=demuxer->video;
3248 sh_audio_t *sh_audio=d_audio->sh;
3249 sh_video_t *sh_video=d_video->sh;
3250 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
3251 int i, video_stats;
3252 off_t newpos;
3254 //================= seek in MPEG-TS ==========================
3256 ts_dump_streams(demuxer->priv);
3257 reset_fifos(demuxer, sh_audio != NULL, sh_video != NULL, demuxer->sub->id > 0);
3259 demux_flush(demuxer);
3263 video_stats = (sh_video != NULL);
3264 if(video_stats)
3266 mp_msg(MSGT_DEMUX, MSGL_V, "IBPS: %d, vb: %d\r\n", sh_video->i_bps, priv->vbitrate);
3267 if(priv->vbitrate)
3268 video_stats = priv->vbitrate;
3269 else
3270 video_stats = sh_video->i_bps;
3273 newpos = (flags & SEEK_ABSOLUTE) ? demuxer->movi_start : demuxer->filepos;
3274 if(flags & SEEK_FACTOR) // float seek 0..1
3275 newpos+=(demuxer->movi_end-demuxer->movi_start)*rel_seek_secs;
3276 else
3278 // time seek (secs)
3279 if(! video_stats) // unspecified or VBR
3280 newpos += 2324*75*rel_seek_secs; // 174.3 kbyte/sec
3281 else
3282 newpos += video_stats*rel_seek_secs;
3286 if(newpos < demuxer->movi_start)
3287 newpos = demuxer->movi_start; //begininng of stream
3289 stream_seek(demuxer->stream, newpos);
3290 for(i = 0; i < NB_PID_MAX; i++)
3291 if(priv->ts.pids[i] != NULL)
3292 priv->ts.pids[i]->is_synced = 0;
3294 videobuf_code_len = 0;
3296 if(sh_video != NULL)
3297 ds_fill_buffer(d_video);
3299 if(sh_audio != NULL)
3301 ds_fill_buffer(d_audio);
3304 while(sh_video != NULL)
3306 if(sh_audio && !d_audio->eof && d_video->pts && d_audio->pts)
3308 double a_pts=d_audio->pts;
3309 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(double)sh_audio->i_bps;
3310 if(d_video->pts > a_pts)
3312 skip_audio_frame(sh_audio); // sync audio
3313 continue;
3318 i = sync_video_packet(d_video);
3319 if((sh_video->format == VIDEO_MPEG1) || (sh_video->format == VIDEO_MPEG2))
3321 if(i==0x1B3 || i==0x1B8) break; // found it!
3323 else if((sh_video->format == VIDEO_MPEG4) && (i==0x1B6))
3324 break;
3325 else if(sh_video->format == VIDEO_VC1 && (i==0x10E || i==0x10F))
3326 break;
3327 else //H264
3329 if((i & ~0x60) == 0x105 || (i & ~0x60) == 0x107) break;
3332 if(!i || !skip_video_packet(d_video)) break; // EOF?
3337 static int demux_ts_fill_buffer(demuxer_t * demuxer, demux_stream_t *ds)
3339 ES_stream_t es;
3340 ts_priv_t *priv = (ts_priv_t *)demuxer->priv;
3342 return -ts_parse(demuxer, &es, priv->packet, 0);
3346 static int ts_check_file_dmx(demuxer_t *demuxer)
3348 return ts_check_file(demuxer) ? DEMUXER_TYPE_MPEG_TS : 0;
3351 static int is_usable_program(ts_priv_t *priv, pmt_t *pmt)
3353 int j;
3355 for(j = 0; j < pmt->es_cnt; j++)
3357 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3358 continue;
3360 priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO ||
3361 priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO
3363 return 1;
3366 return 0;
3369 static int demux_ts_control(demuxer_t *demuxer, int cmd, void *arg)
3371 ts_priv_t* priv = (ts_priv_t *)demuxer->priv;
3373 switch(cmd)
3375 case DEMUXER_CTRL_SWITCH_AUDIO:
3376 case DEMUXER_CTRL_SWITCH_VIDEO:
3378 void *sh = NULL;
3379 int i, n;
3380 int reftype, areset = 0, vreset = 0;
3381 demux_stream_t *ds;
3383 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
3385 reftype = TYPE_VIDEO;
3386 ds = demuxer->video;
3387 vreset = 1;
3389 else
3391 reftype = TYPE_AUDIO;
3392 ds = demuxer->audio;
3393 areset = 1;
3395 n = *((int*)arg);
3396 if(n == -2)
3398 reset_fifos(demuxer, areset, vreset, 0);
3399 ds->id = -2;
3400 ds->sh = NULL;
3401 ds_free_packs(ds);
3402 *((int*)arg) = ds->id;
3403 return DEMUXER_CTRL_OK;
3406 if(n < 0)
3408 for(i = 0; i < 8192; i++)
3410 if(priv->ts.streams[i].id == ds->id && priv->ts.streams[i].type == reftype)
3411 break;
3414 while(!sh)
3416 i = (i+1) % 8192;
3417 if(priv->ts.streams[i].type == reftype)
3419 if(priv->ts.streams[i].id == ds->id) //we made a complete loop
3420 break;
3421 sh = priv->ts.streams[i].sh;
3425 else //audio track <n>
3427 if (n >= 8192 || priv->ts.streams[n].type != reftype) return DEMUXER_CTRL_NOTIMPL;
3428 i = n;
3429 sh = priv->ts.streams[i].sh;
3432 if(sh)
3434 if(ds->id != priv->ts.streams[i].id)
3435 reset_fifos(demuxer, areset, vreset, 0);
3436 ds->id = priv->ts.streams[i].id;
3437 ds->sh = sh;
3438 ds_free_packs(ds);
3439 mp_msg(MSGT_DEMUX, MSGL_V, "\r\ndemux_ts, switched to audio pid %d, id: %d, sh: %p\r\n", i, ds->id, sh);
3442 *((int*)arg) = ds->id;
3443 return DEMUXER_CTRL_OK;
3446 case DEMUXER_CTRL_IDENTIFY_PROGRAM: //returns in prog->{aid,vid} the new ids that comprise a program
3448 int i, j, cnt=0;
3449 int vid_done=0, aid_done=0;
3450 pmt_t *pmt = NULL;
3451 demux_program_t *prog = arg;
3453 if(priv->pmt_cnt < 2)
3454 return DEMUXER_CTRL_NOTIMPL;
3456 if(prog->progid == -1)
3458 int cur_pmt_idx = 0;
3460 for(i = 0; i < priv->pmt_cnt; i++)
3461 if(priv->pmt[i].progid == priv->prog)
3463 cur_pmt_idx = i;
3464 break;
3467 i = (cur_pmt_idx + 1) % priv->pmt_cnt;
3468 while(i != cur_pmt_idx)
3470 pmt = &priv->pmt[i];
3471 cnt = is_usable_program(priv, pmt);
3472 if(cnt)
3473 break;
3474 i = (i + 1) % priv->pmt_cnt;
3477 else
3479 for(i = 0; i < priv->pmt_cnt; i++)
3480 if(priv->pmt[i].progid == prog->progid)
3482 pmt = &priv->pmt[i]; //required program
3483 cnt = is_usable_program(priv, pmt);
3487 if(!cnt)
3488 return DEMUXER_CTRL_NOTIMPL;
3490 //finally some food
3491 prog->aid = prog->vid = -2; //no audio and no video by default
3492 for(j = 0; j < pmt->es_cnt; j++)
3494 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3495 continue;
3497 if(!vid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO)
3499 vid_done = 1;
3500 prog->vid = pmt->es[j].pid;
3502 else if(!aid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO)
3504 aid_done = 1;
3505 prog->aid = pmt->es[j].pid;
3509 priv->prog = prog->progid = pmt->progid;
3510 return DEMUXER_CTRL_OK;
3513 default:
3514 return DEMUXER_CTRL_NOTIMPL;
3519 const demuxer_desc_t demuxer_desc_mpeg_ts = {
3520 "MPEG-TS demuxer",
3521 "mpegts",
3522 "TS",
3523 "Nico Sabbi",
3525 DEMUXER_TYPE_MPEG_TS,
3526 0, // unsafe autodetect
3527 ts_check_file_dmx,
3528 demux_ts_fill_buffer,
3529 demux_open_ts,
3530 demux_close_ts,
3531 demux_seek_ts,
3532 demux_ts_control