您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

267 行
8.1KB

  1. // ITU-T H.222.0(10/2014)
  2. // Information technology - Generic coding of moving pictures and associated audio information: Systems
  3. // 2.4.4.8 Program map table(p68)
  4. #include "mpeg-ts-internal.h"
  5. #include "mpeg-ts-opus.h"
  6. #include "mpeg-util.h"
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. static struct pes_t* pmt_fetch(struct pmt_t* pmt, uint16_t pid)
  11. {
  12. unsigned int i;
  13. for(i = 0; i < pmt->stream_count; i++)
  14. {
  15. if(pmt->streams[i].pid == pid)
  16. return &pmt->streams[i];
  17. }
  18. if(pmt->stream_count >= sizeof(pmt->streams) / sizeof(pmt->streams[0]))
  19. {
  20. assert(0);
  21. return NULL;
  22. }
  23. // new stream
  24. return &pmt->streams[pmt->stream_count++];
  25. }
  26. static int pmt_read_descriptor(struct pes_t* stream, const uint8_t* data, uint16_t bytes)
  27. {
  28. uint8_t tag;
  29. uint8_t len;
  30. uint8_t channels;
  31. // Registration descriptor
  32. while (bytes > 2)
  33. {
  34. tag = data[0];
  35. len = data[1];
  36. if (len + 2 > bytes)
  37. return -1; // invalid len
  38. // ISO/IEC 13818-1:2018 (E) Table 2-45 ?Program and program element descriptors (p90)
  39. switch (tag)
  40. {
  41. case 0x05: // 2.6.8 Registration descriptor(p94)
  42. if (len >= 4 && 'O' == data[2] && 'p' == data[3] && 'u' == data[4] && 's' == data[5])
  43. {
  44. assert(PSI_STREAM_PRIVATE_DATA == stream->codecid);
  45. stream->codecid = PSI_STREAM_AUDIO_OPUS;
  46. }
  47. break;
  48. case 0x7f: // DVB-Service Information: 6.1 Descriptor identification and location (p38)
  49. // 2.6.90 Extension descriptor
  50. if (len >= 2 && data[3] <= sizeof(opus_channel_map)/sizeof(opus_channel_map[0]) && PSI_STREAM_AUDIO_OPUS == stream->codecid && OPUS_EXTENSION_DESCRIPTOR_TAG == data[2]) // User defined (provisional Opus)
  51. {
  52. channels = data[3];
  53. stream->esinfo = (uint8_t*)calloc(1, sizeof(opus_default_extradata));
  54. if (stream->esinfo)
  55. {
  56. stream->esinfo_len = sizeof(opus_default_extradata);
  57. memcpy(stream->esinfo, opus_default_extradata, stream->esinfo_len);
  58. stream->esinfo[9] = channels ? channels : 2;
  59. stream->esinfo[18] = channels ? (channels > 2 ? 1 : 0) : /* Dual Mono */ 255;
  60. stream->esinfo[19] = opus_stream_cnt[channels];
  61. stream->esinfo[20] = opus_coupled_stream_cnt[channels];
  62. memcpy(stream->esinfo + 21, opus_channel_map[(channels ? channels : 2) - 1], channels ? channels : 2);
  63. }
  64. }
  65. }
  66. data += len + 2;
  67. bytes -= len + 2;
  68. }
  69. assert(0 == bytes);
  70. return 0;
  71. }
  72. static int pmt_write_descriptor(const struct pes_t* stream, uint8_t* data, int bytes)
  73. {
  74. uint8_t* p;
  75. p = data;
  76. if (PSI_STREAM_AUDIO_OPUS == stream->codecid && bytes > 2 + 4 /*fourcc*/ + 4 /*DVI OPUS*/ )
  77. {
  78. *p++ = 0x05; // 2.6.8 Registration descriptor(p94)
  79. *p++ = 4;
  80. memcpy(p, "Opus", 4);
  81. p += 4;
  82. *p++ = 0x7f; // DVB-Service Information: 6.1 Descriptor identification and location (p38)
  83. *p++ = 2;
  84. *p++ = OPUS_EXTENSION_DESCRIPTOR_TAG;
  85. *p++ = stream->esinfo_len > 8 ? stream->esinfo[9] : 2 /*0xFF*/ ; // default 2-channels
  86. }
  87. return (int)(p - data);
  88. }
  89. size_t pmt_read(struct pmt_t *pmt, const uint8_t* data, size_t bytes)
  90. {
  91. struct pes_t* stream;
  92. uint16_t pid, len;
  93. uint32_t i, section_length, program_number, version_number;
  94. uint32_t PCR_PID, program_info_length;
  95. if (bytes < 12)
  96. return 0; // invalid data length
  97. // printf("PMT: %0x %0x %0x %0x %0x %0x %0x %0x, %0x, %0x, %0x, %0x\n", (unsigned int)data[0], (unsigned int)data[1], (unsigned int)data[2], (unsigned int)data[3], (unsigned int)data[4], (unsigned int)data[5], (unsigned int)data[6],(unsigned int)data[7],(unsigned int)data[8],(unsigned int)data[9],(unsigned int)data[10],(unsigned int)data[11]);
  98. assert(PAT_TID_PMS == data[0]);
  99. assert(1 == ((data[1] >> 7) & 0x01));
  100. section_length = ((data[1] & 0x0F) << 8) | data[2];
  101. program_number = (data[3] << 8) | data[4];
  102. // uint32_t reserved2 = (data[5] >> 6) & 0x03;
  103. version_number = (data[5] >> 1) & 0x1F;
  104. // uint32_t current_next_indicator = data[5] & 0x01;
  105. assert(0 == data[6]); // sector_number
  106. assert(0 == data[7]); // last_sector_number
  107. // uint32_t reserved3 = (data[8] >> 5) & 0x07;
  108. PCR_PID = ((data[8] & 0x1F) << 8) | data[9];
  109. // uint32_t reserved4 = (data[10] >> 4) & 0x0F;
  110. program_info_length = ((data[10] & 0x0F) << 8) | data[11];
  111. if (PAT_TID_PMS != data[0] || section_length + 3 < 12 + 4 /*crc32*/ || section_length + 3 > bytes
  112. || program_info_length + 12 /*head*/ > section_length + 3 /*head*/ - 4 /*crc32*/ )
  113. {
  114. assert(0);
  115. return 0; // invalid data length
  116. }
  117. if(pmt->ver != version_number)
  118. pmt->stream_count = 0; // clear all streams
  119. pmt->PCR_PID = PCR_PID;
  120. pmt->pn = program_number;
  121. pmt->ver = version_number;
  122. pmt->pminfo_len = program_info_length;
  123. if(program_info_length > 2)
  124. {
  125. // descriptor(data + 12, program_info_length)
  126. }
  127. assert(bytes >= section_length + 3); // PMT = section_length + 3
  128. for (i = 12 + program_info_length; i + 5 <= section_length + 3 - 4/*CRC32*/ && section_length + 3 <= bytes; i += len + 5) // 9: follow section_length item
  129. {
  130. pid = ((data[i+1] & 0x1F) << 8) | data[i+2];
  131. len = ((data[i+3] & 0x0F) << 8) | data[i+4];
  132. // printf("PMT: pn: %0x, pid: %0x, codec: %0x, eslen: %d\n", (unsigned int)pmt->pn, (unsigned int)pid, (unsigned int)data[i], (unsigned int)len);
  133. if (i + len + 5 > section_length + 3 - 4/*CRC32*/)
  134. break; // mark error ?
  135. assert(pmt->stream_count <= sizeof(pmt->streams)/sizeof(pmt->streams[0]));
  136. stream = pmt_fetch(pmt, pid);
  137. if(NULL == stream)
  138. continue;
  139. stream->pn = (uint16_t)pmt->pn;
  140. stream->pid = pid;
  141. stream->codecid = data[i];
  142. stream->esinfo_len = 0; // default nothing
  143. if (len > 2)
  144. {
  145. //descriptor(data + i + 5, len)
  146. pmt_read_descriptor(stream, data + i + 5, len);
  147. }
  148. }
  149. //assert(j+4 == bytes);
  150. //crc = (data[j] << 24) | (data[j+1] << 16) | (data[j+2] << 8) | data[j+3];
  151. // assert(0 == mpeg_crc32(0xffffffff, data, section_length+3));
  152. return section_length + 3;
  153. }
  154. size_t pmt_write(const struct pmt_t *pmt, uint8_t *data)
  155. {
  156. // 2.4.4.8 Program map table (p68)
  157. // Table 2-33
  158. uint32_t i = 0;
  159. uint32_t crc = 0;
  160. ptrdiff_t len = 0;
  161. uint8_t *p = NULL;
  162. data[0] = PAT_TID_PMS; // program map table
  163. // skip section_length
  164. // program_number
  165. nbo_w16(data + 3, (uint16_t)pmt->pn);
  166. // reserved '11'
  167. // version_number 'xxxxx'
  168. // current_next_indicator '1'
  169. data[5] = (uint8_t)(0xC1 | (pmt->ver << 1));
  170. // section_number/last_section_number
  171. data[6] = 0x00;
  172. data[7] = 0x00;
  173. // reserved '111'
  174. // PCR_PID 13-bits 0x1FFF
  175. nbo_w16(data + 8, (uint16_t)(0xE000 | pmt->PCR_PID));
  176. // reserved '1111'
  177. // program_info_length 12-bits, the first two bits of which shall be '00'.
  178. assert(pmt->pminfo_len < 0x400);
  179. nbo_w16(data + 10, (uint16_t)(0xF000 | pmt->pminfo_len));
  180. if(pmt->pminfo_len > 0 && pmt->pminfo_len < 0x400)
  181. {
  182. // fill program info
  183. assert(pmt->pminfo);
  184. memcpy(data + 12, pmt->pminfo, pmt->pminfo_len);
  185. }
  186. // streams
  187. p = data + 12 + pmt->pminfo_len;
  188. for(i = 0; i < pmt->stream_count && p - data < 1021 - 4 - 5 - pmt->streams[i].esinfo_len; i++)
  189. {
  190. // stream_type
  191. *p = (uint8_t)(PSI_STREAM_AUDIO_OPUS == pmt->streams[i].codecid) ? PSI_STREAM_PRIVATE_DATA : pmt->streams[i].codecid;
  192. // reserved '111'
  193. // elementary_PID 13-bits
  194. nbo_w16(p + 1, 0xE000 | pmt->streams[i].pid);
  195. len = 0;
  196. // fill elementary stream info
  197. //if(PSI_STREAM_AUDIO_OPUS == pmt->streams[i].codecid || (pmt->streams[i].esinfo_len > 0 && pmt->streams[i].esinfo))
  198. {
  199. //assert(pmt->streams[i].esinfo);
  200. //memcpy(p, pmt->streams[i].esinfo, pmt->streams[i].esinfo_len);
  201. //p += pmt->streams[i].esinfo_len;
  202. len = pmt_write_descriptor(&pmt->streams[i], p + 5, 1021 - (int)(p + 5 - data));
  203. }
  204. // reserved '1111'
  205. // ES_info_lengt 12-bits
  206. nbo_w16(p + 3, 0xF000 | (uint16_t)len);
  207. p += 5 + len;
  208. }
  209. // section_length
  210. len = p + 4 - (data + 3); // 4 bytes crc32
  211. assert(len <= 1021); // shall not exceed 1021 (0x3FD).
  212. assert(len <= TS_PACKET_SIZE - 7);
  213. // section_syntax_indicator '1'
  214. // '0'
  215. // reserved '11'
  216. nbo_w16(data + 1, (uint16_t)(0xb000 | len));
  217. // crc32
  218. crc = mpeg_crc32(0xffffffff, data, (uint32_t)(p-data));
  219. //put32(p, crc);
  220. p[3] = (crc >> 24) & 0xFF;
  221. p[2] = (crc >> 16) & 0xFF;
  222. p[1] = (crc >> 8) & 0xFF;
  223. p[0] = crc & 0xFF;
  224. return (p - data) + 4; // total length
  225. }