| 1 |
/* |
| 2 |
* Copyright (C) 2013-2021 Jean-Luc Barrière |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 5 |
* See LICENSE.md for more information. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "tsDemuxer.h" |
| 9 |
#include "ES_MPEGVideo.h" |
| 10 |
#include "ES_MPEGAudio.h" |
| 11 |
#include "ES_h264.h" |
| 12 |
#include "ES_hevc.h" |
| 13 |
#include "ES_AAC.h" |
| 14 |
#include "ES_AC3.h" |
| 15 |
#include "ES_Subtitle.h" |
| 16 |
#include "ES_Teletext.h" |
| 17 |
#include "debug.h" |
| 18 |
|
| 19 |
#include <cassert> |
| 20 |
|
| 21 |
#define MAX_RESYNC_SIZE 65536 |
| 22 |
|
| 23 |
using namespace TSDemux; |
| 24 |
|
| 25 |
AVContext::AVContext(TSDemuxer* const demux, uint64_t pos, uint16_t channel) |
| 26 |
: av_pos(pos) |
| 27 |
, av_data_len(FLUTS_NORMAL_TS_PACKETSIZE) |
| 28 |
, av_pkt_size(0) |
| 29 |
, is_configured(false) |
| 30 |
, channel(channel) |
| 31 |
, pid(0xffff) |
| 32 |
, transport_error(false) |
| 33 |
, has_payload(false) |
| 34 |
, payload_unit_start(false) |
| 35 |
, discontinuity(false) |
| 36 |
, payload(NULL) |
| 37 |
, payload_len(0) |
| 38 |
, packet(NULL) |
| 39 |
{ |
| 40 |
m_demux = demux; |
| 41 |
memset(av_buf, 0, sizeof(av_buf)); |
| 42 |
}; |
| 43 |
|
| 44 |
void AVContext::Reset(void) |
| 45 |
{ |
| 46 |
Myth::OS::CLockGuard lock(mutex); |
| 47 |
|
| 48 |
pid = 0xffff; |
| 49 |
transport_error = false; |
| 50 |
has_payload = false; |
| 51 |
payload_unit_start = false; |
| 52 |
discontinuity = false; |
| 53 |
payload = NULL; |
| 54 |
payload_len = 0; |
| 55 |
packet = NULL; |
| 56 |
} |
| 57 |
|
| 58 |
uint16_t AVContext::GetPID() const |
| 59 |
{ |
| 60 |
return pid; |
| 61 |
} |
| 62 |
|
| 63 |
PACKET_TYPE AVContext::GetPIDType() const |
| 64 |
{ |
| 65 |
Myth::OS::CLockGuard lock(mutex); |
| 66 |
|
| 67 |
if (packet) |
| 68 |
return packet->packet_type; |
| 69 |
return PACKET_TYPE_UNKNOWN; |
| 70 |
} |
| 71 |
|
| 72 |
uint16_t AVContext::GetPIDChannel() const |
| 73 |
{ |
| 74 |
Myth::OS::CLockGuard lock(mutex); |
| 75 |
|
| 76 |
if (packet) |
| 77 |
return packet->channel; |
| 78 |
return 0xffff; |
| 79 |
} |
| 80 |
|
| 81 |
bool AVContext::HasPIDStreamData() const |
| 82 |
{ |
| 83 |
Myth::OS::CLockGuard lock(mutex); |
| 84 |
|
| 85 |
// PES packets append frame buffer of elementary stream until next start of unit |
| 86 |
// On new unit start, flag is held |
| 87 |
if (packet && packet->has_stream_data) |
| 88 |
return true; |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
bool AVContext::HasPIDPayload() const |
| 93 |
{ |
| 94 |
return has_payload; |
| 95 |
} |
| 96 |
|
| 97 |
ElementaryStream* AVContext::GetPIDStream() |
| 98 |
{ |
| 99 |
Myth::OS::CLockGuard lock(mutex); |
| 100 |
|
| 101 |
if (packet && packet->packet_type == PACKET_TYPE_PES) |
| 102 |
return packet->stream; |
| 103 |
return NULL; |
| 104 |
} |
| 105 |
|
| 106 |
std::vector<ElementaryStream*> AVContext::GetStreams() |
| 107 |
{ |
| 108 |
Myth::OS::CLockGuard lock(mutex); |
| 109 |
|
| 110 |
std::vector<ElementaryStream*> v; |
| 111 |
for (std::map<uint16_t, Packet>::iterator it = packets.begin(); it != packets.end(); ++it) |
| 112 |
if (it->second.packet_type == PACKET_TYPE_PES && it->second.stream) |
| 113 |
v.push_back(it->second.stream); |
| 114 |
return v; |
| 115 |
} |
| 116 |
|
| 117 |
void AVContext::StartStreaming(uint16_t pid) |
| 118 |
{ |
| 119 |
Myth::OS::CLockGuard lock(mutex); |
| 120 |
|
| 121 |
std::map<uint16_t, Packet>::iterator it = packets.find(pid); |
| 122 |
if (it != packets.end()) |
| 123 |
it->second.streaming = true; |
| 124 |
} |
| 125 |
|
| 126 |
void AVContext::StopStreaming(uint16_t pid) |
| 127 |
{ |
| 128 |
Myth::OS::CLockGuard lock(mutex); |
| 129 |
|
| 130 |
std::map<uint16_t, Packet>::iterator it = packets.find(pid); |
| 131 |
if (it != packets.end()) |
| 132 |
it->second.streaming = false; |
| 133 |
} |
| 134 |
|
| 135 |
ElementaryStream* AVContext::GetStream(uint16_t pid) const |
| 136 |
{ |
| 137 |
Myth::OS::CLockGuard lock(mutex); |
| 138 |
|
| 139 |
std::map<uint16_t, Packet>::const_iterator it = packets.find(pid); |
| 140 |
if (it != packets.end()) |
| 141 |
return it->second.stream; |
| 142 |
return NULL; |
| 143 |
} |
| 144 |
|
| 145 |
uint16_t AVContext::GetChannel(uint16_t pid) const |
| 146 |
{ |
| 147 |
Myth::OS::CLockGuard lock(mutex); |
| 148 |
|
| 149 |
std::map<uint16_t, Packet>::const_iterator it = packets.find(pid); |
| 150 |
if (it != packets.end()) |
| 151 |
return it->second.channel; |
| 152 |
return 0xffff; |
| 153 |
} |
| 154 |
|
| 155 |
void AVContext::ResetPackets() |
| 156 |
{ |
| 157 |
Myth::OS::CLockGuard lock(mutex); |
| 158 |
|
| 159 |
for (std::map<uint16_t, Packet>::iterator it = packets.begin(); it != packets.end(); ++it) |
| 160 |
{ |
| 161 |
it->second.Reset(); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
//////////////////////////////////////////////////////////////////////////////// |
| 166 |
///// |
| 167 |
///// MPEG-TS parser for the context |
| 168 |
///// |
| 169 |
|
| 170 |
uint8_t AVContext::av_rb8(const unsigned char* p) |
| 171 |
{ |
| 172 |
uint8_t val = *(uint8_t*)p; |
| 173 |
return val; |
| 174 |
} |
| 175 |
|
| 176 |
uint16_t AVContext::av_rb16(const unsigned char* p) |
| 177 |
{ |
| 178 |
uint16_t val = av_rb8(p) << 8; |
| 179 |
val |= av_rb8(p + 1); |
| 180 |
return val; |
| 181 |
} |
| 182 |
|
| 183 |
uint32_t AVContext::av_rb32(const unsigned char* p) |
| 184 |
{ |
| 185 |
uint32_t val = av_rb16(p) << 16; |
| 186 |
val |= av_rb16(p + 2); |
| 187 |
return val; |
| 188 |
} |
| 189 |
|
| 190 |
uint64_t AVContext::decode_pts(const unsigned char* p) |
| 191 |
{ |
| 192 |
uint64_t pts = (uint64_t)(av_rb8(p) & 0x0e) << 29 | (av_rb16(p + 1) >> 1) << 15 | av_rb16(p + 3) >> 1; |
| 193 |
return pts; |
| 194 |
} |
| 195 |
|
| 196 |
STREAM_TYPE AVContext::get_stream_type(uint8_t pes_type) |
| 197 |
{ |
| 198 |
switch (pes_type) |
| 199 |
{ |
| 200 |
case 0x01: |
| 201 |
return STREAM_TYPE_VIDEO_MPEG1; |
| 202 |
case 0x02: |
| 203 |
return STREAM_TYPE_VIDEO_MPEG2; |
| 204 |
case 0x03: |
| 205 |
return STREAM_TYPE_AUDIO_MPEG1; |
| 206 |
case 0x04: |
| 207 |
return STREAM_TYPE_AUDIO_MPEG2; |
| 208 |
case 0x06: |
| 209 |
return STREAM_TYPE_PRIVATE_DATA; |
| 210 |
case 0x0f: |
| 211 |
case 0x11: |
| 212 |
return STREAM_TYPE_AUDIO_AAC; |
| 213 |
case 0x10: |
| 214 |
return STREAM_TYPE_VIDEO_MPEG4; |
| 215 |
case 0x1b: |
| 216 |
return STREAM_TYPE_VIDEO_H264; |
| 217 |
case 0x24: |
| 218 |
return STREAM_TYPE_VIDEO_HEVC; |
| 219 |
case 0xea: |
| 220 |
return STREAM_TYPE_VIDEO_VC1; |
| 221 |
case 0x80: |
| 222 |
return STREAM_TYPE_AUDIO_LPCM; |
| 223 |
case 0x81: |
| 224 |
case 0x83: |
| 225 |
case 0x84: |
| 226 |
case 0x87: |
| 227 |
return STREAM_TYPE_AUDIO_AC3; |
| 228 |
case 0x82: |
| 229 |
case 0x85: |
| 230 |
case 0x8a: |
| 231 |
return STREAM_TYPE_AUDIO_DTS; |
| 232 |
} |
| 233 |
return STREAM_TYPE_UNKNOWN; |
| 234 |
} |
| 235 |
|
| 236 |
int AVContext::configure_ts() |
| 237 |
{ |
| 238 |
size_t data_size = AV_CONTEXT_PACKETSIZE; |
| 239 |
uint64_t pos = av_pos; |
| 240 |
int fluts[][2] = { |
| 241 |
{FLUTS_NORMAL_TS_PACKETSIZE, 0}, |
| 242 |
{FLUTS_M2TS_TS_PACKETSIZE, 0}, |
| 243 |
{FLUTS_DVB_ASI_TS_PACKETSIZE, 0}, |
| 244 |
{FLUTS_ATSC_TS_PACKETSIZE, 0} |
| 245 |
}; |
| 246 |
int nb = sizeof (fluts) / (2 * sizeof (int)); |
| 247 |
int score = TS_CHECK_MIN_SCORE; |
| 248 |
|
| 249 |
for (int i = 0; i < MAX_RESYNC_SIZE; i++) |
| 250 |
{ |
| 251 |
const unsigned char* data = m_demux->ReadAV(pos, data_size); |
| 252 |
if (!data) |
| 253 |
return AVCONTEXT_IO_ERROR; |
| 254 |
if (data[0] == 0x47) |
| 255 |
{ |
| 256 |
int count, found; |
| 257 |
for (int t = 0; t < nb; t++) // for all fluts |
| 258 |
{ |
| 259 |
const unsigned char* ndata; |
| 260 |
uint64_t npos = pos; |
| 261 |
int do_retry = score; // Reach for score |
| 262 |
do |
| 263 |
{ |
| 264 |
--do_retry; |
| 265 |
npos += fluts[t][0]; |
| 266 |
if (!(ndata = m_demux->ReadAV(npos, data_size))) |
| 267 |
return AVCONTEXT_IO_ERROR; |
| 268 |
} |
| 269 |
while (ndata[0] == 0x47 && (++fluts[t][1]) && do_retry); |
| 270 |
} |
| 271 |
// Is score reached ? |
| 272 |
count = found = 0; |
| 273 |
for (int t = 0; t < nb; t++) |
| 274 |
{ |
| 275 |
if (fluts[t][1] == score) |
| 276 |
{ |
| 277 |
found = t; |
| 278 |
++count; |
| 279 |
} |
| 280 |
// Reset score for next retry |
| 281 |
fluts[t][1] = 0; |
| 282 |
} |
| 283 |
// One and only one is eligible |
| 284 |
if (count == 1) |
| 285 |
{ |
| 286 |
DBG(DEMUX_DBG_DEBUG, "%s: packet size is %d\n", __FUNCTION__, fluts[found][0]); |
| 287 |
av_pkt_size = fluts[found][0]; |
| 288 |
av_pos = pos; |
| 289 |
return AVCONTEXT_CONTINUE; |
| 290 |
} |
| 291 |
// More one: Retry for highest score |
| 292 |
else if (count > 1 && ++score > TS_CHECK_MAX_SCORE) |
| 293 |
// Packet size remains undetermined |
| 294 |
break; |
| 295 |
// None: Bad sync. Shift and retry |
| 296 |
else |
| 297 |
pos++; |
| 298 |
} |
| 299 |
else |
| 300 |
pos++; |
| 301 |
} |
| 302 |
|
| 303 |
DBG(DEMUX_DBG_ERROR, "%s: invalid stream\n", __FUNCTION__); |
| 304 |
return AVCONTEXT_TS_NOSYNC; |
| 305 |
} |
| 306 |
|
| 307 |
int AVContext::TSResync() |
| 308 |
{ |
| 309 |
if (!is_configured) |
| 310 |
{ |
| 311 |
int ret = configure_ts(); |
| 312 |
if (ret != AVCONTEXT_CONTINUE) |
| 313 |
return ret; |
| 314 |
is_configured = true; |
| 315 |
} |
| 316 |
for (int i = 0; i < MAX_RESYNC_SIZE; i++) |
| 317 |
{ |
| 318 |
const unsigned char* data = m_demux->ReadAV(av_pos, av_pkt_size); |
| 319 |
if (!data) |
| 320 |
return AVCONTEXT_IO_ERROR; |
| 321 |
if (data[0] == 0x47) |
| 322 |
{ |
| 323 |
memcpy(av_buf, data, av_pkt_size); |
| 324 |
Reset(); |
| 325 |
return AVCONTEXT_CONTINUE; |
| 326 |
} |
| 327 |
av_pos++; |
| 328 |
} |
| 329 |
|
| 330 |
return AVCONTEXT_TS_NOSYNC; |
| 331 |
} |
| 332 |
|
| 333 |
uint64_t AVContext::GoNext() |
| 334 |
{ |
| 335 |
av_pos += av_pkt_size; |
| 336 |
Reset(); |
| 337 |
return av_pos; |
| 338 |
} |
| 339 |
|
| 340 |
uint64_t AVContext::Shift() |
| 341 |
{ |
| 342 |
av_pos++; |
| 343 |
Reset(); |
| 344 |
return av_pos; |
| 345 |
} |
| 346 |
|
| 347 |
void AVContext::GoPosition(uint64_t pos) |
| 348 |
{ |
| 349 |
av_pos = pos; |
| 350 |
Reset(); |
| 351 |
} |
| 352 |
|
| 353 |
uint64_t AVContext::GetPosition() const |
| 354 |
{ |
| 355 |
return av_pos; |
| 356 |
} |
| 357 |
|
| 358 |
/* |
| 359 |
* Process TS packet |
| 360 |
* |
| 361 |
* returns: |
| 362 |
* |
| 363 |
* AVCONTEXT_CONTINUE |
| 364 |
* Parse completed. If has payload, process it else Continue to next packet. |
| 365 |
* |
| 366 |
* AVCONTEXT_STREAM_PID_DATA |
| 367 |
* Parse completed. A new PES unit starts and data of elementary stream for |
| 368 |
* the PID must be picked before processing this payload. |
| 369 |
* |
| 370 |
* AVCONTEXT_DISCONTINUITY |
| 371 |
* Discontinuity. PID will wait until next unit start. So continue to next |
| 372 |
* packet. |
| 373 |
* |
| 374 |
* AVCONTEXT_TS_NOSYNC |
| 375 |
* Bad sync byte. Should run TSResync(). |
| 376 |
* |
| 377 |
* AVCONTEXT_TS_ERROR |
| 378 |
* Parsing error ! |
| 379 |
*/ |
| 380 |
int AVContext::ProcessTSPacket() |
| 381 |
{ |
| 382 |
Myth::OS::CLockGuard lock(mutex); |
| 383 |
|
| 384 |
int ret = AVCONTEXT_CONTINUE; |
| 385 |
std::map<uint16_t, Packet>::iterator it; |
| 386 |
|
| 387 |
if (av_rb8(this->av_buf) != 0x47) // ts sync byte |
| 388 |
return AVCONTEXT_TS_NOSYNC; |
| 389 |
|
| 390 |
uint16_t header = av_rb16(this->av_buf + 1); |
| 391 |
this->pid = header & 0x1fff; |
| 392 |
this->transport_error = (header & 0x8000) != 0; |
| 393 |
this->payload_unit_start = (header & 0x4000) != 0; |
| 394 |
// Cleaning context |
| 395 |
this->discontinuity = false; |
| 396 |
this->has_payload = false; |
| 397 |
this->payload = NULL; |
| 398 |
this->payload_len = 0; |
| 399 |
|
| 400 |
if (this->transport_error) |
| 401 |
return AVCONTEXT_CONTINUE; |
| 402 |
// Null packet |
| 403 |
if (this->pid == 0x1fff) |
| 404 |
return AVCONTEXT_CONTINUE; |
| 405 |
|
| 406 |
uint8_t flags = av_rb8(this->av_buf + 3); |
| 407 |
bool has_payload = (flags & 0x10) != 0; |
| 408 |
bool is_discontinuity = false; |
| 409 |
uint8_t continuity_counter = flags & 0x0f; |
| 410 |
bool has_adaptation = (flags & 0x20) != 0; |
| 411 |
size_t n = 0; |
| 412 |
if (has_adaptation) |
| 413 |
{ |
| 414 |
size_t len = (size_t)av_rb8(this->av_buf + 4); |
| 415 |
if (len > (this->av_data_len - 5)) |
| 416 |
{ |
| 417 |
#if defined(TSDEMUX_DEBUG) |
| 418 |
assert(false); |
| 419 |
#else |
| 420 |
return AVCONTEXT_TS_ERROR; |
| 421 |
#endif |
| 422 |
} |
| 423 |
n = len + 1; |
| 424 |
if (len > 0) |
| 425 |
{ |
| 426 |
is_discontinuity = (av_rb8(this->av_buf + 5) & 0x80) != 0; |
| 427 |
} |
| 428 |
} |
| 429 |
if (has_payload) |
| 430 |
{ |
| 431 |
// Payload start after adaptation fields |
| 432 |
this->payload = this->av_buf + n + 4; |
| 433 |
this->payload_len = this->av_data_len - n - 4; |
| 434 |
} |
| 435 |
|
| 436 |
it = this->packets.find(this->pid); |
| 437 |
if (it == this->packets.end()) |
| 438 |
{ |
| 439 |
// Not registred PID |
| 440 |
// We are waiting for unit start of PID 0 else next packet is required |
| 441 |
if (this->pid == 0 && this->payload_unit_start) |
| 442 |
{ |
| 443 |
// Registering PID 0 |
| 444 |
Packet pid0; |
| 445 |
pid0.pid = this->pid; |
| 446 |
pid0.packet_type = PACKET_TYPE_PSI; |
| 447 |
pid0.continuity = continuity_counter; |
| 448 |
it = this->packets.insert(it, std::make_pair(this->pid, pid0)); |
| 449 |
} |
| 450 |
else |
| 451 |
return AVCONTEXT_CONTINUE; |
| 452 |
} |
| 453 |
else |
| 454 |
{ |
| 455 |
// PID is registred |
| 456 |
// Checking unit start is required |
| 457 |
if (it->second.wait_unit_start && !this->payload_unit_start) |
| 458 |
{ |
| 459 |
// Not unit start. Save packet flow continuity... |
| 460 |
it->second.continuity = continuity_counter; |
| 461 |
this->discontinuity = true; |
| 462 |
return AVCONTEXT_DISCONTINUITY; |
| 463 |
} |
| 464 |
// Checking continuity where possible |
| 465 |
if (it->second.continuity != 0xff) |
| 466 |
{ |
| 467 |
uint8_t expected_cc = has_payload ? (it->second.continuity + 1) & 0x0f : it->second.continuity; |
| 468 |
if (!is_discontinuity && expected_cc != continuity_counter) |
| 469 |
{ |
| 470 |
this->discontinuity = true; |
| 471 |
// If unit is not start then reset PID and wait the next unit start |
| 472 |
if (!this->payload_unit_start) |
| 473 |
{ |
| 474 |
it->second.Reset(); |
| 475 |
DBG(DEMUX_DBG_WARN, "PID %.4x discontinuity detected: found %u, expected %u\n", this->pid, continuity_counter, expected_cc); |
| 476 |
return AVCONTEXT_DISCONTINUITY; |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
it->second.continuity = continuity_counter; |
| 481 |
} |
| 482 |
|
| 483 |
this->discontinuity |= is_discontinuity; |
| 484 |
this->has_payload = has_payload; |
| 485 |
this->packet = &(it->second); |
| 486 |
|
| 487 |
// It is time to stream data for PES |
| 488 |
if (this->payload_unit_start && |
| 489 |
this->packet->streaming && |
| 490 |
this->packet->packet_type == PACKET_TYPE_PES && |
| 491 |
!this->packet->wait_unit_start) |
| 492 |
{ |
| 493 |
this->packet->has_stream_data = true; |
| 494 |
ret = AVCONTEXT_STREAM_PID_DATA; |
| 495 |
} |
| 496 |
return ret; |
| 497 |
} |
| 498 |
|
| 499 |
/* |
| 500 |
* Process payload of packet depending of its type |
| 501 |
* |
| 502 |
* PACKET_TYPE_PSI -> parse_ts_psi() |
| 503 |
* PACKET_TYPE_PES -> parse_ts_pes() |
| 504 |
*/ |
| 505 |
int AVContext::ProcessTSPayload() |
| 506 |
{ |
| 507 |
Myth::OS::CLockGuard lock(mutex); |
| 508 |
|
| 509 |
if (!this->packet) |
| 510 |
return AVCONTEXT_CONTINUE; |
| 511 |
|
| 512 |
int ret = 0; |
| 513 |
switch (this->packet->packet_type) |
| 514 |
{ |
| 515 |
case PACKET_TYPE_PSI: |
| 516 |
ret = parse_ts_psi(); |
| 517 |
break; |
| 518 |
case PACKET_TYPE_PES: |
| 519 |
ret = parse_ts_pes(); |
| 520 |
break; |
| 521 |
case PACKET_TYPE_UNKNOWN: |
| 522 |
break; |
| 523 |
} |
| 524 |
|
| 525 |
return ret; |
| 526 |
} |
| 527 |
|
| 528 |
void AVContext::clear_pmt() |
| 529 |
{ |
| 530 |
DBG(DEMUX_DBG_DEBUG, "%s\n", __FUNCTION__); |
| 531 |
std::vector<uint16_t> pid_list; |
| 532 |
for (std::map<uint16_t, Packet>::iterator it = this->packets.begin(); it != this->packets.end(); ++it) |
| 533 |
{ |
| 534 |
if (it->second.packet_type == PACKET_TYPE_PSI && it->second.packet_table.table_id == 0x02) |
| 535 |
{ |
| 536 |
pid_list.push_back(it->first); |
| 537 |
clear_pes(it->second.channel); |
| 538 |
} |
| 539 |
} |
| 540 |
for (std::vector<uint16_t>::iterator it = pid_list.begin(); it != pid_list.end(); ++it) |
| 541 |
this->packets.erase(*it); |
| 542 |
} |
| 543 |
|
| 544 |
void AVContext::clear_pes(uint16_t channel) |
| 545 |
{ |
| 546 |
DBG(DEMUX_DBG_DEBUG, "%s(%u)\n", __FUNCTION__, channel); |
| 547 |
std::vector<uint16_t> pid_list; |
| 548 |
for (std::map<uint16_t, Packet>::iterator it = this->packets.begin(); it != this->packets.end(); ++it) |
| 549 |
{ |
| 550 |
if (it->second.packet_type == PACKET_TYPE_PES && it->second.channel == channel) |
| 551 |
pid_list.push_back(it->first); |
| 552 |
} |
| 553 |
for (std::vector<uint16_t>::iterator it = pid_list.begin(); it != pid_list.end(); ++it) |
| 554 |
this->packets.erase(*it); |
| 555 |
} |
| 556 |
|
| 557 |
/* |
| 558 |
* Parse PSI payload |
| 559 |
* |
| 560 |
* returns: |
| 561 |
* |
| 562 |
* AVCONTEXT_CONTINUE |
| 563 |
* Parse completed. Continue to next packet |
| 564 |
* |
| 565 |
* AVCONTEXT_PROGRAM_CHANGE |
| 566 |
* Parse completed. The program has changed. All streams are resetted and |
| 567 |
* streaming flag is set to false. Client must inspect streams MAP and enable |
| 568 |
* streaming for those recognized. |
| 569 |
* |
| 570 |
* AVCONTEXT_TS_ERROR |
| 571 |
* Parsing error ! |
| 572 |
*/ |
| 573 |
int AVContext::parse_ts_psi() |
| 574 |
{ |
| 575 |
size_t len; |
| 576 |
|
| 577 |
if (!this->has_payload || !this->payload || !this->payload_len || !this->packet) |
| 578 |
return AVCONTEXT_CONTINUE; |
| 579 |
|
| 580 |
if (this->payload_unit_start) |
| 581 |
{ |
| 582 |
// Reset wait for unit start |
| 583 |
this->packet->wait_unit_start = false; |
| 584 |
// pointer field present |
| 585 |
len = (size_t)av_rb8(this->payload); |
| 586 |
if (len > this->payload_len) |
| 587 |
{ |
| 588 |
#if defined(TSDEMUX_DEBUG) |
| 589 |
assert(false); |
| 590 |
#else |
| 591 |
return AVCONTEXT_TS_ERROR; |
| 592 |
#endif |
| 593 |
} |
| 594 |
|
| 595 |
// table ID |
| 596 |
uint8_t table_id = av_rb8(this->payload + 1); |
| 597 |
|
| 598 |
// table length |
| 599 |
len = (size_t)av_rb16(this->payload + 2); |
| 600 |
if ((len & 0x3000) != 0x3000) |
| 601 |
{ |
| 602 |
#if defined(TSDEMUX_DEBUG) |
| 603 |
assert(false); |
| 604 |
#else |
| 605 |
return AVCONTEXT_TS_ERROR; |
| 606 |
#endif |
| 607 |
} |
| 608 |
len &= 0x0fff; |
| 609 |
|
| 610 |
this->packet->packet_table.Reset(); |
| 611 |
|
| 612 |
size_t n = this->payload_len - 4; |
| 613 |
memcpy(this->packet->packet_table.buf, this->payload + 4, n); |
| 614 |
this->packet->packet_table.table_id = table_id; |
| 615 |
this->packet->packet_table.offset = n; |
| 616 |
this->packet->packet_table.len = len; |
| 617 |
// check for incomplete section |
| 618 |
if (this->packet->packet_table.offset < this->packet->packet_table.len) |
| 619 |
return AVCONTEXT_CONTINUE; |
| 620 |
} |
| 621 |
else |
| 622 |
{ |
| 623 |
// next part of PSI |
| 624 |
if (this->packet->packet_table.offset == 0) |
| 625 |
{ |
| 626 |
#if defined(TSDEMUX_DEBUG) |
| 627 |
assert(false); |
| 628 |
#else |
| 629 |
return AVCONTEXT_TS_ERROR; |
| 630 |
#endif |
| 631 |
} |
| 632 |
|
| 633 |
if ((this->payload_len + this->packet->packet_table.offset) > TABLE_BUFFER_SIZE) |
| 634 |
{ |
| 635 |
#if defined(TSDEMUX_DEBUG) |
| 636 |
assert(false); |
| 637 |
#else |
| 638 |
return AVCONTEXT_TS_ERROR; |
| 639 |
#endif |
| 640 |
} |
| 641 |
memcpy(this->packet->packet_table.buf + this->packet->packet_table.offset, this->payload, this->payload_len); |
| 642 |
this->packet->packet_table.offset += this->payload_len; |
| 643 |
// check for incomplete section |
| 644 |
if (this->packet->packet_table.offset < this->packet->packet_table.len) |
| 645 |
return AVCONTEXT_CONTINUE; |
| 646 |
} |
| 647 |
|
| 648 |
// now entire table is filled |
| 649 |
const unsigned char* psi = this->packet->packet_table.buf; |
| 650 |
const unsigned char* end_psi = psi + this->packet->packet_table.len; |
| 651 |
|
| 652 |
switch (this->packet->packet_table.table_id) |
| 653 |
{ |
| 654 |
case 0x00: // parse PAT table |
| 655 |
{ |
| 656 |
// check if version number changed |
| 657 |
uint16_t id = av_rb16(psi); |
| 658 |
// check if applicable |
| 659 |
if ((av_rb8(psi + 2) & 0x01) == 0) |
| 660 |
return AVCONTEXT_CONTINUE; |
| 661 |
// check if version number changed |
| 662 |
uint8_t version = (av_rb8(psi + 2) & 0x3e) >> 1; |
| 663 |
if (id == this->packet->packet_table.id && version == this->packet->packet_table.version) |
| 664 |
return AVCONTEXT_CONTINUE; |
| 665 |
DBG(DEMUX_DBG_DEBUG, "%s: new PAT version %u\n", __FUNCTION__, version); |
| 666 |
|
| 667 |
// clear old associated pmt |
| 668 |
clear_pmt(); |
| 669 |
|
| 670 |
// parse new version of PAT |
| 671 |
psi += 5; |
| 672 |
|
| 673 |
end_psi -= 4; // CRC32 |
| 674 |
|
| 675 |
if (psi >= end_psi) |
| 676 |
{ |
| 677 |
#if defined(TSDEMUX_DEBUG) |
| 678 |
assert(false); |
| 679 |
#else |
| 680 |
return AVCONTEXT_TS_ERROR; |
| 681 |
#endif |
| 682 |
} |
| 683 |
|
| 684 |
len = end_psi - psi; |
| 685 |
|
| 686 |
if (len % 4) |
| 687 |
{ |
| 688 |
#if defined(TSDEMUX_DEBUG) |
| 689 |
assert(false); |
| 690 |
#else |
| 691 |
return AVCONTEXT_TS_ERROR; |
| 692 |
#endif |
| 693 |
} |
| 694 |
|
| 695 |
size_t n = len / 4; |
| 696 |
|
| 697 |
for (size_t i = 0; i < n; i++, psi += 4) |
| 698 |
{ |
| 699 |
uint16_t channel = av_rb16(psi); |
| 700 |
uint16_t pmt_pid = av_rb16(psi + 2); |
| 701 |
|
| 702 |
// Reserved fields in table sections must be "set" to '1' bits. |
| 703 |
//if ((pmt_pid & 0xe000) != 0xe000) |
| 704 |
// return AVCONTEXT_TS_ERROR; |
| 705 |
|
| 706 |
pmt_pid &= 0x1fff; |
| 707 |
|
| 708 |
DBG(DEMUX_DBG_DEBUG, "%s: PAT version %u: new PMT %.4x channel %u\n", __FUNCTION__, version, pmt_pid, channel); |
| 709 |
if (this->channel == 0 || this->channel == channel) |
| 710 |
{ |
| 711 |
Packet& pmt = this->packets[pmt_pid]; |
| 712 |
pmt.pid = pmt_pid; |
| 713 |
pmt.packet_type = PACKET_TYPE_PSI; |
| 714 |
pmt.channel = channel; |
| 715 |
DBG(DEMUX_DBG_DEBUG, "%s: PAT version %u: register PMT %.4x channel %u\n", __FUNCTION__, version, pmt_pid, channel); |
| 716 |
} |
| 717 |
} |
| 718 |
// PAT is processed. New version is available |
| 719 |
this->packet->packet_table.id = id; |
| 720 |
this->packet->packet_table.version = version; |
| 721 |
break; |
| 722 |
} |
| 723 |
case 0x02: // parse PMT table |
| 724 |
{ |
| 725 |
uint16_t id = av_rb16(psi); |
| 726 |
// check if applicable |
| 727 |
if ((av_rb8(psi + 2) & 0x01) == 0) |
| 728 |
return AVCONTEXT_CONTINUE; |
| 729 |
// check if version number changed |
| 730 |
uint8_t version = (av_rb8(psi + 2) & 0x3e) >> 1; |
| 731 |
if (id == this->packet->packet_table.id && version == this->packet->packet_table.version) |
| 732 |
return AVCONTEXT_CONTINUE; |
| 733 |
DBG(DEMUX_DBG_DEBUG, "%s: PMT(%.4x) version %u\n", __FUNCTION__, this->packet->pid, version); |
| 734 |
|
| 735 |
// clear old pes |
| 736 |
clear_pes(this->packet->channel); |
| 737 |
|
| 738 |
// parse new version of PMT |
| 739 |
psi += 7; |
| 740 |
|
| 741 |
end_psi -= 4; // CRC32 |
| 742 |
|
| 743 |
if (psi >= end_psi) |
| 744 |
{ |
| 745 |
#if defined(TSDEMUX_DEBUG) |
| 746 |
assert(false); |
| 747 |
#else |
| 748 |
return AVCONTEXT_TS_ERROR; |
| 749 |
#endif |
| 750 |
} |
| 751 |
|
| 752 |
len = (size_t)(av_rb16(psi) & 0x0fff); |
| 753 |
psi += 2 + len; |
| 754 |
|
| 755 |
while (psi < end_psi) |
| 756 |
{ |
| 757 |
if (end_psi - psi < 5) |
| 758 |
{ |
| 759 |
#if defined(TSDEMUX_DEBUG) |
| 760 |
assert(false); |
| 761 |
#else |
| 762 |
return AVCONTEXT_TS_ERROR; |
| 763 |
#endif |
| 764 |
} |
| 765 |
|
| 766 |
uint8_t pes_type = av_rb8(psi); |
| 767 |
uint16_t pes_pid = av_rb16(psi + 1); |
| 768 |
|
| 769 |
// Reserved fields in table sections must be "set" to '1' bits. |
| 770 |
//if ((pes_pid & 0xe000) != 0xe000) |
| 771 |
// return AVCONTEXT_TS_ERROR; |
| 772 |
|
| 773 |
pes_pid &= 0x1fff; |
| 774 |
|
| 775 |
// len of descriptor section |
| 776 |
len = (size_t)(av_rb16(psi + 3) & 0x0fff); |
| 777 |
psi += 5; |
| 778 |
|
| 779 |
// ignore unknown streams |
| 780 |
STREAM_TYPE stream_type = get_stream_type(pes_type); |
| 781 |
DBG(DEMUX_DBG_DEBUG, "%s: PMT(%.4x) version %u: new PES %.4x %s\n", __FUNCTION__, |
| 782 |
this->packet->pid, version, pes_pid, ElementaryStream::GetStreamCodecName(stream_type)); |
| 783 |
if (stream_type != STREAM_TYPE_UNKNOWN) |
| 784 |
{ |
| 785 |
Packet& pes = this->packets[pes_pid]; |
| 786 |
pes.pid = pes_pid; |
| 787 |
pes.packet_type = PACKET_TYPE_PES; |
| 788 |
pes.channel = this->packet->channel; |
| 789 |
// Disable streaming by default |
| 790 |
pes.streaming = false; |
| 791 |
// Get basic stream infos from PMT table |
| 792 |
STREAM_INFO stream_info; |
| 793 |
stream_info = parse_pes_descriptor(psi, len, &stream_type); |
| 794 |
|
| 795 |
ElementaryStream* es; |
| 796 |
switch (stream_type) |
| 797 |
{ |
| 798 |
case STREAM_TYPE_VIDEO_MPEG1: |
| 799 |
case STREAM_TYPE_VIDEO_MPEG2: |
| 800 |
es = new ES_MPEG2Video(pes_pid); |
| 801 |
break; |
| 802 |
case STREAM_TYPE_AUDIO_MPEG1: |
| 803 |
case STREAM_TYPE_AUDIO_MPEG2: |
| 804 |
es = new ES_MPEG2Audio(pes_pid); |
| 805 |
break; |
| 806 |
case STREAM_TYPE_AUDIO_AAC: |
| 807 |
case STREAM_TYPE_AUDIO_AAC_ADTS: |
| 808 |
case STREAM_TYPE_AUDIO_AAC_LATM: |
| 809 |
es = new ES_AAC(pes_pid); |
| 810 |
break; |
| 811 |
case STREAM_TYPE_VIDEO_H264: |
| 812 |
es = new ES_h264(pes_pid); |
| 813 |
break; |
| 814 |
case STREAM_TYPE_VIDEO_HEVC: |
| 815 |
es = new ES_hevc(pes_pid); |
| 816 |
break; |
| 817 |
case STREAM_TYPE_AUDIO_AC3: |
| 818 |
case STREAM_TYPE_AUDIO_EAC3: |
| 819 |
es = new ES_AC3(pes_pid); |
| 820 |
break; |
| 821 |
case STREAM_TYPE_DVB_SUBTITLE: |
| 822 |
es = new ES_Subtitle(pes_pid); |
| 823 |
break; |
| 824 |
case STREAM_TYPE_DVB_TELETEXT: |
| 825 |
es = new ES_Teletext(pes_pid); |
| 826 |
break; |
| 827 |
default: |
| 828 |
// No parser: pass-through |
| 829 |
es = new ElementaryStream(pes_pid); |
| 830 |
es->has_stream_info = true; |
| 831 |
break; |
| 832 |
} |
| 833 |
|
| 834 |
es->stream_type = stream_type; |
| 835 |
es->stream_info = stream_info; |
| 836 |
pes.stream = es; |
| 837 |
DBG(DEMUX_DBG_DEBUG, "%s: PMT(%.4x) version %u: register PES %.4x %s\n", __FUNCTION__, |
| 838 |
this->packet->pid, version, pes_pid, es->GetStreamCodecName()); |
| 839 |
} |
| 840 |
psi += len; |
| 841 |
} |
| 842 |
|
| 843 |
if (psi != end_psi) |
| 844 |
{ |
| 845 |
#if defined(TSDEMUX_DEBUG) |
| 846 |
assert(false); |
| 847 |
#else |
| 848 |
return AVCONTEXT_TS_ERROR; |
| 849 |
#endif |
| 850 |
} |
| 851 |
|
| 852 |
// PMT is processed. New version is available |
| 853 |
this->packet->packet_table.id = id; |
| 854 |
this->packet->packet_table.version = version; |
| 855 |
return AVCONTEXT_PROGRAM_CHANGE; |
| 856 |
} |
| 857 |
default: |
| 858 |
// CAT, NIT table |
| 859 |
break; |
| 860 |
} |
| 861 |
|
| 862 |
return AVCONTEXT_CONTINUE; |
| 863 |
} |
| 864 |
|
| 865 |
STREAM_INFO AVContext::parse_pes_descriptor(const unsigned char* p, size_t len, STREAM_TYPE* st) |
| 866 |
{ |
| 867 |
const unsigned char* desc_end = p + len; |
| 868 |
STREAM_INFO si; |
| 869 |
memset(&si, 0, sizeof(STREAM_INFO)); |
| 870 |
|
| 871 |
while (p < desc_end) |
| 872 |
{ |
| 873 |
uint8_t desc_tag = av_rb8(p); |
| 874 |
uint8_t desc_len = av_rb8(p + 1); |
| 875 |
p += 2; |
| 876 |
DBG(DEMUX_DBG_DEBUG, "%s: tag %.2x len %d\n", __FUNCTION__, desc_tag, desc_len); |
| 877 |
switch (desc_tag) |
| 878 |
{ |
| 879 |
case 0x02: |
| 880 |
case 0x03: |
| 881 |
break; |
| 882 |
case 0x0a: /* ISO 639 language descriptor */ |
| 883 |
if (desc_len >= 4) |
| 884 |
{ |
| 885 |
si.language[0] = av_rb8(p); |
| 886 |
si.language[1] = av_rb8(p + 1); |
| 887 |
si.language[2] = av_rb8(p + 2); |
| 888 |
si.language[3] = 0; |
| 889 |
} |
| 890 |
break; |
| 891 |
case 0x56: /* DVB teletext descriptor */ |
| 892 |
*st = STREAM_TYPE_DVB_TELETEXT; |
| 893 |
break; |
| 894 |
case 0x6a: /* DVB AC3 */ |
| 895 |
case 0x81: /* AC3 audio stream */ |
| 896 |
*st = STREAM_TYPE_AUDIO_AC3; |
| 897 |
break; |
| 898 |
case 0x7a: /* DVB enhanced AC3 */ |
| 899 |
*st = STREAM_TYPE_AUDIO_EAC3; |
| 900 |
break; |
| 901 |
case 0x7b: /* DVB DTS */ |
| 902 |
*st = STREAM_TYPE_AUDIO_DTS; |
| 903 |
break; |
| 904 |
case 0x7c: /* DVB AAC */ |
| 905 |
*st = STREAM_TYPE_AUDIO_AAC; |
| 906 |
break; |
| 907 |
case 0x59: /* subtitling descriptor */ |
| 908 |
if (desc_len >= 8) |
| 909 |
{ |
| 910 |
/* |
| 911 |
* Byte 4 is the subtitling_type field |
| 912 |
* av_rb8(p + 3) & 0x10 : normal |
| 913 |
* av_rb8(p + 3) & 0x20 : for the hard of hearing |
| 914 |
*/ |
| 915 |
*st = STREAM_TYPE_DVB_SUBTITLE; |
| 916 |
si.language[0] = av_rb8(p); |
| 917 |
si.language[1] = av_rb8(p + 1); |
| 918 |
si.language[2] = av_rb8(p + 2); |
| 919 |
si.language[3] = 0; |
| 920 |
si.composition_id = (int)av_rb16(p + 4); |
| 921 |
si.ancillary_id = (int)av_rb16(p + 6); |
| 922 |
} |
| 923 |
break; |
| 924 |
case 0x05: /* registration descriptor */ |
| 925 |
case 0x1E: /* SL descriptor */ |
| 926 |
case 0x1F: /* FMC descriptor */ |
| 927 |
case 0x52: /* stream identifier descriptor */ |
| 928 |
default: |
| 929 |
break; |
| 930 |
} |
| 931 |
p += desc_len; |
| 932 |
} |
| 933 |
|
| 934 |
return si; |
| 935 |
} |
| 936 |
|
| 937 |
/* |
| 938 |
* Parse PES payload |
| 939 |
* |
| 940 |
* returns: |
| 941 |
* |
| 942 |
* AVCONTEXT_CONTINUE |
| 943 |
* Parse completed. When streaming is enabled for PID, data is appended to |
| 944 |
* the frame buffer of corresponding elementary stream. |
| 945 |
* |
| 946 |
* AVCONTEXT_TS_ERROR |
| 947 |
* Parsing error ! |
| 948 |
*/ |
| 949 |
int AVContext::parse_ts_pes() |
| 950 |
{ |
| 951 |
if (!this->has_payload || !this->payload || !this->payload_len || !this->packet) |
| 952 |
return AVCONTEXT_CONTINUE; |
| 953 |
|
| 954 |
if (!this->packet->stream) |
| 955 |
return AVCONTEXT_CONTINUE; |
| 956 |
|
| 957 |
if (this->payload_unit_start) |
| 958 |
{ |
| 959 |
// Wait for unit start: Reset frame buffer to clear old data |
| 960 |
if (this->packet->wait_unit_start) |
| 961 |
{ |
| 962 |
packet->stream->Reset(); |
| 963 |
packet->stream->p_dts = PTS_UNSET; |
| 964 |
packet->stream->p_pts = PTS_UNSET; |
| 965 |
} |
| 966 |
this->packet->wait_unit_start = false; |
| 967 |
this->packet->has_stream_data = false; |
| 968 |
// Reset header table |
| 969 |
this->packet->packet_table.Reset(); |
| 970 |
// Header len is at least 6 bytes. So getting 6 bytes first |
| 971 |
this->packet->packet_table.len = 6; |
| 972 |
} |
| 973 |
|
| 974 |
// Position in the payload buffer. Start at 0 |
| 975 |
size_t pos = 0; |
| 976 |
|
| 977 |
while (this->packet->packet_table.offset < this->packet->packet_table.len) |
| 978 |
{ |
| 979 |
if (pos >= this->payload_len) |
| 980 |
return AVCONTEXT_CONTINUE; |
| 981 |
|
| 982 |
size_t n = this->packet->packet_table.len - this->packet->packet_table.offset; |
| 983 |
|
| 984 |
if (n > (this->payload_len - pos)) |
| 985 |
n = this->payload_len - pos; |
| 986 |
|
| 987 |
memcpy(this->packet->packet_table.buf + this->packet->packet_table.offset, this->payload + pos, n); |
| 988 |
this->packet->packet_table.offset += n; |
| 989 |
pos += n; |
| 990 |
|
| 991 |
if (this->packet->packet_table.offset == 6) |
| 992 |
{ |
| 993 |
if (memcmp(this->packet->packet_table.buf, "\x00\x00\x01", 3) == 0) |
| 994 |
{ |
| 995 |
uint8_t stream_id = av_rb8(this->packet->packet_table.buf + 3); |
| 996 |
if (stream_id == 0xbd || (stream_id >= 0xc0 && stream_id <= 0xef)) |
| 997 |
this->packet->packet_table.len = 9; |
| 998 |
} |
| 999 |
} |
| 1000 |
else if (this->packet->packet_table.offset == 9) |
| 1001 |
{ |
| 1002 |
this->packet->packet_table.len += av_rb8(this->packet->packet_table.buf + 8); |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
// parse header table |
| 1007 |
bool has_pts = false; |
| 1008 |
|
| 1009 |
if (this->packet->packet_table.len >= 9) |
| 1010 |
{ |
| 1011 |
uint8_t flags = av_rb8(this->packet->packet_table.buf + 7); |
| 1012 |
|
| 1013 |
//this->packet->stream->frame_num++; |
| 1014 |
|
| 1015 |
switch (flags & 0xc0) |
| 1016 |
{ |
| 1017 |
case 0x80: // PTS only |
| 1018 |
{ |
| 1019 |
has_pts = true; |
| 1020 |
if (this->packet->packet_table.len >= 14) |
| 1021 |
{ |
| 1022 |
uint64_t pts = decode_pts(this->packet->packet_table.buf + 9); |
| 1023 |
this->packet->stream->p_dts = this->packet->stream->c_dts; |
| 1024 |
this->packet->stream->p_pts = this->packet->stream->c_pts; |
| 1025 |
this->packet->stream->c_dts = this->packet->stream->c_pts = pts; |
| 1026 |
} |
| 1027 |
else |
| 1028 |
{ |
| 1029 |
this->packet->stream->c_dts = this->packet->stream->c_pts = PTS_UNSET; |
| 1030 |
} |
| 1031 |
} |
| 1032 |
break; |
| 1033 |
case 0xc0: // PTS,DTS |
| 1034 |
{ |
| 1035 |
has_pts = true; |
| 1036 |
if (this->packet->packet_table.len >= 19 ) |
| 1037 |
{ |
| 1038 |
uint64_t pts = decode_pts(this->packet->packet_table.buf + 9); |
| 1039 |
uint64_t dts = decode_pts(this->packet->packet_table.buf + 14); |
| 1040 |
int64_t d = (pts - dts) & PTS_MASK; |
| 1041 |
// more than two seconds of PTS/DTS delta, probably corrupt |
| 1042 |
if(d > 180000) |
| 1043 |
{ |
| 1044 |
this->packet->stream->c_dts = this->packet->stream->c_pts = PTS_UNSET; |
| 1045 |
} |
| 1046 |
else |
| 1047 |
{ |
| 1048 |
this->packet->stream->p_dts = this->packet->stream->c_dts; |
| 1049 |
this->packet->stream->p_pts = this->packet->stream->c_pts; |
| 1050 |
this->packet->stream->c_dts = dts; |
| 1051 |
this->packet->stream->c_pts = pts; |
| 1052 |
} |
| 1053 |
} |
| 1054 |
else |
| 1055 |
{ |
| 1056 |
this->packet->stream->c_dts = this->packet->stream->c_pts = PTS_UNSET; |
| 1057 |
} |
| 1058 |
} |
| 1059 |
break; |
| 1060 |
} |
| 1061 |
this->packet->packet_table.Reset(); |
| 1062 |
} |
| 1063 |
|
| 1064 |
if (this->packet->streaming) |
| 1065 |
{ |
| 1066 |
const unsigned char* data = this->payload + pos; |
| 1067 |
size_t len = this->payload_len - pos; |
| 1068 |
this->packet->stream->Append(data, len, has_pts); |
| 1069 |
} |
| 1070 |
|
| 1071 |
return AVCONTEXT_CONTINUE; |
| 1072 |
} |