vo_corevideo: Send KEY_CLOSE_WIN instead of KEY_ESC for quit
[mplayer/glamo.git] / libmpdemux / demux_ts.c
blobec6718854cc310a270819f3ef4f7547a64ff66a0
1 /*
2 * Demultiplexer for MPEG2 Transport Streams.
4 * Written by Nico <nsabbi@libero.it>
5 * Kind feedback is appreciated; 'sucks' and alike is not.
6 * Originally based on demux_pva.c written by Matteo Giani and FFmpeg (libavformat) sources
8 * This file is part of MPlayer.
10 * MPlayer is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * MPlayer is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "config.h"
30 #include "mp_msg.h"
31 #include "options.h"
33 #include "stream/stream.h"
34 #include "demuxer.h"
35 #include "parse_es.h"
36 #include "stheader.h"
37 #include "ms_hdr.h"
38 #include "mpeg_hdr.h"
39 #include "demux_ts.h"
41 #define TS_PH_PACKET_SIZE 192
42 #define TS_FEC_PACKET_SIZE 204
43 #define TS_PACKET_SIZE 188
44 #define NB_PID_MAX 8192
46 #define MAX_HEADER_SIZE 6 /* enough for PES header + length */
47 #define MAX_CHECK_SIZE 65535
48 #define 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;
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_DIRAC = mmioFOURCC('d', 'r', 'a', 'c'),
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 AUDIO_AAC_LATM = mmioFOURCC('M', 'P', '4', 'L'),
80 AUDIO_TRUEHD = mmioFOURCC('T', 'R', 'H', 'D'),
81 SPU_DVD = 0x3000000,
82 SPU_DVB = 0x3000001,
83 SPU_TELETEXT = 0x3000002,
84 SPU_PGS = 0x3000003,
85 PES_PRIVATE1 = 0xBD00000,
86 SL_PES_STREAM = 0xD000000,
87 SL_SECTION = 0xD100000,
88 MP4_OD = 0xD200000,
89 } es_stream_type_t;
91 typedef struct {
92 uint8_t *buffer;
93 uint16_t buffer_len;
94 } ts_section_t;
96 typedef struct {
97 int size;
98 unsigned char *start;
99 uint16_t payload_size;
100 es_stream_type_t type, subtype;
101 double pts, last_pts;
102 int pid;
103 char lang[4];
104 int last_cc; // last cc code (-1 if first packet)
105 int is_synced;
106 ts_section_t section;
107 uint8_t *extradata;
108 int extradata_alloc, extradata_len;
109 struct {
110 uint8_t au_start, au_end, last_au_end;
111 } sl;
112 } ES_stream_t;
114 typedef struct {
115 void *sh;
116 int id;
117 int type;
118 } sh_av_t;
120 typedef struct MpegTSContext {
121 int packet_size; // raw packet size, including FEC if present e.g. 188 bytes
122 ES_stream_t *pids[NB_PID_MAX];
123 sh_av_t streams[NB_PID_MAX];
124 } MpegTSContext;
127 typedef struct {
128 demux_stream_t *ds;
129 demux_packet_t *pack;
130 int offset, buffer_size;
131 } av_fifo_t;
133 #define MAX_EXTRADATA_SIZE 64*1024
134 typedef struct {
135 int32_t object_type; //aka codec used
136 int32_t stream_type; //video, audio etc.
137 uint8_t buf[MAX_EXTRADATA_SIZE];
138 uint16_t buf_size;
139 uint8_t szm1;
140 } mp4_decoder_config_t;
142 typedef struct {
143 //flags
144 uint8_t flags;
145 uint8_t au_start;
146 uint8_t au_end;
147 uint8_t random_accesspoint;
148 uint8_t random_accesspoint_only;
149 uint8_t padding;
150 uint8_t use_ts;
151 uint8_t idle;
152 uint8_t duration;
154 uint32_t ts_resolution, ocr_resolution;
155 uint8_t ts_len, ocr_len, au_len, instant_bitrate_len, degr_len, au_seqnum_len, packet_seqnum_len;
156 uint32_t timescale;
157 uint16_t au_duration, cts_duration;
158 uint64_t ocr, dts, cts;
159 } mp4_sl_config_t;
161 typedef struct {
162 uint16_t id;
163 uint8_t flags;
164 mp4_decoder_config_t decoder;
165 mp4_sl_config_t sl;
166 } mp4_es_descr_t;
168 typedef struct {
169 uint16_t id;
170 uint8_t flags;
171 mp4_es_descr_t *es;
172 uint16_t es_cnt;
173 } mp4_od_t;
175 typedef struct {
176 uint8_t skip;
177 uint8_t table_id;
178 uint8_t ssi;
179 uint16_t section_length;
180 uint16_t ts_id;
181 uint8_t version_number;
182 uint8_t curr_next;
183 uint8_t section_number;
184 uint8_t last_section_number;
185 struct pat_progs_t {
186 uint16_t id;
187 uint16_t pmt_pid;
188 } *progs;
189 uint16_t progs_cnt;
190 ts_section_t section;
191 } pat_t;
193 typedef struct {
194 uint16_t progid;
195 uint8_t skip;
196 uint8_t table_id;
197 uint8_t ssi;
198 uint16_t section_length;
199 uint8_t version_number;
200 uint8_t curr_next;
201 uint8_t section_number;
202 uint8_t last_section_number;
203 uint16_t PCR_PID;
204 uint16_t prog_descr_length;
205 ts_section_t section;
206 uint16_t es_cnt;
207 struct pmt_es_t {
208 uint16_t pid;
209 uint32_t type; //it's 8 bit long, but cast to the right type as FOURCC
210 uint16_t descr_length;
211 uint8_t format_descriptor[5];
212 uint8_t lang[4];
213 uint16_t mp4_es_id;
214 } *es;
215 mp4_od_t iod, *od;
216 mp4_es_descr_t *mp4es;
217 int od_cnt, mp4es_cnt;
218 } pmt_t;
220 typedef struct {
221 uint64_t size;
222 float duration;
223 double first_pts;
224 double last_pts;
225 } TS_stream_info;
227 typedef struct {
228 MpegTSContext ts;
229 int last_pid;
230 av_fifo_t fifo[3]; //0 for audio, 1 for video, 2 for subs
231 pat_t pat;
232 pmt_t *pmt;
233 uint16_t pmt_cnt;
234 uint32_t prog;
235 uint32_t vbitrate;
236 int keep_broken;
237 int last_aid;
238 int last_vid;
239 int last_sid;
240 char packet[TS_FEC_PACKET_SIZE];
241 TS_stream_info vstr, astr;
242 } ts_priv_t;
245 typedef struct {
246 es_stream_type_t type;
247 ts_section_t section;
248 } TS_pids_t;
251 static int IS_AUDIO(es_stream_type_t type)
253 switch (type) {
254 case AUDIO_MP2:
255 case AUDIO_A52:
256 case AUDIO_LPCM_BE:
257 case AUDIO_AAC:
258 case AUDIO_AAC_LATM:
259 case AUDIO_DTS:
260 case AUDIO_TRUEHD:
261 return 1;
263 return 0;
266 static int IS_VIDEO(es_stream_type_t type)
268 switch (type) {
269 case VIDEO_MPEG1:
270 case VIDEO_MPEG2:
271 case VIDEO_MPEG4:
272 case VIDEO_H264:
273 case VIDEO_AVC:
274 case VIDEO_DIRAC:
275 case VIDEO_VC1:
276 return 1;
278 return 0;
281 static int IS_SUB(es_stream_type_t type)
283 switch (type) {
284 case SPU_DVD:
285 case SPU_DVB:
286 case SPU_PGS:
287 case SPU_TELETEXT:
288 return 1;
290 return 0;
293 static int ts_parse(demuxer_t *demuxer, ES_stream_t *es, unsigned char *packet, int probe);
295 static uint8_t get_packet_size(const unsigned char *buf, int size)
297 int i;
299 if (size < (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS))
300 return 0;
302 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
304 if (buf[i * TS_PACKET_SIZE] != 0x47)
306 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
307 goto try_fec;
310 return TS_PACKET_SIZE;
312 try_fec:
313 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
315 if (buf[i * TS_FEC_PACKET_SIZE] != 0x47){
316 mp_msg(MSGT_DEMUX, MSGL_DBG2, "GET_PACKET_SIZE, pos %d, char: %2x\n", i, buf[i * TS_PACKET_SIZE]);
317 goto try_philips;
320 return TS_FEC_PACKET_SIZE;
322 try_philips:
323 for(i=0; i<NUM_CONSECUTIVE_TS_PACKETS; i++)
325 if (buf[i * TS_PH_PACKET_SIZE] != 0x47)
326 return 0;
328 return TS_PH_PACKET_SIZE;
331 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h);
332 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid);
334 static void ts_add_stream(demuxer_t * demuxer, ES_stream_t *es)
336 int i;
337 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
339 if(priv->ts.streams[es->pid].sh)
340 return;
342 if((IS_AUDIO(es->type) || IS_AUDIO(es->subtype)) && priv->last_aid+1 < MAX_A_STREAMS)
344 sh_audio_t *sh = new_sh_audio_aid(demuxer, priv->last_aid, es->pid);
345 if(sh)
347 const char *lang = pid_lang_from_pmt(priv, es->pid);
348 sh->needs_parsing = 1;
349 sh->format = IS_AUDIO(es->type) ? es->type : es->subtype;
350 sh->ds = demuxer->audio;
352 priv->ts.streams[es->pid].id = priv->last_aid;
353 priv->ts.streams[es->pid].sh = sh;
354 priv->ts.streams[es->pid].type = TYPE_AUDIO;
355 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);
356 if (lang && lang[0])
357 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_AID_%d_LANG=%s\n", es->pid, lang);
358 priv->last_aid++;
361 if(es->extradata && es->extradata_len)
363 sh->wf = malloc(sizeof (WAVEFORMATEX) + es->extradata_len);
364 sh->wf->cbSize = es->extradata_len;
365 memcpy(sh->wf + 1, es->extradata, es->extradata_len);
369 if((IS_VIDEO(es->type) || IS_VIDEO(es->subtype)) && priv->last_vid+1 < MAX_V_STREAMS)
371 sh_video_t *sh = new_sh_video_vid(demuxer, priv->last_vid, es->pid);
372 if(sh)
374 sh->format = IS_VIDEO(es->type) ? es->type : es->subtype;
375 sh->ds = demuxer->video;
377 priv->ts.streams[es->pid].id = priv->last_vid;
378 priv->ts.streams[es->pid].sh = sh;
379 priv->ts.streams[es->pid].type = TYPE_VIDEO;
380 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);
381 priv->last_vid++;
384 if(sh->format == VIDEO_AVC && es->extradata && es->extradata_len)
386 int w = 0, h = 0;
387 sh->bih = calloc(1, sizeof(BITMAPINFOHEADER) + es->extradata_len);
388 sh->bih->biSize= sizeof(BITMAPINFOHEADER) + es->extradata_len;
389 sh->bih->biCompression = sh->format;
390 memcpy(sh->bih + 1, es->extradata, es->extradata_len);
391 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "EXTRADATA(%d BYTES): \n", es->extradata_len);
392 for(i = 0;i < es->extradata_len; i++)
393 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "%02x ", (int) es->extradata[i]);
394 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"\n");
395 if(parse_avc_sps(es->extradata, es->extradata_len, &w, &h))
397 sh->bih->biWidth = w;
398 sh->bih->biHeight = h;
404 if(IS_SUB(es->type) && priv->last_sid+1 < MAX_S_STREAMS)
406 sh_sub_t *sh = new_sh_sub_sid_lang(demuxer, priv->last_sid, es->pid, pid_lang_from_pmt(priv, es->pid));
407 if (sh) {
408 switch (es->type) {
409 case SPU_DVD:
410 sh->type = 'v'; break;
411 case SPU_PGS:
412 sh->type = 'p'; break;
414 priv->ts.streams[es->pid].id = priv->last_aid;
415 priv->ts.streams[es->pid].sh = sh;
416 priv->ts.streams[es->pid].type = TYPE_AUDIO;
417 priv->last_sid++;
422 static int ts_check_file(demuxer_t * demuxer)
424 const int buf_size = (TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS);
425 unsigned char buf[TS_FEC_PACKET_SIZE * NUM_CONSECUTIVE_TS_PACKETS], done = 0, *ptr;
426 uint32_t _read, i, count = 0, is_ts;
427 int cc[NB_PID_MAX], last_cc[NB_PID_MAX], pid, cc_ok, c, good, bad;
428 uint8_t size = 0;
429 off_t pos = 0;
430 off_t init_pos;
432 mp_msg(MSGT_DEMUX, MSGL_V, "Checking for MPEG-TS...\n");
434 init_pos = stream_tell(demuxer->stream);
435 is_ts = 0;
436 while(! done)
438 i = 1;
439 c = 0;
441 while(((c=stream_read_char(demuxer->stream)) != 0x47)
442 && (c >= 0)
443 && (i < MAX_CHECK_SIZE)
444 && ! demuxer->stream->eof
445 ) i++;
448 if(c != 0x47)
450 mp_msg(MSGT_DEMUX, MSGL_V, "THIS DOESN'T LOOK LIKE AN MPEG-TS FILE!\n");
451 is_ts = 0;
452 done = 1;
453 continue;
456 pos = stream_tell(demuxer->stream) - 1;
457 buf[0] = c;
458 _read = stream_read(demuxer->stream, &buf[1], buf_size-1);
460 if(_read < buf_size-1)
462 mp_msg(MSGT_DEMUX, MSGL_V, "COULDN'T READ ENOUGH DATA, EXITING TS_CHECK\n");
463 stream_reset(demuxer->stream);
464 return 0;
467 size = get_packet_size(buf, buf_size);
468 if(size)
470 done = 1;
471 is_ts = 1;
474 if(pos - init_pos >= MAX_CHECK_SIZE)
476 done = 1;
477 is_ts = 0;
481 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);
482 stream_seek(demuxer->stream, pos);
484 if(! is_ts)
485 return 0;
487 //LET'S CHECK continuity counters
488 good = bad = 0;
489 for(count = 0; count < NB_PID_MAX; count++)
491 cc[count] = last_cc[count] = -1;
494 for(count = 0; count < NUM_CONSECUTIVE_TS_PACKETS; count++)
496 ptr = &(buf[size * count]);
497 pid = ((ptr[1] & 0x1f) << 8) | ptr[2];
498 mp_msg(MSGT_DEMUX, MSGL_DBG2, "BUF: %02x %02x %02x %02x, PID %d, SIZE: %d \n",
499 ptr[0], ptr[1], ptr[2], ptr[3], pid, size);
501 if((pid == 8191) || (pid < 16))
502 continue;
504 cc[pid] = (ptr[3] & 0xf);
505 cc_ok = (last_cc[pid] < 0) || ((((last_cc[pid] + 1) & 0x0f) == cc[pid]));
506 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PID %d, COMPARE CC %d AND LAST_CC %d\n", pid, cc[pid], last_cc[pid]);
507 if(! cc_ok)
508 //return 0;
509 bad++;
510 else
511 good++;
513 last_cc[pid] = cc[pid];
516 mp_msg(MSGT_DEMUX, MSGL_V, "GOOD CC: %d, BAD CC: %d\n", good, bad);
518 if(good >= bad)
519 return size;
520 else
521 return 0;
525 static inline int32_t progid_idx_in_pmt(ts_priv_t *priv, uint16_t progid)
527 int x;
529 if(priv->pmt == NULL)
530 return -1;
532 for(x = 0; x < priv->pmt_cnt; x++)
534 if(priv->pmt[x].progid == progid)
535 return x;
538 return -1;
542 static inline int32_t progid_for_pid(ts_priv_t *priv, int pid, int32_t req) //finds the first program listing a pid
544 int i, j;
545 pmt_t *pmt;
548 if(priv->pmt == NULL)
549 return -1;
552 for(i=0; i < priv->pmt_cnt; i++)
554 pmt = &(priv->pmt[i]);
556 if(pmt->es == NULL)
557 return -1;
559 for(j = 0; j < pmt->es_cnt; j++)
561 if(pmt->es[j].pid == pid)
563 if((req == 0) || (req == pmt->progid))
564 return pmt->progid;
569 return -1;
572 static inline int32_t prog_pcr_pid(ts_priv_t *priv, int progid)
574 int i;
576 if(priv->pmt == NULL)
577 return -1;
578 for(i=0; i < priv->pmt_cnt; i++)
580 if(priv->pmt[i].progid == progid)
581 return priv->pmt[i].PCR_PID;
583 return -1;
587 static inline int pid_match_lang(ts_priv_t *priv, uint16_t pid, char *lang)
589 uint16_t i, j;
590 pmt_t *pmt;
592 if(priv->pmt == NULL)
593 return -1;
595 for(i=0; i < priv->pmt_cnt; i++)
597 pmt = &(priv->pmt[i]);
599 if(pmt->es == NULL)
600 return -1;
602 for(j = 0; j < pmt->es_cnt; j++)
604 if(pmt->es[j].pid != pid)
605 continue;
607 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);
608 if(strncmp(pmt->es[j].lang, lang, 3) == 0)
610 return 1;
616 return -1;
619 typedef struct {
620 int32_t atype, vtype, stype; //types
621 int32_t apid, vpid, spid; //stream ids
622 char alang[4]; //languages
623 uint16_t prog;
624 off_t probe;
625 } tsdemux_init_t;
627 //stripped down version of a52_syncinfo() from liba52
628 //copyright belongs to Michel Lespinasse <walken@zoy.org> and Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
629 int mp_a52_framesize(uint8_t * buf, int *srate)
631 int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
632 128, 160, 192, 224, 256, 320, 384, 448,
633 512, 576, 640
635 uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
636 int frmsizecod, bitrate, half;
638 if((buf[0] != 0x0b) || (buf[1] != 0x77)) /* syncword */
639 return 0;
641 if(buf[5] >= 0x60) /* bsid >= 12 */
642 return 0;
644 half = halfrate[buf[5] >> 3];
646 frmsizecod = buf[4] & 63;
647 if(frmsizecod >= 38)
648 return 0;
650 bitrate = rate[frmsizecod >> 1];
652 switch(buf[4] & 0xc0)
654 case 0: /* 48 KHz */
655 *srate = 48000 >> half;
656 return 4 * bitrate;
657 case 0x40: /* 44.1 KHz */
658 *srate = 44100 >> half;
659 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
660 case 0x80: /* 32 KHz */
661 *srate = 32000 >> half;
662 return 6 * bitrate;
665 return 0;
668 //second stage: returns the count of A52 syncwords found
669 static int a52_check(char *buf, int len)
671 int cnt, frame_length, ok, srate;
673 cnt = ok = 0;
674 if(len < 8)
675 return 0;
677 while(cnt < len - 7)
679 if(buf[cnt] == 0x0B && buf[cnt+1] == 0x77)
681 frame_length = mp_a52_framesize(&buf[cnt], &srate);
682 if(frame_length>=7 && frame_length<=3840)
684 cnt += frame_length;
685 ok++;
687 else
688 cnt++;
690 else
691 cnt++;
694 mp_msg(MSGT_DEMUXER, MSGL_V, "A52_CHECK(%d input bytes), found %d frame syncwords of %d bytes length\n", len, ok, frame_length);
695 return ok;
699 static off_t ts_detect_streams(demuxer_t *demuxer, tsdemux_init_t *param)
701 int video_found = 0, audio_found = 0, sub_found = 0, i, num_packets = 0, req_apid, req_vpid, req_spid;
702 int is_audio, is_video, is_sub, has_tables;
703 int32_t p, chosen_pid = 0;
704 off_t pos=0, ret = 0, init_pos, end_pos;
705 ES_stream_t es;
706 unsigned char tmp[TS_FEC_PACKET_SIZE];
707 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
708 struct {
709 char *buf;
710 int pos;
711 } pes_priv1[8192], *pptr;
712 char *tmpbuf;
714 priv->last_pid = 8192; //invalid pid
716 req_apid = param->apid;
717 req_vpid = param->vpid;
718 req_spid = param->spid;
720 has_tables = 0;
721 memset(pes_priv1, 0, sizeof(pes_priv1));
722 init_pos = stream_tell(demuxer->stream);
723 mp_msg(MSGT_DEMUXER, MSGL_V, "PROBING UP TO %"PRIu64", PROG: %d\n", (uint64_t) param->probe, param->prog);
724 end_pos = init_pos + (param->probe ? param->probe : TS_MAX_PROBE_SIZE);
725 while(1)
727 pos = stream_tell(demuxer->stream);
728 if(pos > end_pos || demuxer->stream->eof)
729 break;
731 if(ts_parse(demuxer, &es, tmp, 1))
733 //Non PES-aligned A52 audio may escape detection if PMT is not present;
734 //in this case we try to find at least 3 A52 syncwords
735 if((es.type == PES_PRIVATE1) && (! audio_found) && req_apid > -2)
737 pptr = &pes_priv1[es.pid];
738 if(pptr->pos < 64*1024)
740 tmpbuf = realloc(pptr->buf, pptr->pos + es.size);
741 if(tmpbuf != NULL)
743 pptr->buf = tmpbuf;
744 memcpy(&(pptr->buf[ pptr->pos ]), es.start, es.size);
745 pptr->pos += es.size;
746 if(a52_check(pptr->buf, pptr->pos) > 2)
748 param->atype = AUDIO_A52;
749 param->apid = es.pid;
750 es.type = AUDIO_A52;
756 is_audio = IS_AUDIO(es.type) || ((es.type==SL_PES_STREAM) && IS_AUDIO(es.subtype));
757 is_video = IS_VIDEO(es.type) || ((es.type==SL_PES_STREAM) && IS_VIDEO(es.subtype));
758 is_sub = IS_SUB(es.type);
761 if((! is_audio) && (! is_video) && (! is_sub))
762 continue;
763 if(is_audio && req_apid==-2)
764 continue;
766 if(is_video)
768 chosen_pid = (req_vpid == es.pid);
769 if((! chosen_pid) && (req_vpid > 0))
770 continue;
772 else if(is_audio)
774 if(req_apid > 0)
776 chosen_pid = (req_apid == es.pid);
777 if(! chosen_pid)
778 continue;
780 else if(param->alang[0] > 0 && es.lang[0] > 0)
782 if(pid_match_lang(priv, es.pid, param->alang) == -1)
783 continue;
785 chosen_pid = 1;
786 param->apid = req_apid = es.pid;
789 else if(is_sub)
791 chosen_pid = (req_spid == es.pid);
792 if((! chosen_pid) && (req_spid > 0))
793 continue;
796 if(req_apid < 0 && (param->alang[0] == 0) && req_vpid < 0 && req_spid < 0)
797 chosen_pid = 1;
799 if((ret == 0) && chosen_pid)
801 ret = stream_tell(demuxer->stream);
804 p = progid_for_pid(priv, es.pid, param->prog);
805 if(p != -1)
807 has_tables++;
808 if(!param->prog && chosen_pid)
809 param->prog = p;
812 if((param->prog > 0) && (param->prog != p))
814 if(audio_found)
816 if(is_video && (req_vpid == es.pid))
818 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
819 param->vpid = es.pid;
820 video_found = 1;
821 break;
825 if(video_found)
827 if(is_audio && (req_apid == es.pid))
829 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
830 param->apid = es.pid;
831 audio_found = 1;
832 break;
837 continue;
841 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "TYPE: %x, PID: %d, PROG FOUND: %d\n", es.type, es.pid, param->prog);
844 if(is_video)
846 if((req_vpid == -1) || (req_vpid == es.pid))
848 param->vtype = IS_VIDEO(es.type) ? es.type : es.subtype;
849 param->vpid = es.pid;
850 video_found = 1;
855 if(((req_vpid == -2) || (num_packets >= NUM_CONSECUTIVE_AUDIO_PACKETS)) && audio_found && !param->probe)
857 //novideo or we have at least 348 audio packets (64 KB) without video (TS with audio only)
858 param->vtype = 0;
859 break;
862 if(is_sub)
864 if((req_spid == -1) || (req_spid == es.pid))
866 param->stype = es.type;
867 param->spid = es.pid;
868 sub_found = 1;
872 if(is_audio)
874 if((req_apid == -1) || (req_apid == es.pid))
876 param->atype = IS_AUDIO(es.type) ? es.type : es.subtype;
877 param->apid = es.pid;
878 audio_found = 1;
882 if(audio_found && (param->apid == es.pid) && (! video_found))
883 num_packets++;
885 if((has_tables==0) && (video_found && audio_found) && (pos >= 1000000))
886 break;
890 for(i=0; i<8192; i++)
892 if(pes_priv1[i].buf != NULL)
894 free(pes_priv1[i].buf);
895 pes_priv1[i].buf = NULL;
896 pes_priv1[i].pos = 0;
900 if(video_found)
902 if(param->vtype == VIDEO_MPEG1)
903 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG1(pid=%d) ", param->vpid);
904 else if(param->vtype == VIDEO_MPEG2)
905 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG2(pid=%d) ", param->vpid);
906 else if(param->vtype == VIDEO_MPEG4)
907 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG4(pid=%d) ", param->vpid);
908 else if(param->vtype == VIDEO_H264)
909 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO H264(pid=%d) ", param->vpid);
910 else if(param->vtype == VIDEO_VC1)
911 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO VC1(pid=%d) ", param->vpid);
912 else if(param->vtype == VIDEO_AVC)
913 mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO AVC(NAL-H264, pid=%d) ", param->vpid);
915 else
917 param->vtype = UNKNOWN;
918 //WE DIDN'T MATCH ANY VIDEO STREAM
919 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO VIDEO! ");
922 if(param->atype == AUDIO_MP2)
923 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO MPA(pid=%d)", param->apid);
924 else if(param->atype == AUDIO_A52)
925 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO A52(pid=%d)", param->apid);
926 else if(param->atype == AUDIO_DTS)
927 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO DTS(pid=%d)", param->apid);
928 else if(param->atype == AUDIO_LPCM_BE)
929 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO LPCM(pid=%d)", param->apid);
930 else if(param->atype == AUDIO_AAC)
931 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO AAC(pid=%d)", param->apid);
932 else if(param->atype == AUDIO_AAC_LATM)
933 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO AAC LATM(pid=%d)", param->apid);
934 else if(param->atype == AUDIO_TRUEHD)
935 mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO TRUEHD(pid=%d)", param->apid);
936 else
938 audio_found = 0;
939 param->atype = UNKNOWN;
940 //WE DIDN'T MATCH ANY AUDIO STREAM, SO WE FORCE THE DEMUXER TO IGNORE AUDIO
941 mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO AUDIO! ");
944 if(IS_SUB(param->stype))
945 mp_msg(MSGT_DEMUXER, MSGL_INFO, " SUB %s(pid=%d) ", (param->stype==SPU_DVD ? "DVD" : param->stype==SPU_DVB ? "DVB" : "Teletext"), param->spid);
946 else
948 param->stype = UNKNOWN;
949 mp_msg(MSGT_DEMUXER, MSGL_INFO, " NO SUBS (yet)! ");
952 if(video_found || audio_found)
954 if(!param->prog)
956 p = progid_for_pid(priv, video_found ? param->vpid : param->apid, 0);
957 if(p != -1)
958 param->prog = p;
961 if(demuxer->stream->eof && (ret == 0))
962 ret = init_pos;
963 mp_msg(MSGT_DEMUXER, MSGL_INFO, " PROGRAM N. %d\n", param->prog);
965 else
966 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\n");
969 for(i=0; i<8192; i++)
971 if(priv->ts.pids[i] != NULL)
973 priv->ts.pids[i]->payload_size = 0;
974 priv->ts.pids[i]->pts = priv->ts.pids[i]->last_pts = 0;
975 priv->ts.pids[i]->last_cc = -1;
976 priv->ts.pids[i]->is_synced = 0;
980 return ret;
983 static int parse_avc_sps(uint8_t *buf, int len, int *w, int *h)
985 int sps, sps_len;
986 unsigned char *ptr;
987 mp_mpeg_header_t picture;
988 if(len < 6)
989 return 0;
990 sps = buf[5] & 0x1f;
991 if(!sps)
992 return 0;
993 sps_len = (buf[6] << 8) | buf[7];
994 if(!sps_len || (sps_len > len - 8))
995 return 0;
996 ptr = &(buf[8]);
997 picture.display_picture_width = picture.display_picture_height = 0;
998 h264_parse_sps(&picture, ptr, len - 8);
999 if(!picture.display_picture_width || !picture.display_picture_height)
1000 return 0;
1001 *w = picture.display_picture_width;
1002 *h = picture.display_picture_height;
1003 return 1;
1006 static demuxer_t *demux_open_ts(demuxer_t * demuxer)
1008 int i;
1009 uint8_t packet_size;
1010 sh_video_t *sh_video;
1011 sh_audio_t *sh_audio;
1012 off_t start_pos;
1013 tsdemux_init_t params;
1014 ts_priv_t * priv = demuxer->priv;
1016 mp_msg(MSGT_DEMUX, MSGL_V, "DEMUX OPEN, AUDIO_ID: %d, VIDEO_ID: %d, SUBTITLE_ID: %d,\n",
1017 demuxer->audio->id, demuxer->video->id, demuxer->sub->id);
1020 demuxer->type= DEMUXER_TYPE_MPEG_TS;
1023 stream_reset(demuxer->stream);
1025 packet_size = ts_check_file(demuxer);
1026 if(!packet_size)
1027 return NULL;
1029 priv = calloc(1, sizeof(ts_priv_t));
1030 if(priv == NULL)
1032 mp_msg(MSGT_DEMUX, MSGL_FATAL, "DEMUX_OPEN_TS, couldn't allocate enough memory for ts->priv, exit\n");
1033 return NULL;
1036 for(i=0; i < 8192; i++)
1038 priv->ts.pids[i] = NULL;
1039 priv->ts.streams[i].id = -3;
1041 priv->pat.progs = NULL;
1042 priv->pat.progs_cnt = 0;
1043 priv->pat.section.buffer = NULL;
1044 priv->pat.section.buffer_len = 0;
1046 priv->pmt = NULL;
1047 priv->pmt_cnt = 0;
1049 priv->keep_broken = ts_keep_broken;
1050 priv->ts.packet_size = packet_size;
1053 demuxer->priv = priv;
1054 if(demuxer->stream->type != STREAMTYPE_FILE)
1055 demuxer->seekable = 1;
1056 else
1057 demuxer->seekable = 1;
1060 params.atype = params.vtype = params.stype = UNKNOWN;
1061 params.apid = demuxer->audio->id;
1062 params.vpid = demuxer->video->id;
1063 params.spid = demuxer->sub->id;
1064 params.prog = ts_prog;
1065 params.probe = ts_probe;
1067 if(demuxer->opts->audio_lang != NULL)
1069 strncpy(params.alang, demuxer->opts->audio_lang, 3);
1070 params.alang[3] = 0;
1072 else
1073 memset(params.alang, 0, 4);
1075 start_pos = ts_detect_streams(demuxer, &params);
1077 demuxer->sub->id = params.spid;
1078 priv->prog = params.prog;
1080 if(params.vtype != UNKNOWN)
1082 ts_add_stream(demuxer, priv->ts.pids[params.vpid]);
1083 sh_video = priv->ts.streams[params.vpid].sh;
1084 demuxer->video->id = priv->ts.streams[params.vpid].id;
1085 sh_video->ds = demuxer->video;
1086 sh_video->format = params.vtype;
1087 demuxer->video->sh = sh_video;
1090 if(params.atype != UNKNOWN)
1092 ES_stream_t *es = priv->ts.pids[params.apid];
1094 if(!IS_AUDIO(es->type) && !IS_AUDIO(es->subtype) && IS_AUDIO(params.atype)) es->subtype = params.atype;
1095 ts_add_stream(demuxer, priv->ts.pids[params.apid]);
1096 sh_audio = priv->ts.streams[params.apid].sh;
1097 demuxer->audio->id = priv->ts.streams[params.apid].id;
1098 sh_audio->ds = demuxer->audio;
1099 sh_audio->format = params.atype;
1100 demuxer->audio->sh = sh_audio;
1104 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);
1107 start_pos = (start_pos <= priv->ts.packet_size ? 0 : start_pos - priv->ts.packet_size);
1108 demuxer->movi_start = start_pos;
1109 demuxer->reference_clock = MP_NOPTS_VALUE;
1110 stream_reset(demuxer->stream);
1111 stream_seek(demuxer->stream, start_pos); //IF IT'S FROM A PIPE IT WILL FAIL, BUT WHO CARES?
1114 priv->last_pid = 8192; //invalid pid
1116 for(i = 0; i < 3; i++)
1118 priv->fifo[i].pack = NULL;
1119 priv->fifo[i].offset = 0;
1121 priv->fifo[0].ds = demuxer->audio;
1122 priv->fifo[1].ds = demuxer->video;
1123 priv->fifo[2].ds = demuxer->sub;
1125 priv->fifo[0].buffer_size = 1536;
1126 priv->fifo[1].buffer_size = 32767;
1127 priv->fifo[2].buffer_size = 32767;
1129 priv->pat.section.buffer_len = 0;
1130 for(i = 0; i < priv->pmt_cnt; i++)
1131 priv->pmt[i].section.buffer_len = 0;
1133 demuxer->filepos = stream_tell(demuxer->stream);
1134 return demuxer;
1137 static void demux_close_ts(demuxer_t * demuxer)
1139 uint16_t i;
1140 ts_priv_t *priv = (ts_priv_t*) demuxer->priv;
1142 if(priv)
1144 if(priv->pat.section.buffer)
1145 free(priv->pat.section.buffer);
1146 if(priv->pat.progs)
1147 free(priv->pat.progs);
1149 if(priv->pmt)
1151 for(i = 0; i < priv->pmt_cnt; i++)
1153 if(priv->pmt[i].section.buffer)
1154 free(priv->pmt[i].section.buffer);
1155 if(priv->pmt[i].es)
1156 free(priv->pmt[i].es);
1158 free(priv->pmt);
1160 free(priv);
1162 demuxer->priv=NULL;
1166 #define getbits mp_getbits
1168 static int mp4_parse_sl_packet(pmt_t *pmt, uint8_t *buf, uint16_t packet_len, int pid, ES_stream_t *pes_es)
1170 int i, n, m, mp4_es_id = -1;
1171 uint64_t v = 0;
1172 uint32_t pl_size = 0;
1173 int deg_flag = 0;
1174 mp4_es_descr_t *es = NULL;
1175 mp4_sl_config_t *sl = NULL;
1176 uint8_t au_start = 0, au_end = 0, rap_flag = 0, ocr_flag = 0, padding = 0, padding_bits = 0, idle = 0;
1178 pes_es->is_synced = 0;
1179 mp_msg(MSGT_DEMUXER,MSGL_V, "mp4_parse_sl_packet, pid: %d, pmt: %pm, packet_len: %d\n", pid, pmt, packet_len);
1180 if(! pmt || !packet_len)
1181 return 0;
1183 for(i = 0; i < pmt->es_cnt; i++)
1185 if(pmt->es[i].pid == pid)
1186 mp4_es_id = pmt->es[i].mp4_es_id;
1188 if(mp4_es_id < 0)
1189 return -1;
1191 for(i = 0; i < pmt->mp4es_cnt; i++)
1193 if(pmt->mp4es[i].id == mp4_es_id)
1194 es = &(pmt->mp4es[i]);
1196 if(! es)
1197 return -1;
1199 pes_es->subtype = es->decoder.object_type;
1201 sl = &(es->sl);
1202 if(!sl)
1203 return -1;
1205 //now es is the complete es_descriptor of out mp4 ES stream
1206 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "ID: %d, FLAGS: 0x%x, subtype: %x\n", es->id, sl->flags, pes_es->subtype);
1208 n = 0;
1209 if(sl->au_start)
1210 pes_es->sl.au_start = au_start = getbits(buf, n++, 1);
1211 else
1212 pes_es->sl.au_start = (pes_es->sl.last_au_end ? 1 : 0);
1213 if(sl->au_end)
1214 pes_es->sl.au_end = au_end = getbits(buf, n++, 1);
1216 if(!sl->au_start && !sl->au_end)
1218 pes_es->sl.au_start = pes_es->sl.au_end = au_start = au_end = 1;
1220 pes_es->sl.last_au_end = pes_es->sl.au_end;
1223 if(sl->ocr_len > 0)
1224 ocr_flag = getbits(buf, n++, 1);
1225 if(sl->idle)
1226 idle = getbits(buf, n++, 1);
1227 if(sl->padding)
1228 padding = getbits(buf, n++, 1);
1229 if(padding)
1231 padding_bits = getbits(buf, n, 3);
1232 n += 3;
1235 if(idle || (padding && !padding_bits))
1237 pes_es->payload_size = 0;
1238 return -1;
1241 //(! idle && (!padding || padding_bits != 0)) is true
1242 n += sl->packet_seqnum_len;
1243 if(sl->degr_len)
1244 deg_flag = getbits(buf, n++, 1);
1245 if(deg_flag)
1246 n += sl->degr_len;
1248 if(ocr_flag)
1250 n += sl->ocr_len;
1251 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "OCR: %d bits\n", sl->ocr_len);
1254 if(packet_len * 8 <= n)
1255 return -1;
1257 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "\nAU_START: %d, AU_END: %d\n", au_start, au_end);
1258 if(au_start)
1260 int dts_flag = 0, cts_flag = 0, ib_flag = 0;
1262 if(sl->random_accesspoint)
1263 rap_flag = getbits(buf, n++, 1);
1265 //check commented because it seems it's rarely used, and we need this flag set in case of au_start
1266 //the decoder will eventually discard the payload if it can't decode it
1267 //if(rap_flag || sl->random_accesspoint_only)
1268 pes_es->is_synced = 1;
1270 n += sl->au_seqnum_len;
1271 if(packet_len * 8 <= n+8)
1272 return -1;
1273 if(sl->use_ts)
1275 dts_flag = getbits(buf, n++, 1);
1276 cts_flag = getbits(buf, n++, 1);
1278 if(sl->instant_bitrate_len)
1279 ib_flag = getbits(buf, n++, 1);
1280 if(packet_len * 8 <= n+8)
1281 return -1;
1282 if(dts_flag && (sl->ts_len > 0))
1284 n += sl->ts_len;
1285 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "DTS: %d bits\n", sl->ts_len);
1287 if(packet_len * 8 <= n+8)
1288 return -1;
1289 if(cts_flag && (sl->ts_len > 0))
1291 int i = 0, m;
1293 while(i < sl->ts_len)
1295 m = FFMIN(8, sl->ts_len - i);
1296 v |= getbits(buf, n, m);
1297 if(sl->ts_len - i > 8)
1298 v <<= 8;
1299 i += m;
1300 n += m;
1301 if(packet_len * 8 <= n+8)
1302 return -1;
1305 pes_es->pts = (double) v / (double) sl->ts_resolution;
1306 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "CTS: %d bits, value: %"PRIu64"/%d = %.3f\n", sl->ts_len, v, sl->ts_resolution, pes_es->pts);
1310 i = 0;
1311 pl_size = 0;
1312 while(i < sl->au_len)
1314 m = FFMIN(8, sl->au_len - i);
1315 pl_size |= getbits(buf, n, m);
1316 if(sl->au_len - i > 8)
1317 pl_size <<= 8;
1318 i += m;
1319 n += m;
1320 if(packet_len * 8 <= n+8)
1321 return -1;
1323 mp_msg(MSGT_DEMUXER,MSGL_DBG2, "AU_LEN: %u (%d bits)\n", pl_size, sl->au_len);
1324 if(ib_flag)
1325 n += sl->instant_bitrate_len;
1328 m = (n+7)/8;
1329 if(0 < pl_size && pl_size < pes_es->payload_size)
1330 pes_es->payload_size = pl_size;
1332 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",
1333 n, m, pes_es->payload_size, pl_size, (int) rap_flag, (int) sl->random_accesspoint_only);
1335 return m;
1338 //this function parses the extension fields in the PES header and returns the substream_id, or -1 in case of errors
1339 static int parse_pes_extension_fields(unsigned char *p, int pkt_len)
1341 int skip;
1342 unsigned char flags;
1344 if(!(p[7] & 0x1)) //no extension_field
1345 return -1;
1346 skip = 9;
1347 if(p[7] & 0x80)
1349 skip += 5;
1350 if(p[7] & 0x40)
1351 skip += 5;
1353 if(p[7] & 0x20) //escr_flag
1354 skip += 6;
1355 if(p[7] & 0x10) //es_rate_flag
1356 skip += 3;
1357 if(p[7] & 0x08)//dsm_trick_mode is unsupported, skip
1359 skip = 0;//don't let's parse the extension fields
1361 if(p[7] & 0x04) //additional_copy_info
1362 skip += 1;
1363 if(p[7] & 0x02) //pes_crc_flag
1364 skip += 2;
1365 if(skip >= pkt_len) //too few bytes
1366 return -1;
1367 flags = p[skip];
1368 skip++;
1369 if(flags & 0x80) //pes_private_data_flag
1370 skip += 16;
1371 if(skip >= pkt_len)
1372 return -1;
1373 if(flags & 0x40) //pack_header_field_flag
1375 unsigned char l = p[skip];
1376 skip += l;
1378 if(flags & 0x20) //program_packet_sequence_counter
1379 skip += 2;
1380 if(flags & 0x10) //p_std
1381 skip += 2;
1382 if(skip >= pkt_len)
1383 return -1;
1384 if(flags & 0x01) //finally the long desired pes_extension2
1386 unsigned char l = p[skip]; //ext2 flag+len
1387 skip++;
1388 if((l == 0x81) && (skip < pkt_len))
1390 int ssid = p[skip];
1391 mp_msg(MSGT_IDENTIFY, MSGL_V, "SUBSTREAM_ID=%d (0x%02X)\n", ssid, ssid);
1392 return ssid;
1396 return -1;
1399 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)
1401 unsigned char *p;
1402 uint32_t header_len;
1403 int64_t pts;
1404 uint32_t stream_id;
1405 uint32_t pkt_len, pes_is_aligned;
1407 //Here we are always at the start of a PES packet
1408 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2(%p, %d): \n", buf, (uint32_t) packet_len);
1410 if(packet_len == 0 || packet_len > 184)
1412 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2, BUFFER LEN IS TOO SMALL OR TOO BIG: %d EXIT\n", packet_len);
1413 return 0;
1416 p = buf;
1417 pkt_len = packet_len;
1420 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: HEADER %02x %02x %02x %02x\n", p[0], p[1], p[2], p[3]);
1421 if (p[0] || p[1] || (p[2] != 1))
1423 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: error HEADER %02x %02x %02x (should be 0x000001) \n", p[0], p[1], p[2]);
1424 return 0 ;
1427 packet_len -= 6;
1428 if(packet_len==0)
1430 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: packet too short: %d, exit\n", packet_len);
1431 return 0;
1434 es->payload_size = (p[4] << 8 | p[5]);
1435 pes_is_aligned = (p[6] & 4);
1437 stream_id = p[3];
1440 if (p[7] & 0x80)
1441 { /* pts available */
1442 pts = (int64_t)(p[9] & 0x0E) << 29 ;
1443 pts |= p[10] << 22 ;
1444 pts |= (p[11] & 0xFE) << 14 ;
1445 pts |= p[12] << 7 ;
1446 pts |= (p[13] & 0xFE) >> 1 ;
1448 es->pts = pts / 90000.0;
1450 else
1451 es->pts = 0.0;
1454 header_len = p[8];
1457 if (header_len + 9 > pkt_len) //9 are the bytes read up to the header_length field
1459 mp_msg(MSGT_DEMUX, MSGL_DBG2, "demux_ts: illegal value for PES_header_data_length (0x%02x)\n", header_len);
1460 return 0;
1463 if(stream_id==0xfd)
1465 int ssid = parse_pes_extension_fields(p, pkt_len);
1466 if((audio_substream_id!=-1) && (ssid != audio_substream_id))
1467 return 0;
1468 if(ssid == 0x72 && type_from_pmt != AUDIO_DTS && type_from_pmt != SPU_PGS)
1469 es->type = type_from_pmt = AUDIO_TRUEHD;
1472 p += header_len + 9;
1473 packet_len -= header_len + 3;
1475 if(es->payload_size)
1476 es->payload_size -= header_len + 3;
1479 es->is_synced = 1; //only for SL streams we have to make sure it's really true, see below
1480 if (stream_id == 0xbd)
1482 mp_msg(MSGT_DEMUX, MSGL_DBG3, "pes_parse2: audio buf = %02X %02X %02X %02X %02X %02X %02X %02X, 80: %d\n",
1483 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[0] & 0x80);
1487 * we check the descriptor tag first because some stations
1488 * do not include any of the A52 header info in their audio tracks
1489 * these "raw" streams may begin with a byte that looks like a stream type.
1493 if(type_from_pmt == SPU_PGS)
1495 es->start = p;
1496 es->size = packet_len;
1497 es->type = SPU_PGS;
1498 es->payload_size -= packet_len;
1499 return 1;
1502 (type_from_pmt == AUDIO_A52) || /* A52 - raw */
1503 (packet_len >= 2 && p[0] == 0x0B && p[1] == 0x77) /* A52 - syncword */
1506 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 RAW OR SYNCWORD\n");
1507 es->start = p;
1508 es->size = packet_len;
1509 es->type = AUDIO_A52;
1510 es->payload_size -= packet_len;
1512 return 1;
1514 /* SPU SUBS */
1515 else if(type_from_pmt == SPU_DVB ||
1516 (packet_len >= 1 && (p[0] == 0x20) && pes_is_aligned)) // && p[1] == 0x00))
1518 es->start = p;
1519 es->size = packet_len;
1520 es->type = SPU_DVB;
1521 es->payload_size -= packet_len;
1523 return 1;
1525 else if (pes_is_aligned && packet_len >= 1 && ((p[0] & 0xE0) == 0x20)) //SPU_DVD
1527 //DVD SUBS
1528 es->start = p+1;
1529 es->size = packet_len-1;
1530 es->type = SPU_DVD;
1531 es->payload_size -= packet_len;
1533 return 1;
1535 else if (pes_is_aligned && packet_len >= 4 && (p[0] & 0xF8) == 0x80)
1537 mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 WITH HEADER\n");
1538 es->start = p+4;
1539 es->size = packet_len - 4;
1540 es->type = AUDIO_A52;
1541 es->payload_size -= packet_len;
1543 return 1;
1545 else if (pes_is_aligned && packet_len >= 1 && ((p[0]&0xf0) == 0xa0))
1547 int pcm_offset;
1549 for (pcm_offset=0; ++pcm_offset < packet_len-1 ; )
1551 if (p[pcm_offset] == 0x01 && p[pcm_offset+1] == 0x80)
1552 { /* START */
1553 pcm_offset += 2;
1554 break;
1558 es->start = p + pcm_offset;
1559 es->size = packet_len - pcm_offset;
1560 es->type = AUDIO_LPCM_BE;
1561 es->payload_size -= packet_len;
1563 return 1;
1565 else
1567 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PES_PRIVATE1\n");
1568 es->start = p;
1569 es->size = packet_len;
1570 es->type = (type_from_pmt == UNKNOWN ? PES_PRIVATE1 : type_from_pmt);
1571 es->payload_size -= packet_len;
1573 return 1;
1576 else if(((stream_id >= 0xe0) && (stream_id <= 0xef)) || (stream_id == 0xfd && type_from_pmt != UNKNOWN))
1578 es->start = p;
1579 es->size = packet_len;
1580 if(type_from_pmt != UNKNOWN)
1581 es->type = type_from_pmt;
1582 else
1583 es->type = VIDEO_MPEG2;
1584 if(es->payload_size)
1585 es->payload_size -= packet_len;
1587 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: M2V size %d\n", es->size);
1588 return 1;
1590 else if ((stream_id == 0xfa))
1592 int l;
1594 es->is_synced = 0;
1595 if(type_from_pmt != UNKNOWN) //MP4 A/V or SL
1597 es->start = p;
1598 es->size = packet_len;
1599 es->type = type_from_pmt;
1601 if(type_from_pmt == SL_PES_STREAM)
1603 //if(pes_is_aligned)
1605 l = mp4_parse_sl_packet(pmt, p, packet_len, pid, es);
1606 mp_msg(MSGT_DEMUX, MSGL_DBG2, "L=%d, TYPE=%x\n", l, type_from_pmt);
1607 if(l < 0)
1609 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: couldn't parse SL header, passing along full PES payload\n");
1610 l = 0;
1614 es->start += l;
1615 es->size -= l;
1618 if(es->payload_size)
1619 es->payload_size -= packet_len;
1620 return 1;
1623 else if ((stream_id & 0xe0) == 0xc0)
1625 es->start = p;
1626 es->size = packet_len;
1628 if(type_from_pmt != UNKNOWN)
1629 es->type = type_from_pmt;
1630 else
1631 es->type = AUDIO_MP2;
1633 es->payload_size -= packet_len;
1635 return 1;
1637 else if (type_from_pmt != -1) //as a last resort here we trust the PMT, if present
1639 es->start = p;
1640 es->size = packet_len;
1641 es->type = type_from_pmt;
1642 es->payload_size -= packet_len;
1644 return 1;
1646 else
1648 mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: unknown packet, id: %x\n", stream_id);
1651 es->is_synced = 0;
1652 return 0;
1658 static int ts_sync(stream_t *stream)
1660 int c=0;
1662 mp_msg(MSGT_DEMUX, MSGL_DBG3, "TS_SYNC \n");
1664 while(((c=stream_read_char(stream)) != 0x47) && ! stream->eof);
1666 if(c == 0x47)
1667 return c;
1668 else
1669 return 0;
1673 static void ts_dump_streams(ts_priv_t *priv)
1675 int i;
1677 for(i = 0; i < 3; i++)
1679 if((priv->fifo[i].pack != NULL) && (priv->fifo[i].offset != 0))
1681 resize_demux_packet(priv->fifo[i].pack, priv->fifo[i].offset);
1682 ds_add_packet(priv->fifo[i].ds, priv->fifo[i].pack);
1683 priv->fifo[i].offset = 0;
1684 priv->fifo[i].pack = NULL;
1690 static inline int32_t prog_idx_in_pat(ts_priv_t *priv, uint16_t progid)
1692 int x;
1694 if(priv->pat.progs == NULL)
1695 return -1;
1697 for(x = 0; x < priv->pat.progs_cnt; x++)
1699 if(priv->pat.progs[x].id == progid)
1700 return x;
1703 return -1;
1707 static inline int32_t prog_id_in_pat(ts_priv_t *priv, uint16_t pid)
1709 int x;
1711 if(priv->pat.progs == NULL)
1712 return -1;
1714 for(x = 0; x < priv->pat.progs_cnt; x++)
1716 if(priv->pat.progs[x].pmt_pid == pid)
1717 return priv->pat.progs[x].id;
1720 return -1;
1723 static int collect_section(ts_section_t *section, int is_start, unsigned char *buff, int size)
1725 uint8_t *ptr;
1726 uint16_t tlen;
1727 int skip, tid;
1729 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, start: %d, size: %d, collected: %d\n", is_start, size, section->buffer_len);
1730 if(! is_start && !section->buffer_len)
1731 return 0;
1733 if(is_start)
1735 if(! section->buffer)
1737 section->buffer = malloc(4096 + 256);
1738 if(section->buffer == NULL)
1739 return 0;
1741 section->buffer_len = 0;
1744 if(size + section->buffer_len > 4096+256)
1746 mp_msg(MSGT_DEMUX, MSGL_V, "COLLECT_SECTION, excessive len: %d + %d\n", section->buffer_len, size);
1747 return 0;
1750 memcpy(&(section->buffer[section->buffer_len]), buff, size);
1751 section->buffer_len += size;
1753 if(section->buffer_len < 3)
1754 return 0;
1756 skip = section->buffer[0];
1757 if(skip + 4 > section->buffer_len)
1758 return 0;
1760 ptr = &(section->buffer[skip + 1]);
1761 tid = ptr[0];
1762 tlen = ((ptr[1] & 0x0f) << 8) | ptr[2];
1763 mp_msg(MSGT_DEMUX, MSGL_V, "SKIP: %d+1, TID: %d, TLEN: %d, COLLECTED: %d\n", skip, tid, tlen, section->buffer_len);
1764 if(section->buffer_len < (skip+1+3+tlen))
1766 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DATA IS NOT ENOUGH, NEXT TIME\n");
1767 return 0;
1770 return skip+1;
1773 static int parse_pat(ts_priv_t * priv, int is_start, unsigned char *buff, int size)
1775 int skip;
1776 unsigned char *ptr;
1777 unsigned char *base;
1778 int entries, i;
1779 uint16_t progid;
1780 struct pat_progs_t *tmp;
1781 ts_section_t *section;
1783 section = &(priv->pat.section);
1784 skip = collect_section(section, is_start, buff, size);
1785 if(! skip)
1786 return 0;
1788 ptr = &(section->buffer[skip]);
1789 //PARSING
1790 priv->pat.table_id = ptr[0];
1791 if(priv->pat.table_id != 0)
1792 return 0;
1793 priv->pat.ssi = (ptr[1] >> 7) & 0x1;
1794 priv->pat.curr_next = ptr[5] & 0x01;
1795 priv->pat.ts_id = (ptr[3] << 8 ) | ptr[4];
1796 priv->pat.version_number = (ptr[5] >> 1) & 0x1F;
1797 priv->pat.section_length = ((ptr[1] & 0x03) << 8 ) | ptr[2];
1798 priv->pat.section_number = ptr[6];
1799 priv->pat.last_section_number = ptr[7];
1801 //check_crc32(0xFFFFFFFFL, ptr, priv->pat.buffer_len - 4, &ptr[priv->pat.buffer_len - 4]);
1802 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);
1804 entries = (int) (priv->pat.section_length - 9) / 4; //entries per section
1806 for(i=0; i < entries; i++)
1808 int32_t idx;
1809 base = &ptr[8 + i*4];
1810 progid = (base[0] << 8) | base[1];
1812 if((idx = prog_idx_in_pat(priv, progid)) == -1)
1814 int sz = sizeof(struct pat_progs_t) * (priv->pat.progs_cnt+1);
1815 tmp = realloc_struct(priv->pat.progs, priv->pat.progs_cnt+1, sizeof(struct pat_progs_t));
1816 if(tmp == NULL)
1818 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PAT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
1819 break;
1821 priv->pat.progs = tmp;
1822 idx = priv->pat.progs_cnt;
1823 priv->pat.progs_cnt++;
1826 priv->pat.progs[idx].id = progid;
1827 priv->pat.progs[idx].pmt_pid = ((base[2] & 0x1F) << 8) | base[3];
1828 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);
1829 mp_msg(MSGT_IDENTIFY, MSGL_V, "PROGRAM_ID=%d (0x%02X), PMT_PID: %d(0x%02X)\n",
1830 progid, progid, priv->pat.progs[idx].pmt_pid, priv->pat.progs[idx].pmt_pid);
1833 return 1;
1837 static inline int32_t es_pid_in_pmt(pmt_t * pmt, uint16_t pid)
1839 uint16_t i;
1841 if(pmt == NULL)
1842 return -1;
1844 if(pmt->es == NULL)
1845 return -1;
1847 for(i = 0; i < pmt->es_cnt; i++)
1849 if(pmt->es[i].pid == pid)
1850 return (int32_t) i;
1853 return -1;
1857 static uint16_t get_mp4_desc_len(uint8_t *buf, int *len)
1859 //uint16_t i = 0, size = 0;
1860 int i = 0, j, size = 0;
1862 mp_msg(MSGT_DEMUX, MSGL_DBG2, "PARSE_MP4_DESC_LEN(%d), bytes: ", *len);
1863 j = FFMIN(*len, 4);
1864 while(i < j)
1866 mp_msg(MSGT_DEMUX, MSGL_DBG2, " %x ", buf[i]);
1867 size |= (buf[i] & 0x7f);
1868 if(!(buf[i] & 0x80))
1869 break;
1870 size <<= 7;
1871 i++;
1873 mp_msg(MSGT_DEMUX, MSGL_DBG2, ", SIZE=%d\n", size);
1875 *len = i+1;
1876 return size;
1880 static uint16_t parse_mp4_slconfig_descriptor(uint8_t *buf, int len, void *elem)
1882 int i = 0;
1883 mp4_es_descr_t *es;
1884 mp4_sl_config_t *sl;
1886 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_SLCONFIG_DESCRIPTOR(%d)\n", len);
1887 es = (mp4_es_descr_t *) elem;
1888 if(!es)
1890 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1891 return len;
1893 sl = &(es->sl);
1895 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;
1896 sl->ocr = sl->dts = sl->cts = 0;
1898 if(buf[0] == 0)
1900 i++;
1901 sl->flags = buf[i];
1902 i++;
1903 sl->ts_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1904 i += 4;
1905 sl->ocr_resolution = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1906 i += 4;
1907 sl->ts_len = buf[i];
1908 i++;
1909 sl->ocr_len = buf[i];
1910 i++;
1911 sl->au_len = buf[i];
1912 i++;
1913 sl->instant_bitrate_len = buf[i];
1914 i++;
1915 sl->degr_len = (buf[i] >> 4) & 0x0f;
1916 sl->au_seqnum_len = ((buf[i] & 0x0f) << 1) | ((buf[i+1] >> 7) & 0x01);
1917 i++;
1918 sl->packet_seqnum_len = ((buf[i] >> 2) & 0x1f);
1919 i++;
1922 else if(buf[0] == 1)
1924 sl->flags = 0;
1925 sl->ts_resolution = 1000;
1926 sl->ts_len = 32;
1927 i++;
1929 else if(buf[0] == 2)
1931 sl->flags = 4;
1932 i++;
1934 else
1936 sl->flags = 0;
1937 i++;
1940 sl->au_start = (sl->flags >> 7) & 0x1;
1941 sl->au_end = (sl->flags >> 6) & 0x1;
1942 sl->random_accesspoint = (sl->flags >> 5) & 0x1;
1943 sl->random_accesspoint_only = (sl->flags >> 4) & 0x1;
1944 sl->padding = (sl->flags >> 3) & 0x1;
1945 sl->use_ts = (sl->flags >> 2) & 0x1;
1946 sl->idle = (sl->flags >> 1) & 0x1;
1947 sl->duration = sl->flags & 0x1;
1949 if(sl->duration)
1951 sl->timescale = (buf[i] << 24) | (buf[i+1] << 16) | (buf[i+2] << 8) | buf[i+3];
1952 i += 4;
1953 sl->au_duration = (buf[i] << 8) | buf[i+1];
1954 i += 2;
1955 sl->cts_duration = (buf[i] << 8) | buf[i+1];
1956 i += 2;
1958 else //no support for fixed durations atm
1959 sl->timescale = sl->au_duration = sl->cts_duration = 0;
1961 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",
1962 len, buf[0], sl->flags, sl->use_ts, sl->ts_len, sl->timescale, (uint64_t) sl->dts, (uint64_t) sl->cts);
1964 return len;
1967 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem);
1969 static uint16_t parse_mp4_decoder_config_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
1971 int i = 0, j;
1972 mp4_es_descr_t *es;
1973 mp4_decoder_config_t *dec;
1975 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_CONFIG_DESCRIPTOR(%d)\n", len);
1976 es = (mp4_es_descr_t *) elem;
1977 if(!es)
1979 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
1980 return len;
1982 dec = (mp4_decoder_config_t*) &(es->decoder);
1984 dec->object_type = buf[i];
1985 dec->stream_type = (buf[i+1]>>2) & 0x3f;
1987 if(dec->object_type == 1 && dec->stream_type == 1)
1989 dec->object_type = MP4_OD;
1990 dec->stream_type = MP4_OD;
1992 else if(dec->stream_type == 4)
1994 if(dec->object_type == 0x6a)
1995 dec->object_type = VIDEO_MPEG1;
1996 if(dec->object_type >= 0x60 && dec->object_type <= 0x65)
1997 dec->object_type = VIDEO_MPEG2;
1998 else if(dec->object_type == 0x20)
1999 dec->object_type = VIDEO_MPEG4;
2000 else if(dec->object_type == 0x21)
2001 dec->object_type = VIDEO_AVC;
2002 /*else if(dec->object_type == 0x22)
2003 fprintf(stderr, "TYPE 0x22\n");*/
2004 else dec->object_type = UNKNOWN;
2006 else if(dec->stream_type == 5)
2008 if(dec->object_type == 0x40)
2009 dec->object_type = AUDIO_AAC;
2010 else if(dec->object_type == 0x6b)
2011 dec->object_type = AUDIO_MP2;
2012 else if(dec->object_type >= 0x66 && dec->object_type <= 0x69)
2013 dec->object_type = AUDIO_MP2;
2014 else
2015 dec->object_type = UNKNOWN;
2017 else
2018 dec->object_type = dec->stream_type = UNKNOWN;
2020 if(dec->object_type != UNKNOWN)
2022 //update the type of the current stream
2023 for(j = 0; j < pmt->es_cnt; j++)
2025 if(pmt->es[j].mp4_es_id == es->id)
2027 pmt->es[j].type = SL_PES_STREAM;
2032 if(len > 13)
2033 parse_mp4_descriptors(pmt, &buf[13], len-13, dec);
2035 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);
2037 return len;
2040 static uint16_t parse_mp4_decoder_specific_descriptor(uint8_t *buf, int len, void *elem)
2042 int i;
2043 mp4_decoder_config_t *dec;
2045 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DECODER_SPECIFIC_DESCRIPTOR(%d)\n", len);
2046 dec = (mp4_decoder_config_t *) elem;
2047 if(!dec)
2049 mp_msg(MSGT_DEMUX, MSGL_V, "argh! NULL elem passed, skip\n");
2050 return len;
2053 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4 SPECIFIC INFO BYTES: \n");
2054 for(i=0; i<len; i++)
2055 mp_msg(MSGT_DEMUX, MSGL_DBG2, "%02x ", buf[i]);
2056 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\n");
2058 if(len > MAX_EXTRADATA_SIZE)
2060 mp_msg(MSGT_DEMUX, MSGL_ERR, "DEMUX_TS, EXTRADATA SUSPICIOUSLY BIG: %d, REFUSED\r\n", len);
2061 return len;
2063 memcpy(dec->buf, buf, len);
2064 dec->buf_size = len;
2066 return len;
2069 static uint16_t parse_mp4_es_descriptor(pmt_t *pmt, uint8_t *buf, int len)
2071 int i = 0, j = 0, k, found;
2072 uint8_t flag;
2073 mp4_es_descr_t es, *target_es = NULL, *tmp;
2075 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4ES: len=%d\n", len);
2076 memset(&es, 0, sizeof(mp4_es_descr_t));
2077 while(i < len)
2079 es.id = (buf[i] << 8) | buf[i+1];
2080 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_ID: %d\n", es.id);
2081 i += 2;
2082 flag = buf[i];
2083 i++;
2084 if(flag & 0x80)
2085 i += 2;
2086 if(flag & 0x40)
2087 i += buf[i]+1;
2088 if(flag & 0x20) //OCR, maybe we need it
2089 i += 2;
2091 j = parse_mp4_descriptors(pmt, &buf[i], len-i, &es);
2092 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);
2093 if(es.decoder.object_type != UNKNOWN && es.decoder.stream_type != UNKNOWN)
2095 found = 0;
2096 //search this ES_ID if we already have it
2097 for(k=0; k < pmt->mp4es_cnt; k++)
2099 if(pmt->mp4es[k].id == es.id)
2101 target_es = &(pmt->mp4es[k]);
2102 found = 1;
2106 if(! found)
2108 tmp = realloc_struct(pmt->mp4es, pmt->mp4es_cnt+1, sizeof(mp4_es_descr_t));
2109 if(tmp == NULL)
2111 fprintf(stderr, "CAN'T REALLOC MP4_ES_DESCR\n");
2112 continue;
2114 pmt->mp4es = tmp;
2115 target_es = &(pmt->mp4es[pmt->mp4es_cnt]);
2116 pmt->mp4es_cnt++;
2118 memcpy(target_es, &es, sizeof(mp4_es_descr_t));
2119 mp_msg(MSGT_DEMUX, MSGL_V, "MP4ES_CNT: %d, ID=%d\n", pmt->mp4es_cnt, target_es->id);
2122 i += j;
2125 return len;
2128 static void parse_mp4_object_descriptor(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2130 int i, j = 0, id;
2132 i=0;
2133 id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2134 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_OBJECT_DESCRIPTOR: len=%d, OD_ID=%d\n", len, id);
2135 if(buf[1] & 0x20)
2137 i += buf[2] + 1; //url
2138 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2140 else
2142 i = 2;
2144 while(i < len)
2146 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2147 mp_msg(MSGT_DEMUX, MSGL_V, "OBJD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2148 i += j;
2154 static void parse_mp4_iod(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2156 int i, j = 0;
2157 mp4_od_t *iod = &(pmt->iod);
2159 iod->id = (buf[0] << 2) | ((buf[1] & 0xc0) >> 6);
2160 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_IOD: len=%d, IOD_ID=%d\n", len, iod->id);
2161 i = 2;
2162 if(buf[1] & 0x20)
2164 i += buf[2] + 1; //url
2165 mp_msg(MSGT_DEMUX, MSGL_V, "URL\n");
2167 else
2169 i = 7;
2170 while(i < len)
2172 j = parse_mp4_descriptors(pmt, &(buf[i]), len-i, elem);
2173 mp_msg(MSGT_DEMUX, MSGL_V, "IOD, NOW i = %d, j=%d, LEN=%d\n", i, j, len);
2174 i += j;
2179 static int parse_mp4_descriptors(pmt_t *pmt, uint8_t *buf, int len, void *elem)
2181 int tag, descr_len, i = 0, j = 0;
2183 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_MP4_DESCRIPTORS, len=%d\n", len);
2184 if(! len)
2185 return len;
2187 while(i < len)
2189 tag = buf[i];
2190 j = len - i -1;
2191 descr_len = get_mp4_desc_len(&(buf[i+1]), &j);
2192 mp_msg(MSGT_DEMUX, MSGL_V, "TAG=%d (0x%x), DESCR_len=%d, len=%d, j=%d\n", tag, tag, descr_len, len, j);
2193 if(descr_len > len - j+1)
2195 mp_msg(MSGT_DEMUX, MSGL_V, "descriptor is too long, exit\n");
2196 return len;
2198 i += j+1;
2200 switch(tag)
2202 case 0x1:
2203 parse_mp4_object_descriptor(pmt, &(buf[i]), descr_len, elem);
2204 break;
2205 case 0x2:
2206 parse_mp4_iod(pmt, &(buf[i]), descr_len, elem);
2207 break;
2208 case 0x3:
2209 parse_mp4_es_descriptor(pmt, &(buf[i]), descr_len);
2210 break;
2211 case 0x4:
2212 parse_mp4_decoder_config_descriptor(pmt, &buf[i], descr_len, elem);
2213 break;
2214 case 0x05:
2215 parse_mp4_decoder_specific_descriptor(&buf[i], descr_len, elem);
2216 break;
2217 case 0x6:
2218 parse_mp4_slconfig_descriptor(&buf[i], descr_len, elem);
2219 break;
2220 default:
2221 mp_msg(MSGT_DEMUX, MSGL_V, "Unsupported mp4 descriptor 0x%x\n", tag);
2223 i += descr_len;
2226 return len;
2229 static ES_stream_t *new_pid(ts_priv_t *priv, int pid)
2231 ES_stream_t *tss;
2233 tss = malloc(sizeof(ES_stream_t));
2234 if(! tss)
2235 return NULL;
2236 memset(tss, 0, sizeof(ES_stream_t));
2237 tss->pid = pid;
2238 tss->last_cc = -1;
2239 tss->type = UNKNOWN;
2240 tss->subtype = UNKNOWN;
2241 tss->is_synced = 0;
2242 tss->extradata = NULL;
2243 tss->extradata_alloc = tss->extradata_len = 0;
2244 priv->ts.pids[pid] = tss;
2246 return tss;
2250 static int parse_program_descriptors(pmt_t *pmt, uint8_t *buf, uint16_t len)
2252 uint16_t i = 0, k, olen = len;
2254 while(len > 0)
2256 mp_msg(MSGT_DEMUX, MSGL_V, "PROG DESCR, TAG=%x, LEN=%d(%x)\n", buf[i], buf[i+1], buf[i+1]);
2257 if(buf[i+1] > len-2)
2259 mp_msg(MSGT_DEMUX, MSGL_V, "ERROR, descriptor len is too long, skipping\n");
2260 return olen;
2263 if(buf[i] == 0x1d)
2265 if(buf[i+3] == 2) //buggy versions of vlc muxer make this non-standard mess (missing iod_scope)
2266 k = 3;
2267 else
2268 k = 4; //this is standard compliant
2269 parse_mp4_descriptors(pmt, &buf[i+k], (int) buf[i+1]-(k-2), NULL);
2272 len -= 2 + buf[i+1];
2275 return olen;
2278 static int parse_descriptors(struct pmt_es_t *es, uint8_t *ptr)
2280 int j, descr_len, len;
2282 j = 0;
2283 len = es->descr_length;
2284 while(len > 2)
2286 descr_len = ptr[j+1];
2287 mp_msg(MSGT_DEMUX, MSGL_V, "...descr id: 0x%x, len=%d\n", ptr[j], descr_len);
2288 if(descr_len > len)
2290 mp_msg(MSGT_DEMUX, MSGL_ERR, "INVALID DESCR LEN for tag %02x: %d vs %d max, EXIT LOOP\n", ptr[j], descr_len, len);
2291 return -1;
2295 if(ptr[j] == 0x6a || ptr[j] == 0x7a) //A52 Descriptor
2297 if(es->type == 0x6)
2299 es->type = AUDIO_A52;
2300 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB A52 Descriptor\n");
2303 else if(ptr[j] == 0x7b) //DVB DTS Descriptor
2305 if(es->type == 0x6)
2307 es->type = AUDIO_DTS;
2308 mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB DTS Descriptor\n");
2311 else if(ptr[j] == 0x56) // Teletext
2313 if(descr_len >= 5) {
2314 memcpy(es->lang, ptr+2, 3);
2315 es->lang[3] = 0;
2317 es->type = SPU_TELETEXT;
2319 else if(ptr[j] == 0x59) //Subtitling Descriptor
2321 uint8_t subtype;
2323 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Subtitling Descriptor\n");
2324 if(descr_len < 8)
2326 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Descriptor length too short for DVB Subtitle Descriptor: %d, SKIPPING\n", descr_len);
2328 else
2330 memcpy(es->lang, &ptr[j+2], 3);
2331 es->lang[3] = 0;
2332 subtype = ptr[j+5];
2334 (subtype >= 0x10 && subtype <= 0x13) ||
2335 (subtype >= 0x20 && subtype <= 0x23)
2338 es->type = SPU_DVB;
2339 //page parameters: compo page 2 bytes, ancillary page 2 bytes
2341 else
2342 es->type = UNKNOWN;
2345 else if(ptr[j] == 0x50) //Component Descriptor
2347 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Component Descriptor\n");
2348 memcpy(es->lang, &ptr[j+5], 3);
2349 es->lang[3] = 0;
2351 else if(ptr[j] == 0xa) //Language Descriptor
2353 memcpy(es->lang, &ptr[j+2], 3);
2354 es->lang[3] = 0;
2355 mp_msg(MSGT_DEMUX, MSGL_V, "Language Descriptor: %s\n", es->lang);
2357 else if(ptr[j] == 0x5) //Registration Descriptor (looks like e fourCC :) )
2359 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor\n");
2360 if(descr_len < 4)
2362 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor length too short: %d, SKIPPING\n", descr_len);
2364 else
2366 char *d;
2367 memcpy(es->format_descriptor, &ptr[j+2], 4);
2368 es->format_descriptor[4] = 0;
2370 d = &ptr[j+2];
2371 if(d[0] == 'A' && d[1] == 'C' && d[2] == '-' && d[3] == '3')
2373 es->type = AUDIO_A52;
2375 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '1')
2377 es->type = AUDIO_DTS;
2379 else if(d[0] == 'D' && d[1] == 'T' && d[2] == 'S' && d[3] == '2')
2381 es->type = AUDIO_DTS;
2383 else if(d[0] == 'V' && d[1] == 'C' && d[2] == '-' && d[3] == '1')
2385 es->type = VIDEO_VC1;
2387 else if(d[0] == 'd' && d[1] == 'r' && d[2] == 'a' && d[3] == 'c')
2389 es->type = VIDEO_DIRAC;
2391 else
2392 es->type = UNKNOWN;
2393 mp_msg(MSGT_DEMUX, MSGL_DBG2, "FORMAT %s\n", es->format_descriptor);
2396 else if(ptr[j] == 0x1e)
2398 es->mp4_es_id = (ptr[j+2] << 8) | ptr[j+3];
2399 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);
2401 else
2402 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Unknown descriptor 0x%x, SKIPPING\n", ptr[j]);
2404 len -= 2 + descr_len;
2405 j += 2 + descr_len;
2408 return 1;
2411 static int parse_sl_section(pmt_t *pmt, ts_section_t *section, int is_start, unsigned char *buff, int size)
2413 int tid, len, skip;
2414 uint8_t *ptr;
2415 skip = collect_section(section, is_start, buff, size);
2416 if(! skip)
2417 return 0;
2419 ptr = &(section->buffer[skip]);
2420 tid = ptr[0];
2421 len = ((ptr[1] & 0x0f) << 8) | ptr[2];
2422 mp_msg(MSGT_DEMUX, MSGL_V, "TABLEID: %d (av. %d), skip=%d, LEN: %d\n", tid, section->buffer_len, skip, len);
2423 if(len > 4093 || section->buffer_len < len || tid != 5)
2425 mp_msg(MSGT_DEMUX, MSGL_V, "SECTION TOO LARGE or wrong section type, EXIT\n");
2426 return 0;
2429 if(! (ptr[5] & 1))
2430 return 0;
2432 //8 is the current position, len - 9 is the amount of data available
2433 parse_mp4_descriptors(pmt, &ptr[8], len - 9, NULL);
2435 return 1;
2438 static int parse_pmt(ts_priv_t * priv, uint16_t progid, uint16_t pid, int is_start, unsigned char *buff, int size)
2440 unsigned char *base, *es_base;
2441 pmt_t *pmt;
2442 int32_t idx, es_count, section_bytes;
2443 uint8_t m=0;
2444 int skip;
2445 pmt_t *tmp;
2446 struct pmt_es_t *tmp_es;
2447 ts_section_t *section;
2448 ES_stream_t *tss;
2450 idx = progid_idx_in_pmt(priv, progid);
2452 if(idx == -1)
2454 int sz = (priv->pmt_cnt + 1) * sizeof(pmt_t);
2455 tmp = realloc_struct(priv->pmt, priv->pmt_cnt + 1, sizeof(pmt_t));
2456 if(tmp == NULL)
2458 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT: COULDN'T REALLOC %d bytes, NEXT\n", sz);
2459 return 0;
2461 priv->pmt = tmp;
2462 idx = priv->pmt_cnt;
2463 memset(&(priv->pmt[idx]), 0, sizeof(pmt_t));
2464 priv->pmt_cnt++;
2465 priv->pmt[idx].progid = progid;
2468 pmt = &(priv->pmt[idx]);
2470 section = &(pmt->section);
2471 skip = collect_section(section, is_start, buff, size);
2472 if(! skip)
2473 return 0;
2475 base = &(section->buffer[skip]);
2477 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",
2478 progid, pmt->section.buffer_len, is_start, pid, size, m, pmt->es_cnt, idx, pmt);
2480 pmt->table_id = base[0];
2481 if(pmt->table_id != 2)
2482 return -1;
2483 pmt->ssi = base[1] & 0x80;
2484 pmt->section_length = (((base[1] & 0xf) << 8 ) | base[2]);
2485 pmt->version_number = (base[5] >> 1) & 0x1f;
2486 pmt->curr_next = (base[5] & 1);
2487 pmt->section_number = base[6];
2488 pmt->last_section_number = base[7];
2489 pmt->PCR_PID = ((base[8] & 0x1f) << 8 ) | base[9];
2490 pmt->prog_descr_length = ((base[10] & 0xf) << 8 ) | base[11];
2491 if(pmt->prog_descr_length > pmt->section_length - 9)
2493 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, INVALID PROG_DESCR LENGTH (%d vs %d)\n", pmt->prog_descr_length, pmt->section_length - 9);
2494 return -1;
2497 if(pmt->prog_descr_length)
2498 parse_program_descriptors(pmt, &base[12], pmt->prog_descr_length);
2500 es_base = &base[12 + pmt->prog_descr_length]; //the beginning of th ES loop
2502 section_bytes= pmt->section_length - 13 - pmt->prog_descr_length;
2503 es_count = 0;
2505 while(section_bytes >= 5)
2507 int es_pid, es_type;
2509 es_type = es_base[0];
2510 es_pid = ((es_base[1] & 0x1f) << 8) | es_base[2];
2512 idx = es_pid_in_pmt(pmt, es_pid);
2513 if(idx == -1)
2515 int sz = sizeof(struct pmt_es_t) * (pmt->es_cnt + 1);
2516 tmp_es = realloc_struct(pmt->es, pmt->es_cnt + 1, sizeof(struct pmt_es_t));
2517 if(tmp_es == NULL)
2519 mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT, COULDN'T ALLOCATE %d bytes for PMT_ES\n", sz);
2520 continue;
2522 pmt->es = tmp_es;
2523 idx = pmt->es_cnt;
2524 memset(&(pmt->es[idx]), 0, sizeof(struct pmt_es_t));
2525 pmt->es_cnt++;
2528 pmt->es[idx].descr_length = ((es_base[3] & 0xf) << 8) | es_base[4];
2531 if(pmt->es[idx].descr_length > section_bytes - 5)
2533 mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, ES_DESCR_LENGTH TOO LARGE %d > %d, EXIT\n",
2534 pmt->es[idx].descr_length, section_bytes - 5);
2535 return -1;
2539 pmt->es[idx].pid = es_pid;
2540 if(es_type != 0x6)
2541 pmt->es[idx].type = UNKNOWN;
2542 else
2543 pmt->es[idx].type = es_type;
2545 parse_descriptors(&pmt->es[idx], &es_base[5]);
2547 switch(es_type)
2549 case 1:
2550 pmt->es[idx].type = VIDEO_MPEG1;
2551 break;
2552 case 2:
2553 pmt->es[idx].type = VIDEO_MPEG2;
2554 break;
2555 case 3:
2556 case 4:
2557 pmt->es[idx].type = AUDIO_MP2;
2558 break;
2559 case 6:
2560 if(pmt->es[idx].type == 0x6) //this could have been ovrwritten by parse_descriptors
2561 pmt->es[idx].type = UNKNOWN;
2562 break;
2563 case 0x10:
2564 pmt->es[idx].type = VIDEO_MPEG4;
2565 break;
2566 case 0x0f:
2567 pmt->es[idx].type = AUDIO_AAC;
2568 break;
2569 case 0x11:
2570 pmt->es[idx].type = AUDIO_AAC_LATM;
2571 break;
2572 case 0x1b:
2573 pmt->es[idx].type = VIDEO_H264;
2574 break;
2575 case 0x12:
2576 pmt->es[idx].type = SL_PES_STREAM;
2577 break;
2578 case 0x13:
2579 pmt->es[idx].type = SL_SECTION;
2580 break;
2581 case 0x81:
2582 pmt->es[idx].type = AUDIO_A52;
2583 break;
2584 case 0x8A:
2585 case 0x82:
2586 case 0x85:
2587 case 0x86:
2588 pmt->es[idx].type = AUDIO_DTS;
2589 break;
2590 case 0x90:
2591 pmt->es[idx].type = SPU_PGS;
2592 break;
2593 case 0xD1:
2594 pmt->es[idx].type = VIDEO_DIRAC;
2595 break;
2596 case 0xEA:
2597 pmt->es[idx].type = VIDEO_VC1;
2598 break;
2599 default:
2600 mp_msg(MSGT_DEMUX, MSGL_DBG2, "UNKNOWN ES TYPE=0x%x\n", es_type);
2601 pmt->es[idx].type = UNKNOWN;
2604 tss = priv->ts.pids[es_pid]; //an ES stream
2605 if(tss == NULL)
2607 tss = new_pid(priv, es_pid);
2608 if(tss)
2609 tss->type = pmt->es[idx].type;
2612 section_bytes -= 5 + pmt->es[idx].descr_length;
2613 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",
2614 progid, idx, es_count, pmt->es[idx].pid, pmt->es[idx].pid, pmt->es[idx].type, pmt->es[idx].descr_length, section_bytes);
2617 es_base += 5 + pmt->es[idx].descr_length;
2619 es_count++;
2622 mp_msg(MSGT_DEMUX, MSGL_V, "----------------------------\n");
2623 return 1;
2626 static pmt_t* pmt_of_pid(ts_priv_t *priv, int pid, mp4_decoder_config_t **mp4_dec)
2628 int32_t i, j, k;
2630 if(priv->pmt)
2632 for(i = 0; i < priv->pmt_cnt; i++)
2634 if(priv->pmt[i].es && priv->pmt[i].es_cnt)
2636 for(j = 0; j < priv->pmt[i].es_cnt; j++)
2638 if(priv->pmt[i].es[j].pid == pid)
2640 //search mp4_es_id
2641 if(priv->pmt[i].es[j].mp4_es_id)
2643 for(k = 0; k < priv->pmt[i].mp4es_cnt; k++)
2645 if(priv->pmt[i].mp4es[k].id == priv->pmt[i].es[j].mp4_es_id)
2647 *mp4_dec = &(priv->pmt[i].mp4es[k].decoder);
2648 break;
2653 return &(priv->pmt[i]);
2660 return NULL;
2664 static inline int32_t pid_type_from_pmt(ts_priv_t *priv, int pid)
2666 int32_t pmt_idx, pid_idx, i, j;
2668 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2670 if(pmt_idx != -1)
2672 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2673 if(pid_idx != -1)
2674 return priv->pmt[pmt_idx].es[pid_idx].type;
2676 //else
2678 for(i = 0; i < priv->pmt_cnt; i++)
2680 pmt_t *pmt = &(priv->pmt[i]);
2681 for(j = 0; j < pmt->es_cnt; j++)
2682 if(pmt->es[j].pid == pid)
2683 return pmt->es[j].type;
2687 return UNKNOWN;
2691 static inline uint8_t *pid_lang_from_pmt(ts_priv_t *priv, int pid)
2693 int32_t pmt_idx, pid_idx, i, j;
2695 pmt_idx = progid_idx_in_pmt(priv, priv->prog);
2697 if(pmt_idx != -1)
2699 pid_idx = es_pid_in_pmt(&(priv->pmt[pmt_idx]), pid);
2700 if(pid_idx != -1)
2701 return priv->pmt[pmt_idx].es[pid_idx].lang;
2703 else
2705 for(i = 0; i < priv->pmt_cnt; i++)
2707 pmt_t *pmt = &(priv->pmt[i]);
2708 for(j = 0; j < pmt->es_cnt; j++)
2709 if(pmt->es[j].pid == pid)
2710 return pmt->es[j].lang;
2714 return NULL;
2718 static int fill_packet(demuxer_t *demuxer, demux_stream_t *ds, demux_packet_t **dp, int *dp_offset, TS_stream_info *si)
2720 int ret = 0;
2722 if((*dp != NULL) && (*dp_offset > 0))
2724 ret = *dp_offset;
2725 resize_demux_packet(*dp, ret); //shrinked to the right size
2726 ds_add_packet(ds, *dp);
2727 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);
2728 if(si)
2730 float diff = (*dp)->pts - si->last_pts;
2731 float dur;
2733 if(abs(diff) > 1) //1 second, there's a discontinuity
2735 si->duration += si->last_pts - si->first_pts;
2736 si->first_pts = si->last_pts = (*dp)->pts;
2738 else
2740 si->last_pts = (*dp)->pts;
2742 si->size += ret;
2743 dur = si->duration + (si->last_pts - si->first_pts);
2745 if(dur > 0 && ds == demuxer->video)
2747 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2748 if(dur > 1) //otherwise it may be unreliable
2749 priv->vbitrate = (uint32_t) ((float) si->size / dur);
2754 *dp = NULL;
2755 *dp_offset = 0;
2757 return ret;
2760 static int fill_extradata(mp4_decoder_config_t * mp4_dec, ES_stream_t *tss)
2762 uint8_t *tmp;
2764 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4_dec: %p, pid: %d\n", mp4_dec, tss->pid);
2766 if(mp4_dec->buf_size > tss->extradata_alloc)
2768 tmp = realloc(tss->extradata, mp4_dec->buf_size);
2769 if(!tmp)
2770 return 0;
2771 tss->extradata = tmp;
2772 tss->extradata_alloc = mp4_dec->buf_size;
2774 memcpy(tss->extradata, mp4_dec->buf, mp4_dec->buf_size);
2775 tss->extradata_len = mp4_dec->buf_size;
2776 mp_msg(MSGT_DEMUX, MSGL_V, "EXTRADATA: %p, alloc=%d, len=%d\n", tss->extradata, tss->extradata_alloc, tss->extradata_len);
2778 return tss->extradata_len;
2781 // 0 = EOF or no stream found
2782 // else = [-] number of bytes written to the packet
2783 static int ts_parse(demuxer_t *demuxer , ES_stream_t *es, unsigned char *packet, int probe)
2785 ES_stream_t *tss;
2786 uint8_t done = 0;
2787 int buf_size, is_start, pid, base;
2788 int len, cc, cc_ok, afc, retv = 0, is_video, is_audio, is_sub;
2789 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
2790 stream_t *stream = demuxer->stream;
2791 char *p;
2792 demux_stream_t *ds = NULL;
2793 demux_packet_t **dp = NULL;
2794 int *dp_offset = 0, *buffer_size = 0;
2795 int32_t progid, pid_type, bad, ts_error;
2796 int junk = 0, rap_flag = 0;
2797 pmt_t *pmt;
2798 mp4_decoder_config_t *mp4_dec;
2799 TS_stream_info *si;
2802 while(! done)
2804 bad = ts_error = 0;
2805 ds = (demux_stream_t*) NULL;
2806 dp = (demux_packet_t **) NULL;
2807 dp_offset = buffer_size = NULL;
2808 rap_flag = 0;
2809 mp4_dec = NULL;
2810 es->is_synced = 0;
2811 es->lang[0] = 0;
2812 si = NULL;
2814 junk = priv->ts.packet_size - TS_PACKET_SIZE;
2815 buf_size = priv->ts.packet_size - junk;
2817 if(stream_eof(stream))
2819 if(! probe)
2821 ts_dump_streams(priv);
2822 demuxer->filepos = stream_tell(demuxer->stream);
2825 return 0;
2829 if(! ts_sync(stream))
2831 mp_msg(MSGT_DEMUX, MSGL_INFO, "TS_PARSE: COULDN'T SYNC\n");
2832 return 0;
2835 len = stream_read(stream, &packet[1], 3);
2836 if (len != 3)
2837 return 0;
2838 buf_size -= 4;
2840 if((packet[1] >> 7) & 0x01) //transport error
2841 ts_error = 1;
2844 is_start = packet[1] & 0x40;
2845 pid = ((packet[1] & 0x1f) << 8) | packet[2];
2847 tss = priv->ts.pids[pid]; //an ES stream
2848 if(tss == NULL)
2850 tss = new_pid(priv, pid);
2851 if(tss == NULL)
2852 continue;
2855 cc = (packet[3] & 0xf);
2856 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
2857 tss->last_cc = cc;
2859 bad = ts_error; // || (! cc_ok);
2860 if(bad)
2862 if(priv->keep_broken == 0)
2864 stream_skip(stream, buf_size-1+junk);
2865 continue;
2868 is_start = 0; //queued to the packet data
2871 if(is_start)
2872 tss->is_synced = 1;
2874 if((!is_start && !tss->is_synced) || ((pid > 1) && (pid < 16)) || (pid == 8191)) //invalid pid
2876 stream_skip(stream, buf_size-1+junk);
2877 continue;
2881 afc = (packet[3] >> 4) & 3;
2882 if(! (afc % 2)) //no payload in this TS packet
2884 stream_skip(stream, buf_size-1+junk);
2885 continue;
2888 if(afc > 1)
2890 int c;
2891 c = stream_read_char(stream);
2892 buf_size--;
2893 if(c < 0 || c > 183) //broken from the stream layer or invalid
2895 stream_skip(stream, buf_size-1+junk);
2896 continue;
2899 //c==0 is allowed!
2900 if(c > 0)
2902 uint8_t pcrbuf[188];
2903 int flags = stream_read_char(stream);
2904 int has_pcr;
2905 rap_flag = (flags & 0x40) >> 6;
2906 has_pcr = flags & 0x10;
2908 buf_size--;
2909 c--;
2910 stream_read(stream, pcrbuf, c);
2912 if(has_pcr)
2914 int pcr_pid = prog_pcr_pid(priv, priv->prog);
2915 if(pcr_pid == pid)
2917 uint64_t pcr, pcr_ext;
2919 pcr = (int64_t)(pcrbuf[0]) << 25;
2920 pcr |= pcrbuf[1] << 17 ;
2921 pcr |= (pcrbuf[2]) << 9;
2922 pcr |= pcrbuf[3] << 1 ;
2923 pcr |= (pcrbuf[4] & 0x80) >> 7;
2925 pcr_ext = (pcrbuf[4] & 0x01) << 8;
2926 pcr_ext |= pcrbuf[5];
2928 pcr = pcr * 300 + pcr_ext;
2930 demuxer->reference_clock = (double)pcr/(double)27000000.0;
2934 buf_size -= c;
2935 if(buf_size == 0)
2936 continue;
2940 //find the program that the pid belongs to; if (it's the right one or -1) && pid_type==SL_SECTION
2941 //call parse_sl_section()
2942 pmt = pmt_of_pid(priv, pid, &mp4_dec);
2943 if(mp4_dec)
2945 fill_extradata(mp4_dec, tss);
2946 if(IS_VIDEO(mp4_dec->object_type) || IS_AUDIO(mp4_dec->object_type))
2948 tss->type = SL_PES_STREAM;
2949 tss->subtype = mp4_dec->object_type;
2954 //TABLE PARSING
2956 base = priv->ts.packet_size - buf_size;
2958 priv->last_pid = pid;
2960 is_video = IS_VIDEO(tss->type) || (tss->type==SL_PES_STREAM && IS_VIDEO(tss->subtype));
2961 is_audio = IS_AUDIO(tss->type) || (tss->type==SL_PES_STREAM && IS_AUDIO(tss->subtype)) || (tss->type == PES_PRIVATE1);
2962 is_sub = IS_SUB(tss->type);
2963 pid_type = pid_type_from_pmt(priv, pid);
2965 // PES CONTENT STARTS HERE
2966 if(! probe)
2968 if((is_video || is_audio || is_sub) && is_start)
2969 ts_add_stream(demuxer, tss);
2971 if(is_video && (demuxer->video->id == priv->ts.streams[pid].id))
2973 ds = demuxer->video;
2975 dp = &priv->fifo[1].pack;
2976 dp_offset = &priv->fifo[1].offset;
2977 buffer_size = &priv->fifo[1].buffer_size;
2978 si = &priv->vstr;
2980 else if(is_audio && (demuxer->audio->id == priv->ts.streams[pid].id))
2982 ds = demuxer->audio;
2984 dp = &priv->fifo[0].pack;
2985 dp_offset = &priv->fifo[0].offset;
2986 buffer_size = &priv->fifo[0].buffer_size;
2987 si = &priv->astr;
2989 else if(is_sub)
2991 sh_sub_t *sh_sub = demuxer->sub->sh;
2993 if(sh_sub && sh_sub->sid == tss->pid)
2995 ds = demuxer->sub;
2997 dp = &priv->fifo[2].pack;
2998 dp_offset = &priv->fifo[2].offset;
2999 buffer_size = &priv->fifo[2].buffer_size;
3001 else
3003 stream_skip(stream, buf_size+junk);
3004 continue;
3008 //IS IT TIME TO QUEUE DATA to the dp_packet?
3009 if(is_start && (dp != NULL))
3011 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
3015 if(dp && *dp == NULL)
3017 if(*buffer_size > MAX_PACK_BYTES)
3018 *buffer_size = MAX_PACK_BYTES;
3019 *dp = new_demux_packet(*buffer_size); //es->size
3020 *dp_offset = 0;
3021 if(! *dp)
3023 fprintf(stderr, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", *buffer_size);
3024 continue;
3026 mp_msg(MSGT_DEMUX, MSGL_DBG2, "CREATED DP(%d)\n", *buffer_size);
3031 if(probe || !dp) //dp is NULL for tables and sections
3033 p = &packet[base];
3035 else //feeding
3037 if(*dp_offset + buf_size > *buffer_size)
3039 *buffer_size = *dp_offset + buf_size + TS_FEC_PACKET_SIZE;
3040 resize_demux_packet(*dp, *buffer_size);
3042 p = &((*dp)->buffer[*dp_offset]);
3045 len = stream_read(stream, p, buf_size);
3046 if(len < buf_size)
3048 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\r\nts_parse() couldn't read enough data: %d < %d\r\n", len, buf_size);
3049 continue;
3051 stream_skip(stream, junk);
3053 if(pid == 0)
3055 parse_pat(priv, is_start, p, buf_size);
3056 continue;
3058 else if((tss->type == SL_SECTION) && pmt)
3060 int k, mp4_es_id = -1;
3061 ts_section_t *section;
3062 for(k = 0; k < pmt->mp4es_cnt; k++)
3064 if(pmt->mp4es[k].decoder.object_type == MP4_OD && pmt->mp4es[k].decoder.stream_type == MP4_OD)
3065 mp4_es_id = pmt->mp4es[k].id;
3067 mp_msg(MSGT_DEMUX, MSGL_DBG2, "MP4ESID: %d\n", mp4_es_id);
3068 for(k = 0; k < pmt->es_cnt; k++)
3070 if(pmt->es[k].mp4_es_id == mp4_es_id)
3072 section = &(tss->section);
3073 parse_sl_section(pmt, section, is_start, &packet[base], buf_size);
3076 continue;
3078 else
3080 progid = prog_id_in_pat(priv, pid);
3081 if(progid != -1)
3083 if(pid != demuxer->video->id && pid != demuxer->audio->id && pid != demuxer->sub->id)
3085 parse_pmt(priv, progid, pid, is_start, &packet[base], buf_size);
3086 continue;
3088 else
3089 mp_msg(MSGT_DEMUX, MSGL_ERR, "Argh! Data pid %d used in the PMT, Skipping PMT parsing!\n", pid);
3093 if(!probe && !dp)
3094 continue;
3096 if(is_start)
3098 uint8_t *lang = NULL;
3100 mp_msg(MSGT_DEMUX, MSGL_DBG2, "IS_START\n");
3102 len = pes_parse2(p, buf_size, es, pid_type, pmt, pid);
3103 if(! len)
3105 tss->is_synced = 0;
3106 continue;
3108 es->pid = tss->pid;
3109 tss->is_synced |= es->is_synced || rap_flag;
3110 tss->payload_size = es->payload_size;
3112 if((is_sub || is_audio) && (lang = pid_lang_from_pmt(priv, es->pid)))
3114 memcpy(es->lang, lang, 3);
3115 es->lang[3] = 0;
3117 else
3118 es->lang[0] = 0;
3120 if(probe)
3122 if(es->type == UNKNOWN)
3123 return 0;
3125 tss->type = es->type;
3126 tss->subtype = es->subtype;
3128 return 1;
3130 else
3132 if(es->pts == 0.0)
3133 es->pts = tss->pts = tss->last_pts;
3134 else
3135 tss->pts = tss->last_pts = es->pts;
3137 mp_msg(MSGT_DEMUX, MSGL_DBG2, "ts_parse, NEW pid=%d, PSIZE: %u, type=%X, start=%p, len=%d\n",
3138 es->pid, es->payload_size, es->type, es->start, es->size);
3140 demuxer->filepos = stream_tell(demuxer->stream) - es->size;
3142 if(es->size < 0 || es->size > buf_size) {
3143 mp_msg(MSGT_DEMUX, MSGL_ERR, "Broken ES packet size\n");
3144 es->size = 0;
3146 memmove(p, es->start, es->size);
3147 *dp_offset += es->size;
3148 (*dp)->flags = 0;
3149 (*dp)->pos = stream_tell(demuxer->stream);
3150 (*dp)->pts = es->pts;
3152 if(retv > 0)
3153 return retv;
3154 else
3155 continue;
3158 else
3160 uint16_t sz;
3162 es->pid = tss->pid;
3163 es->type = tss->type;
3164 es->subtype = tss->subtype;
3165 es->pts = tss->pts = tss->last_pts;
3166 es->start = &packet[base];
3169 if(tss->payload_size > 0)
3171 sz = FFMIN(tss->payload_size, buf_size);
3172 tss->payload_size -= sz;
3173 es->size = sz;
3175 else
3177 if(is_video)
3179 sz = es->size = buf_size;
3181 else
3183 continue;
3188 if(! probe)
3190 *dp_offset += sz;
3192 if(*dp_offset >= MAX_PACK_BYTES)
3194 (*dp)->pts = tss->last_pts;
3195 retv = fill_packet(demuxer, ds, dp, dp_offset, si);
3196 return 1;
3199 continue;
3201 else
3203 memcpy(es->start, p, sz);
3205 if(es->size)
3206 return es->size;
3207 else
3208 continue;
3213 return 0;
3217 void skip_audio_frame(sh_audio_t *sh_audio);
3219 static void reset_fifos(demuxer_t *demuxer, int a, int v, int s)
3221 ts_priv_t* priv = demuxer->priv;
3222 if(a)
3224 if(priv->fifo[0].pack != NULL)
3226 free_demux_packet(priv->fifo[0].pack);
3227 priv->fifo[0].pack = NULL;
3229 priv->fifo[0].offset = 0;
3232 if(v)
3234 if(priv->fifo[1].pack != NULL)
3236 free_demux_packet(priv->fifo[1].pack);
3237 priv->fifo[1].pack = NULL;
3239 priv->fifo[1].offset = 0;
3242 if(s)
3244 if(priv->fifo[2].pack != NULL)
3246 free_demux_packet(priv->fifo[2].pack);
3247 priv->fifo[2].pack = NULL;
3249 priv->fifo[2].offset = 0;
3251 demuxer->reference_clock = MP_NOPTS_VALUE;
3255 static void demux_seek_ts(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
3257 demux_stream_t *d_audio=demuxer->audio;
3258 demux_stream_t *d_video=demuxer->video;
3259 sh_audio_t *sh_audio=d_audio->sh;
3260 sh_video_t *sh_video=d_video->sh;
3261 ts_priv_t * priv = (ts_priv_t*) demuxer->priv;
3262 int i, video_stats;
3263 off_t newpos;
3265 //================= seek in MPEG-TS ==========================
3267 ts_dump_streams(demuxer->priv);
3268 reset_fifos(demuxer, sh_audio != NULL, sh_video != NULL, demuxer->sub->id > 0);
3270 demux_flush(demuxer);
3274 video_stats = (sh_video != NULL);
3275 if(video_stats)
3277 mp_msg(MSGT_DEMUX, MSGL_V, "IBPS: %d, vb: %d\r\n", sh_video->i_bps, priv->vbitrate);
3278 if(priv->vbitrate)
3279 video_stats = priv->vbitrate;
3280 else
3281 video_stats = sh_video->i_bps;
3284 newpos = (flags & SEEK_ABSOLUTE) ? demuxer->movi_start : demuxer->filepos;
3285 if(flags & SEEK_FACTOR) // float seek 0..1
3286 newpos+=(demuxer->movi_end-demuxer->movi_start)*rel_seek_secs;
3287 else
3289 // time seek (secs)
3290 if(! video_stats) // unspecified or VBR
3291 newpos += 2324*75*rel_seek_secs; // 174.3 kbyte/sec
3292 else
3293 newpos += video_stats*rel_seek_secs;
3297 if(newpos < demuxer->movi_start)
3298 newpos = demuxer->movi_start; //begininng of stream
3300 stream_seek(demuxer->stream, newpos);
3301 for(i = 0; i < 8192; i++)
3302 if(priv->ts.pids[i] != NULL)
3303 priv->ts.pids[i]->is_synced = 0;
3305 videobuf_code_len = 0;
3307 if(sh_video != NULL)
3308 ds_fill_buffer(d_video);
3310 if(sh_audio != NULL)
3312 ds_fill_buffer(d_audio);
3315 while(sh_video != NULL)
3317 if(sh_audio && !d_audio->eof && d_video->pts && d_audio->pts)
3319 double a_pts=d_audio->pts;
3320 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(double)sh_audio->i_bps;
3321 if(d_video->pts > a_pts)
3323 skip_audio_frame(sh_audio); // sync audio
3324 continue;
3329 i = sync_video_packet(d_video);
3330 if((sh_video->format == VIDEO_MPEG1) || (sh_video->format == VIDEO_MPEG2))
3332 if(i==0x1B3 || i==0x1B8) break; // found it!
3334 else if((sh_video->format == VIDEO_MPEG4) && (i==0x1B6))
3335 break;
3336 else if(sh_video->format == VIDEO_VC1 && (i==0x10E || i==0x10F))
3337 break;
3338 else //H264
3340 if((i & ~0x60) == 0x105 || (i & ~0x60) == 0x107) break;
3343 if(!i || !skip_video_packet(d_video)) break; // EOF?
3348 static int demux_ts_fill_buffer(demuxer_t * demuxer, demux_stream_t *ds)
3350 ES_stream_t es;
3351 ts_priv_t *priv = (ts_priv_t *)demuxer->priv;
3353 return -ts_parse(demuxer, &es, priv->packet, 0);
3357 static int ts_check_file_dmx(demuxer_t *demuxer)
3359 return ts_check_file(demuxer) ? DEMUXER_TYPE_MPEG_TS : 0;
3362 static int is_usable_program(ts_priv_t *priv, pmt_t *pmt)
3364 int j;
3366 for(j = 0; j < pmt->es_cnt; j++)
3368 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3369 continue;
3371 priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO ||
3372 priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO
3374 return 1;
3377 return 0;
3380 static int demux_ts_control(demuxer_t *demuxer, int cmd, void *arg)
3382 ts_priv_t* priv = (ts_priv_t *)demuxer->priv;
3384 switch(cmd)
3386 case DEMUXER_CTRL_SWITCH_AUDIO:
3387 case DEMUXER_CTRL_SWITCH_VIDEO:
3389 void *sh = NULL;
3390 int i, n;
3391 int reftype, areset = 0, vreset = 0;
3392 demux_stream_t *ds;
3394 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
3396 reftype = TYPE_VIDEO;
3397 ds = demuxer->video;
3398 vreset = 1;
3400 else
3402 reftype = TYPE_AUDIO;
3403 ds = demuxer->audio;
3404 areset = 1;
3406 n = *((int*)arg);
3407 if(n == -2)
3409 reset_fifos(demuxer, areset, vreset, 0);
3410 ds->id = -2;
3411 ds->sh = NULL;
3412 ds_free_packs(ds);
3413 *((int*)arg) = ds->id;
3414 return DEMUXER_CTRL_OK;
3417 if(n < 0)
3419 for(i = 0; i < 8192; i++)
3421 if(priv->ts.streams[i].id == ds->id && priv->ts.streams[i].type == reftype)
3422 break;
3425 while(!sh)
3427 i = (i+1) % 8192;
3428 if(priv->ts.streams[i].type == reftype)
3430 if(priv->ts.streams[i].id == ds->id) //we made a complete loop
3431 break;
3432 sh = priv->ts.streams[i].sh;
3436 else //audio track <n>
3438 if (n >= 8192 || priv->ts.streams[n].type != reftype) return DEMUXER_CTRL_NOTIMPL;
3439 i = n;
3440 sh = priv->ts.streams[i].sh;
3443 if(sh)
3445 if(ds->id != priv->ts.streams[i].id)
3446 reset_fifos(demuxer, areset, vreset, 0);
3447 ds->id = priv->ts.streams[i].id;
3448 ds->sh = sh;
3449 ds_free_packs(ds);
3450 mp_msg(MSGT_DEMUX, MSGL_V, "\r\ndemux_ts, switched to audio pid %d, id: %d, sh: %p\r\n", i, ds->id, sh);
3453 *((int*)arg) = ds->id;
3454 return DEMUXER_CTRL_OK;
3457 case DEMUXER_CTRL_IDENTIFY_PROGRAM: //returns in prog->{aid,vid} the new ids that comprise a program
3459 int i, j, cnt=0;
3460 int vid_done=0, aid_done=0;
3461 pmt_t *pmt = NULL;
3462 demux_program_t *prog = arg;
3464 if(priv->pmt_cnt < 2)
3465 return DEMUXER_CTRL_NOTIMPL;
3467 if(prog->progid == -1)
3469 int cur_pmt_idx = 0;
3471 for(i = 0; i < priv->pmt_cnt; i++)
3472 if(priv->pmt[i].progid == priv->prog)
3474 cur_pmt_idx = i;
3475 break;
3478 i = (cur_pmt_idx + 1) % priv->pmt_cnt;
3479 while(i != cur_pmt_idx)
3481 pmt = &priv->pmt[i];
3482 cnt = is_usable_program(priv, pmt);
3483 if(cnt)
3484 break;
3485 i = (i + 1) % priv->pmt_cnt;
3488 else
3490 for(i = 0; i < priv->pmt_cnt; i++)
3491 if(priv->pmt[i].progid == prog->progid)
3493 pmt = &priv->pmt[i]; //required program
3494 cnt = is_usable_program(priv, pmt);
3498 if(!cnt)
3499 return DEMUXER_CTRL_NOTIMPL;
3501 //finally some food
3502 prog->aid = prog->vid = -2; //no audio and no video by default
3503 for(j = 0; j < pmt->es_cnt; j++)
3505 if(priv->ts.pids[pmt->es[j].pid] == NULL || priv->ts.streams[pmt->es[j].pid].sh == NULL)
3506 continue;
3508 if(!vid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_VIDEO)
3510 vid_done = 1;
3511 prog->vid = pmt->es[j].pid;
3513 else if(!aid_done && priv->ts.streams[pmt->es[j].pid].type == TYPE_AUDIO)
3515 aid_done = 1;
3516 prog->aid = pmt->es[j].pid;
3520 priv->prog = prog->progid = pmt->progid;
3521 return DEMUXER_CTRL_OK;
3524 default:
3525 return DEMUXER_CTRL_NOTIMPL;
3530 const demuxer_desc_t demuxer_desc_mpeg_ts = {
3531 "MPEG-TS demuxer",
3532 "mpegts",
3533 "TS",
3534 "Nico Sabbi",
3536 DEMUXER_TYPE_MPEG_TS,
3537 0, // unsafe autodetect
3538 ts_check_file_dmx,
3539 demux_ts_fill_buffer,
3540 demux_open_ts,
3541 demux_close_ts,
3542 demux_seek_ts,
3543 demux_ts_control