Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <fstream>
- #include <bitset>
- #include <cstring>
- /*error code*/
- #define MUXTS_ERROR_FILE_NOTTS -1 /// when file is not .ts file
- #define MUXTS_ERROR_FILE_NULL -2 ///when file string is null
- #define MUXTS_ERRTR_PAT_NOPROGRAM -3 /// can not find program
- #define MUXTS_ERROR_FILLPAYLOAD_OVER -4 ///exceeds memory size
- #define MUXTS_ERROR_OUTPUT_NOTFULL -5 ///packet is not moved to output
- #define MUXTS_ERRTR_PMT_NOSTREAM -6 /// program stream is not available
- #define MUXTS_ERRTR_CODEC_NOTUSE -7 ///unavailable encoding format
- /**/
- #define BYTE unsigned char
- #define BYTE4 unsigned int
- #define BYTE8 unsigned long long
- #define uint64_t unsigned long long int
- #define uint32_t unsigned int
- #define uint8_t unsigned char
- /*whether to enable the size of of endian*/
- #define MUXTS_ENABLE_SWAP_ENDIAN32
- #define MUXTS_ENABLE_SWAP_ENDIAN64
- ///enable writing to DTS. DTS and PTS are same
- #define MUXTS_ENABLE_WRITE_DTS
- /**/
- #define MUXTS_TS_PACKET_SIZE 188 ///Mux Packet Size
- enum AVCODEC
- {
- MUXTS_CODEC_HEVC = 1,
- MUXTS_CODEC_H264,
- MUXTS_CODEC_AAC
- };
- #define MUXTS_CODEC_H265 MUXTS_CODEC_HEVC
- #define MUXTS_TYPE_AUDIO 17
- #define MUXTS_TYPE_VIDEO 18
- #define MUXTS_PAT_SPACEPAT 42
- /**/
- #define BITSET(x,y,z) ((x <<= y)+(x |= z))
- #define INCOUNT(x) ((++x)&=0x0f)
- struct TS_HEADER
- {
- unsigned sync_byte : 8; ///sync byte:0x47
- unsigned transport_error_indicator : 1; /// packet has atleast 1 bit transmission error
- unsigned payload_unit_start_indicator : 1; ///payload start indicator
- unsigned transport_priority : 1; /// transmission priority flag
- unsigned PID : 13; /// packet ID
- unsigned transport_scrambling_control : 2; /// encryption flag.
- unsigned adaptation_field_control : 2; ///adaptation filed
- unsigned continuity_counter : 4; ///continuity counter
- };
- struct TS_PAT_Program
- {
- unsigned program_number : 16; ///Pogram number
- unsigned reserved_3 : 3; ///Reserved bits
- unsigned program_map_PID : 13; ///PID of PMT. It is greater than 1 when multiple programs
- };
- struct TS_PAT
- {
- unsigned table_id : 8;
- unsigned section_syntax_indicator : 1;
- unsigned zero : 1; //0
- unsigned reserved_1 : 2;
- unsigned section_length : 12;
- unsigned transport_stream_id : 16;
- unsigned reserved_2 : 2;
- unsigned version_number : 5;
- unsigned current_next_indicator : 1;
- unsigned section_number : 8;
- unsigned last_section_number : 8;
- std::vector<TS_PAT_Program> program;
- unsigned CRC_32 : 32; //CRC32
- };
- struct TS_PMT_Stream
- {
- unsigned stream_type : 8;
- unsigned reserved_5 : 3; //0x07
- unsigned elementary_PID : 13;
- unsigned reserved_6 : 4; //0x0F
- unsigned ES_info_length : 12;
- unsigned descriptor;
- };
- struct TS_PMT
- {
- unsigned table_id : 8;
- unsigned section_syntax_indicator : 1;
- unsigned zero : 1; //0x01
- unsigned reserved_1 : 2; //0x03
- unsigned section_length : 12;
- unsigned program_number : 16;
- unsigned reserved_2 : 2; //0x03
- unsigned version_number : 5;
- unsigned current_next_indicator : 1;
- unsigned section_number : 8;
- unsigned last_section_number : 8;
- unsigned reserved_3 : 3; //0x07
- unsigned PCR_PID : 13;
- unsigned reserved_4 : 4;
- unsigned program_info_length : 12;
- std::vector<TS_PMT_Stream> PMT_Stream;
- unsigned CRC_32 : 32;
- };
- struct TS_PCR
- {
- uint64_t program_clock_reference_base : 33;
- uint64_t reserved_1 : 6;
- uint64_t program_clock_reference_extension : 9;
- };
- //TS_ADAPTATION ζ ΌεΌεθ https://en.wikipedia.org/wiki/MPEG_transport_stream
- struct TS_ADAPTATION
- {
- unsigned adaptation_field_length : 8; //
- unsigned discontinuity_indicator : 1; //
- unsigned random_access_indicator : 1;
- unsigned elementary_stream_priority_indicator : 1; //
- unsigned PCR_flag : 1; //1
- unsigned OPCR_flag : 1; //
- unsigned splicing_point_flag : 1; //
- unsigned transport_private_data_flag : 1; //
- unsigned adaptation_field_extension_flag : 1; //
- TS_PCR PCR;
- };
- struct TS_PES_PTS
- {
- unsigned head : 4; //
- unsigned pts1 : 3; //pts
- unsigned resvered_1 : 1; //1
- unsigned pts2 : 15; //pts
- unsigned resvered_2 : 1; //1
- unsigned pts3 : 15; //pts
- unsigned resvered_3 : 1; //1
- };
- struct TS_PES_DTS
- {
- unsigned head : 4; //
- unsigned pts1 : 3; //pts
- unsigned resvered_1 : 1; //1
- unsigned pts2 : 15; //pts
- unsigned resvered_2 : 1; //1
- unsigned pts3 : 15; //pts 14
- unsigned resvered_3 : 1; //1
- };
- // https://en.wikipedia.org/wiki/Packetized_elementary_stream
- struct TS_PES
- {
- unsigned packet_start_code_prefix : 24;
- unsigned stream_id : 8;
- unsigned PESPacket_length : 16;
- unsigned marker_bits : 2; //10 binary or 0x02 hex
- unsigned PES_scrambling_control : 2;
- unsigned PES_priority : 1;
- unsigned data_alignment_indicator : 1;
- unsigned copyright : 1;
- unsigned original_or_copy : 1;
- unsigned PTS_DTS_flags : 2; //PTS DTS indicator 11 = both present, 01 is forbidden, 10 = only PTS, 00 = no PTS or DTS
- unsigned ESCR_flag : 1;
- unsigned ES_rate_flag : 1;
- unsigned DSM_trick_mode_flag : 1;
- unsigned additional_copy_info_flag : 1;
- unsigned PES_CRC_flag : 1;
- unsigned PES_extension_flag : 1;
- unsigned PES_header_data_length : 8;
- };
- struct TSData
- {
- BYTE *buff;
- int seek;
- };
- struct INFO_PES
- {
- unsigned PID;
- unsigned count;
- unsigned stream_type;
- unsigned stream_id;
- unsigned aorv; //type :MUXTS_TYPE_VIDEO or MUXTS_TYPE_AUDIO
- };
- struct INFO_PMT
- {
- unsigned PID;
- unsigned count;
- unsigned PES_Quantity;
- unsigned program_number;
- std::vector<INFO_PES> pes;
- };
- struct INFO_PAT
- {
- unsigned PID = 0;
- unsigned count;
- unsigned PMT_Quantity;
- //std::vector<INFO_PMT> pmt;
- INFO_PMT pmt;
- };
- class MuxTs
- {
- public:
- MuxTs();
- ~MuxTs();
- public:
- int CreateFile(const char* filename);
- int CloseFile();
- public:
- int AddNewProgram(unsigned PID, unsigned number);
- int AddNewStream(unsigned PID, AVCODEC codec);
- int WriteFrame(unsigned char * buffer, int len, uint64_t pts, AVCODEC type);
- private:
- int WritePacketHeader(unsigned PID, unsigned payload_unit_start_indicator, unsigned adaptation_field_control, unsigned continuity_counter);
- int WritePAT(std::vector<TS_PAT_Program> &program, bool has_payload_unit_start_indicator);
- int WritePMT(std::vector<TS_PMT_Stream> PMT_Stream, int program_number, bool has_payload_unit_start_indicator);
- int WriteAdaptation(unsigned length, bool PCR_flag, TS_PCR pcr);
- int WritePES(unsigned length, unsigned stream_id, unsigned PTS_DTS_flag, unsigned data_length, TS_PES_PTS ts_pts);
- private:
- int SwapBigLittleEndian(char *ptr, int bitlen);
- int SetCRC32(int begin);
- int FillPayload(int count);
- int Output2File();
- private:
- int ret;
- std::ofstream m_ofile;
- TSData data;
- INFO_PAT m_pat;
- int pes_index_audio;
- int pes_index_video;
- unsigned m_packetCount;
- std::vector<TS_PAT_Program> _pmt;
- std::vector<TS_PMT_Stream> _pes;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement