remove trailing whitespaces
[mplayer/greg.git] / libmpdemux / demux_ts.c
blob33ffcdb689ce5e427e638015b12e621d85f90aca
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/stream.h"
33 #include "demuxer.h"
34 #include "parse_es.h"
35 #include "stheader.h"
37 #include "ms_hdr.h"
38 #include "mpeg_hdr.h"
40 #define TS_PH_PACKET_SIZE 192
41 #define TS_FEC_PACKET_SIZE 204
42 #define TS_PACKET_SIZE 188
43 #define NB_PID_MAX 8192
45 #define MAX_HEADER_SIZE 6 /* enough for PES header + length */
46 #define MAX_CHECK_SIZE 65535
47 #define TS_MAX_PROBE_SIZE 2000000 /* do not forget to change this in cfg-common-opts.h, too */
48 #define NUM_CONSECUTIVE_TS_PACKETS 32
49 #define NUM_CONSECUTIVE_AUDIO_PACKETS 348
50 #define MAX_A52_FRAME_SIZE 3840
52 #ifndef SIZE_MAX
53 #define SIZE_MAX ((size_t)-1)
54 #endif
56 #define TYPE_AUDIO 1
57 #define TYPE_VIDEO 2
59 int ts_prog;
60 int ts_keep_broken=0;
61 off_t ts_probe = 0;
62 int audio_substream_id = -1;
63 extern char *dvdsub_lang, *audio_lang; //for -alang
65 typedef enum
67 UNKNOWN = -1,
68 VIDEO_MPEG1 = 0x10000001,
69 VIDEO_MPEG2 = 0x10000002,
70 VIDEO_MPEG4 = 0x10000004,
71 VIDEO_H264 = 0x10000005,
72 VIDEO_AVC = mmioFOURCC('a', 'v', 'c', '1'),
73 VIDEO_VC1 = mmioFOURCC('W', '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 int last_vid;
235 char packet[TS_FEC_PACKET_SIZE];
236 TS_stream_info vstr, astr;
237 } ts_priv_t;
240 typedef struct {
241 es_stream_type_t type;
242 ts_section_t section;
243 } TS_pids_t;
246 #define IS_AUDIO(x) (((x) == AUDIO_MP2) || ((x) == AUDIO_A52) || ((x) == AUDIO_LPCM_BE) || ((x) == AUDIO_AAC) || ((x) == AUDIO_DTS))
247 #define IS_VIDEO(x) (((x) == VIDEO_MPEG1) || ((x) == VIDEO_MPEG2) || ((x) == VIDEO_MPEG4) || ((x) == VIDEO_H264) || ((x) == VIDEO_AVC) || ((x) == VIDEO_VC1))
249 static int ts_parse(demuxer_t *demuxer, ES_stream_t *es, unsigned char *packet, int probe);
251 static uint8_t get_packet_size(const unsigned char *buf, int size)
253 int i;
255 if (size < (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS))
256 return 0;
258 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
260 if (buf[i * TS_PACKET_SIZE] != 0x47)
262 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
263 goto try_fec;
266 return TS_PACKET_SIZE;
268 try_fec:
269 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
271 if (buf[i * TS_FEC_PACKET_SIZE] != 0x47){
272 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
273 goto try_philips;
276 return TS_FEC_PACKET_SIZE;
278 try_philips:
279 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
281 if (buf[i * TS_PH_PACKET_SIZE] != 0x47)
282 return 0;
284 return TS_PH_PACKET_SIZE;
287 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h);
289 static void ts_add_stream(demuxer_t * demuxer, ES_stream_t *es)
291 int i;
292 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
294 if(priv->ts.streams[es->pid].sh)
295 return;
297 if((IS_AUDIO(es->type) || IS_AUDIO(es->subtype)) && priv->last_aid+1 < MAX_A_STREAMS)
299 sh_audio_t *sh = new_sh_audio_aid(demuxer, priv->last_aid, es->pid);
300 if(sh)
302 sh->format = IS_AUDIO(es->type) ? es->type : es->subtype;
303 sh->ds = demuxer->audio;
305 priv->ts.streams[es->pid].id = priv->last_aid;
306 priv->ts.streams[es->pid].sh = sh;
307 priv->ts.streams[es->pid].type = TYPE_AUDIO;
308 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);
309 priv->last_aid++;
312 if(es->extradata && es->extradata_len)
314 sh->wf = (WAVEFORMATEX *) malloc(sizeof (WAVEFORMATEX) + es->extradata_len);
315 sh->wf->cbSize = es->extradata_len;
316 memcpy(sh->wf + 1, es->extradata, es->extradata_len);
320 if((IS_VIDEO(es->type) || IS_VIDEO(es->subtype)) && priv->last_vid+1 < MAX_V_STREAMS)
322 sh_video_t *sh = new_sh_video_vid(demuxer, priv->last_vid, es->pid);
323 if(sh)
325 sh->format = IS_VIDEO(es->type) ? es->type : es->subtype;
326 sh->ds = demuxer->video;
328 priv->ts.streams[es->pid].id = priv->last_vid;
329 priv->ts.streams[es->pid].sh = sh;
330 priv->ts.streams[es->pid].type = TYPE_VIDEO;
331 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);
332 priv->last_vid++;
335 if(sh->format == VIDEO_AVC && es->extradata && es->extradata_len)
337 int w = 0, h = 0;
338 sh->bih = (BITMAPINFOHEADER *) calloc(1, sizeof(BITMAPINFOHEADER) + es->extradata_len);
339 sh->bih->biSize= sizeof(BITMAPINFOHEADER) + es->extradata_len;
340 sh->bih->biCompression = sh->format;
341 memcpy(sh->bih + 1, es->extradata, es->extradata_len);
342 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "EXTRADATA(%d BYTES): \n", es->extradata_len);
343 for(i = 0;i < es->extradata_len; i++)
344 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "%02x ", (int) es->extradata[i]);
345 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"\n");
346 if(parse_avc_sps(es->extradata, es->extradata_len, &w, &h))
348 sh->bih->biWidth = w;
349 sh->bih->biHeight = h;
356 static int ts_check_file(demuxer_t * demuxer)
358 const int buf_size = (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS);
359 unsigned char buf[TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS], done = 0, *ptr;
360 uint32_t _read, i, count = 0, is_ts;
361 int cc[NB_PID_MAX], last_cc[NB_PID_MAX], pid, cc_ok, c, good, bad;
362 uint8_t size = 0;
363 off_t pos = 0;
364 off_t init_pos;
366 mp_msg(MSGT_DEMUX, MSGL_V, "Checking for MPEG-TS...\n");
368 init_pos = stream_tell(demuxer->stream);
369 is_ts = 0;
370 while(! done)
372 i = 1;
373 c = 0;
375 while(((c=stream_read_char(demuxer->stream)) != 0x47)
376 && (c >= 0)
377 && (i < MAX_CHECK_SIZE)
378 && ! demuxer->stream->eof
379 ) i++;
382 if(c != 0x47)
384 mp_msg(MSGT_DEMUX, MSGL_V, "THIS DOESN'T LOOK LIKE AN MPEG-TS FILE!\n");
385 is_ts = 0;
386 done = 1;
387 continue;
390 pos = stream_tell(demuxer->stream) - 1;
391 buf[0] = c;
392 _read = stream_read(demuxer->stream, &buf[1], buf_size-1);
394 if(_read < buf_size-1)
396 mp_msg(MSGT_DEMUX, MSGL_V, "COULDN'T READ ENOUGH DATA, EXITING TS_CHECK\n");
397 stream_reset(demuxer->stream);
398 return 0;
401 size = get_packet_size(buf, buf_size);
402 if(size)
404 done = 1;
405 is_ts = 1;
408 if(pos - init_pos >= MAX_CHECK_SIZE)
410 done = 1;
411 is_ts = 0;
415 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);
416 stream_seek(demuxer->stream, pos);
418 if(! is_ts)
419 return 0;
421 //LET'S CHECK continuity counters
422 good = bad = 0;
423 for(count = 0; count < NB_PID_MAX; count++)
425 cc[count] = last_cc[count] = -1;
428 for(count = 0; count < NUM_CONSECUTIVE_TS_PACKETS; count++)
430 ptr = &(buf[size * count]);
431 pid = ((ptr[1] & 0x1f) << 8) | ptr[2];
432 mp_msg(MSGT_DEMUX, MSGL_DBG2, "BUF: %02x %02x %02x %02x, PID %d, SIZE: %d \n",
433 ptr[0], ptr[1], ptr[2], ptr[3], pid, size);
435 if((pid == 8191) || (pid < 16))
436 continue;
438 cc[pid] = (ptr[3] & 0xf);
439 cc_ok = (last_cc[pid] < 0) || ((((last_cc[pid] + 1) & 0x0f) == cc[pid]));
440 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PID %d, COMPARE CC %d AND LAST_CC %d\n", pid, cc[pid], last_cc[pid]);
441 if(! cc_ok)
442 //return 0;
443 bad++;
444 else
445 good++;
447 last_cc[pid] = cc[pid];
450 mp_msg(MSGT_DEMUX, MSGL_V, "GOOD CC: %d, BAD CC: %d\n", good, bad);
452 if(good >= bad)
453 return size;
454 else
455 return 0;
459 static inline int32_t progid_idx_in_pmt(ts_priv_t *priv, uint16_t progid)
461 int x;
463 if(priv->pmt == NULL)
464 return -1;
466 for(x = 0; x < priv->pmt_cnt; x++)
468 if(priv->pmt[x].progid == progid)
469 return x;
472 return -1;
476 static inline int32_t progid_for_pid(ts_priv_t *priv, int pid, int32_t req) //finds the first program listing a pid
478 int i, j;
479 pmt_t *pmt;
482 if(priv->pmt == NULL)
483 return -1;
486 for(i=0; i < priv->pmt_cnt; i++)
488 pmt = &(priv->pmt[i]);
490 if(pmt->es == NULL)
491 return -1;
493 for(j = 0; j < pmt->es_cnt; j++)
495 if(pmt->es[j].pid == pid)
497 if((req == 0) || (req == pmt->progid))
498 return pmt->progid;
503 return -1;
506 static inline int32_t prog_pcr_pid(ts_priv_t *priv, int progid)
508 int i;
510 if(priv->pmt == NULL)
511 return -1;
512 for(i=0; i < priv->pmt_cnt; i++)
514 if(priv->pmt[i].progid == progid)
515 return priv->pmt[i].PCR_PID;
517 return -1;
521 static inline int pid_match_lang(ts_priv_t *priv, uint16_t pid, char *lang)
523 uint16_t i, j;
524 pmt_t *pmt;
526 if(priv->pmt == NULL)
527 return -1;
529 for(i=0; i < priv->pmt_cnt; i++)
531 pmt = &(priv->pmt[i]);
533 if(pmt->es == NULL)
534 return -1;
536 for(j = 0; j < pmt->es_cnt; j++)
538 if(pmt->es[j].pid != pid)
539 continue;
541 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);
542 if(strncmp(pmt->es[j].lang, lang, 3) == 0)
544 return 1;
550 return -1;
553 typedef struct {
554 int32_t atype, vtype, stype; //types
555 int32_t apid, vpid, spid; //stream ids
556 char slang[4], alang[4]; //languages
557 uint16_t prog;
558 off_t probe;
559 } tsdemux_init_t;
561 //stripped down version of a52_syncinfo() from liba52
562 //copyright belongs to Michel Lespinasse <walken@zoy.org> and Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
563 int mp_a52_framesize(uint8_t * buf, int *srate)
565 int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
566 128, 160, 192, 224, 256, 320, 384, 448,
567 512, 576, 640
569 uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
570 int frmsizecod, bitrate, half;
572 if((buf[0] != 0x0b) || (buf[1] != 0x77)) /* syncword */
573 return 0;
575 if(buf[5] >= 0x60) /* bsid >= 12 */
576 return 0;
578 half = halfrate[buf[5] >> 3];
580 frmsizecod = buf[4] & 63;
581 if(frmsizecod >= 38)
582 return 0;
584 bitrate = rate[frmsizecod >> 1];
586 switch(buf[4] & 0xc0)
588 case 0: /* 48 KHz */
589 *srate = 48000 >> half;
590 return 4 * bitrate;
591 case 0x40: /* 44.1 KHz */
592 *srate = 44100 >> half;
593 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
594 case 0x80: /* 32 KHz */
595 *srate = 32000 >> half;
596 return 6 * bitrate;
599 return 0;
602 //second stage: returns the count of A52 syncwords found
603 static int a52_check(char *buf, int len)
605 int cnt, frame_length, ok, srate;
607 cnt = ok = 0;
608 if(len < 8)
609 return 0;
611 while(cnt < len - 7)
613 if(buf[cnt] == 0x0B && buf[cnt+1] == 0x77)
615 frame_length = mp_a52_framesize(&buf[cnt], &srate);
616 if(frame_length>=7 && frame_length<=3840)
618 cnt += frame_length;
619 ok++;
621 else
622 cnt++;
624 else
625 cnt++;
628 mp_msg(MSGT_DEMUXER, MSGL_V, "A52_CHECK(%d input bytes), found %d frame syncwords of %d bytes length\n", len, ok, frame_length);
629 return ok;
633 static off_t ts_detect_streams(demuxer_t *demuxer, tsdemux_init_t *param)
635 int video_found = 0, audio_found = 0, sub_found = 0, i, num_packets = 0, req_apid, req_vpid, req_spid;
636 int is_audio, is_video, is_sub, has_tables;
637 int32_t p, chosen_pid = 0;
638 off_t pos=0, ret = 0, init_pos, end_pos;
639 ES_stream_t es;
640 unsigned char tmp[TS_FEC_PACKET_SIZE];
641 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
642 struct {
643 char *buf;
644 int pos;
645 } pes_priv1[8192], *pptr;
646 char *tmpbuf;
648 priv->last_pid = 8192; //invalid pid
650 req_apid = param->apid;
651 req_vpid = param->vpid;
652 req_spid = param->spid;
654 has_tables = 0;
655 memset(pes_priv1, 0, sizeof(pes_priv1));
656 init_pos = stream_tell(demuxer->stream);
657 mp_msg(MSGT_DEMUXER, MSGL_V, "PROBING UP TO %"PRIu64", PROG: %d\n", (uint64_t) param->probe, param->prog);
658 end_pos = init_pos + (param->probe ? param->probe : TS_MAX_PROBE_SIZE);
659 while(1)
661 pos = stream_tell(demuxer->stream);
662 if(pos > end_pos || demuxer->stream->eof)
663 break;
665 if(ts_parse(demuxer, &es, tmp, 1))
667 //Non PES-aligned A52 audio may escape detection if PMT is not present;
668 //in this case we try to find at least 3 A52 syncwords
669 if((es.type == PES_PRIVATE1) && (! audio_found) && req_apid > -2)
671 pptr = &pes_priv1[es.pid];
672 if(pptr->pos < 64*1024)
674 tmpbuf = (char*) realloc(pptr->buf, pptr->pos + es.size);
675 if(tmpbuf != NULL)
677 pptr->buf = tmpbuf;
678 memcpy(&(pptr->buf[ pptr->pos ]), es.start, es.size);
679 pptr->pos += es.size;
680 if(a52_check(pptr->buf, pptr->pos) > 2)
682 param->atype = AUDIO_A52;
683 param->apid = es.pid;
684 es.type = AUDIO_A52;
690 is_audio = IS_AUDIO(es.type) || ((es.type==SL_PES_STREAM) && IS_AUDIO(es.subtype));
691 is_video = IS_VIDEO(es.type) || ((es.type==SL_PES_STREAM) && IS_VIDEO(es.subtype));
692 is_sub = ((es.type == SPU_DVD) || (es.type == SPU_DVB));
695 if((! is_audio) && (! is_video) && (! is_sub))
696 continue;
697 if(is_audio && req_apid==-2)
698 continue;
700 if(is_video)
702 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_VIDEO_ID=%d\n", es.pid);
703 chosen_pid = (req_vpid == es.pid);
704 if((! chosen_pid) && (req_vpid > 0))
705 continue;
707 else if(is_audio)
709 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_AUDIO_ID=%d\n", es.pid);
710 if (es.lang[0] > 0)
711 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_AID_%d_LANG=%s\n", es.pid, es.lang);
712 if(req_apid > 0)
714 chosen_pid = (req_apid == es.pid);
715 if(! chosen_pid)
716 continue;
718 else if(param->alang[0] > 0 && es.lang[0] > 0)
720 if(pid_match_lang(priv, es.pid, param->alang) == -1)
721 continue;
723 chosen_pid = 1;
724 param->apid = req_apid = es.pid;
727 else if(is_sub)
729 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_SUBTITLE_ID=%d\n", es.pid);
730 if (es.lang[0] > 0)
731 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_SID_%d_LANG=%s\n", es.pid, es.lang);
732 chosen_pid = (req_spid == es.pid);
733 if((! chosen_pid) && (req_spid > 0))
734 continue;
737 if(req_apid < 0 && (param->alang[0] == 0) && req_vpid < 0 && req_spid < 0)
738 chosen_pid = 1;
740 if((ret == 0) && chosen_pid)
742 ret = stream_tell(demuxer->stream);
745 p = progid_for_pid(priv, es.pid, param->prog);
746 if(p != -1)
748 has_tables++;
749 if(!param->prog && chosen_pid)
750 param->prog = p;
753 if((param->prog > 0) && (param->prog != p))
755 if(audio_found)
757 if(is_video && (req_vpid == es.pid))
759 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
760 param->vpid = es.pid;
761 video_found = 1;
762 break;
766 if(video_found)
768 if(is_audio && (req_apid == es.pid))
770 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
771 param->apid = es.pid;
772 audio_found = 1;
773 break;
778 continue;
782 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "TYPE: %x, PID: %d, PROG FOUND: %d\n", es.type, es.pid, param->prog);
785 if(is_video)
787 if((req_vpid == -1) || (req_vpid == es.pid))
789 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
790 param->vpid = es.pid;
791 video_found = 1;
796 if(((req_vpid == -2) || (num_packets >= NUM_CONSECUTIVE_AUDIO_PACKETS)) && audio_found && !param->probe)
798 //novideo or we have at least 348 audio packets (64 KB) without video (TS with audio only)
799 param->vtype = 0;
800 break;
803 if(is_sub)
805 if((req_spid == -1) || (req_spid == es.pid))
807 param->stype = es.type;
808 param->spid = es.pid;
809 sub_found = 1;
813 if(is_audio)
815 if((req_apid == -1) || (req_apid == es.pid))
817 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
818 param->apid = es.pid;
819 audio_found = 1;
823 if(audio_found && (param->apid == es.pid) && (! video_found))
824 num_packets++;
826 if((has_tables==0) && (video_found && audio_found) && (pos >= 1000000))
827 break;
831 for(i=0; i<8192; i++)
833 if(pes_priv1[i].buf != NULL)
835 free(pes_priv1[i].buf);
836 pes_priv1[i].buf = NULL;
837 pes_priv1[i].pos = 0;
841 if(video_found)
843 if(param->vtype == VIDEO_MPEG1)
844 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG1(pid=%d) ", param->vpid);
845 else if(param->vtype == VIDEO_MPEG2)
846 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG2(pid=%d) ", param->vpid);
847 else if(param->vtype == VIDEO_MPEG4)
848 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG4(pid=%d) ", param->vpid);
849 else if(param->vtype == VIDEO_H264)
850 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO H264(pid=%d) ", param->vpid);
851 else if(param->vtype == VIDEO_VC1)
852 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO VC1(pid=%d) ", param->vpid);
853 else if(param->vtype == VIDEO_AVC)
854 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO AVC(NAL-H264, pid=%d) ", param->vpid);
856 else
858 param->vtype = UNKNOWN;
859 //WE DIDN'T MATCH ANY VIDEO STREAM
860 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO VIDEO! ");
863 if(param->atype == AUDIO_MP2)
864 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO MPA(pid=%d)", param->apid);
865 else if(param->atype == AUDIO_A52)
866 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO A52(pid=%d)", param->apid);
867 else if(param->atype == AUDIO_DTS)
868 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO DTS(pid=%d)", param->apid);
869 else if(param->atype == AUDIO_LPCM_BE)
870 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO LPCM(pid=%d)", param->apid);
871 else if(param->atype == AUDIO_AAC)
872 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO AAC(pid=%d)", param->apid);
873 else
875 audio_found = 0;
876 param->atype = UNKNOWN;
877 //WE DIDN'T MATCH ANY AUDIO STREAM, SO WE FORCE THE DEMUXER TO IGNORE AUDIO
878 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO AUDIO! ");
881 if(param->stype == SPU_DVD || param->stype == SPU_DVB)
882 mp_msg(MSGT_DEMUXER, MSGL_INFO, " SUB %s(pid=%d) ", (param->stype==SPU_DVD ? "DVD" : "DVB"), param->spid);
883 else
885 param->stype = UNKNOWN;
886 mp_msg(MSGT_DEMUXER, MSGL_INFO, " NO SUBS (yet)! ");
889 if(video_found || audio_found)
891 if(!param->prog)
893 p = progid_for_pid(priv, video_found ? param->vpid : param->apid, 0);
894 if(p != -1)
895 param->prog = p;
898 if(demuxer->stream->eof && (ret == 0))
899 ret = init_pos;
900 mp_msg(MSGT_DEMUXER, MSGL_INFO, " PROGRAM N. %d\n", param->prog);
902 else
903 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\n");
906 for(i=0; i<8192; i++)
908 if(priv->ts.pids[i] != NULL)
910 priv->ts.pids[i]->payload_size = 0;
911 priv->ts.pids[i]->pts = priv->ts.pids[i]->last_pts = 0;
912 priv->ts.pids[i]->last_cc = -1;
913 priv->ts.pids[i]->is_synced = 0;
917 return ret;
920 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h)
922 int sps, sps_len;
923 unsigned char *ptr;
924 mp_mpeg_header_t picture;
925 if(len < 6)
926 return 0;
927 sps = buf[5] & 0x1f;
928 if(!sps)
929 return 0;
930 sps_len = (buf[6] << 8) | buf[7];
931 if(!sps_len || (sps_len > len - 8))
932 return 0;
933 ptr = &(buf[8]);
934 picture.display_picture_width = picture.display_picture_height = 0;
935 h264_parse_sps(&picture, ptr, len - 8);
936 if(!picture.display_picture_width || !picture.display_picture_height)
937 return 0;
938 *w = picture.display_picture_width;
939 *h = picture.display_picture_height;
940 return 1;
943 static demuxer_t *demux_open_ts(demuxer_t * demuxer)
945 int i;
946 uint8_t packet_size;
947 sh_video_t *sh_video;
948 sh_audio_t *sh_audio;
949 off_t start_pos;
950 tsdemux_init_t params;
951 ts_priv_t * priv = demuxer->priv;
953 mp_msg(MSGT_DEMUX, MSGL_V, "DEMUX OPEN, AUDIO_ID: %d, VIDEO_ID: %d, SUBTITLE_ID: %d,\n",
954 demuxer->audio->id, demuxer->video->id, demuxer->sub->id);
957 demuxer->type= DEMUXER_TYPE_MPEG_TS;
960 stream_reset(demuxer->stream);
962 packet_size = ts_check_file(demuxer);
963 if(!packet_size)
964 return NULL;
966 priv = calloc(1, sizeof(ts_priv_t));
967 if(priv == NULL)
969 mp_msg(MSGT_DEMUX, MSGL_FATAL, "DEMUX_OPEN_TS, couldn't allocate enough memory for ts->priv, exit\n");
970 return NULL;
973 for(i=0; i < 8192; i++)
975 priv->ts.pids[i] = NULL;
976 priv->ts.streams[i].id = -3;
978 priv->pat.progs = NULL;
979 priv->pat.progs_cnt = 0;
980 priv->pat.section.buffer = NULL;
981 priv->pat.section.buffer_len = 0;
983 priv->pmt = NULL;
984 priv->pmt_cnt = 0;
986 priv->keep_broken = ts_keep_broken;
987 priv->ts.packet_size = packet_size;
990 demuxer->priv = priv;
991 if(demuxer->stream->type != STREAMTYPE_FILE)
992 demuxer->seekable = 1;
993 else
994 demuxer->seekable = 1;
997 params.atype = params.vtype = params.stype = UNKNOWN;
998 params.apid = demuxer->audio->id;
999 params.vpid = demuxer->video->id;
1000 params.spid = demuxer->sub->id;
1001 params.prog = ts_prog;
1002 params.probe = ts_probe;
1004 if(dvdsub_lang != NULL)
1006 strncpy(params.slang, dvdsub_lang, 3);
1007 params.slang[3] = 0;
1009 else
1010 memset(params.slang, 0, 4);
1012 if(audio_lang != NULL)
1014 strncpy(params.alang, audio_lang, 3);
1015 params.alang[3] = 0;
1017 else
1018 memset(params.alang, 0, 4);
1020 start_pos = ts_detect_streams(demuxer, &params);
1022 demuxer->sub->id = params.spid;
1023 priv->prog = params.prog;
1025 if(params.vtype != UNKNOWN)
1027 ts_add_stream(demuxer, priv->ts.pids[params.vpid]);
1028 sh_video = priv->ts.streams[params.vpid].sh;
1029 demuxer->video->id = priv->ts.streams[params.vpid].id;
1030 sh_video->ds = demuxer->video;
1031 sh_video->format = params.vtype;
1032 demuxer->video->sh = sh_video;
1035 if(params.atype != UNKNOWN)
1037 ES_stream_t *es = priv->ts.pids[params.apid];
1039 if(!IS_AUDIO(es->type) && !IS_AUDIO(es->subtype) && IS_AUDIO(params.atype)) es->subtype = params.atype;
1040 ts_add_stream(demuxer, priv->ts.pids[params.apid]);
1041 sh_audio = priv->ts.streams[params.apid].sh;
1042 demuxer->audio->id = priv->ts.streams[params.apid].id;
1043 sh_audio->ds = demuxer->audio;
1044 sh_audio->format = params.atype;
1045 demuxer->audio->sh = sh_audio;
1049 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);
1052 start_pos = (start_pos <= priv->ts.packet_size ? 0 : start_pos - priv->ts.packet_size);
1053 demuxer->movi_start = start_pos;
1054 demuxer->reference_clock = MP_NOPTS_VALUE;
1055 stream_reset(demuxer->stream);
1056 stream_seek(demuxer->stream, start_pos); //IF IT'S FROM A PIPE IT WILL FAIL, BUT WHO CARES?
1059 priv->last_pid = 8192; //invalid pid
1061 for(i = 0; i < 3; i++)
1063 priv->fifo[i].pack = NULL;
1064 priv->fifo[i].offset = 0;
1066 priv->fifo[0].ds = demuxer->audio;
1067 priv->fifo[1].ds = demuxer->video;
1068 priv->fifo[2].ds = demuxer->sub;
1070 priv->fifo[0].buffer_size = 1536;
1071 priv->fifo[1].buffer_size = 32767;
1072 priv->fifo[2].buffer_size = 32767;
1074 priv->pat.section.buffer_len = 0;
1075 for(i = 0; i < priv->pmt_cnt; i++)
1076 priv->pmt[i].section.buffer_len = 0;
1078 demuxer->filepos = stream_tell(demuxer->stream);
1079 return demuxer;
1082 static void demux_close_ts(demuxer_t * demuxer)
1084 uint16_t i;
1085 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
1087 if(priv)
1089 if(priv->pat.section.buffer)
1090 free(priv->pat.section.buffer);
1091 if(priv->pat.progs)
1092 free(priv->pat.progs);
1094 if(priv->pmt)
1096 for(i = 0; i < priv->pmt_cnt; i++)
1098 if(priv->pmt[i].section.buffer)
1099 free(priv->pmt[i].section.buffer);
1100 if(priv->pmt[i].es)
1101 free(priv->pmt[i].es);
1103 free(priv->pmt);
1105 free(priv);
1107 demuxer->priv=NULL;
1111 unsigned char mp_getbits(unsigned char*, unsigned int, unsigned char);
1112 #define getbits mp_getbits
1114 static int mp4_parse_sl_packet(pmt_t *pmt, uint8_t *buf, uint16_t packet_len, int pid, ES_stream_t *pes_es)
1116 int i, n, m, mp4_es_id = -1;
1117 uint64_t v = 0;
1118 uint32_t pl_size = 0;
1119 int deg_flag = 0;
1120 mp4_es_descr_t *es = NULL;
1121 mp4_sl_config_t *sl = NULL;
1122 uint8_t au_start = 0, au_end = 0, rap_flag = 0, ocr_flag = 0, padding = 0, padding_bits = 0, idle = 0;
1124 pes_es->is_synced = 0;
1125 mp_msg(MSGT_DEMUXER,MSGL_V, "mp4_parse_sl_packet, pid: %d, pmt: %pm, packet_len: %d\n", pid, pmt, packet_len);
1126 if(! pmt || !packet_len)
1127 return 0;
1129 for(i = 0; i < pmt->es_cnt; i++)
1131 if(pmt->es[i].pid == pid)
1132 mp4_es_id = pmt->es[i].mp4_es_id;
1134 if(mp4_es_id < 0)
1135 return -1;
1137 for(i = 0; i < pmt->mp4es_cnt; i++)
1139 if(pmt->mp4es[i].id == mp4_es_id)
1140 es = &(pmt->mp4es[i]);
1142 if(! es)
1143 return -1;
1145 pes_es->subtype = es->decoder.object_type;
1147 sl = &(es->sl);
1148 if(!sl)
1149 return -1;
1151 //now es is the complete es_descriptor of out mp4 ES stream
1152 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "ID: %d, FLAGS: 0x%x, subtype: %x\n", es->id, sl->flags, pes_es->subtype);
1154 n = 0;
1155 if(sl->au_start)
1156 pes_es->sl.au_start = au_start = getbits(buf, n++, 1);
1157 else
1158 pes_es->sl.au_start = (pes_es->sl.last_au_end ? 1 : 0);
1159 if(sl->au_end)
1160 pes_es->sl.au_end = au_end = getbits(buf, n++, 1);
1162 if(!sl->au_start && !sl->au_end)
1164 pes_es->sl.au_start = pes_es->sl.au_end = au_start = au_end = 1;
1166 pes_es->sl.last_au_end = pes_es->sl.au_end;
1169 if(sl->ocr_len > 0)
1170 ocr_flag = getbits(buf, n++, 1);
1171 if(sl->idle)
1172 idle = getbits(buf, n++, 1);
1173 if(sl->padding)
1174 padding = getbits(buf, n++, 1);
1175 if(padding)
1177 padding_bits = getbits(buf, n, 3);
1178 n += 3;
1181 if(idle || (padding && !padding_bits))
1183 pes_es->payload_size = 0;
1184 return -1;
1187 //(! idle && (!padding || padding_bits != 0)) is true
1188 n += sl->packet_seqnum_len;
1189 if(sl->degr_len)
1190 deg_flag = getbits(buf, n++, 1);
1191 if(deg_flag)
1192 n += sl->degr_len;
1194 if(ocr_flag)
1196 n += sl->ocr_len;
1197 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "OCR: %d bits\n", sl->ocr_len);
1200 if(packet_len * 8 <= n)
1201 return -1;
1203 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "\nAU_START: %d, AU_END: %d\n", au_start, au_end);
1204 if(au_start)
1206 int dts_flag = 0, cts_flag = 0, ib_flag = 0;
1208 if(sl->random_accesspoint)
1209 rap_flag = getbits(buf, n++, 1);
1211 //check commented because it seems it's rarely used, and we need this flag set in case of au_start
1212 //the decoder will eventually discard the payload if it can't decode it
1213 //if(rap_flag || sl->random_accesspoint_only)
1214 pes_es->is_synced = 1;
1216 n += sl->au_seqnum_len;
1217 if(packet_len * 8 <= n+8)
1218 return -1;
1219 if(sl->use_ts)
1221 dts_flag = getbits(buf, n++, 1);
1222 cts_flag = getbits(buf, n++, 1);
1224 if(sl->instant_bitrate_len)
1225 ib_flag = getbits(buf, n++, 1);
1226 if(packet_len * 8 <= n+8)
1227 return -1;
1228 if(dts_flag && (sl->ts_len > 0))
1230 n += sl->ts_len;
1231 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "DTS: %d bits\n", sl->ts_len);
1233 if(packet_len * 8 <= n+8)
1234 return -1;
1235 if(cts_flag && (sl->ts_len > 0))
1237 int i = 0, m;
1239 while(i < sl->ts_len)
1241 m = FFMIN(8, sl->ts_len - i);
1242 v |= getbits(buf, n, m);
1243 if(sl->ts_len - i > 8)
1244 v <<= 8;
1245 i += m;
1246 n += m;
1247 if(packet_len * 8 <= n+8)
1248 return -1;
1251 pes_es->pts = (float) v / (float) sl->ts_resolution;
1252 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "CTS: %d bits, value: %"PRIu64"/%d = %.3f\n", sl->ts_len, v, sl->ts_resolution, pes_es->pts);
1256 i = 0;
1257 pl_size = 0;
1258 while(i < sl->au_len)
1260 m = FFMIN(8, sl->au_len - i);
1261 pl_size |= getbits(buf, n, m);
1262 if(sl->au_len - i > 8)
1263 pl_size <<= 8;
1264 i += m;
1265 n += m;
1266 if(packet_len * 8 <= n+8)
1267 return -1;
1269 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "AU_LEN: %u (%d bits)\n", pl_size, sl->au_len);
1270 if(ib_flag)
1271 n += sl->instant_bitrate_len;
1274 m = (n+7)/8;
1275 if(0 < pl_size && pl_size < pes_es->payload_size)
1276 pes_es->payload_size = pl_size;
1278 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",
1279 n, m, pes_es->payload_size, pl_size, (int) rap_flag, (int) sl->random_accesspoint_only);
1281 return m;
1284 //this function parses the extension fields in the PES header and returns the substream_id, or -1 in case of errors
1285 static int parse_pes_extension_fields(unsigned char *p, int pkt_len)
1287 int skip;
1288 unsigned char flags;
1290 if(!(p[7] & 0x1)) //no extension_field
1291 return -1;
1292 skip = 9;
1293 if(p[7] & 0x80)
1295 skip += 5;
1296 if(p[7] & 0x40)
1297 skip += 5;
1299 if(p[7] & 0x20) //escr_flag
1300 skip += 6;
1301 if(p[7] & 0x10) //es_rate_flag
1302 skip += 3;
1303 if(p[7] & 0x08)//dsm_trick_mode is unsupported, skip
1305 skip = 0;//don't let's parse the extension fields
1307 if(p[7] & 0x04) //additional_copy_info
1308 skip += 1;
1309 if(p[7] & 0x02) //pes_crc_flag
1310 skip += 2;
1311 if(skip >= pkt_len) //too few bytes
1312 return -1;
1313 flags = p[skip];
1314 skip++;
1315 if(flags & 0x80) //pes_private_data_flag
1316 skip += 16;
1317 if(skip >= pkt_len)
1318 return -1;
1319 if(flags & 0x40) //pack_header_field_flag
1321 unsigned char l = p[skip];
1322 skip += l;
1324 if(flags & 0x20) //program_packet_sequence_counter
1325 skip += 2;
1326 if(flags & 0x10) //p_std
1327 skip += 2;
1328 if(skip >= pkt_len)
1329 return -1;
1330 if(flags & 0x01) //finally the long desired pes_extension2
1332 unsigned char l = p[skip]; //ext2 flag+len
1333 skip++;
1334 if((l == 0x81) && (skip < pkt_len))
1336 int ssid = p[skip];
1337 mp_msg(MSGT_IDENTIFY, MSGL_V, "SUBSTREAM_ID=%d (0x%02X)\n", ssid, ssid);
1338 return ssid;
1342 return -1;
1345 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)
1347 unsigned char *p;
1348 uint32_t header_len;
1349 int64_t pts;
1350 uint32_t stream_id;
1351 uint32_t pkt_len, pes_is_aligned;
1353 //Here we are always at the start of a PES packet
1354 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2(%p, %d): \n", buf, (uint32_t) packet_len);
1356 if(packet_len == 0 || packet_len > 184)
1358 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2, BUFFER LEN IS TOO SMALL OR TOO BIG: %d EXIT\n", packet_len);
1359 return 0;
1362 p = buf;
1363 pkt_len = packet_len;
1366 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: HEADER %02x %02x %02x %02x\n", p[0], p[1], p[2], p[3]);
1367 if (p[0] || p[1] || (p[2] != 1))
1369 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: error HEADER %02x %02x %02x (should be 0x000001) \n", p[0], p[1], p[2]);
1370 return 0 ;
1373 packet_len -= 6;
1374 if(packet_len==0)
1376 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: packet too short: %d, exit\n", packet_len);
1377 return 0;
1380 es->payload_size = (p[4] << 8 | p[5]);
1381 pes_is_aligned = (p[6] & 4);
1383 stream_id = p[3];
1386 if (p[7] & 0x80)
1387 { /* pts available */
1388 pts = (int64_t)(p[9] & 0x0E) << 29 ;
1389 pts |= p[10] << 22 ;
1390 pts |= (p[11] & 0xFE) << 14 ;
1391 pts |= p[12] << 7 ;
1392 pts |= (p[13] & 0xFE) >> 1 ;
1394 es->pts = pts / 90000.0f;
1396 else
1397 es->pts = 0.0f;
1400 header_len = p[8];
1403 if (header_len + 9 > pkt_len) //9 are the bytes read up to the header_length field
1405 mp_msg(MSGT_DEMUX, MSGL_DBG2, "demux_ts: illegal value for PES_header_data_length (0x%02x)\n", header_len);
1406 return 0;
1409 if(stream_id==0xfd)
1411 int ssid = parse_pes_extension_fields(p, pkt_len);
1412 if((audio_substream_id!=-1) && (ssid != audio_substream_id))
1413 return 0;
1416 p += header_len + 9;
1417 packet_len -= header_len + 3;
1419 if(es->payload_size)
1420 es->payload_size -= header_len + 3;
1423 es->is_synced = 1; //only for SL streams we have to make sure it's really true, see below
1424 if (stream_id == 0xbd)
1426 mp_msg(MSGT_DEMUX, MSGL_DBG3, "pes_parse2: audio buf = %02X %02X %02X %02X %02X %02X %02X %02X, 80: %d\n",
1427 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[0] & 0x80);
1431 * we check the descriptor tag first because some stations
1432 * do not include any of the A52 header info in their audio tracks
1433 * these "raw" streams may begin with a byte that looks like a stream type.
1438 (type_from_pmt == AUDIO_A52) || /* A52 - raw */
1439 (p[0] == 0x0B && p[1] == 0x77) /* A52 - syncword */
1442 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 RAW OR SYNCWORD\n");
1443 es->start = p;
1444 es->size = packet_len;
1445 es->type = AUDIO_A52;
1446 es->payload_size -= packet_len;
1448 return 1;
1450 /* SPU SUBS */
1451 else if(type_from_pmt == SPU_DVB ||
1452 ((p[0] == 0x20) && pes_is_aligned)) // && p[1] == 0x00))
1454 es->start = p;
1455 es->size = packet_len;
1456 es->type = SPU_DVB;
1457 es->payload_size -= packet_len;
1459 return 1;
1461 else if (pes_is_aligned && ((p[0] & 0xE0) == 0x20)) //SPU_DVD
1463 //DVD SUBS
1464 es->start = p+1;
1465 es->size = packet_len-1;
1466 es->type = SPU_DVD;
1467 es->payload_size -= packet_len;
1469 return 1;
1471 else if (pes_is_aligned && (p[0] & 0xF8) == 0x80)
1473 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 WITH HEADER\n");
1474 es->start = p+4;
1475 es->size = packet_len - 4;
1476 es->type = AUDIO_A52;
1477 es->payload_size -= packet_len;
1479 return 1;
1481 else if (pes_is_aligned && ((p[0]&0xf0) == 0xa0))
1483 int pcm_offset;
1485 for (pcm_offset=0; ++pcm_offset < packet_len-1 ; )
1487 if (p[pcm_offset] == 0x01 && p[pcm_offset+1] == 0x80)
1488 { /* START */
1489 pcm_offset += 2;
1490 break;
1494 es->start = p + pcm_offset;
1495 es->size = packet_len - pcm_offset;
1496 es->type = AUDIO_LPCM_BE;
1497 es->payload_size -= packet_len;
1499 return 1;
1501 else
1503 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PES_PRIVATE1\n");
1504 es->start = p;
1505 es->size = packet_len;
1506 es->type = (type_from_pmt == UNKNOWN ? PES_PRIVATE1 : type_from_pmt);
1507 es->payload_size -= packet_len;
1509 return 1;
1512 else if(((stream_id >= 0xe0) && (stream_id <= 0xef)) || (stream_id == 0xfd && type_from_pmt != UNKNOWN))
1514 es->start = p;
1515 es->size = packet_len;
1516 if(type_from_pmt != UNKNOWN)
1517 es->type = type_from_pmt;
1518 else
1519 es->type = VIDEO_MPEG2;
1520 if(es->payload_size)
1521 es->payload_size -= packet_len;
1523 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: M2V size %d\n", es->size);
1524 return 1;
1526 else if ((stream_id == 0xfa))
1528 int l;
1530 es->is_synced = 0;
1531 if(type_from_pmt != UNKNOWN) //MP4 A/V or SL
1533 es->start = p;
1534 es->size = packet_len;
1535 es->type = type_from_pmt;
1537 if(type_from_pmt == SL_PES_STREAM)
1539 //if(pes_is_aligned)
1541 l = mp4_parse_sl_packet(pmt, p, packet_len, pid, es);
1542 mp_msg(MSGT_DEMUX, MSGL_DBG2, "L=%d, TYPE=%x\n", l, type_from_pmt);
1543 if(l < 0)
1545 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: couldn't parse SL header, passing along full PES payload\n");
1546 l = 0;
1550 es->start += l;
1551 es->size -= l;
1554 if(es->payload_size)
1555 es->payload_size -= packet_len;
1556 return 1;
1559 else if ((stream_id & 0xe0) == 0xc0)
1561 es->start = p;
1562 es->size = packet_len;
1564 if(type_from_pmt != UNKNOWN)
1565 es->type = type_from_pmt;
1566 else
1567 es->type = AUDIO_MP2;
1569 es->payload_size -= packet_len;
1571 return 1;
1573 else if (type_from_pmt != -1) //as a last resort here we trust the PMT, if present
1575 es->start = p;
1576 es->size = packet_len;
1577 es->type = type_from_pmt;
1578 es->payload_size -= packet_len;
1580 return 1;
1582 else
1584 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: unknown packet, id: %x\n", stream_id);
1587 es->is_synced = 0;
1588 return 0;
1594 static int ts_sync(stream_t *stream)
1596 int c=0;
1598 mp_msg(MSGT_DEMUX, MSGL_DBG3, "TS_SYNC \n");
1600 while(((c=stream_read_char(stream)) != 0x47) && ! stream->eof);
1602 if(c == 0x47)
1603 return c;
1604 else
1605 return 0;
1609 static void ts_dump_streams(ts_priv_t *priv)
1611 int i;
1613 for(i = 0; i < 3; i++)
1615 if((priv->fifo[i].pack != NULL) && (priv->fifo[i].offset != 0))
1617 resize_demux_packet(priv->fifo[i].pack, priv->fifo[i].offset);
1618 ds_add_packet(priv->fifo[i].ds, priv->fifo[i].pack);
1619 priv->fifo[i].offset = 0;
1620 priv->fifo[i].pack = NULL;
1626 static inline int32_t prog_idx_in_pat(ts_priv_t *priv, uint16_t progid)
1628 int x;
1630 if(priv->pat.progs == NULL)
1631 return -1;
1633 for(x = 0; x < priv->pat.progs_cnt; x++)
1635 if(priv->pat.progs[x].id == progid)
1636 return x;
1639 return -1;
1643 static inline int32_t prog_id_in_pat(ts_priv_t *priv, uint16_t pid)
1645 int x;
1647 if(priv->pat.progs == NULL)
1648 return -1;
1650 for(x = 0; x < priv->pat.progs_cnt; x++)
1652 if(priv->pat.progs[x].pmt_pid == pid)
1653 return priv->pat.progs[x].id;
1656 return -1;
1659 static int collect_section(ts_section_t *section, int is_start, unsigned char *buff, int size)
1661 uint8_t *ptr;
1662 uint16_t tlen;
1663 int skip, tid;
1665 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, start: %d, size: %d, collected: %d\n", is_start, size, section->buffer_len);
1666 if(! is_start && !section->buffer_len)
1667 return 0;
1669 if(is_start)
1671 if(! section->buffer)
1673 section->buffer = (uint8_t*) malloc(4096+256);
1674 if(section->buffer == NULL)
1675 return 0;
1677 section->buffer_len = 0;
1680 if(size + section->buffer_len > 4096+256)
1682 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, excessive len: %d + %d\n", section->buffer_len, size);
1683 return 0;
1686 memcpy(&(section->buffer[section->buffer_len]), buff, size);
1687 section->buffer_len += size;
1689 if(section->buffer_len < 3)
1690 return 0;
1692 skip = section->buffer[0];
1693 if(skip + 4 > section->buffer_len)
1694 return 0;
1696 ptr = &(section->buffer[skip + 1]);
1697 tid = ptr[0];
1698 tlen = ((ptr[1] & 0x0f) << 8) | ptr[2];
1699 mp_msg(MSGT_DEMUX, MSGL_V, "SKIP: %d+1, TID: %d, TLEN: %d, COLLECTED: %d\n", skip, tid, tlen, section->buffer_len);
1700 if(section->buffer_len < (skip+1+3+tlen))
1702 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DATA IS NOT ENOUGH, NEXT TIME\n");
1703 return 0;
1706 return skip+1;
1709 static int parse_pat(ts_priv_t * priv, int is_start, unsigned char *buff, int size)
1711 int skip;
1712 unsigned char *ptr;
1713 unsigned char *base;
1714 int entries, i;
1715 uint16_t progid;
1716 struct pat_progs_t *tmp;
1717 ts_section_t *section;
1719 section = &(priv->pat.section);
1720 skip = collect_section(section, is_start, buff, size);
1721 if(! skip)
1722 return 0;
1724 ptr = &(section->buffer[skip]);
1725 //PARSING
1726 priv->pat.table_id = ptr[0];
1727 if(priv->pat.table_id != 0)
1728 return 0;
1729 priv->pat.ssi = (ptr[1] >> 7) & 0x1;
1730 priv->pat.curr_next = ptr[5] & 0x01;
1731 priv->pat.ts_id = (ptr[3] << 8 ) | ptr[4];
1732 priv->pat.version_number = (ptr[5] >> 1) & 0x1F;
1733 priv->pat.section_length = ((ptr[1] & 0x03) << 8 ) | ptr[2];
1734 priv->pat.section_number = ptr[6];
1735 priv->pat.last_section_number = ptr[7];
1737 //check_crc32(0xFFFFFFFFL, ptr, priv->pat.buffer_len - 4, &ptr[priv->pat.buffer_len - 4]);
1738 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);
1740 entries = (int) (priv->pat.section_length - 9) / 4; //entries per section
1742 for(i=0; i < entries; i++)
1744 int32_t idx;
1745 base = &ptr[8 + i*4];
1746 progid = (base[0] << 8) | base[1];
1748 if((idx = prog_idx_in_pat(priv, progid)) == -1)
1750 int sz = sizeof(struct pat_progs_t) * (priv->pat.progs_cnt+1);
1751 tmp = realloc_struct(priv->pat.progs, priv->pat.progs_cnt+1, sizeof(struct pat_progs_t));
1752 if(tmp == NULL)
1754 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PAT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
1755 break;
1757 priv->pat.progs = tmp;
1758 idx = priv->pat.progs_cnt;
1759 priv->pat.progs_cnt++;
1762 priv->pat.progs[idx].id = progid;
1763 priv->pat.progs[idx].pmt_pid = ((base[2] & 0x1F) << 8) | base[3];
1764 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);
1765 mp_msg(MSGT_IDENTIFY, MSGL_V, "PROGRAM_ID=%d (0x%02X), PMT_PID: %d(0x%02X)\n",
1766 progid, progid, priv->pat.progs[idx].pmt_pid, priv->pat.progs[idx].pmt_pid);
1769 return 1;
1773 static inline int32_t es_pid_in_pmt(pmt_t * pmt, uint16_t pid)
1775 uint16_t i;
1777 if(pmt == NULL)
1778 return -1;
1780 if(pmt->es == NULL)
1781 return -1;
1783 for(i = 0; i < pmt->es_cnt; i++)
1785 if(pmt->es[i].pid == pid)
1786 return (int32_t) i;
1789 return -1;
1793 static uint16_t get_mp4_desc_len(uint8_t *buf, int *len)
1795 //uint16_t i = 0, size = 0;
1796 int i = 0, j, size = 0;
1798 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PARSE_MP4_DESC_LEN(%d), bytes: ", *len);
1799 j = FFMIN(*len, 4);
1800 while(i < j)
1802 mp_msg(MSGT_DEMUX, MSGL_DBG2, " %x ", buf[i]);
1803 size |= (buf[i] & 0x7f);
1804 if(!(buf[i] & 0x80))
1805 break;
1806 size <<= 7;
1807 i++;
1809 mp_msg(MSGT_DEMUX, MSGL_DBG2, ", SIZE=%d\n", size);
1811 *len = i+1;
1812 return size;
1816 static uint16_t parse_mp4_slconfig_descriptor(uint8_t *buf, int len, void *elem)
1818 int i = 0;
1819 mp4_es_descr_t *es;
1820 mp4_sl_config_t *sl;
1822 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_SLCONFIG_DESCRIPTOR(%d)\n", len);
1823 es = (mp4_es_descr_t *) elem;
1824 if(!es)
1826 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1827 return len;
1829 sl = &(es->sl);
1831 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;
1832 sl->ocr = sl->dts = sl->cts = 0;
1834 if(buf[0] == 0)
1836 i++;
1837 sl->flags = buf[i];
1838 i++;
1839 sl->ts_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1840 i += 4;
1841 sl->ocr_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1842 i += 4;
1843 sl->ts_len = buf[i];
1844 i++;
1845 sl->ocr_len = buf[i];
1846 i++;
1847 sl->au_len = buf[i];
1848 i++;
1849 sl->instant_bitrate_len = buf[i];
1850 i++;
1851 sl->degr_len = (buf[i] >> 4) & 0x0f;
1852 sl->au_seqnum_len = ((buf[i] & 0x0f) << 1) | ((buf[i+1] >> 7) & 0x01);
1853 i++;
1854 sl->packet_seqnum_len = ((buf[i] >> 2) & 0x1f);
1855 i++;
1858 else if(buf[0] == 1)
1860 sl->flags = 0;
1861 sl->ts_resolution = 1000;
1862 sl->ts_len = 32;
1863 i++;
1865 else if(buf[0] == 2)
1867 sl->flags = 4;
1868 i++;
1870 else
1872 sl->flags = 0;
1873 i++;
1876 sl->au_start = (sl->flags >> 7) & 0x1;
1877 sl->au_end = (sl->flags >> 6) & 0x1;
1878 sl->random_accesspoint = (sl->flags >> 5) & 0x1;
1879 sl->random_accesspoint_only = (sl->flags >> 4) & 0x1;
1880 sl->padding = (sl->flags >> 3) & 0x1;
1881 sl->use_ts = (sl->flags >> 2) & 0x1;
1882 sl->idle = (sl->flags >> 1) & 0x1;
1883 sl->duration = sl->flags & 0x1;
1885 if(sl->duration)
1887 sl->timescale = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1888 i += 4;
1889 sl->au_duration = (buf[i] << 8) | buf[i+1];
1890 i += 2;
1891 sl->cts_duration = (buf[i] << 8) | buf[i+1];
1892 i += 2;
1894 else //no support for fixed durations atm
1895 sl->timescale = sl->au_duration = sl->cts_duration = 0;
1897 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",
1898 len, buf[0], sl->flags, sl->use_ts, sl->ts_len, sl->timescale, (uint64_t) sl->dts, (uint64_t) sl->cts);
1900 return len;
1903 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem);
1905 static uint16_t parse_mp4_decoder_config_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
1907 int i = 0, j;
1908 mp4_es_descr_t *es;
1909 mp4_decoder_config_t *dec;
1911 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_CONFIG_DESCRIPTOR(%d)\n", len);
1912 es = (mp4_es_descr_t *) elem;
1913 if(!es)
1915 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1916 return len;
1918 dec = (mp4_decoder_config_t*) &(es->decoder);
1920 dec->object_type = buf[i];
1921 dec->stream_type = (buf[i+1]>>2) & 0x3f;
1923 if(dec->object_type == 1 && dec->stream_type == 1)
1925 dec->object_type = MP4_OD;
1926 dec->stream_type = MP4_OD;
1928 else if(dec->stream_type == 4)
1930 if(dec->object_type == 0x6a)
1931 dec->object_type = VIDEO_MPEG1;
1932 if(dec->object_type >= 0x60 && dec->object_type <= 0x65)
1933 dec->object_type = VIDEO_MPEG2;
1934 else if(dec->object_type == 0x20)
1935 dec->object_type = VIDEO_MPEG4;
1936 else if(dec->object_type == 0x21)
1937 dec->object_type = VIDEO_AVC;
1938 /*else if(dec->object_type == 0x22)
1939 fprintf(stderr, "TYPE 0x22\n");*/
1940 else dec->object_type = UNKNOWN;
1942 else if(dec->stream_type == 5)
1944 if(dec->object_type == 0x40)
1945 dec->object_type = AUDIO_AAC;
1946 else if(dec->object_type == 0x6b)
1947 dec->object_type = AUDIO_MP2;
1948 else if(dec->object_type >= 0x66 && dec->object_type <= 0x69)
1949 dec->object_type = AUDIO_MP2;
1950 else
1951 dec->object_type = UNKNOWN;
1953 else
1954 dec->object_type = dec->stream_type = UNKNOWN;
1956 if(dec->object_type != UNKNOWN)
1958 //update the type of the current stream
1959 for(j = 0; j < pmt->es_cnt; j++)
1961 if(pmt->es[j].mp4_es_id == es->id)
1963 pmt->es[j].type = SL_PES_STREAM;
1968 if(len > 13)
1969 parse_mp4_descriptors(pmt, &buf[13], len-13, dec);
1971 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);
1973 return len;
1976 static uint16_t parse_mp4_decoder_specific_descriptor(uint8_t *buf, int len, void *elem)
1978 int i;
1979 mp4_decoder_config_t *dec;
1981 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_SPECIFIC_DESCRIPTOR(%d)\n", len);
1982 dec = (mp4_decoder_config_t *) elem;
1983 if(!dec)
1985 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1986 return len;
1989 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4 SPECIFIC INFO BYTES: \n");
1990 for(i=0; i<len; i++)
1991 mp_msg(MSGT_DEMUX, MSGL_DBG2, "%02x ", buf[i]);
1992 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\n");
1994 if(len > MAX_EXTRADATA_SIZE)
1996 mp_msg(MSGT_DEMUX, MSGL_ERR, "DEMUX_TS, EXTRADATA SUSPICIOUSLY BIG: %d, REFUSED\r\n", len);
1997 return len;
1999 memcpy(dec->buf, buf, len);
2000 dec->buf_size = len;
2002 return len;
2005 static uint16_t parse_mp4_es_descriptor(pmt_t *pmt, uint8_t *buf, int len)
2007 int i = 0, j = 0, k, found;
2008 uint8_t flag;
2009 mp4_es_descr_t es, *target_es = NULL, *tmp;
2011 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4ES: len=%d\n", len);
2012 memset(&es, 0, sizeof(mp4_es_descr_t));
2013 while(i < len)
2015 es.id = (buf[i] << 8) | buf[i+1];
2016 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_ID: %d\n", es.id);
2017 i += 2;
2018 flag = buf[i];
2019 i++;
2020 if(flag & 0x80)
2021 i += 2;
2022 if(flag & 0x40)
2023 i += buf[i]+1;
2024 if(flag & 0x20) //OCR, maybe we need it
2025 i += 2;
2027 j = parse_mp4_descriptors(pmt, &buf[i], len-i, &es);
2028 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);
2029 if(es.decoder.object_type != UNKNOWN && es.decoder.stream_type != UNKNOWN)
2031 found = 0;
2032 //search this ES_ID if we already have it
2033 for(k=0; k < pmt->mp4es_cnt; k++)
2035 if(pmt->mp4es[k].id == es.id)
2037 target_es = &(pmt->mp4es[k]);
2038 found = 1;
2042 if(! found)
2044 tmp = realloc_struct(pmt->mp4es, pmt->mp4es_cnt+1, sizeof(mp4_es_descr_t));
2045 if(tmp == NULL)
2047 fprintf(stderr, "CAN'T REALLOC MP4_ES_DESCR\n");
2048 continue;
2050 pmt->mp4es = tmp;
2051 target_es = &(pmt->mp4es[pmt->mp4es_cnt]);
2052 pmt->mp4es_cnt++;
2054 memcpy(target_es, &es, sizeof(mp4_es_descr_t));
2055 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_CNT: %d, ID=%d\n", pmt->mp4es_cnt, target_es->id);
2058 i += j;
2061 return len;
2064 static void parse_mp4_object_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2066 int i, j = 0, id;
2068 i=0;
2069 id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2070 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_OBJECT_DESCRIPTOR: len=%d, OD_ID=%d\n", len, id);
2071 if(buf[1] & 0x20)
2073 i += buf[2] + 1; //url
2074 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2076 else
2078 i = 2;
2080 while(i < len)
2082 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2083 mp_msg(MSGT_DEMUX, MSGL_V, "OBJD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2084 i += j;
2090 static void parse_mp4_iod(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2092 int i, j = 0;
2093 mp4_od_t *iod = &(pmt->iod);
2095 iod->id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2096 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_IOD: len=%d, IOD_ID=%d\n", len, iod->id);
2097 i = 2;
2098 if(buf[1] & 0x20)
2100 i += buf[2] + 1; //url
2101 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2103 else
2105 i = 7;
2106 while(i < len)
2108 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2109 mp_msg(MSGT_DEMUX, MSGL_V, "IOD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2110 i += j;
2115 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2117 int tag, descr_len, i = 0, j = 0;
2119 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DESCRIPTORS, len=%d\n", len);
2120 if(! len)
2121 return len;
2123 while(i < len)
2125 tag = buf[i];
2126 j = len - i -1;
2127 descr_len = get_mp4_desc_len(&(buf[i+1]), &j);
2128 mp_msg(MSGT_DEMUX, MSGL_V, "TAG=%d (0x%x), DESCR_len=%d, len=%d, j=%d\n", tag, tag, descr_len, len, j);
2129 if(descr_len > len - j+1)
2131 mp_msg(MSGT_DEMUX, MSGL_V, "descriptor is too long, exit\n");
2132 return len;
2134 i += j+1;
2136 switch(tag)
2138 case 0x1:
2139 parse_mp4_object_descriptor(pmt, &(buf[i]), descr_len, elem);
2140 break;
2141 case 0x2:
2142 parse_mp4_iod(pmt, &(buf[i]), descr_len, elem);
2143 break;
2144 case 0x3:
2145 parse_mp4_es_descriptor(pmt, &(buf[i]), descr_len);
2146 break;
2147 case 0x4:
2148 parse_mp4_decoder_config_descriptor(pmt, &buf[i], descr_len, elem);
2149 break;
2150 case 0x05:
2151 parse_mp4_decoder_specific_descriptor(&buf[i], descr_len, elem);
2152 break;
2153 case 0x6:
2154 parse_mp4_slconfig_descriptor(&buf[i], descr_len, elem);
2155 break;
2156 default:
2157 mp_msg(MSGT_DEMUX, MSGL_V, "Unsupported mp4 descriptor 0x%x\n", tag);
2159 i += descr_len;
2162 return len;
2165 static ES_stream_t *new_pid(ts_priv_t *priv, int pid)
2167 ES_stream_t *tss;
2169 tss = malloc(sizeof(ES_stream_t));
2170 if(! tss)
2171 return NULL;
2172 memset(tss, 0, sizeof(ES_stream_t));
2173 tss->pid = pid;
2174 tss->last_cc = -1;
2175 tss->type = UNKNOWN;
2176 tss->subtype = UNKNOWN;
2177 tss->is_synced = 0;
2178 tss->extradata = NULL;
2179 tss->extradata_alloc = tss->extradata_len = 0;
2180 priv->ts.pids[pid] = tss;
2182 return tss;
2186 static int parse_program_descriptors(pmt_t *pmt, uint8_t *buf, uint16_t len)
2188 uint16_t i = 0, k, olen = len;
2190 while(len > 0)
2192 mp_msg(MSGT_DEMUX, MSGL_V, "PROG DESCR, TAG=%x, LEN=%d(%x)\n", buf[i], buf[i+1], buf[i+1]);
2193 if(buf[i+1] > len-2)
2195 mp_msg(MSGT_DEMUX, MSGL_V, "ERROR, descriptor len is too long, skipping\n");
2196 return olen;
2199 if(buf[i] == 0x1d)
2201 if(buf[i+3] == 2) //buggy versions of vlc muxer make this non-standard mess (missing iod_scope)
2202 k = 3;
2203 else
2204 k = 4; //this is standard compliant
2205 parse_mp4_descriptors(pmt, &buf[i+k], (int) buf[i+1]-(k-2), NULL);
2208 len -= 2 + buf[i+1];
2211 return olen;
2214 static int parse_descriptors(struct pmt_es_t *es, uint8_t *ptr)
2216 int j, descr_len, len;
2218 j = 0;
2219 len = es->descr_length;
2220 while(len > 2)
2222 descr_len = ptr[j+1];
2223 mp_msg(MSGT_DEMUX, MSGL_V, "...descr id: 0x%x, len=%d\n", ptr[j], descr_len);
2224 if(descr_len > len)
2226 mp_msg(MSGT_DEMUX, MSGL_ERR, "INVALID DESCR LEN for tag %02x: %d vs %d max, EXIT LOOP\n", ptr[j], descr_len, len);
2227 return -1;
2231 if(ptr[j] == 0x6a || ptr[j] == 0x7a) //A52 Descriptor
2233 if(es->type == 0x6)
2235 es->type = AUDIO_A52;
2236 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB A52 Descriptor\n");
2239 else if(ptr[j] == 0x7b) //DVB DTS Descriptor
2241 if(es->type == 0x6)
2243 es->type = AUDIO_DTS;
2244 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB DTS Descriptor\n");
2247 else if(ptr[j] == 0x59) //Subtitling Descriptor
2249 uint8_t subtype;
2251 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Subtitling Descriptor\n");
2252 if(descr_len < 8)
2254 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Descriptor length too short for DVB Subtitle Descriptor: %d, SKIPPING\n", descr_len);
2256 else
2258 memcpy(es->lang, &ptr[j+2], 3);
2259 es->lang[3] = 0;
2260 subtype = ptr[j+5];
2262 (subtype >= 0x10 && subtype <= 0x13) ||
2263 (subtype >= 0x20 && subtype <= 0x23)
2266 es->type = SPU_DVB;
2267 //page parameters: compo page 2 bytes, ancillary page 2 bytes
2269 else
2270 es->type = UNKNOWN;
2273 else if(ptr[j] == 0x50) //Component Descriptor
2275 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Component Descriptor\n");
2276 memcpy(es->lang, &ptr[j+5], 3);
2277 es->lang[3] = 0;
2279 else if(ptr[j] == 0xa) //Language Descriptor
2281 memcpy(es->lang, &ptr[j+2], 3);
2282 es->lang[3] = 0;
2283 mp_msg(MSGT_DEMUX, MSGL_V, "Language Descriptor: %s\n", es->lang);
2285 else if(ptr[j] == 0x5) //Registration Descriptor (looks like e fourCC :) )
2287 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor\n");
2288 if(descr_len < 4)
2290 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor length too short: %d, SKIPPING\n", descr_len);
2292 else
2294 char *d;
2295 memcpy(es->format_descriptor, &ptr[j+2], 4);
2296 es->format_descriptor[4] = 0;
2298 d = &ptr[j+2];
2299 if(d[0] == 'A' && d[1] == 'C' && d[2] == '-' && d[3] == '3')
2301 es->type = AUDIO_A52;
2303 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '1')
2305 es->type = AUDIO_DTS;
2307 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '2')
2309 es->type = AUDIO_DTS;
2311 else if(d[0] == 'V' && d[1] == 'C' && d[2] == '-' && d[3] == '1')
2313 es->type = VIDEO_VC1;
2315 else
2316 es->type = UNKNOWN;
2317 mp_msg(MSGT_DEMUX, MSGL_DBG2, "FORMAT %s\n", es->format_descriptor);
2320 else if(ptr[j] == 0x1e)
2322 es->mp4_es_id = (ptr[j+2] << 8) | ptr[j+3];
2323 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);
2325 else
2326 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Unknown descriptor 0x%x, SKIPPING\n", ptr[j]);
2328 len -= 2 + descr_len;
2329 j += 2 + descr_len;
2332 return 1;
2335 static int parse_sl_section(pmt_t *pmt, ts_section_t *section, int is_start, unsigned char *buff, int size)
2337 int tid, len, skip;
2338 uint8_t *ptr;
2339 skip = collect_section(section, is_start, buff, size);
2340 if(! skip)
2341 return 0;
2343 ptr = &(section->buffer[skip]);
2344 tid = ptr[0];
2345 len = ((ptr[1] & 0x0f) << 8) | ptr[2];
2346 mp_msg(MSGT_DEMUX, MSGL_V, "TABLEID: %d (av. %d), skip=%d, LEN: %d\n", tid, section->buffer_len, skip, len);
2347 if(len > 4093 || section->buffer_len < len || tid != 5)
2349 mp_msg(MSGT_DEMUX, MSGL_V, "SECTION TOO LARGE or wrong section type, EXIT\n");
2350 return 0;
2353 if(! (ptr[5] & 1))
2354 return 0;
2356 //8 is the current position, len - 9 is the amount of data available
2357 parse_mp4_descriptors(pmt, &ptr[8], len - 9, NULL);
2359 return 1;
2362 static int parse_pmt(ts_priv_t * priv, uint16_t progid, uint16_t pid, int is_start, unsigned char *buff, int size)
2364 unsigned char *base, *es_base;
2365 pmt_t *pmt;
2366 int32_t idx, es_count, section_bytes;
2367 uint8_t m=0;
2368 int skip;
2369 pmt_t *tmp;
2370 struct pmt_es_t *tmp_es;
2371 ts_section_t *section;
2372 ES_stream_t *tss;
2374 idx = progid_idx_in_pmt(priv, progid);
2376 if(idx == -1)
2378 int sz = (priv->pmt_cnt + 1) * sizeof(pmt_t);
2379 tmp = realloc_struct(priv->pmt, priv->pmt_cnt + 1, sizeof(pmt_t));
2380 if(tmp == NULL)
2382 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
2383 return 0;
2385 priv->pmt = tmp;
2386 idx = priv->pmt_cnt;
2387 memset(&(priv->pmt[idx]), 0, sizeof(pmt_t));
2388 priv->pmt_cnt++;
2389 priv->pmt[idx].progid = progid;
2392 pmt = &(priv->pmt[idx]);
2394 section = &(pmt->section);
2395 skip = collect_section(section, is_start, buff, size);
2396 if(! skip)
2397 return 0;
2399 base = &(section->buffer[skip]);
2401 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",
2402 progid, pmt->section.buffer_len, is_start, pid, size, m, pmt->es_cnt, idx, pmt);
2404 pmt->table_id = base[0];
2405 if(pmt->table_id != 2)
2406 return -1;
2407 pmt->ssi = base[1] & 0x80;
2408 pmt->section_length = (((base[1] & 0xf) << 8 ) | base[2]);
2409 pmt->version_number = (base[5] >> 1) & 0x1f;
2410 pmt->curr_next = (base[5] & 1);
2411 pmt->section_number = base[6];
2412 pmt->last_section_number = base[7];
2413 pmt->PCR_PID = ((base[8] & 0x1f) << 8 ) | base[9];
2414 pmt->prog_descr_length = ((base[10] & 0xf) << 8 ) | base[11];
2415 if(pmt->prog_descr_length > pmt->section_length - 9)
2417 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, INVALID PROG_DESCR LENGTH (%d vs %d)\n", pmt->prog_descr_length, pmt->section_length - 9);
2418 return -1;
2421 if(pmt->prog_descr_length)
2422 parse_program_descriptors(pmt, &base[12], pmt->prog_descr_length);
2424 es_base = &base[12 + pmt->prog_descr_length]; //the beginning of th ES loop
2426 section_bytes= pmt->section_length - 13 - pmt->prog_descr_length;
2427 es_count = 0;
2429 while(section_bytes >= 5)
2431 int es_pid, es_type;
2433 es_type = es_base[0];
2434 es_pid = ((es_base[1] & 0x1f) << 8) | es_base[2];
2436 idx = es_pid_in_pmt(pmt, es_pid);
2437 if(idx == -1)
2439 int sz = sizeof(struct pmt_es_t) * (pmt->es_cnt + 1);
2440 tmp_es = realloc_struct(pmt->es, pmt->es_cnt + 1, sizeof(struct pmt_es_t));
2441 if(tmp_es == NULL)
2443 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT, COULDN'T ALLOCATE %d bytes for PMT_ES\n", sz);
2444 continue;
2446 pmt->es = tmp_es;
2447 idx = pmt->es_cnt;
2448 memset(&(pmt->es[idx]), 0, sizeof(struct pmt_es_t));
2449 pmt->es_cnt++;
2452 pmt->es[idx].descr_length = ((es_base[3] & 0xf) << 8) | es_base[4];
2455 if(pmt->es[idx].descr_length > section_bytes - 5)
2457 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, ES_DESCR_LENGTH TOO LARGE %d > %d, EXIT\n",
2458 pmt->es[idx].descr_length, section_bytes - 5);
2459 return -1;
2463 pmt->es[idx].pid = es_pid;
2464 if(es_type != 0x6)
2465 pmt->es[idx].type = UNKNOWN;
2466 else
2467 pmt->es[idx].type = es_type;
2469 parse_descriptors(&pmt->es[idx], &es_base[5]);
2471 switch(es_type)
2473 case 1:
2474 pmt->es[idx].type = VIDEO_MPEG1;
2475 break;
2476 case 2:
2477 pmt->es[idx].type = VIDEO_MPEG2;
2478 break;
2479 case 3:
2480 case 4:
2481 pmt->es[idx].type = AUDIO_MP2;
2482 break;
2483 case 6:
2484 if(pmt->es[idx].type == 0x6) //this could have been ovrwritten by parse_descriptors
2485 pmt->es[idx].type = UNKNOWN;
2486 break;
2487 case 0x10:
2488 pmt->es[idx].type = VIDEO_MPEG4;
2489 break;
2490 case 0x0f:
2491 case 0x11:
2492 pmt->es[idx].type = AUDIO_AAC;
2493 break;
2494 case 0x1b:
2495 pmt->es[idx].type = VIDEO_H264;
2496 break;
2497 case 0x12:
2498 pmt->es[idx].type = SL_PES_STREAM;
2499 break;
2500 case 0x13:
2501 pmt->es[idx].type = SL_SECTION;
2502 break;
2503 case 0x81:
2504 pmt->es[idx].type = AUDIO_A52;
2505 break;
2506 case 0x8A:
2507 case 0x82:
2508 case 0x86:
2509 pmt->es[idx].type = AUDIO_DTS;
2510 break;
2511 case 0xEA:
2512 pmt->es[idx].type = VIDEO_VC1;
2513 break;
2514 default:
2515 mp_msg(MSGT_DEMUX, MSGL_DBG2, "UNKNOWN ES TYPE=0x%x\n", es_type);
2516 pmt->es[idx].type = UNKNOWN;
2519 tss = priv->ts.pids[es_pid]; //an ES stream
2520 if(tss == NULL)
2522 tss = new_pid(priv, es_pid);
2523 if(tss)
2524 tss->type = pmt->es[idx].type;
2527 section_bytes -= 5 + pmt->es[idx].descr_length;
2528 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",
2529 progid, idx, es_count, pmt->es[idx].pid, pmt->es[idx].pid, pmt->es[idx].type, pmt->es[idx].descr_length, section_bytes);
2532 es_base += 5 + pmt->es[idx].descr_length;
2534 es_count++;
2537 mp_msg(MSGT_DEMUX, MSGL_V, "----------------------------\n");
2538 return 1;
2541 static pmt_t* pmt_of_pid(ts_priv_t *priv, int pid, mp4_decoder_config_t **mp4_dec)
2543 int32_t i, j, k;
2545 if(priv->pmt)
2547 for(i = 0; i < priv->pmt_cnt; i++)
2549 if(priv->pmt[i].es && priv->pmt[i].es_cnt)
2551 for(j = 0; j < priv->pmt[i].es_cnt; j++)
2553 if(priv->pmt[i].es[j].pid == pid)
2555 //search mp4_es_id
2556 if(priv->pmt[i].es[j].mp4_es_id)
2558 for(k = 0; k < priv->pmt[i].mp4es_cnt; k++)
2560 if(priv->pmt[i].mp4es[k].id == priv->pmt[i].es[j].mp4_es_id)
2562 *mp4_dec = &(priv->pmt[i].mp4es[k].decoder);
2563 break;
2568 return &(priv->pmt[i]);
2575 return NULL;
2579 static inline int32_t pid_type_from_pmt(ts_priv_t *priv, int pid)
2581 int32_t pmt_idx, pid_idx, i, j;
2583 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2585 if(pmt_idx != -1)
2587 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2588 if(pid_idx != -1)
2589 return priv->pmt[pmt_idx].es[pid_idx].type;
2591 //else
2593 for(i = 0; i < priv->pmt_cnt; i++)
2595 pmt_t *pmt = &(priv->pmt[i]);
2596 for(j = 0; j < pmt->es_cnt; j++)
2597 if(pmt->es[j].pid == pid)
2598 return pmt->es[j].type;
2602 return UNKNOWN;
2606 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid)
2608 int32_t pmt_idx, pid_idx, i, j;
2610 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2612 if(pmt_idx != -1)
2614 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2615 if(pid_idx != -1)
2616 return priv->pmt[pmt_idx].es[pid_idx].lang;
2618 else
2620 for(i = 0; i < priv->pmt_cnt; i++)
2622 pmt_t *pmt = &(priv->pmt[i]);
2623 for(j = 0; j < pmt->es_cnt; j++)
2624 if(pmt->es[j].pid == pid)
2625 return pmt->es[j].lang;
2629 return NULL;
2633 static int fill_packet(demuxer_t *demuxer, demux_stream_t *ds, demux_packet_t **dp, int *dp_offset, TS_stream_info *si)
2635 int ret = 0;
2637 if((*dp != NULL) && (*dp_offset > 0))
2639 ret = *dp_offset;
2640 resize_demux_packet(*dp, ret); //shrinked to the right size
2641 ds_add_packet(ds, *dp);
2642 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);
2643 if(si)
2645 float diff = (*dp)->pts - si->last_pts;
2646 float dur;
2648 if(abs(diff) > 1) //1 second, there's a discontinuity
2650 si->duration += si->last_pts - si->first_pts;
2651 si->first_pts = si->last_pts = (*dp)->pts;
2653 else
2655 si->last_pts = (*dp)->pts;
2657 si->size += ret;
2658 dur = si->duration + (si->last_pts - si->first_pts);
2660 if(dur > 0 && ds == demuxer->video)
2662 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2663 if(dur > 1) //otherwise it may be unreliable
2664 priv->vbitrate = (uint32_t) ((float) si->size / dur);
2669 *dp = NULL;
2670 *dp_offset = 0;
2672 return ret;
2675 static int fill_extradata(mp4_decoder_config_t * mp4_dec, ES_stream_t *tss)
2677 uint8_t *tmp;
2679 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4_dec: %p, pid: %d\n", mp4_dec, tss->pid);
2681 if(mp4_dec->buf_size > tss->extradata_alloc)
2683 tmp = (uint8_t *) realloc(tss->extradata, mp4_dec->buf_size);
2684 if(!tmp)
2685 return 0;
2686 tss->extradata = tmp;
2687 tss->extradata_alloc = mp4_dec->buf_size;
2689 memcpy(tss->extradata, mp4_dec->buf, mp4_dec->buf_size);
2690 tss->extradata_len = mp4_dec->buf_size;
2691 mp_msg(MSGT_DEMUX, MSGL_V, "EXTRADATA: %p, alloc=%d, len=%d\n", tss->extradata, tss->extradata_alloc, tss->extradata_len);
2693 return tss->extradata_len;
2696 // 0 = EOF or no stream found
2697 // else = [-] number of bytes written to the packet
2698 static int ts_parse(demuxer_t *demuxer , ES_stream_t *es, unsigned char *packet, int probe)
2700 ES_stream_t *tss;
2701 uint8_t done = 0;
2702 int buf_size, is_start, pid, base;
2703 int len, cc, cc_ok, afc, retv = 0, is_video, is_audio, is_sub;
2704 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2705 stream_t *stream = demuxer->stream;
2706 char *p;
2707 demux_stream_t *ds = NULL;
2708 demux_packet_t **dp = NULL;
2709 int *dp_offset = 0, *buffer_size = 0;
2710 int32_t progid, pid_type, bad, ts_error;
2711 int junk = 0, rap_flag = 0;
2712 pmt_t *pmt;
2713 mp4_decoder_config_t *mp4_dec;
2714 TS_stream_info *si;
2717 while(! done)
2719 bad = ts_error = 0;
2720 ds = (demux_stream_t*) NULL;
2721 dp = (demux_packet_t **) NULL;
2722 dp_offset = buffer_size = NULL;
2723 rap_flag = 0;
2724 mp4_dec = NULL;
2725 es->is_synced = 0;
2726 si = NULL;
2728 junk = priv->ts.packet_size - TS_PACKET_SIZE;
2729 buf_size = priv->ts.packet_size - junk;
2731 if(stream_eof(stream))
2733 if(! probe)
2735 ts_dump_streams(priv);
2736 demuxer->filepos = stream_tell(demuxer->stream);
2739 return 0;
2743 if(! ts_sync(stream))
2745 mp_msg(MSGT_DEMUX, MSGL_INFO, "TS_PARSE: COULDN'T SYNC\n");
2746 return 0;
2749 len = stream_read(stream, &packet[1], 3);
2750 if (len != 3)
2751 return 0;
2752 buf_size -= 4;
2754 if((packet[1] >> 7) & 0x01) //transport error
2755 ts_error = 1;
2758 is_start = packet[1] & 0x40;
2759 pid = ((packet[1] & 0x1f) << 8) | packet[2];
2761 tss = priv->ts.pids[pid]; //an ES stream
2762 if(tss == NULL)
2764 tss = new_pid(priv, pid);
2765 if(tss == NULL)
2766 continue;
2769 cc = (packet[3] & 0xf);
2770 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
2771 tss->last_cc = cc;
2773 bad = ts_error; // || (! cc_ok);
2774 if(bad)
2776 if(priv->keep_broken == 0)
2778 stream_skip(stream, buf_size-1+junk);
2779 continue;
2782 is_start = 0; //queued to the packet data
2785 if(is_start)
2786 tss->is_synced = 1;
2788 if((!is_start && !tss->is_synced) || ((pid > 1) && (pid < 16)) || (pid == 8191)) //invalid pid
2790 stream_skip(stream, buf_size-1+junk);
2791 continue;
2795 afc = (packet[3] >> 4) & 3;
2796 if(! (afc % 2)) //no payload in this TS packet
2798 stream_skip(stream, buf_size-1+junk);
2799 continue;
2802 if(afc > 1)
2804 int c;
2805 c = stream_read_char(stream);
2806 buf_size--;
2807 if(c < 0 || c > 183) //broken from the stream layer or invalid
2809 stream_skip(stream, buf_size-1+junk);
2810 continue;
2813 //c==0 is allowed!
2814 if(c > 0)
2816 uint8_t pcrbuf[188];
2817 int flags = stream_read_char(stream);
2818 int has_pcr;
2819 rap_flag = (flags & 0x40) >> 6;
2820 has_pcr = flags & 0x10;
2822 buf_size--;
2823 c--;
2824 stream_read(stream, pcrbuf, c);
2826 if(has_pcr)
2828 int pcr_pid = prog_pcr_pid(priv, priv->prog);
2829 if(pcr_pid == pid)
2831 uint64_t pcr, pcr_ext;
2833 pcr = (int64_t)(pcrbuf[0]) << 25;
2834 pcr |= pcrbuf[1] << 17 ;
2835 pcr |= (pcrbuf[2]) << 9;
2836 pcr |= pcrbuf[3] << 1 ;
2837 pcr |= (pcrbuf[4] & 0x80) >> 7;
2839 pcr_ext = (pcrbuf[4] & 0x01) << 8;
2840 pcr_ext |= pcrbuf[5];
2842 pcr = pcr * 300 + pcr_ext;
2844 demuxer->reference_clock = (double)pcr/(double)27000000.0;
2848 buf_size -= c;
2849 if(buf_size == 0)
2850 continue;
2854 //find the program that the pid belongs to; if (it's the right one or -1) && pid_type==SL_SECTION
2855 //call parse_sl_section()
2856 pmt = pmt_of_pid(priv, pid, &mp4_dec);
2857 if(mp4_dec)
2859 fill_extradata(mp4_dec, tss);
2860 if(IS_VIDEO(mp4_dec->object_type) || IS_AUDIO(mp4_dec->object_type))
2862 tss->type = SL_PES_STREAM;
2863 tss->subtype = mp4_dec->object_type;
2868 //TABLE PARSING
2870 base = priv->ts.packet_size - buf_size;
2872 priv->last_pid = pid;
2874 is_video = IS_VIDEO(tss->type) || (tss->type==SL_PES_STREAM && IS_VIDEO(tss->subtype));
2875 is_audio = IS_AUDIO(tss->type) || (tss->type==SL_PES_STREAM && IS_AUDIO(tss->subtype)) || (tss->type == PES_PRIVATE1);
2876 is_sub = ((tss->type == SPU_DVD) || (tss->type == SPU_DVB));
2877 pid_type = pid_type_from_pmt(priv, pid);
2879 // PES CONTENT STARTS HERE
2880 if(! probe)
2882 if((is_video || is_audio) && is_start && !priv->ts.streams[pid].sh)
2883 ts_add_stream(demuxer, tss);
2885 if((pid == demuxer->sub->id)) //or the lang is right
2887 pid_type = SPU_DVD;
2890 if(is_video && (demuxer->video->id == priv->ts.streams[pid].id))
2892 ds = demuxer->video;
2894 dp = &priv->fifo[1].pack;
2895 dp_offset = &priv->fifo[1].offset;
2896 buffer_size = &priv->fifo[1].buffer_size;
2897 si = &priv->vstr;
2899 else if(is_audio && (demuxer->audio->id == priv->ts.streams[pid].id))
2901 ds = demuxer->audio;
2903 dp = &priv->fifo[0].pack;
2904 dp_offset = &priv->fifo[0].offset;
2905 buffer_size = &priv->fifo[0].buffer_size;
2906 si = &priv->astr;
2908 else if(is_sub
2909 || (pid_type == SPU_DVD) || (pid_type == SPU_DVB))
2911 //SUBS are infrequent, so the initial detection may fail
2912 // and we may need to add them at play-time
2913 if(demuxer->sub->id == -1)
2915 uint16_t p;
2916 p = progid_for_pid(priv, tss->pid, priv->prog);
2918 if(p == priv->prog)
2920 int asgn = 0;
2921 uint8_t *lang;
2923 if(dvdsub_lang)
2925 if ((lang = pid_lang_from_pmt(priv, pid)))
2926 asgn = (strncmp(lang, dvdsub_lang, 3) == 0);
2928 else //no language specified with -slang
2929 asgn = 1;
2931 if(asgn)
2933 demuxer->sub->id = tss->pid;
2934 mp_msg(MSGT_DEMUX, MSGL_INFO, "CHOSEN SUBs pid 0x%x (%d) FROM PROG %d\n", tss->pid, tss->pid, priv->prog);
2939 if(demuxer->sub->id == tss->pid)
2941 ds = demuxer->sub;
2943 dp = &priv->fifo[2].pack;
2944 dp_offset = &priv->fifo[2].offset;
2945 buffer_size = &priv->fifo[2].buffer_size;
2947 else
2949 stream_skip(stream, buf_size+junk);
2950 continue;
2954 //IS IT TIME TO QUEUE DATA to the dp_packet?
2955 if(is_start && (dp != NULL))
2957 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
2961 if(dp && *dp == NULL)
2963 if(*buffer_size > MAX_PACK_BYTES)
2964 *buffer_size = MAX_PACK_BYTES;
2965 *dp = new_demux_packet(*buffer_size); //es->size
2966 *dp_offset = 0;
2967 if(! *dp)
2969 fprintf(stderr, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", *buffer_size);
2970 continue;
2972 mp_msg(MSGT_DEMUX, MSGL_DBG2, "CREATED DP(%d)\n", *buffer_size);
2977 if(probe || !dp) //dp is NULL for tables and sections
2979 p = &packet[base];
2981 else //feeding
2983 if(*dp_offset + buf_size > *buffer_size)
2985 *buffer_size = *dp_offset + buf_size + TS_FEC_PACKET_SIZE;
2986 resize_demux_packet(*dp, *buffer_size);
2988 p = &((*dp)->buffer[*dp_offset]);
2991 len = stream_read(stream, p, buf_size);
2992 if(len < buf_size)
2994 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\r\nts_parse() couldn't read enough data: %d < %d\r\n", len, buf_size);
2995 continue;
2997 stream_skip(stream, junk);
2999 if(pid == 0)
3001 parse_pat(priv, is_start, p, buf_size);
3002 continue;
3004 else if((tss->type == SL_SECTION) && pmt)
3006 int k, mp4_es_id = -1;
3007 ts_section_t *section;
3008 for(k = 0; k < pmt->mp4es_cnt; k++)
3010 if(pmt->mp4es[k].decoder.object_type == MP4_OD && pmt->mp4es[k].decoder.stream_type == MP4_OD)
3011 mp4_es_id = pmt->mp4es[k].id;
3013 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4ESID: %d\n", mp4_es_id);
3014 for(k = 0; k < pmt->es_cnt; k++)
3016 if(pmt->es[k].mp4_es_id == mp4_es_id)
3018 section = &(tss->section);
3019 parse_sl_section(pmt, section, is_start, &packet[base], buf_size);
3022 continue;
3024 else
3026 progid = prog_id_in_pat(priv, pid);
3027 if(progid != -1)
3029 if(pid != demuxer->video->id && pid != demuxer->audio->id && pid != demuxer->sub->id)
3031 parse_pmt(priv, progid, pid, is_start, &packet[base], buf_size);
3032 continue;
3034 else
3035 mp_msg(MSGT_DEMUX, MSGL_ERR, "Argh! Data pid %d used in the PMT, Skipping PMT parsing!\n", pid);
3039 if(!probe && !dp)
3040 continue;
3042 if(is_start)
3044 uint8_t *lang = NULL;
3046 mp_msg(MSGT_DEMUX, MSGL_DBG2, "IS_START\n");
3048 len = pes_parse2(p, buf_size, es, pid_type, pmt, pid);
3049 if(! len)
3051 tss->is_synced = 0;
3052 continue;
3054 es->pid = tss->pid;
3055 tss->is_synced |= es->is_synced || rap_flag;
3056 tss->payload_size = es->payload_size;
3058 if(is_audio && (lang = pid_lang_from_pmt(priv, es->pid)))
3060 memcpy(es->lang, lang, 3);
3061 es->lang[3] = 0;
3063 else
3064 es->lang[0] = 0;
3066 if(probe)
3068 if(es->type == UNKNOWN)
3069 return 0;
3071 tss->type = es->type;
3072 tss->subtype = es->subtype;
3074 return 1;
3076 else
3078 if(es->pts == 0.0f)
3079 es->pts = tss->pts = tss->last_pts;
3080 else
3081 tss->pts = tss->last_pts = es->pts;
3083 mp_msg(MSGT_DEMUX, MSGL_DBG2, "ts_parse, NEW pid=%d, PSIZE: %u, type=%X, start=%p, len=%d\n",
3084 es->pid, es->payload_size, es->type, es->start, es->size);
3086 demuxer->filepos = stream_tell(demuxer->stream) - es->size;
3088 memmove(p, es->start, es->size);
3089 *dp_offset += es->size;
3090 (*dp)->flags = 0;
3091 (*dp)->pos = stream_tell(demuxer->stream);
3092 (*dp)->pts = es->pts;
3094 if(retv > 0)
3095 return retv;
3096 else
3097 continue;
3100 else
3102 uint16_t sz;
3104 es->pid = tss->pid;
3105 es->type = tss->type;
3106 es->subtype = tss->subtype;
3107 es->pts = tss->pts = tss->last_pts;
3108 es->start = &packet[base];
3111 if(tss->payload_size > 0)
3113 sz = FFMIN(tss->payload_size, buf_size);
3114 tss->payload_size -= sz;
3115 es->size = sz;
3117 else
3119 if(is_video)
3121 sz = es->size = buf_size;
3123 else
3125 continue;
3130 if(! probe)
3132 *dp_offset += sz;
3134 if(*dp_offset >= MAX_PACK_BYTES)
3136 (*dp)->pts = tss->last_pts;
3137 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
3138 return 1;
3141 continue;
3143 else
3145 memcpy(es->start, p, sz);
3147 if(es->size)
3148 return es->size;
3149 else
3150 continue;
3155 return 0;
3159 void skip_audio_frame(sh_audio_t *sh_audio);
3161 static void reset_fifos(demuxer_t *demuxer, int a, int v, int s)
3163 ts_priv_t* priv = demuxer->priv;
3164 if(a)
3166 if(priv->fifo[0].pack != NULL)
3168 free_demux_packet(priv->fifo[0].pack);
3169 priv->fifo[0].pack = NULL;
3171 priv->fifo[0].offset = 0;
3174 if(v)
3176 if(priv->fifo[1].pack != NULL)
3178 free_demux_packet(priv->fifo[1].pack);
3179 priv->fifo[1].pack = NULL;
3181 priv->fifo[1].offset = 0;
3184 if(s)
3186 if(priv->fifo[2].pack != NULL)
3188 free_demux_packet(priv->fifo[2].pack);
3189 priv->fifo[2].pack = NULL;
3191 priv->fifo[2].offset = 0;
3193 demuxer->reference_clock = MP_NOPTS_VALUE;
3197 static void demux_seek_ts(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
3199 demux_stream_t *d_audio=demuxer->audio;
3200 demux_stream_t *d_video=demuxer->video;
3201 sh_audio_t *sh_audio=d_audio->sh;
3202 sh_video_t *sh_video=d_video->sh;
3203 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
3204 int i, video_stats;
3205 off_t newpos;
3207 //================= seek in MPEG-TS ==========================
3209 ts_dump_streams(demuxer->priv);
3210 reset_fifos(demuxer, sh_audio != NULL, sh_video != NULL, demuxer->sub->id > 0);
3212 demux_flush(demuxer);
3216 video_stats = (sh_video != NULL);
3217 if(video_stats)
3219 mp_msg(MSGT_DEMUX, MSGL_V, "IBPS: %d, vb: %d\r\n", sh_video->i_bps, priv->vbitrate);
3220 if(priv->vbitrate)
3221 video_stats = priv->vbitrate;
3222 else
3223 video_stats = sh_video->i_bps;
3226 newpos = (flags & SEEK_ABSOLUTE) ? demuxer->movi_start : demuxer->filepos;
3227 if(flags & SEEK_FACTOR) // float seek 0..1
3228 newpos+=(demuxer->movi_end-demuxer->movi_start)*rel_seek_secs;
3229 else
3231 // time seek (secs)
3232 if(! video_stats) // unspecified or VBR
3233 newpos += 2324*75*rel_seek_secs; // 174.3 kbyte/sec
3234 else
3235 newpos += video_stats*rel_seek_secs;
3239 if(newpos < demuxer->movi_start)
3240 newpos = demuxer->movi_start; //begininng of stream
3242 stream_seek(demuxer->stream, newpos);
3243 for(i = 0; i < 8192; i++)
3244 if(priv->ts.pids[i] != NULL)
3245 priv->ts.pids[i]->is_synced = 0;
3247 videobuf_code_len = 0;
3249 if(sh_video != NULL)
3250 ds_fill_buffer(d_video);
3252 if(sh_audio != NULL)
3254 ds_fill_buffer(d_audio);
3257 while(sh_video != NULL)
3259 if(sh_audio && !d_audio->eof && d_video->pts && d_audio->pts)
3261 float a_pts=d_audio->pts;
3262 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
3263 if(d_video->pts > a_pts)
3265 skip_audio_frame(sh_audio); // sync audio
3266 continue;
3271 i = sync_video_packet(d_video);
3272 if((sh_video->format == VIDEO_MPEG1) || (sh_video->format == VIDEO_MPEG2))
3274 if(i==0x1B3 || i==0x1B8) break; // found it!
3276 else if((sh_video->format == VIDEO_MPEG4) && (i==0x1B6))
3277 break;
3278 else if(sh_video->format == VIDEO_VC1 && (i==0x10E || i==0x10F))
3279 break;
3280 else //H264
3282 if((i & ~0x60) == 0x105 || (i & ~0x60) == 0x107) break;
3285 if(!i || !skip_video_packet(d_video)) break; // EOF?
3290 static int demux_ts_fill_buffer(demuxer_t * demuxer, demux_stream_t *ds)
3292 ES_stream_t es;
3293 ts_priv_t *priv = (ts_priv_t *)demuxer->priv;
3295 return -ts_parse(demuxer, &es, priv->packet, 0);
3299 static int ts_check_file_dmx(demuxer_t *demuxer)
3301 return ts_check_file(demuxer) ? DEMUXER_TYPE_MPEG_TS : 0;
3304 static int is_usable_program(ts_priv_t *priv, pmt_t *pmt)
3306 int j;
3308 for(j = 0; j < pmt->es_cnt; j++)
3310 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3311 continue;
3313 priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO ||
3314 priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO
3316 return 1;
3319 return 0;
3322 static int demux_ts_control(demuxer_t *demuxer, int cmd, void *arg)
3324 ts_priv_t* priv = (ts_priv_t *)demuxer->priv;
3326 switch(cmd)
3328 case DEMUXER_CTRL_SWITCH_AUDIO:
3329 case DEMUXER_CTRL_SWITCH_VIDEO:
3331 void *sh = NULL;
3332 int i, n;
3333 int reftype, areset = 0, vreset = 0;
3334 demux_stream_t *ds;
3336 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
3338 reftype = TYPE_VIDEO;
3339 ds = demuxer->video;
3340 vreset = 1;
3342 else
3344 reftype = TYPE_AUDIO;
3345 ds = demuxer->audio;
3346 areset = 1;
3348 n = *((int*)arg);
3349 if(n == -2)
3351 reset_fifos(demuxer, areset, vreset, 0);
3352 ds->id = -2;
3353 ds->sh = NULL;
3354 ds_free_packs(ds);
3355 *((int*)arg) = ds->id;
3356 return DEMUXER_CTRL_OK;
3359 if(n < 0)
3361 for(i = 0; i < 8192; i++)
3363 if(priv->ts.streams[i].id == ds->id && priv->ts.streams[i].type == reftype)
3364 break;
3367 while(!sh)
3369 i = (i+1) % 8192;
3370 if(priv->ts.streams[i].type == reftype)
3372 if(priv->ts.streams[i].id == ds->id) //we made a complete loop
3373 break;
3374 sh = priv->ts.streams[i].sh;
3378 else //audio track <n>
3380 for(i = 0; i < 8192; i++)
3382 if(priv->ts.streams[i].id == n && priv->ts.streams[i].type == reftype)
3384 sh = priv->ts.streams[i].sh;
3385 break;
3390 if(sh)
3392 if(ds->id != priv->ts.streams[i].id)
3393 reset_fifos(demuxer, areset, vreset, 0);
3394 ds->id = priv->ts.streams[i].id;
3395 ds->sh = sh;
3396 ds_free_packs(ds);
3397 mp_msg(MSGT_DEMUX, MSGL_V, "\r\ndemux_ts, switched to audio pid %d, id: %d, sh: %p\r\n", i, ds->id, sh);
3400 *((int*)arg) = ds->id;
3401 return DEMUXER_CTRL_OK;
3404 case DEMUXER_CTRL_IDENTIFY_PROGRAM: //returns in prog->{aid,vid} the new ids that comprise a program
3406 int i, j, cnt=0;
3407 int vid_done=0, aid_done=0;
3408 pmt_t *pmt = NULL;
3409 demux_program_t *prog = arg;
3411 if(priv->pmt_cnt < 2)
3412 return DEMUXER_CTRL_NOTIMPL;
3414 if(prog->progid == -1)
3416 int cur_pmt_idx = 0;
3418 for(i = 0; i < priv->pmt_cnt; i++)
3419 if(priv->pmt[i].progid == priv->prog)
3421 cur_pmt_idx = i;
3422 break;
3425 i = (cur_pmt_idx + 1) % priv->pmt_cnt;
3426 while(i != cur_pmt_idx)
3428 pmt = &priv->pmt[i];
3429 cnt = is_usable_program(priv, pmt);
3430 if(cnt)
3431 break;
3432 i = (i + 1) % priv->pmt_cnt;
3435 else
3437 for(i = 0; i < priv->pmt_cnt; i++)
3438 if(priv->pmt[i].progid == prog->progid)
3440 pmt = &priv->pmt[i]; //required program
3441 cnt = is_usable_program(priv, pmt);
3445 if(!cnt)
3446 return DEMUXER_CTRL_NOTIMPL;
3448 //finally some food
3449 prog->aid = prog->vid = -2; //no audio and no video by default
3450 for(j = 0; j < pmt->es_cnt; j++)
3452 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3453 continue;
3455 if(!vid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO)
3457 vid_done = 1;
3458 prog->vid = priv->ts.streams[pmt->es[j].pid].id;
3460 else if(!aid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO)
3462 aid_done = 1;
3463 prog->aid = priv->ts.streams[pmt->es[j].pid].id;
3467 priv->prog = prog->progid = pmt->progid;
3468 return DEMUXER_CTRL_OK;
3471 default:
3472 return DEMUXER_CTRL_NOTIMPL;
3477 const demuxer_desc_t demuxer_desc_mpeg_ts = {
3478 "MPEG-TS demuxer",
3479 "mpegts",
3480 "TS",
3481 "Nico Sabbi",
3483 DEMUXER_TYPE_MPEG_TS,
3484 0, // unsafe autodetect
3485 ts_check_file_dmx,
3486 demux_ts_fill_buffer,
3487 demux_open_ts,
3488 demux_close_ts,
3489 demux_seek_ts,
3490 demux_ts_control