Get rid of completely pointless vt_doit variable
[mplayer/glamo.git] / libmpdemux / demux_ts.c
blobc213c34a35518fe5fa7d7e8c0872f0b5279ae81f
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 "help_mp.h"
33 #include "stream/stream.h"
34 #include "demuxer.h"
35 #include "parse_es.h"
36 #include "stheader.h"
38 #include "ms_hdr.h"
39 #include "mpeg_hdr.h"
41 #define TS_PH_PACKET_SIZE 192
42 #define TS_FEC_PACKET_SIZE 204
43 #define TS_PACKET_SIZE 188
44 #define NB_PID_MAX 8192
46 #define MAX_HEADER_SIZE 6 /* enough for PES header + length */
47 #define MAX_CHECK_SIZE 65535
48 #define TS_MAX_PROBE_SIZE 2000000 /* do not forget to change this in cfg-common-opts.h, too */
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;
64 extern char *dvdsub_lang, *audio_lang; //for -alang
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_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 SPU_DVD = 0x3000000,
81 SPU_DVB = 0x3000001,
82 PES_PRIVATE1 = 0xBD00000,
83 SL_PES_STREAM = 0xD000000,
84 SL_SECTION = 0xD100000,
85 MP4_OD = 0xD200000,
86 } es_stream_type_t;
88 typedef struct {
89 uint8_t *buffer;
90 uint16_t buffer_len;
91 } ts_section_t;
93 typedef struct {
94 int size;
95 unsigned char *start;
96 uint16_t payload_size;
97 es_stream_type_t type, subtype;
98 float pts, last_pts;
99 int pid;
100 char lang[4];
101 int last_cc; // last cc code (-1 if first packet)
102 int is_synced;
103 ts_section_t section;
104 uint8_t *extradata;
105 int extradata_alloc, extradata_len;
106 struct {
107 uint8_t au_start, au_end, last_au_end;
108 } sl;
109 } ES_stream_t;
111 typedef struct {
112 void *sh;
113 int id;
114 int type;
115 } sh_av_t;
117 typedef struct MpegTSContext {
118 int packet_size; // raw packet size, including FEC if present e.g. 188 bytes
119 ES_stream_t *pids[NB_PID_MAX];
120 sh_av_t streams[NB_PID_MAX];
121 } MpegTSContext;
124 typedef struct {
125 demux_stream_t *ds;
126 demux_packet_t *pack;
127 int offset, buffer_size;
128 } av_fifo_t;
130 #define MAX_EXTRADATA_SIZE 64*1024
131 typedef struct {
132 int32_t object_type; //aka codec used
133 int32_t stream_type; //video, audio etc.
134 uint8_t buf[MAX_EXTRADATA_SIZE];
135 uint16_t buf_size;
136 uint8_t szm1;
137 } mp4_decoder_config_t;
139 typedef struct {
140 //flags
141 uint8_t flags;
142 uint8_t au_start;
143 uint8_t au_end;
144 uint8_t random_accesspoint;
145 uint8_t random_accesspoint_only;
146 uint8_t padding;
147 uint8_t use_ts;
148 uint8_t idle;
149 uint8_t duration;
151 uint32_t ts_resolution, ocr_resolution;
152 uint8_t ts_len, ocr_len, au_len, instant_bitrate_len, degr_len, au_seqnum_len, packet_seqnum_len;
153 uint32_t timescale;
154 uint16_t au_duration, cts_duration;
155 uint64_t ocr, dts, cts;
156 } mp4_sl_config_t;
158 typedef struct {
159 uint16_t id;
160 uint8_t flags;
161 mp4_decoder_config_t decoder;
162 mp4_sl_config_t sl;
163 } mp4_es_descr_t;
165 typedef struct {
166 uint16_t id;
167 uint8_t flags;
168 mp4_es_descr_t *es;
169 uint16_t es_cnt;
170 } mp4_od_t;
172 typedef struct {
173 uint8_t skip;
174 uint8_t table_id;
175 uint8_t ssi;
176 uint16_t section_length;
177 uint16_t ts_id;
178 uint8_t version_number;
179 uint8_t curr_next;
180 uint8_t section_number;
181 uint8_t last_section_number;
182 struct pat_progs_t {
183 uint16_t id;
184 uint16_t pmt_pid;
185 } *progs;
186 uint16_t progs_cnt;
187 ts_section_t section;
188 } pat_t;
190 typedef struct {
191 uint16_t progid;
192 uint8_t skip;
193 uint8_t table_id;
194 uint8_t ssi;
195 uint16_t section_length;
196 uint8_t version_number;
197 uint8_t curr_next;
198 uint8_t section_number;
199 uint8_t last_section_number;
200 uint16_t PCR_PID;
201 uint16_t prog_descr_length;
202 ts_section_t section;
203 uint16_t es_cnt;
204 struct pmt_es_t {
205 uint16_t pid;
206 uint32_t type; //it's 8 bit long, but cast to the right type as FOURCC
207 uint16_t descr_length;
208 uint8_t format_descriptor[5];
209 uint8_t lang[4];
210 uint16_t mp4_es_id;
211 } *es;
212 mp4_od_t iod, *od;
213 mp4_es_descr_t *mp4es;
214 int od_cnt, mp4es_cnt;
215 } pmt_t;
217 typedef struct {
218 uint64_t size;
219 float duration;
220 float first_pts;
221 float last_pts;
222 } TS_stream_info;
224 typedef struct {
225 MpegTSContext ts;
226 int last_pid;
227 av_fifo_t fifo[3]; //0 for audio, 1 for video, 2 for subs
228 pat_t pat;
229 pmt_t *pmt;
230 uint16_t pmt_cnt;
231 uint32_t prog;
232 uint32_t vbitrate;
233 int keep_broken;
234 int last_aid;
235 int last_vid;
236 char packet[TS_FEC_PACKET_SIZE];
237 TS_stream_info vstr, astr;
238 } ts_priv_t;
241 typedef struct {
242 es_stream_type_t type;
243 ts_section_t section;
244 } TS_pids_t;
247 #define IS_AUDIO(x) (((x) == AUDIO_MP2) || ((x) == AUDIO_A52) || ((x) == AUDIO_LPCM_BE) || ((x) == AUDIO_AAC) || ((x) == AUDIO_DTS))
248 #define IS_VIDEO(x) (((x) == VIDEO_MPEG1) || ((x) == VIDEO_MPEG2) || ((x) == VIDEO_MPEG4) || ((x) == VIDEO_H264) || ((x) == VIDEO_AVC) || ((x) == VIDEO_VC1))
250 static int ts_parse(demuxer_t *demuxer, ES_stream_t *es, unsigned char *packet, int probe);
252 static uint8_t get_packet_size(const unsigned char *buf, int size)
254 int i;
256 if (size < (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS))
257 return 0;
259 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
261 if (buf[i * TS_PACKET_SIZE] != 0x47)
263 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
264 goto try_fec;
267 return TS_PACKET_SIZE;
269 try_fec:
270 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
272 if (buf[i * TS_FEC_PACKET_SIZE] != 0x47){
273 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
274 goto try_philips;
277 return TS_FEC_PACKET_SIZE;
279 try_philips:
280 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
282 if (buf[i * TS_PH_PACKET_SIZE] != 0x47)
283 return 0;
285 return TS_PH_PACKET_SIZE;
288 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h);
289 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid);
291 static void ts_add_stream(demuxer_t * demuxer, ES_stream_t *es)
293 int i;
294 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
296 if(priv->ts.streams[es->pid].sh)
297 return;
299 if((IS_AUDIO(es->type) || IS_AUDIO(es->subtype)) && priv->last_aid+1 < MAX_A_STREAMS)
301 sh_audio_t *sh = new_sh_audio_aid(demuxer, priv->last_aid, es->pid);
302 if(sh)
304 const char *lang = pid_lang_from_pmt(priv, es->pid);
305 sh->format = IS_AUDIO(es->type) ? es->type : es->subtype;
306 sh->ds = demuxer->audio;
308 priv->ts.streams[es->pid].id = priv->last_aid;
309 priv->ts.streams[es->pid].sh = sh;
310 priv->ts.streams[es->pid].type = TYPE_AUDIO;
311 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);
312 if (lang && lang[0])
313 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_AID_%d_LANG=%s\n", es->pid, lang);
314 priv->last_aid++;
317 if(es->extradata && es->extradata_len)
319 sh->wf = (WAVEFORMATEX *) malloc(sizeof (WAVEFORMATEX) + es->extradata_len);
320 sh->wf->cbSize = es->extradata_len;
321 memcpy(sh->wf + 1, es->extradata, es->extradata_len);
325 if((IS_VIDEO(es->type) || IS_VIDEO(es->subtype)) && priv->last_vid+1 < MAX_V_STREAMS)
327 sh_video_t *sh = new_sh_video_vid(demuxer, priv->last_vid, es->pid);
328 if(sh)
330 sh->format = IS_VIDEO(es->type) ? es->type : es->subtype;
331 sh->ds = demuxer->video;
333 priv->ts.streams[es->pid].id = priv->last_vid;
334 priv->ts.streams[es->pid].sh = sh;
335 priv->ts.streams[es->pid].type = TYPE_VIDEO;
336 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);
337 priv->last_vid++;
340 if(sh->format == VIDEO_AVC && es->extradata && es->extradata_len)
342 int w = 0, h = 0;
343 sh->bih = (BITMAPINFOHEADER *) calloc(1, sizeof(BITMAPINFOHEADER) + es->extradata_len);
344 sh->bih->biSize= sizeof(BITMAPINFOHEADER) + es->extradata_len;
345 sh->bih->biCompression = sh->format;
346 memcpy(sh->bih + 1, es->extradata, es->extradata_len);
347 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "EXTRADATA(%d BYTES): \n", es->extradata_len);
348 for(i = 0;i < es->extradata_len; i++)
349 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "%02x ", (int) es->extradata[i]);
350 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"\n");
351 if(parse_avc_sps(es->extradata, es->extradata_len, &w, &h))
353 sh->bih->biWidth = w;
354 sh->bih->biHeight = h;
361 static int ts_check_file(demuxer_t * demuxer)
363 const int buf_size = (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS);
364 unsigned char buf[TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS], done = 0, *ptr;
365 uint32_t _read, i, count = 0, is_ts;
366 int cc[NB_PID_MAX], last_cc[NB_PID_MAX], pid, cc_ok, c, good, bad;
367 uint8_t size = 0;
368 off_t pos = 0;
369 off_t init_pos;
371 mp_msg(MSGT_DEMUX, MSGL_V, "Checking for MPEG-TS...\n");
373 init_pos = stream_tell(demuxer->stream);
374 is_ts = 0;
375 while(! done)
377 i = 1;
378 c = 0;
380 while(((c=stream_read_char(demuxer->stream)) != 0x47)
381 && (c >= 0)
382 && (i < MAX_CHECK_SIZE)
383 && ! demuxer->stream->eof
384 ) i++;
387 if(c != 0x47)
389 mp_msg(MSGT_DEMUX, MSGL_V, "THIS DOESN'T LOOK LIKE AN MPEG-TS FILE!\n");
390 is_ts = 0;
391 done = 1;
392 continue;
395 pos = stream_tell(demuxer->stream) - 1;
396 buf[0] = c;
397 _read = stream_read(demuxer->stream, &buf[1], buf_size-1);
399 if(_read < buf_size-1)
401 mp_msg(MSGT_DEMUX, MSGL_V, "COULDN'T READ ENOUGH DATA, EXITING TS_CHECK\n");
402 stream_reset(demuxer->stream);
403 return 0;
406 size = get_packet_size(buf, buf_size);
407 if(size)
409 done = 1;
410 is_ts = 1;
413 if(pos - init_pos >= MAX_CHECK_SIZE)
415 done = 1;
416 is_ts = 0;
420 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);
421 stream_seek(demuxer->stream, pos);
423 if(! is_ts)
424 return 0;
426 //LET'S CHECK continuity counters
427 good = bad = 0;
428 for(count = 0; count < NB_PID_MAX; count++)
430 cc[count] = last_cc[count] = -1;
433 for(count = 0; count < NUM_CONSECUTIVE_TS_PACKETS; count++)
435 ptr = &(buf[size * count]);
436 pid = ((ptr[1] & 0x1f) << 8) | ptr[2];
437 mp_msg(MSGT_DEMUX, MSGL_DBG2, "BUF: %02x %02x %02x %02x, PID %d, SIZE: %d \n",
438 ptr[0], ptr[1], ptr[2], ptr[3], pid, size);
440 if((pid == 8191) || (pid < 16))
441 continue;
443 cc[pid] = (ptr[3] & 0xf);
444 cc_ok = (last_cc[pid] < 0) || ((((last_cc[pid] + 1) & 0x0f) == cc[pid]));
445 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PID %d, COMPARE CC %d AND LAST_CC %d\n", pid, cc[pid], last_cc[pid]);
446 if(! cc_ok)
447 //return 0;
448 bad++;
449 else
450 good++;
452 last_cc[pid] = cc[pid];
455 mp_msg(MSGT_DEMUX, MSGL_V, "GOOD CC: %d, BAD CC: %d\n", good, bad);
457 if(good >= bad)
458 return size;
459 else
460 return 0;
464 static inline int32_t progid_idx_in_pmt(ts_priv_t *priv, uint16_t progid)
466 int x;
468 if(priv->pmt == NULL)
469 return -1;
471 for(x = 0; x < priv->pmt_cnt; x++)
473 if(priv->pmt[x].progid == progid)
474 return x;
477 return -1;
481 static inline int32_t progid_for_pid(ts_priv_t *priv, int pid, int32_t req) //finds the first program listing a pid
483 int i, j;
484 pmt_t *pmt;
487 if(priv->pmt == NULL)
488 return -1;
491 for(i=0; i < priv->pmt_cnt; i++)
493 pmt = &(priv->pmt[i]);
495 if(pmt->es == NULL)
496 return -1;
498 for(j = 0; j < pmt->es_cnt; j++)
500 if(pmt->es[j].pid == pid)
502 if((req == 0) || (req == pmt->progid))
503 return pmt->progid;
508 return -1;
511 static inline int32_t prog_pcr_pid(ts_priv_t *priv, int progid)
513 int i;
515 if(priv->pmt == NULL)
516 return -1;
517 for(i=0; i < priv->pmt_cnt; i++)
519 if(priv->pmt[i].progid == progid)
520 return priv->pmt[i].PCR_PID;
522 return -1;
526 static inline int pid_match_lang(ts_priv_t *priv, uint16_t pid, char *lang)
528 uint16_t i, j;
529 pmt_t *pmt;
531 if(priv->pmt == NULL)
532 return -1;
534 for(i=0; i < priv->pmt_cnt; i++)
536 pmt = &(priv->pmt[i]);
538 if(pmt->es == NULL)
539 return -1;
541 for(j = 0; j < pmt->es_cnt; j++)
543 if(pmt->es[j].pid != pid)
544 continue;
546 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);
547 if(strncmp(pmt->es[j].lang, lang, 3) == 0)
549 return 1;
555 return -1;
558 typedef struct {
559 int32_t atype, vtype, stype; //types
560 int32_t apid, vpid, spid; //stream ids
561 char slang[4], alang[4]; //languages
562 uint16_t prog;
563 off_t probe;
564 } tsdemux_init_t;
566 //stripped down version of a52_syncinfo() from liba52
567 //copyright belongs to Michel Lespinasse <walken@zoy.org> and Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
568 int mp_a52_framesize(uint8_t * buf, int *srate)
570 int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
571 128, 160, 192, 224, 256, 320, 384, 448,
572 512, 576, 640
574 uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
575 int frmsizecod, bitrate, half;
577 if((buf[0] != 0x0b) || (buf[1] != 0x77)) /* syncword */
578 return 0;
580 if(buf[5] >= 0x60) /* bsid >= 12 */
581 return 0;
583 half = halfrate[buf[5] >> 3];
585 frmsizecod = buf[4] & 63;
586 if(frmsizecod >= 38)
587 return 0;
589 bitrate = rate[frmsizecod >> 1];
591 switch(buf[4] & 0xc0)
593 case 0: /* 48 KHz */
594 *srate = 48000 >> half;
595 return 4 * bitrate;
596 case 0x40: /* 44.1 KHz */
597 *srate = 44100 >> half;
598 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
599 case 0x80: /* 32 KHz */
600 *srate = 32000 >> half;
601 return 6 * bitrate;
604 return 0;
607 //second stage: returns the count of A52 syncwords found
608 static int a52_check(char *buf, int len)
610 int cnt, frame_length, ok, srate;
612 cnt = ok = 0;
613 if(len < 8)
614 return 0;
616 while(cnt < len - 7)
618 if(buf[cnt] == 0x0B && buf[cnt+1] == 0x77)
620 frame_length = mp_a52_framesize(&buf[cnt], &srate);
621 if(frame_length>=7 && frame_length<=3840)
623 cnt += frame_length;
624 ok++;
626 else
627 cnt++;
629 else
630 cnt++;
633 mp_msg(MSGT_DEMUXER, MSGL_V, "A52_CHECK(%d input bytes), found %d frame syncwords of %d bytes length\n", len, ok, frame_length);
634 return ok;
638 static off_t ts_detect_streams(demuxer_t *demuxer, tsdemux_init_t *param)
640 int video_found = 0, audio_found = 0, sub_found = 0, i, num_packets = 0, req_apid, req_vpid, req_spid;
641 int is_audio, is_video, is_sub, has_tables;
642 int32_t p, chosen_pid = 0;
643 off_t pos=0, ret = 0, init_pos, end_pos;
644 ES_stream_t es;
645 unsigned char tmp[TS_FEC_PACKET_SIZE];
646 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
647 struct {
648 char *buf;
649 int pos;
650 } pes_priv1[8192], *pptr;
651 char *tmpbuf;
653 priv->last_pid = 8192; //invalid pid
655 req_apid = param->apid;
656 req_vpid = param->vpid;
657 req_spid = param->spid;
659 has_tables = 0;
660 memset(pes_priv1, 0, sizeof(pes_priv1));
661 init_pos = stream_tell(demuxer->stream);
662 mp_msg(MSGT_DEMUXER, MSGL_V, "PROBING UP TO %"PRIu64", PROG: %d\n", (uint64_t) param->probe, param->prog);
663 end_pos = init_pos + (param->probe ? param->probe : TS_MAX_PROBE_SIZE);
664 while(1)
666 pos = stream_tell(demuxer->stream);
667 if(pos > end_pos || demuxer->stream->eof)
668 break;
670 if(ts_parse(demuxer, &es, tmp, 1))
672 //Non PES-aligned A52 audio may escape detection if PMT is not present;
673 //in this case we try to find at least 3 A52 syncwords
674 if((es.type == PES_PRIVATE1) && (! audio_found) && req_apid > -2)
676 pptr = &pes_priv1[es.pid];
677 if(pptr->pos < 64*1024)
679 tmpbuf = (char*) realloc(pptr->buf, pptr->pos + es.size);
680 if(tmpbuf != NULL)
682 pptr->buf = tmpbuf;
683 memcpy(&(pptr->buf[ pptr->pos ]), es.start, es.size);
684 pptr->pos += es.size;
685 if(a52_check(pptr->buf, pptr->pos) > 2)
687 param->atype = AUDIO_A52;
688 param->apid = es.pid;
689 es.type = AUDIO_A52;
695 is_audio = IS_AUDIO(es.type) || ((es.type==SL_PES_STREAM) && IS_AUDIO(es.subtype));
696 is_video = IS_VIDEO(es.type) || ((es.type==SL_PES_STREAM) && IS_VIDEO(es.subtype));
697 is_sub = ((es.type == SPU_DVD) || (es.type == SPU_DVB));
700 if((! is_audio) && (! is_video) && (! is_sub))
701 continue;
702 if(is_audio && req_apid==-2)
703 continue;
705 if(is_video)
707 chosen_pid = (req_vpid == es.pid);
708 if((! chosen_pid) && (req_vpid > 0))
709 continue;
711 else if(is_audio)
713 if(req_apid > 0)
715 chosen_pid = (req_apid == es.pid);
716 if(! chosen_pid)
717 continue;
719 else if(param->alang[0] > 0 && es.lang[0] > 0)
721 if(pid_match_lang(priv, es.pid, param->alang) == -1)
722 continue;
724 chosen_pid = 1;
725 param->apid = req_apid = es.pid;
728 else if(is_sub)
730 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_SUBTITLE_ID=%d\n", es.pid);
731 if (es.lang[0] > 0)
732 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_SID_%d_LANG=%s\n", es.pid, es.lang);
733 chosen_pid = (req_spid == es.pid);
734 if((! chosen_pid) && (req_spid > 0))
735 continue;
738 if(req_apid < 0 && (param->alang[0] == 0) && req_vpid < 0 && req_spid < 0)
739 chosen_pid = 1;
741 if((ret == 0) && chosen_pid)
743 ret = stream_tell(demuxer->stream);
746 p = progid_for_pid(priv, es.pid, param->prog);
747 if(p != -1)
749 has_tables++;
750 if(!param->prog && chosen_pid)
751 param->prog = p;
754 if((param->prog > 0) && (param->prog != p))
756 if(audio_found)
758 if(is_video && (req_vpid == es.pid))
760 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
761 param->vpid = es.pid;
762 video_found = 1;
763 break;
767 if(video_found)
769 if(is_audio && (req_apid == es.pid))
771 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
772 param->apid = es.pid;
773 audio_found = 1;
774 break;
779 continue;
783 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "TYPE: %x, PID: %d, PROG FOUND: %d\n", es.type, es.pid, param->prog);
786 if(is_video)
788 if((req_vpid == -1) || (req_vpid == es.pid))
790 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
791 param->vpid = es.pid;
792 video_found = 1;
797 if(((req_vpid == -2) || (num_packets >= NUM_CONSECUTIVE_AUDIO_PACKETS)) && audio_found && !param->probe)
799 //novideo or we have at least 348 audio packets (64 KB) without video (TS with audio only)
800 param->vtype = 0;
801 break;
804 if(is_sub)
806 if((req_spid == -1) || (req_spid == es.pid))
808 param->stype = es.type;
809 param->spid = es.pid;
810 sub_found = 1;
814 if(is_audio)
816 if((req_apid == -1) || (req_apid == es.pid))
818 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
819 param->apid = es.pid;
820 audio_found = 1;
824 if(audio_found && (param->apid == es.pid) && (! video_found))
825 num_packets++;
827 if((has_tables==0) && (video_found && audio_found) && (pos >= 1000000))
828 break;
832 for(i=0; i<8192; i++)
834 if(pes_priv1[i].buf != NULL)
836 free(pes_priv1[i].buf);
837 pes_priv1[i].buf = NULL;
838 pes_priv1[i].pos = 0;
842 if(video_found)
844 if(param->vtype == VIDEO_MPEG1)
845 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG1(pid=%d) ", param->vpid);
846 else if(param->vtype == VIDEO_MPEG2)
847 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG2(pid=%d) ", param->vpid);
848 else if(param->vtype == VIDEO_MPEG4)
849 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG4(pid=%d) ", param->vpid);
850 else if(param->vtype == VIDEO_H264)
851 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO H264(pid=%d) ", param->vpid);
852 else if(param->vtype == VIDEO_VC1)
853 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO VC1(pid=%d) ", param->vpid);
854 else if(param->vtype == VIDEO_AVC)
855 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO AVC(NAL-H264, pid=%d) ", param->vpid);
857 else
859 param->vtype = UNKNOWN;
860 //WE DIDN'T MATCH ANY VIDEO STREAM
861 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO VIDEO! ");
864 if(param->atype == AUDIO_MP2)
865 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO MPA(pid=%d)", param->apid);
866 else if(param->atype == AUDIO_A52)
867 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO A52(pid=%d)", param->apid);
868 else if(param->atype == AUDIO_DTS)
869 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO DTS(pid=%d)", param->apid);
870 else if(param->atype == AUDIO_LPCM_BE)
871 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO LPCM(pid=%d)", param->apid);
872 else if(param->atype == AUDIO_AAC)
873 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO AAC(pid=%d)", param->apid);
874 else
876 audio_found = 0;
877 param->atype = UNKNOWN;
878 //WE DIDN'T MATCH ANY AUDIO STREAM, SO WE FORCE THE DEMUXER TO IGNORE AUDIO
879 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO AUDIO! ");
882 if(param->stype == SPU_DVD || param->stype == SPU_DVB)
883 mp_msg(MSGT_DEMUXER, MSGL_INFO, " SUB %s(pid=%d) ", (param->stype==SPU_DVD ? "DVD" : "DVB"), param->spid);
884 else
886 param->stype = UNKNOWN;
887 mp_msg(MSGT_DEMUXER, MSGL_INFO, " NO SUBS (yet)! ");
890 if(video_found || audio_found)
892 if(!param->prog)
894 p = progid_for_pid(priv, video_found ? param->vpid : param->apid, 0);
895 if(p != -1)
896 param->prog = p;
899 if(demuxer->stream->eof && (ret == 0))
900 ret = init_pos;
901 mp_msg(MSGT_DEMUXER, MSGL_INFO, " PROGRAM N. %d\n", param->prog);
903 else
904 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\n");
907 for(i=0; i<8192; i++)
909 if(priv->ts.pids[i] != NULL)
911 priv->ts.pids[i]->payload_size = 0;
912 priv->ts.pids[i]->pts = priv->ts.pids[i]->last_pts = 0;
913 priv->ts.pids[i]->last_cc = -1;
914 priv->ts.pids[i]->is_synced = 0;
918 return ret;
921 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h)
923 int sps, sps_len;
924 unsigned char *ptr;
925 mp_mpeg_header_t picture;
926 if(len < 6)
927 return 0;
928 sps = buf[5] & 0x1f;
929 if(!sps)
930 return 0;
931 sps_len = (buf[6] << 8) | buf[7];
932 if(!sps_len || (sps_len > len - 8))
933 return 0;
934 ptr = &(buf[8]);
935 picture.display_picture_width = picture.display_picture_height = 0;
936 h264_parse_sps(&picture, ptr, len - 8);
937 if(!picture.display_picture_width || !picture.display_picture_height)
938 return 0;
939 *w = picture.display_picture_width;
940 *h = picture.display_picture_height;
941 return 1;
944 static demuxer_t *demux_open_ts(demuxer_t * demuxer)
946 int i;
947 uint8_t packet_size;
948 sh_video_t *sh_video;
949 sh_audio_t *sh_audio;
950 off_t start_pos;
951 tsdemux_init_t params;
952 ts_priv_t * priv = demuxer->priv;
954 mp_msg(MSGT_DEMUX, MSGL_V, "DEMUX OPEN, AUDIO_ID: %d, VIDEO_ID: %d, SUBTITLE_ID: %d,\n",
955 demuxer->audio->id, demuxer->video->id, demuxer->sub->id);
958 demuxer->type= DEMUXER_TYPE_MPEG_TS;
961 stream_reset(demuxer->stream);
963 packet_size = ts_check_file(demuxer);
964 if(!packet_size)
965 return NULL;
967 priv = calloc(1, sizeof(ts_priv_t));
968 if(priv == NULL)
970 mp_msg(MSGT_DEMUX, MSGL_FATAL, "DEMUX_OPEN_TS, couldn't allocate enough memory for ts->priv, exit\n");
971 return NULL;
974 for(i=0; i < 8192; i++)
976 priv->ts.pids[i] = NULL;
977 priv->ts.streams[i].id = -3;
979 priv->pat.progs = NULL;
980 priv->pat.progs_cnt = 0;
981 priv->pat.section.buffer = NULL;
982 priv->pat.section.buffer_len = 0;
984 priv->pmt = NULL;
985 priv->pmt_cnt = 0;
987 priv->keep_broken = ts_keep_broken;
988 priv->ts.packet_size = packet_size;
991 demuxer->priv = priv;
992 if(demuxer->stream->type != STREAMTYPE_FILE)
993 demuxer->seekable = 1;
994 else
995 demuxer->seekable = 1;
998 params.atype = params.vtype = params.stype = UNKNOWN;
999 params.apid = demuxer->audio->id;
1000 params.vpid = demuxer->video->id;
1001 params.spid = demuxer->sub->id;
1002 params.prog = ts_prog;
1003 params.probe = ts_probe;
1005 if(dvdsub_lang != NULL)
1007 strncpy(params.slang, dvdsub_lang, 3);
1008 params.slang[3] = 0;
1010 else
1011 memset(params.slang, 0, 4);
1013 if(audio_lang != NULL)
1015 strncpy(params.alang, audio_lang, 3);
1016 params.alang[3] = 0;
1018 else
1019 memset(params.alang, 0, 4);
1021 start_pos = ts_detect_streams(demuxer, &params);
1023 demuxer->sub->id = params.spid;
1024 priv->prog = params.prog;
1026 if(params.vtype != UNKNOWN)
1028 ts_add_stream(demuxer, priv->ts.pids[params.vpid]);
1029 sh_video = priv->ts.streams[params.vpid].sh;
1030 demuxer->video->id = priv->ts.streams[params.vpid].id;
1031 sh_video->ds = demuxer->video;
1032 sh_video->format = params.vtype;
1033 demuxer->video->sh = sh_video;
1036 if(params.atype != UNKNOWN)
1038 ES_stream_t *es = priv->ts.pids[params.apid];
1040 if(!IS_AUDIO(es->type) && !IS_AUDIO(es->subtype) && IS_AUDIO(params.atype)) es->subtype = params.atype;
1041 ts_add_stream(demuxer, priv->ts.pids[params.apid]);
1042 sh_audio = priv->ts.streams[params.apid].sh;
1043 demuxer->audio->id = priv->ts.streams[params.apid].id;
1044 sh_audio->ds = demuxer->audio;
1045 sh_audio->format = params.atype;
1046 demuxer->audio->sh = sh_audio;
1050 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);
1053 start_pos = (start_pos <= priv->ts.packet_size ? 0 : start_pos - priv->ts.packet_size);
1054 demuxer->movi_start = start_pos;
1055 demuxer->reference_clock = MP_NOPTS_VALUE;
1056 stream_reset(demuxer->stream);
1057 stream_seek(demuxer->stream, start_pos); //IF IT'S FROM A PIPE IT WILL FAIL, BUT WHO CARES?
1060 priv->last_pid = 8192; //invalid pid
1062 for(i = 0; i < 3; i++)
1064 priv->fifo[i].pack = NULL;
1065 priv->fifo[i].offset = 0;
1067 priv->fifo[0].ds = demuxer->audio;
1068 priv->fifo[1].ds = demuxer->video;
1069 priv->fifo[2].ds = demuxer->sub;
1071 priv->fifo[0].buffer_size = 1536;
1072 priv->fifo[1].buffer_size = 32767;
1073 priv->fifo[2].buffer_size = 32767;
1075 priv->pat.section.buffer_len = 0;
1076 for(i = 0; i < priv->pmt_cnt; i++)
1077 priv->pmt[i].section.buffer_len = 0;
1079 demuxer->filepos = stream_tell(demuxer->stream);
1080 return demuxer;
1083 static void demux_close_ts(demuxer_t * demuxer)
1085 uint16_t i;
1086 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
1088 if(priv)
1090 if(priv->pat.section.buffer)
1091 free(priv->pat.section.buffer);
1092 if(priv->pat.progs)
1093 free(priv->pat.progs);
1095 if(priv->pmt)
1097 for(i = 0; i < priv->pmt_cnt; i++)
1099 if(priv->pmt[i].section.buffer)
1100 free(priv->pmt[i].section.buffer);
1101 if(priv->pmt[i].es)
1102 free(priv->pmt[i].es);
1104 free(priv->pmt);
1106 free(priv);
1108 demuxer->priv=NULL;
1112 unsigned char mp_getbits(unsigned char*, unsigned int, unsigned char);
1113 #define getbits mp_getbits
1115 static int mp4_parse_sl_packet(pmt_t *pmt, uint8_t *buf, uint16_t packet_len, int pid, ES_stream_t *pes_es)
1117 int i, n, m, mp4_es_id = -1;
1118 uint64_t v = 0;
1119 uint32_t pl_size = 0;
1120 int deg_flag = 0;
1121 mp4_es_descr_t *es = NULL;
1122 mp4_sl_config_t *sl = NULL;
1123 uint8_t au_start = 0, au_end = 0, rap_flag = 0, ocr_flag = 0, padding = 0, padding_bits = 0, idle = 0;
1125 pes_es->is_synced = 0;
1126 mp_msg(MSGT_DEMUXER,MSGL_V, "mp4_parse_sl_packet, pid: %d, pmt: %pm, packet_len: %d\n", pid, pmt, packet_len);
1127 if(! pmt || !packet_len)
1128 return 0;
1130 for(i = 0; i < pmt->es_cnt; i++)
1132 if(pmt->es[i].pid == pid)
1133 mp4_es_id = pmt->es[i].mp4_es_id;
1135 if(mp4_es_id < 0)
1136 return -1;
1138 for(i = 0; i < pmt->mp4es_cnt; i++)
1140 if(pmt->mp4es[i].id == mp4_es_id)
1141 es = &(pmt->mp4es[i]);
1143 if(! es)
1144 return -1;
1146 pes_es->subtype = es->decoder.object_type;
1148 sl = &(es->sl);
1149 if(!sl)
1150 return -1;
1152 //now es is the complete es_descriptor of out mp4 ES stream
1153 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "ID: %d, FLAGS: 0x%x, subtype: %x\n", es->id, sl->flags, pes_es->subtype);
1155 n = 0;
1156 if(sl->au_start)
1157 pes_es->sl.au_start = au_start = getbits(buf, n++, 1);
1158 else
1159 pes_es->sl.au_start = (pes_es->sl.last_au_end ? 1 : 0);
1160 if(sl->au_end)
1161 pes_es->sl.au_end = au_end = getbits(buf, n++, 1);
1163 if(!sl->au_start && !sl->au_end)
1165 pes_es->sl.au_start = pes_es->sl.au_end = au_start = au_end = 1;
1167 pes_es->sl.last_au_end = pes_es->sl.au_end;
1170 if(sl->ocr_len > 0)
1171 ocr_flag = getbits(buf, n++, 1);
1172 if(sl->idle)
1173 idle = getbits(buf, n++, 1);
1174 if(sl->padding)
1175 padding = getbits(buf, n++, 1);
1176 if(padding)
1178 padding_bits = getbits(buf, n, 3);
1179 n += 3;
1182 if(idle || (padding && !padding_bits))
1184 pes_es->payload_size = 0;
1185 return -1;
1188 //(! idle && (!padding || padding_bits != 0)) is true
1189 n += sl->packet_seqnum_len;
1190 if(sl->degr_len)
1191 deg_flag = getbits(buf, n++, 1);
1192 if(deg_flag)
1193 n += sl->degr_len;
1195 if(ocr_flag)
1197 n += sl->ocr_len;
1198 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "OCR: %d bits\n", sl->ocr_len);
1201 if(packet_len * 8 <= n)
1202 return -1;
1204 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "\nAU_START: %d, AU_END: %d\n", au_start, au_end);
1205 if(au_start)
1207 int dts_flag = 0, cts_flag = 0, ib_flag = 0;
1209 if(sl->random_accesspoint)
1210 rap_flag = getbits(buf, n++, 1);
1212 //check commented because it seems it's rarely used, and we need this flag set in case of au_start
1213 //the decoder will eventually discard the payload if it can't decode it
1214 //if(rap_flag || sl->random_accesspoint_only)
1215 pes_es->is_synced = 1;
1217 n += sl->au_seqnum_len;
1218 if(packet_len * 8 <= n+8)
1219 return -1;
1220 if(sl->use_ts)
1222 dts_flag = getbits(buf, n++, 1);
1223 cts_flag = getbits(buf, n++, 1);
1225 if(sl->instant_bitrate_len)
1226 ib_flag = getbits(buf, n++, 1);
1227 if(packet_len * 8 <= n+8)
1228 return -1;
1229 if(dts_flag && (sl->ts_len > 0))
1231 n += sl->ts_len;
1232 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "DTS: %d bits\n", sl->ts_len);
1234 if(packet_len * 8 <= n+8)
1235 return -1;
1236 if(cts_flag && (sl->ts_len > 0))
1238 int i = 0, m;
1240 while(i < sl->ts_len)
1242 m = FFMIN(8, sl->ts_len - i);
1243 v |= getbits(buf, n, m);
1244 if(sl->ts_len - i > 8)
1245 v <<= 8;
1246 i += m;
1247 n += m;
1248 if(packet_len * 8 <= n+8)
1249 return -1;
1252 pes_es->pts = (float) v / (float) sl->ts_resolution;
1253 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "CTS: %d bits, value: %"PRIu64"/%d = %.3f\n", sl->ts_len, v, sl->ts_resolution, pes_es->pts);
1257 i = 0;
1258 pl_size = 0;
1259 while(i < sl->au_len)
1261 m = FFMIN(8, sl->au_len - i);
1262 pl_size |= getbits(buf, n, m);
1263 if(sl->au_len - i > 8)
1264 pl_size <<= 8;
1265 i += m;
1266 n += m;
1267 if(packet_len * 8 <= n+8)
1268 return -1;
1270 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "AU_LEN: %u (%d bits)\n", pl_size, sl->au_len);
1271 if(ib_flag)
1272 n += sl->instant_bitrate_len;
1275 m = (n+7)/8;
1276 if(0 < pl_size && pl_size < pes_es->payload_size)
1277 pes_es->payload_size = pl_size;
1279 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",
1280 n, m, pes_es->payload_size, pl_size, (int) rap_flag, (int) sl->random_accesspoint_only);
1282 return m;
1285 //this function parses the extension fields in the PES header and returns the substream_id, or -1 in case of errors
1286 static int parse_pes_extension_fields(unsigned char *p, int pkt_len)
1288 int skip;
1289 unsigned char flags;
1291 if(!(p[7] & 0x1)) //no extension_field
1292 return -1;
1293 skip = 9;
1294 if(p[7] & 0x80)
1296 skip += 5;
1297 if(p[7] & 0x40)
1298 skip += 5;
1300 if(p[7] & 0x20) //escr_flag
1301 skip += 6;
1302 if(p[7] & 0x10) //es_rate_flag
1303 skip += 3;
1304 if(p[7] & 0x08)//dsm_trick_mode is unsupported, skip
1306 skip = 0;//don't let's parse the extension fields
1308 if(p[7] & 0x04) //additional_copy_info
1309 skip += 1;
1310 if(p[7] & 0x02) //pes_crc_flag
1311 skip += 2;
1312 if(skip >= pkt_len) //too few bytes
1313 return -1;
1314 flags = p[skip];
1315 skip++;
1316 if(flags & 0x80) //pes_private_data_flag
1317 skip += 16;
1318 if(skip >= pkt_len)
1319 return -1;
1320 if(flags & 0x40) //pack_header_field_flag
1322 unsigned char l = p[skip];
1323 skip += l;
1325 if(flags & 0x20) //program_packet_sequence_counter
1326 skip += 2;
1327 if(flags & 0x10) //p_std
1328 skip += 2;
1329 if(skip >= pkt_len)
1330 return -1;
1331 if(flags & 0x01) //finally the long desired pes_extension2
1333 unsigned char l = p[skip]; //ext2 flag+len
1334 skip++;
1335 if((l == 0x81) && (skip < pkt_len))
1337 int ssid = p[skip];
1338 mp_msg(MSGT_IDENTIFY, MSGL_V, "SUBSTREAM_ID=%d (0x%02X)\n", ssid, ssid);
1339 return ssid;
1343 return -1;
1346 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)
1348 unsigned char *p;
1349 uint32_t header_len;
1350 int64_t pts;
1351 uint32_t stream_id;
1352 uint32_t pkt_len, pes_is_aligned;
1354 //Here we are always at the start of a PES packet
1355 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2(%p, %d): \n", buf, (uint32_t) packet_len);
1357 if(packet_len == 0 || packet_len > 184)
1359 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2, BUFFER LEN IS TOO SMALL OR TOO BIG: %d EXIT\n", packet_len);
1360 return 0;
1363 p = buf;
1364 pkt_len = packet_len;
1367 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: HEADER %02x %02x %02x %02x\n", p[0], p[1], p[2], p[3]);
1368 if (p[0] || p[1] || (p[2] != 1))
1370 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: error HEADER %02x %02x %02x (should be 0x000001) \n", p[0], p[1], p[2]);
1371 return 0 ;
1374 packet_len -= 6;
1375 if(packet_len==0)
1377 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: packet too short: %d, exit\n", packet_len);
1378 return 0;
1381 es->payload_size = (p[4] << 8 | p[5]);
1382 pes_is_aligned = (p[6] & 4);
1384 stream_id = p[3];
1387 if (p[7] & 0x80)
1388 { /* pts available */
1389 pts = (int64_t)(p[9] & 0x0E) << 29 ;
1390 pts |= p[10] << 22 ;
1391 pts |= (p[11] & 0xFE) << 14 ;
1392 pts |= p[12] << 7 ;
1393 pts |= (p[13] & 0xFE) >> 1 ;
1395 es->pts = pts / 90000.0f;
1397 else
1398 es->pts = 0.0f;
1401 header_len = p[8];
1404 if (header_len + 9 > pkt_len) //9 are the bytes read up to the header_length field
1406 mp_msg(MSGT_DEMUX, MSGL_DBG2, "demux_ts: illegal value for PES_header_data_length (0x%02x)\n", header_len);
1407 return 0;
1410 if(stream_id==0xfd)
1412 int ssid = parse_pes_extension_fields(p, pkt_len);
1413 if((audio_substream_id!=-1) && (ssid != audio_substream_id))
1414 return 0;
1417 p += header_len + 9;
1418 packet_len -= header_len + 3;
1420 if(es->payload_size)
1421 es->payload_size -= header_len + 3;
1424 es->is_synced = 1; //only for SL streams we have to make sure it's really true, see below
1425 if (stream_id == 0xbd)
1427 mp_msg(MSGT_DEMUX, MSGL_DBG3, "pes_parse2: audio buf = %02X %02X %02X %02X %02X %02X %02X %02X, 80: %d\n",
1428 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[0] & 0x80);
1432 * we check the descriptor tag first because some stations
1433 * do not include any of the A52 header info in their audio tracks
1434 * these "raw" streams may begin with a byte that looks like a stream type.
1439 (type_from_pmt == AUDIO_A52) || /* A52 - raw */
1440 (p[0] == 0x0B && p[1] == 0x77) /* A52 - syncword */
1443 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 RAW OR SYNCWORD\n");
1444 es->start = p;
1445 es->size = packet_len;
1446 es->type = AUDIO_A52;
1447 es->payload_size -= packet_len;
1449 return 1;
1451 /* SPU SUBS */
1452 else if(type_from_pmt == SPU_DVB ||
1453 ((p[0] == 0x20) && pes_is_aligned)) // && p[1] == 0x00))
1455 es->start = p;
1456 es->size = packet_len;
1457 es->type = SPU_DVB;
1458 es->payload_size -= packet_len;
1460 return 1;
1462 else if (pes_is_aligned && ((p[0] & 0xE0) == 0x20)) //SPU_DVD
1464 //DVD SUBS
1465 es->start = p+1;
1466 es->size = packet_len-1;
1467 es->type = SPU_DVD;
1468 es->payload_size -= packet_len;
1470 return 1;
1472 else if (pes_is_aligned && (p[0] & 0xF8) == 0x80)
1474 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 WITH HEADER\n");
1475 es->start = p+4;
1476 es->size = packet_len - 4;
1477 es->type = AUDIO_A52;
1478 es->payload_size -= packet_len;
1480 return 1;
1482 else if (pes_is_aligned && ((p[0]&0xf0) == 0xa0))
1484 int pcm_offset;
1486 for (pcm_offset=0; ++pcm_offset < packet_len-1 ; )
1488 if (p[pcm_offset] == 0x01 && p[pcm_offset+1] == 0x80)
1489 { /* START */
1490 pcm_offset += 2;
1491 break;
1495 es->start = p + pcm_offset;
1496 es->size = packet_len - pcm_offset;
1497 es->type = AUDIO_LPCM_BE;
1498 es->payload_size -= packet_len;
1500 return 1;
1502 else
1504 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PES_PRIVATE1\n");
1505 es->start = p;
1506 es->size = packet_len;
1507 es->type = (type_from_pmt == UNKNOWN ? PES_PRIVATE1 : type_from_pmt);
1508 es->payload_size -= packet_len;
1510 return 1;
1513 else if(((stream_id >= 0xe0) && (stream_id <= 0xef)) || (stream_id == 0xfd && type_from_pmt != UNKNOWN))
1515 es->start = p;
1516 es->size = packet_len;
1517 if(type_from_pmt != UNKNOWN)
1518 es->type = type_from_pmt;
1519 else
1520 es->type = VIDEO_MPEG2;
1521 if(es->payload_size)
1522 es->payload_size -= packet_len;
1524 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: M2V size %d\n", es->size);
1525 return 1;
1527 else if ((stream_id == 0xfa))
1529 int l;
1531 es->is_synced = 0;
1532 if(type_from_pmt != UNKNOWN) //MP4 A/V or SL
1534 es->start = p;
1535 es->size = packet_len;
1536 es->type = type_from_pmt;
1538 if(type_from_pmt == SL_PES_STREAM)
1540 //if(pes_is_aligned)
1542 l = mp4_parse_sl_packet(pmt, p, packet_len, pid, es);
1543 mp_msg(MSGT_DEMUX, MSGL_DBG2, "L=%d, TYPE=%x\n", l, type_from_pmt);
1544 if(l < 0)
1546 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: couldn't parse SL header, passing along full PES payload\n");
1547 l = 0;
1551 es->start += l;
1552 es->size -= l;
1555 if(es->payload_size)
1556 es->payload_size -= packet_len;
1557 return 1;
1560 else if ((stream_id & 0xe0) == 0xc0)
1562 es->start = p;
1563 es->size = packet_len;
1565 if(type_from_pmt != UNKNOWN)
1566 es->type = type_from_pmt;
1567 else
1568 es->type = AUDIO_MP2;
1570 es->payload_size -= packet_len;
1572 return 1;
1574 else if (type_from_pmt != -1) //as a last resort here we trust the PMT, if present
1576 es->start = p;
1577 es->size = packet_len;
1578 es->type = type_from_pmt;
1579 es->payload_size -= packet_len;
1581 return 1;
1583 else
1585 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: unknown packet, id: %x\n", stream_id);
1588 es->is_synced = 0;
1589 return 0;
1595 static int ts_sync(stream_t *stream)
1597 int c=0;
1599 mp_msg(MSGT_DEMUX, MSGL_DBG3, "TS_SYNC \n");
1601 while(((c=stream_read_char(stream)) != 0x47) && ! stream->eof);
1603 if(c == 0x47)
1604 return c;
1605 else
1606 return 0;
1610 static void ts_dump_streams(ts_priv_t *priv)
1612 int i;
1614 for(i = 0; i < 3; i++)
1616 if((priv->fifo[i].pack != NULL) && (priv->fifo[i].offset != 0))
1618 resize_demux_packet(priv->fifo[i].pack, priv->fifo[i].offset);
1619 ds_add_packet(priv->fifo[i].ds, priv->fifo[i].pack);
1620 priv->fifo[i].offset = 0;
1621 priv->fifo[i].pack = NULL;
1627 static inline int32_t prog_idx_in_pat(ts_priv_t *priv, uint16_t progid)
1629 int x;
1631 if(priv->pat.progs == NULL)
1632 return -1;
1634 for(x = 0; x < priv->pat.progs_cnt; x++)
1636 if(priv->pat.progs[x].id == progid)
1637 return x;
1640 return -1;
1644 static inline int32_t prog_id_in_pat(ts_priv_t *priv, uint16_t pid)
1646 int x;
1648 if(priv->pat.progs == NULL)
1649 return -1;
1651 for(x = 0; x < priv->pat.progs_cnt; x++)
1653 if(priv->pat.progs[x].pmt_pid == pid)
1654 return priv->pat.progs[x].id;
1657 return -1;
1660 static int collect_section(ts_section_t *section, int is_start, unsigned char *buff, int size)
1662 uint8_t *ptr;
1663 uint16_t tlen;
1664 int skip, tid;
1666 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, start: %d, size: %d, collected: %d\n", is_start, size, section->buffer_len);
1667 if(! is_start && !section->buffer_len)
1668 return 0;
1670 if(is_start)
1672 if(! section->buffer)
1674 section->buffer = (uint8_t*) malloc(4096+256);
1675 if(section->buffer == NULL)
1676 return 0;
1678 section->buffer_len = 0;
1681 if(size + section->buffer_len > 4096+256)
1683 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, excessive len: %d + %d\n", section->buffer_len, size);
1684 return 0;
1687 memcpy(&(section->buffer[section->buffer_len]), buff, size);
1688 section->buffer_len += size;
1690 if(section->buffer_len < 3)
1691 return 0;
1693 skip = section->buffer[0];
1694 if(skip + 4 > section->buffer_len)
1695 return 0;
1697 ptr = &(section->buffer[skip + 1]);
1698 tid = ptr[0];
1699 tlen = ((ptr[1] & 0x0f) << 8) | ptr[2];
1700 mp_msg(MSGT_DEMUX, MSGL_V, "SKIP: %d+1, TID: %d, TLEN: %d, COLLECTED: %d\n", skip, tid, tlen, section->buffer_len);
1701 if(section->buffer_len < (skip+1+3+tlen))
1703 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DATA IS NOT ENOUGH, NEXT TIME\n");
1704 return 0;
1707 return skip+1;
1710 static int parse_pat(ts_priv_t * priv, int is_start, unsigned char *buff, int size)
1712 int skip;
1713 unsigned char *ptr;
1714 unsigned char *base;
1715 int entries, i;
1716 uint16_t progid;
1717 struct pat_progs_t *tmp;
1718 ts_section_t *section;
1720 section = &(priv->pat.section);
1721 skip = collect_section(section, is_start, buff, size);
1722 if(! skip)
1723 return 0;
1725 ptr = &(section->buffer[skip]);
1726 //PARSING
1727 priv->pat.table_id = ptr[0];
1728 if(priv->pat.table_id != 0)
1729 return 0;
1730 priv->pat.ssi = (ptr[1] >> 7) & 0x1;
1731 priv->pat.curr_next = ptr[5] & 0x01;
1732 priv->pat.ts_id = (ptr[3] << 8 ) | ptr[4];
1733 priv->pat.version_number = (ptr[5] >> 1) & 0x1F;
1734 priv->pat.section_length = ((ptr[1] & 0x03) << 8 ) | ptr[2];
1735 priv->pat.section_number = ptr[6];
1736 priv->pat.last_section_number = ptr[7];
1738 //check_crc32(0xFFFFFFFFL, ptr, priv->pat.buffer_len - 4, &ptr[priv->pat.buffer_len - 4]);
1739 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);
1741 entries = (int) (priv->pat.section_length - 9) / 4; //entries per section
1743 for(i=0; i < entries; i++)
1745 int32_t idx;
1746 base = &ptr[8 + i*4];
1747 progid = (base[0] << 8) | base[1];
1749 if((idx = prog_idx_in_pat(priv, progid)) == -1)
1751 int sz = sizeof(struct pat_progs_t) * (priv->pat.progs_cnt+1);
1752 tmp = realloc_struct(priv->pat.progs, priv->pat.progs_cnt+1, sizeof(struct pat_progs_t));
1753 if(tmp == NULL)
1755 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PAT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
1756 break;
1758 priv->pat.progs = tmp;
1759 idx = priv->pat.progs_cnt;
1760 priv->pat.progs_cnt++;
1763 priv->pat.progs[idx].id = progid;
1764 priv->pat.progs[idx].pmt_pid = ((base[2] & 0x1F) << 8) | base[3];
1765 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);
1766 mp_msg(MSGT_IDENTIFY, MSGL_V, "PROGRAM_ID=%d (0x%02X), PMT_PID: %d(0x%02X)\n",
1767 progid, progid, priv->pat.progs[idx].pmt_pid, priv->pat.progs[idx].pmt_pid);
1770 return 1;
1774 static inline int32_t es_pid_in_pmt(pmt_t * pmt, uint16_t pid)
1776 uint16_t i;
1778 if(pmt == NULL)
1779 return -1;
1781 if(pmt->es == NULL)
1782 return -1;
1784 for(i = 0; i < pmt->es_cnt; i++)
1786 if(pmt->es[i].pid == pid)
1787 return (int32_t) i;
1790 return -1;
1794 static uint16_t get_mp4_desc_len(uint8_t *buf, int *len)
1796 //uint16_t i = 0, size = 0;
1797 int i = 0, j, size = 0;
1799 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PARSE_MP4_DESC_LEN(%d), bytes: ", *len);
1800 j = FFMIN(*len, 4);
1801 while(i < j)
1803 mp_msg(MSGT_DEMUX, MSGL_DBG2, " %x ", buf[i]);
1804 size |= (buf[i] & 0x7f);
1805 if(!(buf[i] & 0x80))
1806 break;
1807 size <<= 7;
1808 i++;
1810 mp_msg(MSGT_DEMUX, MSGL_DBG2, ", SIZE=%d\n", size);
1812 *len = i+1;
1813 return size;
1817 static uint16_t parse_mp4_slconfig_descriptor(uint8_t *buf, int len, void *elem)
1819 int i = 0;
1820 mp4_es_descr_t *es;
1821 mp4_sl_config_t *sl;
1823 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_SLCONFIG_DESCRIPTOR(%d)\n", len);
1824 es = (mp4_es_descr_t *) elem;
1825 if(!es)
1827 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1828 return len;
1830 sl = &(es->sl);
1832 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;
1833 sl->ocr = sl->dts = sl->cts = 0;
1835 if(buf[0] == 0)
1837 i++;
1838 sl->flags = buf[i];
1839 i++;
1840 sl->ts_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1841 i += 4;
1842 sl->ocr_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1843 i += 4;
1844 sl->ts_len = buf[i];
1845 i++;
1846 sl->ocr_len = buf[i];
1847 i++;
1848 sl->au_len = buf[i];
1849 i++;
1850 sl->instant_bitrate_len = buf[i];
1851 i++;
1852 sl->degr_len = (buf[i] >> 4) & 0x0f;
1853 sl->au_seqnum_len = ((buf[i] & 0x0f) << 1) | ((buf[i+1] >> 7) & 0x01);
1854 i++;
1855 sl->packet_seqnum_len = ((buf[i] >> 2) & 0x1f);
1856 i++;
1859 else if(buf[0] == 1)
1861 sl->flags = 0;
1862 sl->ts_resolution = 1000;
1863 sl->ts_len = 32;
1864 i++;
1866 else if(buf[0] == 2)
1868 sl->flags = 4;
1869 i++;
1871 else
1873 sl->flags = 0;
1874 i++;
1877 sl->au_start = (sl->flags >> 7) & 0x1;
1878 sl->au_end = (sl->flags >> 6) & 0x1;
1879 sl->random_accesspoint = (sl->flags >> 5) & 0x1;
1880 sl->random_accesspoint_only = (sl->flags >> 4) & 0x1;
1881 sl->padding = (sl->flags >> 3) & 0x1;
1882 sl->use_ts = (sl->flags >> 2) & 0x1;
1883 sl->idle = (sl->flags >> 1) & 0x1;
1884 sl->duration = sl->flags & 0x1;
1886 if(sl->duration)
1888 sl->timescale = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1889 i += 4;
1890 sl->au_duration = (buf[i] << 8) | buf[i+1];
1891 i += 2;
1892 sl->cts_duration = (buf[i] << 8) | buf[i+1];
1893 i += 2;
1895 else //no support for fixed durations atm
1896 sl->timescale = sl->au_duration = sl->cts_duration = 0;
1898 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",
1899 len, buf[0], sl->flags, sl->use_ts, sl->ts_len, sl->timescale, (uint64_t) sl->dts, (uint64_t) sl->cts);
1901 return len;
1904 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem);
1906 static uint16_t parse_mp4_decoder_config_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
1908 int i = 0, j;
1909 mp4_es_descr_t *es;
1910 mp4_decoder_config_t *dec;
1912 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_CONFIG_DESCRIPTOR(%d)\n", len);
1913 es = (mp4_es_descr_t *) elem;
1914 if(!es)
1916 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1917 return len;
1919 dec = (mp4_decoder_config_t*) &(es->decoder);
1921 dec->object_type = buf[i];
1922 dec->stream_type = (buf[i+1]>>2) & 0x3f;
1924 if(dec->object_type == 1 && dec->stream_type == 1)
1926 dec->object_type = MP4_OD;
1927 dec->stream_type = MP4_OD;
1929 else if(dec->stream_type == 4)
1931 if(dec->object_type == 0x6a)
1932 dec->object_type = VIDEO_MPEG1;
1933 if(dec->object_type >= 0x60 && dec->object_type <= 0x65)
1934 dec->object_type = VIDEO_MPEG2;
1935 else if(dec->object_type == 0x20)
1936 dec->object_type = VIDEO_MPEG4;
1937 else if(dec->object_type == 0x21)
1938 dec->object_type = VIDEO_AVC;
1939 /*else if(dec->object_type == 0x22)
1940 fprintf(stderr, "TYPE 0x22\n");*/
1941 else dec->object_type = UNKNOWN;
1943 else if(dec->stream_type == 5)
1945 if(dec->object_type == 0x40)
1946 dec->object_type = AUDIO_AAC;
1947 else if(dec->object_type == 0x6b)
1948 dec->object_type = AUDIO_MP2;
1949 else if(dec->object_type >= 0x66 && dec->object_type <= 0x69)
1950 dec->object_type = AUDIO_MP2;
1951 else
1952 dec->object_type = UNKNOWN;
1954 else
1955 dec->object_type = dec->stream_type = UNKNOWN;
1957 if(dec->object_type != UNKNOWN)
1959 //update the type of the current stream
1960 for(j = 0; j < pmt->es_cnt; j++)
1962 if(pmt->es[j].mp4_es_id == es->id)
1964 pmt->es[j].type = SL_PES_STREAM;
1969 if(len > 13)
1970 parse_mp4_descriptors(pmt, &buf[13], len-13, dec);
1972 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);
1974 return len;
1977 static uint16_t parse_mp4_decoder_specific_descriptor(uint8_t *buf, int len, void *elem)
1979 int i;
1980 mp4_decoder_config_t *dec;
1982 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_SPECIFIC_DESCRIPTOR(%d)\n", len);
1983 dec = (mp4_decoder_config_t *) elem;
1984 if(!dec)
1986 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1987 return len;
1990 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4 SPECIFIC INFO BYTES: \n");
1991 for(i=0; i<len; i++)
1992 mp_msg(MSGT_DEMUX, MSGL_DBG2, "%02x ", buf[i]);
1993 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\n");
1995 if(len > MAX_EXTRADATA_SIZE)
1997 mp_msg(MSGT_DEMUX, MSGL_ERR, "DEMUX_TS, EXTRADATA SUSPICIOUSLY BIG: %d, REFUSED\r\n", len);
1998 return len;
2000 memcpy(dec->buf, buf, len);
2001 dec->buf_size = len;
2003 return len;
2006 static uint16_t parse_mp4_es_descriptor(pmt_t *pmt, uint8_t *buf, int len)
2008 int i = 0, j = 0, k, found;
2009 uint8_t flag;
2010 mp4_es_descr_t es, *target_es = NULL, *tmp;
2012 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4ES: len=%d\n", len);
2013 memset(&es, 0, sizeof(mp4_es_descr_t));
2014 while(i < len)
2016 es.id = (buf[i] << 8) | buf[i+1];
2017 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_ID: %d\n", es.id);
2018 i += 2;
2019 flag = buf[i];
2020 i++;
2021 if(flag & 0x80)
2022 i += 2;
2023 if(flag & 0x40)
2024 i += buf[i]+1;
2025 if(flag & 0x20) //OCR, maybe we need it
2026 i += 2;
2028 j = parse_mp4_descriptors(pmt, &buf[i], len-i, &es);
2029 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);
2030 if(es.decoder.object_type != UNKNOWN && es.decoder.stream_type != UNKNOWN)
2032 found = 0;
2033 //search this ES_ID if we already have it
2034 for(k=0; k < pmt->mp4es_cnt; k++)
2036 if(pmt->mp4es[k].id == es.id)
2038 target_es = &(pmt->mp4es[k]);
2039 found = 1;
2043 if(! found)
2045 tmp = realloc_struct(pmt->mp4es, pmt->mp4es_cnt+1, sizeof(mp4_es_descr_t));
2046 if(tmp == NULL)
2048 fprintf(stderr, "CAN'T REALLOC MP4_ES_DESCR\n");
2049 continue;
2051 pmt->mp4es = tmp;
2052 target_es = &(pmt->mp4es[pmt->mp4es_cnt]);
2053 pmt->mp4es_cnt++;
2055 memcpy(target_es, &es, sizeof(mp4_es_descr_t));
2056 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_CNT: %d, ID=%d\n", pmt->mp4es_cnt, target_es->id);
2059 i += j;
2062 return len;
2065 static void parse_mp4_object_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2067 int i, j = 0, id;
2069 i=0;
2070 id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2071 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_OBJECT_DESCRIPTOR: len=%d, OD_ID=%d\n", len, id);
2072 if(buf[1] & 0x20)
2074 i += buf[2] + 1; //url
2075 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2077 else
2079 i = 2;
2081 while(i < len)
2083 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2084 mp_msg(MSGT_DEMUX, MSGL_V, "OBJD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2085 i += j;
2091 static void parse_mp4_iod(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2093 int i, j = 0;
2094 mp4_od_t *iod = &(pmt->iod);
2096 iod->id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2097 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_IOD: len=%d, IOD_ID=%d\n", len, iod->id);
2098 i = 2;
2099 if(buf[1] & 0x20)
2101 i += buf[2] + 1; //url
2102 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2104 else
2106 i = 7;
2107 while(i < len)
2109 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2110 mp_msg(MSGT_DEMUX, MSGL_V, "IOD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2111 i += j;
2116 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2118 int tag, descr_len, i = 0, j = 0;
2120 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DESCRIPTORS, len=%d\n", len);
2121 if(! len)
2122 return len;
2124 while(i < len)
2126 tag = buf[i];
2127 j = len - i -1;
2128 descr_len = get_mp4_desc_len(&(buf[i+1]), &j);
2129 mp_msg(MSGT_DEMUX, MSGL_V, "TAG=%d (0x%x), DESCR_len=%d, len=%d, j=%d\n", tag, tag, descr_len, len, j);
2130 if(descr_len > len - j+1)
2132 mp_msg(MSGT_DEMUX, MSGL_V, "descriptor is too long, exit\n");
2133 return len;
2135 i += j+1;
2137 switch(tag)
2139 case 0x1:
2140 parse_mp4_object_descriptor(pmt, &(buf[i]), descr_len, elem);
2141 break;
2142 case 0x2:
2143 parse_mp4_iod(pmt, &(buf[i]), descr_len, elem);
2144 break;
2145 case 0x3:
2146 parse_mp4_es_descriptor(pmt, &(buf[i]), descr_len);
2147 break;
2148 case 0x4:
2149 parse_mp4_decoder_config_descriptor(pmt, &buf[i], descr_len, elem);
2150 break;
2151 case 0x05:
2152 parse_mp4_decoder_specific_descriptor(&buf[i], descr_len, elem);
2153 break;
2154 case 0x6:
2155 parse_mp4_slconfig_descriptor(&buf[i], descr_len, elem);
2156 break;
2157 default:
2158 mp_msg(MSGT_DEMUX, MSGL_V, "Unsupported mp4 descriptor 0x%x\n", tag);
2160 i += descr_len;
2163 return len;
2166 static ES_stream_t *new_pid(ts_priv_t *priv, int pid)
2168 ES_stream_t *tss;
2170 tss = malloc(sizeof(ES_stream_t));
2171 if(! tss)
2172 return NULL;
2173 memset(tss, 0, sizeof(ES_stream_t));
2174 tss->pid = pid;
2175 tss->last_cc = -1;
2176 tss->type = UNKNOWN;
2177 tss->subtype = UNKNOWN;
2178 tss->is_synced = 0;
2179 tss->extradata = NULL;
2180 tss->extradata_alloc = tss->extradata_len = 0;
2181 priv->ts.pids[pid] = tss;
2183 return tss;
2187 static int parse_program_descriptors(pmt_t *pmt, uint8_t *buf, uint16_t len)
2189 uint16_t i = 0, k, olen = len;
2191 while(len > 0)
2193 mp_msg(MSGT_DEMUX, MSGL_V, "PROG DESCR, TAG=%x, LEN=%d(%x)\n", buf[i], buf[i+1], buf[i+1]);
2194 if(buf[i+1] > len-2)
2196 mp_msg(MSGT_DEMUX, MSGL_V, "ERROR, descriptor len is too long, skipping\n");
2197 return olen;
2200 if(buf[i] == 0x1d)
2202 if(buf[i+3] == 2) //buggy versions of vlc muxer make this non-standard mess (missing iod_scope)
2203 k = 3;
2204 else
2205 k = 4; //this is standard compliant
2206 parse_mp4_descriptors(pmt, &buf[i+k], (int) buf[i+1]-(k-2), NULL);
2209 len -= 2 + buf[i+1];
2212 return olen;
2215 static int parse_descriptors(struct pmt_es_t *es, uint8_t *ptr)
2217 int j, descr_len, len;
2219 j = 0;
2220 len = es->descr_length;
2221 while(len > 2)
2223 descr_len = ptr[j+1];
2224 mp_msg(MSGT_DEMUX, MSGL_V, "...descr id: 0x%x, len=%d\n", ptr[j], descr_len);
2225 if(descr_len > len)
2227 mp_msg(MSGT_DEMUX, MSGL_ERR, "INVALID DESCR LEN for tag %02x: %d vs %d max, EXIT LOOP\n", ptr[j], descr_len, len);
2228 return -1;
2232 if(ptr[j] == 0x6a || ptr[j] == 0x7a) //A52 Descriptor
2234 if(es->type == 0x6)
2236 es->type = AUDIO_A52;
2237 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB A52 Descriptor\n");
2240 else if(ptr[j] == 0x7b) //DVB DTS Descriptor
2242 if(es->type == 0x6)
2244 es->type = AUDIO_DTS;
2245 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB DTS Descriptor\n");
2248 else if(ptr[j] == 0x59) //Subtitling Descriptor
2250 uint8_t subtype;
2252 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Subtitling Descriptor\n");
2253 if(descr_len < 8)
2255 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Descriptor length too short for DVB Subtitle Descriptor: %d, SKIPPING\n", descr_len);
2257 else
2259 memcpy(es->lang, &ptr[j+2], 3);
2260 es->lang[3] = 0;
2261 subtype = ptr[j+5];
2263 (subtype >= 0x10 && subtype <= 0x13) ||
2264 (subtype >= 0x20 && subtype <= 0x23)
2267 es->type = SPU_DVB;
2268 //page parameters: compo page 2 bytes, ancillary page 2 bytes
2270 else
2271 es->type = UNKNOWN;
2274 else if(ptr[j] == 0x50) //Component Descriptor
2276 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Component Descriptor\n");
2277 memcpy(es->lang, &ptr[j+5], 3);
2278 es->lang[3] = 0;
2280 else if(ptr[j] == 0xa) //Language Descriptor
2282 memcpy(es->lang, &ptr[j+2], 3);
2283 es->lang[3] = 0;
2284 mp_msg(MSGT_DEMUX, MSGL_V, "Language Descriptor: %s\n", es->lang);
2286 else if(ptr[j] == 0x5) //Registration Descriptor (looks like e fourCC :) )
2288 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor\n");
2289 if(descr_len < 4)
2291 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor length too short: %d, SKIPPING\n", descr_len);
2293 else
2295 char *d;
2296 memcpy(es->format_descriptor, &ptr[j+2], 4);
2297 es->format_descriptor[4] = 0;
2299 d = &ptr[j+2];
2300 if(d[0] == 'A' && d[1] == 'C' && d[2] == '-' && d[3] == '3')
2302 es->type = AUDIO_A52;
2304 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '1')
2306 es->type = AUDIO_DTS;
2308 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '2')
2310 es->type = AUDIO_DTS;
2312 else if(d[0] == 'V' && d[1] == 'C' && d[2] == '-' && d[3] == '1')
2314 es->type = VIDEO_VC1;
2316 else
2317 es->type = UNKNOWN;
2318 mp_msg(MSGT_DEMUX, MSGL_DBG2, "FORMAT %s\n", es->format_descriptor);
2321 else if(ptr[j] == 0x1e)
2323 es->mp4_es_id = (ptr[j+2] << 8) | ptr[j+3];
2324 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);
2326 else
2327 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Unknown descriptor 0x%x, SKIPPING\n", ptr[j]);
2329 len -= 2 + descr_len;
2330 j += 2 + descr_len;
2333 return 1;
2336 static int parse_sl_section(pmt_t *pmt, ts_section_t *section, int is_start, unsigned char *buff, int size)
2338 int tid, len, skip;
2339 uint8_t *ptr;
2340 skip = collect_section(section, is_start, buff, size);
2341 if(! skip)
2342 return 0;
2344 ptr = &(section->buffer[skip]);
2345 tid = ptr[0];
2346 len = ((ptr[1] & 0x0f) << 8) | ptr[2];
2347 mp_msg(MSGT_DEMUX, MSGL_V, "TABLEID: %d (av. %d), skip=%d, LEN: %d\n", tid, section->buffer_len, skip, len);
2348 if(len > 4093 || section->buffer_len < len || tid != 5)
2350 mp_msg(MSGT_DEMUX, MSGL_V, "SECTION TOO LARGE or wrong section type, EXIT\n");
2351 return 0;
2354 if(! (ptr[5] & 1))
2355 return 0;
2357 //8 is the current position, len - 9 is the amount of data available
2358 parse_mp4_descriptors(pmt, &ptr[8], len - 9, NULL);
2360 return 1;
2363 static int parse_pmt(ts_priv_t * priv, uint16_t progid, uint16_t pid, int is_start, unsigned char *buff, int size)
2365 unsigned char *base, *es_base;
2366 pmt_t *pmt;
2367 int32_t idx, es_count, section_bytes;
2368 uint8_t m=0;
2369 int skip;
2370 pmt_t *tmp;
2371 struct pmt_es_t *tmp_es;
2372 ts_section_t *section;
2373 ES_stream_t *tss;
2375 idx = progid_idx_in_pmt(priv, progid);
2377 if(idx == -1)
2379 int sz = (priv->pmt_cnt + 1) * sizeof(pmt_t);
2380 tmp = realloc_struct(priv->pmt, priv->pmt_cnt + 1, sizeof(pmt_t));
2381 if(tmp == NULL)
2383 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
2384 return 0;
2386 priv->pmt = tmp;
2387 idx = priv->pmt_cnt;
2388 memset(&(priv->pmt[idx]), 0, sizeof(pmt_t));
2389 priv->pmt_cnt++;
2390 priv->pmt[idx].progid = progid;
2393 pmt = &(priv->pmt[idx]);
2395 section = &(pmt->section);
2396 skip = collect_section(section, is_start, buff, size);
2397 if(! skip)
2398 return 0;
2400 base = &(section->buffer[skip]);
2402 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",
2403 progid, pmt->section.buffer_len, is_start, pid, size, m, pmt->es_cnt, idx, pmt);
2405 pmt->table_id = base[0];
2406 if(pmt->table_id != 2)
2407 return -1;
2408 pmt->ssi = base[1] & 0x80;
2409 pmt->section_length = (((base[1] & 0xf) << 8 ) | base[2]);
2410 pmt->version_number = (base[5] >> 1) & 0x1f;
2411 pmt->curr_next = (base[5] & 1);
2412 pmt->section_number = base[6];
2413 pmt->last_section_number = base[7];
2414 pmt->PCR_PID = ((base[8] & 0x1f) << 8 ) | base[9];
2415 pmt->prog_descr_length = ((base[10] & 0xf) << 8 ) | base[11];
2416 if(pmt->prog_descr_length > pmt->section_length - 9)
2418 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, INVALID PROG_DESCR LENGTH (%d vs %d)\n", pmt->prog_descr_length, pmt->section_length - 9);
2419 return -1;
2422 if(pmt->prog_descr_length)
2423 parse_program_descriptors(pmt, &base[12], pmt->prog_descr_length);
2425 es_base = &base[12 + pmt->prog_descr_length]; //the beginning of th ES loop
2427 section_bytes= pmt->section_length - 13 - pmt->prog_descr_length;
2428 es_count = 0;
2430 while(section_bytes >= 5)
2432 int es_pid, es_type;
2434 es_type = es_base[0];
2435 es_pid = ((es_base[1] & 0x1f) << 8) | es_base[2];
2437 idx = es_pid_in_pmt(pmt, es_pid);
2438 if(idx == -1)
2440 int sz = sizeof(struct pmt_es_t) * (pmt->es_cnt + 1);
2441 tmp_es = realloc_struct(pmt->es, pmt->es_cnt + 1, sizeof(struct pmt_es_t));
2442 if(tmp_es == NULL)
2444 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT, COULDN'T ALLOCATE %d bytes for PMT_ES\n", sz);
2445 continue;
2447 pmt->es = tmp_es;
2448 idx = pmt->es_cnt;
2449 memset(&(pmt->es[idx]), 0, sizeof(struct pmt_es_t));
2450 pmt->es_cnt++;
2453 pmt->es[idx].descr_length = ((es_base[3] & 0xf) << 8) | es_base[4];
2456 if(pmt->es[idx].descr_length > section_bytes - 5)
2458 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, ES_DESCR_LENGTH TOO LARGE %d > %d, EXIT\n",
2459 pmt->es[idx].descr_length, section_bytes - 5);
2460 return -1;
2464 pmt->es[idx].pid = es_pid;
2465 if(es_type != 0x6)
2466 pmt->es[idx].type = UNKNOWN;
2467 else
2468 pmt->es[idx].type = es_type;
2470 parse_descriptors(&pmt->es[idx], &es_base[5]);
2472 switch(es_type)
2474 case 1:
2475 pmt->es[idx].type = VIDEO_MPEG1;
2476 break;
2477 case 2:
2478 pmt->es[idx].type = VIDEO_MPEG2;
2479 break;
2480 case 3:
2481 case 4:
2482 pmt->es[idx].type = AUDIO_MP2;
2483 break;
2484 case 6:
2485 if(pmt->es[idx].type == 0x6) //this could have been ovrwritten by parse_descriptors
2486 pmt->es[idx].type = UNKNOWN;
2487 break;
2488 case 0x10:
2489 pmt->es[idx].type = VIDEO_MPEG4;
2490 break;
2491 case 0x0f:
2492 case 0x11:
2493 pmt->es[idx].type = AUDIO_AAC;
2494 break;
2495 case 0x1b:
2496 pmt->es[idx].type = VIDEO_H264;
2497 break;
2498 case 0x12:
2499 pmt->es[idx].type = SL_PES_STREAM;
2500 break;
2501 case 0x13:
2502 pmt->es[idx].type = SL_SECTION;
2503 break;
2504 case 0x81:
2505 pmt->es[idx].type = AUDIO_A52;
2506 break;
2507 case 0x8A:
2508 case 0x82:
2509 case 0x86:
2510 pmt->es[idx].type = AUDIO_DTS;
2511 break;
2512 case 0xEA:
2513 pmt->es[idx].type = VIDEO_VC1;
2514 break;
2515 default:
2516 mp_msg(MSGT_DEMUX, MSGL_DBG2, "UNKNOWN ES TYPE=0x%x\n", es_type);
2517 pmt->es[idx].type = UNKNOWN;
2520 tss = priv->ts.pids[es_pid]; //an ES stream
2521 if(tss == NULL)
2523 tss = new_pid(priv, es_pid);
2524 if(tss)
2525 tss->type = pmt->es[idx].type;
2528 section_bytes -= 5 + pmt->es[idx].descr_length;
2529 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",
2530 progid, idx, es_count, pmt->es[idx].pid, pmt->es[idx].pid, pmt->es[idx].type, pmt->es[idx].descr_length, section_bytes);
2533 es_base += 5 + pmt->es[idx].descr_length;
2535 es_count++;
2538 mp_msg(MSGT_DEMUX, MSGL_V, "----------------------------\n");
2539 return 1;
2542 static pmt_t* pmt_of_pid(ts_priv_t *priv, int pid, mp4_decoder_config_t **mp4_dec)
2544 int32_t i, j, k;
2546 if(priv->pmt)
2548 for(i = 0; i < priv->pmt_cnt; i++)
2550 if(priv->pmt[i].es && priv->pmt[i].es_cnt)
2552 for(j = 0; j < priv->pmt[i].es_cnt; j++)
2554 if(priv->pmt[i].es[j].pid == pid)
2556 //search mp4_es_id
2557 if(priv->pmt[i].es[j].mp4_es_id)
2559 for(k = 0; k < priv->pmt[i].mp4es_cnt; k++)
2561 if(priv->pmt[i].mp4es[k].id == priv->pmt[i].es[j].mp4_es_id)
2563 *mp4_dec = &(priv->pmt[i].mp4es[k].decoder);
2564 break;
2569 return &(priv->pmt[i]);
2576 return NULL;
2580 static inline int32_t pid_type_from_pmt(ts_priv_t *priv, int pid)
2582 int32_t pmt_idx, pid_idx, i, j;
2584 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2586 if(pmt_idx != -1)
2588 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2589 if(pid_idx != -1)
2590 return priv->pmt[pmt_idx].es[pid_idx].type;
2592 //else
2594 for(i = 0; i < priv->pmt_cnt; i++)
2596 pmt_t *pmt = &(priv->pmt[i]);
2597 for(j = 0; j < pmt->es_cnt; j++)
2598 if(pmt->es[j].pid == pid)
2599 return pmt->es[j].type;
2603 return UNKNOWN;
2607 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid)
2609 int32_t pmt_idx, pid_idx, i, j;
2611 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2613 if(pmt_idx != -1)
2615 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2616 if(pid_idx != -1)
2617 return priv->pmt[pmt_idx].es[pid_idx].lang;
2619 else
2621 for(i = 0; i < priv->pmt_cnt; i++)
2623 pmt_t *pmt = &(priv->pmt[i]);
2624 for(j = 0; j < pmt->es_cnt; j++)
2625 if(pmt->es[j].pid == pid)
2626 return pmt->es[j].lang;
2630 return NULL;
2634 static int fill_packet(demuxer_t *demuxer, demux_stream_t *ds, demux_packet_t **dp, int *dp_offset, TS_stream_info *si)
2636 int ret = 0;
2638 if((*dp != NULL) && (*dp_offset > 0))
2640 ret = *dp_offset;
2641 resize_demux_packet(*dp, ret); //shrinked to the right size
2642 ds_add_packet(ds, *dp);
2643 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);
2644 if(si)
2646 float diff = (*dp)->pts - si->last_pts;
2647 float dur;
2649 if(abs(diff) > 1) //1 second, there's a discontinuity
2651 si->duration += si->last_pts - si->first_pts;
2652 si->first_pts = si->last_pts = (*dp)->pts;
2654 else
2656 si->last_pts = (*dp)->pts;
2658 si->size += ret;
2659 dur = si->duration + (si->last_pts - si->first_pts);
2661 if(dur > 0 && ds == demuxer->video)
2663 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2664 if(dur > 1) //otherwise it may be unreliable
2665 priv->vbitrate = (uint32_t) ((float) si->size / dur);
2670 *dp = NULL;
2671 *dp_offset = 0;
2673 return ret;
2676 static int fill_extradata(mp4_decoder_config_t * mp4_dec, ES_stream_t *tss)
2678 uint8_t *tmp;
2680 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4_dec: %p, pid: %d\n", mp4_dec, tss->pid);
2682 if(mp4_dec->buf_size > tss->extradata_alloc)
2684 tmp = (uint8_t *) realloc(tss->extradata, mp4_dec->buf_size);
2685 if(!tmp)
2686 return 0;
2687 tss->extradata = tmp;
2688 tss->extradata_alloc = mp4_dec->buf_size;
2690 memcpy(tss->extradata, mp4_dec->buf, mp4_dec->buf_size);
2691 tss->extradata_len = mp4_dec->buf_size;
2692 mp_msg(MSGT_DEMUX, MSGL_V, "EXTRADATA: %p, alloc=%d, len=%d\n", tss->extradata, tss->extradata_alloc, tss->extradata_len);
2694 return tss->extradata_len;
2697 // 0 = EOF or no stream found
2698 // else = [-] number of bytes written to the packet
2699 static int ts_parse(demuxer_t *demuxer , ES_stream_t *es, unsigned char *packet, int probe)
2701 ES_stream_t *tss;
2702 uint8_t done = 0;
2703 int buf_size, is_start, pid, base;
2704 int len, cc, cc_ok, afc, retv = 0, is_video, is_audio, is_sub;
2705 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2706 stream_t *stream = demuxer->stream;
2707 char *p;
2708 demux_stream_t *ds = NULL;
2709 demux_packet_t **dp = NULL;
2710 int *dp_offset = 0, *buffer_size = 0;
2711 int32_t progid, pid_type, bad, ts_error;
2712 int junk = 0, rap_flag = 0;
2713 pmt_t *pmt;
2714 mp4_decoder_config_t *mp4_dec;
2715 TS_stream_info *si;
2718 while(! done)
2720 bad = ts_error = 0;
2721 ds = (demux_stream_t*) NULL;
2722 dp = (demux_packet_t **) NULL;
2723 dp_offset = buffer_size = NULL;
2724 rap_flag = 0;
2725 mp4_dec = NULL;
2726 es->is_synced = 0;
2727 es->lang[0] = 0;
2728 si = NULL;
2730 junk = priv->ts.packet_size - TS_PACKET_SIZE;
2731 buf_size = priv->ts.packet_size - junk;
2733 if(stream_eof(stream))
2735 if(! probe)
2737 ts_dump_streams(priv);
2738 demuxer->filepos = stream_tell(demuxer->stream);
2741 return 0;
2745 if(! ts_sync(stream))
2747 mp_msg(MSGT_DEMUX, MSGL_INFO, "TS_PARSE: COULDN'T SYNC\n");
2748 return 0;
2751 len = stream_read(stream, &packet[1], 3);
2752 if (len != 3)
2753 return 0;
2754 buf_size -= 4;
2756 if((packet[1] >> 7) & 0x01) //transport error
2757 ts_error = 1;
2760 is_start = packet[1] & 0x40;
2761 pid = ((packet[1] & 0x1f) << 8) | packet[2];
2763 tss = priv->ts.pids[pid]; //an ES stream
2764 if(tss == NULL)
2766 tss = new_pid(priv, pid);
2767 if(tss == NULL)
2768 continue;
2771 cc = (packet[3] & 0xf);
2772 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
2773 tss->last_cc = cc;
2775 bad = ts_error; // || (! cc_ok);
2776 if(bad)
2778 if(priv->keep_broken == 0)
2780 stream_skip(stream, buf_size-1+junk);
2781 continue;
2784 is_start = 0; //queued to the packet data
2787 if(is_start)
2788 tss->is_synced = 1;
2790 if((!is_start && !tss->is_synced) || ((pid > 1) && (pid < 16)) || (pid == 8191)) //invalid pid
2792 stream_skip(stream, buf_size-1+junk);
2793 continue;
2797 afc = (packet[3] >> 4) & 3;
2798 if(! (afc % 2)) //no payload in this TS packet
2800 stream_skip(stream, buf_size-1+junk);
2801 continue;
2804 if(afc > 1)
2806 int c;
2807 c = stream_read_char(stream);
2808 buf_size--;
2809 if(c < 0 || c > 183) //broken from the stream layer or invalid
2811 stream_skip(stream, buf_size-1+junk);
2812 continue;
2815 //c==0 is allowed!
2816 if(c > 0)
2818 uint8_t pcrbuf[188];
2819 int flags = stream_read_char(stream);
2820 int has_pcr;
2821 rap_flag = (flags & 0x40) >> 6;
2822 has_pcr = flags & 0x10;
2824 buf_size--;
2825 c--;
2826 stream_read(stream, pcrbuf, c);
2828 if(has_pcr)
2830 int pcr_pid = prog_pcr_pid(priv, priv->prog);
2831 if(pcr_pid == pid)
2833 uint64_t pcr, pcr_ext;
2835 pcr = (int64_t)(pcrbuf[0]) << 25;
2836 pcr |= pcrbuf[1] << 17 ;
2837 pcr |= (pcrbuf[2]) << 9;
2838 pcr |= pcrbuf[3] << 1 ;
2839 pcr |= (pcrbuf[4] & 0x80) >> 7;
2841 pcr_ext = (pcrbuf[4] & 0x01) << 8;
2842 pcr_ext |= pcrbuf[5];
2844 pcr = pcr * 300 + pcr_ext;
2846 demuxer->reference_clock = (double)pcr/(double)27000000.0;
2850 buf_size -= c;
2851 if(buf_size == 0)
2852 continue;
2856 //find the program that the pid belongs to; if (it's the right one or -1) && pid_type==SL_SECTION
2857 //call parse_sl_section()
2858 pmt = pmt_of_pid(priv, pid, &mp4_dec);
2859 if(mp4_dec)
2861 fill_extradata(mp4_dec, tss);
2862 if(IS_VIDEO(mp4_dec->object_type) || IS_AUDIO(mp4_dec->object_type))
2864 tss->type = SL_PES_STREAM;
2865 tss->subtype = mp4_dec->object_type;
2870 //TABLE PARSING
2872 base = priv->ts.packet_size - buf_size;
2874 priv->last_pid = pid;
2876 is_video = IS_VIDEO(tss->type) || (tss->type==SL_PES_STREAM && IS_VIDEO(tss->subtype));
2877 is_audio = IS_AUDIO(tss->type) || (tss->type==SL_PES_STREAM && IS_AUDIO(tss->subtype)) || (tss->type == PES_PRIVATE1);
2878 is_sub = ((tss->type == SPU_DVD) || (tss->type == SPU_DVB));
2879 pid_type = pid_type_from_pmt(priv, pid);
2881 // PES CONTENT STARTS HERE
2882 if(! probe)
2884 if((is_video || is_audio) && is_start && !priv->ts.streams[pid].sh)
2885 ts_add_stream(demuxer, tss);
2887 if((pid == demuxer->sub->id)) //or the lang is right
2889 pid_type = SPU_DVD;
2892 if(is_video && (demuxer->video->id == priv->ts.streams[pid].id))
2894 ds = demuxer->video;
2896 dp = &priv->fifo[1].pack;
2897 dp_offset = &priv->fifo[1].offset;
2898 buffer_size = &priv->fifo[1].buffer_size;
2899 si = &priv->vstr;
2901 else if(is_audio && (demuxer->audio->id == priv->ts.streams[pid].id))
2903 ds = demuxer->audio;
2905 dp = &priv->fifo[0].pack;
2906 dp_offset = &priv->fifo[0].offset;
2907 buffer_size = &priv->fifo[0].buffer_size;
2908 si = &priv->astr;
2910 else if(is_sub
2911 || (pid_type == SPU_DVD) || (pid_type == SPU_DVB))
2913 //SUBS are infrequent, so the initial detection may fail
2914 // and we may need to add them at play-time
2915 if(demuxer->sub->id == -1)
2917 uint16_t p;
2918 p = progid_for_pid(priv, tss->pid, priv->prog);
2920 if(p == priv->prog)
2922 int asgn = 0;
2923 uint8_t *lang;
2925 if(dvdsub_lang)
2927 if ((lang = pid_lang_from_pmt(priv, pid)))
2928 asgn = (strncmp(lang, dvdsub_lang, 3) == 0);
2930 else //no language specified with -slang
2931 asgn = 1;
2933 if(asgn)
2935 demuxer->sub->id = tss->pid;
2936 mp_msg(MSGT_DEMUX, MSGL_INFO, "CHOSEN SUBs pid 0x%x (%d) FROM PROG %d\n", tss->pid, tss->pid, priv->prog);
2941 if(demuxer->sub->id == tss->pid)
2943 ds = demuxer->sub;
2945 dp = &priv->fifo[2].pack;
2946 dp_offset = &priv->fifo[2].offset;
2947 buffer_size = &priv->fifo[2].buffer_size;
2949 else
2951 stream_skip(stream, buf_size+junk);
2952 continue;
2956 //IS IT TIME TO QUEUE DATA to the dp_packet?
2957 if(is_start && (dp != NULL))
2959 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
2963 if(dp && *dp == NULL)
2965 if(*buffer_size > MAX_PACK_BYTES)
2966 *buffer_size = MAX_PACK_BYTES;
2967 *dp = new_demux_packet(*buffer_size); //es->size
2968 *dp_offset = 0;
2969 if(! *dp)
2971 fprintf(stderr, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", *buffer_size);
2972 continue;
2974 mp_msg(MSGT_DEMUX, MSGL_DBG2, "CREATED DP(%d)\n", *buffer_size);
2979 if(probe || !dp) //dp is NULL for tables and sections
2981 p = &packet[base];
2983 else //feeding
2985 if(*dp_offset + buf_size > *buffer_size)
2987 *buffer_size = *dp_offset + buf_size + TS_FEC_PACKET_SIZE;
2988 resize_demux_packet(*dp, *buffer_size);
2990 p = &((*dp)->buffer[*dp_offset]);
2993 len = stream_read(stream, p, buf_size);
2994 if(len < buf_size)
2996 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\r\nts_parse() couldn't read enough data: %d < %d\r\n", len, buf_size);
2997 continue;
2999 stream_skip(stream, junk);
3001 if(pid == 0)
3003 parse_pat(priv, is_start, p, buf_size);
3004 continue;
3006 else if((tss->type == SL_SECTION) && pmt)
3008 int k, mp4_es_id = -1;
3009 ts_section_t *section;
3010 for(k = 0; k < pmt->mp4es_cnt; k++)
3012 if(pmt->mp4es[k].decoder.object_type == MP4_OD && pmt->mp4es[k].decoder.stream_type == MP4_OD)
3013 mp4_es_id = pmt->mp4es[k].id;
3015 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4ESID: %d\n", mp4_es_id);
3016 for(k = 0; k < pmt->es_cnt; k++)
3018 if(pmt->es[k].mp4_es_id == mp4_es_id)
3020 section = &(tss->section);
3021 parse_sl_section(pmt, section, is_start, &packet[base], buf_size);
3024 continue;
3026 else
3028 progid = prog_id_in_pat(priv, pid);
3029 if(progid != -1)
3031 if(pid != demuxer->video->id && pid != demuxer->audio->id && pid != demuxer->sub->id)
3033 parse_pmt(priv, progid, pid, is_start, &packet[base], buf_size);
3034 continue;
3036 else
3037 mp_msg(MSGT_DEMUX, MSGL_ERR, "Argh! Data pid %d used in the PMT, Skipping PMT parsing!\n", pid);
3041 if(!probe && !dp)
3042 continue;
3044 if(is_start)
3046 uint8_t *lang = NULL;
3048 mp_msg(MSGT_DEMUX, MSGL_DBG2, "IS_START\n");
3050 len = pes_parse2(p, buf_size, es, pid_type, pmt, pid);
3051 if(! len)
3053 tss->is_synced = 0;
3054 continue;
3056 es->pid = tss->pid;
3057 tss->is_synced |= es->is_synced || rap_flag;
3058 tss->payload_size = es->payload_size;
3060 if(is_audio && (lang = pid_lang_from_pmt(priv, es->pid)))
3062 memcpy(es->lang, lang, 3);
3063 es->lang[3] = 0;
3065 else
3066 es->lang[0] = 0;
3068 if(probe)
3070 if(es->type == UNKNOWN)
3071 return 0;
3073 tss->type = es->type;
3074 tss->subtype = es->subtype;
3076 return 1;
3078 else
3080 if(es->pts == 0.0f)
3081 es->pts = tss->pts = tss->last_pts;
3082 else
3083 tss->pts = tss->last_pts = es->pts;
3085 mp_msg(MSGT_DEMUX, MSGL_DBG2, "ts_parse, NEW pid=%d, PSIZE: %u, type=%X, start=%p, len=%d\n",
3086 es->pid, es->payload_size, es->type, es->start, es->size);
3088 demuxer->filepos = stream_tell(demuxer->stream) - es->size;
3090 memmove(p, es->start, es->size);
3091 *dp_offset += es->size;
3092 (*dp)->flags = 0;
3093 (*dp)->pos = stream_tell(demuxer->stream);
3094 (*dp)->pts = es->pts;
3096 if(retv > 0)
3097 return retv;
3098 else
3099 continue;
3102 else
3104 uint16_t sz;
3106 es->pid = tss->pid;
3107 es->type = tss->type;
3108 es->subtype = tss->subtype;
3109 es->pts = tss->pts = tss->last_pts;
3110 es->start = &packet[base];
3113 if(tss->payload_size > 0)
3115 sz = FFMIN(tss->payload_size, buf_size);
3116 tss->payload_size -= sz;
3117 es->size = sz;
3119 else
3121 if(is_video)
3123 sz = es->size = buf_size;
3125 else
3127 continue;
3132 if(! probe)
3134 *dp_offset += sz;
3136 if(*dp_offset >= MAX_PACK_BYTES)
3138 (*dp)->pts = tss->last_pts;
3139 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
3140 return 1;
3143 continue;
3145 else
3147 memcpy(es->start, p, sz);
3149 if(es->size)
3150 return es->size;
3151 else
3152 continue;
3157 return 0;
3161 void skip_audio_frame(sh_audio_t *sh_audio);
3163 static void reset_fifos(demuxer_t *demuxer, int a, int v, int s)
3165 ts_priv_t* priv = demuxer->priv;
3166 if(a)
3168 if(priv->fifo[0].pack != NULL)
3170 free_demux_packet(priv->fifo[0].pack);
3171 priv->fifo[0].pack = NULL;
3173 priv->fifo[0].offset = 0;
3176 if(v)
3178 if(priv->fifo[1].pack != NULL)
3180 free_demux_packet(priv->fifo[1].pack);
3181 priv->fifo[1].pack = NULL;
3183 priv->fifo[1].offset = 0;
3186 if(s)
3188 if(priv->fifo[2].pack != NULL)
3190 free_demux_packet(priv->fifo[2].pack);
3191 priv->fifo[2].pack = NULL;
3193 priv->fifo[2].offset = 0;
3195 demuxer->reference_clock = MP_NOPTS_VALUE;
3199 static void demux_seek_ts(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
3201 demux_stream_t *d_audio=demuxer->audio;
3202 demux_stream_t *d_video=demuxer->video;
3203 sh_audio_t *sh_audio=d_audio->sh;
3204 sh_video_t *sh_video=d_video->sh;
3205 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
3206 int i, video_stats;
3207 off_t newpos;
3209 //================= seek in MPEG-TS ==========================
3211 ts_dump_streams(demuxer->priv);
3212 reset_fifos(demuxer, sh_audio != NULL, sh_video != NULL, demuxer->sub->id > 0);
3214 demux_flush(demuxer);
3218 video_stats = (sh_video != NULL);
3219 if(video_stats)
3221 mp_msg(MSGT_DEMUX, MSGL_V, "IBPS: %d, vb: %d\r\n", sh_video->i_bps, priv->vbitrate);
3222 if(priv->vbitrate)
3223 video_stats = priv->vbitrate;
3224 else
3225 video_stats = sh_video->i_bps;
3228 newpos = (flags & SEEK_ABSOLUTE) ? demuxer->movi_start : demuxer->filepos;
3229 if(flags & SEEK_FACTOR) // float seek 0..1
3230 newpos+=(demuxer->movi_end-demuxer->movi_start)*rel_seek_secs;
3231 else
3233 // time seek (secs)
3234 if(! video_stats) // unspecified or VBR
3235 newpos += 2324*75*rel_seek_secs; // 174.3 kbyte/sec
3236 else
3237 newpos += video_stats*rel_seek_secs;
3241 if(newpos < demuxer->movi_start)
3242 newpos = demuxer->movi_start; //begininng of stream
3244 stream_seek(demuxer->stream, newpos);
3245 for(i = 0; i < 8192; i++)
3246 if(priv->ts.pids[i] != NULL)
3247 priv->ts.pids[i]->is_synced = 0;
3249 videobuf_code_len = 0;
3251 if(sh_video != NULL)
3252 ds_fill_buffer(d_video);
3254 if(sh_audio != NULL)
3256 ds_fill_buffer(d_audio);
3259 while(sh_video != NULL)
3261 if(sh_audio && !d_audio->eof && d_video->pts && d_audio->pts)
3263 float a_pts=d_audio->pts;
3264 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
3265 if(d_video->pts > a_pts)
3267 skip_audio_frame(sh_audio); // sync audio
3268 continue;
3273 i = sync_video_packet(d_video);
3274 if((sh_video->format == VIDEO_MPEG1) || (sh_video->format == VIDEO_MPEG2))
3276 if(i==0x1B3 || i==0x1B8) break; // found it!
3278 else if((sh_video->format == VIDEO_MPEG4) && (i==0x1B6))
3279 break;
3280 else if(sh_video->format == VIDEO_VC1 && (i==0x10E || i==0x10F))
3281 break;
3282 else //H264
3284 if((i & ~0x60) == 0x105 || (i & ~0x60) == 0x107) break;
3287 if(!i || !skip_video_packet(d_video)) break; // EOF?
3292 static int demux_ts_fill_buffer(demuxer_t * demuxer, demux_stream_t *ds)
3294 ES_stream_t es;
3295 ts_priv_t *priv = (ts_priv_t *)demuxer->priv;
3297 return -ts_parse(demuxer, &es, priv->packet, 0);
3301 static int ts_check_file_dmx(demuxer_t *demuxer)
3303 return ts_check_file(demuxer) ? DEMUXER_TYPE_MPEG_TS : 0;
3306 static int is_usable_program(ts_priv_t *priv, pmt_t *pmt)
3308 int j;
3310 for(j = 0; j < pmt->es_cnt; j++)
3312 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3313 continue;
3315 priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO ||
3316 priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO
3318 return 1;
3321 return 0;
3324 static int demux_ts_control(demuxer_t *demuxer, int cmd, void *arg)
3326 ts_priv_t* priv = (ts_priv_t *)demuxer->priv;
3328 switch(cmd)
3330 case DEMUXER_CTRL_SWITCH_AUDIO:
3331 case DEMUXER_CTRL_SWITCH_VIDEO:
3333 void *sh = NULL;
3334 int i, n;
3335 int reftype, areset = 0, vreset = 0;
3336 demux_stream_t *ds;
3338 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
3340 reftype = TYPE_VIDEO;
3341 ds = demuxer->video;
3342 vreset = 1;
3344 else
3346 reftype = TYPE_AUDIO;
3347 ds = demuxer->audio;
3348 areset = 1;
3350 n = *((int*)arg);
3351 if(n == -2)
3353 reset_fifos(demuxer, areset, vreset, 0);
3354 ds->id = -2;
3355 ds->sh = NULL;
3356 ds_free_packs(ds);
3357 *((int*)arg) = ds->id;
3358 return DEMUXER_CTRL_OK;
3361 if(n < 0)
3363 for(i = 0; i < 8192; i++)
3365 if(priv->ts.streams[i].id == ds->id && priv->ts.streams[i].type == reftype)
3366 break;
3369 while(!sh)
3371 i = (i+1) % 8192;
3372 if(priv->ts.streams[i].type == reftype)
3374 if(priv->ts.streams[i].id == ds->id) //we made a complete loop
3375 break;
3376 sh = priv->ts.streams[i].sh;
3380 else //audio track <n>
3382 if (n >= 8192 || priv->ts.streams[n].type != reftype) return DEMUXER_CTRL_NOTIMPL;
3383 i = n;
3384 sh = priv->ts.streams[i].sh;
3387 if(sh)
3389 if(ds->id != priv->ts.streams[i].id)
3390 reset_fifos(demuxer, areset, vreset, 0);
3391 ds->id = priv->ts.streams[i].id;
3392 ds->sh = sh;
3393 ds_free_packs(ds);
3394 mp_msg(MSGT_DEMUX, MSGL_V, "\r\ndemux_ts, switched to audio pid %d, id: %d, sh: %p\r\n", i, ds->id, sh);
3397 *((int*)arg) = ds->id;
3398 return DEMUXER_CTRL_OK;
3401 case DEMUXER_CTRL_IDENTIFY_PROGRAM: //returns in prog->{aid,vid} the new ids that comprise a program
3403 int i, j, cnt=0;
3404 int vid_done=0, aid_done=0;
3405 pmt_t *pmt = NULL;
3406 demux_program_t *prog = arg;
3408 if(priv->pmt_cnt < 2)
3409 return DEMUXER_CTRL_NOTIMPL;
3411 if(prog->progid == -1)
3413 int cur_pmt_idx = 0;
3415 for(i = 0; i < priv->pmt_cnt; i++)
3416 if(priv->pmt[i].progid == priv->prog)
3418 cur_pmt_idx = i;
3419 break;
3422 i = (cur_pmt_idx + 1) % priv->pmt_cnt;
3423 while(i != cur_pmt_idx)
3425 pmt = &priv->pmt[i];
3426 cnt = is_usable_program(priv, pmt);
3427 if(cnt)
3428 break;
3429 i = (i + 1) % priv->pmt_cnt;
3432 else
3434 for(i = 0; i < priv->pmt_cnt; i++)
3435 if(priv->pmt[i].progid == prog->progid)
3437 pmt = &priv->pmt[i]; //required program
3438 cnt = is_usable_program(priv, pmt);
3442 if(!cnt)
3443 return DEMUXER_CTRL_NOTIMPL;
3445 //finally some food
3446 prog->aid = prog->vid = -2; //no audio and no video by default
3447 for(j = 0; j < pmt->es_cnt; j++)
3449 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3450 continue;
3452 if(!vid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO)
3454 vid_done = 1;
3455 prog->vid = pmt->es[j].pid;
3457 else if(!aid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO)
3459 aid_done = 1;
3460 prog->aid = pmt->es[j].pid;
3464 priv->prog = prog->progid = pmt->progid;
3465 return DEMUXER_CTRL_OK;
3468 default:
3469 return DEMUXER_CTRL_NOTIMPL;
3474 const demuxer_desc_t demuxer_desc_mpeg_ts = {
3475 "MPEG-TS demuxer",
3476 "mpegts",
3477 "TS",
3478 "Nico Sabbi",
3480 DEMUXER_TYPE_MPEG_TS,
3481 0, // unsafe autodetect
3482 ts_check_file_dmx,
3483 demux_ts_fill_buffer,
3484 demux_open_ts,
3485 demux_close_ts,
3486 demux_seek_ts,
3487 demux_ts_control