1 #ifndef TSREMUXCPP_UTILS_H_
2 #define TSREMUXCPP_UTILS_H_
8 #include <boost/shared_array.hpp>
9 // #include "BlueMux.h"
10 #include "tsremuxcpp_define.h"
32 enum ElementaryStreamTypes {
34 VIDEO_STREAM_MPEG1 = 0x01,
35 VIDEO_STREAM_MPEG2 = 0x02,
36 AUDIO_STREAM_MPEG1 = 0x03, // all layers including mp3
37 AUDIO_STREAM_MPEG2 = 0x04,
38 VIDEO_STREAM_H264 = 0x1b,
39 AUDIO_STREAM_LPCM = 0x80,
40 AUDIO_STREAM_AC3 = 0x81,
41 AUDIO_STREAM_DTS = 0x82,
42 AUDIO_STREAM_AC3_TRUE_HD = 0x83,
43 AUDIO_STREAM_AC3_PLUS = 0x84,
44 AUDIO_STREAM_DTS_HD = 0x85,
45 AUDIO_STREAM_DTS_HD_MASTER_AUDIO = 0x86,
46 PRESENTATION_GRAPHICS_STREAM = 0x90,
47 INTERACTIVE_GRAPHICS_STREAM = 0x91,
48 SUBTITLE_STREAM = 0x92,
49 SECONDARY_AUDIO_AC3_PLUS = 0xa1,
50 SECONDARY_AUDIO_DTS_HD = 0xa2,
51 VIDEO_STREAM_VC1 = 0xea
81 enum AudioPresentationType {
89 enum SamplingFrequency {
100 signed long long pts_;
102 EpElement(signed long long pts, unsigned long spn);
107 // static pByte ToArray(pByte vector);
112 StreamInfo(pByte data, int index)throw(std::invalid_argument);
113 StreamInfo(ElementaryStreamTypes streamType, ushort elementaryPid);
114 VideoFormat GetVideoFormat(void);
115 void SetVideoFormat(VideoFormat videoformat);
116 AspectRatio GetAspectRatio(void);
117 void SetAspectRatio(AspectRatio aspectratio);
118 FrameRate GetFrameRate(void);
119 void SetFrameRate(FrameRate frameRate);
120 AudioPresentationType GetAudioPresentationType(void);
121 void SetAudioPresentationType(AudioPresentationType audioPresentationTyp);
122 SamplingFrequency GetSamplingFrequency(void);
123 void SetSamplingFrequency(SamplingFrequency samplingFrequency);
124 ElementaryStreamTypes GetElementaryStreamTypes(void);
125 void SetElementaryStreamTypes(ElementaryStreamTypes stream_type);
126 ushort GetElementaryPID(void);
127 void SetElementaryPID(ushort pid);
128 pByte GetElementaryDescriptors(void);
129 void SetElementaryDescriptors(pByte value)throw(std::invalid_argument);
130 pByte GetByteData(void);
133 VideoFormat mVideoFormat;
134 AspectRatio mAspectRatio;
135 FrameRate mFrameRate;
136 AudioPresentationType mAudioPresentationType;
137 SamplingFrequency mSamplingFrequency;
138 // ElementaryStreamTypes StreamType;
139 // ushort ElementaryPID;
140 // pByte ElementaryDescriptors;
145 BluRayOutput(std::string path, TimeSpan chapterLen);
146 void Author(EpElement* EpInfo, StreamInfo* sis, UInt32 numOfSourcePackets);
147 pByte BuildPlayList(byte* PlayItems);
148 pByte BuildFirstPlayItem(byte stc_id, UInt32 start,
149 UInt32 end, byte* StnTable);
150 pByte BuildFirstPlayMarks(UInt32 start, UInt32 end, UInt32 interval);
151 pByte Build_clpi(byte* ClipInfo, byte* SequenceInfo,
152 byte* ProgramInfo, byte* CPI);
153 pByte BuildClipInfo(UInt32 numOfSourcePackets, EpElement* EpInfo);
154 pByte BuildSequenceInfo(UInt32 start, UInt32 end);
155 pByte BuildProgramInfo(ushort pids, byte* StreamCodingInfos);
156 pByte BuildVideoStreamCodingInfo(ElementaryStreamTypes type,
157 VideoFormat format, FrameRate rate, AspectRatio ratio);
158 pByte BuildAudioStreamCodingInfo(ElementaryStreamTypes type,
159 AudioPresentationType format, SamplingFrequency rate);
160 pByte BuildPgStreamCodingInfo(void);
161 pByte BuildCpi(byte* EpMap);
162 pByte BuildEpMap(EpElement* EpInfo);
164 pByte BuildStreamEntry(ushort pid);
165 pByte BuildVideoStreamAttributes(byte type, VideoFormat vf, FrameRate fr);
166 pByte BuildAudioStreamAttributes(byte type, AudioPresentationType vf,
167 SamplingFrequency fr);
168 pByte BuildStnTable(byte* VideoEntry, byte* VideoAttributes,
169 byte* AudioEntries, byte* AudioAttributes,
170 byte* PgEntries, byte* PgAttributes);
171 pByte UintToByteArraryNetwork(UInt32 value);
172 pByte Build_mlps(byte* PlayList, byte* PlayListMark);
177 Descriptor(pByte data, int startIndex)throw(std::invalid_argument);
180 byte GetLength(void);
185 class DTCP_Descriptor : public Descriptor {
187 DTCP_Descriptor(pByte data, int startIndex)
188 throw(std::invalid_argument);
189 DtcpCci GetCopyStatus(void);
190 bool GetAnalogConstrain(void);
191 bool GetMacrovision(void);
196 static const int TS_PAYLOAD_SIZE;
197 static const int TS_SIZE;
198 static const int STRIDE_SIZE;
199 static const int DISK_BUFFER;
200 static const byte SYNC_BYTE;
201 static const byte PAT_PID;
202 static const byte SIT_PID;
203 static const byte PAT_TABLE_ID;
204 static const byte PMT_TABLE_ID;
205 static const byte DTCP_DESCRIPTOR_TAG;
206 static const byte PACK_ID;
207 static const byte SYS_ID;
208 static const byte MAP_ID;
209 static const byte DIR_ID;
210 static const byte PAD_ID;
213 static const ushort DEFAULT_PMT_PID;
214 static const ushort DEFAULT_VIDEO_PID;
215 static const ushort MAX_VIDEO_PID;
216 static const ushort DEFAULT_AUDIO_PID;
217 static const ushort MAX_AUDIO_PID;
218 static const ushort DEFAULT_PCR_PID;
219 static const ushort DEFAULT_SUBTITLE_PID;
220 static const ushort DEFAULT_PRESENTATION_GRAPHICS_PID;
221 static const ushort DEFAULT_INTERACTIVE_GRAPHICS_PID;
222 static const ushort DEFAULT_PROGRAM_NUMBER;
223 static const int MAX_BUFFER_COUNT;
224 static const int MIN_BUFFER_COUNT;
225 static const Int64 AUDIO_DELAY;
226 static const UInt32 MKVCLUSTER_START;
227 static const UInt32 MKVFILE_START;
228 static const UInt32 MKVSEGMENT_START;
229 static const UInt32 MKVTRACKINFO_START;
230 static const byte MKVTIMECODE_START;
233 static const byte PES_VIDEO;
234 static const byte PES_AUDIO_MPEG;
235 static const byte PES_PRIVATE1;
236 static const byte PES_PADDING;
237 static const byte PES_PRIVATE2;
238 static const byte PES_VIDEO_VC1;
239 static const byte PES_PRIVATE_AC3;
240 static const byte PES_PRIVATE_AC3_PLUS;
241 static const byte PES_PRIVATE_DTS_HD;
242 static const byte PES_PRIVATE_LPCM;
243 static const byte PES_PRIVATE_AC3_TRUE_HD;
244 static const UInt32 VC1_SEQ_SC;
245 static const UInt32 VC1_END_OF_STREAM;
246 static const ushort AC3_SYNC;
247 static const UInt32 H264_PREFIX;
248 static const UInt32 H264_END_OF_STREAM;
249 static const UInt32 DTS_SYNC;
250 static const UInt32 DTS_EXT_SYNC;
251 static const UInt32 MLP_SYNC;
252 static const UInt32 MPEG2_SEQ_CODE;
253 static const UInt32 MPEG2_SEQ_EXT;
254 static const UInt32 MPEG2_SEQ_END;
257 static const Int64 MPEG2TS_CLOCK_RATE;
258 static const Int64 MAX_MPEG2TS_CLOCK;
259 static const Int64 MAX_BLURAY_CLOCK;
260 static const Int64 MAX_FIREWIRE_CLOCK;
261 static const Int64 MAX_PTS_CLOCK;
262 static const Int64 PTS_CLOCK_RATE;
263 static const int MAX_OFFSET;
264 static const int MAX_COUNT;
267 static readonly byte hdmv_registration_descriptor[];
268 static readonly byte copy_control_descriptor[];
269 static readonly byte vc1_descriptor[];
270 static readonly byte ac3_registration_descriptor[];
271 static readonly byte DefaultSitTableOne[];
272 static readonly uint crc_table[];
273 static uint ComputeCrc(pByte data);
274 static uint ComputeCrc(pByte data, int length);
275 static uint ComputeCrc(pByte data, int length, int startIndex);
280 ProgramInfo(pByte data, int index)throw(std::invalid_argument);
281 ProgramInfo(ushort programNumber, ushort programPid);
282 ushort GetProgramNumber(void);
283 void SetProgramNumber(ushort programNumber);
284 ushort GetProgramPID(void);
285 void SetProgramPID(ushort programPID);
294 bool GetPriority(void);
295 void SetPriority(bool priority);
297 void SetPID(ushort pid);
298 byte GetPointerSize(void);
300 void SetData(pByte data, int startIndex)throw(std::invalid_argument);
302 Int64 GetPcr(void)throw(std::out_of_range);
304 void IncrementContinuityCounter(void);
305 byte GetContinuityCounter(void);
306 void SetContinuityCounter(byte value)throw(std::out_of_range);
307 bool HasPesHeader(void);
315 class PcrPacket : TsPacket {
317 PcrPacket(Int64 pcr, byte counter, ushort pid);
320 class TsTable : public TsPacket {
324 void AddData(pByte data, int offset, int len);
326 byte GetTableId(void);
327 void SetTableId(byte value);
328 ushort GetNumberId(void);
329 void SetNumberId(ushort value);
330 ushort GetLength(void);
331 void SetLength(ushort value);
333 void RefreshCrc(void);
336 class PatPacket : public TsTable {
339 PatPacket(pByte data)throw(std::invalid_argument);
340 ushort GetTransportStreamId(void);
341 void SetTransportStreamId(ushort TSId);
342 boost::shared_array<ProgramInfo> GetPrograms(void);
343 void SetPrograms(boost::shared_array<ProgramInfo> programinfo)
344 throw(std::invalid_argument);
345 ushort GetProgramInfoLength(void);
348 class SitPacket : TsTable {
351 SitPacket(byte* data);
354 class PmPacket : TsTable {
357 PmPacket(byte* data);
358 DTCP_Descriptor DtcpInfo;
359 pByte ProgramDescriptorsData;
360 StreamInfo* ElementaryStreams;
361 ushort ProgramNumber;
364 ushort ProgramDescriptorsLength;
365 ushort StreamInfoLength;
370 PesHeader(pByte data)throw(std::invalid_argument);
371 byte GetStreamId(void);
373 void SetByte(int i, byte dat);
374 byte GetHeaderLength(void);
375 int GetTotalHeaderLength(void);
376 ushort GetPacketLength(void);
379 Int64 GetPts(void)throw(std::invalid_argument);
380 void SetPts(Int64 value)throw(std::invalid_argument);
381 Int64 GetDts(void)throw(std::invalid_argument);
382 void SetDts(Int64 value)throw(std::invalid_argument);
383 byte GetExtention2(void);
391 PesPacket(pByte buff, int offset, int length, ushort pid);
392 bool GetPriority(void);
393 void SetPriority(bool priority);
395 pByte GetPayload(void);
397 void SetByte(int i, byte dat);
399 void SetPID(ushort id);
400 bool GetComplete(void);
401 void SetComplete(bool value);
402 boost::shared_ptr<PesHeader> GetHeader(void);
403 void AddData(pByte moredata);
404 void AddData(pByte buff, int offset, int length);
405 byte GetBaseId(void);
406 byte GetExtendedId(void);
407 UInt32 GetExtendedType(void);
414 class VC1SequenceInfo {
416 VC1SequenceInfo(pByte data, int offset);
420 bool Interlaced(void);
421 bool DisplayExt(void);
422 bool AspectFlag(void);
423 byte GetVc1AspectRatio(void);
424 bool FrameFlag(void);
425 bool FrameRateIndicatorFlag(void);
426 AspectRatio GetAspectRatio(void);
427 VideoFormat GetVideoFormat(void);
428 FrameRate GetFrameRate(void);
440 class ElementaryParse {
442 ElementaryParse(void);
443 virtual VideoFormat GetVideoFormati(void);
444 virtual void SetVideoFormat(VideoFormat videoformat);
445 virtual AspectRatio GetAspectRatio(void);
446 virtual void SetAspectRatio(AspectRatio aspectratio);
447 virtual FrameRate GetFrameRate(void);
448 virtual void SetFrameRate(FrameRate frameRate);
449 virtual AudioPresentationType GetAudioPresentationType(void);
450 virtual void SetAudioPresentationType(
451 AudioPresentationType audioPresentationTyp);
452 virtual SamplingFrequency GetsamplingFrequency(void);
453 virtual void SetSamplingFrequency(SamplingFrequency samplingFrequency);
454 virtual pByte GetElementaryDescriptors(void);
456 byte GetNextBit(void);
460 VideoFormat mVideoFormat;
461 FrameRate mFrameRate;
462 AspectRatio mAspectRatio;
463 SamplingFrequency mSamplingFrequency;
464 AudioPresentationType mAudioPresentationType;
465 pByte ElementaryDescriptors;
468 class H264Info : ElementaryParse {
470 H264Info(pByte data, int offset);
471 pByte ElementaryDescriptors;
472 VideoFormat GetVideoFormati(void);
473 void SetVideoFormat(VideoFormat videoformat);
474 AspectRatio GetAspectRatio(void);
475 void SetAspectRatio(AspectRatio aspectratio);
476 FrameRate GetFrameRate(void);
477 void SetFrameRate(FrameRate frameRate);
478 AudioPresentationType GetAudioPresentationType(void);
479 void SetAudioPresentationType(AudioPresentationType audioPresentationTyp);
480 SamplingFrequency GetsamplingFrequency(void);
481 void SetSamplingFrequency(SamplingFrequency samplingFrequency);
483 UInt32 GetNextExpGolomb();
484 void ScalingListSkip(int skip);
487 pByte HdmvVideoRegistrationDescriptor;
488 VideoFormat mVideoFormat;
489 AspectRatio mAspectRatio;
490 FrameRate mFrameRate;
491 AudioPresentationType mAudioPresentationType;
492 SamplingFrequency mSamplingFrequency;
495 class AC3Info : ElementaryParse {
499 bool IndependentStream;
500 Ac3SyntaxType SyntaxType;
501 AC3Info(pByte data, int offset);
502 VideoFormat GetVideoFormati(void);
503 void SetVideoFormat(VideoFormat videoformat);
504 AspectRatio GetAspectRatio(void);
505 void SetAspectRatio(AspectRatio aspectratio);
506 FrameRate GetFrameRate(void);
507 void SetFrameRate(FrameRate frameRate);
508 AudioPresentationType GetAudioPresentationType(void);
509 void SetAudioPresentationType(AudioPresentationType audioPresentationTyp);
510 SamplingFrequency GetsamplingFrequency(void);
511 void SetSamplingFrequency(SamplingFrequency samplingFrequency);
512 pByte GetElementaryDescriptors(void);
514 static readonly int* len48k;
515 static readonly int* len44k;
516 static readonly int* len32k;
522 class DtsInfo : ElementaryParse {
524 DtsInfo(pByte data, int offset);
525 VideoFormat GetVideoFormati(void);
526 void SetVideoFormat(VideoFormat videoformat);
527 AspectRatio GetAspectRatio(void);
528 void SetAspectRatio(AspectRatio aspectratio);
529 FrameRate GetFrameRate(void);
530 void SetFrameRate(FrameRate frameRate);
531 AudioPresentationType GetAudioPresentationType(void);
532 void SetAudioPresentationType( AudioPresentationType audioPresentationTyp);
533 SamplingFrequency GetsamplingFrequency(void);
534 void SetSamplingFrequency(SamplingFrequency samplingFrequency);
535 pByte GetElementaryDescriptors(void);
544 class MlpInfo : ElementaryParse {
546 MlpInfo(pByte data, int offset);
547 VideoFormat GetVideoFormati(void);
548 void SetVideoFormat(VideoFormat videoformat);
549 AspectRatio GetAspectRatio(void);
550 void SetAspectRatio(AspectRatio aspectratio);
551 FrameRate GetFrameRate(void);
552 void SetFrameRate(FrameRate frameRate);
553 AudioPresentationType GetAudioPresentationType(void);
554 void SetAudioPresentationType(AudioPresentationType audioPresentationTyp);
555 SamplingFrequency GetsamplingFrequency(void);
556 void SetSamplingFrequency(SamplingFrequency samplingFrequency);
557 pByte GetElementaryDescriptors(void);
559 pByte AC3AudioDescriptor;
563 class Mpeg2Info : ElementaryParse {
565 Mpeg2Info(pByte data, int offset);
566 VideoFormat GetVideoFormati(void);
567 void SetVideoFormat(VideoFormat videoformat);
568 AspectRatio GetAspectRatio(void);
569 void SetAspectRatio(AspectRatio aspectratio);
570 FrameRate GetFrameRate(void);
571 void SetFrameRate(FrameRate frameRate);
572 AudioPresentationType GetAudioPresentationType(void);
573 void SetAudioPresentationType(AudioPresentationType audioPresentationTyp);
574 SamplingFrequency GetsamplingFrequency(void);
575 void SetSamplingFrequency(SamplingFrequency samplingFrequency);
576 pByte GetElementaryDescriptors(void);
578 pByte Mpeg2VideoRegistrationDescriptor;
587 #endif TSREMUXCPP_UTILS_H_