OSDN Git Service

b8bfe71c4bf2f5eb8955a3119a40b1f051164588
[tsremuxcpp/developing01.git] / src / Utils.h
1 #ifndef TSREMUXCPP_UTILS_H_
2 #define TSREMUXCPP_UTILS_H_
3
4 #include <cstring>
5 #include <ctime>
6 #include <list>
7 #include <vector>
8 #include <stdexcept>
9 #include <boost/shared_array.hpp>
10 #include <boost/filesystem.hpp>
11 #include "tsremuxcpp_define.h"
12
13 namespace TsRemux {
14 enum TsFileType {
15     UNKNOWN = 0,
16     TS,
17     M2TS,
18     EVOB,
19     ELEMENTARY,
20     PES_ELEMENTARY,
21     SUP_ELEMENTARY,
22     BLU_RAY,
23     MKV
24 };
25
26 enum DtcpCci {
27     CopyFree = 0,
28     NoMoreCopies,
29     CopyOnce,
30     CopyNever
31 };
32  
33 enum ElementaryStreamTypes {
34     INVALID = 0,
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
53 };
54
55 enum VideoFormat {
56     VF_Reserved = 0,
57     i480,
58     i576,
59     p480,
60     i1080,
61     p720,
62     p1080,
63     p576
64 };
65
66 enum FrameRate {
67     FR_Reserved = 0,
68     f23_976,
69     f24,
70     f25,
71     f29_97,
72     f50 = 6,
73     f59_94
74 };
75
76 enum AspectRatio {
77     AR_Reserved = 0,
78     a4_3 = 2,
79     a16_9
80 };
81
82 enum AudioPresentationType {
83     AP_Reserved = 0,
84     mono,
85     stereo = 3,
86     multi = 6,
87     combo = 12
88 };
89
90 enum SamplingFrequency {
91     SF_Reserved = 0,
92     kHz48,
93     kHz96 = 4,
94     kHz192,
95     kHz48_192 = 12,
96     kHz48_96 = 14
97 };
98
99 struct EpElement {
100  public:
101   signed long long pts_;
102   unsigned long spn_;
103   EpElement(signed long long pts, unsigned long spn);
104 };
105
106 class Utility {
107  public:
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);
112 };
113
114 class StreamInfo {
115  public:
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);
136  private:
137   pByte mData;
138   VideoFormat mVideoFormat;
139   AspectRatio mAspectRatio;
140   FrameRate mFrameRate;
141   AudioPresentationType mAudioPresentationType;
142   SamplingFrequency mSamplingFrequency;
143 //  ElementaryStreamTypes StreamType;
144 //  ushort ElementaryPID;
145 //  pByte ElementaryDescriptors;
146 };
147
148 class BluRayOutput {
149  public:
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);
171  private:
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);
181   boost::filesystem::path path;
182   TimeSpan chapterLen; 
183 };
184
185 class Descriptor {
186  public:
187   Descriptor(pByte data, int startIndex)throw(std::invalid_argument);
188   pByte GetData(void);
189   byte GetTag(void);
190   byte GetLength(void);
191  protected:
192   pByte mData;
193 };
194
195 class DTCP_Descriptor : public Descriptor {
196  public:
197   DTCP_Descriptor(pByte data, int startIndex)
198     throw(std::invalid_argument);
199   DtcpCci GetCopyStatus(void);
200   bool GetAnalogConstrain(void);
201   bool GetMacrovision(void);
202 };
203
204 class Constants {
205  public:
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;
221
222   // defaults
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;
241
242   // stream types
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;
265
266   // clocks
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;
275
276   // descriptors
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);
286 };
287
288 class ProgramInfo {
289  public:
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);
296   pByte GetData(void);
297  private:
298   pByte mData;
299 };
300
301 class TsPacket {
302  public:
303   TsPacket(void);
304   bool GetPriority(void);
305   void SetPriority(bool priority);
306   ushort GetPID(void);
307   void SetPID(ushort pid);
308   byte GetPointerSize(void);
309   pByte GetData(void);
310   void SetData(pByte data, int startIndex)throw(std::invalid_argument);
311   bool HasPcr(void);
312   Int64 GetPcr(void)throw(std::out_of_range);
313   pByte Payload(void);
314   void IncrementContinuityCounter(void);
315   byte GetContinuityCounter(void);
316   void SetContinuityCounter(byte value)throw(std::out_of_range);
317   bool HasPesHeader(void);
318  protected:
319   pByte mData;
320  private:
321   bool Priority;
322   ushort PID;
323 };
324
325 class PcrPacket : TsPacket {
326  public:
327   PcrPacket(Int64 pcr, byte counter, ushort pid);
328 };
329
330 class TsTable : public TsPacket {
331  public:
332   TsTable();
333   TsTable(pByte data);
334   void AddData(pByte data, int offset, int len);
335   bool Complete(void);
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);
342  protected:
343   void RefreshCrc(void);
344 };
345
346 class PatPacket : public TsTable {
347  public:
348   PatPacket(void);
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);
356 };
357
358 class SitPacket : TsTable {
359  public:
360   SitPacket(void);
361   SitPacket(pByte data);
362 };
363
364 class PmtPacket : TsTable {
365  public:
366   PmtPacket(void);
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);
378  private:
379   ushort PcrPID;
380 };
381
382 class PesHeader {
383  public:
384   PesHeader(pByte data)throw(std::invalid_argument);
385   byte GetStreamId(void);
386   byte GetByte(int i);
387   void SetByte(int i, byte dat);
388   byte GetHeaderLength(void);
389   int GetTotalHeaderLength(void);
390   ushort GetPacketLength(void);
391   bool HasPts(void);
392   bool HasDts(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);
398   pByte GetData(void);
399  private:
400   pByte mData;
401 };
402
403 class PesPacket {
404  public:
405   PesPacket(pByte buff, int offset, int length, ushort pid);
406   bool GetPriority(void);
407   void SetPriority(bool priority);
408   pByte GetData(void);
409   pByte GetPayload(void);
410   byte GetByte(int i);
411   void SetByte(int i, byte dat);
412   ushort GetPID(void);
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);
422  private:
423   pByte mData;
424   bool mPriority;
425   ushort mPID;
426 };
427
428 class VC1SequenceInfo {
429  public:
430   VC1SequenceInfo(pByte data, int offset);
431   int GetHeight(void);
432   int GetWidth(void);
433   bool Valid(void);
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);
443  private:
444   pByte mData;
445 };
446
447 enum Ac3SyntaxType {
448         Invalid = 0,
449         Standard = 8,
450         Alternative = 6,
451         Enhanced = 16
452 };
453
454 class ElementaryParse {
455  public:
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);
469  protected:
470   byte GetNextBit(void);
471   pByte mData;
472   int indicator;
473   bool mValid;
474   VideoFormat mVideoFormat;
475   FrameRate mFrameRate;
476   AspectRatio mAspectRatio;
477   SamplingFrequency mSamplingFrequency;
478   AudioPresentationType mAudioPresentationType;
479   pByte ElementaryDescriptors;
480 };
481
482 class H264Info : ElementaryParse {
483  public:
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);
496 };
497
498 class AC3Info : ElementaryParse {
499  public:
500   int GetMaxFrameLength(void);
501   int GetFrameLength(void);
502   Ac3SyntaxType GetSyntaxType(void);
503   byte GetBsid(void);
504   byte GetBsmod(void);
505   byte GetAcmod(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);
516   bool Valid(void);
517  private:
518   static readonly int len48k[];
519   static readonly int len44k[];
520   static readonly int len32k[];
521 };
522
523 class DtsInfo : ElementaryParse {
524  public:
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);
535   byte GetAmode(void);
536   byte GetGetSamplingFrequency(void);
537 };
538
539 class MlpInfo : ElementaryParse {
540  public:
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);
550 };
551
552 class Mpeg2Info : ElementaryParse {
553  public:
554   Mpeg2Info(void);
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);
568  private:
569   class Mpeg2Ext :ElementaryParse {
570    public:
571     Mpeg2Ext(void);
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);
580   };
581   Mpeg2Ext mpgext;
582 };
583
584 } //namespace
585 #endif TSREMUXCPP_UTILS_H_