OSDN Git Service

implement VC1SequenceInfo class in Utils
[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(Bytes vector);
108 };
109 class StreamInfo {
110  public:
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);
130  private:
131   Bytes mData;
132   VideoFormat mVideoFormat;
133   AspectRatio mAspectRatio;
134   FrameRate mFrameRate;
135   AudioPresentationType mAudioPresentationType;
136   SamplingFrequency mSamplingFrequency;
137 //  ElementaryStreamTypes StreamType;
138 //  ushort ElementaryPID;
139 //  Bytes ElementaryDescriptors;
140 };
141
142 class BluRayOutput {
143  public:
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);
162  private:
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);
172 };
173
174 class Descriptor {
175  public:
176   Descriptor(pByte data, int startIndex)throw(std::invalid_argument);
177   pByte GetData(void);
178   byte GetTag(void);
179   byte GetLength(void);
180  private:
181   Bytes mData;
182 };
183
184 class DTCP_Descriptor : Descriptor {
185  public:
186   DTCP_Descriptor(pByte data, int startIndex);
187 };
188
189 class Constants {
190  public:
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;
206
207   // defaults
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;
226
227   // stream types
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;
250
251   // clocks
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;
260
261   // descriptors
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);
271 };
272
273 class ProgramInfo {
274  public:
275   ProgramInfo(pByte data, int index);
276   ProgramInfo(ushort programNumber, ushort programPid);
277   ushort ProgramNumber;
278   ushort ProgramPID;
279   Bytes Data;
280  private:
281   Bytes mData;
282 };
283
284 class TsPacket {
285  public:
286   TsPacket(void);
287   bool GetPriority(void);
288   void SetPriority(bool priority);
289   ushort GetPID(void);
290   void SetPID(ushort pid);
291   byte GetPointerSize(void);
292   pByte GetData(void);
293   void SetData(pByte data, int startIndex)throw(std::invalid_argument);
294   bool HasPcr(void);
295   Int64 GetPcr(void)throw(std::out_of_range);
296   pByte Payload(void);
297   void IncrementContinuityCounter(void);
298   byte GetContinuityCounter(void);
299   void SetContinuityCounter(byte value)throw(std::out_of_range);
300   bool HasPesHeader(void);
301  protected:
302   Bytes mData;
303  private:
304   bool Priority;
305   ushort PID;
306 };
307
308 class PcrPacket : TsPacket {
309  public:
310   PcrPacket(Int64 pcr, byte counter, ushort pid);
311 };
312
313 class TsTable : TsPacket {
314  public:
315   TsTable();
316   TsTable(pByte data);
317   void AddData(pByte data, int offset, int len);
318   bool Complete(void);
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);
325  protected:
326   void RefreshCrc(void);
327 };
328
329 class PatPacket : TsTable {
330  public:
331   PatPacket(void);
332   PatPacket(pByte data);
333   ushort TransportStreamId;
334   ProgramInfo* Programs;
335  private:
336   ushort ProgramInfoLength;
337 };
338
339 class SitPacket : TsTable {
340  public:
341   SitPacket(void);
342   SitPacket(byte* data);
343 };
344
345 class PmPacket : TsTable {
346  public:
347   PmPacket(void);
348   PmPacket(byte* data);
349   DTCP_Descriptor DtcpInfo;
350   Bytes ProgramDescriptorsData;
351   StreamInfo* ElementaryStreams;
352   ushort ProgramNumber;
353   ushort PcrPID;
354  private:
355   ushort ProgramDescriptorsLength;
356   ushort StreamInfoLength;
357 };
358
359 class PesHeader {
360  public:
361   PesHeader(pByte data)throw(std::invalid_argument);
362   byte GetStreamId(void);
363   byte GetByte(int i);
364   void SetByte(int i, byte dat);
365   byte GetHeaderLength(void);
366   int GetTotalHeaderLength(void);
367   ushort GetPacketLength(void);
368   bool HasPts(void);
369   bool HasDts(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);
375   pByte GetData(void);
376  private:
377   Bytes mData;
378 };
379
380 class PesPacket {
381  public:
382   PesPacket(pByte buff, int offset, int length, ushort pid);
383   bool GetPriority(void);
384   void SetPriority(bool priority);
385   pByte GetData(void);
386   pByte GetPayload(void);
387   byte GetByte(int i);
388   void SetByte(byte dat);
389   ushort GetPID(void);
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);
399  private:
400   Bytes mData;
401   bool mPriority;
402   ushort mPID;
403 };
404
405 class VC1SequenceInfo {
406  public:
407   VC1SequenceInfo(pByte data, int offset);
408   int GetHeight(void);
409   int GetWidth(void);
410   bool Valid(void);
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);
420  private:
421   Bytes mData;
422 };
423
424 enum Ac3SyntaxType {
425         Invalid = 0,
426         Standard = 8,
427         Alternative = 6,
428         Enhanced = 16
429 };
430
431 class ElementaryParse {
432  public:
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);
446  protected:
447   byte GetNextBit(void);
448   Bytes mData;
449   int indicator;
450   bool mValid;
451   VideoFormat mVideoFormat;
452   FrameRate mFrameRate;
453   AspectRatio mAspectRatio;
454   SamplingFrequency mSamplingFrequency;
455   AudioPresentationType mAudioPresentationType;
456   Bytes ElementaryDescriptors;
457 };
458
459 class H264Info : ElementaryParse {
460  public:
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);
473  private:
474   UInt32 GetNextExpGolomb();
475   void ScalingListSkip(int skip);
476   UInt32 Width;
477   UInt32 Heigth;
478   Bytes HdmvVideoRegistrationDescriptor;
479   VideoFormat mVideoFormat;
480   AspectRatio mAspectRatio;
481   FrameRate   mFrameRate;
482   AudioPresentationType mAudioPresentationType;
483   SamplingFrequency mSamplingFrequency;
484 };
485
486 class AC3Info : ElementaryParse {
487  public:
488   int MaxFrameLength;
489   int FrameLength;
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);
504  private:
505   static readonly int* len48k;
506   static readonly int* len44k;
507   static readonly int* len32k;
508   byte Bsid;
509   byte Bsmod;
510   byte Acmod;
511 };
512
513 class DtsInfo : ElementaryParse {
514  public:
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);
527   ushort FrameSize;
528   byte ExtAudioId;
529  private:
530   byte Amode;
531   byte SampleFreq;
532   bool ExtAudio;
533 };
534
535 class MlpInfo : ElementaryParse {
536  public:
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);
549  private:
550   Bytes AC3AudioDescriptor;
551   byte SampleRateCode;
552 };
553
554 class Mpeg2Info : ElementaryParse {
555  public:
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);
568  private:
569   Bytes Mpeg2VideoRegistrationDescriptor;
570   ushort Horizontal;
571   ushort Vertical;
572   byte Aspect;
573   byte FrameRateCode;
574   bool Progressive;
575 };
576
577 } //namespace
578 #endif TSREMUXCPP_UTILS_H_