Makefile: Merge dhahelperwinclean and dhahelperclean targets
[mplayer.git] / libmpdemux / demux_ts.c
blobb98d766470857bacc708c6afaf6adfdc91e9d5c9
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 "stream/stream.h"
34 #include "demuxer.h"
35 #include "parse_es.h"
36 #include "stheader.h"
37 #include "ms_hdr.h"
38 #include "mpeg_hdr.h"
39 #include "demux_ts.h"
41 #define TS_PH_PACKET_SIZE 192
42 #define TS_FEC_PACKET_SIZE 204
43 #define TS_PACKET_SIZE 188
44 #define NB_PID_MAX 8192
46 #define MAX_HEADER_SIZE 6 /* enough for PES header + length */
47 #define MAX_CHECK_SIZE 65535
48 #define 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;
64 typedef enum
66 UNKNOWN = -1,
67 VIDEO_MPEG1 = 0x10000001,
68 VIDEO_MPEG2 = 0x10000002,
69 VIDEO_MPEG4 = 0x10000004,
70 VIDEO_H264 = 0x10000005,
71 VIDEO_AVC = mmioFOURCC('a', 'v', 'c', '1'),
72 VIDEO_DIRAC = mmioFOURCC('d', 'r', 'a', 'c'),
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_AAC_LATM = mmioFOURCC('M', 'P', '4', 'L'),
80 AUDIO_TRUEHD = mmioFOURCC('T', 'R', 'H', 'D'),
81 SPU_DVD = 0x3000000,
82 SPU_DVB = 0x3000001,
83 SPU_TELETEXT = 0x3000002,
84 SPU_PGS = 0x3000003,
85 PES_PRIVATE1 = 0xBD00000,
86 SL_PES_STREAM = 0xD000000,
87 SL_SECTION = 0xD100000,
88 MP4_OD = 0xD200000,
89 } es_stream_type_t;
91 typedef struct {
92 uint8_t *buffer;
93 uint16_t buffer_len;
94 } ts_section_t;
96 typedef struct {
97 int size;
98 unsigned char *start;
99 uint16_t payload_size;
100 es_stream_type_t type, subtype;
101 double pts, last_pts;
102 int pid;
103 char lang[4];
104 int last_cc; // last cc code (-1 if first packet)
105 int is_synced;
106 ts_section_t section;
107 uint8_t *extradata;
108 int extradata_alloc, extradata_len;
109 struct {
110 uint8_t au_start, au_end, last_au_end;
111 } sl;
112 } ES_stream_t;
114 typedef struct {
115 void *sh;
116 int id;
117 int type;
118 } sh_av_t;
120 typedef struct MpegTSContext {
121 int packet_size; // raw packet size, including FEC if present e.g. 188 bytes
122 ES_stream_t *pids[NB_PID_MAX];
123 sh_av_t streams[NB_PID_MAX];
124 } MpegTSContext;
127 typedef struct {
128 demux_stream_t *ds;
129 demux_packet_t *pack;
130 int offset, buffer_size;
131 } av_fifo_t;
133 #define MAX_EXTRADATA_SIZE 64*1024
134 typedef struct {
135 int32_t object_type; //aka codec used
136 int32_t stream_type; //video, audio etc.
137 uint8_t buf[MAX_EXTRADATA_SIZE];
138 uint16_t buf_size;
139 uint8_t szm1;
140 } mp4_decoder_config_t;
142 typedef struct {
143 //flags
144 uint8_t flags;
145 uint8_t au_start;
146 uint8_t au_end;
147 uint8_t random_accesspoint;
148 uint8_t random_accesspoint_only;
149 uint8_t padding;
150 uint8_t use_ts;
151 uint8_t idle;
152 uint8_t duration;
154 uint32_t ts_resolution, ocr_resolution;
155 uint8_t ts_len, ocr_len, au_len, instant_bitrate_len, degr_len, au_seqnum_len, packet_seqnum_len;
156 uint32_t timescale;
157 uint16_t au_duration, cts_duration;
158 uint64_t ocr, dts, cts;
159 } mp4_sl_config_t;
161 typedef struct {
162 uint16_t id;
163 uint8_t flags;
164 mp4_decoder_config_t decoder;
165 mp4_sl_config_t sl;
166 } mp4_es_descr_t;
168 typedef struct {
169 uint16_t id;
170 uint8_t flags;
171 mp4_es_descr_t *es;
172 uint16_t es_cnt;
173 } mp4_od_t;
175 typedef struct {
176 uint8_t skip;
177 uint8_t table_id;
178 uint8_t ssi;
179 uint16_t section_length;
180 uint16_t ts_id;
181 uint8_t version_number;
182 uint8_t curr_next;
183 uint8_t section_number;
184 uint8_t last_section_number;
185 struct pat_progs_t {
186 uint16_t id;
187 uint16_t pmt_pid;
188 } *progs;
189 uint16_t progs_cnt;
190 ts_section_t section;
191 } pat_t;
193 typedef struct {
194 uint16_t progid;
195 uint8_t skip;
196 uint8_t table_id;
197 uint8_t ssi;
198 uint16_t section_length;
199 uint8_t version_number;
200 uint8_t curr_next;
201 uint8_t section_number;
202 uint8_t last_section_number;
203 uint16_t PCR_PID;
204 uint16_t prog_descr_length;
205 ts_section_t section;
206 uint16_t es_cnt;
207 struct pmt_es_t {
208 uint16_t pid;
209 uint32_t type; //it's 8 bit long, but cast to the right type as FOURCC
210 uint16_t descr_length;
211 uint8_t format_descriptor[5];
212 uint8_t lang[4];
213 uint16_t mp4_es_id;
214 } *es;
215 mp4_od_t iod, *od;
216 mp4_es_descr_t *mp4es;
217 int od_cnt, mp4es_cnt;
218 } pmt_t;
220 typedef struct {
221 uint64_t size;
222 float duration;
223 double first_pts;
224 double last_pts;
225 } TS_stream_info;
227 typedef struct {
228 MpegTSContext ts;
229 int last_pid;
230 av_fifo_t fifo[3]; //0 for audio, 1 for video, 2 for subs
231 pat_t pat;
232 pmt_t *pmt;
233 uint16_t pmt_cnt;
234 uint32_t prog;
235 uint32_t vbitrate;
236 int keep_broken;
237 int last_aid;
238 int last_vid;
239 int last_sid;
240 char packet[TS_FEC_PACKET_SIZE];
241 TS_stream_info vstr, astr;
242 } ts_priv_t;
245 typedef struct {
246 es_stream_type_t type;
247 ts_section_t section;
248 } TS_pids_t;
251 static int IS_AUDIO(es_stream_type_t type)
253 switch (type) {
254 case AUDIO_MP2:
255 case AUDIO_A52:
256 case AUDIO_LPCM_BE:
257 case AUDIO_AAC:
258 case AUDIO_AAC_LATM:
259 case AUDIO_DTS:
260 case AUDIO_TRUEHD:
261 return 1;
263 return 0;
266 static int IS_VIDEO(es_stream_type_t type)
268 switch (type) {
269 case VIDEO_MPEG1:
270 case VIDEO_MPEG2:
271 case VIDEO_MPEG4:
272 case VIDEO_H264:
273 case VIDEO_AVC:
274 case VIDEO_DIRAC:
275 case VIDEO_VC1:
276 return 1;
278 return 0;
281 static int IS_SUB(es_stream_type_t type)
283 switch (type) {
284 case SPU_DVD:
285 case SPU_DVB:
286 case SPU_PGS:
287 case SPU_TELETEXT:
288 return 1;
290 return 0;
293 static int ts_parse(demuxer_t *demuxer, ES_stream_t *es, unsigned char *packet, int probe);
295 static uint8_t get_packet_size(const unsigned char *buf, int size)
297 int i;
299 if (size < (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS))
300 return 0;
302 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
304 if (buf[i * TS_PACKET_SIZE] != 0x47)
306 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
307 goto try_fec;
310 return TS_PACKET_SIZE;
312 try_fec:
313 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
315 if (buf[i * TS_FEC_PACKET_SIZE] != 0x47){
316 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
317 goto try_philips;
320 return TS_FEC_PACKET_SIZE;
322 try_philips:
323 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
325 if (buf[i * TS_PH_PACKET_SIZE] != 0x47)
326 return 0;
328 return TS_PH_PACKET_SIZE;
331 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h);
332 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid);
334 static void ts_add_stream(demuxer_t * demuxer, ES_stream_t *es)
336 int i;
337 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
339 if(priv->ts.streams[es->pid].sh)
340 return;
342 if((IS_AUDIO(es->type) || IS_AUDIO(es->subtype)) && priv->last_aid+1 < MAX_A_STREAMS)
344 sh_audio_t *sh = new_sh_audio_aid(demuxer, priv->last_aid, es->pid);
345 if(sh)
347 const char *lang = pid_lang_from_pmt(priv, es->pid);
348 sh->needs_parsing = 1;
349 sh->format = IS_AUDIO(es->type) ? es->type : es->subtype;
350 sh->ds = demuxer->audio;
352 priv->ts.streams[es->pid].id = priv->last_aid;
353 priv->ts.streams[es->pid].sh = sh;
354 priv->ts.streams[es->pid].type = TYPE_AUDIO;
355 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);
356 if (lang && lang[0])
357 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_AID_%d_LANG=%s\n", es->pid, lang);
358 priv->last_aid++;
361 if(es->extradata && es->extradata_len)
363 sh->wf = malloc(sizeof(*sh->wf) + es->extradata_len);
364 sh->wf->cbSize = es->extradata_len;
365 memcpy(sh->wf + 1, es->extradata, es->extradata_len);
369 if((IS_VIDEO(es->type) || IS_VIDEO(es->subtype)) && priv->last_vid+1 < MAX_V_STREAMS)
371 sh_video_t *sh = new_sh_video_vid(demuxer, priv->last_vid, es->pid);
372 if(sh)
374 sh->format = IS_VIDEO(es->type) ? es->type : es->subtype;
375 sh->ds = demuxer->video;
377 priv->ts.streams[es->pid].id = priv->last_vid;
378 priv->ts.streams[es->pid].sh = sh;
379 priv->ts.streams[es->pid].type = TYPE_VIDEO;
380 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);
381 priv->last_vid++;
384 if(sh->format == VIDEO_AVC && es->extradata && es->extradata_len)
386 int w = 0, h = 0;
387 sh->bih = calloc(1, sizeof(*sh->bih) + es->extradata_len);
388 sh->bih->biSize= sizeof(*sh->bih) + es->extradata_len;
389 sh->bih->biCompression = sh->format;
390 memcpy(sh->bih + 1, es->extradata, es->extradata_len);
391 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "EXTRADATA(%d BYTES): \n", es->extradata_len);
392 for(i = 0;i < es->extradata_len; i++)
393 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "%02x ", (int) es->extradata[i]);
394 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"\n");
395 if(parse_avc_sps(es->extradata, es->extradata_len, &w, &h))
397 sh->bih->biWidth = w;
398 sh->bih->biHeight = h;
404 if(IS_SUB(es->type) && priv->last_sid+1 < MAX_S_STREAMS)
406 sh_sub_t *sh = new_sh_sub_sid_lang(demuxer, priv->last_sid, es->pid, pid_lang_from_pmt(priv, es->pid));
407 if (sh) {
408 switch (es->type) {
409 case SPU_DVD:
410 sh->type = 'v'; break;
411 case SPU_PGS:
412 sh->type = 'p'; break;
414 priv->ts.streams[es->pid].id = priv->last_aid;
415 priv->ts.streams[es->pid].sh = sh;
416 priv->ts.streams[es->pid].type = TYPE_AUDIO;
417 priv->last_sid++;
422 static int ts_check_file(demuxer_t * demuxer)
424 const int buf_size = (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS);
425 unsigned char buf[TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS], done = 0, *ptr;
426 uint32_t _read, i, count = 0, is_ts;
427 int cc[NB_PID_MAX], last_cc[NB_PID_MAX], pid, cc_ok, c, good, bad;
428 uint8_t size = 0;
429 off_t pos = 0;
430 off_t init_pos;
432 mp_msg(MSGT_DEMUX, MSGL_V, "Checking for MPEG-TS...\n");
434 init_pos = stream_tell(demuxer->stream);
435 is_ts = 0;
436 while(! done)
438 i = 1;
439 c = 0;
441 while(((c=stream_read_char(demuxer->stream)) != 0x47)
442 && (c >= 0)
443 && (i < MAX_CHECK_SIZE)
444 && ! demuxer->stream->eof
445 ) i++;
448 if(c != 0x47)
450 mp_msg(MSGT_DEMUX, MSGL_V, "THIS DOESN'T LOOK LIKE AN MPEG-TS FILE!\n");
451 is_ts = 0;
452 done = 1;
453 continue;
456 pos = stream_tell(demuxer->stream) - 1;
457 buf[0] = c;
458 _read = stream_read(demuxer->stream, &buf[1], buf_size-1);
460 if(_read < buf_size-1)
462 mp_msg(MSGT_DEMUX, MSGL_V, "COULDN'T READ ENOUGH DATA, EXITING TS_CHECK\n");
463 stream_reset(demuxer->stream);
464 return 0;
467 size = get_packet_size(buf, buf_size);
468 if(size)
470 done = 1;
471 is_ts = 1;
474 if(pos - init_pos >= MAX_CHECK_SIZE)
476 done = 1;
477 is_ts = 0;
481 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);
482 stream_seek(demuxer->stream, pos);
484 if(! is_ts)
485 return 0;
487 //LET'S CHECK continuity counters
488 good = bad = 0;
489 for(count = 0; count < NB_PID_MAX; count++)
491 cc[count] = last_cc[count] = -1;
494 for(count = 0; count < NUM_CONSECUTIVE_TS_PACKETS; count++)
496 ptr = &(buf[size * count]);
497 pid = ((ptr[1] & 0x1f) << 8) | ptr[2];
498 mp_msg(MSGT_DEMUX, MSGL_DBG2, "BUF: %02x %02x %02x %02x, PID %d, SIZE: %d \n",
499 ptr[0], ptr[1], ptr[2], ptr[3], pid, size);
501 if((pid == 8191) || (pid < 16))
502 continue;
504 cc[pid] = (ptr[3] & 0xf);
505 cc_ok = (last_cc[pid] < 0) || ((((last_cc[pid] + 1) & 0x0f) == cc[pid]));
506 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PID %d, COMPARE CC %d AND LAST_CC %d\n", pid, cc[pid], last_cc[pid]);
507 if(! cc_ok)
508 //return 0;
509 bad++;
510 else
511 good++;
513 last_cc[pid] = cc[pid];
516 mp_msg(MSGT_DEMUX, MSGL_V, "GOOD CC: %d, BAD CC: %d\n", good, bad);
518 if(good >= bad)
519 return size;
520 else
521 return 0;
525 static inline int32_t progid_idx_in_pmt(ts_priv_t *priv, uint16_t progid)
527 int x;
529 if(priv->pmt == NULL)
530 return -1;
532 for(x = 0; x < priv->pmt_cnt; x++)
534 if(priv->pmt[x].progid == progid)
535 return x;
538 return -1;
542 static inline int32_t progid_for_pid(ts_priv_t *priv, int pid, int32_t req) //finds the first program listing a pid
544 int i, j;
545 pmt_t *pmt;
548 if(priv->pmt == NULL)
549 return -1;
552 for(i=0; i < priv->pmt_cnt; i++)
554 pmt = &(priv->pmt[i]);
556 if(pmt->es == NULL)
557 return -1;
559 for(j = 0; j < pmt->es_cnt; j++)
561 if(pmt->es[j].pid == pid)
563 if((req == 0) || (req == pmt->progid))
564 return pmt->progid;
569 return -1;
572 static inline int32_t prog_pcr_pid(ts_priv_t *priv, int progid)
574 int i;
576 if(priv->pmt == NULL)
577 return -1;
578 for(i=0; i < priv->pmt_cnt; i++)
580 if(priv->pmt[i].progid == progid)
581 return priv->pmt[i].PCR_PID;
583 return -1;
587 static inline int pid_match_lang(ts_priv_t *priv, uint16_t pid, char *lang)
589 uint16_t i, j;
590 pmt_t *pmt;
592 if(priv->pmt == NULL)
593 return -1;
595 for(i=0; i < priv->pmt_cnt; i++)
597 pmt = &(priv->pmt[i]);
599 if(pmt->es == NULL)
600 return -1;
602 for(j = 0; j < pmt->es_cnt; j++)
604 if(pmt->es[j].pid != pid)
605 continue;
607 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);
608 if(strncmp(pmt->es[j].lang, lang, 3) == 0)
610 return 1;
616 return -1;
619 typedef struct {
620 int32_t atype, vtype, stype; //types
621 int32_t apid, vpid, spid; //stream ids
622 char alang[4]; //languages
623 uint16_t prog;
624 off_t probe;
625 } tsdemux_init_t;
627 //second stage: returns the count of A52 syncwords found
628 static int a52_check(char *buf, int len)
630 int cnt, frame_length = 0, ok, srate;
632 cnt = ok = 0;
633 if(len < 8)
634 return 0;
636 while(cnt < len - 7)
638 if(buf[cnt] == 0x0B && buf[cnt+1] == 0x77)
640 frame_length = mp_a52_framesize(&buf[cnt], &srate);
641 if(frame_length>=7 && frame_length<=3840)
643 cnt += frame_length;
644 ok++;
646 else
647 cnt++;
649 else
650 cnt++;
653 mp_msg(MSGT_DEMUXER, MSGL_V, "A52_CHECK(%d input bytes), found %d frame syncwords of %d bytes length\n", len, ok, frame_length);
654 return ok;
658 static off_t ts_detect_streams(demuxer_t *demuxer, tsdemux_init_t *param)
660 int video_found = 0, audio_found = 0, sub_found = 0, i, num_packets = 0, req_apid, req_vpid, req_spid;
661 int is_audio, is_video, is_sub, has_tables;
662 int32_t p, chosen_pid = 0;
663 off_t pos=0, ret = 0, init_pos, end_pos;
664 ES_stream_t es;
665 unsigned char tmp[TS_FEC_PACKET_SIZE];
666 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
667 struct {
668 char *buf;
669 int pos;
670 } pes_priv1[8192], *pptr;
671 char *tmpbuf;
673 priv->last_pid = 8192; //invalid pid
675 req_apid = param->apid;
676 req_vpid = param->vpid;
677 req_spid = param->spid;
679 has_tables = 0;
680 memset(pes_priv1, 0, sizeof(pes_priv1));
681 init_pos = stream_tell(demuxer->stream);
682 mp_msg(MSGT_DEMUXER, MSGL_V, "PROBING UP TO %"PRIu64", PROG: %d\n", (uint64_t) param->probe, param->prog);
683 end_pos = init_pos + (param->probe ? param->probe : TS_MAX_PROBE_SIZE);
684 while(1)
686 pos = stream_tell(demuxer->stream);
687 if(pos > end_pos || demuxer->stream->eof)
688 break;
690 if(ts_parse(demuxer, &es, tmp, 1))
692 //Non PES-aligned A52 audio may escape detection if PMT is not present;
693 //in this case we try to find at least 3 A52 syncwords
694 if((es.type == PES_PRIVATE1) && (! audio_found) && req_apid > -2)
696 pptr = &pes_priv1[es.pid];
697 if(pptr->pos < 64*1024)
699 tmpbuf = realloc(pptr->buf, pptr->pos + es.size);
700 if(tmpbuf != NULL)
702 pptr->buf = tmpbuf;
703 memcpy(&(pptr->buf[ pptr->pos ]), es.start, es.size);
704 pptr->pos += es.size;
705 if(a52_check(pptr->buf, pptr->pos) > 2)
707 param->atype = AUDIO_A52;
708 param->apid = es.pid;
709 es.type = AUDIO_A52;
715 is_audio = IS_AUDIO(es.type) || ((es.type==SL_PES_STREAM) && IS_AUDIO(es.subtype));
716 is_video = IS_VIDEO(es.type) || ((es.type==SL_PES_STREAM) && IS_VIDEO(es.subtype));
717 is_sub = IS_SUB(es.type);
720 if((! is_audio) && (! is_video) && (! is_sub))
721 continue;
722 if(is_audio && req_apid==-2)
723 continue;
725 if(is_video)
727 chosen_pid = (req_vpid == es.pid);
728 if((! chosen_pid) && (req_vpid > 0))
729 continue;
731 else if(is_audio)
733 if(req_apid > 0)
735 chosen_pid = (req_apid == es.pid);
736 if(! chosen_pid)
737 continue;
739 else if(param->alang[0] > 0 && es.lang[0] > 0)
741 if(pid_match_lang(priv, es.pid, param->alang) == -1)
742 continue;
744 chosen_pid = 1;
745 param->apid = req_apid = es.pid;
748 else if(is_sub)
750 chosen_pid = (req_spid == es.pid);
751 if((! chosen_pid) && (req_spid > 0))
752 continue;
755 if(req_apid < 0 && (param->alang[0] == 0) && req_vpid < 0 && req_spid < 0)
756 chosen_pid = 1;
758 if((ret == 0) && chosen_pid)
760 ret = stream_tell(demuxer->stream);
763 p = progid_for_pid(priv, es.pid, param->prog);
764 if(p != -1)
766 has_tables++;
767 if(!param->prog && chosen_pid)
768 param->prog = p;
771 if((param->prog > 0) && (param->prog != p))
773 if(audio_found)
775 if(is_video && (req_vpid == es.pid))
777 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
778 param->vpid = es.pid;
779 video_found = 1;
780 break;
784 if(video_found)
786 if(is_audio && (req_apid == es.pid))
788 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
789 param->apid = es.pid;
790 audio_found = 1;
791 break;
796 continue;
800 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "TYPE: %x, PID: %d, PROG FOUND: %d\n", es.type, es.pid, param->prog);
803 if(is_video)
805 if((req_vpid == -1) || (req_vpid == es.pid))
807 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
808 param->vpid = es.pid;
809 video_found = 1;
814 if(((req_vpid == -2) || (num_packets >= NUM_CONSECUTIVE_AUDIO_PACKETS)) && audio_found && !param->probe)
816 //novideo or we have at least 348 audio packets (64 KB) without video (TS with audio only)
817 param->vtype = 0;
818 break;
821 if(is_sub)
823 if((req_spid == -1) || (req_spid == es.pid))
825 param->stype = es.type;
826 param->spid = es.pid;
827 sub_found = 1;
831 if(is_audio)
833 if((req_apid == -1) || (req_apid == es.pid))
835 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
836 param->apid = es.pid;
837 audio_found = 1;
841 if(audio_found && (param->apid == es.pid) && (! video_found))
842 num_packets++;
844 if((has_tables==0) && (video_found && audio_found) && (pos >= 1000000))
845 break;
849 for(i=0; i<8192; i++)
851 if(pes_priv1[i].buf != NULL)
853 free(pes_priv1[i].buf);
854 pes_priv1[i].buf = NULL;
855 pes_priv1[i].pos = 0;
859 if(video_found)
861 if(param->vtype == VIDEO_MPEG1)
862 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG1(pid=%d) ", param->vpid);
863 else if(param->vtype == VIDEO_MPEG2)
864 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG2(pid=%d) ", param->vpid);
865 else if(param->vtype == VIDEO_MPEG4)
866 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG4(pid=%d) ", param->vpid);
867 else if(param->vtype == VIDEO_H264)
868 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO H264(pid=%d) ", param->vpid);
869 else if(param->vtype == VIDEO_VC1)
870 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO VC1(pid=%d) ", param->vpid);
871 else if(param->vtype == VIDEO_AVC)
872 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO AVC(NAL-H264, pid=%d) ", param->vpid);
874 else
876 param->vtype = UNKNOWN;
877 //WE DIDN'T MATCH ANY VIDEO STREAM
878 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO VIDEO! ");
881 if(param->atype == AUDIO_MP2)
882 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO MPA(pid=%d)", param->apid);
883 else if(param->atype == AUDIO_A52)
884 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO A52(pid=%d)", param->apid);
885 else if(param->atype == AUDIO_DTS)
886 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO DTS(pid=%d)", param->apid);
887 else if(param->atype == AUDIO_LPCM_BE)
888 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO LPCM(pid=%d)", param->apid);
889 else if(param->atype == AUDIO_AAC)
890 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO AAC(pid=%d)", param->apid);
891 else if(param->atype == AUDIO_AAC_LATM)
892 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO AAC LATM(pid=%d)", param->apid);
893 else if(param->atype == AUDIO_TRUEHD)
894 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO TRUEHD(pid=%d)", param->apid);
895 else
897 audio_found = 0;
898 param->atype = UNKNOWN;
899 //WE DIDN'T MATCH ANY AUDIO STREAM, SO WE FORCE THE DEMUXER TO IGNORE AUDIO
900 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO AUDIO! ");
903 if(IS_SUB(param->stype))
904 mp_msg(MSGT_DEMUXER, MSGL_INFO, " SUB %s(pid=%d) ", (param->stype==SPU_DVD ? "DVD" : param->stype==SPU_DVB ? "DVB" : "Teletext"), param->spid);
905 else
907 param->stype = UNKNOWN;
908 mp_msg(MSGT_DEMUXER, MSGL_INFO, " NO SUBS (yet)! ");
911 if(video_found || audio_found)
913 if(!param->prog)
915 p = progid_for_pid(priv, video_found ? param->vpid : param->apid, 0);
916 if(p != -1)
917 param->prog = p;
920 if(demuxer->stream->eof && (ret == 0))
921 ret = init_pos;
922 mp_msg(MSGT_DEMUXER, MSGL_INFO, " PROGRAM N. %d\n", param->prog);
924 else
925 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\n");
928 for(i=0; i<8192; i++)
930 if(priv->ts.pids[i] != NULL)
932 priv->ts.pids[i]->payload_size = 0;
933 priv->ts.pids[i]->pts = priv->ts.pids[i]->last_pts = 0;
934 priv->ts.pids[i]->last_cc = -1;
935 priv->ts.pids[i]->is_synced = 0;
939 return ret;
942 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h)
944 int sps, sps_len;
945 unsigned char *ptr;
946 mp_mpeg_header_t picture;
947 if(len < 6)
948 return 0;
949 sps = buf[5] & 0x1f;
950 if(!sps)
951 return 0;
952 sps_len = (buf[6] << 8) | buf[7];
953 if(!sps_len || (sps_len > len - 8))
954 return 0;
955 ptr = &(buf[8]);
956 picture.display_picture_width = picture.display_picture_height = 0;
957 h264_parse_sps(&picture, ptr, len - 8);
958 if(!picture.display_picture_width || !picture.display_picture_height)
959 return 0;
960 *w = picture.display_picture_width;
961 *h = picture.display_picture_height;
962 return 1;
965 static demuxer_t *demux_open_ts(demuxer_t * demuxer)
967 int i;
968 uint8_t packet_size;
969 sh_video_t *sh_video;
970 sh_audio_t *sh_audio;
971 off_t start_pos;
972 tsdemux_init_t params;
973 ts_priv_t * priv = demuxer->priv;
975 mp_msg(MSGT_DEMUX, MSGL_V, "DEMUX OPEN, AUDIO_ID: %d, VIDEO_ID: %d, SUBTITLE_ID: %d,\n",
976 demuxer->audio->id, demuxer->video->id, demuxer->sub->id);
979 demuxer->type= DEMUXER_TYPE_MPEG_TS;
982 stream_reset(demuxer->stream);
984 packet_size = ts_check_file(demuxer);
985 if(!packet_size)
986 return NULL;
988 priv = calloc(1, sizeof(ts_priv_t));
989 if(priv == NULL)
991 mp_msg(MSGT_DEMUX, MSGL_FATAL, "DEMUX_OPEN_TS, couldn't allocate enough memory for ts->priv, exit\n");
992 return NULL;
995 for(i=0; i < 8192; i++)
997 priv->ts.pids[i] = NULL;
998 priv->ts.streams[i].id = -3;
1000 priv->pat.progs = NULL;
1001 priv->pat.progs_cnt = 0;
1002 priv->pat.section.buffer = NULL;
1003 priv->pat.section.buffer_len = 0;
1005 priv->pmt = NULL;
1006 priv->pmt_cnt = 0;
1008 priv->keep_broken = ts_keep_broken;
1009 priv->ts.packet_size = packet_size;
1012 demuxer->priv = priv;
1013 if(demuxer->stream->type != STREAMTYPE_FILE)
1014 demuxer->seekable = 1;
1015 else
1016 demuxer->seekable = 1;
1019 params.atype = params.vtype = params.stype = UNKNOWN;
1020 params.apid = demuxer->audio->id;
1021 params.vpid = demuxer->video->id;
1022 params.spid = demuxer->sub->id;
1023 params.prog = ts_prog;
1024 params.probe = ts_probe;
1026 if(demuxer->opts->audio_lang != NULL)
1028 strncpy(params.alang, demuxer->opts->audio_lang, 3);
1029 params.alang[3] = 0;
1031 else
1032 memset(params.alang, 0, 4);
1034 start_pos = ts_detect_streams(demuxer, &params);
1036 demuxer->sub->id = params.spid;
1037 priv->prog = params.prog;
1039 if(params.vtype != UNKNOWN)
1041 ts_add_stream(demuxer, priv->ts.pids[params.vpid]);
1042 sh_video = priv->ts.streams[params.vpid].sh;
1043 demuxer->video->id = priv->ts.streams[params.vpid].id;
1044 sh_video->ds = demuxer->video;
1045 sh_video->format = params.vtype;
1046 demuxer->video->sh = sh_video;
1049 if(params.atype != UNKNOWN)
1051 ES_stream_t *es = priv->ts.pids[params.apid];
1053 if(!IS_AUDIO(es->type) && !IS_AUDIO(es->subtype) && IS_AUDIO(params.atype)) es->subtype = params.atype;
1054 ts_add_stream(demuxer, priv->ts.pids[params.apid]);
1055 sh_audio = priv->ts.streams[params.apid].sh;
1056 demuxer->audio->id = priv->ts.streams[params.apid].id;
1057 sh_audio->ds = demuxer->audio;
1058 sh_audio->format = params.atype;
1059 demuxer->audio->sh = sh_audio;
1063 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);
1066 start_pos = (start_pos <= priv->ts.packet_size ? 0 : start_pos - priv->ts.packet_size);
1067 demuxer->movi_start = start_pos;
1068 demuxer->reference_clock = MP_NOPTS_VALUE;
1069 stream_reset(demuxer->stream);
1070 stream_seek(demuxer->stream, start_pos); //IF IT'S FROM A PIPE IT WILL FAIL, BUT WHO CARES?
1073 priv->last_pid = 8192; //invalid pid
1075 for(i = 0; i < 3; i++)
1077 priv->fifo[i].pack = NULL;
1078 priv->fifo[i].offset = 0;
1080 priv->fifo[0].ds = demuxer->audio;
1081 priv->fifo[1].ds = demuxer->video;
1082 priv->fifo[2].ds = demuxer->sub;
1084 priv->fifo[0].buffer_size = 1536;
1085 priv->fifo[1].buffer_size = 32767;
1086 priv->fifo[2].buffer_size = 32767;
1088 priv->pat.section.buffer_len = 0;
1089 for(i = 0; i < priv->pmt_cnt; i++)
1090 priv->pmt[i].section.buffer_len = 0;
1092 demuxer->filepos = stream_tell(demuxer->stream);
1093 return demuxer;
1096 static void demux_close_ts(demuxer_t * demuxer)
1098 uint16_t i;
1099 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
1101 if(priv)
1103 if(priv->pat.section.buffer)
1104 free(priv->pat.section.buffer);
1105 if(priv->pat.progs)
1106 free(priv->pat.progs);
1108 if(priv->pmt)
1110 for(i = 0; i < priv->pmt_cnt; i++)
1112 if(priv->pmt[i].section.buffer)
1113 free(priv->pmt[i].section.buffer);
1114 if(priv->pmt[i].es)
1115 free(priv->pmt[i].es);
1117 free(priv->pmt);
1119 free(priv);
1121 demuxer->priv=NULL;
1125 #define getbits mp_getbits
1127 static int mp4_parse_sl_packet(pmt_t *pmt, uint8_t *buf, uint16_t packet_len, int pid, ES_stream_t *pes_es)
1129 int i, n, m, mp4_es_id = -1;
1130 uint64_t v = 0;
1131 uint32_t pl_size = 0;
1132 int deg_flag = 0;
1133 mp4_es_descr_t *es = NULL;
1134 mp4_sl_config_t *sl = NULL;
1135 uint8_t au_start = 0, au_end = 0, rap_flag = 0, ocr_flag = 0, padding = 0, padding_bits = 0, idle = 0;
1137 pes_es->is_synced = 0;
1138 mp_msg(MSGT_DEMUXER,MSGL_V, "mp4_parse_sl_packet, pid: %d, pmt: %pm, packet_len: %d\n", pid, pmt, packet_len);
1139 if(! pmt || !packet_len)
1140 return 0;
1142 for(i = 0; i < pmt->es_cnt; i++)
1144 if(pmt->es[i].pid == pid)
1145 mp4_es_id = pmt->es[i].mp4_es_id;
1147 if(mp4_es_id < 0)
1148 return -1;
1150 for(i = 0; i < pmt->mp4es_cnt; i++)
1152 if(pmt->mp4es[i].id == mp4_es_id)
1153 es = &(pmt->mp4es[i]);
1155 if(! es)
1156 return -1;
1158 pes_es->subtype = es->decoder.object_type;
1160 sl = &(es->sl);
1161 if(!sl)
1162 return -1;
1164 //now es is the complete es_descriptor of out mp4 ES stream
1165 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "ID: %d, FLAGS: 0x%x, subtype: %x\n", es->id, sl->flags, pes_es->subtype);
1167 n = 0;
1168 if(sl->au_start)
1169 pes_es->sl.au_start = au_start = getbits(buf, n++, 1);
1170 else
1171 pes_es->sl.au_start = (pes_es->sl.last_au_end ? 1 : 0);
1172 if(sl->au_end)
1173 pes_es->sl.au_end = au_end = getbits(buf, n++, 1);
1175 if(!sl->au_start && !sl->au_end)
1177 pes_es->sl.au_start = pes_es->sl.au_end = au_start = au_end = 1;
1179 pes_es->sl.last_au_end = pes_es->sl.au_end;
1182 if(sl->ocr_len > 0)
1183 ocr_flag = getbits(buf, n++, 1);
1184 if(sl->idle)
1185 idle = getbits(buf, n++, 1);
1186 if(sl->padding)
1187 padding = getbits(buf, n++, 1);
1188 if(padding)
1190 padding_bits = getbits(buf, n, 3);
1191 n += 3;
1194 if(idle || (padding && !padding_bits))
1196 pes_es->payload_size = 0;
1197 return -1;
1200 //(! idle && (!padding || padding_bits != 0)) is true
1201 n += sl->packet_seqnum_len;
1202 if(sl->degr_len)
1203 deg_flag = getbits(buf, n++, 1);
1204 if(deg_flag)
1205 n += sl->degr_len;
1207 if(ocr_flag)
1209 n += sl->ocr_len;
1210 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "OCR: %d bits\n", sl->ocr_len);
1213 if(packet_len * 8 <= n)
1214 return -1;
1216 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "\nAU_START: %d, AU_END: %d\n", au_start, au_end);
1217 if(au_start)
1219 int dts_flag = 0, cts_flag = 0, ib_flag = 0;
1221 if(sl->random_accesspoint)
1222 rap_flag = getbits(buf, n++, 1);
1224 //check commented because it seems it's rarely used, and we need this flag set in case of au_start
1225 //the decoder will eventually discard the payload if it can't decode it
1226 //if(rap_flag || sl->random_accesspoint_only)
1227 pes_es->is_synced = 1;
1229 n += sl->au_seqnum_len;
1230 if(packet_len * 8 <= n+8)
1231 return -1;
1232 if(sl->use_ts)
1234 dts_flag = getbits(buf, n++, 1);
1235 cts_flag = getbits(buf, n++, 1);
1237 if(sl->instant_bitrate_len)
1238 ib_flag = getbits(buf, n++, 1);
1239 if(packet_len * 8 <= n+8)
1240 return -1;
1241 if(dts_flag && (sl->ts_len > 0))
1243 n += sl->ts_len;
1244 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "DTS: %d bits\n", sl->ts_len);
1246 if(packet_len * 8 <= n+8)
1247 return -1;
1248 if(cts_flag && (sl->ts_len > 0))
1250 int i = 0, m;
1252 while(i < sl->ts_len)
1254 m = FFMIN(8, sl->ts_len - i);
1255 v |= getbits(buf, n, m);
1256 if(sl->ts_len - i > 8)
1257 v <<= 8;
1258 i += m;
1259 n += m;
1260 if(packet_len * 8 <= n+8)
1261 return -1;
1264 pes_es->pts = (double) v / (double) sl->ts_resolution;
1265 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "CTS: %d bits, value: %"PRIu64"/%d = %.3f\n", sl->ts_len, v, sl->ts_resolution, pes_es->pts);
1269 i = 0;
1270 pl_size = 0;
1271 while(i < sl->au_len)
1273 m = FFMIN(8, sl->au_len - i);
1274 pl_size |= getbits(buf, n, m);
1275 if(sl->au_len - i > 8)
1276 pl_size <<= 8;
1277 i += m;
1278 n += m;
1279 if(packet_len * 8 <= n+8)
1280 return -1;
1282 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "AU_LEN: %u (%d bits)\n", pl_size, sl->au_len);
1283 if(ib_flag)
1284 n += sl->instant_bitrate_len;
1287 m = (n+7)/8;
1288 if(0 < pl_size && pl_size < pes_es->payload_size)
1289 pes_es->payload_size = pl_size;
1291 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",
1292 n, m, pes_es->payload_size, pl_size, (int) rap_flag, (int) sl->random_accesspoint_only);
1294 return m;
1297 //this function parses the extension fields in the PES header and returns the substream_id, or -1 in case of errors
1298 static int parse_pes_extension_fields(unsigned char *p, int pkt_len)
1300 int skip;
1301 unsigned char flags;
1303 if(!(p[7] & 0x1)) //no extension_field
1304 return -1;
1305 skip = 9;
1306 if(p[7] & 0x80)
1308 skip += 5;
1309 if(p[7] & 0x40)
1310 skip += 5;
1312 if(p[7] & 0x20) //escr_flag
1313 skip += 6;
1314 if(p[7] & 0x10) //es_rate_flag
1315 skip += 3;
1316 if(p[7] & 0x08)//dsm_trick_mode is unsupported, skip
1318 skip = 0;//don't let's parse the extension fields
1320 if(p[7] & 0x04) //additional_copy_info
1321 skip += 1;
1322 if(p[7] & 0x02) //pes_crc_flag
1323 skip += 2;
1324 if(skip >= pkt_len) //too few bytes
1325 return -1;
1326 flags = p[skip];
1327 skip++;
1328 if(flags & 0x80) //pes_private_data_flag
1329 skip += 16;
1330 if(skip >= pkt_len)
1331 return -1;
1332 if(flags & 0x40) //pack_header_field_flag
1334 unsigned char l = p[skip];
1335 skip += l;
1337 if(flags & 0x20) //program_packet_sequence_counter
1338 skip += 2;
1339 if(flags & 0x10) //p_std
1340 skip += 2;
1341 if(skip >= pkt_len)
1342 return -1;
1343 if(flags & 0x01) //finally the long desired pes_extension2
1345 unsigned char l = p[skip]; //ext2 flag+len
1346 skip++;
1347 if((l == 0x81) && (skip < pkt_len))
1349 int ssid = p[skip];
1350 mp_msg(MSGT_IDENTIFY, MSGL_V, "SUBSTREAM_ID=%d (0x%02X)\n", ssid, ssid);
1351 return ssid;
1355 return -1;
1358 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)
1360 unsigned char *p;
1361 uint32_t header_len;
1362 int64_t pts;
1363 uint32_t stream_id;
1364 uint32_t pkt_len, pes_is_aligned;
1366 //Here we are always at the start of a PES packet
1367 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2(%p, %d): \n", buf, (uint32_t) packet_len);
1369 if(packet_len == 0 || packet_len > 184)
1371 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2, BUFFER LEN IS TOO SMALL OR TOO BIG: %d EXIT\n", packet_len);
1372 return 0;
1375 p = buf;
1376 pkt_len = packet_len;
1379 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: HEADER %02x %02x %02x %02x\n", p[0], p[1], p[2], p[3]);
1380 if (p[0] || p[1] || (p[2] != 1))
1382 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: error HEADER %02x %02x %02x (should be 0x000001) \n", p[0], p[1], p[2]);
1383 return 0 ;
1386 packet_len -= 6;
1387 if(packet_len==0)
1389 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: packet too short: %d, exit\n", packet_len);
1390 return 0;
1393 es->payload_size = (p[4] << 8 | p[5]);
1394 pes_is_aligned = (p[6] & 4);
1396 stream_id = p[3];
1399 if (p[7] & 0x80)
1400 { /* pts available */
1401 pts = (int64_t)(p[9] & 0x0E) << 29 ;
1402 pts |= p[10] << 22 ;
1403 pts |= (p[11] & 0xFE) << 14 ;
1404 pts |= p[12] << 7 ;
1405 pts |= (p[13] & 0xFE) >> 1 ;
1407 es->pts = pts / 90000.0;
1409 else
1410 es->pts = 0.0;
1413 header_len = p[8];
1416 if (header_len + 9 > pkt_len) //9 are the bytes read up to the header_length field
1418 mp_msg(MSGT_DEMUX, MSGL_DBG2, "demux_ts: illegal value for PES_header_data_length (0x%02x)\n", header_len);
1419 return 0;
1422 if(stream_id==0xfd)
1424 int ssid = parse_pes_extension_fields(p, pkt_len);
1425 if((audio_substream_id!=-1) && (ssid != audio_substream_id))
1426 return 0;
1427 if(ssid == 0x72 && type_from_pmt != AUDIO_DTS && type_from_pmt != SPU_PGS)
1428 es->type = type_from_pmt = AUDIO_TRUEHD;
1431 p += header_len + 9;
1432 packet_len -= header_len + 3;
1434 if(es->payload_size)
1435 es->payload_size -= header_len + 3;
1438 es->is_synced = 1; //only for SL streams we have to make sure it's really true, see below
1439 if (stream_id == 0xbd)
1441 mp_msg(MSGT_DEMUX, MSGL_DBG3, "pes_parse2: audio buf = %02X %02X %02X %02X %02X %02X %02X %02X, 80: %d\n",
1442 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[0] & 0x80);
1446 * we check the descriptor tag first because some stations
1447 * do not include any of the A52 header info in their audio tracks
1448 * these "raw" streams may begin with a byte that looks like a stream type.
1452 if(type_from_pmt == SPU_PGS)
1454 es->start = p;
1455 es->size = packet_len;
1456 es->type = SPU_PGS;
1457 es->payload_size -= packet_len;
1458 return 1;
1461 (type_from_pmt == AUDIO_A52) || /* A52 - raw */
1462 (packet_len >= 2 && p[0] == 0x0B && p[1] == 0x77) /* A52 - syncword */
1465 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 RAW OR SYNCWORD\n");
1466 es->start = p;
1467 es->size = packet_len;
1468 es->type = AUDIO_A52;
1469 es->payload_size -= packet_len;
1471 return 1;
1473 /* SPU SUBS */
1474 else if(type_from_pmt == SPU_DVB ||
1475 (packet_len >= 1 && (p[0] == 0x20) && pes_is_aligned)) // && p[1] == 0x00))
1477 es->start = p;
1478 es->size = packet_len;
1479 es->type = SPU_DVB;
1480 es->payload_size -= packet_len;
1482 return 1;
1484 else if (pes_is_aligned && packet_len >= 1 && ((p[0] & 0xE0) == 0x20)) //SPU_DVD
1486 //DVD SUBS
1487 es->start = p+1;
1488 es->size = packet_len-1;
1489 es->type = SPU_DVD;
1490 es->payload_size -= packet_len;
1492 return 1;
1494 else if (pes_is_aligned && packet_len >= 4 && (p[0] & 0xF8) == 0x80)
1496 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 WITH HEADER\n");
1497 es->start = p+4;
1498 es->size = packet_len - 4;
1499 es->type = AUDIO_A52;
1500 es->payload_size -= packet_len;
1502 return 1;
1504 else if (pes_is_aligned && packet_len >= 1 && ((p[0]&0xf0) == 0xa0))
1506 int pcm_offset;
1508 for (pcm_offset=0; ++pcm_offset < packet_len-1 ; )
1510 if (p[pcm_offset] == 0x01 && p[pcm_offset+1] == 0x80)
1511 { /* START */
1512 pcm_offset += 2;
1513 break;
1517 es->start = p + pcm_offset;
1518 es->size = packet_len - pcm_offset;
1519 es->type = AUDIO_LPCM_BE;
1520 es->payload_size -= packet_len;
1522 return 1;
1524 else
1526 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PES_PRIVATE1\n");
1527 es->start = p;
1528 es->size = packet_len;
1529 es->type = (type_from_pmt == UNKNOWN ? PES_PRIVATE1 : type_from_pmt);
1530 es->payload_size -= packet_len;
1532 return 1;
1535 else if(((stream_id >= 0xe0) && (stream_id <= 0xef)) || (stream_id == 0xfd && type_from_pmt != UNKNOWN))
1537 es->start = p;
1538 es->size = packet_len;
1539 if(type_from_pmt != UNKNOWN)
1540 es->type = type_from_pmt;
1541 else
1542 es->type = VIDEO_MPEG2;
1543 if(es->payload_size)
1544 es->payload_size -= packet_len;
1546 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: M2V size %d\n", es->size);
1547 return 1;
1549 else if ((stream_id == 0xfa))
1551 int l;
1553 es->is_synced = 0;
1554 if(type_from_pmt != UNKNOWN) //MP4 A/V or SL
1556 es->start = p;
1557 es->size = packet_len;
1558 es->type = type_from_pmt;
1560 if(type_from_pmt == SL_PES_STREAM)
1562 //if(pes_is_aligned)
1564 l = mp4_parse_sl_packet(pmt, p, packet_len, pid, es);
1565 mp_msg(MSGT_DEMUX, MSGL_DBG2, "L=%d, TYPE=%x\n", l, type_from_pmt);
1566 if(l < 0)
1568 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: couldn't parse SL header, passing along full PES payload\n");
1569 l = 0;
1573 es->start += l;
1574 es->size -= l;
1577 if(es->payload_size)
1578 es->payload_size -= packet_len;
1579 return 1;
1582 else if ((stream_id & 0xe0) == 0xc0)
1584 es->start = p;
1585 es->size = packet_len;
1587 if(type_from_pmt != UNKNOWN)
1588 es->type = type_from_pmt;
1589 else
1590 es->type = AUDIO_MP2;
1592 es->payload_size -= packet_len;
1594 return 1;
1596 else if (type_from_pmt != -1) //as a last resort here we trust the PMT, if present
1598 es->start = p;
1599 es->size = packet_len;
1600 es->type = type_from_pmt;
1601 es->payload_size -= packet_len;
1603 return 1;
1605 else
1607 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: unknown packet, id: %x\n", stream_id);
1610 es->is_synced = 0;
1611 return 0;
1617 static int ts_sync(stream_t *stream)
1619 mp_msg(MSGT_DEMUX, MSGL_DBG3, "TS_SYNC \n");
1621 while (!stream->eof)
1622 if (stream_read_char(stream) == 0x47)
1623 return 1;
1625 return 0;
1629 static void ts_dump_streams(ts_priv_t *priv)
1631 int i;
1633 for(i = 0; i < 3; i++)
1635 if((priv->fifo[i].pack != NULL) && (priv->fifo[i].offset != 0))
1637 resize_demux_packet(priv->fifo[i].pack, priv->fifo[i].offset);
1638 ds_add_packet(priv->fifo[i].ds, priv->fifo[i].pack);
1639 priv->fifo[i].offset = 0;
1640 priv->fifo[i].pack = NULL;
1646 static inline int32_t prog_idx_in_pat(ts_priv_t *priv, uint16_t progid)
1648 int x;
1650 if(priv->pat.progs == NULL)
1651 return -1;
1653 for(x = 0; x < priv->pat.progs_cnt; x++)
1655 if(priv->pat.progs[x].id == progid)
1656 return x;
1659 return -1;
1663 static inline int32_t prog_id_in_pat(ts_priv_t *priv, uint16_t pid)
1665 int x;
1667 if(priv->pat.progs == NULL)
1668 return -1;
1670 for(x = 0; x < priv->pat.progs_cnt; x++)
1672 if(priv->pat.progs[x].pmt_pid == pid)
1673 return priv->pat.progs[x].id;
1676 return -1;
1679 static int collect_section(ts_section_t *section, int is_start, unsigned char *buff, int size)
1681 uint8_t *ptr;
1682 uint16_t tlen;
1683 int skip, tid;
1685 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, start: %d, size: %d, collected: %d\n", is_start, size, section->buffer_len);
1686 if(! is_start && !section->buffer_len)
1687 return 0;
1689 if(is_start)
1691 if(! section->buffer)
1693 section->buffer = malloc(4096 + 256);
1694 if(section->buffer == NULL)
1695 return 0;
1697 section->buffer_len = 0;
1700 if(size + section->buffer_len > 4096+256)
1702 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, excessive len: %d + %d\n", section->buffer_len, size);
1703 return 0;
1706 memcpy(&(section->buffer[section->buffer_len]), buff, size);
1707 section->buffer_len += size;
1709 if(section->buffer_len < 3)
1710 return 0;
1712 skip = section->buffer[0];
1713 if(skip + 4 > section->buffer_len)
1714 return 0;
1716 ptr = &(section->buffer[skip + 1]);
1717 tid = ptr[0];
1718 tlen = ((ptr[1] & 0x0f) << 8) | ptr[2];
1719 mp_msg(MSGT_DEMUX, MSGL_V, "SKIP: %d+1, TID: %d, TLEN: %d, COLLECTED: %d\n", skip, tid, tlen, section->buffer_len);
1720 if(section->buffer_len < (skip+1+3+tlen))
1722 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DATA IS NOT ENOUGH, NEXT TIME\n");
1723 return 0;
1726 return skip+1;
1729 static int parse_pat(ts_priv_t * priv, int is_start, unsigned char *buff, int size)
1731 int skip;
1732 unsigned char *ptr;
1733 unsigned char *base;
1734 int entries, i;
1735 uint16_t progid;
1736 struct pat_progs_t *tmp;
1737 ts_section_t *section;
1739 section = &(priv->pat.section);
1740 skip = collect_section(section, is_start, buff, size);
1741 if(! skip)
1742 return 0;
1744 ptr = &(section->buffer[skip]);
1745 //PARSING
1746 priv->pat.table_id = ptr[0];
1747 if(priv->pat.table_id != 0)
1748 return 0;
1749 priv->pat.ssi = (ptr[1] >> 7) & 0x1;
1750 priv->pat.curr_next = ptr[5] & 0x01;
1751 priv->pat.ts_id = (ptr[3] << 8 ) | ptr[4];
1752 priv->pat.version_number = (ptr[5] >> 1) & 0x1F;
1753 priv->pat.section_length = ((ptr[1] & 0x03) << 8 ) | ptr[2];
1754 priv->pat.section_number = ptr[6];
1755 priv->pat.last_section_number = ptr[7];
1757 //check_crc32(0xFFFFFFFFL, ptr, priv->pat.buffer_len - 4, &ptr[priv->pat.buffer_len - 4]);
1758 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);
1760 entries = (int) (priv->pat.section_length - 9) / 4; //entries per section
1762 for(i=0; i < entries; i++)
1764 int32_t idx;
1765 base = &ptr[8 + i*4];
1766 progid = (base[0] << 8) | base[1];
1768 if((idx = prog_idx_in_pat(priv, progid)) == -1)
1770 int sz = sizeof(struct pat_progs_t) * (priv->pat.progs_cnt+1);
1771 tmp = realloc_struct(priv->pat.progs, priv->pat.progs_cnt+1, sizeof(struct pat_progs_t));
1772 if(tmp == NULL)
1774 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PAT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
1775 break;
1777 priv->pat.progs = tmp;
1778 idx = priv->pat.progs_cnt;
1779 priv->pat.progs_cnt++;
1782 priv->pat.progs[idx].id = progid;
1783 priv->pat.progs[idx].pmt_pid = ((base[2] & 0x1F) << 8) | base[3];
1784 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);
1785 mp_msg(MSGT_IDENTIFY, MSGL_V, "PROGRAM_ID=%d (0x%02X), PMT_PID: %d(0x%02X)\n",
1786 progid, progid, priv->pat.progs[idx].pmt_pid, priv->pat.progs[idx].pmt_pid);
1789 return 1;
1793 static inline int32_t es_pid_in_pmt(pmt_t * pmt, uint16_t pid)
1795 uint16_t i;
1797 if(pmt == NULL)
1798 return -1;
1800 if(pmt->es == NULL)
1801 return -1;
1803 for(i = 0; i < pmt->es_cnt; i++)
1805 if(pmt->es[i].pid == pid)
1806 return (int32_t) i;
1809 return -1;
1813 static uint16_t get_mp4_desc_len(uint8_t *buf, int *len)
1815 //uint16_t i = 0, size = 0;
1816 int i = 0, j, size = 0;
1818 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PARSE_MP4_DESC_LEN(%d), bytes: ", *len);
1819 j = FFMIN(*len, 4);
1820 while(i < j)
1822 mp_msg(MSGT_DEMUX, MSGL_DBG2, " %x ", buf[i]);
1823 size |= (buf[i] & 0x7f);
1824 if(!(buf[i] & 0x80))
1825 break;
1826 size <<= 7;
1827 i++;
1829 mp_msg(MSGT_DEMUX, MSGL_DBG2, ", SIZE=%d\n", size);
1831 *len = i+1;
1832 return size;
1836 static uint16_t parse_mp4_slconfig_descriptor(uint8_t *buf, int len, void *elem)
1838 int i = 0;
1839 mp4_es_descr_t *es;
1840 mp4_sl_config_t *sl;
1842 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_SLCONFIG_DESCRIPTOR(%d)\n", len);
1843 es = (mp4_es_descr_t *) elem;
1844 if(!es)
1846 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1847 return len;
1849 sl = &(es->sl);
1851 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;
1852 sl->ocr = sl->dts = sl->cts = 0;
1854 if(buf[0] == 0)
1856 i++;
1857 sl->flags = buf[i];
1858 i++;
1859 sl->ts_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1860 i += 4;
1861 sl->ocr_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1862 i += 4;
1863 sl->ts_len = buf[i];
1864 i++;
1865 sl->ocr_len = buf[i];
1866 i++;
1867 sl->au_len = buf[i];
1868 i++;
1869 sl->instant_bitrate_len = buf[i];
1870 i++;
1871 sl->degr_len = (buf[i] >> 4) & 0x0f;
1872 sl->au_seqnum_len = ((buf[i] & 0x0f) << 1) | ((buf[i+1] >> 7) & 0x01);
1873 i++;
1874 sl->packet_seqnum_len = ((buf[i] >> 2) & 0x1f);
1875 i++;
1878 else if(buf[0] == 1)
1880 sl->flags = 0;
1881 sl->ts_resolution = 1000;
1882 sl->ts_len = 32;
1883 i++;
1885 else if(buf[0] == 2)
1887 sl->flags = 4;
1888 i++;
1890 else
1892 sl->flags = 0;
1893 i++;
1896 sl->au_start = (sl->flags >> 7) & 0x1;
1897 sl->au_end = (sl->flags >> 6) & 0x1;
1898 sl->random_accesspoint = (sl->flags >> 5) & 0x1;
1899 sl->random_accesspoint_only = (sl->flags >> 4) & 0x1;
1900 sl->padding = (sl->flags >> 3) & 0x1;
1901 sl->use_ts = (sl->flags >> 2) & 0x1;
1902 sl->idle = (sl->flags >> 1) & 0x1;
1903 sl->duration = sl->flags & 0x1;
1905 if(sl->duration)
1907 sl->timescale = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1908 i += 4;
1909 sl->au_duration = (buf[i] << 8) | buf[i+1];
1910 i += 2;
1911 sl->cts_duration = (buf[i] << 8) | buf[i+1];
1912 i += 2;
1914 else //no support for fixed durations atm
1915 sl->timescale = sl->au_duration = sl->cts_duration = 0;
1917 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",
1918 len, buf[0], sl->flags, sl->use_ts, sl->ts_len, sl->timescale, (uint64_t) sl->dts, (uint64_t) sl->cts);
1920 return len;
1923 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem);
1925 static uint16_t parse_mp4_decoder_config_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
1927 int i = 0, j;
1928 mp4_es_descr_t *es;
1929 mp4_decoder_config_t *dec;
1931 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_CONFIG_DESCRIPTOR(%d)\n", len);
1932 es = (mp4_es_descr_t *) elem;
1933 if(!es)
1935 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1936 return len;
1938 dec = (mp4_decoder_config_t*) &(es->decoder);
1940 dec->object_type = buf[i];
1941 dec->stream_type = (buf[i+1]>>2) & 0x3f;
1943 if(dec->object_type == 1 && dec->stream_type == 1)
1945 dec->object_type = MP4_OD;
1946 dec->stream_type = MP4_OD;
1948 else if(dec->stream_type == 4)
1950 if(dec->object_type == 0x6a)
1951 dec->object_type = VIDEO_MPEG1;
1952 if(dec->object_type >= 0x60 && dec->object_type <= 0x65)
1953 dec->object_type = VIDEO_MPEG2;
1954 else if(dec->object_type == 0x20)
1955 dec->object_type = VIDEO_MPEG4;
1956 else if(dec->object_type == 0x21)
1957 dec->object_type = VIDEO_AVC;
1958 /*else if(dec->object_type == 0x22)
1959 fprintf(stderr, "TYPE 0x22\n");*/
1960 else dec->object_type = UNKNOWN;
1962 else if(dec->stream_type == 5)
1964 if(dec->object_type == 0x40)
1965 dec->object_type = AUDIO_AAC;
1966 else if(dec->object_type == 0x6b)
1967 dec->object_type = AUDIO_MP2;
1968 else if(dec->object_type >= 0x66 && dec->object_type <= 0x69)
1969 dec->object_type = AUDIO_MP2;
1970 else
1971 dec->object_type = UNKNOWN;
1973 else
1974 dec->object_type = dec->stream_type = UNKNOWN;
1976 if(dec->object_type != UNKNOWN)
1978 //update the type of the current stream
1979 for(j = 0; j < pmt->es_cnt; j++)
1981 if(pmt->es[j].mp4_es_id == es->id)
1983 pmt->es[j].type = SL_PES_STREAM;
1988 if(len > 13)
1989 parse_mp4_descriptors(pmt, &buf[13], len-13, dec);
1991 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);
1993 return len;
1996 static uint16_t parse_mp4_decoder_specific_descriptor(uint8_t *buf, int len, void *elem)
1998 int i;
1999 mp4_decoder_config_t *dec;
2001 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_SPECIFIC_DESCRIPTOR(%d)\n", len);
2002 dec = (mp4_decoder_config_t *) elem;
2003 if(!dec)
2005 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
2006 return len;
2009 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4 SPECIFIC INFO BYTES: \n");
2010 for(i=0; i<len; i++)
2011 mp_msg(MSGT_DEMUX, MSGL_DBG2, "%02x ", buf[i]);
2012 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\n");
2014 if(len > MAX_EXTRADATA_SIZE)
2016 mp_msg(MSGT_DEMUX, MSGL_ERR, "DEMUX_TS, EXTRADATA SUSPICIOUSLY BIG: %d, REFUSED\r\n", len);
2017 return len;
2019 memcpy(dec->buf, buf, len);
2020 dec->buf_size = len;
2022 return len;
2025 static uint16_t parse_mp4_es_descriptor(pmt_t *pmt, uint8_t *buf, int len)
2027 int i = 0, j = 0, k, found;
2028 uint8_t flag;
2029 mp4_es_descr_t es, *target_es = NULL, *tmp;
2031 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4ES: len=%d\n", len);
2032 memset(&es, 0, sizeof(mp4_es_descr_t));
2033 while(i < len)
2035 es.id = (buf[i] << 8) | buf[i+1];
2036 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_ID: %d\n", es.id);
2037 i += 2;
2038 flag = buf[i];
2039 i++;
2040 if(flag & 0x80)
2041 i += 2;
2042 if(flag & 0x40)
2043 i += buf[i]+1;
2044 if(flag & 0x20) //OCR, maybe we need it
2045 i += 2;
2047 j = parse_mp4_descriptors(pmt, &buf[i], len-i, &es);
2048 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);
2049 if(es.decoder.object_type != UNKNOWN && es.decoder.stream_type != UNKNOWN)
2051 found = 0;
2052 //search this ES_ID if we already have it
2053 for(k=0; k < pmt->mp4es_cnt; k++)
2055 if(pmt->mp4es[k].id == es.id)
2057 target_es = &(pmt->mp4es[k]);
2058 found = 1;
2062 if(! found)
2064 tmp = realloc_struct(pmt->mp4es, pmt->mp4es_cnt+1, sizeof(mp4_es_descr_t));
2065 if(tmp == NULL)
2067 fprintf(stderr, "CAN'T REALLOC MP4_ES_DESCR\n");
2068 continue;
2070 pmt->mp4es = tmp;
2071 target_es = &(pmt->mp4es[pmt->mp4es_cnt]);
2072 pmt->mp4es_cnt++;
2074 memcpy(target_es, &es, sizeof(mp4_es_descr_t));
2075 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_CNT: %d, ID=%d\n", pmt->mp4es_cnt, target_es->id);
2078 i += j;
2081 return len;
2084 static void parse_mp4_object_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2086 int i, j = 0, id;
2088 i=0;
2089 id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2090 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_OBJECT_DESCRIPTOR: len=%d, OD_ID=%d\n", len, id);
2091 if(buf[1] & 0x20)
2093 i += buf[2] + 1; //url
2094 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2096 else
2098 i = 2;
2100 while(i < len)
2102 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2103 mp_msg(MSGT_DEMUX, MSGL_V, "OBJD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2104 i += j;
2110 static void parse_mp4_iod(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2112 int i, j = 0;
2113 mp4_od_t *iod = &(pmt->iod);
2115 iod->id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2116 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_IOD: len=%d, IOD_ID=%d\n", len, iod->id);
2117 i = 2;
2118 if(buf[1] & 0x20)
2120 i += buf[2] + 1; //url
2121 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2123 else
2125 i = 7;
2126 while(i < len)
2128 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2129 mp_msg(MSGT_DEMUX, MSGL_V, "IOD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2130 i += j;
2135 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2137 int tag, descr_len, i = 0, j = 0;
2139 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DESCRIPTORS, len=%d\n", len);
2140 if(! len)
2141 return len;
2143 while(i < len)
2145 tag = buf[i];
2146 j = len - i -1;
2147 descr_len = get_mp4_desc_len(&(buf[i+1]), &j);
2148 mp_msg(MSGT_DEMUX, MSGL_V, "TAG=%d (0x%x), DESCR_len=%d, len=%d, j=%d\n", tag, tag, descr_len, len, j);
2149 if(descr_len > len - j+1)
2151 mp_msg(MSGT_DEMUX, MSGL_V, "descriptor is too long, exit\n");
2152 return len;
2154 i += j+1;
2156 switch(tag)
2158 case 0x1:
2159 parse_mp4_object_descriptor(pmt, &(buf[i]), descr_len, elem);
2160 break;
2161 case 0x2:
2162 parse_mp4_iod(pmt, &(buf[i]), descr_len, elem);
2163 break;
2164 case 0x3:
2165 parse_mp4_es_descriptor(pmt, &(buf[i]), descr_len);
2166 break;
2167 case 0x4:
2168 parse_mp4_decoder_config_descriptor(pmt, &buf[i], descr_len, elem);
2169 break;
2170 case 0x05:
2171 parse_mp4_decoder_specific_descriptor(&buf[i], descr_len, elem);
2172 break;
2173 case 0x6:
2174 parse_mp4_slconfig_descriptor(&buf[i], descr_len, elem);
2175 break;
2176 default:
2177 mp_msg(MSGT_DEMUX, MSGL_V, "Unsupported mp4 descriptor 0x%x\n", tag);
2179 i += descr_len;
2182 return len;
2185 static ES_stream_t *new_pid(ts_priv_t *priv, int pid)
2187 ES_stream_t *tss;
2189 tss = malloc(sizeof(ES_stream_t));
2190 if(! tss)
2191 return NULL;
2192 memset(tss, 0, sizeof(ES_stream_t));
2193 tss->pid = pid;
2194 tss->last_cc = -1;
2195 tss->type = UNKNOWN;
2196 tss->subtype = UNKNOWN;
2197 tss->is_synced = 0;
2198 tss->extradata = NULL;
2199 tss->extradata_alloc = tss->extradata_len = 0;
2200 priv->ts.pids[pid] = tss;
2202 return tss;
2206 static int parse_program_descriptors(pmt_t *pmt, uint8_t *buf, uint16_t len)
2208 uint16_t i = 0, k, olen = len;
2210 while(len > 0)
2212 mp_msg(MSGT_DEMUX, MSGL_V, "PROG DESCR, TAG=%x, LEN=%d(%x)\n", buf[i], buf[i+1], buf[i+1]);
2213 if(buf[i+1] > len-2)
2215 mp_msg(MSGT_DEMUX, MSGL_V, "ERROR, descriptor len is too long, skipping\n");
2216 return olen;
2219 if(buf[i] == 0x1d)
2221 if(buf[i+3] == 2) //buggy versions of vlc muxer make this non-standard mess (missing iod_scope)
2222 k = 3;
2223 else
2224 k = 4; //this is standard compliant
2225 parse_mp4_descriptors(pmt, &buf[i+k], (int) buf[i+1]-(k-2), NULL);
2228 len -= 2 + buf[i+1];
2231 return olen;
2234 static int parse_descriptors(struct pmt_es_t *es, uint8_t *ptr)
2236 int j, descr_len, len;
2238 j = 0;
2239 len = es->descr_length;
2240 while(len > 2)
2242 descr_len = ptr[j+1];
2243 mp_msg(MSGT_DEMUX, MSGL_V, "...descr id: 0x%x, len=%d\n", ptr[j], descr_len);
2244 if(descr_len > len)
2246 mp_msg(MSGT_DEMUX, MSGL_ERR, "INVALID DESCR LEN for tag %02x: %d vs %d max, EXIT LOOP\n", ptr[j], descr_len, len);
2247 return -1;
2251 if(ptr[j] == 0x6a || ptr[j] == 0x7a) //A52 Descriptor
2253 if(es->type == 0x6)
2255 es->type = AUDIO_A52;
2256 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB A52 Descriptor\n");
2259 else if(ptr[j] == 0x7b) //DVB DTS Descriptor
2261 if(es->type == 0x6)
2263 es->type = AUDIO_DTS;
2264 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB DTS Descriptor\n");
2267 else if(ptr[j] == 0x56) // Teletext
2269 if(descr_len >= 5) {
2270 memcpy(es->lang, ptr+2, 3);
2271 es->lang[3] = 0;
2273 es->type = SPU_TELETEXT;
2275 else if(ptr[j] == 0x59) //Subtitling Descriptor
2277 uint8_t subtype;
2279 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Subtitling Descriptor\n");
2280 if(descr_len < 8)
2282 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Descriptor length too short for DVB Subtitle Descriptor: %d, SKIPPING\n", descr_len);
2284 else
2286 memcpy(es->lang, &ptr[j+2], 3);
2287 es->lang[3] = 0;
2288 subtype = ptr[j+5];
2290 (subtype >= 0x10 && subtype <= 0x13) ||
2291 (subtype >= 0x20 && subtype <= 0x23)
2294 es->type = SPU_DVB;
2295 //page parameters: compo page 2 bytes, ancillary page 2 bytes
2297 else
2298 es->type = UNKNOWN;
2301 else if(ptr[j] == 0x50) //Component Descriptor
2303 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Component Descriptor\n");
2304 memcpy(es->lang, &ptr[j+5], 3);
2305 es->lang[3] = 0;
2307 else if(ptr[j] == 0xa) //Language Descriptor
2309 memcpy(es->lang, &ptr[j+2], 3);
2310 es->lang[3] = 0;
2311 mp_msg(MSGT_DEMUX, MSGL_V, "Language Descriptor: %s\n", es->lang);
2313 else if(ptr[j] == 0x5) //Registration Descriptor (looks like e fourCC :) )
2315 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor\n");
2316 if(descr_len < 4)
2318 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor length too short: %d, SKIPPING\n", descr_len);
2320 else
2322 char *d;
2323 memcpy(es->format_descriptor, &ptr[j+2], 4);
2324 es->format_descriptor[4] = 0;
2326 d = &ptr[j+2];
2327 if(d[0] == 'A' && d[1] == 'C' && d[2] == '-' && d[3] == '3')
2329 es->type = AUDIO_A52;
2331 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '1')
2333 es->type = AUDIO_DTS;
2335 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '2')
2337 es->type = AUDIO_DTS;
2339 else if(d[0] == 'V' && d[1] == 'C' && d[2] == '-' && d[3] == '1')
2341 es->type = VIDEO_VC1;
2343 else if(d[0] == 'd' && d[1] == 'r' && d[2] == 'a' && d[3] == 'c')
2345 es->type = VIDEO_DIRAC;
2347 else
2348 es->type = UNKNOWN;
2349 mp_msg(MSGT_DEMUX, MSGL_DBG2, "FORMAT %s\n", es->format_descriptor);
2352 else if(ptr[j] == 0x1e)
2354 es->mp4_es_id = (ptr[j+2] << 8) | ptr[j+3];
2355 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);
2357 else
2358 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Unknown descriptor 0x%x, SKIPPING\n", ptr[j]);
2360 len -= 2 + descr_len;
2361 j += 2 + descr_len;
2364 return 1;
2367 static int parse_sl_section(pmt_t *pmt, ts_section_t *section, int is_start, unsigned char *buff, int size)
2369 int tid, len, skip;
2370 uint8_t *ptr;
2371 skip = collect_section(section, is_start, buff, size);
2372 if(! skip)
2373 return 0;
2375 ptr = &(section->buffer[skip]);
2376 tid = ptr[0];
2377 len = ((ptr[1] & 0x0f) << 8) | ptr[2];
2378 mp_msg(MSGT_DEMUX, MSGL_V, "TABLEID: %d (av. %d), skip=%d, LEN: %d\n", tid, section->buffer_len, skip, len);
2379 if(len > 4093 || section->buffer_len < len || tid != 5)
2381 mp_msg(MSGT_DEMUX, MSGL_V, "SECTION TOO LARGE or wrong section type, EXIT\n");
2382 return 0;
2385 if(! (ptr[5] & 1))
2386 return 0;
2388 //8 is the current position, len - 9 is the amount of data available
2389 parse_mp4_descriptors(pmt, &ptr[8], len - 9, NULL);
2391 return 1;
2394 static int parse_pmt(ts_priv_t * priv, uint16_t progid, uint16_t pid, int is_start, unsigned char *buff, int size)
2396 unsigned char *base, *es_base;
2397 pmt_t *pmt;
2398 int32_t idx, es_count, section_bytes;
2399 uint8_t m=0;
2400 int skip;
2401 pmt_t *tmp;
2402 struct pmt_es_t *tmp_es;
2403 ts_section_t *section;
2404 ES_stream_t *tss;
2406 idx = progid_idx_in_pmt(priv, progid);
2408 if(idx == -1)
2410 int sz = (priv->pmt_cnt + 1) * sizeof(pmt_t);
2411 tmp = realloc_struct(priv->pmt, priv->pmt_cnt + 1, sizeof(pmt_t));
2412 if(tmp == NULL)
2414 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
2415 return 0;
2417 priv->pmt = tmp;
2418 idx = priv->pmt_cnt;
2419 memset(&(priv->pmt[idx]), 0, sizeof(pmt_t));
2420 priv->pmt_cnt++;
2421 priv->pmt[idx].progid = progid;
2424 pmt = &(priv->pmt[idx]);
2426 section = &(pmt->section);
2427 skip = collect_section(section, is_start, buff, size);
2428 if(! skip)
2429 return 0;
2431 base = &(section->buffer[skip]);
2433 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",
2434 progid, pmt->section.buffer_len, is_start, pid, size, m, pmt->es_cnt, idx, pmt);
2436 pmt->table_id = base[0];
2437 if(pmt->table_id != 2)
2438 return -1;
2439 pmt->ssi = base[1] & 0x80;
2440 pmt->section_length = (((base[1] & 0xf) << 8 ) | base[2]);
2441 pmt->version_number = (base[5] >> 1) & 0x1f;
2442 pmt->curr_next = (base[5] & 1);
2443 pmt->section_number = base[6];
2444 pmt->last_section_number = base[7];
2445 pmt->PCR_PID = ((base[8] & 0x1f) << 8 ) | base[9];
2446 pmt->prog_descr_length = ((base[10] & 0xf) << 8 ) | base[11];
2447 if(pmt->prog_descr_length > pmt->section_length - 9)
2449 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, INVALID PROG_DESCR LENGTH (%d vs %d)\n", pmt->prog_descr_length, pmt->section_length - 9);
2450 return -1;
2453 if(pmt->prog_descr_length)
2454 parse_program_descriptors(pmt, &base[12], pmt->prog_descr_length);
2456 es_base = &base[12 + pmt->prog_descr_length]; //the beginning of th ES loop
2458 section_bytes= pmt->section_length - 13 - pmt->prog_descr_length;
2459 es_count = 0;
2461 while(section_bytes >= 5)
2463 int es_pid, es_type;
2465 es_type = es_base[0];
2466 es_pid = ((es_base[1] & 0x1f) << 8) | es_base[2];
2468 idx = es_pid_in_pmt(pmt, es_pid);
2469 if(idx == -1)
2471 int sz = sizeof(struct pmt_es_t) * (pmt->es_cnt + 1);
2472 tmp_es = realloc_struct(pmt->es, pmt->es_cnt + 1, sizeof(struct pmt_es_t));
2473 if(tmp_es == NULL)
2475 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT, COULDN'T ALLOCATE %d bytes for PMT_ES\n", sz);
2476 continue;
2478 pmt->es = tmp_es;
2479 idx = pmt->es_cnt;
2480 memset(&(pmt->es[idx]), 0, sizeof(struct pmt_es_t));
2481 pmt->es_cnt++;
2484 pmt->es[idx].descr_length = ((es_base[3] & 0xf) << 8) | es_base[4];
2487 if(pmt->es[idx].descr_length > section_bytes - 5)
2489 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, ES_DESCR_LENGTH TOO LARGE %d > %d, EXIT\n",
2490 pmt->es[idx].descr_length, section_bytes - 5);
2491 return -1;
2495 pmt->es[idx].pid = es_pid;
2496 if(es_type != 0x6)
2497 pmt->es[idx].type = UNKNOWN;
2498 else
2499 pmt->es[idx].type = es_type;
2501 parse_descriptors(&pmt->es[idx], &es_base[5]);
2503 switch(es_type)
2505 case 1:
2506 pmt->es[idx].type = VIDEO_MPEG1;
2507 break;
2508 case 2:
2509 pmt->es[idx].type = VIDEO_MPEG2;
2510 break;
2511 case 3:
2512 case 4:
2513 pmt->es[idx].type = AUDIO_MP2;
2514 break;
2515 case 6:
2516 if(pmt->es[idx].type == 0x6) //this could have been ovrwritten by parse_descriptors
2517 pmt->es[idx].type = UNKNOWN;
2518 break;
2519 case 0x10:
2520 pmt->es[idx].type = VIDEO_MPEG4;
2521 break;
2522 case 0x0f:
2523 pmt->es[idx].type = AUDIO_AAC;
2524 break;
2525 case 0x11:
2526 pmt->es[idx].type = AUDIO_AAC_LATM;
2527 break;
2528 case 0x1b:
2529 pmt->es[idx].type = VIDEO_H264;
2530 break;
2531 case 0x12:
2532 pmt->es[idx].type = SL_PES_STREAM;
2533 break;
2534 case 0x13:
2535 pmt->es[idx].type = SL_SECTION;
2536 break;
2537 case 0x81:
2538 pmt->es[idx].type = AUDIO_A52;
2539 break;
2540 case 0x8A:
2541 case 0x82:
2542 case 0x85:
2543 case 0x86:
2544 pmt->es[idx].type = AUDIO_DTS;
2545 break;
2546 case 0x90:
2547 pmt->es[idx].type = SPU_PGS;
2548 break;
2549 case 0xD1:
2550 pmt->es[idx].type = VIDEO_DIRAC;
2551 break;
2552 case 0xEA:
2553 pmt->es[idx].type = VIDEO_VC1;
2554 break;
2555 default:
2556 mp_msg(MSGT_DEMUX, MSGL_DBG2, "UNKNOWN ES TYPE=0x%x\n", es_type);
2557 pmt->es[idx].type = UNKNOWN;
2560 tss = priv->ts.pids[es_pid]; //an ES stream
2561 if(tss == NULL)
2563 tss = new_pid(priv, es_pid);
2564 if(tss)
2565 tss->type = pmt->es[idx].type;
2568 section_bytes -= 5 + pmt->es[idx].descr_length;
2569 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",
2570 progid, idx, es_count, pmt->es[idx].pid, pmt->es[idx].pid, pmt->es[idx].type, pmt->es[idx].descr_length, section_bytes);
2573 es_base += 5 + pmt->es[idx].descr_length;
2575 es_count++;
2578 mp_msg(MSGT_DEMUX, MSGL_V, "----------------------------\n");
2579 return 1;
2582 static pmt_t* pmt_of_pid(ts_priv_t *priv, int pid, mp4_decoder_config_t **mp4_dec)
2584 int32_t i, j, k;
2586 if(priv->pmt)
2588 for(i = 0; i < priv->pmt_cnt; i++)
2590 if(priv->pmt[i].es && priv->pmt[i].es_cnt)
2592 for(j = 0; j < priv->pmt[i].es_cnt; j++)
2594 if(priv->pmt[i].es[j].pid == pid)
2596 //search mp4_es_id
2597 if(priv->pmt[i].es[j].mp4_es_id)
2599 for(k = 0; k < priv->pmt[i].mp4es_cnt; k++)
2601 if(priv->pmt[i].mp4es[k].id == priv->pmt[i].es[j].mp4_es_id)
2603 *mp4_dec = &(priv->pmt[i].mp4es[k].decoder);
2604 break;
2609 return &(priv->pmt[i]);
2616 return NULL;
2620 static inline int32_t pid_type_from_pmt(ts_priv_t *priv, int pid)
2622 int32_t pmt_idx, pid_idx, i, j;
2624 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2626 if(pmt_idx != -1)
2628 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2629 if(pid_idx != -1)
2630 return priv->pmt[pmt_idx].es[pid_idx].type;
2632 //else
2634 for(i = 0; i < priv->pmt_cnt; i++)
2636 pmt_t *pmt = &(priv->pmt[i]);
2637 for(j = 0; j < pmt->es_cnt; j++)
2638 if(pmt->es[j].pid == pid)
2639 return pmt->es[j].type;
2643 return UNKNOWN;
2647 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid)
2649 int32_t pmt_idx, pid_idx, i, j;
2651 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2653 if(pmt_idx != -1)
2655 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2656 if(pid_idx != -1)
2657 return priv->pmt[pmt_idx].es[pid_idx].lang;
2659 else
2661 for(i = 0; i < priv->pmt_cnt; i++)
2663 pmt_t *pmt = &(priv->pmt[i]);
2664 for(j = 0; j < pmt->es_cnt; j++)
2665 if(pmt->es[j].pid == pid)
2666 return pmt->es[j].lang;
2670 return NULL;
2674 static int fill_packet(demuxer_t *demuxer, demux_stream_t *ds, demux_packet_t **dp, int *dp_offset, TS_stream_info *si)
2676 int ret = 0;
2678 if((*dp != NULL) && (*dp_offset > 0))
2680 ret = *dp_offset;
2681 resize_demux_packet(*dp, ret); //shrinked to the right size
2682 ds_add_packet(ds, *dp);
2683 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);
2684 if(si)
2686 float diff = (*dp)->pts - si->last_pts;
2687 float dur;
2689 if(abs(diff) > 1) //1 second, there's a discontinuity
2691 si->duration += si->last_pts - si->first_pts;
2692 si->first_pts = si->last_pts = (*dp)->pts;
2694 else
2696 si->last_pts = (*dp)->pts;
2698 si->size += ret;
2699 dur = si->duration + (si->last_pts - si->first_pts);
2701 if(dur > 0 && ds == demuxer->video)
2703 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2704 if(dur > 1) //otherwise it may be unreliable
2705 priv->vbitrate = (uint32_t) ((float) si->size / dur);
2710 *dp = NULL;
2711 *dp_offset = 0;
2713 return ret;
2716 static int fill_extradata(mp4_decoder_config_t * mp4_dec, ES_stream_t *tss)
2718 uint8_t *tmp;
2720 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4_dec: %p, pid: %d\n", mp4_dec, tss->pid);
2722 if(mp4_dec->buf_size > tss->extradata_alloc)
2724 tmp = realloc(tss->extradata, mp4_dec->buf_size);
2725 if(!tmp)
2726 return 0;
2727 tss->extradata = tmp;
2728 tss->extradata_alloc = mp4_dec->buf_size;
2730 memcpy(tss->extradata, mp4_dec->buf, mp4_dec->buf_size);
2731 tss->extradata_len = mp4_dec->buf_size;
2732 mp_msg(MSGT_DEMUX, MSGL_V, "EXTRADATA: %p, alloc=%d, len=%d\n", tss->extradata, tss->extradata_alloc, tss->extradata_len);
2734 return tss->extradata_len;
2737 // 0 = EOF or no stream found
2738 // else = [-] number of bytes written to the packet
2739 static int ts_parse(demuxer_t *demuxer , ES_stream_t *es, unsigned char *packet, int probe)
2741 ES_stream_t *tss;
2742 int buf_size, is_start, pid, base;
2743 int len, cc, cc_ok, afc, retv = 0, is_video, is_audio, is_sub;
2744 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2745 stream_t *stream = demuxer->stream;
2746 char *p;
2747 demux_stream_t *ds = NULL;
2748 demux_packet_t **dp = NULL;
2749 int *dp_offset = 0, *buffer_size = 0;
2750 int32_t progid, pid_type, bad, ts_error;
2751 int junk = 0, rap_flag = 0;
2752 pmt_t *pmt;
2753 mp4_decoder_config_t *mp4_dec;
2754 TS_stream_info *si;
2757 while(1)
2759 bad = ts_error = 0;
2760 ds = NULL;
2761 dp = NULL;
2762 dp_offset = buffer_size = NULL;
2763 rap_flag = 0;
2764 mp4_dec = NULL;
2765 es->is_synced = 0;
2766 es->lang[0] = 0;
2767 si = NULL;
2769 junk = priv->ts.packet_size - TS_PACKET_SIZE;
2770 buf_size = priv->ts.packet_size - junk;
2772 if(stream_eof(stream))
2774 if(! probe)
2776 ts_dump_streams(priv);
2777 demuxer->filepos = stream_tell(demuxer->stream);
2780 return 0;
2784 if(! ts_sync(stream))
2786 mp_msg(MSGT_DEMUX, MSGL_INFO, "TS_PARSE: COULDN'T SYNC\n");
2787 return 0;
2790 len = stream_read(stream, &packet[1], 3);
2791 if (len != 3)
2792 return 0;
2793 buf_size -= 4;
2795 if((packet[1] >> 7) & 0x01) //transport error
2796 ts_error = 1;
2799 is_start = packet[1] & 0x40;
2800 pid = ((packet[1] & 0x1f) << 8) | packet[2];
2802 tss = priv->ts.pids[pid]; //an ES stream
2803 if(tss == NULL)
2805 tss = new_pid(priv, pid);
2806 if(tss == NULL)
2807 continue;
2810 cc = (packet[3] & 0xf);
2811 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
2812 tss->last_cc = cc;
2814 bad = ts_error; // || (! cc_ok);
2815 if(bad)
2817 if(priv->keep_broken == 0)
2819 stream_skip(stream, buf_size-1+junk);
2820 continue;
2823 is_start = 0; //queued to the packet data
2826 if(is_start)
2827 tss->is_synced = 1;
2829 if((!is_start && !tss->is_synced) || ((pid > 1) && (pid < 16)) || (pid == 8191)) //invalid pid
2831 stream_skip(stream, buf_size-1+junk);
2832 continue;
2836 afc = (packet[3] >> 4) & 3;
2837 if(! (afc % 2)) //no payload in this TS packet
2839 stream_skip(stream, buf_size-1+junk);
2840 continue;
2843 if(afc > 1)
2845 int c;
2846 c = stream_read_char(stream);
2847 buf_size--;
2848 if(c < 0 || c > 183) //broken from the stream layer or invalid
2850 stream_skip(stream, buf_size-1+junk);
2851 continue;
2854 //c==0 is allowed!
2855 if(c > 0)
2857 uint8_t pcrbuf[188];
2858 int flags = stream_read_char(stream);
2859 int has_pcr;
2860 rap_flag = (flags & 0x40) >> 6;
2861 has_pcr = flags & 0x10;
2863 buf_size--;
2864 c--;
2865 stream_read(stream, pcrbuf, c);
2867 if(has_pcr)
2869 int pcr_pid = prog_pcr_pid(priv, priv->prog);
2870 if(pcr_pid == pid)
2872 uint64_t pcr, pcr_ext;
2874 pcr = (int64_t)(pcrbuf[0]) << 25;
2875 pcr |= pcrbuf[1] << 17 ;
2876 pcr |= (pcrbuf[2]) << 9;
2877 pcr |= pcrbuf[3] << 1 ;
2878 pcr |= (pcrbuf[4] & 0x80) >> 7;
2880 pcr_ext = (pcrbuf[4] & 0x01) << 8;
2881 pcr_ext |= pcrbuf[5];
2883 pcr = pcr * 300 + pcr_ext;
2885 demuxer->reference_clock = (double)pcr/(double)27000000.0;
2889 buf_size -= c;
2890 if(buf_size == 0)
2891 continue;
2895 //find the program that the pid belongs to; if (it's the right one or -1) && pid_type==SL_SECTION
2896 //call parse_sl_section()
2897 pmt = pmt_of_pid(priv, pid, &mp4_dec);
2898 if(mp4_dec)
2900 fill_extradata(mp4_dec, tss);
2901 if(IS_VIDEO(mp4_dec->object_type) || IS_AUDIO(mp4_dec->object_type))
2903 tss->type = SL_PES_STREAM;
2904 tss->subtype = mp4_dec->object_type;
2909 //TABLE PARSING
2911 base = priv->ts.packet_size - buf_size;
2913 priv->last_pid = pid;
2915 is_video = IS_VIDEO(tss->type) || (tss->type==SL_PES_STREAM && IS_VIDEO(tss->subtype));
2916 is_audio = IS_AUDIO(tss->type) || (tss->type==SL_PES_STREAM && IS_AUDIO(tss->subtype)) || (tss->type == PES_PRIVATE1);
2917 is_sub = IS_SUB(tss->type);
2918 pid_type = pid_type_from_pmt(priv, pid);
2920 // PES CONTENT STARTS HERE
2921 if(! probe)
2923 if((is_video || is_audio || is_sub) && is_start)
2924 ts_add_stream(demuxer, tss);
2926 if(is_video && (demuxer->video->id == priv->ts.streams[pid].id))
2928 ds = demuxer->video;
2930 dp = &priv->fifo[1].pack;
2931 dp_offset = &priv->fifo[1].offset;
2932 buffer_size = &priv->fifo[1].buffer_size;
2933 si = &priv->vstr;
2935 else if(is_audio && (demuxer->audio->id == priv->ts.streams[pid].id))
2937 ds = demuxer->audio;
2939 dp = &priv->fifo[0].pack;
2940 dp_offset = &priv->fifo[0].offset;
2941 buffer_size = &priv->fifo[0].buffer_size;
2942 si = &priv->astr;
2944 else if(is_sub)
2946 sh_sub_t *sh_sub = demuxer->sub->sh;
2948 if(sh_sub && sh_sub->sid == tss->pid)
2950 ds = demuxer->sub;
2952 dp = &priv->fifo[2].pack;
2953 dp_offset = &priv->fifo[2].offset;
2954 buffer_size = &priv->fifo[2].buffer_size;
2956 else
2958 stream_skip(stream, buf_size+junk);
2959 continue;
2963 //IS IT TIME TO QUEUE DATA to the dp_packet?
2964 if(is_start && (dp != NULL))
2966 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
2970 if(dp && *dp == NULL)
2972 if(*buffer_size > MAX_PACK_BYTES)
2973 *buffer_size = MAX_PACK_BYTES;
2974 *dp = new_demux_packet(*buffer_size); //es->size
2975 *dp_offset = 0;
2976 if(! *dp)
2978 fprintf(stderr, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", *buffer_size);
2979 continue;
2981 mp_msg(MSGT_DEMUX, MSGL_DBG2, "CREATED DP(%d)\n", *buffer_size);
2986 if(probe || !dp) //dp is NULL for tables and sections
2988 p = &packet[base];
2990 else //feeding
2992 if(*dp_offset + buf_size > *buffer_size)
2994 *buffer_size = *dp_offset + buf_size + TS_FEC_PACKET_SIZE;
2995 resize_demux_packet(*dp, *buffer_size);
2997 p = &((*dp)->buffer[*dp_offset]);
3000 len = stream_read(stream, p, buf_size);
3001 if(len < buf_size)
3003 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\r\nts_parse() couldn't read enough data: %d < %d\r\n", len, buf_size);
3004 continue;
3006 stream_skip(stream, junk);
3008 if(pid == 0)
3010 parse_pat(priv, is_start, p, buf_size);
3011 continue;
3013 else if((tss->type == SL_SECTION) && pmt)
3015 int k, mp4_es_id = -1;
3016 ts_section_t *section;
3017 for(k = 0; k < pmt->mp4es_cnt; k++)
3019 if(pmt->mp4es[k].decoder.object_type == MP4_OD && pmt->mp4es[k].decoder.stream_type == MP4_OD)
3020 mp4_es_id = pmt->mp4es[k].id;
3022 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4ESID: %d\n", mp4_es_id);
3023 for(k = 0; k < pmt->es_cnt; k++)
3025 if(pmt->es[k].mp4_es_id == mp4_es_id)
3027 section = &(tss->section);
3028 parse_sl_section(pmt, section, is_start, &packet[base], buf_size);
3031 continue;
3033 else
3035 progid = prog_id_in_pat(priv, pid);
3036 if(progid != -1)
3038 if(pid != demuxer->video->id && pid != demuxer->audio->id && pid != demuxer->sub->id)
3040 parse_pmt(priv, progid, pid, is_start, &packet[base], buf_size);
3041 continue;
3043 else
3044 mp_msg(MSGT_DEMUX, MSGL_ERR, "Argh! Data pid %d used in the PMT, Skipping PMT parsing!\n", pid);
3048 if(!probe && !dp)
3049 continue;
3051 if(is_start)
3053 uint8_t *lang = NULL;
3055 mp_msg(MSGT_DEMUX, MSGL_DBG2, "IS_START\n");
3057 len = pes_parse2(p, buf_size, es, pid_type, pmt, pid);
3058 if(! len)
3060 tss->is_synced = 0;
3061 continue;
3063 es->pid = tss->pid;
3064 tss->is_synced |= es->is_synced || rap_flag;
3065 tss->payload_size = es->payload_size;
3067 if((is_sub || is_audio) && (lang = pid_lang_from_pmt(priv, es->pid)))
3069 memcpy(es->lang, lang, 3);
3070 es->lang[3] = 0;
3072 else
3073 es->lang[0] = 0;
3075 if(probe)
3077 if(es->type == UNKNOWN)
3078 return 0;
3080 tss->type = es->type;
3081 tss->subtype = es->subtype;
3083 return 1;
3085 else
3087 if(es->pts == 0.0)
3088 es->pts = tss->pts = tss->last_pts;
3089 else
3090 tss->pts = tss->last_pts = es->pts;
3092 mp_msg(MSGT_DEMUX, MSGL_DBG2, "ts_parse, NEW pid=%d, PSIZE: %u, type=%X, start=%p, len=%d\n",
3093 es->pid, es->payload_size, es->type, es->start, es->size);
3095 demuxer->filepos = stream_tell(demuxer->stream) - es->size;
3097 if(es->size < 0 || es->size > buf_size) {
3098 mp_msg(MSGT_DEMUX, MSGL_ERR, "Broken ES packet size\n");
3099 es->size = 0;
3101 memmove(p, es->start, es->size);
3102 *dp_offset += es->size;
3103 (*dp)->flags = 0;
3104 (*dp)->pos = stream_tell(demuxer->stream);
3105 (*dp)->pts = es->pts;
3107 if(retv > 0)
3108 return retv;
3109 else
3110 continue;
3113 else
3115 uint16_t sz;
3117 es->pid = tss->pid;
3118 es->type = tss->type;
3119 es->subtype = tss->subtype;
3120 es->pts = tss->pts = tss->last_pts;
3121 es->start = &packet[base];
3124 if(tss->payload_size > 0)
3126 sz = FFMIN(tss->payload_size, buf_size);
3127 tss->payload_size -= sz;
3128 es->size = sz;
3130 else
3132 if(is_video)
3134 sz = es->size = buf_size;
3136 else
3138 continue;
3143 if(! probe)
3145 *dp_offset += sz;
3147 if(*dp_offset >= MAX_PACK_BYTES)
3149 (*dp)->pts = tss->last_pts;
3150 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
3151 return 1;
3154 continue;
3156 else
3158 memcpy(es->start, p, sz);
3160 if(es->size)
3161 return es->size;
3162 else
3163 continue;
3168 return 0;
3172 void skip_audio_frame(sh_audio_t *sh_audio);
3174 static void reset_fifos(demuxer_t *demuxer, int a, int v, int s)
3176 ts_priv_t* priv = demuxer->priv;
3177 if(a)
3179 if(priv->fifo[0].pack != NULL)
3181 free_demux_packet(priv->fifo[0].pack);
3182 priv->fifo[0].pack = NULL;
3184 priv->fifo[0].offset = 0;
3187 if(v)
3189 if(priv->fifo[1].pack != NULL)
3191 free_demux_packet(priv->fifo[1].pack);
3192 priv->fifo[1].pack = NULL;
3194 priv->fifo[1].offset = 0;
3197 if(s)
3199 if(priv->fifo[2].pack != NULL)
3201 free_demux_packet(priv->fifo[2].pack);
3202 priv->fifo[2].pack = NULL;
3204 priv->fifo[2].offset = 0;
3206 demuxer->reference_clock = MP_NOPTS_VALUE;
3210 static void demux_seek_ts(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
3212 demux_stream_t *d_audio=demuxer->audio;
3213 demux_stream_t *d_video=demuxer->video;
3214 sh_audio_t *sh_audio=d_audio->sh;
3215 sh_video_t *sh_video=d_video->sh;
3216 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
3217 int i, video_stats;
3218 off_t newpos;
3220 //================= seek in MPEG-TS ==========================
3222 ts_dump_streams(demuxer->priv);
3223 reset_fifos(demuxer, sh_audio != NULL, sh_video != NULL, demuxer->sub->id > 0);
3225 demux_flush(demuxer);
3229 video_stats = (sh_video != NULL);
3230 if(video_stats)
3232 mp_msg(MSGT_DEMUX, MSGL_V, "IBPS: %d, vb: %d\r\n", sh_video->i_bps, priv->vbitrate);
3233 if(priv->vbitrate)
3234 video_stats = priv->vbitrate;
3235 else
3236 video_stats = sh_video->i_bps;
3239 newpos = (flags & SEEK_ABSOLUTE) ? demuxer->movi_start : demuxer->filepos;
3240 if(flags & SEEK_FACTOR) // float seek 0..1
3241 newpos+=(demuxer->movi_end-demuxer->movi_start)*rel_seek_secs;
3242 else
3244 // time seek (secs)
3245 if(! video_stats) // unspecified or VBR
3246 newpos += 2324*75*rel_seek_secs; // 174.3 kbyte/sec
3247 else
3248 newpos += video_stats*rel_seek_secs;
3252 if(newpos < demuxer->movi_start)
3253 newpos = demuxer->movi_start; //begininng of stream
3255 stream_seek(demuxer->stream, newpos);
3256 for(i = 0; i < 8192; i++)
3257 if(priv->ts.pids[i] != NULL)
3258 priv->ts.pids[i]->is_synced = 0;
3260 videobuf_code_len = 0;
3262 if(sh_video != NULL)
3263 ds_fill_buffer(d_video);
3265 if(sh_audio != NULL)
3267 ds_fill_buffer(d_audio);
3270 while(sh_video != NULL)
3272 if(sh_audio && !d_audio->eof && d_video->pts && d_audio->pts)
3274 double a_pts=d_audio->pts;
3275 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(double)sh_audio->i_bps;
3276 if(d_video->pts > a_pts)
3278 skip_audio_frame(sh_audio); // sync audio
3279 continue;
3284 i = sync_video_packet(d_video);
3285 if((sh_video->format == VIDEO_MPEG1) || (sh_video->format == VIDEO_MPEG2))
3287 if(i==0x1B3 || i==0x1B8) break; // found it!
3289 else if((sh_video->format == VIDEO_MPEG4) && (i==0x1B6))
3290 break;
3291 else if(sh_video->format == VIDEO_VC1 && (i==0x10E || i==0x10F))
3292 break;
3293 else //H264
3295 if((i & ~0x60) == 0x105 || (i & ~0x60) == 0x107) break;
3298 if(!i || !skip_video_packet(d_video)) break; // EOF?
3303 static int demux_ts_fill_buffer(demuxer_t * demuxer, demux_stream_t *ds)
3305 ES_stream_t es;
3306 ts_priv_t *priv = (ts_priv_t *)demuxer->priv;
3308 return -ts_parse(demuxer, &es, priv->packet, 0);
3312 static int ts_check_file_dmx(demuxer_t *demuxer)
3314 return ts_check_file(demuxer) ? DEMUXER_TYPE_MPEG_TS : 0;
3317 static int is_usable_program(ts_priv_t *priv, pmt_t *pmt)
3319 int j;
3321 for(j = 0; j < pmt->es_cnt; j++)
3323 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3324 continue;
3326 priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO ||
3327 priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO
3329 return 1;
3332 return 0;
3335 static int demux_ts_control(demuxer_t *demuxer, int cmd, void *arg)
3337 ts_priv_t* priv = (ts_priv_t *)demuxer->priv;
3339 switch(cmd)
3341 case DEMUXER_CTRL_SWITCH_AUDIO:
3342 case DEMUXER_CTRL_SWITCH_VIDEO:
3344 void *sh = NULL;
3345 int i, n;
3346 int reftype, areset = 0, vreset = 0;
3347 demux_stream_t *ds;
3349 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
3351 reftype = TYPE_VIDEO;
3352 ds = demuxer->video;
3353 vreset = 1;
3355 else
3357 reftype = TYPE_AUDIO;
3358 ds = demuxer->audio;
3359 areset = 1;
3361 n = *((int*)arg);
3362 if(n == -2)
3364 reset_fifos(demuxer, areset, vreset, 0);
3365 ds->id = -2;
3366 ds->sh = NULL;
3367 ds_free_packs(ds);
3368 *((int*)arg) = ds->id;
3369 return DEMUXER_CTRL_OK;
3372 if(n < 0)
3374 for(i = 0; i < 8192; i++)
3376 if(priv->ts.streams[i].id == ds->id && priv->ts.streams[i].type == reftype)
3377 break;
3380 while(!sh)
3382 i = (i+1) % 8192;
3383 if(priv->ts.streams[i].type == reftype)
3385 if(priv->ts.streams[i].id == ds->id) //we made a complete loop
3386 break;
3387 sh = priv->ts.streams[i].sh;
3391 else //audio track <n>
3393 if (n >= 8192 || priv->ts.streams[n].type != reftype) return DEMUXER_CTRL_NOTIMPL;
3394 i = n;
3395 sh = priv->ts.streams[i].sh;
3398 if(sh)
3400 if(ds->id != priv->ts.streams[i].id)
3401 reset_fifos(demuxer, areset, vreset, 0);
3402 ds->id = priv->ts.streams[i].id;
3403 ds->sh = sh;
3404 ds_free_packs(ds);
3405 mp_msg(MSGT_DEMUX, MSGL_V, "\r\ndemux_ts, switched to audio pid %d, id: %d, sh: %p\r\n", i, ds->id, sh);
3408 *((int*)arg) = ds->id;
3409 return DEMUXER_CTRL_OK;
3412 case DEMUXER_CTRL_IDENTIFY_PROGRAM: //returns in prog->{aid,vid} the new ids that comprise a program
3414 int i, j, cnt=0;
3415 int vid_done=0, aid_done=0;
3416 pmt_t *pmt = NULL;
3417 demux_program_t *prog = arg;
3419 if(priv->pmt_cnt < 2)
3420 return DEMUXER_CTRL_NOTIMPL;
3422 if(prog->progid == -1)
3424 int cur_pmt_idx = 0;
3426 for(i = 0; i < priv->pmt_cnt; i++)
3427 if(priv->pmt[i].progid == priv->prog)
3429 cur_pmt_idx = i;
3430 break;
3433 i = (cur_pmt_idx + 1) % priv->pmt_cnt;
3434 while(i != cur_pmt_idx)
3436 pmt = &priv->pmt[i];
3437 cnt = is_usable_program(priv, pmt);
3438 if(cnt)
3439 break;
3440 i = (i + 1) % priv->pmt_cnt;
3443 else
3445 for(i = 0; i < priv->pmt_cnt; i++)
3446 if(priv->pmt[i].progid == prog->progid)
3448 pmt = &priv->pmt[i]; //required program
3449 cnt = is_usable_program(priv, pmt);
3453 if(!cnt)
3454 return DEMUXER_CTRL_NOTIMPL;
3456 //finally some food
3457 prog->aid = prog->vid = -2; //no audio and no video by default
3458 for(j = 0; j < pmt->es_cnt; j++)
3460 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3461 continue;
3463 if(!vid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO)
3465 vid_done = 1;
3466 prog->vid = pmt->es[j].pid;
3468 else if(!aid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO)
3470 aid_done = 1;
3471 prog->aid = pmt->es[j].pid;
3475 priv->prog = prog->progid = pmt->progid;
3476 return DEMUXER_CTRL_OK;
3479 default:
3480 return DEMUXER_CTRL_NOTIMPL;
3485 const demuxer_desc_t demuxer_desc_mpeg_ts = {
3486 "MPEG-TS demuxer",
3487 "mpegts",
3488 "TS",
3489 "Nico Sabbi",
3491 DEMUXER_TYPE_MPEG_TS,
3492 0, // unsafe autodetect
3493 ts_check_file_dmx,
3494 demux_ts_fill_buffer,
3495 demux_open_ts,
3496 demux_close_ts,
3497 demux_seek_ts,
3498 demux_ts_control