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
|
/*
* $Id$
*
* (C) 2006-2010 see AUTHORS
*
* This file is part of mplayerc.
*
* Mplayerc 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 3 of the License, or
* (at your option) any later version.
*
* Mplayerc 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "stdafx.h"
#include <streams.h>
#include <mpeg2data.h>
#include "../../DSUtil/GolombBuffer.h"
#include "Mpeg2SectionData.h"
#define BeginEnumDescriptors(gb, nType, nLength) \
{ \
BYTE DescBuffer[256]; \
int nLimit = gb.BitRead(12)+gb.GetPos(); \
while (gb.GetPos() < nLimit) \
{ \
MPEG2_DESCRIPTOR nType = (MPEG2_DESCRIPTOR)gb.BitRead(8); \
WORD nLength = gb.BitRead(8);
#define SkipDescriptor(gb, nType, nLength) \
gb.ReadBuffer(DescBuffer, nLength); \
TRACE ("Skipped descriptor : 0x%02x\n", nType);
#define EndEnumDescriptors }}
CMpeg2DataParser::CMpeg2DataParser(IBaseFilter* pFilter)
{
m_pData = pFilter;
memset(&m_Filter, 0, sizeof(m_Filter));
m_Filter.bVersionNumber = 1;
m_Filter.wFilterSize = MPEG2_FILTER_VERSION_1_SIZE;
m_Filter.fSpecifySectionNumber = TRUE;
}
CString CMpeg2DataParser::ConvertString (BYTE* pBuffer, int nLength)
{
static const UINT16 codepages[0x20] =
{
28591, // 00 - ISO 8859-1 Latin I
28595, // 01 - ISO 8859-5 Cyrillic
28596, // 02 - ISO 8859-6 Arabic
28597, // 03 - ISO 8859-7 Greek
28598, // 04 - ISO 8859-8 Hebrew
28599, // 05 - ISO 8859-9 Latin 5
28591, // 06 - ??? - ISO/IEC 8859-10 - Latin alphabet No. 6
28591, // 07 - ??? - ISO/IEC 8859-11 - Latin/Thai (draft only)
28591, // 08 - ??? - ISO/IEC 8859-12 - possibly reserved for Indian
28591, // 09 - ??? - ISO/IEC 8859-13 - Latin alphabet No. 7
28591, // 0a - ??? - ISO/IEC 8859-14 - Latin alphabet No. 8 (Celtic)
28605, // 0b - ISO 8859-15 Latin 9
28591, // 0c - réservé
28591, // 0d - réservé
28591, // 0e - réservé
28591, // 0f - réservé
// TODO !
28591, 28591, 28591, 28591, 28591, 28591, 28591, 28591,
28591, 28591, 28591, 28591, 28591, 28591, 28591, 28591
};
UINT cp = CP_ACP;
int nDestSize;
CString strResult;
if (nLength>0 && pBuffer[0]<0x20)
{
cp = codepages[pBuffer[0]];
pBuffer++;
nLength--;
}
nDestSize = MultiByteToWideChar (cp, MB_PRECOMPOSED, (LPCSTR)pBuffer, nLength, NULL, 0);
if(nDestSize < 0) return strResult;
MultiByteToWideChar (cp, MB_PRECOMPOSED, (LPCSTR)pBuffer, nLength, strResult.GetBuffer(nLength), nDestSize);
strResult.ReleaseBuffer();
return strResult;
}
DVB_STREAM_TYPE CMpeg2DataParser::ConvertToDVBType(PES_STREAM_TYPE nType)
{
switch (nType)
{
case VIDEO_STREAM_MPEG1 :
case VIDEO_STREAM_MPEG2 :
return DVB_MPV;
case AUDIO_STREAM_MPEG1 :
case AUDIO_STREAM_MPEG2 :
return DVB_MPA;
case VIDEO_STREAM_H264 :
return DVB_H264;
case AUDIO_STREAM_AC3 :
return DVB_AC3;
case AUDIO_STREAM_AC3_PLUS :
return DVB_EAC3;
case SUBTITLE_STREAM :
return DVB_SUBTITLE;
}
return DVB_UNKNOWN;
}
HRESULT CMpeg2DataParser::ParseSIHeader(CGolombBuffer& gb, DVB_SI SIType, WORD& wSectionLength, WORD& wTSID)
{
if (gb.BitRead(8) != SIType) return ERROR_INVALID_DATA; // table_id
gb.BitRead(1); // section_syntax_indicator
gb.BitRead(1); // reserved_future_use
gb.BitRead(2); // reserved
wSectionLength = gb.BitRead(12); // section_length
wTSID = gb.BitRead(16); // transport_stream_id
gb.BitRead(2); // reserved
gb.BitRead(5); // version_number
gb.BitRead(1); // current_next_indicator
gb.BitRead(8); // section_number
gb.BitRead(8); // last_section_number
return S_OK;
}
HRESULT CMpeg2DataParser::ParseSDT(ULONG ulFreq)
{
HRESULT hr;
CComPtr<ISectionList> pSectionList;
DWORD dwLength;
PSECTION data;
WORD wTSID;
WORD wONID;
WORD wSectionLength;
CheckNoLog (m_pData->GetSection (PID_SDT, SI_SDT, &m_Filter, 5000, &pSectionList));
CheckNoLog (pSectionList->GetSectionData (0, &dwLength, &data));
CGolombBuffer gb ((BYTE*)data, dwLength);
// service_description_section()
CheckNoLog (ParseSIHeader (gb, SI_SDT, wSectionLength, wTSID));
wONID = gb.BitRead(16); // original_network_id
gb.BitRead(8); // reserved_future_use
while (gb.GetSize() - gb.GetPos() > 4)
{
CDVBChannel Channel;
Channel.SetFrequency (ulFreq);
Channel.SetTSID (wTSID);
Channel.SetONID (wONID);
Channel.SetSID (gb.BitRead(16)); // service_id uimsbf
gb.BitRead(6); // reserved_future_use bslbf
gb.BitRead(1); // EIT_schedule_flag bslbf
gb.BitRead(1); // EIT_present_following_flag bslbf
gb.BitRead(3); // running_status uimsbf
gb.BitRead(1); // free_CA_mode bslbf
BeginEnumDescriptors(gb, nType, nLength)
{
switch (nType)
{
case DT_SERVICE :
gb.BitRead(8); // service_type
nLength = gb.BitRead(8); // service_provider_name_length
gb.ReadBuffer (DescBuffer, nLength); // service_provider_name
nLength = gb.BitRead(8); // service_name_length
gb.ReadBuffer (DescBuffer, nLength); // service_name
DescBuffer[nLength] = 0;
Channel.SetName (ConvertString (DescBuffer, nLength));
TRACE ("%15S %d\n", Channel.GetName(), Channel.GetSID());
break;
default :
SkipDescriptor (gb, nType, nLength); // descriptor()
break;
}
}
EndEnumDescriptors
if (!Channels.Lookup(Channel.GetSID()))
Channels [Channel.GetSID()] = Channel;
}
return S_OK;
}
HRESULT CMpeg2DataParser::ParsePAT()
{
HRESULT hr;
CComPtr<ISectionList> pSectionList;
DWORD dwLength;
PSECTION data;
WORD wTSID;
WORD wSectionLength;
CheckNoLog (m_pData->GetSection (PID_PAT, SI_PAT, &m_Filter, 5000, &pSectionList));
CheckNoLog (pSectionList->GetSectionData (0, &dwLength, &data));
CGolombBuffer gb ((BYTE*)data, dwLength);
// program_association_section()
CheckNoLog (ParseSIHeader (gb, SI_PAT, wSectionLength, wTSID));
while (gb.GetSize() - gb.GetPos() > 4)
{
WORD program_number;
WORD program_map_PID = 0;
program_number = gb.BitRead(16); // program_number
gb.BitRead(3); // reserved
if (program_number==0)
gb.BitRead(13); // network_PID
else
{
program_map_PID = gb.BitRead(13); // program_map_PID
if (Channels.Lookup(program_number))
{
Channels [program_number].SetPMT (program_map_PID);
ParsePMT (Channels [program_number]);
}
}
}
return S_OK;
}
HRESULT CMpeg2DataParser::ParsePMT(CDVBChannel& Channel)
{
HRESULT hr;
CComPtr<ISectionList> pSectionList;
DWORD dwLength;
PSECTION data;
WORD wTSID;
WORD wSectionLength;
int nLimit;
CheckNoLog (m_pData->GetSection (Channel.GetPMT(), SI_PMT, &m_Filter, 5000, &pSectionList));
CheckNoLog (pSectionList->GetSectionData (0, &dwLength, &data));
CGolombBuffer gb ((BYTE*)data, dwLength);
// TS_program_map_section()
CheckNoLog (ParseSIHeader (gb, SI_PMT, wSectionLength, wTSID));
gb.BitRead(3); // reserved
Channel.SetPCR (gb.BitRead(13)); // PCR_PID
gb.BitRead(4); // reserved
BeginEnumDescriptors(gb, nType, nLength) // for (i=0;i<N;i++) {
{
SkipDescriptor (gb, nType, nLength); // descriptor()
}
EndEnumDescriptors
while (gb.GetSize() - gb.GetPos() > 4)
{
PES_STREAM_TYPE pes_stream_type;
DVB_STREAM_TYPE dvb_stream_type;
WORD wPID;
CString strLanguage;
pes_stream_type = (PES_STREAM_TYPE)gb.BitRead(8); // stream_type
gb.BitRead(3); // reserved
wPID = gb.BitRead(13); // elementary_PID
gb.BitRead(4); // reserved
BeginEnumDescriptors(gb, nType, nLength) // ES_info_length
{
switch (nType)
{
case DT_ISO_639_LANGUAGE :
gb.ReadBuffer(DescBuffer, nLength);
strLanguage = ConvertString (DescBuffer, 3);
break;
case DT_AC3_AUDIO :
pes_stream_type = AUDIO_STREAM_AC3;
SkipDescriptor (gb, nType, nLength);
break;
case DT_EXTENDED_AC3_AUDIO :
pes_stream_type = AUDIO_STREAM_AC3_PLUS;
SkipDescriptor (gb, nType, nLength);
break;
case DT_SUBTITLING :
{
gb.ReadBuffer(DescBuffer, nLength);
strLanguage = ConvertString (DescBuffer, 3);
pes_stream_type = SUBTITLE_STREAM;
}
break;
default :
SkipDescriptor (gb, nType, nLength);
break;
}
}
EndEnumDescriptors
if ((dvb_stream_type = ConvertToDVBType(pes_stream_type)) != DVB_UNKNOWN)
Channel.AddStreamInfo (wPID, dvb_stream_type, pes_stream_type, strLanguage);
}
return S_OK;
}
HRESULT CMpeg2DataParser::ParseNIT()
{
HRESULT hr;
CComPtr<ISectionList> pSectionList;
DWORD dwLength;
PSECTION data;
WORD wTSID;
WORD wSectionLength;
WORD transport_stream_loop_length;
CheckNoLog (m_pData->GetSection (PID_NIT, SI_NIT, &m_Filter, 5000, &pSectionList));
CheckNoLog (pSectionList->GetSectionData (0, &dwLength, &data));
CGolombBuffer gb ((BYTE*)data, dwLength);
// network_information_section()
CheckNoLog (ParseSIHeader (gb, SI_NIT, wSectionLength, wTSID));
gb.BitRead(4); // reserved_future_use
BeginEnumDescriptors(gb, nType, nLength) // for (i=0;i<N;i++) {
{
SkipDescriptor (gb, nType, nLength); // descriptor()
}
EndEnumDescriptors
gb.BitRead(4); // reserved_future_use
transport_stream_loop_length = gb.BitRead(12); // network_descriptors_length
while (gb.GetSize() - gb.GetPos() > 4)
{
WORD transport_stream_id = gb.BitRead(16); // transport_stream_id
UNUSED_ALWAYS(transport_stream_id);
WORD original_network_id = gb.BitRead(16); // original_network_id
UNUSED_ALWAYS(original_network_id);
gb.BitRead(4); // reserved_future_use
BeginEnumDescriptors (gb, nType, nLength)
{
switch (nType)
{
case DT_LOGICAL_CHANNEL :
for (int i=0; i<nLength/4; i++)
{
WORD service_id = gb.BitRead (16);
gb.BitRead(6);
WORD logical_channel_number = gb.BitRead(10);
if (Channels.Lookup(service_id))
{
Channels[service_id].SetOriginNumber (logical_channel_number);
TRACE ("NIT association : %d -> %S\n", logical_channel_number, Channels[service_id].ToString());
}
}
break;
default :
SkipDescriptor (gb, nType, nLength);
break;
}
}
EndEnumDescriptors
}
return S_OK;
}
|