ao: fix crash after ao init failure (from recent 3a5fd15fa2)
[mplayer/greg.git] / libmpdemux / demux_ts.c
blobebb1709f5a20f913e9f1497e8ae106bfc19993dd
1 /*
2 * Demultiplexer for MPEG2 Transport Streams.
4 * Written by Nico <nsabbi@libero.it>
5 * Kind feedback is appreciated; 'sucks' and alike is not.
6 * Originally based on demux_pva.c written by Matteo Giani and FFmpeg (libavformat) sources
8 * This file is part of MPlayer.
10 * MPlayer is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * MPlayer is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "config.h"
30 #include "mp_msg.h"
31 #include "options.h"
33 #include "libmpcodecs/dec_audio.h"
34 #include "stream/stream.h"
35 #include "demuxer.h"
36 #include "parse_es.h"
37 #include "stheader.h"
38 #include "ms_hdr.h"
39 #include "mpeg_hdr.h"
40 #include "demux_ts.h"
42 #define TS_PH_PACKET_SIZE 192
43 #define TS_FEC_PACKET_SIZE 204
44 #define TS_PACKET_SIZE 188
45 #define NB_PID_MAX 8192
47 #define MAX_HEADER_SIZE 6 /* enough for PES header + length */
48 #define MAX_CHECK_SIZE 65535
49 #define NUM_CONSECUTIVE_TS_PACKETS 32
50 #define NUM_CONSECUTIVE_AUDIO_PACKETS 348
51 #define MAX_A52_FRAME_SIZE 3840
53 #ifndef SIZE_MAX
54 #define SIZE_MAX ((size_t)-1)
55 #endif
57 #define TYPE_AUDIO 1
58 #define TYPE_VIDEO 2
59 #define TYPE_SUB 3
61 int ts_prog;
62 int ts_keep_broken=0;
63 off_t ts_probe = 0;
64 int audio_substream_id = -1;
66 typedef enum
68 UNKNOWN = -1,
69 VIDEO_MPEG1 = 0x10000001,
70 VIDEO_MPEG2 = 0x10000002,
71 VIDEO_MPEG4 = 0x10000004,
72 VIDEO_H264 = 0x10000005,
73 VIDEO_AVC = mmioFOURCC('a', 'v', 'c', '1'),
74 VIDEO_DIRAC = mmioFOURCC('d', 'r', 'a', 'c'),
75 VIDEO_VC1 = mmioFOURCC('W', 'V', 'C', '1'),
76 AUDIO_MP2 = 0x50,
77 AUDIO_A52 = 0x2000,
78 AUDIO_DTS = 0x2001,
79 AUDIO_LPCM_BE = 0x10001,
80 AUDIO_AAC = mmioFOURCC('M', 'P', '4', 'A'),
81 AUDIO_AAC_LATM = mmioFOURCC('M', 'P', '4', 'L'),
82 AUDIO_TRUEHD = mmioFOURCC('T', 'R', 'H', 'D'),
83 SPU_DVD = 0x3000000,
84 SPU_DVB = 0x3000001,
85 SPU_TELETEXT = 0x3000002,
86 SPU_PGS = 0x3000003,
87 PES_PRIVATE1 = 0xBD00000,
88 SL_PES_STREAM = 0xD000000,
89 SL_SECTION = 0xD100000,
90 MP4_OD = 0xD200000,
91 } es_stream_type_t;
93 typedef struct {
94 uint8_t *buffer;
95 uint16_t buffer_len;
96 } ts_section_t;
98 typedef struct {
99 int size;
100 unsigned char *start;
101 uint16_t payload_size;
102 es_stream_type_t type, subtype;
103 double pts, last_pts;
104 int pid;
105 char lang[4];
106 int last_cc; // last cc code (-1 if first packet)
107 int is_synced;
108 ts_section_t section;
109 uint8_t *extradata;
110 int extradata_alloc, extradata_len;
111 struct {
112 uint8_t au_start, au_end, last_au_end;
113 } sl;
114 } ES_stream_t;
116 typedef struct {
117 void *sh;
118 int id;
119 int type;
120 } sh_av_t;
122 typedef struct MpegTSContext {
123 int packet_size; // raw packet size, including FEC if present e.g. 188 bytes
124 ES_stream_t *pids[NB_PID_MAX];
125 sh_av_t streams[NB_PID_MAX];
126 } MpegTSContext;
129 typedef struct {
130 demux_stream_t *ds;
131 demux_packet_t *pack;
132 int offset, buffer_size;
133 } av_fifo_t;
135 #define MAX_EXTRADATA_SIZE 64*1024
136 typedef struct {
137 int32_t object_type; //aka codec used
138 int32_t stream_type; //video, audio etc.
139 uint8_t buf[MAX_EXTRADATA_SIZE];
140 uint16_t buf_size;
141 uint8_t szm1;
142 } mp4_decoder_config_t;
144 typedef struct {
145 //flags
146 uint8_t flags;
147 uint8_t au_start;
148 uint8_t au_end;
149 uint8_t random_accesspoint;
150 uint8_t random_accesspoint_only;
151 uint8_t padding;
152 uint8_t use_ts;
153 uint8_t idle;
154 uint8_t duration;
156 uint32_t ts_resolution, ocr_resolution;
157 uint8_t ts_len, ocr_len, au_len, instant_bitrate_len, degr_len, au_seqnum_len, packet_seqnum_len;
158 uint32_t timescale;
159 uint16_t au_duration, cts_duration;
160 uint64_t ocr, dts, cts;
161 } mp4_sl_config_t;
163 typedef struct {
164 uint16_t id;
165 uint8_t flags;
166 mp4_decoder_config_t decoder;
167 mp4_sl_config_t sl;
168 } mp4_es_descr_t;
170 typedef struct {
171 uint16_t id;
172 uint8_t flags;
173 mp4_es_descr_t *es;
174 uint16_t es_cnt;
175 } mp4_od_t;
177 typedef struct {
178 uint8_t skip;
179 uint8_t table_id;
180 uint8_t ssi;
181 uint16_t section_length;
182 uint16_t ts_id;
183 uint8_t version_number;
184 uint8_t curr_next;
185 uint8_t section_number;
186 uint8_t last_section_number;
187 struct pat_progs_t {
188 uint16_t id;
189 uint16_t pmt_pid;
190 } *progs;
191 uint16_t progs_cnt;
192 ts_section_t section;
193 } pat_t;
195 typedef struct {
196 uint16_t progid;
197 uint8_t skip;
198 uint8_t table_id;
199 uint8_t ssi;
200 uint16_t section_length;
201 uint8_t version_number;
202 uint8_t curr_next;
203 uint8_t section_number;
204 uint8_t last_section_number;
205 uint16_t PCR_PID;
206 uint16_t prog_descr_length;
207 ts_section_t section;
208 uint16_t es_cnt;
209 struct pmt_es_t {
210 uint16_t pid;
211 uint32_t type; //it's 8 bit long, but cast to the right type as FOURCC
212 uint16_t descr_length;
213 uint8_t format_descriptor[5];
214 uint8_t lang[4];
215 uint16_t mp4_es_id;
216 } *es;
217 mp4_od_t iod, *od;
218 mp4_es_descr_t *mp4es;
219 int od_cnt, mp4es_cnt;
220 } pmt_t;
222 typedef struct {
223 uint64_t size;
224 float duration;
225 double first_pts;
226 double last_pts;
227 } TS_stream_info;
229 typedef struct {
230 MpegTSContext ts;
231 int last_pid;
232 av_fifo_t fifo[3]; //0 for audio, 1 for video, 2 for subs
233 pat_t pat;
234 pmt_t *pmt;
235 uint16_t pmt_cnt;
236 uint32_t prog;
237 uint32_t vbitrate;
238 int keep_broken;
239 int last_aid;
240 int last_vid;
241 int last_sid;
242 char packet[TS_FEC_PACKET_SIZE];
243 TS_stream_info vstr, astr;
244 } ts_priv_t;
247 typedef struct {
248 es_stream_type_t type;
249 ts_section_t section;
250 } TS_pids_t;
253 static int IS_AUDIO(es_stream_type_t type)
255 switch (type) {
256 case AUDIO_MP2:
257 case AUDIO_A52:
258 case AUDIO_LPCM_BE:
259 case AUDIO_AAC:
260 case AUDIO_AAC_LATM:
261 case AUDIO_DTS:
262 case AUDIO_TRUEHD:
263 return 1;
265 return 0;
268 static int IS_VIDEO(es_stream_type_t type)
270 switch (type) {
271 case VIDEO_MPEG1:
272 case VIDEO_MPEG2:
273 case VIDEO_MPEG4:
274 case VIDEO_H264:
275 case VIDEO_AVC:
276 case VIDEO_DIRAC:
277 case VIDEO_VC1:
278 return 1;
280 return 0;
283 static int IS_SUB(es_stream_type_t type)
285 switch (type) {
286 case SPU_DVD:
287 case SPU_DVB:
288 case SPU_PGS:
289 case SPU_TELETEXT:
290 return 1;
292 return 0;
295 static int ts_parse(demuxer_t *demuxer, ES_stream_t *es, unsigned char *packet, int probe);
297 static uint8_t get_packet_size(const unsigned char *buf, int size)
299 int i;
301 if (size < (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS))
302 return 0;
304 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
306 if (buf[i * TS_PACKET_SIZE] != 0x47)
308 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
309 goto try_fec;
312 return TS_PACKET_SIZE;
314 try_fec:
315 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
317 if (buf[i * TS_FEC_PACKET_SIZE] != 0x47){
318 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
319 goto try_philips;
322 return TS_FEC_PACKET_SIZE;
324 try_philips:
325 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
327 if (buf[i * TS_PH_PACKET_SIZE] != 0x47)
328 return 0;
330 return TS_PH_PACKET_SIZE;
333 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h);
334 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid);
336 static void ts_add_stream(demuxer_t * demuxer, ES_stream_t *es)
338 int i;
339 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
341 if(priv->ts.streams[es->pid].sh)
342 return;
344 if((IS_AUDIO(es->type) || IS_AUDIO(es->subtype)) && priv->last_aid+1 < MAX_A_STREAMS)
346 sh_audio_t *sh = new_sh_audio_aid(demuxer, priv->last_aid, es->pid);
347 if(sh)
349 const char *lang = pid_lang_from_pmt(priv, es->pid);
350 sh->needs_parsing = 1;
351 sh->format = IS_AUDIO(es->type) ? es->type : es->subtype;
352 sh->ds = demuxer->audio;
354 priv->ts.streams[es->pid].id = priv->last_aid;
355 priv->ts.streams[es->pid].sh = sh;
356 priv->ts.streams[es->pid].type = TYPE_AUDIO;
357 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);
358 if (lang && lang[0])
359 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_AID_%d_LANG=%s\n", es->pid, lang);
360 priv->last_aid++;
363 if(es->extradata && es->extradata_len)
365 sh->wf = malloc(sizeof(*sh->wf) + es->extradata_len);
366 sh->wf->cbSize = es->extradata_len;
367 memcpy(sh->wf + 1, es->extradata, es->extradata_len);
371 if((IS_VIDEO(es->type) || IS_VIDEO(es->subtype)) && priv->last_vid+1 < MAX_V_STREAMS)
373 sh_video_t *sh = new_sh_video_vid(demuxer, priv->last_vid, es->pid);
374 if(sh)
376 sh->format = IS_VIDEO(es->type) ? es->type : es->subtype;
377 sh->ds = demuxer->video;
379 priv->ts.streams[es->pid].id = priv->last_vid;
380 priv->ts.streams[es->pid].sh = sh;
381 priv->ts.streams[es->pid].type = TYPE_VIDEO;
382 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);
383 priv->last_vid++;
386 if(sh->format == VIDEO_AVC && es->extradata && es->extradata_len)
388 int w = 0, h = 0;
389 sh->bih = calloc(1, sizeof(*sh->bih) + es->extradata_len);
390 sh->bih->biSize= sizeof(*sh->bih) + es->extradata_len;
391 sh->bih->biCompression = sh->format;
392 memcpy(sh->bih + 1, es->extradata, es->extradata_len);
393 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "EXTRADATA(%d BYTES): \n", es->extradata_len);
394 for(i = 0;i < es->extradata_len; i++)
395 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "%02x ", (int) es->extradata[i]);
396 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"\n");
397 if(parse_avc_sps(es->extradata, es->extradata_len, &w, &h))
399 sh->bih->biWidth = w;
400 sh->bih->biHeight = h;
406 if(IS_SUB(es->type) && priv->last_sid+1 < MAX_S_STREAMS)
408 sh_sub_t *sh = new_sh_sub_sid_lang(demuxer, priv->last_sid, es->pid, pid_lang_from_pmt(priv, es->pid));
409 if (sh) {
410 switch (es->type) {
411 case SPU_DVB:
412 sh->type = 'b'; break;
413 case SPU_DVD:
414 sh->type = 'v'; break;
415 case SPU_PGS:
416 sh->type = 'p'; break;
418 priv->ts.streams[es->pid].id = priv->last_sid;
419 priv->ts.streams[es->pid].sh = sh;
420 priv->ts.streams[es->pid].type = TYPE_SUB;
421 priv->last_sid++;
426 static int ts_check_file(demuxer_t * demuxer)
428 const int buf_size = (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS);
429 unsigned char buf[TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS], done = 0, *ptr;
430 uint32_t _read, i, count = 0, is_ts;
431 int cc[NB_PID_MAX], last_cc[NB_PID_MAX], pid, cc_ok, c, good, bad;
432 uint8_t size = 0;
433 off_t pos = 0;
434 off_t init_pos;
436 mp_msg(MSGT_DEMUX, MSGL_V, "Checking for MPEG-TS...\n");
438 init_pos = stream_tell(demuxer->stream);
439 is_ts = 0;
440 while(! done)
442 i = 1;
443 c = 0;
445 while(((c=stream_read_char(demuxer->stream)) != 0x47)
446 && (c >= 0)
447 && (i < MAX_CHECK_SIZE)
448 && ! demuxer->stream->eof
449 ) i++;
452 if(c != 0x47)
454 mp_msg(MSGT_DEMUX, MSGL_V, "THIS DOESN'T LOOK LIKE AN MPEG-TS FILE!\n");
455 is_ts = 0;
456 done = 1;
457 continue;
460 pos = stream_tell(demuxer->stream) - 1;
461 buf[0] = c;
462 _read = stream_read(demuxer->stream, &buf[1], buf_size-1);
464 if(_read < buf_size-1)
466 mp_msg(MSGT_DEMUX, MSGL_V, "COULDN'T READ ENOUGH DATA, EXITING TS_CHECK\n");
467 stream_reset(demuxer->stream);
468 return 0;
471 size = get_packet_size(buf, buf_size);
472 if(size)
474 done = 1;
475 is_ts = 1;
478 if(pos - init_pos >= MAX_CHECK_SIZE)
480 done = 1;
481 is_ts = 0;
485 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);
486 stream_seek(demuxer->stream, pos);
488 if(! is_ts)
489 return 0;
491 //LET'S CHECK continuity counters
492 good = bad = 0;
493 for(count = 0; count < NB_PID_MAX; count++)
495 cc[count] = last_cc[count] = -1;
498 for(count = 0; count < NUM_CONSECUTIVE_TS_PACKETS; count++)
500 ptr = &(buf[size * count]);
501 pid = ((ptr[1] & 0x1f) << 8) | ptr[2];
502 mp_msg(MSGT_DEMUX, MSGL_DBG2, "BUF: %02x %02x %02x %02x, PID %d, SIZE: %d \n",
503 ptr[0], ptr[1], ptr[2], ptr[3], pid, size);
505 if((pid == 8191) || (pid < 16))
506 continue;
508 cc[pid] = (ptr[3] & 0xf);
509 cc_ok = (last_cc[pid] < 0) || ((((last_cc[pid] + 1) & 0x0f) == cc[pid]));
510 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PID %d, COMPARE CC %d AND LAST_CC %d\n", pid, cc[pid], last_cc[pid]);
511 if(! cc_ok)
512 //return 0;
513 bad++;
514 else
515 good++;
517 last_cc[pid] = cc[pid];
520 mp_msg(MSGT_DEMUX, MSGL_V, "GOOD CC: %d, BAD CC: %d\n", good, bad);
522 if(good >= bad)
523 return size;
524 else
525 return 0;
529 static inline int32_t progid_idx_in_pmt(ts_priv_t *priv, uint16_t progid)
531 int x;
533 if(priv->pmt == NULL)
534 return -1;
536 for(x = 0; x < priv->pmt_cnt; x++)
538 if(priv->pmt[x].progid == progid)
539 return x;
542 return -1;
546 static inline int32_t progid_for_pid(ts_priv_t *priv, int pid, int32_t req) //finds the first program listing a pid
548 int i, j;
549 pmt_t *pmt;
552 if(priv->pmt == NULL)
553 return -1;
556 for(i=0; i < priv->pmt_cnt; i++)
558 pmt = &(priv->pmt[i]);
560 if(pmt->es == NULL)
561 return -1;
563 for(j = 0; j < pmt->es_cnt; j++)
565 if(pmt->es[j].pid == pid)
567 if((req == 0) || (req == pmt->progid))
568 return pmt->progid;
573 return -1;
576 static inline int32_t prog_pcr_pid(ts_priv_t *priv, int progid)
578 int i;
580 if(priv->pmt == NULL)
581 return -1;
582 for(i=0; i < priv->pmt_cnt; i++)
584 if(priv->pmt[i].progid == progid)
585 return priv->pmt[i].PCR_PID;
587 return -1;
591 static inline int pid_match_lang(ts_priv_t *priv, uint16_t pid, char *lang)
593 uint16_t i, j;
594 pmt_t *pmt;
596 if(priv->pmt == NULL)
597 return -1;
599 for(i=0; i < priv->pmt_cnt; i++)
601 pmt = &(priv->pmt[i]);
603 if(pmt->es == NULL)
604 return -1;
606 for(j = 0; j < pmt->es_cnt; j++)
608 if(pmt->es[j].pid != pid)
609 continue;
611 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);
612 if(strncmp(pmt->es[j].lang, lang, 3) == 0)
614 return 1;
620 return -1;
623 typedef struct {
624 int32_t atype, vtype, stype; //types
625 int32_t apid, vpid, spid; //stream ids
626 char alang[4]; //languages
627 uint16_t prog;
628 off_t probe;
629 } tsdemux_init_t;
631 //second stage: returns the count of A52 syncwords found
632 static int a52_check(char *buf, int len)
634 int cnt, frame_length = 0, ok, srate;
636 cnt = ok = 0;
637 if(len < 8)
638 return 0;
640 while(cnt < len - 7)
642 if(buf[cnt] == 0x0B && buf[cnt+1] == 0x77)
644 frame_length = mp_a52_framesize(&buf[cnt], &srate);
645 if(frame_length>=7 && frame_length<=3840)
647 cnt += frame_length;
648 ok++;
650 else
651 cnt++;
653 else
654 cnt++;
657 mp_msg(MSGT_DEMUXER, MSGL_V, "A52_CHECK(%d input bytes), found %d frame syncwords of %d bytes length\n", len, ok, frame_length);
658 return ok;
662 static off_t ts_detect_streams(demuxer_t *demuxer, tsdemux_init_t *param)
664 int video_found = 0, audio_found = 0, sub_found av_unused = 0, i, num_packets = 0, req_apid, req_vpid, req_spid;
665 int is_audio, is_video, is_sub, has_tables;
666 int32_t p, chosen_pid = 0;
667 off_t pos=0, ret = 0, init_pos, end_pos;
668 ES_stream_t es;
669 unsigned char tmp[TS_FEC_PACKET_SIZE];
670 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
671 struct {
672 char *buf;
673 int pos;
674 } pes_priv1[8192], *pptr;
675 char *tmpbuf;
677 priv->last_pid = 8192; //invalid pid
679 req_apid = param->apid;
680 req_vpid = param->vpid;
681 req_spid = param->spid;
683 has_tables = 0;
684 memset(pes_priv1, 0, sizeof(pes_priv1));
685 init_pos = stream_tell(demuxer->stream);
686 mp_msg(MSGT_DEMUXER, MSGL_V, "PROBING UP TO %"PRIu64", PROG: %d\n", (uint64_t) param->probe, param->prog);
687 end_pos = init_pos + (param->probe ? param->probe : TS_MAX_PROBE_SIZE);
688 while(1)
690 pos = stream_tell(demuxer->stream);
691 if(pos > end_pos || demuxer->stream->eof)
692 break;
694 if(ts_parse(demuxer, &es, tmp, 1))
696 //Non PES-aligned A52 audio may escape detection if PMT is not present;
697 //in this case we try to find at least 3 A52 syncwords
698 if((es.type == PES_PRIVATE1) && (! audio_found) && req_apid > -2)
700 pptr = &pes_priv1[es.pid];
701 if(pptr->pos < 64*1024)
703 tmpbuf = realloc(pptr->buf, pptr->pos + es.size);
704 if(tmpbuf != NULL)
706 pptr->buf = tmpbuf;
707 memcpy(&(pptr->buf[ pptr->pos ]), es.start, es.size);
708 pptr->pos += es.size;
709 if(a52_check(pptr->buf, pptr->pos) > 2)
711 param->atype = AUDIO_A52;
712 param->apid = es.pid;
713 es.type = AUDIO_A52;
719 is_audio = IS_AUDIO(es.type) || ((es.type==SL_PES_STREAM) && IS_AUDIO(es.subtype));
720 is_video = IS_VIDEO(es.type) || ((es.type==SL_PES_STREAM) && IS_VIDEO(es.subtype));
721 is_sub = IS_SUB(es.type);
724 if((! is_audio) && (! is_video) && (! is_sub))
725 continue;
726 if(is_audio && req_apid==-2)
727 continue;
729 if(is_video)
731 chosen_pid = (req_vpid == es.pid);
732 if((! chosen_pid) && (req_vpid > 0))
733 continue;
735 else if(is_audio)
737 if(req_apid > 0)
739 chosen_pid = (req_apid == es.pid);
740 if(! chosen_pid)
741 continue;
743 else if(param->alang[0] > 0 && es.lang[0] > 0)
745 if(pid_match_lang(priv, es.pid, param->alang) == -1)
746 continue;
748 chosen_pid = 1;
749 param->apid = req_apid = es.pid;
752 else if(is_sub)
754 chosen_pid = (req_spid == es.pid);
755 if((! chosen_pid) && (req_spid > 0))
756 continue;
759 if(req_apid < 0 && (param->alang[0] == 0) && req_vpid < 0 && req_spid < 0)
760 chosen_pid = 1;
762 if((ret == 0) && chosen_pid)
764 ret = stream_tell(demuxer->stream);
767 p = progid_for_pid(priv, es.pid, param->prog);
768 if(p != -1)
770 has_tables++;
771 if(!param->prog && chosen_pid)
772 param->prog = p;
775 if((param->prog > 0) && (param->prog != p))
777 if(audio_found)
779 if(is_video && (req_vpid == es.pid))
781 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
782 param->vpid = es.pid;
783 video_found = 1;
784 break;
788 if(video_found)
790 if(is_audio && (req_apid == es.pid))
792 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
793 param->apid = es.pid;
794 audio_found = 1;
795 break;
800 continue;
804 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "TYPE: %x, PID: %d, PROG FOUND: %d\n", es.type, es.pid, param->prog);
807 if(is_video)
809 if((req_vpid == -1) || (req_vpid == es.pid))
811 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
812 param->vpid = es.pid;
813 video_found = 1;
818 if(((req_vpid == -2) || (num_packets >= NUM_CONSECUTIVE_AUDIO_PACKETS)) && audio_found && !param->probe)
820 //novideo or we have at least 348 audio packets (64 KB) without video (TS with audio only)
821 param->vtype = 0;
822 break;
825 if(is_sub)
827 if((req_spid == -1) || (req_spid == es.pid))
829 param->stype = es.type;
830 param->spid = es.pid;
831 sub_found = 1;
835 if(is_audio)
837 if((req_apid == -1) || (req_apid == es.pid))
839 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
840 param->apid = es.pid;
841 audio_found = 1;
845 if(audio_found && (param->apid == es.pid) && (! video_found))
846 num_packets++;
848 if((has_tables==0) && (video_found && audio_found) && (pos >= 1000000))
849 break;
853 for(i=0; i<8192; i++)
855 if(pes_priv1[i].buf != NULL)
857 free(pes_priv1[i].buf);
858 pes_priv1[i].buf = NULL;
859 pes_priv1[i].pos = 0;
863 if(video_found)
865 if(param->vtype == VIDEO_MPEG1)
866 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG1(pid=%d) ", param->vpid);
867 else if(param->vtype == VIDEO_MPEG2)
868 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG2(pid=%d) ", param->vpid);
869 else if(param->vtype == VIDEO_MPEG4)
870 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG4(pid=%d) ", param->vpid);
871 else if(param->vtype == VIDEO_H264)
872 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO H264(pid=%d) ", param->vpid);
873 else if(param->vtype == VIDEO_VC1)
874 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO VC1(pid=%d) ", param->vpid);
875 else if(param->vtype == VIDEO_AVC)
876 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO AVC(NAL-H264, pid=%d) ", param->vpid);
878 else
880 param->vtype = UNKNOWN;
881 //WE DIDN'T MATCH ANY VIDEO STREAM
882 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO VIDEO! ");
885 if(param->atype == AUDIO_MP2)
886 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO MPA(pid=%d)", param->apid);
887 else if(param->atype == AUDIO_A52)
888 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO A52(pid=%d)", param->apid);
889 else if(param->atype == AUDIO_DTS)
890 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO DTS(pid=%d)", param->apid);
891 else if(param->atype == AUDIO_LPCM_BE)
892 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO LPCM(pid=%d)", param->apid);
893 else if(param->atype == AUDIO_AAC)
894 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO AAC(pid=%d)", param->apid);
895 else if(param->atype == AUDIO_AAC_LATM)
896 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO AAC LATM(pid=%d)", param->apid);
897 else if(param->atype == AUDIO_TRUEHD)
898 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO TRUEHD(pid=%d)", param->apid);
899 else
901 audio_found = 0;
902 param->atype = UNKNOWN;
903 //WE DIDN'T MATCH ANY AUDIO STREAM, SO WE FORCE THE DEMUXER TO IGNORE AUDIO
904 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO AUDIO! (try increasing -tsprobe)");
907 if(IS_SUB(param->stype))
908 mp_msg(MSGT_DEMUXER, MSGL_INFO, " SUB %s(pid=%d) ", (param->stype==SPU_DVD ? "DVD" : param->stype==SPU_DVB ? "DVB" : "Teletext"), param->spid);
909 else
911 param->stype = UNKNOWN;
912 mp_msg(MSGT_DEMUXER, MSGL_INFO, " NO SUBS (yet)! ");
915 if(video_found || audio_found)
917 if(!param->prog)
919 p = progid_for_pid(priv, video_found ? param->vpid : param->apid, 0);
920 if(p != -1)
921 param->prog = p;
924 if(demuxer->stream->eof && (ret == 0))
925 ret = init_pos;
926 mp_msg(MSGT_DEMUXER, MSGL_INFO, " PROGRAM N. %d\n", param->prog);
928 else
929 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\n");
932 for(i=0; i<NB_PID_MAX; i++)
934 if(priv->ts.pids[i] != NULL)
936 priv->ts.pids[i]->payload_size = 0;
937 priv->ts.pids[i]->pts = priv->ts.pids[i]->last_pts = 0;
938 priv->ts.pids[i]->last_cc = -1;
939 priv->ts.pids[i]->is_synced = 0;
943 return ret;
946 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h)
948 int sps, sps_len;
949 unsigned char *ptr;
950 mp_mpeg_header_t picture;
951 if(len < 6)
952 return 0;
953 sps = buf[5] & 0x1f;
954 if(!sps)
955 return 0;
956 sps_len = (buf[6] << 8) | buf[7];
957 if(!sps_len || (sps_len > len - 8))
958 return 0;
959 ptr = &(buf[8]);
960 picture.display_picture_width = picture.display_picture_height = 0;
961 h264_parse_sps(&picture, ptr, len - 8);
962 if(!picture.display_picture_width || !picture.display_picture_height)
963 return 0;
964 *w = picture.display_picture_width;
965 *h = picture.display_picture_height;
966 return 1;
969 static demuxer_t *demux_open_ts(demuxer_t * demuxer)
971 int i;
972 uint8_t packet_size;
973 sh_video_t *sh_video;
974 sh_audio_t *sh_audio;
975 off_t start_pos;
976 tsdemux_init_t params;
977 ts_priv_t * priv = demuxer->priv;
979 mp_msg(MSGT_DEMUX, MSGL_V, "DEMUX OPEN, AUDIO_ID: %d, VIDEO_ID: %d, SUBTITLE_ID: %d,\n",
980 demuxer->audio->id, demuxer->video->id, demuxer->sub->id);
983 demuxer->type= DEMUXER_TYPE_MPEG_TS;
986 stream_reset(demuxer->stream);
988 packet_size = ts_check_file(demuxer);
989 if(!packet_size)
990 return NULL;
992 priv = calloc(1, sizeof(ts_priv_t));
993 if(priv == NULL)
995 mp_msg(MSGT_DEMUX, MSGL_FATAL, "DEMUX_OPEN_TS, couldn't allocate enough memory for ts->priv, exit\n");
996 return NULL;
999 for(i=0; i < NB_PID_MAX; i++)
1001 priv->ts.pids[i] = NULL;
1002 priv->ts.streams[i].id = -3;
1004 priv->pat.progs = NULL;
1005 priv->pat.progs_cnt = 0;
1006 priv->pat.section.buffer = NULL;
1007 priv->pat.section.buffer_len = 0;
1009 priv->pmt = NULL;
1010 priv->pmt_cnt = 0;
1012 priv->keep_broken = ts_keep_broken;
1013 priv->ts.packet_size = packet_size;
1016 demuxer->priv = priv;
1017 if(demuxer->stream->type != STREAMTYPE_FILE)
1018 demuxer->seekable = 1;
1019 else
1020 demuxer->seekable = 1;
1023 params.atype = params.vtype = params.stype = UNKNOWN;
1024 params.apid = demuxer->audio->id;
1025 params.vpid = demuxer->video->id;
1026 params.spid = demuxer->sub->id;
1027 params.prog = ts_prog;
1028 params.probe = ts_probe;
1030 if(demuxer->opts->audio_lang != NULL)
1032 strncpy(params.alang, demuxer->opts->audio_lang[0], 3);
1033 params.alang[3] = 0;
1035 else
1036 memset(params.alang, 0, 4);
1038 start_pos = ts_detect_streams(demuxer, &params);
1040 demuxer->sub->id = params.spid;
1041 priv->prog = params.prog;
1043 if(params.vtype != UNKNOWN)
1045 ts_add_stream(demuxer, priv->ts.pids[params.vpid]);
1046 sh_video = priv->ts.streams[params.vpid].sh;
1047 demuxer->video->id = priv->ts.streams[params.vpid].id;
1048 sh_video->ds = demuxer->video;
1049 sh_video->format = params.vtype;
1050 demuxer->video->sh = sh_video;
1053 if(params.atype != UNKNOWN)
1055 ES_stream_t *es = priv->ts.pids[params.apid];
1057 if(!IS_AUDIO(es->type) && !IS_AUDIO(es->subtype) && IS_AUDIO(params.atype)) es->subtype = params.atype;
1058 ts_add_stream(demuxer, priv->ts.pids[params.apid]);
1059 sh_audio = priv->ts.streams[params.apid].sh;
1060 demuxer->audio->id = priv->ts.streams[params.apid].id;
1061 sh_audio->ds = demuxer->audio;
1062 sh_audio->format = params.atype;
1063 demuxer->audio->sh = sh_audio;
1067 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);
1070 start_pos = start_pos <= priv->ts.packet_size ?
1071 demuxer->stream->start_pos :
1072 start_pos - priv->ts.packet_size;
1073 demuxer->movi_start = start_pos;
1074 demuxer->reference_clock = MP_NOPTS_VALUE;
1075 stream_reset(demuxer->stream);
1076 stream_seek(demuxer->stream, start_pos); //IF IT'S FROM A PIPE IT WILL FAIL, BUT WHO CARES?
1079 priv->last_pid = 8192; //invalid pid
1081 for(i = 0; i < 3; i++)
1083 priv->fifo[i].pack = NULL;
1084 priv->fifo[i].offset = 0;
1086 priv->fifo[0].ds = demuxer->audio;
1087 priv->fifo[1].ds = demuxer->video;
1088 priv->fifo[2].ds = demuxer->sub;
1090 priv->fifo[0].buffer_size = 1536;
1091 priv->fifo[1].buffer_size = 32767;
1092 priv->fifo[2].buffer_size = 32767;
1094 priv->pat.section.buffer_len = 0;
1095 for(i = 0; i < priv->pmt_cnt; i++)
1096 priv->pmt[i].section.buffer_len = 0;
1098 demuxer->filepos = stream_tell(demuxer->stream);
1099 return demuxer;
1102 static void demux_close_ts(demuxer_t * demuxer)
1104 uint16_t i;
1105 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
1107 if(priv)
1109 free(priv->pat.section.buffer);
1110 free(priv->pat.progs);
1112 if(priv->pmt)
1114 for(i = 0; i < priv->pmt_cnt; i++)
1116 free(priv->pmt[i].section.buffer);
1117 free(priv->pmt[i].es);
1119 free(priv->pmt);
1121 for (i = 0; i < NB_PID_MAX; i++)
1123 free(priv->ts.pids[i]);
1124 priv->ts.pids[i] = NULL;
1126 for (i = 0; i < 3; i++)
1128 if (priv->fifo[i].pack)
1129 free_demux_packet(priv->fifo[i].pack);
1130 priv->fifo[i].pack = NULL;
1132 free(priv);
1134 demuxer->priv=NULL;
1138 #define getbits mp_getbits
1140 static int mp4_parse_sl_packet(pmt_t *pmt, uint8_t *buf, uint16_t packet_len, int pid, ES_stream_t *pes_es)
1142 int i, n, m, mp4_es_id = -1;
1143 uint64_t v = 0;
1144 uint32_t pl_size = 0;
1145 int deg_flag = 0;
1146 mp4_es_descr_t *es = NULL;
1147 mp4_sl_config_t *sl = NULL;
1148 uint8_t au_start = 0, au_end = 0, rap_flag = 0, ocr_flag = 0, padding = 0, padding_bits = 0, idle = 0;
1150 pes_es->is_synced = 0;
1151 mp_msg(MSGT_DEMUXER,MSGL_V, "mp4_parse_sl_packet, pid: %d, pmt: %pm, packet_len: %d\n", pid, pmt, packet_len);
1152 if(! pmt || !packet_len)
1153 return 0;
1155 for(i = 0; i < pmt->es_cnt; i++)
1157 if(pmt->es[i].pid == pid)
1158 mp4_es_id = pmt->es[i].mp4_es_id;
1160 if(mp4_es_id < 0)
1161 return -1;
1163 for(i = 0; i < pmt->mp4es_cnt; i++)
1165 if(pmt->mp4es[i].id == mp4_es_id)
1166 es = &(pmt->mp4es[i]);
1168 if(! es)
1169 return -1;
1171 pes_es->subtype = es->decoder.object_type;
1173 sl = &(es->sl);
1174 if(!sl)
1175 return -1;
1177 //now es is the complete es_descriptor of out mp4 ES stream
1178 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "ID: %d, FLAGS: 0x%x, subtype: %x\n", es->id, sl->flags, pes_es->subtype);
1180 n = 0;
1181 if(sl->au_start)
1182 pes_es->sl.au_start = au_start = getbits(buf, n++, 1);
1183 else
1184 pes_es->sl.au_start = (pes_es->sl.last_au_end ? 1 : 0);
1185 if(sl->au_end)
1186 pes_es->sl.au_end = au_end = getbits(buf, n++, 1);
1188 if(!sl->au_start && !sl->au_end)
1190 pes_es->sl.au_start = pes_es->sl.au_end = au_start = au_end = 1;
1192 pes_es->sl.last_au_end = pes_es->sl.au_end;
1195 if(sl->ocr_len > 0)
1196 ocr_flag = getbits(buf, n++, 1);
1197 if(sl->idle)
1198 idle = getbits(buf, n++, 1);
1199 if(sl->padding)
1200 padding = getbits(buf, n++, 1);
1201 if(padding)
1203 padding_bits = getbits(buf, n, 3);
1204 n += 3;
1207 if(idle || (padding && !padding_bits))
1209 pes_es->payload_size = 0;
1210 return -1;
1213 //(! idle && (!padding || padding_bits != 0)) is true
1214 n += sl->packet_seqnum_len;
1215 if(sl->degr_len)
1216 deg_flag = getbits(buf, n++, 1);
1217 if(deg_flag)
1218 n += sl->degr_len;
1220 if(ocr_flag)
1222 n += sl->ocr_len;
1223 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "OCR: %d bits\n", sl->ocr_len);
1226 if(packet_len * 8 <= n)
1227 return -1;
1229 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "\nAU_START: %d, AU_END: %d\n", au_start, au_end);
1230 if(au_start)
1232 int dts_flag = 0, cts_flag = 0, ib_flag = 0;
1234 if(sl->random_accesspoint)
1235 rap_flag = getbits(buf, n++, 1);
1237 //check commented because it seems it's rarely used, and we need this flag set in case of au_start
1238 //the decoder will eventually discard the payload if it can't decode it
1239 //if(rap_flag || sl->random_accesspoint_only)
1240 pes_es->is_synced = 1;
1242 n += sl->au_seqnum_len;
1243 if(packet_len * 8 <= n+8)
1244 return -1;
1245 if(sl->use_ts)
1247 dts_flag = getbits(buf, n++, 1);
1248 cts_flag = getbits(buf, n++, 1);
1250 if(sl->instant_bitrate_len)
1251 ib_flag = getbits(buf, n++, 1);
1252 if(packet_len * 8 <= n+8)
1253 return -1;
1254 if(dts_flag && (sl->ts_len > 0))
1256 n += sl->ts_len;
1257 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "DTS: %d bits\n", sl->ts_len);
1259 if(packet_len * 8 <= n+8)
1260 return -1;
1261 if(cts_flag && (sl->ts_len > 0))
1263 int i = 0, m;
1265 while(i < sl->ts_len)
1267 m = FFMIN(8, sl->ts_len - i);
1268 v |= getbits(buf, n, m);
1269 if(sl->ts_len - i > 8)
1270 v <<= 8;
1271 i += m;
1272 n += m;
1273 if(packet_len * 8 <= n+8)
1274 return -1;
1277 pes_es->pts = (double) v / (double) sl->ts_resolution;
1278 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "CTS: %d bits, value: %"PRIu64"/%d = %.3f\n", sl->ts_len, v, sl->ts_resolution, pes_es->pts);
1282 i = 0;
1283 pl_size = 0;
1284 while(i < sl->au_len)
1286 m = FFMIN(8, sl->au_len - i);
1287 pl_size |= getbits(buf, n, m);
1288 if(sl->au_len - i > 8)
1289 pl_size <<= 8;
1290 i += m;
1291 n += m;
1292 if(packet_len * 8 <= n+8)
1293 return -1;
1295 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "AU_LEN: %u (%d bits)\n", pl_size, sl->au_len);
1296 if(ib_flag)
1297 n += sl->instant_bitrate_len;
1300 m = (n+7)/8;
1301 if(0 < pl_size && pl_size < pes_es->payload_size)
1302 pes_es->payload_size = pl_size;
1304 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",
1305 n, m, pes_es->payload_size, pl_size, (int) rap_flag, (int) sl->random_accesspoint_only);
1307 return m;
1310 //this function parses the extension fields in the PES header and returns the substream_id, or -1 in case of errors
1311 static int parse_pes_extension_fields(unsigned char *p, int pkt_len)
1313 int skip;
1314 unsigned char flags;
1316 if(!(p[7] & 0x1)) //no extension_field
1317 return -1;
1318 skip = 9;
1319 if(p[7] & 0x80)
1321 skip += 5;
1322 if(p[7] & 0x40)
1323 skip += 5;
1325 if(p[7] & 0x20) //escr_flag
1326 skip += 6;
1327 if(p[7] & 0x10) //es_rate_flag
1328 skip += 3;
1329 if(p[7] & 0x08)//dsm_trick_mode is unsupported, skip
1331 skip = 0;//don't let's parse the extension fields
1333 if(p[7] & 0x04) //additional_copy_info
1334 skip += 1;
1335 if(p[7] & 0x02) //pes_crc_flag
1336 skip += 2;
1337 if(skip >= pkt_len) //too few bytes
1338 return -1;
1339 flags = p[skip];
1340 skip++;
1341 if(flags & 0x80) //pes_private_data_flag
1342 skip += 16;
1343 if(skip >= pkt_len)
1344 return -1;
1345 if(flags & 0x40) //pack_header_field_flag
1347 unsigned char l = p[skip];
1348 skip += l;
1350 if(flags & 0x20) //program_packet_sequence_counter
1351 skip += 2;
1352 if(flags & 0x10) //p_std
1353 skip += 2;
1354 if(skip >= pkt_len)
1355 return -1;
1356 if(flags & 0x01) //finally the long desired pes_extension2
1358 unsigned char l = p[skip]; //ext2 flag+len
1359 skip++;
1360 if((l == 0x81) && (skip < pkt_len))
1362 int ssid = p[skip];
1363 mp_msg(MSGT_IDENTIFY, MSGL_V, "SUBSTREAM_ID=%d (0x%02X)\n", ssid, ssid);
1364 return ssid;
1368 return -1;
1371 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)
1373 unsigned char *p;
1374 uint32_t header_len;
1375 int64_t pts;
1376 uint32_t stream_id;
1377 uint32_t pkt_len, pes_is_aligned;
1379 //Here we are always at the start of a PES packet
1380 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2(%p, %d): \n", buf, (uint32_t) packet_len);
1382 if(packet_len == 0 || packet_len > 184)
1384 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2, BUFFER LEN IS TOO SMALL OR TOO BIG: %d EXIT\n", packet_len);
1385 return 0;
1388 p = buf;
1389 pkt_len = packet_len;
1392 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: HEADER %02x %02x %02x %02x\n", p[0], p[1], p[2], p[3]);
1393 if (p[0] || p[1] || (p[2] != 1))
1395 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: error HEADER %02x %02x %02x (should be 0x000001) \n", p[0], p[1], p[2]);
1396 return 0 ;
1399 packet_len -= 6;
1400 if(packet_len==0)
1402 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: packet too short: %d, exit\n", packet_len);
1403 return 0;
1406 es->payload_size = (p[4] << 8 | p[5]);
1407 pes_is_aligned = (p[6] & 4);
1409 stream_id = p[3];
1412 if (p[7] & 0x80)
1413 { /* pts available */
1414 pts = (int64_t)(p[9] & 0x0E) << 29 ;
1415 pts |= p[10] << 22 ;
1416 pts |= (p[11] & 0xFE) << 14 ;
1417 pts |= p[12] << 7 ;
1418 pts |= (p[13] & 0xFE) >> 1 ;
1420 es->pts = pts / 90000.0;
1422 else
1423 es->pts = 0.0;
1426 header_len = p[8];
1429 if (header_len + 9 > pkt_len) //9 are the bytes read up to the header_length field
1431 mp_msg(MSGT_DEMUX, MSGL_DBG2, "demux_ts: illegal value for PES_header_data_length (0x%02x)\n", header_len);
1432 return 0;
1435 if(stream_id==0xfd)
1437 int ssid = parse_pes_extension_fields(p, pkt_len);
1438 if((audio_substream_id!=-1) && (ssid != audio_substream_id))
1439 return 0;
1440 if(ssid == 0x72 && type_from_pmt != AUDIO_DTS && type_from_pmt != SPU_PGS)
1441 es->type = type_from_pmt = AUDIO_TRUEHD;
1444 p += header_len + 9;
1445 packet_len -= header_len + 3;
1447 if(es->payload_size)
1448 es->payload_size -= header_len + 3;
1451 es->is_synced = 1; //only for SL streams we have to make sure it's really true, see below
1452 if (stream_id == 0xbd)
1454 mp_msg(MSGT_DEMUX, MSGL_DBG3, "pes_parse2: audio buf = %02X %02X %02X %02X %02X %02X %02X %02X, 80: %d\n",
1455 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[0] & 0x80);
1459 * we check the descriptor tag first because some stations
1460 * do not include any of the A52 header info in their audio tracks
1461 * these "raw" streams may begin with a byte that looks like a stream type.
1465 if(type_from_pmt == SPU_PGS)
1467 es->start = p;
1468 es->size = packet_len;
1469 es->type = SPU_PGS;
1470 es->payload_size -= packet_len;
1471 return 1;
1474 (type_from_pmt == AUDIO_A52) || /* A52 - raw */
1475 (packet_len >= 2 && p[0] == 0x0B && p[1] == 0x77) /* A52 - syncword */
1478 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 RAW OR SYNCWORD\n");
1479 es->start = p;
1480 es->size = packet_len;
1481 es->type = AUDIO_A52;
1482 es->payload_size -= packet_len;
1484 return 1;
1486 /* SPU SUBS */
1487 else if(type_from_pmt == SPU_DVB ||
1488 (packet_len >= 2 && (p[0] == 0x20) && pes_is_aligned)) // && p[1] == 0x00))
1490 // offset/length fiddling to make decoding with lavc possible
1491 es->start = p + 2;
1492 es->size = packet_len - 2;
1493 es->type = SPU_DVB;
1494 es->payload_size -= packet_len;
1496 return 1;
1498 else if (pes_is_aligned && packet_len >= 1 && ((p[0] & 0xE0) == 0x20)) //SPU_DVD
1500 //DVD SUBS
1501 es->start = p+1;
1502 es->size = packet_len-1;
1503 es->type = SPU_DVD;
1504 es->payload_size -= packet_len;
1506 return 1;
1508 else if (pes_is_aligned && packet_len >= 4 && (p[0] & 0xF8) == 0x80)
1510 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 WITH HEADER\n");
1511 es->start = p+4;
1512 es->size = packet_len - 4;
1513 es->type = AUDIO_A52;
1514 es->payload_size -= packet_len;
1516 return 1;
1518 else if (pes_is_aligned && packet_len >= 1 && ((p[0]&0xf0) == 0xa0))
1520 int pcm_offset;
1522 for (pcm_offset=0; ++pcm_offset < packet_len-1 ; )
1524 if (p[pcm_offset] == 0x01 && p[pcm_offset+1] == 0x80)
1525 { /* START */
1526 pcm_offset += 2;
1527 break;
1531 es->start = p + pcm_offset;
1532 es->size = packet_len - pcm_offset;
1533 es->type = AUDIO_LPCM_BE;
1534 es->payload_size -= packet_len;
1536 return 1;
1538 else
1540 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PES_PRIVATE1\n");
1541 es->start = p;
1542 es->size = packet_len;
1543 es->type = (type_from_pmt == UNKNOWN ? PES_PRIVATE1 : type_from_pmt);
1544 es->payload_size -= packet_len;
1546 return 1;
1549 else if(((stream_id >= 0xe0) && (stream_id <= 0xef)) || (stream_id == 0xfd && type_from_pmt != UNKNOWN))
1551 es->start = p;
1552 es->size = packet_len;
1553 if(type_from_pmt != UNKNOWN)
1554 es->type = type_from_pmt;
1555 else
1556 es->type = VIDEO_MPEG2;
1557 if(es->payload_size)
1558 es->payload_size -= packet_len;
1560 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: M2V size %d\n", es->size);
1561 return 1;
1563 else if ((stream_id == 0xfa))
1565 int l;
1567 es->is_synced = 0;
1568 if(type_from_pmt != UNKNOWN) //MP4 A/V or SL
1570 es->start = p;
1571 es->size = packet_len;
1572 es->type = type_from_pmt;
1574 if(type_from_pmt == SL_PES_STREAM)
1576 //if(pes_is_aligned)
1578 l = mp4_parse_sl_packet(pmt, p, packet_len, pid, es);
1579 mp_msg(MSGT_DEMUX, MSGL_DBG2, "L=%d, TYPE=%x\n", l, type_from_pmt);
1580 if(l < 0)
1582 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: couldn't parse SL header, passing along full PES payload\n");
1583 l = 0;
1587 es->start += l;
1588 es->size -= l;
1591 if(es->payload_size)
1592 es->payload_size -= packet_len;
1593 return 1;
1596 else if ((stream_id & 0xe0) == 0xc0)
1598 es->start = p;
1599 es->size = packet_len;
1601 if(type_from_pmt != UNKNOWN)
1602 es->type = type_from_pmt;
1603 else
1604 es->type = AUDIO_MP2;
1606 es->payload_size -= packet_len;
1608 return 1;
1610 else if (type_from_pmt != -1) //as a last resort here we trust the PMT, if present
1612 es->start = p;
1613 es->size = packet_len;
1614 es->type = type_from_pmt;
1615 es->payload_size -= packet_len;
1617 return 1;
1619 else
1621 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: unknown packet, id: %x\n", stream_id);
1624 es->is_synced = 0;
1625 return 0;
1631 static int ts_sync(stream_t *stream)
1633 mp_msg(MSGT_DEMUX, MSGL_DBG3, "TS_SYNC \n");
1635 while (!stream->eof)
1636 if (stream_read_char(stream) == 0x47)
1637 return 1;
1639 return 0;
1643 static void ts_dump_streams(ts_priv_t *priv)
1645 int i;
1647 for(i = 0; i < 3; i++)
1649 if((priv->fifo[i].pack != NULL) && (priv->fifo[i].offset != 0))
1651 resize_demux_packet(priv->fifo[i].pack, priv->fifo[i].offset);
1652 ds_add_packet(priv->fifo[i].ds, priv->fifo[i].pack);
1653 priv->fifo[i].offset = 0;
1654 priv->fifo[i].pack = NULL;
1660 static inline int32_t prog_idx_in_pat(ts_priv_t *priv, uint16_t progid)
1662 int x;
1664 if(priv->pat.progs == NULL)
1665 return -1;
1667 for(x = 0; x < priv->pat.progs_cnt; x++)
1669 if(priv->pat.progs[x].id == progid)
1670 return x;
1673 return -1;
1677 static inline int32_t prog_id_in_pat(ts_priv_t *priv, uint16_t pid)
1679 int x;
1681 if(priv->pat.progs == NULL)
1682 return -1;
1684 for(x = 0; x < priv->pat.progs_cnt; x++)
1686 if(priv->pat.progs[x].pmt_pid == pid)
1687 return priv->pat.progs[x].id;
1690 return -1;
1693 static int collect_section(ts_section_t *section, int is_start, unsigned char *buff, int size)
1695 uint8_t *ptr;
1696 uint16_t tlen;
1697 int skip, tid;
1699 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, start: %d, size: %d, collected: %d\n", is_start, size, section->buffer_len);
1700 if(! is_start && !section->buffer_len)
1701 return 0;
1703 if(is_start)
1705 if(! section->buffer)
1707 section->buffer = malloc(4096 + 256);
1708 if(section->buffer == NULL)
1709 return 0;
1711 section->buffer_len = 0;
1714 if(size + section->buffer_len > 4096+256)
1716 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, excessive len: %d + %d\n", section->buffer_len, size);
1717 return 0;
1720 memcpy(&(section->buffer[section->buffer_len]), buff, size);
1721 section->buffer_len += size;
1723 if(section->buffer_len < 3)
1724 return 0;
1726 skip = section->buffer[0];
1727 if(skip + 4 > section->buffer_len)
1728 return 0;
1730 ptr = &(section->buffer[skip + 1]);
1731 tid = ptr[0];
1732 tlen = ((ptr[1] & 0x0f) << 8) | ptr[2];
1733 mp_msg(MSGT_DEMUX, MSGL_V, "SKIP: %d+1, TID: %d, TLEN: %d, COLLECTED: %d\n", skip, tid, tlen, section->buffer_len);
1734 if(section->buffer_len < (skip+1+3+tlen))
1736 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DATA IS NOT ENOUGH, NEXT TIME\n");
1737 return 0;
1740 return skip+1;
1743 static int parse_pat(ts_priv_t * priv, int is_start, unsigned char *buff, int size)
1745 int skip;
1746 unsigned char *ptr;
1747 unsigned char *base;
1748 int entries, i;
1749 uint16_t progid;
1750 struct pat_progs_t *tmp;
1751 ts_section_t *section;
1753 section = &(priv->pat.section);
1754 skip = collect_section(section, is_start, buff, size);
1755 if(! skip)
1756 return 0;
1758 ptr = &(section->buffer[skip]);
1759 //PARSING
1760 priv->pat.table_id = ptr[0];
1761 if(priv->pat.table_id != 0)
1762 return 0;
1763 priv->pat.ssi = (ptr[1] >> 7) & 0x1;
1764 priv->pat.curr_next = ptr[5] & 0x01;
1765 priv->pat.ts_id = (ptr[3] << 8 ) | ptr[4];
1766 priv->pat.version_number = (ptr[5] >> 1) & 0x1F;
1767 priv->pat.section_length = ((ptr[1] & 0x03) << 8 ) | ptr[2];
1768 priv->pat.section_number = ptr[6];
1769 priv->pat.last_section_number = ptr[7];
1771 //check_crc32(0xFFFFFFFFL, ptr, priv->pat.buffer_len - 4, &ptr[priv->pat.buffer_len - 4]);
1772 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);
1774 entries = (int) (priv->pat.section_length - 9) / 4; //entries per section
1776 for(i=0; i < entries; i++)
1778 int32_t idx;
1779 base = &ptr[8 + i*4];
1780 progid = (base[0] << 8) | base[1];
1782 if((idx = prog_idx_in_pat(priv, progid)) == -1)
1784 int sz = sizeof(struct pat_progs_t) * (priv->pat.progs_cnt+1);
1785 tmp = realloc_struct(priv->pat.progs, priv->pat.progs_cnt+1, sizeof(struct pat_progs_t));
1786 if(tmp == NULL)
1788 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PAT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
1789 break;
1791 priv->pat.progs = tmp;
1792 idx = priv->pat.progs_cnt;
1793 priv->pat.progs_cnt++;
1796 priv->pat.progs[idx].id = progid;
1797 priv->pat.progs[idx].pmt_pid = ((base[2] & 0x1F) << 8) | base[3];
1798 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);
1799 mp_msg(MSGT_IDENTIFY, MSGL_V, "PROGRAM_ID=%d (0x%02X), PMT_PID: %d(0x%02X)\n",
1800 progid, progid, priv->pat.progs[idx].pmt_pid, priv->pat.progs[idx].pmt_pid);
1803 return 1;
1807 static inline int32_t es_pid_in_pmt(pmt_t * pmt, uint16_t pid)
1809 uint16_t i;
1811 if(pmt == NULL)
1812 return -1;
1814 if(pmt->es == NULL)
1815 return -1;
1817 for(i = 0; i < pmt->es_cnt; i++)
1819 if(pmt->es[i].pid == pid)
1820 return (int32_t) i;
1823 return -1;
1827 static uint16_t get_mp4_desc_len(uint8_t *buf, int *len)
1829 //uint16_t i = 0, size = 0;
1830 int i = 0, j, size = 0;
1832 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PARSE_MP4_DESC_LEN(%d), bytes: ", *len);
1833 j = FFMIN(*len, 4);
1834 while(i < j)
1836 mp_msg(MSGT_DEMUX, MSGL_DBG2, " %x ", buf[i]);
1837 size |= (buf[i] & 0x7f);
1838 if(!(buf[i] & 0x80))
1839 break;
1840 size <<= 7;
1841 i++;
1843 mp_msg(MSGT_DEMUX, MSGL_DBG2, ", SIZE=%d\n", size);
1845 *len = i+1;
1846 return size;
1850 static uint16_t parse_mp4_slconfig_descriptor(uint8_t *buf, int len, void *elem)
1852 int i = 0;
1853 mp4_es_descr_t *es;
1854 mp4_sl_config_t *sl;
1856 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_SLCONFIG_DESCRIPTOR(%d)\n", len);
1857 es = (mp4_es_descr_t *) elem;
1858 if(!es)
1860 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1861 return len;
1863 sl = &(es->sl);
1865 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;
1866 sl->ocr = sl->dts = sl->cts = 0;
1868 if(buf[0] == 0)
1870 i++;
1871 sl->flags = buf[i];
1872 i++;
1873 sl->ts_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1874 i += 4;
1875 sl->ocr_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1876 i += 4;
1877 sl->ts_len = buf[i];
1878 i++;
1879 sl->ocr_len = buf[i];
1880 i++;
1881 sl->au_len = buf[i];
1882 i++;
1883 sl->instant_bitrate_len = buf[i];
1884 i++;
1885 sl->degr_len = (buf[i] >> 4) & 0x0f;
1886 sl->au_seqnum_len = ((buf[i] & 0x0f) << 1) | ((buf[i+1] >> 7) & 0x01);
1887 i++;
1888 sl->packet_seqnum_len = ((buf[i] >> 2) & 0x1f);
1889 i++;
1892 else if(buf[0] == 1)
1894 sl->flags = 0;
1895 sl->ts_resolution = 1000;
1896 sl->ts_len = 32;
1897 i++;
1899 else if(buf[0] == 2)
1901 sl->flags = 4;
1902 i++;
1904 else
1906 sl->flags = 0;
1907 i++;
1910 sl->au_start = (sl->flags >> 7) & 0x1;
1911 sl->au_end = (sl->flags >> 6) & 0x1;
1912 sl->random_accesspoint = (sl->flags >> 5) & 0x1;
1913 sl->random_accesspoint_only = (sl->flags >> 4) & 0x1;
1914 sl->padding = (sl->flags >> 3) & 0x1;
1915 sl->use_ts = (sl->flags >> 2) & 0x1;
1916 sl->idle = (sl->flags >> 1) & 0x1;
1917 sl->duration = sl->flags & 0x1;
1919 if(sl->duration)
1921 sl->timescale = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1922 i += 4;
1923 sl->au_duration = (buf[i] << 8) | buf[i+1];
1924 i += 2;
1925 sl->cts_duration = (buf[i] << 8) | buf[i+1];
1926 i += 2;
1928 else //no support for fixed durations atm
1929 sl->timescale = sl->au_duration = sl->cts_duration = 0;
1931 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",
1932 len, buf[0], sl->flags, sl->use_ts, sl->ts_len, sl->timescale, (uint64_t) sl->dts, (uint64_t) sl->cts);
1934 return len;
1937 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem);
1939 static uint16_t parse_mp4_decoder_config_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
1941 int i = 0, j;
1942 mp4_es_descr_t *es;
1943 mp4_decoder_config_t *dec;
1945 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_CONFIG_DESCRIPTOR(%d)\n", len);
1946 es = (mp4_es_descr_t *) elem;
1947 if(!es)
1949 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1950 return len;
1952 dec = (mp4_decoder_config_t*) &(es->decoder);
1954 dec->object_type = buf[i];
1955 dec->stream_type = (buf[i+1]>>2) & 0x3f;
1957 if(dec->object_type == 1 && dec->stream_type == 1)
1959 dec->object_type = MP4_OD;
1960 dec->stream_type = MP4_OD;
1962 else if(dec->stream_type == 4)
1964 if(dec->object_type == 0x6a)
1965 dec->object_type = VIDEO_MPEG1;
1966 if(dec->object_type >= 0x60 && dec->object_type <= 0x65)
1967 dec->object_type = VIDEO_MPEG2;
1968 else if(dec->object_type == 0x20)
1969 dec->object_type = VIDEO_MPEG4;
1970 else if(dec->object_type == 0x21)
1971 dec->object_type = VIDEO_AVC;
1972 /*else if(dec->object_type == 0x22)
1973 fprintf(stderr, "TYPE 0x22\n");*/
1974 else dec->object_type = UNKNOWN;
1976 else if(dec->stream_type == 5)
1978 if(dec->object_type == 0x40)
1979 dec->object_type = AUDIO_AAC;
1980 else if(dec->object_type == 0x6b)
1981 dec->object_type = AUDIO_MP2;
1982 else if(dec->object_type >= 0x66 && dec->object_type <= 0x69)
1983 dec->object_type = AUDIO_MP2;
1984 else
1985 dec->object_type = UNKNOWN;
1987 else
1988 dec->object_type = dec->stream_type = UNKNOWN;
1990 if(dec->object_type != UNKNOWN)
1992 //update the type of the current stream
1993 for(j = 0; j < pmt->es_cnt; j++)
1995 if(pmt->es[j].mp4_es_id == es->id)
1997 pmt->es[j].type = SL_PES_STREAM;
2002 if(len > 13)
2003 parse_mp4_descriptors(pmt, &buf[13], len-13, dec);
2005 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);
2007 return len;
2010 static uint16_t parse_mp4_decoder_specific_descriptor(uint8_t *buf, int len, void *elem)
2012 int i;
2013 mp4_decoder_config_t *dec;
2015 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_SPECIFIC_DESCRIPTOR(%d)\n", len);
2016 dec = (mp4_decoder_config_t *) elem;
2017 if(!dec)
2019 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
2020 return len;
2023 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4 SPECIFIC INFO BYTES: \n");
2024 for(i=0; i<len; i++)
2025 mp_msg(MSGT_DEMUX, MSGL_DBG2, "%02x ", buf[i]);
2026 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\n");
2028 if(len > MAX_EXTRADATA_SIZE)
2030 mp_msg(MSGT_DEMUX, MSGL_ERR, "DEMUX_TS, EXTRADATA SUSPICIOUSLY BIG: %d, REFUSED\r\n", len);
2031 return len;
2033 memcpy(dec->buf, buf, len);
2034 dec->buf_size = len;
2036 return len;
2039 static uint16_t parse_mp4_es_descriptor(pmt_t *pmt, uint8_t *buf, int len)
2041 int i = 0, j = 0, k, found;
2042 uint8_t flag;
2043 mp4_es_descr_t es, *target_es = NULL, *tmp;
2045 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4ES: len=%d\n", len);
2046 memset(&es, 0, sizeof(mp4_es_descr_t));
2047 while(i < len)
2049 es.id = (buf[i] << 8) | buf[i+1];
2050 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_ID: %d\n", es.id);
2051 i += 2;
2052 flag = buf[i];
2053 i++;
2054 if(flag & 0x80)
2055 i += 2;
2056 if(flag & 0x40)
2057 i += buf[i]+1;
2058 if(flag & 0x20) //OCR, maybe we need it
2059 i += 2;
2061 j = parse_mp4_descriptors(pmt, &buf[i], len-i, &es);
2062 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);
2063 if(es.decoder.object_type != UNKNOWN && es.decoder.stream_type != UNKNOWN)
2065 found = 0;
2066 //search this ES_ID if we already have it
2067 for(k=0; k < pmt->mp4es_cnt; k++)
2069 if(pmt->mp4es[k].id == es.id)
2071 target_es = &(pmt->mp4es[k]);
2072 found = 1;
2076 if(! found)
2078 tmp = realloc_struct(pmt->mp4es, pmt->mp4es_cnt+1, sizeof(mp4_es_descr_t));
2079 if(tmp == NULL)
2081 fprintf(stderr, "CAN'T REALLOC MP4_ES_DESCR\n");
2082 continue;
2084 pmt->mp4es = tmp;
2085 target_es = &(pmt->mp4es[pmt->mp4es_cnt]);
2086 pmt->mp4es_cnt++;
2088 memcpy(target_es, &es, sizeof(mp4_es_descr_t));
2089 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_CNT: %d, ID=%d\n", pmt->mp4es_cnt, target_es->id);
2092 i += j;
2095 return len;
2098 static void parse_mp4_object_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2100 int i, j = 0, id;
2102 i=0;
2103 id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2104 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_OBJECT_DESCRIPTOR: len=%d, OD_ID=%d\n", len, id);
2105 if(buf[1] & 0x20)
2107 i += buf[2] + 1; //url
2108 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2110 else
2112 i = 2;
2114 while(i < len)
2116 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2117 mp_msg(MSGT_DEMUX, MSGL_V, "OBJD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2118 i += j;
2124 static void parse_mp4_iod(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2126 int i, j = 0;
2127 mp4_od_t *iod = &(pmt->iod);
2129 iod->id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2130 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_IOD: len=%d, IOD_ID=%d\n", len, iod->id);
2131 i = 2;
2132 if(buf[1] & 0x20)
2134 i += buf[2] + 1; //url
2135 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2137 else
2139 i = 7;
2140 while(i < len)
2142 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2143 mp_msg(MSGT_DEMUX, MSGL_V, "IOD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2144 i += j;
2149 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2151 int tag, descr_len, i = 0, j = 0;
2153 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DESCRIPTORS, len=%d\n", len);
2154 if(! len)
2155 return len;
2157 while(i < len)
2159 tag = buf[i];
2160 j = len - i -1;
2161 descr_len = get_mp4_desc_len(&(buf[i+1]), &j);
2162 mp_msg(MSGT_DEMUX, MSGL_V, "TAG=%d (0x%x), DESCR_len=%d, len=%d, j=%d\n", tag, tag, descr_len, len, j);
2163 if(descr_len > len - j+1)
2165 mp_msg(MSGT_DEMUX, MSGL_V, "descriptor is too long, exit\n");
2166 return len;
2168 i += j+1;
2170 switch(tag)
2172 case 0x1:
2173 parse_mp4_object_descriptor(pmt, &(buf[i]), descr_len, elem);
2174 break;
2175 case 0x2:
2176 parse_mp4_iod(pmt, &(buf[i]), descr_len, elem);
2177 break;
2178 case 0x3:
2179 parse_mp4_es_descriptor(pmt, &(buf[i]), descr_len);
2180 break;
2181 case 0x4:
2182 parse_mp4_decoder_config_descriptor(pmt, &buf[i], descr_len, elem);
2183 break;
2184 case 0x05:
2185 parse_mp4_decoder_specific_descriptor(&buf[i], descr_len, elem);
2186 break;
2187 case 0x6:
2188 parse_mp4_slconfig_descriptor(&buf[i], descr_len, elem);
2189 break;
2190 default:
2191 mp_msg(MSGT_DEMUX, MSGL_V, "Unsupported mp4 descriptor 0x%x\n", tag);
2193 i += descr_len;
2196 return len;
2199 static ES_stream_t *new_pid(ts_priv_t *priv, int pid)
2201 ES_stream_t *tss;
2203 tss = calloc(sizeof(*tss), 1);
2204 if(! tss)
2205 return NULL;
2206 tss->pid = pid;
2207 tss->last_cc = -1;
2208 tss->type = UNKNOWN;
2209 tss->subtype = UNKNOWN;
2210 tss->is_synced = 0;
2211 tss->extradata = NULL;
2212 tss->extradata_alloc = tss->extradata_len = 0;
2213 priv->ts.pids[pid] = tss;
2215 return tss;
2219 static int parse_program_descriptors(pmt_t *pmt, uint8_t *buf, uint16_t len)
2221 uint16_t i = 0, k, olen = len;
2223 while(len > 0)
2225 mp_msg(MSGT_DEMUX, MSGL_V, "PROG DESCR, TAG=%x, LEN=%d(%x)\n", buf[i], buf[i+1], buf[i+1]);
2226 if(buf[i+1] > len-2)
2228 mp_msg(MSGT_DEMUX, MSGL_V, "ERROR, descriptor len is too long, skipping\n");
2229 return olen;
2232 if(buf[i] == 0x1d)
2234 if(buf[i+3] == 2) //buggy versions of vlc muxer make this non-standard mess (missing iod_scope)
2235 k = 3;
2236 else
2237 k = 4; //this is standard compliant
2238 parse_mp4_descriptors(pmt, &buf[i+k], (int) buf[i+1]-(k-2), NULL);
2241 len -= 2 + buf[i+1];
2244 return olen;
2247 static int parse_descriptors(struct pmt_es_t *es, uint8_t *ptr)
2249 int j, descr_len, len;
2251 j = 0;
2252 len = es->descr_length;
2253 while(len > 2)
2255 descr_len = ptr[j+1];
2256 mp_msg(MSGT_DEMUX, MSGL_V, "...descr id: 0x%x, len=%d\n", ptr[j], descr_len);
2257 if(descr_len > len)
2259 mp_msg(MSGT_DEMUX, MSGL_ERR, "INVALID DESCR LEN for tag %02x: %d vs %d max, EXIT LOOP\n", ptr[j], descr_len, len);
2260 return -1;
2264 if(ptr[j] == 0x6a || ptr[j] == 0x7a) //A52 Descriptor
2266 if(es->type == 0x6)
2268 es->type = AUDIO_A52;
2269 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB A52 Descriptor\n");
2272 else if(ptr[j] == 0x7b) //DVB DTS Descriptor
2274 if(es->type == 0x6)
2276 es->type = AUDIO_DTS;
2277 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB DTS Descriptor\n");
2280 else if(ptr[j] == 0x56) // Teletext
2282 if(descr_len >= 5) {
2283 memcpy(es->lang, ptr+2, 3);
2284 es->lang[3] = 0;
2286 es->type = SPU_TELETEXT;
2288 else if(ptr[j] == 0x59) //Subtitling Descriptor
2290 uint8_t subtype;
2292 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Subtitling Descriptor\n");
2293 if(descr_len < 8)
2295 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Descriptor length too short for DVB Subtitle Descriptor: %d, SKIPPING\n", descr_len);
2297 else
2299 memcpy(es->lang, &ptr[j+2], 3);
2300 es->lang[3] = 0;
2301 subtype = ptr[j+5];
2303 (subtype >= 0x10 && subtype <= 0x13) ||
2304 (subtype >= 0x20 && subtype <= 0x23)
2307 es->type = SPU_DVB;
2308 //page parameters: compo page 2 bytes, ancillary page 2 bytes
2310 else
2311 es->type = UNKNOWN;
2314 else if(ptr[j] == 0x50) //Component Descriptor
2316 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Component Descriptor\n");
2317 memcpy(es->lang, &ptr[j+5], 3);
2318 es->lang[3] = 0;
2320 else if(ptr[j] == 0xa) //Language Descriptor
2322 memcpy(es->lang, &ptr[j+2], 3);
2323 es->lang[3] = 0;
2324 mp_msg(MSGT_DEMUX, MSGL_V, "Language Descriptor: %s\n", es->lang);
2326 else if(ptr[j] == 0x5) //Registration Descriptor (looks like e fourCC :) )
2328 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor\n");
2329 if(descr_len < 4)
2331 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor length too short: %d, SKIPPING\n", descr_len);
2333 else
2335 char *d;
2336 memcpy(es->format_descriptor, &ptr[j+2], 4);
2337 es->format_descriptor[4] = 0;
2339 d = &ptr[j+2];
2340 if(d[0] == 'A' && d[1] == 'C' && d[2] == '-' && d[3] == '3')
2342 es->type = AUDIO_A52;
2344 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '1')
2346 es->type = AUDIO_DTS;
2348 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '2')
2350 es->type = AUDIO_DTS;
2352 else if(d[0] == 'V' && d[1] == 'C' && d[2] == '-' && d[3] == '1')
2354 es->type = VIDEO_VC1;
2356 else if(d[0] == 'd' && d[1] == 'r' && d[2] == 'a' && d[3] == 'c')
2358 es->type = VIDEO_DIRAC;
2360 else
2361 es->type = UNKNOWN;
2362 mp_msg(MSGT_DEMUX, MSGL_DBG2, "FORMAT %s\n", es->format_descriptor);
2365 else if(ptr[j] == 0x1e || ptr[j] == 0x1f)
2367 // 0x1f is FMC, but currently it is easiest to handle them the same way
2368 es->mp4_es_id = (ptr[j+2] << 8) | ptr[j+3];
2369 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);
2371 else
2372 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Unknown descriptor 0x%x, SKIPPING\n", ptr[j]);
2374 len -= 2 + descr_len;
2375 j += 2 + descr_len;
2378 return 1;
2381 static int parse_sl_section(pmt_t *pmt, ts_section_t *section, int is_start, unsigned char *buff, int size)
2383 int tid, len, skip;
2384 uint8_t *ptr;
2385 skip = collect_section(section, is_start, buff, size);
2386 if(! skip)
2387 return 0;
2389 ptr = &(section->buffer[skip]);
2390 tid = ptr[0];
2391 len = ((ptr[1] & 0x0f) << 8) | ptr[2];
2392 mp_msg(MSGT_DEMUX, MSGL_V, "TABLEID: %d (av. %d), skip=%d, LEN: %d\n", tid, section->buffer_len, skip, len);
2393 if(len > 4093 || section->buffer_len < len || tid != 5)
2395 mp_msg(MSGT_DEMUX, MSGL_V, "SECTION TOO LARGE or wrong section type, EXIT\n");
2396 return 0;
2399 if(! (ptr[5] & 1))
2400 return 0;
2402 //8 is the current position, len - 9 is the amount of data available
2403 parse_mp4_descriptors(pmt, &ptr[8], len - 9, NULL);
2405 return 1;
2408 static int parse_pmt(ts_priv_t * priv, uint16_t progid, uint16_t pid, int is_start, unsigned char *buff, int size)
2410 unsigned char *base, *es_base;
2411 pmt_t *pmt;
2412 int32_t idx, es_count, section_bytes;
2413 uint8_t m=0;
2414 int skip;
2415 pmt_t *tmp;
2416 struct pmt_es_t *tmp_es;
2417 ts_section_t *section;
2418 ES_stream_t *tss;
2419 int i;
2421 idx = progid_idx_in_pmt(priv, progid);
2423 if(idx == -1)
2425 int sz = (priv->pmt_cnt + 1) * sizeof(pmt_t);
2426 tmp = realloc_struct(priv->pmt, priv->pmt_cnt + 1, sizeof(pmt_t));
2427 if(tmp == NULL)
2429 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
2430 return 0;
2432 priv->pmt = tmp;
2433 idx = priv->pmt_cnt;
2434 memset(&(priv->pmt[idx]), 0, sizeof(pmt_t));
2435 priv->pmt_cnt++;
2436 priv->pmt[idx].progid = progid;
2439 pmt = &(priv->pmt[idx]);
2441 section = &(pmt->section);
2442 skip = collect_section(section, is_start, buff, size);
2443 if(! skip)
2444 return 0;
2446 base = &(section->buffer[skip]);
2448 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",
2449 progid, pmt->section.buffer_len, is_start, pid, size, m, pmt->es_cnt, idx, pmt);
2451 pmt->table_id = base[0];
2452 if(pmt->table_id != 2)
2453 return -1;
2454 pmt->ssi = base[1] & 0x80;
2455 pmt->section_length = (((base[1] & 0xf) << 8 ) | base[2]);
2456 pmt->version_number = (base[5] >> 1) & 0x1f;
2457 pmt->curr_next = (base[5] & 1);
2458 pmt->section_number = base[6];
2459 pmt->last_section_number = base[7];
2460 pmt->PCR_PID = ((base[8] & 0x1f) << 8 ) | base[9];
2461 pmt->prog_descr_length = ((base[10] & 0xf) << 8 ) | base[11];
2462 if(pmt->prog_descr_length > pmt->section_length - 9)
2464 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, INVALID PROG_DESCR LENGTH (%d vs %d)\n", pmt->prog_descr_length, pmt->section_length - 9);
2465 return -1;
2468 if(pmt->prog_descr_length)
2469 parse_program_descriptors(pmt, &base[12], pmt->prog_descr_length);
2471 es_base = &base[12 + pmt->prog_descr_length]; //the beginning of th ES loop
2473 section_bytes= pmt->section_length - 13 - pmt->prog_descr_length;
2474 es_count = 0;
2476 while(section_bytes >= 5)
2478 int es_pid, es_type;
2480 es_type = es_base[0];
2481 es_pid = ((es_base[1] & 0x1f) << 8) | es_base[2];
2483 idx = es_pid_in_pmt(pmt, es_pid);
2484 if(idx == -1)
2486 int sz = sizeof(struct pmt_es_t) * (pmt->es_cnt + 1);
2487 tmp_es = realloc_struct(pmt->es, pmt->es_cnt + 1, sizeof(struct pmt_es_t));
2488 if(tmp_es == NULL)
2490 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT, COULDN'T ALLOCATE %d bytes for PMT_ES\n", sz);
2491 continue;
2493 pmt->es = tmp_es;
2494 idx = pmt->es_cnt;
2495 memset(&(pmt->es[idx]), 0, sizeof(struct pmt_es_t));
2496 pmt->es_cnt++;
2499 pmt->es[idx].descr_length = ((es_base[3] & 0xf) << 8) | es_base[4];
2502 if(pmt->es[idx].descr_length > section_bytes - 5)
2504 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, ES_DESCR_LENGTH TOO LARGE %d > %d, EXIT\n",
2505 pmt->es[idx].descr_length, section_bytes - 5);
2506 return -1;
2510 pmt->es[idx].pid = es_pid;
2511 if(es_type != 0x6)
2512 pmt->es[idx].type = UNKNOWN;
2513 else
2514 pmt->es[idx].type = es_type;
2516 parse_descriptors(&pmt->es[idx], &es_base[5]);
2518 switch(es_type)
2520 case 1:
2521 pmt->es[idx].type = VIDEO_MPEG1;
2522 break;
2523 case 2:
2524 pmt->es[idx].type = VIDEO_MPEG2;
2525 break;
2526 case 3:
2527 case 4:
2528 pmt->es[idx].type = AUDIO_MP2;
2529 break;
2530 case 6:
2531 if(pmt->es[idx].type == 0x6) //this could have been ovrwritten by parse_descriptors
2532 pmt->es[idx].type = UNKNOWN;
2533 break;
2534 case 0x10:
2535 pmt->es[idx].type = VIDEO_MPEG4;
2536 break;
2537 case 0x0f:
2538 pmt->es[idx].type = AUDIO_AAC;
2539 break;
2540 case 0x11:
2541 pmt->es[idx].type = AUDIO_AAC_LATM;
2542 for (i = 0; i < pmt->mp4es_cnt; i++)
2543 if (pmt->mp4es[i].id == pmt->es[idx].mp4_es_id &&
2544 pmt->mp4es[i].decoder.object_type == AUDIO_AAC)
2545 pmt->es[idx].type = AUDIO_AAC;
2546 break;
2547 case 0x1b:
2548 pmt->es[idx].type = VIDEO_H264;
2549 break;
2550 case 0x12:
2551 pmt->es[idx].type = SL_PES_STREAM;
2552 break;
2553 case 0x13:
2554 pmt->es[idx].type = SL_SECTION;
2555 break;
2556 case 0x81:
2557 pmt->es[idx].type = AUDIO_A52;
2558 break;
2559 case 0x8A:
2560 case 0x82:
2561 case 0x85:
2562 case 0x86:
2563 pmt->es[idx].type = AUDIO_DTS;
2564 break;
2565 case 0x90:
2566 pmt->es[idx].type = SPU_PGS;
2567 break;
2568 case 0xD1:
2569 pmt->es[idx].type = VIDEO_DIRAC;
2570 break;
2571 case 0xEA:
2572 pmt->es[idx].type = VIDEO_VC1;
2573 break;
2574 default:
2575 mp_msg(MSGT_DEMUX, MSGL_DBG2, "UNKNOWN ES TYPE=0x%x\n", es_type);
2576 pmt->es[idx].type = UNKNOWN;
2579 tss = priv->ts.pids[es_pid]; //an ES stream
2580 if(tss == NULL)
2582 tss = new_pid(priv, es_pid);
2583 if(tss)
2584 tss->type = pmt->es[idx].type;
2587 section_bytes -= 5 + pmt->es[idx].descr_length;
2588 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",
2589 progid, idx, es_count, pmt->es[idx].pid, pmt->es[idx].pid, pmt->es[idx].type, pmt->es[idx].descr_length, section_bytes);
2592 es_base += 5 + pmt->es[idx].descr_length;
2594 es_count++;
2597 mp_msg(MSGT_DEMUX, MSGL_V, "----------------------------\n");
2598 return 1;
2601 static pmt_t* pmt_of_pid(ts_priv_t *priv, int pid, mp4_decoder_config_t **mp4_dec)
2603 int32_t i, j, k;
2605 if(priv->pmt)
2607 for(i = 0; i < priv->pmt_cnt; i++)
2609 if(priv->pmt[i].es && priv->pmt[i].es_cnt)
2611 for(j = 0; j < priv->pmt[i].es_cnt; j++)
2613 if(priv->pmt[i].es[j].pid == pid)
2615 //search mp4_es_id
2616 if(priv->pmt[i].es[j].mp4_es_id)
2618 for(k = 0; k < priv->pmt[i].mp4es_cnt; k++)
2620 if(priv->pmt[i].mp4es[k].id == priv->pmt[i].es[j].mp4_es_id)
2622 *mp4_dec = &(priv->pmt[i].mp4es[k].decoder);
2623 break;
2628 return &(priv->pmt[i]);
2635 return NULL;
2639 static inline int32_t pid_type_from_pmt(ts_priv_t *priv, int pid)
2641 int32_t pmt_idx, pid_idx, i, j;
2643 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2645 if(pmt_idx != -1)
2647 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2648 if(pid_idx != -1)
2649 return priv->pmt[pmt_idx].es[pid_idx].type;
2651 //else
2653 for(i = 0; i < priv->pmt_cnt; i++)
2655 pmt_t *pmt = &(priv->pmt[i]);
2656 for(j = 0; j < pmt->es_cnt; j++)
2657 if(pmt->es[j].pid == pid)
2658 return pmt->es[j].type;
2662 return UNKNOWN;
2666 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid)
2668 int32_t pmt_idx, pid_idx, i, j;
2670 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2672 if(pmt_idx != -1)
2674 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2675 if(pid_idx != -1)
2676 return priv->pmt[pmt_idx].es[pid_idx].lang;
2678 else
2680 for(i = 0; i < priv->pmt_cnt; i++)
2682 pmt_t *pmt = &(priv->pmt[i]);
2683 for(j = 0; j < pmt->es_cnt; j++)
2684 if(pmt->es[j].pid == pid)
2685 return pmt->es[j].lang;
2689 return NULL;
2693 static int fill_packet(demuxer_t *demuxer, demux_stream_t *ds, demux_packet_t **dp, int *dp_offset, TS_stream_info *si)
2695 int ret = 0;
2697 if(*dp && *dp_offset <= 0)
2699 free_demux_packet(*dp);
2700 *dp = NULL;
2702 if(*dp)
2704 ret = *dp_offset;
2705 resize_demux_packet(*dp, ret); //shrinked to the right size
2706 ds_add_packet(ds, *dp);
2707 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);
2708 if(si)
2710 float diff = (*dp)->pts - si->last_pts;
2711 float dur;
2713 if(abs(diff) > 1) //1 second, there's a discontinuity
2715 si->duration += si->last_pts - si->first_pts;
2716 si->first_pts = si->last_pts = (*dp)->pts;
2718 else
2720 si->last_pts = (*dp)->pts;
2722 si->size += ret;
2723 dur = si->duration + (si->last_pts - si->first_pts);
2725 if(dur > 0 && ds == demuxer->video)
2727 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2728 if(dur > 1) //otherwise it may be unreliable
2729 priv->vbitrate = (uint32_t) ((float) si->size / dur);
2734 *dp = NULL;
2735 *dp_offset = 0;
2737 return ret;
2740 static int fill_extradata(mp4_decoder_config_t * mp4_dec, ES_stream_t *tss)
2742 uint8_t *tmp;
2744 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4_dec: %p, pid: %d\n", mp4_dec, tss->pid);
2746 if(mp4_dec->buf_size > tss->extradata_alloc)
2748 tmp = realloc(tss->extradata, mp4_dec->buf_size);
2749 if(!tmp)
2750 return 0;
2751 tss->extradata = tmp;
2752 tss->extradata_alloc = mp4_dec->buf_size;
2754 memcpy(tss->extradata, mp4_dec->buf, mp4_dec->buf_size);
2755 tss->extradata_len = mp4_dec->buf_size;
2756 mp_msg(MSGT_DEMUX, MSGL_V, "EXTRADATA: %p, alloc=%d, len=%d\n", tss->extradata, tss->extradata_alloc, tss->extradata_len);
2758 return tss->extradata_len;
2761 // 0 = EOF or no stream found
2762 // else = [-] number of bytes written to the packet
2763 static int ts_parse(demuxer_t *demuxer , ES_stream_t *es, unsigned char *packet, int probe)
2765 ES_stream_t *tss;
2766 int buf_size, is_start, pid, base;
2767 int len, cc, cc_ok av_unused, afc, retv = 0, is_video, is_audio, is_sub;
2768 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2769 stream_t *stream = demuxer->stream;
2770 char *p;
2771 demux_stream_t *ds = NULL;
2772 demux_packet_t **dp = NULL;
2773 int *dp_offset = 0, *buffer_size = 0;
2774 int32_t progid, pid_type, bad, ts_error;
2775 int junk = 0, rap_flag = 0;
2776 pmt_t *pmt;
2777 mp4_decoder_config_t *mp4_dec;
2778 TS_stream_info *si;
2781 memset(es, 0, sizeof(*es));
2782 while(1)
2784 bad = ts_error = 0;
2785 ds = NULL;
2786 dp = NULL;
2787 dp_offset = buffer_size = NULL;
2788 rap_flag = 0;
2789 mp4_dec = NULL;
2790 es->is_synced = 0;
2791 es->lang[0] = 0;
2792 si = NULL;
2794 junk = priv->ts.packet_size - TS_PACKET_SIZE;
2795 buf_size = priv->ts.packet_size - junk;
2797 if(stream_eof(stream))
2799 if(! probe)
2801 ts_dump_streams(priv);
2802 demuxer->filepos = stream_tell(demuxer->stream);
2805 return 0;
2809 if(! ts_sync(stream))
2811 mp_msg(MSGT_DEMUX, MSGL_INFO, "TS_PARSE: COULDN'T SYNC\n");
2812 return 0;
2815 len = stream_read(stream, &packet[1], 3);
2816 if (len != 3)
2817 return 0;
2818 buf_size -= 4;
2820 if((packet[1] >> 7) & 0x01) //transport error
2821 ts_error = 1;
2824 is_start = packet[1] & 0x40;
2825 pid = ((packet[1] & 0x1f) << 8) | packet[2];
2827 tss = priv->ts.pids[pid]; //an ES stream
2828 if(tss == NULL)
2830 tss = new_pid(priv, pid);
2831 if(tss == NULL)
2832 continue;
2835 cc = (packet[3] & 0xf);
2836 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
2837 tss->last_cc = cc;
2839 bad = ts_error; // || (! cc_ok);
2840 if(bad)
2842 if(priv->keep_broken == 0)
2844 stream_skip(stream, buf_size-1+junk);
2845 continue;
2848 is_start = 0; //queued to the packet data
2851 if(is_start)
2852 tss->is_synced = 1;
2854 if((!is_start && !tss->is_synced) || ((pid > 1) && (pid < 16)) || (pid == 8191)) //invalid pid
2856 stream_skip(stream, buf_size-1+junk);
2857 continue;
2861 afc = (packet[3] >> 4) & 3;
2862 if(! (afc % 2)) //no payload in this TS packet
2864 stream_skip(stream, buf_size-1+junk);
2865 continue;
2868 if(afc > 1)
2870 int c;
2871 c = stream_read_char(stream);
2872 buf_size--;
2873 if(c < 0 || c > 183) //broken from the stream layer or invalid
2875 stream_skip(stream, buf_size-1+junk);
2876 continue;
2879 //c==0 is allowed!
2880 if(c > 0)
2882 uint8_t pcrbuf[188];
2883 int flags = stream_read_char(stream);
2884 int has_pcr;
2885 rap_flag = (flags & 0x40) >> 6;
2886 has_pcr = flags & 0x10;
2888 buf_size--;
2889 c--;
2890 stream_read(stream, pcrbuf, c);
2892 if(has_pcr)
2894 int pcr_pid = prog_pcr_pid(priv, priv->prog);
2895 if(pcr_pid == pid)
2897 uint64_t pcr, pcr_ext;
2899 pcr = (int64_t)(pcrbuf[0]) << 25;
2900 pcr |= pcrbuf[1] << 17 ;
2901 pcr |= (pcrbuf[2]) << 9;
2902 pcr |= pcrbuf[3] << 1 ;
2903 pcr |= (pcrbuf[4] & 0x80) >> 7;
2905 pcr_ext = (pcrbuf[4] & 0x01) << 8;
2906 pcr_ext |= pcrbuf[5];
2908 pcr = pcr * 300 + pcr_ext;
2910 demuxer->reference_clock = (double)pcr/(double)27000000.0;
2914 buf_size -= c;
2915 if(buf_size == 0)
2916 continue;
2920 //find the program that the pid belongs to; if (it's the right one or -1) && pid_type==SL_SECTION
2921 //call parse_sl_section()
2922 pmt = pmt_of_pid(priv, pid, &mp4_dec);
2923 if(mp4_dec)
2925 fill_extradata(mp4_dec, tss);
2926 if(IS_VIDEO(mp4_dec->object_type) || IS_AUDIO(mp4_dec->object_type))
2928 tss->type = SL_PES_STREAM;
2929 tss->subtype = mp4_dec->object_type;
2934 //TABLE PARSING
2936 base = priv->ts.packet_size - buf_size;
2938 priv->last_pid = pid;
2940 is_video = IS_VIDEO(tss->type) || (tss->type==SL_PES_STREAM && IS_VIDEO(tss->subtype));
2941 is_audio = IS_AUDIO(tss->type) || (tss->type==SL_PES_STREAM && IS_AUDIO(tss->subtype)) || (tss->type == PES_PRIVATE1);
2942 is_sub = IS_SUB(tss->type);
2943 pid_type = pid_type_from_pmt(priv, pid);
2945 // PES CONTENT STARTS HERE
2946 if(! probe)
2948 if((is_video || is_audio || is_sub) && is_start)
2949 ts_add_stream(demuxer, tss);
2951 if(is_video && (demuxer->video->id == priv->ts.streams[pid].id))
2953 ds = demuxer->video;
2955 dp = &priv->fifo[1].pack;
2956 dp_offset = &priv->fifo[1].offset;
2957 buffer_size = &priv->fifo[1].buffer_size;
2958 si = &priv->vstr;
2960 else if(is_audio && (demuxer->audio->id == priv->ts.streams[pid].id))
2962 ds = demuxer->audio;
2964 dp = &priv->fifo[0].pack;
2965 dp_offset = &priv->fifo[0].offset;
2966 buffer_size = &priv->fifo[0].buffer_size;
2967 si = &priv->astr;
2969 else if(is_sub)
2971 sh_sub_t *sh_sub = demuxer->sub->sh;
2973 if(sh_sub && sh_sub->sid == tss->pid)
2975 ds = demuxer->sub;
2977 dp = &priv->fifo[2].pack;
2978 dp_offset = &priv->fifo[2].offset;
2979 buffer_size = &priv->fifo[2].buffer_size;
2981 else
2983 stream_skip(stream, buf_size+junk);
2984 continue;
2988 //IS IT TIME TO QUEUE DATA to the dp_packet?
2989 if(is_start && (dp != NULL))
2991 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
2995 if(dp && *dp == NULL)
2997 if(*buffer_size > MAX_PACK_BYTES)
2998 *buffer_size = MAX_PACK_BYTES;
2999 *dp = new_demux_packet(*buffer_size); //es->size
3000 *dp_offset = 0;
3001 if(! *dp)
3003 fprintf(stderr, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", *buffer_size);
3004 continue;
3006 mp_msg(MSGT_DEMUX, MSGL_DBG2, "CREATED DP(%d)\n", *buffer_size);
3011 if(probe || !dp) //dp is NULL for tables and sections
3013 p = &packet[base];
3015 else //feeding
3017 if(*dp_offset + buf_size > *buffer_size)
3019 *buffer_size = *dp_offset + buf_size + TS_FEC_PACKET_SIZE;
3020 resize_demux_packet(*dp, *buffer_size);
3022 p = &((*dp)->buffer[*dp_offset]);
3025 len = stream_read(stream, p, buf_size);
3026 if(len < buf_size)
3028 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\r\nts_parse() couldn't read enough data: %d < %d\r\n", len, buf_size);
3029 continue;
3031 stream_skip(stream, junk);
3033 if(pid == 0)
3035 parse_pat(priv, is_start, p, buf_size);
3036 continue;
3038 else if((tss->type == SL_SECTION) && pmt)
3040 int k, mp4_es_id = -1;
3041 ts_section_t *section;
3042 for(k = 0; k < pmt->mp4es_cnt; k++)
3044 if(pmt->mp4es[k].decoder.object_type == MP4_OD && pmt->mp4es[k].decoder.stream_type == MP4_OD)
3045 mp4_es_id = pmt->mp4es[k].id;
3047 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4ESID: %d\n", mp4_es_id);
3048 for(k = 0; k < pmt->es_cnt; k++)
3050 if(pmt->es[k].mp4_es_id == mp4_es_id)
3052 section = &(tss->section);
3053 parse_sl_section(pmt, section, is_start, &packet[base], buf_size);
3056 continue;
3058 else
3060 progid = prog_id_in_pat(priv, pid);
3061 if(progid != -1)
3063 if(pid != demuxer->video->id && pid != demuxer->audio->id && pid != demuxer->sub->id)
3065 parse_pmt(priv, progid, pid, is_start, &packet[base], buf_size);
3066 continue;
3068 else
3069 mp_msg(MSGT_DEMUX, MSGL_ERR, "Argh! Data pid %d used in the PMT, Skipping PMT parsing!\n", pid);
3073 if(!probe && !dp)
3074 continue;
3076 if(is_start)
3078 uint8_t *lang = NULL;
3080 mp_msg(MSGT_DEMUX, MSGL_DBG2, "IS_START\n");
3082 len = pes_parse2(p, buf_size, es, pid_type, pmt, pid);
3083 if(! len)
3085 tss->is_synced = 0;
3086 continue;
3088 es->pid = tss->pid;
3089 tss->is_synced |= es->is_synced || rap_flag;
3090 tss->payload_size = es->payload_size;
3092 if((is_sub || is_audio) && (lang = pid_lang_from_pmt(priv, es->pid)))
3094 memcpy(es->lang, lang, 3);
3095 es->lang[3] = 0;
3097 else
3098 es->lang[0] = 0;
3100 if(probe)
3102 if(es->type == UNKNOWN)
3103 return 0;
3105 tss->type = es->type;
3106 tss->subtype = es->subtype;
3108 return 1;
3110 else
3112 if(es->pts == 0.0)
3113 es->pts = tss->pts = tss->last_pts;
3114 else
3115 tss->pts = tss->last_pts = es->pts;
3117 mp_msg(MSGT_DEMUX, MSGL_DBG2, "ts_parse, NEW pid=%d, PSIZE: %u, type=%X, start=%p, len=%d\n",
3118 es->pid, es->payload_size, es->type, es->start, es->size);
3120 demuxer->filepos = stream_tell(demuxer->stream) - es->size;
3122 if(es->size < 0 || es->size > buf_size) {
3123 mp_msg(MSGT_DEMUX, MSGL_ERR, "Broken ES packet size\n");
3124 es->size = 0;
3126 memmove(p, es->start, es->size);
3127 *dp_offset += es->size;
3128 (*dp)->flags = 0;
3129 (*dp)->pos = stream_tell(demuxer->stream);
3130 (*dp)->pts = es->pts;
3131 // subtitle packets must be returned immediately if possible
3132 if (is_sub && !tss->payload_size)
3133 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
3135 if(retv > 0)
3136 return retv;
3137 else
3138 continue;
3141 else
3143 uint16_t sz;
3145 es->pid = tss->pid;
3146 es->type = tss->type;
3147 es->subtype = tss->subtype;
3148 es->pts = tss->pts = tss->last_pts;
3149 es->start = &packet[base];
3152 if(tss->payload_size > 0)
3154 sz = FFMIN(tss->payload_size, buf_size);
3155 tss->payload_size -= sz;
3156 es->size = sz;
3158 else
3160 if(is_video)
3162 sz = es->size = buf_size;
3164 else
3166 continue;
3171 if(! probe)
3173 *dp_offset += sz;
3175 // subtitle packets must be returned immediately if possible
3176 if(*dp_offset >= MAX_PACK_BYTES || (is_sub && !tss->payload_size))
3178 (*dp)->pts = tss->last_pts;
3179 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
3180 return 1;
3183 continue;
3185 else
3187 memmove(es->start, p, sz);
3189 if(es->size)
3190 return es->size;
3191 else
3192 continue;
3197 return 0;
3201 static void reset_fifos(demuxer_t *demuxer, int a, int v, int s)
3203 ts_priv_t* priv = demuxer->priv;
3204 if(a)
3206 if(priv->fifo[0].pack != NULL)
3208 free_demux_packet(priv->fifo[0].pack);
3209 priv->fifo[0].pack = NULL;
3211 priv->fifo[0].offset = 0;
3214 if(v)
3216 if(priv->fifo[1].pack != NULL)
3218 free_demux_packet(priv->fifo[1].pack);
3219 priv->fifo[1].pack = NULL;
3221 priv->fifo[1].offset = 0;
3224 if(s)
3226 if(priv->fifo[2].pack != NULL)
3228 free_demux_packet(priv->fifo[2].pack);
3229 priv->fifo[2].pack = NULL;
3231 priv->fifo[2].offset = 0;
3233 demuxer->reference_clock = MP_NOPTS_VALUE;
3237 static void demux_seek_ts(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
3239 demux_stream_t *d_audio=demuxer->audio;
3240 demux_stream_t *d_video=demuxer->video;
3241 sh_audio_t *sh_audio=d_audio->sh;
3242 sh_video_t *sh_video=d_video->sh;
3243 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
3244 int i, video_stats;
3245 off_t newpos;
3247 //================= seek in MPEG-TS ==========================
3249 ts_dump_streams(demuxer->priv);
3250 reset_fifos(demuxer, sh_audio != NULL, sh_video != NULL, demuxer->sub->id > 0);
3252 demux_flush(demuxer);
3256 video_stats = (sh_video != NULL);
3257 if(video_stats)
3259 mp_msg(MSGT_DEMUX, MSGL_V, "IBPS: %d, vb: %d\r\n", sh_video->i_bps, priv->vbitrate);
3260 if(priv->vbitrate)
3261 video_stats = priv->vbitrate;
3262 else
3263 video_stats = sh_video->i_bps;
3266 newpos = (flags & SEEK_ABSOLUTE) ? demuxer->movi_start : demuxer->filepos;
3267 if(flags & SEEK_FACTOR) // float seek 0..1
3268 newpos+=(demuxer->movi_end-demuxer->movi_start)*rel_seek_secs;
3269 else
3271 // time seek (secs)
3272 if(! video_stats) // unspecified or VBR
3273 newpos += 2324*75*rel_seek_secs; // 174.3 kbyte/sec
3274 else
3275 newpos += video_stats*rel_seek_secs;
3279 if(newpos < demuxer->movi_start)
3280 newpos = demuxer->movi_start; //begininng of stream
3282 stream_seek(demuxer->stream, newpos);
3283 for(i = 0; i < NB_PID_MAX; i++)
3284 if(priv->ts.pids[i] != NULL)
3285 priv->ts.pids[i]->is_synced = 0;
3287 videobuf_code_len = 0;
3289 if(sh_video != NULL)
3290 ds_fill_buffer(d_video);
3292 if(sh_audio != NULL)
3294 ds_fill_buffer(d_audio);
3297 while(sh_video != NULL)
3299 if(sh_audio && !d_audio->eof && d_video->pts && d_audio->pts)
3301 double a_pts=d_audio->pts;
3302 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(double)sh_audio->i_bps;
3303 if(d_video->pts > a_pts)
3305 skip_audio_frame(sh_audio); // sync audio
3306 continue;
3311 i = sync_video_packet(d_video);
3312 if((sh_video->format == VIDEO_MPEG1) || (sh_video->format == VIDEO_MPEG2))
3314 if(i==0x1B3 || i==0x1B8) break; // found it!
3316 else if((sh_video->format == VIDEO_MPEG4) && (i==0x1B6))
3317 break;
3318 else if(sh_video->format == VIDEO_VC1 && (i==0x10E || i==0x10F))
3319 break;
3320 else //H264
3322 if((i & ~0x60) == 0x105 || (i & ~0x60) == 0x107) break;
3325 if(!i || !skip_video_packet(d_video)) break; // EOF?
3330 static int demux_ts_fill_buffer(demuxer_t * demuxer, demux_stream_t *ds)
3332 ES_stream_t es;
3333 ts_priv_t *priv = (ts_priv_t *)demuxer->priv;
3335 return -ts_parse(demuxer, &es, priv->packet, 0);
3339 static int ts_check_file_dmx(demuxer_t *demuxer)
3341 return ts_check_file(demuxer) ? DEMUXER_TYPE_MPEG_TS : 0;
3344 static int is_usable_program(ts_priv_t *priv, pmt_t *pmt)
3346 int j;
3348 for(j = 0; j < pmt->es_cnt; j++)
3350 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3351 continue;
3353 priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO ||
3354 priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO
3356 return 1;
3359 return 0;
3362 static int demux_ts_control(demuxer_t *demuxer, int cmd, void *arg)
3364 ts_priv_t* priv = (ts_priv_t *)demuxer->priv;
3366 switch(cmd)
3368 case DEMUXER_CTRL_SWITCH_AUDIO:
3369 case DEMUXER_CTRL_SWITCH_VIDEO:
3371 void *sh = NULL;
3372 int i, n;
3373 int reftype, areset = 0, vreset = 0;
3374 demux_stream_t *ds;
3376 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
3378 reftype = TYPE_VIDEO;
3379 ds = demuxer->video;
3380 vreset = 1;
3382 else
3384 reftype = TYPE_AUDIO;
3385 ds = demuxer->audio;
3386 areset = 1;
3388 n = *((int*)arg);
3389 if(n == -2)
3391 reset_fifos(demuxer, areset, vreset, 0);
3392 ds->id = -2;
3393 ds->sh = NULL;
3394 ds_free_packs(ds);
3395 *((int*)arg) = ds->id;
3396 return DEMUXER_CTRL_OK;
3399 if(n < 0)
3401 for(i = 0; i < 8192; i++)
3403 if(priv->ts.streams[i].id == ds->id && priv->ts.streams[i].type == reftype)
3404 break;
3407 while(!sh)
3409 i = (i+1) % 8192;
3410 if(priv->ts.streams[i].type == reftype)
3412 if(priv->ts.streams[i].id == ds->id) //we made a complete loop
3413 break;
3414 sh = priv->ts.streams[i].sh;
3418 else //audio track <n>
3420 if (n >= 8192 || priv->ts.streams[n].type != reftype) return DEMUXER_CTRL_NOTIMPL;
3421 i = n;
3422 sh = priv->ts.streams[i].sh;
3425 if(sh)
3427 if(ds->id != priv->ts.streams[i].id)
3428 reset_fifos(demuxer, areset, vreset, 0);
3429 ds->id = priv->ts.streams[i].id;
3430 ds->sh = sh;
3431 ds_free_packs(ds);
3432 mp_msg(MSGT_DEMUX, MSGL_V, "\r\ndemux_ts, switched to audio pid %d, id: %d, sh: %p\r\n", i, ds->id, sh);
3435 *((int*)arg) = ds->id;
3436 return DEMUXER_CTRL_OK;
3439 case DEMUXER_CTRL_IDENTIFY_PROGRAM: //returns in prog->{aid,vid} the new ids that comprise a program
3441 int i, j, cnt=0;
3442 int vid_done=0, aid_done=0;
3443 pmt_t *pmt = NULL;
3444 demux_program_t *prog = arg;
3446 if(priv->pmt_cnt < 2)
3447 return DEMUXER_CTRL_NOTIMPL;
3449 if(prog->progid == -1)
3451 int cur_pmt_idx = 0;
3453 for(i = 0; i < priv->pmt_cnt; i++)
3454 if(priv->pmt[i].progid == priv->prog)
3456 cur_pmt_idx = i;
3457 break;
3460 i = (cur_pmt_idx + 1) % priv->pmt_cnt;
3461 while(i != cur_pmt_idx)
3463 pmt = &priv->pmt[i];
3464 cnt = is_usable_program(priv, pmt);
3465 if(cnt)
3466 break;
3467 i = (i + 1) % priv->pmt_cnt;
3470 else
3472 for(i = 0; i < priv->pmt_cnt; i++)
3473 if(priv->pmt[i].progid == prog->progid)
3475 pmt = &priv->pmt[i]; //required program
3476 cnt = is_usable_program(priv, pmt);
3480 if(!cnt)
3481 return DEMUXER_CTRL_NOTIMPL;
3483 //finally some food
3484 prog->aid = prog->vid = -2; //no audio and no video by default
3485 for(j = 0; j < pmt->es_cnt; j++)
3487 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3488 continue;
3490 if(!vid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO)
3492 vid_done = 1;
3493 prog->vid = pmt->es[j].pid;
3495 else if(!aid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO)
3497 aid_done = 1;
3498 prog->aid = pmt->es[j].pid;
3502 priv->prog = prog->progid = pmt->progid;
3503 return DEMUXER_CTRL_OK;
3506 default:
3507 return DEMUXER_CTRL_NOTIMPL;
3512 const demuxer_desc_t demuxer_desc_mpeg_ts = {
3513 "MPEG-TS demuxer",
3514 "mpegts",
3515 "TS",
3516 "Nico Sabbi",
3518 DEMUXER_TYPE_MPEG_TS,
3519 0, // unsafe autodetect
3520 ts_check_file_dmx,
3521 demux_ts_fill_buffer,
3522 demux_open_ts,
3523 demux_close_ts,
3524 demux_seek_ts,
3525 demux_ts_control