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(Bytes vector);
111 StreamInfo(pByte data, int index)throw(std::invalid_argument);
112 StreamInfo(ElementaryStreamTypes streamType, ushort elementaryPid);
113 VideoFormat GetVideoFormat(void);
114 void SetVideoFormat(VideoFormat videoformat);
115 AspectRatio GetAspectRatio(void);
116 void SetAspectRatio(AspectRatio aspectratio);
117 FrameRate GetFrameRate(void);
118 void SetFrameRate(FrameRate frameRate);
119 AudioPresentationType GetAudioPresentationType(void);
120 void SetAudioPresentationType(AudioPresentationType audioPresentationTyp);
121 SamplingFrequency GetSamplingFrequency(void);
122 void SetSamplingFrequency(SamplingFrequency samplingFrequency);
123 ElementaryStreamTypes GetElementaryStreamTypes(void);
124 void SetElementaryStreamTypes(ElementaryStreamTypes stream_type);
125 ushort GetElementaryPID(void);
126 void SetElementaryPID(ushort pid);
127 pByte GetElementaryDescriptors(void);
128 void SetElementaryDescriptors(pByte value)throw(std::invalid_argument);
129 pByte GetByteData(void);
132 VideoFormat mVideoFormat;
133 AspectRatio mAspectRatio;
134 FrameRate mFrameRate;
135 AudioPresentationType mAudioPresentationType;
136 SamplingFrequency mSamplingFrequency;
137 // ElementaryStreamTypes StreamType;
138 // ushort ElementaryPID;
139 // Bytes ElementaryDescriptors;
144 BluRayOutput(std::string path, TimeSpan chapterLen);
145 void Author(EpElement* EpInfo, StreamInfo* sis, UInt32 numOfSourcePackets);
146 pByte BuildPlayList(byte* PlayItems);
147 pByte BuildFirstPlayItem(byte stc_id, UInt32 start,
148 UInt32 end, byte* StnTable);
149 pByte BuildFirstPlayMarks(UInt32 start, UInt32 end, UInt32 interval);
150 pByte Build_clpi(byte* ClipInfo, byte* SequenceInfo,
151 byte* ProgramInfo, byte* CPI);
152 pByte BuildClipInfo(UInt32 numOfSourcePackets, EpElement* EpInfo);
153 pByte BuildSequenceInfo(UInt32 start, UInt32 end);
154 pByte BuildProgramInfo(ushort pids, byte* StreamCodingInfos);
155 pByte BuildVideoStreamCodingInfo(ElementaryStreamTypes type,
156 VideoFormat format, FrameRate rate, AspectRatio ratio);
157 pByte BuildAudioStreamCodingInfo(ElementaryStreamTypes type,
158 AudioPresentationType format, SamplingFrequency rate);
159 pByte BuildPgStreamCodingInfo(void);
160 pByte BuildCpi(byte* EpMap);
161 pByte BuildEpMap(EpElement* EpInfo);
163 pByte BuildStreamEntry(ushort pid);
164 pByte BuildVideoStreamAttributes(byte type, VideoFormat vf, FrameRate fr);
165 pByte BuildAudioStreamAttributes(byte type, AudioPresentationType vf,
166 SamplingFrequency fr);
167 pByte BuildStnTable(byte* VideoEntry, byte* VideoAttributes,
168 byte* AudioEntries, byte* AudioAttributes,
169 byte* PgEntries, byte* PgAttributes);
170 pByte UintToByteArraryNetwork(UInt32 value);
171 pByte Build_mlps(byte* PlayList, byte* PlayListMark);
176 Descriptor(pByte data, int startIndex)throw(std::invalid_argument);
179 byte GetLength(void);
184 class DTCP_Descriptor : Descriptor {
186 DTCP_Descriptor(pByte data, int startIndex);
191 static const int TS_PAYLOAD_SIZE;
192 static const int TS_SIZE;
193 static const int STRIDE_SIZE;
194 static const int DISK_BUFFER;
195 static const byte SYNC_BYTE;
196 static const byte PAT_PID;
197 static const byte SIT_PID;
198 static const byte PAT_TABLE_ID;
199 static const byte PMT_TABLE_ID;
200 static const byte DTCP_DESCRIPTOR_TAG;
201 static const byte PACK_ID;
202 static const byte SYS_ID;
203 static const byte MAP_ID;
204 static const byte DIR_ID;
205 static const byte PAD_ID;
208 static const ushort DEFAULT_PMT_PID;
209 static const ushort DEFAULT_VIDEO_PID;
210 static const ushort MAX_VIDEO_PID;
211 static const ushort DEFAULT_AUDIO_PID;
212 static const ushort MAX_AUDIO_PID;
213 static const ushort DEFAULT_PCR_PID;
214 static const ushort DEFAULT_SUBTITLE_PID;
215 static const ushort DEFAULT_PRESENTATION_GRAPHICS_PID;
216 static const ushort DEFAULT_INTERACTIVE_GRAPHICS_PID;
217 static const ushort DEFAULT_PROGRAM_NUMBER;
218 static const int MAX_BUFFER_COUNT;
219 static const int MIN_BUFFER_COUNT;
220 static const Int64 AUDIO_DELAY;
221 static const UInt32 MKVCLUSTER_START;
222 static const UInt32 MKVFILE_START;
223 static const UInt32 MKVSEGMENT_START;
224 static const UInt32 MKVTRACKINFO_START;
225 static const byte MKVTIMECODE_START;
228 static const byte PES_VIDEO;
229 static const byte PES_AUDIO_MPEG;
230 static const byte PES_PRIVATE1;
231 static const byte PES_PADDING;
232 static const byte PES_PRIVATE2;
233 static const byte PES_VIDEO_VC1;
234 static const byte PES_PRIVATE_AC3;
235 static const byte PES_PRIVATE_AC3_PLUS;
236 static const byte PES_PRIVATE_DTS_HD;
237 static const byte PES_PRIVATE_LPCM;
238 static const byte PES_PRIVATE_AC3_TRUE_HD;
239 static const UInt32 VC1_SEQ_SC;
240 static const UInt32 VC1_END_OF_STREAM;
241 static const ushort AC3_SYNC;
242 static const UInt32 H264_PREFIX;
243 static const UInt32 H264_END_OF_STREAM;
244 static const UInt32 DTS_SYNC;
245 static const UInt32 DTS_EXT_SYNC;
246 static const UInt32 MLP_SYNC;
247 static const UInt32 MPEG2_SEQ_CODE;
248 static const UInt32 MPEG2_SEQ_EXT;
249 static const UInt32 MPEG2_SEQ_END;
252 static const Int64 MPEG2TS_CLOCK_RATE;
253 static const Int64 MAX_MPEG2TS_CLOCK;
254 static const Int64 MAX_BLURAY_CLOCK;
255 static const Int64 MAX_FIREWIRE_CLOCK;
256 static const Int64 MAX_PTS_CLOCK;
257 static const Int64 PTS_CLOCK_RATE;
258 static const int MAX_OFFSET;
259 static const int MAX_COUNT;
262 static readonly byte hdmv_registration_descriptor[];
263 static readonly byte copy_control_descriptor[];
264 static readonly byte vc1_descriptor[];
265 static readonly byte ac3_registration_descriptor[];
266 static readonly byte DefaultSitTableOne[];
267 static readonly uint crc_table[];
268 static uint ComputeCrc(pByte data);
269 static uint ComputeCrc(pByte data, int length);
270 static uint ComputeCrc(pByte data, int length, int startIndex);
275 ProgramInfo(pByte data, int index);
276 ProgramInfo(ushort programNumber, ushort programPid);
277 ushort ProgramNumber;
287 bool GetPriority(void);
288 void SetPriority(bool priority);
290 void SetPID(ushort pid);
291 byte GetPointerSize(void);
293 void SetData(pByte data, int startIndex)throw(std::invalid_argument);
295 Int64 GetPcr(void)throw(std::out_of_range);
297 void IncrementContinuityCounter(void);
298 byte GetContinuityCounter(void);
299 void SetContinuityCounter(byte value)throw(std::out_of_range);
300 bool HasPesHeader(void);
308 class PcrPacket : TsPacket {
310 PcrPacket(Int64 pcr, byte counter, ushort pid);
313 class TsTable : TsPacket {
317 void AddData(pByte data, int offset, int len);
319 byte GetTableId(void);
320 void SetTableId(byte value);
321 ushort GetNumberId(void);
322 void SetNumberId(ushort value);
323 ushort GetLength(void);
324 void SetLength(ushort value);
326 void RefreshCrc(void);
329 class PatPacket : TsTable {
332 PatPacket(pByte data);
333 ushort TransportStreamId;
334 ProgramInfo* Programs;
336 ushort ProgramInfoLength;
339 class SitPacket : TsTable {
342 SitPacket(byte* data);
345 class PmPacket : TsTable {
348 PmPacket(byte* data);
349 DTCP_Descriptor DtcpInfo;
350 Bytes ProgramDescriptorsData;
351 StreamInfo* ElementaryStreams;
352 ushort ProgramNumber;
355 ushort ProgramDescriptorsLength;
356 ushort StreamInfoLength;
361 PesHeader(pByte data)throw(std::invalid_argument);
362 byte GetStreamId(void);
364 void SetByte(int i, byte dat);
365 byte GetHeaderLength(void);
366 int GetTotalHeaderLength(void);
367 ushort GetPacketLength(void);
370 Int64 GetPts(void)throw(std::invalid_argument);
371 void SetPts(Int64 value)throw(std::invalid_argument);
372 Int64 GetDts(void)throw(std::invalid_argument);
373 void SetDts(Int64 value)throw(std::invalid_argument);
374 byte GetExtention2(void);
382 PesPacket(pByte buff, int offset, int length, ushort pid);
383 bool GetPriority(void);
384 void SetPriority(bool priority);
386 pByte GetPayload(void);
388 void SetByte(byte dat);
390 void SetPID(ushort id);
391 bool GetComplete(void);
392 void SetComplete(bool value);
393 boost::shared_ptr<PesHeader> GetHeader(void);
394 void AddData(pByte moredata);
395 void AddData(pByte buff, int offset, int length);
396 byte GetBaseId(void);
397 byte GetExtendedId(void);
398 UInt32 GetExtendedType(void);
405 class VC1SequenceInfo {
407 VC1SequenceInfo(pByte data, int offset);
411 bool Interlaced(void);
412 bool DisplayExt(void);
413 bool AspectFlag(void);
414 byte GetVc1AspectRatio(void);
415 bool FrameFlag(void);
416 bool FrameRateIndicatorFlag(void);
417 AspectRatio GetAspectRatio(void);
418 VideoFormat GetVideoFormat(void);
419 FrameRate GetFrameRate(void);
431 class ElementaryParse {
433 ElementaryParse(void);
434 virtual VideoFormat GetVideoFormati(void);
435 virtual void SetVideoFormat(VideoFormat videoformat);
436 virtual AspectRatio GetAspectRatio(void);
437 virtual void SetAspectRatio(AspectRatio aspectratio);
438 virtual FrameRate GetFrameRate(void);
439 virtual void SetFrameRate(FrameRate frameRate);
440 virtual AudioPresentationType GetAudioPresentationType(void);
441 virtual void SetAudioPresentationType(
442 AudioPresentationType audioPresentationTyp);
443 virtual SamplingFrequency GetsamplingFrequency(void);
444 virtual void SetSamplingFrequency(SamplingFrequency samplingFrequency);
445 virtual pByte GetElementaryDescriptors(void);
447 byte GetNextBit(void);
451 VideoFormat mVideoFormat;
452 FrameRate mFrameRate;
453 AspectRatio mAspectRatio;
454 SamplingFrequency mSamplingFrequency;
455 AudioPresentationType mAudioPresentationType;
456 Bytes ElementaryDescriptors;
459 class H264Info : ElementaryParse {
461 H264Info(pByte data, int offset);
462 Bytes ElementaryDescriptors;
463 VideoFormat GetVideoFormati(void);
464 void SetVideoFormat(VideoFormat videoformat);
465 AspectRatio GetAspectRatio(void);
466 void SetAspectRatio(AspectRatio aspectratio);
467 FrameRate GetFrameRate(void);
468 void SetFrameRate(FrameRate frameRate);
469 AudioPresentationType GetAudioPresentationType(void);
470 void SetAudioPresentationType(AudioPresentationType audioPresentationTyp);
471 SamplingFrequency GetsamplingFrequency(void);
472 void SetSamplingFrequency(SamplingFrequency samplingFrequency);
474 UInt32 GetNextExpGolomb();
475 void ScalingListSkip(int skip);
478 Bytes HdmvVideoRegistrationDescriptor;
479 VideoFormat mVideoFormat;
480 AspectRatio mAspectRatio;
481 FrameRate mFrameRate;
482 AudioPresentationType mAudioPresentationType;
483 SamplingFrequency mSamplingFrequency;
486 class AC3Info : ElementaryParse {
490 bool IndependentStream;
491 Ac3SyntaxType SyntaxType;
492 AC3Info(pByte data, int offset);
493 VideoFormat GetVideoFormati(void);
494 void SetVideoFormat(VideoFormat videoformat);
495 AspectRatio GetAspectRatio(void);
496 void SetAspectRatio(AspectRatio aspectratio);
497 FrameRate GetFrameRate(void);
498 void SetFrameRate(FrameRate frameRate);
499 AudioPresentationType GetAudioPresentationType(void);
500 void SetAudioPresentationType(AudioPresentationType audioPresentationTyp);
501 SamplingFrequency GetsamplingFrequency(void);
502 void SetSamplingFrequency(SamplingFrequency samplingFrequency);
503 pByte GetElementaryDescriptors(void);
505 static readonly int* len48k;
506 static readonly int* len44k;
507 static readonly int* len32k;
513 class DtsInfo : ElementaryParse {
515 DtsInfo(pByte data, int offset);
516 VideoFormat GetVideoFormati(void);
517 void SetVideoFormat(VideoFormat videoformat);
518 AspectRatio GetAspectRatio(void);
519 void SetAspectRatio(AspectRatio aspectratio);
520 FrameRate GetFrameRate(void);
521 void SetFrameRate(FrameRate frameRate);
522 AudioPresentationType GetAudioPresentationType(void);
523 void SetAudioPresentationType( AudioPresentationType audioPresentationTyp);
524 SamplingFrequency GetsamplingFrequency(void);
525 void SetSamplingFrequency(SamplingFrequency samplingFrequency);
526 pByte GetElementaryDescriptors(void);
535 class MlpInfo : ElementaryParse {
537 MlpInfo(pByte data, int offset);
538 VideoFormat GetVideoFormati(void);
539 void SetVideoFormat(VideoFormat videoformat);
540 AspectRatio GetAspectRatio(void);
541 void SetAspectRatio(AspectRatio aspectratio);
542 FrameRate GetFrameRate(void);
543 void SetFrameRate(FrameRate frameRate);
544 AudioPresentationType GetAudioPresentationType(void);
545 void SetAudioPresentationType(AudioPresentationType audioPresentationTyp);
546 SamplingFrequency GetsamplingFrequency(void);
547 void SetSamplingFrequency(SamplingFrequency samplingFrequency);
548 pByte GetElementaryDescriptors(void);
550 Bytes AC3AudioDescriptor;
554 class Mpeg2Info : ElementaryParse {
556 Mpeg2Info(pByte data, int offset);
557 VideoFormat GetVideoFormati(void);
558 void SetVideoFormat(VideoFormat videoformat);
559 AspectRatio GetAspectRatio(void);
560 void SetAspectRatio(AspectRatio aspectratio);
561 FrameRate GetFrameRate(void);
562 void SetFrameRate(FrameRate frameRate);
563 AudioPresentationType GetAudioPresentationType(void);
564 void SetAudioPresentationType(AudioPresentationType audioPresentationTyp);
565 SamplingFrequency GetsamplingFrequency(void);
566 void SetSamplingFrequency(SamplingFrequency samplingFrequency);
567 pByte GetElementaryDescriptors(void);
569 Bytes Mpeg2VideoRegistrationDescriptor;
578 #endif TSREMUXCPP_UTILS_H_