diff options
author | attila <attila@b3059339-0415-0410-9bf9-f77b7e298cf2> | 2003-10-20 13:35:55 +0000 |
---|---|---|
committer | attila <attila@b3059339-0415-0410-9bf9-f77b7e298cf2> | 2003-10-20 13:35:55 +0000 |
commit | b95debe5edbced87ee36bd2f72a6085e382a2f17 (patch) | |
tree | 6307116a8461a088964ac61e38b3bef597c83f92 | |
parent | 492bcc7a3f475247f93a635795a6a4cf2872cb88 (diff) | |
download | mpv-b95debe5edbced87ee36bd2f72a6085e382a2f17.tar.bz2 mpv-b95debe5edbced87ee36bd2f72a6085e382a2f17.tar.xz |
big demux_ts fix from Nico <nsabbi@libero.it>
git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@11191 b3059339-0415-0410-9bf9-f77b7e298cf2
-rw-r--r-- | cfg-common.h | 11 | ||||
-rw-r--r-- | libmpdemux/demux_ts.c | 1031 |
2 files changed, 723 insertions, 319 deletions
diff --git a/cfg-common.h b/cfg-common.h index 5e64467869..c48f86d9ed 100644 --- a/cfg-common.h +++ b/cfg-common.h @@ -76,9 +76,9 @@ {"forceidx", &index_mode, CONF_TYPE_FLAG, 0, -1, 2, NULL}, // select audio/videosubtitle stream - {"aid", &audio_id, CONF_TYPE_INT, CONF_RANGE, 0, 8192, NULL}, - {"vid", &video_id, CONF_TYPE_INT, CONF_RANGE, 0, 8192, NULL}, - {"sid", &dvdsub_id, CONF_TYPE_INT, CONF_RANGE, 0, 31, NULL}, + {"aid", &audio_id, CONF_TYPE_INT, CONF_RANGE, 0, 8190, NULL}, + {"vid", &video_id, CONF_TYPE_INT, CONF_RANGE, 0, 8190, NULL}, + {"sid", &dvdsub_id, CONF_TYPE_INT, CONF_RANGE, 0, 8190, NULL}, {"novideo", &video_id, CONF_TYPE_FLAG, 0, -1, -2, NULL}, { "hr-mp3-seek", &hr_mp3_seek, CONF_TYPE_FLAG, 0, 0, 1, NULL }, @@ -184,6 +184,9 @@ {"tsfastparse", "-tsfastparse isn't a valid option anymore.\n", CONF_TYPE_PRINT, CONF_NOCFG ,0,0, NULL }, {"tsprog", &ts_prog, CONF_TYPE_INT, CONF_RANGE, 0, 65534, NULL}, +#define TS_MAX_PROBE_SIZE 2000000 /* dont forget to change this in libmpdemux/demux_ts.c too */ + {"tsprobe", &ts_probe, CONF_TYPE_POSITION, 0, 0, TS_MAX_PROBE_SIZE, NULL}, + {"tskeepbroken", &ts_keep_broken, CONF_TYPE_FLAG, 0, 0, 1, NULL}, // draw by slices or whole frame (useful with libmpeg2/libavcodec) {"slices", &vd_use_slices, CONF_TYPE_FLAG, 0, 0, 1, NULL}, @@ -286,6 +289,8 @@ extern char* audio_stream; extern char* sub_stream; extern int demuxer_type, audio_demuxer_type, sub_demuxer_type; extern int ts_prog; +extern int ts_keep_broken; +extern off_t ts_probe; #include "libmpdemux/tv.h" diff --git a/libmpdemux/demux_ts.c b/libmpdemux/demux_ts.c index eb46833e03..954326f1ce 100644 --- a/libmpdemux/demux_ts.c +++ b/libmpdemux/demux_ts.c @@ -34,6 +34,7 @@ #include "stheader.h" #include "bswap.h" +#include "../unrarlib.h" #define TS_FEC_PACKET_SIZE 204 @@ -42,12 +43,15 @@ #define MAX_HEADER_SIZE 6 /* enough for PES header + length */ #define MAX_CHECK_SIZE 65535 -#define MAX_PROBE_SIZE 2000000 +#define TS_MAX_PROBE_SIZE 2000000 /* dont forget to change this in cfg-common.h too */ #define NUM_CONSECUTIVE_TS_PACKETS 32 #define NUM_CONSECUTIVE_AUDIO_PACKETS 348 int ts_prog; +int ts_keep_broken=0; +off_t ts_probe = TS_MAX_PROBE_SIZE; +extern char *dvdsub_lang, *audio_lang; //for -alang typedef enum { @@ -58,11 +62,9 @@ typedef enum AUDIO_MP2 = 0x50, AUDIO_A52 = 0x2000, AUDIO_LPCM_BE = 0x10001, - AUDIO_AAC = (('A' << 24) | ('4' << 16) | ('P' << 8) | 'M') - /*, + AUDIO_AAC = mmioFOURCC('M', 'P', '4', 'A'), SPU_DVD = 0x3000000, - SPU_DVB = 0x3000001, - */ + SPU_DVB = 0x3000001 } es_stream_type_t; @@ -73,7 +75,9 @@ typedef struct { es_stream_type_t type; float pts, last_pts; int pid; + char lang[4]; int last_cc; // last cc code (-1 if first packet) + uint64_t seen; } ES_stream_t; @@ -106,8 +110,8 @@ typedef struct { } *progs; uint16_t progs_cnt; char buffer[65535]; - uint8_t buffer_len; - } pat_t; + uint16_t buffer_len; +} pat_t; typedef struct { uint16_t progid; @@ -122,27 +126,26 @@ typedef struct { uint16_t PCR_PID; uint16_t prog_descr_length; char buffer[2048]; - uint8_t buffer_len; + uint16_t buffer_len; uint16_t es_cnt; struct pmt_es_t { uint16_t pid; uint32_t type; //it's 8 bit long, but cast to the right type as FOURCC uint16_t descr_length; + uint8_t format_descriptor[5]; + uint8_t lang[4]; } *es; } pmt_t; typedef struct { MpegTSContext ts; - int is_synced; //synced to the beginning of priv->last_pid PES header - int last_afc; //bytes read from the last adaption field int last_pid; - int is_start; - int eof; - av_fifo_t fifo[2]; //0 for audio, 1 for video + av_fifo_t fifo[3]; //0 for audio, 1 for video, 2 for subs pat_t pat; pmt_t *pmt; uint16_t pmt_cnt; uint32_t prog; + int keep_broken; } ts_priv_t; @@ -185,9 +188,11 @@ int ts_check_file(demuxer_t * demuxer) int cc[NB_PID_MAX], last_cc[NB_PID_MAX], pid, cc_ok, c, good, bad; uint8_t size = 0; off_t pos = 0; + off_t init_pos; mp_msg(MSGT_DEMUX, MSGL_V, "Checking for MPEG-TS...\n"); + init_pos = stream_tell(demuxer->stream); is_ts = 0; while(! done) { @@ -227,14 +232,14 @@ int ts_check_file(demuxer_t * demuxer) is_ts = 1; } - if(pos >= MAX_CHECK_SIZE) + if(pos - init_pos >= MAX_CHECK_SIZE) { done = 1; is_ts = 0; } } - mp_msg(MSGT_DEMUX, MSGL_V, "TRIED UP TO POSITION %u, FOUND %x, packet_size= %d\n", pos, c, size); + mp_msg(MSGT_DEMUX, MSGL_V, "TRIED UP TO POSITION %llu, FOUND %x, packet_size= %d, SEEMS A TS? %d\n", (uint64_t) pos, c, size, is_ts); stream_seek(demuxer->stream, pos); if(! is_ts) @@ -326,36 +331,76 @@ static inline int32_t progid_for_pid(ts_priv_t *priv, int pid, int32_t req) //f } -static void ts_detect_streams(demuxer_t *demuxer, uint32_t *a, uint32_t *v, int *fapid, int *fvpid, int32_t *prog) +static inline int pid_match_lang(ts_priv_t *priv, uint16_t pid, char *lang) { - int video_found = 0, audio_found = 0, i, num_packets = 0, req_apid, req_vpid; - int is_audio, is_video, has_tables; - int32_t p, chosen_pid; - off_t pos=0; + uint16_t i, j; + pmt_t *pmt; + + if(priv->pmt == NULL) + return -1; + + for(i=0; i < priv->pmt_cnt; i++) + { + pmt = &(priv->pmt[i]); + + if(pmt->es == NULL) + return -1; + + for(j = 0; j < pmt->es_cnt; j++) + { + if(pmt->es[j].pid != pid) + continue; + + 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); + if(strncmp(pmt->es[j].lang, lang, 3) == 0) + { + return 1; + } + } + + } + + return -1; +} + +typedef struct { + int32_t atype, vtype, stype; //types + int32_t apid, vpid, spid; //stream ids + char slang[4], alang[4]; //languages + int16_t prog; + off_t probe; +} tsdemux_init_t; + +static off_t ts_detect_streams(demuxer_t *demuxer, tsdemux_init_t *param) +{ + int video_found = 0, audio_found = 0, sub_found = 0, i, num_packets = 0, req_apid, req_vpid, req_spid; + int is_audio, is_video, is_sub, has_tables; + int32_t p, chosen_pid = 0; + off_t pos=0, ret = 0, init_pos; ES_stream_t es; unsigned char tmp[TS_FEC_PACKET_SIZE]; ts_priv_t *priv = (ts_priv_t*) demuxer->priv; - priv->is_synced = 0; - priv->last_afc = 0; priv->last_pid = 8192; //invalid pid - priv->is_start = 0; - priv->eof = 0; - req_apid = *fapid; - req_vpid = *fvpid; + req_apid = param->apid; + req_vpid = param->vpid; + req_spid = param->spid; has_tables = 0; - mp_msg(MSGT_DEMUXER, MSGL_INFO, "PROBING UP TO %u, PROG: %d\n", MAX_PROBE_SIZE, *prog); - while(pos <= MAX_PROBE_SIZE) + init_pos = stream_tell(demuxer->stream); + mp_msg(MSGT_DEMUXER, MSGL_INFO, "PROBING UP TO %llu, PROG: %d\n", (uint64_t) param->probe, param->prog); + while((pos <= init_pos + param->probe) && (! demuxer->stream->eof)) { pos = stream_tell(demuxer->stream); if(ts_parse(demuxer, &es, tmp, 1)) { is_audio = ((es.type == AUDIO_MP2) || (es.type == AUDIO_A52) || (es.type == AUDIO_LPCM_BE) || (es.type == AUDIO_AAC)); is_video = ((es.type == VIDEO_MPEG1) || (es.type == VIDEO_MPEG2) || (es.type == VIDEO_MPEG4)); + is_sub = ((es.type == SPU_DVD) || (es.type == SPU_DVB)); + - if((! is_audio) && (! is_video)) + if((! is_audio) && (! is_video) && (! is_sub)) continue; if(is_video) @@ -366,32 +411,54 @@ static void ts_detect_streams(demuxer_t *demuxer, uint32_t *a, uint32_t *v, int } else if(is_audio) { - chosen_pid = (req_apid == es.pid); - if((! chosen_pid) && (req_apid > 0)) + if(req_apid > 0) + { + chosen_pid = (req_apid == es.pid); + if(! chosen_pid) + continue; + } + else if(param->alang[0] > 0) + { + if(pid_match_lang(priv, es.pid, param->alang) == -1) + continue; + + chosen_pid = 1; + param->apid = req_apid = es.pid; + } + } + else if(is_sub) + { + chosen_pid = (req_spid == es.pid); + if((! chosen_pid) && (req_spid > 0)) continue; } - if(req_vpid < 0 && req_apid < 0) + if(req_apid < 0 && (param->alang[0] == 0) && req_vpid < 0 && req_spid < 0) chosen_pid = 1; - p = progid_for_pid(priv, es.pid, *prog); + if((ret == 0) && chosen_pid) + { + ret = stream_tell(demuxer->stream); + } + + p = progid_for_pid(priv, es.pid, param->prog); if(p != -1) has_tables++; - if((*prog == 0) && (p != -1)) + if((param->prog == 0) && (p != -1)) { if(chosen_pid) - *prog = p; + param->prog = p; } - if((*prog > 0) && (*prog != p)) + if((param->prog > 0) && (param->prog != p)) { if(audio_found) { if(is_video && (req_vpid == es.pid)) { - *v = es.type; - *fvpid = es.pid; + param->vtype = es.type; + param->vpid = es.pid; video_found = 1; break; } @@ -401,8 +468,8 @@ static void ts_detect_streams(demuxer_t *demuxer, uint32_t *a, uint32_t *v, int { if(is_audio && (req_apid == es.pid)) { - *a = es.type; - *fapid = es.pid; + param->atype = es.type; + param->apid = es.pid; audio_found = 1; break; } @@ -413,15 +480,15 @@ static void ts_detect_streams(demuxer_t *demuxer, uint32_t *a, uint32_t *v, int } - mp_msg(MSGT_DEMUXER, MSGL_DBG2, "TYPE: %x, PID: %d, PROG FOUND: %d\n", es.type, es.pid, *prog); + mp_msg(MSGT_DEMUXER, MSGL_DBG2, "TYPE: %x, PID: %d, PROG FOUND: %d\n", es.type, es.pid, param->prog); if(is_video) { if((req_vpid == -1) || (req_vpid == es.pid)) { - *v = es.type; - *fvpid = es.pid; + param->vtype = es.type; + param->vpid = es.pid; video_found = 1; } } @@ -430,27 +497,36 @@ static void ts_detect_streams(demuxer_t *demuxer, uint32_t *a, uint32_t *v, int if(((req_vpid == -2) || (num_packets >= NUM_CONSECUTIVE_AUDIO_PACKETS)) && audio_found) { //novideo or we have at least 348 audio packets (64 KB) without video (TS with audio only) - *v = 0; + param->vtype = 0; break; } + if(is_sub) + { + if((req_spid == -1) || (req_spid == es.pid)) + { + param->stype = es.type; + param->spid = es.pid; + sub_found = 1; + } + } if(is_audio) { if((req_apid == -1) || (req_apid == es.pid)) { - *a = es.type; - *fapid = es.pid; + param->atype = es.type; + param->apid = es.pid; audio_found = 1; } } - if(audio_found && (*fapid == es.pid) && (! video_found)) + if(audio_found && (param->apid == es.pid) && (! video_found)) num_packets++; if((req_apid == -2) && video_found) { - *a = 0; + param->atype = 0; break; } @@ -460,28 +536,48 @@ static void ts_detect_streams(demuxer_t *demuxer, uint32_t *a, uint32_t *v, int } if(video_found) - mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG%d(pid=%d)...", (*v == VIDEO_MPEG1 ? 1 : (*v == VIDEO_MPEG2 ? 2 : 4)), *fvpid); + mp_msg(MSGT_DEMUXER, MSGL_INFO, "VIDEO MPEG%d(pid=%d)...", (param->vtype == VIDEO_MPEG1 ? 1 : (param->vtype == VIDEO_MPEG2 ? 2 : 4)), param->vpid); else { - //WE DIDN'T MATCH ANY VIDEO STREAM - mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO VIDEO! "); + video_found = 0; + param->vtype = UNKNOWN; + //WE DIDN'T MATCH ANY VIDEO STREAM + mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO VIDEO! "); } - if(*a == AUDIO_MP2) - mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO MP2(pid=%d)", *fapid); - else if(*a == AUDIO_A52) - mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO A52(pid=%d)", *fapid); - else if(*a == AUDIO_LPCM_BE) - mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO LPCM(pid=%d)", *fapid); - else if(*a == AUDIO_AAC) - mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO AAC(pid=%d)", *fapid); + if(param->atype == AUDIO_MP2) + mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO MP2(pid=%d)", param->apid); + else if(param->atype == AUDIO_A52) + mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO A52(pid=%d)", param->apid); + else if(param->atype == AUDIO_LPCM_BE) + mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO LPCM(pid=%d)", param->apid); + else if(param->atype == AUDIO_AAC) + mp_msg(MSGT_DEMUXER, MSGL_INFO, "AUDIO AAC(pid=%d)", param->apid); else { + audio_found = 0; + param->atype = UNKNOWN; //WE DIDN'T MATCH ANY AUDIO STREAM, SO WE FORCE THE DEMUXER TO IGNORE AUDIO mp_msg(MSGT_DEMUXER, MSGL_INFO, "NO AUDIO! "); } - mp_msg(MSGT_DEMUXER, MSGL_INFO, " PROGRAM N. %d\n", *prog); + if(param->stype == SPU_DVD || param->stype == SPU_DVB) + mp_msg(MSGT_DEMUXER, MSGL_INFO, " SUB DVx(pid=%d) ", param->spid); + else + { + param->stype = UNKNOWN; + mp_msg(MSGT_DEMUXER, MSGL_INFO, " NO SUBS (yet)! "); + } + + if(video_found || audio_found) + { + if(demuxer->stream->eof && (ret == 0)) + ret = init_pos; + mp_msg(MSGT_DEMUXER, MSGL_INFO, " PROGRAM N. %d\n", param->prog); + } + else + mp_msg(MSGT_DEMUXER, MSGL_INFO, "\n"); + for(i=0; i<8192; i++) { @@ -489,11 +585,13 @@ static void ts_detect_streams(demuxer_t *demuxer, uint32_t *a, uint32_t *v, int { priv->ts.pids[i]->payload_size = 0; priv->ts.pids[i]->pts = priv->ts.pids[i]->last_pts = 0; - //priv->ts.pids[i]->type = UNKNOWN; + priv->ts.pids[i]->seen = 0; + priv->ts.pids[i]->last_cc = -1; } } -} + return ret; +} demuxer_t *demux_open_ts(demuxer_t * demuxer) @@ -502,11 +600,11 @@ demuxer_t *demux_open_ts(demuxer_t * demuxer) uint8_t packet_size; sh_video_t *sh_video; sh_audio_t *sh_audio; - uint32_t at = 0, vt = 0; + off_t start_pos; + tsdemux_init_t params; ts_priv_t * priv = (ts_priv_t*) demuxer->priv; - demuxer_t *od; - mp_msg(MSGT_DEMUX, MSGL_DBG2, "DEMUX OPEN, AUDIO_ID: %d, VIDEO_ID: %d, SUBTITLE_ID: %d,\n", + mp_msg(MSGT_DEMUX, MSGL_INFO, "DEMUX OPEN, AUDIO_ID: %d, VIDEO_ID: %d, SUBTITLE_ID: %d,\n", demuxer->audio->id, demuxer->video->id, demuxer->sub->id); @@ -514,7 +612,6 @@ demuxer_t *demux_open_ts(demuxer_t * demuxer) stream_reset(demuxer->stream); - stream_seek(demuxer->stream, 0); packet_size = ts_check_file(demuxer); if(!packet_size) @@ -523,8 +620,7 @@ demuxer_t *demux_open_ts(demuxer_t * demuxer) priv = malloc(sizeof(ts_priv_t)); if(priv == NULL) { - mp_msg(MSGT_DEMUX, MSGL_FATAL, "DEMUX_OPEN_TS, couldn't allocate %lu bytes, exit\n", - sizeof(ts_priv_t)); + mp_msg(MSGT_DEMUX, MSGL_FATAL, "DEMUX_OPEN_TS, couldn't allocate enough memory for ts->priv, exit\n"); return NULL; } @@ -536,61 +632,80 @@ demuxer_t *demux_open_ts(demuxer_t * demuxer) priv->pmt = NULL; priv->pmt_cnt = 0; + priv->keep_broken = ts_keep_broken; priv->ts.packet_size = packet_size; demuxer->priv = priv; if(demuxer->stream->type != STREAMTYPE_FILE) - demuxer->seekable = 0; + demuxer->seekable = 1; else demuxer->seekable = 1; - stream_seek(demuxer->stream, 0); //IF IT'S FROM A PIPE IT WILL FAIL, BUT WHO CARES? - ts_detect_streams(demuxer, &at, &vt, &demuxer->audio->id, &demuxer->video->id, &ts_prog); - mp_msg(MSGT_DEMUXER,MSGL_INFO, "Opened TS demuxer, audio: %x(pid %d), video: %x(pid %d)...\n", at, demuxer->audio->id, vt, demuxer->video->id); + params.atype = params.vtype = params.stype = UNKNOWN; + params.apid = demuxer->audio->id; + params.vpid = demuxer->video->id; + params.spid = demuxer->sub->id; + params.prog = ts_prog; + params.probe = ts_probe; + + if(dvdsub_lang != NULL) + { + strncpy(params.slang, dvdsub_lang, 3); + params.slang[3] = 0; + } + else + memset(params.slang, 0, 4); + + if(audio_lang != NULL) + { + strncpy(params.alang, audio_lang, 3); + params.alang[3] = 0; + } + else + memset(params.alang, 0, 4); + + start_pos = ts_detect_streams(demuxer, ¶ms); + + demuxer->audio->id = params.apid; + demuxer->video->id = params.vpid; + demuxer->sub->id = params.spid; + priv->prog = params.prog; - if(vt) + + if(params.vtype != UNKNOWN) { - if(vt == VIDEO_MPEG4) + if(params.vtype == VIDEO_MPEG4) demuxer->file_format= DEMUXER_TYPE_MPEG4_IN_TS; sh_video = new_sh_video(demuxer, 0); sh_video->ds = demuxer->video; - sh_video->format = vt; + sh_video->format = params.vtype; demuxer->video->sh = sh_video; - - mp_msg(MSGT_DEMUXER,MSGL_INFO, "OPENED_SH_VIDEO\n"); } - if(at) + if(params.atype != UNKNOWN) { sh_audio = new_sh_audio(demuxer, 0); sh_audio->ds = demuxer->audio; - sh_audio->format = at; + sh_audio->format = params.atype; demuxer->audio->sh = sh_audio; - - mp_msg(MSGT_DEMUXER,MSGL_INFO, "OPENED_SH_AUDIO\n"); } - mp_msg(MSGT_DEMUXER,MSGL_INFO, "Opened TS demuxer2\n"); + mp_msg(MSGT_DEMUXER,MSGL_INFO, "Opened TS demuxer, audio: %x(pid %d), video: %x(pid %d)...POS=%llu\n", params.atype, demuxer->audio->id, params.vtype, demuxer->video->id, (uint64_t) start_pos); - /* - demuxer->movi_start = 0; - demuxer->movi_end = demuxer->stream->end_pos; - */ - - stream_seek(demuxer->stream, 0); //IF IT'S FROM A PIPE IT WILL FAIL, BUT WHO CARES? + start_pos = (start_pos <= priv->ts.packet_size ? 0 : start_pos - priv->ts.packet_size); + demuxer->movi_start = start_pos; + stream_reset(demuxer->stream); + stream_seek(demuxer->stream, start_pos); //IF IT'S FROM A PIPE IT WILL FAIL, BUT WHO CARES? - priv->is_synced = 0; - priv->last_afc = 0; priv->last_pid = 8192; //invalid pid - priv->is_start = 0; - for(i=0; i< 2; i++) + for(i = 0; i < 3; i++) { priv->fifo[i].pack = NULL; priv->fifo[i].offset = 0; @@ -598,10 +713,11 @@ demuxer_t *demux_open_ts(demuxer_t * demuxer) } priv->fifo[0].ds = demuxer->audio; priv->fifo[1].ds = demuxer->video; + priv->fifo[2].ds = demuxer->sub; priv->fifo[0].buffer_size = 1536; priv->fifo[1].buffer_size = 32767; - priv->eof = 0; + priv->fifo[2].buffer_size = 32767; priv->pat.buffer_len = 0; @@ -609,11 +725,6 @@ demuxer_t *demux_open_ts(demuxer_t * demuxer) return demuxer; } - - - - - void demux_close_ts(demuxer_t * demuxer) { if(demuxer->priv) @@ -623,10 +734,6 @@ void demux_close_ts(demuxer_t * demuxer) } } - - - - static int pes_parse2(unsigned char *buf, uint16_t packet_len, ES_stream_t *es, int32_t type_from_pmt) { unsigned char *p; @@ -641,7 +748,7 @@ static int pes_parse2(unsigned char *buf, uint16_t packet_len, ES_stream_t *es, //uint32_t es_rate; //Here we are always at the start of a PES packet - mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2(%X, %d): \n", buf, packet_len); + mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2(%p, %d): \n", buf, (uint32_t) packet_len); if(packet_len == 0) { @@ -669,7 +776,10 @@ static int pes_parse2(unsigned char *buf, uint16_t packet_len, ES_stream_t *es, packet_len -= 6; if(packet_len==0) + { + mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: packet too short: %d, exit\n", packet_len); return 0; + } es->payload_size = (p[4] << 8 | p[5]); @@ -729,6 +839,11 @@ static int pes_parse2(unsigned char *buf, uint16_t packet_len, ES_stream_t *es, p += header_len + 9; packet_len -= header_len + 3; + /* + if(packet_len <= 0) + mp_msg(MSGT_DEMUX, MSGL_INFO, "\n\nNow: %d, prima: %d, ORIG: %d\n\n\n", packet_len, packet_len+3+header_len, pkt_len); + */ + if(es->payload_size) es->payload_size -= header_len + 3; @@ -741,64 +856,59 @@ static int pes_parse2(unsigned char *buf, uint16_t packet_len, ES_stream_t *es, p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[0] & 0x80); track = p[0] & 0x0F; - mp_msg(MSGT_DEMUX, MSGL_DBG2, "AC3 TRACK: %d\n", track); + mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 TRACK: %d\n", track); /* * we check the descriptor tag first because some stations - * do not include any of the ac3 header info in their audio tracks + * do not include any of the A52 header info in their audio tracks * these "raw" streams may begin with a byte that looks like a stream type. */ if( - (type_from_pmt == AUDIO_A52) || /* ac3 - raw */ - (p[0] == 0x0B && p[1] == 0x77) /* ac3 - syncword */ + (type_from_pmt == AUDIO_A52) || /* A52 - raw */ + (p[0] == 0x0B && p[1] == 0x77) /* A52 - syncword */ ) { - mp_msg(MSGT_DEMUX, MSGL_DBG2, "AC3 RAW OR SYNCWORD\n"); + mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 RAW OR SYNCWORD\n"); es->start = p; es->size = packet_len; es->type = AUDIO_A52; es->payload_size -= packet_len; - return es->size; + return 1; } - /* - SUBS, it seems they are not streams in g1, what to do? - else if (//m->descriptor_tag == 0x06 && - p[0] == 0x20 && p[1] == 0x00) + /* SPU SUBS */ + else if(type_from_pmt == SPU_DVB || + (p[0] == 0x20)) // && p[1] == 0x00)) { - // DVBSUB - long payload_len = ((buf[4] << 8) | buf[5]) - header_len - 3; es->start = p; es->size = packet_len; - es->type = SPU_DVB + payload_len; + es->type = SPU_DVB; es->payload_size -= packet_len; - return es->size; + return 1; } - - else if ((p[0] & 0xE0) == 0x20) + else if ((p[0] & 0xE0) == 0x20) //SPU_DVD { - spu_id = (p[0] & 0x1f); + //DVD SUBS es->start = p+1; es->size = packet_len-1; - es->type = SPU_DVD + spu_id; + es->type = SPU_DVD; es->payload_size -= packet_len; - return es->size; + return 1; } - */ else if ((p[0] & 0xF0) == 0x80) { - mp_msg(MSGT_DEMUX, MSGL_DBG2, "AC3 WITH HEADER\n"); + mp_msg(MSGT_DEMUX, MSGL_DBG2, "A52 WITH HEADER\n"); es->start = p+4; es->size = packet_len - 4; es->type = AUDIO_A52; es->payload_size -= packet_len; - return es->size; + return 1; } else if ((p[0]&0xf0) == 0xa0) { @@ -818,7 +928,7 @@ static int pes_parse2(unsigned char *buf, uint16_t packet_len, ES_stream_t *es, es->type = AUDIO_LPCM_BE; es->payload_size -= packet_len; - return es->size; + return 1; } } else if ((stream_id >= 0xbc) && ((stream_id & 0xf0) == 0xe0)) @@ -829,7 +939,8 @@ static int pes_parse2(unsigned char *buf, uint16_t packet_len, ES_stream_t *es, if(es->payload_size) es->payload_size -= packet_len; - return es->size; + mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: M2V size %d\n", es->size); + return 1; } else if ((stream_id == 0xfa)) { @@ -841,20 +952,38 @@ static int pes_parse2(unsigned char *buf, uint16_t packet_len, ES_stream_t *es, if(es->payload_size) es->payload_size -= packet_len; - return es->size; + return 1; } } else if ((stream_id & 0xe0) == 0xc0) { - int track; + int profile = 0, srate = 0, channels = 0; + uint32_t hdr, l = 0; + + hdr = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; + if((hdr & 0xfff00000) == 0xfff00000) + { + // ADTS AAC shows with MPA layer 4 (00 in the layer bitstream) + l = 4 - ((hdr & 0x00060000) >> 17); + profile = ((hdr & 0x0000C000) >> 14); + srate = ((hdr & 0x00003c00) >> 10); + channels = ((hdr & 0x000001E0) >> 5); + } + mp_msg(MSGT_DEMUX, MSGL_DBG2, "\n\naudio header: %2X %2X %2X %2X\nLAYER: %d, PROFILE: %d, SRATE=%d, CHANNELS=%d\n\n", p[0], p[1], p[3], p[4], l, profile, srate, channels); - track = stream_id & 0x1f; es->start = p; es->size = packet_len; - es->type = AUDIO_MP2; + + + //if((type_from_pmt == 0x0f) || (l == 4)) //see in parse_pmt() + if(l==4) + es->type = AUDIO_AAC; + else + es->type = AUDIO_MP2; + es->payload_size -= packet_len; - return es->size; + return 1; } else if (type_from_pmt != -1) //as a last resort here we trust the PMT, if present { @@ -863,9 +992,8 @@ static int pes_parse2(unsigned char *buf, uint16_t packet_len, ES_stream_t *es, es->type = type_from_pmt; es->payload_size -= packet_len; - return es->size; + return 1; } - else { mp_msg(MSGT_DEMUX, MSGL_DBG2, "pes_parse2: unknown packet, id: %x\n", stream_id); @@ -896,16 +1024,17 @@ static void ts_dump_streams(ts_priv_t *priv) { int i; - for(i = 0; i < 2; i++) + for(i = 0; i < 3; i++) { if((priv->fifo[i].pack != NULL) && (priv->fifo[i].offset != 0)) { resize_demux_packet(priv->fifo[i].pack, priv->fifo[i].offset); ds_add_packet(priv->fifo[i].ds, priv->fifo[i].pack); + priv->fifo[i].offset = 0; + priv->fifo[i].pack = NULL; } + priv->fifo[i].broken = 1; } - - priv->eof = 1; } @@ -942,15 +1071,29 @@ static inline int32_t prog_id_in_pat(ts_priv_t *priv, uint16_t pid) return -1; } +/* +DON'T REMOVE, I'LL NEED IT IN THE (NEAR) FUTURE) +static int check_crc32(uint32_t val, uint8_t *ptr, uint16_t len, uint8_t *vbase) +{ + uint32_t tab_crc32, calc_crc32; + + calc_crc32 = CalcCRC32(val, ptr, len); + + tab_crc32 = (vbase[0] << 24) | (vbase[1] << 16) | (vbase[2] << 8) | vbase[3]; + + printf("CRC32, TAB: %x, CALC: %x, eq: %x\n", tab_crc32, calc_crc32, tab_crc32 == calc_crc32); + return (tab_crc32 == calc_crc32); + +} +*/ static int parse_pat(ts_priv_t * priv, int is_start, unsigned char *buff, int size) { uint8_t skip, m = 0; unsigned char *ptr; - unsigned char *base; //, *crc; + unsigned char *base; int entries, i, sections; uint16_t progid; - //uint32_t o_crc, calc_crc; //PRE-FILLING if(! is_start) @@ -996,13 +1139,6 @@ static int parse_pat(ts_priv_t * priv, int is_start, unsigned char *buff, int si priv->pat.section_number = ptr[6]; priv->pat.last_section_number = ptr[7]; - /*CRC CHECK - crc = &(priv->pat.buffer[priv->pat.section_length - 4]); - o_crc = (crc[0] << 24) | (crc[1] << 16) | (crc[2] << 8) | crc[3]; - calc_crc = CalcCRC32(0xFFFFFFFFL, priv->pat.buffer, priv->pat.section_length); - printf("CRC ORIGINALE: %x, CALCOLATO: %x\n", o_crc, calc_crc); - */ - if((! priv->pat.curr_next) || (priv->pat.table_id != 0)) // || (! priv->pat.ssi)) return 0; @@ -1020,6 +1156,8 @@ static int parse_pat(ts_priv_t * priv, int is_start, unsigned char *buff, int si return -1; //KEEP ON FILLING THE TABLE } + //check_crc32(0xFFFFFFFFL, ptr, priv->pat.buffer_len - 4, &ptr[priv->pat.buffer_len - 4]); + entries = (int) (priv->pat.section_length - 9) / 4; //entries per section @@ -1054,7 +1192,7 @@ static int parse_pat(ts_priv_t * priv, int is_start, unsigned char *buff, int si static inline int32_t es_pid_in_pmt(pmt_t * pmt, uint16_t pid) { - int i; + uint16_t i; if(pmt == NULL) return -1; @@ -1065,13 +1203,107 @@ static inline int32_t es_pid_in_pmt(pmt_t * pmt, uint16_t pid) for(i = 0; i < pmt->es_cnt; i++) { if(pmt->es[i].pid == pid) - return i; + return (int32_t) i; } return -1; } +static int parse_descriptors(struct pmt_es_t *es, uint8_t *ptr) +{ + int j, descr_len, len; + + j = 0; + len = es->descr_length; + while(len > 2) + { + descr_len = ptr[j+1]; + mp_msg(MSGT_DEMUX, MSGL_DBG2, "...descr id: 0x%x, len=%d\n", ptr[j], descr_len); + if(descr_len > len) + { + mp_msg(MSGT_DEMUX, MSGL_DBG2, "INVALID DESCR LEN: %d vs %d max, EXIT LOOP\n", descr_len, len); + return -1; + } + + + if(ptr[j] == 0x6a) //A52 Descriptor + { + es->type = AUDIO_A52; + mp_msg(MSGT_DEMUX, MSGL_DBG2, "DVB A52 Descriptor\n"); + } + else if(ptr[j] == 0x59) //Subtitling Descriptor + { + uint8_t subtype; + + mp_msg(MSGT_DEMUX, MSGL_DBG2, "Subtitling Descriptor\n"); + if(descr_len < 8) + { + mp_msg(MSGT_DEMUX, MSGL_DBG2, "Descriptor length too short for DVB Subtitle Descriptor: %d, SKIPPING\n", descr_len); + } + else + { + memcpy(es->lang, &ptr[j+2], 3); + es->lang[3] = 0; + subtype = ptr[j+5]; + if( + (subtype >= 0x10 && subtype <= 0x13) || + (subtype >= 0x20 && subtype <= 0x23) + ) + { + es->type = SPU_DVB; + //page parameters: compo page 2 bytes, ancillary page 2 bytes + } + else + es->type = UNKNOWN; + } + } + else if(ptr[j] == 0x50) //Component Descriptor + { + mp_msg(MSGT_DEMUX, MSGL_DBG2, "Component Descriptor\n"); + memcpy(es->lang, &ptr[j+5], 3); + es->lang[3] = 0; + } + else if(ptr[j] == 0xa) //Language Descriptor + { + memcpy(es->lang, &ptr[j+2], 3); + es->lang[3] = 0; + mp_msg(MSGT_DEMUX, MSGL_V, "Language Descriptor: %s\n", es->lang); + } + else if(ptr[j] == 0x5) //Registration Descriptor (looks like e fourCC :) ) + { + mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor\n"); + if(descr_len < 4) + { + mp_msg(MSGT_DEMUX, MSGL_DBG2, "Registration Descriptor length too short: %d, SKIPPING\n", descr_len); + } + else + { + char *d; + memcpy(es->format_descriptor, &ptr[j+2], 4); + es->format_descriptor[4] = 0; + + d = &ptr[j+2]; + if(d[0] == 'A' && d[1] == 'C' && d[2] == '-' && d[3] == '3') + { + es->type = AUDIO_A52; + } + else + es->type = UNKNOWN; + mp_msg(MSGT_DEMUX, MSGL_DBG2, "FORMAT %s\n", es->format_descriptor); + } + } + else + mp_msg(MSGT_DEMUX, MSGL_DBG2, "Unknown descriptor 0x%x, SKIPPING\n", ptr[j]); + + len -= 2 + descr_len; + j += 2 + descr_len; + } + + return 1; +} + + static int parse_pmt(ts_priv_t * priv, uint16_t progid, uint16_t pid, int is_start, unsigned char *buff, int size) { @@ -1088,7 +1320,7 @@ static int parse_pmt(ts_priv_t * priv, uint16_t progid, uint16_t pid, int is_sta priv->pmt = (pmt_t *) realloc(priv->pmt, sz); if(priv->pmt == NULL) { - mp_msg(MSGT_DEMUX, MSGL_ERR, "FILL_PMT: COULDN'T REALLOC %d bytes, NEXT\n", sz); + mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT: COULDN'T REALLOC %d bytes, NEXT\n", sz); return NULL; } @@ -1127,13 +1359,20 @@ static int parse_pmt(ts_priv_t * priv, uint16_t progid, uint16_t pid, int is_sta return 0; } + + if(size-m + pmt->buffer_len > 2048) + { + mp_msg(MSGT_DEMUX, MSGL_V, "FILL_PMT(prog=%d, PID=%d), ERROR! PMT TOO LONG, IGNORING\n", progid, pid); + pmt->buffer_len = 0; + return NULL; + } + memcpy(&(pmt->buffer[pmt->buffer_len]), &buff[m], size - m); pmt->progid = progid; pmt->buffer_len += size - m; - - mp_msg(MSGT_DEMUX, MSGL_V, "FILL_PMT(prog=%d), PMT_len: %d, IS_START: %d, TSPID: %d\n", - progid, pmt->buffer_len, is_start, pid); + 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", + progid, pmt->buffer_len, is_start, pid, size, m, pmt->es_cnt, idx, pmt); base = pmt->buffer; @@ -1157,6 +1396,7 @@ static int parse_pmt(ts_priv_t * priv, uint16_t progid, uint16_t pid, int is_sta if(pmt->section_length + 3 > pmt->buffer_len) { mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, SECTION LENGTH TOO LARGE FOR CURRENT BUFFER (%d vs %d), NEXT TIME\n", pmt->section_length, pmt->buffer_len); + return -1; } @@ -1190,6 +1430,7 @@ static int parse_pmt(ts_priv_t * priv, uint16_t progid, uint16_t pid, int is_sta continue; } idx = pmt->es_cnt; + memset(&(pmt->es[idx]), 0, sizeof(struct pmt_es_t)); pmt->es_cnt++; } @@ -1198,8 +1439,8 @@ static int parse_pmt(ts_priv_t * priv, uint16_t progid, uint16_t pid, int is_sta if(pmt->es[idx].descr_length > section_bytes - 5) { - mp_msg(MSGT_DEMUX, MSGL_ERR, "PARSE_PMT, ES_DESCR_LENGTH TOO LARGE %d > %d, EXIT %d bytes for PMT_ES\n", - pmt->es[idx].descr_length, section_bytes - 5, section_bytes); + mp_msg(MSGT_DEMUX, MSGL_V, "PARSE_PMT, ES_DESCR_LENGTH TOO LARGE %d > %d, EXIT\n", + pmt->es[idx].descr_length, section_bytes - 5); return -1; } @@ -1207,6 +1448,8 @@ static int parse_pmt(ts_priv_t * priv, uint16_t progid, uint16_t pid, int is_sta pmt->es[idx].pid = es_pid; pmt->es[idx].type = es_type; + pmt->es[idx].type = UNKNOWN; + switch(es_type) |