typo fix
[mplayer/greg.git] / libmpdemux / demux_ts.c
blob92c5b0e84845bba60ce3b6b5eb84135b5e21e137
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 /* dont forget to change this in cfg-common.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 extern char *dvdsub_lang, *audio_lang; //for -alang
64 typedef enum
66 UNKNOWN = -1,
67 VIDEO_MPEG1 = 0x10000001,
68 VIDEO_MPEG2 = 0x10000002,
69 VIDEO_MPEG4 = 0x10000004,
70 VIDEO_H264 = 0x10000005,
71 VIDEO_AVC = mmioFOURCC('a', 'v', 'c', '1'),
72 VIDEO_VC1 = mmioFOURCC('W', 'V', 'C', '1'),
73 AUDIO_MP2 = 0x50,
74 AUDIO_A52 = 0x2000,
75 AUDIO_DTS = 0x2001,
76 AUDIO_LPCM_BE = 0x10001,
77 AUDIO_AAC = mmioFOURCC('M', 'P', '4', 'A'),
78 SPU_DVD = 0x3000000,
79 SPU_DVB = 0x3000001,
80 PES_PRIVATE1 = 0xBD00000,
81 SL_PES_STREAM = 0xD000000,
82 SL_SECTION = 0xD100000,
83 MP4_OD = 0xD200000,
84 } es_stream_type_t;
86 typedef struct {
87 uint8_t *buffer;
88 uint16_t buffer_len;
89 } ts_section_t;
91 typedef struct {
92 int size;
93 unsigned char *start;
94 uint16_t payload_size;
95 es_stream_type_t type, subtype;
96 float pts, last_pts;
97 int pid;
98 char lang[4];
99 int last_cc; // last cc code (-1 if first packet)
100 int is_synced;
101 ts_section_t section;
102 uint8_t *extradata;
103 int extradata_alloc, extradata_len;
104 struct {
105 uint8_t au_start, au_end, last_au_end;
106 } sl;
107 } ES_stream_t;
109 typedef struct {
110 void *sh;
111 int id;
112 int type;
113 } sh_av_t;
115 typedef struct MpegTSContext {
116 int packet_size; // raw packet size, including FEC if present e.g. 188 bytes
117 ES_stream_t *pids[NB_PID_MAX];
118 sh_av_t streams[NB_PID_MAX];
119 } MpegTSContext;
122 typedef struct {
123 demux_stream_t *ds;
124 demux_packet_t *pack;
125 int offset, buffer_size;
126 } av_fifo_t;
128 #define MAX_EXTRADATA_SIZE 64*1024
129 typedef struct {
130 int32_t object_type; //aka codec used
131 int32_t stream_type; //video, audio etc.
132 uint8_t buf[MAX_EXTRADATA_SIZE];
133 uint16_t buf_size;
134 uint8_t szm1;
135 } mp4_decoder_config_t;
137 typedef struct {
138 //flags
139 uint8_t flags;
140 uint8_t au_start;
141 uint8_t au_end;
142 uint8_t random_accesspoint;
143 uint8_t random_accesspoint_only;
144 uint8_t padding;
145 uint8_t use_ts;
146 uint8_t idle;
147 uint8_t duration;
149 uint32_t ts_resolution, ocr_resolution;
150 uint8_t ts_len, ocr_len, au_len, instant_bitrate_len, degr_len, au_seqnum_len, packet_seqnum_len;
151 uint32_t timescale;
152 uint16_t au_duration, cts_duration;
153 uint64_t ocr, dts, cts;
154 } mp4_sl_config_t;
156 typedef struct {
157 uint16_t id;
158 uint8_t flags;
159 mp4_decoder_config_t decoder;
160 mp4_sl_config_t sl;
161 } mp4_es_descr_t;
163 typedef struct {
164 uint16_t id;
165 uint8_t flags;
166 mp4_es_descr_t *es;
167 uint16_t es_cnt;
168 } mp4_od_t;
170 typedef struct {
171 uint8_t skip;
172 uint8_t table_id;
173 uint8_t ssi;
174 uint16_t section_length;
175 uint16_t ts_id;
176 uint8_t version_number;
177 uint8_t curr_next;
178 uint8_t section_number;
179 uint8_t last_section_number;
180 struct pat_progs_t {
181 uint16_t id;
182 uint16_t pmt_pid;
183 } *progs;
184 uint16_t progs_cnt;
185 ts_section_t section;
186 } pat_t;
188 typedef struct {
189 uint16_t progid;
190 uint8_t skip;
191 uint8_t table_id;
192 uint8_t ssi;
193 uint16_t section_length;
194 uint8_t version_number;
195 uint8_t curr_next;
196 uint8_t section_number;
197 uint8_t last_section_number;
198 uint16_t PCR_PID;
199 uint16_t prog_descr_length;
200 ts_section_t section;
201 uint16_t es_cnt;
202 struct pmt_es_t {
203 uint16_t pid;
204 uint32_t type; //it's 8 bit long, but cast to the right type as FOURCC
205 uint16_t descr_length;
206 uint8_t format_descriptor[5];
207 uint8_t lang[4];
208 uint16_t mp4_es_id;
209 } *es;
210 mp4_od_t iod, *od;
211 mp4_es_descr_t *mp4es;
212 int od_cnt, mp4es_cnt;
213 } pmt_t;
215 typedef struct {
216 uint64_t size;
217 float duration;
218 float first_pts;
219 float last_pts;
220 } TS_stream_info;
222 typedef struct {
223 MpegTSContext ts;
224 int last_pid;
225 av_fifo_t fifo[3]; //0 for audio, 1 for video, 2 for subs
226 pat_t pat;
227 pmt_t *pmt;
228 uint16_t pmt_cnt;
229 uint32_t prog;
230 uint32_t vbitrate;
231 int keep_broken;
232 int last_aid;
233 int last_vid;
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) || ((x) == VIDEO_VC1))
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;
286 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h);
288 static void ts_add_stream(demuxer_t * demuxer, ES_stream_t *es)
290 int i;
291 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
293 if(priv->ts.streams[es->pid].sh)
294 return;
296 if((IS_AUDIO(es->type) || IS_AUDIO(es->subtype)) && priv->last_aid+1 < MAX_A_STREAMS)
298 sh_audio_t *sh = new_sh_audio_aid(demuxer, priv->last_aid, es->pid);
299 if(sh)
301 sh->format = IS_AUDIO(es->type) ? es->type : es->subtype;
302 sh->ds = demuxer->audio;
304 priv->ts.streams[es->pid].id = priv->last_aid;
305 priv->ts.streams[es->pid].sh = sh;
306 priv->ts.streams[es->pid].type = TYPE_AUDIO;
307 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);
308 priv->last_aid++;
311 if(es->extradata && es->extradata_len)
313 sh->wf = (WAVEFORMATEX *) malloc(sizeof (WAVEFORMATEX) + es->extradata_len);
314 sh->wf->cbSize = es->extradata_len;
315 memcpy(sh->wf + 1, es->extradata, es->extradata_len);
319 if((IS_VIDEO(es->type) || IS_VIDEO(es->subtype)) && priv->last_vid+1 < MAX_V_STREAMS)
321 sh_video_t *sh = new_sh_video_vid(demuxer, priv->last_vid, es->pid);
322 if(sh)
324 sh->format = IS_VIDEO(es->type) ? es->type : es->subtype;;
325 sh->ds = demuxer->video;
327 priv->ts.streams[es->pid].id = priv->last_vid;
328 priv->ts.streams[es->pid].sh = sh;
329 priv->ts.streams[es->pid].type = TYPE_VIDEO;
330 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);
331 priv->last_vid++;
334 if(sh->format == VIDEO_AVC && es->extradata && es->extradata_len)
336 int w = 0, h = 0;
337 sh->bih = (BITMAPINFOHEADER *) calloc(1, sizeof(BITMAPINFOHEADER) + es->extradata_len);
338 sh->bih->biSize= sizeof(BITMAPINFOHEADER) + es->extradata_len;
339 sh->bih->biCompression = sh->format;
340 memcpy(sh->bih + 1, es->extradata, es->extradata_len);
341 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "EXTRADATA(%d BYTES): \n", es->extradata_len);
342 for(i = 0;i < es->extradata_len; i++)
343 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "%02x ", (int) es->extradata[i]);
344 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"\n");
345 if(parse_avc_sps(es->extradata, es->extradata_len, &w, &h))
347 sh->bih->biWidth = w;
348 sh->bih->biHeight = h;
355 static int ts_check_file(demuxer_t * demuxer)
357 const int buf_size = (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS);
358 unsigned char buf[TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS], done = 0, *ptr;
359 uint32_t _read, i, count = 0, is_ts;
360 int cc[NB_PID_MAX], last_cc[NB_PID_MAX], pid, cc_ok, c, good, bad;
361 uint8_t size = 0;
362 off_t pos = 0;
363 off_t init_pos;
365 mp_msg(MSGT_DEMUX, MSGL_V, "Checking for MPEG-TS...\n");
367 init_pos = stream_tell(demuxer->stream);
368 is_ts = 0;
369 while(! done)
371 i = 1;
372 c = 0;
374 while(((c=stream_read_char(demuxer->stream)) != 0x47)
375 && (c >= 0)
376 && (i < MAX_CHECK_SIZE)
377 && ! demuxer->stream->eof
378 ) i++;
381 if(c != 0x47)
383 mp_msg(MSGT_DEMUX, MSGL_V, "THIS DOESN'T LOOK LIKE AN MPEG-TS FILE!\n");
384 is_ts = 0;
385 done = 1;
386 continue;
389 pos = stream_tell(demuxer->stream) - 1;
390 buf[0] = c;
391 _read = stream_read(demuxer->stream, &buf[1], buf_size-1);
393 if(_read < buf_size-1)
395 mp_msg(MSGT_DEMUX, MSGL_V, "COULDN'T READ ENOUGH DATA, EXITING TS_CHECK\n");
396 stream_reset(demuxer->stream);
397 return 0;
400 size = get_packet_size(buf, buf_size);
401 if(size)
403 done = 1;
404 is_ts = 1;
407 if(pos - init_pos >= MAX_CHECK_SIZE)
409 done = 1;
410 is_ts = 0;
414 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);
415 stream_seek(demuxer->stream, pos);
417 if(! is_ts)
418 return 0;
420 //LET'S CHECK continuity counters
421 good = bad = 0;
422 for(count = 0; count < NB_PID_MAX; count++)
424 cc[count] = last_cc[count] = -1;
427 for(count = 0; count < NUM_CONSECUTIVE_TS_PACKETS; count++)
429 ptr = &(buf[size * count]);
430 pid = ((ptr[1] & 0x1f) << 8) | ptr[2];
431 mp_msg(MSGT_DEMUX, MSGL_DBG2, "BUF: %02x %02x %02x %02x, PID %d, SIZE: %d \n",
432 ptr[0], ptr[1], ptr[2], ptr[3], pid, size);
434 if((pid == 8191) || (pid < 16))
435 continue;
437 cc[pid] = (ptr[3] & 0xf);
438 cc_ok = (last_cc[pid] < 0) || ((((last_cc[pid] + 1) & 0x0f) == cc[pid]));
439 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PID %d, COMPARE CC %d AND LAST_CC %d\n", pid, cc[pid], last_cc[pid]);
440 if(! cc_ok)
441 //return 0;
442 bad++;
443 else
444 good++;
446 last_cc[pid] = cc[pid];
449 mp_msg(MSGT_DEMUX, MSGL_V, "GOOD CC: %d, BAD CC: %d\n", good, bad);
451 if(good >= bad)
452 return size;
453 else
454 return 0;
458 static inline int32_t progid_idx_in_pmt(ts_priv_t *priv, uint16_t progid)
460 int x;
462 if(priv->pmt == NULL)
463 return -1;
465 for(x = 0; x < priv->pmt_cnt; x++)
467 if(priv->pmt[x].progid == progid)
468 return x;
471 return -1;
475 static inline int32_t progid_for_pid(ts_priv_t *priv, int pid, int32_t req) //finds the first program listing a pid
477 int i, j;
478 pmt_t *pmt;
481 if(priv->pmt == NULL)
482 return -1;
485 for(i=0; i < priv->pmt_cnt; i++)
487 pmt = &(priv->pmt[i]);
489 if(pmt->es == NULL)
490 return -1;
492 for(j = 0; j < pmt->es_cnt; j++)
494 if(pmt->es[j].pid == pid)
496 if((req == 0) || (req == pmt->progid))
497 return pmt->progid;
502 return -1;
506 static inline int pid_match_lang(ts_priv_t *priv, uint16_t pid, char *lang)
508 uint16_t i, j;
509 pmt_t *pmt;
511 if(priv->pmt == NULL)
512 return -1;
514 for(i=0; i < priv->pmt_cnt; i++)
516 pmt = &(priv->pmt[i]);
518 if(pmt->es == NULL)
519 return -1;
521 for(j = 0; j < pmt->es_cnt; j++)
523 if(pmt->es[j].pid != pid)
524 continue;
526 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);
527 if(strncmp(pmt->es[j].lang, lang, 3) == 0)
529 return 1;
535 return -1;
538 typedef struct {
539 int32_t atype, vtype, stype; //types
540 int32_t apid, vpid, spid; //stream ids
541 char slang[4], alang[4]; //languages
542 uint16_t prog;
543 off_t probe;
544 } tsdemux_init_t;
546 //stripped down version of a52_syncinfo() from liba52
547 //copyright belongs to Michel Lespinasse <walken@zoy.org> and Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
548 int mp_a52_framesize(uint8_t * buf, int *srate)
550 int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
551 128, 160, 192, 224, 256, 320, 384, 448,
552 512, 576, 640
554 uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
555 int frmsizecod, bitrate, half;
557 if((buf[0] != 0x0b) || (buf[1] != 0x77)) /* syncword */
558 return 0;
560 if(buf[5] >= 0x60) /* bsid >= 12 */
561 return 0;
563 half = halfrate[buf[5] >> 3];
565 frmsizecod = buf[4] & 63;
566 if(frmsizecod >= 38)
567 return 0;
569 bitrate = rate[frmsizecod >> 1];
571 switch(buf[4] & 0xc0)
573 case 0: /* 48 KHz */
574 *srate = 48000 >> half;
575 return 4 * bitrate;
576 case 0x40: /* 44.1 KHz */
577 *srate = 44100 >> half;
578 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
579 case 0x80: /* 32 KHz */
580 *srate = 32000 >> half;
581 return 6 * bitrate;
584 return 0;
587 //second stage: returns the count of A52 syncwords found
588 static int a52_check(char *buf, int len)
590 int cnt, frame_length, ok, srate;
592 cnt = ok = 0;
593 if(len < 8)
594 return 0;
596 while(cnt < len - 7)
598 if(buf[cnt] == 0x0B && buf[cnt+1] == 0x77)
600 frame_length = mp_a52_framesize(&buf[cnt], &srate);
601 if(frame_length>=7 && frame_length<=3840)
603 cnt += frame_length;
604 ok++;
606 else
607 cnt++;
609 else
610 cnt++;
613 mp_msg(MSGT_DEMUXER, MSGL_V, "A52_CHECK(%d input bytes), found %d frame syncwords of %d bytes length\n", len, ok, frame_length);
614 return ok;
618 static off_t ts_detect_streams(demuxer_t *demuxer, tsdemux_init_t *param)
620 int video_found = 0, audio_found = 0, sub_found = 0, i, num_packets = 0, req_apid, req_vpid, req_spid;
621 int is_audio, is_video, is_sub, has_tables;
622 int32_t p, chosen_pid = 0;
623 off_t pos=0, ret = 0, init_pos, end_pos;
624 ES_stream_t es;
625 unsigned char tmp[TS_FEC_PACKET_SIZE];
626 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
627 struct {
628 char *buf;
629 int pos;
630 } pes_priv1[8192], *pptr;
631 char *tmpbuf;
633 priv->last_pid = 8192; //invalid pid
635 req_apid = param->apid;
636 req_vpid = param->vpid;
637 req_spid = param->spid;
639 has_tables = 0;
640 memset(pes_priv1, 0, sizeof(pes_priv1));
641 init_pos = stream_tell(demuxer->stream);
642 mp_msg(MSGT_DEMUXER, MSGL_V, "PROBING UP TO %"PRIu64", PROG: %d\n", (uint64_t) param->probe, param->prog);
643 end_pos = init_pos + (param->probe ? param->probe : TS_MAX_PROBE_SIZE);
644 while(1)
646 pos = stream_tell(demuxer->stream);
647 if(pos > end_pos || demuxer->stream->eof)
648 break;
650 if(ts_parse(demuxer, &es, tmp, 1))
652 //Non PES-aligned A52 audio may escape detection if PMT is not present;
653 //in this case we try to find at least 3 A52 syncwords
654 if((es.type == PES_PRIVATE1) && (! audio_found) && req_apid > -2)
656 pptr = &pes_priv1[es.pid];
657 if(pptr->pos < 64*1024)
659 tmpbuf = (char*) realloc(pptr->buf, pptr->pos + es.size);
660 if(tmpbuf != NULL)
662 pptr->buf = tmpbuf;
663 memcpy(&(pptr->buf[ pptr->pos ]), es.start, es.size);
664 pptr->pos += es.size;
665 if(a52_check(pptr->buf, pptr->pos) > 2)
667 param->atype = AUDIO_A52;
668 param->apid = es.pid;
669 es.type = AUDIO_A52;
675 is_audio = IS_AUDIO(es.type) || ((es.type==SL_PES_STREAM) && IS_AUDIO(es.subtype));
676 is_video = IS_VIDEO(es.type) || ((es.type==SL_PES_STREAM) && IS_VIDEO(es.subtype));
677 is_sub = ((es.type == SPU_DVD) || (es.type == SPU_DVB));
680 if((! is_audio) && (! is_video) && (! is_sub))
681 continue;
682 if(is_audio && req_apid==-2)
683 continue;
685 if(is_video)
687 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_VIDEO_ID=%d\n", es.pid);
688 chosen_pid = (req_vpid == es.pid);
689 if((! chosen_pid) && (req_vpid > 0))
690 continue;
692 else if(is_audio)
694 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_AUDIO_ID=%d\n", es.pid);
695 if (es.lang[0] > 0)
696 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_AID_%d_LANG=%s\n", es.pid, es.lang);
697 if(req_apid > 0)
699 chosen_pid = (req_apid == es.pid);
700 if(! chosen_pid)
701 continue;
703 else if(param->alang[0] > 0 && es.lang[0] > 0)
705 if(pid_match_lang(priv, es.pid, param->alang) == -1)
706 continue;
708 chosen_pid = 1;
709 param->apid = req_apid = es.pid;
712 else if(is_sub)
714 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_SUBTITLE_ID=%d\n", es.pid);
715 if (es.lang[0] > 0)
716 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_SID_%d_LANG=%s\n", es.pid, es.lang);
717 chosen_pid = (req_spid == es.pid);
718 if((! chosen_pid) && (req_spid > 0))
719 continue;
722 if(req_apid < 0 && (param->alang[0] == 0) && req_vpid < 0 && req_spid < 0)
723 chosen_pid = 1;
725 if((ret == 0) && chosen_pid)
727 ret = stream_tell(demuxer->stream);
730 p = progid_for_pid(priv, es.pid, param->prog);
731 if(p != -1)
732 has_tables++;
734 if((param->prog == 0) && (p != -1))
736 if(chosen_pid)
737 param->prog = p;
740 if((param->prog > 0) && (param->prog != p))
742 if(audio_found)
744 if(is_video && (req_vpid == es.pid))
746 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
747 param->vpid = es.pid;
748 video_found = 1;
749 break;
753 if(video_found)
755 if(is_audio && (req_apid == es.pid))
757 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
758 param->apid = es.pid;
759 audio_found = 1;
760 break;
765 continue;
769 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "TYPE: %x, PID: %d, PROG FOUND: %d\n", es.type, es.pid, param->prog);
772 if(is_video)
774 if((req_vpid == -1) || (req_vpid == es.pid))
776 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
777 param->vpid = es.pid;
778 video_found = 1;
783 if(((req_vpid == -2) || (num_packets >= NUM_CONSECUTIVE_AUDIO_PACKETS)) && audio_found && !param->probe)
785 //novideo or we have at least 348 audio packets (64 KB) without video (TS with audio only)
786 param->vtype = 0;
787 break;
790 if(is_sub)
792 if((req_spid == -1) || (req_spid == es.pid))
794 param->stype = es.type;
795 param->spid = es.pid;
796 sub_found = 1;
800 if(is_audio)
802 if((req_apid == -1) || (req_apid == es.pid))
804 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
805 param->apid = es.pid;
806 audio_found = 1;
810 if(audio_found && (param->apid == es.pid) && (! video_found))
811 num_packets++;
813 if((has_tables==0) && (video_found && audio_found) && (pos >= 1000000))
814 break;
818 for(i=0; i<8192; i++)
820 if(pes_priv1[i].buf != NULL)
822 free(pes_priv1[i].buf);
823 pes_priv1[i].buf = NULL;
824 pes_priv1[i].pos = 0;
828 if(video_found)
830 if(param->vtype == VIDEO_MPEG1)
831 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG1(pid=%d) ", param->vpid);
832 else if(param->vtype == VIDEO_MPEG2)
833 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG2(pid=%d) ", param->vpid);
834 else if(param->vtype == VIDEO_MPEG4)
835 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG4(pid=%d) ", param->vpid);
836 else if(param->vtype == VIDEO_H264)
837 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO H264(pid=%d) ", param->vpid);
838 else if(param->vtype == VIDEO_VC1)
839 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO VC1(pid=%d) ", param->vpid);
840 else if(param->vtype == VIDEO_AVC)
841 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO AVC(NAL-H264, pid=%d) ", param->vpid);
843 else
845 param->vtype = UNKNOWN;
846 //WE DIDN'T MATCH ANY VIDEO STREAM
847 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO VIDEO! ");
850 if(param->atype == AUDIO_MP2)
851 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO MPA(pid=%d)", param->apid);
852 else if(param->atype == AUDIO_A52)
853 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO A52(pid=%d)", param->apid);
854 else if(param->atype == AUDIO_DTS)
855 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO DTS(pid=%d)", param->apid);
856 else if(param->atype == AUDIO_LPCM_BE)
857 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO LPCM(pid=%d)", param->apid);
858 else if(param->atype == AUDIO_AAC)
859 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO AAC(pid=%d)", param->apid);
860 else
862 audio_found = 0;
863 param->atype = UNKNOWN;
864 //WE DIDN'T MATCH ANY AUDIO STREAM, SO WE FORCE THE DEMUXER TO IGNORE AUDIO
865 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO AUDIO! ");
868 if(param->stype == SPU_DVD || param->stype == SPU_DVB)
869 mp_msg(MSGT_DEMUXER, MSGL_INFO, " SUB %s(pid=%d) ", (param->stype==SPU_DVD ? "DVD" : "DVB"), param->spid);
870 else
872 param->stype = UNKNOWN;
873 mp_msg(MSGT_DEMUXER, MSGL_INFO, " NO SUBS (yet)! ");
876 if(video_found || audio_found)
878 if(demuxer->stream->eof && (ret == 0))
879 ret = init_pos;
880 mp_msg(MSGT_DEMUXER, MSGL_INFO, " PROGRAM N. %d\n", param->prog);
882 else
883 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\n");
886 for(i=0; i<8192; i++)
888 if(priv->ts.pids[i] != NULL)
890 priv->ts.pids[i]->payload_size = 0;
891 priv->ts.pids[i]->pts = priv->ts.pids[i]->last_pts = 0;
892 priv->ts.pids[i]->last_cc = -1;
893 priv->ts.pids[i]->is_synced = 0;
897 return ret;
900 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h)
902 int sps, sps_len;
903 unsigned char *ptr;
904 mp_mpeg_header_t picture;
905 if(len < 6)
906 return 0;
907 sps = buf[5] & 0x1f;
908 if(!sps)
909 return 0;
910 sps_len = (buf[6] << 8) | buf[7];
911 if(!sps_len || (sps_len > len - 8))
912 return 0;
913 ptr = &(buf[8]);
914 picture.display_picture_width = picture.display_picture_height = 0;
915 h264_parse_sps(&picture, ptr, len - 8);
916 if(!picture.display_picture_width || !picture.display_picture_height)
917 return 0;
918 *w = picture.display_picture_width;
919 *h = picture.display_picture_height;
920 return 1;
923 static demuxer_t *demux_open_ts(demuxer_t * demuxer)
925 int i;
926 uint8_t packet_size;
927 sh_video_t *sh_video;
928 sh_audio_t *sh_audio;
929 off_t start_pos;
930 tsdemux_init_t params;
931 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
933 mp_msg(MSGT_DEMUX, MSGL_V, "DEMUX OPEN, AUDIO_ID: %d, VIDEO_ID: %d, SUBTITLE_ID: %d,\n",
934 demuxer->audio->id, demuxer->video->id, demuxer->sub->id);
937 demuxer->type= DEMUXER_TYPE_MPEG_TS;
940 stream_reset(demuxer->stream);
942 packet_size = ts_check_file(demuxer);
943 if(!packet_size)
944 return NULL;
946 priv = calloc(1, sizeof(ts_priv_t));
947 if(priv == NULL)
949 mp_msg(MSGT_DEMUX, MSGL_FATAL, "DEMUX_OPEN_TS, couldn't allocate enough memory for ts->priv, exit\n");
950 return NULL;
953 for(i=0; i < 8192; i++)
955 priv->ts.pids[i] = NULL;
956 priv->ts.streams[i].id = -3;
958 priv->pat.progs = NULL;
959 priv->pat.progs_cnt = 0;
960 priv->pat.section.buffer = NULL;
961 priv->pat.section.buffer_len = 0;
963 priv->pmt = NULL;
964 priv->pmt_cnt = 0;
966 priv->keep_broken = ts_keep_broken;
967 priv->ts.packet_size = packet_size;
970 demuxer->priv = priv;
971 if(demuxer->stream->type != STREAMTYPE_FILE)
972 demuxer->seekable = 1;
973 else
974 demuxer->seekable = 1;
977 params.atype = params.vtype = params.stype = UNKNOWN;
978 params.apid = demuxer->audio->id;
979 params.vpid = demuxer->video->id;
980 params.spid = demuxer->sub->id;
981 params.prog = ts_prog;
982 params.probe = ts_probe;
984 if(dvdsub_lang != NULL)
986 strncpy(params.slang, dvdsub_lang, 3);
987 params.slang[3] = 0;
989 else
990 memset(params.slang, 0, 4);
992 if(audio_lang != NULL)
994 strncpy(params.alang, audio_lang, 3);
995 params.alang[3] = 0;
997 else
998 memset(params.alang, 0, 4);
1000 start_pos = ts_detect_streams(demuxer, &params);
1002 demuxer->sub->id = params.spid;
1003 priv->prog = params.prog;
1005 if(params.vtype != UNKNOWN)
1007 ts_add_stream(demuxer, priv->ts.pids[params.vpid]);
1008 sh_video = priv->ts.streams[params.vpid].sh;
1009 demuxer->video->id = priv->ts.streams[params.vpid].id;
1010 sh_video->ds = demuxer->video;
1011 sh_video->format = params.vtype;
1012 demuxer->video->sh = sh_video;
1015 if(params.atype != UNKNOWN)
1017 ES_stream_t *es = priv->ts.pids[params.apid];
1019 if(!IS_AUDIO(es->type) && !IS_AUDIO(es->subtype) && IS_AUDIO(params.atype)) es->subtype = params.atype;
1020 ts_add_stream(demuxer, priv->ts.pids[params.apid]);
1021 sh_audio = priv->ts.streams[params.apid].sh;
1022 demuxer->audio->id = priv->ts.streams[params.apid].id;
1023 sh_audio->ds = demuxer->audio;
1024 sh_audio->format = params.atype;
1025 demuxer->audio->sh = sh_audio;
1029 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);
1032 start_pos = (start_pos <= priv->ts.packet_size ? 0 : start_pos - priv->ts.packet_size);
1033 demuxer->movi_start = start_pos;
1034 stream_reset(demuxer->stream);
1035 stream_seek(demuxer->stream, start_pos); //IF IT'S FROM A PIPE IT WILL FAIL, BUT WHO CARES?
1038 priv->last_pid = 8192; //invalid pid
1040 for(i = 0; i < 3; i++)
1042 priv->fifo[i].pack = NULL;
1043 priv->fifo[i].offset = 0;
1045 priv->fifo[0].ds = demuxer->audio;
1046 priv->fifo[1].ds = demuxer->video;
1047 priv->fifo[2].ds = demuxer->sub;
1049 priv->fifo[0].buffer_size = 1536;
1050 priv->fifo[1].buffer_size = 32767;
1051 priv->fifo[2].buffer_size = 32767;
1053 priv->pat.section.buffer_len = 0;
1054 for(i = 0; i < priv->pmt_cnt; i++)
1055 priv->pmt[i].section.buffer_len = 0;
1057 demuxer->filepos = stream_tell(demuxer->stream);
1058 return demuxer;
1061 static void demux_close_ts(demuxer_t * demuxer)
1063 uint16_t i;
1064 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
1066 if(priv)
1068 if(priv->pat.section.buffer)
1069 free(priv->pat.section.buffer);
1070 if(priv->pat.progs)
1071 free(priv->pat.progs);
1073 if(priv->pmt)
1075 for(i = 0; i < priv->pmt_cnt; i++)
1077 if(priv->pmt[i].section.buffer)
1078 free(priv->pmt[i].section.buffer);
1079 if(priv->pmt[i].es)
1080 free(priv->pmt[i].es);
1082 free(priv->pmt);
1084 free(priv);
1086 demuxer->priv=NULL;
1090 extern unsigned char mp_getbits(unsigned char*, unsigned int, unsigned char);
1091 #define getbits mp_getbits
1093 static int mp4_parse_sl_packet(pmt_t *pmt, uint8_t *buf, uint16_t packet_len, int pid, ES_stream_t *pes_es)
1095 int i, n, m, mp4_es_id = -1;
1096 uint64_t v = 0;
1097 uint32_t pl_size = 0;
1098 int deg_flag = 0;
1099 mp4_es_descr_t *es = NULL;
1100 mp4_sl_config_t *sl = NULL;
1101 uint8_t au_start = 0, au_end = 0, rap_flag = 0, ocr_flag = 0, padding = 0, padding_bits = 0, idle = 0;
1103 pes_es->is_synced = 0;
1104 mp_msg(MSGT_DEMUXER,MSGL_V, "mp4_parse_sl_packet, pid: %d, pmt: %pm, packet_len: %d\n", pid, pmt, packet_len);
1105 if(! pmt || !packet_len)
1106 return 0;
1108 for(i = 0; i < pmt->es_cnt; i++)
1110 if(pmt->es[i].pid == pid)
1111 mp4_es_id = pmt->es[i].mp4_es_id;
1113 if(mp4_es_id < 0)
1114 return -1;
1116 for(i = 0; i < pmt->mp4es_cnt; i++)
1118 if(pmt->mp4es[i].id == mp4_es_id)
1119 es = &(pmt->mp4es[i]);
1121 if(! es)
1122 return -1;
1124 pes_es->subtype = es->decoder.object_type;
1126 sl = &(es->sl);
1127 if(!sl)
1128 return -1;
1130 //now es is the complete es_descriptor of out mp4 ES stream
1131 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "ID: %d, FLAGS: 0x%x, subtype: %x\n", es->id, sl->flags, pes_es->subtype);
1133 n = 0;
1134 if(sl->au_start)
1135 pes_es->sl.au_start = au_start = getbits(buf, n++, 1);
1136 else
1137 pes_es->sl.au_start = (pes_es->sl.last_au_end ? 1 : 0);
1138 if(sl->au_end)
1139 pes_es->sl.au_end = au_end = getbits(buf, n++, 1);
1141 if(!sl->au_start && !sl->au_end)
1143 pes_es->sl.au_start = pes_es->sl.au_end = au_start = au_end = 1;
1145 pes_es->sl.last_au_end = pes_es->sl.au_end;
1148 if(sl->ocr_len > 0)
1149 ocr_flag = getbits(buf, n++, 1);
1150 if(sl->idle)
1151 idle = getbits(buf, n++, 1);
1152 if(sl->padding)
1153 padding = getbits(buf, n++, 1);
1154 if(padding)
1156 padding_bits = getbits(buf, n, 3);
1157 n += 3;
1160 if(idle || (padding && !padding_bits))
1162 pes_es->payload_size = 0;
1163 return -1;
1166 //(! idle && (!padding || padding_bits != 0)) is true
1167 n += sl->packet_seqnum_len;
1168 if(sl->degr_len)
1169 deg_flag = getbits(buf, n++, 1);
1170 if(deg_flag)
1171 n += sl->degr_len;
1173 if(ocr_flag)
1175 n += sl->ocr_len;
1176 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "OCR: %d bits\n", sl->ocr_len);
1179 if(packet_len * 8 <= n)
1180 return -1;
1182 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "\nAU_START: %d, AU_END: %d\n", au_start, au_end);
1183 if(au_start)
1185 int dts_flag = 0, cts_flag = 0, ib_flag = 0;
1187 if(sl->random_accesspoint)
1188 rap_flag = getbits(buf, n++, 1);
1190 //check commented because it seems it's rarely used, and we need this flag set in case of au_start
1191 //the decoder will eventually discard the payload if it can't decode it
1192 //if(rap_flag || sl->random_accesspoint_only)
1193 pes_es->is_synced = 1;
1195 n += sl->au_seqnum_len;
1196 if(packet_len * 8 <= n+8)
1197 return -1;
1198 if(sl->use_ts)
1200 dts_flag = getbits(buf, n++, 1);
1201 cts_flag = getbits(buf, n++, 1);
1203 if(sl->instant_bitrate_len)
1204 ib_flag = getbits(buf, n++, 1);
1205 if(packet_len * 8 <= n+8)
1206 return -1;
1207 if(dts_flag && (sl->ts_len > 0))
1209 n += sl->ts_len;
1210 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "DTS: %d bits\n", sl->ts_len);
1212 if(packet_len * 8 <= n+8)
1213 return -1;
1214 if(cts_flag && (sl->ts_len > 0))
1216 int i = 0, m;
1218 while(i < sl->ts_len)
1220 m = FFMIN(8, sl->ts_len - i);
1221 v |= getbits(buf, n, m);
1222 if(sl->ts_len - i > 8)
1223 v <<= 8;
1224 i += m;
1225 n += m;
1226 if(packet_len * 8 <= n+8)
1227 return -1;
1230 pes_es->pts = (float) v / (float) sl->ts_resolution;
1231 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "CTS: %d bits, value: %"PRIu64"/%d = %.3f\n", sl->ts_len, v, sl->ts_resolution, pes_es->pts);
1235 i = 0;
1236 pl_size = 0;
1237 while(i < sl->au_len)
1239 m = FFMIN(8, sl->au_len - i);
1240 pl_size |= getbits(buf, n, m);
1241 if(sl->au_len - i > 8)
1242 pl_size <<= 8;
1243 i += m;
1244 n += m;
1245 if(packet_len * 8 <= n+8)
1246 return -1;
1248 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "AU_LEN: %u (%d bits)\n", pl_size, sl->au_len);
1249 if(ib_flag)
1250 n += sl->instant_bitrate_len;
1253 m = (n+7)/8;
1254 if(0 < pl_size && pl_size < pes_es->payload_size)
1255 pes_es->payload_size = pl_size;
1257 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",
1258 n, m, pes_es->payload_size, pl_size, (int) rap_flag, (int) sl->random_accesspoint_only);
1260 return m;
1263 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)
1265 unsigned char *p;
1266 uint32_t header_len;
1267 int64_t pts;
1268 uint32_t stream_id;
1269 uint32_t pkt_len, pes_is_aligned;
1271 //Here we are always at the start of a PES packet
1272 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2(%p, %d): \n", buf, (uint32_t) packet_len);
1274 if(packet_len == 0 || packet_len > 184)
1276 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2, BUFFER LEN IS TOO SMALL OR TOO BIG: %d EXIT\n", packet_len);
1277 return 0;
1280 p = buf;
1281 pkt_len = packet_len;
1284 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: HEADER %02x %02x %02x %02x\n", p[0], p[1], p[2], p[3]);
1285 if (p[0] || p[1] || (p[2] != 1))
1287 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: error HEADER %02x %02x %02x (should be 0x000001) \n", p[0], p[1], p[2]);
1288 return 0 ;
1291 packet_len -= 6;
1292 if(packet_len==0)
1294 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: packet too short: %d, exit\n", packet_len);
1295 return 0;
1298 es->payload_size = (p[4] << 8 | p[5]);
1299 pes_is_aligned = (p[6] & 4);
1301 stream_id = p[3];
1304 if (p[7] & 0x80)
1305 { /* pts available */
1306 pts = (int64_t)(p[9] & 0x0E) << 29 ;
1307 pts |= p[10] << 22 ;
1308 pts |= (p[11] & 0xFE) << 14 ;
1309 pts |= p[12] << 7 ;
1310 pts |= (p[13] & 0xFE) >> 1 ;
1312 es->pts = pts / 90000.0f;
1314 else
1315 es->pts = 0.0f;
1318 header_len = p[8];
1321 if (header_len + 9 > pkt_len) //9 are the bytes read up to the header_length field
1323 mp_msg(MSGT_DEMUX, MSGL_DBG2, "demux_ts: illegal value for PES_header_data_length (0x%02x)\n", header_len);
1324 return 0;
1327 p += header_len + 9;
1328 packet_len -= header_len + 3;
1330 if(es->payload_size)
1331 es->payload_size -= header_len + 3;
1334 es->is_synced = 1; //only for SL streams we have to make sure it's really true, see below
1335 if (stream_id == 0xbd)
1337 mp_msg(MSGT_DEMUX, MSGL_DBG3, "pes_parse2: audio buf = %02X %02X %02X %02X %02X %02X %02X %02X, 80: %d\n",
1338 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[0] & 0x80);
1342 * we check the descriptor tag first because some stations
1343 * do not include any of the A52 header info in their audio tracks
1344 * these "raw" streams may begin with a byte that looks like a stream type.
1349 (type_from_pmt == AUDIO_A52) || /* A52 - raw */
1350 (p[0] == 0x0B && p[1] == 0x77) /* A52 - syncword */
1353 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 RAW OR SYNCWORD\n");
1354 es->start = p;
1355 es->size = packet_len;
1356 es->type = AUDIO_A52;
1357 es->payload_size -= packet_len;
1359 return 1;
1361 /* SPU SUBS */
1362 else if(type_from_pmt == SPU_DVB ||
1363 ((p[0] == 0x20) && pes_is_aligned)) // && p[1] == 0x00))
1365 es->start = p;
1366 es->size = packet_len;
1367 es->type = SPU_DVB;
1368 es->payload_size -= packet_len;
1370 return 1;
1372 else if (pes_is_aligned && ((p[0] & 0xE0) == 0x20)) //SPU_DVD
1374 //DVD SUBS
1375 es->start = p+1;
1376 es->size = packet_len-1;
1377 es->type = SPU_DVD;
1378 es->payload_size -= packet_len;
1380 return 1;
1382 else if (pes_is_aligned && (p[0] & 0xF8) == 0x80)
1384 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 WITH HEADER\n");
1385 es->start = p+4;
1386 es->size = packet_len - 4;
1387 es->type = AUDIO_A52;
1388 es->payload_size -= packet_len;
1390 return 1;
1392 else if (pes_is_aligned && ((p[0]&0xf0) == 0xa0))
1394 int pcm_offset;
1396 for (pcm_offset=0; ++pcm_offset < packet_len-1 ; )
1398 if (p[pcm_offset] == 0x01 && p[pcm_offset+1] == 0x80)
1399 { /* START */
1400 pcm_offset += 2;
1401 break;
1405 es->start = p + pcm_offset;
1406 es->size = packet_len - pcm_offset;
1407 es->type = AUDIO_LPCM_BE;
1408 es->payload_size -= packet_len;
1410 return 1;
1412 else
1414 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PES_PRIVATE1\n");
1415 es->start = p;
1416 es->size = packet_len;
1417 es->type = (type_from_pmt == UNKNOWN ? PES_PRIVATE1 : type_from_pmt);
1418 es->payload_size -= packet_len;
1420 return 1;
1423 else if(((stream_id >= 0xe0) && (stream_id <= 0xef)) || (stream_id == 0xfd && type_from_pmt != UNKNOWN))
1425 es->start = p;
1426 es->size = packet_len;
1427 if(type_from_pmt != UNKNOWN)
1428 es->type = type_from_pmt;
1429 else
1430 es->type = VIDEO_MPEG2;
1431 if(es->payload_size)
1432 es->payload_size -= packet_len;
1434 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: M2V size %d\n", es->size);
1435 return 1;
1437 else if ((stream_id == 0xfa))
1439 int l;
1441 es->is_synced = 0;
1442 if(type_from_pmt != UNKNOWN) //MP4 A/V or SL
1444 es->start = p;
1445 es->size = packet_len;
1446 es->type = type_from_pmt;
1448 if(type_from_pmt == SL_PES_STREAM)
1450 //if(pes_is_aligned)
1452 l = mp4_parse_sl_packet(pmt, p, packet_len, pid, es);
1453 mp_msg(MSGT_DEMUX, MSGL_DBG2, "L=%d, TYPE=%x\n", l, type_from_pmt);
1454 if(l < 0)
1456 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: couldn't parse SL header, passing along full PES payload\n");
1457 l = 0;
1461 es->start += l;
1462 es->size -= l;
1465 if(es->payload_size)
1466 es->payload_size -= packet_len;
1467 return 1;
1470 else if ((stream_id & 0xe0) == 0xc0)
1472 es->start = p;
1473 es->size = packet_len;
1475 if(type_from_pmt != UNKNOWN)
1476 es->type = type_from_pmt;
1477 else
1478 es->type = AUDIO_MP2;
1480 es->payload_size -= packet_len;
1482 return 1;
1484 else if (type_from_pmt != -1) //as a last resort here we trust the PMT, if present
1486 es->start = p;
1487 es->size = packet_len;
1488 es->type = type_from_pmt;
1489 es->payload_size -= packet_len;
1491 return 1;
1493 else
1495 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: unknown packet, id: %x\n", stream_id);
1498 es->is_synced = 0;
1499 return 0;
1505 static int ts_sync(stream_t *stream)
1507 int c=0;
1509 mp_msg(MSGT_DEMUX, MSGL_DBG3, "TS_SYNC \n");
1511 while(((c=stream_read_char(stream)) != 0x47) && ! stream->eof);
1513 if(c == 0x47)
1514 return c;
1515 else
1516 return 0;
1520 static void ts_dump_streams(ts_priv_t *priv)
1522 int i;
1524 for(i = 0; i < 3; i++)
1526 if((priv->fifo[i].pack != NULL) && (priv->fifo[i].offset != 0))
1528 resize_demux_packet(priv->fifo[i].pack, priv->fifo[i].offset);
1529 ds_add_packet(priv->fifo[i].ds, priv->fifo[i].pack);
1530 priv->fifo[i].offset = 0;
1531 priv->fifo[i].pack = NULL;
1537 static inline int32_t prog_idx_in_pat(ts_priv_t *priv, uint16_t progid)
1539 int x;
1541 if(priv->pat.progs == NULL)
1542 return -1;
1544 for(x = 0; x < priv->pat.progs_cnt; x++)
1546 if(priv->pat.progs[x].id == progid)
1547 return x;
1550 return -1;
1554 static inline int32_t prog_id_in_pat(ts_priv_t *priv, uint16_t pid)
1556 int x;
1558 if(priv->pat.progs == NULL)
1559 return -1;
1561 for(x = 0; x < priv->pat.progs_cnt; x++)
1563 if(priv->pat.progs[x].pmt_pid == pid)
1564 return priv->pat.progs[x].id;
1567 return -1;
1570 static int collect_section(ts_section_t *section, int is_start, unsigned char *buff, int size)
1572 uint8_t *ptr;
1573 uint16_t tlen;
1574 int skip, tid;
1576 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, start: %d, size: %d, collected: %d\n", is_start, size, section->buffer_len);
1577 if(! is_start && !section->buffer_len)
1578 return 0;
1580 if(is_start)
1582 if(! section->buffer)
1584 section->buffer = (uint8_t*) malloc(4096+256);
1585 if(section->buffer == NULL)
1586 return 0;
1588 section->buffer_len = 0;
1591 if(size + section->buffer_len > 4096+256)
1593 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, excessive len: %d + %d\n", section->buffer_len, size);
1594 return 0;
1597 memcpy(&(section->buffer[section->buffer_len]), buff, size);
1598 section->buffer_len += size;
1600 if(section->buffer_len < 3)
1601 return 0;
1603 skip = section->buffer[0];
1604 if(skip + 4 > section->buffer_len)
1605 return 0;
1607 ptr = &(section->buffer[skip + 1]);
1608 tid = ptr[0];
1609 tlen = ((ptr[1] & 0x0f) << 8) | ptr[2];
1610 mp_msg(MSGT_DEMUX, MSGL_V, "SKIP: %d+1, TID: %d, TLEN: %d, COLLECTED: %d\n", skip, tid, tlen, section->buffer_len);
1611 if(section->buffer_len < (skip+1+3+tlen))
1613 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DATA IS NOT ENOUGH, NEXT TIME\n");
1614 return 0;
1617 return skip+1;
1620 static int parse_pat(ts_priv_t * priv, int is_start, unsigned char *buff, int size)
1622 int skip;
1623 unsigned char *ptr;
1624 unsigned char *base;
1625 int entries, i;
1626 uint16_t progid;
1627 struct pat_progs_t *tmp;
1628 ts_section_t *section;
1630 section = &(priv->pat.section);
1631 skip = collect_section(section, is_start, buff, size);
1632 if(! skip)
1633 return 0;
1635 ptr = &(section->buffer[skip]);
1636 //PARSING
1637 priv->pat.table_id = ptr[0];
1638 if(priv->pat.table_id != 0)
1639 return 0;
1640 priv->pat.ssi = (ptr[1] >> 7) & 0x1;
1641 priv->pat.curr_next = ptr[5] & 0x01;
1642 priv->pat.ts_id = (ptr[3] << 8 ) | ptr[4];
1643 priv->pat.version_number = (ptr[5] >> 1) & 0x1F;
1644 priv->pat.section_length = ((ptr[1] & 0x03) << 8 ) | ptr[2];
1645 priv->pat.section_number = ptr[6];
1646 priv->pat.last_section_number = ptr[7];
1648 //check_crc32(0xFFFFFFFFL, ptr, priv->pat.buffer_len - 4, &ptr[priv->pat.buffer_len - 4]);
1649 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);
1651 entries = (int) (priv->pat.section_length - 9) / 4; //entries per section
1653 for(i=0; i < entries; i++)
1655 int32_t idx;
1656 base = &ptr[8 + i*4];
1657 progid = (base[0] << 8) | base[1];
1659 if((idx = prog_idx_in_pat(priv, progid)) == -1)
1661 int sz = sizeof(struct pat_progs_t) * (priv->pat.progs_cnt+1);
1662 tmp = realloc_struct(priv->pat.progs, priv->pat.progs_cnt+1, sizeof(struct pat_progs_t));
1663 if(tmp == NULL)
1665 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PAT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
1666 break;
1668 priv->pat.progs = tmp;
1669 idx = priv->pat.progs_cnt;
1670 priv->pat.progs_cnt++;
1673 priv->pat.progs[idx].id = progid;
1674 priv->pat.progs[idx].pmt_pid = ((base[2] & 0x1F) << 8) | base[3];
1675 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);
1678 return 1;
1682 static inline int32_t es_pid_in_pmt(pmt_t * pmt, uint16_t pid)
1684 uint16_t i;
1686 if(pmt == NULL)
1687 return -1;
1689 if(pmt->es == NULL)
1690 return -1;
1692 for(i = 0; i < pmt->es_cnt; i++)
1694 if(pmt->es[i].pid == pid)
1695 return (int32_t) i;
1698 return -1;
1702 static uint16_t get_mp4_desc_len(uint8_t *buf, int *len)
1704 //uint16_t i = 0, size = 0;
1705 int i = 0, j, size = 0;
1707 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PARSE_MP4_DESC_LEN(%d), bytes: ", *len);
1708 j = FFMIN(*len, 4);
1709 while(i < j)
1711 mp_msg(MSGT_DEMUX, MSGL_DBG2, " %x ", buf[i]);
1712 size |= (buf[i] & 0x7f);
1713 if(!(buf[i] & 0x80))
1714 break;
1715 size <<= 7;
1716 i++;
1718 mp_msg(MSGT_DEMUX, MSGL_DBG2, ", SIZE=%d\n", size);
1720 *len = i+1;
1721 return size;
1725 static uint16_t parse_mp4_slconfig_descriptor(uint8_t *buf, int len, void *elem)
1727 int i = 0;
1728 mp4_es_descr_t *es;
1729 mp4_sl_config_t *sl;
1731 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_SLCONFIG_DESCRIPTOR(%d)\n", len);
1732 es = (mp4_es_descr_t *) elem;
1733 if(!es)
1735 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1736 return len;
1738 sl = &(es->sl);
1740 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;
1741 sl->ocr = sl->dts = sl->cts = 0;
1743 if(buf[0] == 0)
1745 i++;
1746 sl->flags = buf[i];
1747 i++;
1748 sl->ts_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1749 i += 4;
1750 sl->ocr_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1751 i += 4;
1752 sl->ts_len = buf[i];
1753 i++;
1754 sl->ocr_len = buf[i];
1755 i++;
1756 sl->au_len = buf[i];
1757 i++;
1758 sl->instant_bitrate_len = buf[i];
1759 i++;
1760 sl->degr_len = (buf[i] >> 4) & 0x0f;
1761 sl->au_seqnum_len = ((buf[i] & 0x0f) << 1) | ((buf[i+1] >> 7) & 0x01);
1762 i++;
1763 sl->packet_seqnum_len = ((buf[i] >> 2) & 0x1f);
1764 i++;
1767 else if(buf[0] == 1)
1769 sl->flags = 0;
1770 sl->ts_resolution = 1000;
1771 sl->ts_len = 32;
1772 i++;
1774 else if(buf[0] == 2)
1776 sl->flags = 4;
1777 i++;
1779 else
1781 sl->flags = 0;
1782 i++;
1785 sl->au_start = (sl->flags >> 7) & 0x1;
1786 sl->au_end = (sl->flags >> 6) & 0x1;
1787 sl->random_accesspoint = (sl->flags >> 5) & 0x1;
1788 sl->random_accesspoint_only = (sl->flags >> 4) & 0x1;
1789 sl->padding = (sl->flags >> 3) & 0x1;
1790 sl->use_ts = (sl->flags >> 2) & 0x1;
1791 sl->idle = (sl->flags >> 1) & 0x1;
1792 sl->duration = sl->flags & 0x1;
1794 if(sl->duration)
1796 sl->timescale = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1797 i += 4;
1798 sl->au_duration = (buf[i] << 8) | buf[i+1];
1799 i += 2;
1800 sl->cts_duration = (buf[i] << 8) | buf[i+1];
1801 i += 2;
1803 else //no support for fixed durations atm
1804 sl->timescale = sl->au_duration = sl->cts_duration = 0;
1806 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",
1807 len, buf[0], sl->flags, sl->use_ts, sl->ts_len, sl->timescale, (uint64_t) sl->dts, (uint64_t) sl->cts);
1809 return len;
1812 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem);
1814 static uint16_t parse_mp4_decoder_config_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
1816 int i = 0, j;
1817 mp4_es_descr_t *es;
1818 mp4_decoder_config_t *dec;
1820 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_CONFIG_DESCRIPTOR(%d)\n", len);
1821 es = (mp4_es_descr_t *) elem;
1822 if(!es)
1824 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1825 return len;
1827 dec = (mp4_decoder_config_t*) &(es->decoder);
1829 dec->object_type = buf[i];
1830 dec->stream_type = (buf[i+1]>>2) & 0x3f;
1832 if(dec->object_type == 1 && dec->stream_type == 1)
1834 dec->object_type = MP4_OD;
1835 dec->stream_type = MP4_OD;
1837 else if(dec->stream_type == 4)
1839 if(dec->object_type == 0x6a)
1840 dec->object_type = VIDEO_MPEG1;
1841 if(dec->object_type >= 0x60 && dec->object_type <= 0x65)
1842 dec->object_type = VIDEO_MPEG2;
1843 else if(dec->object_type == 0x20)
1844 dec->object_type = VIDEO_MPEG4;
1845 else if(dec->object_type == 0x21)
1846 dec->object_type = VIDEO_AVC;
1847 /*else if(dec->object_type == 0x22)
1848 fprintf(stderr, "TYPE 0x22\n");*/
1849 else dec->object_type = UNKNOWN;
1851 else if(dec->stream_type == 5)
1853 if(dec->object_type == 0x40)
1854 dec->object_type = AUDIO_AAC;
1855 else if(dec->object_type == 0x6b)
1856 dec->object_type = AUDIO_MP2;
1857 else if(dec->object_type >= 0x66 && dec->object_type <= 0x69)
1858 dec->object_type = AUDIO_MP2;
1859 else
1860 dec->object_type = UNKNOWN;
1862 else
1863 dec->object_type = dec->stream_type = UNKNOWN;
1865 if(dec->object_type != UNKNOWN)
1867 //update the type of the current stream
1868 for(j = 0; j < pmt->es_cnt; j++)
1870 if(pmt->es[j].mp4_es_id == es->id)
1872 pmt->es[j].type = SL_PES_STREAM;
1877 if(len > 13)
1878 parse_mp4_descriptors(pmt, &buf[13], len-13, dec);
1880 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);
1882 return len;
1885 static uint16_t parse_mp4_decoder_specific_descriptor(uint8_t *buf, int len, void *elem)
1887 int i;
1888 mp4_decoder_config_t *dec;
1890 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_SPECIFIC_DESCRIPTOR(%d)\n", len);
1891 dec = (mp4_decoder_config_t *) elem;
1892 if(!dec)
1894 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1895 return len;
1898 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4 SPECIFIC INFO BYTES: \n");
1899 for(i=0; i<len; i++)
1900 mp_msg(MSGT_DEMUX, MSGL_DBG2, "%02x ", buf[i]);
1901 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\n");
1903 if(len > MAX_EXTRADATA_SIZE)
1905 mp_msg(MSGT_DEMUX, MSGL_ERR, "DEMUX_TS, EXTRADATA SUSPICIOUSLY BIG: %d, REFUSED\r\n", len);
1906 return len;
1908 memcpy(dec->buf, buf, len);
1909 dec->buf_size = len;
1911 return len;
1914 static uint16_t parse_mp4_es_descriptor(pmt_t *pmt, uint8_t *buf, int len)
1916 int i = 0, j = 0, k, found;
1917 uint8_t flag;
1918 mp4_es_descr_t es, *target_es = NULL, *tmp;
1920 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4ES: len=%d\n", len);
1921 memset(&es, 0, sizeof(mp4_es_descr_t));
1922 while(i < len)
1924 es.id = (buf[i] << 8) | buf[i+1];
1925 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_ID: %d\n", es.id);
1926 i += 2;
1927 flag = buf[i];
1928 i++;
1929 if(flag & 0x80)
1930 i += 2;
1931 if(flag & 0x40)
1932 i += buf[i]+1;
1933 if(flag & 0x20) //OCR, maybe we need it
1934 i += 2;
1936 j = parse_mp4_descriptors(pmt, &buf[i], len-i, &es);
1937 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);
1938 if(es.decoder.object_type != UNKNOWN && es.decoder.stream_type != UNKNOWN)
1940 found = 0;
1941 //search this ES_ID if we already have it
1942 for(k=0; k < pmt->mp4es_cnt; k++)
1944 if(pmt->mp4es[k].id == es.id)
1946 target_es = &(pmt->mp4es[k]);
1947 found = 1;
1951 if(! found)
1953 tmp = realloc_struct(pmt->mp4es, pmt->mp4es_cnt+1, sizeof(mp4_es_descr_t));
1954 if(tmp == NULL)
1956 fprintf(stderr, "CAN'T REALLOC MP4_ES_DESCR\n");
1957 continue;
1959 pmt->mp4es = tmp;
1960 target_es = &(pmt->mp4es[pmt->mp4es_cnt]);
1961 pmt->mp4es_cnt++;
1963 memcpy(target_es, &es, sizeof(mp4_es_descr_t));
1964 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_CNT: %d, ID=%d\n", pmt->mp4es_cnt, target_es->id);
1967 i += j;
1970 return len;
1973 static void parse_mp4_object_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
1975 int i, j = 0, id;
1977 i=0;
1978 id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
1979 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_OBJECT_DESCRIPTOR: len=%d, OD_ID=%d\n", len, id);
1980 if(buf[1] & 0x20)
1982 i += buf[2] + 1; //url
1983 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
1985 else
1987 i = 2;
1989 while(i < len)
1991 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
1992 mp_msg(MSGT_DEMUX, MSGL_V, "OBJD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
1993 i += j;
1999 static void parse_mp4_iod(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2001 int i, j = 0;
2002 mp4_od_t *iod = &(pmt->iod);
2004 iod->id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2005 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_IOD: len=%d, IOD_ID=%d\n", len, iod->id);
2006 i = 2;
2007 if(buf[1] & 0x20)
2009 i += buf[2] + 1; //url
2010 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2012 else
2014 i = 7;
2015 while(i < len)
2017 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2018 mp_msg(MSGT_DEMUX, MSGL_V, "IOD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2019 i += j;
2024 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2026 int tag, descr_len, i = 0, j = 0;
2028 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DESCRIPTORS, len=%d\n", len);
2029 if(! len)
2030 return len;
2032 while(i < len)
2034 tag = buf[i];
2035 j = len - i -1;
2036 descr_len = get_mp4_desc_len(&(buf[i+1]), &j);
2037 mp_msg(MSGT_DEMUX, MSGL_V, "TAG=%d (0x%x), DESCR_len=%d, len=%d, j=%d\n", tag, tag, descr_len, len, j);
2038 if(descr_len > len - j+1)
2040 mp_msg(MSGT_DEMUX, MSGL_V, "descriptor is too long, exit\n");
2041 return len;
2043 i += j+1;
2045 switch(tag)
2047 case 0x1:
2048 parse_mp4_object_descriptor(pmt, &(buf[i]), descr_len, elem);
2049 break;
2050 case 0x2:
2051 parse_mp4_iod(pmt, &(buf[i]), descr_len, elem);
2052 break;
2053 case 0x3:
2054 parse_mp4_es_descriptor(pmt, &(buf[i]), descr_len);
2055 break;
2056 case 0x4:
2057 parse_mp4_decoder_config_descriptor(pmt, &buf[i], descr_len, elem);
2058 break;
2059 case 0x05:
2060 parse_mp4_decoder_specific_descriptor(&buf[i], descr_len, elem);
2061 break;
2062 case 0x6:
2063 parse_mp4_slconfig_descriptor(&buf[i], descr_len, elem);
2064 break;
2065 default:
2066 mp_msg(MSGT_DEMUX, MSGL_V, "Unsupported mp4 descriptor 0x%x\n", tag);
2068 i += descr_len;
2071 return len;
2074 static ES_stream_t *new_pid(ts_priv_t *priv, int pid)
2076 ES_stream_t *tss;
2078 tss = malloc(sizeof(ES_stream_t));
2079 if(! tss)
2080 return NULL;
2081 memset(tss, 0, sizeof(ES_stream_t));
2082 tss->pid = pid;
2083 tss->last_cc = -1;
2084 tss->type = UNKNOWN;
2085 tss->subtype = UNKNOWN;
2086 tss->is_synced = 0;
2087 tss->extradata = NULL;
2088 tss->extradata_alloc = tss->extradata_len = 0;
2089 priv->ts.pids[pid] = tss;
2091 return tss;
2095 static int parse_program_descriptors(pmt_t *pmt, uint8_t *buf, uint16_t len)
2097 uint16_t i = 0, k, olen = len;
2099 while(len > 0)
2101 mp_msg(MSGT_DEMUX, MSGL_V, "PROG DESCR, TAG=%x, LEN=%d(%x)\n", buf[i], buf[i+1], buf[i+1]);
2102 if(buf[i+1] > len-2)
2104 mp_msg(MSGT_DEMUX, MSGL_V, "ERROR, descriptor len is too long, skipping\n");
2105 return olen;
2108 if(buf[i] == 0x1d)
2110 if(buf[i+3] == 2) //buggy versions of vlc muxer make this non-standard mess (missing iod_scope)
2111 k = 3;
2112 else
2113 k = 4; //this is standard compliant
2114 parse_mp4_descriptors(pmt, &buf[i+k], (int) buf[i+1]-(k-2), NULL);
2117 len -= 2 + buf[i+1];
2120 return olen;
2123 static int parse_descriptors(struct pmt_es_t *es, uint8_t *ptr)
2125 int j, descr_len, len;
2127 j = 0;
2128 len = es->descr_length;
2129 while(len > 2)
2131 descr_len = ptr[j+1];
2132 mp_msg(MSGT_DEMUX, MSGL_V, "...descr id: 0x%x, len=%d\n", ptr[j], descr_len);
2133 if(descr_len > len)
2135 mp_msg(MSGT_DEMUX, MSGL_ERR, "INVALID DESCR LEN for tag %02x: %d vs %d max, EXIT LOOP\n", ptr[j], descr_len, len);
2136 return -1;
2140 if(ptr[j] == 0x6a || ptr[j] == 0x7a) //A52 Descriptor
2142 if(es->type == 0x6)
2144 es->type = AUDIO_A52;
2145 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB A52 Descriptor\n");
2148 else if(ptr[j] == 0x7b) //DVB DTS Descriptor
2150 if(es->type == 0x6)
2152 es->type = AUDIO_DTS;
2153 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB DTS Descriptor\n");
2156 else if(ptr[j] == 0x59) //Subtitling Descriptor
2158 uint8_t subtype;
2160 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Subtitling Descriptor\n");
2161 if(descr_len < 8)
2163 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Descriptor length too short for DVB Subtitle Descriptor: %d, SKIPPING\n", descr_len);
2165 else
2167 memcpy(es->lang, &ptr[j+2], 3);
2168 es->lang[3] = 0;
2169 subtype = ptr[j+5];
2171 (subtype >= 0x10 && subtype <= 0x13) ||
2172 (subtype >= 0x20 && subtype <= 0x23)
2175 es->type = SPU_DVB;
2176 //page parameters: compo page 2 bytes, ancillary page 2 bytes
2178 else
2179 es->type = UNKNOWN;
2182 else if(ptr[j] == 0x50) //Component Descriptor
2184 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Component Descriptor\n");
2185 memcpy(es->lang, &ptr[j+5], 3);
2186 es->lang[3] = 0;
2188 else if(ptr[j] == 0xa) //Language Descriptor
2190 memcpy(es->lang, &ptr[j+2], 3);
2191 es->lang[3] = 0;
2192 mp_msg(MSGT_DEMUX, MSGL_V, "Language Descriptor: %s\n", es->lang);
2194 else if(ptr[j] == 0x5) //Registration Descriptor (looks like e fourCC :) )
2196 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor\n");
2197 if(descr_len < 4)
2199 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor length too short: %d, SKIPPING\n", descr_len);
2201 else
2203 char *d;
2204 memcpy(es->format_descriptor, &ptr[j+2], 4);
2205 es->format_descriptor[4] = 0;
2207 d = &ptr[j+2];
2208 if(d[0] == 'A' && d[1] == 'C' && d[2] == '-' && d[3] == '3')
2210 es->type = AUDIO_A52;
2212 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '1')
2214 es->type = AUDIO_DTS;
2216 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '2')
2218 es->type = AUDIO_DTS;
2220 else if(d[0] == 'V' && d[1] == 'C' && d[2] == '-' && d[3] == '1')
2222 es->type = VIDEO_VC1;
2224 else
2225 es->type = UNKNOWN;
2226 mp_msg(MSGT_DEMUX, MSGL_DBG2, "FORMAT %s\n", es->format_descriptor);
2229 else if(ptr[j] == 0x1e)
2231 es->mp4_es_id = (ptr[j+2] << 8) | ptr[j+3];
2232 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);
2234 else
2235 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Unknown descriptor 0x%x, SKIPPING\n", ptr[j]);
2237 len -= 2 + descr_len;
2238 j += 2 + descr_len;
2241 return 1;
2244 static int parse_sl_section(pmt_t *pmt, ts_section_t *section, int is_start, unsigned char *buff, int size)
2246 int tid, len, skip;
2247 uint8_t *ptr;
2248 skip = collect_section(section, is_start, buff, size);
2249 if(! skip)
2250 return 0;
2252 ptr = &(section->buffer[skip]);
2253 tid = ptr[0];
2254 len = ((ptr[1] & 0x0f) << 8) | ptr[2];
2255 mp_msg(MSGT_DEMUX, MSGL_V, "TABLEID: %d (av. %d), skip=%d, LEN: %d\n", tid, section->buffer_len, skip, len);
2256 if(len > 4093 || section->buffer_len < len || tid != 5)
2258 mp_msg(MSGT_DEMUX, MSGL_V, "SECTION TOO LARGE or wrong section type, EXIT\n");
2259 return 0;
2262 if(! (ptr[5] & 1))
2263 return 0;
2265 //8 is the current position, len - 9 is the amount of data available
2266 parse_mp4_descriptors(pmt, &ptr[8], len - 9, NULL);
2268 return 1;
2271 static int parse_pmt(ts_priv_t * priv, uint16_t progid, uint16_t pid, int is_start, unsigned char *buff, int size)
2273 unsigned char *base, *es_base;
2274 pmt_t *pmt;
2275 int32_t idx, es_count, section_bytes;
2276 uint8_t m=0;
2277 int skip;
2278 pmt_t *tmp;
2279 struct pmt_es_t *tmp_es;
2280 ts_section_t *section;
2281 ES_stream_t *tss;
2283 idx = progid_idx_in_pmt(priv, progid);
2285 if(idx == -1)
2287 int sz = (priv->pmt_cnt + 1) * sizeof(pmt_t);
2288 tmp = realloc_struct(priv->pmt, priv->pmt_cnt + 1, sizeof(pmt_t));
2289 if(tmp == NULL)
2291 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
2292 return 0;
2294 priv->pmt = tmp;
2295 idx = priv->pmt_cnt;
2296 memset(&(priv->pmt[idx]), 0, sizeof(pmt_t));
2297 priv->pmt_cnt++;
2298 priv->pmt[idx].progid = progid;
2301 pmt = &(priv->pmt[idx]);
2303 section = &(pmt->section);
2304 skip = collect_section(section, is_start, buff, size);
2305 if(! skip)
2306 return 0;
2308 base = &(section->buffer[skip]);
2310 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",
2311 progid, pmt->section.buffer_len, is_start, pid, size, m, pmt->es_cnt, idx, pmt);
2313 pmt->table_id = base[0];
2314 if(pmt->table_id != 2)
2315 return -1;
2316 pmt->ssi = base[1] & 0x80;
2317 pmt->section_length = (((base[1] & 0xf) << 8 ) | base[2]);
2318 pmt->version_number = (base[5] >> 1) & 0x1f;
2319 pmt->curr_next = (base[5] & 1);
2320 pmt->section_number = base[6];
2321 pmt->last_section_number = base[7];
2322 pmt->PCR_PID = ((base[8] & 0x1f) << 8 ) | base[9];
2323 pmt->prog_descr_length = ((base[10] & 0xf) << 8 ) | base[11];
2324 if(pmt->prog_descr_length > pmt->section_length - 9)
2326 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, INVALID PROG_DESCR LENGTH (%d vs %d)\n", pmt->prog_descr_length, pmt->section_length - 9);
2327 return -1;
2330 if(pmt->prog_descr_length)
2331 parse_program_descriptors(pmt, &base[12], pmt->prog_descr_length);
2333 es_base = &base[12 + pmt->prog_descr_length]; //the beginning of th ES loop
2335 section_bytes= pmt->section_length - 13 - pmt->prog_descr_length;
2336 es_count = 0;
2338 while(section_bytes >= 5)
2340 int es_pid, es_type;
2342 es_type = es_base[0];
2343 es_pid = ((es_base[1] & 0x1f) << 8) | es_base[2];
2345 idx = es_pid_in_pmt(pmt, es_pid);
2346 if(idx == -1)
2348 int sz = sizeof(struct pmt_es_t) * (pmt->es_cnt + 1);
2349 tmp_es = realloc_struct(pmt->es, pmt->es_cnt + 1, sizeof(struct pmt_es_t));
2350 if(tmp_es == NULL)
2352 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT, COULDN'T ALLOCATE %d bytes for PMT_ES\n", sz);
2353 continue;
2355 pmt->es = tmp_es;
2356 idx = pmt->es_cnt;
2357 memset(&(pmt->es[idx]), 0, sizeof(struct pmt_es_t));
2358 pmt->es_cnt++;
2361 pmt->es[idx].descr_length = ((es_base[3] & 0xf) << 8) | es_base[4];
2364 if(pmt->es[idx].descr_length > section_bytes - 5)
2366 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, ES_DESCR_LENGTH TOO LARGE %d > %d, EXIT\n",
2367 pmt->es[idx].descr_length, section_bytes - 5);
2368 return -1;
2372 pmt->es[idx].pid = es_pid;
2373 if(es_type != 0x6)
2374 pmt->es[idx].type = UNKNOWN;
2375 else
2376 pmt->es[idx].type = es_type;
2378 parse_descriptors(&pmt->es[idx], &es_base[5]);
2380 switch(es_type)
2382 case 1:
2383 pmt->es[idx].type = VIDEO_MPEG1;
2384 break;
2385 case 2:
2386 pmt->es[idx].type = VIDEO_MPEG2;
2387 break;
2388 case 3:
2389 case 4:
2390 pmt->es[idx].type = AUDIO_MP2;
2391 break;
2392 case 6:
2393 if(pmt->es[idx].type == 0x6) //this could have been ovrwritten by parse_descriptors
2394 pmt->es[idx].type = UNKNOWN;
2395 break;
2396 case 0x10:
2397 pmt->es[idx].type = VIDEO_MPEG4;
2398 break;
2399 case 0x0f:
2400 pmt->es[idx].type = AUDIO_AAC;
2401 break;
2402 case 0x1b:
2403 pmt->es[idx].type = VIDEO_H264;
2404 break;
2405 case 0x12:
2406 pmt->es[idx].type = SL_PES_STREAM;
2407 break;
2408 case 0x13:
2409 pmt->es[idx].type = SL_SECTION;
2410 break;
2411 case 0x81:
2412 pmt->es[idx].type = AUDIO_A52;
2413 break;
2414 case 0x8A:
2415 pmt->es[idx].type = AUDIO_DTS;
2416 break;
2417 case 0xEA:
2418 pmt->es[idx].type = VIDEO_VC1;
2419 break;
2420 default:
2421 mp_msg(MSGT_DEMUX, MSGL_DBG2, "UNKNOWN ES TYPE=0x%x\n", es_type);
2422 pmt->es[idx].type = UNKNOWN;
2425 tss = priv->ts.pids[es_pid]; //an ES stream
2426 if(tss == NULL)
2428 tss = new_pid(priv, es_pid);
2429 if(tss)
2430 tss->type = pmt->es[idx].type;
2433 section_bytes -= 5 + pmt->es[idx].descr_length;
2434 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",
2435 progid, idx, es_count, pmt->es[idx].pid, pmt->es[idx].pid, pmt->es[idx].type, pmt->es[idx].descr_length, section_bytes);
2438 es_base += 5 + pmt->es[idx].descr_length;
2440 es_count++;
2443 mp_msg(MSGT_DEMUX, MSGL_V, "----------------------------\n");
2444 return 1;
2447 static pmt_t* pmt_of_pid(ts_priv_t *priv, int pid, mp4_decoder_config_t **mp4_dec)
2449 int32_t i, j, k;
2451 if(priv->pmt)
2453 for(i = 0; i < priv->pmt_cnt; i++)
2455 if(priv->pmt[i].es && priv->pmt[i].es_cnt)
2457 for(j = 0; j < priv->pmt[i].es_cnt; j++)
2459 if(priv->pmt[i].es[j].pid == pid)
2461 //search mp4_es_id
2462 if(priv->pmt[i].es[j].mp4_es_id)
2464 for(k = 0; k < priv->pmt[i].mp4es_cnt; k++)
2466 if(priv->pmt[i].mp4es[k].id == priv->pmt[i].es[j].mp4_es_id)
2468 *mp4_dec = &(priv->pmt[i].mp4es[k].decoder);
2469 break;
2474 return &(priv->pmt[i]);
2481 return NULL;
2485 static inline int32_t pid_type_from_pmt(ts_priv_t *priv, int pid)
2487 int32_t pmt_idx, pid_idx, i, j;
2489 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2491 if(pmt_idx != -1)
2493 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2494 if(pid_idx != -1)
2495 return priv->pmt[pmt_idx].es[pid_idx].type;
2497 //else
2499 for(i = 0; i < priv->pmt_cnt; i++)
2501 pmt_t *pmt = &(priv->pmt[i]);
2502 for(j = 0; j < pmt->es_cnt; j++)
2503 if(pmt->es[j].pid == pid)
2504 return pmt->es[j].type;
2508 return UNKNOWN;
2512 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid)
2514 int32_t pmt_idx, pid_idx, i, j;
2516 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2518 if(pmt_idx != -1)
2520 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2521 if(pid_idx != -1)
2522 return priv->pmt[pmt_idx].es[pid_idx].lang;
2524 else
2526 for(i = 0; i < priv->pmt_cnt; i++)
2528 pmt_t *pmt = &(priv->pmt[i]);
2529 for(j = 0; j < pmt->es_cnt; j++)
2530 if(pmt->es[j].pid == pid)
2531 return pmt->es[j].lang;
2535 return NULL;
2539 static int fill_packet(demuxer_t *demuxer, demux_stream_t *ds, demux_packet_t **dp, int *dp_offset, TS_stream_info *si)
2541 int ret = 0;
2543 if((*dp != NULL) && (*dp_offset > 0))
2545 ret = *dp_offset;
2546 resize_demux_packet(*dp, ret); //shrinked to the right size
2547 ds_add_packet(ds, *dp);
2548 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);
2549 if(si)
2551 float diff = (*dp)->pts - si->last_pts;
2552 float dur;
2554 if(abs(diff) > 1) //1 second, there's a discontinuity
2556 si->duration += si->last_pts - si->first_pts;
2557 si->first_pts = si->last_pts = (*dp)->pts;
2559 else
2561 si->last_pts = (*dp)->pts;
2563 si->size += ret;
2564 dur = si->duration + (si->last_pts - si->first_pts);
2566 if(dur > 0 && ds == demuxer->video)
2568 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2569 if(dur > 1) //otherwise it may be unreliable
2570 priv->vbitrate = (uint32_t) ((float) si->size / dur);
2575 *dp = NULL;
2576 *dp_offset = 0;
2578 return ret;
2581 static int fill_extradata(mp4_decoder_config_t * mp4_dec, ES_stream_t *tss)
2583 uint8_t *tmp;
2585 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4_dec: %p, pid: %d\n", mp4_dec, tss->pid);
2587 if(mp4_dec->buf_size > tss->extradata_alloc)
2589 tmp = (uint8_t *) realloc(tss->extradata, mp4_dec->buf_size);
2590 if(!tmp)
2591 return 0;
2592 tss->extradata = tmp;
2593 tss->extradata_alloc = mp4_dec->buf_size;
2595 memcpy(tss->extradata, mp4_dec->buf, mp4_dec->buf_size);
2596 tss->extradata_len = mp4_dec->buf_size;
2597 mp_msg(MSGT_DEMUX, MSGL_V, "EXTRADATA: %p, alloc=%d, len=%d\n", tss->extradata, tss->extradata_alloc, tss->extradata_len);
2599 return tss->extradata_len;
2602 // 0 = EOF or no stream found
2603 // else = [-] number of bytes written to the packet
2604 static int ts_parse(demuxer_t *demuxer , ES_stream_t *es, unsigned char *packet, int probe)
2606 ES_stream_t *tss;
2607 uint8_t done = 0;
2608 int buf_size, is_start, pid, base;
2609 int len, cc, cc_ok, afc, retv = 0, is_video, is_audio, is_sub;
2610 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2611 stream_t *stream = demuxer->stream;
2612 char *p;
2613 demux_stream_t *ds = NULL;
2614 demux_packet_t **dp = NULL;
2615 int *dp_offset = 0, *buffer_size = 0;
2616 int32_t progid, pid_type, bad, ts_error;
2617 int junk = 0, rap_flag = 0;
2618 pmt_t *pmt;
2619 mp4_decoder_config_t *mp4_dec;
2620 TS_stream_info *si;
2623 while(! done)
2625 bad = ts_error = 0;
2626 ds = (demux_stream_t*) NULL;
2627 dp = (demux_packet_t **) NULL;
2628 dp_offset = buffer_size = NULL;
2629 rap_flag = 0;
2630 mp4_dec = NULL;
2631 es->is_synced = 0;
2632 si = NULL;
2634 junk = priv->ts.packet_size - TS_PACKET_SIZE;
2635 buf_size = priv->ts.packet_size - junk;
2637 if(stream_eof(stream))
2639 if(! probe)
2641 ts_dump_streams(priv);
2642 demuxer->filepos = stream_tell(demuxer->stream);
2645 return 0;
2649 if(! ts_sync(stream))
2651 mp_msg(MSGT_DEMUX, MSGL_INFO, "TS_PARSE: COULDN'T SYNC\n");
2652 return 0;
2655 len = stream_read(stream, &packet[1], 3);
2656 if (len != 3)
2657 return 0;
2658 buf_size -= 4;
2660 if((packet[1] >> 7) & 0x01) //transport error
2661 ts_error = 1;
2664 is_start = packet[1] & 0x40;
2665 pid = ((packet[1] & 0x1f) << 8) | packet[2];
2667 tss = priv->ts.pids[pid]; //an ES stream
2668 if(tss == NULL)
2670 tss = new_pid(priv, pid);
2671 if(tss == NULL)
2672 continue;
2675 cc = (packet[3] & 0xf);
2676 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
2677 tss->last_cc = cc;
2679 bad = ts_error; // || (! cc_ok);
2680 if(bad)
2682 if(priv->keep_broken == 0)
2684 stream_skip(stream, buf_size-1+junk);
2685 continue;
2688 is_start = 0; //queued to the packet data
2691 if(is_start)
2692 tss->is_synced = 1;
2694 if((!is_start && !tss->is_synced) || ((pid > 1) && (pid < 16)) || (pid == 8191)) //invalid pid
2696 stream_skip(stream, buf_size-1+junk);
2697 continue;
2701 afc = (packet[3] >> 4) & 3;
2702 if(! (afc % 2)) //no payload in this TS packet
2704 stream_skip(stream, buf_size-1+junk);
2705 continue;
2708 if(afc > 1)
2710 int c;
2711 c = stream_read_char(stream);
2712 buf_size--;
2713 if(c < 0 || c > 183) //broken from the stream layer or invalid
2715 stream_skip(stream, buf_size-1+junk);
2716 continue;
2719 //c==0 is allowed!
2720 if(c > 0)
2722 rap_flag = (stream_read_char(stream) & 0x40) >> 6;
2723 buf_size--;
2725 c--;
2726 stream_skip(stream, c);
2727 buf_size -= c;
2728 if(buf_size == 0)
2729 continue;
2733 //find the program that the pid belongs to; if (it's the right one or -1) && pid_type==SL_SECTION
2734 //call parse_sl_section()
2735 pmt = pmt_of_pid(priv, pid, &mp4_dec);
2736 if(mp4_dec)
2738 fill_extradata(mp4_dec, tss);
2739 if(IS_VIDEO(mp4_dec->object_type) || IS_AUDIO(mp4_dec->object_type))
2741 tss->type = SL_PES_STREAM;
2742 tss->subtype = mp4_dec->object_type;
2747 //TABLE PARSING
2749 base = priv->ts.packet_size - buf_size;
2751 priv->last_pid = pid;
2753 is_video = IS_VIDEO(tss->type) || (tss->type==SL_PES_STREAM && IS_VIDEO(tss->subtype));
2754 is_audio = IS_AUDIO(tss->type) || (tss->type==SL_PES_STREAM && IS_AUDIO(tss->subtype)) || (tss->type == PES_PRIVATE1);
2755 is_sub = ((tss->type == SPU_DVD) || (tss->type == SPU_DVB));
2756 pid_type = pid_type_from_pmt(priv, pid);
2758 // PES CONTENT STARTS HERE
2759 if(! probe)
2761 if((is_video || is_audio) && is_start && !priv->ts.streams[pid].sh)
2762 ts_add_stream(demuxer, tss);
2764 if((pid == demuxer->sub->id)) //or the lang is right
2766 pid_type = SPU_DVD;
2769 if(is_video && (demuxer->video->id == priv->ts.streams[pid].id))
2771 ds = demuxer->video;
2773 dp = &priv->fifo[1].pack;
2774 dp_offset = &priv->fifo[1].offset;
2775 buffer_size = &priv->fifo[1].buffer_size;
2776 si = &priv->vstr;
2778 else if(is_audio && (demuxer->audio->id == priv->ts.streams[pid].id))
2780 ds = demuxer->audio;
2782 dp = &priv->fifo[0].pack;
2783 dp_offset = &priv->fifo[0].offset;
2784 buffer_size = &priv->fifo[0].buffer_size;
2785 si = &priv->astr;
2787 else if(is_sub
2788 || (pid_type == SPU_DVD) || (pid_type == SPU_DVB))
2790 //SUBS are infrequent, so the initial detection may fail
2791 // and we may need to add them at play-time
2792 if(demuxer->sub->id == -1)
2794 uint16_t p;
2795 p = progid_for_pid(priv, tss->pid, priv->prog);
2797 if(p == priv->prog)
2799 int asgn = 0;
2800 uint8_t *lang;
2802 if(dvdsub_lang)
2804 if ((lang = pid_lang_from_pmt(priv, pid)))
2805 asgn = (strncmp(lang, dvdsub_lang, 3) == 0);
2807 else //no language specified with -slang
2808 asgn = 1;
2810 if(asgn)
2812 demuxer->sub->id = tss->pid;
2813 mp_msg(MSGT_DEMUX, MSGL_INFO, "CHOSEN SUBs pid 0x%x (%d) FROM PROG %d\n", tss->pid, tss->pid, priv->prog);
2818 if(demuxer->sub->id == tss->pid)
2820 ds = demuxer->sub;
2822 dp = &priv->fifo[2].pack;
2823 dp_offset = &priv->fifo[2].offset;
2824 buffer_size = &priv->fifo[2].buffer_size;
2826 else
2828 stream_skip(stream, buf_size+junk);
2829 continue;
2833 //IS IT TIME TO QUEUE DATA to the dp_packet?
2834 if(is_start && (dp != NULL))
2836 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
2840 if(dp && *dp == NULL)
2842 if(*buffer_size > MAX_PACK_BYTES)
2843 *buffer_size = MAX_PACK_BYTES;
2844 *dp = new_demux_packet(*buffer_size); //es->size
2845 *dp_offset = 0;
2846 if(! *dp)
2848 fprintf(stderr, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", *buffer_size);
2849 continue;
2851 mp_msg(MSGT_DEMUX, MSGL_DBG2, "CREATED DP(%d)\n", *buffer_size);
2856 if(probe || !dp) //dp is NULL for tables and sections
2858 p = &packet[base];
2860 else //feeding
2862 if(*dp_offset + buf_size > *buffer_size)
2864 *buffer_size = *dp_offset + buf_size + TS_FEC_PACKET_SIZE;
2865 resize_demux_packet(*dp, *buffer_size);
2867 p = &((*dp)->buffer[*dp_offset]);
2870 len = stream_read(stream, p, buf_size);
2871 if(len < buf_size)
2873 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\r\nts_parse() couldn't read enough data: %d < %d\r\n", len, buf_size);
2874 continue;
2876 stream_skip(stream, junk);
2878 if(pid == 0)
2880 parse_pat(priv, is_start, p, buf_size);
2881 continue;
2883 else if((tss->type == SL_SECTION) && pmt)
2885 int k, mp4_es_id = -1;
2886 ts_section_t *section;
2887 for(k = 0; k < pmt->mp4es_cnt; k++)
2889 if(pmt->mp4es[k].decoder.object_type == MP4_OD && pmt->mp4es[k].decoder.stream_type == MP4_OD)
2890 mp4_es_id = pmt->mp4es[k].id;
2892 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4ESID: %d\n", mp4_es_id);
2893 for(k = 0; k < pmt->es_cnt; k++)
2895 if(pmt->es[k].mp4_es_id == mp4_es_id)
2897 section = &(tss->section);
2898 parse_sl_section(pmt, section, is_start, &packet[base], buf_size);
2901 continue;
2903 else
2905 progid = prog_id_in_pat(priv, pid);
2906 if(progid != -1)
2908 if(pid != demuxer->video->id && pid != demuxer->audio->id && pid != demuxer->sub->id)
2910 parse_pmt(priv, progid, pid, is_start, &packet[base], buf_size);
2911 continue;
2913 else
2914 mp_msg(MSGT_DEMUX, MSGL_ERR, "Argh! Data pid %d used in the PMT, Skipping PMT parsing!\n", pid);
2918 if(!probe && !dp)
2919 continue;
2921 if(is_start)
2923 uint8_t *lang = NULL;
2925 mp_msg(MSGT_DEMUX, MSGL_DBG2, "IS_START\n");
2927 len = pes_parse2(p, buf_size, es, pid_type, pmt, pid);
2928 if(! len)
2930 tss->is_synced = 0;
2931 continue;
2933 es->pid = tss->pid;
2934 tss->is_synced |= es->is_synced || rap_flag;
2935 tss->payload_size = es->payload_size;
2937 if(is_audio && (lang = pid_lang_from_pmt(priv, es->pid)))
2939 memcpy(es->lang, lang, 3);
2940 es->lang[3] = 0;
2942 else
2943 es->lang[0] = 0;
2945 if(probe)
2947 if(es->type == UNKNOWN)
2948 return 0;
2950 tss->type = es->type;
2951 tss->subtype = es->subtype;
2953 return 1;
2955 else
2957 if(es->pts == 0.0f)
2958 es->pts = tss->pts = tss->last_pts;
2959 else
2960 tss->pts = tss->last_pts = es->pts;
2962 mp_msg(MSGT_DEMUX, MSGL_DBG2, "ts_parse, NEW pid=%d, PSIZE: %u, type=%X, start=%p, len=%d\n",
2963 es->pid, es->payload_size, es->type, es->start, es->size);
2965 demuxer->filepos = stream_tell(demuxer->stream) - es->size;
2967 memmove(p, es->start, es->size);
2968 *dp_offset += es->size;
2969 (*dp)->flags = 0;
2970 (*dp)->pos = stream_tell(demuxer->stream);
2971 (*dp)->pts = es->pts;
2973 if(retv > 0)
2974 return retv;
2975 else
2976 continue;
2979 else
2981 uint16_t sz;
2983 es->pid = tss->pid;
2984 es->type = tss->type;
2985 es->subtype = tss->subtype;
2986 es->pts = tss->pts = tss->last_pts;
2987 es->start = &packet[base];
2990 if(tss->payload_size > 0)
2992 sz = FFMIN(tss->payload_size, buf_size);
2993 tss->payload_size -= sz;
2994 es->size = sz;
2996 else
2998 if(is_video)
3000 sz = es->size = buf_size;
3002 else
3004 continue;
3009 if(! probe)
3011 *dp_offset += sz;
3013 if(*dp_offset >= MAX_PACK_BYTES)
3015 (*dp)->pts = tss->last_pts;
3016 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
3017 return 1;
3020 continue;
3022 else
3024 memcpy(es->start, p, sz);
3026 if(es->size)
3027 return es->size;
3028 else
3029 continue;
3034 return 0;
3038 extern void skip_audio_frame(sh_audio_t *sh_audio);
3040 static void reset_fifos(ts_priv_t* priv, int a, int v, int s)
3042 if(a)
3044 if(priv->fifo[0].pack != NULL)
3046 free_demux_packet(priv->fifo[0].pack);
3047 priv->fifo[0].pack = NULL;
3049 priv->fifo[0].offset = 0;
3052 if(v)
3054 if(priv->fifo[1].pack != NULL)
3056 free_demux_packet(priv->fifo[1].pack);
3057 priv->fifo[1].pack = NULL;
3059 priv->fifo[1].offset = 0;
3062 if(s)
3064 if(priv->fifo[2].pack != NULL)
3066 free_demux_packet(priv->fifo[2].pack);
3067 priv->fifo[2].pack = NULL;
3069 priv->fifo[2].offset = 0;
3074 static void demux_seek_ts(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
3076 demux_stream_t *d_audio=demuxer->audio;
3077 demux_stream_t *d_video=demuxer->video;
3078 demux_stream_t *d_sub=demuxer->sub;
3079 sh_audio_t *sh_audio=d_audio->sh;
3080 sh_video_t *sh_video=d_video->sh;
3081 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
3082 int i, video_stats;
3083 off_t newpos;
3085 //================= seek in MPEG-TS ==========================
3087 ts_dump_streams(demuxer->priv);
3088 reset_fifos(priv, sh_audio != NULL, sh_video != NULL, demuxer->sub->id > 0);
3091 if(sh_audio != NULL)
3092 ds_free_packs(d_audio);
3093 if(sh_video != NULL)
3094 ds_free_packs(d_video);
3095 if(demuxer->sub->id > 0)
3096 ds_free_packs(d_sub);
3099 video_stats = (sh_video != NULL);
3100 if(video_stats)
3102 mp_msg(MSGT_DEMUX, MSGL_V, "IBPS: %d, vb: %d\r\n", sh_video->i_bps, priv->vbitrate);
3103 if(priv->vbitrate)
3104 video_stats = priv->vbitrate;
3105 else
3106 video_stats = sh_video->i_bps;
3109 newpos = (flags & 1) ? demuxer->movi_start : demuxer->filepos;
3110 if(flags & 2) // float seek 0..1
3111 newpos+=(demuxer->movi_end-demuxer->movi_start)*rel_seek_secs;
3112 else
3114 // time seek (secs)
3115 if(! video_stats) // unspecified or VBR
3116 newpos += 2324*75*rel_seek_secs; // 174.3 kbyte/sec
3117 else
3118 newpos += video_stats*rel_seek_secs;
3122 if(newpos < demuxer->movi_start)
3123 newpos = demuxer->movi_start; //begininng of stream
3125 stream_seek(demuxer->stream, newpos);
3126 for(i = 0; i < 8192; i++)
3127 if(priv->ts.pids[i] != NULL)
3128 priv->ts.pids[i]->is_synced = 0;
3130 videobuf_code_len = 0;
3132 if(sh_video != NULL)
3133 ds_fill_buffer(d_video);
3135 if(sh_audio != NULL)
3137 ds_fill_buffer(d_audio);
3140 while(sh_video != NULL)
3142 if(sh_audio && !d_audio->eof && d_video->pts && d_audio->pts)
3144 float a_pts=d_audio->pts;
3145 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
3146 if(d_video->pts > a_pts)
3148 skip_audio_frame(sh_audio); // sync audio
3149 continue;
3154 i = sync_video_packet(d_video);
3155 if((sh_video->format == VIDEO_MPEG1) || (sh_video->format == VIDEO_MPEG2))
3157 if(i==0x1B3 || i==0x1B8) break; // found it!
3159 else if((sh_video->format == VIDEO_MPEG4) && (i==0x1B6))
3160 break;
3161 else if(sh_video->format == VIDEO_VC1 && (i==0x10E || i==0x10F))
3162 break;
3163 else //H264
3165 if((i & ~0x60) == 0x105) break;
3168 if(!i || !skip_video_packet(d_video)) break; // EOF?
3173 static int demux_ts_fill_buffer(demuxer_t * demuxer, demux_stream_t *ds)
3175 ES_stream_t es;
3176 ts_priv_t *priv = (ts_priv_t *)demuxer->priv;
3178 return -ts_parse(demuxer, &es, priv->packet, 0);
3182 static int ts_check_file_dmx(demuxer_t *demuxer)
3184 return ts_check_file(demuxer) ? DEMUXER_TYPE_MPEG_TS : 0;
3187 static int is_usable_program(ts_priv_t *priv, pmt_t *pmt)
3189 int j;
3191 for(j = 0; j < pmt->es_cnt; j++)
3193 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3194 continue;
3196 priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO ||
3197 priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO
3199 return 1;
3202 return 0;
3205 static int demux_ts_control(demuxer_t *demuxer, int cmd, void *arg)
3207 ts_priv_t* priv = (ts_priv_t *)demuxer->priv;
3209 switch(cmd)
3211 case DEMUXER_CTRL_SWITCH_AUDIO:
3212 case DEMUXER_CTRL_SWITCH_VIDEO:
3214 void *sh = NULL;
3215 int i, n;
3216 int reftype, areset = 0, vreset = 0;
3217 demux_stream_t *ds;
3219 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
3221 reftype = TYPE_VIDEO;
3222 ds = demuxer->video;
3223 vreset = 1;
3225 else
3227 reftype = TYPE_AUDIO;
3228 ds = demuxer->audio;
3229 areset = 1;
3231 n = *((int*)arg);
3232 if(n == -2)
3234 reset_fifos(priv, areset, vreset, 0);
3235 ds->id = -2;
3236 ds->sh = NULL;
3237 ds_free_packs(ds);
3238 *((int*)arg) = ds->id;
3239 return DEMUXER_CTRL_OK;
3242 if(n < 0)
3244 for(i = 0; i < 8192; i++)
3246 if(priv->ts.streams[i].id == ds->id && priv->ts.streams[i].type == reftype)
3247 break;
3250 while(!sh)
3252 i = (i+1) % 8192;
3253 if(priv->ts.streams[i].type == reftype)
3255 if(priv->ts.streams[i].id == ds->id) //we made a complete loop
3256 break;
3257 sh = priv->ts.streams[i].sh;
3261 else //audio track <n>
3263 for(i = 0; i < 8192; i++)
3265 if(priv->ts.streams[i].id == n && priv->ts.streams[i].type == reftype)
3267 sh = priv->ts.streams[i].sh;
3268 break;
3273 if(sh)
3275 if(ds->id != priv->ts.streams[i].id)
3276 reset_fifos(priv, areset, vreset, 0);
3277 ds->id = priv->ts.streams[i].id;
3278 ds->sh = sh;
3279 ds_free_packs(ds);
3280 mp_msg(MSGT_DEMUX, MSGL_V, "\r\ndemux_ts, switched to audio pid %d, id: %d, sh: %p\r\n", i, ds->id, sh);
3283 *((int*)arg) = ds->id;
3284 return DEMUXER_CTRL_OK;
3287 case DEMUXER_CTRL_IDENTIFY_PROGRAM: //returns in prog->{aid,vid} the new ids that comprise a program
3289 int i, j, cnt=0;
3290 int vid_done=0, aid_done=0;
3291 pmt_t *pmt = NULL;
3292 demux_program_t *prog = arg;
3294 if(priv->pmt_cnt < 2)
3295 return DEMUXER_CTRL_NOTIMPL;
3297 if(prog->progid == -1)
3299 int cur_pmt_idx = 0;
3301 for(i = 0; i < priv->pmt_cnt; i++)
3302 if(priv->pmt[i].progid == priv->prog)
3304 cur_pmt_idx = i;
3305 break;
3308 i = (cur_pmt_idx + 1) % priv->pmt_cnt;
3309 while(i != cur_pmt_idx)
3311 pmt = &priv->pmt[i];
3312 cnt = is_usable_program(priv, pmt);
3313 if(cnt)
3314 break;
3315 i = (i + 1) % priv->pmt_cnt;
3318 else
3320 for(i = 0; i < priv->pmt_cnt; i++)
3321 if(priv->pmt[i].progid == prog->progid)
3323 pmt = &priv->pmt[i]; //required program
3324 cnt = is_usable_program(priv, pmt);
3328 if(!cnt)
3329 return DEMUXER_CTRL_NOTIMPL;
3331 //finally some food
3332 prog->aid = prog->vid = -2; //no audio and no video by default
3333 for(j = 0; j < pmt->es_cnt; j++)
3335 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3336 continue;
3338 if(!vid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO)
3340 vid_done = 1;
3341 prog->vid = priv->ts.streams[pmt->es[j].pid].id;
3343 else if(!aid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO)
3345 aid_done = 1;
3346 prog->aid = priv->ts.streams[pmt->es[j].pid].id;
3350 priv->prog = prog->progid = pmt->progid;
3351 return DEMUXER_CTRL_OK;
3354 default:
3355 return DEMUXER_CTRL_NOTIMPL;
3360 const demuxer_desc_t demuxer_desc_mpeg_ts = {
3361 "MPEG-TS demuxer",
3362 "mpegts",
3363 "TS",
3364 "Nico Sabbi",
3366 DEMUXER_TYPE_MPEG_TS,
3367 0, // unsafe autodetect
3368 ts_check_file_dmx,
3369 demux_ts_fill_buffer,
3370 demux_open_ts,
3371 demux_close_ts,
3372 demux_seek_ts,
3373 demux_ts_control