OSDN Git Service

87ee521de584ea692e8ec9b69a066af9c476e62f
[tsremuxcpp/developing01.git] / src / Utils.h
1 #ifndef TSREMUXCPP_UTILS_H_
2 #define TSREMUXCPP_UTILS_H_
3
4 #include <string>
5 #include <list>
6 #include <vector>
7 #include <stdexcept>
8 #include <boost/shared_array.hpp>
9 // #include "BlueMux.h"
10 #include "tsremuxcpp_define.h"
11
12 namespace TsRemux {
13 enum TsFileType {
14     UNKNOWN = 0,
15     TS,
16     M2TS,
17     EVOB,
18     ELEMENTARY,
19     PES_ELEMENTARY,
20     SUP_ELEMENTARY,
21     BLU_RAY,
22     MKV
23 };
24
25 enum DtcpCci {
26     CopyFree = 0,
27     NoMoreCopies,
28     CopyOnce,
29     CopyNever
30 };
31  
32 enum ElementaryStreamTypes {
33     INVALID = 0,
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
52 };
53
54 enum VideoFormat {
55     VF_Reserved = 0,
56     i480,
57     i576,
58     p480,
59     i1080,
60     p720,
61     p1080,
62     p576
63 };
64
65 enum FrameRate {
66     FR_Reserved = 0,
67     f23_976,
68     f24,
69     f25,
70     f29_97,
71     f50 = 6,
72     f59_94
73 };
74
75 enum AspectRatio {
76     AR_Reserved = 0,
77     a4_3 = 2,
78     a16_9
79 };
80
81 enum AudioPresentationType {
82     AP_Reserved = 0,
83     mono,
84     stereo = 3,
85     multi = 6,
86     combo = 12
87 };
88
89 enum SamplingFrequency {
90     SF_Reserved = 0,
91     kHz48,
92     kHz96 = 4,
93     kHz192,
94     kHz48_192 = 12,
95     kHz48_96 = 14
96 };
97
98 struct EpElement {
99  public:
100   signed long long pts_;
101   unsigned long spn_;
102   EpElement(signed long long pts, unsigned long spn);
103 };
104
105 //class Utility {
106 // public:
107 //  static pByte ToArray(pByte vector);
108 //};
109
110 class StreamInfo {
111  public:
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);
131  private:
132   pByte mData;
133   VideoFormat mVideoFormat;
134   AspectRatio mAspectRatio;
135   FrameRate mFrameRate;
136   AudioPresentationType mAudioPresentationType;
137   SamplingFrequency mSamplingFrequency;
138 //  ElementaryStreamTypes StreamType;
139 //  ushort ElementaryPID;
140 //  pByte ElementaryDescriptors;
141 };
142
143 class BluRayOutput {
144  public:
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);
163  private:
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);
173 };
174
175 class Descriptor {
176  public:
177   Descriptor(pByte data, int startIndex)throw(std::invalid_argument);
178   pByte GetData(void);
179   byte GetTag(void);
180   byte GetLength(void);
181  protected:
182   pByte mData;
183 };
184
185 class DTCP_Descriptor : public Descriptor {
186  public:
187   DTCP_Descriptor(pByte data, int startIndex)
188     throw(std::invalid_argument);
189   DtcpCci GetCopyStatus(void);
190   bool GetAnalogConstrain(void);
191   bool GetMacrovision(void);
192 };
193
194 class Constants {
195  public:
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;
211
212   // defaults
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;
231
232   // stream types
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;
255
256   // clocks
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;
265
266   // descriptors
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);
276 };
277
278 class ProgramInfo {
279  public:
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);
286   pByte GetData(void);
287  private:
288   pByte mData;
289 };
290
291 class TsPacket {
292  public:
293   TsPacket(void);
294   bool GetPriority(void);
295   void SetPriority(bool priority);
296   ushort GetPID(void);
297   void SetPID(ushort pid);
298   byte GetPointerSize(void);
299   pByte GetData(void);
300   void SetData(pByte data, int startIndex)throw(std::invalid_argument);
301   bool HasPcr(void);
302   Int64 GetPcr(void)throw(std::out_of_range);
303   pByte Payload(void);
304   void IncrementContinuityCounter(void);
305   byte GetContinuityCounter(void);
306   void SetContinuityCounter(byte value)throw(std::out_of_range);
307   bool HasPesHeader(void);
308  protected:
309   pByte mData;
310  private:
311   bool Priority;
312   ushort PID;
313 };
314
315 class PcrPacket : TsPacket {
316  public:
317   PcrPacket(Int64 pcr, byte counter, ushort pid);
318 };
319
320 class TsTable : public TsPacket {
321  public:
322   TsTable();
323   TsTable(pByte data);
324   void AddData(pByte data, int offset, int len);
325   bool Complete(void);
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);
332  protected:
333   void RefreshCrc(void);
334 };
335
336 class PatPacket : public TsTable {
337  public:
338   PatPacket(void);
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);
346 };
347
348 class SitPacket : TsTable {
349  public:
350   SitPacket(void);
351   SitPacket(byte* data);
352 };
353
354 class PmPacket : TsTable {
355  public:
356   PmPacket(void);
357   PmPacket(byte* data);
358   DTCP_Descriptor DtcpInfo;
359   pByte ProgramDescriptorsData;
360   StreamInfo* ElementaryStreams;
361   ushort ProgramNumber;
362   ushort PcrPID;
363  private:
364   ushort ProgramDescriptorsLength;
365   ushort StreamInfoLength;
366 };
367
368 class PesHeader {
369  public:
370   PesHeader(pByte data)throw(std::invalid_argument);
371   byte GetStreamId(void);
372   byte GetByte(int i);
373   void SetByte(int i, byte dat);
374   byte GetHeaderLength(void);
375   int GetTotalHeaderLength(void);
376   ushort GetPacketLength(void);
377   bool HasPts(void);
378   bool HasDts(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);
384   pByte GetData(void);
385  private:
386   pByte mData;
387 };
388
389 class PesPacket {
390  public:
391   PesPacket(pByte buff, int offset, int length, ushort pid);
392   bool GetPriority(void);
393   void SetPriority(bool priority);
394   pByte GetData(void);
395   pByte GetPayload(void);
396   byte GetByte(int i);
397   void SetByte(int i, byte dat);
398   ushort GetPID(void);
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);
408  private:
409   pByte mData;
410   bool mPriority;
411   ushort mPID;
412 };
413
414 class VC1SequenceInfo {
415  public:
416   VC1SequenceInfo(pByte data, int offset);
417   int GetHeight(void);
418   int GetWidth(void);
419   bool Valid(void);
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);
429  private:
430   pByte mData;
431 };
432
433 enum Ac3SyntaxType {
434         Invalid = 0,
435         Standard = 8,
436         Alternative = 6,
437         Enhanced = 16
438 };
439
440 class ElementaryParse {
441  public:
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);
455  protected:
456   byte GetNextBit(void);
457   pByte mData;
458   int indicator;
459   bool mValid;
460   VideoFormat mVideoFormat;
461   FrameRate mFrameRate;
462   AspectRatio mAspectRatio;
463   SamplingFrequency mSamplingFrequency;
464   AudioPresentationType mAudioPresentationType;
465   pByte ElementaryDescriptors;
466 };
467
468 class H264Info : ElementaryParse {
469  public:
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);
482  private:
483   UInt32 GetNextExpGolomb();
484   void ScalingListSkip(int skip);
485   UInt32 Width;
486   UInt32 Heigth;
487   pByte HdmvVideoRegistrationDescriptor;
488   VideoFormat mVideoFormat;
489   AspectRatio mAspectRatio;
490   FrameRate   mFrameRate;
491   AudioPresentationType mAudioPresentationType;
492   SamplingFrequency mSamplingFrequency;
493 };
494
495 class AC3Info : ElementaryParse {
496  public:
497   int MaxFrameLength;
498   int FrameLength;
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);
513  private:
514   static readonly int* len48k;
515   static readonly int* len44k;
516   static readonly int* len32k;
517   byte Bsid;
518   byte Bsmod;
519   byte Acmod;
520 };
521
522 class DtsInfo : ElementaryParse {
523  public:
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);
536   ushort FrameSize;
537   byte ExtAudioId;
538  private:
539   byte Amode;
540   byte SampleFreq;
541   bool ExtAudio;
542 };
543
544 class MlpInfo : ElementaryParse {
545  public:
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);
558  private:
559   pByte AC3AudioDescriptor;
560   byte SampleRateCode;
561 };
562
563 class Mpeg2Info : ElementaryParse {
564  public:
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);
577  private:
578   pByte Mpeg2VideoRegistrationDescriptor;
579   ushort Horizontal;
580   ushort Vertical;
581   byte Aspect;
582   byte FrameRateCode;
583   bool Progressive;
584 };
585
586 } //namespace
587 #endif TSREMUXCPP_UTILS_H_