Advertisement
asghaer

Untitled

Jun 27th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.76 KB | None | 0 0
  1.  
  2. #include <vector>
  3. #include <fstream>
  4. #include <bitset>
  5. #include <cstring>
  6.  
  7. /*error code*/
  8. #define MUXTS_ERROR_FILE_NOTTS -1   /// when file is not .ts file
  9. #define MUXTS_ERROR_FILE_NULL -2    ///when file string is null
  10. #define MUXTS_ERRTR_PAT_NOPROGRAM -3 /// can not find program
  11. #define MUXTS_ERROR_FILLPAYLOAD_OVER -4 ///exceeds memory size
  12. #define MUXTS_ERROR_OUTPUT_NOTFULL -5 ///packet is not moved to output
  13. #define MUXTS_ERRTR_PMT_NOSTREAM -6 /// program stream is not available
  14. #define MUXTS_ERRTR_CODEC_NOTUSE -7 ///unavailable encoding format
  15.  
  16. /**/
  17. #define BYTE unsigned char
  18. #define BYTE4 unsigned int
  19. #define BYTE8 unsigned long long
  20. #define uint64_t unsigned long long int
  21. #define uint32_t unsigned int
  22. #define uint8_t unsigned char
  23.  
  24. /*whether to enable the size of of endian*/
  25. #define MUXTS_ENABLE_SWAP_ENDIAN32
  26. #define MUXTS_ENABLE_SWAP_ENDIAN64
  27. ///enable writing to DTS. DTS and PTS are same
  28. #define MUXTS_ENABLE_WRITE_DTS
  29.  
  30. /**/
  31. #define MUXTS_TS_PACKET_SIZE 188 ///Mux Packet Size
  32. enum AVCODEC
  33. {
  34.     MUXTS_CODEC_HEVC = 1,
  35.     MUXTS_CODEC_H264,
  36.     MUXTS_CODEC_AAC
  37. };
  38. #define MUXTS_CODEC_H265 MUXTS_CODEC_HEVC
  39. #define MUXTS_TYPE_AUDIO 17
  40. #define MUXTS_TYPE_VIDEO 18
  41. #define MUXTS_PAT_SPACEPAT 42
  42. /**/
  43. #define BITSET(x,y,z) ((x <<= y)+(x |= z))
  44. #define INCOUNT(x) ((++x)&=0x0f)
  45.  
  46. struct TS_HEADER
  47. {
  48.     unsigned sync_byte : 8; ///sync byte:0x47
  49.     unsigned transport_error_indicator : 1; /// packet has atleast 1 bit transmission error
  50.     unsigned payload_unit_start_indicator : 1; ///payload start indicator
  51.     unsigned transport_priority : 1; /// transmission priority flag
  52.     unsigned PID : 13;  /// packet ID
  53.     unsigned transport_scrambling_control : 2; /// encryption flag.
  54.     unsigned adaptation_field_control : 2;  ///adaptation filed
  55.     unsigned continuity_counter : 4; ///continuity counter
  56. };
  57.  
  58. struct TS_PAT_Program
  59. {
  60.     unsigned program_number : 16;  ///Pogram number
  61.     unsigned reserved_3 : 3; ///Reserved bits
  62.     unsigned program_map_PID : 13; ///PID of PMT. It is greater than 1 when multiple programs
  63. };
  64.  
  65. struct TS_PAT
  66. {
  67.     unsigned table_id : 8;
  68.     unsigned section_syntax_indicator : 1;
  69.     unsigned zero : 1; //0
  70.     unsigned reserved_1 : 2;
  71.     unsigned section_length : 12;
  72.     unsigned transport_stream_id : 16;
  73.     unsigned reserved_2 : 2;
  74.     unsigned version_number : 5;
  75.     unsigned current_next_indicator : 1;
  76.     unsigned section_number : 8;
  77.     unsigned last_section_number : 8;  
  78.  
  79.     std::vector<TS_PAT_Program> program;
  80.  
  81.     unsigned CRC_32 : 32;  //CRC32
  82. };
  83.  
  84. struct TS_PMT_Stream
  85. {
  86.     unsigned stream_type : 8;
  87.     unsigned reserved_5 : 3; //0x07
  88.     unsigned elementary_PID : 13;
  89.     unsigned reserved_6 : 4; //0x0F
  90.     unsigned ES_info_length : 12;
  91.     unsigned descriptor;
  92. };
  93.  
  94. struct TS_PMT
  95. {
  96.     unsigned table_id : 8;
  97.     unsigned section_syntax_indicator : 1;
  98.     unsigned zero : 1; //0x01
  99.     unsigned reserved_1 : 2; //0x03
  100.     unsigned section_length : 12;
  101.     unsigned program_number : 16;
  102.     unsigned reserved_2 : 2; //0x03
  103.     unsigned version_number : 5;
  104.     unsigned current_next_indicator : 1;
  105.     unsigned section_number : 8;
  106.     unsigned last_section_number : 8;
  107.     unsigned reserved_3 : 3; //0x07
  108.     unsigned PCR_PID : 13;
  109.  
  110.     unsigned reserved_4 : 4;
  111.     unsigned program_info_length : 12;
  112.  
  113.     std::vector<TS_PMT_Stream> PMT_Stream;  
  114.  
  115.     unsigned CRC_32 : 32;
  116. };
  117.  
  118. struct TS_PCR
  119. {
  120.     uint64_t program_clock_reference_base : 33;
  121.     uint64_t reserved_1 : 6;
  122.     uint64_t program_clock_reference_extension : 9;
  123. };
  124.  
  125. //TS_ADAPTATION 格式参考 https://en.wikipedia.org/wiki/MPEG_transport_stream
  126. struct TS_ADAPTATION
  127. {
  128.     unsigned adaptation_field_length : 8; //
  129.     unsigned discontinuity_indicator : 1; //
  130.     unsigned random_access_indicator : 1;
  131.     unsigned elementary_stream_priority_indicator : 1; //
  132.     unsigned PCR_flag : 1; //1
  133.     unsigned OPCR_flag : 1; //
  134.     unsigned splicing_point_flag : 1; //
  135.     unsigned transport_private_data_flag : 1; //
  136.     unsigned adaptation_field_extension_flag : 1; //
  137.     TS_PCR PCR;
  138. };
  139.  
  140. struct TS_PES_PTS
  141. {
  142.     unsigned head : 4; //
  143.     unsigned pts1 : 3; //pts
  144.     unsigned resvered_1 : 1; //1
  145.     unsigned pts2 : 15; //pts
  146.     unsigned resvered_2 : 1; //1
  147.     unsigned pts3 : 15; //pts
  148.     unsigned resvered_3 : 1; //1
  149. };
  150.  
  151. struct TS_PES_DTS
  152. {
  153.     unsigned head : 4; //
  154.     unsigned pts1 : 3; //pts
  155.     unsigned resvered_1 : 1; //1
  156.     unsigned pts2 : 15; //pts
  157.     unsigned resvered_2 : 1; //1
  158.     unsigned pts3 : 15; //pts 14
  159.     unsigned resvered_3 : 1; //1
  160. };
  161.  
  162. // https://en.wikipedia.org/wiki/Packetized_elementary_stream
  163. struct TS_PES
  164. {
  165.     unsigned packet_start_code_prefix : 24;
  166.     unsigned stream_id : 8;
  167.     unsigned PESPacket_length : 16;
  168.     unsigned marker_bits : 2; //10 binary or 0x02 hex
  169.     unsigned PES_scrambling_control : 2;
  170.     unsigned PES_priority : 1;
  171.     unsigned data_alignment_indicator : 1;
  172.     unsigned copyright : 1;
  173.     unsigned original_or_copy : 1;
  174.     unsigned PTS_DTS_flags : 2; //PTS DTS indicator 11 = both present, 01 is forbidden, 10 = only PTS, 00 = no PTS or DTS
  175.     unsigned ESCR_flag : 1;
  176.     unsigned ES_rate_flag : 1;
  177.     unsigned DSM_trick_mode_flag : 1;
  178.     unsigned additional_copy_info_flag : 1;
  179.     unsigned PES_CRC_flag : 1;
  180.     unsigned PES_extension_flag : 1;
  181.     unsigned PES_header_data_length : 8;
  182. };
  183.  
  184. struct TSData
  185. {
  186.     BYTE *buff;
  187.     int seek;
  188. };
  189.  
  190. struct INFO_PES
  191. {
  192.     unsigned PID;
  193.     unsigned count;
  194.     unsigned stream_type;
  195.     unsigned stream_id;
  196.     unsigned aorv; //type :MUXTS_TYPE_VIDEO or MUXTS_TYPE_AUDIO
  197. };
  198.  
  199. struct INFO_PMT
  200. {
  201.     unsigned PID;
  202.     unsigned count;
  203.     unsigned PES_Quantity;
  204.     unsigned program_number;
  205.     std::vector<INFO_PES> pes;
  206. };
  207. struct INFO_PAT
  208. {
  209.     unsigned PID = 0;
  210.     unsigned count;
  211.     unsigned PMT_Quantity;
  212.     //std::vector<INFO_PMT> pmt;
  213.     INFO_PMT pmt;
  214. };
  215. class MuxTs
  216. {
  217. public:
  218.     MuxTs();
  219.     ~MuxTs();
  220. public:
  221.     int CreateFile(const char* filename);
  222.  
  223.     int CloseFile();
  224. public:
  225.     int AddNewProgram(unsigned PID, unsigned number);
  226.     int AddNewStream(unsigned PID, AVCODEC codec);
  227.     int WriteFrame(unsigned char * buffer, int len, uint64_t pts, AVCODEC type);
  228. private:
  229.     int WritePacketHeader(unsigned PID, unsigned payload_unit_start_indicator, unsigned adaptation_field_control, unsigned continuity_counter);
  230.     int WritePAT(std::vector<TS_PAT_Program> &program, bool has_payload_unit_start_indicator);
  231.     int WritePMT(std::vector<TS_PMT_Stream> PMT_Stream, int program_number, bool has_payload_unit_start_indicator);
  232.     int WriteAdaptation(unsigned length, bool PCR_flag, TS_PCR pcr);
  233.     int WritePES(unsigned length, unsigned stream_id, unsigned PTS_DTS_flag, unsigned data_length, TS_PES_PTS ts_pts);
  234. private:
  235.     int SwapBigLittleEndian(char *ptr, int bitlen);
  236.     int SetCRC32(int begin);
  237.     int FillPayload(int count);
  238.     int Output2File();
  239. private:
  240.     int ret;
  241.     std::ofstream m_ofile;
  242.     TSData data;
  243.     INFO_PAT m_pat;
  244.     int pes_index_audio;
  245.     int pes_index_video;
  246.     unsigned m_packetCount;
  247.     std::vector<TS_PAT_Program> _pmt;
  248.     std::vector<TS_PMT_Stream> _pes;
  249. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement