Debian Codegraph @

src/lib/tsreader/Section.cpp

raw
1 /*
2 * Copyright (C) 2006-2010 Team MediaPortal
3 * http://www.team-mediaportal.com
4 *
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 *
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with GNU Make; see the file COPYING. If not, write to
17 * the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
18 * MA 02110-1335 USA
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 */
22 #include "os-dependent.h"
23 #include "Section.h"
24
25 #include <cstring>
26
27 namespace MPTV
28 {
29 CSection::CSection(void)
30 {
31 Reset();
32 }
33
34 CSection::~CSection(void)
35 {
36 }
37
38 void CSection::Reset()
39 {
40 table_id = -1;
41 table_id_extension = -1;
42 section_length = -1;
43 section_number = -1;
44 version_number = -1;
45 section_syntax_indicator = -1;
46 BufferPos = 0;
47 }
48
49 CSection& CSection::operator = (const CSection &section)
50 {
51 if (&section == this)
52 {
53 return *this;
54 }
55 Copy(section);
56 return *this;
57 }
58
59
60 void CSection::Copy(const CSection &section)
61 {
62 table_id = section.table_id;
63 table_id_extension = section.table_id_extension;
64 section_length = section.section_length;
65 section_number = section.section_number;
66 version_number = section.version_number;
67 section_syntax_indicator = section.section_syntax_indicator;
68 memcpy(Data, section.Data, sizeof(Data));
69 BufferPos = 0;
70 }
71
72 int CSection::CalcSectionLength(byte* tsPacket, int start)
73 {
74 if (start >= 188)
75 return 0;
76
77 if (BufferPos < 3)
78 {
79 byte bHi = 0;
80 byte bLow = 0;
81 if (BufferPos == 1)
82 {
83 bHi = tsPacket[start];
84 bLow = tsPacket[start + 1];
85 }
86 else if (BufferPos == 2)
87 {
88 bHi = Data[1];
89 bLow = tsPacket[start];
90 }
91 section_length = (int)(((bHi & 0xF) << 8) + bLow);
92 }
93 else
94 section_length = (int)(((Data[1] & 0xF) << 8) + Data[2]);
95 return section_length;
96 }
97
98 bool CSection::DecodeHeader()
99 {
100 if (BufferPos < 8)
101 return false;
102
103 table_id = Data[0];
104 section_syntax_indicator = ((Data[1] >> 7) & 1);
105
106 if (section_length == -1)
107 section_length = (((Data[1] & 0xF) << 8) + Data[2]);
108
109 table_id_extension = ((Data[3] << 8) + Data[4]);
110 version_number = ((Data[5] >> 1) & 0x1F);
111 section_number = Data[6];
112 section_syntax_indicator = ((Data[1] >> 7) & 1);
113
114 return true;
115 }
116
117 bool CSection::SectionComplete()
118 {
119 if (!DecodeHeader() && BufferPos - 3 > section_length && section_length > 0)
120 return true;
121 if (!DecodeHeader())
122 return false;
123 return (BufferPos - 3 >= section_length);
124 }
125 }