Debian Codegraph @

src/lib/tsreader/PmtParser.cpp

raw
1 /*
2 * Copyright (C) 2006 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
23 #include "os-dependent.h"
24 #include <kodi/General.h> //for kodi::Log
25 #include "PmtParser.h"
26 #include "ChannelInfo.h"
27 #include <cassert>
28
29 namespace MPTV
30 {
31 CPmtParser::CPmtParser()
32 {
33 m_pmtCallback = NULL;
34 m_isFound = false;
35 }
36
37 CPmtParser::~CPmtParser(void)
38 {
39 }
40
41 void CPmtParser::SetPmtCallBack(IPmtCallBack* callback)
42 {
43 m_pmtCallback = callback;
44 }
45
46 bool CPmtParser::IsReady()
47 {
48 return m_isFound;
49 }
50
51 void CPmtParser::OnNewSection(CSection& section)
52 {
53 if (section.table_id != 2)
54 {
55 return;
56 }
57
58 try
59 {
60 bool lpcm_audio_found = false;
61 int program_number = section.table_id_extension;
62 int pcr_pid = ((section.Data[8] & 0x1F) << 8) + section.Data[9];
63 int program_info_length = ((section.Data[10] & 0xF) << 8) + section.Data[11];
64 int len2 = program_info_length;
65 int pointer = 12;
66 int len1 = section.section_length - (9 + program_info_length + 4);
67 int x;
68
69 if (!m_isFound)
70 {
71 kodi::Log(ADDON_LOG_DEBUG, "got pmt:%x service id:%x", GetPid(), program_number);
72 m_isFound = true;
73 if (m_pmtCallback != NULL)
74 {
75 m_pmtCallback->OnPmtReceived(GetPid());
76 }
77 }
78
79 // loop 1
80 while (len2 > 0)
81 {
82 //int indicator=section.Data[pointer];
83 int descriptorLen = section.Data[pointer + 1];
84 len2 -= (descriptorLen + 2);
85 pointer += (descriptorLen + 2);
86 }
87 // loop 2
88 int stream_type = 0;
89 short elementary_PID = 0;
90 short ES_info_length = 0;
91 std::vector<TempPid> tempPids;
92
93 m_pidInfo.Reset();
94 m_pidInfo.PmtPid = GetPid();
95 m_pidInfo.ServiceId = program_number;
96
97 while (len1 > 0)
98 {
99 //if (start+pointer+4>=sectionLen+9) return ;
100 stream_type = section.Data[pointer];
101 elementary_PID = ((section.Data[pointer + 1] & 0x1F) << 8) + section.Data[pointer + 2];
102 ES_info_length = ((section.Data[pointer + 3] & 0xF) << 8) + section.Data[pointer + 4];
103 kodi::Log(ADDON_LOG_DEBUG, "pmt: pid:%x type:%x", elementary_PID, stream_type);
104
105 if (stream_type == SERVICE_TYPE_VIDEO_MPEG1
106 || stream_type == SERVICE_TYPE_VIDEO_MPEG2
107 || stream_type == SERVICE_TYPE_VIDEO_MPEG4
108 || stream_type == SERVICE_TYPE_VIDEO_H264)
109 {
110 VideoPid pid;
111 pid.Pid = elementary_PID;
112 pid.VideoServiceType = stream_type;
113 m_pidInfo.videoPids.push_back(pid);
114 }
115 if (stream_type == SERVICE_TYPE_AUDIO_MPEG1 ||
116 stream_type == SERVICE_TYPE_AUDIO_MPEG2 ||
117 stream_type == SERVICE_TYPE_AUDIO_AC3 ||
118 stream_type == SERVICE_TYPE_AUDIO_AAC ||
119 stream_type == SERVICE_TYPE_AUDIO_LATM_AAC ||
120 stream_type == SERVICE_TYPE_AUDIO_DD_PLUS)
121 {
122 AudioPid pid;
123 pid.Pid = elementary_PID;
124 pid.AudioServiceType = (short)stream_type;
125 m_pidInfo.audioPids.push_back(pid);
126 }
127 m_pidInfo.PcrPid = pcr_pid;
128
129 pointer += 5;
130 len1 -= 5;
131 len2 = ES_info_length;
132
133 while (len2 > 0)
134 {
135 if (pointer + 1 >= section.section_length)
136 {
137 kodi::Log(ADDON_LOG_DEBUG, "pmt parser check1");
138 return;
139 }
140
141 int indicator = section.Data[pointer];
142 x = section.Data[pointer + 1] + 2;
143
144 if (indicator == DESCRIPTOR_DVB_AC3 || indicator == DESCRIPTOR_DVB_E_AC3)
145 {
146 AudioPid pid;
147 pid.Pid = elementary_PID;
148 pid.AudioServiceType = (indicator == DESCRIPTOR_DVB_AC3) ? SERVICE_TYPE_AUDIO_AC3 : SERVICE_TYPE_AUDIO_DD_PLUS;
149
150 for (size_t i(0); i < tempPids.size(); i++)
151 {
152 if (tempPids[i].Pid == elementary_PID)
153 {
154 pid.Lang[0] = tempPids[i].Lang[0];
155 pid.Lang[1] = tempPids[i].Lang[1];
156 pid.Lang[2] = tempPids[i].Lang[2];
157 pid.Lang[3] = tempPids[i].Lang[3]; // should be null if no extra data is available
158 pid.Lang[4] = tempPids[i].Lang[4];
159 pid.Lang[5] = tempPids[i].Lang[5];
160 tempPids.pop_back();
161 break;
162 }
163 }
164
165 m_pidInfo.audioPids.push_back(pid);
166 }
167
168 // audio and subtitle languages
169 if (indicator == DESCRIPTOR_MPEG_ISO639_Lang)
170 {
171 if (pointer + 4 >= section.section_length)
172 {
173 kodi::Log(ADDON_LOG_DEBUG, "pmt parser check2");
174 return;
175 }
176
177 bool pidFound(false);
178
179 // Find corresponding audio stream by PID, if not found
180 // the stream type should be unknown to us
181 for (unsigned int i(0); i < m_pidInfo.audioPids.size(); i++)
182 {
183 if (m_pidInfo.audioPids[i].Pid == elementary_PID)
184 {
185 int descriptorLen = section.Data[pointer + 1];
186
187 m_pidInfo.audioPids[i].Lang[0] = section.Data[pointer + 2];
188 m_pidInfo.audioPids[i].Lang[1] = section.Data[pointer + 3];
189 m_pidInfo.audioPids[i].Lang[2] = section.Data[pointer + 4];
190 m_pidInfo.audioPids[i].Lang[3] = 0;
191
192 // Get the additional language descriptor data (NORSWE etc.)
193 if (descriptorLen == 8)
194 {
195 m_pidInfo.audioPids[i].Lang[3] = section.Data[pointer + 6];
196 m_pidInfo.audioPids[i].Lang[4] = section.Data[pointer + 7];
197 m_pidInfo.audioPids[i].Lang[5] = section.Data[pointer + 8];
198 m_pidInfo.audioPids[i].Lang[6] = 0;
199 }
200
201 pidFound = true;
202 }
203 // Find corresponding subtitle stream by PID, if not found
204 // the stream type is be unknown to us
205 for (unsigned int j(0); j < m_pidInfo.subtitlePids.size(); j++)
206 {
207 if (m_pidInfo.subtitlePids[j].Pid == elementary_PID)
208 {
209 m_pidInfo.subtitlePids[j].Lang[0] = section.Data[pointer + 2];
210 m_pidInfo.subtitlePids[j].Lang[1] = section.Data[pointer + 3];
211 m_pidInfo.subtitlePids[j].Lang[2] = section.Data[pointer + 4];
212 m_pidInfo.subtitlePids[j].Lang[3] = 0;
213 pidFound = true;
214 }
215 }
216
217 if (!pidFound)
218 {
219 int descriptorLen = section.Data[pointer + 1];
220 TempPid pid;
221 pid.Pid = elementary_PID;
222 pid.Lang[0] = section.Data[pointer + 2];
223 pid.Lang[1] = section.Data[pointer + 3];
224 pid.Lang[2] = section.Data[pointer + 4];
225 // Get the additional language descriptor data (NORSWE etc.)
226 if (descriptorLen == 8)
227 {
228 pid.Lang[3] = section.Data[pointer + 6];
229 pid.Lang[4] = section.Data[pointer + 7];
230 pid.Lang[5] = section.Data[pointer + 8];
231 }
232 else
233 {
234 pid.Lang[3] = 0;
235 pid.Lang[4] = 0;
236 pid.Lang[5] = 0;
237 }
238
239 tempPids.push_back(pid);
240 }
241 }
242 }
243 if (indicator == DESCRIPTOR_VBI_TELETEXT)
244 {
245 kodi::Log(ADDON_LOG_DEBUG, "VBI teletext descriptor");
246 }
247 if (indicator == DESCRIPTOR_DVB_TELETEXT /*&& m_pidInfo.TeletextPid==0*/)
248 {
249 m_pidInfo.TeletextPid = elementary_PID;
250 assert(section.Data[pointer + 0] == DESCRIPTOR_DVB_TELETEXT);
251 int descriptorLen = section.Data[pointer + 1];
252
253 int varBytes = 5; // 4 additional fields for a total of 32 bits (see 6.2.40)
254
255 assert(descriptorLen % varBytes == 0); // there shouldnt be any left over bytes :)
256
257 int N = descriptorLen / varBytes;
258
259 //BYTE b = 0x02 << 3;
260
261 kodi::Log(ADDON_LOG_DEBUG, "Descriptor length %i, N= %i", descriptorLen, N);
262 for (int j = 0; j < N; j++)
263 {
264 //BYTE ISO_639_language_code[3];
265 //ISO_639_language_code[0] = section.Data[pointer + varBytes*j + 2];
266 //ISO_639_language_code[1] = section.Data[pointer + varBytes*j + 3];
267 //ISO_639_language_code[2] = section.Data[pointer + varBytes*j + 4];
268
269 byte b3 = section.Data[pointer + varBytes*j + 5];
270 byte teletext_type = (b3 & 0xF8) >> 3; // 5 first(msb) bits
271
272 assert(teletext_type <= 0x05); // 0x06 and upwards reserved for future use and shouldnt appear
273 //for (int i = 0; i < 8; i++){
274 // if( ((b3 << i) & 128) != 0) LogDebug("1");
275 // else LogDebug("0");
276 //}
277
278 int teletext_magazine_number = (b3 & 0x07); // last(lsb) 3 bits
279
280 int teletext_page_number = (section.Data[pointer + varBytes*j + 6]);
281
282 int real_page_tens = (teletext_page_number & 0xF0) >> 4;
283 int real_page_units = teletext_page_number & 0x0F;
284
285 int real_page = teletext_magazine_number * 100 + real_page_tens * 10 + real_page_units;
286
287 //kodi::Log(ADDON_LOG_DEBUG, "Mag: %i, tens %i, units %i, total ?= %i", teletext_magazine_number,real_page_tens,real_page_units,real_page);
288
289 //if its a teletext subtitle service (standard / hard of hearing respectively)
290 if (teletext_type == 0x02 || teletext_type == 0x05)
291 {
292 if (!m_pidInfo.HasTeletextPageInfo(real_page))
293 {
294 kodi::Log(ADDON_LOG_DEBUG, "TODO: Teletext subtitles in PMT: PID %i, mag %i, page %i", elementary_PID, teletext_magazine_number, real_page);
295 //kodi::Log(ADDON_LOG_DEBUG, "Teletext subtitles in PMT: PID %i, mag %i, page %i, prevPage %i", elementary_PID, teletext_magazine_number, real_page, m_pidInfo.TeletextSubPage);
296 //TeletextServiceInfo info;
297 //info.page = real_page;
298 //info.type = teletext_type;
299 //info.lang[0] = ISO_639_language_code[0];
300 //info.lang[1] = ISO_639_language_code[1];
301 //info.lang[2] = ISO_639_language_code[2];
302 //m_pidInfo.TeletextInfo.push_back(info);
303 }
304 }
305 else
306 {
307 kodi::Log(ADDON_LOG_DEBUG, "Teletext SI: Page %i Type %X", real_page, teletext_type);
308 }
309 }
310 }
311 if (indicator == DESCRIPTOR_DVB_SUBTITLING)
312 {
313 if (stream_type == SERVICE_TYPE_DVB_SUBTITLES2)
314 {
315 SubtitlePid pid;
316 pid.Pid = elementary_PID;
317 pid.Lang[0] = section.Data[pointer + 2];
318 pid.Lang[1] = section.Data[pointer + 3];
319 pid.Lang[2] = section.Data[pointer + 4];
320 pid.Lang[3] = 0;
321 pid.SubtitleServiceType = SERVICE_TYPE_DVB_SUBTITLES2;
322 m_pidInfo.subtitlePids.push_back(pid);
323 }
324 }
325 if (indicator == DESCRIPTOR_REGISTRATION)
326 {
327 if (section.Data[pointer + 2] == 'H' &&
328 section.Data[pointer + 3] == 'D' &&
329 section.Data[pointer + 4] == 'M' &&
330 section.Data[pointer + 5] == 'V' &&
331 stream_type == SERVICE_TYPE_DCII_OR_LPCM)
332 {
333 AudioPid pid;
334 pid.Pid = elementary_PID;
335 pid.AudioServiceType = (short)stream_type;
336 m_pidInfo.audioPids.push_back(pid);
337 lpcm_audio_found = true;
338 }
339 }
340 len2 -= x;
341 len1 -= x;
342 pointer += x;
343 }
344 if (stream_type == SERVICE_TYPE_DCII_OR_LPCM && !lpcm_audio_found)
345 {
346 VideoPid pid;
347 pid.Pid = elementary_PID;
348 pid.VideoServiceType = SERVICE_TYPE_VIDEO_MPEG2;
349 m_pidInfo.videoPids.push_back(pid);
350 }
351 }
352 if (m_pmtCallback != NULL)
353 {
354 kodi::Log(ADDON_LOG_DEBUG, "DecodePMT pid:0x%x pcrpid:0x%x sid:%x", m_pidInfo.PmtPid, m_pidInfo.PcrPid, m_pidInfo.ServiceId);
355 m_pmtCallback->OnPidsReceived(m_pidInfo);
356 }
357 }
358 catch (...)
359 {
360 kodi::Log(ADDON_LOG_DEBUG, "Exception in PmtParser");
361 }
362 }
363
364
365 CPidTable& CPmtParser::GetPidInfo()
366 {
367 return m_pidInfo;
368 }
369 }