Remove duplicate printing of ID_AUDIO_ID/ID_VIDEO_ID, those are already
[mplayer/glamo.git] / libmpdemux / demux_ts.c
blob4ee2aa51f873db8e190ff4457fc85809f0864a71
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 "help_mp.h"
33 #include "stream/stream.h"
34 #include "demuxer.h"
35 #include "parse_es.h"
36 #include "stheader.h"
38 #include "ms_hdr.h"
39 #include "mpeg_hdr.h"
41 #define TS_PH_PACKET_SIZE 192
42 #define TS_FEC_PACKET_SIZE 204
43 #define TS_PACKET_SIZE 188
44 #define NB_PID_MAX 8192
46 #define MAX_HEADER_SIZE 6 /* enough for PES header + length */
47 #define MAX_CHECK_SIZE 65535
48 #define TS_MAX_PROBE_SIZE 2000000 /* do not forget to change this in cfg-common-opts.h, too */
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
60 int ts_prog;
61 int ts_keep_broken=0;
62 off_t ts_probe = 0;
63 int audio_substream_id = -1;
64 extern char *dvdsub_lang, *audio_lang; //for -alang
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_VC1 = mmioFOURCC('W', 'V', 'C', '1'),
75 AUDIO_MP2 = 0x50,
76 AUDIO_A52 = 0x2000,
77 AUDIO_DTS = 0x2001,
78 AUDIO_LPCM_BE = 0x10001,
79 AUDIO_AAC = mmioFOURCC('M', 'P', '4', 'A'),
80 SPU_DVD = 0x3000000,
81 SPU_DVB = 0x3000001,
82 PES_PRIVATE1 = 0xBD00000,
83 SL_PES_STREAM = 0xD000000,
84 SL_SECTION = 0xD100000,
85 MP4_OD = 0xD200000,
86 } es_stream_type_t;
88 typedef struct {
89 uint8_t *buffer;
90 uint16_t buffer_len;
91 } ts_section_t;
93 typedef struct {
94 int size;
95 unsigned char *start;
96 uint16_t payload_size;
97 es_stream_type_t type, subtype;
98 float pts, last_pts;
99 int pid;
100 char lang[4];
101 int last_cc; // last cc code (-1 if first packet)
102 int is_synced;
103 ts_section_t section;
104 uint8_t *extradata;
105 int extradata_alloc, extradata_len;
106 struct {
107 uint8_t au_start, au_end, last_au_end;
108 } sl;
109 } ES_stream_t;
111 typedef struct {
112 void *sh;
113 int id;
114 int type;
115 } sh_av_t;
117 typedef struct MpegTSContext {
118 int packet_size; // raw packet size, including FEC if present e.g. 188 bytes
119 ES_stream_t *pids[NB_PID_MAX];
120 sh_av_t streams[NB_PID_MAX];
121 } MpegTSContext;
124 typedef struct {
125 demux_stream_t *ds;
126 demux_packet_t *pack;
127 int offset, buffer_size;
128 } av_fifo_t;
130 #define MAX_EXTRADATA_SIZE 64*1024
131 typedef struct {
132 int32_t object_type; //aka codec used
133 int32_t stream_type; //video, audio etc.
134 uint8_t buf[MAX_EXTRADATA_SIZE];
135 uint16_t buf_size;
136 uint8_t szm1;
137 } mp4_decoder_config_t;
139 typedef struct {
140 //flags
141 uint8_t flags;
142 uint8_t au_start;
143 uint8_t au_end;
144 uint8_t random_accesspoint;
145 uint8_t random_accesspoint_only;
146 uint8_t padding;
147 uint8_t use_ts;
148 uint8_t idle;
149 uint8_t duration;
151 uint32_t ts_resolution, ocr_resolution;
152 uint8_t ts_len, ocr_len, au_len, instant_bitrate_len, degr_len, au_seqnum_len, packet_seqnum_len;
153 uint32_t timescale;
154 uint16_t au_duration, cts_duration;
155 uint64_t ocr, dts, cts;
156 } mp4_sl_config_t;
158 typedef struct {
159 uint16_t id;
160 uint8_t flags;
161 mp4_decoder_config_t decoder;
162 mp4_sl_config_t sl;
163 } mp4_es_descr_t;
165 typedef struct {
166 uint16_t id;
167 uint8_t flags;
168 mp4_es_descr_t *es;
169 uint16_t es_cnt;
170 } mp4_od_t;
172 typedef struct {
173 uint8_t skip;
174 uint8_t table_id;
175 uint8_t ssi;
176 uint16_t section_length;
177 uint16_t ts_id;
178 uint8_t version_number;
179 uint8_t curr_next;
180 uint8_t section_number;
181 uint8_t last_section_number;
182 struct pat_progs_t {
183 uint16_t id;
184 uint16_t pmt_pid;
185 } *progs;
186 uint16_t progs_cnt;
187 ts_section_t section;
188 } pat_t;
190 typedef struct {
191 uint16_t progid;
192 uint8_t skip;
193 uint8_t table_id;
194 uint8_t ssi;
195 uint16_t section_length;
196 uint8_t version_number;
197 uint8_t curr_next;
198 uint8_t section_number;
199 uint8_t last_section_number;
200 uint16_t PCR_PID;
201 uint16_t prog_descr_length;
202 ts_section_t section;
203 uint16_t es_cnt;
204 struct pmt_es_t {
205 uint16_t pid;
206 uint32_t type; //it's 8 bit long, but cast to the right type as FOURCC
207 uint16_t descr_length;
208 uint8_t format_descriptor[5];
209 uint8_t lang[4];
210 uint16_t mp4_es_id;
211 } *es;
212 mp4_od_t iod, *od;
213 mp4_es_descr_t *mp4es;
214 int od_cnt, mp4es_cnt;
215 } pmt_t;
217 typedef struct {
218 uint64_t size;
219 float duration;
220 float first_pts;
221 float last_pts;
222 } TS_stream_info;
224 typedef struct {
225 MpegTSContext ts;
226 int last_pid;
227 av_fifo_t fifo[3]; //0 for audio, 1 for video, 2 for subs
228 pat_t pat;
229 pmt_t *pmt;
230 uint16_t pmt_cnt;
231 uint32_t prog;
232 uint32_t vbitrate;
233 int keep_broken;
234 int last_aid;
235 int last_vid;
236 char packet[TS_FEC_PACKET_SIZE];
237 TS_stream_info vstr, astr;
238 } ts_priv_t;
241 typedef struct {
242 es_stream_type_t type;
243 ts_section_t section;
244 } TS_pids_t;
247 #define IS_AUDIO(x) (((x) == AUDIO_MP2) || ((x) == AUDIO_A52) || ((x) == AUDIO_LPCM_BE) || ((x) == AUDIO_AAC) || ((x) == AUDIO_DTS))
248 #define IS_VIDEO(x) (((x) == VIDEO_MPEG1) || ((x) == VIDEO_MPEG2) || ((x) == VIDEO_MPEG4) || ((x) == VIDEO_H264) || ((x) == VIDEO_AVC) || ((x) == VIDEO_VC1))
250 static int ts_parse(demuxer_t *demuxer, ES_stream_t *es, unsigned char *packet, int probe);
252 static uint8_t get_packet_size(const unsigned char *buf, int size)
254 int i;
256 if (size < (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS))
257 return 0;
259 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
261 if (buf[i * TS_PACKET_SIZE] != 0x47)
263 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
264 goto try_fec;
267 return TS_PACKET_SIZE;
269 try_fec:
270 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
272 if (buf[i * TS_FEC_PACKET_SIZE] != 0x47){
273 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
274 goto try_philips;
277 return TS_FEC_PACKET_SIZE;
279 try_philips:
280 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
282 if (buf[i * TS_PH_PACKET_SIZE] != 0x47)
283 return 0;
285 return TS_PH_PACKET_SIZE;
288 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h);
290 static void ts_add_stream(demuxer_t * demuxer, ES_stream_t *es)
292 int i;
293 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
295 if(priv->ts.streams[es->pid].sh)
296 return;
298 if((IS_AUDIO(es->type) || IS_AUDIO(es->subtype)) && priv->last_aid+1 < MAX_A_STREAMS)
300 sh_audio_t *sh = new_sh_audio_aid(demuxer, priv->last_aid, es->pid);
301 if(sh)
303 sh->format = IS_AUDIO(es->type) ? es->type : es->subtype;
304 sh->ds = demuxer->audio;
306 priv->ts.streams[es->pid].id = priv->last_aid;
307 priv->ts.streams[es->pid].sh = sh;
308 priv->ts.streams[es->pid].type = TYPE_AUDIO;
309 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);
310 priv->last_aid++;
313 if(es->extradata && es->extradata_len)
315 sh->wf = (WAVEFORMATEX *) malloc(sizeof (WAVEFORMATEX) + es->extradata_len);
316 sh->wf->cbSize = es->extradata_len;
317 memcpy(sh->wf + 1, es->extradata, es->extradata_len);
321 if((IS_VIDEO(es->type) || IS_VIDEO(es->subtype)) && priv->last_vid+1 < MAX_V_STREAMS)
323 sh_video_t *sh = new_sh_video_vid(demuxer, priv->last_vid, es->pid);
324 if(sh)
326 sh->format = IS_VIDEO(es->type) ? es->type : es->subtype;
327 sh->ds = demuxer->video;
329 priv->ts.streams[es->pid].id = priv->last_vid;
330 priv->ts.streams[es->pid].sh = sh;
331 priv->ts.streams[es->pid].type = TYPE_VIDEO;
332 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);
333 priv->last_vid++;
336 if(sh->format == VIDEO_AVC && es->extradata && es->extradata_len)
338 int w = 0, h = 0;
339 sh->bih = (BITMAPINFOHEADER *) calloc(1, sizeof(BITMAPINFOHEADER) + es->extradata_len);
340 sh->bih->biSize= sizeof(BITMAPINFOHEADER) + es->extradata_len;
341 sh->bih->biCompression = sh->format;
342 memcpy(sh->bih + 1, es->extradata, es->extradata_len);
343 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "EXTRADATA(%d BYTES): \n", es->extradata_len);
344 for(i = 0;i < es->extradata_len; i++)
345 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "%02x ", (int) es->extradata[i]);
346 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"\n");
347 if(parse_avc_sps(es->extradata, es->extradata_len, &w, &h))
349 sh->bih->biWidth = w;
350 sh->bih->biHeight = h;
357 static int ts_check_file(demuxer_t * demuxer)
359 const int buf_size = (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS);
360 unsigned char buf[TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS], done = 0, *ptr;
361 uint32_t _read, i, count = 0, is_ts;
362 int cc[NB_PID_MAX], last_cc[NB_PID_MAX], pid, cc_ok, c, good, bad;
363 uint8_t size = 0;
364 off_t pos = 0;
365 off_t init_pos;
367 mp_msg(MSGT_DEMUX, MSGL_V, "Checking for MPEG-TS...\n");
369 init_pos = stream_tell(demuxer->stream);
370 is_ts = 0;
371 while(! done)
373 i = 1;
374 c = 0;
376 while(((c=stream_read_char(demuxer->stream)) != 0x47)
377 && (c >= 0)
378 && (i < MAX_CHECK_SIZE)
379 && ! demuxer->stream->eof
380 ) i++;
383 if(c != 0x47)
385 mp_msg(MSGT_DEMUX, MSGL_V, "THIS DOESN'T LOOK LIKE AN MPEG-TS FILE!\n");
386 is_ts = 0;
387 done = 1;
388 continue;
391 pos = stream_tell(demuxer->stream) - 1;
392 buf[0] = c;
393 _read = stream_read(demuxer->stream, &buf[1], buf_size-1);
395 if(_read < buf_size-1)
397 mp_msg(MSGT_DEMUX, MSGL_V, "COULDN'T READ ENOUGH DATA, EXITING TS_CHECK\n");
398 stream_reset(demuxer->stream);
399 return 0;
402 size = get_packet_size(buf, buf_size);
403 if(size)
405 done = 1;
406 is_ts = 1;
409 if(pos - init_pos >= MAX_CHECK_SIZE)
411 done = 1;
412 is_ts = 0;
416 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);
417 stream_seek(demuxer->stream, pos);
419 if(! is_ts)
420 return 0;
422 //LET'S CHECK continuity counters
423 good = bad = 0;
424 for(count = 0; count < NB_PID_MAX; count++)
426 cc[count] = last_cc[count] = -1;
429 for(count = 0; count < NUM_CONSECUTIVE_TS_PACKETS; count++)
431 ptr = &(buf[size * count]);
432 pid = ((ptr[1] & 0x1f) << 8) | ptr[2];
433 mp_msg(MSGT_DEMUX, MSGL_DBG2, "BUF: %02x %02x %02x %02x, PID %d, SIZE: %d \n",
434 ptr[0], ptr[1], ptr[2], ptr[3], pid, size);
436 if((pid == 8191) || (pid < 16))
437 continue;
439 cc[pid] = (ptr[3] & 0xf);
440 cc_ok = (last_cc[pid] < 0) || ((((last_cc[pid] + 1) & 0x0f) == cc[pid]));
441 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PID %d, COMPARE CC %d AND LAST_CC %d\n", pid, cc[pid], last_cc[pid]);
442 if(! cc_ok)
443 //return 0;
444 bad++;
445 else
446 good++;
448 last_cc[pid] = cc[pid];
451 mp_msg(MSGT_DEMUX, MSGL_V, "GOOD CC: %d, BAD CC: %d\n", good, bad);
453 if(good >= bad)
454 return size;
455 else
456 return 0;
460 static inline int32_t progid_idx_in_pmt(ts_priv_t *priv, uint16_t progid)
462 int x;
464 if(priv->pmt == NULL)
465 return -1;
467 for(x = 0; x < priv->pmt_cnt; x++)
469 if(priv->pmt[x].progid == progid)
470 return x;
473 return -1;
477 static inline int32_t progid_for_pid(ts_priv_t *priv, int pid, int32_t req) //finds the first program listing a pid
479 int i, j;
480 pmt_t *pmt;
483 if(priv->pmt == NULL)
484 return -1;
487 for(i=0; i < priv->pmt_cnt; i++)
489 pmt = &(priv->pmt[i]);
491 if(pmt->es == NULL)
492 return -1;
494 for(j = 0; j < pmt->es_cnt; j++)
496 if(pmt->es[j].pid == pid)
498 if((req == 0) || (req == pmt->progid))
499 return pmt->progid;
504 return -1;
507 static inline int32_t prog_pcr_pid(ts_priv_t *priv, int progid)
509 int i;
511 if(priv->pmt == NULL)
512 return -1;
513 for(i=0; i < priv->pmt_cnt; i++)
515 if(priv->pmt[i].progid == progid)
516 return priv->pmt[i].PCR_PID;
518 return -1;
522 static inline int pid_match_lang(ts_priv_t *priv, uint16_t pid, char *lang)
524 uint16_t i, j;
525 pmt_t *pmt;
527 if(priv->pmt == NULL)
528 return -1;
530 for(i=0; i < priv->pmt_cnt; i++)
532 pmt = &(priv->pmt[i]);
534 if(pmt->es == NULL)
535 return -1;
537 for(j = 0; j < pmt->es_cnt; j++)
539 if(pmt->es[j].pid != pid)
540 continue;
542 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);
543 if(strncmp(pmt->es[j].lang, lang, 3) == 0)
545 return 1;
551 return -1;
554 typedef struct {
555 int32_t atype, vtype, stype; //types
556 int32_t apid, vpid, spid; //stream ids
557 char slang[4], alang[4]; //languages
558 uint16_t prog;
559 off_t probe;
560 } tsdemux_init_t;
562 //stripped down version of a52_syncinfo() from liba52
563 //copyright belongs to Michel Lespinasse <walken@zoy.org> and Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
564 int mp_a52_framesize(uint8_t * buf, int *srate)
566 int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
567 128, 160, 192, 224, 256, 320, 384, 448,
568 512, 576, 640
570 uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
571 int frmsizecod, bitrate, half;
573 if((buf[0] != 0x0b) || (buf[1] != 0x77)) /* syncword */
574 return 0;
576 if(buf[5] >= 0x60) /* bsid >= 12 */
577 return 0;
579 half = halfrate[buf[5] >> 3];
581 frmsizecod = buf[4] & 63;
582 if(frmsizecod >= 38)
583 return 0;
585 bitrate = rate[frmsizecod >> 1];
587 switch(buf[4] & 0xc0)
589 case 0: /* 48 KHz */
590 *srate = 48000 >> half;
591 return 4 * bitrate;
592 case 0x40: /* 44.1 KHz */
593 *srate = 44100 >> half;
594 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
595 case 0x80: /* 32 KHz */
596 *srate = 32000 >> half;
597 return 6 * bitrate;
600 return 0;
603 //second stage: returns the count of A52 syncwords found
604 static int a52_check(char *buf, int len)
606 int cnt, frame_length, ok, srate;
608 cnt = ok = 0;
609 if(len < 8)
610 return 0;
612 while(cnt < len - 7)
614 if(buf[cnt] == 0x0B && buf[cnt+1] == 0x77)
616 frame_length = mp_a52_framesize(&buf[cnt], &srate);
617 if(frame_length>=7 && frame_length<=3840)
619 cnt += frame_length;
620 ok++;
622 else
623 cnt++;
625 else
626 cnt++;
629 mp_msg(MSGT_DEMUXER, MSGL_V, "A52_CHECK(%d input bytes), found %d frame syncwords of %d bytes length\n", len, ok, frame_length);
630 return ok;
634 static off_t ts_detect_streams(demuxer_t *demuxer, tsdemux_init_t *param)
636 int video_found = 0, audio_found = 0, sub_found = 0, i, num_packets = 0, req_apid, req_vpid, req_spid;
637 int is_audio, is_video, is_sub, has_tables;
638 int32_t p, chosen_pid = 0;
639 off_t pos=0, ret = 0, init_pos, end_pos;
640 ES_stream_t es;
641 unsigned char tmp[TS_FEC_PACKET_SIZE];
642 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
643 struct {
644 char *buf;
645 int pos;
646 } pes_priv1[8192], *pptr;
647 char *tmpbuf;
649 priv->last_pid = 8192; //invalid pid
651 req_apid = param->apid;
652 req_vpid = param->vpid;
653 req_spid = param->spid;
655 has_tables = 0;
656 memset(pes_priv1, 0, sizeof(pes_priv1));
657 init_pos = stream_tell(demuxer->stream);
658 mp_msg(MSGT_DEMUXER, MSGL_V, "PROBING UP TO %"PRIu64", PROG: %d\n", (uint64_t) param->probe, param->prog);
659 end_pos = init_pos + (param->probe ? param->probe : TS_MAX_PROBE_SIZE);
660 while(1)
662 pos = stream_tell(demuxer->stream);
663 if(pos > end_pos || demuxer->stream->eof)
664 break;
666 if(ts_parse(demuxer, &es, tmp, 1))
668 //Non PES-aligned A52 audio may escape detection if PMT is not present;
669 //in this case we try to find at least 3 A52 syncwords
670 if((es.type == PES_PRIVATE1) && (! audio_found) && req_apid > -2)
672 pptr = &pes_priv1[es.pid];
673 if(pptr->pos < 64*1024)
675 tmpbuf = (char*) realloc(pptr->buf, pptr->pos + es.size);
676 if(tmpbuf != NULL)
678 pptr->buf = tmpbuf;
679 memcpy(&(pptr->buf[ pptr->pos ]), es.start, es.size);
680 pptr->pos += es.size;
681 if(a52_check(pptr->buf, pptr->pos) > 2)
683 param->atype = AUDIO_A52;
684 param->apid = es.pid;
685 es.type = AUDIO_A52;
691 is_audio = IS_AUDIO(es.type) || ((es.type==SL_PES_STREAM) && IS_AUDIO(es.subtype));
692 is_video = IS_VIDEO(es.type) || ((es.type==SL_PES_STREAM) && IS_VIDEO(es.subtype));
693 is_sub = ((es.type == SPU_DVD) || (es.type == SPU_DVB));
696 if((! is_audio) && (! is_video) && (! is_sub))
697 continue;
698 if(is_audio && req_apid==-2)
699 continue;
701 if(is_video)
703 chosen_pid = (req_vpid == es.pid);
704 if((! chosen_pid) && (req_vpid > 0))
705 continue;
707 else if(is_audio)
709 if (es.lang[0] > 0)
710 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_AID_%d_LANG=%s\n", es.pid, es.lang);
711 if(req_apid > 0)
713 chosen_pid = (req_apid == es.pid);
714 if(! chosen_pid)
715 continue;
717 else if(param->alang[0] > 0 && es.lang[0] > 0)
719 if(pid_match_lang(priv, es.pid, param->alang) == -1)
720 continue;
722 chosen_pid = 1;
723 param->apid = req_apid = es.pid;
726 else if(is_sub)
728 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_SUBTITLE_ID=%d\n", es.pid);
729 if (es.lang[0] > 0)
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))
733 continue;
736 if(req_apid < 0 && (param->alang[0] == 0) && req_vpid < 0 && req_spid < 0)
737 chosen_pid = 1;
739 if((ret == 0) && chosen_pid)
741 ret = stream_tell(demuxer->stream);
744 p = progid_for_pid(priv, es.pid, param->prog);
745 if(p != -1)
747 has_tables++;
748 if(!param->prog && chosen_pid)
749 param->prog = p;
752 if((param->prog > 0) && (param->prog != p))
754 if(audio_found)
756 if(is_video && (req_vpid == es.pid))
758 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
759 param->vpid = es.pid;
760 video_found = 1;
761 break;
765 if(video_found)
767 if(is_audio && (req_apid == es.pid))
769 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
770 param->apid = es.pid;
771 audio_found = 1;
772 break;
777 continue;
781 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "TYPE: %x, PID: %d, PROG FOUND: %d\n", es.type, es.pid, param->prog);
784 if(is_video)
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;
790 video_found = 1;
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)
798 param->vtype = 0;
799 break;
802 if(is_sub)
804 if((req_spid == -1) || (req_spid == es.pid))
806 param->stype = es.type;
807 param->spid = es.pid;
808 sub_found = 1;
812 if(is_audio)
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;
818 audio_found = 1;
822 if(audio_found && (param->apid == es.pid) && (! video_found))
823 num_packets++;
825 if((has_tables==0) && (video_found && audio_found) && (pos >= 1000000))
826 break;
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;
840 if(video_found)
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);
855 else
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);
872 else
874 audio_found = 0;
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);
882 else
884 param->stype = UNKNOWN;
885 mp_msg(MSGT_DEMUXER, MSGL_INFO, " NO SUBS (yet)! ");
888 if(video_found || audio_found)
890 if(!param->prog)
892 p = progid_for_pid(priv, video_found ? param->vpid : param->apid, 0);
893 if(p != -1)
894 param->prog = p;
897 if(demuxer->stream->eof && (ret == 0))
898 ret = init_pos;
899 mp_msg(MSGT_DEMUXER, MSGL_INFO, " PROGRAM N. %d\n", param->prog);
901 else
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;
916 return ret;
919 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h)
921 int sps, sps_len;
922 unsigned char *ptr;
923 mp_mpeg_header_t picture;
924 if(len < 6)
925 return 0;
926 sps = buf[5] & 0x1f;
927 if(!sps)
928 return 0;
929 sps_len = (buf[6] << 8) | buf[7];
930 if(!sps_len || (sps_len > len - 8))
931 return 0;
932 ptr = &(buf[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)
936 return 0;
937 *w = picture.display_picture_width;
938 *h = picture.display_picture_height;
939 return 1;
942 static demuxer_t *demux_open_ts(demuxer_t * demuxer)
944 int i;
945 uint8_t packet_size;
946 sh_video_t *sh_video;
947 sh_audio_t *sh_audio;
948 off_t start_pos;
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);
962 if(!packet_size)
963 return NULL;
965 priv = calloc(1, sizeof(ts_priv_t));
966 if(priv == NULL)
968 mp_msg(MSGT_DEMUX, MSGL_FATAL, "DEMUX_OPEN_TS, couldn't allocate enough memory for ts->priv, exit\n");
969 return NULL;
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;
982 priv->pmt = NULL;
983 priv->pmt_cnt = 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;
992 else
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;
1008 else
1009 memset(params.slang, 0, 4);
1011 if(audio_lang != NULL)
1013 strncpy(params.alang, audio_lang, 3);
1014 params.alang[3] = 0;
1016 else
1017 memset(params.alang, 0, 4);
1019 start_pos = ts_detect_streams(demuxer, &params);
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);
1078 return demuxer;
1081 static void demux_close_ts(demuxer_t * demuxer)
1083 uint16_t i;
1084 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
1086 if(priv)
1088 if(priv->pat.section.buffer)
1089 free(priv->pat.section.buffer);
1090 if(priv->pat.progs)
1091 free(priv->pat.progs);
1093 if(priv->pmt)
1095 for(i = 0; i < priv->pmt_cnt; i++)
1097 if(priv->pmt[i].section.buffer)
1098 free(priv->pmt[i].section.buffer);
1099 if(priv->pmt[i].es)
1100 free(priv->pmt[i].es);
1102 free(priv->pmt);
1104 free(priv);
1106 demuxer->priv=NULL;
1110 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;
1116 uint64_t v = 0;
1117 uint32_t pl_size = 0;
1118 int deg_flag = 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)
1126 return 0;
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;
1133 if(mp4_es_id < 0)
1134 return -1;
1136 for(i = 0; i < pmt->mp4es_cnt; i++)
1138 if(pmt->mp4es[i].id == mp4_es_id)
1139 es = &(pmt->mp4es[i]);
1141 if(! es)
1142 return -1;
1144 pes_es->subtype = es->decoder.object_type;
1146 sl = &(es->sl);
1147 if(!sl)
1148 return -1;
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);
1153 n = 0;
1154 if(sl->au_start)
1155 pes_es->sl.au_start = au_start = getbits(buf, n++, 1);
1156 else
1157 pes_es->sl.au_start = (pes_es->sl.last_au_end ? 1 : 0);
1158 if(sl->au_end)
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;
1168 if(sl->ocr_len > 0)
1169 ocr_flag = getbits(buf, n++, 1);
1170 if(sl->idle)
1171 idle = getbits(buf, n++, 1);
1172 if(sl->padding)
1173 padding = getbits(buf, n++, 1);
1174 if(padding)
1176 padding_bits = getbits(buf, n, 3);
1177 n += 3;
1180 if(idle || (padding && !padding_bits))
1182 pes_es->payload_size = 0;
1183 return -1;
1186 //(! idle && (!padding || padding_bits != 0)) is true
1187 n += sl->packet_seqnum_len;
1188 if(sl->degr_len)
1189 deg_flag = getbits(buf, n++, 1);
1190 if(deg_flag)
1191 n += sl->degr_len;
1193 if(ocr_flag)
1195 n += sl->ocr_len;
1196 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "OCR: %d bits\n", sl->ocr_len);
1199 if(packet_len * 8 <= n)
1200 return -1;
1202 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "\nAU_START: %d, AU_END: %d\n", au_start, au_end);
1203 if(au_start)
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)
1217 return -1;
1218 if(sl->use_ts)
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)
1226 return -1;
1227 if(dts_flag && (sl->ts_len > 0))
1229 n += sl->ts_len;
1230 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "DTS: %d bits\n", sl->ts_len);
1232 if(packet_len * 8 <= n+8)
1233 return -1;
1234 if(cts_flag && (sl->ts_len > 0))
1236 int i = 0, m;
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)
1243 v <<= 8;
1244 i += m;
1245 n += m;
1246 if(packet_len * 8 <= n+8)
1247 return -1;
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);
1255 i = 0;
1256 pl_size = 0;
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)
1262 pl_size <<= 8;
1263 i += m;
1264 n += m;
1265 if(packet_len * 8 <= n+8)
1266 return -1;
1268 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "AU_LEN: %u (%d bits)\n", pl_size, sl->au_len);
1269 if(ib_flag)
1270 n += sl->instant_bitrate_len;
1273 m = (n+7)/8;
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);
1280 return m;
1283 //this function parses the extension fields in the PES header and returns the substream_id, or -1 in case of errors
1284 static int parse_pes_extension_fields(unsigned char *p, int pkt_len)
1286 int skip;
1287 unsigned char flags;
1289 if(!(p[7] & 0x1)) //no extension_field
1290 return -1;
1291 skip = 9;
1292 if(p[7] & 0x80)
1294 skip += 5;
1295 if(p[7] & 0x40)
1296 skip += 5;
1298 if(p[7] & 0x20) //escr_flag
1299 skip += 6;
1300 if(p[7] & 0x10) //es_rate_flag
1301 skip += 3;
1302 if(p[7] & 0x08)//dsm_trick_mode is unsupported, skip
1304 skip = 0;//don't let's parse the extension fields
1306 if(p[7] & 0x04) //additional_copy_info
1307 skip += 1;
1308 if(p[7] & 0x02) //pes_crc_flag
1309 skip += 2;
1310 if(skip >= pkt_len) //too few bytes
1311 return -1;
1312 flags = p[skip];
1313 skip++;
1314 if(flags & 0x80) //pes_private_data_flag
1315 skip += 16;
1316 if(skip >= pkt_len)
1317 return -1;
1318 if(flags & 0x40) //pack_header_field_flag
1320 unsigned char l = p[skip];
1321 skip += l;
1323 if(flags & 0x20) //program_packet_sequence_counter
1324 skip += 2;
1325 if(flags & 0x10) //p_std
1326 skip += 2;
1327 if(skip >= pkt_len)
1328 return -1;
1329 if(flags & 0x01) //finally the long desired pes_extension2
1331 unsigned char l = p[skip]; //ext2 flag+len
1332 skip++;
1333 if((l == 0x81) && (skip < pkt_len))
1335 int ssid = p[skip];
1336 mp_msg(MSGT_IDENTIFY, MSGL_V, "SUBSTREAM_ID=%d (0x%02X)\n", ssid, ssid);
1337 return ssid;
1341 return -1;
1344 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)
1346 unsigned char *p;
1347 uint32_t header_len;
1348 int64_t pts;
1349 uint32_t stream_id;
1350 uint32_t pkt_len, pes_is_aligned;
1352 //Here we are always at the start of a PES packet
1353 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2(%p, %d): \n", buf, (uint32_t) packet_len);
1355 if(packet_len == 0 || packet_len > 184)
1357 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2, BUFFER LEN IS TOO SMALL OR TOO BIG: %d EXIT\n", packet_len);
1358 return 0;
1361 p = buf;
1362 pkt_len = packet_len;
1365 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: HEADER %02x %02x %02x %02x\n", p[0], p[1], p[2], p[3]);
1366 if (p[0] || p[1] || (p[2] != 1))
1368 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: error HEADER %02x %02x %02x (should be 0x000001) \n", p[0], p[1], p[2]);
1369 return 0 ;
1372 packet_len -= 6;
1373 if(packet_len==0)
1375 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: packet too short: %d, exit\n", packet_len);
1376 return 0;
1379 es->payload_size = (p[4] << 8 | p[5]);
1380 pes_is_aligned = (p[6] & 4);
1382 stream_id = p[3];
1385 if (p[7] & 0x80)
1386 { /* pts available */
1387 pts = (int64_t)(p[9] & 0x0E) << 29 ;
1388 pts |= p[10] << 22 ;
1389 pts |= (p[11] & 0xFE) << 14 ;
1390 pts |= p[12] << 7 ;
1391 pts |= (p[13] & 0xFE) >> 1 ;
1393 es->pts = pts / 90000.0f;
1395 else
1396 es->pts = 0.0f;
1399 header_len = p[8];
1402 if (header_len + 9 > pkt_len) //9 are the bytes read up to the header_length field
1404 mp_msg(MSGT_DEMUX, MSGL_DBG2, "demux_ts: illegal value for PES_header_data_length (0x%02x)\n", header_len);
1405 return 0;
1408 if(stream_id==0xfd)
1410 int ssid = parse_pes_extension_fields(p, pkt_len);
1411 if((audio_substream_id!=-1) && (ssid != audio_substream_id))
1412 return 0;
1415 p += header_len + 9;
1416 packet_len -= header_len + 3;
1418 if(es->payload_size)
1419 es->payload_size -= header_len + 3;
1422 es->is_synced = 1; //only for SL streams we have to make sure it's really true, see below
1423 if (stream_id == 0xbd)
1425 mp_msg(MSGT_DEMUX, MSGL_DBG3, "pes_parse2: audio buf = %02X %02X %02X %02X %02X %02X %02X %02X, 80: %d\n",
1426 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[0] & 0x80);
1430 * we check the descriptor tag first because some stations
1431 * do not include any of the A52 header info in their audio tracks
1432 * these "raw" streams may begin with a byte that looks like a stream type.
1437 (type_from_pmt == AUDIO_A52) || /* A52 - raw */
1438 (p[0] == 0x0B && p[1] == 0x77) /* A52 - syncword */
1441 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 RAW OR SYNCWORD\n");
1442 es->start = p;
1443 es->size = packet_len;
1444 es->type = AUDIO_A52;
1445 es->payload_size -= packet_len;
1447 return 1;
1449 /* SPU SUBS */
1450 else if(type_from_pmt == SPU_DVB ||
1451 ((p[0] == 0x20) && pes_is_aligned)) // && p[1] == 0x00))
1453 es->start = p;
1454 es->size = packet_len;
1455 es->type = SPU_DVB;
1456 es->payload_size -= packet_len;
1458 return 1;
1460 else if (pes_is_aligned && ((p[0] & 0xE0) == 0x20)) //SPU_DVD
1462 //DVD SUBS
1463 es->start = p+1;
1464 es->size = packet_len-1;
1465 es->type = SPU_DVD;
1466 es->payload_size -= packet_len;
1468 return 1;
1470 else if (pes_is_aligned && (p[0] & 0xF8) == 0x80)
1472 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 WITH HEADER\n");
1473 es->start = p+4;
1474 es->size = packet_len - 4;
1475 es->type = AUDIO_A52;
1476 es->payload_size -= packet_len;
1478 return 1;
1480 else if (pes_is_aligned && ((p[0]&0xf0) == 0xa0))
1482 int pcm_offset;
1484 for (pcm_offset=0; ++pcm_offset < packet_len-1 ; )
1486 if (p[pcm_offset] == 0x01 && p[pcm_offset+1] == 0x80)
1487 { /* START */
1488 pcm_offset += 2;
1489 break;
1493 es->start = p + pcm_offset;
1494 es->size = packet_len - pcm_offset;
1495 es->type = AUDIO_LPCM_BE;
1496 es->payload_size -= packet_len;
1498 return 1;
1500 else
1502 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PES_PRIVATE1\n");
1503 es->start = p;
1504 es->size = packet_len;
1505 es->type = (type_from_pmt == UNKNOWN ? PES_PRIVATE1 : type_from_pmt);
1506 es->payload_size -= packet_len;
1508 return 1;
1511 else if(((stream_id >= 0xe0) && (stream_id <= 0xef)) || (stream_id == 0xfd && type_from_pmt != UNKNOWN))
1513 es->start = p;
1514 es->size = packet_len;
1515 if(type_from_pmt != UNKNOWN)
1516 es->type = type_from_pmt;
1517 else
1518 es->type = VIDEO_MPEG2;
1519 if(es->payload_size)
1520 es->payload_size -= packet_len;
1522 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: M2V size %d\n", es->size);
1523 return 1;
1525 else if ((stream_id == 0xfa))
1527 int l;
1529 es->is_synced = 0;
1530 if(type_from_pmt != UNKNOWN) //MP4 A/V or SL
1532 es->start = p;
1533 es->size = packet_len;
1534 es->type = type_from_pmt;
1536 if(type_from_pmt == SL_PES_STREAM)
1538 //if(pes_is_aligned)
1540 l = mp4_parse_sl_packet(pmt, p, packet_len, pid, es);
1541 mp_msg(MSGT_DEMUX, MSGL_DBG2, "L=%d, TYPE=%x\n", l, type_from_pmt);
1542 if(l < 0)
1544 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: couldn't parse SL header, passing along full PES payload\n");
1545 l = 0;
1549 es->start += l;
1550 es->size -= l;
1553 if(es->payload_size)
1554 es->payload_size -= packet_len;
1555 return 1;
1558 else if ((stream_id & 0xe0) == 0xc0)
1560 es->start = p;
1561 es->size = packet_len;
1563 if(type_from_pmt != UNKNOWN)
1564 es->type = type_from_pmt;
1565 else
1566 es->type = AUDIO_MP2;
1568 es->payload_size -= packet_len;
1570 return 1;
1572 else if (type_from_pmt != -1) //as a last resort here we trust the PMT, if present
1574 es->start = p;
1575 es->size = packet_len;
1576 es->type = type_from_pmt;
1577 es->payload_size -= packet_len;
1579 return 1;
1581 else
1583 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: unknown packet, id: %x\n", stream_id);
1586 es->is_synced = 0;
1587 return 0;
1593 static int ts_sync(stream_t *stream)
1595 int c=0;
1597 mp_msg(MSGT_DEMUX, MSGL_DBG3, "TS_SYNC \n");
1599 while(((c=stream_read_char(stream)) != 0x47) && ! stream->eof);
1601 if(c == 0x47)
1602 return c;
1603 else
1604 return 0;
1608 static void ts_dump_streams(ts_priv_t *priv)
1610 int i;
1612 for(i = 0; i < 3; i++)
1614 if((priv->fifo[i].pack != NULL) && (priv->fifo[i].offset != 0))
1616 resize_demux_packet(priv->fifo[i].pack, priv->fifo[i].offset);
1617 ds_add_packet(priv->fifo[i].ds, priv->fifo[i].pack);
1618 priv->fifo[i].offset = 0;
1619 priv->fifo[i].pack = NULL;
1625 static inline int32_t prog_idx_in_pat(ts_priv_t *priv, uint16_t progid)
1627 int x;
1629 if(priv->pat.progs == NULL)
1630 return -1;
1632 for(x = 0; x < priv->pat.progs_cnt; x++)
1634 if(priv->pat.progs[x].id == progid)
1635 return x;
1638 return -1;
1642 static inline int32_t prog_id_in_pat(ts_priv_t *priv, uint16_t pid)
1644 int x;
1646 if(priv->pat.progs == NULL)
1647 return -1;
1649 for(x = 0; x < priv->pat.progs_cnt; x++)
1651 if(priv->pat.progs[x].pmt_pid == pid)
1652 return priv->pat.progs[x].id;
1655 return -1;
1658 static int collect_section(ts_section_t *section, int is_start, unsigned char *buff, int size)
1660 uint8_t *ptr;
1661 uint16_t tlen;
1662 int skip, tid;
1664 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, start: %d, size: %d, collected: %d\n", is_start, size, section->buffer_len);
1665 if(! is_start && !section->buffer_len)
1666 return 0;
1668 if(is_start)
1670 if(! section->buffer)
1672 section->buffer = (uint8_t*) malloc(4096+256);
1673 if(section->buffer == NULL)
1674 return 0;
1676 section->buffer_len = 0;
1679 if(size + section->buffer_len > 4096+256)
1681 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, excessive len: %d + %d\n", section->buffer_len, size);
1682 return 0;
1685 memcpy(&(section->buffer[section->buffer_len]), buff, size);
1686 section->buffer_len += size;
1688 if(section->buffer_len < 3)
1689 return 0;
1691 skip = section->buffer[0];
1692 if(skip + 4 > section->buffer_len)
1693 return 0;
1695 ptr = &(section->buffer[skip + 1]);
1696 tid = ptr[0];
1697 tlen = ((ptr[1] & 0x0f) << 8) | ptr[2];
1698 mp_msg(MSGT_DEMUX, MSGL_V, "SKIP: %d+1, TID: %d, TLEN: %d, COLLECTED: %d\n", skip, tid, tlen, section->buffer_len);
1699 if(section->buffer_len < (skip+1+3+tlen))
1701 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DATA IS NOT ENOUGH, NEXT TIME\n");
1702 return 0;
1705 return skip+1;
1708 static int parse_pat(ts_priv_t * priv, int is_start, unsigned char *buff, int size)
1710 int skip;
1711 unsigned char *ptr;
1712 unsigned char *base;
1713 int entries, i;
1714 uint16_t progid;
1715 struct pat_progs_t *tmp;
1716 ts_section_t *section;
1718 section = &(priv->pat.section);
1719 skip = collect_section(section, is_start, buff, size);
1720 if(! skip)
1721 return 0;
1723 ptr = &(section->buffer[skip]);
1724 //PARSING
1725 priv->pat.table_id = ptr[0];
1726 if(priv->pat.table_id != 0)
1727 return 0;
1728 priv->pat.ssi = (ptr[1] >> 7) & 0x1;
1729 priv->pat.curr_next = ptr[5] & 0x01;
1730 priv->pat.ts_id = (ptr[3] << 8 ) | ptr[4];
1731 priv->pat.version_number = (ptr[5] >> 1) & 0x1F;
1732 priv->pat.section_length = ((ptr[1] & 0x03) << 8 ) | ptr[2];
1733 priv->pat.section_number = ptr[6];
1734 priv->pat.last_section_number = ptr[7];
1736 //check_crc32(0xFFFFFFFFL, ptr, priv->pat.buffer_len - 4, &ptr[priv->pat.buffer_len - 4]);
1737 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);
1739 entries = (int) (priv->pat.section_length - 9) / 4; //entries per section
1741 for(i=0; i < entries; i++)
1743 int32_t idx;
1744 base = &ptr[8 + i*4];
1745 progid = (base[0] << 8) | base[1];
1747 if((idx = prog_idx_in_pat(priv, progid)) == -1)
1749 int sz = sizeof(struct pat_progs_t) * (priv->pat.progs_cnt+1);
1750 tmp = realloc_struct(priv->pat.progs, priv->pat.progs_cnt+1, sizeof(struct pat_progs_t));
1751 if(tmp == NULL)
1753 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PAT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
1754 break;
1756 priv->pat.progs = tmp;
1757 idx = priv->pat.progs_cnt;
1758 priv->pat.progs_cnt++;
1761 priv->pat.progs[idx].id = progid;
1762 priv->pat.progs[idx].pmt_pid = ((base[2] & 0x1F) << 8) | base[3];
1763 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);
1764 mp_msg(MSGT_IDENTIFY, MSGL_V, "PROGRAM_ID=%d (0x%02X), PMT_PID: %d(0x%02X)\n",
1765 progid, progid, priv->pat.progs[idx].pmt_pid, priv->pat.progs[idx].pmt_pid);
1768 return 1;
1772 static inline int32_t es_pid_in_pmt(pmt_t * pmt, uint16_t pid)
1774 uint16_t i;
1776 if(pmt == NULL)
1777 return -1;
1779 if(pmt->es == NULL)
1780 return -1;
1782 for(i = 0; i < pmt->es_cnt; i++)
1784 if(pmt->es[i].pid == pid)
1785 return (int32_t) i;
1788 return -1;
1792 static uint16_t get_mp4_desc_len(uint8_t *buf, int *len)
1794 //uint16_t i = 0, size = 0;
1795 int i = 0, j, size = 0;
1797 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PARSE_MP4_DESC_LEN(%d), bytes: ", *len);
1798 j = FFMIN(*len, 4);
1799 while(i < j)
1801 mp_msg(MSGT_DEMUX, MSGL_DBG2, " %x ", buf[i]);
1802 size |= (buf[i] & 0x7f);
1803 if(!(buf[i] & 0x80))
1804 break;
1805 size <<= 7;
1806 i++;
1808 mp_msg(MSGT_DEMUX, MSGL_DBG2, ", SIZE=%d\n", size);
1810 *len = i+1;
1811 return size;
1815 static uint16_t parse_mp4_slconfig_descriptor(uint8_t *buf, int len, void *elem)
1817 int i = 0;
1818 mp4_es_descr_t *es;
1819 mp4_sl_config_t *sl;
1821 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_SLCONFIG_DESCRIPTOR(%d)\n", len);
1822 es = (mp4_es_descr_t *) elem;
1823 if(!es)
1825 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1826 return len;
1828 sl = &(es->sl);
1830 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;
1831 sl->ocr = sl->dts = sl->cts = 0;
1833 if(buf[0] == 0)
1835 i++;
1836 sl->flags = buf[i];
1837 i++;
1838 sl->ts_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1839 i += 4;
1840 sl->ocr_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1841 i += 4;
1842 sl->ts_len = buf[i];
1843 i++;
1844 sl->ocr_len = buf[i];
1845 i++;
1846 sl->au_len = buf[i];
1847 i++;
1848 sl->instant_bitrate_len = buf[i];
1849 i++;
1850 sl->degr_len = (buf[i] >> 4) & 0x0f;
1851 sl->au_seqnum_len = ((buf[i] & 0x0f) << 1) | ((buf[i+1] >> 7) & 0x01);
1852 i++;
1853 sl->packet_seqnum_len = ((buf[i] >> 2) & 0x1f);
1854 i++;
1857 else if(buf[0] == 1)
1859 sl->flags = 0;
1860 sl->ts_resolution = 1000;
1861 sl->ts_len = 32;
1862 i++;
1864 else if(buf[0] == 2)
1866 sl->flags = 4;
1867 i++;
1869 else
1871 sl->flags = 0;
1872 i++;
1875 sl->au_start = (sl->flags >> 7) & 0x1;
1876 sl->au_end = (sl->flags >> 6) & 0x1;
1877 sl->random_accesspoint = (sl->flags >> 5) & 0x1;
1878 sl->random_accesspoint_only = (sl->flags >> 4) & 0x1;
1879 sl->padding = (sl->flags >> 3) & 0x1;
1880 sl->use_ts = (sl->flags >> 2) & 0x1;
1881 sl->idle = (sl->flags >> 1) & 0x1;
1882 sl->duration = sl->flags & 0x1;
1884 if(sl->duration)
1886 sl->timescale = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1887 i += 4;
1888 sl->au_duration = (buf[i] << 8) | buf[i+1];
1889 i += 2;
1890 sl->cts_duration = (buf[i] << 8) | buf[i+1];
1891 i += 2;
1893 else //no support for fixed durations atm
1894 sl->timescale = sl->au_duration = sl->cts_duration = 0;
1896 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",
1897 len, buf[0], sl->flags, sl->use_ts, sl->ts_len, sl->timescale, (uint64_t) sl->dts, (uint64_t) sl->cts);
1899 return len;
1902 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem);
1904 static uint16_t parse_mp4_decoder_config_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
1906 int i = 0, j;
1907 mp4_es_descr_t *es;
1908 mp4_decoder_config_t *dec;
1910 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_CONFIG_DESCRIPTOR(%d)\n", len);
1911 es = (mp4_es_descr_t *) elem;
1912 if(!es)
1914 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1915 return len;
1917 dec = (mp4_decoder_config_t*) &(es->decoder);
1919 dec->object_type = buf[i];
1920 dec->stream_type = (buf[i+1]>>2) & 0x3f;
1922 if(dec->object_type == 1 && dec->stream_type == 1)
1924 dec->object_type = MP4_OD;
1925 dec->stream_type = MP4_OD;
1927 else if(dec->stream_type == 4)
1929 if(dec->object_type == 0x6a)
1930 dec->object_type = VIDEO_MPEG1;
1931 if(dec->object_type >= 0x60 && dec->object_type <= 0x65)
1932 dec->object_type = VIDEO_MPEG2;
1933 else if(dec->object_type == 0x20)
1934 dec->object_type = VIDEO_MPEG4;
1935 else if(dec->object_type == 0x21)
1936 dec->object_type = VIDEO_AVC;
1937 /*else if(dec->object_type == 0x22)
1938 fprintf(stderr, "TYPE 0x22\n");*/
1939 else dec->object_type = UNKNOWN;
1941 else if(dec->stream_type == 5)
1943 if(dec->object_type == 0x40)
1944 dec->object_type = AUDIO_AAC;
1945 else if(dec->object_type == 0x6b)
1946 dec->object_type = AUDIO_MP2;
1947 else if(dec->object_type >= 0x66 && dec->object_type <= 0x69)
1948 dec->object_type = AUDIO_MP2;
1949 else
1950 dec->object_type = UNKNOWN;
1952 else
1953 dec->object_type = dec->stream_type = UNKNOWN;
1955 if(dec->object_type != UNKNOWN)
1957 //update the type of the current stream
1958 for(j = 0; j < pmt->es_cnt; j++)
1960 if(pmt->es[j].mp4_es_id == es->id)
1962 pmt->es[j].type = SL_PES_STREAM;
1967 if(len > 13)
1968 parse_mp4_descriptors(pmt, &buf[13], len-13, dec);
1970 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);
1972 return len;
1975 static uint16_t parse_mp4_decoder_specific_descriptor(uint8_t *buf, int len, void *elem)
1977 int i;
1978 mp4_decoder_config_t *dec;
1980 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_SPECIFIC_DESCRIPTOR(%d)\n", len);
1981 dec = (mp4_decoder_config_t *) elem;
1982 if(!dec)
1984 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1985 return len;
1988 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4 SPECIFIC INFO BYTES: \n");
1989 for(i=0; i<len; i++)
1990 mp_msg(MSGT_DEMUX, MSGL_DBG2, "%02x ", buf[i]);
1991 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\n");
1993 if(len > MAX_EXTRADATA_SIZE)
1995 mp_msg(MSGT_DEMUX, MSGL_ERR, "DEMUX_TS, EXTRADATA SUSPICIOUSLY BIG: %d, REFUSED\r\n", len);
1996 return len;
1998 memcpy(dec->buf, buf, len);
1999 dec->buf_size = len;
2001 return len;
2004 static uint16_t parse_mp4_es_descriptor(pmt_t *pmt, uint8_t *buf, int len)
2006 int i = 0, j = 0, k, found;
2007 uint8_t flag;
2008 mp4_es_descr_t es, *target_es = NULL, *tmp;
2010 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4ES: len=%d\n", len);
2011 memset(&es, 0, sizeof(mp4_es_descr_t));
2012 while(i < len)
2014 es.id = (buf[i] << 8) | buf[i+1];
2015 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_ID: %d\n", es.id);
2016 i += 2;
2017 flag = buf[i];
2018 i++;
2019 if(flag & 0x80)
2020 i += 2;
2021 if(flag & 0x40)
2022 i += buf[i]+1;
2023 if(flag & 0x20) //OCR, maybe we need it
2024 i += 2;
2026 j = parse_mp4_descriptors(pmt, &buf[i], len-i, &es);
2027 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);
2028 if(es.decoder.object_type != UNKNOWN && es.decoder.stream_type != UNKNOWN)
2030 found = 0;
2031 //search this ES_ID if we already have it
2032 for(k=0; k < pmt->mp4es_cnt; k++)
2034 if(pmt->mp4es[k].id == es.id)
2036 target_es = &(pmt->mp4es[k]);
2037 found = 1;
2041 if(! found)
2043 tmp = realloc_struct(pmt->mp4es, pmt->mp4es_cnt+1, sizeof(mp4_es_descr_t));
2044 if(tmp == NULL)
2046 fprintf(stderr, "CAN'T REALLOC MP4_ES_DESCR\n");
2047 continue;
2049 pmt->mp4es = tmp;
2050 target_es = &(pmt->mp4es[pmt->mp4es_cnt]);
2051 pmt->mp4es_cnt++;
2053 memcpy(target_es, &es, sizeof(mp4_es_descr_t));
2054 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_CNT: %d, ID=%d\n", pmt->mp4es_cnt, target_es->id);
2057 i += j;
2060 return len;
2063 static void parse_mp4_object_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2065 int i, j = 0, id;
2067 i=0;
2068 id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2069 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_OBJECT_DESCRIPTOR: len=%d, OD_ID=%d\n", len, id);
2070 if(buf[1] & 0x20)
2072 i += buf[2] + 1; //url
2073 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2075 else
2077 i = 2;
2079 while(i < len)
2081 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2082 mp_msg(MSGT_DEMUX, MSGL_V, "OBJD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2083 i += j;
2089 static void parse_mp4_iod(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2091 int i, j = 0;
2092 mp4_od_t *iod = &(pmt->iod);
2094 iod->id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2095 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_IOD: len=%d, IOD_ID=%d\n", len, iod->id);
2096 i = 2;
2097 if(buf[1] & 0x20)
2099 i += buf[2] + 1; //url
2100 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2102 else
2104 i = 7;
2105 while(i < len)
2107 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2108 mp_msg(MSGT_DEMUX, MSGL_V, "IOD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2109 i += j;
2114 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2116 int tag, descr_len, i = 0, j = 0;
2118 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DESCRIPTORS, len=%d\n", len);
2119 if(! len)
2120 return len;
2122 while(i < len)
2124 tag = buf[i];
2125 j = len - i -1;
2126 descr_len = get_mp4_desc_len(&(buf[i+1]), &j);
2127 mp_msg(MSGT_DEMUX, MSGL_V, "TAG=%d (0x%x), DESCR_len=%d, len=%d, j=%d\n", tag, tag, descr_len, len, j);
2128 if(descr_len > len - j+1)
2130 mp_msg(MSGT_DEMUX, MSGL_V, "descriptor is too long, exit\n");
2131 return len;
2133 i += j+1;
2135 switch(tag)
2137 case 0x1:
2138 parse_mp4_object_descriptor(pmt, &(buf[i]), descr_len, elem);
2139 break;
2140 case 0x2:
2141 parse_mp4_iod(pmt, &(buf[i]), descr_len, elem);
2142 break;
2143 case 0x3:
2144 parse_mp4_es_descriptor(pmt, &(buf[i]), descr_len);
2145 break;
2146 case 0x4:
2147 parse_mp4_decoder_config_descriptor(pmt, &buf[i], descr_len, elem);
2148 break;
2149 case 0x05:
2150 parse_mp4_decoder_specific_descriptor(&buf[i], descr_len, elem);
2151 break;
2152 case 0x6:
2153 parse_mp4_slconfig_descriptor(&buf[i], descr_len, elem);
2154 break;
2155 default:
2156 mp_msg(MSGT_DEMUX, MSGL_V, "Unsupported mp4 descriptor 0x%x\n", tag);
2158 i += descr_len;
2161 return len;
2164 static ES_stream_t *new_pid(ts_priv_t *priv, int pid)
2166 ES_stream_t *tss;
2168 tss = malloc(sizeof(ES_stream_t));
2169 if(! tss)
2170 return NULL;
2171 memset(tss, 0, sizeof(ES_stream_t));
2172 tss->pid = pid;
2173 tss->last_cc = -1;
2174 tss->type = UNKNOWN;
2175 tss->subtype = UNKNOWN;
2176 tss->is_synced = 0;
2177 tss->extradata = NULL;
2178 tss->extradata_alloc = tss->extradata_len = 0;
2179 priv->ts.pids[pid] = tss;
2181 return tss;
2185 static int parse_program_descriptors(pmt_t *pmt, uint8_t *buf, uint16_t len)
2187 uint16_t i = 0, k, olen = len;
2189 while(len > 0)
2191 mp_msg(MSGT_DEMUX, MSGL_V, "PROG DESCR, TAG=%x, LEN=%d(%x)\n", buf[i], buf[i+1], buf[i+1]);
2192 if(buf[i+1] > len-2)
2194 mp_msg(MSGT_DEMUX, MSGL_V, "ERROR, descriptor len is too long, skipping\n");
2195 return olen;
2198 if(buf[i] == 0x1d)
2200 if(buf[i+3] == 2) //buggy versions of vlc muxer make this non-standard mess (missing iod_scope)
2201 k = 3;
2202 else
2203 k = 4; //this is standard compliant
2204 parse_mp4_descriptors(pmt, &buf[i+k], (int) buf[i+1]-(k-2), NULL);
2207 len -= 2 + buf[i+1];
2210 return olen;
2213 static int parse_descriptors(struct pmt_es_t *es, uint8_t *ptr)
2215 int j, descr_len, len;
2217 j = 0;
2218 len = es->descr_length;
2219 while(len > 2)
2221 descr_len = ptr[j+1];
2222 mp_msg(MSGT_DEMUX, MSGL_V, "...descr id: 0x%x, len=%d\n", ptr[j], descr_len);
2223 if(descr_len > len)
2225 mp_msg(MSGT_DEMUX, MSGL_ERR, "INVALID DESCR LEN for tag %02x: %d vs %d max, EXIT LOOP\n", ptr[j], descr_len, len);
2226 return -1;
2230 if(ptr[j] == 0x6a || ptr[j] == 0x7a) //A52 Descriptor
2232 if(es->type == 0x6)
2234 es->type = AUDIO_A52;
2235 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB A52 Descriptor\n");
2238 else if(ptr[j] == 0x7b) //DVB DTS Descriptor
2240 if(es->type == 0x6)
2242 es->type = AUDIO_DTS;
2243 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB DTS Descriptor\n");
2246 else if(ptr[j] == 0x59) //Subtitling Descriptor
2248 uint8_t subtype;
2250 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Subtitling Descriptor\n");
2251 if(descr_len < 8)
2253 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Descriptor length too short for DVB Subtitle Descriptor: %d, SKIPPING\n", descr_len);
2255 else
2257 memcpy(es->lang, &ptr[j+2], 3);
2258 es->lang[3] = 0;
2259 subtype = ptr[j+5];
2261 (subtype >= 0x10 && subtype <= 0x13) ||
2262 (subtype >= 0x20 && subtype <= 0x23)
2265 es->type = SPU_DVB;
2266 //page parameters: compo page 2 bytes, ancillary page 2 bytes
2268 else
2269 es->type = UNKNOWN;
2272 else if(ptr[j] == 0x50) //Component Descriptor
2274 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Component Descriptor\n");
2275 memcpy(es->lang, &ptr[j+5], 3);
2276 es->lang[3] = 0;
2278 else if(ptr[j] == 0xa) //Language Descriptor
2280 memcpy(es->lang, &ptr[j+2], 3);
2281 es->lang[3] = 0;
2282 mp_msg(MSGT_DEMUX, MSGL_V, "Language Descriptor: %s\n", es->lang);
2284 else if(ptr[j] == 0x5) //Registration Descriptor (looks like e fourCC :) )
2286 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor\n");
2287 if(descr_len < 4)
2289 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor length too short: %d, SKIPPING\n", descr_len);
2291 else
2293 char *d;
2294 memcpy(es->format_descriptor, &ptr[j+2], 4);
2295 es->format_descriptor[4] = 0;
2297 d = &ptr[j+2];
2298 if(d[0] == 'A' && d[1] == 'C' && d[2] == '-' && d[3] == '3')
2300 es->type = AUDIO_A52;
2302 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '1')
2304 es->type = AUDIO_DTS;
2306 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '2')
2308 es->type = AUDIO_DTS;
2310 else if(d[0] == 'V' && d[1] == 'C' && d[2] == '-' && d[3] == '1')
2312 es->type = VIDEO_VC1;
2314 else
2315 es->type = UNKNOWN;
2316 mp_msg(MSGT_DEMUX, MSGL_DBG2, "FORMAT %s\n", es->format_descriptor);
2319 else if(ptr[j] == 0x1e)
2321 es->mp4_es_id = (ptr[j+2] << 8) | ptr[j+3];
2322 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);
2324 else
2325 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Unknown descriptor 0x%x, SKIPPING\n", ptr[j]);
2327 len -= 2 + descr_len;
2328 j += 2 + descr_len;
2331 return 1;
2334 static int parse_sl_section(pmt_t *pmt, ts_section_t *section, int is_start, unsigned char *buff, int size)
2336 int tid, len, skip;
2337 uint8_t *ptr;
2338 skip = collect_section(section, is_start, buff, size);
2339 if(! skip)
2340 return 0;
2342 ptr = &(section->buffer[skip]);
2343 tid = ptr[0];
2344 len = ((ptr[1] & 0x0f) << 8) | ptr[2];
2345 mp_msg(MSGT_DEMUX, MSGL_V, "TABLEID: %d (av. %d), skip=%d, LEN: %d\n", tid, section->buffer_len, skip, len);
2346 if(len > 4093 || section->buffer_len < len || tid != 5)
2348 mp_msg(MSGT_DEMUX, MSGL_V, "SECTION TOO LARGE or wrong section type, EXIT\n");
2349 return 0;
2352 if(! (ptr[5] & 1))
2353 return 0;
2355 //8 is the current position, len - 9 is the amount of data available
2356 parse_mp4_descriptors(pmt, &ptr[8], len - 9, NULL);
2358 return 1;
2361 static int parse_pmt(ts_priv_t * priv, uint16_t progid, uint16_t pid, int is_start, unsigned char *buff, int size)
2363 unsigned char *base, *es_base;
2364 pmt_t *pmt;
2365 int32_t idx, es_count, section_bytes;
2366 uint8_t m=0;
2367 int skip;
2368 pmt_t *tmp;
2369 struct pmt_es_t *tmp_es;
2370 ts_section_t *section;
2371 ES_stream_t *tss;
2373 idx = progid_idx_in_pmt(priv, progid);
2375 if(idx == -1)
2377 int sz = (priv->pmt_cnt + 1) * sizeof(pmt_t);
2378 tmp = realloc_struct(priv->pmt, priv->pmt_cnt + 1, sizeof(pmt_t));
2379 if(tmp == NULL)
2381 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
2382 return 0;
2384 priv->pmt = tmp;
2385 idx = priv->pmt_cnt;
2386 memset(&(priv->pmt[idx]), 0, sizeof(pmt_t));
2387 priv->pmt_cnt++;
2388 priv->pmt[idx].progid = progid;
2391 pmt = &(priv->pmt[idx]);
2393 section = &(pmt->section);
2394 skip = collect_section(section, is_start, buff, size);
2395 if(! skip)
2396 return 0;
2398 base = &(section->buffer[skip]);
2400 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",
2401 progid, pmt->section.buffer_len, is_start, pid, size, m, pmt->es_cnt, idx, pmt);
2403 pmt->table_id = base[0];
2404 if(pmt->table_id != 2)
2405 return -1;
2406 pmt->ssi = base[1] & 0x80;
2407 pmt->section_length = (((base[1] & 0xf) << 8 ) | base[2]);
2408 pmt->version_number = (base[5] >> 1) & 0x1f;
2409 pmt->curr_next = (base[5] & 1);
2410 pmt->section_number = base[6];
2411 pmt->last_section_number = base[7];
2412 pmt->PCR_PID = ((base[8] & 0x1f) << 8 ) | base[9];
2413 pmt->prog_descr_length = ((base[10] & 0xf) << 8 ) | base[11];
2414 if(pmt->prog_descr_length > pmt->section_length - 9)
2416 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, INVALID PROG_DESCR LENGTH (%d vs %d)\n", pmt->prog_descr_length, pmt->section_length - 9);
2417 return -1;
2420 if(pmt->prog_descr_length)
2421 parse_program_descriptors(pmt, &base[12], pmt->prog_descr_length);
2423 es_base = &base[12 + pmt->prog_descr_length]; //the beginning of th ES loop
2425 section_bytes= pmt->section_length - 13 - pmt->prog_descr_length;
2426 es_count = 0;
2428 while(section_bytes >= 5)
2430 int es_pid, es_type;
2432 es_type = es_base[0];
2433 es_pid = ((es_base[1] & 0x1f) << 8) | es_base[2];
2435 idx = es_pid_in_pmt(pmt, es_pid);
2436 if(idx == -1)
2438 int sz = sizeof(struct pmt_es_t) * (pmt->es_cnt + 1);
2439 tmp_es = realloc_struct(pmt->es, pmt->es_cnt + 1, sizeof(struct pmt_es_t));
2440 if(tmp_es == NULL)
2442 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT, COULDN'T ALLOCATE %d bytes for PMT_ES\n", sz);
2443 continue;
2445 pmt->es = tmp_es;
2446 idx = pmt->es_cnt;
2447 memset(&(pmt->es[idx]), 0, sizeof(struct pmt_es_t));
2448 pmt->es_cnt++;
2451 pmt->es[idx].descr_length = ((es_base[3] & 0xf) << 8) | es_base[4];
2454 if(pmt->es[idx].descr_length > section_bytes - 5)
2456 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, ES_DESCR_LENGTH TOO LARGE %d > %d, EXIT\n",
2457 pmt->es[idx].descr_length, section_bytes - 5);
2458 return -1;
2462 pmt->es[idx].pid = es_pid;
2463 if(es_type != 0x6)
2464 pmt->es[idx].type = UNKNOWN;
2465 else
2466 pmt->es[idx].type = es_type;
2468 parse_descriptors(&pmt->es[idx], &es_base[5]);
2470 switch(es_type)
2472 case 1:
2473 pmt->es[idx].type = VIDEO_MPEG1;
2474 break;
2475 case 2:
2476 pmt->es[idx].type = VIDEO_MPEG2;
2477 break;
2478 case 3:
2479 case 4:
2480 pmt->es[idx].type = AUDIO_MP2;
2481 break;
2482 case 6:
2483 if(pmt->es[idx].type == 0x6) //this could have been ovrwritten by parse_descriptors
2484 pmt->es[idx].type = UNKNOWN;
2485 break;
2486 case 0x10:
2487 pmt->es[idx].type = VIDEO_MPEG4;
2488 break;
2489 case 0x0f:
2490 case 0x11:
2491 pmt->es[idx].type = AUDIO_AAC;
2492 break;
2493 case 0x1b:
2494 pmt->es[idx].type = VIDEO_H264;
2495 break;
2496 case 0x12:
2497 pmt->es[idx].type = SL_PES_STREAM;
2498 break;
2499 case 0x13:
2500 pmt->es[idx].type = SL_SECTION;
2501 break;
2502 case 0x81:
2503 pmt->es[idx].type = AUDIO_A52;
2504 break;
2505 case 0x8A:
2506 case 0x82:
2507 case 0x86:
2508 pmt->es[idx].type = AUDIO_DTS;
2509 break;
2510 case 0xEA:
2511 pmt->es[idx].type = VIDEO_VC1;
2512 break;
2513 default:
2514 mp_msg(MSGT_DEMUX, MSGL_DBG2, "UNKNOWN ES TYPE=0x%x\n", es_type);
2515 pmt->es[idx].type = UNKNOWN;
2518 tss = priv->ts.pids[es_pid]; //an ES stream
2519 if(tss == NULL)
2521 tss = new_pid(priv, es_pid);
2522 if(tss)
2523 tss->type = pmt->es[idx].type;
2526 section_bytes -= 5 + pmt->es[idx].descr_length;
2527 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",
2528 progid, idx, es_count, pmt->es[idx].pid, pmt->es[idx].pid, pmt->es[idx].type, pmt->es[idx].descr_length, section_bytes);
2531 es_base += 5 + pmt->es[idx].descr_length;
2533 es_count++;
2536 mp_msg(MSGT_DEMUX, MSGL_V, "----------------------------\n");
2537 return 1;
2540 static pmt_t* pmt_of_pid(ts_priv_t *priv, int pid, mp4_decoder_config_t **mp4_dec)
2542 int32_t i, j, k;
2544 if(priv->pmt)
2546 for(i = 0; i < priv->pmt_cnt; i++)
2548 if(priv->pmt[i].es && priv->pmt[i].es_cnt)
2550 for(j = 0; j < priv->pmt[i].es_cnt; j++)
2552 if(priv->pmt[i].es[j].pid == pid)
2554 //search mp4_es_id
2555 if(priv->pmt[i].es[j].mp4_es_id)
2557 for(k = 0; k < priv->pmt[i].mp4es_cnt; k++)
2559 if(priv->pmt[i].mp4es[k].id == priv->pmt[i].es[j].mp4_es_id)
2561 *mp4_dec = &(priv->pmt[i].mp4es[k].decoder);
2562 break;
2567 return &(priv->pmt[i]);
2574 return NULL;
2578 static inline int32_t pid_type_from_pmt(ts_priv_t *priv, int pid)
2580 int32_t pmt_idx, pid_idx, i, j;
2582 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2584 if(pmt_idx != -1)
2586 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2587 if(pid_idx != -1)
2588 return priv->pmt[pmt_idx].es[pid_idx].type;
2590 //else
2592 for(i = 0; i < priv->pmt_cnt; i++)
2594 pmt_t *pmt = &(priv->pmt[i]);
2595 for(j = 0; j < pmt->es_cnt; j++)
2596 if(pmt->es[j].pid == pid)
2597 return pmt->es[j].type;
2601 return UNKNOWN;
2605 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid)
2607 int32_t pmt_idx, pid_idx, i, j;
2609 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2611 if(pmt_idx != -1)
2613 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2614 if(pid_idx != -1)
2615 return priv->pmt[pmt_idx].es[pid_idx].lang;
2617 else
2619 for(i = 0; i < priv->pmt_cnt; i++)
2621 pmt_t *pmt = &(priv->pmt[i]);
2622 for(j = 0; j < pmt->es_cnt; j++)
2623 if(pmt->es[j].pid == pid)
2624 return pmt->es[j].lang;
2628 return NULL;
2632 static int fill_packet(demuxer_t *demuxer, demux_stream_t *ds, demux_packet_t **dp, int *dp_offset, TS_stream_info *si)
2634 int ret = 0;
2636 if((*dp != NULL) && (*dp_offset > 0))
2638 ret = *dp_offset;
2639 resize_demux_packet(*dp, ret); //shrinked to the right size
2640 ds_add_packet(ds, *dp);
2641 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);
2642 if(si)
2644 float diff = (*dp)->pts - si->last_pts;
2645 float dur;
2647 if(abs(diff) > 1) //1 second, there's a discontinuity
2649 si->duration += si->last_pts - si->first_pts;
2650 si->first_pts = si->last_pts = (*dp)->pts;
2652 else
2654 si->last_pts = (*dp)->pts;
2656 si->size += ret;
2657 dur = si->duration + (si->last_pts - si->first_pts);
2659 if(dur > 0 && ds == demuxer->video)
2661 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2662 if(dur > 1) //otherwise it may be unreliable
2663 priv->vbitrate = (uint32_t) ((float) si->size / dur);
2668 *dp = NULL;
2669 *dp_offset = 0;
2671 return ret;
2674 static int fill_extradata(mp4_decoder_config_t * mp4_dec, ES_stream_t *tss)
2676 uint8_t *tmp;
2678 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4_dec: %p, pid: %d\n", mp4_dec, tss->pid);
2680 if(mp4_dec->buf_size > tss->extradata_alloc)
2682 tmp = (uint8_t *) realloc(tss->extradata, mp4_dec->buf_size);
2683 if(!tmp)
2684 return 0;
2685 tss->extradata = tmp;
2686 tss->extradata_alloc = mp4_dec->buf_size;
2688 memcpy(tss->extradata, mp4_dec->buf, mp4_dec->buf_size);
2689 tss->extradata_len = mp4_dec->buf_size;
2690 mp_msg(MSGT_DEMUX, MSGL_V, "EXTRADATA: %p, alloc=%d, len=%d\n", tss->extradata, tss->extradata_alloc, tss->extradata_len);
2692 return tss->extradata_len;
2695 // 0 = EOF or no stream found
2696 // else = [-] number of bytes written to the packet
2697 static int ts_parse(demuxer_t *demuxer , ES_stream_t *es, unsigned char *packet, int probe)
2699 ES_stream_t *tss;
2700 uint8_t done = 0;
2701 int buf_size, is_start, pid, base;
2702 int len, cc, cc_ok, afc, retv = 0, is_video, is_audio, is_sub;
2703 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2704 stream_t *stream = demuxer->stream;
2705 char *p;
2706 demux_stream_t *ds = NULL;
2707 demux_packet_t **dp = NULL;
2708 int *dp_offset = 0, *buffer_size = 0;
2709 int32_t progid, pid_type, bad, ts_error;
2710 int junk = 0, rap_flag = 0;
2711 pmt_t *pmt;
2712 mp4_decoder_config_t *mp4_dec;
2713 TS_stream_info *si;
2716 while(! done)
2718 bad = ts_error = 0;
2719 ds = (demux_stream_t*) NULL;
2720 dp = (demux_packet_t **) NULL;
2721 dp_offset = buffer_size = NULL;
2722 rap_flag = 0;
2723 mp4_dec = NULL;
2724 es->is_synced = 0;
2725 si = NULL;
2727 junk = priv->ts.packet_size - TS_PACKET_SIZE;
2728 buf_size = priv->ts.packet_size - junk;
2730 if(stream_eof(stream))
2732 if(! probe)
2734 ts_dump_streams(priv);
2735 demuxer->filepos = stream_tell(demuxer->stream);
2738 return 0;
2742 if(! ts_sync(stream))
2744 mp_msg(MSGT_DEMUX, MSGL_INFO, "TS_PARSE: COULDN'T SYNC\n");
2745 return 0;
2748 len = stream_read(stream, &packet[1], 3);
2749 if (len != 3)
2750 return 0;
2751 buf_size -= 4;
2753 if((packet[1] >> 7) & 0x01) //transport error
2754 ts_error = 1;
2757 is_start = packet[1] & 0x40;
2758 pid = ((packet[1] & 0x1f) << 8) | packet[2];
2760 tss = priv->ts.pids[pid]; //an ES stream
2761 if(tss == NULL)
2763 tss = new_pid(priv, pid);
2764 if(tss == NULL)
2765 continue;
2768 cc = (packet[3] & 0xf);
2769 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
2770 tss->last_cc = cc;
2772 bad = ts_error; // || (! cc_ok);
2773 if(bad)
2775 if(priv->keep_broken == 0)
2777 stream_skip(stream, buf_size-1+junk);
2778 continue;
2781 is_start = 0; //queued to the packet data
2784 if(is_start)
2785 tss->is_synced = 1;
2787 if((!is_start && !tss->is_synced) || ((pid > 1) && (pid < 16)) || (pid == 8191)) //invalid pid
2789 stream_skip(stream, buf_size-1+junk);
2790 continue;
2794 afc = (packet[3] >> 4) & 3;
2795 if(! (afc % 2)) //no payload in this TS packet
2797 stream_skip(stream, buf_size-1+junk);
2798 continue;
2801 if(afc > 1)
2803 int c;
2804 c = stream_read_char(stream);
2805 buf_size--;
2806 if(c < 0 || c > 183) //broken from the stream layer or invalid
2808 stream_skip(stream, buf_size-1+junk);
2809 continue;
2812 //c==0 is allowed!
2813 if(c > 0)
2815 uint8_t pcrbuf[188];
2816 int flags = stream_read_char(stream);
2817 int has_pcr;
2818 rap_flag = (flags & 0x40) >> 6;
2819 has_pcr = flags & 0x10;
2821 buf_size--;
2822 c--;
2823 stream_read(stream, pcrbuf, c);
2825 if(has_pcr)
2827 int pcr_pid = prog_pcr_pid(priv, priv->prog);
2828 if(pcr_pid == pid)
2830 uint64_t pcr, pcr_ext;
2832 pcr = (int64_t)(pcrbuf[0]) << 25;
2833 pcr |= pcrbuf[1] << 17 ;
2834 pcr |= (pcrbuf[2]) << 9;
2835 pcr |= pcrbuf[3] << 1 ;
2836 pcr |= (pcrbuf[4] & 0x80) >> 7;
2838 pcr_ext = (pcrbuf[4] & 0x01) << 8;
2839 pcr_ext |= pcrbuf[5];
2841 pcr = pcr * 300 + pcr_ext;
2843 demuxer->reference_clock = (double)pcr/(double)27000000.0;
2847 buf_size -= c;
2848 if(buf_size == 0)
2849 continue;
2853 //find the program that the pid belongs to; if (it's the right one or -1) && pid_type==SL_SECTION
2854 //call parse_sl_section()
2855 pmt = pmt_of_pid(priv, pid, &mp4_dec);
2856 if(mp4_dec)
2858 fill_extradata(mp4_dec, tss);
2859 if(IS_VIDEO(mp4_dec->object_type) || IS_AUDIO(mp4_dec->object_type))
2861 tss->type = SL_PES_STREAM;
2862 tss->subtype = mp4_dec->object_type;
2867 //TABLE PARSING
2869 base = priv->ts.packet_size - buf_size;
2871 priv->last_pid = pid;
2873 is_video = IS_VIDEO(tss->type) || (tss->type==SL_PES_STREAM && IS_VIDEO(tss->subtype));
2874 is_audio = IS_AUDIO(tss->type) || (tss->type==SL_PES_STREAM && IS_AUDIO(tss->subtype)) || (tss->type == PES_PRIVATE1);
2875 is_sub = ((tss->type == SPU_DVD) || (tss->type == SPU_DVB));
2876 pid_type = pid_type_from_pmt(priv, pid);
2878 // PES CONTENT STARTS HERE
2879 if(! probe)
2881 if((is_video || is_audio) && is_start && !priv->ts.streams[pid].sh)
2882 ts_add_stream(demuxer, tss);
2884 if((pid == demuxer->sub->id)) //or the lang is right
2886 pid_type = SPU_DVD;
2889 if(is_video && (demuxer->video->id == priv->ts.streams[pid].id))
2891 ds = demuxer->video;
2893 dp = &priv->fifo[1].pack;
2894 dp_offset = &priv->fifo[1].offset;
2895 buffer_size = &priv->fifo[1].buffer_size;
2896 si = &priv->vstr;
2898 else if(is_audio && (demuxer->audio->id == priv->ts.streams[pid].id))
2900 ds = demuxer->audio;
2902 dp = &priv->fifo[0].pack;
2903 dp_offset = &priv->fifo[0].offset;
2904 buffer_size = &priv->fifo[0].buffer_size;
2905 si = &priv->astr;
2907 else if(is_sub
2908 || (pid_type == SPU_DVD) || (pid_type == SPU_DVB))
2910 //SUBS are infrequent, so the initial detection may fail
2911 // and we may need to add them at play-time
2912 if(demuxer->sub->id == -1)
2914 uint16_t p;
2915 p = progid_for_pid(priv, tss->pid, priv->prog);
2917 if(p == priv->prog)
2919 int asgn = 0;
2920 uint8_t *lang;
2922 if(dvdsub_lang)
2924 if ((lang = pid_lang_from_pmt(priv, pid)))
2925 asgn = (strncmp(lang, dvdsub_lang, 3) == 0);
2927 else //no language specified with -slang
2928 asgn = 1;
2930 if(asgn)
2932 demuxer->sub->id = tss->pid;
2933 mp_msg(MSGT_DEMUX, MSGL_INFO, "CHOSEN SUBs pid 0x%x (%d) FROM PROG %d\n", tss->pid, tss->pid, priv->prog);
2938 if(demuxer->sub->id == tss->pid)
2940 ds = demuxer->sub;
2942 dp = &priv->fifo[2].pack;
2943 dp_offset = &priv->fifo[2].offset;
2944 buffer_size = &priv->fifo[2].buffer_size;
2946 else
2948 stream_skip(stream, buf_size+junk);
2949 continue;
2953 //IS IT TIME TO QUEUE DATA to the dp_packet?
2954 if(is_start && (dp != NULL))
2956 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
2960 if(dp && *dp == NULL)
2962 if(*buffer_size > MAX_PACK_BYTES)
2963 *buffer_size = MAX_PACK_BYTES;
2964 *dp = new_demux_packet(*buffer_size); //es->size
2965 *dp_offset = 0;
2966 if(! *dp)
2968 fprintf(stderr, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", *buffer_size);
2969 continue;
2971 mp_msg(MSGT_DEMUX, MSGL_DBG2, "CREATED DP(%d)\n", *buffer_size);
2976 if(probe || !dp) //dp is NULL for tables and sections
2978 p = &packet[base];
2980 else //feeding
2982 if(*dp_offset + buf_size > *buffer_size)
2984 *buffer_size = *dp_offset + buf_size + TS_FEC_PACKET_SIZE;
2985 resize_demux_packet(*dp, *buffer_size);
2987 p = &((*dp)->buffer[*dp_offset]);
2990 len = stream_read(stream, p, buf_size);
2991 if(len < buf_size)
2993 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\r\nts_parse() couldn't read enough data: %d < %d\r\n", len, buf_size);
2994 continue;
2996 stream_skip(stream, junk);
2998 if(pid == 0)
3000 parse_pat(priv, is_start, p, buf_size);
3001 continue;
3003 else if((tss->type == SL_SECTION) && pmt)
3005 int k, mp4_es_id = -1;
3006 ts_section_t *section;
3007 for(k = 0; k < pmt->mp4es_cnt; k++)
3009 if(pmt->mp4es[k].decoder.object_type == MP4_OD && pmt->mp4es[k].decoder.stream_type == MP4_OD)
3010 mp4_es_id = pmt->mp4es[k].id;
3012 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4ESID: %d\n", mp4_es_id);
3013 for(k = 0; k < pmt->es_cnt; k++)
3015 if(pmt->es[k].mp4_es_id == mp4_es_id)
3017 section = &(tss->section);
3018 parse_sl_section(pmt, section, is_start, &packet[base], buf_size);
3021 continue;
3023 else
3025 progid = prog_id_in_pat(priv, pid);
3026 if(progid != -1)
3028 if(pid != demuxer->video->id && pid != demuxer->audio->id && pid != demuxer->sub->id)
3030 parse_pmt(priv, progid, pid, is_start, &packet[base], buf_size);
3031 continue;
3033 else
3034 mp_msg(MSGT_DEMUX, MSGL_ERR, "Argh! Data pid %d used in the PMT, Skipping PMT parsing!\n", pid);
3038 if(!probe && !dp)
3039 continue;
3041 if(is_start)
3043 uint8_t *lang = NULL;
3045 mp_msg(MSGT_DEMUX, MSGL_DBG2, "IS_START\n");
3047 len = pes_parse2(p, buf_size, es, pid_type, pmt, pid);
3048 if(! len)
3050 tss->is_synced = 0;
3051 continue;
3053 es->pid = tss->pid;
3054 tss->is_synced |= es->is_synced || rap_flag;
3055 tss->payload_size = es->payload_size;
3057 if(is_audio && (lang = pid_lang_from_pmt(priv, es->pid)))
3059 memcpy(es->lang, lang, 3);
3060 es->lang[3] = 0;
3062 else
3063 es->lang[0] = 0;
3065 if(probe)
3067 if(es->type == UNKNOWN)
3068 return 0;
3070 tss->type = es->type;
3071 tss->subtype = es->subtype;
3073 return 1;
3075 else
3077 if(es->pts == 0.0f)
3078 es->pts = tss->pts = tss->last_pts;
3079 else
3080 tss->pts = tss->last_pts = es->pts;
3082 mp_msg(MSGT_DEMUX, MSGL_DBG2, "ts_parse, NEW pid=%d, PSIZE: %u, type=%X, start=%p, len=%d\n",
3083 es->pid, es->payload_size, es->type, es->start, es->size);
3085 demuxer->filepos = stream_tell(demuxer->stream) - es->size;
3087 memmove(p, es->start, es->size);
3088 *dp_offset += es->size;
3089 (*dp)->flags = 0;
3090 (*dp)->pos = stream_tell(demuxer->stream);
3091 (*dp)->pts = es->pts;
3093 if(retv > 0)
3094 return retv;
3095 else
3096 continue;
3099 else
3101 uint16_t sz;
3103 es->pid = tss->pid;
3104 es->type = tss->type;
3105 es->subtype = tss->subtype;
3106 es->pts = tss->pts = tss->last_pts;
3107 es->start = &packet[base];
3110 if(tss->payload_size > 0)
3112 sz = FFMIN(tss->payload_size, buf_size);
3113 tss->payload_size -= sz;
3114 es->size = sz;
3116 else
3118 if(is_video)
3120 sz = es->size = buf_size;
3122 else
3124 continue;
3129 if(! probe)
3131 *dp_offset += sz;
3133 if(*dp_offset >= MAX_PACK_BYTES)
3135 (*dp)->pts = tss->last_pts;
3136 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
3137 return 1;
3140 continue;
3142 else
3144 memcpy(es->start, p, sz);
3146 if(es->size)
3147 return es->size;
3148 else
3149 continue;
3154 return 0;
3158 void skip_audio_frame(sh_audio_t *sh_audio);
3160 static void reset_fifos(demuxer_t *demuxer, int a, int v, int s)
3162 ts_priv_t* priv = demuxer->priv;
3163 if(a)
3165 if(priv->fifo[0].pack != NULL)
3167 free_demux_packet(priv->fifo[0].pack);
3168 priv->fifo[0].pack = NULL;
3170 priv->fifo[0].offset = 0;
3173 if(v)
3175 if(priv->fifo[1].pack != NULL)
3177 free_demux_packet(priv->fifo[1].pack);
3178 priv->fifo[1].pack = NULL;
3180 priv->fifo[1].offset = 0;
3183 if(s)
3185 if(priv->fifo[2].pack != NULL)
3187 free_demux_packet(priv->fifo[2].pack);
3188 priv->fifo[2].pack = NULL;
3190 priv->fifo[2].offset = 0;
3192 demuxer->reference_clock = MP_NOPTS_VALUE;
3196 static void demux_seek_ts(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
3198 demux_stream_t *d_audio=demuxer->audio;
3199 demux_stream_t *d_video=demuxer->video;
3200 sh_audio_t *sh_audio=d_audio->sh;
3201 sh_video_t *sh_video=d_video->sh;
3202 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
3203 int i, video_stats;
3204 off_t newpos;
3206 //================= seek in MPEG-TS ==========================
3208 ts_dump_streams(demuxer->priv);
3209 reset_fifos(demuxer, sh_audio != NULL, sh_video != NULL, demuxer->sub->id > 0);
3211 demux_flush(demuxer);
3215 video_stats = (sh_video != NULL);
3216 if(video_stats)
3218 mp_msg(MSGT_DEMUX, MSGL_V, "IBPS: %d, vb: %d\r\n", sh_video->i_bps, priv->vbitrate);
3219 if(priv->vbitrate)
3220 video_stats = priv->vbitrate;
3221 else
3222 video_stats = sh_video->i_bps;
3225 newpos = (flags & SEEK_ABSOLUTE) ? demuxer->movi_start : demuxer->filepos;
3226 if(flags & SEEK_FACTOR) // float seek 0..1
3227 newpos+=(demuxer->movi_end-demuxer->movi_start)*rel_seek_secs;
3228 else
3230 // time seek (secs)
3231 if(! video_stats) // unspecified or VBR
3232 newpos += 2324*75*rel_seek_secs; // 174.3 kbyte/sec
3233 else
3234 newpos += video_stats*rel_seek_secs;
3238 if(newpos < demuxer->movi_start)
3239 newpos = demuxer->movi_start; //begininng of stream
3241 stream_seek(demuxer->stream, newpos);
3242 for(i = 0; i < 8192; i++)
3243 if(priv->ts.pids[i] != NULL)
3244 priv->ts.pids[i]->is_synced = 0;
3246 videobuf_code_len = 0;
3248 if(sh_video != NULL)
3249 ds_fill_buffer(d_video);
3251 if(sh_audio != NULL)
3253 ds_fill_buffer(d_audio);
3256 while(sh_video != NULL)
3258 if(sh_audio && !d_audio->eof && d_video->pts && d_audio->pts)
3260 float a_pts=d_audio->pts;
3261 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
3262 if(d_video->pts > a_pts)
3264 skip_audio_frame(sh_audio); // sync audio
3265 continue;
3270 i = sync_video_packet(d_video);
3271 if((sh_video->format == VIDEO_MPEG1) || (sh_video->format == VIDEO_MPEG2))
3273 if(i==0x1B3 || i==0x1B8) break; // found it!
3275 else if((sh_video->format == VIDEO_MPEG4) && (i==0x1B6))
3276 break;
3277 else if(sh_video->format == VIDEO_VC1 && (i==0x10E || i==0x10F))
3278 break;
3279 else //H264
3281 if((i & ~0x60) == 0x105 || (i & ~0x60) == 0x107) break;
3284 if(!i || !skip_video_packet(d_video)) break; // EOF?
3289 static int demux_ts_fill_buffer(demuxer_t * demuxer, demux_stream_t *ds)
3291 ES_stream_t es;
3292 ts_priv_t *priv = (ts_priv_t *)demuxer->priv;
3294 return -ts_parse(demuxer, &es, priv->packet, 0);
3298 static int ts_check_file_dmx(demuxer_t *demuxer)
3300 return ts_check_file(demuxer) ? DEMUXER_TYPE_MPEG_TS : 0;
3303 static int is_usable_program(ts_priv_t *priv, pmt_t *pmt)
3305 int j;
3307 for(j = 0; j < pmt->es_cnt; j++)
3309 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3310 continue;
3312 priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO ||
3313 priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO
3315 return 1;
3318 return 0;
3321 static int demux_ts_control(demuxer_t *demuxer, int cmd, void *arg)
3323 ts_priv_t* priv = (ts_priv_t *)demuxer->priv;
3325 switch(cmd)
3327 case DEMUXER_CTRL_SWITCH_AUDIO:
3328 case DEMUXER_CTRL_SWITCH_VIDEO:
3330 void *sh = NULL;
3331 int i, n;
3332 int reftype, areset = 0, vreset = 0;
3333 demux_stream_t *ds;
3335 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
3337 reftype = TYPE_VIDEO;
3338 ds = demuxer->video;
3339 vreset = 1;
3341 else
3343 reftype = TYPE_AUDIO;
3344 ds = demuxer->audio;
3345 areset = 1;
3347 n = *((int*)arg);
3348 if(n == -2)
3350 reset_fifos(demuxer, areset, vreset, 0);
3351 ds->id = -2;
3352 ds->sh = NULL;
3353 ds_free_packs(ds);
3354 *((int*)arg) = ds->id;
3355 return DEMUXER_CTRL_OK;
3358 if(n < 0)
3360 for(i = 0; i < 8192; i++)
3362 if(priv->ts.streams[i].id == ds->id && priv->ts.streams[i].type == reftype)
3363 break;
3366 while(!sh)
3368 i = (i+1) % 8192;
3369 if(priv->ts.streams[i].type == reftype)
3371 if(priv->ts.streams[i].id == ds->id) //we made a complete loop
3372 break;
3373 sh = priv->ts.streams[i].sh;
3377 else //audio track <n>
3379 if (n >= 8192 || priv->ts.streams[n].type != reftype) return DEMUXER_CTRL_NOTIMPL;
3380 i = n;
3381 sh = priv->ts.streams[i].sh;
3384 if(sh)
3386 if(ds->id != priv->ts.streams[i].id)
3387 reset_fifos(demuxer, areset, vreset, 0);
3388 ds->id = priv->ts.streams[i].id;
3389 ds->sh = sh;
3390 ds_free_packs(ds);
3391 mp_msg(MSGT_DEMUX, MSGL_V, "\r\ndemux_ts, switched to audio pid %d, id: %d, sh: %p\r\n", i, ds->id, sh);
3394 *((int*)arg) = ds->id;
3395 return DEMUXER_CTRL_OK;
3398 case DEMUXER_CTRL_IDENTIFY_PROGRAM: //returns in prog->{aid,vid} the new ids that comprise a program
3400 int i, j, cnt=0;
3401 int vid_done=0, aid_done=0;
3402 pmt_t *pmt = NULL;
3403 demux_program_t *prog = arg;
3405 if(priv->pmt_cnt < 2)
3406 return DEMUXER_CTRL_NOTIMPL;
3408 if(prog->progid == -1)
3410 int cur_pmt_idx = 0;
3412 for(i = 0; i < priv->pmt_cnt; i++)
3413 if(priv->pmt[i].progid == priv->prog)
3415 cur_pmt_idx = i;
3416 break;
3419 i = (cur_pmt_idx + 1) % priv->pmt_cnt;
3420 while(i != cur_pmt_idx)
3422 pmt = &priv->pmt[i];
3423 cnt = is_usable_program(priv, pmt);
3424 if(cnt)
3425 break;
3426 i = (i + 1) % priv->pmt_cnt;
3429 else
3431 for(i = 0; i < priv->pmt_cnt; i++)
3432 if(priv->pmt[i].progid == prog->progid)
3434 pmt = &priv->pmt[i]; //required program
3435 cnt = is_usable_program(priv, pmt);
3439 if(!cnt)
3440 return DEMUXER_CTRL_NOTIMPL;
3442 //finally some food
3443 prog->aid = prog->vid = -2; //no audio and no video by default
3444 for(j = 0; j < pmt->es_cnt; j++)
3446 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3447 continue;
3449 if(!vid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO)
3451 vid_done = 1;
3452 prog->vid = pmt->es[j].pid;
3454 else if(!aid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO)
3456 aid_done = 1;
3457 prog->aid = pmt->es[j].pid;
3461 priv->prog = prog->progid = pmt->progid;
3462 return DEMUXER_CTRL_OK;
3465 default:
3466 return DEMUXER_CTRL_NOTIMPL;
3471 const demuxer_desc_t demuxer_desc_mpeg_ts = {
3472 "MPEG-TS demuxer",
3473 "mpegts",
3474 "TS",
3475 "Nico Sabbi",
3477 DEMUXER_TYPE_MPEG_TS,
3478 0, // unsafe autodetect
3479 ts_check_file_dmx,
3480 demux_ts_fill_buffer,
3481 demux_open_ts,
3482 demux_close_ts,
3483 demux_seek_ts,
3484 demux_ts_control