Merge svn changes up to r30216
[mplayer/glamo.git] / libmpdemux / demux_ts.c
blobaf9e2109f4c073269472c4cee8805ddfa4dd51e9
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 AUDIO_TRUEHD = mmioFOURCC('T', 'R', 'H', 'D'),
81 SPU_DVD = 0x3000000,
82 SPU_DVB = 0x3000001,
83 SPU_TELETEXT = 0x3000002,
84 PES_PRIVATE1 = 0xBD00000,
85 SL_PES_STREAM = 0xD000000,
86 SL_SECTION = 0xD100000,
87 MP4_OD = 0xD200000,
88 } es_stream_type_t;
90 typedef struct {
91 uint8_t *buffer;
92 uint16_t buffer_len;
93 } ts_section_t;
95 typedef struct {
96 int size;
97 unsigned char *start;
98 uint16_t payload_size;
99 es_stream_type_t type, subtype;
100 float pts, last_pts;
101 int pid;
102 char lang[4];
103 int last_cc; // last cc code (-1 if first packet)
104 int is_synced;
105 ts_section_t section;
106 uint8_t *extradata;
107 int extradata_alloc, extradata_len;
108 struct {
109 uint8_t au_start, au_end, last_au_end;
110 } sl;
111 } ES_stream_t;
113 typedef struct {
114 void *sh;
115 int id;
116 int type;
117 } sh_av_t;
119 typedef struct MpegTSContext {
120 int packet_size; // raw packet size, including FEC if present e.g. 188 bytes
121 ES_stream_t *pids[NB_PID_MAX];
122 sh_av_t streams[NB_PID_MAX];
123 } MpegTSContext;
126 typedef struct {
127 demux_stream_t *ds;
128 demux_packet_t *pack;
129 int offset, buffer_size;
130 } av_fifo_t;
132 #define MAX_EXTRADATA_SIZE 64*1024
133 typedef struct {
134 int32_t object_type; //aka codec used
135 int32_t stream_type; //video, audio etc.
136 uint8_t buf[MAX_EXTRADATA_SIZE];
137 uint16_t buf_size;
138 uint8_t szm1;
139 } mp4_decoder_config_t;
141 typedef struct {
142 //flags
143 uint8_t flags;
144 uint8_t au_start;
145 uint8_t au_end;
146 uint8_t random_accesspoint;
147 uint8_t random_accesspoint_only;
148 uint8_t padding;
149 uint8_t use_ts;
150 uint8_t idle;
151 uint8_t duration;
153 uint32_t ts_resolution, ocr_resolution;
154 uint8_t ts_len, ocr_len, au_len, instant_bitrate_len, degr_len, au_seqnum_len, packet_seqnum_len;
155 uint32_t timescale;
156 uint16_t au_duration, cts_duration;
157 uint64_t ocr, dts, cts;
158 } mp4_sl_config_t;
160 typedef struct {
161 uint16_t id;
162 uint8_t flags;
163 mp4_decoder_config_t decoder;
164 mp4_sl_config_t sl;
165 } mp4_es_descr_t;
167 typedef struct {
168 uint16_t id;
169 uint8_t flags;
170 mp4_es_descr_t *es;
171 uint16_t es_cnt;
172 } mp4_od_t;
174 typedef struct {
175 uint8_t skip;
176 uint8_t table_id;
177 uint8_t ssi;
178 uint16_t section_length;
179 uint16_t ts_id;
180 uint8_t version_number;
181 uint8_t curr_next;
182 uint8_t section_number;
183 uint8_t last_section_number;
184 struct pat_progs_t {
185 uint16_t id;
186 uint16_t pmt_pid;
187 } *progs;
188 uint16_t progs_cnt;
189 ts_section_t section;
190 } pat_t;
192 typedef struct {
193 uint16_t progid;
194 uint8_t skip;
195 uint8_t table_id;
196 uint8_t ssi;
197 uint16_t section_length;
198 uint8_t version_number;
199 uint8_t curr_next;
200 uint8_t section_number;
201 uint8_t last_section_number;
202 uint16_t PCR_PID;
203 uint16_t prog_descr_length;
204 ts_section_t section;
205 uint16_t es_cnt;
206 struct pmt_es_t {
207 uint16_t pid;
208 uint32_t type; //it's 8 bit long, but cast to the right type as FOURCC
209 uint16_t descr_length;
210 uint8_t format_descriptor[5];
211 uint8_t lang[4];
212 uint16_t mp4_es_id;
213 } *es;
214 mp4_od_t iod, *od;
215 mp4_es_descr_t *mp4es;
216 int od_cnt, mp4es_cnt;
217 } pmt_t;
219 typedef struct {
220 uint64_t size;
221 float duration;
222 float first_pts;
223 float last_pts;
224 } TS_stream_info;
226 typedef struct {
227 MpegTSContext ts;
228 int last_pid;
229 av_fifo_t fifo[3]; //0 for audio, 1 for video, 2 for subs
230 pat_t pat;
231 pmt_t *pmt;
232 uint16_t pmt_cnt;
233 uint32_t prog;
234 uint32_t vbitrate;
235 int keep_broken;
236 int last_aid;
237 int last_vid;
238 char packet[TS_FEC_PACKET_SIZE];
239 TS_stream_info vstr, astr;
240 } ts_priv_t;
243 typedef struct {
244 es_stream_type_t type;
245 ts_section_t section;
246 } TS_pids_t;
249 #define IS_AUDIO(x) (((x) == AUDIO_MP2) || ((x) == AUDIO_A52) || ((x) == AUDIO_LPCM_BE) || ((x) == AUDIO_AAC) || ((x) == AUDIO_DTS) || ((x) == AUDIO_TRUEHD))
250 #define IS_VIDEO(x) (((x) == VIDEO_MPEG1) || ((x) == VIDEO_MPEG2) || ((x) == VIDEO_MPEG4) || ((x) == VIDEO_H264) || ((x) == VIDEO_AVC) || ((x) == VIDEO_VC1))
251 #define IS_SUB(x) (((x) == SPU_DVD) || ((x) == SPU_DVB) || ((x) == SPU_TELETEXT))
253 static int ts_parse(demuxer_t *demuxer, ES_stream_t *es, unsigned char *packet, int probe);
255 static uint8_t get_packet_size(const unsigned char *buf, int size)
257 int i;
259 if (size < (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS))
260 return 0;
262 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
264 if (buf[i * TS_PACKET_SIZE] != 0x47)
266 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
267 goto try_fec;
270 return TS_PACKET_SIZE;
272 try_fec:
273 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
275 if (buf[i * TS_FEC_PACKET_SIZE] != 0x47){
276 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
277 goto try_philips;
280 return TS_FEC_PACKET_SIZE;
282 try_philips:
283 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
285 if (buf[i * TS_PH_PACKET_SIZE] != 0x47)
286 return 0;
288 return TS_PH_PACKET_SIZE;
291 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h);
292 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid);
294 static void ts_add_stream(demuxer_t * demuxer, ES_stream_t *es)
296 int i;
297 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
299 if(priv->ts.streams[es->pid].sh)
300 return;
302 if((IS_AUDIO(es->type) || IS_AUDIO(es->subtype)) && priv->last_aid+1 < MAX_A_STREAMS)
304 sh_audio_t *sh = new_sh_audio_aid(demuxer, priv->last_aid, es->pid);
305 if(sh)
307 const char *lang = pid_lang_from_pmt(priv, es->pid);
308 sh->needs_parsing = 1;
309 sh->format = IS_AUDIO(es->type) ? es->type : es->subtype;
310 sh->ds = demuxer->audio;
312 priv->ts.streams[es->pid].id = priv->last_aid;
313 priv->ts.streams[es->pid].sh = sh;
314 priv->ts.streams[es->pid].type = TYPE_AUDIO;
315 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);
316 if (lang && lang[0])
317 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_AID_%d_LANG=%s\n", es->pid, lang);
318 priv->last_aid++;
321 if(es->extradata && es->extradata_len)
323 sh->wf = (WAVEFORMATEX *) malloc(sizeof (WAVEFORMATEX) + es->extradata_len);
324 sh->wf->cbSize = es->extradata_len;
325 memcpy(sh->wf + 1, es->extradata, es->extradata_len);
329 if((IS_VIDEO(es->type) || IS_VIDEO(es->subtype)) && priv->last_vid+1 < MAX_V_STREAMS)
331 sh_video_t *sh = new_sh_video_vid(demuxer, priv->last_vid, es->pid);
332 if(sh)
334 sh->format = IS_VIDEO(es->type) ? es->type : es->subtype;
335 sh->ds = demuxer->video;
337 priv->ts.streams[es->pid].id = priv->last_vid;
338 priv->ts.streams[es->pid].sh = sh;
339 priv->ts.streams[es->pid].type = TYPE_VIDEO;
340 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);
341 priv->last_vid++;
344 if(sh->format == VIDEO_AVC && es->extradata && es->extradata_len)
346 int w = 0, h = 0;
347 sh->bih = (BITMAPINFOHEADER *) calloc(1, sizeof(BITMAPINFOHEADER) + es->extradata_len);
348 sh->bih->biSize= sizeof(BITMAPINFOHEADER) + es->extradata_len;
349 sh->bih->biCompression = sh->format;
350 memcpy(sh->bih + 1, es->extradata, es->extradata_len);
351 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "EXTRADATA(%d BYTES): \n", es->extradata_len);
352 for(i = 0;i < es->extradata_len; i++)
353 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "%02x ", (int) es->extradata[i]);
354 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"\n");
355 if(parse_avc_sps(es->extradata, es->extradata_len, &w, &h))
357 sh->bih->biWidth = w;
358 sh->bih->biHeight = h;
365 static int ts_check_file(demuxer_t * demuxer)
367 const int buf_size = (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS);
368 unsigned char buf[TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS], done = 0, *ptr;
369 uint32_t _read, i, count = 0, is_ts;
370 int cc[NB_PID_MAX], last_cc[NB_PID_MAX], pid, cc_ok, c, good, bad;
371 uint8_t size = 0;
372 off_t pos = 0;
373 off_t init_pos;
375 mp_msg(MSGT_DEMUX, MSGL_V, "Checking for MPEG-TS...\n");
377 init_pos = stream_tell(demuxer->stream);
378 is_ts = 0;
379 while(! done)
381 i = 1;
382 c = 0;
384 while(((c=stream_read_char(demuxer->stream)) != 0x47)
385 && (c >= 0)
386 && (i < MAX_CHECK_SIZE)
387 && ! demuxer->stream->eof
388 ) i++;
391 if(c != 0x47)
393 mp_msg(MSGT_DEMUX, MSGL_V, "THIS DOESN'T LOOK LIKE AN MPEG-TS FILE!\n");
394 is_ts = 0;
395 done = 1;
396 continue;
399 pos = stream_tell(demuxer->stream) - 1;
400 buf[0] = c;
401 _read = stream_read(demuxer->stream, &buf[1], buf_size-1);
403 if(_read < buf_size-1)
405 mp_msg(MSGT_DEMUX, MSGL_V, "COULDN'T READ ENOUGH DATA, EXITING TS_CHECK\n");
406 stream_reset(demuxer->stream);
407 return 0;
410 size = get_packet_size(buf, buf_size);
411 if(size)
413 done = 1;
414 is_ts = 1;
417 if(pos - init_pos >= MAX_CHECK_SIZE)
419 done = 1;
420 is_ts = 0;
424 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);
425 stream_seek(demuxer->stream, pos);
427 if(! is_ts)
428 return 0;
430 //LET'S CHECK continuity counters
431 good = bad = 0;
432 for(count = 0; count < NB_PID_MAX; count++)
434 cc[count] = last_cc[count] = -1;
437 for(count = 0; count < NUM_CONSECUTIVE_TS_PACKETS; count++)
439 ptr = &(buf[size * count]);
440 pid = ((ptr[1] & 0x1f) << 8) | ptr[2];
441 mp_msg(MSGT_DEMUX, MSGL_DBG2, "BUF: %02x %02x %02x %02x, PID %d, SIZE: %d \n",
442 ptr[0], ptr[1], ptr[2], ptr[3], pid, size);
444 if((pid == 8191) || (pid < 16))
445 continue;
447 cc[pid] = (ptr[3] & 0xf);
448 cc_ok = (last_cc[pid] < 0) || ((((last_cc[pid] + 1) & 0x0f) == cc[pid]));
449 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PID %d, COMPARE CC %d AND LAST_CC %d\n", pid, cc[pid], last_cc[pid]);
450 if(! cc_ok)
451 //return 0;
452 bad++;
453 else
454 good++;
456 last_cc[pid] = cc[pid];
459 mp_msg(MSGT_DEMUX, MSGL_V, "GOOD CC: %d, BAD CC: %d\n", good, bad);
461 if(good >= bad)
462 return size;
463 else
464 return 0;
468 static inline int32_t progid_idx_in_pmt(ts_priv_t *priv, uint16_t progid)
470 int x;
472 if(priv->pmt == NULL)
473 return -1;
475 for(x = 0; x < priv->pmt_cnt; x++)
477 if(priv->pmt[x].progid == progid)
478 return x;
481 return -1;
485 static inline int32_t progid_for_pid(ts_priv_t *priv, int pid, int32_t req) //finds the first program listing a pid
487 int i, j;
488 pmt_t *pmt;
491 if(priv->pmt == NULL)
492 return -1;
495 for(i=0; i < priv->pmt_cnt; i++)
497 pmt = &(priv->pmt[i]);
499 if(pmt->es == NULL)
500 return -1;
502 for(j = 0; j < pmt->es_cnt; j++)
504 if(pmt->es[j].pid == pid)
506 if((req == 0) || (req == pmt->progid))
507 return pmt->progid;
512 return -1;
515 static inline int32_t prog_pcr_pid(ts_priv_t *priv, int progid)
517 int i;
519 if(priv->pmt == NULL)
520 return -1;
521 for(i=0; i < priv->pmt_cnt; i++)
523 if(priv->pmt[i].progid == progid)
524 return priv->pmt[i].PCR_PID;
526 return -1;
530 static inline int pid_match_lang(ts_priv_t *priv, uint16_t pid, char *lang)
532 uint16_t i, j;
533 pmt_t *pmt;
535 if(priv->pmt == NULL)
536 return -1;
538 for(i=0; i < priv->pmt_cnt; i++)
540 pmt = &(priv->pmt[i]);
542 if(pmt->es == NULL)
543 return -1;
545 for(j = 0; j < pmt->es_cnt; j++)
547 if(pmt->es[j].pid != pid)
548 continue;
550 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);
551 if(strncmp(pmt->es[j].lang, lang, 3) == 0)
553 return 1;
559 return -1;
562 typedef struct {
563 int32_t atype, vtype, stype; //types
564 int32_t apid, vpid, spid; //stream ids
565 char slang[4], alang[4]; //languages
566 uint16_t prog;
567 off_t probe;
568 } tsdemux_init_t;
570 //stripped down version of a52_syncinfo() from liba52
571 //copyright belongs to Michel Lespinasse <walken@zoy.org> and Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
572 int mp_a52_framesize(uint8_t * buf, int *srate)
574 int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
575 128, 160, 192, 224, 256, 320, 384, 448,
576 512, 576, 640
578 uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
579 int frmsizecod, bitrate, half;
581 if((buf[0] != 0x0b) || (buf[1] != 0x77)) /* syncword */
582 return 0;
584 if(buf[5] >= 0x60) /* bsid >= 12 */
585 return 0;
587 half = halfrate[buf[5] >> 3];
589 frmsizecod = buf[4] & 63;
590 if(frmsizecod >= 38)
591 return 0;
593 bitrate = rate[frmsizecod >> 1];
595 switch(buf[4] & 0xc0)
597 case 0: /* 48 KHz */
598 *srate = 48000 >> half;
599 return 4 * bitrate;
600 case 0x40: /* 44.1 KHz */
601 *srate = 44100 >> half;
602 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
603 case 0x80: /* 32 KHz */
604 *srate = 32000 >> half;
605 return 6 * bitrate;
608 return 0;
611 //second stage: returns the count of A52 syncwords found
612 static int a52_check(char *buf, int len)
614 int cnt, frame_length, ok, srate;
616 cnt = ok = 0;
617 if(len < 8)
618 return 0;
620 while(cnt < len - 7)
622 if(buf[cnt] == 0x0B && buf[cnt+1] == 0x77)
624 frame_length = mp_a52_framesize(&buf[cnt], &srate);
625 if(frame_length>=7 && frame_length<=3840)
627 cnt += frame_length;
628 ok++;
630 else
631 cnt++;
633 else
634 cnt++;
637 mp_msg(MSGT_DEMUXER, MSGL_V, "A52_CHECK(%d input bytes), found %d frame syncwords of %d bytes length\n", len, ok, frame_length);
638 return ok;
642 static off_t ts_detect_streams(demuxer_t *demuxer, tsdemux_init_t *param)
644 int video_found = 0, audio_found = 0, sub_found = 0, i, num_packets = 0, req_apid, req_vpid, req_spid;
645 int is_audio, is_video, is_sub, has_tables;
646 int32_t p, chosen_pid = 0;
647 off_t pos=0, ret = 0, init_pos, end_pos;
648 ES_stream_t es;
649 unsigned char tmp[TS_FEC_PACKET_SIZE];
650 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
651 struct {
652 char *buf;
653 int pos;
654 } pes_priv1[8192], *pptr;
655 char *tmpbuf;
657 priv->last_pid = 8192; //invalid pid
659 req_apid = param->apid;
660 req_vpid = param->vpid;
661 req_spid = param->spid;
663 has_tables = 0;
664 memset(pes_priv1, 0, sizeof(pes_priv1));
665 init_pos = stream_tell(demuxer->stream);
666 mp_msg(MSGT_DEMUXER, MSGL_V, "PROBING UP TO %"PRIu64", PROG: %d\n", (uint64_t) param->probe, param->prog);
667 end_pos = init_pos + (param->probe ? param->probe : TS_MAX_PROBE_SIZE);
668 while(1)
670 pos = stream_tell(demuxer->stream);
671 if(pos > end_pos || demuxer->stream->eof)
672 break;
674 if(ts_parse(demuxer, &es, tmp, 1))
676 //Non PES-aligned A52 audio may escape detection if PMT is not present;
677 //in this case we try to find at least 3 A52 syncwords
678 if((es.type == PES_PRIVATE1) && (! audio_found) && req_apid > -2)
680 pptr = &pes_priv1[es.pid];
681 if(pptr->pos < 64*1024)
683 tmpbuf = (char*) realloc(pptr->buf, pptr->pos + es.size);
684 if(tmpbuf != NULL)
686 pptr->buf = tmpbuf;
687 memcpy(&(pptr->buf[ pptr->pos ]), es.start, es.size);
688 pptr->pos += es.size;
689 if(a52_check(pptr->buf, pptr->pos) > 2)
691 param->atype = AUDIO_A52;
692 param->apid = es.pid;
693 es.type = AUDIO_A52;
699 is_audio = IS_AUDIO(es.type) || ((es.type==SL_PES_STREAM) && IS_AUDIO(es.subtype));
700 is_video = IS_VIDEO(es.type) || ((es.type==SL_PES_STREAM) && IS_VIDEO(es.subtype));
701 is_sub = IS_SUB(es.type);
704 if((! is_audio) && (! is_video) && (! is_sub))
705 continue;
706 if(is_audio && req_apid==-2)
707 continue;
709 if(is_video)
711 chosen_pid = (req_vpid == es.pid);
712 if((! chosen_pid) && (req_vpid > 0))
713 continue;
715 else if(is_audio)
717 if(req_apid > 0)
719 chosen_pid = (req_apid == es.pid);
720 if(! chosen_pid)
721 continue;
723 else if(param->alang[0] > 0 && es.lang[0] > 0)
725 if(pid_match_lang(priv, es.pid, param->alang) == -1)
726 continue;
728 chosen_pid = 1;
729 param->apid = req_apid = es.pid;
732 else if(is_sub)
734 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_SUBTITLE_ID=%d\n", es.pid);
735 if (es.lang[0] > 0)
736 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_SID_%d_LANG=%s\n", es.pid, es.lang);
737 chosen_pid = (req_spid == es.pid);
738 if((! chosen_pid) && (req_spid > 0))
739 continue;
742 if(req_apid < 0 && (param->alang[0] == 0) && req_vpid < 0 && req_spid < 0)
743 chosen_pid = 1;
745 if((ret == 0) && chosen_pid)
747 ret = stream_tell(demuxer->stream);
750 p = progid_for_pid(priv, es.pid, param->prog);
751 if(p != -1)
753 has_tables++;
754 if(!param->prog && chosen_pid)
755 param->prog = p;
758 if((param->prog > 0) && (param->prog != p))
760 if(audio_found)
762 if(is_video && (req_vpid == es.pid))
764 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
765 param->vpid = es.pid;
766 video_found = 1;
767 break;
771 if(video_found)
773 if(is_audio && (req_apid == es.pid))
775 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
776 param->apid = es.pid;
777 audio_found = 1;
778 break;
783 continue;
787 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "TYPE: %x, PID: %d, PROG FOUND: %d\n", es.type, es.pid, param->prog);
790 if(is_video)
792 if((req_vpid == -1) || (req_vpid == es.pid))
794 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
795 param->vpid = es.pid;
796 video_found = 1;
801 if(((req_vpid == -2) || (num_packets >= NUM_CONSECUTIVE_AUDIO_PACKETS)) && audio_found && !param->probe)
803 //novideo or we have at least 348 audio packets (64 KB) without video (TS with audio only)
804 param->vtype = 0;
805 break;
808 if(is_sub)
810 if((req_spid == -1) || (req_spid == es.pid))
812 param->stype = es.type;
813 param->spid = es.pid;
814 sub_found = 1;
818 if(is_audio)
820 if((req_apid == -1) || (req_apid == es.pid))
822 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
823 param->apid = es.pid;
824 audio_found = 1;
828 if(audio_found && (param->apid == es.pid) && (! video_found))
829 num_packets++;
831 if((has_tables==0) && (video_found && audio_found) && (pos >= 1000000))
832 break;
836 for(i=0; i<8192; i++)
838 if(pes_priv1[i].buf != NULL)
840 free(pes_priv1[i].buf);
841 pes_priv1[i].buf = NULL;
842 pes_priv1[i].pos = 0;
846 if(video_found)
848 if(param->vtype == VIDEO_MPEG1)
849 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG1(pid=%d) ", param->vpid);
850 else if(param->vtype == VIDEO_MPEG2)
851 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG2(pid=%d) ", param->vpid);
852 else if(param->vtype == VIDEO_MPEG4)
853 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG4(pid=%d) ", param->vpid);
854 else if(param->vtype == VIDEO_H264)
855 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO H264(pid=%d) ", param->vpid);
856 else if(param->vtype == VIDEO_VC1)
857 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO VC1(pid=%d) ", param->vpid);
858 else if(param->vtype == VIDEO_AVC)
859 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO AVC(NAL-H264, pid=%d) ", param->vpid);
861 else
863 param->vtype = UNKNOWN;
864 //WE DIDN'T MATCH ANY VIDEO STREAM
865 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO VIDEO! ");
868 if(param->atype == AUDIO_MP2)
869 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO MPA(pid=%d)", param->apid);
870 else if(param->atype == AUDIO_A52)
871 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO A52(pid=%d)", param->apid);
872 else if(param->atype == AUDIO_DTS)
873 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO DTS(pid=%d)", param->apid);
874 else if(param->atype == AUDIO_LPCM_BE)
875 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO LPCM(pid=%d)", param->apid);
876 else if(param->atype == AUDIO_AAC)
877 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO AAC(pid=%d)", param->apid);
878 else if(param->atype == AUDIO_TRUEHD)
879 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO TRUEHD(pid=%d)", param->apid);
880 else
882 audio_found = 0;
883 param->atype = UNKNOWN;
884 //WE DIDN'T MATCH ANY AUDIO STREAM, SO WE FORCE THE DEMUXER TO IGNORE AUDIO
885 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO AUDIO! ");
888 if(IS_SUB(param->stype))
889 mp_msg(MSGT_DEMUXER, MSGL_INFO, " SUB %s(pid=%d) ", (param->stype==SPU_DVD ? "DVD" : param->stype==SPU_DVB ? "DVB" : "Teletext"), param->spid);
890 else
892 param->stype = UNKNOWN;
893 mp_msg(MSGT_DEMUXER, MSGL_INFO, " NO SUBS (yet)! ");
896 if(video_found || audio_found)
898 if(!param->prog)
900 p = progid_for_pid(priv, video_found ? param->vpid : param->apid, 0);
901 if(p != -1)
902 param->prog = p;
905 if(demuxer->stream->eof && (ret == 0))
906 ret = init_pos;
907 mp_msg(MSGT_DEMUXER, MSGL_INFO, " PROGRAM N. %d\n", param->prog);
909 else
910 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\n");
913 for(i=0; i<8192; i++)
915 if(priv->ts.pids[i] != NULL)
917 priv->ts.pids[i]->payload_size = 0;
918 priv->ts.pids[i]->pts = priv->ts.pids[i]->last_pts = 0;
919 priv->ts.pids[i]->last_cc = -1;
920 priv->ts.pids[i]->is_synced = 0;
924 return ret;
927 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h)
929 int sps, sps_len;
930 unsigned char *ptr;
931 mp_mpeg_header_t picture;
932 if(len < 6)
933 return 0;
934 sps = buf[5] & 0x1f;
935 if(!sps)
936 return 0;
937 sps_len = (buf[6] << 8) | buf[7];
938 if(!sps_len || (sps_len > len - 8))
939 return 0;
940 ptr = &(buf[8]);
941 picture.display_picture_width = picture.display_picture_height = 0;
942 h264_parse_sps(&picture, ptr, len - 8);
943 if(!picture.display_picture_width || !picture.display_picture_height)
944 return 0;
945 *w = picture.display_picture_width;
946 *h = picture.display_picture_height;
947 return 1;
950 static demuxer_t *demux_open_ts(demuxer_t * demuxer)
952 int i;
953 uint8_t packet_size;
954 sh_video_t *sh_video;
955 sh_audio_t *sh_audio;
956 off_t start_pos;
957 tsdemux_init_t params;
958 ts_priv_t * priv = demuxer->priv;
960 mp_msg(MSGT_DEMUX, MSGL_V, "DEMUX OPEN, AUDIO_ID: %d, VIDEO_ID: %d, SUBTITLE_ID: %d,\n",
961 demuxer->audio->id, demuxer->video->id, demuxer->sub->id);
964 demuxer->type= DEMUXER_TYPE_MPEG_TS;
967 stream_reset(demuxer->stream);
969 packet_size = ts_check_file(demuxer);
970 if(!packet_size)
971 return NULL;
973 priv = calloc(1, sizeof(ts_priv_t));
974 if(priv == NULL)
976 mp_msg(MSGT_DEMUX, MSGL_FATAL, "DEMUX_OPEN_TS, couldn't allocate enough memory for ts->priv, exit\n");
977 return NULL;
980 for(i=0; i < 8192; i++)
982 priv->ts.pids[i] = NULL;
983 priv->ts.streams[i].id = -3;
985 priv->pat.progs = NULL;
986 priv->pat.progs_cnt = 0;
987 priv->pat.section.buffer = NULL;
988 priv->pat.section.buffer_len = 0;
990 priv->pmt = NULL;
991 priv->pmt_cnt = 0;
993 priv->keep_broken = ts_keep_broken;
994 priv->ts.packet_size = packet_size;
997 demuxer->priv = priv;
998 if(demuxer->stream->type != STREAMTYPE_FILE)
999 demuxer->seekable = 1;
1000 else
1001 demuxer->seekable = 1;
1004 params.atype = params.vtype = params.stype = UNKNOWN;
1005 params.apid = demuxer->audio->id;
1006 params.vpid = demuxer->video->id;
1007 params.spid = demuxer->sub->id;
1008 params.prog = ts_prog;
1009 params.probe = ts_probe;
1011 if(dvdsub_lang != NULL)
1013 strncpy(params.slang, dvdsub_lang, 3);
1014 params.slang[3] = 0;
1016 else
1017 memset(params.slang, 0, 4);
1019 if(audio_lang != NULL)
1021 strncpy(params.alang, audio_lang, 3);
1022 params.alang[3] = 0;
1024 else
1025 memset(params.alang, 0, 4);
1027 start_pos = ts_detect_streams(demuxer, &params);
1029 demuxer->sub->id = params.spid;
1030 priv->prog = params.prog;
1032 if(params.vtype != UNKNOWN)
1034 ts_add_stream(demuxer, priv->ts.pids[params.vpid]);
1035 sh_video = priv->ts.streams[params.vpid].sh;
1036 demuxer->video->id = priv->ts.streams[params.vpid].id;
1037 sh_video->ds = demuxer->video;
1038 sh_video->format = params.vtype;
1039 demuxer->video->sh = sh_video;
1042 if(params.atype != UNKNOWN)
1044 ES_stream_t *es = priv->ts.pids[params.apid];
1046 if(!IS_AUDIO(es->type) && !IS_AUDIO(es->subtype) && IS_AUDIO(params.atype)) es->subtype = params.atype;
1047 ts_add_stream(demuxer, priv->ts.pids[params.apid]);
1048 sh_audio = priv->ts.streams[params.apid].sh;
1049 demuxer->audio->id = priv->ts.streams[params.apid].id;
1050 sh_audio->ds = demuxer->audio;
1051 sh_audio->format = params.atype;
1052 demuxer->audio->sh = sh_audio;
1056 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);
1059 start_pos = (start_pos <= priv->ts.packet_size ? 0 : start_pos - priv->ts.packet_size);
1060 demuxer->movi_start = start_pos;
1061 demuxer->reference_clock = MP_NOPTS_VALUE;
1062 stream_reset(demuxer->stream);
1063 stream_seek(demuxer->stream, start_pos); //IF IT'S FROM A PIPE IT WILL FAIL, BUT WHO CARES?
1066 priv->last_pid = 8192; //invalid pid
1068 for(i = 0; i < 3; i++)
1070 priv->fifo[i].pack = NULL;
1071 priv->fifo[i].offset = 0;
1073 priv->fifo[0].ds = demuxer->audio;
1074 priv->fifo[1].ds = demuxer->video;
1075 priv->fifo[2].ds = demuxer->sub;
1077 priv->fifo[0].buffer_size = 1536;
1078 priv->fifo[1].buffer_size = 32767;
1079 priv->fifo[2].buffer_size = 32767;
1081 priv->pat.section.buffer_len = 0;
1082 for(i = 0; i < priv->pmt_cnt; i++)
1083 priv->pmt[i].section.buffer_len = 0;
1085 demuxer->filepos = stream_tell(demuxer->stream);
1086 return demuxer;
1089 static void demux_close_ts(demuxer_t * demuxer)
1091 uint16_t i;
1092 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
1094 if(priv)
1096 if(priv->pat.section.buffer)
1097 free(priv->pat.section.buffer);
1098 if(priv->pat.progs)
1099 free(priv->pat.progs);
1101 if(priv->pmt)
1103 for(i = 0; i < priv->pmt_cnt; i++)
1105 if(priv->pmt[i].section.buffer)
1106 free(priv->pmt[i].section.buffer);
1107 if(priv->pmt[i].es)
1108 free(priv->pmt[i].es);
1110 free(priv->pmt);
1112 free(priv);
1114 demuxer->priv=NULL;
1118 unsigned char mp_getbits(unsigned char*, unsigned int, unsigned char);
1119 #define getbits mp_getbits
1121 static int mp4_parse_sl_packet(pmt_t *pmt, uint8_t *buf, uint16_t packet_len, int pid, ES_stream_t *pes_es)
1123 int i, n, m, mp4_es_id = -1;
1124 uint64_t v = 0;
1125 uint32_t pl_size = 0;
1126 int deg_flag = 0;
1127 mp4_es_descr_t *es = NULL;
1128 mp4_sl_config_t *sl = NULL;
1129 uint8_t au_start = 0, au_end = 0, rap_flag = 0, ocr_flag = 0, padding = 0, padding_bits = 0, idle = 0;
1131 pes_es->is_synced = 0;
1132 mp_msg(MSGT_DEMUXER,MSGL_V, "mp4_parse_sl_packet, pid: %d, pmt: %pm, packet_len: %d\n", pid, pmt, packet_len);
1133 if(! pmt || !packet_len)
1134 return 0;
1136 for(i = 0; i < pmt->es_cnt; i++)
1138 if(pmt->es[i].pid == pid)
1139 mp4_es_id = pmt->es[i].mp4_es_id;
1141 if(mp4_es_id < 0)
1142 return -1;
1144 for(i = 0; i < pmt->mp4es_cnt; i++)
1146 if(pmt->mp4es[i].id == mp4_es_id)
1147 es = &(pmt->mp4es[i]);
1149 if(! es)
1150 return -1;
1152 pes_es->subtype = es->decoder.object_type;
1154 sl = &(es->sl);
1155 if(!sl)
1156 return -1;
1158 //now es is the complete es_descriptor of out mp4 ES stream
1159 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "ID: %d, FLAGS: 0x%x, subtype: %x\n", es->id, sl->flags, pes_es->subtype);
1161 n = 0;
1162 if(sl->au_start)
1163 pes_es->sl.au_start = au_start = getbits(buf, n++, 1);
1164 else
1165 pes_es->sl.au_start = (pes_es->sl.last_au_end ? 1 : 0);
1166 if(sl->au_end)
1167 pes_es->sl.au_end = au_end = getbits(buf, n++, 1);
1169 if(!sl->au_start && !sl->au_end)
1171 pes_es->sl.au_start = pes_es->sl.au_end = au_start = au_end = 1;
1173 pes_es->sl.last_au_end = pes_es->sl.au_end;
1176 if(sl->ocr_len > 0)
1177 ocr_flag = getbits(buf, n++, 1);
1178 if(sl->idle)
1179 idle = getbits(buf, n++, 1);
1180 if(sl->padding)
1181 padding = getbits(buf, n++, 1);
1182 if(padding)
1184 padding_bits = getbits(buf, n, 3);
1185 n += 3;
1188 if(idle || (padding && !padding_bits))
1190 pes_es->payload_size = 0;
1191 return -1;
1194 //(! idle && (!padding || padding_bits != 0)) is true
1195 n += sl->packet_seqnum_len;
1196 if(sl->degr_len)
1197 deg_flag = getbits(buf, n++, 1);
1198 if(deg_flag)
1199 n += sl->degr_len;
1201 if(ocr_flag)
1203 n += sl->ocr_len;
1204 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "OCR: %d bits\n", sl->ocr_len);
1207 if(packet_len * 8 <= n)
1208 return -1;
1210 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "\nAU_START: %d, AU_END: %d\n", au_start, au_end);
1211 if(au_start)
1213 int dts_flag = 0, cts_flag = 0, ib_flag = 0;
1215 if(sl->random_accesspoint)
1216 rap_flag = getbits(buf, n++, 1);
1218 //check commented because it seems it's rarely used, and we need this flag set in case of au_start
1219 //the decoder will eventually discard the payload if it can't decode it
1220 //if(rap_flag || sl->random_accesspoint_only)
1221 pes_es->is_synced = 1;
1223 n += sl->au_seqnum_len;
1224 if(packet_len * 8 <= n+8)
1225 return -1;
1226 if(sl->use_ts)
1228 dts_flag = getbits(buf, n++, 1);
1229 cts_flag = getbits(buf, n++, 1);
1231 if(sl->instant_bitrate_len)
1232 ib_flag = getbits(buf, n++, 1);
1233 if(packet_len * 8 <= n+8)
1234 return -1;
1235 if(dts_flag && (sl->ts_len > 0))
1237 n += sl->ts_len;
1238 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "DTS: %d bits\n", sl->ts_len);
1240 if(packet_len * 8 <= n+8)
1241 return -1;
1242 if(cts_flag && (sl->ts_len > 0))
1244 int i = 0, m;
1246 while(i < sl->ts_len)
1248 m = FFMIN(8, sl->ts_len - i);
1249 v |= getbits(buf, n, m);
1250 if(sl->ts_len - i > 8)
1251 v <<= 8;
1252 i += m;
1253 n += m;
1254 if(packet_len * 8 <= n+8)
1255 return -1;
1258 pes_es->pts = (float) v / (float) sl->ts_resolution;
1259 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "CTS: %d bits, value: %"PRIu64"/%d = %.3f\n", sl->ts_len, v, sl->ts_resolution, pes_es->pts);
1263 i = 0;
1264 pl_size = 0;
1265 while(i < sl->au_len)
1267 m = FFMIN(8, sl->au_len - i);
1268 pl_size |= getbits(buf, n, m);
1269 if(sl->au_len - i > 8)
1270 pl_size <<= 8;
1271 i += m;
1272 n += m;
1273 if(packet_len * 8 <= n+8)
1274 return -1;
1276 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "AU_LEN: %u (%d bits)\n", pl_size, sl->au_len);
1277 if(ib_flag)
1278 n += sl->instant_bitrate_len;
1281 m = (n+7)/8;
1282 if(0 < pl_size && pl_size < pes_es->payload_size)
1283 pes_es->payload_size = pl_size;
1285 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",
1286 n, m, pes_es->payload_size, pl_size, (int) rap_flag, (int) sl->random_accesspoint_only);
1288 return m;
1291 //this function parses the extension fields in the PES header and returns the substream_id, or -1 in case of errors
1292 static int parse_pes_extension_fields(unsigned char *p, int pkt_len)
1294 int skip;
1295 unsigned char flags;
1297 if(!(p[7] & 0x1)) //no extension_field
1298 return -1;
1299 skip = 9;
1300 if(p[7] & 0x80)
1302 skip += 5;
1303 if(p[7] & 0x40)
1304 skip += 5;
1306 if(p[7] & 0x20) //escr_flag
1307 skip += 6;
1308 if(p[7] & 0x10) //es_rate_flag
1309 skip += 3;
1310 if(p[7] & 0x08)//dsm_trick_mode is unsupported, skip
1312 skip = 0;//don't let's parse the extension fields
1314 if(p[7] & 0x04) //additional_copy_info
1315 skip += 1;
1316 if(p[7] & 0x02) //pes_crc_flag
1317 skip += 2;
1318 if(skip >= pkt_len) //too few bytes
1319 return -1;
1320 flags = p[skip];
1321 skip++;
1322 if(flags & 0x80) //pes_private_data_flag
1323 skip += 16;
1324 if(skip >= pkt_len)
1325 return -1;
1326 if(flags & 0x40) //pack_header_field_flag
1328 unsigned char l = p[skip];
1329 skip += l;
1331 if(flags & 0x20) //program_packet_sequence_counter
1332 skip += 2;
1333 if(flags & 0x10) //p_std
1334 skip += 2;
1335 if(skip >= pkt_len)
1336 return -1;
1337 if(flags & 0x01) //finally the long desired pes_extension2
1339 unsigned char l = p[skip]; //ext2 flag+len
1340 skip++;
1341 if((l == 0x81) && (skip < pkt_len))
1343 int ssid = p[skip];
1344 mp_msg(MSGT_IDENTIFY, MSGL_V, "SUBSTREAM_ID=%d (0x%02X)\n", ssid, ssid);
1345 return ssid;
1349 return -1;
1352 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)
1354 unsigned char *p;
1355 uint32_t header_len;
1356 int64_t pts;
1357 uint32_t stream_id;
1358 uint32_t pkt_len, pes_is_aligned;
1360 //Here we are always at the start of a PES packet
1361 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2(%p, %d): \n", buf, (uint32_t) packet_len);
1363 if(packet_len == 0 || packet_len > 184)
1365 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2, BUFFER LEN IS TOO SMALL OR TOO BIG: %d EXIT\n", packet_len);
1366 return 0;
1369 p = buf;
1370 pkt_len = packet_len;
1373 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: HEADER %02x %02x %02x %02x\n", p[0], p[1], p[2], p[3]);
1374 if (p[0] || p[1] || (p[2] != 1))
1376 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: error HEADER %02x %02x %02x (should be 0x000001) \n", p[0], p[1], p[2]);
1377 return 0 ;
1380 packet_len -= 6;
1381 if(packet_len==0)
1383 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: packet too short: %d, exit\n", packet_len);
1384 return 0;
1387 es->payload_size = (p[4] << 8 | p[5]);
1388 pes_is_aligned = (p[6] & 4);
1390 stream_id = p[3];
1393 if (p[7] & 0x80)
1394 { /* pts available */
1395 pts = (int64_t)(p[9] & 0x0E) << 29 ;
1396 pts |= p[10] << 22 ;
1397 pts |= (p[11] & 0xFE) << 14 ;
1398 pts |= p[12] << 7 ;
1399 pts |= (p[13] & 0xFE) >> 1 ;
1401 es->pts = pts / 90000.0f;
1403 else
1404 es->pts = 0.0f;
1407 header_len = p[8];
1410 if (header_len + 9 > pkt_len) //9 are the bytes read up to the header_length field
1412 mp_msg(MSGT_DEMUX, MSGL_DBG2, "demux_ts: illegal value for PES_header_data_length (0x%02x)\n", header_len);
1413 return 0;
1416 if(stream_id==0xfd)
1418 int ssid = parse_pes_extension_fields(p, pkt_len);
1419 if((audio_substream_id!=-1) && (ssid != audio_substream_id))
1420 return 0;
1421 if(ssid == 0x72)
1422 es->type = type_from_pmt = AUDIO_TRUEHD;
1425 p += header_len + 9;
1426 packet_len -= header_len + 3;
1428 if(es->payload_size)
1429 es->payload_size -= header_len + 3;
1432 es->is_synced = 1; //only for SL streams we have to make sure it's really true, see below
1433 if (stream_id == 0xbd)
1435 mp_msg(MSGT_DEMUX, MSGL_DBG3, "pes_parse2: audio buf = %02X %02X %02X %02X %02X %02X %02X %02X, 80: %d\n",
1436 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[0] & 0x80);
1440 * we check the descriptor tag first because some stations
1441 * do not include any of the A52 header info in their audio tracks
1442 * these "raw" streams may begin with a byte that looks like a stream type.
1447 (type_from_pmt == AUDIO_A52) || /* A52 - raw */
1448 (p[0] == 0x0B && p[1] == 0x77) /* A52 - syncword */
1451 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 RAW OR SYNCWORD\n");
1452 es->start = p;
1453 es->size = packet_len;
1454 es->type = AUDIO_A52;
1455 es->payload_size -= packet_len;
1457 return 1;
1459 /* SPU SUBS */
1460 else if(type_from_pmt == SPU_DVB ||
1461 ((p[0] == 0x20) && pes_is_aligned)) // && p[1] == 0x00))
1463 es->start = p;
1464 es->size = packet_len;
1465 es->type = SPU_DVB;
1466 es->payload_size -= packet_len;
1468 return 1;
1470 else if (pes_is_aligned && ((p[0] & 0xE0) == 0x20)) //SPU_DVD
1472 //DVD SUBS
1473 es->start = p+1;
1474 es->size = packet_len-1;
1475 es->type = SPU_DVD;
1476 es->payload_size -= packet_len;
1478 return 1;
1480 else if (pes_is_aligned && (p[0] & 0xF8) == 0x80)
1482 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 WITH HEADER\n");
1483 es->start = p+4;
1484 es->size = packet_len - 4;
1485 es->type = AUDIO_A52;
1486 es->payload_size -= packet_len;
1488 return 1;
1490 else if (pes_is_aligned && ((p[0]&0xf0) == 0xa0))
1492 int pcm_offset;
1494 for (pcm_offset=0; ++pcm_offset < packet_len-1 ; )
1496 if (p[pcm_offset] == 0x01 && p[pcm_offset+1] == 0x80)
1497 { /* START */
1498 pcm_offset += 2;
1499 break;
1503 es->start = p + pcm_offset;
1504 es->size = packet_len - pcm_offset;
1505 es->type = AUDIO_LPCM_BE;
1506 es->payload_size -= packet_len;
1508 return 1;
1510 else
1512 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PES_PRIVATE1\n");
1513 es->start = p;
1514 es->size = packet_len;
1515 es->type = (type_from_pmt == UNKNOWN ? PES_PRIVATE1 : type_from_pmt);
1516 es->payload_size -= packet_len;
1518 return 1;
1521 else if(((stream_id >= 0xe0) && (stream_id <= 0xef)) || (stream_id == 0xfd && type_from_pmt != UNKNOWN))
1523 es->start = p;
1524 es->size = packet_len;
1525 if(type_from_pmt != UNKNOWN)
1526 es->type = type_from_pmt;
1527 else
1528 es->type = VIDEO_MPEG2;
1529 if(es->payload_size)
1530 es->payload_size -= packet_len;
1532 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: M2V size %d\n", es->size);
1533 return 1;
1535 else if ((stream_id == 0xfa))
1537 int l;
1539 es->is_synced = 0;
1540 if(type_from_pmt != UNKNOWN) //MP4 A/V or SL
1542 es->start = p;
1543 es->size = packet_len;
1544 es->type = type_from_pmt;
1546 if(type_from_pmt == SL_PES_STREAM)
1548 //if(pes_is_aligned)
1550 l = mp4_parse_sl_packet(pmt, p, packet_len, pid, es);
1551 mp_msg(MSGT_DEMUX, MSGL_DBG2, "L=%d, TYPE=%x\n", l, type_from_pmt);
1552 if(l < 0)
1554 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: couldn't parse SL header, passing along full PES payload\n");
1555 l = 0;
1559 es->start += l;
1560 es->size -= l;
1563 if(es->payload_size)
1564 es->payload_size -= packet_len;
1565 return 1;
1568 else if ((stream_id & 0xe0) == 0xc0)
1570 es->start = p;
1571 es->size = packet_len;
1573 if(type_from_pmt != UNKNOWN)
1574 es->type = type_from_pmt;
1575 else
1576 es->type = AUDIO_MP2;
1578 es->payload_size -= packet_len;
1580 return 1;
1582 else if (type_from_pmt != -1) //as a last resort here we trust the PMT, if present
1584 es->start = p;
1585 es->size = packet_len;
1586 es->type = type_from_pmt;
1587 es->payload_size -= packet_len;
1589 return 1;
1591 else
1593 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: unknown packet, id: %x\n", stream_id);
1596 es->is_synced = 0;
1597 return 0;
1603 static int ts_sync(stream_t *stream)
1605 int c=0;
1607 mp_msg(MSGT_DEMUX, MSGL_DBG3, "TS_SYNC \n");
1609 while(((c=stream_read_char(stream)) != 0x47) && ! stream->eof);
1611 if(c == 0x47)
1612 return c;
1613 else
1614 return 0;
1618 static void ts_dump_streams(ts_priv_t *priv)
1620 int i;
1622 for(i = 0; i < 3; i++)
1624 if((priv->fifo[i].pack != NULL) && (priv->fifo[i].offset != 0))
1626 resize_demux_packet(priv->fifo[i].pack, priv->fifo[i].offset);
1627 ds_add_packet(priv->fifo[i].ds, priv->fifo[i].pack);
1628 priv->fifo[i].offset = 0;
1629 priv->fifo[i].pack = NULL;
1635 static inline int32_t prog_idx_in_pat(ts_priv_t *priv, uint16_t progid)
1637 int x;
1639 if(priv->pat.progs == NULL)
1640 return -1;
1642 for(x = 0; x < priv->pat.progs_cnt; x++)
1644 if(priv->pat.progs[x].id == progid)
1645 return x;
1648 return -1;
1652 static inline int32_t prog_id_in_pat(ts_priv_t *priv, uint16_t pid)
1654 int x;
1656 if(priv->pat.progs == NULL)
1657 return -1;
1659 for(x = 0; x < priv->pat.progs_cnt; x++)
1661 if(priv->pat.progs[x].pmt_pid == pid)
1662 return priv->pat.progs[x].id;
1665 return -1;
1668 static int collect_section(ts_section_t *section, int is_start, unsigned char *buff, int size)
1670 uint8_t *ptr;
1671 uint16_t tlen;
1672 int skip, tid;
1674 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, start: %d, size: %d, collected: %d\n", is_start, size, section->buffer_len);
1675 if(! is_start && !section->buffer_len)
1676 return 0;
1678 if(is_start)
1680 if(! section->buffer)
1682 section->buffer = (uint8_t*) malloc(4096+256);
1683 if(section->buffer == NULL)
1684 return 0;
1686 section->buffer_len = 0;
1689 if(size + section->buffer_len > 4096+256)
1691 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, excessive len: %d + %d\n", section->buffer_len, size);
1692 return 0;
1695 memcpy(&(section->buffer[section->buffer_len]), buff, size);
1696 section->buffer_len += size;
1698 if(section->buffer_len < 3)
1699 return 0;
1701 skip = section->buffer[0];
1702 if(skip + 4 > section->buffer_len)
1703 return 0;
1705 ptr = &(section->buffer[skip + 1]);
1706 tid = ptr[0];
1707 tlen = ((ptr[1] & 0x0f) << 8) | ptr[2];
1708 mp_msg(MSGT_DEMUX, MSGL_V, "SKIP: %d+1, TID: %d, TLEN: %d, COLLECTED: %d\n", skip, tid, tlen, section->buffer_len);
1709 if(section->buffer_len < (skip+1+3+tlen))
1711 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DATA IS NOT ENOUGH, NEXT TIME\n");
1712 return 0;
1715 return skip+1;
1718 static int parse_pat(ts_priv_t * priv, int is_start, unsigned char *buff, int size)
1720 int skip;
1721 unsigned char *ptr;
1722 unsigned char *base;
1723 int entries, i;
1724 uint16_t progid;
1725 struct pat_progs_t *tmp;
1726 ts_section_t *section;
1728 section = &(priv->pat.section);
1729 skip = collect_section(section, is_start, buff, size);
1730 if(! skip)
1731 return 0;
1733 ptr = &(section->buffer[skip]);
1734 //PARSING
1735 priv->pat.table_id = ptr[0];
1736 if(priv->pat.table_id != 0)
1737 return 0;
1738 priv->pat.ssi = (ptr[1] >> 7) & 0x1;
1739 priv->pat.curr_next = ptr[5] & 0x01;
1740 priv->pat.ts_id = (ptr[3] << 8 ) | ptr[4];
1741 priv->pat.version_number = (ptr[5] >> 1) & 0x1F;
1742 priv->pat.section_length = ((ptr[1] & 0x03) << 8 ) | ptr[2];
1743 priv->pat.section_number = ptr[6];
1744 priv->pat.last_section_number = ptr[7];
1746 //check_crc32(0xFFFFFFFFL, ptr, priv->pat.buffer_len - 4, &ptr[priv->pat.buffer_len - 4]);
1747 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);
1749 entries = (int) (priv->pat.section_length - 9) / 4; //entries per section
1751 for(i=0; i < entries; i++)
1753 int32_t idx;
1754 base = &ptr[8 + i*4];
1755 progid = (base[0] << 8) | base[1];
1757 if((idx = prog_idx_in_pat(priv, progid)) == -1)
1759 int sz = sizeof(struct pat_progs_t) * (priv->pat.progs_cnt+1);
1760 tmp = realloc_struct(priv->pat.progs, priv->pat.progs_cnt+1, sizeof(struct pat_progs_t));
1761 if(tmp == NULL)
1763 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PAT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
1764 break;
1766 priv->pat.progs = tmp;
1767 idx = priv->pat.progs_cnt;
1768 priv->pat.progs_cnt++;
1771 priv->pat.progs[idx].id = progid;
1772 priv->pat.progs[idx].pmt_pid = ((base[2] & 0x1F) << 8) | base[3];
1773 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);
1774 mp_msg(MSGT_IDENTIFY, MSGL_V, "PROGRAM_ID=%d (0x%02X), PMT_PID: %d(0x%02X)\n",
1775 progid, progid, priv->pat.progs[idx].pmt_pid, priv->pat.progs[idx].pmt_pid);
1778 return 1;
1782 static inline int32_t es_pid_in_pmt(pmt_t * pmt, uint16_t pid)
1784 uint16_t i;
1786 if(pmt == NULL)
1787 return -1;
1789 if(pmt->es == NULL)
1790 return -1;
1792 for(i = 0; i < pmt->es_cnt; i++)
1794 if(pmt->es[i].pid == pid)
1795 return (int32_t) i;
1798 return -1;
1802 static uint16_t get_mp4_desc_len(uint8_t *buf, int *len)
1804 //uint16_t i = 0, size = 0;
1805 int i = 0, j, size = 0;
1807 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PARSE_MP4_DESC_LEN(%d), bytes: ", *len);
1808 j = FFMIN(*len, 4);
1809 while(i < j)
1811 mp_msg(MSGT_DEMUX, MSGL_DBG2, " %x ", buf[i]);
1812 size |= (buf[i] & 0x7f);
1813 if(!(buf[i] & 0x80))
1814 break;
1815 size <<= 7;
1816 i++;
1818 mp_msg(MSGT_DEMUX, MSGL_DBG2, ", SIZE=%d\n", size);
1820 *len = i+1;
1821 return size;
1825 static uint16_t parse_mp4_slconfig_descriptor(uint8_t *buf, int len, void *elem)
1827 int i = 0;
1828 mp4_es_descr_t *es;
1829 mp4_sl_config_t *sl;
1831 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_SLCONFIG_DESCRIPTOR(%d)\n", len);
1832 es = (mp4_es_descr_t *) elem;
1833 if(!es)
1835 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1836 return len;
1838 sl = &(es->sl);
1840 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;
1841 sl->ocr = sl->dts = sl->cts = 0;
1843 if(buf[0] == 0)
1845 i++;
1846 sl->flags = buf[i];
1847 i++;
1848 sl->ts_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1849 i += 4;
1850 sl->ocr_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1851 i += 4;
1852 sl->ts_len = buf[i];
1853 i++;
1854 sl->ocr_len = buf[i];
1855 i++;
1856 sl->au_len = buf[i];
1857 i++;
1858 sl->instant_bitrate_len = buf[i];
1859 i++;
1860 sl->degr_len = (buf[i] >> 4) & 0x0f;
1861 sl->au_seqnum_len = ((buf[i] & 0x0f) << 1) | ((buf[i+1] >> 7) & 0x01);
1862 i++;
1863 sl->packet_seqnum_len = ((buf[i] >> 2) & 0x1f);
1864 i++;
1867 else if(buf[0] == 1)
1869 sl->flags = 0;
1870 sl->ts_resolution = 1000;
1871 sl->ts_len = 32;
1872 i++;
1874 else if(buf[0] == 2)
1876 sl->flags = 4;
1877 i++;
1879 else
1881 sl->flags = 0;
1882 i++;
1885 sl->au_start = (sl->flags >> 7) & 0x1;
1886 sl->au_end = (sl->flags >> 6) & 0x1;
1887 sl->random_accesspoint = (sl->flags >> 5) & 0x1;
1888 sl->random_accesspoint_only = (sl->flags >> 4) & 0x1;
1889 sl->padding = (sl->flags >> 3) & 0x1;
1890 sl->use_ts = (sl->flags >> 2) & 0x1;
1891 sl->idle = (sl->flags >> 1) & 0x1;
1892 sl->duration = sl->flags & 0x1;
1894 if(sl->duration)
1896 sl->timescale = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1897 i += 4;
1898 sl->au_duration = (buf[i] << 8) | buf[i+1];
1899 i += 2;
1900 sl->cts_duration = (buf[i] << 8) | buf[i+1];
1901 i += 2;
1903 else //no support for fixed durations atm
1904 sl->timescale = sl->au_duration = sl->cts_duration = 0;
1906 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",
1907 len, buf[0], sl->flags, sl->use_ts, sl->ts_len, sl->timescale, (uint64_t) sl->dts, (uint64_t) sl->cts);
1909 return len;
1912 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem);
1914 static uint16_t parse_mp4_decoder_config_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
1916 int i = 0, j;
1917 mp4_es_descr_t *es;
1918 mp4_decoder_config_t *dec;
1920 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_CONFIG_DESCRIPTOR(%d)\n", len);
1921 es = (mp4_es_descr_t *) elem;
1922 if(!es)
1924 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1925 return len;
1927 dec = (mp4_decoder_config_t*) &(es->decoder);
1929 dec->object_type = buf[i];
1930 dec->stream_type = (buf[i+1]>>2) & 0x3f;
1932 if(dec->object_type == 1 && dec->stream_type == 1)
1934 dec->object_type = MP4_OD;
1935 dec->stream_type = MP4_OD;
1937 else if(dec->stream_type == 4)
1939 if(dec->object_type == 0x6a)
1940 dec->object_type = VIDEO_MPEG1;
1941 if(dec->object_type >= 0x60 && dec->object_type <= 0x65)
1942 dec->object_type = VIDEO_MPEG2;
1943 else if(dec->object_type == 0x20)
1944 dec->object_type = VIDEO_MPEG4;
1945 else if(dec->object_type == 0x21)
1946 dec->object_type = VIDEO_AVC;
1947 /*else if(dec->object_type == 0x22)
1948 fprintf(stderr, "TYPE 0x22\n");*/
1949 else dec->object_type = UNKNOWN;
1951 else if(dec->stream_type == 5)
1953 if(dec->object_type == 0x40)
1954 dec->object_type = AUDIO_AAC;
1955 else if(dec->object_type == 0x6b)
1956 dec->object_type = AUDIO_MP2;
1957 else if(dec->object_type >= 0x66 && dec->object_type <= 0x69)
1958 dec->object_type = AUDIO_MP2;
1959 else
1960 dec->object_type = UNKNOWN;
1962 else
1963 dec->object_type = dec->stream_type = UNKNOWN;
1965 if(dec->object_type != UNKNOWN)
1967 //update the type of the current stream
1968 for(j = 0; j < pmt->es_cnt; j++)
1970 if(pmt->es[j].mp4_es_id == es->id)
1972 pmt->es[j].type = SL_PES_STREAM;
1977 if(len > 13)
1978 parse_mp4_descriptors(pmt, &buf[13], len-13, dec);
1980 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);
1982 return len;
1985 static uint16_t parse_mp4_decoder_specific_descriptor(uint8_t *buf, int len, void *elem)
1987 int i;
1988 mp4_decoder_config_t *dec;
1990 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_SPECIFIC_DESCRIPTOR(%d)\n", len);
1991 dec = (mp4_decoder_config_t *) elem;
1992 if(!dec)
1994 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1995 return len;
1998 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4 SPECIFIC INFO BYTES: \n");
1999 for(i=0; i<len; i++)
2000 mp_msg(MSGT_DEMUX, MSGL_DBG2, "%02x ", buf[i]);
2001 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\n");
2003 if(len > MAX_EXTRADATA_SIZE)
2005 mp_msg(MSGT_DEMUX, MSGL_ERR, "DEMUX_TS, EXTRADATA SUSPICIOUSLY BIG: %d, REFUSED\r\n", len);
2006 return len;
2008 memcpy(dec->buf, buf, len);
2009 dec->buf_size = len;
2011 return len;
2014 static uint16_t parse_mp4_es_descriptor(pmt_t *pmt, uint8_t *buf, int len)
2016 int i = 0, j = 0, k, found;
2017 uint8_t flag;
2018 mp4_es_descr_t es, *target_es = NULL, *tmp;
2020 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4ES: len=%d\n", len);
2021 memset(&es, 0, sizeof(mp4_es_descr_t));
2022 while(i < len)
2024 es.id = (buf[i] << 8) | buf[i+1];
2025 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_ID: %d\n", es.id);
2026 i += 2;
2027 flag = buf[i];
2028 i++;
2029 if(flag & 0x80)
2030 i += 2;
2031 if(flag & 0x40)
2032 i += buf[i]+1;
2033 if(flag & 0x20) //OCR, maybe we need it
2034 i += 2;
2036 j = parse_mp4_descriptors(pmt, &buf[i], len-i, &es);
2037 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);
2038 if(es.decoder.object_type != UNKNOWN && es.decoder.stream_type != UNKNOWN)
2040 found = 0;
2041 //search this ES_ID if we already have it
2042 for(k=0; k < pmt->mp4es_cnt; k++)
2044 if(pmt->mp4es[k].id == es.id)
2046 target_es = &(pmt->mp4es[k]);
2047 found = 1;
2051 if(! found)
2053 tmp = realloc_struct(pmt->mp4es, pmt->mp4es_cnt+1, sizeof(mp4_es_descr_t));
2054 if(tmp == NULL)
2056 fprintf(stderr, "CAN'T REALLOC MP4_ES_DESCR\n");
2057 continue;
2059 pmt->mp4es = tmp;
2060 target_es = &(pmt->mp4es[pmt->mp4es_cnt]);
2061 pmt->mp4es_cnt++;
2063 memcpy(target_es, &es, sizeof(mp4_es_descr_t));
2064 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_CNT: %d, ID=%d\n", pmt->mp4es_cnt, target_es->id);
2067 i += j;
2070 return len;
2073 static void parse_mp4_object_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2075 int i, j = 0, id;
2077 i=0;
2078 id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2079 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_OBJECT_DESCRIPTOR: len=%d, OD_ID=%d\n", len, id);
2080 if(buf[1] & 0x20)
2082 i += buf[2] + 1; //url
2083 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2085 else
2087 i = 2;
2089 while(i < len)
2091 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2092 mp_msg(MSGT_DEMUX, MSGL_V, "OBJD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2093 i += j;
2099 static void parse_mp4_iod(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2101 int i, j = 0;
2102 mp4_od_t *iod = &(pmt->iod);
2104 iod->id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2105 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_IOD: len=%d, IOD_ID=%d\n", len, iod->id);
2106 i = 2;
2107 if(buf[1] & 0x20)
2109 i += buf[2] + 1; //url
2110 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2112 else
2114 i = 7;
2115 while(i < len)
2117 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2118 mp_msg(MSGT_DEMUX, MSGL_V, "IOD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2119 i += j;
2124 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2126 int tag, descr_len, i = 0, j = 0;
2128 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DESCRIPTORS, len=%d\n", len);
2129 if(! len)
2130 return len;
2132 while(i < len)
2134 tag = buf[i];
2135 j = len - i -1;
2136 descr_len = get_mp4_desc_len(&(buf[i+1]), &j);
2137 mp_msg(MSGT_DEMUX, MSGL_V, "TAG=%d (0x%x), DESCR_len=%d, len=%d, j=%d\n", tag, tag, descr_len, len, j);
2138 if(descr_len > len - j+1)
2140 mp_msg(MSGT_DEMUX, MSGL_V, "descriptor is too long, exit\n");
2141 return len;
2143 i += j+1;
2145 switch(tag)
2147 case 0x1:
2148 parse_mp4_object_descriptor(pmt, &(buf[i]), descr_len, elem);
2149 break;
2150 case 0x2:
2151 parse_mp4_iod(pmt, &(buf[i]), descr_len, elem);
2152 break;
2153 case 0x3:
2154 parse_mp4_es_descriptor(pmt, &(buf[i]), descr_len);
2155 break;
2156 case 0x4:
2157 parse_mp4_decoder_config_descriptor(pmt, &buf[i], descr_len, elem);
2158 break;
2159 case 0x05:
2160 parse_mp4_decoder_specific_descriptor(&buf[i], descr_len, elem);
2161 break;
2162 case 0x6:
2163 parse_mp4_slconfig_descriptor(&buf[i], descr_len, elem);
2164 break;
2165 default:
2166 mp_msg(MSGT_DEMUX, MSGL_V, "Unsupported mp4 descriptor 0x%x\n", tag);
2168 i += descr_len;
2171 return len;
2174 static ES_stream_t *new_pid(ts_priv_t *priv, int pid)
2176 ES_stream_t *tss;
2178 tss = malloc(sizeof(ES_stream_t));
2179 if(! tss)
2180 return NULL;
2181 memset(tss, 0, sizeof(ES_stream_t));
2182 tss->pid = pid;
2183 tss->last_cc = -1;
2184 tss->type = UNKNOWN;
2185 tss->subtype = UNKNOWN;
2186 tss->is_synced = 0;
2187 tss->extradata = NULL;
2188 tss->extradata_alloc = tss->extradata_len = 0;
2189 priv->ts.pids[pid] = tss;
2191 return tss;
2195 static int parse_program_descriptors(pmt_t *pmt, uint8_t *buf, uint16_t len)
2197 uint16_t i = 0, k, olen = len;
2199 while(len > 0)
2201 mp_msg(MSGT_DEMUX, MSGL_V, "PROG DESCR, TAG=%x, LEN=%d(%x)\n", buf[i], buf[i+1], buf[i+1]);
2202 if(buf[i+1] > len-2)
2204 mp_msg(MSGT_DEMUX, MSGL_V, "ERROR, descriptor len is too long, skipping\n");
2205 return olen;
2208 if(buf[i] == 0x1d)
2210 if(buf[i+3] == 2) //buggy versions of vlc muxer make this non-standard mess (missing iod_scope)
2211 k = 3;
2212 else
2213 k = 4; //this is standard compliant
2214 parse_mp4_descriptors(pmt, &buf[i+k], (int) buf[i+1]-(k-2), NULL);
2217 len -= 2 + buf[i+1];
2220 return olen;
2223 static int parse_descriptors(struct pmt_es_t *es, uint8_t *ptr)
2225 int j, descr_len, len;
2227 j = 0;
2228 len = es->descr_length;
2229 while(len > 2)
2231 descr_len = ptr[j+1];
2232 mp_msg(MSGT_DEMUX, MSGL_V, "...descr id: 0x%x, len=%d\n", ptr[j], descr_len);
2233 if(descr_len > len)
2235 mp_msg(MSGT_DEMUX, MSGL_ERR, "INVALID DESCR LEN for tag %02x: %d vs %d max, EXIT LOOP\n", ptr[j], descr_len, len);
2236 return -1;
2240 if(ptr[j] == 0x6a || ptr[j] == 0x7a) //A52 Descriptor
2242 if(es->type == 0x6)
2244 es->type = AUDIO_A52;
2245 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB A52 Descriptor\n");
2248 else if(ptr[j] == 0x7b) //DVB DTS Descriptor
2250 if(es->type == 0x6)
2252 es->type = AUDIO_DTS;
2253 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB DTS Descriptor\n");
2256 else if(ptr[j] == 0x56) // Teletext
2258 if(descr_len >= 5) {
2259 memcpy(es->lang, ptr+2, 3);
2260 es->lang[3] = 0;
2262 es->type = SPU_TELETEXT;
2264 else if(ptr[j] == 0x59) //Subtitling Descriptor
2266 uint8_t subtype;
2268 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Subtitling Descriptor\n");
2269 if(descr_len < 8)
2271 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Descriptor length too short for DVB Subtitle Descriptor: %d, SKIPPING\n", descr_len);
2273 else
2275 memcpy(es->lang, &ptr[j+2], 3);
2276 es->lang[3] = 0;
2277 subtype = ptr[j+5];
2279 (subtype >= 0x10 && subtype <= 0x13) ||
2280 (subtype >= 0x20 && subtype <= 0x23)
2283 es->type = SPU_DVB;
2284 //page parameters: compo page 2 bytes, ancillary page 2 bytes
2286 else
2287 es->type = UNKNOWN;
2290 else if(ptr[j] == 0x50) //Component Descriptor
2292 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Component Descriptor\n");
2293 memcpy(es->lang, &ptr[j+5], 3);
2294 es->lang[3] = 0;
2296 else if(ptr[j] == 0xa) //Language Descriptor
2298 memcpy(es->lang, &ptr[j+2], 3);
2299 es->lang[3] = 0;
2300 mp_msg(MSGT_DEMUX, MSGL_V, "Language Descriptor: %s\n", es->lang);
2302 else if(ptr[j] == 0x5) //Registration Descriptor (looks like e fourCC :) )
2304 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor\n");
2305 if(descr_len < 4)
2307 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor length too short: %d, SKIPPING\n", descr_len);
2309 else
2311 char *d;
2312 memcpy(es->format_descriptor, &ptr[j+2], 4);
2313 es->format_descriptor[4] = 0;
2315 d = &ptr[j+2];
2316 if(d[0] == 'A' && d[1] == 'C' && d[2] == '-' && d[3] == '3')
2318 es->type = AUDIO_A52;
2320 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '1')
2322 es->type = AUDIO_DTS;
2324 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '2')
2326 es->type = AUDIO_DTS;
2328 else if(d[0] == 'V' && d[1] == 'C' && d[2] == '-' && d[3] == '1')
2330 es->type = VIDEO_VC1;
2332 else
2333 es->type = UNKNOWN;
2334 mp_msg(MSGT_DEMUX, MSGL_DBG2, "FORMAT %s\n", es->format_descriptor);
2337 else if(ptr[j] == 0x1e)
2339 es->mp4_es_id = (ptr[j+2] << 8) | ptr[j+3];
2340 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);
2342 else
2343 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Unknown descriptor 0x%x, SKIPPING\n", ptr[j]);
2345 len -= 2 + descr_len;
2346 j += 2 + descr_len;
2349 return 1;
2352 static int parse_sl_section(pmt_t *pmt, ts_section_t *section, int is_start, unsigned char *buff, int size)
2354 int tid, len, skip;
2355 uint8_t *ptr;
2356 skip = collect_section(section, is_start, buff, size);
2357 if(! skip)
2358 return 0;
2360 ptr = &(section->buffer[skip]);
2361 tid = ptr[0];
2362 len = ((ptr[1] & 0x0f) << 8) | ptr[2];
2363 mp_msg(MSGT_DEMUX, MSGL_V, "TABLEID: %d (av. %d), skip=%d, LEN: %d\n", tid, section->buffer_len, skip, len);
2364 if(len > 4093 || section->buffer_len < len || tid != 5)
2366 mp_msg(MSGT_DEMUX, MSGL_V, "SECTION TOO LARGE or wrong section type, EXIT\n");
2367 return 0;
2370 if(! (ptr[5] & 1))
2371 return 0;
2373 //8 is the current position, len - 9 is the amount of data available
2374 parse_mp4_descriptors(pmt, &ptr[8], len - 9, NULL);
2376 return 1;
2379 static int parse_pmt(ts_priv_t * priv, uint16_t progid, uint16_t pid, int is_start, unsigned char *buff, int size)
2381 unsigned char *base, *es_base;
2382 pmt_t *pmt;
2383 int32_t idx, es_count, section_bytes;
2384 uint8_t m=0;
2385 int skip;
2386 pmt_t *tmp;
2387 struct pmt_es_t *tmp_es;
2388 ts_section_t *section;
2389 ES_stream_t *tss;
2391 idx = progid_idx_in_pmt(priv, progid);
2393 if(idx == -1)
2395 int sz = (priv->pmt_cnt + 1) * sizeof(pmt_t);
2396 tmp = realloc_struct(priv->pmt, priv->pmt_cnt + 1, sizeof(pmt_t));
2397 if(tmp == NULL)
2399 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
2400 return 0;
2402 priv->pmt = tmp;
2403 idx = priv->pmt_cnt;
2404 memset(&(priv->pmt[idx]), 0, sizeof(pmt_t));
2405 priv->pmt_cnt++;
2406 priv->pmt[idx].progid = progid;
2409 pmt = &(priv->pmt[idx]);
2411 section = &(pmt->section);
2412 skip = collect_section(section, is_start, buff, size);
2413 if(! skip)
2414 return 0;
2416 base = &(section->buffer[skip]);
2418 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",
2419 progid, pmt->section.buffer_len, is_start, pid, size, m, pmt->es_cnt, idx, pmt);
2421 pmt->table_id = base[0];
2422 if(pmt->table_id != 2)
2423 return -1;
2424 pmt->ssi = base[1] & 0x80;
2425 pmt->section_length = (((base[1] & 0xf) << 8 ) | base[2]);
2426 pmt->version_number = (base[5] >> 1) & 0x1f;
2427 pmt->curr_next = (base[5] & 1);
2428 pmt->section_number = base[6];
2429 pmt->last_section_number = base[7];
2430 pmt->PCR_PID = ((base[8] & 0x1f) << 8 ) | base[9];
2431 pmt->prog_descr_length = ((base[10] & 0xf) << 8 ) | base[11];
2432 if(pmt->prog_descr_length > pmt->section_length - 9)
2434 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, INVALID PROG_DESCR LENGTH (%d vs %d)\n", pmt->prog_descr_length, pmt->section_length - 9);
2435 return -1;
2438 if(pmt->prog_descr_length)
2439 parse_program_descriptors(pmt, &base[12], pmt->prog_descr_length);
2441 es_base = &base[12 + pmt->prog_descr_length]; //the beginning of th ES loop
2443 section_bytes= pmt->section_length - 13 - pmt->prog_descr_length;
2444 es_count = 0;
2446 while(section_bytes >= 5)
2448 int es_pid, es_type;
2450 es_type = es_base[0];
2451 es_pid = ((es_base[1] & 0x1f) << 8) | es_base[2];
2453 idx = es_pid_in_pmt(pmt, es_pid);
2454 if(idx == -1)
2456 int sz = sizeof(struct pmt_es_t) * (pmt->es_cnt + 1);
2457 tmp_es = realloc_struct(pmt->es, pmt->es_cnt + 1, sizeof(struct pmt_es_t));
2458 if(tmp_es == NULL)
2460 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT, COULDN'T ALLOCATE %d bytes for PMT_ES\n", sz);
2461 continue;
2463 pmt->es = tmp_es;
2464 idx = pmt->es_cnt;
2465 memset(&(pmt->es[idx]), 0, sizeof(struct pmt_es_t));
2466 pmt->es_cnt++;
2469 pmt->es[idx].descr_length = ((es_base[3] & 0xf) << 8) | es_base[4];
2472 if(pmt->es[idx].descr_length > section_bytes - 5)
2474 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, ES_DESCR_LENGTH TOO LARGE %d > %d, EXIT\n",
2475 pmt->es[idx].descr_length, section_bytes - 5);
2476 return -1;
2480 pmt->es[idx].pid = es_pid;
2481 if(es_type != 0x6)
2482 pmt->es[idx].type = UNKNOWN;
2483 else
2484 pmt->es[idx].type = es_type;
2486 parse_descriptors(&pmt->es[idx], &es_base[5]);
2488 switch(es_type)
2490 case 1:
2491 pmt->es[idx].type = VIDEO_MPEG1;
2492 break;
2493 case 2:
2494 pmt->es[idx].type = VIDEO_MPEG2;
2495 break;
2496 case 3:
2497 case 4:
2498 pmt->es[idx].type = AUDIO_MP2;
2499 break;
2500 case 6:
2501 if(pmt->es[idx].type == 0x6) //this could have been ovrwritten by parse_descriptors
2502 pmt->es[idx].type = UNKNOWN;
2503 break;
2504 case 0x10:
2505 pmt->es[idx].type = VIDEO_MPEG4;
2506 break;
2507 case 0x0f:
2508 case 0x11:
2509 pmt->es[idx].type = AUDIO_AAC;
2510 break;
2511 case 0x1b:
2512 pmt->es[idx].type = VIDEO_H264;
2513 break;
2514 case 0x12:
2515 pmt->es[idx].type = SL_PES_STREAM;
2516 break;
2517 case 0x13:
2518 pmt->es[idx].type = SL_SECTION;
2519 break;
2520 case 0x81:
2521 pmt->es[idx].type = AUDIO_A52;
2522 break;
2523 case 0x8A:
2524 case 0x82:
2525 case 0x85:
2526 case 0x86:
2527 pmt->es[idx].type = AUDIO_DTS;
2528 break;
2529 case 0xEA:
2530 pmt->es[idx].type = VIDEO_VC1;
2531 break;
2532 default:
2533 mp_msg(MSGT_DEMUX, MSGL_DBG2, "UNKNOWN ES TYPE=0x%x\n", es_type);
2534 pmt->es[idx].type = UNKNOWN;
2537 tss = priv->ts.pids[es_pid]; //an ES stream
2538 if(tss == NULL)
2540 tss = new_pid(priv, es_pid);
2541 if(tss)
2542 tss->type = pmt->es[idx].type;
2545 section_bytes -= 5 + pmt->es[idx].descr_length;
2546 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",
2547 progid, idx, es_count, pmt->es[idx].pid, pmt->es[idx].pid, pmt->es[idx].type, pmt->es[idx].descr_length, section_bytes);
2550 es_base += 5 + pmt->es[idx].descr_length;
2552 es_count++;
2555 mp_msg(MSGT_DEMUX, MSGL_V, "----------------------------\n");
2556 return 1;
2559 static pmt_t* pmt_of_pid(ts_priv_t *priv, int pid, mp4_decoder_config_t **mp4_dec)
2561 int32_t i, j, k;
2563 if(priv->pmt)
2565 for(i = 0; i < priv->pmt_cnt; i++)
2567 if(priv->pmt[i].es && priv->pmt[i].es_cnt)
2569 for(j = 0; j < priv->pmt[i].es_cnt; j++)
2571 if(priv->pmt[i].es[j].pid == pid)
2573 //search mp4_es_id
2574 if(priv->pmt[i].es[j].mp4_es_id)
2576 for(k = 0; k < priv->pmt[i].mp4es_cnt; k++)
2578 if(priv->pmt[i].mp4es[k].id == priv->pmt[i].es[j].mp4_es_id)
2580 *mp4_dec = &(priv->pmt[i].mp4es[k].decoder);
2581 break;
2586 return &(priv->pmt[i]);
2593 return NULL;
2597 static inline int32_t pid_type_from_pmt(ts_priv_t *priv, int pid)
2599 int32_t pmt_idx, pid_idx, i, j;
2601 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2603 if(pmt_idx != -1)
2605 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2606 if(pid_idx != -1)
2607 return priv->pmt[pmt_idx].es[pid_idx].type;
2609 //else
2611 for(i = 0; i < priv->pmt_cnt; i++)
2613 pmt_t *pmt = &(priv->pmt[i]);
2614 for(j = 0; j < pmt->es_cnt; j++)
2615 if(pmt->es[j].pid == pid)
2616 return pmt->es[j].type;
2620 return UNKNOWN;
2624 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid)
2626 int32_t pmt_idx, pid_idx, i, j;
2628 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2630 if(pmt_idx != -1)
2632 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2633 if(pid_idx != -1)
2634 return priv->pmt[pmt_idx].es[pid_idx].lang;
2636 else
2638 for(i = 0; i < priv->pmt_cnt; i++)
2640 pmt_t *pmt = &(priv->pmt[i]);
2641 for(j = 0; j < pmt->es_cnt; j++)
2642 if(pmt->es[j].pid == pid)
2643 return pmt->es[j].lang;
2647 return NULL;
2651 static int fill_packet(demuxer_t *demuxer, demux_stream_t *ds, demux_packet_t **dp, int *dp_offset, TS_stream_info *si)
2653 int ret = 0;
2655 if((*dp != NULL) && (*dp_offset > 0))
2657 ret = *dp_offset;
2658 resize_demux_packet(*dp, ret); //shrinked to the right size
2659 ds_add_packet(ds, *dp);
2660 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);
2661 if(si)
2663 float diff = (*dp)->pts - si->last_pts;
2664 float dur;
2666 if(abs(diff) > 1) //1 second, there's a discontinuity
2668 si->duration += si->last_pts - si->first_pts;
2669 si->first_pts = si->last_pts = (*dp)->pts;
2671 else
2673 si->last_pts = (*dp)->pts;
2675 si->size += ret;
2676 dur = si->duration + (si->last_pts - si->first_pts);
2678 if(dur > 0 && ds == demuxer->video)
2680 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2681 if(dur > 1) //otherwise it may be unreliable
2682 priv->vbitrate = (uint32_t) ((float) si->size / dur);
2687 *dp = NULL;
2688 *dp_offset = 0;
2690 return ret;
2693 static int fill_extradata(mp4_decoder_config_t * mp4_dec, ES_stream_t *tss)
2695 uint8_t *tmp;
2697 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4_dec: %p, pid: %d\n", mp4_dec, tss->pid);
2699 if(mp4_dec->buf_size > tss->extradata_alloc)
2701 tmp = (uint8_t *) realloc(tss->extradata, mp4_dec->buf_size);
2702 if(!tmp)
2703 return 0;
2704 tss->extradata = tmp;
2705 tss->extradata_alloc = mp4_dec->buf_size;
2707 memcpy(tss->extradata, mp4_dec->buf, mp4_dec->buf_size);
2708 tss->extradata_len = mp4_dec->buf_size;
2709 mp_msg(MSGT_DEMUX, MSGL_V, "EXTRADATA: %p, alloc=%d, len=%d\n", tss->extradata, tss->extradata_alloc, tss->extradata_len);
2711 return tss->extradata_len;
2714 // 0 = EOF or no stream found
2715 // else = [-] number of bytes written to the packet
2716 static int ts_parse(demuxer_t *demuxer , ES_stream_t *es, unsigned char *packet, int probe)
2718 ES_stream_t *tss;
2719 uint8_t done = 0;
2720 int buf_size, is_start, pid, base;
2721 int len, cc, cc_ok, afc, retv = 0, is_video, is_audio, is_sub;
2722 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2723 stream_t *stream = demuxer->stream;
2724 char *p;
2725 demux_stream_t *ds = NULL;
2726 demux_packet_t **dp = NULL;
2727 int *dp_offset = 0, *buffer_size = 0;
2728 int32_t progid, pid_type, bad, ts_error;
2729 int junk = 0, rap_flag = 0;
2730 pmt_t *pmt;
2731 mp4_decoder_config_t *mp4_dec;
2732 TS_stream_info *si;
2735 while(! done)
2737 bad = ts_error = 0;
2738 ds = (demux_stream_t*) NULL;
2739 dp = (demux_packet_t **) NULL;
2740 dp_offset = buffer_size = NULL;
2741 rap_flag = 0;
2742 mp4_dec = NULL;
2743 es->is_synced = 0;
2744 es->lang[0] = 0;
2745 si = NULL;
2747 junk = priv->ts.packet_size - TS_PACKET_SIZE;
2748 buf_size = priv->ts.packet_size - junk;
2750 if(stream_eof(stream))
2752 if(! probe)
2754 ts_dump_streams(priv);
2755 demuxer->filepos = stream_tell(demuxer->stream);
2758 return 0;
2762 if(! ts_sync(stream))
2764 mp_msg(MSGT_DEMUX, MSGL_INFO, "TS_PARSE: COULDN'T SYNC\n");
2765 return 0;
2768 len = stream_read(stream, &packet[1], 3);
2769 if (len != 3)
2770 return 0;
2771 buf_size -= 4;
2773 if((packet[1] >> 7) & 0x01) //transport error
2774 ts_error = 1;
2777 is_start = packet[1] & 0x40;
2778 pid = ((packet[1] & 0x1f) << 8) | packet[2];
2780 tss = priv->ts.pids[pid]; //an ES stream
2781 if(tss == NULL)
2783 tss = new_pid(priv, pid);
2784 if(tss == NULL)
2785 continue;
2788 cc = (packet[3] & 0xf);
2789 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
2790 tss->last_cc = cc;
2792 bad = ts_error; // || (! cc_ok);
2793 if(bad)
2795 if(priv->keep_broken == 0)
2797 stream_skip(stream, buf_size-1+junk);
2798 continue;
2801 is_start = 0; //queued to the packet data
2804 if(is_start)
2805 tss->is_synced = 1;
2807 if((!is_start && !tss->is_synced) || ((pid > 1) && (pid < 16)) || (pid == 8191)) //invalid pid
2809 stream_skip(stream, buf_size-1+junk);
2810 continue;
2814 afc = (packet[3] >> 4) & 3;
2815 if(! (afc % 2)) //no payload in this TS packet
2817 stream_skip(stream, buf_size-1+junk);
2818 continue;
2821 if(afc > 1)
2823 int c;
2824 c = stream_read_char(stream);
2825 buf_size--;
2826 if(c < 0 || c > 183) //broken from the stream layer or invalid
2828 stream_skip(stream, buf_size-1+junk);
2829 continue;
2832 //c==0 is allowed!
2833 if(c > 0)
2835 uint8_t pcrbuf[188];
2836 int flags = stream_read_char(stream);
2837 int has_pcr;
2838 rap_flag = (flags & 0x40) >> 6;
2839 has_pcr = flags & 0x10;
2841 buf_size--;
2842 c--;
2843 stream_read(stream, pcrbuf, c);
2845 if(has_pcr)
2847 int pcr_pid = prog_pcr_pid(priv, priv->prog);
2848 if(pcr_pid == pid)
2850 uint64_t pcr, pcr_ext;
2852 pcr = (int64_t)(pcrbuf[0]) << 25;
2853 pcr |= pcrbuf[1] << 17 ;
2854 pcr |= (pcrbuf[2]) << 9;
2855 pcr |= pcrbuf[3] << 1 ;
2856 pcr |= (pcrbuf[4] & 0x80) >> 7;
2858 pcr_ext = (pcrbuf[4] & 0x01) << 8;
2859 pcr_ext |= pcrbuf[5];
2861 pcr = pcr * 300 + pcr_ext;
2863 demuxer->reference_clock = (double)pcr/(double)27000000.0;
2867 buf_size -= c;
2868 if(buf_size == 0)
2869 continue;
2873 //find the program that the pid belongs to; if (it's the right one or -1) && pid_type==SL_SECTION
2874 //call parse_sl_section()
2875 pmt = pmt_of_pid(priv, pid, &mp4_dec);
2876 if(mp4_dec)
2878 fill_extradata(mp4_dec, tss);
2879 if(IS_VIDEO(mp4_dec->object_type) || IS_AUDIO(mp4_dec->object_type))
2881 tss->type = SL_PES_STREAM;
2882 tss->subtype = mp4_dec->object_type;
2887 //TABLE PARSING
2889 base = priv->ts.packet_size - buf_size;
2891 priv->last_pid = pid;
2893 is_video = IS_VIDEO(tss->type) || (tss->type==SL_PES_STREAM && IS_VIDEO(tss->subtype));
2894 is_audio = IS_AUDIO(tss->type) || (tss->type==SL_PES_STREAM && IS_AUDIO(tss->subtype)) || (tss->type == PES_PRIVATE1);
2895 is_sub = IS_SUB(tss->type);
2896 pid_type = pid_type_from_pmt(priv, pid);
2898 // PES CONTENT STARTS HERE
2899 if(! probe)
2901 if((is_video || is_audio) && is_start && !priv->ts.streams[pid].sh)
2902 ts_add_stream(demuxer, tss);
2904 if((pid == demuxer->sub->id)) //or the lang is right
2906 pid_type = SPU_DVD;
2909 if(is_video && (demuxer->video->id == priv->ts.streams[pid].id))
2911 ds = demuxer->video;
2913 dp = &priv->fifo[1].pack;
2914 dp_offset = &priv->fifo[1].offset;
2915 buffer_size = &priv->fifo[1].buffer_size;
2916 si = &priv->vstr;
2918 else if(is_audio && (demuxer->audio->id == priv->ts.streams[pid].id))
2920 ds = demuxer->audio;
2922 dp = &priv->fifo[0].pack;
2923 dp_offset = &priv->fifo[0].offset;
2924 buffer_size = &priv->fifo[0].buffer_size;
2925 si = &priv->astr;
2927 else if(is_sub
2928 || IS_SUB(pid_type))
2930 //SUBS are infrequent, so the initial detection may fail
2931 // and we may need to add them at play-time
2932 if(demuxer->sub->id == -1)
2934 uint16_t p;
2935 p = progid_for_pid(priv, tss->pid, priv->prog);
2937 if(p == priv->prog)
2939 int asgn = 0;
2940 uint8_t *lang;
2942 if(dvdsub_lang)
2944 if ((lang = pid_lang_from_pmt(priv, pid)))
2945 asgn = (strncmp(lang, dvdsub_lang, 3) == 0);
2947 else //no language specified with -slang
2948 asgn = 1;
2950 if(asgn)
2952 demuxer->sub->id = tss->pid;
2953 mp_msg(MSGT_DEMUX, MSGL_INFO, "CHOSEN SUBs pid 0x%x (%d) FROM PROG %d\n", tss->pid, tss->pid, priv->prog);
2958 if(demuxer->sub->id == tss->pid)
2960 ds = demuxer->sub;
2962 dp = &priv->fifo[2].pack;
2963 dp_offset = &priv->fifo[2].offset;
2964 buffer_size = &priv->fifo[2].buffer_size;
2966 else
2968 stream_skip(stream, buf_size+junk);
2969 continue;
2973 //IS IT TIME TO QUEUE DATA to the dp_packet?
2974 if(is_start && (dp != NULL))
2976 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
2980 if(dp && *dp == NULL)
2982 if(*buffer_size > MAX_PACK_BYTES)
2983 *buffer_size = MAX_PACK_BYTES;
2984 *dp = new_demux_packet(*buffer_size); //es->size
2985 *dp_offset = 0;
2986 if(! *dp)
2988 fprintf(stderr, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", *buffer_size);
2989 continue;
2991 mp_msg(MSGT_DEMUX, MSGL_DBG2, "CREATED DP(%d)\n", *buffer_size);
2996 if(probe || !dp) //dp is NULL for tables and sections
2998 p = &packet[base];
3000 else //feeding
3002 if(*dp_offset + buf_size > *buffer_size)
3004 *buffer_size = *dp_offset + buf_size + TS_FEC_PACKET_SIZE;
3005 resize_demux_packet(*dp, *buffer_size);
3007 p = &((*dp)->buffer[*dp_offset]);
3010 len = stream_read(stream, p, buf_size);
3011 if(len < buf_size)
3013 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\r\nts_parse() couldn't read enough data: %d < %d\r\n", len, buf_size);
3014 continue;
3016 stream_skip(stream, junk);
3018 if(pid == 0)
3020 parse_pat(priv, is_start, p, buf_size);
3021 continue;
3023 else if((tss->type == SL_SECTION) && pmt)
3025 int k, mp4_es_id = -1;
3026 ts_section_t *section;
3027 for(k = 0; k < pmt->mp4es_cnt; k++)
3029 if(pmt->mp4es[k].decoder.object_type == MP4_OD && pmt->mp4es[k].decoder.stream_type == MP4_OD)
3030 mp4_es_id = pmt->mp4es[k].id;
3032 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4ESID: %d\n", mp4_es_id);
3033 for(k = 0; k < pmt->es_cnt; k++)
3035 if(pmt->es[k].mp4_es_id == mp4_es_id)
3037 section = &(tss->section);
3038 parse_sl_section(pmt, section, is_start, &packet[base], buf_size);
3041 continue;
3043 else
3045 progid = prog_id_in_pat(priv, pid);
3046 if(progid != -1)
3048 if(pid != demuxer->video->id && pid != demuxer->audio->id && pid != demuxer->sub->id)
3050 parse_pmt(priv, progid, pid, is_start, &packet[base], buf_size);
3051 continue;
3053 else
3054 mp_msg(MSGT_DEMUX, MSGL_ERR, "Argh! Data pid %d used in the PMT, Skipping PMT parsing!\n", pid);
3058 if(!probe && !dp)
3059 continue;
3061 if(is_start)
3063 uint8_t *lang = NULL;
3065 mp_msg(MSGT_DEMUX, MSGL_DBG2, "IS_START\n");
3067 len = pes_parse2(p, buf_size, es, pid_type, pmt, pid);
3068 if(! len)
3070 tss->is_synced = 0;
3071 continue;
3073 es->pid = tss->pid;
3074 tss->is_synced |= es->is_synced || rap_flag;
3075 tss->payload_size = es->payload_size;
3077 if((is_sub || is_audio) && (lang = pid_lang_from_pmt(priv, es->pid)))
3079 memcpy(es->lang, lang, 3);
3080 es->lang[3] = 0;
3082 else
3083 es->lang[0] = 0;
3085 if(probe)
3087 if(es->type == UNKNOWN)
3088 return 0;
3090 tss->type = es->type;
3091 tss->subtype = es->subtype;
3093 return 1;
3095 else
3097 if(es->pts == 0.0f)
3098 es->pts = tss->pts = tss->last_pts;
3099 else
3100 tss->pts = tss->last_pts = es->pts;
3102 mp_msg(MSGT_DEMUX, MSGL_DBG2, "ts_parse, NEW pid=%d, PSIZE: %u, type=%X, start=%p, len=%d\n",
3103 es->pid, es->payload_size, es->type, es->start, es->size);
3105 demuxer->filepos = stream_tell(demuxer->stream) - es->size;
3107 memmove(p, es->start, es->size);
3108 *dp_offset += es->size;
3109 (*dp)->flags = 0;
3110 (*dp)->pos = stream_tell(demuxer->stream);
3111 (*dp)->pts = es->pts;
3113 if(retv > 0)
3114 return retv;
3115 else
3116 continue;
3119 else
3121 uint16_t sz;
3123 es->pid = tss->pid;
3124 es->type = tss->type;
3125 es->subtype = tss->subtype;
3126 es->pts = tss->pts = tss->last_pts;
3127 es->start = &packet[base];
3130 if(tss->payload_size > 0)
3132 sz = FFMIN(tss->payload_size, buf_size);
3133 tss->payload_size -= sz;
3134 es->size = sz;
3136 else
3138 if(is_video)
3140 sz = es->size = buf_size;
3142 else
3144 continue;
3149 if(! probe)
3151 *dp_offset += sz;
3153 if(*dp_offset >= MAX_PACK_BYTES)
3155 (*dp)->pts = tss->last_pts;
3156 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
3157 return 1;
3160 continue;
3162 else
3164 memcpy(es->start, p, sz);
3166 if(es->size)
3167 return es->size;
3168 else
3169 continue;
3174 return 0;
3178 void skip_audio_frame(sh_audio_t *sh_audio);
3180 static void reset_fifos(demuxer_t *demuxer, int a, int v, int s)
3182 ts_priv_t* priv = demuxer->priv;
3183 if(a)
3185 if(priv->fifo[0].pack != NULL)
3187 free_demux_packet(priv->fifo[0].pack);
3188 priv->fifo[0].pack = NULL;
3190 priv->fifo[0].offset = 0;
3193 if(v)
3195 if(priv->fifo[1].pack != NULL)
3197 free_demux_packet(priv->fifo[1].pack);
3198 priv->fifo[1].pack = NULL;
3200 priv->fifo[1].offset = 0;
3203 if(s)
3205 if(priv->fifo[2].pack != NULL)
3207 free_demux_packet(priv->fifo[2].pack);
3208 priv->fifo[2].pack = NULL;
3210 priv->fifo[2].offset = 0;
3212 demuxer->reference_clock = MP_NOPTS_VALUE;
3216 static void demux_seek_ts(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
3218 demux_stream_t *d_audio=demuxer->audio;
3219 demux_stream_t *d_video=demuxer->video;
3220 sh_audio_t *sh_audio=d_audio->sh;
3221 sh_video_t *sh_video=d_video->sh;
3222 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
3223 int i, video_stats;
3224 off_t newpos;
3226 //================= seek in MPEG-TS ==========================
3228 ts_dump_streams(demuxer->priv);
3229 reset_fifos(demuxer, sh_audio != NULL, sh_video != NULL, demuxer->sub->id > 0);
3231 demux_flush(demuxer);
3235 video_stats = (sh_video != NULL);
3236 if(video_stats)
3238 mp_msg(MSGT_DEMUX, MSGL_V, "IBPS: %d, vb: %d\r\n", sh_video->i_bps, priv->vbitrate);
3239 if(priv->vbitrate)
3240 video_stats = priv->vbitrate;
3241 else
3242 video_stats = sh_video->i_bps;
3245 newpos = (flags & SEEK_ABSOLUTE) ? demuxer->movi_start : demuxer->filepos;
3246 if(flags & SEEK_FACTOR) // float seek 0..1
3247 newpos+=(demuxer->movi_end-demuxer->movi_start)*rel_seek_secs;
3248 else
3250 // time seek (secs)
3251 if(! video_stats) // unspecified or VBR
3252 newpos += 2324*75*rel_seek_secs; // 174.3 kbyte/sec
3253 else
3254 newpos += video_stats*rel_seek_secs;
3258 if(newpos < demuxer->movi_start)
3259 newpos = demuxer->movi_start; //begininng of stream
3261 stream_seek(demuxer->stream, newpos);
3262 for(i = 0; i < 8192; i++)
3263 if(priv->ts.pids[i] != NULL)
3264 priv->ts.pids[i]->is_synced = 0;
3266 videobuf_code_len = 0;
3268 if(sh_video != NULL)
3269 ds_fill_buffer(d_video);
3271 if(sh_audio != NULL)
3273 ds_fill_buffer(d_audio);
3276 while(sh_video != NULL)
3278 if(sh_audio && !d_audio->eof && d_video->pts && d_audio->pts)
3280 float a_pts=d_audio->pts;
3281 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
3282 if(d_video->pts > a_pts)
3284 skip_audio_frame(sh_audio); // sync audio
3285 continue;
3290 i = sync_video_packet(d_video);
3291 if((sh_video->format == VIDEO_MPEG1) || (sh_video->format == VIDEO_MPEG2))
3293 if(i==0x1B3 || i==0x1B8) break; // found it!
3295 else if((sh_video->format == VIDEO_MPEG4) && (i==0x1B6))
3296 break;
3297 else if(sh_video->format == VIDEO_VC1 && (i==0x10E || i==0x10F))
3298 break;
3299 else //H264
3301 if((i & ~0x60) == 0x105 || (i & ~0x60) == 0x107) break;
3304 if(!i || !skip_video_packet(d_video)) break; // EOF?
3309 static int demux_ts_fill_buffer(demuxer_t * demuxer, demux_stream_t *ds)
3311 ES_stream_t es;
3312 ts_priv_t *priv = (ts_priv_t *)demuxer->priv;
3314 return -ts_parse(demuxer, &es, priv->packet, 0);
3318 static int ts_check_file_dmx(demuxer_t *demuxer)
3320 return ts_check_file(demuxer) ? DEMUXER_TYPE_MPEG_TS : 0;
3323 static int is_usable_program(ts_priv_t *priv, pmt_t *pmt)
3325 int j;
3327 for(j = 0; j < pmt->es_cnt; j++)
3329 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3330 continue;
3332 priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO ||
3333 priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO
3335 return 1;
3338 return 0;
3341 static int demux_ts_control(demuxer_t *demuxer, int cmd, void *arg)
3343 ts_priv_t* priv = (ts_priv_t *)demuxer->priv;
3345 switch(cmd)
3347 case DEMUXER_CTRL_SWITCH_AUDIO:
3348 case DEMUXER_CTRL_SWITCH_VIDEO:
3350 void *sh = NULL;
3351 int i, n;
3352 int reftype, areset = 0, vreset = 0;
3353 demux_stream_t *ds;
3355 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
3357 reftype = TYPE_VIDEO;
3358 ds = demuxer->video;
3359 vreset = 1;
3361 else
3363 reftype = TYPE_AUDIO;
3364 ds = demuxer->audio;
3365 areset = 1;
3367 n = *((int*)arg);
3368 if(n == -2)
3370 reset_fifos(demuxer, areset, vreset, 0);
3371 ds->id = -2;
3372 ds->sh = NULL;
3373 ds_free_packs(ds);
3374 *((int*)arg) = ds->id;
3375 return DEMUXER_CTRL_OK;
3378 if(n < 0)
3380 for(i = 0; i < 8192; i++)
3382 if(priv->ts.streams[i].id == ds->id && priv->ts.streams[i].type == reftype)
3383 break;
3386 while(!sh)
3388 i = (i+1) % 8192;
3389 if(priv->ts.streams[i].type == reftype)
3391 if(priv->ts.streams[i].id == ds->id) //we made a complete loop
3392 break;
3393 sh = priv->ts.streams[i].sh;
3397 else //audio track <n>
3399 if (n >= 8192 || priv->ts.streams[n].type != reftype) return DEMUXER_CTRL_NOTIMPL;
3400 i = n;
3401 sh = priv->ts.streams[i].sh;
3404 if(sh)
3406 if(ds->id != priv->ts.streams[i].id)
3407 reset_fifos(demuxer, areset, vreset, 0);
3408 ds->id = priv->ts.streams[i].id;
3409 ds->sh = sh;
3410 ds_free_packs(ds);
3411 mp_msg(MSGT_DEMUX, MSGL_V, "\r\ndemux_ts, switched to audio pid %d, id: %d, sh: %p\r\n", i, ds->id, sh);
3414 *((int*)arg) = ds->id;
3415 return DEMUXER_CTRL_OK;
3418 case DEMUXER_CTRL_IDENTIFY_PROGRAM: //returns in prog->{aid,vid} the new ids that comprise a program
3420 int i, j, cnt=0;
3421 int vid_done=0, aid_done=0;
3422 pmt_t *pmt = NULL;
3423 demux_program_t *prog = arg;
3425 if(priv->pmt_cnt < 2)
3426 return DEMUXER_CTRL_NOTIMPL;
3428 if(prog->progid == -1)
3430 int cur_pmt_idx = 0;
3432 for(i = 0; i < priv->pmt_cnt; i++)
3433 if(priv->pmt[i].progid == priv->prog)
3435 cur_pmt_idx = i;
3436 break;
3439 i = (cur_pmt_idx + 1) % priv->pmt_cnt;
3440 while(i != cur_pmt_idx)
3442 pmt = &priv->pmt[i];
3443 cnt = is_usable_program(priv, pmt);
3444 if(cnt)
3445 break;
3446 i = (i + 1) % priv->pmt_cnt;
3449 else
3451 for(i = 0; i < priv->pmt_cnt; i++)
3452 if(priv->pmt[i].progid == prog->progid)
3454 pmt = &priv->pmt[i]; //required program
3455 cnt = is_usable_program(priv, pmt);
3459 if(!cnt)
3460 return DEMUXER_CTRL_NOTIMPL;
3462 //finally some food
3463 prog->aid = prog->vid = -2; //no audio and no video by default
3464 for(j = 0; j < pmt->es_cnt; j++)
3466 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3467 continue;
3469 if(!vid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO)
3471 vid_done = 1;
3472 prog->vid = pmt->es[j].pid;
3474 else if(!aid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO)
3476 aid_done = 1;
3477 prog->aid = pmt->es[j].pid;
3481 priv->prog = prog->progid = pmt->progid;
3482 return DEMUXER_CTRL_OK;
3485 default:
3486 return DEMUXER_CTRL_NOTIMPL;
3491 const demuxer_desc_t demuxer_desc_mpeg_ts = {
3492 "MPEG-TS demuxer",
3493 "mpegts",
3494 "TS",
3495 "Nico Sabbi",
3497 DEMUXER_TYPE_MPEG_TS,
3498 0, // unsafe autodetect
3499 ts_check_file_dmx,
3500 demux_ts_fill_buffer,
3501 demux_open_ts,
3502 demux_close_ts,
3503 demux_seek_ts,
3504 demux_ts_control