Fixes
[mplayer/glamo.git] / libmpdemux / demux_ts.c
blob01c8ee69f52fef88dbe7f13abdfbeaddf48aa343
1 /*
2 * Demultiplexer for MPEG2 Transport Streams.
4 * Written by Nico <nsabbi@libero.it>
5 * Kind feedback is appreciated; 'sucks' and alike is not.
6 * Originally based on demux_pva.c written by Matteo Giani and FFmpeg (libavformat) sources
8 * This file is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This file is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "config.h"
29 #include "mp_msg.h"
30 #include "help_mp.h"
32 #include "stream.h"
33 #include "demuxer.h"
34 #include "parse_es.h"
35 #include "stheader.h"
37 #include "bswap.h"
38 #include "unrarlib.h"
39 #include "ms_hdr.h"
40 #include "mpeg_hdr.h"
42 #define TS_PH_PACKET_SIZE 192
43 #define TS_FEC_PACKET_SIZE 204
44 #define TS_PACKET_SIZE 188
45 #define NB_PID_MAX 8192
47 #define MAX_HEADER_SIZE 6 /* enough for PES header + length */
48 #define MAX_CHECK_SIZE 65535
49 #define TS_MAX_PROBE_SIZE 2000000 /* dont forget to change this in cfg-common.h too */
50 #define NUM_CONSECUTIVE_TS_PACKETS 32
51 #define NUM_CONSECUTIVE_AUDIO_PACKETS 348
52 #define MAX_A52_FRAME_SIZE 3840
54 #ifndef SIZE_MAX
55 #define SIZE_MAX ((size_t)-1)
56 #endif
58 #define TYPE_AUDIO 1
59 #define TYPE_VIDEO 2
61 int ts_prog;
62 int ts_keep_broken=0;
63 off_t ts_probe = TS_MAX_PROBE_SIZE;
64 extern char *dvdsub_lang, *audio_lang; //for -alang
66 typedef enum
68 UNKNOWN = -1,
69 VIDEO_MPEG1 = 0x10000001,
70 VIDEO_MPEG2 = 0x10000002,
71 VIDEO_MPEG4 = 0x10000004,
72 VIDEO_H264 = 0x10000005,
73 VIDEO_AVC = mmioFOURCC('a', 'v', 'c', '1'),
74 AUDIO_MP2 = 0x50,
75 AUDIO_A52 = 0x2000,
76 AUDIO_DTS = 0x2001,
77 AUDIO_LPCM_BE = 0x10001,
78 AUDIO_AAC = mmioFOURCC('M', 'P', '4', 'A'),
79 SPU_DVD = 0x3000000,
80 SPU_DVB = 0x3000001,
81 PES_PRIVATE1 = 0xBD00000,
82 SL_PES_STREAM = 0xD000000,
83 SL_SECTION = 0xD100000,
84 MP4_OD = 0xD200000,
85 } es_stream_type_t;
87 typedef struct {
88 uint8_t *buffer;
89 uint16_t buffer_len;
90 } ts_section_t;
92 typedef struct {
93 int size;
94 unsigned char *start;
95 uint16_t payload_size;
96 es_stream_type_t type, subtype;
97 float pts, last_pts;
98 int pid;
99 char lang[4];
100 int last_cc; // last cc code (-1 if first packet)
101 int is_synced;
102 ts_section_t section;
103 uint8_t *extradata;
104 int extradata_alloc, extradata_len;
105 struct {
106 uint8_t au_start, au_end, last_au_end;
107 } sl;
108 } ES_stream_t;
110 typedef struct {
111 void *sh;
112 int id;
113 int type;
114 } sh_av_t;
116 typedef struct MpegTSContext {
117 int packet_size; // raw packet size, including FEC if present e.g. 188 bytes
118 ES_stream_t *pids[NB_PID_MAX];
119 sh_av_t streams[NB_PID_MAX];
120 } MpegTSContext;
123 typedef struct {
124 demux_stream_t *ds;
125 demux_packet_t *pack;
126 int offset, buffer_size;
127 } av_fifo_t;
129 #define MAX_EXTRADATA_SIZE 64*1024
130 typedef struct {
131 int32_t object_type; //aka codec used
132 int32_t stream_type; //video, audio etc.
133 uint8_t buf[MAX_EXTRADATA_SIZE];
134 uint16_t buf_size;
135 uint8_t szm1;
136 } mp4_decoder_config_t;
138 typedef struct {
139 //flags
140 uint8_t flags;
141 uint8_t au_start;
142 uint8_t au_end;
143 uint8_t random_accesspoint;
144 uint8_t random_accesspoint_only;
145 uint8_t padding;
146 uint8_t use_ts;
147 uint8_t idle;
148 uint8_t duration;
150 uint32_t ts_resolution, ocr_resolution;
151 uint8_t ts_len, ocr_len, au_len, instant_bitrate_len, degr_len, au_seqnum_len, packet_seqnum_len;
152 uint32_t timescale;
153 uint16_t au_duration, cts_duration;
154 uint64_t ocr, dts, cts;
155 } mp4_sl_config_t;
157 typedef struct {
158 uint16_t id;
159 uint8_t flags;
160 mp4_decoder_config_t decoder;
161 mp4_sl_config_t sl;
162 } mp4_es_descr_t;
164 typedef struct {
165 uint16_t id;
166 uint8_t flags;
167 mp4_es_descr_t *es;
168 uint16_t es_cnt;
169 } mp4_od_t;
171 typedef struct {
172 uint8_t skip;
173 uint8_t table_id;
174 uint8_t ssi;
175 uint16_t section_length;
176 uint16_t ts_id;
177 uint8_t version_number;
178 uint8_t curr_next;
179 uint8_t section_number;
180 uint8_t last_section_number;
181 struct pat_progs_t {
182 uint16_t id;
183 uint16_t pmt_pid;
184 } *progs;
185 uint16_t progs_cnt;
186 ts_section_t section;
187 } pat_t;
189 typedef struct {
190 uint16_t progid;
191 uint8_t skip;
192 uint8_t table_id;
193 uint8_t ssi;
194 uint16_t section_length;
195 uint8_t version_number;
196 uint8_t curr_next;
197 uint8_t section_number;
198 uint8_t last_section_number;
199 uint16_t PCR_PID;
200 uint16_t prog_descr_length;
201 ts_section_t section;
202 uint16_t es_cnt;
203 struct pmt_es_t {
204 uint16_t pid;
205 uint32_t type; //it's 8 bit long, but cast to the right type as FOURCC
206 uint16_t descr_length;
207 uint8_t format_descriptor[5];
208 uint8_t lang[4];
209 uint16_t mp4_es_id;
210 } *es;
211 mp4_od_t iod, *od;
212 mp4_es_descr_t *mp4es;
213 int od_cnt, mp4es_cnt;
214 } pmt_t;
216 typedef struct {
217 uint64_t size;
218 float duration;
219 float first_pts;
220 float last_pts;
221 } TS_stream_info;
223 typedef struct {
224 MpegTSContext ts;
225 int last_pid;
226 av_fifo_t fifo[3]; //0 for audio, 1 for video, 2 for subs
227 pat_t pat;
228 pmt_t *pmt;
229 uint16_t pmt_cnt;
230 uint32_t prog;
231 uint32_t vbitrate;
232 int keep_broken;
233 int last_aid;
234 char packet[TS_FEC_PACKET_SIZE];
235 TS_stream_info vstr, astr;
236 } ts_priv_t;
239 typedef struct {
240 es_stream_type_t type;
241 ts_section_t section;
242 } TS_pids_t;
245 #define IS_AUDIO(x) (((x) == AUDIO_MP2) || ((x) == AUDIO_A52) || ((x) == AUDIO_LPCM_BE) || ((x) == AUDIO_AAC) || ((x) == AUDIO_DTS))
246 #define IS_VIDEO(x) (((x) == VIDEO_MPEG1) || ((x) == VIDEO_MPEG2) || ((x) == VIDEO_MPEG4) || ((x) == VIDEO_H264) || ((x) == VIDEO_AVC))
248 static int ts_parse(demuxer_t *demuxer, ES_stream_t *es, unsigned char *packet, int probe);
250 static uint8_t get_packet_size(const unsigned char *buf, int size)
252 int i;
254 if (size < (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS))
255 return 0;
257 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
259 if (buf[i * TS_PACKET_SIZE] != 0x47)
261 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
262 goto try_fec;
265 return TS_PACKET_SIZE;
267 try_fec:
268 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
270 if (buf[i * TS_FEC_PACKET_SIZE] != 0x47){
271 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
272 goto try_philips;
275 return TS_FEC_PACKET_SIZE;
277 try_philips:
278 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
280 if (buf[i * TS_PH_PACKET_SIZE] != 0x47)
281 return 0;
283 return TS_PH_PACKET_SIZE;
288 static int ts_check_file(demuxer_t * demuxer)
290 const int buf_size = (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS);
291 unsigned char buf[TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS], done = 0, *ptr;
292 uint32_t _read, i, count = 0, is_ts;
293 int cc[NB_PID_MAX], last_cc[NB_PID_MAX], pid, cc_ok, c, good, bad;
294 uint8_t size = 0;
295 off_t pos = 0;
296 off_t init_pos;
298 mp_msg(MSGT_DEMUX, MSGL_V, "Checking for MPEG-TS...\n");
300 init_pos = stream_tell(demuxer->stream);
301 is_ts = 0;
302 while(! done)
304 i = 1;
305 c = 0;
307 while(((c=stream_read_char(demuxer->stream)) != 0x47)
308 && (c >= 0)
309 && (i < MAX_CHECK_SIZE)
310 && ! demuxer->stream->eof
311 ) i++;
314 if(c != 0x47)
316 mp_msg(MSGT_DEMUX, MSGL_V, "THIS DOESN'T LOOK LIKE AN MPEG-TS FILE!\n");
317 is_ts = 0;
318 done = 1;
319 continue;
322 pos = stream_tell(demuxer->stream) - 1;
323 buf[0] = c;
324 _read = stream_read(demuxer->stream, &buf[1], buf_size-1);
326 if(_read < buf_size-1)
328 mp_msg(MSGT_DEMUX, MSGL_V, "COULDN'T READ ENOUGH DATA, EXITING TS_CHECK\n");
329 stream_reset(demuxer->stream);
330 return 0;
333 size = get_packet_size(buf, buf_size);
334 if(size)
336 done = 1;
337 is_ts = 1;
340 if(pos - init_pos >= MAX_CHECK_SIZE)
342 done = 1;
343 is_ts = 0;
347 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);
348 stream_seek(demuxer->stream, pos);
350 if(! is_ts)
351 return 0;
353 //LET'S CHECK continuity counters
354 good = bad = 0;
355 for(count = 0; count < NB_PID_MAX; count++)
357 cc[count] = last_cc[count] = -1;
360 for(count = 0; count < NUM_CONSECUTIVE_TS_PACKETS; count++)
362 ptr = &(buf[size * count]);
363 pid = ((ptr[1] & 0x1f) << 8) | ptr[2];
364 mp_msg(MSGT_DEMUX, MSGL_DBG2, "BUF: %02x %02x %02x %02x, PID %d, SIZE: %d \n",
365 ptr[0], ptr[1], ptr[2], ptr[3], pid, size);
367 if((pid == 8191) || (pid < 16))
368 continue;
370 cc[pid] = (ptr[3] & 0xf);
371 cc_ok = (last_cc[pid] < 0) || ((((last_cc[pid] + 1) & 0x0f) == cc[pid]));
372 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PID %d, COMPARE CC %d AND LAST_CC %d\n", pid, cc[pid], last_cc[pid]);
373 if(! cc_ok)
374 //return 0;
375 bad++;
376 else
377 good++;
379 last_cc[pid] = cc[pid];
382 mp_msg(MSGT_DEMUX, MSGL_V, "GOOD CC: %d, BAD CC: %d\n", good, bad);
384 if(good >= bad)
385 return size;
386 else
387 return 0;
391 static inline int32_t progid_idx_in_pmt(ts_priv_t *priv, uint16_t progid)
393 int x;
395 if(priv->pmt == NULL)
396 return -1;
398 for(x = 0; x < priv->pmt_cnt; x++)
400 if(priv->pmt[x].progid == progid)
401 return x;
404 return -1;
408 static inline int32_t progid_for_pid(ts_priv_t *priv, int pid, int32_t req) //finds the first program listing a pid
410 int i, j;
411 pmt_t *pmt;
414 if(priv->pmt == NULL)
415 return -1;
418 for(i=0; i < priv->pmt_cnt; i++)
420 pmt = &(priv->pmt[i]);
422 if(pmt->es == NULL)
423 return -1;
425 for(j = 0; j < pmt->es_cnt; j++)
427 if(pmt->es[j].pid == pid)
429 if((req == 0) || (req == pmt->progid))
430 return pmt->progid;
435 return -1;
439 static inline int pid_match_lang(ts_priv_t *priv, uint16_t pid, char *lang)
441 uint16_t i, j;
442 pmt_t *pmt;
444 if(priv->pmt == NULL)
445 return -1;
447 for(i=0; i < priv->pmt_cnt; i++)
449 pmt = &(priv->pmt[i]);
451 if(pmt->es == NULL)
452 return -1;
454 for(j = 0; j < pmt->es_cnt; j++)
456 if(pmt->es[j].pid != pid)
457 continue;
459 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);
460 if(strncmp(pmt->es[j].lang, lang, 3) == 0)
462 return 1;
468 return -1;
471 typedef struct {
472 int32_t atype, vtype, stype; //types
473 int32_t apid, vpid, spid; //stream ids
474 char slang[4], alang[4]; //languages
475 int16_t prog;
476 off_t probe;
477 } tsdemux_init_t;
479 //stripped down version of a52_syncinfo() from liba52
480 //copyright belongs to Michel Lespinasse <walken@zoy.org> and Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
481 int mp_a52_framesize(uint8_t * buf, int *srate)
483 int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
484 128, 160, 192, 224, 256, 320, 384, 448,
485 512, 576, 640
487 uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
488 int frmsizecod, bitrate, half;
490 if((buf[0] != 0x0b) || (buf[1] != 0x77)) /* syncword */
491 return 0;
493 if(buf[5] >= 0x60) /* bsid >= 12 */
494 return 0;
496 half = halfrate[buf[5] >> 3];
498 frmsizecod = buf[4] & 63;
499 if(frmsizecod >= 38)
500 return 0;
502 bitrate = rate[frmsizecod >> 1];
504 switch(buf[4] & 0xc0)
506 case 0: /* 48 KHz */
507 *srate = 48000 >> half;
508 return 4 * bitrate;
509 case 0x40: /* 44.1 KHz */
510 *srate = 44100 >> half;
511 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
512 case 0x80: /* 32 KHz */
513 *srate = 32000 >> half;
514 return 6 * bitrate;
517 return 0;
520 //second stage: returns the count of A52 syncwords found
521 static int a52_check(char *buf, int len)
523 int cnt, frame_length, ok, srate;
525 cnt = ok = 0;
526 if(len < 8)
527 return 0;
529 while(cnt < len - 7)
531 if(buf[cnt] == 0x0B && buf[cnt+1] == 0x77)
533 frame_length = mp_a52_framesize(&buf[cnt], &srate);
534 if(frame_length>=7 && frame_length<=3840)
536 cnt += frame_length;
537 ok++;
539 else
540 cnt++;
542 else
543 cnt++;
546 mp_msg(MSGT_DEMUXER, MSGL_V, "A52_CHECK(%d input bytes), found %d frame syncwords of %d bytes length\n", len, ok, frame_length);
547 return ok;
551 static off_t ts_detect_streams(demuxer_t *demuxer, tsdemux_init_t *param)
553 int video_found = 0, audio_found = 0, sub_found = 0, i, num_packets = 0, req_apid, req_vpid, req_spid;
554 int is_audio, is_video, is_sub, has_tables;
555 int32_t p, chosen_pid = 0;
556 off_t pos=0, ret = 0, init_pos;
557 ES_stream_t es;
558 unsigned char tmp[TS_FEC_PACKET_SIZE];
559 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
560 struct {
561 char *buf;
562 int pos;
563 } pes_priv1[8192], *pptr;
564 char *tmpbuf;
566 priv->last_pid = 8192; //invalid pid
568 req_apid = param->apid;
569 req_vpid = param->vpid;
570 req_spid = param->spid;
572 has_tables = 0;
573 memset(pes_priv1, 0, sizeof(pes_priv1));
574 init_pos = stream_tell(demuxer->stream);
575 mp_msg(MSGT_DEMUXER, MSGL_V, "PROBING UP TO %"PRIu64", PROG: %d\n", (uint64_t) param->probe, param->prog);
576 while((pos <= init_pos + param->probe) && (! demuxer->stream->eof))
578 pos = stream_tell(demuxer->stream);
579 if(ts_parse(demuxer, &es, tmp, 1))
581 //Non PES-aligned A52 audio may escape detection if PMT is not present;
582 //in this case we try to find at least 3 A52 syncwords
583 if((es.type == PES_PRIVATE1) && (! audio_found))
585 pptr = &pes_priv1[es.pid];
586 if(pptr->pos < 64*1024)
588 tmpbuf = (char*) realloc(pptr->buf, pptr->pos + es.size);
589 if(tmpbuf != NULL)
591 pptr->buf = tmpbuf;
592 memcpy(&(pptr->buf[ pptr->pos ]), es.start, es.size);
593 pptr->pos += es.size;
594 if(a52_check(pptr->buf, pptr->pos) > 2)
596 param->atype = AUDIO_A52;
597 param->apid = es.pid;
598 es.type = AUDIO_A52;
604 is_audio = IS_AUDIO(es.type) || ((es.type==SL_PES_STREAM) && IS_AUDIO(es.subtype));
605 is_video = IS_VIDEO(es.type) || ((es.type==SL_PES_STREAM) && IS_VIDEO(es.subtype));
606 is_sub = ((es.type == SPU_DVD) || (es.type == SPU_DVB));
609 if((! is_audio) && (! is_video) && (! is_sub))
610 continue;
612 if(is_video)
614 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_VIDEO_ID=%d\n", es.pid);
615 chosen_pid = (req_vpid == es.pid);
616 if((! chosen_pid) && (req_vpid > 0))
617 continue;
619 else if(is_audio)
621 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_AUDIO_ID=%d\n", es.pid);
622 if (es.lang[0] > 0)
623 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_AID_%d_LANG=%s\n", es.pid, es.lang);
624 if(req_apid > 0)
626 chosen_pid = (req_apid == es.pid);
627 if(! chosen_pid)
628 continue;
630 else if(param->alang[0] > 0)
632 if(pid_match_lang(priv, es.pid, param->alang) == -1)
633 continue;
635 chosen_pid = 1;
636 param->apid = req_apid = es.pid;
639 else if(is_sub)
641 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_SUBTITLE_ID=%d\n", es.pid);
642 if (es.lang[0] > 0)
643 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_SID_%d_LANG=%s\n", es.pid, es.lang);
644 chosen_pid = (req_spid == es.pid);
645 if((! chosen_pid) && (req_spid > 0))
646 continue;
649 if(req_apid < 0 && (param->alang[0] == 0) && req_vpid < 0 && req_spid < 0)
650 chosen_pid = 1;
652 if((ret == 0) && chosen_pid)
654 ret = stream_tell(demuxer->stream);
657 p = progid_for_pid(priv, es.pid, param->prog);
658 if(p != -1)
659 has_tables++;
661 if((param->prog == 0) && (p != -1))
663 if(chosen_pid)
664 param->prog = p;
667 if((param->prog > 0) && (param->prog != p))
669 if(audio_found)
671 if(is_video && (req_vpid == es.pid))
673 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
674 param->vpid = es.pid;
675 video_found = 1;
676 break;
680 if(video_found)
682 if(is_audio && (req_apid == es.pid))
684 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
685 param->apid = es.pid;
686 audio_found = 1;
687 break;
692 continue;
696 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "TYPE: %x, PID: %d, PROG FOUND: %d\n", es.type, es.pid, param->prog);
699 if(is_video)
701 if((req_vpid == -1) || (req_vpid == es.pid))
703 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
704 param->vpid = es.pid;
705 video_found = 1;
710 if(((req_vpid == -2) || (num_packets >= NUM_CONSECUTIVE_AUDIO_PACKETS)) && audio_found)
712 //novideo or we have at least 348 audio packets (64 KB) without video (TS with audio only)
713 param->vtype = 0;
714 break;
717 if(is_sub)
719 if((req_spid == -1) || (req_spid == es.pid))
721 param->stype = es.type;
722 param->spid = es.pid;
723 sub_found = 1;
727 if(is_audio)
729 if((req_apid == -1) || (req_apid == es.pid))
731 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
732 param->apid = es.pid;
733 audio_found = 1;
737 if(audio_found && (param->apid == es.pid) && (! video_found))
738 num_packets++;
740 if((req_apid == -2) && video_found)
742 param->atype = 0;
743 break;
746 if((has_tables==0) && (video_found && audio_found) && (pos >= 1000000))
747 break;
751 for(i=0; i<8192; i++)
753 if(pes_priv1[i].buf != NULL)
755 free(pes_priv1[i].buf);
756 pes_priv1[i].buf = NULL;
757 pes_priv1[i].pos = 0;
761 if(video_found)
763 if(param->vtype == VIDEO_MPEG1)
764 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG1(pid=%d) ", param->vpid);
765 else if(param->vtype == VIDEO_MPEG2)
766 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG2(pid=%d) ", param->vpid);
767 else if(param->vtype == VIDEO_MPEG4)
768 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG4(pid=%d) ", param->vpid);
769 else if(param->vtype == VIDEO_H264)
770 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO H264(pid=%d) ", param->vpid);
771 else if(param->vtype == VIDEO_AVC)
772 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO AVC(NAL-H264, pid=%d) ", param->vpid);
774 else
776 param->vtype = UNKNOWN;
777 //WE DIDN'T MATCH ANY VIDEO STREAM
778 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO VIDEO! ");
781 if(param->atype == AUDIO_MP2)
782 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO MPA(pid=%d)", param->apid);
783 else if(param->atype == AUDIO_A52)
784 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO A52(pid=%d)", param->apid);
785 else if(param->atype == AUDIO_DTS)
786 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO DTS(pid=%d)", param->apid);
787 else if(param->atype == AUDIO_LPCM_BE)
788 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO LPCM(pid=%d)", param->apid);
789 else if(param->atype == AUDIO_AAC)
790 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO AAC(pid=%d)", param->apid);
791 else
793 audio_found = 0;
794 param->atype = UNKNOWN;
795 //WE DIDN'T MATCH ANY AUDIO STREAM, SO WE FORCE THE DEMUXER TO IGNORE AUDIO
796 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO AUDIO! ");
799 if(param->stype == SPU_DVD || param->stype == SPU_DVB)
800 mp_msg(MSGT_DEMUXER, MSGL_INFO, " SUB %s(pid=%d) ", (param->stype==SPU_DVD ? "DVD" : "DVB"), param->spid);
801 else
803 param->stype = UNKNOWN;
804 mp_msg(MSGT_DEMUXER, MSGL_INFO, " NO SUBS (yet)! ");
807 if(video_found || audio_found)
809 if(demuxer->stream->eof && (ret == 0))
810 ret = init_pos;
811 mp_msg(MSGT_DEMUXER, MSGL_INFO, " PROGRAM N. %d\n", param->prog);
813 else
814 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\n");
817 for(i=0; i<8192; i++)
819 if(priv->ts.pids[i] != NULL)
821 priv->ts.pids[i]->payload_size = 0;
822 priv->ts.pids[i]->pts = priv->ts.pids[i]->last_pts = 0;
823 priv->ts.pids[i]->last_cc = -1;
824 priv->ts.pids[i]->is_synced = 0;
828 return ret;
831 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h)
833 int sps, sps_len;
834 unsigned char *ptr;
835 mp_mpeg_header_t picture;
836 if(len < 6)
837 return 0;
838 sps = buf[5] & 0x1f;
839 if(!sps)
840 return 0;
841 sps_len = (buf[6] << 8) | buf[7];
842 if(!sps_len || (sps_len > len - 8))
843 return 0;
844 ptr = &(buf[8]);
845 picture.display_picture_width = picture.display_picture_height = 0;
846 h264_parse_sps(&picture, ptr, len - 8);
847 if(!picture.display_picture_width || !picture.display_picture_height)
848 return 0;
849 *w = picture.display_picture_width;
850 *h = picture.display_picture_height;
851 return 1;
854 static demuxer_t *demux_open_ts(demuxer_t * demuxer)
856 int i;
857 uint8_t packet_size;
858 sh_video_t *sh_video;
859 sh_audio_t *sh_audio;
860 off_t start_pos;
861 tsdemux_init_t params;
862 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
864 mp_msg(MSGT_DEMUX, MSGL_V, "DEMUX OPEN, AUDIO_ID: %d, VIDEO_ID: %d, SUBTITLE_ID: %d,\n",
865 demuxer->audio->id, demuxer->video->id, demuxer->sub->id);
868 demuxer->type= DEMUXER_TYPE_MPEG_TS;
871 stream_reset(demuxer->stream);
873 packet_size = ts_check_file(demuxer);
874 if(!packet_size)
875 return NULL;
877 priv = calloc(1, sizeof(ts_priv_t));
878 if(priv == NULL)
880 mp_msg(MSGT_DEMUX, MSGL_FATAL, "DEMUX_OPEN_TS, couldn't allocate enough memory for ts->priv, exit\n");
881 return NULL;
884 for(i=0; i < 8192; i++)
886 priv->ts.pids[i] = NULL;
887 priv->ts.streams[i].id = -3;
889 priv->pat.progs = NULL;
890 priv->pat.progs_cnt = 0;
891 priv->pat.section.buffer = NULL;
892 priv->pat.section.buffer_len = 0;
894 priv->pmt = NULL;
895 priv->pmt_cnt = 0;
897 priv->keep_broken = ts_keep_broken;
898 priv->ts.packet_size = packet_size;
901 demuxer->priv = priv;
902 if(demuxer->stream->type != STREAMTYPE_FILE)
903 demuxer->seekable = 1;
904 else
905 demuxer->seekable = 1;
908 params.atype = params.vtype = params.stype = UNKNOWN;
909 params.apid = demuxer->audio->id;
910 params.vpid = demuxer->video->id;
911 params.spid = demuxer->sub->id;
912 params.prog = ts_prog;
913 params.probe = ts_probe;
915 if(dvdsub_lang != NULL)
917 strncpy(params.slang, dvdsub_lang, 3);
918 params.slang[3] = 0;
920 else
921 memset(params.slang, 0, 4);
923 if(audio_lang != NULL)
925 strncpy(params.alang, audio_lang, 3);
926 params.alang[3] = 0;
928 else
929 memset(params.alang, 0, 4);
931 start_pos = ts_detect_streams(demuxer, &params);
933 demuxer->video->id = params.vpid;
934 demuxer->sub->id = params.spid;
935 priv->prog = params.prog;
937 if(params.vtype != UNKNOWN)
939 ES_stream_t *es = priv->ts.pids[params.vpid];
940 sh_video = new_sh_video_vid(demuxer, 0, es->pid);
941 if(params.vtype == VIDEO_AVC && es->extradata && es->extradata_len)
943 int w = 0, h = 0;
944 sh_video->bih = (BITMAPINFOHEADER *) calloc(1, sizeof(BITMAPINFOHEADER) + es->extradata_len);
945 sh_video->bih->biSize= sizeof(BITMAPINFOHEADER) + es->extradata_len;
946 sh_video->bih->biCompression = params.vtype;
947 memcpy(sh_video->bih + 1, es->extradata, es->extradata_len);
948 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "EXTRADATA(%d BYTES): \n", es->extradata_len);
949 for(i = 0;i < es->extradata_len; i++)
950 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "%02x ", (int) es->extradata[i]);
951 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"\n");
952 if(parse_avc_sps(es->extradata, es->extradata_len, &w, &h))
954 sh_video->bih->biWidth = w;
955 sh_video->bih->biHeight = h;
958 sh_video->ds = demuxer->video;
959 sh_video->format = params.vtype;
960 demuxer->video->sh = sh_video;
963 if(params.atype != UNKNOWN)
965 ES_stream_t *es = priv->ts.pids[params.apid];
966 sh_audio = new_sh_audio_aid(demuxer, 0, es->pid);
967 priv->ts.streams[params.apid].id = 0;
968 priv->ts.streams[params.apid].sh = sh_audio;
969 priv->ts.streams[params.apid].type = TYPE_AUDIO;
970 priv->last_aid = 0;
971 demuxer->audio->id = 0;
972 sh_audio->ds = demuxer->audio;
973 sh_audio->format = params.atype;
974 demuxer->audio->sh = sh_audio;
975 if(es->extradata && es->extradata_len)
977 sh_audio->wf = (WAVEFORMATEX *) malloc(sizeof (WAVEFORMATEX) + es->extradata_len);
978 sh_audio->wf->cbSize = es->extradata_len;
979 memcpy(sh_audio->wf + 1, es->extradata, es->extradata_len);
984 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);
987 start_pos = (start_pos <= priv->ts.packet_size ? 0 : start_pos - priv->ts.packet_size);
988 demuxer->movi_start = start_pos;
989 stream_reset(demuxer->stream);
990 stream_seek(demuxer->stream, start_pos); //IF IT'S FROM A PIPE IT WILL FAIL, BUT WHO CARES?
993 priv->last_pid = 8192; //invalid pid
995 for(i = 0; i < 3; i++)
997 priv->fifo[i].pack = NULL;
998 priv->fifo[i].offset = 0;
1000 priv->fifo[0].ds = demuxer->audio;
1001 priv->fifo[1].ds = demuxer->video;
1002 priv->fifo[2].ds = demuxer->sub;
1004 priv->fifo[0].buffer_size = 1536;
1005 priv->fifo[1].buffer_size = 32767;
1006 priv->fifo[2].buffer_size = 32767;
1008 priv->pat.section.buffer_len = 0;
1009 for(i = 0; i < priv->pmt_cnt; i++)
1010 priv->pmt[i].section.buffer_len = 0;
1012 demuxer->filepos = stream_tell(demuxer->stream);
1013 return demuxer;
1016 static void demux_close_ts(demuxer_t * demuxer)
1018 uint16_t i;
1019 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
1021 if(priv)
1023 if(priv->pat.section.buffer)
1024 free(priv->pat.section.buffer);
1025 if(priv->pat.progs)
1026 free(priv->pat.progs);
1028 if(priv->pmt)
1030 for(i = 0; i < priv->pmt_cnt; i++)
1032 if(priv->pmt[i].section.buffer)
1033 free(priv->pmt[i].section.buffer);
1034 if(priv->pmt[i].es)
1035 free(priv->pmt[i].es);
1037 free(priv->pmt);
1039 free(priv);
1041 demuxer->priv=NULL;
1045 extern unsigned char mp_getbits(unsigned char*, unsigned int, unsigned char);
1046 #define getbits mp_getbits
1048 static int mp4_parse_sl_packet(pmt_t *pmt, uint8_t *buf, uint16_t packet_len, int pid, ES_stream_t *pes_es)
1050 int i, n, m, mp4_es_id = -1;
1051 uint64_t v = 0;
1052 uint32_t pl_size = 0;
1053 int deg_flag = 0;
1054 mp4_es_descr_t *es = NULL;
1055 mp4_sl_config_t *sl = NULL;
1056 uint8_t au_start = 0, au_end = 0, rap_flag = 0, ocr_flag = 0, padding = 0, padding_bits = 0, idle = 0;
1058 pes_es->is_synced = 0;
1059 mp_msg(MSGT_DEMUXER,MSGL_V, "mp4_parse_sl_packet, pid: %d, pmt: %pm, packet_len: %d\n", pid, pmt, packet_len);
1060 if(! pmt || !packet_len)
1061 return 0;
1063 for(i = 0; i < pmt->es_cnt; i++)
1065 if(pmt->es[i].pid == pid)
1066 mp4_es_id = pmt->es[i].mp4_es_id;
1068 if(mp4_es_id < 0)
1069 return -1;
1071 for(i = 0; i < pmt->mp4es_cnt; i++)
1073 if(pmt->mp4es[i].id == mp4_es_id)
1074 es = &(pmt->mp4es[i]);
1076 if(! es)
1077 return -1;
1079 pes_es->subtype = es->decoder.object_type;
1081 sl = &(es->sl);
1082 if(!sl)
1083 return -1;
1085 //now es is the complete es_descriptor of out mp4 ES stream
1086 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "ID: %d, FLAGS: 0x%x, subtype: %x\n", es->id, sl->flags, pes_es->subtype);
1088 n = 0;
1089 if(sl->au_start)
1090 pes_es->sl.au_start = au_start = getbits(buf, n++, 1);
1091 else
1092 pes_es->sl.au_start = (pes_es->sl.last_au_end ? 1 : 0);
1093 if(sl->au_end)
1094 pes_es->sl.au_end = au_end = getbits(buf, n++, 1);
1096 if(!sl->au_start && !sl->au_end)
1098 pes_es->sl.au_start = pes_es->sl.au_end = au_start = au_end = 1;
1100 pes_es->sl.last_au_end = pes_es->sl.au_end;
1103 if(sl->ocr_len > 0)
1104 ocr_flag = getbits(buf, n++, 1);
1105 if(sl->idle)
1106 idle = getbits(buf, n++, 1);
1107 if(sl->padding)
1108 padding = getbits(buf, n++, 1);
1109 if(padding)
1111 padding_bits = getbits(buf, n, 3);
1112 n += 3;
1115 if(idle || (padding && !padding_bits))
1117 pes_es->payload_size = 0;
1118 return -1;
1121 //(! idle && (!padding || padding_bits != 0)) is true
1122 n += sl->packet_seqnum_len;
1123 if(sl->degr_len)
1124 deg_flag = getbits(buf, n++, 1);
1125 if(deg_flag)
1126 n += sl->degr_len;
1128 if(ocr_flag)
1130 n += sl->ocr_len;
1131 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "OCR: %d bits\n", sl->ocr_len);
1134 if(packet_len * 8 <= n)
1135 return -1;
1137 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "\nAU_START: %d, AU_END: %d\n", au_start, au_end);
1138 if(au_start)
1140 int dts_flag = 0, cts_flag = 0, ib_flag = 0;
1142 if(sl->random_accesspoint)
1143 rap_flag = getbits(buf, n++, 1);
1145 //check commented because it seems it's rarely used, and we need this flag set in case of au_start
1146 //the decoder will eventually discard the payload if it can't decode it
1147 //if(rap_flag || sl->random_accesspoint_only)
1148 pes_es->is_synced = 1;
1150 n += sl->au_seqnum_len;
1151 if(packet_len * 8 <= n+8)
1152 return -1;
1153 if(sl->use_ts)
1155 dts_flag = getbits(buf, n++, 1);
1156 cts_flag = getbits(buf, n++, 1);
1158 if(sl->instant_bitrate_len)
1159 ib_flag = getbits(buf, n++, 1);
1160 if(packet_len * 8 <= n+8)
1161 return -1;
1162 if(dts_flag && (sl->ts_len > 0))
1164 n += sl->ts_len;
1165 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "DTS: %d bits\n", sl->ts_len);
1167 if(packet_len * 8 <= n+8)
1168 return -1;
1169 if(cts_flag && (sl->ts_len > 0))
1171 int i = 0, m;
1173 while(i < sl->ts_len)
1175 m = min(8, sl->ts_len - i);
1176 v |= getbits(buf, n, m);
1177 if(sl->ts_len - i > 8)
1178 v <<= 8;
1179 i += m;
1180 n += m;
1181 if(packet_len * 8 <= n+8)
1182 return -1;
1185 pes_es->pts = (float) v / (float) sl->ts_resolution;
1186 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "CTS: %d bits, value: %"PRIu64"/%d = %.3f\n", sl->ts_len, v, sl->ts_resolution, pes_es->pts);
1190 i = 0;
1191 pl_size = 0;
1192 while(i < sl->au_len)
1194 m = min(8, sl->au_len - i);
1195 pl_size |= getbits(buf, n, m);
1196 if(sl->au_len - i > 8)
1197 pl_size <<= 8;
1198 i += m;
1199 n += m;
1200 if(packet_len * 8 <= n+8)
1201 return -1;
1203 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "AU_LEN: %u (%d bits)\n", pl_size, sl->au_len);
1204 if(ib_flag)
1205 n += sl->instant_bitrate_len;
1208 m = (n+7)/8;
1209 if(0 < pl_size && pl_size < pes_es->payload_size)
1210 pes_es->payload_size = pl_size;
1212 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",
1213 n, m, pes_es->payload_size, pl_size, (int) rap_flag, (int) sl->random_accesspoint_only);
1215 return m;
1218 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)
1220 unsigned char *p;
1221 uint32_t header_len;
1222 int64_t pts;
1223 uint32_t stream_id;
1224 uint32_t pkt_len, pes_is_aligned;
1226 //Here we are always at the start of a PES packet
1227 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2(%p, %d): \n", buf, (uint32_t) packet_len);
1229 if(packet_len == 0 || packet_len > 184)
1231 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2, BUFFER LEN IS TOO SMALL OR TOO BIG: %d EXIT\n", packet_len);
1232 return 0;
1235 p = buf;
1236 pkt_len = packet_len;
1239 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: HEADER %02x %02x %02x %02x\n", p[0], p[1], p[2], p[3]);
1240 if (p[0] || p[1] || (p[2] != 1))
1242 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: error HEADER %02x %02x %02x (should be 0x000001) \n", p[0], p[1], p[2]);
1243 return 0 ;
1246 packet_len -= 6;
1247 if(packet_len==0)
1249 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: packet too short: %d, exit\n", packet_len);
1250 return 0;
1253 es->payload_size = (p[4] << 8 | p[5]);
1254 pes_is_aligned = (p[6] & 4);
1256 stream_id = p[3];
1259 if (p[7] & 0x80)
1260 { /* pts available */
1261 pts = (int64_t)(p[9] & 0x0E) << 29 ;
1262 pts |= p[10] << 22 ;
1263 pts |= (p[11] & 0xFE) << 14 ;
1264 pts |= p[12] << 7 ;
1265 pts |= (p[13] & 0xFE) >> 1 ;
1267 es->pts = pts / 90000.0f;
1269 else
1270 es->pts = 0.0f;
1273 header_len = p[8];
1276 if (header_len + 9 > pkt_len) //9 are the bytes read up to the header_length field
1278 mp_msg(MSGT_DEMUX, MSGL_DBG2, "demux_ts: illegal value for PES_header_data_length (0x%02x)\n", header_len);
1279 return 0;
1282 p += header_len + 9;
1283 packet_len -= header_len + 3;
1285 if(es->payload_size)
1286 es->payload_size -= header_len + 3;
1289 if (stream_id == 0xbd)
1291 mp_msg(MSGT_DEMUX, MSGL_DBG3, "pes_parse2: audio buf = %02X %02X %02X %02X %02X %02X %02X %02X, 80: %d\n",
1292 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[0] & 0x80);
1296 * we check the descriptor tag first because some stations
1297 * do not include any of the A52 header info in their audio tracks
1298 * these "raw" streams may begin with a byte that looks like a stream type.
1303 (type_from_pmt == AUDIO_A52) || /* A52 - raw */
1304 (p[0] == 0x0B && p[1] == 0x77) /* A52 - syncword */
1307 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 RAW OR SYNCWORD\n");
1308 es->start = p;
1309 es->size = packet_len;
1310 es->type = AUDIO_A52;
1311 es->payload_size -= packet_len;
1313 return 1;
1315 /* SPU SUBS */
1316 else if(type_from_pmt == SPU_DVB ||
1317 ((p[0] == 0x20) && pes_is_aligned)) // && p[1] == 0x00))
1319 es->start = p;
1320 es->size = packet_len;
1321 es->type = SPU_DVB;
1322 es->payload_size -= packet_len;
1324 return 1;
1326 else if (pes_is_aligned && ((p[0] & 0xE0) == 0x20)) //SPU_DVD
1328 //DVD SUBS
1329 es->start = p+1;
1330 es->size = packet_len-1;
1331 es->type = SPU_DVD;
1332 es->payload_size -= packet_len;
1334 return 1;
1336 else if (pes_is_aligned && (p[0] & 0xF8) == 0x80)
1338 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 WITH HEADER\n");
1339 es->start = p+4;
1340 es->size = packet_len - 4;
1341 es->type = AUDIO_A52;
1342 es->payload_size -= packet_len;
1344 return 1;
1346 else if (pes_is_aligned && ((p[0]&0xf0) == 0xa0))
1348 int pcm_offset;
1350 for (pcm_offset=0; ++pcm_offset < packet_len-1 ; )
1352 if (p[pcm_offset] == 0x01 && p[pcm_offset+1] == 0x80)
1353 { /* START */
1354 pcm_offset += 2;
1355 break;
1359 es->start = p + pcm_offset;
1360 es->size = packet_len - pcm_offset;
1361 es->type = AUDIO_LPCM_BE;
1362 es->payload_size -= packet_len;
1364 return 1;
1366 else
1368 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PES_PRIVATE1\n");
1369 es->start = p;
1370 es->size = packet_len;
1371 es->type = (type_from_pmt == UNKNOWN ? PES_PRIVATE1 : type_from_pmt);
1372 es->payload_size -= packet_len;
1374 return 1;
1377 else if((stream_id >= 0xe0) && (stream_id <= 0xef))
1379 es->start = p;
1380 es->size = packet_len;
1381 if(type_from_pmt != UNKNOWN)
1382 es->type = type_from_pmt;
1383 else
1384 es->type = VIDEO_MPEG2;
1385 if(es->payload_size)
1386 es->payload_size -= packet_len;
1388 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: M2V size %d\n", es->size);
1389 return 1;
1391 else if ((stream_id == 0xfa))
1393 int l;
1395 if(type_from_pmt != UNKNOWN) //MP4 A/V or SL
1397 es->start = p;
1398 es->size = packet_len;
1399 es->type = type_from_pmt;
1401 if(type_from_pmt == SL_PES_STREAM)
1403 //if(pes_is_aligned)
1405 l = mp4_parse_sl_packet(pmt, p, packet_len, pid, es);
1406 mp_msg(MSGT_DEMUX, MSGL_DBG2, "L=%d, TYPE=%x\n", l, type_from_pmt);
1407 if(l < 0)
1409 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: couldn't parse SL header, passing along full PES payload\n");
1410 l = 0;
1414 es->start += l;
1415 es->size -= l;
1418 if(es->payload_size)
1419 es->payload_size -= packet_len;
1420 return 1;
1423 else if ((stream_id & 0xe0) == 0xc0)
1425 es->start = p;
1426 es->size = packet_len;
1428 if(type_from_pmt != UNKNOWN)
1429 es->type = type_from_pmt;
1430 else
1431 es->type = AUDIO_MP2;
1433 es->payload_size -= packet_len;
1435 return 1;
1437 else if (type_from_pmt != -1) //as a last resort here we trust the PMT, if present
1439 es->start = p;
1440 es->size = packet_len;
1441 es->type = type_from_pmt;
1442 es->payload_size -= packet_len;
1444 return 1;
1446 else
1448 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: unknown packet, id: %x\n", stream_id);
1451 return 0;
1457 static int ts_sync(stream_t *stream)
1459 int c=0;
1461 mp_msg(MSGT_DEMUX, MSGL_DBG2, "TS_SYNC \n");
1463 while(((c=stream_read_char(stream)) != 0x47) && ! stream->eof);
1465 if(c == 0x47)
1466 return c;
1467 else
1468 return 0;
1472 static void ts_dump_streams(ts_priv_t *priv)
1474 int i;
1476 for(i = 0; i < 3; i++)
1478 if((priv->fifo[i].pack != NULL) && (priv->fifo[i].offset != 0))
1480 resize_demux_packet(priv->fifo[i].pack, priv->fifo[i].offset);
1481 ds_add_packet(priv->fifo[i].ds, priv->fifo[i].pack);
1482 priv->fifo[i].offset = 0;
1483 priv->fifo[i].pack = NULL;
1489 static inline int32_t prog_idx_in_pat(ts_priv_t *priv, uint16_t progid)
1491 int x;
1493 if(priv->pat.progs == NULL)
1494 return -1;
1496 for(x = 0; x < priv->pat.progs_cnt; x++)
1498 if(priv->pat.progs[x].id == progid)
1499 return x;
1502 return -1;
1506 static inline int32_t prog_id_in_pat(ts_priv_t *priv, uint16_t pid)
1508 int x;
1510 if(priv->pat.progs == NULL)
1511 return -1;
1513 for(x = 0; x < priv->pat.progs_cnt; x++)
1515 if(priv->pat.progs[x].pmt_pid == pid)
1516 return priv->pat.progs[x].id;
1519 return -1;
1523 DON'T REMOVE, I'LL NEED IT IN THE (NEAR) FUTURE)
1524 static int check_crc32(uint32_t val, uint8_t *ptr, uint16_t len, uint8_t *vbase)
1526 uint32_t tab_crc32, calc_crc32;
1528 calc_crc32 = CalcCRC32(val, ptr, len);
1530 tab_crc32 = (vbase[0] << 24) | (vbase[1] << 16) | (vbase[2] << 8) | vbase[3];
1532 printf("CRC32, TAB: %x, CALC: %x, eq: %x\n", tab_crc32, calc_crc32, tab_crc32 == calc_crc32);
1533 return (tab_crc32 == calc_crc32);
1538 static int collect_section(ts_section_t *section, int is_start, unsigned char *buff, int size)
1540 uint8_t *ptr;
1541 uint16_t tlen;
1542 int skip, tid;
1544 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, start: %d, size: %d, collected: %d\n", is_start, size, section->buffer_len);
1545 if(! is_start && !section->buffer_len)
1546 return 0;
1548 if(is_start)
1550 if(! section->buffer)
1552 section->buffer = (uint8_t*) malloc(4096+256);
1553 if(section->buffer == NULL)
1554 return 0;
1556 section->buffer_len = 0;
1559 if(size + section->buffer_len > 4096+256)
1561 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, excessive len: %d + %d\n", section->buffer_len, size);
1562 return 0;
1565 memcpy(&(section->buffer[section->buffer_len]), buff, size);
1566 section->buffer_len += size;
1568 if(section->buffer_len < 3)
1569 return 0;
1571 skip = section->buffer[0];
1572 if(skip + 4 > section->buffer_len)
1573 return 0;
1575 ptr = &(section->buffer[skip + 1]);
1576 tid = ptr[0];
1577 tlen = ((ptr[1] & 0x0f) << 8) | ptr[2];
1578 mp_msg(MSGT_DEMUX, MSGL_V, "SKIP: %d+1, TID: %d, TLEN: %d, COLLECTED: %d\n", skip, tid, tlen, section->buffer_len);
1579 if(section->buffer_len < (skip+1+3+tlen))
1581 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DATA IS NOT ENOUGH, NEXT TIME\n");
1582 return 0;
1585 return skip+1;
1588 static int parse_pat(ts_priv_t * priv, int is_start, unsigned char *buff, int size)
1590 int skip;
1591 unsigned char *ptr;
1592 unsigned char *base;
1593 int entries, i;
1594 uint16_t progid;
1595 struct pat_progs_t *tmp;
1596 ts_section_t *section;
1598 section = &(priv->pat.section);
1599 skip = collect_section(section, is_start, buff, size);
1600 if(! skip)
1601 return 0;
1603 ptr = &(section->buffer[skip]);
1604 //PARSING
1605 priv->pat.table_id = ptr[0];
1606 if(priv->pat.table_id != 0)
1607 return 0;
1608 priv->pat.ssi = (ptr[1] >> 7) & 0x1;
1609 priv->pat.curr_next = ptr[5] & 0x01;
1610 priv->pat.ts_id = (ptr[3] << 8 ) | ptr[4];
1611 priv->pat.version_number = (ptr[5] >> 1) & 0x1F;
1612 priv->pat.section_length = ((ptr[1] & 0x03) << 8 ) | ptr[2];
1613 priv->pat.section_number = ptr[6];
1614 priv->pat.last_section_number = ptr[7];
1616 //check_crc32(0xFFFFFFFFL, ptr, priv->pat.buffer_len - 4, &ptr[priv->pat.buffer_len - 4]);
1617 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);
1619 entries = (int) (priv->pat.section_length - 9) / 4; //entries per section
1621 for(i=0; i < entries; i++)
1623 int32_t idx;
1624 base = &ptr[8 + i*4];
1625 progid = (base[0] << 8) | base[1];
1627 if((idx = prog_idx_in_pat(priv, progid)) == -1)
1629 int sz = sizeof(struct pat_progs_t) * (priv->pat.progs_cnt+1);
1630 tmp = realloc_struct(priv->pat.progs, priv->pat.progs_cnt+1, sizeof(struct pat_progs_t));
1631 if(tmp == NULL)
1633 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PAT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
1634 break;
1636 priv->pat.progs = tmp;
1637 idx = priv->pat.progs_cnt;
1638 priv->pat.progs_cnt++;
1641 priv->pat.progs[idx].id = progid;
1642 priv->pat.progs[idx].pmt_pid = ((base[2] & 0x1F) << 8) | base[3];
1643 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);
1646 return 1;
1650 static inline int32_t es_pid_in_pmt(pmt_t * pmt, uint16_t pid)
1652 uint16_t i;
1654 if(pmt == NULL)
1655 return -1;
1657 if(pmt->es == NULL)
1658 return -1;
1660 for(i = 0; i < pmt->es_cnt; i++)
1662 if(pmt->es[i].pid == pid)
1663 return (int32_t) i;
1666 return -1;
1670 static uint16_t get_mp4_desc_len(uint8_t *buf, int *len)
1672 //uint16_t i = 0, size = 0;
1673 int i = 0, j, size = 0;
1675 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PARSE_MP4_DESC_LEN(%d), bytes: ", *len);
1676 j = min(*len, 4);
1677 while(i < j)
1679 mp_msg(MSGT_DEMUX, MSGL_DBG2, " %x ", buf[i]);
1680 size |= (buf[i] & 0x7f);
1681 if(!(buf[i] & 0x80))
1682 break;
1683 size <<= 7;
1684 i++;
1686 mp_msg(MSGT_DEMUX, MSGL_DBG2, ", SIZE=%d\n", size);
1688 *len = i+1;
1689 return size;
1693 static uint16_t parse_mp4_slconfig_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
1695 int i = 0;
1696 mp4_es_descr_t *es;
1697 mp4_sl_config_t *sl;
1699 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_SLCONFIG_DESCRIPTOR(%d)\n", len);
1700 es = (mp4_es_descr_t *) elem;
1701 if(!es)
1703 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1704 return len;
1706 sl = &(es->sl);
1708 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;
1709 sl->ocr = sl->dts = sl->cts = 0;
1711 if(buf[0] == 0)
1713 i++;
1714 sl->flags = buf[i];
1715 i++;
1716 sl->ts_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1717 i += 4;
1718 sl->ocr_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1719 i += 4;
1720 sl->ts_len = buf[i];
1721 i++;
1722 sl->ocr_len = buf[i];
1723 i++;
1724 sl->au_len = buf[i];
1725 i++;
1726 sl->instant_bitrate_len = buf[i];
1727 i++;
1728 sl->degr_len = (buf[i] >> 4) & 0x0f;
1729 sl->au_seqnum_len = ((buf[i] & 0x0f) << 1) | ((buf[i+1] >> 7) & 0x01);
1730 i++;
1731 sl->packet_seqnum_len = ((buf[i] >> 2) & 0x1f);
1732 i++;
1735 else if(buf[0] == 1)
1737 sl->flags = 0;
1738 sl->ts_resolution = 1000;
1739 sl->ts_len = 32;
1740 i++;
1742 else if(buf[0] == 2)
1744 sl->flags = 4;
1745 i++;
1747 else
1749 sl->flags = 0;
1750 i++;
1753 sl->au_start = (sl->flags >> 7) & 0x1;
1754 sl->au_end = (sl->flags >> 6) & 0x1;
1755 sl->random_accesspoint = (sl->flags >> 5) & 0x1;
1756 sl->random_accesspoint_only = (sl->flags >> 4) & 0x1;
1757 sl->padding = (sl->flags >> 3) & 0x1;
1758 sl->use_ts = (sl->flags >> 2) & 0x1;
1759 sl->idle = (sl->flags >> 1) & 0x1;
1760 sl->duration = sl->flags & 0x1;
1762 if(sl->duration)
1764 sl->timescale = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1765 i += 4;
1766 sl->au_duration = (buf[i] << 8) | buf[i+1];
1767 i += 2;
1768 sl->cts_duration = (buf[i] << 8) | buf[i+1];
1769 i += 2;
1771 else //no support for fixed durations atm
1772 sl->timescale = sl->au_duration = sl->cts_duration = 0;
1774 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",
1775 len, buf[0], sl->flags, sl->use_ts, sl->ts_len, sl->timescale, (uint64_t) sl->dts, (uint64_t) sl->cts);
1777 return len;
1780 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem);
1782 static uint16_t parse_mp4_decoder_config_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
1784 int i = 0, j;
1785 mp4_es_descr_t *es;
1786 mp4_decoder_config_t *dec;
1788 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_CONFIG_DESCRIPTOR(%d)\n", len);
1789 es = (mp4_es_descr_t *) elem;
1790 if(!es)
1792 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1793 return len;
1795 dec = (mp4_decoder_config_t*) &(es->decoder);
1797 dec->object_type = buf[i];
1798 dec->stream_type = (buf[i+1]>>2) & 0x3f;
1800 if(dec->object_type == 1 && dec->stream_type == 1)
1802 dec->object_type = MP4_OD;
1803 dec->stream_type = MP4_OD;
1805 else if(dec->stream_type == 4)
1807 if(dec->object_type == 0x6a)
1808 dec->object_type = VIDEO_MPEG1;
1809 if(dec->object_type >= 0x60 && dec->object_type <= 0x65)
1810 dec->object_type = VIDEO_MPEG2;
1811 else if(dec->object_type == 0x20)
1812 dec->object_type = VIDEO_MPEG4;
1813 else if(dec->object_type == 0x21)
1814 dec->object_type = VIDEO_AVC;
1815 /*else if(dec->object_type == 0x22)
1816 fprintf(stderr, "TYPE 0x22\n");*/
1817 else dec->object_type = UNKNOWN;
1819 else if(dec->stream_type == 5)
1821 if(dec->object_type == 0x40)
1822 dec->object_type = AUDIO_AAC;
1823 else if(dec->object_type == 0x6b)
1824 dec->object_type = AUDIO_MP2;
1825 else if(dec->object_type >= 0x66 && dec->object_type <= 0x69)
1826 dec->object_type = AUDIO_MP2;
1827 else
1828 dec->object_type = UNKNOWN;
1830 else
1831 dec->object_type = dec->stream_type = UNKNOWN;
1833 if(dec->object_type != UNKNOWN)
1835 //update the type of the current stream
1836 for(j = 0; j < pmt->es_cnt; j++)
1838 if(pmt->es[j].mp4_es_id == es->id)
1840 pmt->es[j].type = SL_PES_STREAM;
1845 if(len > 13)
1846 parse_mp4_descriptors(pmt, &buf[13], len-13, dec);
1848 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);
1850 return len;
1853 static uint16_t parse_mp4_decoder_specific_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
1855 int i;
1856 mp4_decoder_config_t *dec;
1858 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_SPECIFIC_DESCRIPTOR(%d)\n", len);
1859 dec = (mp4_decoder_config_t *) elem;
1860 if(!dec)
1862 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1863 return len;
1866 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4 SPECIFIC INFO BYTES: \n");
1867 for(i=0; i<len; i++)
1868 mp_msg(MSGT_DEMUX, MSGL_DBG2, "%02x ", buf[i]);
1869 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\n");
1871 if(len > MAX_EXTRADATA_SIZE)
1873 mp_msg(MSGT_DEMUX, MSGL_ERR, "DEMUX_TS, EXTRADATA SUSPICIOUSLY BIG: %d, REFUSED\r\n", len);
1874 return len;
1876 memcpy(dec->buf, buf, len);
1877 dec->buf_size = len;
1879 return len;
1882 static uint16_t parse_mp4_es_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
1884 int i = 0, j = 0, k, found;
1885 uint8_t flag;
1886 mp4_es_descr_t es, *target_es = NULL, *tmp;
1888 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4ES: len=%d\n", len);
1889 memset(&es, 0, sizeof(mp4_es_descr_t));
1890 while(i < len)
1892 es.id = (buf[i] << 8) | buf[i+1];
1893 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_ID: %d\n", es.id);
1894 i += 2;
1895 flag = buf[i];
1896 i++;
1897 if(flag & 0x80)
1898 i += 2;
1899 if(flag & 0x40)
1900 i += buf[i]+1;
1901 if(flag & 0x20) //OCR, maybe we need it
1902 i += 2;
1904 j = parse_mp4_descriptors(pmt, &buf[i], len-i, &es);
1905 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);
1906 if(es.decoder.object_type != UNKNOWN && es.decoder.stream_type != UNKNOWN)
1908 found = 0;
1909 //search this ES_ID if we already have it
1910 for(k=0; k < pmt->mp4es_cnt; k++)
1912 if(pmt->mp4es[k].id == es.id)
1914 target_es = &(pmt->mp4es[k]);
1915 found = 1;
1919 if(! found)
1921 tmp = realloc_struct(pmt->mp4es, pmt->mp4es_cnt+1, sizeof(mp4_es_descr_t));
1922 if(tmp == NULL)
1924 fprintf(stderr, "CAN'T REALLOC MP4_ES_DESCR\n");
1925 continue;
1927 pmt->mp4es = tmp;
1928 target_es = &(pmt->mp4es[pmt->mp4es_cnt]);
1929 pmt->mp4es_cnt++;
1931 memcpy(target_es, &es, sizeof(mp4_es_descr_t));
1932 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_CNT: %d, ID=%d\n", pmt->mp4es_cnt, target_es->id);
1935 i += j;
1938 return len;
1941 static void parse_mp4_object_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
1943 int i, j = 0, id;
1945 i=0;
1946 id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
1947 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_OBJECT_DESCRIPTOR: len=%d, OD_ID=%d\n", len, id);
1948 if(buf[1] & 0x20)
1950 i += buf[2] + 1; //url
1951 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
1953 else
1955 i = 2;
1957 while(i < len)
1959 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
1960 mp_msg(MSGT_DEMUX, MSGL_V, "OBJD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
1961 i += j;
1967 static void parse_mp4_iod(pmt_t *pmt, uint8_t *buf, int len, void *elem)
1969 int i, j = 0;
1970 mp4_od_t *iod = &(pmt->iod);
1972 iod->id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
1973 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_IOD: len=%d, IOD_ID=%d\n", len, iod->id);
1974 i = 2;
1975 if(buf[1] & 0x20)
1977 i += buf[2] + 1; //url
1978 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
1980 else
1982 i = 7;
1983 while(i < len)
1985 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
1986 mp_msg(MSGT_DEMUX, MSGL_V, "IOD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
1987 i += j;
1992 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem)
1994 int tag, descr_len, i = 0, j = 0;
1996 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DESCRIPTORS, len=%d\n", len);
1997 if(! len)
1998 return len;
2000 while(i < len)
2002 tag = buf[i];
2003 j = len - i -1;
2004 descr_len = get_mp4_desc_len(&(buf[i+1]), &j);
2005 mp_msg(MSGT_DEMUX, MSGL_V, "TAG=%d (0x%x), DESCR_len=%d, len=%d, j=%d\n", tag, tag, descr_len, len, j);
2006 if(descr_len > len - j+1)
2008 mp_msg(MSGT_DEMUX, MSGL_V, "descriptor is too long, exit\n");
2009 return len;
2011 i += j+1;
2013 switch(tag)
2015 case 0x1:
2016 parse_mp4_object_descriptor(pmt, &(buf[i]), descr_len, elem);
2017 break;
2018 case 0x2:
2019 parse_mp4_iod(pmt, &(buf[i]), descr_len, elem);
2020 break;
2021 case 0x3:
2022 parse_mp4_es_descriptor(pmt, &(buf[i]), descr_len, elem);
2023 break;
2024 case 0x4:
2025 parse_mp4_decoder_config_descriptor(pmt, &buf[i], descr_len, elem);
2026 break;
2027 case 0x05:
2028 parse_mp4_decoder_specific_descriptor(pmt, &buf[i], descr_len, elem);
2029 break;
2030 case 0x6:
2031 parse_mp4_slconfig_descriptor(pmt, &buf[i], descr_len, elem);
2032 break;
2033 default:
2034 mp_msg(MSGT_DEMUX, MSGL_V, "Unsupported mp4 descriptor 0x%x\n", tag);
2036 i += descr_len;
2039 return len;
2042 static ES_stream_t *new_pid(ts_priv_t *priv, int pid)
2044 ES_stream_t *tss;
2046 tss = malloc(sizeof(ES_stream_t));
2047 if(! tss)
2048 return NULL;
2049 memset(tss, 0, sizeof(ES_stream_t));
2050 tss->pid = pid;
2051 tss->last_cc = -1;
2052 tss->type = UNKNOWN;
2053 tss->subtype = UNKNOWN;
2054 tss->is_synced = 0;
2055 tss->extradata = NULL;
2056 tss->extradata_alloc = tss->extradata_len = 0;
2057 priv->ts.pids[pid] = tss;
2059 return tss;
2063 static int parse_program_descriptors(pmt_t *pmt, uint8_t *buf, uint16_t len)
2065 uint16_t i = 0, k, olen = len;
2067 while(len > 0)
2069 mp_msg(MSGT_DEMUX, MSGL_V, "PROG DESCR, TAG=%x, LEN=%d(%x)\n", buf[i], buf[i+1], buf[i+1]);
2070 if(buf[i+1] > len-2)
2072 mp_msg(MSGT_DEMUX, MSGL_V, "ERROR, descriptor len is too long, skipping\n");
2073 return olen;
2076 if(buf[i] == 0x1d)
2078 if(buf[i+3] == 2) //buggy versions of vlc muxer make this non-standard mess (missing iod_scope)
2079 k = 3;
2080 else
2081 k = 4; //this is standard compliant
2082 parse_mp4_descriptors(pmt, &buf[i+k], (int) buf[i+1]-(k-2), NULL);
2085 len -= 2 + buf[i+1];
2088 return olen;
2091 static int parse_descriptors(struct pmt_es_t *es, uint8_t *ptr)
2093 int j, descr_len, len;
2095 j = 0;
2096 len = es->descr_length;
2097 while(len > 2)
2099 descr_len = ptr[j+1];
2100 mp_msg(MSGT_DEMUX, MSGL_V, "...descr id: 0x%x, len=%d\n", ptr[j], descr_len);
2101 if(descr_len > len)
2103 mp_msg(MSGT_DEMUX, MSGL_ERR, "INVALID DESCR LEN for tag %02x: %d vs %d max, EXIT LOOP\n", ptr[j], descr_len, len);
2104 return -1;
2108 if(ptr[j] == 0x6a || ptr[j] == 0x7a) //A52 Descriptor
2110 if(es->type == 0x6)
2112 es->type = AUDIO_A52;
2113 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB A52 Descriptor\n");
2116 else if(ptr[j] == 0x59) //Subtitling Descriptor
2118 uint8_t subtype;
2120 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Subtitling Descriptor\n");
2121 if(descr_len < 8)
2123 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Descriptor length too short for DVB Subtitle Descriptor: %d, SKIPPING\n", descr_len);
2125 else
2127 memcpy(es->lang, &ptr[j+2], 3);
2128 es->lang[3] = 0;
2129 subtype = ptr[j+5];
2131 (subtype >= 0x10 && subtype <= 0x13) ||
2132 (subtype >= 0x20 && subtype <= 0x23)
2135 es->type = SPU_DVB;
2136 //page parameters: compo page 2 bytes, ancillary page 2 bytes
2138 else
2139 es->type = UNKNOWN;
2142 else if(ptr[j] == 0x50) //Component Descriptor
2144 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Component Descriptor\n");
2145 memcpy(es->lang, &ptr[j+5], 3);
2146 es->lang[3] = 0;
2148 else if(ptr[j] == 0xa) //Language Descriptor
2150 memcpy(es->lang, &ptr[j+2], 3);
2151 es->lang[3] = 0;
2152 mp_msg(MSGT_DEMUX, MSGL_V, "Language Descriptor: %s\n", es->lang);
2154 else if(ptr[j] == 0x5) //Registration Descriptor (looks like e fourCC :) )
2156 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor\n");
2157 if(descr_len < 4)
2159 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor length too short: %d, SKIPPING\n", descr_len);
2161 else
2163 char *d;
2164 memcpy(es->format_descriptor, &ptr[j+2], 4);
2165 es->format_descriptor[4] = 0;
2167 d = &ptr[j+2];
2168 if(d[0] == 'A' && d[1] == 'C' && d[2] == '-' && d[3] == '3')
2170 es->type = AUDIO_A52;
2172 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '2')
2174 es->type = AUDIO_DTS;
2176 else
2177 es->type = UNKNOWN;
2178 mp_msg(MSGT_DEMUX, MSGL_DBG2, "FORMAT %s\n", es->format_descriptor);
2181 else if(ptr[j] == 0x1e)
2183 es->mp4_es_id = (ptr[j+2] << 8) | ptr[j+3];
2184 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);
2186 else
2187 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Unknown descriptor 0x%x, SKIPPING\n", ptr[j]);
2189 len -= 2 + descr_len;
2190 j += 2 + descr_len;
2193 return 1;
2196 static int parse_sl_section(pmt_t *pmt, ts_section_t *section, uint16_t progid, uint16_t pid, int is_start, unsigned char *buff, int size)
2198 int tid, len, skip;
2199 uint8_t *ptr;
2200 skip = collect_section(section, is_start, buff, size);
2201 if(! skip)
2202 return 0;
2204 ptr = &(section->buffer[skip]);
2205 tid = ptr[0];
2206 len = ((ptr[1] & 0x0f) << 8) | ptr[2];
2207 mp_msg(MSGT_DEMUX, MSGL_V, "TABLEID: %d (av. %d), skip=%d, LEN: %d\n", tid, section->buffer_len, skip, len);
2208 if(len > 4093 || section->buffer_len < len || tid != 5)
2210 mp_msg(MSGT_DEMUX, MSGL_V, "SECTION TOO LARGE or wrong section type, EXIT\n");
2211 return 0;
2214 if(! (ptr[5] & 1))
2215 return 0;
2217 //8 is the current position, len - 9 is the amount of data available
2218 parse_mp4_descriptors(pmt, &ptr[8], len - 9, NULL);
2220 return 1;
2223 static int parse_pmt(ts_priv_t * priv, uint16_t progid, uint16_t pid, int is_start, unsigned char *buff, int size)
2225 unsigned char *base, *es_base;
2226 pmt_t *pmt;
2227 int32_t idx, es_count, section_bytes;
2228 uint8_t m=0;
2229 int skip;
2230 pmt_t *tmp;
2231 struct pmt_es_t *tmp_es;
2232 ts_section_t *section;
2233 ES_stream_t *tss;
2235 idx = progid_idx_in_pmt(priv, progid);
2237 if(idx == -1)
2239 int sz = (priv->pmt_cnt + 1) * sizeof(pmt_t);
2240 tmp = realloc_struct(priv->pmt, priv->pmt_cnt + 1, sizeof(pmt_t));
2241 if(tmp == NULL)
2243 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
2244 return 0;
2246 priv->pmt = tmp;
2247 idx = priv->pmt_cnt;
2248 memset(&(priv->pmt[idx]), 0, sizeof(pmt_t));
2249 priv->pmt_cnt++;
2250 priv->pmt[idx].progid = progid;
2253 pmt = &(priv->pmt[idx]);
2255 section = &(pmt->section);
2256 skip = collect_section(section, is_start, buff, size);
2257 if(! skip)
2258 return 0;
2260 base = &(section->buffer[skip]);
2262 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",
2263 progid, pmt->section.buffer_len, is_start, pid, size, m, pmt->es_cnt, idx, pmt);
2265 pmt->table_id = base[0];
2266 if(pmt->table_id != 2)
2267 return -1;
2268 pmt->ssi = base[1] & 0x80;
2269 pmt->section_length = (((base[1] & 0xf) << 8 ) | base[2]);
2270 pmt->version_number = (base[5] >> 1) & 0x1f;
2271 pmt->curr_next = (base[5] & 1);
2272 pmt->section_number = base[6];
2273 pmt->last_section_number = base[7];
2274 pmt->PCR_PID = ((base[8] & 0x1f) << 8 ) | base[9];
2275 pmt->prog_descr_length = ((base[10] & 0xf) << 8 ) | base[11];
2276 if(pmt->prog_descr_length > pmt->section_length - 9)
2278 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, INVALID PROG_DESCR LENGTH (%d vs %d)\n", pmt->prog_descr_length, pmt->section_length - 9);
2279 return -1;
2282 if(pmt->prog_descr_length)
2283 parse_program_descriptors(pmt, &base[12], pmt->prog_descr_length);
2285 es_base = &base[12 + pmt->prog_descr_length]; //the beginning of th ES loop
2287 section_bytes= pmt->section_length - 13 - pmt->prog_descr_length;
2288 es_count = 0;
2290 while(section_bytes >= 5)
2292 int es_pid, es_type;
2294 es_type = es_base[0];
2295 es_pid = ((es_base[1] & 0x1f) << 8) | es_base[2];
2297 idx = es_pid_in_pmt(pmt, es_pid);
2298 if(idx == -1)
2300 int sz = sizeof(struct pmt_es_t) * (pmt->es_cnt + 1);
2301 tmp_es = realloc_struct(pmt->es, pmt->es_cnt + 1, sizeof(struct pmt_es_t));
2302 if(tmp_es == NULL)
2304 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT, COULDN'T ALLOCATE %d bytes for PMT_ES\n", sz);
2305 continue;
2307 pmt->es = tmp_es;
2308 idx = pmt->es_cnt;
2309 memset(&(pmt->es[idx]), 0, sizeof(struct pmt_es_t));
2310 pmt->es_cnt++;
2313 pmt->es[idx].descr_length = ((es_base[3] & 0xf) << 8) | es_base[4];
2316 if(pmt->es[idx].descr_length > section_bytes - 5)
2318 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, ES_DESCR_LENGTH TOO LARGE %d > %d, EXIT\n",
2319 pmt->es[idx].descr_length, section_bytes - 5);
2320 return -1;
2324 pmt->es[idx].pid = es_pid;
2325 if(es_type != 0x6)
2326 pmt->es[idx].type = UNKNOWN;
2327 else
2328 pmt->es[idx].type = es_type;
2330 parse_descriptors(&pmt->es[idx], &es_base[5]);
2332 switch(es_type)
2334 case 1:
2335 pmt->es[idx].type = VIDEO_MPEG1;
2336 break;
2337 case 2:
2338 pmt->es[idx].type = VIDEO_MPEG2;
2339 break;
2340 case 3:
2341 case 4:
2342 pmt->es[idx].type = AUDIO_MP2;
2343 break;
2344 case 6:
2345 if(pmt->es[idx].type == 0x6) //this could have been ovrwritten by parse_descriptors
2346 pmt->es[idx].type = UNKNOWN;
2347 break;
2348 case 0x10:
2349 pmt->es[idx].type = VIDEO_MPEG4;
2350 break;
2351 case 0x0f:
2352 case 0x11:
2353 pmt->es[idx].type = AUDIO_AAC;
2354 break;
2355 case 0x1b:
2356 pmt->es[idx].type = VIDEO_H264;
2357 break;
2358 case 0x12:
2359 pmt->es[idx].type = SL_PES_STREAM;
2360 break;
2361 case 0x13:
2362 pmt->es[idx].type = SL_SECTION;
2363 break;
2364 case 0x81:
2365 pmt->es[idx].type = AUDIO_A52;
2366 break;
2367 case 0x8A:
2368 pmt->es[idx].type = AUDIO_DTS;
2369 break;
2370 default:
2371 mp_msg(MSGT_DEMUX, MSGL_DBG2, "UNKNOWN ES TYPE=0x%x\n", es_type);
2372 pmt->es[idx].type = UNKNOWN;
2375 tss = priv->ts.pids[es_pid]; //an ES stream
2376 if(tss == NULL)
2378 tss = new_pid(priv, es_pid);
2379 if(tss)
2380 tss->type = pmt->es[idx].type;
2383 section_bytes -= 5 + pmt->es[idx].descr_length;
2384 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",
2385 progid, idx, es_count, pmt->es[idx].pid, pmt->es[idx].pid, pmt->es[idx].type, pmt->es[idx].descr_length, section_bytes);
2388 es_base += 5 + pmt->es[idx].descr_length;
2390 es_count++;
2393 mp_msg(MSGT_DEMUX, MSGL_V, "----------------------------\n");
2394 return 1;
2397 static pmt_t* pmt_of_pid(ts_priv_t *priv, int pid, mp4_decoder_config_t **mp4_dec)
2399 int32_t i, j, k;
2401 if(priv->pmt)
2403 for(i = 0; i < priv->pmt_cnt; i++)
2405 if(priv->pmt[i].es && priv->pmt[i].es_cnt)
2407 for(j = 0; j < priv->pmt[i].es_cnt; j++)
2409 if(priv->pmt[i].es[j].pid == pid)
2411 //search mp4_es_id
2412 if(priv->pmt[i].es[j].mp4_es_id)
2414 for(k = 0; k < priv->pmt[i].mp4es_cnt; k++)
2416 if(priv->pmt[i].mp4es[k].id == priv->pmt[i].es[j].mp4_es_id)
2418 *mp4_dec = &(priv->pmt[i].mp4es[k].decoder);
2419 break;
2424 return &(priv->pmt[i]);
2431 return NULL;
2435 static inline int32_t pid_type_from_pmt(ts_priv_t *priv, int pid)
2437 int32_t pmt_idx, pid_idx, i, j;
2439 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2441 if(pmt_idx != -1)
2443 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2444 if(pid_idx != -1)
2445 return priv->pmt[pmt_idx].es[pid_idx].type;
2447 //else
2449 for(i = 0; i < priv->pmt_cnt; i++)
2451 pmt_t *pmt = &(priv->pmt[i]);
2452 for(j = 0; j < pmt->es_cnt; j++)
2453 if(pmt->es[j].pid == pid)
2454 return pmt->es[j].type;
2458 return UNKNOWN;
2462 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid)
2464 int32_t pmt_idx, pid_idx, i, j;
2466 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2468 if(pmt_idx != -1)
2470 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2471 if(pid_idx != -1)
2472 return priv->pmt[pmt_idx].es[pid_idx].lang;
2474 else
2476 for(i = 0; i < priv->pmt_cnt; i++)
2478 pmt_t *pmt = &(priv->pmt[i]);
2479 for(j = 0; j < pmt->es_cnt; j++)
2480 if(pmt->es[j].pid == pid)
2481 return pmt->es[j].lang;
2485 return NULL;
2489 static int fill_packet(demuxer_t *demuxer, demux_stream_t *ds, demux_packet_t **dp, int *dp_offset, TS_stream_info *si)
2491 int ret = 0;
2493 if((*dp != NULL) && (*dp_offset > 0))
2495 ret = *dp_offset;
2496 resize_demux_packet(*dp, ret); //shrinked to the right size
2497 ds_add_packet(ds, *dp);
2498 mp_msg(MSGT_DEMUX, MSGL_DBG2, "ADDED %d bytes to %s fifo, PTS=%.3f\n", ret, (ds == demuxer->audio ? "audio" : (ds == demuxer->video ? "video" : "sub")), (*dp)->pts);
2499 if(si)
2501 float diff = (*dp)->pts - si->last_pts;
2502 float dur;
2504 if(abs(diff) > 1) //1 second, there's a discontinuity
2506 si->duration += si->last_pts - si->first_pts;
2507 si->first_pts = si->last_pts = (*dp)->pts;
2509 else
2511 si->last_pts = (*dp)->pts;
2513 si->size += ret;
2514 dur = si->duration + (si->last_pts - si->first_pts);
2516 if(dur > 0 && ds == demuxer->video)
2518 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2519 if(dur > 1) //otherwise it may be unreliable
2520 priv->vbitrate = (uint32_t) ((float) si->size / dur);
2525 *dp = NULL;
2526 *dp_offset = 0;
2528 return ret;
2531 static int fill_extradata(mp4_decoder_config_t * mp4_dec, ES_stream_t *tss)
2533 uint8_t *tmp;
2535 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4_dec: %p, pid: %d\n", mp4_dec, tss->pid);
2537 if(mp4_dec->buf_size > tss->extradata_alloc)
2539 tmp = (uint8_t *) realloc(tss->extradata, mp4_dec->buf_size);
2540 if(!tmp)
2541 return 0;
2542 tss->extradata = tmp;
2543 tss->extradata_alloc = mp4_dec->buf_size;
2545 memcpy(tss->extradata, mp4_dec->buf, mp4_dec->buf_size);
2546 tss->extradata_len = mp4_dec->buf_size;
2547 mp_msg(MSGT_DEMUX, MSGL_V, "EXTRADATA: %p, alloc=%d, len=%d\n", tss->extradata, tss->extradata_alloc, tss->extradata_len);
2549 return tss->extradata_len;
2552 // 0 = EOF or no stream found
2553 // else = [-] number of bytes written to the packet
2554 static int ts_parse(demuxer_t *demuxer , ES_stream_t *es, unsigned char *packet, int probe)
2556 ES_stream_t *tss;
2557 uint8_t done = 0;
2558 int buf_size, is_start, pid, base;
2559 int len, cc, cc_ok, afc, retv = 0, is_video, is_audio, is_sub;
2560 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2561 stream_t *stream = demuxer->stream;
2562 char *p;
2563 demux_stream_t *ds = NULL;
2564 demux_packet_t **dp = NULL;
2565 int *dp_offset = 0, *buffer_size = 0;
2566 int32_t progid, pid_type, bad, ts_error;
2567 int junk = 0, rap_flag = 0;
2568 pmt_t *pmt;
2569 mp4_decoder_config_t *mp4_dec;
2570 TS_stream_info *si;
2573 while(! done)
2575 bad = ts_error = 0;
2576 ds = (demux_stream_t*) NULL;
2577 dp = (demux_packet_t **) NULL;
2578 dp_offset = buffer_size = NULL;
2579 rap_flag = 0;
2580 mp4_dec = NULL;
2581 es->is_synced = 0;
2582 si = NULL;
2584 junk = priv->ts.packet_size - TS_PACKET_SIZE;
2585 buf_size = priv->ts.packet_size - junk;
2587 if(stream_eof(stream))
2589 if(! probe)
2591 ts_dump_streams(priv);
2592 demuxer->filepos = stream_tell(demuxer->stream);
2595 return 0;
2599 if(! ts_sync(stream))
2601 mp_msg(MSGT_DEMUX, MSGL_INFO, "TS_PARSE: COULDN'T SYNC\n");
2602 return 0;
2605 len = stream_read(stream, &packet[1], 3);
2606 if (len != 3)
2607 return 0;
2609 if((packet[1] >> 7) & 0x01) //transport error
2610 ts_error = 1;
2612 buf_size -= 4;
2614 is_start = packet[1] & 0x40;
2615 pid = ((packet[1] & 0x1f) << 8) | packet[2];
2617 tss = priv->ts.pids[pid]; //an ES stream
2618 if(tss == NULL)
2620 tss = new_pid(priv, pid);
2621 if(tss == NULL)
2622 continue;
2627 if(((pid > 1) && (pid < 16)) || (pid == 8191)) //invalid pid
2629 stream_skip(stream, buf_size-1+junk);
2630 continue;
2633 cc = (packet[3] & 0xf);
2634 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
2635 tss->last_cc = cc;
2637 bad = ts_error; // || (! cc_ok);
2639 afc = (packet[3] >> 4) & 3;
2640 if(afc > 1)
2642 int c;
2643 c = stream_read_char(stream);
2644 buf_size--;
2645 rap_flag = (stream_read_char(stream) & 0x40) >> 6;
2646 buf_size--;
2648 c = min(c-1, buf_size);
2649 stream_skip(stream, c);
2650 buf_size -= c;
2651 if(buf_size == 0)
2652 continue;
2655 if(! (afc % 2)) //no payload in this TS packet
2657 stream_skip(stream, buf_size-1+junk);
2658 continue;
2661 if(bad)
2663 // logically this packet should be dropped, but if I do it
2664 // certain streams play corrupted. Maybe the decoders know
2665 // how to deal with it, but at least I consider the packet
2666 // as "not initial"
2667 mp_msg(MSGT_DEMUX, MSGL_V, "ts_parse: PID=%d, Transport error: %d, CC_OK: %s\n\n", tss->pid, ts_error, (cc_ok ? "yes" : "no"));
2669 if(priv->keep_broken == 0)
2671 stream_skip(stream, buf_size-1+junk);
2672 continue;
2675 is_start = 0; //queued to the packet data
2678 //find the program that the pid belongs to; if (it's the right one or -1) && pid_type==SL_SECTION
2679 //call parse_sl_section()
2680 pmt = pmt_of_pid(priv, pid, &mp4_dec);
2681 if(mp4_dec)
2683 fill_extradata(mp4_dec, tss);
2684 if(IS_VIDEO(mp4_dec->object_type) || IS_AUDIO(mp4_dec->object_type))
2686 tss->type = SL_PES_STREAM;
2687 tss->subtype = mp4_dec->object_type;
2692 //TABLE PARSING
2694 base = priv->ts.packet_size - buf_size;
2695 if(pid == 0)
2697 stream_read(stream,&packet[base], buf_size);
2698 stream_skip(stream, junk);
2699 parse_pat(priv, is_start, &packet[base], buf_size);
2700 continue;
2702 else if((tss->type == SL_SECTION) && pmt)
2704 int k, ok=0, mp4_es_id = -1;
2705 ts_section_t *section;
2706 for(k = 0; k < pmt->mp4es_cnt; k++)
2708 if(pmt->mp4es[k].decoder.object_type == MP4_OD && pmt->mp4es[k].decoder.stream_type == MP4_OD)
2709 mp4_es_id = pmt->mp4es[k].id;
2711 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4ESID: %d\n", mp4_es_id);
2712 for(k = 0; k < pmt->es_cnt; k++)
2714 if(pmt->es[k].mp4_es_id == mp4_es_id)
2715 ok = 1;
2717 stream_read(stream,&packet[base], buf_size);
2718 stream_skip(stream, junk);
2719 if(ok)
2721 section = &(tss->section);
2722 parse_sl_section(pmt, section, progid, pid, is_start, &packet[base], buf_size);
2724 continue;
2726 else
2728 progid = prog_id_in_pat(priv, pid);
2729 if(progid != -1)
2731 if(pid != demuxer->video->id && pid != demuxer->audio->id && pid != demuxer->sub->id)
2733 stream_read(stream,&packet[base], buf_size);
2734 stream_skip(stream, junk);
2735 parse_pmt(priv, progid, pid, is_start, &packet[base], buf_size);
2736 continue;
2738 else
2739 mp_msg(MSGT_DEMUX, MSGL_ERR, "Argh! Data pid %d used in the PMT, Skipping PMT parsing!\n", pid);
2744 priv->last_pid = pid;
2746 is_video = IS_VIDEO(tss->type) || (tss->type==SL_PES_STREAM && IS_VIDEO(tss->subtype));
2747 is_audio = IS_AUDIO(tss->type) || (tss->type==SL_PES_STREAM && IS_AUDIO(tss->subtype)) || (tss->type == PES_PRIVATE1);
2748 is_sub = ((tss->type == SPU_DVD) || (tss->type == SPU_DVB));
2749 pid_type = pid_type_from_pmt(priv, pid);
2751 // PES CONTENT STARTS HERE
2752 if(! probe)
2754 if((IS_AUDIO(tss->type) || IS_AUDIO(tss->subtype)) && is_start && !priv->ts.streams[pid].sh && priv->last_aid+1 < MAX_A_STREAMS)
2756 sh_audio_t *sh = new_sh_audio_aid(demuxer, priv->last_aid+1, pid);
2757 if(sh)
2759 sh->format = IS_AUDIO(tss->type) ? tss->type : tss->subtype;
2760 sh->ds = demuxer->audio;
2762 priv->last_aid++;
2763 priv->ts.streams[pid].id = priv->last_aid;
2764 priv->ts.streams[pid].sh = sh;
2765 priv->ts.streams[pid].type = TYPE_AUDIO;
2766 mp_msg(MSGT_DEMUX, MSGL_V, "\r\nADDED AUDIO PID %d, type: %x stream n. %d\r\n", pid, sh->format, priv->last_aid);
2770 if((pid == demuxer->sub->id)) //or the lang is right
2772 pid_type = SPU_DVD;
2775 if(is_video && (demuxer->video->id == tss->pid))
2777 ds = demuxer->video;
2779 dp = &priv->fifo[1].pack;
2780 dp_offset = &priv->fifo[1].offset;
2781 buffer_size = &priv->fifo[1].buffer_size;
2782 si = &priv->vstr;
2784 else if(is_audio && (demuxer->audio->id == priv->ts.streams[pid].id))
2786 ds = demuxer->audio;
2788 dp = &priv->fifo[0].pack;
2789 dp_offset = &priv->fifo[0].offset;
2790 buffer_size = &priv->fifo[0].buffer_size;
2791 si = &priv->astr;
2793 else if(is_sub
2794 || (pid_type == SPU_DVD) || (pid_type == SPU_DVB))
2796 //SUBS are infrequent, so the initial detection may fail
2797 // and we may need to add them at play-time
2798 if(demuxer->sub->id == -1)
2800 uint16_t p;
2801 p = progid_for_pid(priv, tss->pid, priv->prog);
2803 if(p == priv->prog)
2805 int asgn = 0;
2806 uint8_t *lang;
2808 if(dvdsub_lang)
2810 if(!strcmp(dvdsub_lang, ""))
2811 asgn = 1;
2812 else
2814 lang = pid_lang_from_pmt(priv, pid);
2815 if(lang != NULL)
2816 asgn = (strncmp(lang, dvdsub_lang, 3) == 0);
2817 else
2818 asgn = 0;
2822 if(asgn)
2824 demuxer->sub->id = tss->pid;
2825 mp_msg(MSGT_DEMUX, MSGL_INFO, "CHOSEN SUBs pid 0x%x (%d) FROM PROG %d\n", tss->pid, tss->pid, priv->prog);
2828 else
2830 mp_msg(MSGT_DEMUX, MSGL_V, "DISCARDED SUBs pid 0x%x (%d) NOT CHOSEN OR NOT IN PROG %d\n", tss->pid, tss->pid, priv->prog);
2834 if(demuxer->sub->id == tss->pid)
2836 ds = demuxer->sub;
2838 dp = &priv->fifo[2].pack;
2839 dp_offset = &priv->fifo[2].offset;
2840 buffer_size = &priv->fifo[2].buffer_size;
2842 else
2844 stream_skip(stream, buf_size+junk);
2845 continue;
2848 else
2850 stream_skip(stream, buf_size+junk);
2851 continue;
2854 //IS IT TIME TO QUEUE DATA to the dp_packet?
2855 if(is_start && (dp != NULL))
2857 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
2861 if(*dp == NULL)
2863 if(*buffer_size > MAX_PACK_BYTES)
2864 *buffer_size = MAX_PACK_BYTES;
2865 *dp = new_demux_packet(*buffer_size); //es->size
2866 *dp_offset = 0;
2867 if(! *dp)
2869 fprintf(stderr, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", *buffer_size);
2870 continue;
2872 mp_msg(MSGT_DEMUX, MSGL_DBG2, "CREATED DP(%d)\n", *buffer_size);
2875 mp_msg(MSGT_DEMUX, MSGL_DBG2, "NOW PACKET_SIZE = %d, DP_OFFSET = %d\n", *buffer_size, *dp_offset);
2880 if(is_start)
2882 mp_msg(MSGT_DEMUX, MSGL_DBG2, "IS_START\n");
2884 p = &packet[base];
2885 stream_read(stream, p, buf_size);
2886 stream_skip(stream, junk);
2888 len = pes_parse2(p, buf_size, es, pid_type, pmt, pid);
2889 es->pid = tss->pid;
2890 tss->is_synced |= es->is_synced || rap_flag;
2892 if(es->type==SL_PES_STREAM && !tss->is_synced)
2894 if(probe)
2895 return 0;
2896 else
2897 continue;
2900 if(probe)
2902 if(es->type == UNKNOWN)
2903 return 0;
2905 tss->payload_size = es->payload_size;
2906 if(len == 0)
2908 if(tss->type != UNKNOWN)
2910 es->size = buf_size;
2911 es->start = p;
2912 return 1;
2915 else
2917 uint8_t *lang = NULL;
2918 tss->type = es->type;
2919 tss->subtype = es->subtype;
2921 if(is_audio)
2922 lang = pid_lang_from_pmt(priv, es->pid);
2923 if(lang != NULL)
2925 memcpy(es->lang, lang, 3);
2926 es->lang[3] = 0;
2928 else
2929 es->lang[0] = 0;
2930 return 1;
2933 else
2935 if(len == 0)
2937 if(tss->type != UNKNOWN)
2939 len = es->size = buf_size; //push the whole packet to the fifo
2940 //(we already learned what it is during the probe phase)
2941 es->start = p;
2943 else
2944 continue;
2947 if(es->pts == 0.0f)
2948 es->pts = tss->pts = tss->last_pts;
2949 else
2950 tss->pts = tss->last_pts = es->pts;
2952 mp_msg(MSGT_DEMUX, MSGL_DBG2, "ts_parse, NEW pid=%d, PSIZE: %u, type=%X, start=%p, len=%d\n",
2953 es->pid, es->payload_size, es->type, es->start, es->size);
2955 tss->payload_size = es->payload_size;
2957 demuxer->filepos = stream_tell(demuxer->stream) - es->size;
2959 if(*dp_offset + es->size > *buffer_size)
2961 *buffer_size = *dp_offset + es->size + TS_FEC_PACKET_SIZE;
2962 resize_demux_packet(*dp, *buffer_size);
2963 //we'll skip at least one RESIZE() in the next iteration of ts_parse()
2964 mp_msg(MSGT_DEMUX, MSGL_DBG2, "RESIZE DP TO %d\n", *buffer_size);
2966 memcpy(&((*dp)->buffer[*dp_offset]), es->start, es->size);
2967 *dp_offset += es->size;
2968 (*dp)->flags = 0;
2969 (*dp)->pos = stream_tell(demuxer->stream);
2970 (*dp)->pts = es->pts;
2972 if(*dp_offset >= MAX_PACK_BYTES)
2974 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
2975 return 1;
2978 if(retv > 0)
2979 return retv;
2980 else
2981 continue;
2984 else
2986 uint16_t sz;
2988 if((tss->type == UNKNOWN) || (tss->type==SL_PES_STREAM && !tss->is_synced))
2990 stream_skip(stream, buf_size+junk);
2991 if(probe)
2992 return (is_video || is_audio || is_sub);
2993 else
2994 continue;
2998 es->pid = tss->pid;
2999 es->type = tss->type;
3000 es->subtype = tss->subtype;
3001 es->pts = tss->pts = tss->last_pts;
3002 es->start = &packet[base];
3005 if(tss->payload_size > 0)
3007 sz = min(tss->payload_size, buf_size);
3008 tss->payload_size -= sz;
3009 es->size = sz;
3011 else
3013 if(is_video)
3015 sz = es->size = buf_size;
3017 else
3019 stream_skip(stream, buf_size+junk);
3020 continue;
3025 if(! probe)
3027 if(*dp_offset + sz > *buffer_size)
3029 *buffer_size = *dp_offset + sz + TS_FEC_PACKET_SIZE;
3030 resize_demux_packet(*dp, *buffer_size);
3031 //we'll skip at least one RESIZE() in the next iteration of ts_parse()
3032 mp_msg(MSGT_DEMUX, MSGL_DBG2, "RESIZE DP TO %d\n", *buffer_size);
3035 stream_read(stream, &((*dp)->buffer[*dp_offset]), sz);
3036 *dp_offset += sz;
3038 if(buf_size - sz > 0)
3040 stream_skip(stream, buf_size - sz);
3042 stream_skip(stream, junk);
3044 if(*dp_offset >= MAX_PACK_BYTES)
3046 (*dp)->pts = tss->last_pts;
3047 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
3048 return 1;
3051 continue;
3053 else
3055 stream_read(stream, es->start, sz);
3056 if(buf_size - sz) stream_skip(stream, buf_size-sz);
3057 stream_skip(stream, junk);
3059 if(es->size)
3060 return es->size;
3061 else
3062 continue;
3067 return 0;
3071 extern void skip_audio_frame(sh_audio_t *sh_audio);
3073 static void reset_fifos(ts_priv_t* priv, int a, int v, int s)
3075 if(a)
3077 if(priv->fifo[0].pack != NULL)
3079 free_demux_packet(priv->fifo[0].pack);
3080 priv->fifo[0].pack = NULL;
3082 priv->fifo[0].offset = 0;
3085 if(v)
3087 if(priv->fifo[1].pack != NULL)
3089 free_demux_packet(priv->fifo[1].pack);
3090 priv->fifo[1].pack = NULL;
3092 priv->fifo[1].offset = 0;
3095 if(s)
3097 if(priv->fifo[2].pack != NULL)
3099 free_demux_packet(priv->fifo[2].pack);
3100 priv->fifo[2].pack = NULL;
3102 priv->fifo[2].offset = 0;
3106 extern int videobuf_code_len;
3107 extern int sync_video_packet(demux_stream_t *);
3108 extern int skip_video_packet(demux_stream_t *);
3110 static void demux_seek_ts(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
3112 demux_stream_t *d_audio=demuxer->audio;
3113 demux_stream_t *d_video=demuxer->video;
3114 demux_stream_t *d_sub=demuxer->sub;
3115 sh_audio_t *sh_audio=d_audio->sh;
3116 sh_video_t *sh_video=d_video->sh;
3117 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
3118 int i, video_stats;
3119 off_t newpos;
3121 //================= seek in MPEG-TS ==========================
3123 ts_dump_streams(demuxer->priv);
3124 reset_fifos(priv, sh_audio != NULL, sh_video != NULL, demuxer->sub->id > 0);
3127 if(sh_audio != NULL)
3128 ds_free_packs(d_audio);
3129 if(sh_video != NULL)
3130 ds_free_packs(d_video);
3131 if(demuxer->sub->id > 0)
3132 ds_free_packs(d_sub);
3135 video_stats = (sh_video != NULL);
3136 if(video_stats)
3138 mp_msg(MSGT_DEMUX, MSGL_V, "IBPS: %d, vb: %d\r\n", sh_video->i_bps, priv->vbitrate);
3139 if(priv->vbitrate)
3140 video_stats = priv->vbitrate;
3141 else
3142 video_stats = sh_video->i_bps;
3145 newpos = (flags & 1) ? demuxer->movi_start : demuxer->filepos;
3146 if(flags & 2) // float seek 0..1
3147 newpos+=(demuxer->movi_end-demuxer->movi_start)*rel_seek_secs;
3148 else
3150 // time seek (secs)
3152 if(! video_stats) // unspecified or VBR
3153 newpos += 2324*75*rel_seek_secs; // 174.3 kbyte/sec
3154 else
3155 newpos += video_stats*rel_seek_secs;
3159 if(newpos < demuxer->movi_start)
3160 newpos = demuxer->movi_start; //begininng of stream
3162 #ifdef _LARGEFILE_SOURCE
3163 newpos &= ~((long long) (STREAM_BUFFER_SIZE - 1)); /* sector boundary */
3164 #else
3165 newpos &= ~(STREAM_BUFFER_SIZE - 1); /* sector boundary */
3166 #endif
3168 stream_seek(demuxer->stream, newpos);
3169 for(i = 0; i < 8192; i++)
3170 if(priv->ts.pids[i] != NULL)
3171 priv->ts.pids[i]->is_synced = 0;
3173 videobuf_code_len = 0;
3175 if(sh_video != NULL)
3176 ds_fill_buffer(d_video);
3178 if(sh_audio != NULL)
3180 ds_fill_buffer(d_audio);
3183 while(sh_video != NULL)
3185 if(sh_audio && !d_audio->eof && d_video->pts && d_audio->pts)
3187 float a_pts=d_audio->pts;
3188 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
3189 if(d_video->pts > a_pts)
3191 skip_audio_frame(sh_audio); // sync audio
3192 continue;
3197 i = sync_video_packet(d_video);
3198 if((sh_video->format == VIDEO_MPEG1) || (sh_video->format == VIDEO_MPEG2))
3200 if(i==0x1B3 || i==0x1B8) break; // found it!
3202 else if(sh_video->format == VIDEO_MPEG4)
3204 if(i==0x1B6) break; // found it!
3206 else //H264
3208 if((i & ~0x60) == 0x101 || (i & ~0x60) == 0x102 || (i & ~0x60) == 0x105) break;
3211 if(!i || !skip_video_packet(d_video)) break; // EOF?
3216 static int demux_ts_fill_buffer(demuxer_t * demuxer, demux_stream_t *ds)
3218 ES_stream_t es;
3219 ts_priv_t *priv = (ts_priv_t *)demuxer->priv;
3221 return -ts_parse(demuxer, &es, priv->packet, 0);
3225 static int ts_check_file_dmx(demuxer_t *demuxer)
3227 return ts_check_file(demuxer) ? DEMUXER_TYPE_MPEG_TS : 0;
3230 static int demux_ts_control(demuxer_t *demuxer, int cmd, void *arg)
3232 ts_priv_t* priv = (ts_priv_t *)demuxer->priv;
3234 switch(cmd)
3236 case DEMUXER_CTRL_SWITCH_AUDIO:
3238 sh_audio_t *sh_audio = demuxer->audio->sh;
3239 sh_audio_t *sh_a = NULL;
3240 int i, n;
3241 if(!sh_audio)
3242 return DEMUXER_CTRL_NOTIMPL;
3244 n = *((int*)arg);
3245 if(n < 0)
3247 for(i = 0; i < 8192; i++)
3249 if(priv->ts.streams[i].id == demuxer->audio->id && priv->ts.streams[i].type == TYPE_AUDIO)
3250 break;
3253 while(!sh_a)
3255 i = (i+1) % 8192;
3256 if(priv->ts.streams[i].id == demuxer->audio->id) //we made a complete loop
3257 break;
3258 if(priv->ts.streams[i].type == TYPE_AUDIO)
3259 sh_a = (sh_audio_t*)priv->ts.streams[i].sh;
3262 else if(n <= priv->last_aid)
3264 for(i = 0; i < 8192; i++)
3266 if(priv->ts.streams[i].id == n && priv->ts.streams[i].type == TYPE_AUDIO)
3268 sh_a = (sh_audio_t*)priv->ts.streams[i].sh;
3269 break;
3274 if(sh_a)
3276 demuxer->audio->id = priv->ts.streams[i].id;
3277 demuxer->audio->sh = sh_a;
3278 ds_free_packs(demuxer->audio);
3279 mp_msg(MSGT_DEMUX, MSGL_V, "\r\ndemux_ts, switched to audio pid %d, id: %d, sh: %p\r\n", i, demuxer->audio->id, sh_a);
3282 *((int*)arg) = demuxer->audio->id;
3283 return DEMUXER_CTRL_OK;
3287 default:
3288 return DEMUXER_CTRL_NOTIMPL;
3293 demuxer_desc_t demuxer_desc_mpeg_ts = {
3294 "MPEG-TS demuxer",
3295 "mpegts",
3296 "TS",
3297 "Nico Sabbi",
3299 DEMUXER_TYPE_MPEG_TS,
3300 0, // unsafe autodetect
3301 ts_check_file_dmx,
3302 demux_ts_fill_buffer,
3303 demux_open_ts,
3304 demux_close_ts,
3305 demux_seek_ts,
3306 demux_ts_control