Update to MPlayer SVN rev 29473 and FFmpeg SVN rev 19572.
[vaapi:athaifas-mplayer.git] / libmpdemux / demux_ts.c
1 /*
2  * Demultiplexer for MPEG2 Transport Streams.
3  *
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
7  *
8  * This file is part of MPlayer.
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "config.h"
30 #include "mp_msg.h"
31 #include "help_mp.h"
32
33 #include "stream/stream.h"
34 #include "demuxer.h"
35 #include "parse_es.h"
36 #include "stheader.h"
37
38 #include "ms_hdr.h"
39 #include "mpeg_hdr.h"
40
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
45
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
52
53 #ifndef SIZE_MAX
54 #define SIZE_MAX ((size_t)-1)
55 #endif
56
57 #define TYPE_AUDIO 1
58 #define TYPE_VIDEO 2
59
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
65
66 typedef enum
67 {
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         AUDIO_TRUEHD    = mmioFOURCC('T', 'R', 'H', 'D'),
81         SPU_DVD         = 0x3000000,
82         SPU_DVB         = 0x3000001,
83         PES_PRIVATE1    = 0xBD00000,
84         SL_PES_STREAM   = 0xD000000,
85         SL_SECTION      = 0xD100000,
86         MP4_OD          = 0xD200000,
87 } es_stream_type_t;
88
89 typedef struct {
90         uint8_t *buffer;
91         uint16_t buffer_len;
92 } ts_section_t;
93
94 typedef struct {
95         int size;
96         unsigned char *start;
97         uint16_t payload_size;
98         es_stream_type_t type, subtype;
99         float pts, last_pts;
100         int pid;
101         char lang[4];
102         int last_cc;                            // last cc code (-1 if first packet)
103         int is_synced;
104         ts_section_t section;
105         uint8_t *extradata;
106         int extradata_alloc, extradata_len;
107         struct {
108                 uint8_t au_start, au_end, last_au_end;
109         } sl;
110 } ES_stream_t;
111
112 typedef struct {
113         void *sh;
114         int id;
115         int type;
116 } sh_av_t;
117
118 typedef struct MpegTSContext {
119         int packet_size;                // raw packet size, including FEC if present e.g. 188 bytes
120         ES_stream_t *pids[NB_PID_MAX];
121         sh_av_t streams[NB_PID_MAX];
122 } MpegTSContext;
123
124
125 typedef struct {
126         demux_stream_t *ds;
127         demux_packet_t *pack;
128         int offset, buffer_size;
129 } av_fifo_t;
130
131 #define MAX_EXTRADATA_SIZE 64*1024
132 typedef struct {
133         int32_t object_type;    //aka codec used
134         int32_t stream_type;    //video, audio etc.
135         uint8_t buf[MAX_EXTRADATA_SIZE];
136         uint16_t buf_size;
137         uint8_t szm1;
138 } mp4_decoder_config_t;
139
140 typedef struct {
141         //flags
142         uint8_t flags;
143         uint8_t au_start;
144         uint8_t au_end;
145         uint8_t random_accesspoint;
146         uint8_t random_accesspoint_only;
147         uint8_t padding;
148         uint8_t use_ts;
149         uint8_t idle;
150         uint8_t duration;
151
152         uint32_t ts_resolution, ocr_resolution;
153         uint8_t ts_len, ocr_len, au_len, instant_bitrate_len, degr_len, au_seqnum_len, packet_seqnum_len;
154         uint32_t timescale;
155         uint16_t au_duration, cts_duration;
156         uint64_t ocr, dts, cts;
157 } mp4_sl_config_t;
158
159 typedef struct {
160         uint16_t id;
161         uint8_t flags;
162         mp4_decoder_config_t decoder;
163         mp4_sl_config_t sl;
164 } mp4_es_descr_t;
165
166 typedef struct {
167         uint16_t id;
168         uint8_t flags;
169         mp4_es_descr_t *es;
170         uint16_t es_cnt;
171 } mp4_od_t;
172
173 typedef struct {
174         uint8_t skip;
175         uint8_t table_id;
176         uint8_t ssi;
177         uint16_t section_length;
178         uint16_t ts_id;
179         uint8_t version_number;
180         uint8_t curr_next;
181         uint8_t section_number;
182         uint8_t last_section_number;
183         struct pat_progs_t {
184                 uint16_t id;
185                 uint16_t pmt_pid;
186         } *progs;
187         uint16_t progs_cnt;
188         ts_section_t section;
189 } pat_t;
190
191 typedef struct {
192         uint16_t progid;
193         uint8_t skip;
194         uint8_t table_id;
195         uint8_t ssi;
196         uint16_t section_length;
197         uint8_t version_number;
198         uint8_t curr_next;
199         uint8_t section_number;
200         uint8_t last_section_number;
201         uint16_t PCR_PID;
202         uint16_t prog_descr_length;
203         ts_section_t section;
204         uint16_t es_cnt;
205         struct pmt_es_t {
206                 uint16_t pid;
207                 uint32_t type;  //it's 8 bit long, but cast to the right type as FOURCC
208                 uint16_t descr_length;
209                 uint8_t format_descriptor[5];
210                 uint8_t lang[4];
211                 uint16_t mp4_es_id;
212         } *es;
213         mp4_od_t iod, *od;
214         mp4_es_descr_t *mp4es;
215         int od_cnt, mp4es_cnt;
216 } pmt_t;
217
218 typedef struct {
219         uint64_t size;
220         float duration;
221         float first_pts;
222         float last_pts;
223 } TS_stream_info;
224
225 typedef struct {
226         MpegTSContext ts;
227         int last_pid;
228         av_fifo_t fifo[3];      //0 for audio, 1 for video, 2 for subs
229         pat_t pat;
230         pmt_t *pmt;
231         uint16_t pmt_cnt;
232         uint32_t prog;
233         uint32_t vbitrate;
234         int keep_broken;
235         int last_aid;
236         int last_vid;
237         char packet[TS_FEC_PACKET_SIZE];
238         TS_stream_info vstr, astr;
239 } ts_priv_t;
240
241
242 typedef struct {
243         es_stream_type_t type;
244         ts_section_t section;
245 } TS_pids_t;
246
247
248 #define IS_AUDIO(x) (((x) == AUDIO_MP2) || ((x) == AUDIO_A52) || ((x) == AUDIO_LPCM_BE) || ((x) == AUDIO_AAC) || ((x) == AUDIO_DTS) || ((x) == AUDIO_TRUEHD))
249 #define IS_VIDEO(x) (((x) == VIDEO_MPEG1) || ((x) == VIDEO_MPEG2) || ((x) == VIDEO_MPEG4) || ((x) == VIDEO_H264) || ((x) == VIDEO_AVC)  || ((x) == VIDEO_VC1))
250
251 static int ts_parse(demuxer_t *demuxer, ES_stream_t *es, unsigned char *packet, int probe);
252
253 static uint8_t get_packet_size(const unsigned char *buf, int size)
254 {
255         int i;
256
257         if (size < (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS))
258                 return 0;
259
260         for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
261         {
262                 if (buf[i * TS_PACKET_SIZE] != 0x47)
263                 {
264                         mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
265                         goto try_fec;
266                 }
267         }
268         return TS_PACKET_SIZE;
269
270 try_fec:
271         for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
272         {
273                 if (buf[i * TS_FEC_PACKET_SIZE] != 0x47){
274                         mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
275                         goto try_philips;
276                 }
277         }
278         return TS_FEC_PACKET_SIZE;
279
280  try_philips:
281         for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
282         {
283                 if (buf[i * TS_PH_PACKET_SIZE] != 0x47)
284                 return 0;
285         }
286         return TS_PH_PACKET_SIZE;
287 }
288
289 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h);
290 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid);
291
292 static void ts_add_stream(demuxer_t * demuxer, ES_stream_t *es)
293 {
294         int i;
295         ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
296
297         if(priv->ts.streams[es->pid].sh)
298                 return;
299
300         if((IS_AUDIO(es->type) || IS_AUDIO(es->subtype)) && priv->last_aid+1 < MAX_A_STREAMS)
301         {
302                 sh_audio_t *sh = new_sh_audio_aid(demuxer, priv->last_aid, es->pid);
303                 if(sh)
304                 {
305                         const char *lang = pid_lang_from_pmt(priv, es->pid);
306                         sh->format = IS_AUDIO(es->type) ? es->type : es->subtype;
307                         sh->ds = demuxer->audio;
308
309                         priv->ts.streams[es->pid].id = priv->last_aid;
310                         priv->ts.streams[es->pid].sh = sh;
311                         priv->ts.streams[es->pid].type = TYPE_AUDIO;
312                         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);
313                         if (lang && lang[0])
314                                 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_AID_%d_LANG=%s\n", es->pid, lang);
315                         priv->last_aid++;
316                 }
317
318                 if(es->extradata && es->extradata_len)
319                 {
320                         sh->wf = (WAVEFORMATEX *) malloc(sizeof (WAVEFORMATEX) + es->extradata_len);
321                         sh->wf->cbSize = es->extradata_len;
322                         memcpy(sh->wf + 1, es->extradata, es->extradata_len);
323                 }
324         }
325
326         if((IS_VIDEO(es->type) || IS_VIDEO(es->subtype)) && priv->last_vid+1 < MAX_V_STREAMS)
327         {
328                 sh_video_t *sh = new_sh_video_vid(demuxer, priv->last_vid, es->pid);
329                 if(sh)
330                 {
331                         sh->format = IS_VIDEO(es->type) ? es->type : es->subtype;
332                         sh->ds = demuxer->video;
333
334                         priv->ts.streams[es->pid].id = priv->last_vid;
335                         priv->ts.streams[es->pid].sh = sh;
336                         priv->ts.streams[es->pid].type = TYPE_VIDEO;
337                         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);
338                         priv->last_vid++;
339
340
341                         if(sh->format == VIDEO_AVC && es->extradata && es->extradata_len)
342                         {
343                                 int w = 0, h = 0;
344                                 sh->bih = (BITMAPINFOHEADER *) calloc(1, sizeof(BITMAPINFOHEADER) + es->extradata_len);
345                                 sh->bih->biSize= sizeof(BITMAPINFOHEADER) + es->extradata_len;
346                                 sh->bih->biCompression = sh->format;
347                                 memcpy(sh->bih + 1, es->extradata, es->extradata_len);
348                                 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "EXTRADATA(%d BYTES): \n", es->extradata_len);
349                                 for(i = 0;i < es->extradata_len; i++)
350                                         mp_msg(MSGT_DEMUXER,MSGL_DBG2, "%02x ", (int) es->extradata[i]);
351                                 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"\n");
352                                 if(parse_avc_sps(es->extradata, es->extradata_len, &w, &h))
353                                 {
354                                         sh->bih->biWidth = w;
355                                         sh->bih->biHeight = h;
356                                 }
357                         }
358                 }
359         }
360 }
361
362 static int ts_check_file(demuxer_t * demuxer)
363 {
364         const int buf_size = (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS);
365         unsigned char buf[TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS], done = 0, *ptr;
366         uint32_t _read, i, count = 0, is_ts;
367         int cc[NB_PID_MAX], last_cc[NB_PID_MAX], pid, cc_ok, c, good, bad;
368         uint8_t size = 0;
369         off_t pos = 0;
370         off_t init_pos;
371
372         mp_msg(MSGT_DEMUX, MSGL_V, "Checking for MPEG-TS...\n");
373
374         init_pos = stream_tell(demuxer->stream);
375         is_ts = 0;
376         while(! done)
377         {
378                 i = 1;
379                 c = 0;
380
381                 while(((c=stream_read_char(demuxer->stream)) != 0x47)
382                         && (c >= 0)
383                         && (i < MAX_CHECK_SIZE)
384                         && ! demuxer->stream->eof
385                 ) i++;
386
387
388                 if(c != 0x47)
389                 {
390                         mp_msg(MSGT_DEMUX, MSGL_V, "THIS DOESN'T LOOK LIKE AN MPEG-TS FILE!\n");
391                         is_ts = 0;
392                         done = 1;
393                         continue;
394                 }
395
396                 pos = stream_tell(demuxer->stream) - 1;
397                 buf[0] = c;
398                 _read = stream_read(demuxer->stream, &buf[1], buf_size-1);
399
400                 if(_read < buf_size-1)
401                 {
402                         mp_msg(MSGT_DEMUX, MSGL_V, "COULDN'T READ ENOUGH DATA, EXITING TS_CHECK\n");
403                         stream_reset(demuxer->stream);
404                         return 0;
405                 }
406
407                 size = get_packet_size(buf, buf_size);
408                 if(size)
409                 {
410                         done = 1;
411                         is_ts = 1;
412                 }
413
414                 if(pos - init_pos >= MAX_CHECK_SIZE)
415                 {
416                         done = 1;
417                         is_ts = 0;
418                 }
419         }
420
421         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);
422         stream_seek(demuxer->stream, pos);
423
424         if(! is_ts)
425           return 0;
426
427         //LET'S CHECK continuity counters
428         good = bad = 0;
429         for(count = 0; count < NB_PID_MAX; count++)
430         {
431                 cc[count] = last_cc[count] = -1;
432         }
433
434         for(count = 0; count < NUM_CONSECUTIVE_TS_PACKETS; count++)
435         {
436                 ptr = &(buf[size * count]);
437                 pid = ((ptr[1] & 0x1f) << 8) | ptr[2];
438                 mp_msg(MSGT_DEMUX, MSGL_DBG2, "BUF: %02x %02x %02x %02x, PID %d, SIZE: %d \n",
439                 ptr[0], ptr[1], ptr[2], ptr[3], pid, size);
440
441                 if((pid == 8191) || (pid < 16))
442                         continue;
443
444                 cc[pid] = (ptr[3] & 0xf);
445                 cc_ok = (last_cc[pid] < 0) || ((((last_cc[pid] + 1) & 0x0f) == cc[pid]));
446                 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PID %d, COMPARE CC %d AND LAST_CC %d\n", pid, cc[pid], last_cc[pid]);
447                 if(! cc_ok)
448                         //return 0;
449                         bad++;
450                 else
451                         good++;
452
453                 last_cc[pid] = cc[pid];
454         }
455
456         mp_msg(MSGT_DEMUX, MSGL_V, "GOOD CC: %d, BAD CC: %d\n", good, bad);
457
458         if(good >= bad)
459                         return size;
460         else
461                         return 0;
462 }
463
464
465 static inline int32_t progid_idx_in_pmt(ts_priv_t *priv, uint16_t progid)
466 {
467         int x;
468
469         if(priv->pmt == NULL)
470                 return -1;
471
472         for(x = 0; x < priv->pmt_cnt; x++)
473         {
474                 if(priv->pmt[x].progid == progid)
475                         return x;
476         }
477
478         return -1;
479 }
480
481
482 static inline int32_t progid_for_pid(ts_priv_t *priv, int pid, int32_t req)             //finds the first program listing a pid
483 {
484         int i, j;
485         pmt_t *pmt;
486
487
488         if(priv->pmt == NULL)
489                 return -1;
490
491
492         for(i=0; i < priv->pmt_cnt; i++)
493         {
494                 pmt = &(priv->pmt[i]);
495
496                 if(pmt->es == NULL)
497                         return -1;
498
499                 for(j = 0; j < pmt->es_cnt; j++)
500                 {
501                         if(pmt->es[j].pid == pid)
502                         {
503                                 if((req == 0) || (req == pmt->progid))
504                                         return pmt->progid;
505                         }
506                 }
507
508         }
509         return -1;
510 }
511
512 static inline int32_t prog_pcr_pid(ts_priv_t *priv, int progid)
513 {
514         int i;
515
516         if(priv->pmt == NULL)
517                 return -1;
518         for(i=0; i < priv->pmt_cnt; i++)
519         {
520                 if(priv->pmt[i].progid == progid)
521                         return priv->pmt[i].PCR_PID;
522         }
523         return -1;
524 }
525
526
527 static inline int pid_match_lang(ts_priv_t *priv, uint16_t pid, char *lang)
528 {
529         uint16_t i, j;
530         pmt_t *pmt;
531
532         if(priv->pmt == NULL)
533                 return -1;
534
535         for(i=0; i < priv->pmt_cnt; i++)
536         {
537                 pmt = &(priv->pmt[i]);
538
539                 if(pmt->es == NULL)
540                         return -1;
541
542                 for(j = 0; j < pmt->es_cnt; j++)
543                 {
544                         if(pmt->es[j].pid != pid)
545                                 continue;
546
547                         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);
548                         if(strncmp(pmt->es[j].lang, lang, 3) == 0)
549                         {
550                                 return 1;
551                         }
552                 }
553
554         }
555
556         return -1;
557 }
558
559 typedef struct {
560         int32_t atype, vtype, stype;    //types
561         int32_t apid, vpid, spid;       //stream ids
562         char slang[4], alang[4];        //languages
563         uint16_t prog;
564         off_t probe;
565 } tsdemux_init_t;
566
567 //stripped down version of a52_syncinfo() from liba52
568 //copyright belongs to Michel Lespinasse <walken@zoy.org> and Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
569 int mp_a52_framesize(uint8_t * buf, int *srate)
570 {
571         int rate[] = {  32,  40,  48,  56,  64,  80,  96, 112,
572                         128, 160, 192, 224, 256, 320, 384, 448,
573                         512, 576, 640
574         };
575         uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
576         int frmsizecod, bitrate, half;
577
578         if((buf[0] != 0x0b) || (buf[1] != 0x77))        /* syncword */
579                 return 0;
580
581         if(buf[5] >= 0x60)              /* bsid >= 12 */
582                 return 0;
583
584         half = halfrate[buf[5] >> 3];
585
586         frmsizecod = buf[4] & 63;
587         if(frmsizecod >= 38)
588                 return 0;
589
590         bitrate = rate[frmsizecod >> 1];
591
592         switch(buf[4] & 0xc0)
593         {
594                 case 0: /* 48 KHz */
595                         *srate = 48000 >> half;
596                         return 4 * bitrate;
597                 case 0x40:      /* 44.1 KHz */
598                         *srate = 44100 >> half;
599                         return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
600                 case 0x80: /* 32 KHz */
601                         *srate = 32000 >> half;
602                         return 6 * bitrate;
603         }
604
605         return 0;
606 }
607
608 //second stage: returns the count of A52 syncwords found
609 static int a52_check(char *buf, int len)
610 {
611         int cnt, frame_length, ok, srate;
612
613         cnt = ok = 0;
614         if(len < 8)
615                 return 0;
616
617         while(cnt < len - 7)
618         {
619                 if(buf[cnt] == 0x0B && buf[cnt+1] == 0x77)
620                 {
621                         frame_length = mp_a52_framesize(&buf[cnt], &srate);
622                         if(frame_length>=7 && frame_length<=3840)
623                         {
624                                 cnt += frame_length;
625                                 ok++;
626                         }
627                         else
628                             cnt++;
629                 }
630                 else
631                         cnt++;
632         }
633
634         mp_msg(MSGT_DEMUXER, MSGL_V, "A52_CHECK(%d input bytes), found %d frame syncwords of %d bytes length\n", len, ok, frame_length);
635         return ok;
636 }
637
638
639 static off_t ts_detect_streams(demuxer_t *demuxer, tsdemux_init_t *param)
640 {
641         int video_found = 0, audio_found = 0, sub_found = 0, i, num_packets = 0, req_apid, req_vpid, req_spid;
642         int is_audio, is_video, is_sub, has_tables;
643         int32_t p, chosen_pid = 0;
644         off_t pos=0, ret = 0, init_pos, end_pos;
645         ES_stream_t es;
646         unsigned char tmp[TS_FEC_PACKET_SIZE];
647         ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
648         struct {
649                 char *buf;
650                 int pos;
651         } pes_priv1[8192], *pptr;
652         char *tmpbuf;
653
654         priv->last_pid = 8192;          //invalid pid
655
656         req_apid = param->apid;
657         req_vpid = param->vpid;
658         req_spid = param->spid;
659
660         has_tables = 0;
661         memset(pes_priv1, 0, sizeof(pes_priv1));
662         init_pos = stream_tell(demuxer->stream);
663         mp_msg(MSGT_DEMUXER, MSGL_V, "PROBING UP TO %"PRIu64", PROG: %d\n", (uint64_t) param->probe, param->prog);
664         end_pos = init_pos + (param->probe ? param->probe : TS_MAX_PROBE_SIZE);
665         while(1)
666         {
667                 pos = stream_tell(demuxer->stream);
668                 if(pos > end_pos || demuxer->stream->eof)
669                         break;
670
671                 if(ts_parse(demuxer, &es, tmp, 1))
672                 {
673                         //Non PES-aligned A52 audio may escape detection if PMT is not present;
674                         //in this case we try to find at least 3 A52 syncwords
675                         if((es.type == PES_PRIVATE1) && (! audio_found) && req_apid > -2)
676                         {
677                                 pptr = &pes_priv1[es.pid];
678                                 if(pptr->pos < 64*1024)
679                                 {
680                                 tmpbuf = (char*) realloc(pptr->buf, pptr->pos + es.size);
681                                 if(tmpbuf != NULL)
682                                 {
683                                         pptr->buf = tmpbuf;
684                                         memcpy(&(pptr->buf[ pptr->pos ]), es.start, es.size);
685                                         pptr->pos += es.size;
686                                         if(a52_check(pptr->buf, pptr->pos) > 2)
687                                         {
688                                                 param->atype = AUDIO_A52;
689                                                 param->apid = es.pid;
690                                                 es.type = AUDIO_A52;
691                                         }
692                                 }
693                                 }
694                         }
695
696                         is_audio = IS_AUDIO(es.type) || ((es.type==SL_PES_STREAM) && IS_AUDIO(es.subtype));
697                         is_video = IS_VIDEO(es.type) || ((es.type==SL_PES_STREAM) && IS_VIDEO(es.subtype));
698                         is_sub   = ((es.type == SPU_DVD) || (es.type == SPU_DVB));
699
700
701                         if((! is_audio) && (! is_video) && (! is_sub))
702                                 continue;
703                         if(is_audio && req_apid==-2)
704                                 continue;
705
706                         if(is_video)
707                         {
708                                 chosen_pid = (req_vpid == es.pid);
709                                 if((! chosen_pid) && (req_vpid > 0))
710                                         continue;
711                         }
712                         else if(is_audio)
713                         {
714                                 if(req_apid > 0)
715                                 {
716                                         chosen_pid = (req_apid == es.pid);
717                                         if(! chosen_pid)
718                                                 continue;
719                                 }
720                                 else if(param->alang[0] > 0 && es.lang[0] > 0)
721                                 {
722                                         if(pid_match_lang(priv, es.pid, param->alang) == -1)
723                                                 continue;
724
725                                         chosen_pid = 1;
726                                         param->apid = req_apid = es.pid;
727                                 }
728                         }
729                         else if(is_sub)
730                         {
731                                 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_SUBTITLE_ID=%d\n", es.pid);
732                                 if (es.lang[0] > 0)
733                                         mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_SID_%d_LANG=%s\n", es.pid, es.lang);
734                                 chosen_pid = (req_spid == es.pid);
735                                 if((! chosen_pid) && (req_spid > 0))
736                                         continue;
737                         }
738
739                         if(req_apid < 0 && (param->alang[0] == 0) && req_vpid < 0 && req_spid < 0)
740                                 chosen_pid = 1;
741
742                         if((ret == 0) && chosen_pid)
743                         {
744                                 ret = stream_tell(demuxer->stream);
745                         }
746
747                         p = progid_for_pid(priv, es.pid, param->prog);
748                         if(p != -1)
749                         {
750                                 has_tables++;
751                                 if(!param->prog && chosen_pid)
752                                         param->prog = p;
753                         }
754
755                         if((param->prog > 0) && (param->prog != p))
756                         {
757                                 if(audio_found)
758                                 {
759                                         if(is_video && (req_vpid == es.pid))
760                                         {
761                                                 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
762                                                 param->vpid = es.pid;
763                                                 video_found = 1;
764                                                 break;
765                                         }
766                                 }
767
768                                 if(video_found)
769                                 {
770                                         if(is_audio && (req_apid == es.pid))
771                                         {
772                                                 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
773                                                 param->apid = es.pid;
774                                                 audio_found = 1;
775                                                 break;
776                                         }
777                                 }
778
779
780                                 continue;
781                         }
782
783
784                         mp_msg(MSGT_DEMUXER, MSGL_DBG2, "TYPE: %x, PID: %d, PROG FOUND: %d\n", es.type, es.pid, param->prog);
785
786
787                         if(is_video)
788                         {
789                                 if((req_vpid == -1) || (req_vpid == es.pid))
790                                 {
791                                         param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
792                                         param->vpid = es.pid;
793                                         video_found = 1;
794                                 }
795                         }
796
797
798                         if(((req_vpid == -2) || (num_packets >= NUM_CONSECUTIVE_AUDIO_PACKETS)) && audio_found && !param->probe)
799                         {
800                                 //novideo or we have at least 348 audio packets (64 KB) without video (TS with audio only)
801                                 param->vtype = 0;
802                                 break;
803                         }
804
805                         if(is_sub)
806                         {
807                                 if((req_spid == -1) || (req_spid == es.pid))
808                                 {
809                                         param->stype = es.type;
810                                         param->spid = es.pid;
811                                         sub_found = 1;
812                                 }
813                         }
814
815                         if(is_audio)
816                         {
817                                 if((req_apid == -1) || (req_apid == es.pid))
818                                 {
819                                         param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
820                                         param->apid = es.pid;
821                                         audio_found = 1;
822                                 }
823                         }
824
825                         if(audio_found && (param->apid == es.pid) && (! video_found))
826                                 num_packets++;
827
828                         if((has_tables==0) && (video_found && audio_found) && (pos >= 1000000))
829                                 break;
830                 }
831         }
832
833         for(i=0; i<8192; i++)
834         {
835                 if(pes_priv1[i].buf != NULL)
836                 {
837                         free(pes_priv1[i].buf);
838                         pes_priv1[i].buf = NULL;
839                         pes_priv1[i].pos = 0;
840                 }
841         }
842
843         if(video_found)
844         {
845                 if(param->vtype == VIDEO_MPEG1)
846                         mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG1(pid=%d) ", param->vpid);
847                 else if(param->vtype == VIDEO_MPEG2)
848                         mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG2(pid=%d) ", param->vpid);
849                 else if(param->vtype == VIDEO_MPEG4)
850                         mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG4(pid=%d) ", param->vpid);
851                 else if(param->vtype == VIDEO_H264)
852                         mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO H264(pid=%d) ", param->vpid);
853                 else if(param->vtype == VIDEO_VC1)
854                         mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO VC1(pid=%d) ", param->vpid);
855                 else if(param->vtype == VIDEO_AVC)
856                         mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO AVC(NAL-H264, pid=%d) ", param->vpid);
857         }
858         else
859         {
860                 param->vtype = UNKNOWN;
861                 //WE DIDN'T MATCH ANY VIDEO STREAM
862                 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO VIDEO! ");
863         }
864
865         if(param->atype == AUDIO_MP2)
866                 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO MPA(pid=%d)", param->apid);
867         else if(param->atype == AUDIO_A52)
868                 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO A52(pid=%d)", param->apid);
869         else if(param->atype == AUDIO_DTS)
870                 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO DTS(pid=%d)", param->apid);
871         else if(param->atype == AUDIO_LPCM_BE)
872                 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO LPCM(pid=%d)", param->apid);
873         else if(param->atype == AUDIO_AAC)
874                 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO AAC(pid=%d)", param->apid);
875         else if(param->atype == AUDIO_TRUEHD)
876                 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO TRUEHD(pid=%d)", param->apid);
877         else
878         {
879                 audio_found = 0;
880                 param->atype = UNKNOWN;
881                 //WE DIDN'T MATCH ANY AUDIO STREAM, SO WE FORCE THE DEMUXER TO IGNORE AUDIO
882                 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO AUDIO! ");
883         }
884
885         if(param->stype == SPU_DVD || param->stype == SPU_DVB)
886                 mp_msg(MSGT_DEMUXER, MSGL_INFO, " SUB %s(pid=%d) ", (param->stype==SPU_DVD ? "DVD" : "DVB"), param->spid);
887         else
888         {
889                 param->stype = UNKNOWN;
890                 mp_msg(MSGT_DEMUXER, MSGL_INFO, " NO SUBS (yet)! ");
891         }
892
893         if(video_found || audio_found)
894         {
895                 if(!param->prog)
896                 {
897                         p = progid_for_pid(priv, video_found ? param->vpid : param->apid, 0);
898                         if(p != -1)
899                                 param->prog = p;
900                 }
901
902                 if(demuxer->stream->eof && (ret == 0))
903                         ret = init_pos;
904                 mp_msg(MSGT_DEMUXER, MSGL_INFO, " PROGRAM N. %d\n", param->prog);
905         }
906         else
907                 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\n");
908
909
910         for(i=0; i<8192; i++)
911         {
912                 if(priv->ts.pids[i] != NULL)
913                 {
914                         priv->ts.pids[i]->payload_size = 0;
915                         priv->ts.pids[i]->pts = priv->ts.pids[i]->last_pts = 0;
916                         priv->ts.pids[i]->last_cc = -1;
917                         priv->ts.pids[i]->is_synced = 0;
918                 }
919         }
920
921         return ret;
922 }
923
924 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h)
925 {
926         int sps, sps_len;
927         unsigned char *ptr;
928         mp_mpeg_header_t picture;
929         if(len < 6)
930                 return 0;
931         sps = buf[5] & 0x1f;
932         if(!sps)
933                 return 0;
934         sps_len = (buf[6] << 8) | buf[7];
935         if(!sps_len || (sps_len > len - 8))
936                 return 0;
937         ptr = &(buf[8]);
938         picture.display_picture_width = picture.display_picture_height = 0;
939         h264_parse_sps(&picture, ptr, len - 8);
940         if(!picture.display_picture_width || !picture.display_picture_height)
941                 return 0;
942         *w = picture.display_picture_width;
943         *h = picture.display_picture_height;
944         return 1;
945 }
946
947 static demuxer_t *demux_open_ts(demuxer_t * demuxer)
948 {
949         int i;
950         uint8_t packet_size;
951         sh_video_t *sh_video;
952         sh_audio_t *sh_audio;
953         off_t start_pos;
954         tsdemux_init_t params;
955         ts_priv_t * priv = demuxer->priv;
956
957         mp_msg(MSGT_DEMUX, MSGL_V, "DEMUX OPEN, AUDIO_ID: %d, VIDEO_ID: %d, SUBTITLE_ID: %d,\n",
958                 demuxer->audio->id, demuxer->video->id, demuxer->sub->id);
959
960
961         demuxer->type= DEMUXER_TYPE_MPEG_TS;
962
963
964         stream_reset(demuxer->stream);
965
966         packet_size = ts_check_file(demuxer);
967         if(!packet_size)
968             return NULL;
969
970         priv = calloc(1, sizeof(ts_priv_t));
971         if(priv == NULL)
972         {
973                 mp_msg(MSGT_DEMUX, MSGL_FATAL, "DEMUX_OPEN_TS, couldn't allocate enough memory for ts->priv, exit\n");
974                 return NULL;
975         }
976
977         for(i=0; i < 8192; i++)
978         {
979             priv->ts.pids[i] = NULL;
980             priv->ts.streams[i].id = -3;
981         }
982         priv->pat.progs = NULL;
983         priv->pat.progs_cnt = 0;
984         priv->pat.section.buffer = NULL;
985         priv->pat.section.buffer_len = 0;
986
987         priv->pmt = NULL;
988         priv->pmt_cnt = 0;
989
990         priv->keep_broken = ts_keep_broken;
991         priv->ts.packet_size = packet_size;
992
993
994         demuxer->priv = priv;
995         if(demuxer->stream->type != STREAMTYPE_FILE)
996                 demuxer->seekable = 1;
997         else
998                 demuxer->seekable = 1;
999
1000
1001         params.atype = params.vtype = params.stype = UNKNOWN;
1002         params.apid = demuxer->audio->id;
1003         params.vpid = demuxer->video->id;
1004         params.spid = demuxer->sub->id;
1005         params.prog = ts_prog;
1006         params.probe = ts_probe;
1007
1008         if(dvdsub_lang != NULL)
1009         {
1010                 strncpy(params.slang, dvdsub_lang, 3);
1011                 params.slang[3] = 0;
1012         }
1013         else
1014                 memset(params.slang, 0, 4);
1015
1016         if(audio_lang != NULL)
1017         {
1018                 strncpy(params.alang, audio_lang, 3);
1019                 params.alang[3] = 0;
1020         }
1021         else
1022                 memset(params.alang, 0, 4);
1023
1024         start_pos = ts_detect_streams(demuxer, &params);
1025
1026         demuxer->sub->id = params.spid;
1027         priv->prog = params.prog;
1028
1029         if(params.vtype != UNKNOWN)
1030         {
1031                 ts_add_stream(demuxer, priv->ts.pids[params.vpid]);
1032                 sh_video = priv->ts.streams[params.vpid].sh;
1033                 demuxer->video->id = priv->ts.streams[params.vpid].id;
1034                 sh_video->ds = demuxer->video;
1035                 sh_video->format = params.vtype;
1036                 demuxer->video->sh = sh_video;
1037         }
1038
1039         if(params.atype != UNKNOWN)
1040         {
1041                 ES_stream_t *es = priv->ts.pids[params.apid];
1042
1043                 if(!IS_AUDIO(es->type) && !IS_AUDIO(es->subtype) && IS_AUDIO(params.atype)) es->subtype = params.atype;
1044                 ts_add_stream(demuxer, priv->ts.pids[params.apid]);
1045                 sh_audio = priv->ts.streams[params.apid].sh;
1046                 demuxer->audio->id = priv->ts.streams[params.apid].id;
1047                 sh_audio->ds = demuxer->audio;
1048                 sh_audio->format = params.atype;
1049                 demuxer->audio->sh = sh_audio;
1050         }
1051
1052
1053         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);
1054
1055
1056         start_pos = (start_pos <= priv->ts.packet_size ? 0 : start_pos - priv->ts.packet_size);
1057         demuxer->movi_start = start_pos;
1058         demuxer->reference_clock = MP_NOPTS_VALUE;
1059         stream_reset(demuxer->stream);
1060         stream_seek(demuxer->stream, start_pos);        //IF IT'S FROM A PIPE IT WILL FAIL, BUT WHO CARES?
1061
1062
1063         priv->last_pid = 8192;          //invalid pid
1064
1065         for(i = 0; i < 3; i++)
1066         {
1067                 priv->fifo[i].pack  = NULL;
1068                 priv->fifo[i].offset = 0;
1069         }
1070         priv->fifo[0].ds = demuxer->audio;
1071         priv->fifo[1].ds = demuxer->video;
1072         priv->fifo[2].ds = demuxer->sub;
1073
1074         priv->fifo[0].buffer_size = 1536;
1075         priv->fifo[1].buffer_size = 32767;
1076         priv->fifo[2].buffer_size = 32767;
1077
1078         priv->pat.section.buffer_len = 0;
1079         for(i = 0; i < priv->pmt_cnt; i++)
1080                 priv->pmt[i].section.buffer_len = 0;
1081
1082         demuxer->filepos = stream_tell(demuxer->stream);
1083         return demuxer;
1084 }
1085
1086 static void demux_close_ts(demuxer_t * demuxer)
1087 {
1088         uint16_t i;
1089         ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
1090
1091         if(priv)
1092         {
1093                 if(priv->pat.section.buffer)
1094                         free(priv->pat.section.buffer);
1095                 if(priv->pat.progs)
1096                         free(priv->pat.progs);
1097
1098                 if(priv->pmt)
1099                 {
1100                         for(i = 0; i < priv->pmt_cnt; i++)
1101                         {
1102                                 if(priv->pmt[i].section.buffer)
1103                                         free(priv->pmt[i].section.buffer);
1104                                 if(priv->pmt[i].es)
1105                                         free(priv->pmt[i].es);
1106                         }
1107                         free(priv->pmt);
1108                 }
1109                 free(priv);
1110         }
1111         demuxer->priv=NULL;
1112 }
1113
1114
1115 unsigned char mp_getbits(unsigned char*, unsigned int, unsigned char);
1116 #define getbits mp_getbits
1117
1118 static int mp4_parse_sl_packet(pmt_t *pmt, uint8_t *buf, uint16_t packet_len, int pid, ES_stream_t *pes_es)
1119 {
1120         int i, n, m, mp4_es_id = -1;
1121         uint64_t v = 0;
1122         uint32_t pl_size = 0;
1123         int deg_flag = 0;
1124         mp4_es_descr_t *es = NULL;
1125         mp4_sl_config_t *sl = NULL;
1126         uint8_t au_start = 0, au_end = 0, rap_flag = 0, ocr_flag = 0, padding = 0,  padding_bits = 0, idle = 0;
1127
1128         pes_es->is_synced = 0;
1129         mp_msg(MSGT_DEMUXER,MSGL_V, "mp4_parse_sl_packet, pid: %d, pmt: %pm, packet_len: %d\n", pid, pmt, packet_len);
1130         if(! pmt || !packet_len)
1131                 return 0;
1132
1133         for(i = 0; i < pmt->es_cnt; i++)
1134         {
1135                 if(pmt->es[i].pid == pid)
1136                         mp4_es_id = pmt->es[i].mp4_es_id;
1137         }
1138         if(mp4_es_id < 0)
1139                 return -1;
1140
1141         for(i = 0; i < pmt->mp4es_cnt; i++)
1142         {
1143                 if(pmt->mp4es[i].id == mp4_es_id)
1144                         es = &(pmt->mp4es[i]);
1145         }
1146         if(! es)
1147                 return -1;
1148
1149         pes_es->subtype = es->decoder.object_type;
1150
1151         sl = &(es->sl);
1152         if(!sl)
1153                 return -1;
1154
1155         //now es is the complete es_descriptor of out mp4 ES stream
1156         mp_msg(MSGT_DEMUXER,MSGL_DBG2, "ID: %d, FLAGS: 0x%x, subtype: %x\n", es->id, sl->flags, pes_es->subtype);
1157
1158         n = 0;
1159         if(sl->au_start)
1160                 pes_es->sl.au_start = au_start = getbits(buf, n++, 1);
1161         else
1162                 pes_es->sl.au_start = (pes_es->sl.last_au_end ? 1 : 0);
1163         if(sl->au_end)
1164                 pes_es->sl.au_end = au_end = getbits(buf, n++, 1);
1165
1166         if(!sl->au_start && !sl->au_end)
1167         {
1168                 pes_es->sl.au_start = pes_es->sl.au_end = au_start = au_end = 1;
1169         }
1170         pes_es->sl.last_au_end = pes_es->sl.au_end;
1171
1172
1173         if(sl->ocr_len > 0)
1174                 ocr_flag = getbits(buf, n++, 1);
1175         if(sl->idle)
1176                 idle = getbits(buf, n++, 1);
1177         if(sl->padding)
1178                 padding = getbits(buf, n++, 1);
1179         if(padding)
1180         {
1181                 padding_bits = getbits(buf, n, 3);
1182                 n += 3;
1183         }
1184
1185         if(idle || (padding && !padding_bits))
1186         {
1187                 pes_es->payload_size = 0;
1188                 return -1;
1189         }
1190
1191         //(! idle && (!padding || padding_bits != 0)) is true
1192         n += sl->packet_seqnum_len;
1193         if(sl->degr_len)
1194                 deg_flag = getbits(buf, n++, 1);
1195         if(deg_flag)
1196                 n += sl->degr_len;
1197
1198         if(ocr_flag)
1199         {
1200                 n += sl->ocr_len;
1201                 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "OCR: %d bits\n", sl->ocr_len);
1202         }
1203
1204         if(packet_len * 8 <= n)
1205                 return -1;
1206
1207         mp_msg(MSGT_DEMUXER,MSGL_DBG2, "\nAU_START: %d, AU_END: %d\n", au_start, au_end);
1208         if(au_start)
1209         {
1210                 int dts_flag = 0, cts_flag = 0, ib_flag = 0;
1211
1212                 if(sl->random_accesspoint)
1213                         rap_flag = getbits(buf, n++, 1);
1214
1215                 //check commented because it seems it's rarely used, and we need this flag set in case of au_start
1216                 //the decoder will eventually discard the payload if it can't decode it
1217                 //if(rap_flag || sl->random_accesspoint_only)
1218                         pes_es->is_synced = 1;
1219
1220                 n += sl->au_seqnum_len;
1221                 if(packet_len * 8 <= n+8)
1222                         return -1;
1223                 if(sl->use_ts)
1224                 {
1225                         dts_flag = getbits(buf, n++, 1);
1226                         cts_flag = getbits(buf, n++, 1);
1227                 }
1228                 if(sl->instant_bitrate_len)
1229                         ib_flag = getbits(buf, n++, 1);
1230                 if(packet_len * 8 <= n+8)
1231                         return -1;
1232                 if(dts_flag && (sl->ts_len > 0))
1233                 {
1234                         n += sl->ts_len;
1235                         mp_msg(MSGT_DEMUXER,MSGL_DBG2, "DTS: %d bits\n", sl->ts_len);
1236                 }
1237                 if(packet_len * 8 <= n+8)
1238                         return -1;
1239                 if(cts_flag && (sl->ts_len > 0))
1240                 {
1241                         int i = 0, m;
1242
1243                         while(i < sl->ts_len)
1244                         {
1245                                 m = FFMIN(8, sl->ts_len - i);
1246                                 v |= getbits(buf, n, m);
1247                                 if(sl->ts_len - i > 8)
1248                                         v <<= 8;
1249                                 i += m;
1250                                 n += m;
1251                                 if(packet_len * 8 <= n+8)
1252                                         return -1;
1253                         }
1254
1255                         pes_es->pts = (float) v / (float) sl->ts_resolution;
1256                         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                 }
1258
1259
1260                 i = 0;
1261                 pl_size = 0;
1262                 while(i < sl->au_len)
1263                 {
1264                         m = FFMIN(8, sl->au_len - i);
1265                         pl_size |= getbits(buf, n, m);
1266                         if(sl->au_len - i > 8)
1267                                 pl_size <<= 8;
1268                         i += m;
1269                         n += m;
1270                         if(packet_len * 8 <= n+8)
1271                                 return -1;
1272                 }
1273                 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "AU_LEN: %u (%d bits)\n", pl_size, sl->au_len);
1274                 if(ib_flag)
1275                         n += sl->instant_bitrate_len;
1276         }
1277
1278         m = (n+7)/8;
1279         if(0 < pl_size && pl_size < pes_es->payload_size)
1280                 pes_es->payload_size = pl_size;
1281
1282         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",
1283                 n, m, pes_es->payload_size, pl_size, (int) rap_flag, (int) sl->random_accesspoint_only);
1284
1285         return m;
1286 }
1287
1288 //this function parses the extension fields in the PES header and returns the substream_id, or -1 in case of errors
1289 static int parse_pes_extension_fields(unsigned char *p, int pkt_len)
1290 {
1291         int skip;
1292         unsigned char flags;
1293
1294         if(!(p[7] & 0x1))       //no extension_field
1295                 return -1;
1296         skip = 9;
1297         if(p[7] & 0x80)
1298         {
1299                 skip += 5;
1300                 if(p[7] & 0x40)
1301                         skip += 5;
1302         }
1303         if(p[7] & 0x20) //escr_flag
1304                 skip += 6;
1305         if(p[7] & 0x10) //es_rate_flag
1306                 skip += 3;
1307         if(p[7] & 0x08)//dsm_trick_mode is unsupported, skip
1308         {
1309                 skip = 0;//don't let's parse the extension fields
1310         }
1311         if(p[7] & 0x04) //additional_copy_info
1312                 skip += 1;
1313         if(p[7] & 0x02) //pes_crc_flag
1314                 skip += 2;
1315         if(skip >= pkt_len)     //too few bytes
1316                 return -1;
1317         flags = p[skip];
1318         skip++;
1319         if(flags & 0x80)        //pes_private_data_flag
1320                 skip += 16;
1321         if(skip >= pkt_len)
1322                 return -1;
1323         if(flags & 0x40)        //pack_header_field_flag
1324         {
1325                 unsigned char l = p[skip];
1326                 skip += l;
1327         }
1328         if(flags & 0x20)        //program_packet_sequence_counter
1329                 skip += 2;
1330         if(flags & 0x10)        //p_std
1331                 skip += 2;
1332         if(skip >= pkt_len)
1333                 return -1;
1334         if(flags & 0x01)        //finally the long desired pes_extension2
1335         {
1336                 unsigned char l = p[skip];      //ext2 flag+len
1337                 skip++;
1338                 if((l == 0x81) && (skip < pkt_len))
1339                 {
1340                         int ssid = p[skip];
1341                         mp_msg(MSGT_IDENTIFY, MSGL_V, "SUBSTREAM_ID=%d (0x%02X)\n", ssid, ssid);
1342                         return ssid;
1343                 }
1344         }
1345
1346         return -1;
1347 }
1348
1349 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)
1350 {
1351         unsigned char  *p;
1352         uint32_t       header_len;
1353         int64_t        pts;
1354         uint32_t       stream_id;
1355         uint32_t       pkt_len, pes_is_aligned;
1356
1357         //Here we are always at the start of a PES packet
1358         mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2(%p, %d): \n", buf, (uint32_t) packet_len);
1359
1360         if(packet_len == 0 || packet_len > 184)
1361         {
1362                 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2, BUFFER LEN IS TOO SMALL OR TOO BIG: %d EXIT\n", packet_len);
1363                 return 0;
1364         }
1365
1366         p = buf;
1367         pkt_len = packet_len;
1368
1369
1370         mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: HEADER %02x %02x %02x %02x\n", p[0], p[1], p[2], p[3]);
1371         if (p[0] || p[1] || (p[2] != 1))
1372         {
1373                 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: error HEADER %02x %02x %02x (should be 0x000001) \n", p[0], p[1], p[2]);
1374                 return 0 ;
1375         }
1376
1377         packet_len -= 6;
1378         if(packet_len==0)
1379         {
1380                 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: packet too short: %d, exit\n", packet_len);
1381                 return 0;
1382         }
1383
1384         es->payload_size = (p[4] << 8 | p[5]);
1385         pes_is_aligned = (p[6] & 4);
1386
1387         stream_id  = p[3];
1388
1389
1390         if (p[7] & 0x80)
1391         {       /* pts available */
1392                 pts  = (int64_t)(p[9] & 0x0E) << 29 ;
1393                 pts |=  p[10]         << 22 ;
1394                 pts |= (p[11] & 0xFE) << 14 ;
1395                 pts |=  p[12]         <<  7 ;
1396                 pts |= (p[13] & 0xFE) >>  1 ;
1397
1398                 es->pts = pts / 90000.0f;
1399         }
1400         else
1401                 es->pts = 0.0f;
1402
1403
1404         header_len = p[8];
1405
1406
1407         if (header_len + 9 > pkt_len) //9 are the bytes read up to the header_length field
1408         {
1409                 mp_msg(MSGT_DEMUX, MSGL_DBG2, "demux_ts: illegal value for PES_header_data_length (0x%02x)\n", header_len);
1410                 return 0;
1411         }
1412
1413         if(stream_id==0xfd)
1414         {
1415                 int ssid = parse_pes_extension_fields(p, pkt_len);
1416                 if((audio_substream_id!=-1) && (ssid != audio_substream_id))
1417                         return 0;
1418                 if(ssid == 0x72)
1419                         es->type  = type_from_pmt = AUDIO_TRUEHD;
1420         }
1421
1422         p += header_len + 9;
1423         packet_len -= header_len + 3;
1424
1425         if(es->payload_size)
1426                 es->payload_size -= header_len + 3;
1427
1428
1429         es->is_synced = 1;      //only for SL streams we have to make sure it's really true, see below
1430         if (stream_id == 0xbd)
1431         {
1432                 mp_msg(MSGT_DEMUX, MSGL_DBG3, "pes_parse2: audio buf = %02X %02X %02X %02X %02X %02X %02X %02X, 80: %d\n",
1433                         p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[0] & 0x80);
1434
1435
1436                 /*
1437                 * we check the descriptor tag first because some stations
1438                 * do not include any of the A52 header info in their audio tracks
1439                 * these "raw" streams may begin with a byte that looks like a stream type.
1440                 */
1441
1442
1443                 if(
1444                         (type_from_pmt == AUDIO_A52) ||          /* A52 - raw */
1445                         (p[0] == 0x0B && p[1] == 0x77)          /* A52 - syncword */
1446                 )
1447                 {
1448                         mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 RAW OR SYNCWORD\n");
1449                         es->start = p;
1450                         es->size  = packet_len;
1451                         es->type  = AUDIO_A52;
1452                         es->payload_size -= packet_len;
1453
1454                         return 1;
1455                 }
1456                 /* SPU SUBS */
1457                 else if(type_from_pmt == SPU_DVB ||
1458                 ((p[0] == 0x20) && pes_is_aligned)) // && p[1] == 0x00))
1459                 {
1460                         es->start = p;
1461                         es->size  = packet_len;
1462                         es->type  = SPU_DVB;
1463                         es->payload_size -= packet_len;
1464
1465                         return 1;
1466                 }
1467                 else if (pes_is_aligned && ((p[0] & 0xE0) == 0x20))     //SPU_DVD
1468                 {
1469                         //DVD SUBS
1470                         es->start   = p+1;
1471                         es->size    = packet_len-1;
1472                         es->type    = SPU_DVD;
1473                         es->payload_size -= packet_len;
1474
1475                         return 1;
1476                 }
1477                 else if (pes_is_aligned && (p[0] & 0xF8) == 0x80)
1478                 {
1479                         mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 WITH HEADER\n");
1480                         es->start   = p+4;
1481                         es->size    = packet_len - 4;
1482                         es->type    = AUDIO_A52;
1483                         es->payload_size -= packet_len;
1484
1485                         return 1;
1486                 }
1487                 else if (pes_is_aligned && ((p[0]&0xf0) == 0xa0))
1488                 {
1489                         int pcm_offset;
1490
1491                         for (pcm_offset=0; ++pcm_offset < packet_len-1 ; )
1492                         {
1493                                 if (p[pcm_offset] == 0x01 && p[pcm_offset+1] == 0x80)
1494                                 {       /* START */
1495                                         pcm_offset += 2;
1496                                         break;
1497                                 }
1498                         }
1499
1500                         es->start   = p + pcm_offset;
1501                         es->size    = packet_len - pcm_offset;
1502                         es->type    = AUDIO_LPCM_BE;
1503                         es->payload_size -= packet_len;
1504
1505                         return 1;
1506                 }
1507                 else
1508                 {
1509                         mp_msg(MSGT_DEMUX, MSGL_DBG2, "PES_PRIVATE1\n");
1510                         es->start   = p;
1511                         es->size    = packet_len;
1512                         es->type    = (type_from_pmt == UNKNOWN ? PES_PRIVATE1 : type_from_pmt);
1513                         es->payload_size -= packet_len;
1514
1515                         return 1;
1516                 }
1517         }
1518         else if(((stream_id >= 0xe0) && (stream_id <= 0xef)) || (stream_id == 0xfd && type_from_pmt != UNKNOWN))
1519         {
1520                 es->start   = p;
1521                 es->size    = packet_len;
1522                 if(type_from_pmt != UNKNOWN)
1523                     es->type    = type_from_pmt;
1524                 else
1525                     es->type    = VIDEO_MPEG2;
1526                 if(es->payload_size)
1527                         es->payload_size -= packet_len;
1528
1529                 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: M2V size %d\n", es->size);
1530                 return 1;
1531         }
1532         else if ((stream_id == 0xfa))
1533         {
1534                 int l;
1535
1536                 es->is_synced = 0;
1537                 if(type_from_pmt != UNKNOWN)    //MP4 A/V or SL
1538                 {
1539                         es->start   = p;
1540                         es->size    = packet_len;
1541                         es->type    = type_from_pmt;
1542
1543                         if(type_from_pmt == SL_PES_STREAM)
1544                         {
1545                                 //if(pes_is_aligned)
1546                                 //{
1547                                         l = mp4_parse_sl_packet(pmt, p, packet_len, pid, es);
1548                                         mp_msg(MSGT_DEMUX, MSGL_DBG2, "L=%d, TYPE=%x\n", l, type_from_pmt);
1549                                         if(l < 0)
1550                                         {
1551                                                 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: couldn't parse SL header, passing along full PES payload\n");
1552                                                 l = 0;
1553                                         }
1554                                 //}
1555
1556                                 es->start   += l;
1557                                 es->size    -= l;
1558                         }
1559
1560                         if(es->payload_size)
1561                                 es->payload_size -= packet_len;
1562                         return 1;
1563                 }
1564         }
1565         else if ((stream_id & 0xe0) == 0xc0)
1566         {
1567                 es->start   = p;
1568                 es->size    = packet_len;
1569
1570                 if(type_from_pmt != UNKNOWN)
1571                         es->type = type_from_pmt;
1572                 else
1573                         es->type    = AUDIO_MP2;
1574
1575                 es->payload_size -= packet_len;
1576
1577                 return 1;
1578         }
1579         else if (type_from_pmt != -1)   //as a last resort here we trust the PMT, if present
1580         {
1581                 es->start   = p;
1582                 es->size    = packet_len;
1583                 es->type    = type_from_pmt;
1584                 es->payload_size -= packet_len;
1585
1586                 return 1;
1587         }
1588         else
1589         {
1590                 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: unknown packet, id: %x\n", stream_id);
1591         }
1592
1593         es->is_synced = 0;
1594         return 0;
1595 }
1596
1597
1598
1599
1600 static int ts_sync(stream_t *stream)
1601 {
1602         int c=0;
1603
1604         mp_msg(MSGT_DEMUX, MSGL_DBG3, "TS_SYNC \n");
1605
1606         while(((c=stream_read_char(stream)) != 0x47) && ! stream->eof);
1607
1608         if(c == 0x47)
1609                 return c;
1610         else
1611                 return 0;
1612 }
1613
1614
1615 static void ts_dump_streams(ts_priv_t *priv)
1616 {
1617         int i;
1618
1619         for(i = 0; i < 3; i++)
1620         {
1621                 if((priv->fifo[i].pack != NULL) && (priv->fifo[i].offset != 0))
1622                 {
1623                         resize_demux_packet(priv->fifo[i].pack, priv->fifo[i].offset);
1624                         ds_add_packet(priv->fifo[i].ds, priv->fifo[i].pack);
1625                         priv->fifo[i].offset = 0;
1626                         priv->fifo[i].pack = NULL;
1627                 }
1628         }
1629 }
1630
1631
1632 static inline int32_t prog_idx_in_pat(ts_priv_t *priv, uint16_t progid)
1633 {
1634         int x;
1635
1636         if(priv->pat.progs == NULL)
1637                         return -1;
1638
1639         for(x = 0; x < priv->pat.progs_cnt; x++)
1640         {
1641                 if(priv->pat.progs[x].id == progid)
1642                         return x;
1643         }
1644
1645         return -1;
1646 }
1647
1648
1649 static inline int32_t prog_id_in_pat(ts_priv_t *priv, uint16_t pid)
1650 {
1651         int x;
1652
1653         if(priv->pat.progs == NULL)
1654                 return -1;
1655
1656         for(x = 0; x < priv->pat.progs_cnt; x++)
1657         {
1658                 if(priv->pat.progs[x].pmt_pid == pid)
1659                         return priv->pat.progs[x].id;
1660         }
1661
1662         return -1;
1663 }
1664
1665 static int collect_section(ts_section_t *section, int is_start, unsigned char *buff, int size)
1666 {
1667         uint8_t *ptr;
1668         uint16_t tlen;
1669         int skip, tid;
1670
1671         mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, start: %d, size: %d, collected: %d\n", is_start, size, section->buffer_len);
1672         if(! is_start && !section->buffer_len)
1673                 return 0;
1674
1675         if(is_start)
1676         {
1677                 if(! section->buffer)
1678                 {
1679                         section->buffer = (uint8_t*) malloc(4096+256);
1680                         if(section->buffer == NULL)
1681                                 return 0;
1682                 }
1683                 section->buffer_len = 0;
1684         }
1685
1686         if(size + section->buffer_len > 4096+256)
1687         {
1688                 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, excessive len: %d + %d\n", section->buffer_len, size);
1689                 return 0;
1690         }
1691
1692         memcpy(&(section->buffer[section->buffer_len]), buff, size);
1693         section->buffer_len += size;
1694
1695         if(section->buffer_len < 3)
1696                 return 0;
1697
1698         skip = section->buffer[0];
1699         if(skip + 4 > section->buffer_len)
1700                 return 0;
1701
1702         ptr = &(section->buffer[skip + 1]);
1703         tid = ptr[0];
1704         tlen = ((ptr[1] & 0x0f) << 8) | ptr[2];
1705         mp_msg(MSGT_DEMUX, MSGL_V, "SKIP: %d+1, TID: %d, TLEN: %d, COLLECTED: %d\n", skip, tid, tlen, section->buffer_len);
1706         if(section->buffer_len < (skip+1+3+tlen))
1707         {
1708                 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DATA IS NOT ENOUGH, NEXT TIME\n");
1709                 return 0;
1710         }
1711
1712         return skip+1;
1713 }
1714
1715 static int parse_pat(ts_priv_t * priv, int is_start, unsigned char *buff, int size)
1716 {
1717         int skip;
1718         unsigned char *ptr;
1719         unsigned char *base;
1720         int entries, i;
1721         uint16_t progid;
1722         struct pat_progs_t *tmp;
1723         ts_section_t *section;
1724
1725         section = &(priv->pat.section);
1726         skip = collect_section(section, is_start, buff, size);
1727         if(! skip)
1728                 return 0;
1729
1730         ptr = &(section->buffer[skip]);
1731         //PARSING
1732         priv->pat.table_id = ptr[0];
1733         if(priv->pat.table_id != 0)
1734                 return 0;
1735         priv->pat.ssi = (ptr[1] >> 7) & 0x1;
1736         priv->pat.curr_next = ptr[5] & 0x01;
1737         priv->pat.ts_id = (ptr[3]  << 8 ) | ptr[4];
1738         priv->pat.version_number = (ptr[5] >> 1) & 0x1F;
1739         priv->pat.section_length = ((ptr[1] & 0x03) << 8 ) | ptr[2];
1740         priv->pat.section_number = ptr[6];
1741         priv->pat.last_section_number = ptr[7];
1742
1743         //check_crc32(0xFFFFFFFFL, ptr, priv->pat.buffer_len - 4, &ptr[priv->pat.buffer_len - 4]);
1744         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);
1745
1746         entries = (int) (priv->pat.section_length - 9) / 4;     //entries per section
1747
1748         for(i=0; i < entries; i++)
1749         {
1750                 int32_t idx;
1751                 base = &ptr[8 + i*4];
1752                 progid = (base[0] << 8) | base[1];
1753
1754                 if((idx = prog_idx_in_pat(priv, progid)) == -1)
1755                 {
1756                         int sz = sizeof(struct pat_progs_t) * (priv->pat.progs_cnt+1);
1757                         tmp = realloc_struct(priv->pat.progs, priv->pat.progs_cnt+1, sizeof(struct pat_progs_t));
1758                         if(tmp == NULL)
1759                         {
1760                                 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PAT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
1761                                 break;
1762                         }
1763                         priv->pat.progs = tmp;
1764                         idx = priv->pat.progs_cnt;
1765                         priv->pat.progs_cnt++;
1766                 }
1767
1768                 priv->pat.progs[idx].id = progid;
1769                 priv->pat.progs[idx].pmt_pid = ((base[2]  & 0x1F) << 8) | base[3];
1770                 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);
1771                 mp_msg(MSGT_IDENTIFY, MSGL_V, "PROGRAM_ID=%d (0x%02X), PMT_PID: %d(0x%02X)\n",
1772                         progid, progid, priv->pat.progs[idx].pmt_pid, priv->pat.progs[idx].pmt_pid);
1773         }
1774
1775         return 1;
1776 }
1777
1778
1779 static inline int32_t es_pid_in_pmt(pmt_t * pmt, uint16_t pid)
1780 {
1781         uint16_t i;
1782
1783         if(pmt == NULL)
1784                 return -1;
1785
1786         if(pmt->es == NULL)
1787                 return -1;
1788
1789         for(i = 0; i < pmt->es_cnt; i++)
1790         {
1791                 if(pmt->es[i].pid == pid)
1792                         return (int32_t) i;
1793         }
1794
1795         return -1;
1796 }
1797
1798
1799 static uint16_t get_mp4_desc_len(uint8_t *buf, int *len)
1800 {
1801         //uint16_t i = 0, size = 0;
1802         int i = 0, j, size = 0;
1803
1804         mp_msg(MSGT_DEMUX, MSGL_DBG2, "PARSE_MP4_DESC_LEN(%d), bytes: ", *len);
1805         j = FFMIN(*len, 4);
1806         while(i < j)
1807         {
1808                 mp_msg(MSGT_DEMUX, MSGL_DBG2, " %x ", buf[i]);
1809                 size |= (buf[i] & 0x7f);
1810                 if(!(buf[i] & 0x80))
1811                         break;
1812                 size <<= 7;
1813                 i++;
1814         }
1815         mp_msg(MSGT_DEMUX, MSGL_DBG2, ", SIZE=%d\n", size);
1816
1817         *len = i+1;
1818         return size;
1819 }
1820
1821
1822 static uint16_t parse_mp4_slconfig_descriptor(uint8_t *buf, int len, void *elem)
1823 {
1824         int i = 0;
1825         mp4_es_descr_t *es;
1826         mp4_sl_config_t *sl;
1827
1828         mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_SLCONFIG_DESCRIPTOR(%d)\n", len);
1829         es = (mp4_es_descr_t *) elem;
1830         if(!es)
1831         {
1832                 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1833                 return len;
1834         }
1835         sl = &(es->sl);
1836
1837         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;
1838         sl->ocr = sl->dts = sl->cts = 0;
1839
1840         if(buf[0] == 0)
1841         {
1842                 i++;
1843                 sl->flags = buf[i];
1844                 i++;
1845                 sl->ts_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1846                 i += 4;
1847                 sl->ocr_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1848                 i += 4;
1849                 sl->ts_len = buf[i];
1850                 i++;
1851                 sl->ocr_len = buf[i];
1852                 i++;
1853                 sl->au_len = buf[i];
1854                 i++;
1855                 sl->instant_bitrate_len = buf[i];
1856                 i++;
1857                 sl->degr_len = (buf[i] >> 4) & 0x0f;
1858                 sl->au_seqnum_len = ((buf[i] & 0x0f) << 1) | ((buf[i+1] >> 7) & 0x01);
1859                 i++;
1860                 sl->packet_seqnum_len = ((buf[i] >> 2) & 0x1f);
1861                 i++;
1862
1863         }
1864         else if(buf[0] == 1)
1865         {
1866                 sl->flags = 0;
1867                 sl->ts_resolution = 1000;
1868                 sl->ts_len = 32;
1869                 i++;
1870         }
1871         else if(buf[0] == 2)
1872         {
1873                 sl->flags = 4;
1874                 i++;
1875         }
1876         else
1877         {
1878                 sl->flags = 0;
1879                 i++;
1880         }
1881
1882         sl->au_start = (sl->flags >> 7) & 0x1;
1883         sl->au_end = (sl->flags >> 6) & 0x1;
1884         sl->random_accesspoint = (sl->flags >> 5) & 0x1;
1885         sl->random_accesspoint_only = (sl->flags >> 4) & 0x1;
1886         sl->padding = (sl->flags >> 3) & 0x1;
1887         sl->use_ts = (sl->flags >> 2) & 0x1;
1888         sl->idle = (sl->flags >> 1) & 0x1;
1889         sl->duration = sl->flags & 0x1;
1890
1891         if(sl->duration)
1892         {
1893                 sl->timescale = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1894                 i += 4;
1895                 sl->au_duration = (buf[i] << 8) | buf[i+1];
1896                 i += 2;
1897                 sl->cts_duration = (buf[i] << 8) | buf[i+1];
1898                 i += 2;
1899         }
1900         else    //no support for fixed durations atm
1901                 sl->timescale = sl->au_duration = sl->cts_duration = 0;
1902
1903         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",
1904                 len, buf[0], sl->flags, sl->use_ts, sl->ts_len, sl->timescale, (uint64_t) sl->dts, (uint64_t) sl->cts);
1905
1906         return len;
1907 }
1908
1909 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem);
1910
1911 static uint16_t parse_mp4_decoder_config_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
1912 {
1913         int i = 0, j;
1914         mp4_es_descr_t *es;
1915         mp4_decoder_config_t *dec;
1916
1917         mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_CONFIG_DESCRIPTOR(%d)\n", len);
1918         es = (mp4_es_descr_t *) elem;
1919         if(!es)
1920         {
1921                 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1922                 return len;
1923         }
1924         dec = (mp4_decoder_config_t*) &(es->decoder);
1925
1926         dec->object_type = buf[i];
1927         dec->stream_type =  (buf[i+1]>>2) & 0x3f;
1928
1929         if(dec->object_type == 1 && dec->stream_type == 1)
1930         {
1931                  dec->object_type = MP4_OD;
1932                  dec->stream_type = MP4_OD;
1933         }
1934         else if(dec->stream_type == 4)
1935         {
1936                 if(dec->object_type == 0x6a)
1937                         dec->object_type = VIDEO_MPEG1;
1938                 if(dec->object_type >= 0x60 && dec->object_type <= 0x65)
1939                         dec->object_type = VIDEO_MPEG2;
1940                 else if(dec->object_type == 0x20)
1941                         dec->object_type = VIDEO_MPEG4;
1942                 else if(dec->object_type == 0x21)
1943                         dec->object_type = VIDEO_AVC;
1944                 /*else if(dec->object_type == 0x22)
1945                         fprintf(stderr, "TYPE 0x22\n");*/
1946                 else dec->object_type = UNKNOWN;
1947         }
1948         else if(dec->stream_type == 5)
1949         {
1950                 if(dec->object_type == 0x40)
1951                         dec->object_type = AUDIO_AAC;
1952                 else if(dec->object_type == 0x6b)
1953                         dec->object_type = AUDIO_MP2;
1954                 else if(dec->object_type >= 0x66 && dec->object_type <= 0x69)
1955                         dec->object_type = AUDIO_MP2;
1956                 else
1957                         dec->object_type = UNKNOWN;
1958         }
1959         else
1960                 dec->object_type = dec->stream_type = UNKNOWN;
1961
1962         if(dec->object_type != UNKNOWN)
1963         {
1964                 //update the type of the current stream
1965                 for(j = 0; j < pmt->es_cnt; j++)
1966                 {
1967                         if(pmt->es[j].mp4_es_id == es->id)
1968                         {
1969                                 pmt->es[j].type = SL_PES_STREAM;
1970                         }
1971                 }
1972         }
1973
1974         if(len > 13)
1975                 parse_mp4_descriptors(pmt, &buf[13], len-13, dec);
1976
1977         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);
1978
1979         return len;
1980 }
1981
1982 static uint16_t parse_mp4_decoder_specific_descriptor(uint8_t *buf, int len, void *elem)
1983 {
1984         int i;
1985         mp4_decoder_config_t *dec;
1986
1987         mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_SPECIFIC_DESCRIPTOR(%d)\n", len);
1988         dec = (mp4_decoder_config_t *) elem;
1989         if(!dec)
1990         {
1991                 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1992                 return len;
1993         }
1994
1995         mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4 SPECIFIC INFO BYTES: \n");
1996         for(i=0; i<len; i++)
1997                 mp_msg(MSGT_DEMUX, MSGL_DBG2, "%02x ", buf[i]);
1998         mp_msg(MSGT_DEMUX, MSGL_DBG2, "\n");
1999
2000         if(len > MAX_EXTRADATA_SIZE)
2001         {
2002                 mp_msg(MSGT_DEMUX, MSGL_ERR, "DEMUX_TS, EXTRADATA SUSPICIOUSLY BIG: %d, REFUSED\r\n", len);
2003                 return len;
2004         }
2005         memcpy(dec->buf, buf, len);
2006         dec->buf_size = len;
2007
2008         return len;
2009 }
2010
2011 static uint16_t parse_mp4_es_descriptor(pmt_t *pmt, uint8_t *buf, int len)
2012 {
2013         int i = 0, j = 0, k, found;
2014         uint8_t flag;
2015         mp4_es_descr_t es, *target_es = NULL, *tmp;
2016
2017         mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4ES: len=%d\n", len);
2018         memset(&es, 0, sizeof(mp4_es_descr_t));
2019         while(i < len)
2020         {
2021                 es.id = (buf[i] << 8) | buf[i+1];
2022                 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_ID: %d\n", es.id);
2023                 i += 2;
2024                 flag = buf[i];
2025                 i++;
2026                 if(flag & 0x80)
2027                         i += 2;
2028                 if(flag & 0x40)
2029                         i += buf[i]+1;
2030                 if(flag & 0x20)         //OCR, maybe we need it
2031                         i += 2;
2032
2033                 j = parse_mp4_descriptors(pmt, &buf[i], len-i, &es);
2034                 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);
2035                 if(es.decoder.object_type != UNKNOWN && es.decoder.stream_type != UNKNOWN)
2036                 {
2037                         found = 0;
2038                         //search this ES_ID if we already have it
2039                         for(k=0; k < pmt->mp4es_cnt; k++)
2040                         {
2041                                 if(pmt->mp4es[k].id == es.id)
2042                                 {
2043                                         target_es = &(pmt->mp4es[k]);
2044                                         found = 1;
2045                                 }
2046                         }
2047
2048                         if(! found)
2049                         {
2050                                 tmp = realloc_struct(pmt->mp4es, pmt->mp4es_cnt+1, sizeof(mp4_es_descr_t));
2051                                 if(tmp == NULL)
2052                                 {
2053                                         fprintf(stderr, "CAN'T REALLOC MP4_ES_DESCR\n");
2054                                         continue;
2055                                 }
2056                                 pmt->mp4es = tmp;
2057                                 target_es = &(pmt->mp4es[pmt->mp4es_cnt]);
2058                                 pmt->mp4es_cnt++;
2059                         }
2060                         memcpy(target_es, &es, sizeof(mp4_es_descr_t));
2061                         mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_CNT: %d, ID=%d\n", pmt->mp4es_cnt, target_es->id);
2062                 }
2063
2064                 i += j;
2065         }
2066
2067         return len;
2068 }
2069
2070 static void parse_mp4_object_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2071 {
2072         int i, j = 0, id;
2073
2074         i=0;
2075         id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2076         mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_OBJECT_DESCRIPTOR: len=%d, OD_ID=%d\n", len, id);
2077         if(buf[1] & 0x20)
2078         {
2079                 i += buf[2] + 1;        //url
2080                 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2081         }
2082         else
2083         {
2084                 i = 2;
2085
2086                 while(i < len)
2087                 {
2088                         j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2089                         mp_msg(MSGT_DEMUX, MSGL_V, "OBJD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2090                         i += j;
2091                 }
2092         }
2093 }
2094
2095
2096 static void parse_mp4_iod(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2097 {
2098         int i, j = 0;
2099         mp4_od_t *iod = &(pmt->iod);
2100
2101         iod->id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2102         mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_IOD: len=%d, IOD_ID=%d\n", len, iod->id);
2103         i = 2;
2104         if(buf[1] & 0x20)
2105         {
2106                 i += buf[2] + 1;        //url
2107                 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2108         }
2109         else
2110         {
2111                 i = 7;
2112                 while(i < len)
2113                 {
2114                         j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2115                         mp_msg(MSGT_DEMUX, MSGL_V, "IOD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2116                         i += j;
2117                 }
2118         }
2119 }
2120
2121 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2122 {
2123         int tag, descr_len, i = 0, j = 0;
2124
2125         mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DESCRIPTORS, len=%d\n", len);
2126         if(! len)
2127                 return len;
2128
2129         while(i < len)
2130         {
2131                 tag = buf[i];
2132                 j = len - i -1;
2133                 descr_len = get_mp4_desc_len(&(buf[i+1]), &j);
2134                 mp_msg(MSGT_DEMUX, MSGL_V, "TAG=%d (0x%x), DESCR_len=%d, len=%d, j=%d\n", tag, tag, descr_len, len, j);
2135                 if(descr_len > len - j+1)
2136                 {
2137                         mp_msg(MSGT_DEMUX, MSGL_V, "descriptor is too long, exit\n");
2138                         return len;
2139                 }
2140                 i += j+1;
2141
2142                 switch(tag)
2143                 {
2144                         case 0x1:
2145                                 parse_mp4_object_descriptor(pmt, &(buf[i]), descr_len, elem);
2146                                 break;
2147                         case 0x2:
2148                                 parse_mp4_iod(pmt, &(buf[i]), descr_len, elem);
2149                                 break;
2150                         case 0x3:
2151                                 parse_mp4_es_descriptor(pmt, &(buf[i]), descr_len);
2152                                 break;
2153                         case 0x4:
2154                                 parse_mp4_decoder_config_descriptor(pmt, &buf[i], descr_len, elem);
2155                                 break;
2156                         case 0x05:
2157                                 parse_mp4_decoder_specific_descriptor(&buf[i], descr_len, elem);
2158                                 break;
2159                         case 0x6:
2160                                 parse_mp4_slconfig_descriptor(&buf[i], descr_len, elem);
2161                                 break;
2162                         default:
2163                                 mp_msg(MSGT_DEMUX, MSGL_V, "Unsupported mp4 descriptor 0x%x\n", tag);
2164                 }
2165                 i += descr_len;
2166         }
2167
2168         return len;
2169 }
2170
2171 static ES_stream_t *new_pid(ts_priv_t *priv, int pid)
2172 {
2173         ES_stream_t *tss;
2174
2175         tss = malloc(sizeof(ES_stream_t));
2176         if(! tss)
2177                 return NULL;
2178         memset(tss, 0, sizeof(ES_stream_t));
2179         tss->pid = pid;
2180         tss->last_cc = -1;
2181         tss->type = UNKNOWN;
2182         tss->subtype = UNKNOWN;
2183         tss->is_synced = 0;
2184         tss->extradata = NULL;
2185         tss->extradata_alloc = tss->extradata_len = 0;
2186         priv->ts.pids[pid] = tss;
2187
2188         return tss;
2189 }
2190
2191
2192 static int parse_program_descriptors(pmt_t *pmt, uint8_t *buf, uint16_t len)
2193 {
2194         uint16_t i = 0, k, olen = len;
2195
2196         while(len > 0)
2197         {
2198                 mp_msg(MSGT_DEMUX, MSGL_V, "PROG DESCR, TAG=%x, LEN=%d(%x)\n", buf[i], buf[i+1], buf[i+1]);
2199                 if(buf[i+1] > len-2)
2200                 {
2201                         mp_msg(MSGT_DEMUX, MSGL_V, "ERROR, descriptor len is too long, skipping\n");
2202                         return olen;
2203                 }
2204
2205                 if(buf[i] == 0x1d)
2206                 {
2207                         if(buf[i+3] == 2)       //buggy versions of vlc muxer make this non-standard mess (missing iod_scope)
2208                                 k = 3;
2209                         else
2210                                 k = 4;          //this is standard compliant
2211                         parse_mp4_descriptors(pmt, &buf[i+k], (int) buf[i+1]-(k-2), NULL);
2212                 }
2213
2214                 len -= 2 + buf[i+1];
2215         }
2216
2217         return olen;
2218 }
2219
2220 static int parse_descriptors(struct pmt_es_t *es, uint8_t *ptr)
2221 {
2222         int j, descr_len, len;
2223
2224         j = 0;
2225         len = es->descr_length;
2226         while(len > 2)
2227         {
2228                 descr_len = ptr[j+1];
2229                 mp_msg(MSGT_DEMUX, MSGL_V, "...descr id: 0x%x, len=%d\n", ptr[j], descr_len);
2230                 if(descr_len > len)
2231                 {
2232                         mp_msg(MSGT_DEMUX, MSGL_ERR, "INVALID DESCR LEN for tag %02x: %d vs %d max, EXIT LOOP\n", ptr[j], descr_len, len);
2233                         return -1;
2234                 }
2235
2236
2237                 if(ptr[j] == 0x6a || ptr[j] == 0x7a)    //A52 Descriptor
2238                 {
2239                         if(es->type == 0x6)
2240                         {
2241                                 es->type = AUDIO_A52;
2242                                 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB A52 Descriptor\n");
2243                         }
2244                 }
2245                 else if(ptr[j] == 0x7b) //DVB DTS Descriptor
2246                 {
2247                         if(es->type == 0x6)
2248                         {
2249                                 es->type = AUDIO_DTS;
2250                                 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB DTS Descriptor\n");
2251                         }
2252                 }
2253                 else if(ptr[j] == 0x59) //Subtitling Descriptor
2254                 {
2255                         uint8_t subtype;
2256
2257                         mp_msg(MSGT_DEMUX, MSGL_DBG2, "Subtitling Descriptor\n");
2258                         if(descr_len < 8)
2259                         {
2260                                 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Descriptor length too short for DVB Subtitle Descriptor: %d, SKIPPING\n", descr_len);
2261                         }
2262                         else
2263                         {
2264                                 memcpy(es->lang, &ptr[j+2], 3);
2265                                 es->lang[3] = 0;
2266                                 subtype = ptr[j+5];
2267                                 if(
2268                                         (subtype >= 0x10 && subtype <= 0x13) ||
2269                                         (subtype >= 0x20 && subtype <= 0x23)
2270                                 )
2271                                 {
2272                                         es->type = SPU_DVB;
2273                                         //page parameters: compo page 2 bytes, ancillary page 2 bytes
2274                                 }
2275                                 else
2276                                         es->type = UNKNOWN;
2277                         }
2278                 }
2279                 else if(ptr[j] == 0x50) //Component Descriptor
2280                 {
2281                         mp_msg(MSGT_DEMUX, MSGL_DBG2, "Component Descriptor\n");
2282                         memcpy(es->lang, &ptr[j+5], 3);
2283                         es->lang[3] = 0;
2284                 }
2285                 else if(ptr[j] == 0xa)  //Language Descriptor
2286                 {
2287                         memcpy(es->lang, &ptr[j+2], 3);
2288                         es->lang[3] = 0;
2289                         mp_msg(MSGT_DEMUX, MSGL_V, "Language Descriptor: %s\n", es->lang);
2290                 }
2291                 else if(ptr[j] == 0x5)  //Registration Descriptor (looks like e fourCC :) )
2292                 {
2293                         mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor\n");
2294                         if(descr_len < 4)
2295                         {
2296                                 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor length too short: %d, SKIPPING\n", descr_len);
2297                         }
2298                         else
2299                         {
2300                                 char *d;
2301                                 memcpy(es->format_descriptor, &ptr[j+2], 4);
2302                                 es->format_descriptor[4] = 0;
2303
2304                                 d = &ptr[j+2];
2305                                 if(d[0] == 'A' && d[1] == 'C' && d[2] == '-' && d[3] == '3')
2306                                 {
2307                                         es->type = AUDIO_A52;
2308                                 }
2309                                 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '1')
2310                                 {
2311                                         es->type = AUDIO_DTS;
2312                                 }
2313                                 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '2')
2314                                 {
2315                                         es->type = AUDIO_DTS;
2316                                 }
2317                                 else if(d[0] == 'V' && d[1] == 'C' && d[2] == '-' && d[3] == '1')
2318                                 {
2319                                         es->type = VIDEO_VC1;
2320                                 }
2321                                 else
2322                                         es->type = UNKNOWN;
2323                                 mp_msg(MSGT_DEMUX, MSGL_DBG2, "FORMAT %s\n", es->format_descriptor);
2324                         }
2325                 }
2326                 else if(ptr[j] == 0x1e)
2327                 {
2328                         es->mp4_es_id = (ptr[j+2] << 8) | ptr[j+3];
2329                         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);
2330                 }
2331                 else
2332                         mp_msg(MSGT_DEMUX, MSGL_DBG2, "Unknown descriptor 0x%x, SKIPPING\n", ptr[j]);
2333
2334                 len -= 2 + descr_len;
2335                 j += 2 + descr_len;
2336         }
2337
2338         return 1;
2339 }
2340
2341 static int parse_sl_section(pmt_t *pmt, ts_section_t *section, int is_start, unsigned char *buff, int size)
2342 {
2343         int tid, len, skip;
2344         uint8_t *ptr;
2345         skip = collect_section(section, is_start, buff, size);
2346         if(! skip)
2347                 return 0;
2348
2349         ptr = &(section->buffer[skip]);
2350         tid = ptr[0];
2351         len = ((ptr[1] & 0x0f) << 8) | ptr[2];
2352         mp_msg(MSGT_DEMUX, MSGL_V, "TABLEID: %d (av. %d), skip=%d, LEN: %d\n", tid, section->buffer_len, skip, len);
2353         if(len > 4093 || section->buffer_len < len || tid != 5)
2354         {
2355                 mp_msg(MSGT_DEMUX, MSGL_V, "SECTION TOO LARGE or wrong section type, EXIT\n");
2356                 return 0;
2357         }
2358
2359         if(! (ptr[5] & 1))
2360                 return 0;
2361
2362         //8 is the current position, len - 9 is the amount of data available
2363         parse_mp4_descriptors(pmt, &ptr[8], len - 9, NULL);
2364
2365         return 1;
2366 }
2367
2368 static int parse_pmt(ts_priv_t * priv, uint16_t progid, uint16_t pid, int is_start, unsigned char *buff, int size)
2369 {
2370         unsigned char *base, *es_base;
2371         pmt_t *pmt;
2372         int32_t idx, es_count, section_bytes;
2373         uint8_t m=0;
2374         int skip;
2375         pmt_t *tmp;
2376         struct pmt_es_t *tmp_es;
2377         ts_section_t *section;
2378         ES_stream_t *tss;
2379
2380         idx = progid_idx_in_pmt(priv, progid);
2381
2382         if(idx == -1)
2383         {
2384                 int sz = (priv->pmt_cnt + 1) * sizeof(pmt_t);
2385                 tmp = realloc_struct(priv->pmt, priv->pmt_cnt + 1, sizeof(pmt_t));
2386                 if(tmp == NULL)
2387                 {
2388                         mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
2389                         return 0;
2390                 }
2391                 priv->pmt = tmp;
2392                 idx = priv->pmt_cnt;
2393                 memset(&(priv->pmt[idx]), 0, sizeof(pmt_t));
2394                 priv->pmt_cnt++;
2395                 priv->pmt[idx].progid = progid;
2396         }
2397
2398         pmt = &(priv->pmt[idx]);
2399
2400         section = &(pmt->section);
2401         skip = collect_section(section, is_start, buff, size);
2402         if(! skip)
2403                 return 0;
2404
2405         base = &(section->buffer[skip]);
2406
2407         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",
2408                 progid, pmt->section.buffer_len, is_start, pid, size, m, pmt->es_cnt, idx, pmt);
2409
2410         pmt->table_id = base[0];
2411         if(pmt->table_id != 2)
2412                 return -1;
2413         pmt->ssi = base[1] & 0x80;
2414         pmt->section_length = (((base[1] & 0xf) << 8 ) | base[2]);
2415         pmt->version_number = (base[5] >> 1) & 0x1f;
2416         pmt->curr_next = (base[5] & 1);
2417         pmt->section_number = base[6];
2418         pmt->last_section_number = base[7];
2419         pmt->PCR_PID = ((base[8] & 0x1f) << 8 ) | base[9];
2420         pmt->prog_descr_length = ((base[10] & 0xf) << 8 ) | base[11];
2421         if(pmt->prog_descr_length > pmt->section_length - 9)
2422         {
2423                 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, INVALID PROG_DESCR LENGTH (%d vs %d)\n", pmt->prog_descr_length, pmt->section_length - 9);
2424                 return -1;
2425         }
2426
2427         if(pmt->prog_descr_length)
2428                 parse_program_descriptors(pmt, &base[12], pmt->prog_descr_length);
2429
2430         es_base = &base[12 + pmt->prog_descr_length];   //the beginning of th ES loop
2431
2432         section_bytes= pmt->section_length - 13 - pmt->prog_descr_length;
2433         es_count  = 0;
2434
2435         while(section_bytes >= 5)
2436         {
2437                 int es_pid, es_type;
2438
2439                 es_type = es_base[0];
2440                 es_pid = ((es_base[1] & 0x1f) << 8) | es_base[2];
2441
2442                 idx = es_pid_in_pmt(pmt, es_pid);
2443                 if(idx == -1)
2444                 {
2445                         int sz = sizeof(struct pmt_es_t) * (pmt->es_cnt + 1);
2446                         tmp_es = realloc_struct(pmt->es, pmt->es_cnt + 1, sizeof(struct pmt_es_t));
2447                         if(tmp_es == NULL)
2448                         {
2449                                 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT, COULDN'T ALLOCATE %d bytes for PMT_ES\n", sz);
2450                                 continue;
2451                         }
2452                         pmt->es = tmp_es;
2453                         idx = pmt->es_cnt;
2454                         memset(&(pmt->es[idx]), 0, sizeof(struct pmt_es_t));
2455                         pmt->es_cnt++;
2456                 }
2457
2458                 pmt->es[idx].descr_length = ((es_base[3] & 0xf) << 8) | es_base[4];
2459
2460
2461                 if(pmt->es[idx].descr_length > section_bytes - 5)
2462                 {
2463                         mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, ES_DESCR_LENGTH TOO LARGE %d > %d, EXIT\n",
2464                                 pmt->es[idx].descr_length, section_bytes - 5);
2465                         return -1;
2466                 }
2467
2468
2469                 pmt->es[idx].pid = es_pid;
2470                 if(es_type != 0x6)
2471                         pmt->es[idx].type = UNKNOWN;
2472                 else
2473                         pmt->es[idx].type = es_type;
2474
2475                 parse_descriptors(&pmt->es[idx], &es_base[5]);
2476
2477                 switch(es_type)
2478                 {
2479                         case 1:
2480                                 pmt->es[idx].type = VIDEO_MPEG1;
2481                                 break;
2482                         case 2:
2483                                 pmt->es[idx].type = VIDEO_MPEG2;
2484                                 break;
2485                         case 3:
2486                         case 4:
2487                                 pmt->es[idx].type = AUDIO_MP2;
2488                                 break;
2489                         case 6:
2490                                 if(pmt->es[idx].type == 0x6)    //this could have been ovrwritten by parse_descriptors
2491                                         pmt->es[idx].type = UNKNOWN;
2492                                 break;
2493                         case 0x10:
2494                                 pmt->es[idx].type = VIDEO_MPEG4;
2495                                 break;
2496                         case 0x0f:
2497                         case 0x11:
2498                                 pmt->es[idx].type = AUDIO_AAC;
2499                                 break;
2500                         case 0x1b:
2501                                 pmt->es[idx].type = VIDEO_H264;
2502                                 break;
2503                         case 0x12:
2504                                 pmt->es[idx].type = SL_PES_STREAM;
2505                                 break;
2506                         case 0x13:
2507                                 pmt->es[idx].type = SL_SECTION;
2508                                 break;
2509                         case 0x81:
2510                                 pmt->es[idx].type = AUDIO_A52;
2511                                 break;
2512                         case 0x8A:
2513                         case 0x82:
2514                         case 0x86:
2515                                 pmt->es[idx].type = AUDIO_DTS;
2516                                 break;
2517                         case 0xEA:
2518                                 pmt->es[idx].type = VIDEO_VC1;
2519                                 break;
2520                         default:
2521                                 mp_msg(MSGT_DEMUX, MSGL_DBG2, "UNKNOWN ES TYPE=0x%x\n", es_type);
2522                                 pmt->es[idx].type = UNKNOWN;
2523                 }
2524
2525                 tss = priv->ts.pids[es_pid];                    //an ES stream
2526                 if(tss == NULL)
2527                 {
2528                         tss = new_pid(priv, es_pid);
2529                         if(tss)
2530                                 tss->type = pmt->es[idx].type;
2531                 }
2532
2533                 section_bytes -= 5 + pmt->es[idx].descr_length;
2534                 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",
2535                         progid, idx, es_count, pmt->es[idx].pid, pmt->es[idx].pid, pmt->es[idx].type, pmt->es[idx].descr_length, section_bytes);
2536
2537
2538                 es_base += 5 + pmt->es[idx].descr_length;
2539
2540                 es_count++;
2541         }
2542
2543         mp_msg(MSGT_DEMUX, MSGL_V, "----------------------------\n");
2544         return 1;
2545 }
2546
2547 static pmt_t* pmt_of_pid(ts_priv_t *priv, int pid, mp4_decoder_config_t **mp4_dec)
2548 {
2549         int32_t i, j, k;
2550
2551         if(priv->pmt)
2552         {
2553                 for(i = 0; i < priv->pmt_cnt; i++)
2554                 {
2555                         if(priv->pmt[i].es && priv->pmt[i].es_cnt)
2556                         {
2557                                 for(j = 0; j < priv->pmt[i].es_cnt; j++)
2558                                 {
2559                                         if(priv->pmt[i].es[j].pid == pid)
2560                                         {
2561                                   &nb