OSDN Git Service

implement class TsTable 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 <stdexcept>
7 // #include "BlueMux.h"
8 #include "tsremuxcpp_define.h"
9
10 namespace TsRemux {
11 enum TsFileType {
12     UNKNOWN = 0,
13     TS,
14     M2TS,
15     EVOB,
16     ELEMENTARY,
17     PES_ELEMENTARY,
18     SUP_ELEMENTARY,
19     BLU_RAY,
20     MKV
21 };
22
23 enum DtcpCci {
24     CopyFree = 0,
25     NoMoreCopies,
26     CopyOnce,
27     CopyNever
28 };
29  
30 enum ElementaryStreamTypes {
31     INVALID = 0,
32     VIDEO_STREAM_MPEG1 = 0x01,
33     VIDEO_STREAM_MPEG2 = 0x02,
34     AUDIO_STREAM_MPEG1 = 0x03, // all layers including mp3
35     AUDIO_STREAM_MPEG2 = 0x04,
36     VIDEO_STREAM_H264 = 0x1b,
37     AUDIO_STREAM_LPCM = 0x80,
38     AUDIO_STREAM_AC3 = 0x81,
39     AUDIO_STREAM_DTS = 0x82,
40     AUDIO_STREAM_AC3_TRUE_HD = 0x83,
41     AUDIO_STREAM_AC3_PLUS = 0x84,
42     AUDIO_STREAM_DTS_HD = 0x85,
43     AUDIO_STREAM_DTS_HD_MASTER_AUDIO = 0x86,
44     PRESENTATION_GRAPHICS_STREAM = 0x90,
45     INTERACTIVE_GRAPHICS_STREAM = 0x91,
46     SUBTITLE_STREAM = 0x92,
47     SECONDARY_AUDIO_AC3_PLUS = 0xa1,
48     SECONDARY_AUDIO_DTS_HD = 0xa2,
49     VIDEO_STREAM_VC1 = 0xea
50 };
51
52 enum VideoFormat {
53     VF_Reserved = 0,
54     i480,
55     i576,
56     p480,
57     i1080,
58     p720,
59     p1080,
60     p576
61 };
62
63 enum FrameRate {
64     FR_Reserved = 0,
65     f23_976,
66     f24,
67     f25,
68     f29_97,
69     f50 = 6,
70     f59_94
71 };
72
73 enum AspectRatio {
74     AR_Reserved = 0,
75     a4_3 = 2,
76     a16_9
77 };
78
79 enum AudioPresentationType {
80     AP_Reserved = 0,
81     mono,
82     stereo = 3,
83     multi = 6,
84     combo = 12
85 };
86
87 enum SamplingFrequency {
88     SF_Reserved = 0,
89     kHz48,
90     kHz96 = 4,
91     kHz192,
92     kHz48_192 = 12,
93     kHz48_96 = 14
94 };
95
96 struct EpElement {
97  public:
98   signed long long pts_;
99   unsigned long spn_;
100   EpElement(signed long long pts, unsigned long spn);
101 };
102
103 class StreamInfo {
104  public:
105   StreamInfo(byte* data, int index)throw(std::invalid_argument);
106   VideoFormat GetVideoFormat(void);
107   void SetVideoFormat(VideoFormat videoformat);
108   AspectRatio GetAspectRatio(void);
109   void SetAspectRatio(AspectRatio aspectratio);
110   FrameRate GetFrameRate(void);
111   void SetFrameRate(FrameRate frameRate);
112   AudioPresentationType GetAudioPresentationType(void);
113   void SetAudioPresentationType(AudioPresentationType audioPresentationTyp);
114   SamplingFrequency GetSamplingFrequency(void);
115   void SetSamplingFrequency(SamplingFrequency samplingFrequency);
116   StreamInfo(ElementaryStreamTypes streamType, ushort elementaryPid);
117   byte* GetByteData(void);
118   ElementaryStreamTypes GetElementaryStreamTypes(void);
119   void SetElementaryStreamTypes(ElementaryStreamTypes stream_type);
120   ushort GetElementaryPID(void);
121   void GetElementaryPID(ushort pid);
122   byte* GetElementaryDescriptors(void);
123   void SetElementaryDescriptors(byte* value)throw(std::invalid_argument);
124  private:
125   byte* mData;
126   VideoFormat mVideoFormat;
127   AspectRatio mAspectRatio;
128   FrameRate mFrameRate;
129   AudioPresentationType mAudioPresentationType;
130   SamplingFrequency mSamplingFrequency;
131   ElementaryStreamTypes StreamType;
132   ushort ElementaryPID;
133   byte* ElementaryDescriptors;
134 };
135
136 class BluRayOutput {
137  public:
138   BluRayOutput(std::string path, TimeSpan chapterLen);
139   void Author(EpElement* EpInfo, StreamInfo* sis, UInt32 numOfSourcePackets);
140   byte* BuildPlayList(byte* PlayItems);
141   byte* BuildFirstPlayItem(byte stc_id, UInt32 start,
142         UInt32 end, byte* StnTable);
143   byte* BuildFirstPlayMarks(UInt32 start, UInt32 end, UInt32 interval);
144   byte* Build_clpi(byte* ClipInfo, byte* SequenceInfo,
145         byte* ProgramInfo, byte* CPI);
146   byte* BuildClipInfo(UInt32 numOfSourcePackets, EpElement* EpInfo);
147   byte* BuildSequenceInfo(UInt32 start, UInt32 end);
148   byte* BuildProgramInfo(ushort pids, byte* StreamCodingInfos);
149   byte* BuildVideoStreamCodingInfo(ElementaryStreamTypes type,
150         VideoFormat format, FrameRate rate, AspectRatio ratio);
151   byte* BuildAudioStreamCodingInfo(ElementaryStreamTypes type,
152         AudioPresentationType format, SamplingFrequency rate);
153   byte* BuildPgStreamCodingInfo(void);
154   byte* BuildCpi(byte* EpMap);
155   byte* BuildEpMap(EpElement* EpInfo);
156  private:
157   byte* BuildStreamEntry(ushort pid);
158   byte* BuildVideoStreamAttributes(byte type, VideoFormat vf, FrameRate fr);
159   byte* BuildAudioStreamAttributes(byte type, AudioPresentationType vf,
160         SamplingFrequency fr);
161   byte* BuildStnTable(byte* VideoEntry, byte* VideoAttributes,
162         byte* AudioEntries, byte* AudioAttributes,
163         byte* PgEntries, byte* PgAttributes);
164   byte* UintToByteArraryNetwork(UInt32 value);
165   byte* Build_mlps(byte* PlayList, byte* PlayListMark);
166 };
167
168 class Descriptor {
169  public:
170   Descriptor(byte* data, int startIndex);
171 };
172
173 class DTCP_Descriptor : Descriptor {
174  public:
175   DTCP_Descriptor(byte* data, int startIndex);
176 };
177
178 class Constants {
179  public:
180   static const int TS_PAYLOAD_SIZE;
181   static const int TS_SIZE;
182   static const int STRIDE_SIZE;
183   static const int DISK_BUFFER;  
184   static const byte SYNC_BYTE;
185   static const byte PAT_PID;
186   static const byte SIT_PID;
187   static const byte PAT_TABLE_ID;
188   static const byte PMT_TABLE_ID;
189   static const byte DTCP_DESCRIPTOR_TAG;
190   static const byte PACK_ID;
191   static const byte SYS_ID;
192   static const byte MAP_ID;
193   static const byte DIR_ID;
194   static const byte PAD_ID;
195
196   // defaults
197   static const ushort DEFAULT_PMT_PID;
198   static const ushort DEFAULT_VIDEO_PID;
199   static const ushort MAX_VIDEO_PID;
200   static const ushort DEFAULT_AUDIO_PID;
201   static const ushort MAX_AUDIO_PID;
202   static const ushort DEFAULT_PCR_PID;
203   static const ushort DEFAULT_SUBTITLE_PID;
204   static const ushort DEFAULT_PRESENTATION_GRAPHICS_PID;
205   static const ushort DEFAULT_INTERACTIVE_GRAPHICS_PID;
206   static const ushort DEFAULT_PROGRAM_NUMBER;
207   static const int MAX_BUFFER_COUNT;
208   static const int MIN_BUFFER_COUNT;
209   static const Int64 AUDIO_DELAY;
210   static const UInt32 MKVCLUSTER_START;
211   static const UInt32 MKVFILE_START;
212   static const UInt32 MKVSEGMENT_START;
213   static const UInt32 MKVTRACKINFO_START;
214   static const byte MKVTIMECODE_START;
215
216   // stream types
217   static const byte PES_VIDEO;
218   static const byte PES_AUDIO_MPEG;
219   static const byte PES_PRIVATE1;
220   static const byte PES_PADDING;
221   static const byte PES_PRIVATE2;
222   static const byte PES_VIDEO_VC1;
223   static const byte PES_PRIVATE_AC3;
224   static const byte PES_PRIVATE_AC3_PLUS;
225   static const byte PES_PRIVATE_DTS_HD;
226   static const byte PES_PRIVATE_LPCM;
227   static const byte PES_PRIVATE_AC3_TRUE_HD;
228   static const UInt32 VC1_SEQ_SC;
229   static const UInt32 VC1_END_OF_STREAM;
230   static const ushort AC3_SYNC;
231   static const UInt32 H264_PREFIX;
232   static const UInt32 H264_END_OF_STREAM;
233   static const UInt32 DTS_SYNC;
234   static const UInt32 DTS_EXT_SYNC;
235   static const UInt32 MLP_SYNC;
236   static const UInt32 MPEG2_SEQ_CODE;
237   static const UInt32 MPEG2_SEQ_EXT;
238   static const UInt32 MPEG2_SEQ_END;
239
240   // clocks
241   static const Int64 MPEG2TS_CLOCK_RATE;
242   static const Int64 MAX_MPEG2TS_CLOCK;
243   static const Int64 MAX_BLURAY_CLOCK;
244   static const Int64 MAX_FIREWIRE_CLOCK;
245   static const Int64 MAX_PTS_CLOCK;
246   static const Int64 PTS_CLOCK_RATE;
247   static const int MAX_OFFSET;
248   static const int MAX_COUNT;
249
250   // descriptors
251   static readonly byte hdmv_registration_descriptor[];
252   static readonly byte copy_control_descriptor[];
253   static readonly byte vc1_descriptor[];
254   static readonly byte ac3_registration_descriptor[];
255   static readonly byte DefaultSitTableOne[];
256   static readonly uint crc_table[];
257   static uint ComputeCrc(byte* data);
258   static uint ComputeCrc(byte* data, int length);
259   static uint ComputeCrc(byte* data, int length, int startIndex);
260 };
261
262 class ProgramInfo {
263  public:
264   ProgramInfo(byte* data, int index);
265   ProgramInfo(ushort programNumber, ushort programPid);
266   ushort ProgramNumber;
267   ushort ProgramPID;
268   byte* Data;
269  private:
270   byte* mData;
271 };
272
273 class TsPacket {
274  public:
275   TsPacket(void);
276   bool GetPriority(void);
277   void SetPriority(bool priority);
278   ushort GetPID(void);
279   void SetPID(ushort pid);
280   byte GetPointerSize(void);
281   byte* GetData(void);
282   void SetData(byte* data, int startIndex)throw(std::invalid_argument);
283   bool HasPcr(void);
284   Int64 GetPcr(void)throw(std::out_of_range);
285   byte* Payload(void);
286   void IncrementContinuityCounter(void);
287   byte GetContinuityCounter(void);
288   void SetContinuityCounter(byte value)throw(std::out_of_range);
289   bool HasPesHeader(void);
290  protected:
291   byte* mData;
292  private:
293   bool Priority;
294   ushort PID;
295 };
296
297 class PcrPacket : TsPacket {
298  public:
299   PcrPacket(Int64 pcr, byte counter, ushort pid);
300 };
301
302 class TsTable : TsPacket {
303  public:
304   TsTable();
305   TsTable(byte* data);
306   void AddData(byte* data, int offset, int len);
307   bool Complete(void);
308   byte GetTableId(void);
309   void SetTableId(byte value);
310   ushort GetNumberId(void);
311   void SetNumberId(ushort value);
312   ushort GetLength(void);
313   void SetLength(ushort value);
314  protected:
315   void RefreshCrc(void);
316 };
317
318 class PatPacket : TsTable {
319  public:
320   PatPacket(void);
321   PatPacket(byte* data);
322   ushort TransportStreamId;
323   ProgramInfo* Programs;
324  private:
325   ushort ProgramInfoLength;
326 };
327
328 class SitPacket : TsTable {
329  public:
330   SitPacket(void);
331   SitPacket(byte* data);
332 };
333
334 class PmPacket : TsTable {
335  public:
336   PmPacket(void);
337   PmPacket(byte* data);
338   DTCP_Descriptor DtcpInfo;
339   byte* ProgramDescriptorsData;
340   StreamInfo* ElementaryStreams;
341   ushort ProgramNumber;
342   ushort PcrPID;
343  private:
344   ushort ProgramDescriptorsLength;
345   ushort StreamInfoLength;
346 };
347
348 class PesHeader {
349  public:
350   PesHeader(byte* data);
351   byte StreamId;
352   byte GetByte(int i);
353   void SetByte(byte dat);
354   byte HeaderLength;
355   int TotalHeaderLength;
356   ushort PacketLength;
357   bool HasPts;
358   bool HasDts;
359   Int64 Pts;
360   Int64 Dts;
361   byte Extention2;
362   byte* Data;
363  private:
364   byte* mData;
365 };
366
367 class PesPacket {
368  public:
369   PesPacket(byte* buff, int offset, int length, ushort pid);
370   bool Priority;
371   byte* GetData(void);
372   byte* GetPayload(void);
373   byte GetByte(int i);
374   void SetByte(byte dat);
375   ushort PID;
376   bool Complete;
377   PesHeader GetHeader(void);
378   void AddData(std::list<byte> moredata);
379   void AddData(byte* buff, int offset, int length);
380   byte BaseId;
381   byte ExtendedId;
382   UInt32 ExtendedType;
383  private:
384   std::list<byte> data;
385   ushort pid;
386   bool priority;
387 };
388
389 class VC1SequenceInfo {
390  public:
391   VC1SequenceInfo(byte* data, int offset);
392   bool Valid;
393   VideoFormat GetVideoFormati(void);
394   void SetVideoFormat(VideoFormat videoformat);
395   AspectRatio GetAspectRatio(void);
396   void SetAspectRatio(AspectRatio aspectratio);
397   FrameRate GetFrameRate(void);
398   void SetFrameRate(FrameRate frameRate);
399  private:
400   byte* mData;
401   int Height;
402   int Width;
403   bool Interlaced;
404   bool DisplayExt;
405   bool AspectFlag;
406   byte Vc1AspectRatio;
407   bool FrameFlag;
408   bool FrameRateIndicatorFlag;
409   AspectRatio mAspectRatio;
410   VideoFormat mVideoFormat;
411   FrameRate   mFrameRate;
412 };
413
414 class H264Info {
415  public:
416   H264Info(byte* data, int offset);
417   byte* ElementaryDescriptors;
418   VideoFormat GetVideoFormati(void);
419   void SetVideoFormat(VideoFormat videoformat);
420   AspectRatio GetAspectRatio(void);
421   void SetAspectRatio(AspectRatio aspectratio);
422   FrameRate GetFrameRate(void);
423   void SetFrameRate(FrameRate frameRate);
424   AudioPresentationType GetAudioPresentationType(void);
425   void SetAudioPresentationType(AudioPresentationType audioPresentationTyp);
426   SamplingFrequency GetsamplingFrequency(void);
427   void SetSamplingFrequency(SamplingFrequency samplingFrequency);
428  private:
429   UInt32 GetNextExpGolomb();
430   void ScalingListSkip(int skip);
431   UInt32 Width;
432   UInt32 Heigth;
433   byte* HdmvVideoRegistrationDescriptor;
434   VideoFormat mVideoFormat;
435   AspectRatio mAspectRatio;
436   FrameRate   mFrameRate;
437   AudioPresentationType mAudioPresentationType;
438   SamplingFrequency mSamplingFrequency;
439 };
440
441 enum Ac3SyntaxType {
442         Invalid = 0,
443         Standard = 8,
444         Alternative = 6,
445         Enhanced = 16
446 };
447
448 class ElementaryParse {
449  public:
450   ElementaryParse(void);
451   virtual VideoFormat GetVideoFormati(void);
452   virtual void SetVideoFormat(VideoFormat videoformat);
453   virtual AspectRatio GetAspectRatio(void);
454   virtual void SetAspectRatio(AspectRatio aspectratio);
455   virtual FrameRate GetFrameRate(void);
456   virtual void SetFrameRate(FrameRate frameRate);
457   virtual AudioPresentationType GetAudioPresentationType(void);
458   virtual void SetAudioPresentationType(
459       AudioPresentationType audioPresentationTyp);
460   virtual SamplingFrequency GetsamplingFrequency(void);
461   virtual void SetSamplingFrequency(SamplingFrequency samplingFrequency);
462   virtual byte* GetElementaryDescriptors(void);
463  protected:
464   byte GetNextBit(void);
465   byte* mData;
466   int indicator;
467   bool mValid;
468   VideoFormat mVideoFormat;
469   FrameRate mFrameRate;
470   AspectRatio mAspectRatio;
471   SamplingFrequency mSamplingFrequency;
472   AudioPresentationType mAudioPresentationType;
473   byte* ElementaryDescriptors;
474 };
475
476 class AC3Info : ElementaryParse {
477  public:
478   int MaxFrameLength;
479   int FrameLength;
480   bool IndependentStream;
481   Ac3SyntaxType SyntaxType;
482   AC3Info(byte* data, int offset);
483   VideoFormat GetVideoFormati(void);
484   void SetVideoFormat(VideoFormat videoformat);
485   AspectRatio GetAspectRatio(void);
486   void SetAspectRatio(AspectRatio aspectratio);
487   FrameRate GetFrameRate(void);
488   void SetFrameRate(FrameRate frameRate);
489   AudioPresentationType GetAudioPresentationType(void);
490   void SetAudioPresentationType(AudioPresentationType audioPresentationTyp);
491   SamplingFrequency GetsamplingFrequency(void);
492   void SetSamplingFrequency(SamplingFrequency samplingFrequency);
493   byte* GetElementaryDescriptors(void);
494  private:
495   static readonly int* len48k;
496   static readonly int* len44k;
497   static readonly int* len32k;
498   byte Bsid;
499   byte Bsmod;
500   byte Acmod;
501 };
502
503 class DtsInfo : ElementaryParse {
504  public:
505   DtsInfo(byte* data, int offset);
506   VideoFormat GetVideoFormati(void);
507   void SetVideoFormat(VideoFormat videoformat);
508   AspectRatio GetAspectRatio(void);
509   void SetAspectRatio(AspectRatio aspectratio);
510   FrameRate GetFrameRate(void);
511   void SetFrameRate(FrameRate frameRate);
512   AudioPresentationType GetAudioPresentationType(void);
513   void SetAudioPresentationType( AudioPresentationType audioPresentationTyp);
514   SamplingFrequency GetsamplingFrequency(void);
515   void SetSamplingFrequency(SamplingFrequency samplingFrequency);
516   byte* GetElementaryDescriptors(void);
517   ushort FrameSize;
518   byte ExtAudioId;
519  private:
520   byte Amode;
521   byte SampleFreq;
522   bool ExtAudio;
523 };
524
525 class MlpInfo : ElementaryParse {
526  public:
527   MlpInfo(byte* data, int offset);
528   VideoFormat GetVideoFormati(void);
529   void SetVideoFormat(VideoFormat videoformat);
530   AspectRatio GetAspectRatio(void);
531   void SetAspectRatio(AspectRatio aspectratio);
532   FrameRate GetFrameRate(void);
533   void SetFrameRate(FrameRate frameRate);
534   AudioPresentationType GetAudioPresentationType(void);
535   void SetAudioPresentationType(AudioPresentationType audioPresentationTyp);
536   SamplingFrequency GetsamplingFrequency(void);
537   void SetSamplingFrequency(SamplingFrequency samplingFrequency);
538   byte* GetElementaryDescriptors(void);
539  private:
540   byte* AC3AudioDescriptor;
541   byte SampleRateCode;
542 };
543
544 class Mpeg2Info : ElementaryParse {
545  public:
546   Mpeg2Info(byte* 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   byte* GetElementaryDescriptors(void);
558  private:
559   byte* Mpeg2VideoRegistrationDescriptor;
560   ushort Horizontal;
561   ushort Vertical;
562   byte Aspect;
563   byte FrameRateCode;
564   bool Progressive;
565 };
566
567 } //namespace
568 #endif TSREMUXCPP_UTILS_H_