demux_real.c: Remove unused hexdump() forward declaration
[mplayer/greg.git] / libmpdemux / demux_ts.c
blobce79c166cdb0aca8fbe60930927490ef4ec409d0
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
60 int ts_prog;
61 int ts_keep_broken=0;
62 off_t ts_probe = 0;
63 int audio_substream_id = -1;
65 typedef enum
67 UNKNOWN = -1,
68 VIDEO_MPEG1 = 0x10000001,
69 VIDEO_MPEG2 = 0x10000002,
70 VIDEO_MPEG4 = 0x10000004,
71 VIDEO_H264 = 0x10000005,
72 VIDEO_AVC = mmioFOURCC('a', 'v', 'c', '1'),
73 VIDEO_DIRAC = mmioFOURCC('d', 'r', 'a', 'c'),
74 VIDEO_VC1 = mmioFOURCC('W', 'V', 'C', '1'),
75 AUDIO_MP2 = 0x50,
76 AUDIO_A52 = 0x2000,
77 AUDIO_DTS = 0x2001,
78 AUDIO_LPCM_BE = 0x10001,
79 AUDIO_AAC = mmioFOURCC('M', 'P', '4', 'A'),
80 AUDIO_AAC_LATM = mmioFOURCC('M', 'P', '4', 'L'),
81 AUDIO_TRUEHD = mmioFOURCC('T', 'R', 'H', 'D'),
82 SPU_DVD = 0x3000000,
83 SPU_DVB = 0x3000001,
84 SPU_TELETEXT = 0x3000002,
85 SPU_PGS = 0x3000003,
86 PES_PRIVATE1 = 0xBD00000,
87 SL_PES_STREAM = 0xD000000,
88 SL_SECTION = 0xD100000,
89 MP4_OD = 0xD200000,
90 } es_stream_type_t;
92 typedef struct {
93 uint8_t *buffer;
94 uint16_t buffer_len;
95 } ts_section_t;
97 typedef struct {
98 int size;
99 unsigned char *start;
100 uint16_t payload_size;
101 es_stream_type_t type, subtype;
102 double pts, last_pts;
103 int pid;
104 char lang[4];
105 int last_cc; // last cc code (-1 if first packet)
106 int is_synced;
107 ts_section_t section;
108 uint8_t *extradata;
109 int extradata_alloc, extradata_len;
110 struct {
111 uint8_t au_start, au_end, last_au_end;
112 } sl;
113 } ES_stream_t;
115 typedef struct {
116 void *sh;
117 int id;
118 int type;
119 } sh_av_t;
121 typedef struct MpegTSContext {
122 int packet_size; // raw packet size, including FEC if present e.g. 188 bytes
123 ES_stream_t *pids[NB_PID_MAX];
124 sh_av_t streams[NB_PID_MAX];
125 } MpegTSContext;
128 typedef struct {
129 demux_stream_t *ds;
130 demux_packet_t *pack;
131 int offset, buffer_size;
132 } av_fifo_t;
134 #define MAX_EXTRADATA_SIZE 64*1024
135 typedef struct {
136 int32_t object_type; //aka codec used
137 int32_t stream_type; //video, audio etc.
138 uint8_t buf[MAX_EXTRADATA_SIZE];
139 uint16_t buf_size;
140 uint8_t szm1;
141 } mp4_decoder_config_t;
143 typedef struct {
144 //flags
145 uint8_t flags;
146 uint8_t au_start;
147 uint8_t au_end;
148 uint8_t random_accesspoint;
149 uint8_t random_accesspoint_only;
150 uint8_t padding;
151 uint8_t use_ts;
152 uint8_t idle;
153 uint8_t duration;
155 uint32_t ts_resolution, ocr_resolution;
156 uint8_t ts_len, ocr_len, au_len, instant_bitrate_len, degr_len, au_seqnum_len, packet_seqnum_len;
157 uint32_t timescale;
158 uint16_t au_duration, cts_duration;
159 uint64_t ocr, dts, cts;
160 } mp4_sl_config_t;
162 typedef struct {
163 uint16_t id;
164 uint8_t flags;
165 mp4_decoder_config_t decoder;
166 mp4_sl_config_t sl;
167 } mp4_es_descr_t;
169 typedef struct {
170 uint16_t id;
171 uint8_t flags;
172 mp4_es_descr_t *es;
173 uint16_t es_cnt;
174 } mp4_od_t;
176 typedef struct {
177 uint8_t skip;
178 uint8_t table_id;
179 uint8_t ssi;
180 uint16_t section_length;
181 uint16_t ts_id;
182 uint8_t version_number;
183 uint8_t curr_next;
184 uint8_t section_number;
185 uint8_t last_section_number;
186 struct pat_progs_t {
187 uint16_t id;
188 uint16_t pmt_pid;
189 } *progs;
190 uint16_t progs_cnt;
191 ts_section_t section;
192 } pat_t;
194 typedef struct {
195 uint16_t progid;
196 uint8_t skip;
197 uint8_t table_id;
198 uint8_t ssi;
199 uint16_t section_length;
200 uint8_t version_number;
201 uint8_t curr_next;
202 uint8_t section_number;
203 uint8_t last_section_number;
204 uint16_t PCR_PID;
205 uint16_t prog_descr_length;
206 ts_section_t section;
207 uint16_t es_cnt;
208 struct pmt_es_t {
209 uint16_t pid;
210 uint32_t type; //it's 8 bit long, but cast to the right type as FOURCC
211 uint16_t descr_length;
212 uint8_t format_descriptor[5];
213 uint8_t lang[4];
214 uint16_t mp4_es_id;
215 } *es;
216 mp4_od_t iod, *od;
217 mp4_es_descr_t *mp4es;
218 int od_cnt, mp4es_cnt;
219 } pmt_t;
221 typedef struct {
222 uint64_t size;
223 float duration;
224 double first_pts;
225 double last_pts;
226 } TS_stream_info;
228 typedef struct {
229 MpegTSContext ts;
230 int last_pid;
231 av_fifo_t fifo[3]; //0 for audio, 1 for video, 2 for subs
232 pat_t pat;
233 pmt_t *pmt;
234 uint16_t pmt_cnt;
235 uint32_t prog;
236 uint32_t vbitrate;
237 int keep_broken;
238 int last_aid;
239 int last_vid;
240 int last_sid;
241 char packet[TS_FEC_PACKET_SIZE];
242 TS_stream_info vstr, astr;
243 } ts_priv_t;
246 typedef struct {
247 es_stream_type_t type;
248 ts_section_t section;
249 } TS_pids_t;
252 static int IS_AUDIO(es_stream_type_t type)
254 switch (type) {
255 case AUDIO_MP2:
256 case AUDIO_A52:
257 case AUDIO_LPCM_BE:
258 case AUDIO_AAC:
259 case AUDIO_AAC_LATM:
260 case AUDIO_DTS:
261 case AUDIO_TRUEHD:
262 return 1;
264 return 0;
267 static int IS_VIDEO(es_stream_type_t type)
269 switch (type) {
270 case VIDEO_MPEG1:
271 case VIDEO_MPEG2:
272 case VIDEO_MPEG4:
273 case VIDEO_H264:
274 case VIDEO_AVC:
275 case VIDEO_DIRAC:
276 case VIDEO_VC1:
277 return 1;
279 return 0;
282 static int IS_SUB(es_stream_type_t type)
284 switch (type) {
285 case SPU_DVD:
286 case SPU_DVB:
287 case SPU_PGS:
288 case SPU_TELETEXT:
289 return 1;
291 return 0;
294 static int ts_parse(demuxer_t *demuxer, ES_stream_t *es, unsigned char *packet, int probe);
296 static uint8_t get_packet_size(const unsigned char *buf, int size)
298 int i;
300 if (size < (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS))
301 return 0;
303 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
305 if (buf[i * TS_PACKET_SIZE] != 0x47)
307 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
308 goto try_fec;
311 return TS_PACKET_SIZE;
313 try_fec:
314 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
316 if (buf[i * TS_FEC_PACKET_SIZE] != 0x47){
317 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
318 goto try_philips;
321 return TS_FEC_PACKET_SIZE;
323 try_philips:
324 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
326 if (buf[i * TS_PH_PACKET_SIZE] != 0x47)
327 return 0;
329 return TS_PH_PACKET_SIZE;
332 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h);
333 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid);
335 static void ts_add_stream(demuxer_t * demuxer, ES_stream_t *es)
337 int i;
338 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
340 if(priv->ts.streams[es->pid].sh)
341 return;
343 if((IS_AUDIO(es->type) || IS_AUDIO(es->subtype)) && priv->last_aid+1 < MAX_A_STREAMS)
345 sh_audio_t *sh = new_sh_audio_aid(demuxer, priv->last_aid, es->pid);
346 if(sh)
348 const char *lang = pid_lang_from_pmt(priv, es->pid);
349 sh->needs_parsing = 1;
350 sh->format = IS_AUDIO(es->type) ? es->type : es->subtype;
351 sh->ds = demuxer->audio;
353 priv->ts.streams[es->pid].id = priv->last_aid;
354 priv->ts.streams[es->pid].sh = sh;
355 priv->ts.streams[es->pid].type = TYPE_AUDIO;
356 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);
357 if (lang && lang[0])
358 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_AID_%d_LANG=%s\n", es->pid, lang);
359 priv->last_aid++;
362 if(es->extradata && es->extradata_len)
364 sh->wf = malloc(sizeof(*sh->wf) + es->extradata_len);
365 sh->wf->cbSize = es->extradata_len;
366 memcpy(sh->wf + 1, es->extradata, es->extradata_len);
370 if((IS_VIDEO(es->type) || IS_VIDEO(es->subtype)) && priv->last_vid+1 < MAX_V_STREAMS)
372 sh_video_t *sh = new_sh_video_vid(demuxer, priv->last_vid, es->pid);
373 if(sh)
375 sh->format = IS_VIDEO(es->type) ? es->type : es->subtype;
376 sh->ds = demuxer->video;
378 priv->ts.streams[es->pid].id = priv->last_vid;
379 priv->ts.streams[es->pid].sh = sh;
380 priv->ts.streams[es->pid].type = TYPE_VIDEO;
381 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);
382 priv->last_vid++;
385 if(sh->format == VIDEO_AVC && es->extradata && es->extradata_len)
387 int w = 0, h = 0;
388 sh->bih = calloc(1, sizeof(*sh->bih) + es->extradata_len);
389 sh->bih->biSize= sizeof(*sh->bih) + es->extradata_len;
390 sh->bih->biCompression = sh->format;
391 memcpy(sh->bih + 1, es->extradata, es->extradata_len);
392 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "EXTRADATA(%d BYTES): \n", es->extradata_len);
393 for(i = 0;i < es->extradata_len; i++)
394 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "%02x ", (int) es->extradata[i]);
395 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"\n");
396 if(parse_avc_sps(es->extradata, es->extradata_len, &w, &h))
398 sh->bih->biWidth = w;
399 sh->bih->biHeight = h;
405 if(IS_SUB(es->type) && priv->last_sid+1 < MAX_S_STREAMS)
407 sh_sub_t *sh = new_sh_sub_sid_lang(demuxer, priv->last_sid, es->pid, pid_lang_from_pmt(priv, es->pid));
408 if (sh) {
409 switch (es->type) {
410 case SPU_DVD:
411 sh->type = 'v'; break;
412 case SPU_PGS:
413 sh->type = 'p'; break;
415 priv->ts.streams[es->pid].id = priv->last_aid;
416 priv->ts.streams[es->pid].sh = sh;
417 priv->ts.streams[es->pid].type = TYPE_AUDIO;
418 priv->last_sid++;
423 static int ts_check_file(demuxer_t * demuxer)
425 const int buf_size = (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS);
426 unsigned char buf[TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS], done = 0, *ptr;
427 uint32_t _read, i, count = 0, is_ts;
428 int cc[NB_PID_MAX], last_cc[NB_PID_MAX], pid, cc_ok, c, good, bad;
429 uint8_t size = 0;
430 off_t pos = 0;
431 off_t init_pos;
433 mp_msg(MSGT_DEMUX, MSGL_V, "Checking for MPEG-TS...\n");
435 init_pos = stream_tell(demuxer->stream);
436 is_ts = 0;
437 while(! done)
439 i = 1;
440 c = 0;
442 while(((c=stream_read_char(demuxer->stream)) != 0x47)
443 && (c >= 0)
444 && (i < MAX_CHECK_SIZE)
445 && ! demuxer->stream->eof
446 ) i++;
449 if(c != 0x47)
451 mp_msg(MSGT_DEMUX, MSGL_V, "THIS DOESN'T LOOK LIKE AN MPEG-TS FILE!\n");
452 is_ts = 0;
453 done = 1;
454 continue;
457 pos = stream_tell(demuxer->stream) - 1;
458 buf[0] = c;
459 _read = stream_read(demuxer->stream, &buf[1], buf_size-1);
461 if(_read < buf_size-1)
463 mp_msg(MSGT_DEMUX, MSGL_V, "COULDN'T READ ENOUGH DATA, EXITING TS_CHECK\n");
464 stream_reset(demuxer->stream);
465 return 0;
468 size = get_packet_size(buf, buf_size);
469 if(size)
471 done = 1;
472 is_ts = 1;
475 if(pos - init_pos >= MAX_CHECK_SIZE)
477 done = 1;
478 is_ts = 0;
482 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);
483 stream_seek(demuxer->stream, pos);
485 if(! is_ts)
486 return 0;
488 //LET'S CHECK continuity counters
489 good = bad = 0;
490 for(count = 0; count < NB_PID_MAX; count++)
492 cc[count] = last_cc[count] = -1;
495 for(count = 0; count < NUM_CONSECUTIVE_TS_PACKETS; count++)
497 ptr = &(buf[size * count]);
498 pid = ((ptr[1] & 0x1f) << 8) | ptr[2];
499 mp_msg(MSGT_DEMUX, MSGL_DBG2, "BUF: %02x %02x %02x %02x, PID %d, SIZE: %d \n",
500 ptr[0], ptr[1], ptr[2], ptr[3], pid, size);
502 if((pid == 8191) || (pid < 16))
503 continue;
505 cc[pid] = (ptr[3] & 0xf);
506 cc_ok = (last_cc[pid] < 0) || ((((last_cc[pid] + 1) & 0x0f) == cc[pid]));
507 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PID %d, COMPARE CC %d AND LAST_CC %d\n", pid, cc[pid], last_cc[pid]);
508 if(! cc_ok)
509 //return 0;
510 bad++;
511 else
512 good++;
514 last_cc[pid] = cc[pid];
517 mp_msg(MSGT_DEMUX, MSGL_V, "GOOD CC: %d, BAD CC: %d\n", good, bad);
519 if(good >= bad)
520 return size;
521 else
522 return 0;
526 static inline int32_t progid_idx_in_pmt(ts_priv_t *priv, uint16_t progid)
528 int x;
530 if(priv->pmt == NULL)
531 return -1;
533 for(x = 0; x < priv->pmt_cnt; x++)
535 if(priv->pmt[x].progid == progid)
536 return x;
539 return -1;
543 static inline int32_t progid_for_pid(ts_priv_t *priv, int pid, int32_t req) //finds the first program listing a pid
545 int i, j;
546 pmt_t *pmt;
549 if(priv->pmt == NULL)
550 return -1;
553 for(i=0; i < priv->pmt_cnt; i++)
555 pmt = &(priv->pmt[i]);
557 if(pmt->es == NULL)
558 return -1;
560 for(j = 0; j < pmt->es_cnt; j++)
562 if(pmt->es[j].pid == pid)
564 if((req == 0) || (req == pmt->progid))
565 return pmt->progid;
570 return -1;
573 static inline int32_t prog_pcr_pid(ts_priv_t *priv, int progid)
575 int i;
577 if(priv->pmt == NULL)
578 return -1;
579 for(i=0; i < priv->pmt_cnt; i++)
581 if(priv->pmt[i].progid == progid)
582 return priv->pmt[i].PCR_PID;
584 return -1;
588 static inline int pid_match_lang(ts_priv_t *priv, uint16_t pid, char *lang)
590 uint16_t i, j;
591 pmt_t *pmt;
593 if(priv->pmt == NULL)
594 return -1;
596 for(i=0; i < priv->pmt_cnt; i++)
598 pmt = &(priv->pmt[i]);
600 if(pmt->es == NULL)
601 return -1;
603 for(j = 0; j < pmt->es_cnt; j++)
605 if(pmt->es[j].pid != pid)
606 continue;
608 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);
609 if(strncmp(pmt->es[j].lang, lang, 3) == 0)
611 return 1;
617 return -1;
620 typedef struct {
621 int32_t atype, vtype, stype; //types
622 int32_t apid, vpid, spid; //stream ids
623 char alang[4]; //languages
624 uint16_t prog;
625 off_t probe;
626 } tsdemux_init_t;
628 //second stage: returns the count of A52 syncwords found
629 static int a52_check(char *buf, int len)
631 int cnt, frame_length = 0, ok, srate;
633 cnt = ok = 0;
634 if(len < 8)
635 return 0;
637 while(cnt < len - 7)
639 if(buf[cnt] == 0x0B && buf[cnt+1] == 0x77)
641 frame_length = mp_a52_framesize(&buf[cnt], &srate);
642 if(frame_length>=7 && frame_length<=3840)
644 cnt += frame_length;
645 ok++;
647 else
648 cnt++;
650 else
651 cnt++;
654 mp_msg(MSGT_DEMUXER, MSGL_V, "A52_CHECK(%d input bytes), found %d frame syncwords of %d bytes length\n", len, ok, frame_length);
655 return ok;
659 static off_t ts_detect_streams(demuxer_t *demuxer, tsdemux_init_t *param)
661 int video_found = 0, audio_found = 0, sub_found = 0, i, num_packets = 0, req_apid, req_vpid, req_spid;
662 int is_audio, is_video, is_sub, has_tables;
663 int32_t p, chosen_pid = 0;
664 off_t pos=0, ret = 0, init_pos, end_pos;
665 ES_stream_t es;
666 unsigned char tmp[TS_FEC_PACKET_SIZE];
667 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
668 struct {
669 char *buf;
670 int pos;
671 } pes_priv1[8192], *pptr;
672 char *tmpbuf;
674 priv->last_pid = 8192; //invalid pid
676 req_apid = param->apid;
677 req_vpid = param->vpid;
678 req_spid = param->spid;
680 has_tables = 0;
681 memset(pes_priv1, 0, sizeof(pes_priv1));
682 init_pos = stream_tell(demuxer->stream);
683 mp_msg(MSGT_DEMUXER, MSGL_V, "PROBING UP TO %"PRIu64", PROG: %d\n", (uint64_t) param->probe, param->prog);
684 end_pos = init_pos + (param->probe ? param->probe : TS_MAX_PROBE_SIZE);
685 while(1)
687 pos = stream_tell(demuxer->stream);
688 if(pos > end_pos || demuxer->stream->eof)
689 break;
691 if(ts_parse(demuxer, &es, tmp, 1))
693 //Non PES-aligned A52 audio may escape detection if PMT is not present;
694 //in this case we try to find at least 3 A52 syncwords
695 if((es.type == PES_PRIVATE1) && (! audio_found) && req_apid > -2)
697 pptr = &pes_priv1[es.pid];
698 if(pptr->pos < 64*1024)
700 tmpbuf = realloc(pptr->buf, pptr->pos + es.size);
701 if(tmpbuf != NULL)
703 pptr->buf = tmpbuf;
704 memcpy(&(pptr->buf[ pptr->pos ]), es.start, es.size);
705 pptr->pos += es.size;
706 if(a52_check(pptr->buf, pptr->pos) > 2)
708 param->atype = AUDIO_A52;
709 param->apid = es.pid;
710 es.type = AUDIO_A52;
716 is_audio = IS_AUDIO(es.type) || ((es.type==SL_PES_STREAM) && IS_AUDIO(es.subtype));
717 is_video = IS_VIDEO(es.type) || ((es.type==SL_PES_STREAM) && IS_VIDEO(es.subtype));
718 is_sub = IS_SUB(es.type);
721 if((! is_audio) && (! is_video) && (! is_sub))
722 continue;
723 if(is_audio && req_apid==-2)
724 continue;
726 if(is_video)
728 chosen_pid = (req_vpid == es.pid);
729 if((! chosen_pid) && (req_vpid > 0))
730 continue;
732 else if(is_audio)
734 if(req_apid > 0)
736 chosen_pid = (req_apid == es.pid);
737 if(! chosen_pid)
738 continue;
740 else if(param->alang[0] > 0 && es.lang[0] > 0)
742 if(pid_match_lang(priv, es.pid, param->alang) == -1)
743 continue;
745 chosen_pid = 1;
746 param->apid = req_apid = es.pid;
749 else if(is_sub)
751 chosen_pid = (req_spid == es.pid);
752 if((! chosen_pid) && (req_spid > 0))
753 continue;
756 if(req_apid < 0 && (param->alang[0] == 0) && req_vpid < 0 && req_spid < 0)
757 chosen_pid = 1;
759 if((ret == 0) && chosen_pid)
761 ret = stream_tell(demuxer->stream);
764 p = progid_for_pid(priv, es.pid, param->prog);
765 if(p != -1)
767 has_tables++;
768 if(!param->prog && chosen_pid)
769 param->prog = p;
772 if((param->prog > 0) && (param->prog != p))
774 if(audio_found)
776 if(is_video && (req_vpid == es.pid))
778 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
779 param->vpid = es.pid;
780 video_found = 1;
781 break;
785 if(video_found)
787 if(is_audio && (req_apid == es.pid))
789 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
790 param->apid = es.pid;
791 audio_found = 1;
792 break;
797 continue;
801 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "TYPE: %x, PID: %d, PROG FOUND: %d\n", es.type, es.pid, param->prog);
804 if(is_video)
806 if((req_vpid == -1) || (req_vpid == es.pid))
808 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
809 param->vpid = es.pid;
810 video_found = 1;
815 if(((req_vpid == -2) || (num_packets >= NUM_CONSECUTIVE_AUDIO_PACKETS)) && audio_found && !param->probe)
817 //novideo or we have at least 348 audio packets (64 KB) without video (TS with audio only)
818 param->vtype = 0;
819 break;
822 if(is_sub)
824 if((req_spid == -1) || (req_spid == es.pid))
826 param->stype = es.type;
827 param->spid = es.pid;
828 sub_found = 1;
832 if(is_audio)
834 if((req_apid == -1) || (req_apid == es.pid))
836 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
837 param->apid = es.pid;
838 audio_found = 1;
842 if(audio_found && (param->apid == es.pid) && (! video_found))
843 num_packets++;
845 if((has_tables==0) && (video_found && audio_found) && (pos >= 1000000))
846 break;
850 for(i=0; i<8192; i++)
852 if(pes_priv1[i].buf != NULL)
854 free(pes_priv1[i].buf);
855 pes_priv1[i].buf = NULL;
856 pes_priv1[i].pos = 0;
860 if(video_found)
862 if(param->vtype == VIDEO_MPEG1)
863 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG1(pid=%d) ", param->vpid);
864 else if(param->vtype == VIDEO_MPEG2)
865 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG2(pid=%d) ", param->vpid);
866 else if(param->vtype == VIDEO_MPEG4)
867 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG4(pid=%d) ", param->vpid);
868 else if(param->vtype == VIDEO_H264)
869 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO H264(pid=%d) ", param->vpid);
870 else if(param->vtype == VIDEO_VC1)
871 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO VC1(pid=%d) ", param->vpid);
872 else if(param->vtype == VIDEO_AVC)
873 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO AVC(NAL-H264, pid=%d) ", param->vpid);
875 else
877 param->vtype = UNKNOWN;
878 //WE DIDN'T MATCH ANY VIDEO STREAM
879 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO VIDEO! ");
882 if(param->atype == AUDIO_MP2)
883 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO MPA(pid=%d)", param->apid);
884 else if(param->atype == AUDIO_A52)
885 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO A52(pid=%d)", param->apid);
886 else if(param->atype == AUDIO_DTS)
887 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO DTS(pid=%d)", param->apid);
888 else if(param->atype == AUDIO_LPCM_BE)
889 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO LPCM(pid=%d)", param->apid);
890 else if(param->atype == AUDIO_AAC)
891 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO AAC(pid=%d)", param->apid);
892 else if(param->atype == AUDIO_AAC_LATM)
893 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO AAC LATM(pid=%d)", param->apid);
894 else if(param->atype == AUDIO_TRUEHD)
895 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO TRUEHD(pid=%d)", param->apid);
896 else
898 audio_found = 0;
899 param->atype = UNKNOWN;
900 //WE DIDN'T MATCH ANY AUDIO STREAM, SO WE FORCE THE DEMUXER TO IGNORE AUDIO
901 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO AUDIO! ");
904 if(IS_SUB(param->stype))
905 mp_msg(MSGT_DEMUXER, MSGL_INFO, " SUB %s(pid=%d) ", (param->stype==SPU_DVD ? "DVD" : param->stype==SPU_DVB ? "DVB" : "Teletext"), param->spid);
906 else
908 param->stype = UNKNOWN;
909 mp_msg(MSGT_DEMUXER, MSGL_INFO, " NO SUBS (yet)! ");
912 if(video_found || audio_found)
914 if(!param->prog)
916 p = progid_for_pid(priv, video_found ? param->vpid : param->apid, 0);
917 if(p != -1)
918 param->prog = p;
921 if(demuxer->stream->eof && (ret == 0))
922 ret = init_pos;
923 mp_msg(MSGT_DEMUXER, MSGL_INFO, " PROGRAM N. %d\n", param->prog);
925 else
926 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\n");
929 for(i=0; i<8192; i++)
931 if(priv->ts.pids[i] != NULL)
933 priv->ts.pids[i]->payload_size = 0;
934 priv->ts.pids[i]->pts = priv->ts.pids[i]->last_pts = 0;
935 priv->ts.pids[i]->last_cc = -1;
936 priv->ts.pids[i]->is_synced = 0;
940 return ret;
943 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h)
945 int sps, sps_len;
946 unsigned char *ptr;
947 mp_mpeg_header_t picture;
948 if(len < 6)
949 return 0;
950 sps = buf[5] & 0x1f;
951 if(!sps)
952 return 0;
953 sps_len = (buf[6] << 8) | buf[7];
954 if(!sps_len || (sps_len > len - 8))
955 return 0;
956 ptr = &(buf[8]);
957 picture.display_picture_width = picture.display_picture_height = 0;
958 h264_parse_sps(&picture, ptr, len - 8);
959 if(!picture.display_picture_width || !picture.display_picture_height)
960 return 0;
961 *w = picture.display_picture_width;
962 *h = picture.display_picture_height;
963 return 1;
966 static demuxer_t *demux_open_ts(demuxer_t * demuxer)
968 int i;
969 uint8_t packet_size;
970 sh_video_t *sh_video;
971 sh_audio_t *sh_audio;
972 off_t start_pos;
973 tsdemux_init_t params;
974 ts_priv_t * priv = demuxer->priv;
976 mp_msg(MSGT_DEMUX, MSGL_V, "DEMUX OPEN, AUDIO_ID: %d, VIDEO_ID: %d, SUBTITLE_ID: %d,\n",
977 demuxer->audio->id, demuxer->video->id, demuxer->sub->id);
980 demuxer->type= DEMUXER_TYPE_MPEG_TS;
983 stream_reset(demuxer->stream);
985 packet_size = ts_check_file(demuxer);
986 if(!packet_size)
987 return NULL;
989 priv = calloc(1, sizeof(ts_priv_t));
990 if(priv == NULL)
992 mp_msg(MSGT_DEMUX, MSGL_FATAL, "DEMUX_OPEN_TS, couldn't allocate enough memory for ts->priv, exit\n");
993 return NULL;
996 for(i=0; i < 8192; i++)
998 priv->ts.pids[i] = NULL;
999 priv->ts.streams[i].id = -3;
1001 priv->pat.progs = NULL;
1002 priv->pat.progs_cnt = 0;
1003 priv->pat.section.buffer = NULL;
1004 priv->pat.section.buffer_len = 0;
1006 priv->pmt = NULL;
1007 priv->pmt_cnt = 0;
1009 priv->keep_broken = ts_keep_broken;
1010 priv->ts.packet_size = packet_size;
1013 demuxer->priv = priv;
1014 if(demuxer->stream->type != STREAMTYPE_FILE)
1015 demuxer->seekable = 1;
1016 else
1017 demuxer->seekable = 1;
1020 params.atype = params.vtype = params.stype = UNKNOWN;
1021 params.apid = demuxer->audio->id;
1022 params.vpid = demuxer->video->id;
1023 params.spid = demuxer->sub->id;
1024 params.prog = ts_prog;
1025 params.probe = ts_probe;
1027 if(demuxer->opts->audio_lang != NULL)
1029 strncpy(params.alang, demuxer->opts->audio_lang, 3);
1030 params.alang[3] = 0;
1032 else
1033 memset(params.alang, 0, 4);
1035 start_pos = ts_detect_streams(demuxer, &params);
1037 demuxer->sub->id = params.spid;
1038 priv->prog = params.prog;
1040 if(params.vtype != UNKNOWN)
1042 ts_add_stream(demuxer, priv->ts.pids[params.vpid]);
1043 sh_video = priv->ts.streams[params.vpid].sh;
1044 demuxer->video->id = priv->ts.streams[params.vpid].id;
1045 sh_video->ds = demuxer->video;
1046 sh_video->format = params.vtype;
1047 demuxer->video->sh = sh_video;
1050 if(params.atype != UNKNOWN)
1052 ES_stream_t *es = priv->ts.pids[params.apid];
1054 if(!IS_AUDIO(es->type) && !IS_AUDIO(es->subtype) && IS_AUDIO(params.atype)) es->subtype = params.atype;
1055 ts_add_stream(demuxer, priv->ts.pids[params.apid]);
1056 sh_audio = priv->ts.streams[params.apid].sh;
1057 demuxer->audio->id = priv->ts.streams[params.apid].id;
1058 sh_audio->ds = demuxer->audio;
1059 sh_audio->format = params.atype;
1060 demuxer->audio->sh = sh_audio;
1064 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);
1067 start_pos = (start_pos <= priv->ts.packet_size ? 0 : start_pos - priv->ts.packet_size);
1068 demuxer->movi_start = start_pos;
1069 demuxer->reference_clock = MP_NOPTS_VALUE;
1070 stream_reset(demuxer->stream);
1071 stream_seek(demuxer->stream, start_pos); //IF IT'S FROM A PIPE IT WILL FAIL, BUT WHO CARES?
1074 priv->last_pid = 8192; //invalid pid
1076 for(i = 0; i < 3; i++)
1078 priv->fifo[i].pack = NULL;
1079 priv->fifo[i].offset = 0;
1081 priv->fifo[0].ds = demuxer->audio;
1082 priv->fifo[1].ds = demuxer->video;
1083 priv->fifo[2].ds = demuxer->sub;
1085 priv->fifo[0].buffer_size = 1536;
1086 priv->fifo[1].buffer_size = 32767;
1087 priv->fifo[2].buffer_size = 32767;
1089 priv->pat.section.buffer_len = 0;
1090 for(i = 0; i < priv->pmt_cnt; i++)
1091 priv->pmt[i].section.buffer_len = 0;
1093 demuxer->filepos = stream_tell(demuxer->stream);
1094 return demuxer;
1097 static void demux_close_ts(demuxer_t * demuxer)
1099 uint16_t i;
1100 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
1102 if(priv)
1104 if(priv->pat.section.buffer)
1105 free(priv->pat.section.buffer);
1106 if(priv->pat.progs)
1107 free(priv->pat.progs);
1109 if(priv->pmt)
1111 for(i = 0; i < priv->pmt_cnt; i++)
1113 if(priv->pmt[i].section.buffer)
1114 free(priv->pmt[i].section.buffer);
1115 if(priv->pmt[i].es)
1116 free(priv->pmt[i].es);
1118 free(priv->pmt);
1120 free(priv);
1122 demuxer->priv=NULL;
1126 #define getbits mp_getbits
1128 static int mp4_parse_sl_packet(pmt_t *pmt, uint8_t *buf, uint16_t packet_len, int pid, ES_stream_t *pes_es)
1130 int i, n, m, mp4_es_id = -1;
1131 uint64_t v = 0;
1132 uint32_t pl_size = 0;
1133 int deg_flag = 0;
1134 mp4_es_descr_t *es = NULL;
1135 mp4_sl_config_t *sl = NULL;
1136 uint8_t au_start = 0, au_end = 0, rap_flag = 0, ocr_flag = 0, padding = 0, padding_bits = 0, idle = 0;
1138 pes_es->is_synced = 0;
1139 mp_msg(MSGT_DEMUXER,MSGL_V, "mp4_parse_sl_packet, pid: %d, pmt: %pm, packet_len: %d\n", pid, pmt, packet_len);
1140 if(! pmt || !packet_len)
1141 return 0;
1143 for(i = 0; i < pmt->es_cnt; i++)
1145 if(pmt->es[i].pid == pid)
1146 mp4_es_id = pmt->es[i].mp4_es_id;
1148 if(mp4_es_id < 0)
1149 return -1;
1151 for(i = 0; i < pmt->mp4es_cnt; i++)
1153 if(pmt->mp4es[i].id == mp4_es_id)
1154 es = &(pmt->mp4es[i]);
1156 if(! es)
1157 return -1;
1159 pes_es->subtype = es->decoder.object_type;
1161 sl = &(es->sl);
1162 if(!sl)
1163 return -1;
1165 //now es is the complete es_descriptor of out mp4 ES stream
1166 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "ID: %d, FLAGS: 0x%x, subtype: %x\n", es->id, sl->flags, pes_es->subtype);
1168 n = 0;
1169 if(sl->au_start)
1170 pes_es->sl.au_start = au_start = getbits(buf, n++, 1);
1171 else
1172 pes_es->sl.au_start = (pes_es->sl.last_au_end ? 1 : 0);
1173 if(sl->au_end)
1174 pes_es->sl.au_end = au_end = getbits(buf, n++, 1);
1176 if(!sl->au_start && !sl->au_end)
1178 pes_es->sl.au_start = pes_es->sl.au_end = au_start = au_end = 1;
1180 pes_es->sl.last_au_end = pes_es->sl.au_end;
1183 if(sl->ocr_len > 0)
1184 ocr_flag = getbits(buf, n++, 1);
1185 if(sl->idle)
1186 idle = getbits(buf, n++, 1);
1187 if(sl->padding)
1188 padding = getbits(buf, n++, 1);
1189 if(padding)
1191 padding_bits = getbits(buf, n, 3);
1192 n += 3;
1195 if(idle || (padding && !padding_bits))
1197 pes_es->payload_size = 0;
1198 return -1;
1201 //(! idle && (!padding || padding_bits != 0)) is true
1202 n += sl->packet_seqnum_len;
1203 if(sl->degr_len)
1204 deg_flag = getbits(buf, n++, 1);
1205 if(deg_flag)
1206 n += sl->degr_len;
1208 if(ocr_flag)
1210 n += sl->ocr_len;
1211 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "OCR: %d bits\n", sl->ocr_len);
1214 if(packet_len * 8 <= n)
1215 return -1;
1217 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "\nAU_START: %d, AU_END: %d\n", au_start, au_end);
1218 if(au_start)
1220 int dts_flag = 0, cts_flag = 0, ib_flag = 0;
1222 if(sl->random_accesspoint)
1223 rap_flag = getbits(buf, n++, 1);
1225 //check commented because it seems it's rarely used, and we need this flag set in case of au_start
1226 //the decoder will eventually discard the payload if it can't decode it
1227 //if(rap_flag || sl->random_accesspoint_only)
1228 pes_es->is_synced = 1;
1230 n += sl->au_seqnum_len;
1231 if(packet_len * 8 <= n+8)
1232 return -1;
1233 if(sl->use_ts)
1235 dts_flag = getbits(buf, n++, 1);
1236 cts_flag = getbits(buf, n++, 1);
1238 if(sl->instant_bitrate_len)
1239 ib_flag = getbits(buf, n++, 1);
1240 if(packet_len * 8 <= n+8)
1241 return -1;
1242 if(dts_flag && (sl->ts_len > 0))
1244 n += sl->ts_len;
1245 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "DTS: %d bits\n", sl->ts_len);
1247 if(packet_len * 8 <= n+8)
1248 return -1;
1249 if(cts_flag && (sl->ts_len > 0))
1251 int i = 0, m;
1253 while(i < sl->ts_len)
1255 m = FFMIN(8, sl->ts_len - i);
1256 v |= getbits(buf, n, m);
1257 if(sl->ts_len - i > 8)
1258 v <<= 8;
1259 i += m;
1260 n += m;
1261 if(packet_len * 8 <= n+8)
1262 return -1;
1265 pes_es->pts = (double) v / (double) sl->ts_resolution;
1266 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "CTS: %d bits, value: %"PRIu64"/%d = %.3f\n", sl->ts_len, v, sl->ts_resolution, pes_es->pts);
1270 i = 0;
1271 pl_size = 0;
1272 while(i < sl->au_len)
1274 m = FFMIN(8, sl->au_len - i);
1275 pl_size |= getbits(buf, n, m);
1276 if(sl->au_len - i > 8)
1277 pl_size <<= 8;
1278 i += m;
1279 n += m;
1280 if(packet_len * 8 <= n+8)
1281 return -1;
1283 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "AU_LEN: %u (%d bits)\n", pl_size, sl->au_len);
1284 if(ib_flag)
1285 n += sl->instant_bitrate_len;
1288 m = (n+7)/8;
1289 if(0 < pl_size && pl_size < pes_es->payload_size)
1290 pes_es->payload_size = pl_size;
1292 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",
1293 n, m, pes_es->payload_size, pl_size, (int) rap_flag, (int) sl->random_accesspoint_only);
1295 return m;
1298 //this function parses the extension fields in the PES header and returns the substream_id, or -1 in case of errors
1299 static int parse_pes_extension_fields(unsigned char *p, int pkt_len)
1301 int skip;
1302 unsigned char flags;
1304 if(!(p[7] & 0x1)) //no extension_field
1305 return -1;
1306 skip = 9;
1307 if(p[7] & 0x80)
1309 skip += 5;
1310 if(p[7] & 0x40)
1311 skip += 5;
1313 if(p[7] & 0x20) //escr_flag
1314 skip += 6;
1315 if(p[7] & 0x10) //es_rate_flag
1316 skip += 3;
1317 if(p[7] & 0x08)//dsm_trick_mode is unsupported, skip
1319 skip = 0;//don't let's parse the extension fields
1321 if(p[7] & 0x04) //additional_copy_info
1322 skip += 1;
1323 if(p[7] & 0x02) //pes_crc_flag
1324 skip += 2;
1325 if(skip >= pkt_len) //too few bytes
1326 return -1;
1327 flags = p[skip];
1328 skip++;
1329 if(flags & 0x80) //pes_private_data_flag
1330 skip += 16;
1331 if(skip >= pkt_len)
1332 return -1;
1333 if(flags & 0x40) //pack_header_field_flag
1335 unsigned char l = p[skip];
1336 skip += l;
1338 if(flags & 0x20) //program_packet_sequence_counter
1339 skip += 2;
1340 if(flags & 0x10) //p_std
1341 skip += 2;
1342 if(skip >= pkt_len)
1343 return -1;
1344 if(flags & 0x01) //finally the long desired pes_extension2
1346 unsigned char l = p[skip]; //ext2 flag+len
1347 skip++;
1348 if((l == 0x81) && (skip < pkt_len))
1350 int ssid = p[skip];
1351 mp_msg(MSGT_IDENTIFY, MSGL_V, "SUBSTREAM_ID=%d (0x%02X)\n", ssid, ssid);
1352 return ssid;
1356 return -1;
1359 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)
1361 unsigned char *p;
1362 uint32_t header_len;
1363 int64_t pts;
1364 uint32_t stream_id;
1365 uint32_t pkt_len, pes_is_aligned;
1367 //Here we are always at the start of a PES packet
1368 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2(%p, %d): \n", buf, (uint32_t) packet_len);
1370 if(packet_len == 0 || packet_len > 184)
1372 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2, BUFFER LEN IS TOO SMALL OR TOO BIG: %d EXIT\n", packet_len);
1373 return 0;
1376 p = buf;
1377 pkt_len = packet_len;
1380 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: HEADER %02x %02x %02x %02x\n", p[0], p[1], p[2], p[3]);
1381 if (p[0] || p[1] || (p[2] != 1))
1383 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: error HEADER %02x %02x %02x (should be 0x000001) \n", p[0], p[1], p[2]);
1384 return 0 ;
1387 packet_len -= 6;
1388 if(packet_len==0)
1390 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: packet too short: %d, exit\n", packet_len);
1391 return 0;
1394 es->payload_size = (p[4] << 8 | p[5]);
1395 pes_is_aligned = (p[6] & 4);
1397 stream_id = p[3];
1400 if (p[7] & 0x80)
1401 { /* pts available */
1402 pts = (int64_t)(p[9] & 0x0E) << 29 ;
1403 pts |= p[10] << 22 ;
1404 pts |= (p[11] & 0xFE) << 14 ;
1405 pts |= p[12] << 7 ;
1406 pts |= (p[13] & 0xFE) >> 1 ;
1408 es->pts = pts / 90000.0;
1410 else
1411 es->pts = 0.0;
1414 header_len = p[8];
1417 if (header_len + 9 > pkt_len) //9 are the bytes read up to the header_length field
1419 mp_msg(MSGT_DEMUX, MSGL_DBG2, "demux_ts: illegal value for PES_header_data_length (0x%02x)\n", header_len);
1420 return 0;
1423 if(stream_id==0xfd)
1425 int ssid = parse_pes_extension_fields(p, pkt_len);
1426 if((audio_substream_id!=-1) && (ssid != audio_substream_id))
1427 return 0;
1428 if(ssid == 0x72 && type_from_pmt != AUDIO_DTS && type_from_pmt != SPU_PGS)
1429 es->type = type_from_pmt = AUDIO_TRUEHD;
1432 p += header_len + 9;
1433 packet_len -= header_len + 3;
1435 if(es->payload_size)
1436 es->payload_size -= header_len + 3;
1439 es->is_synced = 1; //only for SL streams we have to make sure it's really true, see below
1440 if (stream_id == 0xbd)
1442 mp_msg(MSGT_DEMUX, MSGL_DBG3, "pes_parse2: audio buf = %02X %02X %02X %02X %02X %02X %02X %02X, 80: %d\n",
1443 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[0] & 0x80);
1447 * we check the descriptor tag first because some stations
1448 * do not include any of the A52 header info in their audio tracks
1449 * these "raw" streams may begin with a byte that looks like a stream type.
1453 if(type_from_pmt == SPU_PGS)
1455 es->start = p;
1456 es->size = packet_len;
1457 es->type = SPU_PGS;
1458 es->payload_size -= packet_len;
1459 return 1;
1462 (type_from_pmt == AUDIO_A52) || /* A52 - raw */
1463 (packet_len >= 2 && p[0] == 0x0B && p[1] == 0x77) /* A52 - syncword */
1466 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 RAW OR SYNCWORD\n");
1467 es->start = p;
1468 es->size = packet_len;
1469 es->type = AUDIO_A52;
1470 es->payload_size -= packet_len;
1472 return 1;
1474 /* SPU SUBS */
1475 else if(type_from_pmt == SPU_DVB ||
1476 (packet_len >= 1 && (p[0] == 0x20) && pes_is_aligned)) // && p[1] == 0x00))
1478 es->start = p;
1479 es->size = packet_len;
1480 es->type = SPU_DVB;
1481 es->payload_size -= packet_len;
1483 return 1;
1485 else if (pes_is_aligned && packet_len >= 1 && ((p[0] & 0xE0) == 0x20)) //SPU_DVD
1487 //DVD SUBS
1488 es->start = p+1;
1489 es->size = packet_len-1;
1490 es->type = SPU_DVD;
1491 es->payload_size -= packet_len;
1493 return 1;
1495 else if (pes_is_aligned && packet_len >= 4 && (p[0] & 0xF8) == 0x80)
1497 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 WITH HEADER\n");
1498 es->start = p+4;
1499 es->size = packet_len - 4;
1500 es->type = AUDIO_A52;
1501 es->payload_size -= packet_len;
1503 return 1;
1505 else if (pes_is_aligned && packet_len >= 1 && ((p[0]&0xf0) == 0xa0))
1507 int pcm_offset;
1509 for (pcm_offset=0; ++pcm_offset < packet_len-1 ; )
1511 if (p[pcm_offset] == 0x01 && p[pcm_offset+1] == 0x80)
1512 { /* START */
1513 pcm_offset += 2;
1514 break;
1518 es->start = p + pcm_offset;
1519 es->size = packet_len - pcm_offset;
1520 es->type = AUDIO_LPCM_BE;
1521 es->payload_size -= packet_len;
1523 return 1;
1525 else
1527 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PES_PRIVATE1\n");
1528 es->start = p;
1529 es->size = packet_len;
1530 es->type = (type_from_pmt == UNKNOWN ? PES_PRIVATE1 : type_from_pmt);
1531 es->payload_size -= packet_len;
1533 return 1;
1536 else if(((stream_id >= 0xe0) && (stream_id <= 0xef)) || (stream_id == 0xfd && type_from_pmt != UNKNOWN))
1538 es->start = p;
1539 es->size = packet_len;
1540 if(type_from_pmt != UNKNOWN)
1541 es->type = type_from_pmt;
1542 else
1543 es->type = VIDEO_MPEG2;
1544 if(es->payload_size)
1545 es->payload_size -= packet_len;
1547 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: M2V size %d\n", es->size);
1548 return 1;
1550 else if ((stream_id == 0xfa))
1552 int l;
1554 es->is_synced = 0;
1555 if(type_from_pmt != UNKNOWN) //MP4 A/V or SL
1557 es->start = p;
1558 es->size = packet_len;
1559 es->type = type_from_pmt;
1561 if(type_from_pmt == SL_PES_STREAM)
1563 //if(pes_is_aligned)
1565 l = mp4_parse_sl_packet(pmt, p, packet_len, pid, es);
1566 mp_msg(MSGT_DEMUX, MSGL_DBG2, "L=%d, TYPE=%x\n", l, type_from_pmt);
1567 if(l < 0)
1569 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: couldn't parse SL header, passing along full PES payload\n");
1570 l = 0;
1574 es->start += l;
1575 es->size -= l;
1578 if(es->payload_size)
1579 es->payload_size -= packet_len;
1580 return 1;
1583 else if ((stream_id & 0xe0) == 0xc0)
1585 es->start = p;
1586 es->size = packet_len;
1588 if(type_from_pmt != UNKNOWN)
1589 es->type = type_from_pmt;
1590 else
1591 es->type = AUDIO_MP2;
1593 es->payload_size -= packet_len;
1595 return 1;
1597 else if (type_from_pmt != -1) //as a last resort here we trust the PMT, if present
1599 es->start = p;
1600 es->size = packet_len;
1601 es->type = type_from_pmt;
1602 es->payload_size -= packet_len;
1604 return 1;
1606 else
1608 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: unknown packet, id: %x\n", stream_id);
1611 es->is_synced = 0;
1612 return 0;
1618 static int ts_sync(stream_t *stream)
1620 mp_msg(MSGT_DEMUX, MSGL_DBG3, "TS_SYNC \n");
1622 while (!stream->eof)
1623 if (stream_read_char(stream) == 0x47)
1624 return 1;
1626 return 0;
1630 static void ts_dump_streams(ts_priv_t *priv)
1632 int i;
1634 for(i = 0; i < 3; i++)
1636 if((priv->fifo[i].pack != NULL) && (priv->fifo[i].offset != 0))
1638 resize_demux_packet(priv->fifo[i].pack, priv->fifo[i].offset);
1639 ds_add_packet(priv->fifo[i].ds, priv->fifo[i].pack);
1640 priv->fifo[i].offset = 0;
1641 priv->fifo[i].pack = NULL;
1647 static inline int32_t prog_idx_in_pat(ts_priv_t *priv, uint16_t progid)
1649 int x;
1651 if(priv->pat.progs == NULL)
1652 return -1;
1654 for(x = 0; x < priv->pat.progs_cnt; x++)
1656 if(priv->pat.progs[x].id == progid)
1657 return x;
1660 return -1;
1664 static inline int32_t prog_id_in_pat(ts_priv_t *priv, uint16_t pid)
1666 int x;
1668 if(priv->pat.progs == NULL)
1669 return -1;
1671 for(x = 0; x < priv->pat.progs_cnt; x++)
1673 if(priv->pat.progs[x].pmt_pid == pid)
1674 return priv->pat.progs[x].id;
1677 return -1;
1680 static int collect_section(ts_section_t *section, int is_start, unsigned char *buff, int size)
1682 uint8_t *ptr;
1683 uint16_t tlen;
1684 int skip, tid;
1686 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, start: %d, size: %d, collected: %d\n", is_start, size, section->buffer_len);
1687 if(! is_start && !section->buffer_len)
1688 return 0;
1690 if(is_start)
1692 if(! section->buffer)
1694 section->buffer = malloc(4096 + 256);
1695 if(section->buffer == NULL)
1696 return 0;
1698 section->buffer_len = 0;
1701 if(size + section->buffer_len > 4096+256)
1703 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, excessive len: %d + %d\n", section->buffer_len, size);
1704 return 0;
1707 memcpy(&(section->buffer[section->buffer_len]), buff, size);
1708 section->buffer_len += size;
1710 if(section->buffer_len < 3)
1711 return 0;
1713 skip = section->buffer[0];
1714 if(skip + 4 > section->buffer_len)
1715 return 0;
1717 ptr = &(section->buffer[skip + 1]);
1718 tid = ptr[0];
1719 tlen = ((ptr[1] & 0x0f) << 8) | ptr[2];
1720 mp_msg(MSGT_DEMUX, MSGL_V, "SKIP: %d+1, TID: %d, TLEN: %d, COLLECTED: %d\n", skip, tid, tlen, section->buffer_len);
1721 if(section->buffer_len < (skip+1+3+tlen))
1723 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DATA IS NOT ENOUGH, NEXT TIME\n");
1724 return 0;
1727 return skip+1;
1730 static int parse_pat(ts_priv_t * priv, int is_start, unsigned char *buff, int size)
1732 int skip;
1733 unsigned char *ptr;
1734 unsigned char *base;
1735 int entries, i;
1736 uint16_t progid;
1737 struct pat_progs_t *tmp;
1738 ts_section_t *section;
1740 section = &(priv->pat.section);
1741 skip = collect_section(section, is_start, buff, size);
1742 if(! skip)
1743 return 0;
1745 ptr = &(section->buffer[skip]);
1746 //PARSING
1747 priv->pat.table_id = ptr[0];
1748 if(priv->pat.table_id != 0)
1749 return 0;
1750 priv->pat.ssi = (ptr[1] >> 7) & 0x1;
1751 priv->pat.curr_next = ptr[5] & 0x01;
1752 priv->pat.ts_id = (ptr[3] << 8 ) | ptr[4];
1753 priv->pat.version_number = (ptr[5] >> 1) & 0x1F;
1754 priv->pat.section_length = ((ptr[1] & 0x03) << 8 ) | ptr[2];
1755 priv->pat.section_number = ptr[6];
1756 priv->pat.last_section_number = ptr[7];
1758 //check_crc32(0xFFFFFFFFL, ptr, priv->pat.buffer_len - 4, &ptr[priv->pat.buffer_len - 4]);
1759 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);
1761 entries = (int) (priv->pat.section_length - 9) / 4; //entries per section
1763 for(i=0; i < entries; i++)
1765 int32_t idx;
1766 base = &ptr[8 + i*4];
1767 progid = (base[0] << 8) | base[1];
1769 if((idx = prog_idx_in_pat(priv, progid)) == -1)
1771 int sz = sizeof(struct pat_progs_t) * (priv->pat.progs_cnt+1);
1772 tmp = realloc_struct(priv->pat.progs, priv->pat.progs_cnt+1, sizeof(struct pat_progs_t));
1773 if(tmp == NULL)
1775 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PAT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
1776 break;
1778 priv->pat.progs = tmp;
1779 idx = priv->pat.progs_cnt;
1780 priv->pat.progs_cnt++;
1783 priv->pat.progs[idx].id = progid;
1784 priv->pat.progs[idx].pmt_pid = ((base[2] & 0x1F) << 8) | base[3];
1785 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);
1786 mp_msg(MSGT_IDENTIFY, MSGL_V, "PROGRAM_ID=%d (0x%02X), PMT_PID: %d(0x%02X)\n",
1787 progid, progid, priv->pat.progs[idx].pmt_pid, priv->pat.progs[idx].pmt_pid);
1790 return 1;
1794 static inline int32_t es_pid_in_pmt(pmt_t * pmt, uint16_t pid)
1796 uint16_t i;
1798 if(pmt == NULL)
1799 return -1;
1801 if(pmt->es == NULL)
1802 return -1;
1804 for(i = 0; i < pmt->es_cnt; i++)
1806 if(pmt->es[i].pid == pid)
1807 return (int32_t) i;
1810 return -1;
1814 static uint16_t get_mp4_desc_len(uint8_t *buf, int *len)
1816 //uint16_t i = 0, size = 0;
1817 int i = 0, j, size = 0;
1819 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PARSE_MP4_DESC_LEN(%d), bytes: ", *len);
1820 j = FFMIN(*len, 4);
1821 while(i < j)
1823 mp_msg(MSGT_DEMUX, MSGL_DBG2, " %x ", buf[i]);
1824 size |= (buf[i] & 0x7f);
1825 if(!(buf[i] & 0x80))
1826 break;
1827 size <<= 7;
1828 i++;
1830 mp_msg(MSGT_DEMUX, MSGL_DBG2, ", SIZE=%d\n", size);
1832 *len = i+1;
1833 return size;
1837 static uint16_t parse_mp4_slconfig_descriptor(uint8_t *buf, int len, void *elem)
1839 int i = 0;
1840 mp4_es_descr_t *es;
1841 mp4_sl_config_t *sl;
1843 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_SLCONFIG_DESCRIPTOR(%d)\n", len);
1844 es = (mp4_es_descr_t *) elem;
1845 if(!es)
1847 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1848 return len;
1850 sl = &(es->sl);
1852 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;
1853 sl->ocr = sl->dts = sl->cts = 0;
1855 if(buf[0] == 0)
1857 i++;
1858 sl->flags = buf[i];
1859 i++;
1860 sl->ts_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1861 i += 4;
1862 sl->ocr_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1863 i += 4;
1864 sl->ts_len = buf[i];
1865 i++;
1866 sl->ocr_len = buf[i];
1867 i++;
1868 sl->au_len = buf[i];
1869 i++;
1870 sl->instant_bitrate_len = buf[i];
1871 i++;
1872 sl->degr_len = (buf[i] >> 4) & 0x0f;
1873 sl->au_seqnum_len = ((buf[i] & 0x0f) << 1) | ((buf[i+1] >> 7) & 0x01);
1874 i++;
1875 sl->packet_seqnum_len = ((buf[i] >> 2) & 0x1f);
1876 i++;
1879 else if(buf[0] == 1)
1881 sl->flags = 0;
1882 sl->ts_resolution = 1000;
1883 sl->ts_len = 32;
1884 i++;
1886 else if(buf[0] == 2)
1888 sl->flags = 4;
1889 i++;
1891 else
1893 sl->flags = 0;
1894 i++;
1897 sl->au_start = (sl->flags >> 7) & 0x1;
1898 sl->au_end = (sl->flags >> 6) & 0x1;
1899 sl->random_accesspoint = (sl->flags >> 5) & 0x1;
1900 sl->random_accesspoint_only = (sl->flags >> 4) & 0x1;
1901 sl->padding = (sl->flags >> 3) & 0x1;
1902 sl->use_ts = (sl->flags >> 2) & 0x1;
1903 sl->idle = (sl->flags >> 1) & 0x1;
1904 sl->duration = sl->flags & 0x1;
1906 if(sl->duration)
1908 sl->timescale = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1909 i += 4;
1910 sl->au_duration = (buf[i] << 8) | buf[i+1];
1911 i += 2;
1912 sl->cts_duration = (buf[i] << 8) | buf[i+1];
1913 i += 2;
1915 else //no support for fixed durations atm
1916 sl->timescale = sl->au_duration = sl->cts_duration = 0;
1918 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",
1919 len, buf[0], sl->flags, sl->use_ts, sl->ts_len, sl->timescale, (uint64_t) sl->dts, (uint64_t) sl->cts);
1921 return len;
1924 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem);
1926 static uint16_t parse_mp4_decoder_config_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
1928 int i = 0, j;
1929 mp4_es_descr_t *es;
1930 mp4_decoder_config_t *dec;
1932 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_CONFIG_DESCRIPTOR(%d)\n", len);
1933 es = (mp4_es_descr_t *) elem;
1934 if(!es)
1936 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1937 return len;
1939 dec = (mp4_decoder_config_t*) &(es->decoder);
1941 dec->object_type = buf[i];
1942 dec->stream_type = (buf[i+1]>>2) & 0x3f;
1944 if(dec->object_type == 1 && dec->stream_type == 1)
1946 dec->object_type = MP4_OD;
1947 dec->stream_type = MP4_OD;
1949 else if(dec->stream_type == 4)
1951 if(dec->object_type == 0x6a)
1952 dec->object_type = VIDEO_MPEG1;
1953 if(dec->object_type >= 0x60 && dec->object_type <= 0x65)
1954 dec->object_type = VIDEO_MPEG2;
1955 else if(dec->object_type == 0x20)
1956 dec->object_type = VIDEO_MPEG4;
1957 else if(dec->object_type == 0x21)
1958 dec->object_type = VIDEO_AVC;
1959 /*else if(dec->object_type == 0x22)
1960 fprintf(stderr, "TYPE 0x22\n");*/
1961 else dec->object_type = UNKNOWN;
1963 else if(dec->stream_type == 5)
1965 if(dec->object_type == 0x40)
1966 dec->object_type = AUDIO_AAC;
1967 else if(dec->object_type == 0x6b)
1968 dec->object_type = AUDIO_MP2;
1969 else if(dec->object_type >= 0x66 && dec->object_type <= 0x69)
1970 dec->object_type = AUDIO_MP2;
1971 else
1972 dec->object_type = UNKNOWN;
1974 else
1975 dec->object_type = dec->stream_type = UNKNOWN;
1977 if(dec->object_type != UNKNOWN)
1979 //update the type of the current stream
1980 for(j = 0; j < pmt->es_cnt; j++)
1982 if(pmt->es[j].mp4_es_id == es->id)
1984 pmt->es[j].type = SL_PES_STREAM;
1989 if(len > 13)
1990 parse_mp4_descriptors(pmt, &buf[13], len-13, dec);
1992 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);
1994 return len;
1997 static uint16_t parse_mp4_decoder_specific_descriptor(uint8_t *buf, int len, void *elem)
1999 int i;
2000 mp4_decoder_config_t *dec;
2002 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_SPECIFIC_DESCRIPTOR(%d)\n", len);
2003 dec = (mp4_decoder_config_t *) elem;
2004 if(!dec)
2006 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
2007 return len;
2010 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4 SPECIFIC INFO BYTES: \n");
2011 for(i=0; i<len; i++)
2012 mp_msg(MSGT_DEMUX, MSGL_DBG2, "%02x ", buf[i]);
2013 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\n");
2015 if(len > MAX_EXTRADATA_SIZE)
2017 mp_msg(MSGT_DEMUX, MSGL_ERR, "DEMUX_TS, EXTRADATA SUSPICIOUSLY BIG: %d, REFUSED\r\n", len);
2018 return len;
2020 memcpy(dec->buf, buf, len);
2021 dec->buf_size = len;
2023 return len;
2026 static uint16_t parse_mp4_es_descriptor(pmt_t *pmt, uint8_t *buf, int len)
2028 int i = 0, j = 0, k, found;
2029 uint8_t flag;
2030 mp4_es_descr_t es, *target_es = NULL, *tmp;
2032 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4ES: len=%d\n", len);
2033 memset(&es, 0, sizeof(mp4_es_descr_t));
2034 while(i < len)
2036 es.id = (buf[i] << 8) | buf[i+1];
2037 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_ID: %d\n", es.id);
2038 i += 2;
2039 flag = buf[i];
2040 i++;
2041 if(flag & 0x80)
2042 i += 2;
2043 if(flag & 0x40)
2044 i += buf[i]+1;
2045 if(flag & 0x20) //OCR, maybe we need it
2046 i += 2;
2048 j = parse_mp4_descriptors(pmt, &buf[i], len-i, &es);
2049 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);
2050 if(es.decoder.object_type != UNKNOWN && es.decoder.stream_type != UNKNOWN)
2052 found = 0;
2053 //search this ES_ID if we already have it
2054 for(k=0; k < pmt->mp4es_cnt; k++)
2056 if(pmt->mp4es[k].id == es.id)
2058 target_es = &(pmt->mp4es[k]);
2059 found = 1;
2063 if(! found)
2065 tmp = realloc_struct(pmt->mp4es, pmt->mp4es_cnt+1, sizeof(mp4_es_descr_t));
2066 if(tmp == NULL)
2068 fprintf(stderr, "CAN'T REALLOC MP4_ES_DESCR\n");
2069 continue;
2071 pmt->mp4es = tmp;
2072 target_es = &(pmt->mp4es[pmt->mp4es_cnt]);
2073 pmt->mp4es_cnt++;
2075 memcpy(target_es, &es, sizeof(mp4_es_descr_t));
2076 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_CNT: %d, ID=%d\n", pmt->mp4es_cnt, target_es->id);
2079 i += j;
2082 return len;
2085 static void parse_mp4_object_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2087 int i, j = 0, id;
2089 i=0;
2090 id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2091 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_OBJECT_DESCRIPTOR: len=%d, OD_ID=%d\n", len, id);
2092 if(buf[1] & 0x20)
2094 i += buf[2] + 1; //url
2095 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2097 else
2099 i = 2;
2101 while(i < len)
2103 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2104 mp_msg(MSGT_DEMUX, MSGL_V, "OBJD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2105 i += j;
2111 static void parse_mp4_iod(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2113 int i, j = 0;
2114 mp4_od_t *iod = &(pmt->iod);
2116 iod->id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2117 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_IOD: len=%d, IOD_ID=%d\n", len, iod->id);
2118 i = 2;
2119 if(buf[1] & 0x20)
2121 i += buf[2] + 1; //url
2122 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2124 else
2126 i = 7;
2127 while(i < len)
2129 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2130 mp_msg(MSGT_DEMUX, MSGL_V, "IOD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2131 i += j;
2136 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2138 int tag, descr_len, i = 0, j = 0;
2140 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DESCRIPTORS, len=%d\n", len);
2141 if(! len)
2142 return len;
2144 while(i < len)
2146 tag = buf[i];
2147 j = len - i -1;
2148 descr_len = get_mp4_desc_len(&(buf[i+1]), &j);
2149 mp_msg(MSGT_DEMUX, MSGL_V, "TAG=%d (0x%x), DESCR_len=%d, len=%d, j=%d\n", tag, tag, descr_len, len, j);
2150 if(descr_len > len - j+1)
2152 mp_msg(MSGT_DEMUX, MSGL_V, "descriptor is too long, exit\n");
2153 return len;
2155 i += j+1;
2157 switch(tag)
2159 case 0x1:
2160 parse_mp4_object_descriptor(pmt, &(buf[i]), descr_len, elem);
2161 break;
2162 case 0x2:
2163 parse_mp4_iod(pmt, &(buf[i]), descr_len, elem);
2164 break;
2165 case 0x3:
2166 parse_mp4_es_descriptor(pmt, &(buf[i]), descr_len);
2167 break;
2168 case 0x4:
2169 parse_mp4_decoder_config_descriptor(pmt, &buf[i], descr_len, elem);
2170 break;
2171 case 0x05:
2172 parse_mp4_decoder_specific_descriptor(&buf[i], descr_len, elem);
2173 break;
2174 case 0x6:
2175 parse_mp4_slconfig_descriptor(&buf[i], descr_len, elem);
2176 break;
2177 default:
2178 mp_msg(MSGT_DEMUX, MSGL_V, "Unsupported mp4 descriptor 0x%x\n", tag);
2180 i += descr_len;
2183 return len;
2186 static ES_stream_t *new_pid(ts_priv_t *priv, int pid)
2188 ES_stream_t *tss;
2190 tss = malloc(sizeof(ES_stream_t));
2191 if(! tss)
2192 return NULL;
2193 memset(tss, 0, sizeof(ES_stream_t));
2194 tss->pid = pid;
2195 tss->last_cc = -1;
2196 tss->type = UNKNOWN;
2197 tss->subtype = UNKNOWN;
2198 tss->is_synced = 0;
2199 tss->extradata = NULL;
2200 tss->extradata_alloc = tss->extradata_len = 0;
2201 priv->ts.pids[pid] = tss;
2203 return tss;
2207 static int parse_program_descriptors(pmt_t *pmt, uint8_t *buf, uint16_t len)
2209 uint16_t i = 0, k, olen = len;
2211 while(len > 0)
2213 mp_msg(MSGT_DEMUX, MSGL_V, "PROG DESCR, TAG=%x, LEN=%d(%x)\n", buf[i], buf[i+1], buf[i+1]);
2214 if(buf[i+1] > len-2)
2216 mp_msg(MSGT_DEMUX, MSGL_V, "ERROR, descriptor len is too long, skipping\n");
2217 return olen;
2220 if(buf[i] == 0x1d)
2222 if(buf[i+3] == 2) //buggy versions of vlc muxer make this non-standard mess (missing iod_scope)
2223 k = 3;
2224 else
2225 k = 4; //this is standard compliant
2226 parse_mp4_descriptors(pmt, &buf[i+k], (int) buf[i+1]-(k-2), NULL);
2229 len -= 2 + buf[i+1];
2232 return olen;
2235 static int parse_descriptors(struct pmt_es_t *es, uint8_t *ptr)
2237 int j, descr_len, len;
2239 j = 0;
2240 len = es->descr_length;
2241 while(len > 2)
2243 descr_len = ptr[j+1];
2244 mp_msg(MSGT_DEMUX, MSGL_V, "...descr id: 0x%x, len=%d\n", ptr[j], descr_len);
2245 if(descr_len > len)
2247 mp_msg(MSGT_DEMUX, MSGL_ERR, "INVALID DESCR LEN for tag %02x: %d vs %d max, EXIT LOOP\n", ptr[j], descr_len, len);
2248 return -1;
2252 if(ptr[j] == 0x6a || ptr[j] == 0x7a) //A52 Descriptor
2254 if(es->type == 0x6)
2256 es->type = AUDIO_A52;
2257 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB A52 Descriptor\n");
2260 else if(ptr[j] == 0x7b) //DVB DTS Descriptor
2262 if(es->type == 0x6)
2264 es->type = AUDIO_DTS;
2265 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB DTS Descriptor\n");
2268 else if(ptr[j] == 0x56) // Teletext
2270 if(descr_len >= 5) {
2271 memcpy(es->lang, ptr+2, 3);
2272 es->lang[3] = 0;
2274 es->type = SPU_TELETEXT;
2276 else if(ptr[j] == 0x59) //Subtitling Descriptor
2278 uint8_t subtype;
2280 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Subtitling Descriptor\n");
2281 if(descr_len < 8)
2283 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Descriptor length too short for DVB Subtitle Descriptor: %d, SKIPPING\n", descr_len);
2285 else
2287 memcpy(es->lang, &ptr[j+2], 3);
2288 es->lang[3] = 0;
2289 subtype = ptr[j+5];
2291 (subtype >= 0x10 && subtype <= 0x13) ||
2292 (subtype >= 0x20 && subtype <= 0x23)
2295 es->type = SPU_DVB;
2296 //page parameters: compo page 2 bytes, ancillary page 2 bytes
2298 else
2299 es->type = UNKNOWN;
2302 else if(ptr[j] == 0x50) //Component Descriptor
2304 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Component Descriptor\n");
2305 memcpy(es->lang, &ptr[j+5], 3);
2306 es->lang[3] = 0;
2308 else if(ptr[j] == 0xa) //Language Descriptor
2310 memcpy(es->lang, &ptr[j+2], 3);
2311 es->lang[3] = 0;
2312 mp_msg(MSGT_DEMUX, MSGL_V, "Language Descriptor: %s\n", es->lang);
2314 else if(ptr[j] == 0x5) //Registration Descriptor (looks like e fourCC :) )
2316 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor\n");
2317 if(descr_len < 4)
2319 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor length too short: %d, SKIPPING\n", descr_len);
2321 else
2323 char *d;
2324 memcpy(es->format_descriptor, &ptr[j+2], 4);
2325 es->format_descriptor[4] = 0;
2327 d = &ptr[j+2];
2328 if(d[0] == 'A' && d[1] == 'C' && d[2] == '-' && d[3] == '3')
2330 es->type = AUDIO_A52;
2332 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '1')
2334 es->type = AUDIO_DTS;
2336 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '2')
2338 es->type = AUDIO_DTS;
2340 else if(d[0] == 'V' && d[1] == 'C' && d[2] == '-' && d[3] == '1')
2342 es->type = VIDEO_VC1;
2344 else if(d[0] == 'd' && d[1] == 'r' && d[2] == 'a' && d[3] == 'c')
2346 es->type = VIDEO_DIRAC;
2348 else
2349 es->type = UNKNOWN;
2350 mp_msg(MSGT_DEMUX, MSGL_DBG2, "FORMAT %s\n", es->format_descriptor);
2353 else if(ptr[j] == 0x1e)
2355 es->mp4_es_id = (ptr[j+2] << 8) | ptr[j+3];
2356 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);
2358 else
2359 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Unknown descriptor 0x%x, SKIPPING\n", ptr[j]);
2361 len -= 2 + descr_len;
2362 j += 2 + descr_len;
2365 return 1;
2368 static int parse_sl_section(pmt_t *pmt, ts_section_t *section, int is_start, unsigned char *buff, int size)
2370 int tid, len, skip;
2371 uint8_t *ptr;
2372 skip = collect_section(section, is_start, buff, size);
2373 if(! skip)
2374 return 0;
2376 ptr = &(section->buffer[skip]);
2377 tid = ptr[0];
2378 len = ((ptr[1] & 0x0f) << 8) | ptr[2];
2379 mp_msg(MSGT_DEMUX, MSGL_V, "TABLEID: %d (av. %d), skip=%d, LEN: %d\n", tid, section->buffer_len, skip, len);
2380 if(len > 4093 || section->buffer_len < len || tid != 5)
2382 mp_msg(MSGT_DEMUX, MSGL_V, "SECTION TOO LARGE or wrong section type, EXIT\n");
2383 return 0;
2386 if(! (ptr[5] & 1))
2387 return 0;
2389 //8 is the current position, len - 9 is the amount of data available
2390 parse_mp4_descriptors(pmt, &ptr[8], len - 9, NULL);
2392 return 1;
2395 static int parse_pmt(ts_priv_t * priv, uint16_t progid, uint16_t pid, int is_start, unsigned char *buff, int size)
2397 unsigned char *base, *es_base;
2398 pmt_t *pmt;
2399 int32_t idx, es_count, section_bytes;
2400 uint8_t m=0;
2401 int skip;
2402 pmt_t *tmp;
2403 struct pmt_es_t *tmp_es;
2404 ts_section_t *section;
2405 ES_stream_t *tss;
2407 idx = progid_idx_in_pmt(priv, progid);
2409 if(idx == -1)
2411 int sz = (priv->pmt_cnt + 1) * sizeof(pmt_t);
2412 tmp = realloc_struct(priv->pmt, priv->pmt_cnt + 1, sizeof(pmt_t));
2413 if(tmp == NULL)
2415 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
2416 return 0;
2418 priv->pmt = tmp;
2419 idx = priv->pmt_cnt;
2420 memset(&(priv->pmt[idx]), 0, sizeof(pmt_t));
2421 priv->pmt_cnt++;
2422 priv->pmt[idx].progid = progid;
2425 pmt = &(priv->pmt[idx]);
2427 section = &(pmt->section);
2428 skip = collect_section(section, is_start, buff, size);
2429 if(! skip)
2430 return 0;
2432 base = &(section->buffer[skip]);
2434 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",
2435 progid, pmt->section.buffer_len, is_start, pid, size, m, pmt->es_cnt, idx, pmt);
2437 pmt->table_id = base[0];
2438 if(pmt->table_id != 2)
2439 return -1;
2440 pmt->ssi = base[1] & 0x80;
2441 pmt->section_length = (((base[1] & 0xf) << 8 ) | base[2]);
2442 pmt->version_number = (base[5] >> 1) & 0x1f;
2443 pmt->curr_next = (base[5] & 1);
2444 pmt->section_number = base[6];
2445 pmt->last_section_number = base[7];
2446 pmt->PCR_PID = ((base[8] & 0x1f) << 8 ) | base[9];
2447 pmt->prog_descr_length = ((base[10] & 0xf) << 8 ) | base[11];
2448 if(pmt->prog_descr_length > pmt->section_length - 9)
2450 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, INVALID PROG_DESCR LENGTH (%d vs %d)\n", pmt->prog_descr_length, pmt->section_length - 9);
2451 return -1;
2454 if(pmt->prog_descr_length)
2455 parse_program_descriptors(pmt, &base[12], pmt->prog_descr_length);
2457 es_base = &base[12 + pmt->prog_descr_length]; //the beginning of th ES loop
2459 section_bytes= pmt->section_length - 13 - pmt->prog_descr_length;
2460 es_count = 0;
2462 while(section_bytes >= 5)
2464 int es_pid, es_type;
2466 es_type = es_base[0];
2467 es_pid = ((es_base[1] & 0x1f) << 8) | es_base[2];
2469 idx = es_pid_in_pmt(pmt, es_pid);
2470 if(idx == -1)
2472 int sz = sizeof(struct pmt_es_t) * (pmt->es_cnt + 1);
2473 tmp_es = realloc_struct(pmt->es, pmt->es_cnt + 1, sizeof(struct pmt_es_t));
2474 if(tmp_es == NULL)
2476 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT, COULDN'T ALLOCATE %d bytes for PMT_ES\n", sz);
2477 continue;
2479 pmt->es = tmp_es;
2480 idx = pmt->es_cnt;
2481 memset(&(pmt->es[idx]), 0, sizeof(struct pmt_es_t));
2482 pmt->es_cnt++;
2485 pmt->es[idx].descr_length = ((es_base[3] & 0xf) << 8) | es_base[4];
2488 if(pmt->es[idx].descr_length > section_bytes - 5)
2490 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, ES_DESCR_LENGTH TOO LARGE %d > %d, EXIT\n",
2491 pmt->es[idx].descr_length, section_bytes - 5);
2492 return -1;
2496 pmt->es[idx].pid = es_pid;
2497 if(es_type != 0x6)
2498 pmt->es[idx].type = UNKNOWN;
2499 else
2500 pmt->es[idx].type = es_type;
2502 parse_descriptors(&pmt->es[idx], &es_base[5]);
2504 switch(es_type)
2506 case 1:
2507 pmt->es[idx].type = VIDEO_MPEG1;
2508 break;
2509 case 2:
2510 pmt->es[idx].type = VIDEO_MPEG2;
2511 break;
2512 case 3:
2513 case 4:
2514 pmt->es[idx].type = AUDIO_MP2;
2515 break;
2516 case 6:
2517 if(pmt->es[idx].type == 0x6) //this could have been ovrwritten by parse_descriptors
2518 pmt->es[idx].type = UNKNOWN;
2519 break;
2520 case 0x10:
2521 pmt->es[idx].type = VIDEO_MPEG4;
2522 break;
2523 case 0x0f:
2524 pmt->es[idx].type = AUDIO_AAC;
2525 break;
2526 case 0x11:
2527 pmt->es[idx].type = AUDIO_AAC_LATM;
2528 break;
2529 case 0x1b:
2530 pmt->es[idx].type = VIDEO_H264;
2531 break;
2532 case 0x12:
2533 pmt->es[idx].type = SL_PES_STREAM;
2534 break;
2535 case 0x13:
2536 pmt->es[idx].type = SL_SECTION;
2537 break;
2538 case 0x81:
2539 pmt->es[idx].type = AUDIO_A52;
2540 break;
2541 case 0x8A:
2542 case 0x82:
2543 case 0x85:
2544 case 0x86:
2545 pmt->es[idx].type = AUDIO_DTS;
2546 break;
2547 case 0x90:
2548 pmt->es[idx].type = SPU_PGS;
2549 break;
2550 case 0xD1:
2551 pmt->es[idx].type = VIDEO_DIRAC;
2552 break;
2553 case 0xEA:
2554 pmt->es[idx].type = VIDEO_VC1;
2555 break;
2556 default:
2557 mp_msg(MSGT_DEMUX, MSGL_DBG2, "UNKNOWN ES TYPE=0x%x\n", es_type);
2558 pmt->es[idx].type = UNKNOWN;
2561 tss = priv->ts.pids[es_pid]; //an ES stream
2562 if(tss == NULL)
2564 tss = new_pid(priv, es_pid);
2565 if(tss)
2566 tss->type = pmt->es[idx].type;
2569 section_bytes -= 5 + pmt->es[idx].descr_length;
2570 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",
2571 progid, idx, es_count, pmt->es[idx].pid, pmt->es[idx].pid, pmt->es[idx].type, pmt->es[idx].descr_length, section_bytes);
2574 es_base += 5 + pmt->es[idx].descr_length;
2576 es_count++;
2579 mp_msg(MSGT_DEMUX, MSGL_V, "----------------------------\n");
2580 return 1;
2583 static pmt_t* pmt_of_pid(ts_priv_t *priv, int pid, mp4_decoder_config_t **mp4_dec)
2585 int32_t i, j, k;
2587 if(priv->pmt)
2589 for(i = 0; i < priv->pmt_cnt; i++)
2591 if(priv->pmt[i].es && priv->pmt[i].es_cnt)
2593 for(j = 0; j < priv->pmt[i].es_cnt; j++)
2595 if(priv->pmt[i].es[j].pid == pid)
2597 //search mp4_es_id
2598 if(priv->pmt[i].es[j].mp4_es_id)
2600 for(k = 0; k < priv->pmt[i].mp4es_cnt; k++)
2602 if(priv->pmt[i].mp4es[k].id == priv->pmt[i].es[j].mp4_es_id)
2604 *mp4_dec = &(priv->pmt[i].mp4es[k].decoder);
2605 break;
2610 return &(priv->pmt[i]);
2617 return NULL;
2621 static inline int32_t pid_type_from_pmt(ts_priv_t *priv, int pid)
2623 int32_t pmt_idx, pid_idx, i, j;
2625 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2627 if(pmt_idx != -1)
2629 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2630 if(pid_idx != -1)
2631 return priv->pmt[pmt_idx].es[pid_idx].type;
2633 //else
2635 for(i = 0; i < priv->pmt_cnt; i++)
2637 pmt_t *pmt = &(priv->pmt[i]);
2638 for(j = 0; j < pmt->es_cnt; j++)
2639 if(pmt->es[j].pid == pid)
2640 return pmt->es[j].type;
2644 return UNKNOWN;
2648 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid)
2650 int32_t pmt_idx, pid_idx, i, j;
2652 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2654 if(pmt_idx != -1)
2656 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2657 if(pid_idx != -1)
2658 return priv->pmt[pmt_idx].es[pid_idx].lang;
2660 else
2662 for(i = 0; i < priv->pmt_cnt; i++)
2664 pmt_t *pmt = &(priv->pmt[i]);
2665 for(j = 0; j < pmt->es_cnt; j++)
2666 if(pmt->es[j].pid == pid)
2667 return pmt->es[j].lang;
2671 return NULL;
2675 static int fill_packet(demuxer_t *demuxer, demux_stream_t *ds, demux_packet_t **dp, int *dp_offset, TS_stream_info *si)
2677 int ret = 0;
2679 if((*dp != NULL) && (*dp_offset > 0))
2681 ret = *dp_offset;
2682 resize_demux_packet(*dp, ret); //shrinked to the right size
2683 ds_add_packet(ds, *dp);
2684 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);
2685 if(si)
2687 float diff = (*dp)->pts - si->last_pts;
2688 float dur;
2690 if(abs(diff) > 1) //1 second, there's a discontinuity
2692 si->duration += si->last_pts - si->first_pts;
2693 si->first_pts = si->last_pts = (*dp)->pts;
2695 else
2697 si->last_pts = (*dp)->pts;
2699 si->size += ret;
2700 dur = si->duration + (si->last_pts - si->first_pts);
2702 if(dur > 0 && ds == demuxer->video)
2704 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2705 if(dur > 1) //otherwise it may be unreliable
2706 priv->vbitrate = (uint32_t) ((float) si->size / dur);
2711 *dp = NULL;
2712 *dp_offset = 0;
2714 return ret;
2717 static int fill_extradata(mp4_decoder_config_t * mp4_dec, ES_stream_t *tss)
2719 uint8_t *tmp;
2721 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4_dec: %p, pid: %d\n", mp4_dec, tss->pid);
2723 if(mp4_dec->buf_size > tss->extradata_alloc)
2725 tmp = realloc(tss->extradata, mp4_dec->buf_size);
2726 if(!tmp)
2727 return 0;
2728 tss->extradata = tmp;
2729 tss->extradata_alloc = mp4_dec->buf_size;
2731 memcpy(tss->extradata, mp4_dec->buf, mp4_dec->buf_size);
2732 tss->extradata_len = mp4_dec->buf_size;
2733 mp_msg(MSGT_DEMUX, MSGL_V, "EXTRADATA: %p, alloc=%d, len=%d\n", tss->extradata, tss->extradata_alloc, tss->extradata_len);
2735 return tss->extradata_len;
2738 // 0 = EOF or no stream found
2739 // else = [-] number of bytes written to the packet
2740 static int ts_parse(demuxer_t *demuxer , ES_stream_t *es, unsigned char *packet, int probe)
2742 ES_stream_t *tss;
2743 int buf_size, is_start, pid, base;
2744 int len, cc, cc_ok, afc, retv = 0, is_video, is_audio, is_sub;
2745 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2746 stream_t *stream = demuxer->stream;
2747 char *p;
2748 demux_stream_t *ds = NULL;
2749 demux_packet_t **dp = NULL;
2750 int *dp_offset = 0, *buffer_size = 0;
2751 int32_t progid, pid_type, bad, ts_error;
2752 int junk = 0, rap_flag = 0;
2753 pmt_t *pmt;
2754 mp4_decoder_config_t *mp4_dec;
2755 TS_stream_info *si;
2758 while(1)
2760 bad = ts_error = 0;
2761 ds = NULL;
2762 dp = NULL;
2763 dp_offset = buffer_size = NULL;
2764 rap_flag = 0;
2765 mp4_dec = NULL;
2766 es->is_synced = 0;
2767 es->lang[0] = 0;
2768 si = NULL;
2770 junk = priv->ts.packet_size - TS_PACKET_SIZE;
2771 buf_size = priv->ts.packet_size - junk;
2773 if(stream_eof(stream))
2775 if(! probe)
2777 ts_dump_streams(priv);
2778 demuxer->filepos = stream_tell(demuxer->stream);
2781 return 0;
2785 if(! ts_sync(stream))
2787 mp_msg(MSGT_DEMUX, MSGL_INFO, "TS_PARSE: COULDN'T SYNC\n");
2788 return 0;
2791 len = stream_read(stream, &packet[1], 3);
2792 if (len != 3)
2793 return 0;
2794 buf_size -= 4;
2796 if((packet[1] >> 7) & 0x01) //transport error
2797 ts_error = 1;
2800 is_start = packet[1] & 0x40;
2801 pid = ((packet[1] & 0x1f) << 8) | packet[2];
2803 tss = priv->ts.pids[pid]; //an ES stream
2804 if(tss == NULL)
2806 tss = new_pid(priv, pid);
2807 if(tss == NULL)
2808 continue;
2811 cc = (packet[3] & 0xf);
2812 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
2813 tss->last_cc = cc;
2815 bad = ts_error; // || (! cc_ok);
2816 if(bad)
2818 if(priv->keep_broken == 0)
2820 stream_skip(stream, buf_size-1+junk);
2821 continue;
2824 is_start = 0; //queued to the packet data
2827 if(is_start)
2828 tss->is_synced = 1;
2830 if((!is_start && !tss->is_synced) || ((pid > 1) && (pid < 16)) || (pid == 8191)) //invalid pid
2832 stream_skip(stream, buf_size-1+junk);
2833 continue;
2837 afc = (packet[3] >> 4) & 3;
2838 if(! (afc % 2)) //no payload in this TS packet
2840 stream_skip(stream, buf_size-1+junk);
2841 continue;
2844 if(afc > 1)
2846 int c;
2847 c = stream_read_char(stream);
2848 buf_size--;
2849 if(c < 0 || c > 183) //broken from the stream layer or invalid
2851 stream_skip(stream, buf_size-1+junk);
2852 continue;
2855 //c==0 is allowed!
2856 if(c > 0)
2858 uint8_t pcrbuf[188];
2859 int flags = stream_read_char(stream);
2860 int has_pcr;
2861 rap_flag = (flags & 0x40) >> 6;
2862 has_pcr = flags & 0x10;
2864 buf_size--;
2865 c--;
2866 stream_read(stream, pcrbuf, c);
2868 if(has_pcr)
2870 int pcr_pid = prog_pcr_pid(priv, priv->prog);
2871 if(pcr_pid == pid)
2873 uint64_t pcr, pcr_ext;
2875 pcr = (int64_t)(pcrbuf[0]) << 25;
2876 pcr |= pcrbuf[1] << 17 ;
2877 pcr |= (pcrbuf[2]) << 9;
2878 pcr |= pcrbuf[3] << 1 ;
2879 pcr |= (pcrbuf[4] & 0x80) >> 7;
2881 pcr_ext = (pcrbuf[4] & 0x01) << 8;
2882 pcr_ext |= pcrbuf[5];
2884 pcr = pcr * 300 + pcr_ext;
2886 demuxer->reference_clock = (double)pcr/(double)27000000.0;
2890 buf_size -= c;
2891 if(buf_size == 0)
2892 continue;
2896 //find the program that the pid belongs to; if (it's the right one or -1) && pid_type==SL_SECTION
2897 //call parse_sl_section()
2898 pmt = pmt_of_pid(priv, pid, &mp4_dec);
2899 if(mp4_dec)
2901 fill_extradata(mp4_dec, tss);
2902 if(IS_VIDEO(mp4_dec->object_type) || IS_AUDIO(mp4_dec->object_type))
2904 tss->type = SL_PES_STREAM;
2905 tss->subtype = mp4_dec->object_type;
2910 //TABLE PARSING
2912 base = priv->ts.packet_size - buf_size;
2914 priv->last_pid = pid;
2916 is_video = IS_VIDEO(tss->type) || (tss->type==SL_PES_STREAM && IS_VIDEO(tss->subtype));
2917 is_audio = IS_AUDIO(tss->type) || (tss->type==SL_PES_STREAM && IS_AUDIO(tss->subtype)) || (tss->type == PES_PRIVATE1);
2918 is_sub = IS_SUB(tss->type);
2919 pid_type = pid_type_from_pmt(priv, pid);
2921 // PES CONTENT STARTS HERE
2922 if(! probe)
2924 if((is_video || is_audio || is_sub) && is_start)
2925 ts_add_stream(demuxer, tss);
2927 if(is_video && (demuxer->video->id == priv->ts.streams[pid].id))
2929 ds = demuxer->video;
2931 dp = &priv->fifo[1].pack;
2932 dp_offset = &priv->fifo[1].offset;
2933 buffer_size = &priv->fifo[1].buffer_size;
2934 si = &priv->vstr;
2936 else if(is_audio && (demuxer->audio->id == priv->ts.streams[pid].id))
2938 ds = demuxer->audio;
2940 dp = &priv->fifo[0].pack;
2941 dp_offset = &priv->fifo[0].offset;
2942 buffer_size = &priv->fifo[0].buffer_size;
2943 si = &priv->astr;
2945 else if(is_sub)
2947 sh_sub_t *sh_sub = demuxer->sub->sh;
2949 if(sh_sub && sh_sub->sid == tss->pid)
2951 ds = demuxer->sub;
2953 dp = &priv->fifo[2].pack;
2954 dp_offset = &priv->fifo[2].offset;
2955 buffer_size = &priv->fifo[2].buffer_size;
2957 else
2959 stream_skip(stream, buf_size+junk);
2960 continue;
2964 //IS IT TIME TO QUEUE DATA to the dp_packet?
2965 if(is_start && (dp != NULL))
2967 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
2971 if(dp && *dp == NULL)
2973 if(*buffer_size > MAX_PACK_BYTES)
2974 *buffer_size = MAX_PACK_BYTES;
2975 *dp = new_demux_packet(*buffer_size); //es->size
2976 *dp_offset = 0;
2977 if(! *dp)
2979 fprintf(stderr, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", *buffer_size);
2980 continue;
2982 mp_msg(MSGT_DEMUX, MSGL_DBG2, "CREATED DP(%d)\n", *buffer_size);
2987 if(probe || !dp) //dp is NULL for tables and sections
2989 p = &packet[base];
2991 else //feeding
2993 if(*dp_offset + buf_size > *buffer_size)
2995 *buffer_size = *dp_offset + buf_size + TS_FEC_PACKET_SIZE;
2996 resize_demux_packet(*dp, *buffer_size);
2998 p = &((*dp)->buffer[*dp_offset]);
3001 len = stream_read(stream, p, buf_size);
3002 if(len < buf_size)
3004 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\r\nts_parse() couldn't read enough data: %d < %d\r\n", len, buf_size);
3005 continue;
3007 stream_skip(stream, junk);
3009 if(pid == 0)
3011 parse_pat(priv, is_start, p, buf_size);
3012 continue;
3014 else if((tss->type == SL_SECTION) && pmt)
3016 int k, mp4_es_id = -1;
3017 ts_section_t *section;
3018 for(k = 0; k < pmt->mp4es_cnt; k++)
3020 if(pmt->mp4es[k].decoder.object_type == MP4_OD && pmt->mp4es[k].decoder.stream_type == MP4_OD)
3021 mp4_es_id = pmt->mp4es[k].id;
3023 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4ESID: %d\n", mp4_es_id);
3024 for(k = 0; k < pmt->es_cnt; k++)
3026 if(pmt->es[k].mp4_es_id == mp4_es_id)
3028 section = &(tss->section);
3029 parse_sl_section(pmt, section, is_start, &packet[base], buf_size);
3032 continue;
3034 else
3036 progid = prog_id_in_pat(priv, pid);
3037 if(progid != -1)
3039 if(pid != demuxer->video->id && pid != demuxer->audio->id && pid != demuxer->sub->id)
3041 parse_pmt(priv, progid, pid, is_start, &packet[base], buf_size);
3042 continue;
3044 else
3045 mp_msg(MSGT_DEMUX, MSGL_ERR, "Argh! Data pid %d used in the PMT, Skipping PMT parsing!\n", pid);
3049 if(!probe && !dp)
3050 continue;
3052 if(is_start)
3054 uint8_t *lang = NULL;
3056 mp_msg(MSGT_DEMUX, MSGL_DBG2, "IS_START\n");
3058 len = pes_parse2(p, buf_size, es, pid_type, pmt, pid);
3059 if(! len)
3061 tss->is_synced = 0;
3062 continue;
3064 es->pid = tss->pid;
3065 tss->is_synced |= es->is_synced || rap_flag;
3066 tss->payload_size = es->payload_size;
3068 if((is_sub || is_audio) && (lang = pid_lang_from_pmt(priv, es->pid)))
3070 memcpy(es->lang, lang, 3);
3071 es->lang[3] = 0;
3073 else
3074 es->lang[0] = 0;
3076 if(probe)
3078 if(es->type == UNKNOWN)
3079 return 0;
3081 tss->type = es->type;
3082 tss->subtype = es->subtype;
3084 return 1;
3086 else
3088 if(es->pts == 0.0)
3089 es->pts = tss->pts = tss->last_pts;
3090 else
3091 tss->pts = tss->last_pts = es->pts;
3093 mp_msg(MSGT_DEMUX, MSGL_DBG2, "ts_parse, NEW pid=%d, PSIZE: %u, type=%X, start=%p, len=%d\n",
3094 es->pid, es->payload_size, es->type, es->start, es->size);
3096 demuxer->filepos = stream_tell(demuxer->stream) - es->size;
3098 if(es->size < 0 || es->size > buf_size) {
3099 mp_msg(MSGT_DEMUX, MSGL_ERR, "Broken ES packet size\n");
3100 es->size = 0;
3102 memmove(p, es->start, es->size);
3103 *dp_offset += es->size;
3104 (*dp)->flags = 0;
3105 (*dp)->pos = stream_tell(demuxer->stream);
3106 (*dp)->pts = es->pts;
3108 if(retv > 0)
3109 return retv;
3110 else
3111 continue;
3114 else
3116 uint16_t sz;
3118 es->pid = tss->pid;
3119 es->type = tss->type;
3120 es->subtype = tss->subtype;
3121 es->pts = tss->pts = tss->last_pts;
3122 es->start = &packet[base];
3125 if(tss->payload_size > 0)
3127 sz = FFMIN(tss->payload_size, buf_size);
3128 tss->payload_size -= sz;
3129 es->size = sz;
3131 else
3133 if(is_video)
3135 sz = es->size = buf_size;
3137 else
3139 continue;
3144 if(! probe)
3146 *dp_offset += sz;
3148 if(*dp_offset >= MAX_PACK_BYTES)
3150 (*dp)->pts = tss->last_pts;
3151 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
3152 return 1;
3155 continue;
3157 else
3159 memcpy(es->start, p, sz);
3161 if(es->size)
3162 return es->size;
3163 else
3164 continue;
3169 return 0;
3173 static void reset_fifos(demuxer_t *demuxer, int a, int v, int s)
3175 ts_priv_t* priv = demuxer->priv;
3176 if(a)
3178 if(priv->fifo[0].pack != NULL)
3180 free_demux_packet(priv->fifo[0].pack);
3181 priv->fifo[0].pack = NULL;
3183 priv->fifo[0].offset = 0;
3186 if(v)
3188 if(priv->fifo[1].pack != NULL)
3190 free_demux_packet(priv->fifo[1].pack);
3191 priv->fifo[1].pack = NULL;
3193 priv->fifo[1].offset = 0;
3196 if(s)
3198 if(priv->fifo[2].pack != NULL)
3200 free_demux_packet(priv->fifo[2].pack);
3201 priv->fifo[2].pack = NULL;
3203 priv->fifo[2].offset = 0;
3205 demuxer->reference_clock = MP_NOPTS_VALUE;
3209 static void demux_seek_ts(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
3211 demux_stream_t *d_audio=demuxer->audio;
3212 demux_stream_t *d_video=demuxer->video;
3213 sh_audio_t *sh_audio=d_audio->sh;
3214 sh_video_t *sh_video=d_video->sh;
3215 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
3216 int i, video_stats;
3217 off_t newpos;
3219 //================= seek in MPEG-TS ==========================
3221 ts_dump_streams(demuxer->priv);
3222 reset_fifos(demuxer, sh_audio != NULL, sh_video != NULL, demuxer->sub->id > 0);
3224 demux_flush(demuxer);
3228 video_stats = (sh_video != NULL);
3229 if(video_stats)
3231 mp_msg(MSGT_DEMUX, MSGL_V, "IBPS: %d, vb: %d\r\n", sh_video->i_bps, priv->vbitrate);
3232 if(priv->vbitrate)
3233 video_stats = priv->vbitrate;
3234 else
3235 video_stats = sh_video->i_bps;
3238 newpos = (flags & SEEK_ABSOLUTE) ? demuxer->movi_start : demuxer->filepos;
3239 if(flags & SEEK_FACTOR) // float seek 0..1
3240 newpos+=(demuxer->movi_end-demuxer->movi_start)*rel_seek_secs;
3241 else
3243 // time seek (secs)
3244 if(! video_stats) // unspecified or VBR
3245 newpos += 2324*75*rel_seek_secs; // 174.3 kbyte/sec
3246 else
3247 newpos += video_stats*rel_seek_secs;
3251 if(newpos < demuxer->movi_start)
3252 newpos = demuxer->movi_start; //begininng of stream
3254 stream_seek(demuxer->stream, newpos);
3255 for(i = 0; i < 8192; i++)
3256 if(priv->ts.pids[i] != NULL)
3257 priv->ts.pids[i]->is_synced = 0;
3259 videobuf_code_len = 0;
3261 if(sh_video != NULL)
3262 ds_fill_buffer(d_video);
3264 if(sh_audio != NULL)
3266 ds_fill_buffer(d_audio);
3269 while(sh_video != NULL)
3271 if(sh_audio && !d_audio->eof && d_video->pts && d_audio->pts)
3273 double a_pts=d_audio->pts;
3274 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(double)sh_audio->i_bps;
3275 if(d_video->pts > a_pts)
3277 skip_audio_frame(sh_audio); // sync audio
3278 continue;
3283 i = sync_video_packet(d_video);
3284 if((sh_video->format == VIDEO_MPEG1) || (sh_video->format == VIDEO_MPEG2))
3286 if(i==0x1B3 || i==0x1B8) break; // found it!
3288 else if((sh_video->format == VIDEO_MPEG4) && (i==0x1B6))
3289 break;
3290 else if(sh_video->format == VIDEO_VC1 && (i==0x10E || i==0x10F))
3291 break;
3292 else //H264
3294 if((i & ~0x60) == 0x105 || (i & ~0x60) == 0x107) break;
3297 if(!i || !skip_video_packet(d_video)) break; // EOF?
3302 static int demux_ts_fill_buffer(demuxer_t * demuxer, demux_stream_t *ds)
3304 ES_stream_t es;
3305 ts_priv_t *priv = (ts_priv_t *)demuxer->priv;
3307 return -ts_parse(demuxer, &es, priv->packet, 0);
3311 static int ts_check_file_dmx(demuxer_t *demuxer)
3313 return ts_check_file(demuxer) ? DEMUXER_TYPE_MPEG_TS : 0;
3316 static int is_usable_program(ts_priv_t *priv, pmt_t *pmt)
3318 int j;
3320 for(j = 0; j < pmt->es_cnt; j++)
3322 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3323 continue;
3325 priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO ||
3326 priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO
3328 return 1;
3331 return 0;
3334 static int demux_ts_control(demuxer_t *demuxer, int cmd, void *arg)
3336 ts_priv_t* priv = (ts_priv_t *)demuxer->priv;
3338 switch(cmd)
3340 case DEMUXER_CTRL_SWITCH_AUDIO:
3341 case DEMUXER_CTRL_SWITCH_VIDEO:
3343 void *sh = NULL;
3344 int i, n;
3345 int reftype, areset = 0, vreset = 0;
3346 demux_stream_t *ds;
3348 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
3350 reftype = TYPE_VIDEO;
3351 ds = demuxer->video;
3352 vreset = 1;
3354 else
3356 reftype = TYPE_AUDIO;
3357 ds = demuxer->audio;
3358 areset = 1;
3360 n = *((int*)arg);
3361 if(n == -2)
3363 reset_fifos(demuxer, areset, vreset, 0);
3364 ds->id = -2;
3365 ds->sh = NULL;
3366 ds_free_packs(ds);
3367 *((int*)arg) = ds->id;
3368 return DEMUXER_CTRL_OK;
3371 if(n < 0)
3373 for(i = 0; i < 8192; i++)
3375 if(priv->ts.streams[i].id == ds->id && priv->ts.streams[i].type == reftype)
3376 break;
3379 while(!sh)
3381 i = (i+1) % 8192;
3382 if(priv->ts.streams[i].type == reftype)
3384 if(priv->ts.streams[i].id == ds->id) //we made a complete loop
3385 break;
3386 sh = priv->ts.streams[i].sh;
3390 else //audio track <n>
3392 if (n >= 8192 || priv->ts.streams[n].type != reftype) return DEMUXER_CTRL_NOTIMPL;
3393 i = n;
3394 sh = priv->ts.streams[i].sh;
3397 if(sh)
3399 if(ds->id != priv->ts.streams[i].id)
3400 reset_fifos(demuxer, areset, vreset, 0);
3401 ds->id = priv->ts.streams[i].id;
3402 ds->sh = sh;
3403 ds_free_packs(ds);
3404 mp_msg(MSGT_DEMUX, MSGL_V, "\r\ndemux_ts, switched to audio pid %d, id: %d, sh: %p\r\n", i, ds->id, sh);
3407 *((int*)arg) = ds->id;
3408 return DEMUXER_CTRL_OK;
3411 case DEMUXER_CTRL_IDENTIFY_PROGRAM: //returns in prog->{aid,vid} the new ids that comprise a program
3413 int i, j, cnt=0;
3414 int vid_done=0, aid_done=0;
3415 pmt_t *pmt = NULL;
3416 demux_program_t *prog = arg;
3418 if(priv->pmt_cnt < 2)
3419 return DEMUXER_CTRL_NOTIMPL;
3421 if(prog->progid == -1)
3423 int cur_pmt_idx = 0;
3425 for(i = 0; i < priv->pmt_cnt; i++)
3426 if(priv->pmt[i].progid == priv->prog)
3428 cur_pmt_idx = i;
3429 break;
3432 i = (cur_pmt_idx + 1) % priv->pmt_cnt;
3433 while(i != cur_pmt_idx)
3435 pmt = &priv->pmt[i];
3436 cnt = is_usable_program(priv, pmt);
3437 if(cnt)
3438 break;
3439 i = (i + 1) % priv->pmt_cnt;
3442 else
3444 for(i = 0; i < priv->pmt_cnt; i++)
3445 if(priv->pmt[i].progid == prog->progid)
3447 pmt = &priv->pmt[i]; //required program
3448 cnt = is_usable_program(priv, pmt);
3452 if(!cnt)
3453 return DEMUXER_CTRL_NOTIMPL;
3455 //finally some food
3456 prog->aid = prog->vid = -2; //no audio and no video by default
3457 for(j = 0; j < pmt->es_cnt; j++)
3459 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3460 continue;
3462 if(!vid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO)
3464 vid_done = 1;
3465 prog->vid = pmt->es[j].pid;
3467 else if(!aid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO)
3469 aid_done = 1;
3470 prog->aid = pmt->es[j].pid;
3474 priv->prog = prog->progid = pmt->progid;
3475 return DEMUXER_CTRL_OK;
3478 default:
3479 return DEMUXER_CTRL_NOTIMPL;
3484 const demuxer_desc_t demuxer_desc_mpeg_ts = {
3485 "MPEG-TS demuxer",
3486 "mpegts",
3487 "TS",
3488 "Nico Sabbi",
3490 DEMUXER_TYPE_MPEG_TS,
3491 0, // unsafe autodetect
3492 ts_check_file_dmx,
3493 demux_ts_fill_buffer,
3494 demux_open_ts,
3495 demux_close_ts,
3496 demux_seek_ts,
3497 demux_ts_control