Fix vf_tcdump's compilation
[mplayer/kovensky.git] / libmpdemux / demux_ts.c
blobc0dd2e632d15c6b371024c316a815cc2500b4800
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"
32 #include "stream/stream.h"
33 #include "demuxer.h"
34 #include "parse_es.h"
35 #include "stheader.h"
36 #include "ms_hdr.h"
37 #include "mpeg_hdr.h"
38 #include "demux_ts.h"
40 #define TS_PH_PACKET_SIZE 192
41 #define TS_FEC_PACKET_SIZE 204
42 #define TS_PACKET_SIZE 188
43 #define NB_PID_MAX 8192
45 #define MAX_HEADER_SIZE 6 /* enough for PES header + length */
46 #define MAX_CHECK_SIZE 65535
47 #define TS_MAX_PROBE_SIZE 2000000 /* do not forget to change this in cfg-common-opts.h, too */
48 #define NUM_CONSECUTIVE_TS_PACKETS 32
49 #define NUM_CONSECUTIVE_AUDIO_PACKETS 348
50 #define MAX_A52_FRAME_SIZE 3840
52 #ifndef SIZE_MAX
53 #define SIZE_MAX ((size_t)-1)
54 #endif
56 #define TYPE_AUDIO 1
57 #define TYPE_VIDEO 2
59 int ts_prog;
60 int ts_keep_broken=0;
61 off_t ts_probe = 0;
62 int audio_substream_id = -1;
63 extern char *dvdsub_lang, *audio_lang; //for -alang
65 typedef enum
67 UNKNOWN = -1,
68 VIDEO_MPEG1 = 0x10000001,
69 VIDEO_MPEG2 = 0x10000002,
70 VIDEO_MPEG4 = 0x10000004,
71 VIDEO_H264 = 0x10000005,
72 VIDEO_AVC = mmioFOURCC('a', 'v', 'c', '1'),
73 VIDEO_VC1 = mmioFOURCC('W', 'V', 'C', '1'),
74 AUDIO_MP2 = 0x50,
75 AUDIO_A52 = 0x2000,
76 AUDIO_DTS = 0x2001,
77 AUDIO_LPCM_BE = 0x10001,
78 AUDIO_AAC = mmioFOURCC('M', 'P', '4', 'A'),
79 AUDIO_TRUEHD = mmioFOURCC('T', 'R', 'H', 'D'),
80 SPU_DVD = 0x3000000,
81 SPU_DVB = 0x3000001,
82 SPU_TELETEXT = 0x3000002,
83 PES_PRIVATE1 = 0xBD00000,
84 SL_PES_STREAM = 0xD000000,
85 SL_SECTION = 0xD100000,
86 MP4_OD = 0xD200000,
87 } es_stream_type_t;
89 typedef struct {
90 uint8_t *buffer;
91 uint16_t buffer_len;
92 } ts_section_t;
94 typedef struct {
95 int size;
96 unsigned char *start;
97 uint16_t payload_size;
98 es_stream_type_t type, subtype;
99 double pts, last_pts;
100 int pid;
101 char lang[4];
102 int last_cc; // last cc code (-1 if first packet)
103 int is_synced;
104 ts_section_t section;
105 uint8_t *extradata;
106 int extradata_alloc, extradata_len;
107 struct {
108 uint8_t au_start, au_end, last_au_end;
109 } sl;
110 } ES_stream_t;
112 typedef struct {
113 void *sh;
114 int id;
115 int type;
116 } sh_av_t;
118 typedef struct MpegTSContext {
119 int packet_size; // raw packet size, including FEC if present e.g. 188 bytes
120 ES_stream_t *pids[NB_PID_MAX];
121 sh_av_t streams[NB_PID_MAX];
122 } MpegTSContext;
125 typedef struct {
126 demux_stream_t *ds;
127 demux_packet_t *pack;
128 int offset, buffer_size;
129 } av_fifo_t;
131 #define MAX_EXTRADATA_SIZE 64*1024
132 typedef struct {
133 int32_t object_type; //aka codec used
134 int32_t stream_type; //video, audio etc.
135 uint8_t buf[MAX_EXTRADATA_SIZE];
136 uint16_t buf_size;
137 uint8_t szm1;
138 } mp4_decoder_config_t;
140 typedef struct {
141 //flags
142 uint8_t flags;
143 uint8_t au_start;
144 uint8_t au_end;
145 uint8_t random_accesspoint;
146 uint8_t random_accesspoint_only;
147 uint8_t padding;
148 uint8_t use_ts;
149 uint8_t idle;
150 uint8_t duration;
152 uint32_t ts_resolution, ocr_resolution;
153 uint8_t ts_len, ocr_len, au_len, instant_bitrate_len, degr_len, au_seqnum_len, packet_seqnum_len;
154 uint32_t timescale;
155 uint16_t au_duration, cts_duration;
156 uint64_t ocr, dts, cts;
157 } mp4_sl_config_t;
159 typedef struct {
160 uint16_t id;
161 uint8_t flags;
162 mp4_decoder_config_t decoder;
163 mp4_sl_config_t sl;
164 } mp4_es_descr_t;
166 typedef struct {
167 uint16_t id;
168 uint8_t flags;
169 mp4_es_descr_t *es;
170 uint16_t es_cnt;
171 } mp4_od_t;
173 typedef struct {
174 uint8_t skip;
175 uint8_t table_id;
176 uint8_t ssi;
177 uint16_t section_length;
178 uint16_t ts_id;
179 uint8_t version_number;
180 uint8_t curr_next;
181 uint8_t section_number;
182 uint8_t last_section_number;
183 struct pat_progs_t {
184 uint16_t id;
185 uint16_t pmt_pid;
186 } *progs;
187 uint16_t progs_cnt;
188 ts_section_t section;
189 } pat_t;
191 typedef struct {
192 uint16_t progid;
193 uint8_t skip;
194 uint8_t table_id;
195 uint8_t ssi;
196 uint16_t section_length;
197 uint8_t version_number;
198 uint8_t curr_next;
199 uint8_t section_number;
200 uint8_t last_section_number;
201 uint16_t PCR_PID;
202 uint16_t prog_descr_length;
203 ts_section_t section;
204 uint16_t es_cnt;
205 struct pmt_es_t {
206 uint16_t pid;
207 uint32_t type; //it's 8 bit long, but cast to the right type as FOURCC
208 uint16_t descr_length;
209 uint8_t format_descriptor[5];
210 uint8_t lang[4];
211 uint16_t mp4_es_id;
212 } *es;
213 mp4_od_t iod, *od;
214 mp4_es_descr_t *mp4es;
215 int od_cnt, mp4es_cnt;
216 } pmt_t;
218 typedef struct {
219 uint64_t size;
220 float duration;
221 double first_pts;
222 double last_pts;
223 } TS_stream_info;
225 typedef struct {
226 MpegTSContext ts;
227 int last_pid;
228 av_fifo_t fifo[3]; //0 for audio, 1 for video, 2 for subs
229 pat_t pat;
230 pmt_t *pmt;
231 uint16_t pmt_cnt;
232 uint32_t prog;
233 uint32_t vbitrate;
234 int keep_broken;
235 int last_aid;
236 int last_vid;
237 char packet[TS_FEC_PACKET_SIZE];
238 TS_stream_info vstr, astr;
239 } ts_priv_t;
242 typedef struct {
243 es_stream_type_t type;
244 ts_section_t section;
245 } TS_pids_t;
248 #define IS_AUDIO(x) (((x) == AUDIO_MP2) || ((x) == AUDIO_A52) || ((x) == AUDIO_LPCM_BE) || ((x) == AUDIO_AAC) || ((x) == AUDIO_DTS) || ((x) == AUDIO_TRUEHD))
249 #define IS_VIDEO(x) (((x) == VIDEO_MPEG1) || ((x) == VIDEO_MPEG2) || ((x) == VIDEO_MPEG4) || ((x) == VIDEO_H264) || ((x) == VIDEO_AVC) || ((x) == VIDEO_VC1))
250 #define IS_SUB(x) (((x) == SPU_DVD) || ((x) == SPU_DVB) || ((x) == SPU_TELETEXT))
252 static int ts_parse(demuxer_t *demuxer, ES_stream_t *es, unsigned char *packet, int probe);
254 static uint8_t get_packet_size(const unsigned char *buf, int size)
256 int i;
258 if (size < (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS))
259 return 0;
261 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
263 if (buf[i * TS_PACKET_SIZE] != 0x47)
265 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
266 goto try_fec;
269 return TS_PACKET_SIZE;
271 try_fec:
272 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
274 if (buf[i * TS_FEC_PACKET_SIZE] != 0x47){
275 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
276 goto try_philips;
279 return TS_FEC_PACKET_SIZE;
281 try_philips:
282 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
284 if (buf[i * TS_PH_PACKET_SIZE] != 0x47)
285 return 0;
287 return TS_PH_PACKET_SIZE;
290 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h);
291 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid);
293 static void ts_add_stream(demuxer_t * demuxer, ES_stream_t *es)
295 int i;
296 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
298 if(priv->ts.streams[es->pid].sh)
299 return;
301 if((IS_AUDIO(es->type) || IS_AUDIO(es->subtype)) && priv->last_aid+1 < MAX_A_STREAMS)
303 sh_audio_t *sh = new_sh_audio_aid(demuxer, priv->last_aid, es->pid);
304 if(sh)
306 const char *lang = pid_lang_from_pmt(priv, es->pid);
307 sh->needs_parsing = 1;
308 sh->format = IS_AUDIO(es->type) ? es->type : es->subtype;
309 sh->ds = demuxer->audio;
311 priv->ts.streams[es->pid].id = priv->last_aid;
312 priv->ts.streams[es->pid].sh = sh;
313 priv->ts.streams[es->pid].type = TYPE_AUDIO;
314 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);
315 if (lang && lang[0])
316 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_AID_%d_LANG=%s\n", es->pid, lang);
317 priv->last_aid++;
320 if(es->extradata && es->extradata_len)
322 sh->wf = malloc(sizeof (WAVEFORMATEX) + es->extradata_len);
323 sh->wf->cbSize = es->extradata_len;
324 memcpy(sh->wf + 1, es->extradata, es->extradata_len);
328 if((IS_VIDEO(es->type) || IS_VIDEO(es->subtype)) && priv->last_vid+1 < MAX_V_STREAMS)
330 sh_video_t *sh = new_sh_video_vid(demuxer, priv->last_vid, es->pid);
331 if(sh)
333 sh->format = IS_VIDEO(es->type) ? es->type : es->subtype;
334 sh->ds = demuxer->video;
336 priv->ts.streams[es->pid].id = priv->last_vid;
337 priv->ts.streams[es->pid].sh = sh;
338 priv->ts.streams[es->pid].type = TYPE_VIDEO;
339 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);
340 priv->last_vid++;
343 if(sh->format == VIDEO_AVC && es->extradata && es->extradata_len)
345 int w = 0, h = 0;
346 sh->bih = calloc(1, sizeof(BITMAPINFOHEADER) + es->extradata_len);
347 sh->bih->biSize= sizeof(BITMAPINFOHEADER) + es->extradata_len;
348 sh->bih->biCompression = sh->format;
349 memcpy(sh->bih + 1, es->extradata, es->extradata_len);
350 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "EXTRADATA(%d BYTES): \n", es->extradata_len);
351 for(i = 0;i < es->extradata_len; i++)
352 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "%02x ", (int) es->extradata[i]);
353 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"\n");
354 if(parse_avc_sps(es->extradata, es->extradata_len, &w, &h))
356 sh->bih->biWidth = w;
357 sh->bih->biHeight = h;
364 static int ts_check_file(demuxer_t * demuxer)
366 const int buf_size = (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS);
367 unsigned char buf[TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS], done = 0, *ptr;
368 uint32_t _read, i, count = 0, is_ts;
369 int cc[NB_PID_MAX], last_cc[NB_PID_MAX], pid, cc_ok, c, good, bad;
370 uint8_t size = 0;
371 off_t pos = 0;
372 off_t init_pos;
374 mp_msg(MSGT_DEMUX, MSGL_V, "Checking for MPEG-TS...\n");
376 init_pos = stream_tell(demuxer->stream);
377 is_ts = 0;
378 while(! done)
380 i = 1;
381 c = 0;
383 while(((c=stream_read_char(demuxer->stream)) != 0x47)
384 && (c >= 0)
385 && (i < MAX_CHECK_SIZE)
386 && ! demuxer->stream->eof
387 ) i++;
390 if(c != 0x47)
392 mp_msg(MSGT_DEMUX, MSGL_V, "THIS DOESN'T LOOK LIKE AN MPEG-TS FILE!\n");
393 is_ts = 0;
394 done = 1;
395 continue;
398 pos = stream_tell(demuxer->stream) - 1;
399 buf[0] = c;
400 _read = stream_read(demuxer->stream, &buf[1], buf_size-1);
402 if(_read < buf_size-1)
404 mp_msg(MSGT_DEMUX, MSGL_V, "COULDN'T READ ENOUGH DATA, EXITING TS_CHECK\n");
405 stream_reset(demuxer->stream);
406 return 0;
409 size = get_packet_size(buf, buf_size);
410 if(size)
412 done = 1;
413 is_ts = 1;
416 if(pos - init_pos >= MAX_CHECK_SIZE)
418 done = 1;
419 is_ts = 0;
423 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);
424 stream_seek(demuxer->stream, pos);
426 if(! is_ts)
427 return 0;
429 //LET'S CHECK continuity counters
430 good = bad = 0;
431 for(count = 0; count < NB_PID_MAX; count++)
433 cc[count] = last_cc[count] = -1;
436 for(count = 0; count < NUM_CONSECUTIVE_TS_PACKETS; count++)
438 ptr = &(buf[size * count]);
439 pid = ((ptr[1] & 0x1f) << 8) | ptr[2];
440 mp_msg(MSGT_DEMUX, MSGL_DBG2, "BUF: %02x %02x %02x %02x, PID %d, SIZE: %d \n",
441 ptr[0], ptr[1], ptr[2], ptr[3], pid, size);
443 if((pid == 8191) || (pid < 16))
444 continue;
446 cc[pid] = (ptr[3] & 0xf);
447 cc_ok = (last_cc[pid] < 0) || ((((last_cc[pid] + 1) & 0x0f) == cc[pid]));
448 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PID %d, COMPARE CC %d AND LAST_CC %d\n", pid, cc[pid], last_cc[pid]);
449 if(! cc_ok)
450 //return 0;
451 bad++;
452 else
453 good++;
455 last_cc[pid] = cc[pid];
458 mp_msg(MSGT_DEMUX, MSGL_V, "GOOD CC: %d, BAD CC: %d\n", good, bad);
460 if(good >= bad)
461 return size;
462 else
463 return 0;
467 static inline int32_t progid_idx_in_pmt(ts_priv_t *priv, uint16_t progid)
469 int x;
471 if(priv->pmt == NULL)
472 return -1;
474 for(x = 0; x < priv->pmt_cnt; x++)
476 if(priv->pmt[x].progid == progid)
477 return x;
480 return -1;
484 static inline int32_t progid_for_pid(ts_priv_t *priv, int pid, int32_t req) //finds the first program listing a pid
486 int i, j;
487 pmt_t *pmt;
490 if(priv->pmt == NULL)
491 return -1;
494 for(i=0; i < priv->pmt_cnt; i++)
496 pmt = &(priv->pmt[i]);
498 if(pmt->es == NULL)
499 return -1;
501 for(j = 0; j < pmt->es_cnt; j++)
503 if(pmt->es[j].pid == pid)
505 if((req == 0) || (req == pmt->progid))
506 return pmt->progid;
511 return -1;
514 static inline int32_t prog_pcr_pid(ts_priv_t *priv, int progid)
516 int i;
518 if(priv->pmt == NULL)
519 return -1;
520 for(i=0; i < priv->pmt_cnt; i++)
522 if(priv->pmt[i].progid == progid)
523 return priv->pmt[i].PCR_PID;
525 return -1;
529 static inline int pid_match_lang(ts_priv_t *priv, uint16_t pid, char *lang)
531 uint16_t i, j;
532 pmt_t *pmt;
534 if(priv->pmt == NULL)
535 return -1;
537 for(i=0; i < priv->pmt_cnt; i++)
539 pmt = &(priv->pmt[i]);
541 if(pmt->es == NULL)
542 return -1;
544 for(j = 0; j < pmt->es_cnt; j++)
546 if(pmt->es[j].pid != pid)
547 continue;
549 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);
550 if(strncmp(pmt->es[j].lang, lang, 3) == 0)
552 return 1;
558 return -1;
561 typedef struct {
562 int32_t atype, vtype, stype; //types
563 int32_t apid, vpid, spid; //stream ids
564 char slang[4], alang[4]; //languages
565 uint16_t prog;
566 off_t probe;
567 } tsdemux_init_t;
569 //stripped down version of a52_syncinfo() from liba52
570 //copyright belongs to Michel Lespinasse <walken@zoy.org> and Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
571 int mp_a52_framesize(uint8_t * buf, int *srate)
573 int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
574 128, 160, 192, 224, 256, 320, 384, 448,
575 512, 576, 640
577 uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
578 int frmsizecod, bitrate, half;
580 if((buf[0] != 0x0b) || (buf[1] != 0x77)) /* syncword */
581 return 0;
583 if(buf[5] >= 0x60) /* bsid >= 12 */
584 return 0;
586 half = halfrate[buf[5] >> 3];
588 frmsizecod = buf[4] & 63;
589 if(frmsizecod >= 38)
590 return 0;
592 bitrate = rate[frmsizecod >> 1];
594 switch(buf[4] & 0xc0)
596 case 0: /* 48 KHz */
597 *srate = 48000 >> half;
598 return 4 * bitrate;
599 case 0x40: /* 44.1 KHz */
600 *srate = 44100 >> half;
601 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
602 case 0x80: /* 32 KHz */
603 *srate = 32000 >> half;
604 return 6 * bitrate;
607 return 0;
610 //second stage: returns the count of A52 syncwords found
611 static int a52_check(char *buf, int len)
613 int cnt, frame_length, ok, srate;
615 cnt = ok = 0;
616 if(len < 8)
617 return 0;
619 while(cnt < len - 7)
621 if(buf[cnt] == 0x0B && buf[cnt+1] == 0x77)
623 frame_length = mp_a52_framesize(&buf[cnt], &srate);
624 if(frame_length>=7 && frame_length<=3840)
626 cnt += frame_length;
627 ok++;
629 else
630 cnt++;
632 else
633 cnt++;
636 mp_msg(MSGT_DEMUXER, MSGL_V, "A52_CHECK(%d input bytes), found %d frame syncwords of %d bytes length\n", len, ok, frame_length);
637 return ok;
641 static off_t ts_detect_streams(demuxer_t *demuxer, tsdemux_init_t *param)
643 int video_found = 0, audio_found = 0, sub_found = 0, i, num_packets = 0, req_apid, req_vpid, req_spid;
644 int is_audio, is_video, is_sub, has_tables;
645 int32_t p, chosen_pid = 0;
646 off_t pos=0, ret = 0, init_pos, end_pos;
647 ES_stream_t es;
648 unsigned char tmp[TS_FEC_PACKET_SIZE];
649 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
650 struct {
651 char *buf;
652 int pos;
653 } pes_priv1[8192], *pptr;
654 char *tmpbuf;
656 priv->last_pid = 8192; //invalid pid
658 req_apid = param->apid;
659 req_vpid = param->vpid;
660 req_spid = param->spid;
662 has_tables = 0;
663 memset(pes_priv1, 0, sizeof(pes_priv1));
664 init_pos = stream_tell(demuxer->stream);
665 mp_msg(MSGT_DEMUXER, MSGL_V, "PROBING UP TO %"PRIu64", PROG: %d\n", (uint64_t) param->probe, param->prog);
666 end_pos = init_pos + (param->probe ? param->probe : TS_MAX_PROBE_SIZE);
667 while(1)
669 pos = stream_tell(demuxer->stream);
670 if(pos > end_pos || demuxer->stream->eof)
671 break;
673 if(ts_parse(demuxer, &es, tmp, 1))
675 //Non PES-aligned A52 audio may escape detection if PMT is not present;
676 //in this case we try to find at least 3 A52 syncwords
677 if((es.type == PES_PRIVATE1) && (! audio_found) && req_apid > -2)
679 pptr = &pes_priv1[es.pid];
680 if(pptr->pos < 64*1024)
682 tmpbuf = realloc(pptr->buf, pptr->pos + es.size);
683 if(tmpbuf != NULL)
685 pptr->buf = tmpbuf;
686 memcpy(&(pptr->buf[ pptr->pos ]), es.start, es.size);
687 pptr->pos += es.size;
688 if(a52_check(pptr->buf, pptr->pos) > 2)
690 param->atype = AUDIO_A52;
691 param->apid = es.pid;
692 es.type = AUDIO_A52;
698 is_audio = IS_AUDIO(es.type) || ((es.type==SL_PES_STREAM) && IS_AUDIO(es.subtype));
699 is_video = IS_VIDEO(es.type) || ((es.type==SL_PES_STREAM) && IS_VIDEO(es.subtype));
700 is_sub = IS_SUB(es.type);
703 if((! is_audio) && (! is_video) && (! is_sub))
704 continue;
705 if(is_audio && req_apid==-2)
706 continue;
708 if(is_video)
710 chosen_pid = (req_vpid == es.pid);
711 if((! chosen_pid) && (req_vpid > 0))
712 continue;
714 else if(is_audio)
716 if(req_apid > 0)
718 chosen_pid = (req_apid == es.pid);
719 if(! chosen_pid)
720 continue;
722 else if(param->alang[0] > 0 && es.lang[0] > 0)
724 if(pid_match_lang(priv, es.pid, param->alang) == -1)
725 continue;
727 chosen_pid = 1;
728 param->apid = req_apid = es.pid;
731 else if(is_sub)
733 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_SUBTITLE_ID=%d\n", es.pid);
734 if (es.lang[0] > 0)
735 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_SID_%d_LANG=%s\n", es.pid, es.lang);
736 chosen_pid = (req_spid == es.pid);
737 if((! chosen_pid) && (req_spid > 0))
738 continue;
741 if(req_apid < 0 && (param->alang[0] == 0) && req_vpid < 0 && req_spid < 0)
742 chosen_pid = 1;
744 if((ret == 0) && chosen_pid)
746 ret = stream_tell(demuxer->stream);
749 p = progid_for_pid(priv, es.pid, param->prog);
750 if(p != -1)
752 has_tables++;
753 if(!param->prog && chosen_pid)
754 param->prog = p;
757 if((param->prog > 0) && (param->prog != p))
759 if(audio_found)
761 if(is_video && (req_vpid == es.pid))
763 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
764 param->vpid = es.pid;
765 video_found = 1;
766 break;
770 if(video_found)
772 if(is_audio && (req_apid == es.pid))
774 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
775 param->apid = es.pid;
776 audio_found = 1;
777 break;
782 continue;
786 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "TYPE: %x, PID: %d, PROG FOUND: %d\n", es.type, es.pid, param->prog);
789 if(is_video)
791 if((req_vpid == -1) || (req_vpid == es.pid))
793 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
794 param->vpid = es.pid;
795 video_found = 1;
800 if(((req_vpid == -2) || (num_packets >= NUM_CONSECUTIVE_AUDIO_PACKETS)) && audio_found && !param->probe)
802 //novideo or we have at least 348 audio packets (64 KB) without video (TS with audio only)
803 param->vtype = 0;
804 break;
807 if(is_sub)
809 if((req_spid == -1) || (req_spid == es.pid))
811 param->stype = es.type;
812 param->spid = es.pid;
813 sub_found = 1;
817 if(is_audio)
819 if((req_apid == -1) || (req_apid == es.pid))
821 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
822 param->apid = es.pid;
823 audio_found = 1;
827 if(audio_found && (param->apid == es.pid) && (! video_found))
828 num_packets++;
830 if((has_tables==0) && (video_found && audio_found) && (pos >= 1000000))
831 break;
835 for(i=0; i<8192; i++)
837 if(pes_priv1[i].buf != NULL)
839 free(pes_priv1[i].buf);
840 pes_priv1[i].buf = NULL;
841 pes_priv1[i].pos = 0;
845 if(video_found)
847 if(param->vtype == VIDEO_MPEG1)
848 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG1(pid=%d) ", param->vpid);
849 else if(param->vtype == VIDEO_MPEG2)
850 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG2(pid=%d) ", param->vpid);
851 else if(param->vtype == VIDEO_MPEG4)
852 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG4(pid=%d) ", param->vpid);
853 else if(param->vtype == VIDEO_H264)
854 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO H264(pid=%d) ", param->vpid);
855 else if(param->vtype == VIDEO_VC1)
856 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO VC1(pid=%d) ", param->vpid);
857 else if(param->vtype == VIDEO_AVC)
858 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO AVC(NAL-H264, pid=%d) ", param->vpid);
860 else
862 param->vtype = UNKNOWN;
863 //WE DIDN'T MATCH ANY VIDEO STREAM
864 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO VIDEO! ");
867 if(param->atype == AUDIO_MP2)
868 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO MPA(pid=%d)", param->apid);
869 else if(param->atype == AUDIO_A52)
870 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO A52(pid=%d)", param->apid);
871 else if(param->atype == AUDIO_DTS)
872 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO DTS(pid=%d)", param->apid);
873 else if(param->atype == AUDIO_LPCM_BE)
874 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO LPCM(pid=%d)", param->apid);
875 else if(param->atype == AUDIO_AAC)
876 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO AAC(pid=%d)", param->apid);
877 else if(param->atype == AUDIO_TRUEHD)
878 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO TRUEHD(pid=%d)", param->apid);
879 else
881 audio_found = 0;
882 param->atype = UNKNOWN;
883 //WE DIDN'T MATCH ANY AUDIO STREAM, SO WE FORCE THE DEMUXER TO IGNORE AUDIO
884 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO AUDIO! ");
887 if(IS_SUB(param->stype))
888 mp_msg(MSGT_DEMUXER, MSGL_INFO, " SUB %s(pid=%d) ", (param->stype==SPU_DVD ? "DVD" : param->stype==SPU_DVB ? "DVB" : "Teletext"), param->spid);
889 else
891 param->stype = UNKNOWN;
892 mp_msg(MSGT_DEMUXER, MSGL_INFO, " NO SUBS (yet)! ");
895 if(video_found || audio_found)
897 if(!param->prog)
899 p = progid_for_pid(priv, video_found ? param->vpid : param->apid, 0);
900 if(p != -1)
901 param->prog = p;
904 if(demuxer->stream->eof && (ret == 0))
905 ret = init_pos;
906 mp_msg(MSGT_DEMUXER, MSGL_INFO, " PROGRAM N. %d\n", param->prog);
908 else
909 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\n");
912 for(i=0; i<8192; i++)
914 if(priv->ts.pids[i] != NULL)
916 priv->ts.pids[i]->payload_size = 0;
917 priv->ts.pids[i]->pts = priv->ts.pids[i]->last_pts = 0;
918 priv->ts.pids[i]->last_cc = -1;
919 priv->ts.pids[i]->is_synced = 0;
923 return ret;
926 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h)
928 int sps, sps_len;
929 unsigned char *ptr;
930 mp_mpeg_header_t picture;
931 if(len < 6)
932 return 0;
933 sps = buf[5] & 0x1f;
934 if(!sps)
935 return 0;
936 sps_len = (buf[6] << 8) | buf[7];
937 if(!sps_len || (sps_len > len - 8))
938 return 0;
939 ptr = &(buf[8]);
940 picture.display_picture_width = picture.display_picture_height = 0;
941 h264_parse_sps(&picture, ptr, len - 8);
942 if(!picture.display_picture_width || !picture.display_picture_height)
943 return 0;
944 *w = picture.display_picture_width;
945 *h = picture.display_picture_height;
946 return 1;
949 static demuxer_t *demux_open_ts(demuxer_t * demuxer)
951 int i;
952 uint8_t packet_size;
953 sh_video_t *sh_video;
954 sh_audio_t *sh_audio;
955 off_t start_pos;
956 tsdemux_init_t params;
957 ts_priv_t * priv = demuxer->priv;
959 mp_msg(MSGT_DEMUX, MSGL_V, "DEMUX OPEN, AUDIO_ID: %d, VIDEO_ID: %d, SUBTITLE_ID: %d,\n",
960 demuxer->audio->id, demuxer->video->id, demuxer->sub->id);
963 demuxer->type= DEMUXER_TYPE_MPEG_TS;
966 stream_reset(demuxer->stream);
968 packet_size = ts_check_file(demuxer);
969 if(!packet_size)
970 return NULL;
972 priv = calloc(1, sizeof(ts_priv_t));
973 if(priv == NULL)
975 mp_msg(MSGT_DEMUX, MSGL_FATAL, "DEMUX_OPEN_TS, couldn't allocate enough memory for ts->priv, exit\n");
976 return NULL;
979 for(i=0; i < 8192; i++)
981 priv->ts.pids[i] = NULL;
982 priv->ts.streams[i].id = -3;
984 priv->pat.progs = NULL;
985 priv->pat.progs_cnt = 0;
986 priv->pat.section.buffer = NULL;
987 priv->pat.section.buffer_len = 0;
989 priv->pmt = NULL;
990 priv->pmt_cnt = 0;
992 priv->keep_broken = ts_keep_broken;
993 priv->ts.packet_size = packet_size;
996 demuxer->priv = priv;
997 if(demuxer->stream->type != STREAMTYPE_FILE)
998 demuxer->seekable = 1;
999 else
1000 demuxer->seekable = 1;
1003 params.atype = params.vtype = params.stype = UNKNOWN;
1004 params.apid = demuxer->audio->id;
1005 params.vpid = demuxer->video->id;
1006 params.spid = demuxer->sub->id;
1007 params.prog = ts_prog;
1008 params.probe = ts_probe;
1010 if(dvdsub_lang != NULL)
1012 strncpy(params.slang, dvdsub_lang, 3);
1013 params.slang[3] = 0;
1015 else
1016 memset(params.slang, 0, 4);
1018 if(audio_lang != NULL)
1020 strncpy(params.alang, audio_lang, 3);
1021 params.alang[3] = 0;
1023 else
1024 memset(params.alang, 0, 4);
1026 start_pos = ts_detect_streams(demuxer, &params);
1028 demuxer->sub->id = params.spid;
1029 priv->prog = params.prog;
1031 if(params.vtype != UNKNOWN)
1033 ts_add_stream(demuxer, priv->ts.pids[params.vpid]);
1034 sh_video = priv->ts.streams[params.vpid].sh;
1035 demuxer->video->id = priv->ts.streams[params.vpid].id;
1036 sh_video->ds = demuxer->video;
1037 sh_video->format = params.vtype;
1038 demuxer->video->sh = sh_video;
1041 if(params.atype != UNKNOWN)
1043 ES_stream_t *es = priv->ts.pids[params.apid];
1045 if(!IS_AUDIO(es->type) && !IS_AUDIO(es->subtype) && IS_AUDIO(params.atype)) es->subtype = params.atype;
1046 ts_add_stream(demuxer, priv->ts.pids[params.apid]);
1047 sh_audio = priv->ts.streams[params.apid].sh;
1048 demuxer->audio->id = priv->ts.streams[params.apid].id;
1049 sh_audio->ds = demuxer->audio;
1050 sh_audio->format = params.atype;
1051 demuxer->audio->sh = sh_audio;
1055 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);
1058 start_pos = (start_pos <= priv->ts.packet_size ? 0 : start_pos - priv->ts.packet_size);
1059 demuxer->movi_start = start_pos;
1060 demuxer->reference_clock = MP_NOPTS_VALUE;
1061 stream_reset(demuxer->stream);
1062 stream_seek(demuxer->stream, start_pos); //IF IT'S FROM A PIPE IT WILL FAIL, BUT WHO CARES?
1065 priv->last_pid = 8192; //invalid pid
1067 for(i = 0; i < 3; i++)
1069 priv->fifo[i].pack = NULL;
1070 priv->fifo[i].offset = 0;
1072 priv->fifo[0].ds = demuxer->audio;
1073 priv->fifo[1].ds = demuxer->video;
1074 priv->fifo[2].ds = demuxer->sub;
1076 priv->fifo[0].buffer_size = 1536;
1077 priv->fifo[1].buffer_size = 32767;
1078 priv->fifo[2].buffer_size = 32767;
1080 priv->pat.section.buffer_len = 0;
1081 for(i = 0; i < priv->pmt_cnt; i++)
1082 priv->pmt[i].section.buffer_len = 0;
1084 demuxer->filepos = stream_tell(demuxer->stream);
1085 return demuxer;
1088 static void demux_close_ts(demuxer_t * demuxer)
1090 uint16_t i;
1091 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
1093 if(priv)
1095 if(priv->pat.section.buffer)
1096 free(priv->pat.section.buffer);
1097 if(priv->pat.progs)
1098 free(priv->pat.progs);
1100 if(priv->pmt)
1102 for(i = 0; i < priv->pmt_cnt; i++)
1104 if(priv->pmt[i].section.buffer)
1105 free(priv->pmt[i].section.buffer);
1106 if(priv->pmt[i].es)
1107 free(priv->pmt[i].es);
1109 free(priv->pmt);
1111 free(priv);
1113 demuxer->priv=NULL;
1117 #define getbits mp_getbits
1119 static int mp4_parse_sl_packet(pmt_t *pmt, uint8_t *buf, uint16_t packet_len, int pid, ES_stream_t *pes_es)
1121 int i, n, m, mp4_es_id = -1;
1122 uint64_t v = 0;
1123 uint32_t pl_size = 0;
1124 int deg_flag = 0;
1125 mp4_es_descr_t *es = NULL;
1126 mp4_sl_config_t *sl = NULL;
1127 uint8_t au_start = 0, au_end = 0, rap_flag = 0, ocr_flag = 0, padding = 0, padding_bits = 0, idle = 0;
1129 pes_es->is_synced = 0;
1130 mp_msg(MSGT_DEMUXER,MSGL_V, "mp4_parse_sl_packet, pid: %d, pmt: %pm, packet_len: %d\n", pid, pmt, packet_len);
1131 if(! pmt || !packet_len)
1132 return 0;
1134 for(i = 0; i < pmt->es_cnt; i++)
1136 if(pmt->es[i].pid == pid)
1137 mp4_es_id = pmt->es[i].mp4_es_id;
1139 if(mp4_es_id < 0)
1140 return -1;
1142 for(i = 0; i < pmt->mp4es_cnt; i++)
1144 if(pmt->mp4es[i].id == mp4_es_id)
1145 es = &(pmt->mp4es[i]);
1147 if(! es)
1148 return -1;
1150 pes_es->subtype = es->decoder.object_type;
1152 sl = &(es->sl);
1153 if(!sl)
1154 return -1;
1156 //now es is the complete es_descriptor of out mp4 ES stream
1157 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "ID: %d, FLAGS: 0x%x, subtype: %x\n", es->id, sl->flags, pes_es->subtype);
1159 n = 0;
1160 if(sl->au_start)
1161 pes_es->sl.au_start = au_start = getbits(buf, n++, 1);
1162 else
1163 pes_es->sl.au_start = (pes_es->sl.last_au_end ? 1 : 0);
1164 if(sl->au_end)
1165 pes_es->sl.au_end = au_end = getbits(buf, n++, 1);
1167 if(!sl->au_start && !sl->au_end)
1169 pes_es->sl.au_start = pes_es->sl.au_end = au_start = au_end = 1;
1171 pes_es->sl.last_au_end = pes_es->sl.au_end;
1174 if(sl->ocr_len > 0)
1175 ocr_flag = getbits(buf, n++, 1);
1176 if(sl->idle)
1177 idle = getbits(buf, n++, 1);
1178 if(sl->padding)
1179 padding = getbits(buf, n++, 1);
1180 if(padding)
1182 padding_bits = getbits(buf, n, 3);
1183 n += 3;
1186 if(idle || (padding && !padding_bits))
1188 pes_es->payload_size = 0;
1189 return -1;
1192 //(! idle && (!padding || padding_bits != 0)) is true
1193 n += sl->packet_seqnum_len;
1194 if(sl->degr_len)
1195 deg_flag = getbits(buf, n++, 1);
1196 if(deg_flag)
1197 n += sl->degr_len;
1199 if(ocr_flag)
1201 n += sl->ocr_len;
1202 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "OCR: %d bits\n", sl->ocr_len);
1205 if(packet_len * 8 <= n)
1206 return -1;
1208 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "\nAU_START: %d, AU_END: %d\n", au_start, au_end);
1209 if(au_start)
1211 int dts_flag = 0, cts_flag = 0, ib_flag = 0;
1213 if(sl->random_accesspoint)
1214 rap_flag = getbits(buf, n++, 1);
1216 //check commented because it seems it's rarely used, and we need this flag set in case of au_start
1217 //the decoder will eventually discard the payload if it can't decode it
1218 //if(rap_flag || sl->random_accesspoint_only)
1219 pes_es->is_synced = 1;
1221 n += sl->au_seqnum_len;
1222 if(packet_len * 8 <= n+8)
1223 return -1;
1224 if(sl->use_ts)
1226 dts_flag = getbits(buf, n++, 1);
1227 cts_flag = getbits(buf, n++, 1);
1229 if(sl->instant_bitrate_len)
1230 ib_flag = getbits(buf, n++, 1);
1231 if(packet_len * 8 <= n+8)
1232 return -1;
1233 if(dts_flag && (sl->ts_len > 0))
1235 n += sl->ts_len;
1236 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "DTS: %d bits\n", sl->ts_len);
1238 if(packet_len * 8 <= n+8)
1239 return -1;
1240 if(cts_flag && (sl->ts_len > 0))
1242 int i = 0, m;
1244 while(i < sl->ts_len)
1246 m = FFMIN(8, sl->ts_len - i);
1247 v |= getbits(buf, n, m);
1248 if(sl->ts_len - i > 8)
1249 v <<= 8;
1250 i += m;
1251 n += m;
1252 if(packet_len * 8 <= n+8)
1253 return -1;
1256 pes_es->pts = (double) v / (double) sl->ts_resolution;
1257 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "CTS: %d bits, value: %"PRIu64"/%d = %.3f\n", sl->ts_len, v, sl->ts_resolution, pes_es->pts);
1261 i = 0;
1262 pl_size = 0;
1263 while(i < sl->au_len)
1265 m = FFMIN(8, sl->au_len - i);
1266 pl_size |= getbits(buf, n, m);
1267 if(sl->au_len - i > 8)
1268 pl_size <<= 8;
1269 i += m;
1270 n += m;
1271 if(packet_len * 8 <= n+8)
1272 return -1;
1274 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "AU_LEN: %u (%d bits)\n", pl_size, sl->au_len);
1275 if(ib_flag)
1276 n += sl->instant_bitrate_len;
1279 m = (n+7)/8;
1280 if(0 < pl_size && pl_size < pes_es->payload_size)
1281 pes_es->payload_size = pl_size;
1283 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",
1284 n, m, pes_es->payload_size, pl_size, (int) rap_flag, (int) sl->random_accesspoint_only);
1286 return m;
1289 //this function parses the extension fields in the PES header and returns the substream_id, or -1 in case of errors
1290 static int parse_pes_extension_fields(unsigned char *p, int pkt_len)
1292 int skip;
1293 unsigned char flags;
1295 if(!(p[7] & 0x1)) //no extension_field
1296 return -1;
1297 skip = 9;
1298 if(p[7] & 0x80)
1300 skip += 5;
1301 if(p[7] & 0x40)
1302 skip += 5;
1304 if(p[7] & 0x20) //escr_flag
1305 skip += 6;
1306 if(p[7] & 0x10) //es_rate_flag
1307 skip += 3;
1308 if(p[7] & 0x08)//dsm_trick_mode is unsupported, skip
1310 skip = 0;//don't let's parse the extension fields
1312 if(p[7] & 0x04) //additional_copy_info
1313 skip += 1;
1314 if(p[7] & 0x02) //pes_crc_flag
1315 skip += 2;
1316 if(skip >= pkt_len) //too few bytes
1317 return -1;
1318 flags = p[skip];
1319 skip++;
1320 if(flags & 0x80) //pes_private_data_flag
1321 skip += 16;
1322 if(skip >= pkt_len)
1323 return -1;
1324 if(flags & 0x40) //pack_header_field_flag
1326 unsigned char l = p[skip];
1327 skip += l;
1329 if(flags & 0x20) //program_packet_sequence_counter
1330 skip += 2;
1331 if(flags & 0x10) //p_std
1332 skip += 2;
1333 if(skip >= pkt_len)
1334 return -1;
1335 if(flags & 0x01) //finally the long desired pes_extension2
1337 unsigned char l = p[skip]; //ext2 flag+len
1338 skip++;
1339 if((l == 0x81) && (skip < pkt_len))
1341 int ssid = p[skip];
1342 mp_msg(MSGT_IDENTIFY, MSGL_V, "SUBSTREAM_ID=%d (0x%02X)\n", ssid, ssid);
1343 return ssid;
1347 return -1;
1350 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)
1352 unsigned char *p;
1353 uint32_t header_len;
1354 int64_t pts;
1355 uint32_t stream_id;
1356 uint32_t pkt_len, pes_is_aligned;
1358 //Here we are always at the start of a PES packet
1359 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2(%p, %d): \n", buf, (uint32_t) packet_len);
1361 if(packet_len == 0 || packet_len > 184)
1363 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2, BUFFER LEN IS TOO SMALL OR TOO BIG: %d EXIT\n", packet_len);
1364 return 0;
1367 p = buf;
1368 pkt_len = packet_len;
1371 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: HEADER %02x %02x %02x %02x\n", p[0], p[1], p[2], p[3]);
1372 if (p[0] || p[1] || (p[2] != 1))
1374 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: error HEADER %02x %02x %02x (should be 0x000001) \n", p[0], p[1], p[2]);
1375 return 0 ;
1378 packet_len -= 6;
1379 if(packet_len==0)
1381 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: packet too short: %d, exit\n", packet_len);
1382 return 0;
1385 es->payload_size = (p[4] << 8 | p[5]);
1386 pes_is_aligned = (p[6] & 4);
1388 stream_id = p[3];
1391 if (p[7] & 0x80)
1392 { /* pts available */
1393 pts = (int64_t)(p[9] & 0x0E) << 29 ;
1394 pts |= p[10] << 22 ;
1395 pts |= (p[11] & 0xFE) << 14 ;
1396 pts |= p[12] << 7 ;
1397 pts |= (p[13] & 0xFE) >> 1 ;
1399 es->pts = pts / 90000.0;
1401 else
1402 es->pts = 0.0;
1405 header_len = p[8];
1408 if (header_len + 9 > pkt_len) //9 are the bytes read up to the header_length field
1410 mp_msg(MSGT_DEMUX, MSGL_DBG2, "demux_ts: illegal value for PES_header_data_length (0x%02x)\n", header_len);
1411 return 0;
1414 if(stream_id==0xfd)
1416 int ssid = parse_pes_extension_fields(p, pkt_len);
1417 if((audio_substream_id!=-1) && (ssid != audio_substream_id))
1418 return 0;
1419 if(ssid == 0x72 && type_from_pmt != AUDIO_DTS)
1420 es->type = type_from_pmt = AUDIO_TRUEHD;
1423 p += header_len + 9;
1424 packet_len -= header_len + 3;
1426 if(es->payload_size)
1427 es->payload_size -= header_len + 3;
1430 es->is_synced = 1; //only for SL streams we have to make sure it's really true, see below
1431 if (stream_id == 0xbd)
1433 mp_msg(MSGT_DEMUX, MSGL_DBG3, "pes_parse2: audio buf = %02X %02X %02X %02X %02X %02X %02X %02X, 80: %d\n",
1434 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[0] & 0x80);
1438 * we check the descriptor tag first because some stations
1439 * do not include any of the A52 header info in their audio tracks
1440 * these "raw" streams may begin with a byte that looks like a stream type.
1445 (type_from_pmt == AUDIO_A52) || /* A52 - raw */
1446 (p[0] == 0x0B && p[1] == 0x77) /* A52 - syncword */
1449 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 RAW OR SYNCWORD\n");
1450 es->start = p;
1451 es->size = packet_len;
1452 es->type = AUDIO_A52;
1453 es->payload_size -= packet_len;
1455 return 1;
1457 /* SPU SUBS */
1458 else if(type_from_pmt == SPU_DVB ||
1459 ((p[0] == 0x20) && pes_is_aligned)) // && p[1] == 0x00))
1461 es->start = p;
1462 es->size = packet_len;
1463 es->type = SPU_DVB;
1464 es->payload_size -= packet_len;
1466 return 1;
1468 else if (pes_is_aligned && ((p[0] & 0xE0) == 0x20)) //SPU_DVD
1470 //DVD SUBS
1471 es->start = p+1;
1472 es->size = packet_len-1;
1473 es->type = SPU_DVD;
1474 es->payload_size -= packet_len;
1476 return 1;
1478 else if (pes_is_aligned && (p[0] & 0xF8) == 0x80)
1480 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 WITH HEADER\n");
1481 es->start = p+4;
1482 es->size = packet_len - 4;
1483 es->type = AUDIO_A52;
1484 es->payload_size -= packet_len;
1486 return 1;
1488 else if (pes_is_aligned && ((p[0]&0xf0) == 0xa0))
1490 int pcm_offset;
1492 for (pcm_offset=0; ++pcm_offset < packet_len-1 ; )
1494 if (p[pcm_offset] == 0x01 && p[pcm_offset+1] == 0x80)
1495 { /* START */
1496 pcm_offset += 2;
1497 break;
1501 es->start = p + pcm_offset;
1502 es->size = packet_len - pcm_offset;
1503 es->type = AUDIO_LPCM_BE;
1504 es->payload_size -= packet_len;
1506 return 1;
1508 else
1510 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PES_PRIVATE1\n");
1511 es->start = p;
1512 es->size = packet_len;
1513 es->type = (type_from_pmt == UNKNOWN ? PES_PRIVATE1 : type_from_pmt);
1514 es->payload_size -= packet_len;
1516 return 1;
1519 else if(((stream_id >= 0xe0) && (stream_id <= 0xef)) || (stream_id == 0xfd && type_from_pmt != UNKNOWN))
1521 es->start = p;
1522 es->size = packet_len;
1523 if(type_from_pmt != UNKNOWN)
1524 es->type = type_from_pmt;
1525 else
1526 es->type = VIDEO_MPEG2;
1527 if(es->payload_size)
1528 es->payload_size -= packet_len;
1530 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: M2V size %d\n", es->size);
1531 return 1;
1533 else if ((stream_id == 0xfa))
1535 int l;
1537 es->is_synced = 0;
1538 if(type_from_pmt != UNKNOWN) //MP4 A/V or SL
1540 es->start = p;
1541 es->size = packet_len;
1542 es->type = type_from_pmt;
1544 if(type_from_pmt == SL_PES_STREAM)
1546 //if(pes_is_aligned)
1548 l = mp4_parse_sl_packet(pmt, p, packet_len, pid, es);
1549 mp_msg(MSGT_DEMUX, MSGL_DBG2, "L=%d, TYPE=%x\n", l, type_from_pmt);
1550 if(l < 0)
1552 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: couldn't parse SL header, passing along full PES payload\n");
1553 l = 0;
1557 es->start += l;
1558 es->size -= l;
1561 if(es->payload_size)
1562 es->payload_size -= packet_len;
1563 return 1;
1566 else if ((stream_id & 0xe0) == 0xc0)
1568 es->start = p;
1569 es->size = packet_len;
1571 if(type_from_pmt != UNKNOWN)
1572 es->type = type_from_pmt;
1573 else
1574 es->type = AUDIO_MP2;
1576 es->payload_size -= packet_len;
1578 return 1;
1580 else if (type_from_pmt != -1) //as a last resort here we trust the PMT, if present
1582 es->start = p;
1583 es->size = packet_len;
1584 es->type = type_from_pmt;
1585 es->payload_size -= packet_len;
1587 return 1;
1589 else
1591 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: unknown packet, id: %x\n", stream_id);
1594 es->is_synced = 0;
1595 return 0;
1601 static int ts_sync(stream_t *stream)
1603 int c=0;
1605 mp_msg(MSGT_DEMUX, MSGL_DBG3, "TS_SYNC \n");
1607 while(((c=stream_read_char(stream)) != 0x47) && ! stream->eof);
1609 if(c == 0x47)
1610 return c;
1611 else
1612 return 0;
1616 static void ts_dump_streams(ts_priv_t *priv)
1618 int i;
1620 for(i = 0; i < 3; i++)
1622 if((priv->fifo[i].pack != NULL) && (priv->fifo[i].offset != 0))
1624 resize_demux_packet(priv->fifo[i].pack, priv->fifo[i].offset);
1625 ds_add_packet(priv->fifo[i].ds, priv->fifo[i].pack);
1626 priv->fifo[i].offset = 0;
1627 priv->fifo[i].pack = NULL;
1633 static inline int32_t prog_idx_in_pat(ts_priv_t *priv, uint16_t progid)
1635 int x;
1637 if(priv->pat.progs == NULL)
1638 return -1;
1640 for(x = 0; x < priv->pat.progs_cnt; x++)
1642 if(priv->pat.progs[x].id == progid)
1643 return x;
1646 return -1;
1650 static inline int32_t prog_id_in_pat(ts_priv_t *priv, uint16_t pid)
1652 int x;
1654 if(priv->pat.progs == NULL)
1655 return -1;
1657 for(x = 0; x < priv->pat.progs_cnt; x++)
1659 if(priv->pat.progs[x].pmt_pid == pid)
1660 return priv->pat.progs[x].id;
1663 return -1;
1666 static int collect_section(ts_section_t *section, int is_start, unsigned char *buff, int size)
1668 uint8_t *ptr;
1669 uint16_t tlen;
1670 int skip, tid;
1672 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, start: %d, size: %d, collected: %d\n", is_start, size, section->buffer_len);
1673 if(! is_start && !section->buffer_len)
1674 return 0;
1676 if(is_start)
1678 if(! section->buffer)
1680 section->buffer = malloc(4096 + 256);
1681 if(section->buffer == NULL)
1682 return 0;
1684 section->buffer_len = 0;
1687 if(size + section->buffer_len > 4096+256)
1689 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, excessive len: %d + %d\n", section->buffer_len, size);
1690 return 0;
1693 memcpy(&(section->buffer[section->buffer_len]), buff, size);
1694 section->buffer_len += size;
1696 if(section->buffer_len < 3)
1697 return 0;
1699 skip = section->buffer[0];
1700 if(skip + 4 > section->buffer_len)
1701 return 0;
1703 ptr = &(section->buffer[skip + 1]);
1704 tid = ptr[0];
1705 tlen = ((ptr[1] & 0x0f) << 8) | ptr[2];
1706 mp_msg(MSGT_DEMUX, MSGL_V, "SKIP: %d+1, TID: %d, TLEN: %d, COLLECTED: %d\n", skip, tid, tlen, section->buffer_len);
1707 if(section->buffer_len < (skip+1+3+tlen))
1709 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DATA IS NOT ENOUGH, NEXT TIME\n");
1710 return 0;
1713 return skip+1;
1716 static int parse_pat(ts_priv_t * priv, int is_start, unsigned char *buff, int size)
1718 int skip;
1719 unsigned char *ptr;
1720 unsigned char *base;
1721 int entries, i;
1722 uint16_t progid;
1723 struct pat_progs_t *tmp;
1724 ts_section_t *section;
1726 section = &(priv->pat.section);
1727 skip = collect_section(section, is_start, buff, size);
1728 if(! skip)
1729 return 0;
1731 ptr = &(section->buffer[skip]);
1732 //PARSING
1733 priv->pat.table_id = ptr[0];
1734 if(priv->pat.table_id != 0)
1735 return 0;
1736 priv->pat.ssi = (ptr[1] >> 7) & 0x1;
1737 priv->pat.curr_next = ptr[5] & 0x01;
1738 priv->pat.ts_id = (ptr[3] << 8 ) | ptr[4];
1739 priv->pat.version_number = (ptr[5] >> 1) & 0x1F;
1740 priv->pat.section_length = ((ptr[1] & 0x03) << 8 ) | ptr[2];
1741 priv->pat.section_number = ptr[6];
1742 priv->pat.last_section_number = ptr[7];
1744 //check_crc32(0xFFFFFFFFL, ptr, priv->pat.buffer_len - 4, &ptr[priv->pat.buffer_len - 4]);
1745 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);
1747 entries = (int) (priv->pat.section_length - 9) / 4; //entries per section
1749 for(i=0; i < entries; i++)
1751 int32_t idx;
1752 base = &ptr[8 + i*4];
1753 progid = (base[0] << 8) | base[1];
1755 if((idx = prog_idx_in_pat(priv, progid)) == -1)
1757 int sz = sizeof(struct pat_progs_t) * (priv->pat.progs_cnt+1);
1758 tmp = realloc_struct(priv->pat.progs, priv->pat.progs_cnt+1, sizeof(struct pat_progs_t));
1759 if(tmp == NULL)
1761 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PAT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
1762 break;
1764 priv->pat.progs = tmp;
1765 idx = priv->pat.progs_cnt;
1766 priv->pat.progs_cnt++;
1769 priv->pat.progs[idx].id = progid;
1770 priv->pat.progs[idx].pmt_pid = ((base[2] & 0x1F) << 8) | base[3];
1771 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);
1772 mp_msg(MSGT_IDENTIFY, MSGL_V, "PROGRAM_ID=%d (0x%02X), PMT_PID: %d(0x%02X)\n",
1773 progid, progid, priv->pat.progs[idx].pmt_pid, priv->pat.progs[idx].pmt_pid);
1776 return 1;
1780 static inline int32_t es_pid_in_pmt(pmt_t * pmt, uint16_t pid)
1782 uint16_t i;
1784 if(pmt == NULL)
1785 return -1;
1787 if(pmt->es == NULL)
1788 return -1;
1790 for(i = 0; i < pmt->es_cnt; i++)
1792 if(pmt->es[i].pid == pid)
1793 return (int32_t) i;
1796 return -1;
1800 static uint16_t get_mp4_desc_len(uint8_t *buf, int *len)
1802 //uint16_t i = 0, size = 0;
1803 int i = 0, j, size = 0;
1805 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PARSE_MP4_DESC_LEN(%d), bytes: ", *len);
1806 j = FFMIN(*len, 4);
1807 while(i < j)
1809 mp_msg(MSGT_DEMUX, MSGL_DBG2, " %x ", buf[i]);
1810 size |= (buf[i] & 0x7f);
1811 if(!(buf[i] & 0x80))
1812 break;
1813 size <<= 7;
1814 i++;
1816 mp_msg(MSGT_DEMUX, MSGL_DBG2, ", SIZE=%d\n", size);
1818 *len = i+1;
1819 return size;
1823 static uint16_t parse_mp4_slconfig_descriptor(uint8_t *buf, int len, void *elem)
1825 int i = 0;
1826 mp4_es_descr_t *es;
1827 mp4_sl_config_t *sl;
1829 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_SLCONFIG_DESCRIPTOR(%d)\n", len);
1830 es = (mp4_es_descr_t *) elem;
1831 if(!es)
1833 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1834 return len;
1836 sl = &(es->sl);
1838 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;
1839 sl->ocr = sl->dts = sl->cts = 0;
1841 if(buf[0] == 0)
1843 i++;
1844 sl->flags = buf[i];
1845 i++;
1846 sl->ts_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1847 i += 4;
1848 sl->ocr_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1849 i += 4;
1850 sl->ts_len = buf[i];
1851 i++;
1852 sl->ocr_len = buf[i];
1853 i++;
1854 sl->au_len = buf[i];
1855 i++;
1856 sl->instant_bitrate_len = buf[i];
1857 i++;
1858 sl->degr_len = (buf[i] >> 4) & 0x0f;
1859 sl->au_seqnum_len = ((buf[i] & 0x0f) << 1) | ((buf[i+1] >> 7) & 0x01);
1860 i++;
1861 sl->packet_seqnum_len = ((buf[i] >> 2) & 0x1f);
1862 i++;
1865 else if(buf[0] == 1)
1867 sl->flags = 0;
1868 sl->ts_resolution = 1000;
1869 sl->ts_len = 32;
1870 i++;
1872 else if(buf[0] == 2)
1874 sl->flags = 4;
1875 i++;
1877 else
1879 sl->flags = 0;
1880 i++;
1883 sl->au_start = (sl->flags >> 7) & 0x1;
1884 sl->au_end = (sl->flags >> 6) & 0x1;
1885 sl->random_accesspoint = (sl->flags >> 5) & 0x1;
1886 sl->random_accesspoint_only = (sl->flags >> 4) & 0x1;
1887 sl->padding = (sl->flags >> 3) & 0x1;
1888 sl->use_ts = (sl->flags >> 2) & 0x1;
1889 sl->idle = (sl->flags >> 1) & 0x1;
1890 sl->duration = sl->flags & 0x1;
1892 if(sl->duration)
1894 sl->timescale = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1895 i += 4;
1896 sl->au_duration = (buf[i] << 8) | buf[i+1];
1897 i += 2;
1898 sl->cts_duration = (buf[i] << 8) | buf[i+1];
1899 i += 2;
1901 else //no support for fixed durations atm
1902 sl->timescale = sl->au_duration = sl->cts_duration = 0;
1904 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",
1905 len, buf[0], sl->flags, sl->use_ts, sl->ts_len, sl->timescale, (uint64_t) sl->dts, (uint64_t) sl->cts);
1907 return len;
1910 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem);
1912 static uint16_t parse_mp4_decoder_config_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
1914 int i = 0, j;
1915 mp4_es_descr_t *es;
1916 mp4_decoder_config_t *dec;
1918 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_CONFIG_DESCRIPTOR(%d)\n", len);
1919 es = (mp4_es_descr_t *) elem;
1920 if(!es)
1922 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1923 return len;
1925 dec = (mp4_decoder_config_t*) &(es->decoder);
1927 dec->object_type = buf[i];
1928 dec->stream_type = (buf[i+1]>>2) & 0x3f;
1930 if(dec->object_type == 1 && dec->stream_type == 1)
1932 dec->object_type = MP4_OD;
1933 dec->stream_type = MP4_OD;
1935 else if(dec->stream_type == 4)
1937 if(dec->object_type == 0x6a)
1938 dec->object_type = VIDEO_MPEG1;
1939 if(dec->object_type >= 0x60 && dec->object_type <= 0x65)
1940 dec->object_type = VIDEO_MPEG2;
1941 else if(dec->object_type == 0x20)
1942 dec->object_type = VIDEO_MPEG4;
1943 else if(dec->object_type == 0x21)
1944 dec->object_type = VIDEO_AVC;
1945 /*else if(dec->object_type == 0x22)
1946 fprintf(stderr, "TYPE 0x22\n");*/
1947 else dec->object_type = UNKNOWN;
1949 else if(dec->stream_type == 5)
1951 if(dec->object_type == 0x40)
1952 dec->object_type = AUDIO_AAC;
1953 else if(dec->object_type == 0x6b)
1954 dec->object_type = AUDIO_MP2;
1955 else if(dec->object_type >= 0x66 && dec->object_type <= 0x69)
1956 dec->object_type = AUDIO_MP2;
1957 else
1958 dec->object_type = UNKNOWN;
1960 else
1961 dec->object_type = dec->stream_type = UNKNOWN;
1963 if(dec->object_type != UNKNOWN)
1965 //update the type of the current stream
1966 for(j = 0; j < pmt->es_cnt; j++)
1968 if(pmt->es[j].mp4_es_id == es->id)
1970 pmt->es[j].type = SL_PES_STREAM;
1975 if(len > 13)
1976 parse_mp4_descriptors(pmt, &buf[13], len-13, dec);
1978 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);
1980 return len;
1983 static uint16_t parse_mp4_decoder_specific_descriptor(uint8_t *buf, int len, void *elem)
1985 int i;
1986 mp4_decoder_config_t *dec;
1988 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_SPECIFIC_DESCRIPTOR(%d)\n", len);
1989 dec = (mp4_decoder_config_t *) elem;
1990 if(!dec)
1992 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1993 return len;
1996 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4 SPECIFIC INFO BYTES: \n");
1997 for(i=0; i<len; i++)
1998 mp_msg(MSGT_DEMUX, MSGL_DBG2, "%02x ", buf[i]);
1999 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\n");
2001 if(len > MAX_EXTRADATA_SIZE)
2003 mp_msg(MSGT_DEMUX, MSGL_ERR, "DEMUX_TS, EXTRADATA SUSPICIOUSLY BIG: %d, REFUSED\r\n", len);
2004 return len;
2006 memcpy(dec->buf, buf, len);
2007 dec->buf_size = len;
2009 return len;
2012 static uint16_t parse_mp4_es_descriptor(pmt_t *pmt, uint8_t *buf, int len)
2014 int i = 0, j = 0, k, found;
2015 uint8_t flag;
2016 mp4_es_descr_t es, *target_es = NULL, *tmp;
2018 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4ES: len=%d\n", len);
2019 memset(&es, 0, sizeof(mp4_es_descr_t));
2020 while(i < len)
2022 es.id = (buf[i] << 8) | buf[i+1];
2023 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_ID: %d\n", es.id);
2024 i += 2;
2025 flag = buf[i];
2026 i++;
2027 if(flag & 0x80)
2028 i += 2;
2029 if(flag & 0x40)
2030 i += buf[i]+1;
2031 if(flag & 0x20) //OCR, maybe we need it
2032 i += 2;
2034 j = parse_mp4_descriptors(pmt, &buf[i], len-i, &es);
2035 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);
2036 if(es.decoder.object_type != UNKNOWN && es.decoder.stream_type != UNKNOWN)
2038 found = 0;
2039 //search this ES_ID if we already have it
2040 for(k=0; k < pmt->mp4es_cnt; k++)
2042 if(pmt->mp4es[k].id == es.id)
2044 target_es = &(pmt->mp4es[k]);
2045 found = 1;
2049 if(! found)
2051 tmp = realloc_struct(pmt->mp4es, pmt->mp4es_cnt+1, sizeof(mp4_es_descr_t));
2052 if(tmp == NULL)
2054 fprintf(stderr, "CAN'T REALLOC MP4_ES_DESCR\n");
2055 continue;
2057 pmt->mp4es = tmp;
2058 target_es = &(pmt->mp4es[pmt->mp4es_cnt]);
2059 pmt->mp4es_cnt++;
2061 memcpy(target_es, &es, sizeof(mp4_es_descr_t));
2062 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_CNT: %d, ID=%d\n", pmt->mp4es_cnt, target_es->id);
2065 i += j;
2068 return len;
2071 static void parse_mp4_object_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2073 int i, j = 0, id;
2075 i=0;
2076 id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2077 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_OBJECT_DESCRIPTOR: len=%d, OD_ID=%d\n", len, id);
2078 if(buf[1] & 0x20)
2080 i += buf[2] + 1; //url
2081 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2083 else
2085 i = 2;
2087 while(i < len)
2089 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2090 mp_msg(MSGT_DEMUX, MSGL_V, "OBJD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2091 i += j;
2097 static void parse_mp4_iod(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2099 int i, j = 0;
2100 mp4_od_t *iod = &(pmt->iod);
2102 iod->id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2103 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_IOD: len=%d, IOD_ID=%d\n", len, iod->id);
2104 i = 2;
2105 if(buf[1] & 0x20)
2107 i += buf[2] + 1; //url
2108 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2110 else
2112 i = 7;
2113 while(i < len)
2115 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2116 mp_msg(MSGT_DEMUX, MSGL_V, "IOD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2117 i += j;
2122 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2124 int tag, descr_len, i = 0, j = 0;
2126 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DESCRIPTORS, len=%d\n", len);
2127 if(! len)
2128 return len;
2130 while(i < len)
2132 tag = buf[i];
2133 j = len - i -1;
2134 descr_len = get_mp4_desc_len(&(buf[i+1]), &j);
2135 mp_msg(MSGT_DEMUX, MSGL_V, "TAG=%d (0x%x), DESCR_len=%d, len=%d, j=%d\n", tag, tag, descr_len, len, j);
2136 if(descr_len > len - j+1)
2138 mp_msg(MSGT_DEMUX, MSGL_V, "descriptor is too long, exit\n");
2139 return len;
2141 i += j+1;
2143 switch(tag)
2145 case 0x1:
2146 parse_mp4_object_descriptor(pmt, &(buf[i]), descr_len, elem);
2147 break;
2148 case 0x2:
2149 parse_mp4_iod(pmt, &(buf[i]), descr_len, elem);
2150 break;
2151 case 0x3:
2152 parse_mp4_es_descriptor(pmt, &(buf[i]), descr_len);
2153 break;
2154 case 0x4:
2155 parse_mp4_decoder_config_descriptor(pmt, &buf[i], descr_len, elem);
2156 break;
2157 case 0x05:
2158 parse_mp4_decoder_specific_descriptor(&buf[i], descr_len, elem);
2159 break;
2160 case 0x6:
2161 parse_mp4_slconfig_descriptor(&buf[i], descr_len, elem);
2162 break;
2163 default:
2164 mp_msg(MSGT_DEMUX, MSGL_V, "Unsupported mp4 descriptor 0x%x\n", tag);
2166 i += descr_len;
2169 return len;
2172 static ES_stream_t *new_pid(ts_priv_t *priv, int pid)
2174 ES_stream_t *tss;
2176 tss = malloc(sizeof(ES_stream_t));
2177 if(! tss)
2178 return NULL;
2179 memset(tss, 0, sizeof(ES_stream_t));
2180 tss->pid = pid;
2181 tss->last_cc = -1;
2182 tss->type = UNKNOWN;
2183 tss->subtype = UNKNOWN;
2184 tss->is_synced = 0;
2185 tss->extradata = NULL;
2186 tss->extradata_alloc = tss->extradata_len = 0;
2187 priv->ts.pids[pid] = tss;
2189 return tss;
2193 static int parse_program_descriptors(pmt_t *pmt, uint8_t *buf, uint16_t len)
2195 uint16_t i = 0, k, olen = len;
2197 while(len > 0)
2199 mp_msg(MSGT_DEMUX, MSGL_V, "PROG DESCR, TAG=%x, LEN=%d(%x)\n", buf[i], buf[i+1], buf[i+1]);
2200 if(buf[i+1] > len-2)
2202 mp_msg(MSGT_DEMUX, MSGL_V, "ERROR, descriptor len is too long, skipping\n");
2203 return olen;
2206 if(buf[i] == 0x1d)
2208 if(buf[i+3] == 2) //buggy versions of vlc muxer make this non-standard mess (missing iod_scope)
2209 k = 3;
2210 else
2211 k = 4; //this is standard compliant
2212 parse_mp4_descriptors(pmt, &buf[i+k], (int) buf[i+1]-(k-2), NULL);
2215 len -= 2 + buf[i+1];
2218 return olen;
2221 static int parse_descriptors(struct pmt_es_t *es, uint8_t *ptr)
2223 int j, descr_len, len;
2225 j = 0;
2226 len = es->descr_length;
2227 while(len > 2)
2229 descr_len = ptr[j+1];
2230 mp_msg(MSGT_DEMUX, MSGL_V, "...descr id: 0x%x, len=%d\n", ptr[j], descr_len);
2231 if(descr_len > len)
2233 mp_msg(MSGT_DEMUX, MSGL_ERR, "INVALID DESCR LEN for tag %02x: %d vs %d max, EXIT LOOP\n", ptr[j], descr_len, len);
2234 return -1;
2238 if(ptr[j] == 0x6a || ptr[j] == 0x7a) //A52 Descriptor
2240 if(es->type == 0x6)
2242 es->type = AUDIO_A52;
2243 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB A52 Descriptor\n");
2246 else if(ptr[j] == 0x7b) //DVB DTS Descriptor
2248 if(es->type == 0x6)
2250 es->type = AUDIO_DTS;
2251 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB DTS Descriptor\n");
2254 else if(ptr[j] == 0x56) // Teletext
2256 if(descr_len >= 5) {
2257 memcpy(es->lang, ptr+2, 3);
2258 es->lang[3] = 0;
2260 es->type = SPU_TELETEXT;
2262 else if(ptr[j] == 0x59) //Subtitling Descriptor
2264 uint8_t subtype;
2266 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Subtitling Descriptor\n");
2267 if(descr_len < 8)
2269 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Descriptor length too short for DVB Subtitle Descriptor: %d, SKIPPING\n", descr_len);
2271 else
2273 memcpy(es->lang, &ptr[j+2], 3);
2274 es->lang[3] = 0;
2275 subtype = ptr[j+5];
2277 (subtype >= 0x10 && subtype <= 0x13) ||
2278 (subtype >= 0x20 && subtype <= 0x23)
2281 es->type = SPU_DVB;
2282 //page parameters: compo page 2 bytes, ancillary page 2 bytes
2284 else
2285 es->type = UNKNOWN;
2288 else if(ptr[j] == 0x50) //Component Descriptor
2290 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Component Descriptor\n");
2291 memcpy(es->lang, &ptr[j+5], 3);
2292 es->lang[3] = 0;
2294 else if(ptr[j] == 0xa) //Language Descriptor
2296 memcpy(es->lang, &ptr[j+2], 3);
2297 es->lang[3] = 0;
2298 mp_msg(MSGT_DEMUX, MSGL_V, "Language Descriptor: %s\n", es->lang);
2300 else if(ptr[j] == 0x5) //Registration Descriptor (looks like e fourCC :) )
2302 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor\n");
2303 if(descr_len < 4)
2305 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor length too short: %d, SKIPPING\n", descr_len);
2307 else
2309 char *d;
2310 memcpy(es->format_descriptor, &ptr[j+2], 4);
2311 es->format_descriptor[4] = 0;
2313 d = &ptr[j+2];
2314 if(d[0] == 'A' && d[1] == 'C' && d[2] == '-' && d[3] == '3')
2316 es->type = AUDIO_A52;
2318 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '1')
2320 es->type = AUDIO_DTS;
2322 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '2')
2324 es->type = AUDIO_DTS;
2326 else if(d[0] == 'V' && d[1] == 'C' && d[2] == '-' && d[3] == '1')
2328 es->type = VIDEO_VC1;
2330 else
2331 es->type = UNKNOWN;
2332 mp_msg(MSGT_DEMUX, MSGL_DBG2, "FORMAT %s\n", es->format_descriptor);
2335 else if(ptr[j] == 0x1e)
2337 es->mp4_es_id = (ptr[j+2] << 8) | ptr[j+3];
2338 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);
2340 else
2341 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Unknown descriptor 0x%x, SKIPPING\n", ptr[j]);
2343 len -= 2 + descr_len;
2344 j += 2 + descr_len;
2347 return 1;
2350 static int parse_sl_section(pmt_t *pmt, ts_section_t *section, int is_start, unsigned char *buff, int size)
2352 int tid, len, skip;
2353 uint8_t *ptr;
2354 skip = collect_section(section, is_start, buff, size);
2355 if(! skip)
2356 return 0;
2358 ptr = &(section->buffer[skip]);
2359 tid = ptr[0];
2360 len = ((ptr[1] & 0x0f) << 8) | ptr[2];
2361 mp_msg(MSGT_DEMUX, MSGL_V, "TABLEID: %d (av. %d), skip=%d, LEN: %d\n", tid, section->buffer_len, skip, len);
2362 if(len > 4093 || section->buffer_len < len || tid != 5)
2364 mp_msg(MSGT_DEMUX, MSGL_V, "SECTION TOO LARGE or wrong section type, EXIT\n");
2365 return 0;
2368 if(! (ptr[5] & 1))
2369 return 0;
2371 //8 is the current position, len - 9 is the amount of data available
2372 parse_mp4_descriptors(pmt, &ptr[8], len - 9, NULL);
2374 return 1;
2377 static int parse_pmt(ts_priv_t * priv, uint16_t progid, uint16_t pid, int is_start, unsigned char *buff, int size)
2379 unsigned char *base, *es_base;
2380 pmt_t *pmt;
2381 int32_t idx, es_count, section_bytes;
2382 uint8_t m=0;
2383 int skip;
2384 pmt_t *tmp;
2385 struct pmt_es_t *tmp_es;
2386 ts_section_t *section;
2387 ES_stream_t *tss;
2389 idx = progid_idx_in_pmt(priv, progid);
2391 if(idx == -1)
2393 int sz = (priv->pmt_cnt + 1) * sizeof(pmt_t);
2394 tmp = realloc_struct(priv->pmt, priv->pmt_cnt + 1, sizeof(pmt_t));
2395 if(tmp == NULL)
2397 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
2398 return 0;
2400 priv->pmt = tmp;
2401 idx = priv->pmt_cnt;
2402 memset(&(priv->pmt[idx]), 0, sizeof(pmt_t));
2403 priv->pmt_cnt++;
2404 priv->pmt[idx].progid = progid;
2407 pmt = &(priv->pmt[idx]);
2409 section = &(pmt->section);
2410 skip = collect_section(section, is_start, buff, size);
2411 if(! skip)
2412 return 0;
2414 base = &(section->buffer[skip]);
2416 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",
2417 progid, pmt->section.buffer_len, is_start, pid, size, m, pmt->es_cnt, idx, pmt);
2419 pmt->table_id = base[0];
2420 if(pmt->table_id != 2)
2421 return -1;
2422 pmt->ssi = base[1] & 0x80;
2423 pmt->section_length = (((base[1] & 0xf) << 8 ) | base[2]);
2424 pmt->version_number = (base[5] >> 1) & 0x1f;
2425 pmt->curr_next = (base[5] & 1);
2426 pmt->section_number = base[6];
2427 pmt->last_section_number = base[7];
2428 pmt->PCR_PID = ((base[8] & 0x1f) << 8 ) | base[9];
2429 pmt->prog_descr_length = ((base[10] & 0xf) << 8 ) | base[11];
2430 if(pmt->prog_descr_length > pmt->section_length - 9)
2432 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, INVALID PROG_DESCR LENGTH (%d vs %d)\n", pmt->prog_descr_length, pmt->section_length - 9);
2433 return -1;
2436 if(pmt->prog_descr_length)
2437 parse_program_descriptors(pmt, &base[12], pmt->prog_descr_length);
2439 es_base = &base[12 + pmt->prog_descr_length]; //the beginning of th ES loop
2441 section_bytes= pmt->section_length - 13 - pmt->prog_descr_length;
2442 es_count = 0;
2444 while(section_bytes >= 5)
2446 int es_pid, es_type;
2448 es_type = es_base[0];
2449 es_pid = ((es_base[1] & 0x1f) << 8) | es_base[2];
2451 idx = es_pid_in_pmt(pmt, es_pid);
2452 if(idx == -1)
2454 int sz = sizeof(struct pmt_es_t) * (pmt->es_cnt + 1);
2455 tmp_es = realloc_struct(pmt->es, pmt->es_cnt + 1, sizeof(struct pmt_es_t));
2456 if(tmp_es == NULL)
2458 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT, COULDN'T ALLOCATE %d bytes for PMT_ES\n", sz);
2459 continue;
2461 pmt->es = tmp_es;
2462 idx = pmt->es_cnt;
2463 memset(&(pmt->es[idx]), 0, sizeof(struct pmt_es_t));
2464 pmt->es_cnt++;
2467 pmt->es[idx].descr_length = ((es_base[3] & 0xf) << 8) | es_base[4];
2470 if(pmt->es[idx].descr_length > section_bytes - 5)
2472 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, ES_DESCR_LENGTH TOO LARGE %d > %d, EXIT\n",
2473 pmt->es[idx].descr_length, section_bytes - 5);
2474 return -1;
2478 pmt->es[idx].pid = es_pid;
2479 if(es_type != 0x6)
2480 pmt->es[idx].type = UNKNOWN;
2481 else
2482 pmt->es[idx].type = es_type;
2484 parse_descriptors(&pmt->es[idx], &es_base[5]);
2486 switch(es_type)
2488 case 1:
2489 pmt->es[idx].type = VIDEO_MPEG1;
2490 break;
2491 case 2:
2492 pmt->es[idx].type = VIDEO_MPEG2;
2493 break;
2494 case 3:
2495 case 4:
2496 pmt->es[idx].type = AUDIO_MP2;
2497 break;
2498 case 6:
2499 if(pmt->es[idx].type == 0x6) //this could have been ovrwritten by parse_descriptors
2500 pmt->es[idx].type = UNKNOWN;
2501 break;
2502 case 0x10:
2503 pmt->es[idx].type = VIDEO_MPEG4;
2504 break;
2505 case 0x0f:
2506 case 0x11:
2507 pmt->es[idx].type = AUDIO_AAC;
2508 break;
2509 case 0x1b:
2510 pmt->es[idx].type = VIDEO_H264;
2511 break;
2512 case 0x12:
2513 pmt->es[idx].type = SL_PES_STREAM;
2514 break;
2515 case 0x13:
2516 pmt->es[idx].type = SL_SECTION;
2517 break;
2518 case 0x81:
2519 pmt->es[idx].type = AUDIO_A52;
2520 break;
2521 case 0x8A:
2522 case 0x82:
2523 case 0x85:
2524 case 0x86:
2525 pmt->es[idx].type = AUDIO_DTS;
2526 break;
2527 case 0xEA:
2528 pmt->es[idx].type = VIDEO_VC1;
2529 break;
2530 default:
2531 mp_msg(MSGT_DEMUX, MSGL_DBG2, "UNKNOWN ES TYPE=0x%x\n", es_type);
2532 pmt->es[idx].type = UNKNOWN;
2535 tss = priv->ts.pids[es_pid]; //an ES stream
2536 if(tss == NULL)
2538 tss = new_pid(priv, es_pid);
2539 if(tss)
2540 tss->type = pmt->es[idx].type;
2543 section_bytes -= 5 + pmt->es[idx].descr_length;
2544 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",
2545 progid, idx, es_count, pmt->es[idx].pid, pmt->es[idx].pid, pmt->es[idx].type, pmt->es[idx].descr_length, section_bytes);
2548 es_base += 5 + pmt->es[idx].descr_length;
2550 es_count++;
2553 mp_msg(MSGT_DEMUX, MSGL_V, "----------------------------\n");
2554 return 1;
2557 static pmt_t* pmt_of_pid(ts_priv_t *priv, int pid, mp4_decoder_config_t **mp4_dec)
2559 int32_t i, j, k;
2561 if(priv->pmt)
2563 for(i = 0; i < priv->pmt_cnt; i++)
2565 if(priv->pmt[i].es && priv->pmt[i].es_cnt)
2567 for(j = 0; j < priv->pmt[i].es_cnt; j++)
2569 if(priv->pmt[i].es[j].pid == pid)
2571 //search mp4_es_id
2572 if(priv->pmt[i].es[j].mp4_es_id)
2574 for(k = 0; k < priv->pmt[i].mp4es_cnt; k++)
2576 if(priv->pmt[i].mp4es[k].id == priv->pmt[i].es[j].mp4_es_id)
2578 *mp4_dec = &(priv->pmt[i].mp4es[k].decoder);
2579 break;
2584 return &(priv->pmt[i]);
2591 return NULL;
2595 static inline int32_t pid_type_from_pmt(ts_priv_t *priv, int pid)
2597 int32_t pmt_idx, pid_idx, i, j;
2599 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2601 if(pmt_idx != -1)
2603 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2604 if(pid_idx != -1)
2605 return priv->pmt[pmt_idx].es[pid_idx].type;
2607 //else
2609 for(i = 0; i < priv->pmt_cnt; i++)
2611 pmt_t *pmt = &(priv->pmt[i]);
2612 for(j = 0; j < pmt->es_cnt; j++)
2613 if(pmt->es[j].pid == pid)
2614 return pmt->es[j].type;
2618 return UNKNOWN;
2622 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid)
2624 int32_t pmt_idx, pid_idx, i, j;
2626 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2628 if(pmt_idx != -1)
2630 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2631 if(pid_idx != -1)
2632 return priv->pmt[pmt_idx].es[pid_idx].lang;
2634 else
2636 for(i = 0; i < priv->pmt_cnt; i++)
2638 pmt_t *pmt = &(priv->pmt[i]);
2639 for(j = 0; j < pmt->es_cnt; j++)
2640 if(pmt->es[j].pid == pid)
2641 return pmt->es[j].lang;
2645 return NULL;
2649 static int fill_packet(demuxer_t *demuxer, demux_stream_t *ds, demux_packet_t **dp, int *dp_offset, TS_stream_info *si)
2651 int ret = 0;
2653 if((*dp != NULL) && (*dp_offset > 0))
2655 ret = *dp_offset;
2656 resize_demux_packet(*dp, ret); //shrinked to the right size
2657 ds_add_packet(ds, *dp);
2658 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);
2659 if(si)
2661 float diff = (*dp)->pts - si->last_pts;
2662 float dur;
2664 if(abs(diff) > 1) //1 second, there's a discontinuity
2666 si->duration += si->last_pts - si->first_pts;
2667 si->first_pts = si->last_pts = (*dp)->pts;
2669 else
2671 si->last_pts = (*dp)->pts;
2673 si->size += ret;
2674 dur = si->duration + (si->last_pts - si->first_pts);
2676 if(dur > 0 && ds == demuxer->video)
2678 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2679 if(dur > 1) //otherwise it may be unreliable
2680 priv->vbitrate = (uint32_t) ((float) si->size / dur);
2685 *dp = NULL;
2686 *dp_offset = 0;
2688 return ret;
2691 static int fill_extradata(mp4_decoder_config_t * mp4_dec, ES_stream_t *tss)
2693 uint8_t *tmp;
2695 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4_dec: %p, pid: %d\n", mp4_dec, tss->pid);
2697 if(mp4_dec->buf_size > tss->extradata_alloc)
2699 tmp = realloc(tss->extradata, mp4_dec->buf_size);
2700 if(!tmp)
2701 return 0;
2702 tss->extradata = tmp;
2703 tss->extradata_alloc = mp4_dec->buf_size;
2705 memcpy(tss->extradata, mp4_dec->buf, mp4_dec->buf_size);
2706 tss->extradata_len = mp4_dec->buf_size;
2707 mp_msg(MSGT_DEMUX, MSGL_V, "EXTRADATA: %p, alloc=%d, len=%d\n", tss->extradata, tss->extradata_alloc, tss->extradata_len);
2709 return tss->extradata_len;
2712 // 0 = EOF or no stream found
2713 // else = [-] number of bytes written to the packet
2714 static int ts_parse(demuxer_t *demuxer , ES_stream_t *es, unsigned char *packet, int probe)
2716 ES_stream_t *tss;
2717 uint8_t done = 0;
2718 int buf_size, is_start, pid, base;
2719 int len, cc, cc_ok, afc, retv = 0, is_video, is_audio, is_sub;
2720 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2721 stream_t *stream = demuxer->stream;
2722 char *p;
2723 demux_stream_t *ds = NULL;
2724 demux_packet_t **dp = NULL;
2725 int *dp_offset = 0, *buffer_size = 0;
2726 int32_t progid, pid_type, bad, ts_error;
2727 int junk = 0, rap_flag = 0;
2728 pmt_t *pmt;
2729 mp4_decoder_config_t *mp4_dec;
2730 TS_stream_info *si;
2733 while(! done)
2735 bad = ts_error = 0;
2736 ds = (demux_stream_t*) NULL;
2737 dp = (demux_packet_t **) NULL;
2738 dp_offset = buffer_size = NULL;
2739 rap_flag = 0;
2740 mp4_dec = NULL;
2741 es->is_synced = 0;
2742 es->lang[0] = 0;
2743 si = NULL;
2745 junk = priv->ts.packet_size - TS_PACKET_SIZE;
2746 buf_size = priv->ts.packet_size - junk;
2748 if(stream_eof(stream))
2750 if(! probe)
2752 ts_dump_streams(priv);
2753 demuxer->filepos = stream_tell(demuxer->stream);
2756 return 0;
2760 if(! ts_sync(stream))
2762 mp_msg(MSGT_DEMUX, MSGL_INFO, "TS_PARSE: COULDN'T SYNC\n");
2763 return 0;
2766 len = stream_read(stream, &packet[1], 3);
2767 if (len != 3)
2768 return 0;
2769 buf_size -= 4;
2771 if((packet[1] >> 7) & 0x01) //transport error
2772 ts_error = 1;
2775 is_start = packet[1] & 0x40;
2776 pid = ((packet[1] & 0x1f) << 8) | packet[2];
2778 tss = priv->ts.pids[pid]; //an ES stream
2779 if(tss == NULL)
2781 tss = new_pid(priv, pid);
2782 if(tss == NULL)
2783 continue;
2786 cc = (packet[3] & 0xf);
2787 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
2788 tss->last_cc = cc;
2790 bad = ts_error; // || (! cc_ok);
2791 if(bad)
2793 if(priv->keep_broken == 0)
2795 stream_skip(stream, buf_size-1+junk);
2796 continue;
2799 is_start = 0; //queued to the packet data
2802 if(is_start)
2803 tss->is_synced = 1;
2805 if((!is_start && !tss->is_synced) || ((pid > 1) && (pid < 16)) || (pid == 8191)) //invalid pid
2807 stream_skip(stream, buf_size-1+junk);
2808 continue;
2812 afc = (packet[3] >> 4) & 3;
2813 if(! (afc % 2)) //no payload in this TS packet
2815 stream_skip(stream, buf_size-1+junk);
2816 continue;
2819 if(afc > 1)
2821 int c;
2822 c = stream_read_char(stream);
2823 buf_size--;
2824 if(c < 0 || c > 183) //broken from the stream layer or invalid
2826 stream_skip(stream, buf_size-1+junk);
2827 continue;
2830 //c==0 is allowed!
2831 if(c > 0)
2833 uint8_t pcrbuf[188];
2834 int flags = stream_read_char(stream);
2835 int has_pcr;
2836 rap_flag = (flags & 0x40) >> 6;
2837 has_pcr = flags & 0x10;
2839 buf_size--;
2840 c--;
2841 stream_read(stream, pcrbuf, c);
2843 if(has_pcr)
2845 int pcr_pid = prog_pcr_pid(priv, priv->prog);
2846 if(pcr_pid == pid)
2848 uint64_t pcr, pcr_ext;
2850 pcr = (int64_t)(pcrbuf[0]) << 25;
2851 pcr |= pcrbuf[1] << 17 ;
2852 pcr |= (pcrbuf[2]) << 9;
2853 pcr |= pcrbuf[3] << 1 ;
2854 pcr |= (pcrbuf[4] & 0x80) >> 7;
2856 pcr_ext = (pcrbuf[4] & 0x01) << 8;
2857 pcr_ext |= pcrbuf[5];
2859 pcr = pcr * 300 + pcr_ext;
2861 demuxer->reference_clock = (double)pcr/(double)27000000.0;
2865 buf_size -= c;
2866 if(buf_size == 0)
2867 continue;
2871 //find the program that the pid belongs to; if (it's the right one or -1) && pid_type==SL_SECTION
2872 //call parse_sl_section()
2873 pmt = pmt_of_pid(priv, pid, &mp4_dec);
2874 if(mp4_dec)
2876 fill_extradata(mp4_dec, tss);
2877 if(IS_VIDEO(mp4_dec->object_type) || IS_AUDIO(mp4_dec->object_type))
2879 tss->type = SL_PES_STREAM;
2880 tss->subtype = mp4_dec->object_type;
2885 //TABLE PARSING
2887 base = priv->ts.packet_size - buf_size;
2889 priv->last_pid = pid;
2891 is_video = IS_VIDEO(tss->type) || (tss->type==SL_PES_STREAM && IS_VIDEO(tss->subtype));
2892 is_audio = IS_AUDIO(tss->type) || (tss->type==SL_PES_STREAM && IS_AUDIO(tss->subtype)) || (tss->type == PES_PRIVATE1);
2893 is_sub = IS_SUB(tss->type);
2894 pid_type = pid_type_from_pmt(priv, pid);
2896 // PES CONTENT STARTS HERE
2897 if(! probe)
2899 if((is_video || is_audio) && is_start && !priv->ts.streams[pid].sh)
2900 ts_add_stream(demuxer, tss);
2902 if((pid == demuxer->sub->id)) //or the lang is right
2904 pid_type = SPU_DVD;
2907 if(is_video && (demuxer->video->id == priv->ts.streams[pid].id))
2909 ds = demuxer->video;
2911 dp = &priv->fifo[1].pack;
2912 dp_offset = &priv->fifo[1].offset;
2913 buffer_size = &priv->fifo[1].buffer_size;
2914 si = &priv->vstr;
2916 else if(is_audio && (demuxer->audio->id == priv->ts.streams[pid].id))
2918 ds = demuxer->audio;
2920 dp = &priv->fifo[0].pack;
2921 dp_offset = &priv->fifo[0].offset;
2922 buffer_size = &priv->fifo[0].buffer_size;
2923 si = &priv->astr;
2925 else if(is_sub
2926 || IS_SUB(pid_type))
2928 //SUBS are infrequent, so the initial detection may fail
2929 // and we may need to add them at play-time
2930 if(demuxer->sub->id == -1)
2932 uint16_t p;
2933 p = progid_for_pid(priv, tss->pid, priv->prog);
2935 if(p == priv->prog)
2937 int asgn = 0;
2938 uint8_t *lang;
2940 if(dvdsub_lang)
2942 if ((lang = pid_lang_from_pmt(priv, pid)))
2943 asgn = (strncmp(lang, dvdsub_lang, 3) == 0);
2945 else //no language specified with -slang
2946 asgn = 1;
2948 if(asgn)
2950 demuxer->sub->id = tss->pid;
2951 mp_msg(MSGT_DEMUX, MSGL_INFO, "CHOSEN SUBs pid 0x%x (%d) FROM PROG %d\n", tss->pid, tss->pid, priv->prog);
2956 if(demuxer->sub->id == tss->pid)
2958 ds = demuxer->sub;
2960 dp = &priv->fifo[2].pack;
2961 dp_offset = &priv->fifo[2].offset;
2962 buffer_size = &priv->fifo[2].buffer_size;
2964 else
2966 stream_skip(stream, buf_size+junk);
2967 continue;
2971 //IS IT TIME TO QUEUE DATA to the dp_packet?
2972 if(is_start && (dp != NULL))
2974 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
2978 if(dp && *dp == NULL)
2980 if(*buffer_size > MAX_PACK_BYTES)
2981 *buffer_size = MAX_PACK_BYTES;
2982 *dp = new_demux_packet(*buffer_size); //es->size
2983 *dp_offset = 0;
2984 if(! *dp)
2986 fprintf(stderr, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", *buffer_size);
2987 continue;
2989 mp_msg(MSGT_DEMUX, MSGL_DBG2, "CREATED DP(%d)\n", *buffer_size);
2994 if(probe || !dp) //dp is NULL for tables and sections
2996 p = &packet[base];
2998 else //feeding
3000 if(*dp_offset + buf_size > *buffer_size)
3002 *buffer_size = *dp_offset + buf_size + TS_FEC_PACKET_SIZE;
3003 resize_demux_packet(*dp, *buffer_size);
3005 p = &((*dp)->buffer[*dp_offset]);
3008 len = stream_read(stream, p, buf_size);
3009 if(len < buf_size)
3011 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\r\nts_parse() couldn't read enough data: %d < %d\r\n", len, buf_size);
3012 continue;
3014 stream_skip(stream, junk);
3016 if(pid == 0)
3018 parse_pat(priv, is_start, p, buf_size);
3019 continue;
3021 else if((tss->type == SL_SECTION) && pmt)
3023 int k, mp4_es_id = -1;
3024 ts_section_t *section;
3025 for(k = 0; k < pmt->mp4es_cnt; k++)
3027 if(pmt->mp4es[k].decoder.object_type == MP4_OD && pmt->mp4es[k].decoder.stream_type == MP4_OD)
3028 mp4_es_id = pmt->mp4es[k].id;
3030 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4ESID: %d\n", mp4_es_id);
3031 for(k = 0; k < pmt->es_cnt; k++)
3033 if(pmt->es[k].mp4_es_id == mp4_es_id)
3035 section = &(tss->section);
3036 parse_sl_section(pmt, section, is_start, &packet[base], buf_size);
3039 continue;
3041 else
3043 progid = prog_id_in_pat(priv, pid);
3044 if(progid != -1)
3046 if(pid != demuxer->video->id && pid != demuxer->audio->id && pid != demuxer->sub->id)
3048 parse_pmt(priv, progid, pid, is_start, &packet[base], buf_size);
3049 continue;
3051 else
3052 mp_msg(MSGT_DEMUX, MSGL_ERR, "Argh! Data pid %d used in the PMT, Skipping PMT parsing!\n", pid);
3056 if(!probe && !dp)
3057 continue;
3059 if(is_start)
3061 uint8_t *lang = NULL;
3063 mp_msg(MSGT_DEMUX, MSGL_DBG2, "IS_START\n");
3065 len = pes_parse2(p, buf_size, es, pid_type, pmt, pid);
3066 if(! len)
3068 tss->is_synced = 0;
3069 continue;
3071 es->pid = tss->pid;
3072 tss->is_synced |= es->is_synced || rap_flag;
3073 tss->payload_size = es->payload_size;
3075 if((is_sub || is_audio) && (lang = pid_lang_from_pmt(priv, es->pid)))
3077 memcpy(es->lang, lang, 3);
3078 es->lang[3] = 0;
3080 else
3081 es->lang[0] = 0;
3083 if(probe)
3085 if(es->type == UNKNOWN)
3086 return 0;
3088 tss->type = es->type;
3089 tss->subtype = es->subtype;
3091 return 1;
3093 else
3095 if(es->pts == 0.0)
3096 es->pts = tss->pts = tss->last_pts;
3097 else
3098 tss->pts = tss->last_pts = es->pts;
3100 mp_msg(MSGT_DEMUX, MSGL_DBG2, "ts_parse, NEW pid=%d, PSIZE: %u, type=%X, start=%p, len=%d\n",
3101 es->pid, es->payload_size, es->type, es->start, es->size);
3103 demuxer->filepos = stream_tell(demuxer->stream) - es->size;
3105 memmove(p, es->start, es->size);
3106 *dp_offset += es->size;
3107 (*dp)->flags = 0;
3108 (*dp)->pos = stream_tell(demuxer->stream);
3109 (*dp)->pts = es->pts;
3111 if(retv > 0)
3112 return retv;
3113 else
3114 continue;
3117 else
3119 uint16_t sz;
3121 es->pid = tss->pid;
3122 es->type = tss->type;
3123 es->subtype = tss->subtype;
3124 es->pts = tss->pts = tss->last_pts;
3125 es->start = &packet[base];
3128 if(tss->payload_size > 0)
3130 sz = FFMIN(tss->payload_size, buf_size);
3131 tss->payload_size -= sz;
3132 es->size = sz;
3134 else
3136 if(is_video)
3138 sz = es->size = buf_size;
3140 else
3142 continue;
3147 if(! probe)
3149 *dp_offset += sz;
3151 if(*dp_offset >= MAX_PACK_BYTES)
3153 (*dp)->pts = tss->last_pts;
3154 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
3155 return 1;
3158 continue;
3160 else
3162 memcpy(es->start, p, sz);
3164 if(es->size)
3165 return es->size;
3166 else
3167 continue;
3172 return 0;
3176 void skip_audio_frame(sh_audio_t *sh_audio);
3178 static void reset_fifos(demuxer_t *demuxer, int a, int v, int s)
3180 ts_priv_t* priv = demuxer->priv;
3181 if(a)
3183 if(priv->fifo[0].pack != NULL)
3185 free_demux_packet(priv->fifo[0].pack);
3186 priv->fifo[0].pack = NULL;
3188 priv->fifo[0].offset = 0;
3191 if(v)
3193 if(priv->fifo[1].pack != NULL)
3195 free_demux_packet(priv->fifo[1].pack);
3196 priv->fifo[1].pack = NULL;
3198 priv->fifo[1].offset = 0;
3201 if(s)
3203 if(priv->fifo[2].pack != NULL)
3205 free_demux_packet(priv->fifo[2].pack);
3206 priv->fifo[2].pack = NULL;
3208 priv->fifo[2].offset = 0;
3210 demuxer->reference_clock = MP_NOPTS_VALUE;
3214 static void demux_seek_ts(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
3216 demux_stream_t *d_audio=demuxer->audio;
3217 demux_stream_t *d_video=demuxer->video;
3218 sh_audio_t *sh_audio=d_audio->sh;
3219 sh_video_t *sh_video=d_video->sh;
3220 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
3221 int i, video_stats;
3222 off_t newpos;
3224 //================= seek in MPEG-TS ==========================
3226 ts_dump_streams(demuxer->priv);
3227 reset_fifos(demuxer, sh_audio != NULL, sh_video != NULL, demuxer->sub->id > 0);
3229 demux_flush(demuxer);
3233 video_stats = (sh_video != NULL);
3234 if(video_stats)
3236 mp_msg(MSGT_DEMUX, MSGL_V, "IBPS: %d, vb: %d\r\n", sh_video->i_bps, priv->vbitrate);
3237 if(priv->vbitrate)
3238 video_stats = priv->vbitrate;
3239 else
3240 video_stats = sh_video->i_bps;
3243 newpos = (flags & SEEK_ABSOLUTE) ? demuxer->movi_start : demuxer->filepos;
3244 if(flags & SEEK_FACTOR) // float seek 0..1
3245 newpos+=(demuxer->movi_end-demuxer->movi_start)*rel_seek_secs;
3246 else
3248 // time seek (secs)
3249 if(! video_stats) // unspecified or VBR
3250 newpos += 2324*75*rel_seek_secs; // 174.3 kbyte/sec
3251 else
3252 newpos += video_stats*rel_seek_secs;
3256 if(newpos < demuxer->movi_start)
3257 newpos = demuxer->movi_start; //begininng of stream
3259 stream_seek(demuxer->stream, newpos);
3260 for(i = 0; i < 8192; i++)
3261 if(priv->ts.pids[i] != NULL)
3262 priv->ts.pids[i]->is_synced = 0;
3264 videobuf_code_len = 0;
3266 if(sh_video != NULL)
3267 ds_fill_buffer(d_video);
3269 if(sh_audio != NULL)
3271 ds_fill_buffer(d_audio);
3274 while(sh_video != NULL)
3276 if(sh_audio && !d_audio->eof && d_video->pts && d_audio->pts)
3278 double a_pts=d_audio->pts;
3279 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(double)sh_audio->i_bps;
3280 if(d_video->pts > a_pts)
3282 skip_audio_frame(sh_audio); // sync audio
3283 continue;
3288 i = sync_video_packet(d_video);
3289 if((sh_video->format == VIDEO_MPEG1) || (sh_video->format == VIDEO_MPEG2))
3291 if(i==0x1B3 || i==0x1B8) break; // found it!
3293 else if((sh_video->format == VIDEO_MPEG4) && (i==0x1B6))
3294 break;
3295 else if(sh_video->format == VIDEO_VC1 && (i==0x10E || i==0x10F))
3296 break;
3297 else //H264
3299 if((i & ~0x60) == 0x105 || (i & ~0x60) == 0x107) break;
3302 if(!i || !skip_video_packet(d_video)) break; // EOF?
3307 static int demux_ts_fill_buffer(demuxer_t * demuxer, demux_stream_t *ds)
3309 ES_stream_t es;
3310 ts_priv_t *priv = (ts_priv_t *)demuxer->priv;
3312 return -ts_parse(demuxer, &es, priv->packet, 0);
3316 static int ts_check_file_dmx(demuxer_t *demuxer)
3318 return ts_check_file(demuxer) ? DEMUXER_TYPE_MPEG_TS : 0;
3321 static int is_usable_program(ts_priv_t *priv, pmt_t *pmt)
3323 int j;
3325 for(j = 0; j < pmt->es_cnt; j++)
3327 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3328 continue;
3330 priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO ||
3331 priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO
3333 return 1;
3336 return 0;
3339 static int demux_ts_control(demuxer_t *demuxer, int cmd, void *arg)
3341 ts_priv_t* priv = (ts_priv_t *)demuxer->priv;
3343 switch(cmd)
3345 case DEMUXER_CTRL_SWITCH_AUDIO:
3346 case DEMUXER_CTRL_SWITCH_VIDEO:
3348 void *sh = NULL;
3349 int i, n;
3350 int reftype, areset = 0, vreset = 0;
3351 demux_stream_t *ds;
3353 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
3355 reftype = TYPE_VIDEO;
3356 ds = demuxer->video;
3357 vreset = 1;
3359 else
3361 reftype = TYPE_AUDIO;
3362 ds = demuxer->audio;
3363 areset = 1;
3365 n = *((int*)arg);
3366 if(n == -2)
3368 reset_fifos(demuxer, areset, vreset, 0);
3369 ds->id = -2;
3370 ds->sh = NULL;
3371 ds_free_packs(ds);
3372 *((int*)arg) = ds->id;
3373 return DEMUXER_CTRL_OK;
3376 if(n < 0)
3378 for(i = 0; i < 8192; i++)
3380 if(priv->ts.streams[i].id == ds->id && priv->ts.streams[i].type == reftype)
3381 break;
3384 while(!sh)
3386 i = (i+1) % 8192;
3387 if(priv->ts.streams[i].type == reftype)
3389 if(priv->ts.streams[i].id == ds->id) //we made a complete loop
3390 break;
3391 sh = priv->ts.streams[i].sh;
3395 else //audio track <n>
3397 if (n >= 8192 || priv->ts.streams[n].type != reftype) return DEMUXER_CTRL_NOTIMPL;
3398 i = n;
3399 sh = priv->ts.streams[i].sh;
3402 if(sh)
3404 if(ds->id != priv->ts.streams[i].id)
3405 reset_fifos(demuxer, areset, vreset, 0);
3406 ds->id = priv->ts.streams[i].id;
3407 ds->sh = sh;
3408 ds_free_packs(ds);
3409 mp_msg(MSGT_DEMUX, MSGL_V, "\r\ndemux_ts, switched to audio pid %d, id: %d, sh: %p\r\n", i, ds->id, sh);
3412 *((int*)arg) = ds->id;
3413 return DEMUXER_CTRL_OK;
3416 case DEMUXER_CTRL_IDENTIFY_PROGRAM: //returns in prog->{aid,vid} the new ids that comprise a program
3418 int i, j, cnt=0;
3419 int vid_done=0, aid_done=0;
3420 pmt_t *pmt = NULL;
3421 demux_program_t *prog = arg;
3423 if(priv->pmt_cnt < 2)
3424 return DEMUXER_CTRL_NOTIMPL;
3426 if(prog->progid == -1)
3428 int cur_pmt_idx = 0;
3430 for(i = 0; i < priv->pmt_cnt; i++)
3431 if(priv->pmt[i].progid == priv->prog)
3433 cur_pmt_idx = i;
3434 break;
3437 i = (cur_pmt_idx + 1) % priv->pmt_cnt;
3438 while(i != cur_pmt_idx)
3440 pmt = &priv->pmt[i];
3441 cnt = is_usable_program(priv, pmt);
3442 if(cnt)
3443 break;
3444 i = (i + 1) % priv->pmt_cnt;
3447 else
3449 for(i = 0; i < priv->pmt_cnt; i++)
3450 if(priv->pmt[i].progid == prog->progid)
3452 pmt = &priv->pmt[i]; //required program
3453 cnt = is_usable_program(priv, pmt);
3457 if(!cnt)
3458 return DEMUXER_CTRL_NOTIMPL;
3460 //finally some food
3461 prog->aid = prog->vid = -2; //no audio and no video by default
3462 for(j = 0; j < pmt->es_cnt; j++)
3464 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3465 continue;
3467 if(!vid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO)
3469 vid_done = 1;
3470 prog->vid = pmt->es[j].pid;
3472 else if(!aid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO)
3474 aid_done = 1;
3475 prog->aid = pmt->es[j].pid;
3479 priv->prog = prog->progid = pmt->progid;
3480 return DEMUXER_CTRL_OK;
3483 default:
3484 return DEMUXER_CTRL_NOTIMPL;
3489 const demuxer_desc_t demuxer_desc_mpeg_ts = {
3490 "MPEG-TS demuxer",
3491 "mpegts",
3492 "TS",
3493 "Nico Sabbi",
3495 DEMUXER_TYPE_MPEG_TS,
3496 0, // unsafe autodetect
3497 ts_check_file_dmx,
3498 demux_ts_fill_buffer,
3499 demux_open_ts,
3500 demux_close_ts,
3501 demux_seek_ts,
3502 demux_ts_control