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