選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

mpeg-ts-dec.c 11KB

10ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // ITU-T H.222.0(06/2012)
  2. // Information technology - Generic coding of moving pictures and associated audio information: Systems
  3. // 2.4.3.1 Transport stream(p34)
  4. #include "mpeg-pes-internal.h"
  5. #include "mpeg-ts-internal.h"
  6. #include "mpeg-util.h"
  7. #include "mpeg-ts.h"
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <assert.h>
  11. #include <stdio.h>
  12. #include <errno.h>
  13. struct ts_demuxer_t
  14. {
  15. struct pat_t pat;
  16. ts_demuxer_onpacket onpacket;
  17. void* param;
  18. struct ts_demuxer_notify_t notify;
  19. void* notify_param;
  20. };
  21. static void ts_demuxer_notify(struct ts_demuxer_t* ts, const struct pmt_t* pmt);
  22. static uint32_t adaptation_filed_read(struct ts_adaptation_field_t *adp, const uint8_t* data, size_t bytes)
  23. {
  24. // 2.4.3.4 Adaptation field
  25. // Table 2-6
  26. uint32_t i = 0;
  27. uint32_t j = 0;
  28. assert(bytes <= TS_PACKET_SIZE);
  29. adp->adaptation_field_length = data[i++];
  30. if(adp->adaptation_field_length > 0)
  31. {
  32. adp->discontinuity_indicator = (data[i] >> 7) & 0x01;
  33. adp->random_access_indicator = (data[i] >> 6) & 0x01;
  34. adp->elementary_stream_priority_indicator = (data[i] >> 5) & 0x01;
  35. adp->PCR_flag = (data[i] >> 4) & 0x01;
  36. adp->OPCR_flag = (data[i] >> 3) & 0x01;
  37. adp->splicing_point_flag = (data[i] >> 2) & 0x01;
  38. adp->transport_private_data_flag = (data[i] >> 1) & 0x01;
  39. adp->adaptation_field_extension_flag = (data[i] >> 0) & 0x01;
  40. i++;
  41. if(adp->PCR_flag && i + 6 <= adp->adaptation_field_length + 1)
  42. {
  43. adp->program_clock_reference_base = ((uint64_t)data[i] << 25) | ((uint64_t)data[i+1] << 17) | ((uint64_t)data[i+2] << 9) | ((uint64_t)data[i+3] << 1) | ((data[i+4] >> 7) & 0x01);
  44. adp->program_clock_reference_extension = ((data[i+4] & 0x01) << 8) | data[i+5];
  45. i += 6;
  46. }
  47. if(adp->OPCR_flag && i + 6 <= adp->adaptation_field_length + 1)
  48. {
  49. adp->original_program_clock_reference_base = (((uint64_t)data[i]) << 25) | ((uint64_t)data[i+1] << 17) | ((uint64_t)data[i+2] << 9) | ((uint64_t)data[i+3] << 1) | ((data[i+4] >> 7) & 0x01);
  50. adp->original_program_clock_reference_extension = ((data[i+4] & 0x01) << 1) | data[i+5];
  51. i += 6;
  52. }
  53. if(adp->splicing_point_flag && i + 1 <= adp->adaptation_field_length + 1)
  54. {
  55. adp->splice_countdown = data[i++];
  56. }
  57. if(adp->transport_private_data_flag && i + 1 <= adp->adaptation_field_length + 1)
  58. {
  59. adp->transport_private_data_length = data[i++];
  60. for(j = 0; j < adp->transport_private_data_length; j++)
  61. {
  62. // uint8_t transport_private_data = data[i+j];
  63. }
  64. i += adp->transport_private_data_length;
  65. }
  66. if(adp->adaptation_field_extension_flag && i + 2 <= adp->adaptation_field_length + 1)
  67. {
  68. //uint8_t reserved;
  69. adp->adaptation_field_extension_length = data[i++];
  70. adp->ltw_flag = (data[i] >> 7) & 0x01;
  71. adp->piecewise_rate_flag = (data[i] >> 6) & 0x01;
  72. adp->seamless_splice_flag = (data[i] >> 5) & 0x01;
  73. //reserved = data[i] & 0x1F;
  74. i++;
  75. if(adp->ltw_flag && i + 2 <= adp->adaptation_field_length + 1)
  76. {
  77. // uint8_t ltw_valid_flag = (data[i] >> 7) & 0x01;
  78. // uint16_t ltw_offset = ((data[i] & 0x7F) << 8) | data[i+1];
  79. i += 2;
  80. }
  81. if(adp->piecewise_rate_flag && i + 3 <= adp->adaptation_field_length + 1)
  82. {
  83. // uint32_t piecewise_rate = ((data[i] & 0x3F) << 16) | (data[i+1] << 8) | data[i+2];
  84. i += 3;
  85. }
  86. if(adp->seamless_splice_flag && i + 5 <= adp->adaptation_field_length + 1)
  87. {
  88. // uint8_t Splice_type = (data[i] >> 4) & 0x0F;
  89. // uint32_t DTS_next_AU = (((data[i] >> 1) & 0x07) << 30) | (data[i+1] << 22) | (((data[i+2] >> 1) & 0x7F) << 15) | (data[i+3] << 7) | ((data[i+4] >> 1) & 0x7F);
  90. i += 5;
  91. }
  92. // reserved byte
  93. }
  94. // stuffing byte
  95. }
  96. return adp->adaptation_field_length + 1;
  97. }
  98. #define TS_IS_SYNC_BYTE(data) (data[0] == TS_SYNC_BYTE)
  99. #define TS_TRANSPORT_ERROR_INDICATOR(data) (data[1] & 0x80)
  100. #define TS_PAYLOAD_UNIT_START_INDICATOR(data) (data[1] & 0x40)
  101. #define TS_TRANSPORT_PRIORITY(data) (data[1] & 0x20)
  102. int ts_demuxer_flush(struct ts_demuxer_t* ts)
  103. {
  104. uint32_t i, j;
  105. for (i = 0; i < ts->pat.pmt_count; i++)
  106. {
  107. for (j = 0; j < ts->pat.pmts[i].stream_count; j++)
  108. {
  109. struct pes_t* pes = &ts->pat.pmts[i].streams[j];
  110. if (pes->pkt.size < 5)
  111. continue;
  112. if (PSI_STREAM_H264 == pes->codecid)
  113. {
  114. const uint8_t aud[] = {0,0,0,1,0x09,0xf0};
  115. pes_packet(&pes->pkt, pes, aud, sizeof(aud), 0, ts->onpacket, ts->param);
  116. }
  117. else if (PSI_STREAM_H265 == pes->codecid)
  118. {
  119. const uint8_t aud[] = {0,0,0,1,0x46,0x01,0x50};
  120. pes_packet(&pes->pkt, pes, aud, sizeof(aud), 0, ts->onpacket, ts->param);
  121. }
  122. else
  123. {
  124. //assert(0);
  125. pes_packet(&pes->pkt, pes, NULL, 0, 0, ts->onpacket, ts->param);
  126. }
  127. }
  128. }
  129. return 0;
  130. }
  131. int ts_demuxer_input(struct ts_demuxer_t* ts, const uint8_t* data, size_t bytes)
  132. {
  133. int r = 0;
  134. uint32_t i, j, k;
  135. uint32_t PID;
  136. unsigned int count;
  137. struct mpeg_bits_t reader;
  138. struct ts_packet_header_t pkhd;
  139. // 2.4.3 Specification of the transport stream syntax and semantics
  140. // Transport stream packets shall be 188 bytes long.
  141. assert(188 == bytes);
  142. // 2.4.3.2 Transport stream packet layer
  143. // Table 2-2
  144. memset(&pkhd, 0, sizeof(pkhd));
  145. assert(0x47 == data[0]); // sync_byte
  146. PID = ((data[1] << 8) | data[2]) & 0x1FFF;
  147. pkhd.transport_error_indicator = (data[1] >> 7) & 0x01;
  148. pkhd.payload_unit_start_indicator = (data[1] >> 6) & 0x01;
  149. pkhd.transport_priority = (data[1] >> 5) & 0x01;
  150. pkhd.transport_scrambling_control = (data[3] >> 6) & 0x03;
  151. pkhd.adaptation_field_control = (data[3] >> 4) & 0x03;
  152. pkhd.continuity_counter = data[3] & 0x0F;
  153. // printf("-----------------------------------------------\n");
  154. // printf("PID[%u]: Error: %u, Start:%u, Priority:%u, Scrambler:%u, AF: %u, CC: %u\n", PID, pkhd.transport_error_indicator, pkhd.payload_unit_start_indicator, pkhd.transport_priority, pkhd.transport_scrambling_control, pkhd.adaptation_field_control, pkhd.continuity_counter);
  155. i = 4;
  156. if(0x02 & pkhd.adaptation_field_control)
  157. {
  158. i += adaptation_filed_read(&pkhd.adaptation, data + 4, bytes - 4);
  159. if(pkhd.adaptation.adaptation_field_length > 0 && pkhd.adaptation.PCR_flag)
  160. {
  161. //int64_t t;
  162. //t = pkhd.adaptation.program_clock_reference_base / 90L; // ms;
  163. //printf("pcr: %02d:%02d:%02d.%03d - %" PRId64 "/%u\n", (int)(t / 3600000), (int)(t % 3600000)/60000, (int)((t/1000) % 60), (int)(t % 1000), pkhd.adaptation.program_clock_reference_base, pkhd.adaptation.program_clock_reference_extension);
  164. }
  165. assert(i <= bytes);
  166. if (i >= bytes)
  167. return 0; // ignore
  168. }
  169. if(0x01 & pkhd.adaptation_field_control)
  170. {
  171. if(TS_PID_PAT == PID)
  172. {
  173. if(pkhd.payload_unit_start_indicator)
  174. i += 1; // pointer 0x00
  175. // TODO: PAT lost
  176. pat_read(&ts->pat, data + i, bytes - i);
  177. }
  178. else if(TS_PID_SDT == PID)
  179. {
  180. if(pkhd.payload_unit_start_indicator)
  181. i += 1; // pointer 0x00
  182. sdt_read(&ts->pat, data + i, bytes - i);
  183. }
  184. else
  185. {
  186. for(j = 0; j < ts->pat.pmt_count; j++)
  187. {
  188. if(PID == ts->pat.pmts[j].pid)
  189. {
  190. // TODO: PMT lost
  191. if(pkhd.payload_unit_start_indicator)
  192. i += 1; // pointer 0x00
  193. count = ts->pat.pmts[j].stream_count;
  194. pmt_read(&ts->pat.pmts[j], data + i, bytes - i);
  195. if(count != ts->pat.pmts[j].stream_count)
  196. ts_demuxer_notify(ts, &ts->pat.pmts[j]);
  197. break;
  198. }
  199. else
  200. {
  201. for (k = 0; k < ts->pat.pmts[j].stream_count; k++)
  202. {
  203. struct pes_t* pes = &ts->pat.pmts[j].streams[k];
  204. if (PID != pes->pid)
  205. continue;
  206. pes->flags |= ((ts->pat.pmts[j].streams[k].cc + 1) % 16) != (uint8_t)pkhd.continuity_counter ? (MPEG_FLAG_PACKET_CORRUPT | MPEG_FLAG_PACKET_LOST) : 0;
  207. ts->pat.pmts[j].streams[k].cc = (uint8_t)pkhd.continuity_counter;
  208. if (pkhd.payload_unit_start_indicator)
  209. {
  210. size_t s;
  211. mpeg_bits_init(&reader, data + i, bytes - i);
  212. s = mpeg_bits_readn(&reader, 3);
  213. pes->sid = mpeg_bits_read8(&reader);
  214. if (MPEG_ERROR_OK != pes_read_header(pes, &reader) || s != 0x000001)
  215. {
  216. assert(0);
  217. return 0; // ignore
  218. }
  219. i += (uint32_t)mpeg_bits_tell(&reader);
  220. pes->flags = (pes->flags & MPEG_FLAG_PACKET_CORRUPT) ? MPEG_FLAG_PACKET_LOST : 0;
  221. pes->flags |= pes->data_alignment_indicator ? MPEG_FLAG_IDR_FRAME : 0;
  222. pes->have_pes_header = 1;
  223. }
  224. else if (!pes->have_pes_header)
  225. {
  226. continue; // don't have pes header yet
  227. }
  228. r = pes_packet(&pes->pkt, pes, data + i, bytes - i, pkhd.payload_unit_start_indicator, ts->onpacket, ts->param);
  229. pes->have_pes_header = (r || (0 == pes->pkt.size && pes->len > 0)) ? 0 : 1; // packet completed
  230. break; // find stream
  231. }
  232. } // PMT handler
  233. }
  234. } // PAT handler
  235. }
  236. return r;
  237. }
  238. static inline int mpeg_ts_is_idr_first_packet(const void* packet, int bytes)
  239. {
  240. const unsigned char *data;
  241. struct ts_packet_header_t pkhd;
  242. int payload_unit_start_indicator;
  243. memset(&pkhd, 0, sizeof(pkhd));
  244. data = (const unsigned char *)packet;
  245. payload_unit_start_indicator = data[1] & 0x40;
  246. pkhd.adaptation_field_control = (data[3] >> 4) & 0x03;
  247. pkhd.continuity_counter = data[3] & 0x0F;
  248. if(0x02 == pkhd.adaptation_field_control || 0x03 == pkhd.adaptation_field_control)
  249. {
  250. adaptation_filed_read(&pkhd.adaptation, data + 4, bytes - 4);
  251. }
  252. return (payload_unit_start_indicator && pkhd.adaptation.random_access_indicator) ? 1 : 0;
  253. }
  254. struct ts_demuxer_t* ts_demuxer_create(ts_demuxer_onpacket onpacket, void* param)
  255. {
  256. struct ts_demuxer_t* ts;
  257. ts = calloc(1, sizeof(struct ts_demuxer_t));
  258. if (!ts)
  259. return NULL;
  260. ts->onpacket = onpacket;
  261. ts->param = param;
  262. return ts;
  263. }
  264. int ts_demuxer_destroy(struct ts_demuxer_t* ts)
  265. {
  266. size_t i, j;
  267. struct pes_t* pes;
  268. for (i = 0; i < ts->pat.pmt_count; i++)
  269. {
  270. for (j = 0; j < ts->pat.pmts[i].stream_count; j++)
  271. {
  272. pes = &ts->pat.pmts[i].streams[j];
  273. if (pes->pkt.data)
  274. free(pes->pkt.data);
  275. pes->pkt.data = NULL;
  276. }
  277. }
  278. if (ts->pat.pmts && ts->pat.pmts != ts->pat.pmt_default)
  279. free(ts->pat.pmts);
  280. free(ts);
  281. return 0;
  282. }
  283. int ts_demuxer_getservice(struct ts_demuxer_t* ts, int program, char* provider, int nprovider, char* name, int nname)
  284. {
  285. struct pmt_t* pmt;
  286. pmt = pat_find(&ts->pat, (uint16_t)program);
  287. if(NULL == pmt)
  288. return -1;
  289. snprintf(provider, nprovider, "%s", pmt->provider);
  290. snprintf(name, nname, "%s", pmt->name);
  291. return 0;
  292. }
  293. void ts_demuxer_set_notify(struct ts_demuxer_t* ts, struct ts_demuxer_notify_t* notify, void* param)
  294. {
  295. ts->notify_param = param;
  296. memcpy(&ts->notify, notify, sizeof(ts->notify));
  297. }
  298. static void ts_demuxer_notify(struct ts_demuxer_t* ts, const struct pmt_t* pmt)
  299. {
  300. unsigned int i;
  301. const struct pes_t* pes;
  302. if (!ts->notify.onstream)
  303. return;
  304. for (i = 0; i < pmt->stream_count; i++)
  305. {
  306. pes = &pmt->streams[i];
  307. ts->notify.onstream(ts->notify_param, pes->pid, pes->codecid, pes->esinfo, pes->esinfo_len, i + 1 >= pmt->stream_count ? 1 : 0);
  308. }
  309. }