Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

563 řádky
15KB

  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-ts-internal.h"
  5. #include "mpeg-util.h"
  6. #include "mpeg-ts.h"
  7. #include <errno.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <assert.h>
  11. #define PCR_DELAY 0 //(700 * 90) // 700ms
  12. #define PAT_PERIOD (400 * 90) // 500ms
  13. #define PAT_CYCLE 50 // 50fps(audio + video)
  14. #define TS_HEADER_LEN 4 // 1-bytes sync byte + 2-bytes PID + 1-byte CC
  15. #define PES_HEADER_LEN 6 // 3-bytes packet_start_code_prefix + 1-byte stream_id + 2-bytes PES_packet_length
  16. #define TS_PAYLOAD_UNIT_START_INDICATOR 0x40
  17. // adaptation flags
  18. #define AF_FLAG_PCR 0x10
  19. #define AF_FLAG_RANDOM_ACCESS_INDICATOR 0x40 // random_access_indicator
  20. typedef struct _mpeg_ts_enc_context_t
  21. {
  22. struct pat_t pat;
  23. int h264_h265_with_aud;
  24. int64_t sdt_period;
  25. int64_t pat_period;
  26. int64_t pcr_period;
  27. int64_t pcr_clock; // last pcr time
  28. int pat_cycle;
  29. uint16_t pid;
  30. struct mpeg_ts_func_t func;
  31. void* param;
  32. uint8_t payload[1024]; // maximum PAT/PMT payload length
  33. } mpeg_ts_enc_context_t;
  34. static void mpeg_ts_pmt_destroy(struct pmt_t* pmt);
  35. static int mpeg_ts_write_section_header(const mpeg_ts_enc_context_t *ts, int pid, unsigned int* cc, const void* payload, size_t len)
  36. {
  37. int r;
  38. uint8_t *data = NULL;
  39. data = ts->func.alloc(ts->param, TS_PACKET_SIZE);
  40. if(!data) return -ENOMEM;
  41. assert(len < TS_PACKET_SIZE - 5); // TS-header + pointer
  42. // TS Header
  43. // sync_byte
  44. data[0] = 0x47;
  45. // transport_error_indicator = 0
  46. // payload_unit_start_indicator = 1
  47. // transport_priority = 0
  48. data[1] = 0x40 | ((pid >> 8) & 0x1F);
  49. data[2] = pid & 0xFF;
  50. // transport_scrambling_control = 0x00
  51. // adaptation_field_control = 0x01-No adaptation_field, payload only, 0x03-adaptation and payload
  52. data[3] = 0x10 | (*cc & 0x0F);
  53. *cc = (*cc + 1) % 16; // update continuity_counter
  54. // // Adaptation
  55. // if(len < TS_PACKET_SIZE - 5)
  56. // {
  57. // data[3] |= 0x20; // with adaptation
  58. // data[4] = TS_PACKET_SIZE - len - 5 - 1; // 4B-Header + 1B-pointer + 1B-self
  59. // if(data[4] > 0)
  60. // {
  61. // // adaptation
  62. // data[5] = 0; // no flag
  63. // memset(data+6, 0xFF, data[4]-1);
  64. // }
  65. // }
  66. // pointer (payload_unit_start_indicator==1)
  67. //data[TS_PACKET_SIZE-len-1] = 0x00;
  68. data[4] = 0x00;
  69. // TS Payload
  70. //memmove(data + TS_PACKET_SIZE - len, payload, len);
  71. memmove(data + 5, payload, len);
  72. memset(data+5+len, 0xff, TS_PACKET_SIZE-len-5);
  73. r = ts->func.write(ts->param, data, TS_PACKET_SIZE);
  74. ts->func.free(ts->param, data);
  75. return r;
  76. }
  77. static int ts_write_pes(mpeg_ts_enc_context_t *tsctx, const struct pmt_t* pmt, struct pes_t *stream, const uint8_t* payload, size_t bytes)
  78. {
  79. // 2.4.3.6 PES packet
  80. // Table 2-21
  81. int r = 0;
  82. size_t len = 0;
  83. int start = 1; // first packet
  84. uint8_t *p = NULL;
  85. uint8_t *data = NULL;
  86. uint8_t *header = NULL;
  87. while(0 == r && bytes > 0)
  88. {
  89. data = tsctx->func.alloc(tsctx->param, TS_PACKET_SIZE);
  90. if(!data) return -ENOMEM;
  91. // TS Header
  92. data[0] = 0x47; // sync_byte
  93. data[1] = 0x00 | ((stream->pid >>8) & 0x1F);
  94. data[2] = stream->pid & 0xFF;
  95. data[3] = 0x10 | (stream->cc & 0x0F); // no adaptation, payload only
  96. data[4] = 0; // clear adaptation length
  97. data[5] = 0; // clear adaptation flags
  98. stream->cc = (stream->cc + 1) % 16;
  99. // 2.7.2 Frequency of coding the program clock reference
  100. // http://www.bretl.com/mpeghtml/SCR.HTM
  101. // the maximum between PCRs is 100ms.
  102. if(start && stream->pid == pmt->PCR_PID)
  103. {
  104. data[3] |= 0x20; // +AF
  105. data[5] |= AF_FLAG_PCR; // +PCR_flag
  106. }
  107. // random_access_indicator
  108. if(start && stream->data_alignment_indicator && PTS_NO_VALUE != stream->pts)
  109. {
  110. //In the PCR_PID the random_access_indicator may only be set to '1'
  111. //in a transport stream packet containing the PCR fields.
  112. data[3] |= 0x20; // +AF
  113. data[5] |= AF_FLAG_RANDOM_ACCESS_INDICATOR; // +random_access_indicator
  114. }
  115. if(data[3] & 0x20)
  116. {
  117. data[4] = 1; // 1-byte flag
  118. if(data[5] & AF_FLAG_PCR) // PCR_flag
  119. {
  120. int64_t pcr = 0;
  121. pcr = (PTS_NO_VALUE==stream->dts) ? stream->pts : stream->dts;
  122. pcr_write(data + 6, (pcr - PCR_DELAY) * 300); // TODO: delay???
  123. data[4] += 6; // 6-PCR
  124. }
  125. header = data + TS_HEADER_LEN + 1 + data[4]; // 4-TS + 1-AF-Len + AF-Payload
  126. }
  127. else
  128. {
  129. header = data + TS_HEADER_LEN;
  130. }
  131. p = header;
  132. // PES header
  133. if(start)
  134. {
  135. data[1] |= TS_PAYLOAD_UNIT_START_INDICATOR; // payload_unit_start_indicator
  136. p += pes_write_header(stream, header, TS_PACKET_SIZE - (header - data));
  137. if(PSI_STREAM_H264 == stream->codecid && !tsctx->h264_h265_with_aud)
  138. {
  139. // 2.14 Carriage of Rec. ITU-T H.264 | ISO/IEC 14496-10 video
  140. // Each AVC access unit shall contain an access unit delimiter NAL Unit
  141. nbo_w32(p, 0x00000001);
  142. p[4] = 0x09; // AUD
  143. p[5] = 0xF0; // any slice type (0xe) + rbsp stop one bit
  144. p += 6;
  145. }
  146. else if (PSI_STREAM_H265 == stream->codecid && !tsctx->h264_h265_with_aud)
  147. {
  148. // 2.17 Carriage of HEVC
  149. // Each HEVC access unit shall contain an access unit delimiter NAL unit.
  150. nbo_w32(p, 0x00000001);
  151. p[4] = 0x46; // 35-AUD_NUT
  152. p[5] = 0x01;
  153. p[6] = 0x50; // B&P&I (0x2) + rbsp stop one bit
  154. p += 7;
  155. }
  156. // PES_packet_length = PES-Header + Payload-Size
  157. // A value of 0 indicates that the PES packet length is neither specified nor bounded
  158. // and is allowed only in PES packets whose payload consists of bytes from a
  159. // video elementary stream contained in transport stream packets
  160. if((p - header - PES_HEADER_LEN) + bytes > 0xFFFF)
  161. nbo_w16(header + 4, 0); // 2.4.3.7 PES packet => PES_packet_length
  162. else
  163. nbo_w16(header + 4, (uint16_t)((p - header - PES_HEADER_LEN) + bytes));
  164. }
  165. len = p - data; // TS + PES header length
  166. if(len + bytes < TS_PACKET_SIZE)
  167. {
  168. // stuffing_len = TS_PACKET_SIZE - (len + bytes)
  169. // move pes header
  170. if(p - header > 0)
  171. {
  172. assert(start);
  173. memmove(data + (TS_PACKET_SIZE - bytes - (p - header)), header, p - header);
  174. }
  175. // adaptation
  176. if(data[3] & 0x20) // has AF?
  177. {
  178. assert(0 != data[5] && data[4] > 0);
  179. memset(data + TS_HEADER_LEN + 1 + data[4], 0xFF, TS_PACKET_SIZE - (len + bytes));
  180. data[4] += (uint8_t)(TS_PACKET_SIZE - (len + bytes));
  181. }
  182. else
  183. {
  184. assert(len == (size_t)(p - header) + TS_HEADER_LEN);
  185. data[3] |= 0x20; // +AF
  186. data[4] = (uint8_t)(TS_PACKET_SIZE - (len + bytes) - 1/*AF length*/);
  187. if (data[4] > 0) data[5] = 0; // no flag
  188. if (data[4] > 1) memset(data + 6, 0xFF, TS_PACKET_SIZE - (len + bytes) - 2);
  189. }
  190. len = bytes;
  191. p = data + 5 + data[4] + (p - header);
  192. }
  193. else
  194. {
  195. len = TS_PACKET_SIZE - len;
  196. }
  197. // payload
  198. memcpy(p, payload, len);
  199. payload += len;
  200. bytes -= len;
  201. start = 0;
  202. // send with TS-header
  203. r = tsctx->func.write(tsctx->param, data, TS_PACKET_SIZE);
  204. tsctx->func.free(tsctx->param, data);
  205. }
  206. return r;
  207. }
  208. static struct pes_t *mpeg_ts_find(mpeg_ts_enc_context_t *ts, int pid, struct pmt_t** pmt)
  209. {
  210. size_t i, j;
  211. struct pes_t* stream;
  212. for (i = 0; i < ts->pat.pmt_count; i++)
  213. {
  214. *pmt = &ts->pat.pmts[i];
  215. for (j = 0; j < (*pmt)->stream_count; j++)
  216. {
  217. stream = &(*pmt)->streams[j];
  218. if (pid == (int)stream->pid)
  219. return stream;
  220. }
  221. }
  222. return NULL;
  223. }
  224. int mpeg_ts_write(void* ts, int pid, int flags, int64_t pts, int64_t dts, const void* data, size_t bytes)
  225. {
  226. int r = 0;
  227. size_t i, n;
  228. struct pmt_t *pmt = NULL;
  229. struct pes_t *stream = NULL;
  230. mpeg_ts_enc_context_t *tsctx;
  231. tsctx = (mpeg_ts_enc_context_t*)ts;
  232. stream = mpeg_ts_find(tsctx, pid, &pmt);
  233. if (NULL == stream)
  234. return -ENOENT; // not found
  235. stream->pts = pts;
  236. stream->dts = dts;
  237. stream->data_alignment_indicator = (flags & MPEG_FLAG_IDR_FRAME) ? 1 : 0; // idr frame
  238. tsctx->h264_h265_with_aud = (flags & MPEG_FLAG_H264_H265_WITH_AUD) ? 1 : 0;
  239. // set PCR_PID
  240. //assert(1 == tsctx->pat.pmt_count);
  241. if (0x1FFF == pmt->PCR_PID || (PES_SID_VIDEO == (stream->sid & PES_SID_VIDEO) && pmt->PCR_PID != stream->pid))
  242. {
  243. pmt->PCR_PID = stream->pid;
  244. tsctx->pat_period = 0;
  245. tsctx->pat_cycle = 0;
  246. }
  247. if (pmt->PCR_PID == stream->pid)
  248. ++tsctx->pcr_clock;
  249. // Add PAT and PMT for video IDR frame
  250. if(0 == ++tsctx->pat_cycle % PAT_CYCLE || 0 == tsctx->pat_period || tsctx->pat_period + PAT_PERIOD <= dts || (PES_SID_VIDEO == (stream->sid & PES_SID_VIDEO) && (flags & MPEG_FLAG_IDR_FRAME)))
  251. {
  252. tsctx->pat_cycle = 0;
  253. tsctx->pat_period = dts;
  254. if (0 == tsctx->sdt_period)
  255. {
  256. // SDT
  257. tsctx->sdt_period = dts;
  258. n = sdt_write(&tsctx->pat, tsctx->payload);
  259. r = mpeg_ts_write_section_header(ts, TS_PID_SDT, &tsctx->pat.cc /*fixme*/ , tsctx->payload, n);
  260. if (0 != r) return r;
  261. }
  262. // PAT(program_association_section)
  263. n = pat_write(&tsctx->pat, tsctx->payload);
  264. r = mpeg_ts_write_section_header(ts, TS_PID_PAT, &tsctx->pat.cc, tsctx->payload, n); // PID = 0x00 program association table
  265. if (0 != r) return r;
  266. // PMT(Transport stream program map section)
  267. for(i = 0; i < tsctx->pat.pmt_count; i++)
  268. {
  269. n = pmt_write(&tsctx->pat.pmts[i], tsctx->payload);
  270. r = mpeg_ts_write_section_header(ts, tsctx->pat.pmts[i].pid, &tsctx->pat.pmts[i].cc, tsctx->payload, n);
  271. if (0 != r) return r;
  272. }
  273. }
  274. return ts_write_pes(tsctx, pmt, stream, data, bytes);
  275. }
  276. void* mpeg_ts_create(const struct mpeg_ts_func_t *func, void* param)
  277. {
  278. mpeg_ts_enc_context_t *tsctx = NULL;
  279. assert(func);
  280. tsctx = (mpeg_ts_enc_context_t *)calloc(1, sizeof(mpeg_ts_enc_context_t));
  281. if(!tsctx)
  282. return NULL;
  283. mpeg_ts_reset(tsctx);
  284. tsctx->pat.tsid = 1;
  285. tsctx->pat.ver = 0x00;
  286. tsctx->pat.cc = 0;
  287. tsctx->pid = 0x100;
  288. //tsctx->pat.pmt_count = 1; // only one program in ts
  289. //tsctx->pat.pmts[0].pid = 0x100;
  290. //tsctx->pat.pmts[0].pn = 1;
  291. //tsctx->pat.pmts[0].ver = 0x00;
  292. //tsctx->pat.pmts[0].cc = 0;
  293. //tsctx->pat.pmts[0].pminfo_len = 0;
  294. //tsctx->pat.pmts[0].pminfo = NULL;
  295. //tsctx->pat.pmts[0].PCR_PID = 0x1FFF; // 0x1FFF-don't set PCR
  296. //tsctx->pat.pmts[0].stream_count = 2; // H.264 + AAC
  297. //tsctx->pat.pmts[0].streams[0].pid = 0x101;
  298. //tsctx->pat.pmts[0].streams[0].sid = PES_SID_AUDIO;
  299. //tsctx->pat.pmts[0].streams[0].codecid = PSI_STREAM_AAC;
  300. //tsctx->pat.pmts[0].streams[1].pid = 0x102;
  301. //tsctx->pat.pmts[0].streams[1].sid = PES_SID_VIDEO;
  302. //tsctx->pat.pmts[0].streams[1].codecid = PSI_STREAM_H264;
  303. memcpy(&tsctx->func, func, sizeof(tsctx->func));
  304. tsctx->param = param;
  305. return tsctx;
  306. }
  307. int mpeg_ts_destroy(void* ts)
  308. {
  309. uint32_t i;
  310. struct pmt_t* pmt;
  311. mpeg_ts_enc_context_t *tsctx;
  312. tsctx = (mpeg_ts_enc_context_t*)ts;
  313. for(i = 0; i < tsctx->pat.pmt_count; i++)
  314. {
  315. pmt = &tsctx->pat.pmts[i];
  316. mpeg_ts_pmt_destroy(pmt);
  317. }
  318. if (tsctx->pat.pmts && tsctx->pat.pmts != tsctx->pat.pmt_default)
  319. free(tsctx->pat.pmts);
  320. free(tsctx);
  321. return 0;
  322. }
  323. int mpeg_ts_reset(void* ts)
  324. {
  325. mpeg_ts_enc_context_t *tsctx;
  326. tsctx = (mpeg_ts_enc_context_t*)ts;
  327. // tsctx->sdt_period = 0;
  328. tsctx->pat_period = 0;
  329. tsctx->pcr_period = 80 * 90; // 100ms maximum
  330. tsctx->pcr_clock = 0;
  331. tsctx->pat_cycle = 0;
  332. return 0;
  333. }
  334. int mpeg_ts_add_program(void* ts, uint16_t pn, const void* info, int bytes)
  335. {
  336. unsigned int i;
  337. struct pmt_t* pmt;
  338. mpeg_ts_enc_context_t* tsctx;
  339. if (pn < 1 || bytes < 0 || bytes >= (1 << 12))
  340. return -1; // EINVAL: pminfo-len 12-bits
  341. tsctx = (mpeg_ts_enc_context_t*)ts;
  342. for (i = 0; i < tsctx->pat.pmt_count; i++)
  343. {
  344. pmt = &tsctx->pat.pmts[i];
  345. if (pmt->pn == pn)
  346. return -1; // EEXIST
  347. }
  348. assert(tsctx->pat.pmt_count == i);
  349. pmt = pat_alloc_pmt(&tsctx->pat);
  350. if (!pmt)
  351. return -1; // E2BIG
  352. pmt->pid = tsctx->pid++;
  353. pmt->pn = pn;
  354. pmt->ver = 0x00;
  355. pmt->cc = 0;
  356. pmt->PCR_PID = 0x1FFF; // 0x1FFF-don't set PCR
  357. if (bytes > 0 && info)
  358. {
  359. pmt->pminfo = (uint8_t*)malloc(bytes);
  360. if (!pmt->pminfo)
  361. return -1; // ENOMEM
  362. memcpy(pmt->pminfo, info, bytes);
  363. pmt->pminfo_len = bytes;
  364. }
  365. tsctx->pat.pmt_count++;
  366. mpeg_ts_reset(ts); // update PAT/PMT
  367. return 0;
  368. }
  369. int mpeg_ts_remove_program(void* ts, uint16_t pn)
  370. {
  371. unsigned int i;
  372. struct pmt_t* pmt = NULL;
  373. mpeg_ts_enc_context_t* tsctx;
  374. tsctx = (mpeg_ts_enc_context_t*)ts;
  375. for (i = 0; i < tsctx->pat.pmt_count; i++)
  376. {
  377. pmt = &tsctx->pat.pmts[i];
  378. if (pmt->pn != pn)
  379. continue;
  380. mpeg_ts_pmt_destroy(pmt);
  381. if (i + 1 < tsctx->pat.pmt_count)
  382. memmove(&tsctx->pat.pmts[i], &tsctx->pat.pmts[i + 1], (tsctx->pat.pmt_count - i - 1) * sizeof(tsctx->pat.pmts[0]));
  383. tsctx->pat.pmt_count--;
  384. mpeg_ts_reset(ts); // update PAT/PMT
  385. return 0;
  386. }
  387. return -1; // ENOTFOUND
  388. }
  389. static void mpeg_ts_pmt_destroy(struct pmt_t* pmt)
  390. {
  391. unsigned int i;
  392. for (i = 0; i < pmt->stream_count; i++)
  393. {
  394. if (pmt->streams[i].esinfo)
  395. free(pmt->streams[i].esinfo);
  396. }
  397. if (pmt->pminfo)
  398. free(pmt->pminfo);
  399. }
  400. static int mpeg_ts_pmt_add_stream(mpeg_ts_enc_context_t* ts, struct pmt_t* pmt, int codecid, const void* extra_data, size_t extra_data_size)
  401. {
  402. struct pes_t* stream = NULL;
  403. if (!ts || !pmt || pmt->stream_count >= sizeof(pmt->streams) / sizeof(pmt->streams[0]))
  404. {
  405. assert(0);
  406. return -1;
  407. }
  408. stream = &pmt->streams[pmt->stream_count];
  409. stream->codecid = (uint8_t)codecid;
  410. stream->pid = (uint16_t)ts->pid++;
  411. stream->esinfo_len = 0;
  412. stream->esinfo = NULL;
  413. // stream id
  414. // Table 2-22 - Stream_id assignments
  415. if (mpeg_stream_type_video(codecid))
  416. {
  417. // Rec. ITU-T H.262 | ISO/IEC 13818-2, ISO/IEC 11172-2, ISO/IEC 14496-2
  418. // or Rec. ITU-T H.264 | ISO/IEC 14496-10 video stream number
  419. stream->sid = PES_SID_VIDEO;
  420. }
  421. else if (mpeg_stream_type_audio(codecid))
  422. {
  423. // ISO/IEC 13818-3 or ISO/IEC 11172-3 or ISO/IEC 13818-7 or ISO/IEC 14496-3
  424. // audio stream number
  425. stream->sid = PES_SID_AUDIO;
  426. }
  427. else
  428. {
  429. // private_stream_1
  430. stream->sid = PES_SID_PRIVATE_1;
  431. }
  432. if (extra_data_size > 0 && extra_data)
  433. {
  434. stream->esinfo = malloc(extra_data_size);
  435. if (!stream->esinfo)
  436. return -ENOMEM;
  437. memcpy(stream->esinfo, extra_data, extra_data_size);
  438. stream->esinfo_len = (uint16_t)extra_data_size;
  439. }
  440. pmt->stream_count++;
  441. pmt->ver = (pmt->ver + 1) % 32;
  442. mpeg_ts_reset(ts); // immediate update pat/pmt
  443. return stream->pid;
  444. }
  445. int mpeg_ts_add_stream(void* ts, int codecid, const void* extra_data, size_t extra_data_size)
  446. {
  447. struct pmt_t *pmt = NULL;
  448. mpeg_ts_enc_context_t *tsctx;
  449. tsctx = (mpeg_ts_enc_context_t*)ts;
  450. if (0 == tsctx->pat.pmt_count)
  451. {
  452. // add default program
  453. if (0 != mpeg_ts_add_program(tsctx, 1, NULL, 0))
  454. return -1;
  455. }
  456. pmt = &tsctx->pat.pmts[0];
  457. return mpeg_ts_pmt_add_stream(tsctx, pmt, codecid, extra_data, extra_data_size);
  458. }
  459. int mpeg_ts_add_program_stream(void* ts, uint16_t pn, int codecid, const void* extra_data, size_t extra_data_size)
  460. {
  461. unsigned int i;
  462. struct pmt_t* pmt = NULL;
  463. mpeg_ts_enc_context_t* tsctx;
  464. tsctx = (mpeg_ts_enc_context_t*)ts;
  465. for (i = 0; i < tsctx->pat.pmt_count; i++)
  466. {
  467. pmt = &tsctx->pat.pmts[i];
  468. if (pmt->pn == pn)
  469. return mpeg_ts_pmt_add_stream(tsctx, pmt, codecid, extra_data, extra_data_size);
  470. }
  471. return -1; // ENOTFOUND: program not found
  472. }