OSDN Git Service

implement SitPacket class in Utils
[tsremuxcpp/developing01.git] / src / Utils.cc
1 #include "Utils.h"
2
3 namespace TsRemux
4 {
5     const int Constants::TS_PAYLOAD_SIZE = 184;
6     const int Constants::TS_SIZE = 188;
7     const int Constants::STRIDE_SIZE = 192;
8     const int Constants::DISK_BUFFER = 0x8D000 << 5;
9     const byte Constants::SYNC_BYTE = 0x47;
10     const byte Constants::PAT_PID = 0x00;
11     const byte Constants::SIT_PID = 0x1f;
12     const byte Constants::PAT_TABLE_ID = 0x00;
13     const byte Constants::PMT_TABLE_ID = 0x02;
14     const byte Constants::DTCP_DESCRIPTOR_TAG = 0x88;
15     const byte Constants::PACK_ID = 0xba;
16     const byte Constants::SYS_ID = 0xbb;
17     const byte Constants::MAP_ID = 0xbc;
18     const byte Constants::DIR_ID = 0xff;
19     const byte Constants::PAD_ID = 0xbe;
20
21     // defaults
22     const ushort Constants::DEFAULT_PMT_PID = 0x0100;
23     const ushort Constants::DEFAULT_VIDEO_PID = 0x1011;
24     const ushort Constants::MAX_VIDEO_PID = 0x1019;
25     const ushort Constants::DEFAULT_AUDIO_PID = 0x1100;
26     const ushort Constants::MAX_AUDIO_PID = 0x111f;
27     const ushort Constants::DEFAULT_PCR_PID = 0x1001;
28     const ushort Constants::DEFAULT_SUBTITLE_PID = 0x1800;
29     const ushort Constants::DEFAULT_PRESENTATION_GRAPHICS_PID = 0x1200;
30     const ushort Constants::DEFAULT_INTERACTIVE_GRAPHICS_PID = 0x1400;
31     const ushort Constants::DEFAULT_PROGRAM_NUMBER = 0x01;
32     const int Constants::MAX_BUFFER_COUNT = 0xff;
33     const int Constants::MIN_BUFFER_COUNT = 0x02;
34     const Int64 Constants::AUDIO_DELAY = 30000;
35     const UInt32 Constants::MKVCLUSTER_START = 0x1f43b675;
36     const UInt32 Constants::MKVFILE_START = 0x1a45dfa3;
37     const UInt32 Constants::MKVSEGMENT_START = 0x18538067;
38     const UInt32 Constants::MKVTRACKINFO_START = 0x1654AE6B;
39
40     // stream types
41     const byte Constants::PES_VIDEO = 0xe0;
42     const byte Constants::PES_AUDIO_MPEG = 0xc0;
43     const byte Constants::PES_PRIVATE1 = 0xbd;
44     const byte Constants::PES_PADDING = 0xbe;
45     const byte Constants::PES_PRIVATE2 = 0xbf;
46     const byte Constants::PES_VIDEO_VC1 = 0xfd;
47     const byte Constants::PES_PRIVATE_AC3 = 0x80;
48     const byte Constants::PES_PRIVATE_AC3_PLUS = 0xc0;
49     const byte Constants::PES_PRIVATE_DTS_HD = 0x88;
50     const byte Constants::PES_PRIVATE_LPCM = 0xa0;
51     const byte Constants::PES_PRIVATE_AC3_TRUE_HD = 0xb0;
52     const UInt32 Constants::VC1_SEQ_SC = 0x0000010f;
53     const UInt32 Constants::VC1_END_OF_STREAM = 0x0000010a;
54     const ushort Constants::AC3_SYNC = 0x0b77;
55     const UInt32 Constants::H264_PREFIX = 0x00000107;
56     const UInt32 Constants::H264_END_OF_STREAM = 0x0000010b;
57     const UInt32 Constants::DTS_SYNC = 0x7ffe8001;
58     const UInt32 Constants::DTS_EXT_SYNC = 0x64582025;
59     const UInt32 Constants::MLP_SYNC = 0xF8726FBA;
60     const UInt32 Constants::MPEG2_SEQ_CODE = 0x000001b3;
61     const UInt32 Constants::MPEG2_SEQ_EXT = 0x000001b5;
62     const UInt32 Constants::MPEG2_SEQ_END = 0x000001b7;
63
64     // clocks
65     const Int64 Constants::MPEG2TS_CLOCK_RATE = 27000000LL;
66     const Int64 Constants::MAX_MPEG2TS_CLOCK = 0x25800000000LL;
67     const Int64 Constants::MAX_BLURAY_CLOCK = 0x40000000LL;
68     const Int64 Constants::MAX_FIREWIRE_CLOCK = 24576000LL;
69     const Int64 Constants::MAX_PTS_CLOCK = 0x200000000LL;
70     const Int64 Constants::PTS_CLOCK_RATE = 90000LL;
71     const int Constants::MAX_OFFSET = 3072;
72     const int Constants::MAX_COUNT = 8000;
73
74    // descriptors
75     readonly byte Constants::hdmv_registration_descriptor[]
76         = { 0x05, 0x04, 0x48, 0x44, 0x4d, 0x56 };
77     readonly byte Constants::copy_control_descriptor[]
78         = { 0x88, 0x04, 0x0f, 0xff, 0x84, 0xfc };
79     readonly byte Constants::vc1_descriptor[]
80         = { 0x05, 0x05, 0x56, 0x43, 0x2d, 0x31, 0xff };
81     readonly byte Constants::ac3_registration_descriptor[] 
82          = { 0x05, 0x04, 0x41, 0x43, 0x2d, 0x33 };
83     readonly byte Constants::DefaultSitTableOne[] = {
84             0x47, 0x40, 0x1f, 0x10, 0x00, 0x7f, 0xf0, 0x19,
85             0xff, 0xff, 0xc1, 0x00, 0x00, 0xf0, 0x0a, 0x63,
86             0x08, 0xc1, 0x5a, 0xae, 0xff, 0xff, 0xff, 0xff,
87             0xff, 0x00, 0x01, 0x80, 0x00, 0x34, 0x1e, 0xe7,
88             0x4e, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
89             0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
90             0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
91             0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
92             0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
93             0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
94             0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
95             0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
96             0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
97             0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
98             0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
99             0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
100             0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
101             0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
102             0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
103             0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
104             0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
105             0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
106             0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
107             0xff, 0xff, 0xff, 0xff };   
108     readonly uint Constants::crc_table[] = {
109         0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b,
110         0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
111         0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd, 0x4c11db70, 0x48d0c6c7,
112         0x4593e01e, 0x4152fda9, 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75,
113         0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3,
114         0x709f7b7a, 0x745e66cd, 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039,
115         0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5, 0xbe2b5b58, 0xbaea46ef,
116         0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d,
117         0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49, 0xc7361b4c, 0xc3f706fb,
118         0xceb42022, 0xca753d95, 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1,
119         0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d, 0x34867077, 0x30476dc0,
120         0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072,
121         0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 0x018aeb13, 0x054bf6a4,
122         0x0808d07d, 0x0cc9cdca, 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde,
123         0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02, 0x5e9f46bf, 0x5a5e5b08,
124         0x571d7dd1, 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
125         0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b, 0xbb60adfc,
126         0xb6238b25, 0xb2e29692, 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6,
127         0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a, 0xe0b41de7, 0xe4750050,
128         0xe9362689, 0xedf73b3e, 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2,
129         0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34,
130         0xdc3abded, 0xd8fba05a, 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637,
131         0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb, 0x4f040d56, 0x4bc510e1,
132         0x46863638, 0x42472b8f, 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53,
133         0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5,
134         0x3f9b762c, 0x3b5a6b9b, 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff,
135         0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623, 0xf12f560e, 0xf5ee4bb9,
136         0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b,
137         0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd,
138         0xcda1f604, 0xc960ebb3, 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7,
139         0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b, 0x9b3660c6, 0x9ff77d71,
140         0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3,
141         0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 0x4e8ee645, 0x4a4ffbf2,
142         0x470cdd2b, 0x43cdc09c, 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8,
143         0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24, 0x119b4be9, 0x155a565e,
144         0x18197087, 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
145         0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d, 0x2056cd3a,
146         0x2d15ebe3, 0x29d4f654, 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0,
147         0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c, 0xe3a1cbc1, 0xe760d676,
148         0xea23f0af, 0xeee2ed18, 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4,
149         0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662,
150         0x933eb0bb, 0x97ffad0c, 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668,
151         0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4 };
152
153 pByte Utility::ToArray(const std::vector<byte> vctr)
154 {
155     pByte array = pByte(new byte[vctr.size()]);
156     for(int i = 0; i < vctr.size(); i++)
157     {
158         array[i] = vctr.at(i);
159     } 
160     return array;
161 }
162
163 const int DATA_HEADER = 5;
164 StreamInfo::StreamInfo(pByte data, int index)throw(std::invalid_argument)
165 {
166     if(!data)
167         throw std::invalid_argument("stream data is NULL");
168
169     if(sizeof(data.get()) + index < DATA_HEADER)
170         throw std::invalid_argument("stream data too short");
171
172     uint descLength = (data[3 + index] & 0x0f) << 8 + data[4 + index];
173
174     if(descLength > Constants::TS_SIZE)
175         throw std::invalid_argument("descriptors data too long");
176
177     if(5 + descLength > sizeof(data.get()) - index)
178     {
179         throw std::invalid_argument("stream data too short");
180     }
181
182     mData = pByte(new byte[DATA_HEADER + descLength]);
183     for(int i = 0; i < sizeof(mData.get()); i++)
184     {
185         mData[i] = data[i + index];
186     }
187
188     SetVideoFormat(VF_Reserved);
189     SetAspectRatio(AR_Reserved);
190     SetFrameRate(FR_Reserved);
191     SetAudioPresentationType(AP_Reserved);
192     SetSamplingFrequency(SF_Reserved);
193 }
194
195 VideoFormat StreamInfo::GetVideoFormat(void)
196 {
197     return mVideoFormat;
198 }
199
200 void StreamInfo::SetVideoFormat(VideoFormat video_format)
201 {
202     mVideoFormat = video_format;
203 }
204
205 AspectRatio StreamInfo::GetAspectRatio(void)
206 {
207     return mAspectRatio;
208 }
209
210 void StreamInfo::SetAspectRatio(AspectRatio aspect_ratio)
211 {
212     mAspectRatio = aspect_ratio;
213 }
214
215 FrameRate StreamInfo::GetFrameRate(void)
216 {
217     return mFrameRate;
218 }
219
220 void StreamInfo::SetFrameRate(FrameRate frame_rate)
221 {
222     mFrameRate = frame_rate;
223 }
224
225 AudioPresentationType StreamInfo::GetAudioPresentationType(void)
226 {
227     return mAudioPresentationType;
228 }
229
230 void StreamInfo::SetAudioPresentationType(AudioPresentationType ap_type)
231 {
232     mAudioPresentationType = ap_type;
233 }
234
235 SamplingFrequency StreamInfo::GetSamplingFrequency(void)
236 {
237     return mSamplingFrequency;
238 }
239
240 void StreamInfo::SetSamplingFrequency(SamplingFrequency sampling_frequency)
241 {
242     mSamplingFrequency = sampling_frequency;
243 }
244
245 StreamInfo::StreamInfo(ElementaryStreamTypes streamType, ushort elementaryPid)
246 {
247     mData = pByte(new byte[DATA_HEADER]);
248     SetElementaryStreamTypes(streamType);
249     SetElementaryPID(elementaryPid);
250     // reserved and descriptors length
251     mData[3] = 0xf0;
252     mData[4] = 0x00;
253 }
254
255 pByte StreamInfo::GetByteData(void)
256 {
257     return mData;
258 }
259
260 ElementaryStreamTypes StreamInfo::GetElementaryStreamTypes(void)
261 {
262     return (ElementaryStreamTypes)mData[0];
263 }
264
265 void StreamInfo::SetElementaryStreamTypes(ElementaryStreamTypes stream_type)
266 {
267     mData[0] = (byte)stream_type;
268 }
269 ushort StreamInfo::GetElementaryPID(void)
270 {
271     return (ushort)(((mData[1] & 0x1f) << 8) + mData[2]);
272 }
273
274 void StreamInfo::SetElementaryPID(ushort pid)
275 {
276     mData[1] = (byte)(((pid >> 8) & 0x1f) | 0xe0);
277     mData[2] = (byte)(pid & 0xff);
278 }
279
280 pByte StreamInfo::GetElementaryDescriptors(void)
281 {
282     pByte descriptors;
283     if(sizeof(mData.get()) == DATA_HEADER)
284     {
285         descriptors.reset();
286     }
287     else
288     {
289         descriptors = pByte(new byte[sizeof(mData.get()) - DATA_HEADER]);
290         for (int i = 0; i < (sizeof(mData.get()) - DATA_HEADER); i++)
291         {
292             descriptors[i] = mData[i + DATA_HEADER];
293         }
294     }
295     return descriptors;
296 }
297
298 void StreamInfo::SetElementaryDescriptors(pByte value)
299     throw(std::invalid_argument)
300 {
301     if(!value || 0 == sizeof(value.get()))
302     {
303         if(sizeof(mData.get()) > DATA_HEADER)
304         {
305             // need to remove existing descriptors
306             pByte data = pByte(new byte[DATA_HEADER]);
307             data[0] = mData[0];
308             data[1] = mData[1];
309             data[2] = mData[2];
310             data[3] = 0xf0;
311             data[4] = 0x00;
312             mData = data;
313         }
314         else
315         {
316             // nothing to do
317             return;
318         }
319     }
320     else
321     {
322         if(sizeof(value.get()) > 180) {
323             throw std::invalid_argument("descriptors data too long");
324         }
325
326         pByte data = pByte(new byte[DATA_HEADER + sizeof(value.get())]);
327         data[0] = mData[0];
328         data[1] = mData[1];
329         data[2] = mData[2];
330         data[3] = (byte)(0xf0 | (byte)((sizeof(value.get()) >> 8) & 0x0f));
331         data[4] = (byte)(sizeof(value.get()) & 0xff);
332         for (int i = 0; i < sizeof(value.get()); i++)
333         {
334             data[DATA_HEADER + i] = value[i];
335         }
336         mData = data;
337     }
338 }
339
340 ProgramInfo::ProgramInfo(pByte data, int index)throw(std::invalid_argument)
341 {
342     if(!data)
343         throw std::invalid_argument("program data is null");
344
345     if(sizeof(data.get()) + index < 4)
346         throw std::invalid_argument("program data too short");
347
348     mData = pByte(new byte[4]);
349     for(int i = 0; i < sizeof(mData.get()); i++)
350     {
351         mData[i] = data[i + index];
352     }
353 }
354
355 ProgramInfo::ProgramInfo(ushort programNumber, ushort programPid)
356 {
357     mData = pByte(new byte[4]);
358     SetProgramNumber(programNumber);
359     SetProgramPID(programPid);
360 }
361
362 ushort ProgramInfo::GetProgramNumber(void)
363 {
364     return (ushort)((mData[0] << 8) + mData[1]);
365 }
366
367 void ProgramInfo::SetProgramNumber(ushort value)
368 {
369     mData[0] = (byte)((value >> 8) & 0xff);
370     mData[1] = (byte)(value & 0xff);
371 }
372
373 ushort ProgramInfo::GetProgramPID(void)
374 {
375     return (ushort)(((mData[2] & 0x1f) << 8) + mData[3]);
376 }
377
378 void ProgramInfo::SetProgramPID(ushort value)
379 {
380     mData[2] = (byte)(((value >> 8) & 0x1f) | 0xe0);
381     mData[3] = (byte)(value & 0xff);
382 }
383
384 pByte ProgramInfo::GetData(void)
385 {
386     return mData;
387 }
388
389 ElementaryParse::ElementaryParse(void)
390 {
391     indicator = 0;
392 }
393
394 byte ElementaryParse::GetNextBit(void)
395 {
396     byte ret = (byte)(((mData[indicator / 8]) >> (7 - (indicator % 8))) & 1);
397     indicator++;
398     return ret;
399 }
400
401 TsPacket::TsPacket(void)
402 {
403     // initialize the packet as a null packet
404     mData = pByte(new byte[Constants::TS_SIZE]);
405     mData[0] = Constants::SYNC_BYTE; // sync byte
406     mData[1] = 0x1f; // PID = 0x1FFF
407     mData[2] = 0xff; // PID == 0x1FFF
408     mData[3] = 0x10; // no adaptation field
409     for (int i = 4; i < Constants::TS_SIZE; i++)
410         mData[i] = 0xff;
411 }
412
413 bool TsPacket::GetPriority(void)
414 {
415     if ((mData[1] & 0x20) > 0)
416         return true;
417     else
418         return false;
419 }
420
421 void TsPacket::SetPriority(bool priority)
422 {
423     if (priority)
424         mData[1] |= 0x20;
425     else
426         mData[1] &= 0xdf;
427 }
428
429 ushort TsPacket::GetPID(void)
430 {
431     return (ushort)(((mData[1] & 0x1f) << 8) + mData[2]);
432 }
433 void TsPacket::SetPID(ushort pid)
434 {
435     byte b = (byte)(mData[1] & 0xE0);
436     b += (byte)((pid >> 8) & 0x1f);
437     mData[1] = b;
438     mData[2] = (byte)(pid & 0xff);
439 }
440
441 byte TsPacket::GetPointerSize(void)
442 {
443     if ((mData[3] & 0x20) == 0) // No adaptation field present
444         return mData[4];
445     return (byte)(mData[4] + 1 + mData[mData[4] + DATA_HEADER]);
446 }
447
448 pByte TsPacket::GetData(void)
449 {
450     return mData;
451 }
452
453 void TsPacket::SetData(pByte data, int startIndex)throw(std::invalid_argument)
454 {
455     if (!data)
456         throw std::invalid_argument("NULL packet");
457     else if (Constants::TS_SIZE > sizeof(data.get()) - startIndex)
458         throw std::invalid_argument("small packet");
459     else if (Constants::SYNC_BYTE != data[0 + startIndex])
460         throw std::invalid_argument("sync byte missing");
461     for (int i = 0; i < Constants::TS_SIZE; i++) {
462         mData[i] = data[i + startIndex];
463     }
464 }
465 bool TsPacket::HasPcr(void)
466 {
467     if ((mData[3] & 0x20) > 0  // Adaptation field present
468      && (mData[4] > 0)         // length > 0
469      && (mData[DATA_HEADER] & 0x10) > 0) // and a PCR
470         return true;
471     return false;
472 }
473 Int64 TsPacket::GetPcr(void)throw(std::out_of_range)
474 {
475     if (false == this->HasPcr())
476         throw std::out_of_range("PCR not present in this packet");
477     Int64 mpeg2tsClock = mData[6];
478     mpeg2tsClock <<= 25;
479     mpeg2tsClock += (mData[7] << 17);
480     mpeg2tsClock += (mData[8] << 9);
481     mpeg2tsClock += (mData[9] << 1);
482     mpeg2tsClock += ((mData[10] & 0x80) >> 7);
483     mpeg2tsClock *= 300;
484     mpeg2tsClock += ((mData[10] & 0x1) << 8);
485     mpeg2tsClock += (mData[11]);
486     return mpeg2tsClock;
487 }
488
489 bool TsPacket::HasPesHeader(void)
490 {
491     if ((mData[1] & 0x40) == 0)
492         return false;
493     int offset = 4;
494     if ((mData[3] & 0x20) > 0)
495     {
496         // adaptation field present
497         offset += (1 + mData[4]);
498         if (offset >= Constants::TS_SIZE)
499             return false;
500     }
501     if ((mData[3] & 0x10) > 0)
502     {
503         // payload present
504         int len = Constants::TS_SIZE - offset;
505         if (len < 10)
506             return false;
507
508         if(mData[offset] == 0
509         && mData[offset + 1] == 0
510         && mData[offset + 2] == 1
511         && len >= 9 + mData[offset + 8])
512             return true;
513         else if(mData[offset] == 0
514              && mData[offset + 1] == 0
515              && mData[offset + 2] == 1)
516             return false;
517     }
518     return false;
519 }
520
521 pByte TsPacket::Payload(void)
522 {
523     pByte ret;
524     ret.reset();
525     int offset = 4;
526     if ((mData[3] & 0x20) > 0)
527     {
528         // adaptation field present
529         offset += (1 + mData[4]);
530         if (offset >= Constants::TS_SIZE)
531             return ret;
532     }
533     if ((mData[3] & 0x10) > 0)
534     {
535         // payload present
536         ret = pByte(new byte[Constants::TS_SIZE - offset]);
537         for (int i = 0; i < sizeof(ret); i++)
538             ret[i] = mData[i + offset];
539         ret[Constants::TS_SIZE - offset] = '\0';
540         return ret;
541     }
542     return ret;
543 }
544
545 void TsPacket::IncrementContinuityCounter(void)
546 {
547     if (0xf == (mData[3] & 0xf))
548         mData[3] &= 0xf0;
549     else
550         mData[3]++;
551 }
552
553 byte TsPacket::GetContinuityCounter(void)
554 {
555     return (byte)(mData[3] & 0xf);
556 }
557
558 void TsPacket::SetContinuityCounter(byte value)throw(std::out_of_range)
559 {
560     if (value > 0x0f)
561         throw std::out_of_range("Invalid continuity counter");
562     mData[3] &= 0xf0;
563     mData[3] |= value;
564 }
565
566 TsTable::TsTable(void)
567 {
568     // error = 0, payload = 1, priority = 0
569     mData[1] = 0x40;
570     // scrambling = 0, adaptation = 01, continuity = 0
571     mData[3] = 0x10;
572     // pointer = 00
573     mData[4] = 0x0;
574     // reserved, version, current/next
575     mData[10] = 0xc1;
576     // section
577     mData[11] = 0x0;
578     // last section
579     mData[12] = 0x0;
580 }
581
582 TsTable::TsTable(pByte data)
583 {
584     SetData(data, 0);
585 }
586
587 void TsTable::AddData(pByte value, int offset, int len)
588 {
589     pByte data = pByte(new byte[sizeof(mData.get()) + len]);
590     memcpy(reinterpret_cast<char*>(data.get()),
591         reinterpret_cast<char*>(mData.get()),
592         sizeof(mData.get()));
593     memcpy(reinterpret_cast<char*>(data.get() + sizeof(mData.get())),
594         reinterpret_cast<char*>(value.get() + offset),
595         len);
596     mData = data;
597 }
598
599 bool TsTable::Complete(void)
600 {
601     int currentLen = sizeof(mData.get()) - (GetPointerSize() + 8);
602     if (GetLength() > currentLen)
603         return false;
604     return true;
605 }
606
607 byte TsTable::GetTableId(void)
608 {
609     return mData[DATA_HEADER + GetPointerSize()];
610 }
611
612 void TsTable::SetTableId(byte value)
613 {
614     mData[DATA_HEADER + GetPointerSize()] = value;
615 }
616
617 ushort TsTable::GetNumberId(void)
618 {
619     return (ushort)((mData[8 + GetPointerSize()] << 8)
620         + mData[9 + GetPointerSize()]);
621 }
622
623 void TsTable::SetNumberId(ushort value)
624 {
625     mData[8 + GetPointerSize()] = (byte)((value >> 8) & 0xff);
626     mData[9 + GetPointerSize()] = (byte)(value & 0xff);
627 }
628
629 ushort TsTable::GetLength(void)
630 {
631     return (ushort)(((mData[6 + GetPointerSize()] & 0x0f) << 8)
632         + mData[7 + GetPointerSize()]);
633 }
634
635 void TsTable::SetLength(ushort value)
636 {
637     // syntax, reserved, length
638     mData[6 + GetPointerSize()]
639         = (byte)(0xb0 | (byte)((value >> 8) & 0x0f));
640     mData[7 + GetPointerSize()] = (byte)(value & 0xff);
641 }
642
643 void TsTable::RefreshCrc(void)
644 {
645     uint crc = Constants::ComputeCrc(
646         mData, GetLength() - 1, DATA_HEADER + GetPointerSize());
647     mData[GetLength() + 4 + GetPointerSize()]
648         = (byte)((crc >> 24) & 0xff);
649     mData[GetLength() + DATA_HEADER + GetPointerSize()]
650         = (byte)((crc >> 16) & 0xff);
651     mData[GetLength() + 6 + GetPointerSize()]
652         = (byte)((crc >> 8) & 0xff);
653     mData[GetLength() + 7 + GetPointerSize()]
654         = (byte)(crc & 0xff);
655     for (int i = GetLength() + 8 + GetPointerSize();
656         i < Constants::TS_SIZE; i++) {
657         mData[i] = 0xff;
658     }
659 }
660
661 Descriptor::Descriptor(pByte data, int startIndex)throw(std::invalid_argument)
662 {
663     if (sizeof(data.get()) < 2)
664         throw std::invalid_argument("Invalid descriptor");
665
666     if (startIndex + 2 + data[startIndex + 1]
667         > strlen(reinterpret_cast<char*>(data.get())))
668         throw std::invalid_argument("Invalid descriptor");
669
670     mData = pByte(new byte[2 + data[startIndex + 1]]);
671     for (int i = 0; i < sizeof(mData.get()); i++)
672         mData[i] = data[i + startIndex];
673 }
674
675 byte Descriptor::GetTag(void)
676 {
677     return mData[0];
678 }
679
680 byte Descriptor::GetLength(void)
681 {
682     return mData[1];
683 }
684
685 pByte Descriptor::GetData(void)
686 {
687     return mData;
688 }
689
690 PcrPacket::PcrPacket(Int64 pcr, byte counter, ushort pid)
691 {
692     SetPID(pid);
693     SetContinuityCounter(counter);
694     mData[3] &= 0x0f; // adaptation field only, no payload
695     mData[3] |= 0x20; // adaptation field only, no payload
696     mData[4] = 183; // length
697     mData[DATA_HEADER] = 0x10; // only PCR present
698     Int64 tsClockValue = pcr / 300;
699     Int64 tsOffsetValue = pcr % 300;
700     mData[6] = (byte)((tsClockValue & 0x1fe000000LL) >> 25);
701     mData[7] = (byte)((tsClockValue & 0x1fe0000) >> 17);
702     mData[8] = (byte)((tsClockValue & 0x1fe00) >> 9);
703     mData[9] = (byte)((tsClockValue & 0x1fe) >> 1);
704     if ((tsClockValue & 0x1) == 0)
705         mData[10] &= 0x7f;
706     if ((tsOffsetValue & 0x100) == 0)
707         mData[10] &= 0xfe;
708     mData[11] = (byte)(tsOffsetValue & 0xff);
709     for (int i = 12; i < Constants::TS_SIZE; i++)
710         mData[i] = 0xff;
711 }
712
713 PesPacket::PesPacket(pByte buff, int offset, int length, ushort pid)
714 {
715     mData = pByte(new byte[length]);
716     SetPID(pid);
717     AddData(buff, offset, length);
718     SetPriority(false);
719 }
720
721 bool PesPacket::GetPriority(void)
722 {
723     return mPriority;
724 }
725
726 void PesPacket::SetPriority(bool value)
727 {
728     mPriority = value;
729 }
730
731 pByte PesPacket::GetData(void)
732 {
733     return mData;
734 }
735
736 pByte PesPacket::GetPayload(void)
737 {
738     boost::shared_ptr<PesHeader> ph = GetHeader();
739     if(!ph) return GetData();
740     pByte buff = pByte(
741         new byte[sizeof(mData.get()) - 9 + ph.get()->GetHeaderLength() + 1]);
742     for(int i=(9 + ph.get()->GetHeaderLength()); i < sizeof(mData.get()); i++)
743         buff[i] = mData[i];
744     buff[sizeof(mData.get()) - (9 + ph.get()->GetHeaderLength())] = '\0';
745     return buff;
746 }
747
748 byte PesPacket::GetByte(int i)
749 {
750     return mData[i];
751 }
752
753 void PesPacket::SetByte(int i, byte value)
754 {
755     mData[i] = value;
756 }
757
758 ushort PesPacket::GetPID(void)
759 {
760     return mPID;
761 }
762
763 void PesPacket::SetPID(ushort id)
764 {
765     mPID = id;
766 }
767
768 bool PesPacket::GetComplete(void)
769 {
770     if(sizeof(mData.get()) < 6) return false;
771     ushort len = (ushort)((mData[4] << 8) + mData[DATA_HEADER]);
772     if(len == 0) return false;
773     if(sizeof(mData.get()) != len + 6) return false;
774     return true;
775 }
776
777 void PesPacket::SetComplete(bool value)
778 {
779     if(value)
780     {
781         ushort len = (ushort)(sizeof(mData.get()) - 6);
782         if (sizeof(mData.get()) > (0xffff - 6))
783             len = 0;
784         mData[4] = (byte)((len >> 8) & 0xff);
785         mData[DATA_HEADER] = (byte)(len & 0xff);
786     }
787 }
788
789 boost::shared_ptr<PesHeader> PesPacket::GetHeader(void)
790 {
791     boost::shared_ptr<PesHeader> ph;
792     try
793     {
794         ph = boost::shared_ptr<PesHeader>(new PesHeader(mData));
795         return ph;
796     }
797     catch(...)
798     {
799         // no valid header (yet)
800         ph.reset();
801         return ph;
802     }
803 }
804
805 void PesPacket::AddData(pByte moredata)
806 {
807     pByte data = pByte(new byte[sizeof(mData.get()) + sizeof(moredata.get())]);
808     memcpy(reinterpret_cast<char*>(data.get()),
809         reinterpret_cast<char*>(mData.get()),
810         sizeof(mData.get()));
811     memcpy(reinterpret_cast<char*>(data.get() + sizeof(mData.get())),
812         reinterpret_cast<char*>(mData.get()),
813         sizeof(mData.get()));
814     mData = data;
815 }
816
817 void PesPacket::AddData(pByte buff, int offset, int len)
818 {
819     pByte data = pByte(new byte[sizeof(mData.get()) + len]);
820     memcpy(reinterpret_cast<char*>(data.get()),
821         reinterpret_cast<char*>(mData.get()),
822         sizeof(mData.get()));
823     memcpy(reinterpret_cast<char*>(data.get() + sizeof(mData.get())),
824         reinterpret_cast<char*>(mData.get() + offset),
825         len);
826     mData = data;
827 }
828
829 byte PesPacket::GetBaseId(void)
830 {
831     if (sizeof(mData.get()) > 3)
832         return mData[3];
833     return 0;
834 }
835
836 byte PesPacket::GetExtendedId(void)
837 {
838     if ((sizeof(mData.get()) > 8) && sizeof(mData.get()) > (8 + mData[8]))
839         return mData[9 + mData[8]];
840     return 0;
841 }
842
843 UInt32 PesPacket::GetExtendedType(void)
844 {
845     if ((sizeof(mData.get()) > 8) && sizeof(mData.get()) > (11 + mData[8]))
846     {
847         UInt32 format = (UInt32)mData[9 + mData[8]] << 24;
848         format += (UInt32)mData[10 + mData[8]] << 16;
849         format += (UInt32)mData[11 + mData[8]] << 8;
850         format += (UInt32)mData[12 + mData[8]];
851         return format;
852     }
853     return 0;
854 }
855
856 PesHeader::PesHeader(pByte data)throw(std::invalid_argument)
857 {
858     if (sizeof(data.get()) < 9)
859         throw std::invalid_argument("Invalid PES header length");
860     if (data[0] != 0x00 || data[1] != 0x00 || data[2] != 0x01)
861         throw std::invalid_argument("Invalid PES prefix");
862     int hlen = 9 + data[8];
863     int plen = 6 + (data[4] << 8) + data[DATA_HEADER];
864     if (plen != 6 && hlen > plen)
865         throw new std::invalid_argument("Invalid PES header/packet length");
866     if (sizeof(data.get()) < hlen)
867         throw std::invalid_argument("PES Header too short");
868     mData = pByte(new byte[hlen]);
869     for (int i = 0; i < hlen; i++)
870         mData[i] = data[i];
871 }
872
873 byte PesHeader::GetStreamId(void)
874 {
875     return mData[3];
876 }
877
878 byte PesHeader::GetByte(int i)
879 {
880     return mData[i];
881 }
882
883 void PesHeader::SetByte(int i, byte data)
884 {
885     mData[i] = data;
886 }
887
888 byte PesHeader::GetHeaderLength(void)
889 {
890     return mData[8];
891 }
892
893 int PesHeader::GetTotalHeaderLength(void)
894 {
895     return 9 + GetHeaderLength();
896 }
897
898 ushort PesHeader::GetPacketLength(void)
899 {
900     return (ushort)((mData[4] << 8) + mData[DATA_HEADER]);
901 }
902
903 bool PesHeader::HasPts(void)
904 {
905     if(sizeof(mData.get()) > 13)
906         return (mData[7] & 0x80) > 0;
907     return false;
908 }
909
910 bool PesHeader::HasDts(void)
911 {
912     if(sizeof(mData.get()) > 18 )
913         return (mData[7] & 0x40) > 0;
914     return false;
915 }
916
917 Int64 PesHeader::GetPts(void)throw(std::invalid_argument)
918 {
919     if (HasPts() == false)
920         throw std::invalid_argument("No Pts available");
921     Int64 ret = 0;
922     ret += ((Int64)(mData[9] & 0x0e)) << 29;
923     ret += ((Int64)(mData[10])) << 22;
924     ret += ((Int64)(mData[11] & 0xfe)) << 14;
925     ret += ((Int64)(mData[12])) << 7;
926     ret += ((Int64)(mData[13] & 0xfe)) >> 1;
927     return ret;
928 }
929
930 void PesHeader::SetPts(Int64 value)throw(std::invalid_argument)
931 {
932     if (HasPts() == false)
933         throw std::invalid_argument("No Pts available");
934     byte old = (byte)(mData[9] & 0xf1);
935     mData[9] = (byte)(((value & 0x1C0000000LL) >> 29) | old);
936     mData[10] = (byte)((value & 0x3fC00000LL) >> 22);
937     mData[11] = (byte)(((value & 0x3f8000LL) >> 14) | 0x01);
938     mData[12] = (byte)((value & 0x7f80LL) >> 7);
939     mData[13] = (byte)(((value & 0x7fLL) << 1) | 0x01);
940 }
941
942 Int64 PesHeader::GetDts(void)throw(std::invalid_argument)
943 {
944     if (HasDts() == false)
945         throw std::invalid_argument("No Dts available");
946     Int64 ret = 0;
947     ret += ((Int64)(mData[14] & 0x0e)) << 29;
948     ret += ((Int64)(mData[15])) << 22;
949     ret += ((Int64)(mData[16] & 0xfe)) << 14;
950     ret += ((Int64)(mData[17])) << 7;
951     ret += ((Int64)(mData[18] & 0xfe)) >> 1;
952     return ret;
953 }
954
955 void PesHeader::SetDts(Int64 value)throw(std::invalid_argument)
956 {
957     if (HasDts() == false)
958         throw std::invalid_argument("No Dts available");
959     byte old = (byte)(mData[14] & 0xf1);
960     mData[14] = (byte)(((value & 0x1C0000000LL) >> 29) | old);
961     mData[15] = (byte)((value & 0x3fC00000LL) >> 22);
962     mData[16] = (byte)(((value & 0x3f8000LL) >> 14) | 0x01);
963     mData[17] = (byte)((value & 0x7f80LL) >> 7);
964     mData[18] = (byte)(((value & 0x7fLL) << 1) | 0x01);
965 }
966
967 byte PesHeader::GetExtention2(void)
968 {
969     int offset = 6;
970     if((mData[offset] & 0xc0) != 0x80)
971         return 0; // first two bits must be '10'
972     byte PTS_DTS_flags = (byte)(mData[offset + 1] & 0xc0);
973     byte ESCR_flag = (byte)(mData[offset + 1] & 0x20);
974     byte ES_rate_flag = (byte)(mData[offset + 1] & 0x10);
975     byte DSM_trick_mode_flag = (byte)(mData[offset + 1] & 0x08);
976     byte additional_copy_info_flag = (byte)(mData[offset + 1] & 0x04);
977     byte PES_CRC_flag = (byte)(mData[offset + 1] & 0x02);
978     byte PES_extension_flag = (byte)(mData[offset + 1] & 0x01);
979     if(mData[offset + 2] == 0)
980         return 0;
981     int length = offset + mData[offset + 2] + 3;
982     if(sizeof(mData.get()) < length)
983         return 0;
984     offset += 3;
985     if(PTS_DTS_flags == 0x80)
986         offset += 5;
987     if(PTS_DTS_flags == 0xc0)
988         offset += 10;
989     if(ESCR_flag > 0)
990         offset += 6;
991     if(ES_rate_flag > 0)
992         offset += 3;
993     if(DSM_trick_mode_flag > 0)
994         offset += 1;
995     if(additional_copy_info_flag > 0)
996         offset += 1;
997     if(PES_CRC_flag > 0)
998         offset += 2;
999     if(PES_extension_flag == 0)
1000         return 0;
1001     byte PES_private_data_flag = (byte)(mData[offset] & 0x80);
1002     byte pack_header_field_flag = (byte)(mData[offset] & 0x40);
1003     byte program_packet_sequence_counter_flag
1004         = (byte)(mData[offset] & 0x20);
1005     byte PSTD_mDataer_flag = (byte)(mData[offset] & 0x10);
1006     byte PES_extension_flag_2 = (byte)(mData[offset] & 0x01);
1007     offset++;
1008     if(PES_private_data_flag > 0)
1009         offset += 25;
1010     if(pack_header_field_flag > 0)
1011         offset += (mData[offset] + 1);
1012     if(program_packet_sequence_counter_flag > 0)
1013         offset += 2;
1014     if(PSTD_mDataer_flag > 0)
1015         offset += 2;
1016     if(PES_extension_flag_2 == 0)
1017         return 0;
1018     if(mData[offset] != 0x81)
1019         return 0;
1020     return mData[offset + 1];
1021 }
1022
1023 pByte PesHeader::GetData(void)
1024 {
1025     return mData;
1026 }
1027
1028 VC1SequenceInfo::VC1SequenceInfo(pByte data, int offset)
1029 {
1030     UInt32 marker = 0xffffffff;
1031     for(; offset < sizeof(data.get()); offset++)
1032     {
1033         marker = marker << 8;
1034         marker &= 0xffffff00;
1035         marker += data[offset];
1036         if(marker == Constants::VC1_SEQ_SC)
1037             break;
1038     }
1039     offset++;
1040     if(offset < sizeof(data.get()))
1041     {
1042         // sequence header
1043         mData = pByte(new byte[sizeof(data.get()) - offset]);
1044         for(int i = 0; offset < sizeof(data.get()); i++, offset++)
1045             mData[i] = data[offset];
1046     }
1047     else
1048         mData.reset();
1049 }
1050
1051 int VC1SequenceInfo::GetHeight(void)
1052 {
1053     if(!mData && sizeof(mData.get()) > 4)
1054         return ((((mData[3] & 0x0f) << 8) + mData[4]) << 1) + 2;
1055     else
1056         return -1;
1057 }
1058
1059 int VC1SequenceInfo::GetWidth(void)
1060 {
1061     if(!mData && sizeof(mData.get()) > 3)
1062         return (((mData[2] << 4) + ((mData[3] & 0xf0) >> 4)) << 1) + 2;
1063     else
1064         return -1;
1065 }
1066
1067 bool VC1SequenceInfo::Valid(void)
1068 {
1069     return !mData;
1070 }
1071
1072 bool VC1SequenceInfo::Interlaced(void)
1073 {
1074     if(!mData && sizeof(mData.get()) > DATA_HEADER)
1075         return ((mData[DATA_HEADER] & 0x40) > 0);
1076     return false;
1077 }
1078
1079 bool VC1SequenceInfo::DisplayExt(void)
1080 {
1081     if(!mData && sizeof(mData.get()) > DATA_HEADER)
1082         return ((mData[DATA_HEADER] & 0x02) > 0);
1083     return false;
1084 }
1085
1086 bool VC1SequenceInfo::AspectFlag(void)
1087 {
1088     if(DisplayExt() && sizeof(mData.get()) > 9)
1089         return ((mData[9] & 0x10) > 0);
1090     return false;
1091 }
1092
1093 byte VC1SequenceInfo::GetVc1AspectRatio(void)
1094 {
1095     if(AspectFlag() && sizeof(mData.get()) > 9)
1096         return (byte)(mData[9] & 0x0f);
1097     return 0;
1098 }
1099
1100 bool VC1SequenceInfo::FrameFlag(void)
1101 {
1102     if(AspectFlag())
1103     {
1104          if(GetVc1AspectRatio() == 15 && sizeof(mData.get()) > 12)
1105              return ((mData[12] & 0x80) > 0);
1106          else if(GetVc1AspectRatio() != 15 && sizeof(mData.get()) > 10)
1107              return ((mData[10] & 0x80) > 0);
1108          else
1109              return false;
1110     }
1111     else if(sizeof(mData.get()) > 9)
1112          return ((mData[9] & 0x08) > 0);
1113     else
1114          return false;
1115 }
1116
1117 bool VC1SequenceInfo::FrameRateIndicatorFlag(void)
1118 {
1119     if(FrameFlag())
1120     {
1121         if(AspectFlag())
1122         {
1123             if (GetVc1AspectRatio() == 15 && sizeof(mData.get()) > 12)
1124                 return ((mData[12] & 0x40) > 0);
1125             else if (GetVc1AspectRatio() != 15 && sizeof(mData.get()) > 10)
1126                 return ((mData[10] & 0x40) > 0);
1127             else
1128                 return false;
1129         }
1130         else if (sizeof(mData.get()) > 9)
1131             return ((mData[9] & 0x04) > 0);
1132         else
1133             return false;
1134     }
1135     else
1136         return false;
1137 }
1138
1139 AspectRatio VC1SequenceInfo::GetAspectRatio(void)
1140 {
1141     if(GetVc1AspectRatio() == 1)
1142     {
1143         if(GetWidth() == 1920 && GetHeight() == 1080)
1144             return a16_9;
1145         if(GetWidth() == 1280 && GetHeight() == 720)
1146             return a16_9;
1147         if(GetWidth() == 640 && GetHeight() == 480)
1148             return a4_3;
1149     }
1150     if(GetVc1AspectRatio() >= 2 && GetVc1AspectRatio() <= 5)
1151         return a4_3;
1152     if(GetVc1AspectRatio() >= 6 && GetVc1AspectRatio() <= 9)
1153         return a16_9;
1154     if(GetVc1AspectRatio() >= 10 && GetVc1AspectRatio() <= 11)
1155         return a4_3;
1156     if(GetVc1AspectRatio() >= 12 && GetVc1AspectRatio() <= 13)
1157         return a16_9;
1158     return AR_Reserved;
1159 }
1160
1161 VideoFormat VC1SequenceInfo::GetVideoFormat(void)
1162 {
1163     if (GetHeight() == 480 && Interlaced() == true)
1164         return i480;
1165     else if (GetHeight() == 480 && Interlaced() == false)
1166         return p480;
1167     else if (GetHeight() == 576 && Interlaced() == true)
1168         return i576;
1169     else if (GetHeight() == 576 && Interlaced() == false)
1170         return p576;
1171     else if (GetHeight() == 720 && Interlaced() == false)
1172         return p720;
1173     else if (GetHeight() == 1080 && Interlaced() == true)
1174         return i1080;
1175     else if (GetHeight() == 1080 && Interlaced() == false)
1176         return p1080;
1177     return VF_Reserved;
1178 }
1179
1180 FrameRate VC1SequenceInfo::GetFrameRate(void)
1181 {
1182     if(false == FrameFlag())
1183         return FR_Reserved;
1184     if(false == FrameRateIndicatorFlag())
1185     {
1186         byte FrameRateNr = 0;
1187         byte FrameRateDr = 0;
1188         if(AspectFlag())
1189         {
1190             if(GetVc1AspectRatio() == 15 && sizeof(mData.get()) > 13)
1191             {
1192                 FrameRateNr = (byte)(((mData[12] & 0x3f) << 2)
1193                     + ((mData[13] & 0xc0) >> 6));
1194                 FrameRateDr = (byte)((mData[13] & 0x3c) >> 2);
1195             }
1196             else if(GetVc1AspectRatio() != 15 && sizeof(mData.get()) > 11)
1197             {
1198                 FrameRateNr = (byte)(((mData[10] & 0x3f) << 2)
1199                     + ((mData[11] & 0xc0) >> 6));
1200                 FrameRateDr = (byte)((mData[11] & 0x3c) >> 2);
1201             }
1202         }
1203         else if(sizeof(mData.get()) > 11)
1204         {
1205             FrameRateNr = (byte)(((mData[9] & 0x03) << 6)
1206                 + ((mData[10] & 0xfc) >> 2));
1207             FrameRateDr = (byte)(((mData[10] & 0x03) << 2)
1208                 + ((mData[11] & 0xc0) >> 6));
1209         }
1210
1211         if(FrameRateNr == 1 && FrameRateDr == 2)
1212                 return f23_976;
1213         else if(FrameRateNr == 1 && FrameRateDr == 1)
1214                 return f24;
1215         else if(FrameRateNr == 2 && FrameRateDr == 1)
1216                 return f25;
1217         else if(FrameRateNr == 3 && FrameRateDr == 2)
1218                 return f29_97;
1219         else if(FrameRateNr == 4 && FrameRateDr == 1)
1220                 return f50;
1221         else if(FrameRateNr == 5 && FrameRateDr == 2)
1222                 return f59_94;
1223     }
1224     return FR_Reserved;
1225 }
1226
1227 // implement class PatPacket : TsTable
1228 PatPacket::PatPacket(void)
1229 {
1230     SetPID(Constants::PAT_PID);
1231     SetTableId(Constants::PAT_TABLE_ID);
1232     SetLength(9);
1233     SetTransportStreamId(1);
1234     RefreshCrc();
1235 }
1236
1237 PatPacket::PatPacket(pByte data)throw(std::invalid_argument)
1238 {
1239     if(GetTableId() != Constants::PAT_TABLE_ID)
1240         throw std::invalid_argument(
1241         "packet does not contain a valid PAT table ID");
1242     if (0 != GetPID())
1243         throw std::invalid_argument("packet does not contain a valid PAT PID");
1244 }
1245
1246 ushort PatPacket::GetTransportStreamId(void)
1247 {
1248     return GetNumberId();
1249 }
1250
1251 void PatPacket::SetTransportStreamId(ushort value)
1252 {
1253     SetNumberId(value);
1254     RefreshCrc();
1255 }
1256 /*
1257 boost::shared_array<ProgramInfo> PatPacket::GetPrograms(void)
1258 {
1259     boost::shared_array<ProgramInfo> programs;
1260     programs.reset();
1261     if (GetProgramInfoLength() == 0)
1262         return programs;
1263     programs = boost::shared_array(new ProgramInfo[GetProgramInfoLength() / 4]);
1264     for (int i = 0; i < GetProgramInfoLength(); i += 4)
1265         programs[i / 4] = boost::shared_array<ProgramInfo>(
1266             new ProgramInfo(mData, 13 + GetPointerSize() + i));
1267     return programs;
1268 }
1269
1270 void PatPacket::SetPrograms(boost::shared_ptr<ProgramInfo> value)
1271     throw(std::invalid_argument)
1272 {
1273     if (NULL == value || sizeof(value.get()) == 0)
1274     {
1275         if (GetProgramInfoLength() == 0)
1276             return;
1277         SetLength(9);
1278         RefreshCrc();
1279     }
1280     else
1281     {
1282         if ((sizeof(value.get()) * 4) + 17 + GetPointerSize()
1283             > Constants.TS_SIZE)
1284             throw std::invalid_argument("program info data too long");
1285         SetLength((ushort)(9 + (sizeof(value.get()) * 4)));
1286         int index = 13 + GetPointerSize();
1287         for(int pi = 0; pi < GetProgramInfoLength() / 4; pi++)
1288         {
1289             for (int i = 0; i < 4; i++)
1290                 mData[index + i] = value[pi].Data[i];
1291             index += 4;
1292         }
1293         RefreshCrc();
1294     }
1295 }
1296 */
1297 ushort PatPacket::GetProgramInfoLength(void)
1298 {
1299     return (ushort)(GetLength() - 9);
1300 }
1301
1302 DTCP_Descriptor::DTCP_Descriptor(pByte data, int startIndex)
1303     throw(std::invalid_argument):Descriptor(data, startIndex)
1304 {
1305     if (sizeof(data.get()) < 6)
1306         throw std::invalid_argument("Invalid DTCP descriptor");
1307     if (GetTag() != Constants::DTCP_DESCRIPTOR_TAG)
1308         throw std::invalid_argument("Invalid DTCP descriptor tag");
1309     if (GetLength() < 4)
1310         throw std::invalid_argument("Invalid DTCP descriptor length");
1311     if (data[startIndex + 2] != 0x0f || data[startIndex + 3] != 0xff)
1312         throw std::invalid_argument("Invalid DTCP descriptor CA system ID");
1313 }
1314
1315 DtcpCci DTCP_Descriptor::GetCopyStatus(void)
1316 {
1317     return (DtcpCci)(mData[4] & 0x3);
1318 }
1319
1320 bool DTCP_Descriptor::GetAnalogConstrain(void)
1321 {
1322     return ((mData[5] & 0x4) == 0);
1323 }
1324
1325 bool DTCP_Descriptor::GetMacrovision(void)
1326 {
1327     return ((mData[5] & 0x3) > 0);
1328 }
1329
1330 MlpInfo::MlpInfo(pByte data, int offset)
1331 {
1332     UInt32 marker = 0xffffffff;
1333     for (; offset < sizeof(data.get()); offset++)
1334     {
1335         marker = (UInt32)marker << 8;
1336         marker &= 0xffffff00;
1337         marker += data[offset];
1338         if (marker == Constants::MLP_SYNC)
1339             break;
1340     }
1341     offset++;
1342     if (offset < sizeof(data.get()))
1343     {
1344         // sequence header
1345         mData = pByte(new byte[sizeof(data.get()) - offset]);
1346         for (int i = 0; offset < sizeof(data.get()); i++, offset++)
1347             mData[i] = data[offset];
1348     }
1349     else
1350         mData.reset();
1351 }
1352
1353 AspectRatio MlpInfo::GetAspectRatio(void)
1354 {
1355     return AR_Reserved;
1356 }
1357
1358 FrameRate MlpInfo::GetFrameRate(void)
1359 {
1360     return FR_Reserved;
1361 }
1362
1363 VideoFormat MlpInfo::GetVideoFormat(void)
1364 {
1365     return VF_Reserved;
1366 }
1367
1368 pByte MlpInfo::GetElementaryDescriptors(void)
1369 {
1370     std::vector<byte> descriptors;
1371     for(int i = 0; i < sizeof(Constants::ac3_registration_descriptor); i++)
1372     {
1373         descriptors.push_back(Constants::ac3_registration_descriptor[i]);
1374     }
1375     pByte ad = GetAC3AudioDescriptor();
1376     for(int i = 0; i < sizeof(ad.get()); i++)
1377     {
1378        descriptors.push_back(ad[i]); 
1379     }
1380     return Utility::ToArray(descriptors);
1381 }
1382
1383 AudioPresentationType MlpInfo::GetAudioPresentationType(void)
1384 {
1385     return multi;
1386 }
1387
1388 SamplingFrequency MlpInfo::GetSamplingFrequency(void)
1389 {
1390     switch (GetSampleRateCode())
1391     {
1392     case 0:
1393         return kHz48;
1394     case 1:
1395         return kHz96;
1396     case 2:
1397         return kHz192;
1398     case 8: // 44.1kHz
1399     case 9: // 88.2kHz
1400     case 10: // 176.4kHz
1401     default:
1402         return SF_Reserved;
1403     }
1404 }
1405
1406 pByte MlpInfo::GetAC3AudioDescriptor(void)
1407 {
1408     std::vector<byte> desc;
1409     desc.push_back(0x81);
1410     desc.push_back(0x00);
1411     desc.push_back((byte)((GetSampleRateCode() << 5) | 0x08));
1412     desc.push_back(200);
1413     desc.push_back((byte)(0x0f));
1414     desc[1] = (byte)(desc.size() - 2);
1415     return Utility::ToArray(desc);
1416 }
1417
1418 byte MlpInfo::GetSampleRateCode(void)
1419 {
1420     if (sizeof(mData.get()) > 0)
1421         return (byte)(mData[0] >> 4);
1422     return 0x00;
1423 }
1424
1425 // implement class AC3Info : ElementaryParse
1426 readonly int AC3Info::len48k[] = {
1427              128,  128,  160,  160,  192,  192,  224,  224,
1428              256,  256,  320,  320,  384,  384,  448,  448,
1429              512,  512,  640,  640,  768,  768,  896,  896,
1430             1024, 1024, 1280, 1280, 1536, 1536, 1792, 1792,
1431             2048, 2048, 2304, 2304, 2560, 2560 };
1432
1433 readonly int AC3Info::len44k[] = {
1434              138,  140,  174,  176,  208,  210,  242,  244,
1435              278,  280,  348,  350,  416,  418,  486,  488,
1436              556,  558,  696,  698,  834,  836,  974,  976,
1437             1114, 1116, 1392, 1394, 1670, 1672, 1950, 1952,
1438             2228, 2230, 2506, 2508, 2786, 2788 };
1439
1440 readonly int AC3Info::len32k[] = { 
1441              192,  192,  240,  240,  288,  288,  336,  336,
1442              384,  384,  480,  480,  576,  576,  672,  672,
1443              768,  768,  960,  960, 1152, 1152, 1344, 1344,
1444             1536, 1536, 1920, 1920, 2304, 2304, 2688, 2688,
1445             3072, 3072, 3456, 3456, 3840, 3840 };
1446
1447 int AC3Info::GetMaxFrameLength(void)
1448 {
1449     return len32k[sizeof(len32k) - 1];
1450 }
1451
1452 int AC3Info::GetFrameLength(void)
1453 {
1454     if(GetSyntaxType() == Standard
1455     || GetSyntaxType() == Alternative)
1456     {
1457         byte index = (byte)(mData[2] & 0x3f);
1458         if (index < 38)
1459         {
1460             switch (GetSampleRateCode())
1461             {
1462             case 00:
1463                 return len48k[mData[2] & 0x3f];
1464             case 01:
1465                 return len44k[mData[2] & 0x3f];
1466             case 02:
1467                 return len32k[mData[2] & 0x3f];
1468             }
1469         }
1470     }
1471     else if (GetSyntaxType() == Enhanced)
1472         return (((mData[0] & 0x03) << 8) + mData[1] + 1) << 1;
1473     return 0;
1474 }
1475
1476 bool AC3Info::Valid(void)
1477 {
1478     return mValid && GetSyntaxType() != Invalid;
1479 }
1480
1481 byte AC3Info::GetSampleRateCode(void)
1482 {
1483     if (sizeof(mData.get()) > 2)
1484         return (byte)(mData[2] >> 6);
1485     return 0x03;
1486 }
1487
1488 byte AC3Info::GetBsid(void)
1489 {
1490     if (sizeof(mData.get()) > 3)
1491         return (byte)(mData[3] >> 3);
1492     return 0;
1493 }
1494
1495 byte AC3Info::GetBsmod(void)
1496 {
1497     if (sizeof(mData.get()) > 3
1498     && (GetSyntaxType() == Standard
1499     || GetSyntaxType() == Alternative))
1500         return (byte)(mData[3] & 0x07);
1501     return 0;
1502 }
1503
1504 byte AC3Info::GetAcmod(void)
1505 {
1506     if (sizeof(mData.get()) > 4
1507     && (GetSyntaxType() == Standard
1508     || GetSyntaxType() == Alternative))
1509         return (byte)(mData[4] >> 5);
1510     else if (sizeof(mData.get()) > 2
1511     && GetSyntaxType() == Enhanced)
1512         return (byte)((mData[2] >> 1) & 0x07);
1513     return 0x07;
1514 }
1515
1516 bool AC3Info::IsIndependentStream(void)
1517 {
1518     if (Enhanced != GetSyntaxType())
1519         return true;
1520     byte res = (byte)(mData[0] >> 6);
1521     if (res != 1)
1522         return true;
1523     return false;
1524 }
1525
1526 Ac3SyntaxType AC3Info::GetSyntaxType(void)
1527 {
1528     switch (GetBsid())
1529     {
1530     case (byte)Standard:
1531         return Standard;
1532     case (byte)Alternative:
1533         return Alternative;
1534     case (byte)Enhanced:
1535         return Enhanced;
1536     }
1537     return Invalid;
1538 }
1539
1540 pByte AC3Info::GetAC3AudioDescriptor(void)
1541 {
1542     std::vector<byte> desc;
1543     desc.push_back(0x81);
1544     desc.push_back((byte)(desc.size() - 2));
1545     desc.push_back((byte)((GetSampleRateCode() << 5) | GetBsid()));
1546     desc.push_back(200);
1547     desc.push_back((byte)((GetBsmod() << 5) | (GetAcmod() << 1) | 1));
1548     return Utility::ToArray(desc);
1549 }
1550
1551 AC3Info::AC3Info(pByte data, int offset)
1552 {
1553     ushort marker = 0xffff;
1554     for (; offset < sizeof(data.get()); offset++)
1555     {
1556         marker = (ushort)(marker << 8);
1557         marker &= 0xff00;
1558         marker += data[offset];
1559         if (marker == Constants::AC3_SYNC)
1560             break;
1561     }
1562     offset++;
1563     if (offset < sizeof(data.get()))
1564     {
1565         // sequence header
1566         mData = pByte(new byte[sizeof(data.get()) - offset]);
1567         for (int i = 0; offset < sizeof(data.get()); i++, offset++)
1568             mData[i] = data[offset];
1569     }
1570     else
1571         mData.reset();
1572 }
1573
1574 AudioPresentationType AC3Info::GetAudioPresentationType(void)
1575 {
1576     switch (GetAcmod())
1577     {
1578     case 0x00:
1579         return stereo;
1580     case 0x01:
1581         return mono;
1582     case 0x02:
1583         return stereo;
1584     default:
1585         return multi;
1586     }
1587 }
1588
1589 SamplingFrequency AC3Info::GetSamplingFrequency(void)
1590 {
1591     switch (GetSampleRateCode())
1592     {
1593     case 0x00:
1594         return kHz48;
1595     }
1596     return SF_Reserved;
1597 }
1598
1599 pByte AC3Info::GetElementaryDescriptors(void)
1600 {
1601     std::vector<byte> descriptors;
1602     for(int i = 0; i < sizeof(Constants::ac3_registration_descriptor); i++)
1603     {
1604         descriptors.push_back(Constants::ac3_registration_descriptor[i]);
1605     }
1606     pByte ac3ad = GetAC3AudioDescriptor();
1607     for(int j = 0; j < sizeof(ac3ad.get()); j++)
1608     {
1609         descriptors.push_back(ac3ad[j]);
1610     }
1611     return Utility::ToArray(descriptors);
1612 }
1613
1614 AspectRatio AC3Info::GetAspectRatio(void)
1615 {
1616     return AR_Reserved;
1617 }
1618
1619 FrameRate AC3Info::GetFrameRate(void)
1620 {
1621     return FR_Reserved;
1622 }
1623
1624 VideoFormat AC3Info::GetVideoFormat(void)
1625 {
1626     return VF_Reserved;
1627 }
1628
1629 // implement class DtsInfo : ElementaryParse
1630 DtsInfo::DtsInfo(pByte data, int offset)
1631 {
1632     UInt32 marker = 0xffffffff;
1633     for(; offset < sizeof(data.get()); offset++)
1634     {
1635         marker = (UInt32)marker << 8;
1636         marker &= 0xffffff00;
1637         marker += data[offset];
1638         if(marker == Constants::DTS_SYNC)
1639             break;
1640     }
1641     offset++;
1642     if(offset < sizeof(data.get()))
1643     {
1644         // sequence header
1645         mData = pByte(new byte[sizeof(data.get()) - offset]);
1646         for(int i = 0; offset < sizeof(data.get()); i++, offset++)
1647             mData[i] = data[offset];
1648     }
1649     else
1650         mData.reset();
1651 }
1652
1653 ushort DtsInfo::GetFrameSize(void)
1654 {
1655     indicator = 14;
1656     ushort ret = 0;
1657     for (int i = 0; i < 14; i++)
1658     {
1659         ret <<= 1;
1660         ret |= GetNextBit();
1661     }
1662     ret++;
1663     return ret;
1664 }
1665
1666 byte DtsInfo::GetAmode(void)
1667 {
1668     indicator = 28;
1669     byte ret = 0;
1670     for (int i = 0; i < 6; i++)
1671     {
1672         ret <<= 1;
1673         ret |= GetNextBit();
1674     }
1675     return ret;
1676 }
1677
1678 /*
1679 byte DtsInfo::GetSamplingFrequency(void)
1680 {
1681     indicator = 34;
1682     byte ret = 0;
1683     for (int i = 0; i < 4; i++)
1684     {
1685         ret <<= 1;
1686         ret |= GetNextBit();
1687     }
1688     return ret;
1689 }
1690 */
1691
1692 bool DtsInfo::GetExtAudio(void)
1693 {
1694     indicator = 51;
1695     if (1 == GetNextBit())
1696         return true;
1697     return false;
1698 }
1699
1700 byte DtsInfo::GetExtAudioId(void)
1701 {
1702     if (false == GetExtAudio())
1703         return 0xff;
1704     indicator = 48;
1705     byte ret = 0;
1706     for (int i = 0; i < 3; i++)
1707     {
1708         ret <<= 1;
1709         ret |= GetNextBit();
1710     }
1711     return ret;
1712 }
1713
1714 AudioPresentationType DtsInfo::GetAudioPresentationType(void)
1715 {
1716     switch (GetAmode())
1717     {
1718     case 0x00:
1719         return mono;
1720     case 0x01:
1721     case 0x02:
1722     case 0x03:
1723     case 0x04:
1724         return stereo;
1725     default:
1726         return multi;
1727     }
1728 }
1729
1730 SamplingFrequency DtsInfo::GetSamplingFrequency(void)
1731 {
1732     switch (mSamplingFrequency)
1733     {
1734     case 0xd:
1735         return kHz48;
1736     }
1737     return SF_Reserved;
1738 }
1739
1740 pByte DtsInfo::GetElementaryDescriptors(void)
1741 {
1742     // DTS registration descriptor
1743     pByte regdesc = pByte(new byte[6]);
1744     regdesc[0] = 0x05;
1745     regdesc[1] = 0x04;
1746     regdesc[2] = 0x44;
1747     regdesc[3] = 0x54;
1748     regdesc[4] = 0x53;
1749     if(sizeof(mData.get()) < 0x400)
1750         regdesc[5] = 0x31;
1751     else if(sizeof(mData.get()) < 0x800)
1752         regdesc[5] = 0x32;
1753     else
1754         regdesc[5] = 0x33;
1755     return regdesc;
1756 }
1757
1758 AspectRatio DtsInfo::GetAspectRatio(void)
1759 {
1760     return AR_Reserved;
1761 }
1762
1763 FrameRate DtsInfo::GetFrameRate(void)
1764 {
1765     return FR_Reserved;
1766 }
1767
1768 VideoFormat DtsInfo::GetVideoFormat(void)
1769 {
1770     return VF_Reserved;
1771 }
1772
1773 /*
1774 // implement class Mpeg2Info : ElementaryParse
1775 Mpeg2Info::Mpeg2Info(void)
1776 {
1777         private class Mpeg2Ext : ElementaryParse
1778         {
1779             public Mpeg2Ext(byte[] data, int offset)
1780                 : base()
1781             {
1782                 UInt32 marker = 0xffffffff;
1783                 for (; offset < data.Length - 1; offset++)
1784                 {
1785                     marker = (UInt32)marker << 8;
1786                     marker &= 0xffffff00;
1787                     marker += data[offset];
1788                     if (marker == Constants.MPEG2_SEQ_EXT)
1789                     {
1790                         if((data[offset + 1] & 0xf0) == 0x10)
1791                             break;
1792                     }
1793                 }
1794                 offset++;
1795                 if (offset < data.Length)
1796                 {
1797                     // sequence header
1798                     mData = new byte[data.Length - offset];
1799                     for (int i = 0; offset < data.Length; i++, offset++)
1800                         mData[i] = data[offset];
1801                 }
1802                 else
1803                     mData = null;
1804             }
1805
1806             public override AspectRatio AspectRatio
1807             {
1808                 get { throw new Exception("The method or operation is not implemented."); }
1809             }
1810             public override AudioPresentationType AudioPresentationType
1811             {
1812                 get { throw new Exception("The method or operation is not implemented."); }
1813             }
1814             public override byte[] ElementaryDescriptors
1815             {
1816                 get { throw new Exception("The method or operation is not implemented."); }
1817             }
1818             public override FrameRate FrameRate
1819             {
1820                 get { throw new Exception("The method or operation is not implemented."); }
1821             }
1822             public override SamplingFrequency SamplingFrequency
1823             {
1824                 get { throw new Exception("The method or operation is not implemented."); }
1825             }
1826             public override VideoFormat VideoFormat
1827             {
1828                 get { throw new Exception("The method or operation is not implemented."); }
1829             }
1830             public bool Progressive
1831             {
1832                 get
1833                 {
1834                     indicator = 12;
1835                     if (GetNextBit() == 1)
1836                         return true;
1837                     return false;
1838                 }
1839 }
1840
1841 Mpeg2Info::Mpeg2Info(pByte data, int offset)
1842 {
1843     UInt32 marker = 0xffffffff;
1844     int oldOffset = offset;
1845     for (; offset < sizeof(data.get()); offset++)
1846     {
1847         marker = (UInt32)marker << 8;
1848         marker &= 0xffffff00;
1849         marker += data[offset];
1850         if (marker == Constants::MPEG2_SEQ_CODE)
1851             break;
1852     }
1853     offset++;
1854     if(offset < sizeof(data.get()))
1855     {
1856         // sequence header
1857         mData = pByte(new byte[sizeof(data.get()) - offset]);
1858         for(int i = 0; offset < sizeof(data.get()); i++, offset++)
1859             mData[i] = data[offset];
1860         mpgext = new Mpeg2Ext(data, oldOffset);
1861     }
1862     else
1863         mData.reset();
1864 }
1865
1866 Mpeg2Info::~Mpeg2Info(void)
1867 {
1868     delete mpgext;
1869 }
1870
1871 AspectRatio Mpeg2Info::GetAspectRatio(void)
1872 {
1873     switch (GetAspect())
1874     {
1875     case 0x01:
1876         if (GetVertical() == 1080 || GetVertical() == 1088 || GetVertical() == 720)
1877             return a16_9;
1878         else
1879             return a4_3;
1880     case 0x02:
1881         return a4_3;
1882     case 0x03:
1883         return a16_9;
1884     default:
1885         return AR_Reserved;
1886     }
1887 }
1888
1889 FrameRate Mpeg2Info::GetFrameRate(void)
1890 {
1891     switch (GetFrameRateCode())
1892     {
1893     case 0x01:
1894         return f23_976;
1895     case 0x02:
1896         return f24;
1897     case 0x03:
1898         return f25;
1899     case 0x04:
1900         return f29_97;
1901     case 0x06:
1902         return f50;
1903     case 0x07:
1904         return f59_94;
1905     default:
1906         return FR_Reserved;
1907     }
1908 }
1909
1910 VideoFormat Mpeg2Info::GetVideoFormat(void)
1911 {
1912     if (GetVertical() == 1080 || GetVertical() == 1088)
1913     {
1914         if(GetProgressive())
1915             return p1080;
1916         else
1917             return i1080;
1918     }
1919     else if (GetVertical() == 576)
1920     {
1921     if (GetProgressive())
1922         return p576;
1923     else
1924         return i576;
1925     }
1926     else if (GetVertical() == 720)
1927         return p720;
1928     else if (GetVertical() == 480)
1929     {
1930     if (GetProgressive())
1931         return p480;
1932     else
1933         return i480;
1934     }
1935     return VF_Reserved;
1936 }
1937
1938 pByte Mpeg2Info::GetElementaryDescriptors(void)
1939 {
1940     return GetMpeg2VideoRegistrationDescriptor();
1941 }
1942
1943 AudioPresentationType Mpeg2Info::GetAudioPresentationType(void)
1944 {
1945     return AP_Reserved;
1946 }
1947
1948 SamplingFrequency Mpeg2Info::GetSamplingFrequency(void)
1949 {
1950     return SF_Reserved;
1951 }
1952
1953 pByte Mpeg2Info::GetMpeg2VideoRegistrationDescriptor(void)
1954 {
1955     pByte data = pByte(new byte[10]);
1956     data[0] = 0x05;
1957     data[1] = 0x08;
1958     data[2] = 0x48;
1959     data[3] = 0x44;
1960     data[4] = 0x4d;
1961     data[5] = 0x56;
1962     data[6] = 0xff;
1963     data[7] = 0x02;
1964     data[8] = (byte)((((byte)GetVideoFormat()) << 4) | ((byte)GetFrameRate()));
1965     data[9] = (byte)((((byte)GetAspectRatio()) << 4) | 0x0f);
1966     return data;
1967 }
1968
1969 ushort Mpeg2Info::GetHorizontal(void)
1970 {
1971     indicator = 0;
1972     ushort ret = 0;
1973     for (int i = 0; i < 12; i++)
1974     {
1975         ret <<= 1;
1976         ret |= GetNextBit();
1977     }
1978     return ret;
1979 }
1980
1981 ushort Mpeg2Info::GetVertical(void)
1982 {
1983     indicator = 12;
1984     ushort ret = 0;
1985     for (int i = 0; i < 12; i++)
1986     {
1987         ret <<= 1;
1988         ret |= GetNextBit();
1989     }
1990     return ret;
1991 }
1992
1993 byte Mpeg2Info::GetAspect(void)
1994 {
1995     indicator = 24;
1996     byte ret = 0;
1997     for (int i = 0; i < 4; i++)
1998     {
1999         ret <<= 1;
2000         ret |= GetNextBit();
2001     }
2002     return ret;
2003 }
2004
2005 byte Mpeg2Info::GetFrameRateCode(void)
2006 {
2007     indicator = 28;
2008     byte ret = 0;
2009     for (int i = 0; i < 4; i++)
2010     {
2011         ret <<= 1;
2012         ret |= GetNextBit();
2013     }
2014     return ret;
2015 }
2016
2017 bool Mpeg2Info::GetProgressive(void)
2018 {
2019     if (mpgext.mValid() && mpgext.GetProgressive())
2020         return true;
2021     return false;
2022 }
2023 */
2024
2025 // implement class H264Info : ElementaryParse
2026 H264Info::H264Info(pByte data, int offset)
2027 {
2028     UInt32 marker = 0xffffffff;
2029     for (; offset < sizeof(data.get()); offset++)
2030     {
2031         marker = marker << 8;
2032         marker &= 0xffffff00;
2033         marker += data[offset];
2034         if ((marker & 0xffffff9f) == Constants::H264_PREFIX)
2035         {
2036                     break;
2037         }
2038     }
2039     offset++;
2040     if (offset < sizeof(data.get()))
2041     {
2042         // sequence parameter set
2043         mData = pByte(new byte[sizeof(data.get()) - offset]);
2044         for (int i = 0; offset < sizeof(data.get()); i++, offset++)
2045                     mData[i] = data[offset];
2046     }
2047     else
2048         mData.reset();
2049 }
2050
2051 UInt32 H264Info::GetNextExpGolomb(void)
2052 {
2053     int leadingZeroBits = -1;
2054     byte b = 0;
2055     for (; b == 0; leadingZeroBits++)
2056         b = GetNextBit();
2057     UInt32 codeNum = (UInt32)(1 << leadingZeroBits);
2058     codeNum -= 1;
2059     UInt32 part2 = 0;
2060     for (; leadingZeroBits > 0; leadingZeroBits-- )
2061     {
2062         b = GetNextBit();
2063         part2 = part2 << 1;
2064         part2 |= b;
2065     }
2066     codeNum += part2;
2067     return codeNum;
2068 }
2069
2070 void H264Info::ScalingListSkip(int skip)
2071 {
2072     int lastScale = 8;
2073     int nextScale = 8;
2074     for (int i = 0; i < skip; i++)
2075     {
2076         if (nextScale != 0)
2077         {
2078             int deltaScale = (int)GetNextExpGolomb();
2079             nextScale = (lastScale + deltaScale) % 256;
2080         }
2081     }
2082 }
2083
2084 UInt32 H264Info::GetWidth(void)
2085 {
2086     indicator = 24;
2087     GetNextExpGolomb();
2088     if (mData[0] == 100 || mData[0] == 110 || mData[0] == 122 || mData[0] == 144)
2089     {
2090         UInt32 chroma = GetNextExpGolomb();
2091         if (chroma == 3)
2092         GetNextBit();
2093         GetNextExpGolomb();
2094         GetNextExpGolomb();
2095         GetNextBit();
2096         if (GetNextBit() == 1)
2097         {
2098             for (int i = 0; i < 6; i++)
2099             {
2100                 if (GetNextBit() == 1)
2101                 {
2102                     ScalingListSkip(16);
2103                 }
2104             }
2105             for (int i = 6; i < 8; i++)
2106             {
2107                 if (GetNextBit() == 1)
2108                 {
2109                     ScalingListSkip(64);
2110                 }
2111             }
2112         }
2113     }
2114     GetNextExpGolomb();
2115     UInt32 pic = GetNextExpGolomb();
2116     if (pic == 0)
2117         GetNextExpGolomb();     
2118     else if (pic == 1)
2119     {
2120         GetNextBit();
2121         GetNextExpGolomb();
2122         GetNextExpGolomb();
2123         UInt32 numFrame = GetNextExpGolomb();
2124         for (int i = 0; i < numFrame; i++)
2125             GetNextExpGolomb();
2126     }
2127     GetNextExpGolomb();
2128     GetNextBit();
2129     UInt32 wid = GetNextExpGolomb();
2130     wid++;
2131     wid <<= 4;
2132     return wid;
2133 }
2134
2135 UInt32 H264Info::GetHeigth(void)
2136 {
2137     UInt32 width = GetWidth();
2138     UInt32 height = GetNextExpGolomb();
2139     height++;
2140     height <<= 4;
2141     return height;
2142 }
2143
2144 pByte H264Info::GetHdmvVideoRegistrationDescriptor(void)
2145 {
2146     pByte data = pByte(new byte[10]);
2147     data[0] = 0x05;
2148     data[1] = 0x08;
2149     data[2] = 0x48;
2150     data[3] = 0x44;
2151     data[4] = 0x4d;
2152     data[5] = 0x56;
2153     data[6] = 0xff;
2154     data[7] = 0x1b;
2155     data[8] = (byte)((((byte)GetVideoFormat()) << 4) | ((byte)GetFrameRate()));
2156     data[9] = (byte)((((byte)GetAspectRatio()) << 4) | 0x0f);
2157     return data;
2158 }
2159
2160 VideoFormat H264Info::GetVideoFormat(void)
2161 {
2162     UInt32 h = GetHeigth();
2163     if (h == 1080 || h == 1088)
2164         return p1080;
2165     else if (h == 720)
2166         return p720;
2167     else if (h == 576)
2168         return p576;
2169     else if (h == 480)
2170         return p480;
2171     else if (h == 540 || h == 544)
2172         return i1080;
2173     else if (h == 288)
2174         return i576;
2175     else if (h == 240)
2176         return i480;
2177     else
2178         return VF_Reserved;
2179 }
2180
2181 AspectRatio H264Info::GetAspectRatio(void)
2182 {
2183     if (GetVideoFormat() == i480 || GetVideoFormat() == i576)
2184         return a4_3;
2185     return a16_9;
2186 }
2187
2188 FrameRate H264Info::GetFrameRate(void)
2189 {
2190     if (GetVideoFormat() == p720)
2191         return f59_94;
2192     else if (GetVideoFormat() == p1080 || GetVideoFormat() == p480)
2193         return f23_976;
2194     else if (GetVideoFormat() == p576 || GetVideoFormat() == i576)
2195         return f25;
2196     else
2197         return f29_97;
2198 }
2199
2200 pByte H264Info::GetElementaryDescriptors(void)
2201 {
2202     return GetHdmvVideoRegistrationDescriptor();
2203 }
2204
2205 AudioPresentationType H264Info::GetAudioPresentationType(void)
2206 {
2207     return AP_Reserved;
2208 }
2209
2210 SamplingFrequency H264Info::GetSamplingFrequency(void)
2211 {
2212     return SF_Reserved;
2213 }
2214
2215 // implement class SitPacket : TsTable
2216 SitPacket::SitPacket(void)
2217 {
2218 }
2219
2220 SitPacket::SitPacket(pByte data)
2221 {
2222 }
2223
2224 // implement class PmPacket : TsTable
2225 PmPacket::PmPacket(void)
2226 {
2227     // table id = 02
2228     SetTableId(Constants::PMT_TABLE_ID);
2229     SetLength(13);
2230     // program id = 1
2231     SetProgramNumber(1);
2232     // reserved, PcrPID
2233     SetPcrPID(Constants::DEFAULT_PCR_PID);
2234     // reserved, program info length
2235     SetProgramDescriptorsLength(0);
2236     SetPID(Constants::DEFAULT_PMT_PID);
2237     RefreshCrc();
2238 }
2239
2240 PmPacket::PmPacket(pByte data)throw(std::invalid_argument)
2241 {
2242     if (GetTableId() != Constants::PMT_TABLE_ID)
2243         throw std::invalid_argument("packet does not contain a valid PMT table ID");
2244 }
2245
2246 DTCP_Descriptor PmPacket::GetDtcpInfo(void)
2247 {
2248     pByte descriptors = GetProgramDescriptorsData();
2249     if (!descriptors)
2250         return reinterpret_cast<DTCP_Descriptor>(NULL);
2251     DTCP_Descriptor dt = NULL;
2252
2253     for (int i = 0; i < sizeof(descriptors.get()); )
2254     {
2255         try
2256         {
2257             dt = new DTCP_Descriptor(descriptors, i);
2258             break;
2259         }
2260         catch (std::invalid_argument)
2261         {
2262             i += (2 + descriptors[i + 1]);
2263         }
2264     }
2265
2266     return dt;
2267 }
2268
2269 pByte PmPacket::GetProgramDescriptorsData(void)
2270 {
2271     if (GetProgramDescriptorsLength() == 0)
2272         return NULL;
2273     pByte descriptors = pByte(new byte[GetProgramDescriptorsLength()]);
2274     for (int i = 0; i < sizeof(descriptors.get()); i++)
2275     {
2276         descriptors[i] = mData[i + 17 + GetPointerSize()];
2277     }
2278     return descriptors;
2279 }
2280
2281 void PmPacket::SetProgramDescriptorsData(pByte value)
2282 {
2283     if (!value || 0 == sizeof(value.get()))
2284     {
2285         if (GetProgramDescriptorsLength() > 0)
2286         {
2287             // need to remove existing descriptors
2288             pByte data = pByte(new byte[GetStreamInfoLength()]);
2289             int index = 17 + GetProgramDescriptorsLength() + GetPointerSize();
2290             // copy data between descriptor and crc
2291             for (int i = 0; i < sizeof(data.get()); i++)
2292             {
2293                 data[i] = mData[index + i];
2294                 mData[17 + i + GetPointerSize()] = data[i];
2295             }
2296             SetLength(GetLength() - GetProgramDescriptorsLength());
2297             SetProgramDescriptorsLength(0);
2298             RefreshCrc();
2299         }
2300         else
2301         {
2302             // nothing to do
2303             return;
2304         }
2305     }
2306     else
2307     {
2308         if (sizeof(value.get()) + GetLength() + GetPointerSize() + 5
2309             - GetProgramDescriptorsLength() > Constants::TS_SIZE)
2310             throw std::invalid_argument("program descriptors data too long");
2311         // need to remove existing descriptors
2312         pByte data = pByte(new byte[GetStreamInfoLength()]);
2313         int index = 17 + GetProgramDescriptorsLength() + GetPointerSize();
2314         // copy data between descriptor and crc
2315         for (int i = 0; i < sizeof(data.get()); i++)
2316             data[i] = mData[index + i];
2317         SetLength(GetLength() - GetProgramDescriptorsLength());
2318         SetLength(GetLength() + sizeof(value.get()));
2319         SetProgramDescriptorsLength(sizeof(value.get()));
2320         // copy the new descriptor
2321         for (int i = 0; i < sizeof(value.get()); i++)
2322             mData[17 + i + GetPointerSize()] = value[i];
2323         // recover data between descriptor and crc
2324         for (int i = 0; i < sizeof(data.get()); i++)
2325             mData[17 + sizeof(value.get()) + i + GetPointerSize()] = data[i];
2326         RefreshCrc();
2327     }
2328 }
2329
2330
2331 // implement class BluRayOutput
2332
2333
2334 } // namespace
2335
2336