options: more mplayer.c options moved to option struct
[mplayer/greg.git] / libmpdemux / demux_ts.c
blob30c063695d6268054af4a0f2c414f5b9ff7c774b
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_DVD:
412 sh->type = 'v'; break;
413 case SPU_PGS:
414 sh->type = 'p'; break;
416 priv->ts.streams[es->pid].id = priv->last_sid;
417 priv->ts.streams[es->pid].sh = sh;
418 priv->ts.streams[es->pid].type = TYPE_SUB;
419 priv->last_sid++;
424 static int ts_check_file(demuxer_t * demuxer)
426 const int buf_size = (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS);
427 unsigned char buf[TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS], done = 0, *ptr;
428 uint32_t _read, i, count = 0, is_ts;
429 int cc[NB_PID_MAX], last_cc[NB_PID_MAX], pid, cc_ok, c, good, bad;
430 uint8_t size = 0;
431 off_t pos = 0;
432 off_t init_pos;
434 mp_msg(MSGT_DEMUX, MSGL_V, "Checking for MPEG-TS...\n");
436 init_pos = stream_tell(demuxer->stream);
437 is_ts = 0;
438 while(! done)
440 i = 1;
441 c = 0;
443 while(((c=stream_read_char(demuxer->stream)) != 0x47)
444 && (c >= 0)
445 && (i < MAX_CHECK_SIZE)
446 && ! demuxer->stream->eof
447 ) i++;
450 if(c != 0x47)
452 mp_msg(MSGT_DEMUX, MSGL_V, "THIS DOESN'T LOOK LIKE AN MPEG-TS FILE!\n");
453 is_ts = 0;
454 done = 1;
455 continue;
458 pos = stream_tell(demuxer->stream) - 1;
459 buf[0] = c;
460 _read = stream_read(demuxer->stream, &buf[1], buf_size-1);
462 if(_read < buf_size-1)
464 mp_msg(MSGT_DEMUX, MSGL_V, "COULDN'T READ ENOUGH DATA, EXITING TS_CHECK\n");
465 stream_reset(demuxer->stream);
466 return 0;
469 size = get_packet_size(buf, buf_size);
470 if(size)
472 done = 1;
473 is_ts = 1;
476 if(pos - init_pos >= MAX_CHECK_SIZE)
478 done = 1;
479 is_ts = 0;
483 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);
484 stream_seek(demuxer->stream, pos);
486 if(! is_ts)
487 return 0;
489 //LET'S CHECK continuity counters
490 good = bad = 0;
491 for(count = 0; count < NB_PID_MAX; count++)
493 cc[count] = last_cc[count] = -1;
496 for(count = 0; count < NUM_CONSECUTIVE_TS_PACKETS; count++)
498 ptr = &(buf[size * count]);
499 pid = ((ptr[1] & 0x1f) << 8) | ptr[2];
500 mp_msg(MSGT_DEMUX, MSGL_DBG2, "BUF: %02x %02x %02x %02x, PID %d, SIZE: %d \n",
501 ptr[0], ptr[1], ptr[2], ptr[3], pid, size);
503 if((pid == 8191) || (pid < 16))
504 continue;
506 cc[pid] = (ptr[3] & 0xf);
507 cc_ok = (last_cc[pid] < 0) || ((((last_cc[pid] + 1) & 0x0f) == cc[pid]));
508 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PID %d, COMPARE CC %d AND LAST_CC %d\n", pid, cc[pid], last_cc[pid]);
509 if(! cc_ok)
510 //return 0;
511 bad++;
512 else
513 good++;
515 last_cc[pid] = cc[pid];
518 mp_msg(MSGT_DEMUX, MSGL_V, "GOOD CC: %d, BAD CC: %d\n", good, bad);
520 if(good >= bad)
521 return size;
522 else
523 return 0;
527 static inline int32_t progid_idx_in_pmt(ts_priv_t *priv, uint16_t progid)
529 int x;
531 if(priv->pmt == NULL)
532 return -1;
534 for(x = 0; x < priv->pmt_cnt; x++)
536 if(priv->pmt[x].progid == progid)
537 return x;
540 return -1;
544 static inline int32_t progid_for_pid(ts_priv_t *priv, int pid, int32_t req) //finds the first program listing a pid
546 int i, j;
547 pmt_t *pmt;
550 if(priv->pmt == NULL)
551 return -1;
554 for(i=0; i < priv->pmt_cnt; i++)
556 pmt = &(priv->pmt[i]);
558 if(pmt->es == NULL)
559 return -1;
561 for(j = 0; j < pmt->es_cnt; j++)
563 if(pmt->es[j].pid == pid)
565 if((req == 0) || (req == pmt->progid))
566 return pmt->progid;
571 return -1;
574 static inline int32_t prog_pcr_pid(ts_priv_t *priv, int progid)
576 int i;
578 if(priv->pmt == NULL)
579 return -1;
580 for(i=0; i < priv->pmt_cnt; i++)
582 if(priv->pmt[i].progid == progid)
583 return priv->pmt[i].PCR_PID;
585 return -1;
589 static inline int pid_match_lang(ts_priv_t *priv, uint16_t pid, char *lang)
591 uint16_t i, j;
592 pmt_t *pmt;
594 if(priv->pmt == NULL)
595 return -1;
597 for(i=0; i < priv->pmt_cnt; i++)
599 pmt = &(priv->pmt[i]);
601 if(pmt->es == NULL)
602 return -1;
604 for(j = 0; j < pmt->es_cnt; j++)
606 if(pmt->es[j].pid != pid)
607 continue;
609 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);
610 if(strncmp(pmt->es[j].lang, lang, 3) == 0)
612 return 1;
618 return -1;
621 typedef struct {
622 int32_t atype, vtype, stype; //types
623 int32_t apid, vpid, spid; //stream ids
624 char alang[4]; //languages
625 uint16_t prog;
626 off_t probe;
627 } tsdemux_init_t;
629 //second stage: returns the count of A52 syncwords found
630 static int a52_check(char *buf, int len)
632 int cnt, frame_length = 0, ok, srate;
634 cnt = ok = 0;
635 if(len < 8)
636 return 0;
638 while(cnt < len - 7)
640 if(buf[cnt] == 0x0B && buf[cnt+1] == 0x77)
642 frame_length = mp_a52_framesize(&buf[cnt], &srate);
643 if(frame_length>=7 && frame_length<=3840)
645 cnt += frame_length;
646 ok++;
648 else
649 cnt++;
651 else
652 cnt++;
655 mp_msg(MSGT_DEMUXER, MSGL_V, "A52_CHECK(%d input bytes), found %d frame syncwords of %d bytes length\n", len, ok, frame_length);
656 return ok;
660 static off_t ts_detect_streams(demuxer_t *demuxer, tsdemux_init_t *param)
662 int video_found = 0, audio_found = 0, sub_found = 0, i, num_packets = 0, req_apid, req_vpid, req_spid;
663 int is_audio, is_video, is_sub, has_tables;
664 int32_t p, chosen_pid = 0;
665 off_t pos=0, ret = 0, init_pos, end_pos;
666 ES_stream_t es;
667 unsigned char tmp[TS_FEC_PACKET_SIZE];
668 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
669 struct {
670 char *buf;
671 int pos;
672 } pes_priv1[8192], *pptr;
673 char *tmpbuf;
675 priv->last_pid = 8192; //invalid pid
677 req_apid = param->apid;
678 req_vpid = param->vpid;
679 req_spid = param->spid;
681 has_tables = 0;
682 memset(pes_priv1, 0, sizeof(pes_priv1));
683 init_pos = stream_tell(demuxer->stream);
684 mp_msg(MSGT_DEMUXER, MSGL_V, "PROBING UP TO %"PRIu64", PROG: %d\n", (uint64_t) param->probe, param->prog);
685 end_pos = init_pos + (param->probe ? param->probe : TS_MAX_PROBE_SIZE);
686 while(1)
688 pos = stream_tell(demuxer->stream);
689 if(pos > end_pos || demuxer->stream->eof)
690 break;
692 if(ts_parse(demuxer, &es, tmp, 1))
694 //Non PES-aligned A52 audio may escape detection if PMT is not present;
695 //in this case we try to find at least 3 A52 syncwords
696 if((es.type == PES_PRIVATE1) && (! audio_found) && req_apid > -2)
698 pptr = &pes_priv1[es.pid];
699 if(pptr->pos < 64*1024)
701 tmpbuf = realloc(pptr->buf, pptr->pos + es.size);
702 if(tmpbuf != NULL)
704 pptr->buf = tmpbuf;
705 memcpy(&(pptr->buf[ pptr->pos ]), es.start, es.size);
706 pptr->pos += es.size;
707 if(a52_check(pptr->buf, pptr->pos) > 2)
709 param->atype = AUDIO_A52;
710 param->apid = es.pid;
711 es.type = AUDIO_A52;
717 is_audio = IS_AUDIO(es.type) || ((es.type==SL_PES_STREAM) && IS_AUDIO(es.subtype));
718 is_video = IS_VIDEO(es.type) || ((es.type==SL_PES_STREAM) && IS_VIDEO(es.subtype));
719 is_sub = IS_SUB(es.type);
722 if((! is_audio) && (! is_video) && (! is_sub))
723 continue;
724 if(is_audio && req_apid==-2)
725 continue;
727 if(is_video)
729 chosen_pid = (req_vpid == es.pid);
730 if((! chosen_pid) && (req_vpid > 0))
731 continue;
733 else if(is_audio)
735 if(req_apid > 0)
737 chosen_pid = (req_apid == es.pid);
738 if(! chosen_pid)
739 continue;
741 else if(param->alang[0] > 0 && es.lang[0] > 0)
743 if(pid_match_lang(priv, es.pid, param->alang) == -1)
744 continue;
746 chosen_pid = 1;
747 param->apid = req_apid = es.pid;
750 else if(is_sub)
752 chosen_pid = (req_spid == es.pid);
753 if((! chosen_pid) && (req_spid > 0))
754 continue;
757 if(req_apid < 0 && (param->alang[0] == 0) && req_vpid < 0 && req_spid < 0)
758 chosen_pid = 1;
760 if((ret == 0) && chosen_pid)
762 ret = stream_tell(demuxer->stream);
765 p = progid_for_pid(priv, es.pid, param->prog);
766 if(p != -1)
768 has_tables++;
769 if(!param->prog && chosen_pid)
770 param->prog = p;
773 if((param->prog > 0) && (param->prog != p))
775 if(audio_found)
777 if(is_video && (req_vpid == es.pid))
779 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
780 param->vpid = es.pid;
781 video_found = 1;
782 break;
786 if(video_found)
788 if(is_audio && (req_apid == es.pid))
790 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
791 param->apid = es.pid;
792 audio_found = 1;
793 break;
798 continue;
802 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "TYPE: %x, PID: %d, PROG FOUND: %d\n", es.type, es.pid, param->prog);
805 if(is_video)
807 if((req_vpid == -1) || (req_vpid == es.pid))
809 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
810 param->vpid = es.pid;
811 video_found = 1;
816 if(((req_vpid == -2) || (num_packets >= NUM_CONSECUTIVE_AUDIO_PACKETS)) && audio_found && !param->probe)
818 //novideo or we have at least 348 audio packets (64 KB) without video (TS with audio only)
819 param->vtype = 0;
820 break;
823 if(is_sub)
825 if((req_spid == -1) || (req_spid == es.pid))
827 param->stype = es.type;
828 param->spid = es.pid;
829 sub_found = 1;
833 if(is_audio)
835 if((req_apid == -1) || (req_apid == es.pid))
837 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
838 param->apid = es.pid;
839 audio_found = 1;
843 if(audio_found && (param->apid == es.pid) && (! video_found))
844 num_packets++;
846 if((has_tables==0) && (video_found && audio_found) && (pos >= 1000000))
847 break;
851 for(i=0; i<8192; i++)
853 if(pes_priv1[i].buf != NULL)
855 free(pes_priv1[i].buf);
856 pes_priv1[i].buf = NULL;
857 pes_priv1[i].pos = 0;
861 if(video_found)
863 if(param->vtype == VIDEO_MPEG1)
864 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG1(pid=%d) ", param->vpid);
865 else if(param->vtype == VIDEO_MPEG2)
866 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG2(pid=%d) ", param->vpid);
867 else if(param->vtype == VIDEO_MPEG4)
868 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG4(pid=%d) ", param->vpid);
869 else if(param->vtype == VIDEO_H264)
870 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO H264(pid=%d) ", param->vpid);
871 else if(param->vtype == VIDEO_VC1)
872 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO VC1(pid=%d) ", param->vpid);
873 else if(param->vtype == VIDEO_AVC)
874 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO AVC(NAL-H264, pid=%d) ", param->vpid);
876 else
878 param->vtype = UNKNOWN;
879 //WE DIDN'T MATCH ANY VIDEO STREAM
880 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO VIDEO! ");
883 if(param->atype == AUDIO_MP2)
884 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO MPA(pid=%d)", param->apid);
885 else if(param->atype == AUDIO_A52)
886 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO A52(pid=%d)", param->apid);
887 else if(param->atype == AUDIO_DTS)
888 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO DTS(pid=%d)", param->apid);
889 else if(param->atype == AUDIO_LPCM_BE)
890 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO LPCM(pid=%d)", param->apid);
891 else if(param->atype == AUDIO_AAC)
892 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO AAC(pid=%d)", param->apid);
893 else if(param->atype == AUDIO_AAC_LATM)
894 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO AAC LATM(pid=%d)", param->apid);
895 else if(param->atype == AUDIO_TRUEHD)
896 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO TRUEHD(pid=%d)", param->apid);
897 else
899 audio_found = 0;
900 param->atype = UNKNOWN;
901 //WE DIDN'T MATCH ANY AUDIO STREAM, SO WE FORCE THE DEMUXER TO IGNORE AUDIO
902 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO AUDIO! ");
905 if(IS_SUB(param->stype))
906 mp_msg(MSGT_DEMUXER, MSGL_INFO, " SUB %s(pid=%d) ", (param->stype==SPU_DVD ? "DVD" : param->stype==SPU_DVB ? "DVB" : "Teletext"), param->spid);
907 else
909 param->stype = UNKNOWN;
910 mp_msg(MSGT_DEMUXER, MSGL_INFO, " NO SUBS (yet)! ");
913 if(video_found || audio_found)
915 if(!param->prog)
917 p = progid_for_pid(priv, video_found ? param->vpid : param->apid, 0);
918 if(p != -1)
919 param->prog = p;
922 if(demuxer->stream->eof && (ret == 0))
923 ret = init_pos;
924 mp_msg(MSGT_DEMUXER, MSGL_INFO, " PROGRAM N. %d\n", param->prog);
926 else
927 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\n");
930 for(i=0; i<8192; i++)
932 if(priv->ts.pids[i] != NULL)
934 priv->ts.pids[i]->payload_size = 0;
935 priv->ts.pids[i]->pts = priv->ts.pids[i]->last_pts = 0;
936 priv->ts.pids[i]->last_cc = -1;
937 priv->ts.pids[i]->is_synced = 0;
941 return ret;
944 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h)
946 int sps, sps_len;
947 unsigned char *ptr;
948 mp_mpeg_header_t picture;
949 if(len < 6)
950 return 0;
951 sps = buf[5] & 0x1f;
952 if(!sps)
953 return 0;
954 sps_len = (buf[6] << 8) | buf[7];
955 if(!sps_len || (sps_len > len - 8))
956 return 0;
957 ptr = &(buf[8]);
958 picture.display_picture_width = picture.display_picture_height = 0;
959 h264_parse_sps(&picture, ptr, len - 8);
960 if(!picture.display_picture_width || !picture.display_picture_height)
961 return 0;
962 *w = picture.display_picture_width;
963 *h = picture.display_picture_height;
964 return 1;
967 static demuxer_t *demux_open_ts(demuxer_t * demuxer)
969 int i;
970 uint8_t packet_size;
971 sh_video_t *sh_video;
972 sh_audio_t *sh_audio;
973 off_t start_pos;
974 tsdemux_init_t params;
975 ts_priv_t * priv = demuxer->priv;
977 mp_msg(MSGT_DEMUX, MSGL_V, "DEMUX OPEN, AUDIO_ID: %d, VIDEO_ID: %d, SUBTITLE_ID: %d,\n",
978 demuxer->audio->id, demuxer->video->id, demuxer->sub->id);
981 demuxer->type= DEMUXER_TYPE_MPEG_TS;
984 stream_reset(demuxer->stream);
986 packet_size = ts_check_file(demuxer);
987 if(!packet_size)
988 return NULL;
990 priv = calloc(1, sizeof(ts_priv_t));
991 if(priv == NULL)
993 mp_msg(MSGT_DEMUX, MSGL_FATAL, "DEMUX_OPEN_TS, couldn't allocate enough memory for ts->priv, exit\n");
994 return NULL;
997 for(i=0; i < 8192; i++)
999 priv->ts.pids[i] = NULL;
1000 priv->ts.streams[i].id = -3;
1002 priv->pat.progs = NULL;
1003 priv->pat.progs_cnt = 0;
1004 priv->pat.section.buffer = NULL;
1005 priv->pat.section.buffer_len = 0;
1007 priv->pmt = NULL;
1008 priv->pmt_cnt = 0;
1010 priv->keep_broken = ts_keep_broken;
1011 priv->ts.packet_size = packet_size;
1014 demuxer->priv = priv;
1015 if(demuxer->stream->type != STREAMTYPE_FILE)
1016 demuxer->seekable = 1;
1017 else
1018 demuxer->seekable = 1;
1021 params.atype = params.vtype = params.stype = UNKNOWN;
1022 params.apid = demuxer->audio->id;
1023 params.vpid = demuxer->video->id;
1024 params.spid = demuxer->sub->id;
1025 params.prog = ts_prog;
1026 params.probe = ts_probe;
1028 if(demuxer->opts->audio_lang != NULL)
1030 strncpy(params.alang, demuxer->opts->audio_lang, 3);
1031 params.alang[3] = 0;
1033 else
1034 memset(params.alang, 0, 4);
1036 start_pos = ts_detect_streams(demuxer, &params);
1038 demuxer->sub->id = params.spid;
1039 priv->prog = params.prog;
1041 if(params.vtype != UNKNOWN)
1043 ts_add_stream(demuxer, priv->ts.pids[params.vpid]);
1044 sh_video = priv->ts.streams[params.vpid].sh;
1045 demuxer->video->id = priv->ts.streams[params.vpid].id;
1046 sh_video->ds = demuxer->video;
1047 sh_video->format = params.vtype;
1048 demuxer->video->sh = sh_video;
1051 if(params.atype != UNKNOWN)
1053 ES_stream_t *es = priv->ts.pids[params.apid];
1055 if(!IS_AUDIO(es->type) && !IS_AUDIO(es->subtype) && IS_AUDIO(params.atype)) es->subtype = params.atype;
1056 ts_add_stream(demuxer, priv->ts.pids[params.apid]);
1057 sh_audio = priv->ts.streams[params.apid].sh;
1058 demuxer->audio->id = priv->ts.streams[params.apid].id;
1059 sh_audio->ds = demuxer->audio;
1060 sh_audio->format = params.atype;
1061 demuxer->audio->sh = sh_audio;
1065 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);
1068 start_pos = (start_pos <= priv->ts.packet_size ? 0 : start_pos - priv->ts.packet_size);
1069 demuxer->movi_start = start_pos;
1070 demuxer->reference_clock = MP_NOPTS_VALUE;
1071 stream_reset(demuxer->stream);
1072 stream_seek(demuxer->stream, start_pos); //IF IT'S FROM A PIPE IT WILL FAIL, BUT WHO CARES?
1075 priv->last_pid = 8192; //invalid pid
1077 for(i = 0; i < 3; i++)
1079 priv->fifo[i].pack = NULL;
1080 priv->fifo[i].offset = 0;
1082 priv->fifo[0].ds = demuxer->audio;
1083 priv->fifo[1].ds = demuxer->video;
1084 priv->fifo[2].ds = demuxer->sub;
1086 priv->fifo[0].buffer_size = 1536;
1087 priv->fifo[1].buffer_size = 32767;
1088 priv->fifo[2].buffer_size = 32767;
1090 priv->pat.section.buffer_len = 0;
1091 for(i = 0; i < priv->pmt_cnt; i++)
1092 priv->pmt[i].section.buffer_len = 0;
1094 demuxer->filepos = stream_tell(demuxer->stream);
1095 return demuxer;
1098 static void demux_close_ts(demuxer_t * demuxer)
1100 uint16_t i;
1101 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
1103 if(priv)
1105 if(priv->pat.section.buffer)
1106 free(priv->pat.section.buffer);
1107 if(priv->pat.progs)
1108 free(priv->pat.progs);
1110 if(priv->pmt)
1112 for(i = 0; i < priv->pmt_cnt; i++)
1114 if(priv->pmt[i].section.buffer)
1115 free(priv->pmt[i].section.buffer);
1116 if(priv->pmt[i].es)
1117 free(priv->pmt[i].es);
1119 free(priv->pmt);
1121 free(priv);
1123 demuxer->priv=NULL;
1127 #define getbits mp_getbits
1129 static int mp4_parse_sl_packet(pmt_t *pmt, uint8_t *buf, uint16_t packet_len, int pid, ES_stream_t *pes_es)
1131 int i, n, m, mp4_es_id = -1;
1132 uint64_t v = 0;
1133 uint32_t pl_size = 0;
1134 int deg_flag = 0;
1135 mp4_es_descr_t *es = NULL;
1136 mp4_sl_config_t *sl = NULL;
1137 uint8_t au_start = 0, au_end = 0, rap_flag = 0, ocr_flag = 0, padding = 0, padding_bits = 0, idle = 0;
1139 pes_es->is_synced = 0;
1140 mp_msg(MSGT_DEMUXER,MSGL_V, "mp4_parse_sl_packet, pid: %d, pmt: %pm, packet_len: %d\n", pid, pmt, packet_len);
1141 if(! pmt || !packet_len)
1142 return 0;
1144 for(i = 0; i < pmt->es_cnt; i++)
1146 if(pmt->es[i].pid == pid)
1147 mp4_es_id = pmt->es[i].mp4_es_id;
1149 if(mp4_es_id < 0)
1150 return -1;
1152 for(i = 0; i < pmt->mp4es_cnt; i++)
1154 if(pmt->mp4es[i].id == mp4_es_id)
1155 es = &(pmt->mp4es[i]);
1157 if(! es)
1158 return -1;
1160 pes_es->subtype = es->decoder.object_type;
1162 sl = &(es->sl);
1163 if(!sl)
1164 return -1;
1166 //now es is the complete es_descriptor of out mp4 ES stream
1167 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "ID: %d, FLAGS: 0x%x, subtype: %x\n", es->id, sl->flags, pes_es->subtype);
1169 n = 0;
1170 if(sl->au_start)
1171 pes_es->sl.au_start = au_start = getbits(buf, n++, 1);
1172 else
1173 pes_es->sl.au_start = (pes_es->sl.last_au_end ? 1 : 0);
1174 if(sl->au_end)
1175 pes_es->sl.au_end = au_end = getbits(buf, n++, 1);
1177 if(!sl->au_start && !sl->au_end)
1179 pes_es->sl.au_start = pes_es->sl.au_end = au_start = au_end = 1;
1181 pes_es->sl.last_au_end = pes_es->sl.au_end;
1184 if(sl->ocr_len > 0)
1185 ocr_flag = getbits(buf, n++, 1);
1186 if(sl->idle)
1187 idle = getbits(buf, n++, 1);
1188 if(sl->padding)
1189 padding = getbits(buf, n++, 1);
1190 if(padding)
1192 padding_bits = getbits(buf, n, 3);
1193 n += 3;
1196 if(idle || (padding && !padding_bits))
1198 pes_es->payload_size = 0;
1199 return -1;
1202 //(! idle && (!padding || padding_bits != 0)) is true
1203 n += sl->packet_seqnum_len;
1204 if(sl->degr_len)
1205 deg_flag = getbits(buf, n++, 1);
1206 if(deg_flag)
1207 n += sl->degr_len;
1209 if(ocr_flag)
1211 n += sl->ocr_len;
1212 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "OCR: %d bits\n", sl->ocr_len);
1215 if(packet_len * 8 <= n)
1216 return -1;
1218 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "\nAU_START: %d, AU_END: %d\n", au_start, au_end);
1219 if(au_start)
1221 int dts_flag = 0, cts_flag = 0, ib_flag = 0;
1223 if(sl->random_accesspoint)
1224 rap_flag = getbits(buf, n++, 1);
1226 //check commented because it seems it's rarely used, and we need this flag set in case of au_start
1227 //the decoder will eventually discard the payload if it can't decode it
1228 //if(rap_flag || sl->random_accesspoint_only)
1229 pes_es->is_synced = 1;
1231 n += sl->au_seqnum_len;
1232 if(packet_len * 8 <= n+8)
1233 return -1;
1234 if(sl->use_ts)
1236 dts_flag = getbits(buf, n++, 1);
1237 cts_flag = getbits(buf, n++, 1);
1239 if(sl->instant_bitrate_len)
1240 ib_flag = getbits(buf, n++, 1);
1241 if(packet_len * 8 <= n+8)
1242 return -1;
1243 if(dts_flag && (sl->ts_len > 0))
1245 n += sl->ts_len;
1246 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "DTS: %d bits\n", sl->ts_len);
1248 if(packet_len * 8 <= n+8)
1249 return -1;
1250 if(cts_flag && (sl->ts_len > 0))
1252 int i = 0, m;
1254 while(i < sl->ts_len)
1256 m = FFMIN(8, sl->ts_len - i);
1257 v |= getbits(buf, n, m);
1258 if(sl->ts_len - i > 8)
1259 v <<= 8;
1260 i += m;
1261 n += m;
1262 if(packet_len * 8 <= n+8)
1263 return -1;
1266 pes_es->pts = (double) v / (double) sl->ts_resolution;
1267 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "CTS: %d bits, value: %"PRIu64"/%d = %.3f\n", sl->ts_len, v, sl->ts_resolution, pes_es->pts);
1271 i = 0;
1272 pl_size = 0;
1273 while(i < sl->au_len)
1275 m = FFMIN(8, sl->au_len - i);
1276 pl_size |= getbits(buf, n, m);
1277 if(sl->au_len - i > 8)
1278 pl_size <<= 8;
1279 i += m;
1280 n += m;
1281 if(packet_len * 8 <= n+8)
1282 return -1;
1284 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "AU_LEN: %u (%d bits)\n", pl_size, sl->au_len);
1285 if(ib_flag)
1286 n += sl->instant_bitrate_len;
1289 m = (n+7)/8;
1290 if(0 < pl_size && pl_size < pes_es->payload_size)
1291 pes_es->payload_size = pl_size;
1293 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",
1294 n, m, pes_es->payload_size, pl_size, (int) rap_flag, (int) sl->random_accesspoint_only);
1296 return m;
1299 //this function parses the extension fields in the PES header and returns the substream_id, or -1 in case of errors
1300 static int parse_pes_extension_fields(unsigned char *p, int pkt_len)
1302 int skip;
1303 unsigned char flags;
1305 if(!(p[7] & 0x1)) //no extension_field
1306 return -1;
1307 skip = 9;
1308 if(p[7] & 0x80)
1310 skip += 5;
1311 if(p[7] & 0x40)
1312 skip += 5;
1314 if(p[7] & 0x20) //escr_flag
1315 skip += 6;
1316 if(p[7] & 0x10) //es_rate_flag
1317 skip += 3;
1318 if(p[7] & 0x08)//dsm_trick_mode is unsupported, skip
1320 skip = 0;//don't let's parse the extension fields
1322 if(p[7] & 0x04) //additional_copy_info
1323 skip += 1;
1324 if(p[7] & 0x02) //pes_crc_flag
1325 skip += 2;
1326 if(skip >= pkt_len) //too few bytes
1327 return -1;
1328 flags = p[skip];
1329 skip++;
1330 if(flags & 0x80) //pes_private_data_flag
1331 skip += 16;
1332 if(skip >= pkt_len)
1333 return -1;
1334 if(flags & 0x40) //pack_header_field_flag
1336 unsigned char l = p[skip];
1337 skip += l;
1339 if(flags & 0x20) //program_packet_sequence_counter
1340 skip += 2;
1341 if(flags & 0x10) //p_std
1342 skip += 2;
1343 if(skip >= pkt_len)
1344 return -1;
1345 if(flags & 0x01) //finally the long desired pes_extension2
1347 unsigned char l = p[skip]; //ext2 flag+len
1348 skip++;
1349 if((l == 0x81) && (skip < pkt_len))
1351 int ssid = p[skip];
1352 mp_msg(MSGT_IDENTIFY, MSGL_V, "SUBSTREAM_ID=%d (0x%02X)\n", ssid, ssid);
1353 return ssid;
1357 return -1;
1360 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)
1362 unsigned char *p;
1363 uint32_t header_len;
1364 int64_t pts;
1365 uint32_t stream_id;
1366 uint32_t pkt_len, pes_is_aligned;
1368 //Here we are always at the start of a PES packet
1369 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2(%p, %d): \n", buf, (uint32_t) packet_len);
1371 if(packet_len == 0 || packet_len > 184)
1373 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2, BUFFER LEN IS TOO SMALL OR TOO BIG: %d EXIT\n", packet_len);
1374 return 0;
1377 p = buf;
1378 pkt_len = packet_len;
1381 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: HEADER %02x %02x %02x %02x\n", p[0], p[1], p[2], p[3]);
1382 if (p[0] || p[1] || (p[2] != 1))
1384 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: error HEADER %02x %02x %02x (should be 0x000001) \n", p[0], p[1], p[2]);
1385 return 0 ;
1388 packet_len -= 6;
1389 if(packet_len==0)
1391 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: packet too short: %d, exit\n", packet_len);
1392 return 0;
1395 es->payload_size = (p[4] << 8 | p[5]);
1396 pes_is_aligned = (p[6] & 4);
1398 stream_id = p[3];
1401 if (p[7] & 0x80)
1402 { /* pts available */
1403 pts = (int64_t)(p[9] & 0x0E) << 29 ;
1404 pts |= p[10] << 22 ;
1405 pts |= (p[11] & 0xFE) << 14 ;
1406 pts |= p[12] << 7 ;
1407 pts |= (p[13] & 0xFE) >> 1 ;
1409 es->pts = pts / 90000.0;
1411 else
1412 es->pts = 0.0;
1415 header_len = p[8];
1418 if (header_len + 9 > pkt_len) //9 are the bytes read up to the header_length field
1420 mp_msg(MSGT_DEMUX, MSGL_DBG2, "demux_ts: illegal value for PES_header_data_length (0x%02x)\n", header_len);
1421 return 0;
1424 if(stream_id==0xfd)
1426 int ssid = parse_pes_extension_fields(p, pkt_len);
1427 if((audio_substream_id!=-1) && (ssid != audio_substream_id))
1428 return 0;
1429 if(ssid == 0x72 && type_from_pmt != AUDIO_DTS && type_from_pmt != SPU_PGS)
1430 es->type = type_from_pmt = AUDIO_TRUEHD;
1433 p += header_len + 9;
1434 packet_len -= header_len + 3;
1436 if(es->payload_size)
1437 es->payload_size -= header_len + 3;
1440 es->is_synced = 1; //only for SL streams we have to make sure it's really true, see below
1441 if (stream_id == 0xbd)
1443 mp_msg(MSGT_DEMUX, MSGL_DBG3, "pes_parse2: audio buf = %02X %02X %02X %02X %02X %02X %02X %02X, 80: %d\n",
1444 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[0] & 0x80);
1448 * we check the descriptor tag first because some stations
1449 * do not include any of the A52 header info in their audio tracks
1450 * these "raw" streams may begin with a byte that looks like a stream type.
1454 if(type_from_pmt == SPU_PGS)
1456 es->start = p;
1457 es->size = packet_len;
1458 es->type = SPU_PGS;
1459 es->payload_size -= packet_len;
1460 return 1;
1463 (type_from_pmt == AUDIO_A52) || /* A52 - raw */
1464 (packet_len >= 2 && p[0] == 0x0B && p[1] == 0x77) /* A52 - syncword */
1467 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 RAW OR SYNCWORD\n");
1468 es->start = p;
1469 es->size = packet_len;
1470 es->type = AUDIO_A52;
1471 es->payload_size -= packet_len;
1473 return 1;
1475 /* SPU SUBS */
1476 else if(type_from_pmt == SPU_DVB ||
1477 (packet_len >= 1 && (p[0] == 0x20) && pes_is_aligned)) // && p[1] == 0x00))
1479 es->start = p;
1480 es->size = packet_len;
1481 es->type = SPU_DVB;
1482 es->payload_size -= packet_len;
1484 return 1;
1486 else if (pes_is_aligned && packet_len >= 1 && ((p[0] & 0xE0) == 0x20)) //SPU_DVD
1488 //DVD SUBS
1489 es->start = p+1;
1490 es->size = packet_len-1;
1491 es->type = SPU_DVD;
1492 es->payload_size -= packet_len;
1494 return 1;
1496 else if (pes_is_aligned && packet_len >= 4 && (p[0] & 0xF8) == 0x80)
1498 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 WITH HEADER\n");
1499 es->start = p+4;
1500 es->size = packet_len - 4;
1501 es->type = AUDIO_A52;
1502 es->payload_size -= packet_len;
1504 return 1;
1506 else if (pes_is_aligned && packet_len >= 1 && ((p[0]&0xf0) == 0xa0))
1508 int pcm_offset;
1510 for (pcm_offset=0; ++pcm_offset < packet_len-1 ; )
1512 if (p[pcm_offset] == 0x01 && p[pcm_offset+1] == 0x80)
1513 { /* START */
1514 pcm_offset += 2;
1515 break;
1519 es->start = p + pcm_offset;
1520 es->size = packet_len - pcm_offset;
1521 es->type = AUDIO_LPCM_BE;
1522 es->payload_size -= packet_len;
1524 return 1;
1526 else
1528 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PES_PRIVATE1\n");
1529 es->start = p;
1530 es->size = packet_len;
1531 es->type = (type_from_pmt == UNKNOWN ? PES_PRIVATE1 : type_from_pmt);
1532 es->payload_size -= packet_len;
1534 return 1;
1537 else if(((stream_id >= 0xe0) && (stream_id <= 0xef)) || (stream_id == 0xfd && type_from_pmt != UNKNOWN))
1539 es->start = p;
1540 es->size = packet_len;
1541 if(type_from_pmt != UNKNOWN)
1542 es->type = type_from_pmt;
1543 else
1544 es->type = VIDEO_MPEG2;
1545 if(es->payload_size)
1546 es->payload_size -= packet_len;
1548 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: M2V size %d\n", es->size);
1549 return 1;
1551 else if ((stream_id == 0xfa))
1553 int l;
1555 es->is_synced = 0;
1556 if(type_from_pmt != UNKNOWN) //MP4 A/V or SL
1558 es->start = p;
1559 es->size = packet_len;
1560 es->type = type_from_pmt;
1562 if(type_from_pmt == SL_PES_STREAM)
1564 //if(pes_is_aligned)
1566 l = mp4_parse_sl_packet(pmt, p, packet_len, pid, es);
1567 mp_msg(MSGT_DEMUX, MSGL_DBG2, "L=%d, TYPE=%x\n", l, type_from_pmt);
1568 if(l < 0)
1570 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: couldn't parse SL header, passing along full PES payload\n");
1571 l = 0;
1575 es->start += l;
1576 es->size -= l;
1579 if(es->payload_size)
1580 es->payload_size -= packet_len;
1581 return 1;
1584 else if ((stream_id & 0xe0) == 0xc0)
1586 es->start = p;
1587 es->size = packet_len;
1589 if(type_from_pmt != UNKNOWN)
1590 es->type = type_from_pmt;
1591 else
1592 es->type = AUDIO_MP2;
1594 es->payload_size -= packet_len;
1596 return 1;
1598 else if (type_from_pmt != -1) //as a last resort here we trust the PMT, if present
1600 es->start = p;
1601 es->size = packet_len;
1602 es->type = type_from_pmt;
1603 es->payload_size -= packet_len;
1605 return 1;
1607 else
1609 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: unknown packet, id: %x\n", stream_id);
1612 es->is_synced = 0;
1613 return 0;
1619 static int ts_sync(stream_t *stream)
1621 mp_msg(MSGT_DEMUX, MSGL_DBG3, "TS_SYNC \n");
1623 while (!stream->eof)
1624 if (stream_read_char(stream) == 0x47)
1625 return 1;
1627 return 0;
1631 static void ts_dump_streams(ts_priv_t *priv)
1633 int i;
1635 for(i = 0; i < 3; i++)
1637 if((priv->fifo[i].pack != NULL) && (priv->fifo[i].offset != 0))
1639 resize_demux_packet(priv->fifo[i].pack, priv->fifo[i].offset);
1640 ds_add_packet(priv->fifo[i].ds, priv->fifo[i].pack);
1641 priv->fifo[i].offset = 0;
1642 priv->fifo[i].pack = NULL;
1648 static inline int32_t prog_idx_in_pat(ts_priv_t *priv, uint16_t progid)
1650 int x;
1652 if(priv->pat.progs == NULL)
1653 return -1;
1655 for(x = 0; x < priv->pat.progs_cnt; x++)
1657 if(priv->pat.progs[x].id == progid)
1658 return x;
1661 return -1;
1665 static inline int32_t prog_id_in_pat(ts_priv_t *priv, uint16_t pid)
1667 int x;
1669 if(priv->pat.progs == NULL)
1670 return -1;
1672 for(x = 0; x < priv->pat.progs_cnt; x++)
1674 if(priv->pat.progs[x].pmt_pid == pid)
1675 return priv->pat.progs[x].id;
1678 return -1;
1681 static int collect_section(ts_section_t *section, int is_start, unsigned char *buff, int size)
1683 uint8_t *ptr;
1684 uint16_t tlen;
1685 int skip, tid;
1687 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, start: %d, size: %d, collected: %d\n", is_start, size, section->buffer_len);
1688 if(! is_start && !section->buffer_len)
1689 return 0;
1691 if(is_start)
1693 if(! section->buffer)
1695 section->buffer = malloc(4096 + 256);
1696 if(section->buffer == NULL)
1697 return 0;
1699 section->buffer_len = 0;
1702 if(size + section->buffer_len > 4096+256)
1704 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, excessive len: %d + %d\n", section->buffer_len, size);
1705 return 0;
1708 memcpy(&(section->buffer[section->buffer_len]), buff, size);
1709 section->buffer_len += size;
1711 if(section->buffer_len < 3)
1712 return 0;
1714 skip = section->buffer[0];
1715 if(skip + 4 > section->buffer_len)
1716 return 0;
1718 ptr = &(section->buffer[skip + 1]);
1719 tid = ptr[0];
1720 tlen = ((ptr[1] & 0x0f) << 8) | ptr[2];
1721 mp_msg(MSGT_DEMUX, MSGL_V, "SKIP: %d+1, TID: %d, TLEN: %d, COLLECTED: %d\n", skip, tid, tlen, section->buffer_len);
1722 if(section->buffer_len < (skip+1+3+tlen))
1724 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DATA IS NOT ENOUGH, NEXT TIME\n");
1725 return 0;
1728 return skip+1;
1731 static int parse_pat(ts_priv_t * priv, int is_start, unsigned char *buff, int size)
1733 int skip;
1734 unsigned char *ptr;
1735 unsigned char *base;
1736 int entries, i;
1737 uint16_t progid;
1738 struct pat_progs_t *tmp;
1739 ts_section_t *section;
1741 section = &(priv->pat.section);
1742 skip = collect_section(section, is_start, buff, size);
1743 if(! skip)
1744 return 0;
1746 ptr = &(section->buffer[skip]);
1747 //PARSING
1748 priv->pat.table_id = ptr[0];
1749 if(priv->pat.table_id != 0)
1750 return 0;
1751 priv->pat.ssi = (ptr[1] >> 7) & 0x1;
1752 priv->pat.curr_next = ptr[5] & 0x01;
1753 priv->pat.ts_id = (ptr[3] << 8 ) | ptr[4];
1754 priv->pat.version_number = (ptr[5] >> 1) & 0x1F;
1755 priv->pat.section_length = ((ptr[1] & 0x03) << 8 ) | ptr[2];
1756 priv->pat.section_number = ptr[6];
1757 priv->pat.last_section_number = ptr[7];
1759 //check_crc32(0xFFFFFFFFL, ptr, priv->pat.buffer_len - 4, &ptr[priv->pat.buffer_len - 4]);
1760 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);
1762 entries = (int) (priv->pat.section_length - 9) / 4; //entries per section
1764 for(i=0; i < entries; i++)
1766 int32_t idx;
1767 base = &ptr[8 + i*4];
1768 progid = (base[0] << 8) | base[1];
1770 if((idx = prog_idx_in_pat(priv, progid)) == -1)
1772 int sz = sizeof(struct pat_progs_t) * (priv->pat.progs_cnt+1);
1773 tmp = realloc_struct(priv->pat.progs, priv->pat.progs_cnt+1, sizeof(struct pat_progs_t));
1774 if(tmp == NULL)
1776 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PAT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
1777 break;
1779 priv->pat.progs = tmp;
1780 idx = priv->pat.progs_cnt;
1781 priv->pat.progs_cnt++;
1784 priv->pat.progs[idx].id = progid;
1785 priv->pat.progs[idx].pmt_pid = ((base[2] & 0x1F) << 8) | base[3];
1786 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);
1787 mp_msg(MSGT_IDENTIFY, MSGL_V, "PROGRAM_ID=%d (0x%02X), PMT_PID: %d(0x%02X)\n",
1788 progid, progid, priv->pat.progs[idx].pmt_pid, priv->pat.progs[idx].pmt_pid);
1791 return 1;
1795 static inline int32_t es_pid_in_pmt(pmt_t * pmt, uint16_t pid)
1797 uint16_t i;
1799 if(pmt == NULL)
1800 return -1;
1802 if(pmt->es == NULL)
1803 return -1;
1805 for(i = 0; i < pmt->es_cnt; i++)
1807 if(pmt->es[i].pid == pid)
1808 return (int32_t) i;
1811 return -1;
1815 static uint16_t get_mp4_desc_len(uint8_t *buf, int *len)
1817 //uint16_t i = 0, size = 0;
1818 int i = 0, j, size = 0;
1820 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PARSE_MP4_DESC_LEN(%d), bytes: ", *len);
1821 j = FFMIN(*len, 4);
1822 while(i < j)
1824 mp_msg(MSGT_DEMUX, MSGL_DBG2, " %x ", buf[i]);
1825 size |= (buf[i] & 0x7f);
1826 if(!(buf[i] & 0x80))
1827 break;
1828 size <<= 7;
1829 i++;
1831 mp_msg(MSGT_DEMUX, MSGL_DBG2, ", SIZE=%d\n", size);
1833 *len = i+1;
1834 return size;
1838 static uint16_t parse_mp4_slconfig_descriptor(uint8_t *buf, int len, void *elem)
1840 int i = 0;
1841 mp4_es_descr_t *es;
1842 mp4_sl_config_t *sl;
1844 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_SLCONFIG_DESCRIPTOR(%d)\n", len);
1845 es = (mp4_es_descr_t *) elem;
1846 if(!es)
1848 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1849 return len;
1851 sl = &(es->sl);
1853 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;
1854 sl->ocr = sl->dts = sl->cts = 0;
1856 if(buf[0] == 0)
1858 i++;
1859 sl->flags = buf[i];
1860 i++;
1861 sl->ts_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1862 i += 4;
1863 sl->ocr_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1864 i += 4;
1865 sl->ts_len = buf[i];
1866 i++;
1867 sl->ocr_len = buf[i];
1868 i++;
1869 sl->au_len = buf[i];
1870 i++;
1871 sl->instant_bitrate_len = buf[i];
1872 i++;
1873 sl->degr_len = (buf[i] >> 4) & 0x0f;
1874 sl->au_seqnum_len = ((buf[i] & 0x0f) << 1) | ((buf[i+1] >> 7) & 0x01);
1875 i++;
1876 sl->packet_seqnum_len = ((buf[i] >> 2) & 0x1f);
1877 i++;
1880 else if(buf[0] == 1)
1882 sl->flags = 0;
1883 sl->ts_resolution = 1000;
1884 sl->ts_len = 32;
1885 i++;
1887 else if(buf[0] == 2)
1889 sl->flags = 4;
1890 i++;
1892 else
1894 sl->flags = 0;
1895 i++;
1898 sl->au_start = (sl->flags >> 7) & 0x1;
1899 sl->au_end = (sl->flags >> 6) & 0x1;
1900 sl->random_accesspoint = (sl->flags >> 5) & 0x1;
1901 sl->random_accesspoint_only = (sl->flags >> 4) & 0x1;
1902 sl->padding = (sl->flags >> 3) & 0x1;
1903 sl->use_ts = (sl->flags >> 2) & 0x1;
1904 sl->idle = (sl->flags >> 1) & 0x1;
1905 sl->duration = sl->flags & 0x1;
1907 if(sl->duration)
1909 sl->timescale = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1910 i += 4;
1911 sl->au_duration = (buf[i] << 8) | buf[i+1];
1912 i += 2;
1913 sl->cts_duration = (buf[i] << 8) | buf[i+1];
1914 i += 2;
1916 else //no support for fixed durations atm
1917 sl->timescale = sl->au_duration = sl->cts_duration = 0;
1919 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",
1920 len, buf[0], sl->flags, sl->use_ts, sl->ts_len, sl->timescale, (uint64_t) sl->dts, (uint64_t) sl->cts);
1922 return len;
1925 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem);
1927 static uint16_t parse_mp4_decoder_config_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
1929 int i = 0, j;
1930 mp4_es_descr_t *es;
1931 mp4_decoder_config_t *dec;
1933 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_CONFIG_DESCRIPTOR(%d)\n", len);
1934 es = (mp4_es_descr_t *) elem;
1935 if(!es)
1937 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1938 return len;
1940 dec = (mp4_decoder_config_t*) &(es->decoder);
1942 dec->object_type = buf[i];
1943 dec->stream_type = (buf[i+1]>>2) & 0x3f;
1945 if(dec->object_type == 1 && dec->stream_type == 1)
1947 dec->object_type = MP4_OD;
1948 dec->stream_type = MP4_OD;
1950 else if(dec->stream_type == 4)
1952 if(dec->object_type == 0x6a)
1953 dec->object_type = VIDEO_MPEG1;
1954 if(dec->object_type >= 0x60 && dec->object_type <= 0x65)
1955 dec->object_type = VIDEO_MPEG2;
1956 else if(dec->object_type == 0x20)
1957 dec->object_type = VIDEO_MPEG4;
1958 else if(dec->object_type == 0x21)
1959 dec->object_type = VIDEO_AVC;
1960 /*else if(dec->object_type == 0x22)
1961 fprintf(stderr, "TYPE 0x22\n");*/
1962 else dec->object_type = UNKNOWN;
1964 else if(dec->stream_type == 5)
1966 if(dec->object_type == 0x40)
1967 dec->object_type = AUDIO_AAC;
1968 else if(dec->object_type == 0x6b)
1969 dec->object_type = AUDIO_MP2;
1970 else if(dec->object_type >= 0x66 && dec->object_type <= 0x69)
1971 dec->object_type = AUDIO_MP2;
1972 else
1973 dec->object_type = UNKNOWN;
1975 else
1976 dec->object_type = dec->stream_type = UNKNOWN;
1978 if(dec->object_type != UNKNOWN)
1980 //update the type of the current stream
1981 for(j = 0; j < pmt->es_cnt; j++)
1983 if(pmt->es[j].mp4_es_id == es->id)
1985 pmt->es[j].type = SL_PES_STREAM;
1990 if(len > 13)
1991 parse_mp4_descriptors(pmt, &buf[13], len-13, dec);
1993 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);
1995 return len;
1998 static uint16_t parse_mp4_decoder_specific_descriptor(uint8_t *buf, int len, void *elem)
2000 int i;
2001 mp4_decoder_config_t *dec;
2003 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_SPECIFIC_DESCRIPTOR(%d)\n", len);
2004 dec = (mp4_decoder_config_t *) elem;
2005 if(!dec)
2007 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
2008 return len;
2011 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4 SPECIFIC INFO BYTES: \n");
2012 for(i=0; i<len; i++)
2013 mp_msg(MSGT_DEMUX, MSGL_DBG2, "%02x ", buf[i]);
2014 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\n");
2016 if(len > MAX_EXTRADATA_SIZE)
2018 mp_msg(MSGT_DEMUX, MSGL_ERR, "DEMUX_TS, EXTRADATA SUSPICIOUSLY BIG: %d, REFUSED\r\n", len);
2019 return len;
2021 memcpy(dec->buf, buf, len);
2022 dec->buf_size = len;
2024 return len;
2027 static uint16_t parse_mp4_es_descriptor(pmt_t *pmt, uint8_t *buf, int len)
2029 int i = 0, j = 0, k, found;
2030 uint8_t flag;
2031 mp4_es_descr_t es, *target_es = NULL, *tmp;
2033 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4ES: len=%d\n", len);
2034 memset(&es, 0, sizeof(mp4_es_descr_t));
2035 while(i < len)
2037 es.id = (buf[i] << 8) | buf[i+1];
2038 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_ID: %d\n", es.id);
2039 i += 2;
2040 flag = buf[i];
2041 i++;
2042 if(flag & 0x80)
2043 i += 2;
2044 if(flag & 0x40)
2045 i += buf[i]+1;
2046 if(flag & 0x20) //OCR, maybe we need it
2047 i += 2;
2049 j = parse_mp4_descriptors(pmt, &buf[i], len-i, &es);
2050 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);
2051 if(es.decoder.object_type != UNKNOWN && es.decoder.stream_type != UNKNOWN)
2053 found = 0;
2054 //search this ES_ID if we already have it
2055 for(k=0; k < pmt->mp4es_cnt; k++)
2057 if(pmt->mp4es[k].id == es.id)
2059 target_es = &(pmt->mp4es[k]);
2060 found = 1;
2064 if(! found)
2066 tmp = realloc_struct(pmt->mp4es, pmt->mp4es_cnt+1, sizeof(mp4_es_descr_t));
2067 if(tmp == NULL)
2069 fprintf(stderr, "CAN'T REALLOC MP4_ES_DESCR\n");
2070 continue;
2072 pmt->mp4es = tmp;
2073 target_es = &(pmt->mp4es[pmt->mp4es_cnt]);
2074 pmt->mp4es_cnt++;
2076 memcpy(target_es, &es, sizeof(mp4_es_descr_t));
2077 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_CNT: %d, ID=%d\n", pmt->mp4es_cnt, target_es->id);
2080 i += j;
2083 return len;
2086 static void parse_mp4_object_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2088 int i, j = 0, id;
2090 i=0;
2091 id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2092 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_OBJECT_DESCRIPTOR: len=%d, OD_ID=%d\n", len, id);
2093 if(buf[1] & 0x20)
2095 i += buf[2] + 1; //url
2096 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2098 else
2100 i = 2;
2102 while(i < len)
2104 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2105 mp_msg(MSGT_DEMUX, MSGL_V, "OBJD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2106 i += j;
2112 static void parse_mp4_iod(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2114 int i, j = 0;
2115 mp4_od_t *iod = &(pmt->iod);
2117 iod->id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2118 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_IOD: len=%d, IOD_ID=%d\n", len, iod->id);
2119 i = 2;
2120 if(buf[1] & 0x20)
2122 i += buf[2] + 1; //url
2123 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2125 else
2127 i = 7;
2128 while(i < len)
2130 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2131 mp_msg(MSGT_DEMUX, MSGL_V, "IOD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2132 i += j;
2137 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2139 int tag, descr_len, i = 0, j = 0;
2141 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DESCRIPTORS, len=%d\n", len);
2142 if(! len)
2143 return len;
2145 while(i < len)
2147 tag = buf[i];
2148 j = len - i -1;
2149 descr_len = get_mp4_desc_len(&(buf[i+1]), &j);
2150 mp_msg(MSGT_DEMUX, MSGL_V, "TAG=%d (0x%x), DESCR_len=%d, len=%d, j=%d\n", tag, tag, descr_len, len, j);
2151 if(descr_len > len - j+1)
2153 mp_msg(MSGT_DEMUX, MSGL_V, "descriptor is too long, exit\n");
2154 return len;
2156 i += j+1;
2158 switch(tag)
2160 case 0x1:
2161 parse_mp4_object_descriptor(pmt, &(buf[i]), descr_len, elem);
2162 break;
2163 case 0x2:
2164 parse_mp4_iod(pmt, &(buf[i]), descr_len, elem);
2165 break;
2166 case 0x3:
2167 parse_mp4_es_descriptor(pmt, &(buf[i]), descr_len);
2168 break;
2169 case 0x4:
2170 parse_mp4_decoder_config_descriptor(pmt, &buf[i], descr_len, elem);
2171 break;
2172 case 0x05:
2173 parse_mp4_decoder_specific_descriptor(&buf[i], descr_len, elem);
2174 break;
2175 case 0x6:
2176 parse_mp4_slconfig_descriptor(&buf[i], descr_len, elem);
2177 break;
2178 default:
2179 mp_msg(MSGT_DEMUX, MSGL_V, "Unsupported mp4 descriptor 0x%x\n", tag);
2181 i += descr_len;
2184 return len;
2187 static ES_stream_t *new_pid(ts_priv_t *priv, int pid)
2189 ES_stream_t *tss;
2191 tss = malloc(sizeof(ES_stream_t));
2192 if(! tss)
2193 return NULL;
2194 memset(tss, 0, sizeof(ES_stream_t));
2195 tss->pid = pid;
2196 tss->last_cc = -1;
2197 tss->type = UNKNOWN;
2198 tss->subtype = UNKNOWN;
2199 tss->is_synced = 0;
2200 tss->extradata = NULL;
2201 tss->extradata_alloc = tss->extradata_len = 0;
2202 priv->ts.pids[pid] = tss;
2204 return tss;
2208 static int parse_program_descriptors(pmt_t *pmt, uint8_t *buf, uint16_t len)
2210 uint16_t i = 0, k, olen = len;
2212 while(len > 0)
2214 mp_msg(MSGT_DEMUX, MSGL_V, "PROG DESCR, TAG=%x, LEN=%d(%x)\n", buf[i], buf[i+1], buf[i+1]);
2215 if(buf[i+1] > len-2)
2217 mp_msg(MSGT_DEMUX, MSGL_V, "ERROR, descriptor len is too long, skipping\n");
2218 return olen;
2221 if(buf[i] == 0x1d)
2223 if(buf[i+3] == 2) //buggy versions of vlc muxer make this non-standard mess (missing iod_scope)
2224 k = 3;
2225 else
2226 k = 4; //this is standard compliant
2227 parse_mp4_descriptors(pmt, &buf[i+k], (int) buf[i+1]-(k-2), NULL);
2230 len -= 2 + buf[i+1];
2233 return olen;
2236 static int parse_descriptors(struct pmt_es_t *es, uint8_t *ptr)
2238 int j, descr_len, len;
2240 j = 0;
2241 len = es->descr_length;
2242 while(len > 2)
2244 descr_len = ptr[j+1];
2245 mp_msg(MSGT_DEMUX, MSGL_V, "...descr id: 0x%x, len=%d\n", ptr[j], descr_len);
2246 if(descr_len > len)
2248 mp_msg(MSGT_DEMUX, MSGL_ERR, "INVALID DESCR LEN for tag %02x: %d vs %d max, EXIT LOOP\n", ptr[j], descr_len, len);
2249 return -1;
2253 if(ptr[j] == 0x6a || ptr[j] == 0x7a) //A52 Descriptor
2255 if(es->type == 0x6)
2257 es->type = AUDIO_A52;
2258 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB A52 Descriptor\n");
2261 else if(ptr[j] == 0x7b) //DVB DTS Descriptor
2263 if(es->type == 0x6)
2265 es->type = AUDIO_DTS;
2266 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB DTS Descriptor\n");
2269 else if(ptr[j] == 0x56) // Teletext
2271 if(descr_len >= 5) {
2272 memcpy(es->lang, ptr+2, 3);
2273 es->lang[3] = 0;
2275 es->type = SPU_TELETEXT;
2277 else if(ptr[j] == 0x59) //Subtitling Descriptor
2279 uint8_t subtype;
2281 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Subtitling Descriptor\n");
2282 if(descr_len < 8)
2284 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Descriptor length too short for DVB Subtitle Descriptor: %d, SKIPPING\n", descr_len);
2286 else
2288 memcpy(es->lang, &ptr[j+2], 3);
2289 es->lang[3] = 0;
2290 subtype = ptr[j+5];
2292 (subtype >= 0x10 && subtype <= 0x13) ||
2293 (subtype >= 0x20 && subtype <= 0x23)
2296 es->type = SPU_DVB;
2297 //page parameters: compo page 2 bytes, ancillary page 2 bytes
2299 else
2300 es->type = UNKNOWN;
2303 else if(ptr[j] == 0x50) //Component Descriptor
2305 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Component Descriptor\n");
2306 memcpy(es->lang, &ptr[j+5], 3);
2307 es->lang[3] = 0;
2309 else if(ptr[j] == 0xa) //Language Descriptor
2311 memcpy(es->lang, &ptr[j+2], 3);
2312 es->lang[3] = 0;
2313 mp_msg(MSGT_DEMUX, MSGL_V, "Language Descriptor: %s\n", es->lang);
2315 else if(ptr[j] == 0x5) //Registration Descriptor (looks like e fourCC :) )
2317 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor\n");
2318 if(descr_len < 4)
2320 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor length too short: %d, SKIPPING\n", descr_len);
2322 else
2324 char *d;
2325 memcpy(es->format_descriptor, &ptr[j+2], 4);
2326 es->format_descriptor[4] = 0;
2328 d = &ptr[j+2];
2329 if(d[0] == 'A' && d[1] == 'C' && d[2] == '-' && d[3] == '3')
2331 es->type = AUDIO_A52;
2333 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '1')
2335 es->type = AUDIO_DTS;
2337 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '2')
2339 es->type = AUDIO_DTS;
2341 else if(d[0] == 'V' && d[1] == 'C' && d[2] == '-' && d[3] == '1')
2343 es->type = VIDEO_VC1;
2345 else if(d[0] == 'd' && d[1] == 'r' && d[2] == 'a' && d[3] == 'c')
2347 es->type = VIDEO_DIRAC;
2349 else
2350 es->type = UNKNOWN;
2351 mp_msg(MSGT_DEMUX, MSGL_DBG2, "FORMAT %s\n", es->format_descriptor);
2354 else if(ptr[j] == 0x1e)
2356 es->mp4_es_id = (ptr[j+2] << 8) | ptr[j+3];
2357 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);
2359 else
2360 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Unknown descriptor 0x%x, SKIPPING\n", ptr[j]);
2362 len -= 2 + descr_len;
2363 j += 2 + descr_len;
2366 return 1;
2369 static int parse_sl_section(pmt_t *pmt, ts_section_t *section, int is_start, unsigned char *buff, int size)
2371 int tid, len, skip;
2372 uint8_t *ptr;
2373 skip = collect_section(section, is_start, buff, size);
2374 if(! skip)
2375 return 0;
2377 ptr = &(section->buffer[skip]);
2378 tid = ptr[0];
2379 len = ((ptr[1] & 0x0f) << 8) | ptr[2];
2380 mp_msg(MSGT_DEMUX, MSGL_V, "TABLEID: %d (av. %d), skip=%d, LEN: %d\n", tid, section->buffer_len, skip, len);
2381 if(len > 4093 || section->buffer_len < len || tid != 5)
2383 mp_msg(MSGT_DEMUX, MSGL_V, "SECTION TOO LARGE or wrong section type, EXIT\n");
2384 return 0;
2387 if(! (ptr[5] & 1))
2388 return 0;
2390 //8 is the current position, len - 9 is the amount of data available
2391 parse_mp4_descriptors(pmt, &ptr[8], len - 9, NULL);
2393 return 1;
2396 static int parse_pmt(ts_priv_t * priv, uint16_t progid, uint16_t pid, int is_start, unsigned char *buff, int size)
2398 unsigned char *base, *es_base;
2399 pmt_t *pmt;
2400 int32_t idx, es_count, section_bytes;
2401 uint8_t m=0;
2402 int skip;
2403 pmt_t *tmp;
2404 struct pmt_es_t *tmp_es;
2405 ts_section_t *section;
2406 ES_stream_t *tss;
2408 idx = progid_idx_in_pmt(priv, progid);
2410 if(idx == -1)
2412 int sz = (priv->pmt_cnt + 1) * sizeof(pmt_t);
2413 tmp = realloc_struct(priv->pmt, priv->pmt_cnt + 1, sizeof(pmt_t));
2414 if(tmp == NULL)
2416 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
2417 return 0;
2419 priv->pmt = tmp;
2420 idx = priv->pmt_cnt;
2421 memset(&(priv->pmt[idx]), 0, sizeof(pmt_t));
2422 priv->pmt_cnt++;
2423 priv->pmt[idx].progid = progid;
2426 pmt = &(priv->pmt[idx]);
2428 section = &(pmt->section);
2429 skip = collect_section(section, is_start, buff, size);
2430 if(! skip)
2431 return 0;
2433 base = &(section->buffer[skip]);
2435 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",
2436 progid, pmt->section.buffer_len, is_start, pid, size, m, pmt->es_cnt, idx, pmt);
2438 pmt->table_id = base[0];
2439 if(pmt->table_id != 2)
2440 return -1;
2441 pmt->ssi = base[1] & 0x80;
2442 pmt->section_length = (((base[1] & 0xf) << 8 ) | base[2]);
2443 pmt->version_number = (base[5] >> 1) & 0x1f;
2444 pmt->curr_next = (base[5] & 1);
2445 pmt->section_number = base[6];
2446 pmt->last_section_number = base[7];
2447 pmt->PCR_PID = ((base[8] & 0x1f) << 8 ) | base[9];
2448 pmt->prog_descr_length = ((base[10] & 0xf) << 8 ) | base[11];
2449 if(pmt->prog_descr_length > pmt->section_length - 9)
2451 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, INVALID PROG_DESCR LENGTH (%d vs %d)\n", pmt->prog_descr_length, pmt->section_length - 9);
2452 return -1;
2455 if(pmt->prog_descr_length)
2456 parse_program_descriptors(pmt, &base[12], pmt->prog_descr_length);
2458 es_base = &base[12 + pmt->prog_descr_length]; //the beginning of th ES loop
2460 section_bytes= pmt->section_length - 13 - pmt->prog_descr_length;
2461 es_count = 0;
2463 while(section_bytes >= 5)
2465 int es_pid, es_type;
2467 es_type = es_base[0];
2468 es_pid = ((es_base[1] & 0x1f) << 8) | es_base[2];
2470 idx = es_pid_in_pmt(pmt, es_pid);
2471 if(idx == -1)
2473 int sz = sizeof(struct pmt_es_t) * (pmt->es_cnt + 1);
2474 tmp_es = realloc_struct(pmt->es, pmt->es_cnt + 1, sizeof(struct pmt_es_t));
2475 if(tmp_es == NULL)
2477 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT, COULDN'T ALLOCATE %d bytes for PMT_ES\n", sz);
2478 continue;
2480 pmt->es = tmp_es;
2481 idx = pmt->es_cnt;
2482 memset(&(pmt->es[idx]), 0, sizeof(struct pmt_es_t));
2483 pmt->es_cnt++;
2486 pmt->es[idx].descr_length = ((es_base[3] & 0xf) << 8) | es_base[4];
2489 if(pmt->es[idx].descr_length > section_bytes - 5)
2491 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, ES_DESCR_LENGTH TOO LARGE %d > %d, EXIT\n",
2492 pmt->es[idx].descr_length, section_bytes - 5);
2493 return -1;
2497 pmt->es[idx].pid = es_pid;
2498 if(es_type != 0x6)
2499 pmt->es[idx].type = UNKNOWN;
2500 else
2501 pmt->es[idx].type = es_type;
2503 parse_descriptors(&pmt->es[idx], &es_base[5]);
2505 switch(es_type)
2507 case 1:
2508 pmt->es[idx].type = VIDEO_MPEG1;
2509 break;
2510 case 2:
2511 pmt->es[idx].type = VIDEO_MPEG2;
2512 break;
2513 case 3:
2514 case 4:
2515 pmt->es[idx].type = AUDIO_MP2;
2516 break;
2517 case 6:
2518 if(pmt->es[idx].type == 0x6) //this could have been ovrwritten by parse_descriptors
2519 pmt->es[idx].type = UNKNOWN;
2520 break;
2521 case 0x10:
2522 pmt->es[idx].type = VIDEO_MPEG4;
2523 break;
2524 case 0x0f:
2525 pmt->es[idx].type = AUDIO_AAC;
2526 break;
2527 case 0x11:
2528 pmt->es[idx].type = AUDIO_AAC_LATM;
2529 break;
2530 case 0x1b:
2531 pmt->es[idx].type = VIDEO_H264;
2532 break;
2533 case 0x12:
2534 pmt->es[idx].type = SL_PES_STREAM;
2535 break;
2536 case 0x13:
2537 pmt->es[idx].type = SL_SECTION;
2538 break;
2539 case 0x81:
2540 pmt->es[idx].type = AUDIO_A52;
2541 break;
2542 case 0x8A:
2543 case 0x82:
2544 case 0x85:
2545 case 0x86:
2546 pmt->es[idx].type = AUDIO_DTS;
2547 break;
2548 case 0x90:
2549 pmt->es[idx].type = SPU_PGS;
2550 break;
2551 case 0xD1:
2552 pmt->es[idx].type = VIDEO_DIRAC;
2553 break;
2554 case 0xEA:
2555 pmt->es[idx].type = VIDEO_VC1;
2556 break;
2557 default:
2558 mp_msg(MSGT_DEMUX, MSGL_DBG2, "UNKNOWN ES TYPE=0x%x\n", es_type);
2559 pmt->es[idx].type = UNKNOWN;
2562 tss = priv->ts.pids[es_pid]; //an ES stream
2563 if(tss == NULL)
2565 tss = new_pid(priv, es_pid);
2566 if(tss)
2567 tss->type = pmt->es[idx].type;
2570 section_bytes -= 5 + pmt->es[idx].descr_length;
2571 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",
2572 progid, idx, es_count, pmt->es[idx].pid, pmt->es[idx].pid, pmt->es[idx].type, pmt->es[idx].descr_length, section_bytes);
2575 es_base += 5 + pmt->es[idx].descr_length;
2577 es_count++;
2580 mp_msg(MSGT_DEMUX, MSGL_V, "----------------------------\n");
2581 return 1;
2584 static pmt_t* pmt_of_pid(ts_priv_t *priv, int pid, mp4_decoder_config_t **mp4_dec)
2586 int32_t i, j, k;
2588 if(priv->pmt)
2590 for(i = 0; i < priv->pmt_cnt; i++)
2592 if(priv->pmt[i].es && priv->pmt[i].es_cnt)
2594 for(j = 0; j < priv->pmt[i].es_cnt; j++)
2596 if(priv->pmt[i].es[j].pid == pid)
2598 //search mp4_es_id
2599 if(priv->pmt[i].es[j].mp4_es_id)
2601 for(k = 0; k < priv->pmt[i].mp4es_cnt; k++)
2603 if(priv->pmt[i].mp4es[k].id == priv->pmt[i].es[j].mp4_es_id)
2605 *mp4_dec = &(priv->pmt[i].mp4es[k].decoder);
2606 break;
2611 return &(priv->pmt[i]);
2618 return NULL;
2622 static inline int32_t pid_type_from_pmt(ts_priv_t *priv, int pid)
2624 int32_t pmt_idx, pid_idx, i, j;
2626 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2628 if(pmt_idx != -1)
2630 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2631 if(pid_idx != -1)
2632 return priv->pmt[pmt_idx].es[pid_idx].type;
2634 //else
2636 for(i = 0; i < priv->pmt_cnt; i++)
2638 pmt_t *pmt = &(priv->pmt[i]);
2639 for(j = 0; j < pmt->es_cnt; j++)
2640 if(pmt->es[j].pid == pid)
2641 return pmt->es[j].type;
2645 return UNKNOWN;
2649 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid)
2651 int32_t pmt_idx, pid_idx, i, j;
2653 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2655 if(pmt_idx != -1)
2657 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2658 if(pid_idx != -1)
2659 return priv->pmt[pmt_idx].es[pid_idx].lang;
2661 else
2663 for(i = 0; i < priv->pmt_cnt; i++)
2665 pmt_t *pmt = &(priv->pmt[i]);
2666 for(j = 0; j < pmt->es_cnt; j++)
2667 if(pmt->es[j].pid == pid)
2668 return pmt->es[j].lang;
2672 return NULL;
2676 static int fill_packet(demuxer_t *demuxer, demux_stream_t *ds, demux_packet_t **dp, int *dp_offset, TS_stream_info *si)
2678 int ret = 0;
2680 if((*dp != NULL) && (*dp_offset > 0))
2682 ret = *dp_offset;
2683 resize_demux_packet(*dp, ret); //shrinked to the right size
2684 ds_add_packet(ds, *dp);
2685 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);
2686 if(si)
2688 float diff = (*dp)->pts - si->last_pts;
2689 float dur;
2691 if(abs(diff) > 1) //1 second, there's a discontinuity
2693 si->duration += si->last_pts - si->first_pts;
2694 si->first_pts = si->last_pts = (*dp)->pts;
2696 else
2698 si->last_pts = (*dp)->pts;
2700 si->size += ret;
2701 dur = si->duration + (si->last_pts - si->first_pts);
2703 if(dur > 0 && ds == demuxer->video)
2705 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2706 if(dur > 1) //otherwise it may be unreliable
2707 priv->vbitrate = (uint32_t) ((float) si->size / dur);
2712 *dp = NULL;
2713 *dp_offset = 0;
2715 return ret;
2718 static int fill_extradata(mp4_decoder_config_t * mp4_dec, ES_stream_t *tss)
2720 uint8_t *tmp;
2722 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4_dec: %p, pid: %d\n", mp4_dec, tss->pid);
2724 if(mp4_dec->buf_size > tss->extradata_alloc)
2726 tmp = realloc(tss->extradata, mp4_dec->buf_size);
2727 if(!tmp)
2728 return 0;
2729 tss->extradata = tmp;
2730 tss->extradata_alloc = mp4_dec->buf_size;
2732 memcpy(tss->extradata, mp4_dec->buf, mp4_dec->buf_size);
2733 tss->extradata_len = mp4_dec->buf_size;
2734 mp_msg(MSGT_DEMUX, MSGL_V, "EXTRADATA: %p, alloc=%d, len=%d\n", tss->extradata, tss->extradata_alloc, tss->extradata_len);
2736 return tss->extradata_len;
2739 // 0 = EOF or no stream found
2740 // else = [-] number of bytes written to the packet
2741 static int ts_parse(demuxer_t *demuxer , ES_stream_t *es, unsigned char *packet, int probe)
2743 ES_stream_t *tss;
2744 int buf_size, is_start, pid, base;
2745 int len, cc, cc_ok, afc, retv = 0, is_video, is_audio, is_sub;
2746 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2747 stream_t *stream = demuxer->stream;
2748 char *p;
2749 demux_stream_t *ds = NULL;
2750 demux_packet_t **dp = NULL;
2751 int *dp_offset = 0, *buffer_size = 0;
2752 int32_t progid, pid_type, bad, ts_error;
2753 int junk = 0, rap_flag = 0;
2754 pmt_t *pmt;
2755 mp4_decoder_config_t *mp4_dec;
2756 TS_stream_info *si;
2759 while(1)
2761 bad = ts_error = 0;
2762 ds = NULL;
2763 dp = NULL;
2764 dp_offset = buffer_size = NULL;
2765 rap_flag = 0;
2766 mp4_dec = NULL;
2767 es->is_synced = 0;
2768 es->lang[0] = 0;
2769 si = NULL;
2771 junk = priv->ts.packet_size - TS_PACKET_SIZE;
2772 buf_size = priv->ts.packet_size - junk;
2774 if(stream_eof(stream))
2776 if(! probe)
2778 ts_dump_streams(priv);
2779 demuxer->filepos = stream_tell(demuxer->stream);
2782 return 0;
2786 if(! ts_sync(stream))
2788 mp_msg(MSGT_DEMUX, MSGL_INFO, "TS_PARSE: COULDN'T SYNC\n");
2789 return 0;
2792 len = stream_read(stream, &packet[1], 3);
2793 if (len != 3)
2794 return 0;
2795 buf_size -= 4;
2797 if((packet[1] >> 7) & 0x01) //transport error
2798 ts_error = 1;
2801 is_start = packet[1] & 0x40;
2802 pid = ((packet[1] & 0x1f) << 8) | packet[2];
2804 tss = priv->ts.pids[pid]; //an ES stream
2805 if(tss == NULL)
2807 tss = new_pid(priv, pid);
2808 if(tss == NULL)
2809 continue;
2812 cc = (packet[3] & 0xf);
2813 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
2814 tss->last_cc = cc;
2816 bad = ts_error; // || (! cc_ok);
2817 if(bad)
2819 if(priv->keep_broken == 0)
2821 stream_skip(stream, buf_size-1+junk);
2822 continue;
2825 is_start = 0; //queued to the packet data
2828 if(is_start)
2829 tss->is_synced = 1;
2831 if((!is_start && !tss->is_synced) || ((pid > 1) && (pid < 16)) || (pid == 8191)) //invalid pid
2833 stream_skip(stream, buf_size-1+junk);
2834 continue;
2838 afc = (packet[3] >> 4) & 3;
2839 if(! (afc % 2)) //no payload in this TS packet
2841 stream_skip(stream, buf_size-1+junk);
2842 continue;
2845 if(afc > 1)
2847 int c;
2848 c = stream_read_char(stream);
2849 buf_size--;
2850 if(c < 0 || c > 183) //broken from the stream layer or invalid
2852 stream_skip(stream, buf_size-1+junk);
2853 continue;
2856 //c==0 is allowed!
2857 if(c > 0)
2859 uint8_t pcrbuf[188];
2860 int flags = stream_read_char(stream);
2861 int has_pcr;
2862 rap_flag = (flags & 0x40) >> 6;
2863 has_pcr = flags & 0x10;
2865 buf_size--;
2866 c--;
2867 stream_read(stream, pcrbuf, c);
2869 if(has_pcr)
2871 int pcr_pid = prog_pcr_pid(priv, priv->prog);
2872 if(pcr_pid == pid)
2874 uint64_t pcr, pcr_ext;
2876 pcr = (int64_t)(pcrbuf[0]) << 25;
2877 pcr |= pcrbuf[1] << 17 ;
2878 pcr |= (pcrbuf[2]) << 9;
2879 pcr |= pcrbuf[3] << 1 ;
2880 pcr |= (pcrbuf[4] & 0x80) >> 7;
2882 pcr_ext = (pcrbuf[4] & 0x01) << 8;
2883 pcr_ext |= pcrbuf[5];
2885 pcr = pcr * 300 + pcr_ext;
2887 demuxer->reference_clock = (double)pcr/(double)27000000.0;
2891 buf_size -= c;
2892 if(buf_size == 0)
2893 continue;
2897 //find the program that the pid belongs to; if (it's the right one or -1) && pid_type==SL_SECTION
2898 //call parse_sl_section()
2899 pmt = pmt_of_pid(priv, pid, &mp4_dec);
2900 if(mp4_dec)
2902 fill_extradata(mp4_dec, tss);
2903 if(IS_VIDEO(mp4_dec->object_type) || IS_AUDIO(mp4_dec->object_type))
2905 tss->type = SL_PES_STREAM;
2906 tss->subtype = mp4_dec->object_type;
2911 //TABLE PARSING
2913 base = priv->ts.packet_size - buf_size;
2915 priv->last_pid = pid;
2917 is_video = IS_VIDEO(tss->type) || (tss->type==SL_PES_STREAM && IS_VIDEO(tss->subtype));
2918 is_audio = IS_AUDIO(tss->type) || (tss->type==SL_PES_STREAM && IS_AUDIO(tss->subtype)) || (tss->type == PES_PRIVATE1);
2919 is_sub = IS_SUB(tss->type);
2920 pid_type = pid_type_from_pmt(priv, pid);
2922 // PES CONTENT STARTS HERE
2923 if(! probe)
2925 if((is_video || is_audio || is_sub) && is_start)
2926 ts_add_stream(demuxer, tss);
2928 if(is_video && (demuxer->video->id == priv->ts.streams[pid].id))
2930 ds = demuxer->video;
2932 dp = &priv->fifo[1].pack;
2933 dp_offset = &priv->fifo[1].offset;
2934 buffer_size = &priv->fifo[1].buffer_size;
2935 si = &priv->vstr;
2937 else if(is_audio && (demuxer->audio->id == priv->ts.streams[pid].id))
2939 ds = demuxer->audio;
2941 dp = &priv->fifo[0].pack;
2942 dp_offset = &priv->fifo[0].offset;
2943 buffer_size = &priv->fifo[0].buffer_size;
2944 si = &priv->astr;
2946 else if(is_sub)
2948 sh_sub_t *sh_sub = demuxer->sub->sh;
2950 if(sh_sub && sh_sub->sid == tss->pid)
2952 ds = demuxer->sub;
2954 dp = &priv->fifo[2].pack;
2955 dp_offset = &priv->fifo[2].offset;
2956 buffer_size = &priv->fifo[2].buffer_size;
2958 else
2960 stream_skip(stream, buf_size+junk);
2961 continue;
2965 //IS IT TIME TO QUEUE DATA to the dp_packet?
2966 if(is_start && (dp != NULL))
2968 // subtitle packets _have_ to be submitted before video, otherwise
2969 // they might get stuck "forever" and subtitles will be completely
2970 // out of sync.
2971 if (is_video)
2972 fill_packet(demuxer, demuxer->sub, &priv->fifo[2].pack, &priv->fifo[2].offset, NULL);
2973 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
2977 if(dp && *dp == NULL)
2979 if(*buffer_size > MAX_PACK_BYTES)
2980 *buffer_size = MAX_PACK_BYTES;
2981 *dp = new_demux_packet(*buffer_size); //es->size
2982 *dp_offset = 0;
2983 if(! *dp)
2985 fprintf(stderr, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", *buffer_size);
2986 continue;
2988 mp_msg(MSGT_DEMUX, MSGL_DBG2, "CREATED DP(%d)\n", *buffer_size);
2993 if(probe || !dp) //dp is NULL for tables and sections
2995 p = &packet[base];
2997 else //feeding
2999 if(*dp_offset + buf_size > *buffer_size)
3001 *buffer_size = *dp_offset + buf_size + TS_FEC_PACKET_SIZE;
3002 resize_demux_packet(*dp, *buffer_size);
3004 p = &((*dp)->buffer[*dp_offset]);
3007 len = stream_read(stream, p, buf_size);
3008 if(len < buf_size)
3010 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\r\nts_parse() couldn't read enough data: %d < %d\r\n", len, buf_size);
3011 continue;
3013 stream_skip(stream, junk);
3015 if(pid == 0)
3017 parse_pat(priv, is_start, p, buf_size);
3018 continue;
3020 else if((tss->type == SL_SECTION) && pmt)
3022 int k, mp4_es_id = -1;
3023 ts_section_t *section;
3024 for(k = 0; k < pmt->mp4es_cnt; k++)
3026 if(pmt->mp4es[k].decoder.object_type == MP4_OD && pmt->mp4es[k].decoder.stream_type == MP4_OD)
3027 mp4_es_id = pmt->mp4es[k].id;
3029 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4ESID: %d\n", mp4_es_id);
3030 for(k = 0; k < pmt->es_cnt; k++)
3032 if(pmt->es[k].mp4_es_id == mp4_es_id)
3034 section = &(tss->section);
3035 parse_sl_section(pmt, section, is_start, &packet[base], buf_size);
3038 continue;
3040 else
3042 progid = prog_id_in_pat(priv, pid);
3043 if(progid != -1)
3045 if(pid != demuxer->video->id && pid != demuxer->audio->id && pid != demuxer->sub->id)
3047 parse_pmt(priv, progid, pid, is_start, &packet[base], buf_size);
3048 continue;
3050 else
3051 mp_msg(MSGT_DEMUX, MSGL_ERR, "Argh! Data pid %d used in the PMT, Skipping PMT parsing!\n", pid);
3055 if(!probe && !dp)
3056 continue;
3058 if(is_start)
3060 uint8_t *lang = NULL;
3062 mp_msg(MSGT_DEMUX, MSGL_DBG2, "IS_START\n");
3064 len = pes_parse2(p, buf_size, es, pid_type, pmt, pid);
3065 if(! len)
3067 tss->is_synced = 0;
3068 continue;
3070 es->pid = tss->pid;
3071 tss->is_synced |= es->is_synced || rap_flag;
3072 tss->payload_size = es->payload_size;
3074 if((is_sub || is_audio) && (lang = pid_lang_from_pmt(priv, es->pid)))
3076 memcpy(es->lang, lang, 3);
3077 es->lang[3] = 0;
3079 else
3080 es->lang[0] = 0;
3082 if(probe)
3084 if(es->type == UNKNOWN)
3085 return 0;
3087 tss->type = es->type;
3088 tss->subtype = es->subtype;
3090 return 1;
3092 else
3094 if(es->pts == 0.0)
3095 es->pts = tss->pts = tss->last_pts;
3096 else
3097 tss->pts = tss->last_pts = es->pts;
3099 mp_msg(MSGT_DEMUX, MSGL_DBG2, "ts_parse, NEW pid=%d, PSIZE: %u, type=%X, start=%p, len=%d\n",
3100 es->pid, es->payload_size, es->type, es->start, es->size);
3102 demuxer->filepos = stream_tell(demuxer->stream) - es->size;
3104 if(es->size < 0 || es->size > buf_size) {
3105 mp_msg(MSGT_DEMUX, MSGL_ERR, "Broken ES packet size\n");
3106 es->size = 0;
3108 memmove(p, es->start, es->size);
3109 *dp_offset += es->size;
3110 (*dp)->flags = 0;
3111 (*dp)->pos = stream_tell(demuxer->stream);
3112 (*dp)->pts = es->pts;
3114 if(retv > 0)
3115 return retv;
3116 else
3117 continue;
3120 else
3122 uint16_t sz;
3124 es->pid = tss->pid;
3125 es->type = tss->type;
3126 es->subtype = tss->subtype;
3127 es->pts = tss->pts = tss->last_pts;
3128 es->start = &packet[base];
3131 if(tss->payload_size > 0)
3133 sz = FFMIN(tss->payload_size, buf_size);
3134 tss->payload_size -= sz;
3135 es->size = sz;
3137 else
3139 if(is_video)
3141 sz = es->size = buf_size;
3143 else
3145 continue;
3150 if(! probe)
3152 *dp_offset += sz;
3154 if(*dp_offset >= MAX_PACK_BYTES)
3156 (*dp)->pts = tss->last_pts;
3157 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
3158 return 1;
3161 continue;
3163 else
3165 memcpy(es->start, p, sz);
3167 if(es->size)
3168 return es->size;
3169 else
3170 continue;
3175 return 0;
3179 static void reset_fifos(demuxer_t *demuxer, int a, int v, int s)
3181 ts_priv_t* priv = demuxer->priv;
3182 if(a)
3184 if(priv->fifo[0].pack != NULL)
3186 free_demux_packet(priv->fifo[0].pack);
3187 priv->fifo[0].pack = NULL;
3189 priv->fifo[0].offset = 0;
3192 if(v)
3194 if(priv->fifo[1].pack != NULL)
3196 free_demux_packet(priv->fifo[1].pack);
3197 priv->fifo[1].pack = NULL;
3199 priv->fifo[1].offset = 0;
3202 if(s)
3204 if(priv->fifo[2].pack != NULL)
3206 free_demux_packet(priv->fifo[2].pack);
3207 priv->fifo[2].pack = NULL;
3209 priv->fifo[2].offset = 0;
3211 demuxer->reference_clock = MP_NOPTS_VALUE;
3215 static void demux_seek_ts(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
3217 demux_stream_t *d_audio=demuxer->audio;
3218 demux_stream_t *d_video=demuxer->video;
3219 sh_audio_t *sh_audio=d_audio->sh;
3220 sh_video_t *sh_video=d_video->sh;
3221 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
3222 int i, video_stats;
3223 off_t newpos;
3225 //================= seek in MPEG-TS ==========================
3227 ts_dump_streams(demuxer->priv);
3228 reset_fifos(demuxer, sh_audio != NULL, sh_video != NULL, demuxer->sub->id > 0);
3230 demux_flush(demuxer);
3234 video_stats = (sh_video != NULL);
3235 if(video_stats)
3237 mp_msg(MSGT_DEMUX, MSGL_V, "IBPS: %d, vb: %d\r\n", sh_video->i_bps, priv->vbitrate);
3238 if(priv->vbitrate)
3239 video_stats = priv->vbitrate;
3240 else
3241 video_stats = sh_video->i_bps;
3244 newpos = (flags & SEEK_ABSOLUTE) ? demuxer->movi_start : demuxer->filepos;
3245 if(flags & SEEK_FACTOR) // float seek 0..1
3246 newpos+=(demuxer->movi_end-demuxer->movi_start)*rel_seek_secs;
3247 else
3249 // time seek (secs)
3250 if(! video_stats) // unspecified or VBR
3251 newpos += 2324*75*rel_seek_secs; // 174.3 kbyte/sec
3252 else
3253 newpos += video_stats*rel_seek_secs;
3257 if(newpos < demuxer->movi_start)
3258 newpos = demuxer->movi_start; //begininng of stream
3260 stream_seek(demuxer->stream, newpos);
3261 for(i = 0; i < 8192; i++)
3262 if(priv->ts.pids[i] != NULL)
3263 priv->ts.pids[i]->is_synced = 0;
3265 videobuf_code_len = 0;
3267 if(sh_video != NULL)
3268 ds_fill_buffer(d_video);
3270 if(sh_audio != NULL)
3272 ds_fill_buffer(d_audio);
3275 while(sh_video != NULL)
3277 if(sh_audio && !d_audio->eof && d_video->pts && d_audio->pts)
3279 double a_pts=d_audio->pts;
3280 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(double)sh_audio->i_bps;
3281 if(d_video->pts > a_pts)
3283 skip_audio_frame(sh_audio); // sync audio
3284 continue;
3289 i = sync_video_packet(d_video);
3290 if((sh_video->format == VIDEO_MPEG1) || (sh_video->format == VIDEO_MPEG2))
3292 if(i==0x1B3 || i==0x1B8) break; // found it!
3294 else if((sh_video->format == VIDEO_MPEG4) && (i==0x1B6))
3295 break;
3296 else if(sh_video->format == VIDEO_VC1 && (i==0x10E || i==0x10F))
3297 break;
3298 else //H264
3300 if((i & ~0x60) == 0x105 || (i & ~0x60) == 0x107) break;
3303 if(!i || !skip_video_packet(d_video)) break; // EOF?
3308 static int demux_ts_fill_buffer(demuxer_t * demuxer, demux_stream_t *ds)
3310 ES_stream_t es;
3311 ts_priv_t *priv = (ts_priv_t *)demuxer->priv;
3313 return -ts_parse(demuxer, &es, priv->packet, 0);
3317 static int ts_check_file_dmx(demuxer_t *demuxer)
3319 return ts_check_file(demuxer) ? DEMUXER_TYPE_MPEG_TS : 0;
3322 static int is_usable_program(ts_priv_t *priv, pmt_t *pmt)
3324 int j;
3326 for(j = 0; j < pmt->es_cnt; j++)
3328 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3329 continue;
3331 priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO ||
3332 priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO
3334 return 1;
3337 return 0;
3340 static int demux_ts_control(demuxer_t *demuxer, int cmd, void *arg)
3342 ts_priv_t* priv = (ts_priv_t *)demuxer->priv;
3344 switch(cmd)
3346 case DEMUXER_CTRL_SWITCH_AUDIO:
3347 case DEMUXER_CTRL_SWITCH_VIDEO:
3349 void *sh = NULL;
3350 int i, n;
3351 int reftype, areset = 0, vreset = 0;
3352 demux_stream_t *ds;
3354 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
3356 reftype = TYPE_VIDEO;
3357 ds = demuxer->video;
3358 vreset = 1;
3360 else
3362 reftype = TYPE_AUDIO;
3363 ds = demuxer->audio;
3364 areset = 1;
3366 n = *((int*)arg);
3367 if(n == -2)
3369 reset_fifos(demuxer, areset, vreset, 0);
3370 ds->id = -2;
3371 ds->sh = NULL;
3372 ds_free_packs(ds);
3373 *((int*)arg) = ds->id;
3374 return DEMUXER_CTRL_OK;
3377 if(n < 0)
3379 for(i = 0; i < 8192; i++)
3381 if(priv->ts.streams[i].id == ds->id && priv->ts.streams[i].type == reftype)
3382 break;
3385 while(!sh)
3387 i = (i+1) % 8192;
3388 if(priv->ts.streams[i].type == reftype)
3390 if(priv->ts.streams[i].id == ds->id) //we made a complete loop
3391 break;
3392 sh = priv->ts.streams[i].sh;
3396 else //audio track <n>
3398 if (n >= 8192 || priv->ts.streams[n].type != reftype) return DEMUXER_CTRL_NOTIMPL;
3399 i = n;
3400 sh = priv->ts.streams[i].sh;
3403 if(sh)
3405 if(ds->id != priv->ts.streams[i].id)
3406 reset_fifos(demuxer, areset, vreset, 0);
3407 ds->id = priv->ts.streams[i].id;
3408 ds->sh = sh;
3409 ds_free_packs(ds);
3410 mp_msg(MSGT_DEMUX, MSGL_V, "\r\ndemux_ts, switched to audio pid %d, id: %d, sh: %p\r\n", i, ds->id, sh);
3413 *((int*)arg) = ds->id;
3414 return DEMUXER_CTRL_OK;
3417 case DEMUXER_CTRL_IDENTIFY_PROGRAM: //returns in prog->{aid,vid} the new ids that comprise a program
3419 int i, j, cnt=0;
3420 int vid_done=0, aid_done=0;
3421 pmt_t *pmt = NULL;
3422 demux_program_t *prog = arg;
3424 if(priv->pmt_cnt < 2)
3425 return DEMUXER_CTRL_NOTIMPL;
3427 if(prog->progid == -1)
3429 int cur_pmt_idx = 0;
3431 for(i = 0; i < priv->pmt_cnt; i++)
3432 if(priv->pmt[i].progid == priv->prog)
3434 cur_pmt_idx = i;
3435 break;
3438 i = (cur_pmt_idx + 1) % priv->pmt_cnt;
3439 while(i != cur_pmt_idx)
3441 pmt = &priv->pmt[i];
3442 cnt = is_usable_program(priv, pmt);
3443 if(cnt)
3444 break;
3445 i = (i + 1) % priv->pmt_cnt;
3448 else
3450 for(i = 0; i < priv->pmt_cnt; i++)
3451 if(priv->pmt[i].progid == prog->progid)
3453 pmt = &priv->pmt[i]; //required program
3454 cnt = is_usable_program(priv, pmt);
3458 if(!cnt)
3459 return DEMUXER_CTRL_NOTIMPL;
3461 //finally some food
3462 prog->aid = prog->vid = -2; //no audio and no video by default
3463 for(j = 0; j < pmt->es_cnt; j++)
3465 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3466 continue;
3468 if(!vid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO)
3470 vid_done = 1;
3471 prog->vid = pmt->es[j].pid;
3473 else if(!aid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO)
3475 aid_done = 1;
3476 prog->aid = pmt->es[j].pid;
3480 priv->prog = prog->progid = pmt->progid;
3481 return DEMUXER_CTRL_OK;
3484 default:
3485 return DEMUXER_CTRL_NOTIMPL;
3490 const demuxer_desc_t demuxer_desc_mpeg_ts = {
3491 "MPEG-TS demuxer",
3492 "mpegts",
3493 "TS",
3494 "Nico Sabbi",
3496 DEMUXER_TYPE_MPEG_TS,
3497 0, // unsafe autodetect
3498 ts_check_file_dmx,
3499 demux_ts_fill_buffer,
3500 demux_open_ts,
3501 demux_close_ts,
3502 demux_seek_ts,
3503 demux_ts_control