Advertisement
asghaer

Untitled

Jun 27th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.90 KB | None | 0 0
  1.  
  2. #include "MuxTs.h"
  3.  
  4. using namespace std;
  5.  
  6. static uint32_t crc32table[256] = {
  7.     0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b,
  8.     0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
  9.     0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd, 0x4c11db70, 0x48d0c6c7,
  10.     0x4593e01e, 0x4152fda9, 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75,
  11.     0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3,
  12.     0x709f7b7a, 0x745e66cd, 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039,
  13.     0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5, 0xbe2b5b58, 0xbaea46ef,
  14.     0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d,
  15.     0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49, 0xc7361b4c, 0xc3f706fb,
  16.     0xceb42022, 0xca753d95, 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1,
  17.     0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d, 0x34867077, 0x30476dc0,
  18.     0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072,
  19.     0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 0x018aeb13, 0x054bf6a4,
  20.     0x0808d07d, 0x0cc9cdca, 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde,
  21.     0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02, 0x5e9f46bf, 0x5a5e5b08,
  22.     0x571d7dd1, 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
  23.     0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b, 0xbb60adfc,
  24.     0xb6238b25, 0xb2e29692, 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6,
  25.     0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a, 0xe0b41de7, 0xe4750050,
  26.     0xe9362689, 0xedf73b3e, 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2,
  27.     0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34,
  28.     0xdc3abded, 0xd8fba05a, 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637,
  29.     0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb, 0x4f040d56, 0x4bc510e1,
  30.     0x46863638, 0x42472b8f, 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53,
  31.     0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5,
  32.     0x3f9b762c, 0x3b5a6b9b, 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff,
  33.     0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623, 0xf12f560e, 0xf5ee4bb9,
  34.     0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b,
  35.     0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd,
  36.     0xcda1f604, 0xc960ebb3, 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7,
  37.     0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b, 0x9b3660c6, 0x9ff77d71,
  38.     0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3,
  39.     0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 0x4e8ee645, 0x4a4ffbf2,
  40.     0x470cdd2b, 0x43cdc09c, 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8,
  41.     0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24, 0x119b4be9, 0x155a565e,
  42.     0x18197087, 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
  43.     0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d, 0x2056cd3a,
  44.     0x2d15ebe3, 0x29d4f654, 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0,
  45.     0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c, 0xe3a1cbc1, 0xe760d676,
  46.     0xea23f0af, 0xeee2ed18, 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4,
  47.     0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662,
  48.     0x933eb0bb, 0x97ffad0c, 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668,
  49.     0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
  50. };
  51.  
  52.  
  53. uint32_t CRC32(const uint8_t *data, int len)
  54. {
  55.     int i;
  56.     uint32_t crc = 0xFFFFFFFF;
  57.     for (i = 0; i < len; i++)
  58.         crc = (crc << 8) ^ crc32table[((crc >> 24) ^ *data++) & 0xFF];
  59.     return crc;
  60. }
  61.  
  62.  
  63. MuxTs::MuxTs()
  64. {
  65. }
  66.  
  67.  
  68. MuxTs::~MuxTs()
  69. {
  70. }
  71.  
  72. int MuxTs::CreateFile(const char* filename)
  73. {
  74.  
  75.     if (NULL == filename)
  76.     {
  77.         return MUXTS_ERROR_FILE_NULL;
  78.     }
  79.     int namelen = strlen(filename);
  80.     if (!((filename[namelen - 1] == 'S') || (filename[namelen - 1] == 's')
  81.         && (filename[namelen - 2] == 'T') || (filename[namelen - 2] == 't')
  82.         && (filename[namelen - 3] == '.')))
  83.     {
  84.         return MUXTS_ERROR_FILE_NOTTS;
  85.     }
  86.     data.buff = new BYTE[MUXTS_TS_PACKET_SIZE];
  87.     data.seek = 0;
  88.     m_ofile.open(filename, ios::out | ios::binary | ios::trunc);
  89.     //init pat
  90.     m_pat.PID = 0;
  91.     m_pat.count = 0;
  92.     m_pat.PMT_Quantity = 0;
  93.     //init
  94.     m_packetCount = 0;
  95.     vector<TS_PAT_Program> _pmt_s;
  96.     _pmt.swap(_pmt_s);
  97.     vector<TS_PMT_Stream> _pes_s;
  98.     _pes.swap(_pes_s);
  99.     return 0;
  100. }
  101.  
  102. int MuxTs::CloseFile()
  103. {
  104.     delete[] data.buff;
  105.     m_ofile.close();
  106.     return 0;
  107. }
  108.  
  109. int MuxTs::AddNewProgram(unsigned PID, unsigned number)
  110. {
  111.     m_pat.pmt.PID = PID;
  112.     m_pat.pmt.count = 0;
  113.     m_pat.pmt.PES_Quantity = 0;
  114.     m_pat.pmt.program_number = number;
  115.     return 0;
  116. }
  117.  
  118. int MuxTs::AddNewStream(unsigned PID, AVCODEC codec)
  119. {
  120.     INFO_PES pes;
  121.     pes.PID = PID;
  122.     pes.count = 0;
  123.     switch (codec)
  124.     {
  125.     case MUXTS_CODEC_HEVC:
  126.         pes.stream_id = 0xe0;
  127.         pes.aorv = MUXTS_TYPE_VIDEO;
  128.         pes.stream_type = 0x24;
  129.         pes_index_video = m_pat.pmt.pes.size();
  130.         break;
  131.     case MUXTS_CODEC_H264:
  132.         pes.stream_id = 0xe0;
  133.         pes.aorv = MUXTS_TYPE_VIDEO;
  134.         pes.stream_type = 0x1b;
  135.         pes_index_video = m_pat.pmt.pes.size();
  136.         break;
  137.     case MUXTS_CODEC_AAC:
  138.         pes.stream_id = 0xc0;
  139.         pes.aorv = MUXTS_TYPE_AUDIO;
  140.         pes.stream_type = 0x0f;
  141.         pes_index_audio = m_pat.pmt.pes.size();
  142.         break;
  143.     default:
  144.         return MUXTS_ERRTR_CODEC_NOTUSE;
  145.         break;
  146.     }
  147.     m_pat.pmt.PES_Quantity++;
  148.     m_pat.pmt.pes.push_back(pes);
  149.     return 0;
  150. }
  151.  
  152. int MuxTs::WriteFrame(unsigned char * buffer, int len, uint64_t pts, AVCODEC type)
  153. {
  154.     unsigned char *pbuf = buffer;
  155.     int length = len;
  156.     unsigned pesLen = 0;
  157.     bool havePesHeader = false;
  158.     unsigned index;
  159.     if (MUXTS_CODEC_H264 == type || MUXTS_CODEC_HEVC == type)
  160.     {
  161.         index = pes_index_video;
  162.     }
  163.     else if (MUXTS_CODEC_AAC == type)
  164.     {
  165.         index = pes_index_audio;
  166.         pesLen = length + 8;
  167.     }
  168.     while (length > 0)
  169.     {
  170.         if (0 == m_packetCount)
  171.         {
  172.             //set _pmt
  173.             TS_PAT_Program _pgm;
  174.             _pgm.program_number = m_pat.pmt.program_number;
  175.             _pgm.program_map_PID = m_pat.pmt.PID;
  176.             _pgm.reserved_3 = 0xff;
  177.             _pmt.push_back(_pgm);
  178.             //set _pes
  179.             for (unsigned i = 0; i < m_pat.pmt.PES_Quantity; i++)
  180.             {
  181.                 TS_PMT_Stream _st;
  182.                 _st.elementary_PID = m_pat.pmt.pes[i].PID;
  183.                 _st.stream_type = m_pat.pmt.pes[i].stream_type;
  184.                 _st.ES_info_length = 0x00;
  185.                 _st.reserved_5 = 0xff;
  186.                 _st.reserved_6 = 0xff;
  187.                 _pes.push_back(_st);
  188.             }
  189.         }
  190.         //insert pat packet and pmt packet
  191.         if (0 == m_packetCount%MUXTS_PAT_SPACEPAT)
  192.         {
  193.             this->WritePacketHeader(m_pat.PID, 0x01, 0x01, m_pat.count);
  194.             this->WritePAT(_pmt, true);
  195.             INCOUNT(m_pat.count);
  196.             m_packetCount++;
  197.             this->WritePacketHeader(m_pat.pmt.PID, 0x01, 0x01, m_pat.pmt.count);
  198.             this->WritePMT(_pes, m_pat.pmt.program_number, true);
  199.             INCOUNT(m_pat.pmt.count);
  200.             m_packetCount++;
  201.         }
  202.         else if (!havePesHeader)
  203.         {
  204.             havePesHeader = true;
  205.             this->WritePacketHeader(m_pat.pmt.pes[index].PID, 0x01, 0x03, m_pat.pmt.pes[index].count);
  206.             TS_PCR pcr;
  207.             pcr.program_clock_reference_base = pts;
  208.             pcr.reserved_1 = 0xff;
  209.             pcr.program_clock_reference_extension = 0x00;
  210.             //AAC need PCR?
  211.             if (MUXTS_CODEC_AAC == type)
  212.             {
  213.                 this->WriteAdaptation(0x01, false, pcr);
  214.             }
  215.             else
  216.             {
  217.                 this->WriteAdaptation(0x07, true, pcr);
  218.             }
  219.             TS_PES_PTS s_pts;
  220.             s_pts.head = 0x02;
  221.             s_pts.resvered_1 = 0xff;
  222.             s_pts.resvered_2 = 0xff;
  223.             s_pts.resvered_3 = 0xff;
  224.             s_pts.pts1 = pts >> 30;
  225.             s_pts.pts2 = pts >> 15;
  226.             s_pts.pts3 = pts;
  227.             if (MUXTS_CODEC_AAC == type)
  228.             {
  229.                 this->WritePES(pesLen, m_pat.pmt.pes[index].stream_id, 0x02, 0x05, s_pts);
  230.             }
  231.             else if (MUXTS_CODEC_H264 == type || MUXTS_CODEC_HEVC == type)
  232.             {
  233.                 this->WritePES(pesLen, m_pat.pmt.pes[index].stream_id, 0x02, 0x05, s_pts);
  234.             }
  235.             INCOUNT(m_pat.pmt.pes[index].count);
  236.             m_packetCount++;
  237.            
  238.             if (length >= MUXTS_TS_PACKET_SIZE - data.seek)
  239.             {
  240.                 memcpy(data.buff + data.seek, pbuf, MUXTS_TS_PACKET_SIZE - data.seek);
  241.                 pbuf += MUXTS_TS_PACKET_SIZE - data.seek;
  242.                 length -= MUXTS_TS_PACKET_SIZE - data.seek;
  243.                 data.seek += MUXTS_TS_PACKET_SIZE - data.seek;
  244.             }
  245.             this->Output2File();
  246.         }
  247.         else
  248.         {
  249.             if (length >= MUXTS_TS_PACKET_SIZE - 4)
  250.             {
  251.                 this->WritePacketHeader(m_pat.pmt.pes[index].PID, 0x00, 0x01, m_pat.pmt.pes[index].count);
  252.                 memcpy(data.buff + data.seek, pbuf, MUXTS_TS_PACKET_SIZE - data.seek);
  253.                 pbuf += MUXTS_TS_PACKET_SIZE - data.seek;
  254.                 length -= MUXTS_TS_PACKET_SIZE - data.seek;
  255.                 data.seek += MUXTS_TS_PACKET_SIZE - data.seek;
  256.             }
  257.             else
  258.             {
  259.                 this->WritePacketHeader(m_pat.pmt.pes[index].PID, 0x00, 0x03, m_pat.pmt.pes[index].count);
  260.                 this->WriteAdaptation(MUXTS_TS_PACKET_SIZE - length - 5, false, TS_PCR());
  261.                 memcpy(data.buff + data.seek, pbuf, MUXTS_TS_PACKET_SIZE - data.seek);
  262.                 pbuf += MUXTS_TS_PACKET_SIZE - data.seek;
  263.                 length -= MUXTS_TS_PACKET_SIZE - data.seek;
  264.                 data.seek += MUXTS_TS_PACKET_SIZE - data.seek;
  265.             }
  266.             INCOUNT(m_pat.pmt.pes[index].count);
  267.             m_packetCount++;
  268.             this->Output2File();
  269.         }
  270.     }
  271.     return 0;
  272. }
  273.  
  274. int MuxTs::WritePacketHeader(unsigned PID, unsigned payload_unit_start_indicator, unsigned adaptation_field_control, unsigned continuity_counter)
  275. {
  276.     TS_HEADER ts_header;
  277.     ts_header.sync_byte = 0x47;
  278.     ts_header.transport_error_indicator = 0x00;
  279.     ts_header.payload_unit_start_indicator = payload_unit_start_indicator;
  280.     ts_header.transport_priority = 0x00;
  281.     ts_header.PID = PID;
  282.     ts_header.transport_scrambling_control = 0x00;
  283.     ts_header.adaptation_field_control = adaptation_field_control;
  284.     ts_header.continuity_counter = continuity_counter;
  285.     BYTE4 bit4 = 0x00;
  286.     BITSET(bit4, 0, ts_header.sync_byte);
  287.     BITSET(bit4, 1, ts_header.transport_error_indicator);
  288.     BITSET(bit4, 1, ts_header.payload_unit_start_indicator);
  289.     BITSET(bit4, 1, ts_header.transport_priority);
  290.     BITSET(bit4, 13, ts_header.PID);
  291.     BITSET(bit4, 2, ts_header.transport_scrambling_control);
  292.     BITSET(bit4, 2, ts_header.adaptation_field_control);
  293.     BITSET(bit4, 4, ts_header.continuity_counter);
  294.     ret = this->SwapBigLittleEndian((char*)&bit4, 32);
  295.     if (ret < 0)
  296.     {
  297.         return ret;
  298.     }
  299.     memcpy(data.buff, &bit4, 4);
  300.     data.seek = 4;
  301.     if (0x01 == payload_unit_start_indicator && 0x01 == adaptation_field_control)
  302.     {
  303.         const char p = 0x00;
  304.         //m_ofile.write(&p, 1);
  305.         memcpy(data.buff+data.seek, &p, 1);
  306.         data.seek++;
  307.     }
  308.     return 0;
  309. }
  310.  
  311. int MuxTs::WritePAT(std::vector<TS_PAT_Program>& program, bool has_payload_unit_start_indicator)
  312. {
  313.     if (program.empty())
  314.     {
  315.         return MUXTS_ERRTR_PAT_NOPROGRAM;
  316.     }
  317.     int CRC32begin = 0;
  318.     TS_PAT ts_pat;
  319.     ts_pat.table_id = 0x00;
  320.     ts_pat.section_syntax_indicator = 0xff;
  321.     ts_pat.zero = 0x00;
  322.     ts_pat.reserved_1 = 0xff;
  323.     unsigned length = 8;
  324.     if (has_payload_unit_start_indicator)
  325.     {
  326.         CRC32begin = 5;
  327.         length++;
  328.     }
  329.     else
  330.     {
  331.         CRC32begin = 4;
  332.     }
  333.     length += 4 * program.size();
  334.     ts_pat.section_length = length;
  335.     ts_pat.transport_stream_id = 0x0001;
  336.     ts_pat.reserved_2 = 0xff;
  337.     ts_pat.version_number = 0x00;
  338.     ts_pat.current_next_indicator = 0xff;
  339.     ts_pat.section_number = 0x00;
  340.     ts_pat.last_section_number = 0x00;
  341.     ts_pat.program = program;
  342.     ts_pat.CRC_32 = 1;
  343.     BYTE8 bit8 = 0x00;
  344.     BITSET(bit8, 0, ts_pat.table_id);
  345.     BITSET(bit8, 1, ts_pat.section_syntax_indicator);
  346.     BITSET(bit8, 1, ts_pat.zero);
  347.     BITSET(bit8, 2, ts_pat.reserved_1);
  348.     BITSET(bit8, 12, ts_pat.section_length);
  349.     BITSET(bit8, 16, ts_pat.transport_stream_id);
  350.     BITSET(bit8, 2, ts_pat.reserved_2);
  351.     BITSET(bit8, 5, ts_pat.version_number);
  352.     BITSET(bit8, 1, ts_pat.current_next_indicator);
  353.     BITSET(bit8, 8, ts_pat.section_number);
  354.     BITSET(bit8, 8, ts_pat.last_section_number);
  355.     ret = this->SwapBigLittleEndian((char*)&bit8, 64);
  356.     if (ret < 0)
  357.     {
  358.         return ret;
  359.     }
  360.     memcpy(data.buff + data.seek, &bit8, 8);
  361.     data.seek += 8;
  362.     vector<TS_PAT_Program>::iterator iter;
  363.     for (iter = program.begin(); iter != program.end(); ++iter)
  364.     {
  365.         BYTE4 bit4 = 0x00;
  366.         BITSET(bit4, 0, iter->program_number);
  367.         BITSET(bit4, 3, iter->reserved_3);
  368.         BITSET(bit4, 13, iter->program_map_PID);
  369.         ret = this->SwapBigLittleEndian((char*)&bit4, 32);
  370.         if (ret < 0)
  371.         {
  372.             return ret;
  373.         }
  374.         memcpy(data.buff + data.seek, &bit4, 4);
  375.         data.seek += 4;
  376.     }
  377.     ret = this->SetCRC32(CRC32begin);
  378.     if (ret < 0)
  379.     {
  380.         return ret;
  381.     }
  382.     ret = this->FillPayload(MUXTS_TS_PACKET_SIZE - data.seek);
  383.     if (ret < 0)
  384.     {
  385.         return ret;
  386.     }
  387.     this->Output2File();
  388.     return 0;
  389. }
  390.  
  391. int MuxTs::WritePMT(std::vector<TS_PMT_Stream> PMT_Stream, int program_number, bool has_payload_unit_start_indicator)
  392. {
  393.     if (PMT_Stream.empty())
  394.     {
  395.         return MUXTS_ERRTR_PMT_NOSTREAM;
  396.     }
  397.     int CRC32begin = 0;
  398.     TS_PMT ts_pmt;
  399.     ts_pmt.table_id = 0x02;
  400.     ts_pmt.section_syntax_indicator = 0x01;
  401.     ts_pmt.zero = 0x00;
  402.     ts_pmt.reserved_1 = 0xff;
  403.     unsigned length = 12;
  404.     if (has_payload_unit_start_indicator)
  405.     {
  406.         CRC32begin = 5;
  407.         length++;
  408.     }
  409.     else
  410.     {
  411.         CRC32begin = 4;
  412.     }
  413.     length += 5 * PMT_Stream.size();
  414.     ts_pmt.section_length = length;
  415.     ts_pmt.program_number = program_number;
  416.     ts_pmt.reserved_2 = 0xff;
  417.     ts_pmt.version_number = 0x00;
  418.     ts_pmt.current_next_indicator = 0xff;
  419.     ts_pmt.section_number = 0x00;
  420.     ts_pmt.last_section_number = 0x00;
  421.     ts_pmt.reserved_3 = 0xff;
  422.     ts_pmt.PCR_PID = 0x100;
  423.     ts_pmt.reserved_4 = 0xff;
  424.     ts_pmt.program_info_length = 0x00;
  425.     ts_pmt.PMT_Stream = PMT_Stream;
  426.     ts_pmt.CRC_32 = 1;
  427.     //1-8bit
  428.     BYTE8 bit8 = 0x00;
  429.     BITSET(bit8, 0, ts_pmt.table_id);
  430.     BITSET(bit8, 1, ts_pmt.section_syntax_indicator);
  431.     BITSET(bit8, 1, ts_pmt.zero);
  432.     BITSET(bit8, 2, ts_pmt.reserved_1);
  433.     BITSET(bit8, 12, ts_pmt.section_length);
  434.     BITSET(bit8, 16, ts_pmt.program_number);
  435.     BITSET(bit8, 2, ts_pmt.reserved_2);
  436.     BITSET(bit8, 5, ts_pmt.version_number);
  437.     BITSET(bit8, 1, ts_pmt.current_next_indicator);
  438.     BITSET(bit8, 8, ts_pmt.section_number);
  439.     BITSET(bit8, 8, ts_pmt.last_section_number);
  440.     ret = this->SwapBigLittleEndian((char*)&bit8, 64);
  441.     if (ret < 0)
  442.     {
  443.         return ret;
  444.     }
  445.     memcpy(data.buff + data.seek, &bit8, 8);
  446.     data.seek += 8;
  447.     //9-12bit
  448.     BYTE4 bit4 = 0x00;
  449.     BITSET(bit4, 0, ts_pmt.reserved_3);
  450.     BITSET(bit4, 13, ts_pmt.PCR_PID);
  451.     BITSET(bit4, 4, ts_pmt.reserved_4);
  452.     BITSET(bit4, 12, ts_pmt.program_info_length);
  453.     ret = this->SwapBigLittleEndian((char*)&bit4, 32);
  454.     if (ret < 0)
  455.     {
  456.         return ret;
  457.     }
  458.     memcpy(data.buff + data.seek, &bit4, 4);
  459.     data.seek += 4;
  460.     //PMT_Stream
  461.     vector<TS_PMT_Stream>::iterator iter;
  462.     for (iter = PMT_Stream.begin(); iter != PMT_Stream.end(); ++iter)
  463.     {
  464.         BYTE bit = 0x00;
  465.         BITSET(bit, 0, iter->stream_type);
  466.         memcpy(data.buff + data.seek, &bit, 1);
  467.         data.seek += 1;
  468.         BYTE4 bit4_f = 0x00;
  469.         BITSET(bit4_f, 0, iter->reserved_5);
  470.         BITSET(bit4_f, 13, iter->elementary_PID);
  471.         BITSET(bit4_f, 4, iter->reserved_6);
  472.         BITSET(bit4_f, 12, iter->ES_info_length);
  473.         ret = this->SwapBigLittleEndian((char*)&bit4_f, 32);
  474.         if (ret < 0)
  475.         {
  476.             return ret;
  477.         }
  478.         memcpy(data.buff + data.seek, &bit4_f, 4);
  479.         data.seek += 4;
  480.     }
  481.     ret = this->SetCRC32(CRC32begin);
  482.     if (ret < 0)
  483.     {
  484.         return ret;
  485.     }
  486.     ret = this->FillPayload(MUXTS_TS_PACKET_SIZE - data.seek);
  487.     if (ret < 0)
  488.     {
  489.         return ret;
  490.     }
  491.     this->Output2File();
  492.     return 0;
  493. }
  494.  
  495. int MuxTs::WriteAdaptation(unsigned length, bool PCR_flag, TS_PCR pcr)
  496. {
  497.     TS_ADAPTATION ts_adap;
  498.     ts_adap.adaptation_field_length = length;
  499.     ts_adap.discontinuity_indicator = 0x00;
  500.     ts_adap.elementary_stream_priority_indicator = 0x00;
  501.     if (PCR_flag)
  502.     {
  503.         ts_adap.PCR_flag = 0xff;
  504.         ts_adap.random_access_indicator = 0x00;
  505.         ts_adap.PCR = pcr;
  506.     }
  507.     else
  508.     {
  509.         ts_adap.PCR_flag = 0x00;
  510.         ts_adap.random_access_indicator = 0x00;
  511.     }
  512.    
  513.     if (0x01 == length)
  514.     {
  515.         ts_adap.random_access_indicator = 0x00;//aac ff
  516.     }
  517.     ts_adap.OPCR_flag = 0x00;
  518.     ts_adap.splicing_point_flag = 0x00;
  519.     ts_adap.transport_private_data_flag = 0x00;
  520.     ts_adap.adaptation_field_extension_flag = 0x00;
  521.     BYTE bit1_len = 0x00;
  522.     BITSET(bit1_len, 0, ts_adap.adaptation_field_length);
  523.     memcpy(data.buff + data.seek, &bit1_len, 1);
  524.     data.seek += 1;
  525.     if (0 == ts_adap.adaptation_field_length)
  526.     {
  527.         return 0;
  528.     }
  529.     BYTE bit1 = 0x00;
  530.     BITSET(bit1, 0, ts_adap.discontinuity_indicator);
  531.     BITSET(bit1, 1, ts_adap.random_access_indicator);
  532.     BITSET(bit1, 1, ts_adap.elementary_stream_priority_indicator);
  533.     BITSET(bit1, 1, ts_adap.PCR_flag);
  534.     BITSET(bit1, 1, ts_adap.OPCR_flag);
  535.     BITSET(bit1, 1, ts_adap.splicing_point_flag);
  536.     BITSET(bit1, 1, ts_adap.transport_private_data_flag);
  537.     BITSET(bit1, 1, ts_adap.adaptation_field_extension_flag);
  538.     memcpy(data.buff + data.seek, &bit1, 1);
  539.     data.seek += 1;
  540.     //PCR
  541.     if (PCR_flag)
  542.     {
  543.         BYTE bit_pcr_1 = 0x00;
  544.         bit_pcr_1 |= ts_adap.PCR.program_clock_reference_base >> 25;
  545.         memcpy(data.buff + data.seek, &bit_pcr_1, 1);
  546.         data.seek += 1;
  547.         BYTE4 bit_pcr_4 = 0x00;
  548.         bit_pcr_4 |= ts_adap.PCR.program_clock_reference_base & 0x1ffffff;
  549.         BITSET(bit_pcr_4, 6, ts_adap.PCR.reserved_1);
  550.         bit_pcr_4 <<= 1;
  551.         bit_pcr_4 |= ts_adap.PCR.program_clock_reference_extension & 0x100;
  552.         ret = this->SwapBigLittleEndian((char*)&bit_pcr_4, 32);
  553.         if (ret < 0)
  554.         {
  555.             return ret;
  556.         }
  557.         memcpy(data.buff + data.seek, &bit_pcr_4, 4);
  558.         data.seek += 4;
  559.         BYTE bit_pcr_2 = 0x00;
  560.         bit_pcr_2 |= ts_adap.PCR.program_clock_reference_extension;
  561.         memcpy(data.buff + data.seek, &bit_pcr_2, 1);
  562.         data.seek += 1;
  563.     }
  564.     else
  565.     {
  566.         unsigned short int payloadLen = ts_adap.adaptation_field_length - 1;
  567.         this->FillPayload((int)payloadLen);
  568.     }
  569.  
  570.     return 0;
  571. }
  572.  
  573. int MuxTs::WritePES(unsigned length, unsigned stream_id, unsigned PTS_DTS_flag, unsigned data_length, TS_PES_PTS ts_pts)
  574. {
  575.     TS_PES ts_pes;
  576.     ts_pes.packet_start_code_prefix = 0x000001;
  577.     ts_pes.stream_id = stream_id;
  578.     ts_pes.PESPacket_length = length;
  579.     ts_pes.marker_bits = 0x02;
  580.     ts_pes.PES_scrambling_control = 0x00;
  581.     ts_pes.PES_priority = 0x00;
  582.     ts_pes.data_alignment_indicator = 0x00;
  583.     ts_pes.copyright = 0x00;
  584.     ts_pes.original_or_copy = 0x00;
  585.     ts_pes.PTS_DTS_flags = PTS_DTS_flag;
  586.     ts_pes.ESCR_flag = 0x00;
  587.     ts_pes.ES_rate_flag = 0x00;
  588.     ts_pes.DSM_trick_mode_flag = 0x00;
  589.     ts_pes.additional_copy_info_flag = 0x00;
  590.     ts_pes.PES_CRC_flag = 0x00;
  591.     ts_pes.PES_extension_flag = 0x00;
  592.     ts_pes.PES_header_data_length = data_length;
  593.     BYTE8 bit8 = 0x00;
  594.     BITSET(bit8, 0, ts_pes.packet_start_code_prefix);
  595.     BITSET(bit8, 8, ts_pes.stream_id);
  596.     BITSET(bit8, 16, ts_pes.PESPacket_length);
  597.     BITSET(bit8, 2, ts_pes.marker_bits);
  598.     BITSET(bit8, 2, ts_pes.PES_scrambling_control);
  599.     BITSET(bit8, 1, ts_pes.PES_priority);
  600.     BITSET(bit8, 1, ts_pes.data_alignment_indicator);
  601.     BITSET(bit8, 1, ts_pes.copyright);
  602.     BITSET(bit8, 1, ts_pes.original_or_copy);
  603.     BITSET(bit8, 2, ts_pes.PTS_DTS_flags);
  604.     BITSET(bit8, 1, ts_pes.ESCR_flag);
  605.     BITSET(bit8, 1, ts_pes.ES_rate_flag);
  606.     BITSET(bit8, 1, ts_pes.DSM_trick_mode_flag);
  607.     BITSET(bit8, 1, ts_pes.additional_copy_info_flag);
  608.     BITSET(bit8, 1, ts_pes.PES_CRC_flag);
  609.     BITSET(bit8, 1, ts_pes.PES_extension_flag);
  610.     ret = this->SwapBigLittleEndian((char*)&bit8, 64);
  611.     if (ret < 0)
  612.     {
  613.         return ret;
  614.     }
  615.     memcpy(data.buff + data.seek, &bit8, 8);
  616.     data.seek += 8;
  617.     BYTE bit1 = 0x00;
  618.     bit1 |= ts_pes.PES_header_data_length;
  619.     memcpy(data.buff + data.seek, &bit1, 1);
  620.     data.seek += 1;
  621.     //PTS
  622.     if (0x02 == ts_pes.PTS_DTS_flags && 0x05 == ts_pes.PES_header_data_length)
  623.     {
  624.         BYTE bit_pts1 = 0x00;
  625.         BITSET(bit_pts1, 0, ts_pts.head);
  626.         BITSET(bit_pts1, 3, ts_pts.pts1);
  627.         BITSET(bit_pts1, 1, ts_pts.resvered_1);
  628.         memcpy(data.buff + data.seek, &bit_pts1, 1);
  629.         data.seek += 1;
  630.         BYTE4 bit_pts4 = 0x00;
  631.         BITSET(bit_pts4, 0, ts_pts.pts2);
  632.         BITSET(bit_pts4, 1, ts_pts.resvered_2);
  633.         BITSET(bit_pts4, 15, ts_pts.pts3);
  634.         BITSET(bit_pts4, 1, ts_pts.resvered_3);
  635.         ret = this->SwapBigLittleEndian((char*)&bit_pts4, 32);
  636.         if (ret < 0)
  637.         {
  638.             return ret;
  639.         }
  640.         memcpy(data.buff + data.seek, &bit_pts4, 4);
  641.         data.seek += 4;
  642.     }
  643.    
  644. #ifdef MUXTS_ENABLE_WRITE_DTS
  645.     if (0x03 == ts_pes.PTS_DTS_flags && 0x0a == ts_pes.PES_header_data_length)
  646.     {
  647.         BYTE bit_pts1 = 0x00;
  648.         ts_pts.head = 0x03;
  649.         BITSET(bit_pts1, 0, ts_pts.head);
  650.         BITSET(bit_pts1, 3, ts_pts.pts1);
  651.         BITSET(bit_pts1, 1, ts_pts.resvered_1);
  652.         memcpy(data.buff + data.seek, &bit_pts1, 1);
  653.         data.seek += 1;
  654.         BYTE4 bit_pts4 = 0x00;
  655.         BITSET(bit_pts4, 0, ts_pts.pts2);
  656.         BITSET(bit_pts4, 1, ts_pts.resvered_2);
  657.         BITSET(bit_pts4, 15, ts_pts.pts3);
  658.         BITSET(bit_pts4, 1, ts_pts.resvered_3);
  659.         ret = this->SwapBigLittleEndian((char*)&bit_pts4, 32);
  660.         if (ret < 0)
  661.         {
  662.             return ret;
  663.         }
  664.         memcpy(data.buff + data.seek, &bit_pts4, 4);
  665.         data.seek += 4;
  666.     }
  667.     data.buff[data.seek] = 0x11;
  668.     data.seek++;
  669.     memcpy(data.buff + data.seek, data.buff + data.seek - 5, 4);
  670.     data.seek += 4;
  671. #endif // MUXTS_ENABLE_WRITE_DTS
  672.  
  673.    
  674.     if (0xe0 == stream_id && 0x1b == m_pat.pmt.pes[pes_index_video].stream_type)
  675.     {
  676.         BYTE _nonBit = 0x00;
  677.         memcpy(data.buff + data.seek, &_nonBit, 1);
  678.         data.seek += 1;
  679.         memcpy(data.buff + data.seek, &_nonBit, 1);
  680.         data.seek += 1;
  681.         memcpy(data.buff + data.seek, &_nonBit, 1);
  682.         data.seek += 1;
  683.         _nonBit = 0x01;
  684.         memcpy(data.buff + data.seek, &_nonBit, 1);
  685.         data.seek += 1;
  686.         _nonBit = 0x09;
  687.         memcpy(data.buff + data.seek, &_nonBit, 1);
  688.         data.seek += 1;
  689.         _nonBit = 0xf0;
  690.         memcpy(data.buff + data.seek, &_nonBit, 1);
  691.         data.seek += 1;
  692.     }
  693.     return 0;
  694. }
  695.  
  696. int MuxTs::SwapBigLittleEndian(char * ptr, int bitlen)
  697. {
  698. #ifdef MUXTS_ENABLE_SWAP_ENDIAN32
  699.     if (32 == bitlen)
  700.     {
  701.         swap(ptr[0], ptr[3]);
  702.         swap(ptr[1], ptr[2]);
  703.         return 0;
  704.     }
  705. #endif // MUXTS_ENABLE_SWAP_ENDIAN32
  706. #ifdef MUXTS_ENABLE_SWAP_ENDIAN64
  707.     if (64 == bitlen)
  708.     {
  709.         swap(ptr[0], ptr[7]);
  710.         swap(ptr[1], ptr[6]);
  711.         swap(ptr[2], ptr[5]);
  712.         swap(ptr[3], ptr[4]);
  713.         return 0;
  714.     }
  715. #endif // MUXTS_ENABLE_SWAP_ENDIAN64
  716.  
  717.     return 0;
  718. }
  719.  
  720. int MuxTs::SetCRC32(int begin)
  721. {
  722.     uint32_t crc32 = CRC32(data.buff+begin, data.seek-begin);
  723.     //uint32_t crc32 = av_crc(crc32table, 0, data.buff+5, data.seek);
  724.     ret = this->SwapBigLittleEndian((char*)&crc32, 32);
  725.     if (ret < 0)
  726.     {
  727.         return ret;
  728.     }
  729.     memcpy(data.buff + data.seek, &crc32, 4);
  730.     data.seek += 4;
  731.     return 0;
  732. }
  733.  
  734. int MuxTs::FillPayload(int count)
  735. {
  736.     if ((data.seek + count) > MUXTS_TS_PACKET_SIZE)
  737.     {
  738.         return MUXTS_ERROR_FILLPAYLOAD_OVER;
  739.     }
  740.     for (int i = 0; i < count; i++)
  741.     {
  742.         *(data.buff+data.seek) = 0xff;
  743.         data.seek++;
  744.     }
  745.     return 0;
  746. }
  747.  
  748. int MuxTs::Output2File()
  749. {
  750.     if (data.seek != MUXTS_TS_PACKET_SIZE)
  751.     {
  752.         return MUXTS_ERROR_FILLPAYLOAD_OVER;
  753.     }
  754.     m_ofile.write((char*)data.buff, data.seek);
  755.     data.seek = 0;
  756.     return 0;
  757. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement