aboutsummaryrefslogtreecommitdiff
path: root/lib/dvb_si/capmt.cpp
blob: 3d5afed64542630cd4fc397917dab81ecd5e7119 (plain)
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
/*
 * $Id$
 *
 * (C) 2002-2003 Andreas Oberritter <obi@saftware.de>
 *
 * 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 of the License, 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; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <lib/dvb_si/descriptor_tag.h>
#include <lib/dvb_si/capmt.h>

CaLengthField::CaLengthField(const uint64_t length)
{
	if (length < 0x80) {
		sizeIndicator = 0;
		lengthValue = length;
	}

	else {
		uint64_t mask = 0xFF;

		sizeIndicator = 1;
		lengthFieldSize = 1;

		while ((length & mask) != length) {
			lengthFieldSize++;
			mask = ((uint64_t)(mask << 8)) | ((uint64_t)0xFFULL);
		}

		for (uint8_t i = lengthFieldSize; i > 0; i--)
			lengthValueByte.push_back((length >> ((i - 1) << 3)) & 0xFF);
	}
}

CaElementaryStreamInfo::CaElementaryStreamInfo(const ElementaryStreamInfo * const info, const uint8_t cmdId)
{
	streamType = info->streamType;
	reserved1 = info->reserved1;
	elementaryPid = info->elementaryPid;
	reserved2 = info->reserved2;
	esInfoLength = 0;

	for (DescriptorConstIterator i = info->getDescriptors()->begin(); i != info->getDescriptors()->end(); ++i)
		if ((*i)->getTag() == CA_DESCRIPTOR) {
			descriptors.push_back(new CaDescriptor(*(CaDescriptor *)*i));
			esInfoLength += (*i)->getLength() + 2;
		}

	if (esInfoLength) {
		caPmtCmdId = cmdId;
		esInfoLength++;
	}
}

CaElementaryStreamInfo::~CaElementaryStreamInfo(void)
{
	for (CaDescriptorIterator i = descriptors.begin(); i != descriptors.end(); ++i)
		delete *i;
}

uint16_t CaElementaryStreamInfo::getLength(void) const
{
	return esInfoLength + 5;
}

CaProgramMapTable::CaProgramMapTable(const ProgramMapTable * const pmt, const uint8_t listManagement, const uint8_t cmdId)
{
	uint64_t length = 6;

	caPmtTag = 0x9F80C3;
	caPmtListManagement = listManagement;

	programNumber = pmt->tableIdExtension;
	reserved1 = pmt->reserved3;
	versionNumber = pmt->versionNumber;
	currentNextIndicator = pmt->currentNextIndicator;
	reserved2 = pmt->reserved5;
	programInfoLength = 0;

	for (DescriptorConstIterator i = pmt->getDescriptors()->begin(); i != pmt->getDescriptors()->end(); ++i)
		if ((*i)->getTag() == CA_DESCRIPTOR) {
			descriptors.push_back(new CaDescriptor(*(CaDescriptor *)*i));
			programInfoLength += (*i)->getLength() + 2;
		}

	if (programInfoLength) {
		caPmtCmdId = cmdId;
		programInfoLength++;
		length += programInfoLength;
	}

	for (ElementaryStreamInfoConstIterator i = pmt->esInfo.begin(); i != pmt->esInfo.end(); ++i) {
		CaElementaryStreamInfo *info = new CaElementaryStreamInfo(*i, cmdId);
		esInfo.push_back(info);
		length += info->getLength();
	}

	lengthField = new CaLengthField(length);
}

CaProgramMapTable::~CaProgramMapTable(void)
{
	for (CaDescriptorIterator i = descriptors.begin(); i != descriptors.end(); ++i)
		delete *i;

	for (CaElementaryStreamInfoIterator i = esInfo.begin(); i != esInfo.end(); ++i)
		delete *i;

	delete lengthField;
}