| 1 |
/* dvbcut |
| 2 |
Copyright (c) 2005 Sven Over <[email protected]> |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation; either version 2 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program; if not, write to the Free Software |
| 16 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 |
*/ |
| 18 |
|
| 19 |
/* $Id$ */ |
| 20 |
|
| 21 |
#include "port.h" |
| 22 |
#include "tsfile.h" |
| 23 |
#include "streamhandle.h" |
| 24 |
#include <list> |
| 25 |
#include <utility> |
| 26 |
#include <algorithm> |
| 27 |
#include <cstring> |
| 28 |
#include <cassert> |
| 29 |
|
| 30 |
tsfile::tsfile(inbuffer &b, int initial_offset, int stride) : mpgfile(b, initial_offset) |
| 31 |
{ |
| 32 |
assert(stride >= TSPACKETSIZE); |
| 33 |
assert(stride <= MAXPACKETSIZE); |
| 34 |
packetsize = stride; |
| 35 |
|
| 36 |
for(unsigned int i=0;i<8192;++i) |
| 37 |
streamnumber[i]=-1; |
| 38 |
|
| 39 |
// check the "header" for receiver specific bookmarks |
| 40 |
const uint8_t *header = (const uint8_t*) buf.data(); |
| 41 |
const char *model[7]={"???","TF4000","TF5000","TF5010","TF5000c","TF5010c","TF7700"}; |
| 42 |
int irc; |
| 43 |
bmtype = none; |
| 44 |
if((irc=isTOPFIELD(header, initial_offset, buf.getfilename(0)))>0) |
| 45 |
fprintf(stderr,"Found Topfield %s recording (%d bytes header, %d bookmarks)\n",model[irc/100],initial_offset,irc%100); |
| 46 |
else |
| 47 |
fprintf(stderr,"Analyzed transport stream, %d bytes of initial data (IRC=%d)\n",initial_offset,irc); |
| 48 |
fprintf(stderr, "Using %d-byte packets\n", packetsize); |
| 49 |
|
| 50 |
// Find video PID and audio PID(s) |
| 51 |
buf.providedata(buf.getsize(), initialoffset); |
| 52 |
if (check_si_tables()) |
| 53 |
return; |
| 54 |
|
| 55 |
// there are no SI tables, so we have to guess |
| 56 |
int vid=-1; |
| 57 |
bool apid[8192]={}; |
| 58 |
std::list<std::pair<int,int> > audios; |
| 59 |
int inpackets=buf.inbytes()/packetsize; |
| 60 |
if (inpackets>40000) |
| 61 |
inpackets=40000; |
| 62 |
const uint8_t *d = (const uint8_t*)buf.data(); |
| 63 |
for(int i=0;i<inpackets;++i) { |
| 64 |
const tspacket *p = (const tspacket*)d; |
| 65 |
d += packetsize; |
| 66 |
if (p->transport_error_indicator()) |
| 67 |
continue; // drop invalid packet --mr |
| 68 |
int pid=p->pid(); |
| 69 |
if (apid[pid]) |
| 70 |
continue; // already had this pid --mr |
| 71 |
int sid=p->sid(); |
| 72 |
if (sid<0) |
| 73 |
continue; |
| 74 |
|
| 75 |
if ((sid&0xe0) == 0xc0) { // mpeg audio stream |
| 76 |
audios.push_back(std::pair<int,int>(sid,pid)); |
| 77 |
apid[pid]=true; |
| 78 |
} |
| 79 |
else if (sid==0xbd) { // private stream 1, possibly AC3 audio stream |
| 80 |
const uint8_t *payload=(const uint8_t*) p->payload(); |
| 81 |
const uint16_t plen = p->payload_length(); |
| 82 |
if (plen >= 9 |
| 83 |
&& plen >= 11 + payload[8] |
| 84 |
&& payload[9 + payload[8]] == 0x0b |
| 85 |
&& payload[10 + payload[8]] == 0x77) { |
| 86 |
audios.push_back(std::pair<int,int>(sid,pid)); |
| 87 |
apid[pid]=true; |
| 88 |
} |
| 89 |
else if (plen == 184 |
| 90 |
&& payload[8] == 0x24 |
| 91 |
&& (payload[45] & 0xf0) == 0x10 |
| 92 |
&& payload[47] == 0x2c) { |
| 93 |
// probably VBI/teletext data |
| 94 |
} |
| 95 |
else if (plen >= 9 |
| 96 |
&& plen >= 12 + payload[8] |
| 97 |
&& payload[9 + payload[8]] == 0x20 |
| 98 |
&& payload[10 + payload[8]] == 0x00 |
| 99 |
&& payload[11 + payload[8]] == 0x0f) { |
| 100 |
// probably dvb subtitles |
| 101 |
} |
| 102 |
} |
| 103 |
else if (vid<0 && ((sid&0xf0)==0xe0)) { // mpeg video stream |
| 104 |
vid=pid; |
| 105 |
streamnumber[vid]=VIDEOSTREAM; |
| 106 |
apid[pid]=true; |
| 107 |
} |
| 108 |
} |
| 109 |
audios.sort(); |
| 110 |
for (std::list<std::pair<int,int> >::iterator it=audios.begin();it!=audios.end();++it) { |
| 111 |
streamnumber[it->second]=audiostream(audiostreams); |
| 112 |
stream *S=&s[audiostream(audiostreams++)]; |
| 113 |
S->id=it->second; |
| 114 |
if (it->first==0xbd) { |
| 115 |
S->type=streamtype::ac3audio; |
| 116 |
} |
| 117 |
else { |
| 118 |
S->type=streamtype::mpegaudio; |
| 119 |
} |
| 120 |
if (audiostreams>=MAXAUDIOSTREAMS) |
| 121 |
break; |
| 122 |
} |
| 123 |
initcodeccontexts(vid); |
| 124 |
} |
| 125 |
|
| 126 |
tsfile::~tsfile() |
| 127 |
{} |
| 128 |
|
| 129 |
int tsfile::streamreader(streamhandle &s) |
| 130 |
{ |
| 131 |
int returnvalue=0; |
| 132 |
bool lostsync(false); |
| 133 |
|
| 134 |
for(;;) { |
| 135 |
dvbcut_off_t packetpos=s.fileposition; |
| 136 |
{ |
| 137 |
int pd=buf.providedata(packetsize,packetpos); |
| 138 |
if (pd<0) |
| 139 |
return pd; |
| 140 |
if (pd < (int)packetsize) |
| 141 |
return returnvalue; |
| 142 |
} |
| 143 |
|
| 144 |
const tspacket *p=(const tspacket*) buf.data(); |
| 145 |
|
| 146 |
if ((p->data[0]!=TSSYNCBYTE)||lostsync) // we lost sync |
| 147 |
{ |
| 148 |
|
| 149 |
{ |
| 150 |
int pd=buf.providedata(4<<10,packetpos); |
| 151 |
if (pd<(4<<10)) |
| 152 |
{ |
| 153 |
return returnvalue; // end of file, probably |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
lostsync=true; |
| 158 |
const uint8_t *data = (const uint8_t*) buf.data(); |
| 159 |
int ts; |
| 160 |
for (ts = 0; ts < 2048; ++ts) |
| 161 |
{ |
| 162 |
int pos; |
| 163 |
for (pos = ts;pos < 4096;pos += packetsize) |
| 164 |
if (data[ pos ] != TSSYNCBYTE) |
| 165 |
break; |
| 166 |
if (pos >= 4096) // from here, we are in sync again |
| 167 |
{ lostsync=false; |
| 168 |
break; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
s.fileposition+=ts; |
| 173 |
continue; |
| 174 |
} |
| 175 |
|
| 176 |
s.fileposition+=packetsize; |
| 177 |
|
| 178 |
// Abandon invalid packets --mr |
| 179 |
if (p->transport_error_indicator()) |
| 180 |
continue; |
| 181 |
// Abandon packets which have no payload or have invalid adaption field length |
| 182 |
if (p->payload_length()<=0) |
| 183 |
continue; |
| 184 |
int sn=streamnumber[p->pid()]; |
| 185 |
if (sn<0) |
| 186 |
continue; |
| 187 |
streamdata *sd=s.stream[sn]; |
| 188 |
if (!sd) |
| 189 |
continue; |
| 190 |
|
| 191 |
if (p->payload_unit_start_indicator()) { |
| 192 |
sd->header=std::string((const char *)p->payload(),p->payload_length()); |
| 193 |
sd->nextfilepos=packetpos; |
| 194 |
} else if (!sd->header.empty()) |
| 195 |
sd->header+=std::string((const char *)p->payload(),p->payload_length()); |
| 196 |
else { |
| 197 |
if (!sd->itemlist().empty()) { |
| 198 |
returnvalue += p->payload_length(); |
| 199 |
sd->append(p->payload(),p->payload_length()); |
| 200 |
} |
| 201 |
continue; |
| 202 |
} |
| 203 |
|
| 204 |
if (sd->header.size()<9) |
| 205 |
continue; |
| 206 |
|
| 207 |
if (sd->header[0]!=0 || sd->header[1]!=0 || sd->header[2]!=1) { |
| 208 |
sd->header.clear(); |
| 209 |
continue; |
| 210 |
} |
| 211 |
int sid=(uint8_t)sd->header[3]; |
| 212 |
|
| 213 |
unsigned int payloadbegin=9u+(uint8_t)sd->header[8]; |
| 214 |
|
| 215 |
if (sd->header.size()<payloadbegin) |
| 216 |
continue; |
| 217 |
// if (sid==0xbd || sid==0xbf) |
| 218 |
// { |
| 219 |
// if (sd->header.size()<payloadbegin+1) continue; |
| 220 |
// sid=((sid==0xbd)?0x100:0x200)|((unsigned char) sd->header[payloadbegin]); |
| 221 |
// ++payloadbegin; |
| 222 |
// } |
| 223 |
// else |
| 224 |
{ |
| 225 |
if (sid>=0xe0 && sid<0xf0 && sd->header.size()>=payloadbegin+4) |
| 226 |
if ( *(uint32_t*)(&sd->header[payloadbegin])==mbo32(0x00000001) ) |
| 227 |
++payloadbegin; |
| 228 |
} |
| 229 |
|
| 230 |
sd->appenditem(filepos_t(sd->nextfilepos,0), std::string(sd->header,6,payloadbegin-6), |
| 231 |
&sd->header[payloadbegin], sd->header.size()-payloadbegin); |
| 232 |
returnvalue += sd->header.size()-payloadbegin; |
| 233 |
sd->header.clear(); |
| 234 |
return returnvalue; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
/// This function probes the data in the given inbuffer for an mpeg transport stream. |
| 239 |
/// If it finds a syncbyte (ascii 'G') within the first 8kB which is then followed by |
| 240 |
/// another syncbyte every 188 bytes (ts packet size) in the next 2kB, then |
| 241 |
/// this data is assumed to be a transport stream. |
| 242 |
/// It returns the buffer offset at which the transport stream starts, or -1 if |
| 243 |
/// no transport stream was identified. |
| 244 |
/// 2011-04-24: use variable packet size supplied by caller. --mr |
| 245 |
int tsfile::probe(inbuffer &buf, int stride) { |
| 246 |
int latestsync=buf.inbytes()-2048; |
| 247 |
if (latestsync>(8<<10)) |
| 248 |
latestsync=8<<10; |
| 249 |
|
| 250 |
// Find TS syncbyte 'G' within the first 2048 bytes? |
| 251 |
const uint8_t *data = (const uint8_t*) buf.data(); |
| 252 |
int ts; |
| 253 |
for (ts = 0; ts < latestsync; ++ts) { |
| 254 |
int testupto=ts+2048; |
| 255 |
int pos; |
| 256 |
for (pos = ts;pos < testupto;pos += stride) |
| 257 |
if (data[ pos ] != TSSYNCBYTE) |
| 258 |
break; |
| 259 |
if (pos >= testupto) // this is a MPEG TS file |
| 260 |
return ts; |
| 261 |
} |
| 262 |
return -1; |
| 263 |
} |
| 264 |
|
| 265 |
// test for Topfield TF4000PVR & TF5xxxPVR and read bookmarks from the proprietary header |
| 266 |
// return codes: <type>*100+<number of bookmarks> or |
| 267 |
// -1: header far to short for topfield/bookmarks, -2: magic mismatch, -3: version mismatch |
| 268 |
int |
| 269 |
tsfile::isTOPFIELD(const uint8_t *header, int len, std::string recfile) { |
| 270 |
unsigned int magic, version, unit=0; |
| 271 |
unsigned int frequency, symbolrate, modulation; |
| 272 |
int boff=len, off=0, type=0, hlen=0, verbose=0, irc; |
| 273 |
|
| 274 |
// topfields use 188-byte packets. |
| 275 |
if (packetsize != TSPACKETSIZE) return -1; |
| 276 |
|
| 277 |
if(verbose) fprintf(stderr,"Header length of %s: %d\n",recfile.c_str(),len); |
| 278 |
|
| 279 |
// topfield receiver with additional info file? |
| 280 |
if((irc=isTF7700HDPVR(recfile))>0) return irc; |
| 281 |
else if(verbose) fprintf(stderr,"No TF7700HDPVR! IRC=%d\n",irc); |
| 282 |
|
| 283 |
// just in case there's a corrupted TS packet at the beginning |
| 284 |
if(len<TSPACKETSIZE) return -1; |
| 285 |
|
| 286 |
// identify the specific header via the magic number 'TFrc' |
| 287 |
magic = (header[0]<<24)|(header[1]<<16)|(header[2]<<8)|header[3]; |
| 288 |
if(magic==TF5XXXPVR_MAGIC) { // all newer models of type TF5???PVR and TF6???PVR |
| 289 |
hlen = TF5XXXPVR_LEN; |
| 290 |
unit = (1<<9)*TSPACKETSIZE; |
| 291 |
// There are 2 different versions, one with 4 bytes larger ServiceInfoStructure (UK models?) |
| 292 |
version = (header[4]<<8)|header[5]; |
| 293 |
if(verbose) fprintf(stderr,"magic: %x version: %x\n",magic,version); |
| 294 |
if(version==TF5000PVR_VERSION) { |
| 295 |
off = 0; |
| 296 |
boff = TF5000PVR_POS; |
| 297 |
type = 2; |
| 298 |
} else if(version==TF5010PVR_VERSION) { |
| 299 |
off = 4; |
| 300 |
boff = TF5010PVR_POS; |
| 301 |
type = 3; |
| 302 |
} else |
| 303 |
return -3; |
| 304 |
// DVB-C boxes have a 4bytes smaller TransponderInfoStructure... grmbl!!! |
| 305 |
// And there's no identifier or version number to indicate this... arghhhh!! |
| 306 |
// ==> check a few transponder parameters (which are stored at different positions if DVB-S or -T!?!) |
| 307 |
frequency = (header[52+off]<<24)|(header[53+off]<<16)|(header[54+off]<<8)|header[55+off]; |
| 308 |
symbolrate = (header[56+off]<<8)|header[57+off]; |
| 309 |
modulation = header[62+off]; |
| 310 |
if(verbose) fprintf(stderr,"DVB-C? freq=%d symb=%d mod=%d\n",frequency,symbolrate,modulation); |
| 311 |
if(frequency>=47000 && frequency<=862000 && symbolrate>=2000 && symbolrate<=30000 && modulation<=4) { |
| 312 |
boff-=4; |
| 313 |
type+=2; |
| 314 |
} |
| 315 |
} else { |
| 316 |
// the old TF4000PVR don't writes a simple magic/version number at the beginning... |
| 317 |
// but start/stop time (MJD/hour/time) ==> check for consistency! |
| 318 |
// start can be equal to stop (if recording is COPY/CUT), but should be year 2000+ (MJD=51543)! |
| 319 |
version = (header[4]<<24)|(header[5]<<16)|(header[6]<<8)|header[7]; |
| 320 |
unsigned int duration = (header[8]<<8)|header[9]; // in minutes |
| 321 |
unsigned int servicetype = (header[12]<<8)|header[13]; // 0:TV, 1:Radio |
| 322 |
unsigned int polarity = header[53]; // 0x80, 0x08, 0x88, 0x00 (hi/lo, hor/ver) |
| 323 |
frequency = (header[54]<<8)|header[55]; |
| 324 |
symbolrate = (header[56]<<8)|header[57]; |
| 325 |
if(verbose) fprintf(stderr,"TF4000? start=%x stop=%x dur=%d serv=%d freq=%d symb=%d pol=%d\n", |
| 326 |
magic,version,duration,servicetype,frequency,symbolrate,polarity); |
| 327 |
if(magic<=version && (magic>>16)>51543 && |
| 328 |
header[2]<24 && header[3]<60 && header[6]<24 && header[7]<60 && |
| 329 |
duration>0 && duration<1440 && servicetype==0 && // up to one full day TV recording should be enough! |
| 330 |
frequency>=10000 && frequency<=13000 && // there's only a DVB-S device, AFAIK! |
| 331 |
symbolrate>=2000 && symbolrate<=30000 && (polarity&0x77)==0) { |
| 332 |
// the above should be sufficient... together with the required length of the header! |
| 333 |
hlen = TF4000PVR_LEN; |
| 334 |
unit = (1<<17); |
| 335 |
boff = TF4000PVR_POS; |
| 336 |
type = 1; |
| 337 |
} else |
| 338 |
return -2; |
| 339 |
} |
| 340 |
|
| 341 |
// OK,... we identified a receiver model! |
| 342 |
int bnum = 0; |
| 343 |
//if(len>=hlen) // discard ALL (slightly) to small headers...? No, only require enough space for the bookmarks... |
| 344 |
if(len>=boff+4*TF5XXXPVR_MAX) { |
| 345 |
// Seems to be a Topfield TF4000PVR/TF5xxxPVR TS-header with 576/3760bytes total length |
| 346 |
// and up to 64 bookmarks (BUT can be shorter/corrupted due to COPY/CUT-procedure on reveiver) |
| 347 |
dvbcut_off_t bookmark; |
| 348 |
while ((bookmark=(header[boff]<<24)|(header[boff+1]<<16)|(header[boff+2]<<8)|header[boff+3]) |
| 349 |
&& bnum<TF5XXXPVR_MAX) { |
| 350 |
// bookmark is stored in 128 resp. 94kbyte units |
| 351 |
bookmark*=unit; |
| 352 |
if(verbose) fprintf(stderr,"BOOKMARK[%d] = %ld\n",bnum,bookmark); |
| 353 |
// fill bookmark vector with byte positions |
| 354 |
byte_bookmarks.push_back(bookmark); |
| 355 |
bnum++; |
| 356 |
boff+=4; |
| 357 |
} |
| 358 |
} else // receiver model identified but header to short! |
| 359 |
fprintf(stderr,"Header probabely corrupted (%dbytes to short), discarding bookmarks!\n",hlen-len); |
| 360 |
|
| 361 |
// this routine stores bookmarks as byte positions! |
| 362 |
bmtype = byte; |
| 363 |
|
| 364 |
return type*100+bnum; |
| 365 |
} |
| 366 |
|
| 367 |
// newest topfield receivers (i.e. TF7700HDPVR) with additional ADD-file instead of a TS-header |
| 368 |
int |
| 369 |
tsfile::isTF7700HDPVR(std::string recfile) { |
| 370 |
int verbose=0; |
| 371 |
std::string addfile; |
| 372 |
|
| 373 |
// contruct the expected file name of the additonal info file |
| 374 |
if (!recfile.empty()) { |
| 375 |
int lastdot = recfile.rfind('.'); |
| 376 |
int lastslash = recfile.rfind('/'); |
| 377 |
if (lastdot >= 0 && lastdot >= lastslash) |
| 378 |
addfile = recfile.substr(0,lastdot) + ".add"; |
| 379 |
else |
| 380 |
return -20; |
| 381 |
} else |
| 382 |
return -10; |
| 383 |
|
| 384 |
uint8_t *buffer; |
| 385 |
ssize_t len = readfile(addfile, &buffer); |
| 386 |
if(len<0) return -30; |
| 387 |
|
| 388 |
/* the whole file (normally 2636 bytes) is now loaded in the memory buffer. */ |
| 389 |
|
| 390 |
int bnum = 0, boff = TF7700HDPVR_POS+4*TF7700HDPVR_MAX, unit=90000; |
| 391 |
// there has to be space for up to 48 bookmarks magic number! |
| 392 |
if(len>=boff+4) { |
| 393 |
// is it a topfield file? |
| 394 |
unsigned int magic = (buffer[boff]<<24)|(buffer[boff+1]<<16)|(buffer[boff+2]<<8)|buffer[boff+3]; |
| 395 |
if(magic!=TF5XXXPVR_MAGIC) { |
| 396 |
free(buffer); |
| 397 |
return -40; |
| 398 |
} |
| 399 |
else if(verbose) fprintf(stderr,"Found Topfield ADD-file: %s\n",addfile.c_str()); |
| 400 |
|
| 401 |
boff = TF7700HDPVR_POS; |
| 402 |
pts_t bookmark; |
| 403 |
// changed byte order compared to old receivers!?! |
| 404 |
while ((bookmark=(buffer[boff+3]<<24)|(buffer[boff+2]<<16)|(buffer[boff+1]<<8)|buffer[boff]) |
| 405 |
&& bnum<TF7700HDPVR_MAX) { |
| 406 |
if(verbose) fprintf(stderr,"BOOKMARK[%d] = %ld\n",bnum,bookmark); |
| 407 |
// bookmark is stored in seconds now, but we'll use full pts! |
| 408 |
bookmark*=unit; |
| 409 |
// fill bookmark vector with times |
| 410 |
time_bookmarks.push_back(bookmark); |
| 411 |
bnum++; |
| 412 |
boff+=4; |
| 413 |
} |
| 414 |
} else // receiver model identified but file to short! |
| 415 |
fprintf(stderr,"ADD-File probabely corrupted (%ld bytes to short), discarding bookmarks!\n",TF7700HDPVR_LEN-len); |
| 416 |
|
| 417 |
// terminate |
| 418 |
free(buffer); |
| 419 |
|
| 420 |
// this routine stores bookmarks as times! |
| 421 |
bmtype = time; |
| 422 |
|
| 423 |
return 600+bnum; |
| 424 |
} |
| 425 |
|
| 426 |
size_t |
| 427 |
tsfile::get_si_table(uint8_t *tbl, size_t max, size_t &index, int pid, int tid) { |
| 428 |
const uint8_t *d = (uint8_t*)buf.data(); |
| 429 |
size_t len = buf.inbytes(); |
| 430 |
uint8_t cc = 0xff; |
| 431 |
size_t size = 0; |
| 432 |
while (index + packetsize <= len) { |
| 433 |
const tspacket *p = (const tspacket*)&d[index]; |
| 434 |
index += packetsize; |
| 435 |
// check packet |
| 436 |
if (p->transport_error_indicator() |
| 437 |
|| p->pid() != pid |
| 438 |
|| !p->contains_payload()) { |
| 439 |
continue; |
| 440 |
} |
| 441 |
if (p->payload_unit_start_indicator()) { |
| 442 |
// first packet of table |
| 443 |
cc = p->continuity_counter(); |
| 444 |
size = 0; |
| 445 |
} |
| 446 |
else if (size != 0) { |
| 447 |
// additional packet, check continuity |
| 448 |
uint8_t ccdelta = (p->continuity_counter() - cc) & 0x0f; |
| 449 |
switch (ccdelta) { |
| 450 |
default: size = 0; continue; // sequence error, retry |
| 451 |
case 0: continue; // repeated packet |
| 452 |
case 1: break; // correct sequence |
| 453 |
} |
| 454 |
cc = p->continuity_counter(); |
| 455 |
} |
| 456 |
else { |
| 457 |
continue; |
| 458 |
} |
| 459 |
if (p->payload_length() < 1) { // payload too short |
| 460 |
continue; |
| 461 |
} |
| 462 |
const uint8_t *payload = (uint8_t*)p->payload(); |
| 463 |
size_t amount = p->payload_length(); |
| 464 |
unsigned n = 0; |
| 465 |
if (p->payload_unit_start_indicator()) { |
| 466 |
// evaluate pointer field |
| 467 |
n = payload[0] + 1; |
| 468 |
if (n >= amount) { // too short |
| 469 |
continue; |
| 470 |
} |
| 471 |
amount -= n; |
| 472 |
if (payload[n] != tid) { // wrong table |
| 473 |
continue; |
| 474 |
} |
| 475 |
} |
| 476 |
if (amount > max - size) { |
| 477 |
amount = max - size; |
| 478 |
} |
| 479 |
if (amount == 0) { |
| 480 |
// table too long, discard it |
| 481 |
size = 0; |
| 482 |
continue; |
| 483 |
} |
| 484 |
memcpy(tbl + size, payload + n, amount); |
| 485 |
size += amount; |
| 486 |
if (size >= 3) { |
| 487 |
size_t tmp = 3 + (((tbl[1] << 8) | tbl[2]) & 0x0fff); |
| 488 |
if (tmp <= size) { |
| 489 |
return tmp; // table complete |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
return 0; |
| 494 |
} |
| 495 |
|
| 496 |
static const uint8_t* |
| 497 |
get_stream_descriptor(const uint8_t *d, unsigned len) { |
| 498 |
while (len >= 2) { |
| 499 |
unsigned x = d[1] + 2; |
| 500 |
if (len < x) // descriptor truncated |
| 501 |
break; |
| 502 |
switch (d[0]) { |
| 503 |
default: |
| 504 |
break; |
| 505 |
/* audio */ |
| 506 |
case 0x6a: // AC-3 descriptor |
| 507 |
/* in the future, maybe also: |
| 508 |
case 0x73: // DTS audio descriptor |
| 509 |
case 0x79: // AAC descriptor |
| 510 |
case 0x7a: // enhanced AC-3 descriptor |
| 511 |
*/ |
| 512 |
return d; |
| 513 |
/* teletext/subtitles */ |
| 514 |
case 0x45: // VBI data descriptor |
| 515 |
case 0x46: // VBI teletext descriptor |
| 516 |
case 0x56: // teletext descriptor |
| 517 |
case 0x59: // subtitling descriptor |
| 518 |
return d; |
| 519 |
} |
| 520 |
d += x; |
| 521 |
len -= x; |
| 522 |
} |
| 523 |
return 0; |
| 524 |
} |
| 525 |
|
| 526 |
bool |
| 527 |
tsfile::check_si_tables() { |
| 528 |
unsigned len = buf.inbytes(); |
| 529 |
const uint8_t *d = (uint8_t*)buf.data(); |
| 530 |
bool pids[8192] = {}; |
| 531 |
|
| 532 |
// limit buffer size |
| 533 |
if (len > 40000 * packetsize) |
| 534 |
len = 40000 * packetsize; |
| 535 |
|
| 536 |
// find all PIDs |
| 537 |
for (unsigned i = 0; i + packetsize <= len; i += packetsize) { |
| 538 |
const tspacket *p = (const tspacket*)&d[i]; |
| 539 |
if (!p->transport_error_indicator()) |
| 540 |
pids[p->pid()] = true; |
| 541 |
} |
| 542 |
|
| 543 |
// drop out if PAT not present |
| 544 |
if (!pids[0]) |
| 545 |
return false; |
| 546 |
|
| 547 |
// read PAT |
| 548 |
uint8_t pat[1024]; |
| 549 |
size_t patlen; |
| 550 |
size_t index = 0; |
| 551 |
for (;;) { |
| 552 |
patlen = get_si_table(pat, sizeof(pat), index, 0x0000, 0x00); |
| 553 |
if (patlen == 0) // not found |
| 554 |
return false; |
| 555 |
if (patlen < 12 // too short |
| 556 |
|| (pat[1] & 0xc0) != 0x80 // bad syntax |
| 557 |
|| !(pat[5] & 0x01)) // not current |
| 558 |
continue; |
| 559 |
// XXX: CRC32 check omitted |
| 560 |
if (pat[6] != 0 || pat[7] != 0) { |
| 561 |
/* this is NOT an error, but currently unhandled */ |
| 562 |
fprintf(stderr, "can not handle segmented PAT; guessing streams\n"); |
| 563 |
return false; |
| 564 |
} |
| 565 |
break; |
| 566 |
} |
| 567 |
|
| 568 |
// get PMT PIDs |
| 569 |
std::list<int> pmts; |
| 570 |
for (unsigned i = 8; i + 8 <= patlen; i += 4) { |
| 571 |
if (pat[i] || pat[i + 1]) { |
| 572 |
int pid = ((pat[i + 2] << 8) | pat[i + 3]) & 0x1fff; |
| 573 |
if (pids[pid]) { |
| 574 |
fprintf(stderr, "PAT: found PMT at pid %d\n", pid); |
| 575 |
pmts.push_back(pid); |
| 576 |
} |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
// drop out if no PMT is present |
| 581 |
if (pmts.empty()) |
| 582 |
return false; |
| 583 |
|
| 584 |
// process PMTs until we find a valid one |
| 585 |
int vpid = -1; |
| 586 |
std::list<std::pair<int, int> > apids; |
| 587 |
for (std::list<int>::iterator it = pmts.begin(); it != pmts.end(); ++it) { |
| 588 |
// read PMT |
| 589 |
uint8_t pmt[1024]; |
| 590 |
size_t pmtlen; |
| 591 |
index = 0; |
| 592 |
for (;;) { |
| 593 |
pmtlen = get_si_table(pmt, sizeof(pmt), index, *it, 0x02); |
| 594 |
if (pmtlen == 0) // not found |
| 595 |
break; |
| 596 |
if (pmtlen < 16 // too short |
| 597 |
|| (pmt[1] & 0xc0) != 0x80 // bad syntax |
| 598 |
|| !(pmt[5] & 0x01) // not current |
| 599 |
|| pmt[6] != 0 || pmt[7] != 0) // not allowed |
| 600 |
continue; |
| 601 |
// XXX: CRC32 check omitted |
| 602 |
break; |
| 603 |
} |
| 604 |
if (pmtlen == 0) |
| 605 |
continue; |
| 606 |
|
| 607 |
// iterate through streams |
| 608 |
unsigned i = ((pmt[10] << 8) | pmt[11]) & 0x0fff; |
| 609 |
i = 12 + i; |
| 610 |
while (i + 9 <= pmtlen) { |
| 611 |
uint8_t stream_type = pmt[i]; |
| 612 |
int pid = ((pmt[i + 1] << 8) | pmt[i + 2]) & 0x1fff; |
| 613 |
size_t dlen = ((pmt[i + 3] << 8) | pmt[i + 4]) & 0x0fff; |
| 614 |
if (i + 5 + dlen + 4 > len) |
| 615 |
break; |
| 616 |
if (pids[pid]) { // is the PID actually present? |
| 617 |
switch (stream_type) { |
| 618 |
default: |
| 619 |
break; |
| 620 |
case 0x01: // MPEG-1 video |
| 621 |
case 0x02: // MPEG-2 video |
| 622 |
case 0x80: // legacy MPEG video |
| 623 |
if (vpid == -1) |
| 624 |
vpid = pid; |
| 625 |
break; |
| 626 |
case 0x03: // MPEG-1 audio |
| 627 |
case 0x04: // MPEG-2 audio |
| 628 |
case 0x81: // legacy AC-3 audio |
| 629 |
case 0x83: // legacy LPCM audio |
| 630 |
case 0x85: // legacy DTS audio |
| 631 |
apids.push_back(std::pair<int, int>(stream_type, pid)); |
| 632 |
break; |
| 633 |
case 0x06: // PES packets containing private data |
| 634 |
const uint8_t *desc = get_stream_descriptor(pmt + i + 5, dlen); |
| 635 |
if (desc) |
| 636 |
apids.push_back(std::pair<int, int>(256 + *desc, pid)); |
| 637 |
break; |
| 638 |
} |
| 639 |
} |
| 640 |
i += 5 + dlen; |
| 641 |
} |
| 642 |
|
| 643 |
// did we find at least a video stream? |
| 644 |
if (vpid != -1) { |
| 645 |
fprintf(stderr, "PMT: found video stream at pid %d\n", vpid); |
| 646 |
if (apids.empty()) { |
| 647 |
fprintf(stderr, "but I have to guess the audio streams :-(\n"); |
| 648 |
return false; |
| 649 |
} |
| 650 |
streamnumber[vpid] = VIDEOSTREAM; |
| 651 |
std::list<std::pair<int, int> >::iterator ait = apids.begin(); |
| 652 |
while (ait != apids.end()) { |
| 653 |
streamtype::type t = streamtype::unknown; |
| 654 |
switch (ait->first) { |
| 655 |
case 0x03: |
| 656 |
case 0x04: |
| 657 |
t = streamtype::mpegaudio; |
| 658 |
break; |
| 659 |
case 0x81: // legacy AC-3 audio |
| 660 |
case 0x16a: // AC-3 descriptor |
| 661 |
t = streamtype::ac3audio; |
| 662 |
break; |
| 663 |
/* in the future, maybe also: |
| 664 |
case 0x83: // legacy LPCM audio |
| 665 |
t = streamtype::pcmaudio; |
| 666 |
break; |
| 667 |
case 0x85: // legacy DTS audio |
| 668 |
case 0x173: // DTS audio descriptor |
| 669 |
t = streamtype::dtsaudio; |
| 670 |
break; |
| 671 |
case 0x179: // AAC descriptor |
| 672 |
t = streamtype::aacaudio; |
| 673 |
break; |
| 674 |
case 0x17a: // enhanced AC-3 descriptor |
| 675 |
t = streamtype::eac3audio; |
| 676 |
break; |
| 677 |
*/ |
| 678 |
case 0x145: // VBI data descriptor |
| 679 |
case 0x146: // VBI teletext descriptor |
| 680 |
case 0x156: // teletext descriptor |
| 681 |
// t = streamtype::vbisub; |
| 682 |
fprintf(stderr, "PMT: can't handle teletext stream at pid %d\n", ait->second); |
| 683 |
break; |
| 684 |
case 0x159: // subtitling descriptor |
| 685 |
// t = streamtype::dvbsub; |
| 686 |
fprintf(stderr, "PMT: can't handle subtitle stream at pid %d\n", ait->second); |
| 687 |
break; |
| 688 |
default: |
| 689 |
break; |
| 690 |
} |
| 691 |
if (t != streamtype::unknown) { |
| 692 |
fprintf(stderr, "PMT: found audio stream at pid %d\n", ait->second); |
| 693 |
streamnumber[ait->second] = audiostream(audiostreams); |
| 694 |
stream *S = &s[audiostream(audiostreams++)]; |
| 695 |
S->id = ait->second; |
| 696 |
S->type = t; |
| 697 |
if (audiostreams >= MAXAUDIOSTREAMS) |
| 698 |
break; |
| 699 |
} |
| 700 |
++ait; |
| 701 |
} |
| 702 |
initcodeccontexts(vpid); |
| 703 |
return true; |
| 704 |
} |
| 705 |
// start over |
| 706 |
apids.clear(); |
| 707 |
} |
| 708 |
return false; |
| 709 |
} |