1 #ifndef TSREMUXCPP_UTILS_H_
2 #define TSREMUXCPP_UTILS_H_
9 #include <boost/shared_array.hpp>
10 #include <boost/filesystem.hpp>
11 #include "tsremuxcpp_define.h"
33 enum ElementaryStreamTypes {
35 VIDEO_STREAM_MPEG1 = 0x01,
36 VIDEO_STREAM_MPEG2 = 0x02,
37 AUDIO_STREAM_MPEG1 = 0x03, // all layers including mp3
38 AUDIO_STREAM_MPEG2 = 0x04,
39 VIDEO_STREAM_H264 = 0x1b,
40 AUDIO_STREAM_LPCM = 0x80,
41 AUDIO_STREAM_AC3 = 0x81,
42 AUDIO_STREAM_DTS = 0x82,
43 AUDIO_STREAM_AC3_TRUE_HD = 0x83,
44 AUDIO_STREAM_AC3_PLUS = 0x84,
45 AUDIO_STREAM_DTS_HD = 0x85,
46 AUDIO_STREAM_DTS_HD_MASTER_AUDIO = 0x86,
47 PRESENTATION_GRAPHICS_STREAM = 0x90,
48 INTERACTIVE_GRAPHICS_STREAM = 0x91,
49 SUBTITLE_STREAM = 0x92,
50 SECONDARY_AUDIO_AC3_PLUS = 0xa1,
51 SECONDARY_AUDIO_DTS_HD = 0xa2,
52 VIDEO_STREAM_VC1 = 0xea
82 enum AudioPresentationType {
90 enum SamplingFrequency {
101 signed long long pts_;
103 EpElement(signed long long pts, unsigned long spn);
108 static pByte ToString(std::vector<byte> vector);
109 static void AddRange(std::vector<byte>& new_vctr, const byte* str);
110 static void AddRange(std::vector<byte>& new_vctr, const pByte str);
111 static void AddRange(std::vector<byte>& new_vctr, const std::vector<byte>& old_vctr);
116 static const int DATA_HEADER;
117 StreamInfo(pByte data, int index)throw(std::invalid_argument);
118 StreamInfo(ElementaryStreamTypes streamType, ushort elementaryPid);
119 VideoFormat GetVideoFormat(void);
120 void SetVideoFormat(VideoFormat videoformat);
121 AspectRatio GetAspectRatio(void);
122 void SetAspectRatio(AspectRatio aspectratio);
123 FrameRate GetFrameRate(void);
124 void SetFrameRate(FrameRate frameRate);
125 AudioPresentationType GetAudioPresentationType(void);
126 void SetAudioPresentationType(AudioPresentationType audioPresentationTyp);
127 SamplingFrequency GetSamplingFrequency(void);
128 void SetSamplingFrequency(SamplingFrequency samplingFrequency);
129 ElementaryStreamTypes GetElementaryStreamTypes(void);
130 void SetElementaryStreamTypes(ElementaryStreamTypes stream_type);
131 ushort GetElementaryPID(void);
132 void SetElementaryPID(ushort pid);
133 pByte GetElementaryDescriptors(void);
134 void SetElementaryDescriptors(pByte value)throw(std::invalid_argument);
135 pByte GetByteData(void);
138 VideoFormat mVideoFormat;
139 AspectRatio mAspectRatio;
140 FrameRate mFrameRate;
141 AudioPresentationType mAudioPresentationType;
142 SamplingFrequency mSamplingFrequency;
143 // ElementaryStreamTypes StreamType;
144 // ushort ElementaryPID;
145 // pByte ElementaryDescriptors;
150 static readonly byte index_bdmv[];
151 static readonly byte MovieObject_bdmv[];
152 static readonly byte PlayList_00000_mpls[];
153 static readonly byte AppInfoPlayList[];
154 static readonly byte PgStreamAttributes[];
155 static readonly byte ClipInfo_0000_clpi[];
156 static readonly byte TsTypeInfoBlock[];
157 BluRayOutput(std::string path, TimeSpan chapterLen);
158 void Author(std::vector<EpElement>& EpInfo, std::vector<StreamInfo>& sis, UInt32 numOfSourcePackets);
159 pByte BuildPlayList(std::vector<pByte> PlayItems);
160 pByte BuildFirstPlayItem(byte stc_id, UInt32 start, UInt32 end, pByte StnTable);
161 pByte BuildFirstPlayMarks(UInt32 start, UInt32 end, UInt32 interval);
162 pByte Build_clpi(pByte ClipInfo, pByte SequenceInfo, pByte ProgramInfo, pByte CPI);
163 pByte BuildClipInfo(UInt32 numOfSourcePackets, std::vector<EpElement> EpInfo);
164 pByte BuildSequenceInfo(UInt32 start, UInt32 end);
165 pByte BuildProgramInfo(std::vector<ushort> pids, std::vector<pByte> StreamCodingInfos);
166 pByte BuildVideoStreamCodingInfo(ElementaryStreamTypes type, VideoFormat format, FrameRate rate, AspectRatio ratio);
167 pByte BuildAudioStreamCodingInfo(ElementaryStreamTypes type, AudioPresentationType format, SamplingFrequency rate);
168 pByte BuildPgStreamCodingInfo(void);
169 pByte BuildCpi(pByte EpMap);
170 pByte BuildEpMap(std::vector<EpElement>& EpInfo);
172 pByte BuildStreamEntry(ushort pid);
173 pByte BuildVideoStreamAttributes(byte type, VideoFormat vf, FrameRate fr)throw(std::invalid_argument);
174 pByte BuildAudioStreamAttributes(byte type, AudioPresentationType vf, SamplingFrequency fr)
175 throw(std::invalid_argument);
176 pByte BuildStnTable(pByte VideoEntry, pByte VideoAttributes,
177 std::vector<pByte>& AudioEntries, std::vector<pByte>& AudioAttributes,
178 std::vector<pByte>& PgEntries, std::vector<pByte>& PgAttributes);
179 pByte UintToByteArraryNetwork(UInt32 value);
180 pByte Build_mlps(pByte PlayList, pByte PlayListMark);
187 Descriptor(pByte data, int startIndex)throw(std::invalid_argument);
190 byte GetLength(void);
195 class DTCP_Descriptor : public Descriptor {
197 DTCP_Descriptor(pByte data, int startIndex)
198 throw(std::invalid_argument);
199 DtcpCci GetCopyStatus(void);
200 bool GetAnalogConstrain(void);
201 bool GetMacrovision(void);
206 static const int TS_PAYLOAD_SIZE;
207 static const int TS_SIZE;
208 static const int STRIDE_SIZE;
209 static const int DISK_BUFFER;
210 static const byte SYNC_BYTE;
211 static const byte PAT_PID;
212 static const byte SIT_PID;
213 static const byte PAT_TABLE_ID;
214 static const byte PMT_TABLE_ID;
215 static const byte DTCP_DESCRIPTOR_TAG;
216 static const byte PACK_ID;
217 static const byte SYS_ID;
218 static const byte MAP_ID;
219 static const byte DIR_ID;
220 static const byte PAD_ID;
223 static const ushort DEFAULT_PMT_PID;
224 static const ushort DEFAULT_VIDEO_PID;
225 static const ushort MAX_VIDEO_PID;
226 static const ushort DEFAULT_AUDIO_PID;
227 static const ushort MAX_AUDIO_PID;
228 static const ushort DEFAULT_PCR_PID;
229 static const ushort DEFAULT_SUBTITLE_PID;
230 static const ushort DEFAULT_PRESENTATION_GRAPHICS_PID;
231 static const ushort DEFAULT_INTERACTIVE_GRAPHICS_PID;
232 static const ushort DEFAULT_PROGRAM_NUMBER;
233 static const int MAX_BUFFER_COUNT;
234 static const int MIN_BUFFER_COUNT;
235 static const Int64 AUDIO_DELAY;
236 static const UInt32 MKVCLUSTER_START;
237 static const UInt32 MKVFILE_START;
238 static const UInt32 MKVSEGMENT_START;
239 static const UInt32 MKVTRACKINFO_START;
240 static const byte MKVTIMECODE_START;
243 static const byte PES_VIDEO;
244 static const byte PES_AUDIO_MPEG;
245 static const byte PES_PRIVATE1;
246 static const byte PES_PADDING;
247 static const byte PES_PRIVATE2;
248 static const byte PES_VIDEO_VC1;
249 static const byte PES_PRIVATE_AC3;
250 static const byte PES_PRIVATE_AC3_PLUS;
251 static const byte PES_PRIVATE_DTS_HD;
252 static const byte PES_PRIVATE_LPCM;
253 static const byte PES_PRIVATE_AC3_TRUE_HD;
254 static const UInt32 VC1_SEQ_SC;
255 static const UInt32 VC1_END_OF_STREAM;
256 static const ushort AC3_SYNC;
257 static const UInt32 H264_PREFIX;
258 static const UInt32 H264_END_OF_STREAM;
259 static const UInt32 DTS_SYNC;
260 static const UInt32 DTS_EXT_SYNC;
261 static const UInt32 MLP_SYNC;
262 static const UInt32 MPEG2_SEQ_CODE;
263 static const UInt32 MPEG2_SEQ_EXT;
264 static const UInt32 MPEG2_SEQ_END;
267 static const Int64 MPEG2TS_CLOCK_RATE;
268 static const Int64 MAX_MPEG2TS_CLOCK;
269 static const Int64 MAX_BLURAY_CLOCK;
270 static const Int64 MAX_FIREWIRE_CLOCK;
271 static const Int64 MAX_PTS_CLOCK;
272 static const Int64 PTS_CLOCK_RATE;
273 static const int MAX_OFFSET;
274 static const int MAX_COUNT;
277 static readonly byte hdmv_registration_descriptor[];
278 static readonly byte copy_control_descriptor[];
279 static readonly byte vc1_descriptor[];
280 static readonly byte ac3_registration_descriptor[];
281 static readonly byte DefaultSitTableOne[];
282 static readonly uint crc_table[];
283 static uint ComputeCrc(pByte data);
284 static uint ComputeCrc(pByte data, int length);
285 static uint ComputeCrc(pByte data, int length, int startIndex);
290 ProgramInfo(pByte data, int index)throw(std::invalid_argument);
291 ProgramInfo(ushort programNumber, ushort programPid);
292 ushort GetProgramNumber(void);
293 void SetProgramNumber(ushort programNumber);
294 ushort GetProgramPID(void);
295 void SetProgramPID(ushort programPID);
304 bool GetPriority(void);
305 void SetPriority(bool priority);
307 void SetPID(ushort pid);
308 byte GetPointerSize(void);
310 void SetData(pByte data, int startIndex)throw(std::invalid_argument);
312 Int64 GetPcr(void)throw(std::out_of_range);
314 void IncrementContinuityCounter(void);
315 byte GetContinuityCounter(void);
316 void SetContinuityCounter(byte value)throw(std::out_of_range);
317 bool HasPesHeader(void);
325 class PcrPacket : TsPacket {
327 PcrPacket(Int64 pcr, byte counter, ushort pid);
330 class TsTable : public TsPacket {
334 void AddData(pByte data, int offset, int len);
336 byte GetTableId(void);
337 void SetTableId(byte value);
338 ushort GetNumberId(void);
339 void SetNumberId(ushort value);
340 ushort GetLength(void);
341 void SetLength(ushort value);
343 void RefreshCrc(void);
346 class PatPacket : public TsTable {
349 PatPacket(pByte data)throw(std::invalid_argument);
350 ushort GetTransportStreamId(void);
351 void SetTransportStreamId(ushort TSId);
352 boost::shared_array<ProgramInfo> GetPrograms(void);
353 void SetPrograms(boost::shared_array<ProgramInfo> programinfo)
354 throw(std::invalid_argument);
355 ushort GetProgramInfoLength(void);
358 class SitPacket : TsTable {
361 SitPacket(pByte data);
364 class PmtPacket : TsTable {
367 PmtPacket(pByte data)throw(std::invalid_argument);
368 boost::shared_array<DTCP_Descriptor> GetDtcpInfo(void);
369 pByte GetProgramDescriptorsData(void);
370 void SetProgramDescriptorsData(pByte value);
371 ushort GetProgramDescriptorsLength(void);
372 void SetProgramDescriptorsLength(ushort value);
373 ushort GetProgramNumber(void);
374 void SetProgramNumber(ushort id);
375 ushort GetPcrPID(void);
376 void SetPcrPID(ushort pid);
377 ushort GetStreamInfoLength(void);
384 PesHeader(pByte data)throw(std::invalid_argument);
385 byte GetStreamId(void);
387 void SetByte(int i, byte dat);
388 byte GetHeaderLength(void);
389 int GetTotalHeaderLength(void);
390 ushort GetPacketLength(void);
393 Int64 GetPts(void)throw(std::invalid_argument);
394 void SetPts(Int64 value)throw(std::invalid_argument);
395 Int64 GetDts(void)throw(std::invalid_argument);
396 void SetDts(Int64 value)throw(std::invalid_argument);
397 byte GetExtention2(void);
405 PesPacket(pByte buff, int offset, int length, ushort pid);
406 bool GetPriority(void);
407 void SetPriority(bool priority);
409 pByte GetPayload(void);
411 void SetByte(int i, byte dat);
413 void SetPID(ushort id);
414 bool GetComplete(void);
415 void SetComplete(bool value);
416 boost::shared_ptr<PesHeader> GetHeader(void);
417 void AddData(pByte moredata);
418 void AddData(pByte buff, int offset, int length);
419 byte GetBaseId(void);
420 byte GetExtendedId(void);
421 UInt32 GetExtendedType(void);
428 class VC1SequenceInfo {
430 VC1SequenceInfo(pByte data, int offset);
434 bool Interlaced(void);
435 bool DisplayExt(void);
436 bool AspectFlag(void);
437 byte GetVc1AspectRatio(void);
438 bool FrameFlag(void);
439 bool FrameRateIndicatorFlag(void);
440 AspectRatio GetAspectRatio(void);
441 VideoFormat GetVideoFormat(void);
442 FrameRate GetFrameRate(void);
454 class ElementaryParse {
456 ElementaryParse(void);
457 virtual VideoFormat GetVideoFormat(void);
458 virtual void SetVideoFormat(VideoFormat videoformat);
459 virtual AspectRatio GetAspectRatio(void);
460 virtual void SetAspectRatio(AspectRatio aspectratio);
461 virtual FrameRate GetFrameRate(void);
462 virtual void SetFrameRate(FrameRate frameRate);
463 virtual AudioPresentationType GetAudioPresentationType(void);
464 virtual void SetAudioPresentationType(
465 AudioPresentationType audioPresentationTyp);
466 virtual SamplingFrequency GetSamplingFrequency(void);
467 virtual void SetSamplingFrequency(SamplingFrequency samplingFrequency);
468 virtual pByte GetElementaryDescriptors(void);
470 byte GetNextBit(void);
474 VideoFormat mVideoFormat;
475 FrameRate mFrameRate;
476 AspectRatio mAspectRatio;
477 SamplingFrequency mSamplingFrequency;
478 AudioPresentationType mAudioPresentationType;
479 pByte ElementaryDescriptors;
482 class H264Info : ElementaryParse {
484 H264Info(pByte data, int offset);
485 pByte GetElementaryDescriptors(void);
486 VideoFormat GetVideoFormat(void);
487 AspectRatio GetAspectRatio(void);
488 FrameRate GetFrameRate(void);
489 AudioPresentationType GetAudioPresentationType(void);
490 SamplingFrequency GetSamplingFrequency(void);
491 UInt32 GetNextExpGolomb();
492 void ScalingListSkip(int skip);
493 UInt32 GetWidth(void);
494 UInt32 GetHeigth(void);
495 pByte GetHdmvVideoRegistrationDescriptor(void);
498 class AC3Info : ElementaryParse {
500 int GetMaxFrameLength(void);
501 int GetFrameLength(void);
502 Ac3SyntaxType GetSyntaxType(void);
506 bool IsIndependentStream(void);
507 AC3Info(pByte data, int offset);
508 VideoFormat GetVideoFormat(void);
509 AspectRatio GetAspectRatio(void);
510 FrameRate GetFrameRate(void);
511 AudioPresentationType GetAudioPresentationType(void);
512 SamplingFrequency GetSamplingFrequency(void);
513 pByte GetElementaryDescriptors(void);
514 pByte AC3Info::GetAC3AudioDescriptor(void);
515 byte GetSampleRateCode(void);
518 static readonly int len48k[];
519 static readonly int len44k[];
520 static readonly int len32k[];
523 class DtsInfo : ElementaryParse {
525 bool GetExtAudio(void);
526 DtsInfo(pByte data, int offset);
527 VideoFormat GetVideoFormat(void);
528 AspectRatio GetAspectRatio(void);
529 FrameRate GetFrameRate(void);
530 AudioPresentationType GetAudioPresentationType(void);
531 SamplingFrequency GetSamplingFrequency(void);
532 pByte GetElementaryDescriptors(void);
533 ushort GetFrameSize(void);
534 byte GetExtAudioId(void);
536 byte GetGetSamplingFrequency(void);
539 class MlpInfo : ElementaryParse {
541 MlpInfo(pByte data, int offset);
542 VideoFormat GetVideoFormat(void);
543 AspectRatio GetAspectRatio(void);
544 FrameRate GetFrameRate(void);
545 AudioPresentationType GetAudioPresentationType(void);
546 SamplingFrequency GetSamplingFrequency(void);
547 pByte GetElementaryDescriptors(void);
548 pByte GetAC3AudioDescriptor(void);
549 byte GetSampleRateCode(void);
552 class Mpeg2Info : ElementaryParse {
555 Mpeg2Info(pByte data, int offset);
556 VideoFormat GetVideoFormat(void);
557 AspectRatio GetAspectRatio(void);
558 FrameRate GetFrameRate(void);
559 AudioPresentationType GetAudioPresentationType(void);
560 SamplingFrequency GetSamplingFrequency(void);
561 pByte GetElementaryDescriptors(void);
562 pByte GetMpeg2VideoRegistrationDescriptor(void);
563 ushort GetHorizontal(void);
564 ushort GetVertical(void);
565 byte GetAspect(void);
566 byte GetFrameRateCode(void);
567 bool GetProgressive(void);
569 class Mpeg2Ext :ElementaryParse {
572 Mpeg2Ext(pByte data, int offset);
573 AspectRatio GetAspectRatio(void);
574 AudioPresentationType GetAudioPresentationType(void);
575 pByte GetElementaryDescriptors(void);
576 FrameRate GetFrameRate(void);
577 SamplingFrequency GetSamplingFrequency(void);
578 VideoFormat GetVideoFormat(void);
579 bool GetProgressive(void);
585 #endif TSREMUXCPP_UTILS_H_