libtsfuncs is a library for mpeg PSI parsing and generation. https://georgi.unixsol.org/programs/libtsfuncs/

pes.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <netdb.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "tsfuncs.h"
  7. #define pes_data_size_buffer (1024)
  8. #define pes_max_data_size (1024*1024)
  9. struct ts_pes *ts_pes_alloc() {
  10. struct ts_pes *pes = calloc(1, sizeof(struct ts_pes));
  11. pes->pes_data_size = pes_data_size_buffer;
  12. pes->pes_data = malloc(pes->pes_data_size);
  13. memset(pes->pes_data, 0x33, pes->pes_data_size);
  14. return pes;
  15. }
  16. void ts_pes_free(struct ts_pes **ppes) {
  17. struct ts_pes *pes = *ppes;
  18. if (pes) {
  19. FREE(pes->pes_data);
  20. FREE(*ppes);
  21. }
  22. }
  23. void ts_pes_clear(struct ts_pes *pes) {
  24. if (!pes)
  25. return;
  26. // save
  27. uint8_t *pes_data = pes->pes_data;
  28. uint32_t pes_data_size = pes->pes_data_size;
  29. // clear
  30. memset(pes_data, 0x33, pes_data_size);
  31. memset(pes, 0, sizeof(struct ts_pes));
  32. // restore
  33. pes->pes_data = pes_data;
  34. pes->pes_data_size = pes_data_size;
  35. }
  36. static void ts_pes_add_payload_to_pes_data(struct ts_pes *pes, uint8_t *payload, uint8_t payload_size) {
  37. uint32_t new_data_pos = pes->pes_data_pos + payload_size;
  38. // Check if there is enough space in pes->pes_data
  39. if (new_data_pos > pes->pes_data_size) {
  40. int old_data_size = pes->pes_data_size;
  41. pes->pes_data_size *= 2;
  42. // ts_LOGf("realloc to %d from %d\n", old_data_size, pes->pes_data_size);
  43. pes->pes_data = realloc(pes->pes_data, pes->pes_data_size);
  44. // Poison
  45. memset(pes->pes_data + pes->pes_data_pos, 0x34, pes->pes_data_size - old_data_size);
  46. }
  47. memcpy(pes->pes_data + pes->pes_data_pos, payload, payload_size);
  48. pes->pes_data_pos += payload_size;
  49. if (pes->pes_packet_len) {
  50. // If packet size is known, mark the packet as initialized
  51. if (pes->pes_data_pos >= pes->pes_packet_len) {
  52. pes->pes_data_initialized = 1;
  53. }
  54. }
  55. }
  56. // This function is used to determine the last PES was finished before current ts_packet
  57. int ts_pes_is_finished(struct ts_pes *pes, uint8_t *ts_packet) {
  58. if (pes->pes_data_initialized)
  59. return 1;
  60. // Time to mark PES as initialized since current packet has PUSI
  61. if (ts_packet_is_pusi(ts_packet) && pes->real_pes_packet_len == -1) {
  62. pes->real_pes_packet_len = pes->pes_data_pos;
  63. pes->pes_data_initialized = 1;
  64. // ts_LOGf("packet finished, len:%d\n", pes->real_pes_packet_len);
  65. if (!ts_pes_parse(pes)) {
  66. ts_LOGf("error parsing!\n");
  67. ts_pes_clear(pes);
  68. return 0;
  69. }
  70. // ts_LOGf("parsed OK!\n");
  71. return 1;
  72. }
  73. return 0;
  74. }
  75. // Fill is_video, is_audio, is_ac3, etc..flags in PES packet
  76. void ts_pes_fill_type(struct ts_pes *pes, struct ts_pmt *pmt, uint16_t pid) {
  77. int i;
  78. pes->is_audio = IS_AUDIO_STREAM_ID(pes->stream_id);
  79. pes->is_video = IS_VIDEO_STREAM_ID(pes->stream_id);
  80. if (pmt && pmt->initialized) {
  81. for (i=0;i<pmt->streams_num;i++) {
  82. struct ts_pmt_stream *stream = pmt->streams[i];
  83. if (stream->pid != pid)
  84. continue;
  85. pes->is_audio = pes->is_audio && ts_is_stream_type_audio(stream->stream_type);
  86. pes->is_audio_mpeg1 = pes->is_audio && stream->stream_type == STREAM_TYPE_MPEG1_AUDIO;
  87. pes->is_audio_mpeg2 = pes->is_audio && stream->stream_type == STREAM_TYPE_MPEG2_AUDIO;
  88. // To determine if type is AC3 we need to check descriptors
  89. //pes->is_audio_ac3 = ts_is_stream_type_ac3(stream->stream_type); // We need to also check descriptors because this is not ENOUGH!
  90. pes->is_audio_aac = pes->is_audio && stream->stream_type == STREAM_TYPE_ADTS_AUDIO;
  91. pes->is_video = pes->is_video && ts_is_stream_type_video(stream->stream_type);
  92. pes->is_video_mpeg1 = pes->is_video && stream->stream_type == STREAM_TYPE_MPEG1_VIDEO;
  93. pes->is_video_mpeg2 = pes->is_video && stream->stream_type == STREAM_TYPE_MPEG2_VIDEO;
  94. pes->is_video_mpeg4 = pes->is_video && stream->stream_type == STREAM_TYPE_MPEG4_PART2_VIDEO;
  95. pes->is_video_h264 = pes->is_video && stream->stream_type == STREAM_TYPE_AVC_VIDEO;
  96. pes->is_video_avs = pes->is_video && stream->stream_type == STREAM_TYPE_AVS_VIDEO;
  97. if (!stream->ES_info)
  98. break;
  99. // Parse stream descriptors to gather more information
  100. uint8_t tag, this_length;
  101. uint8_t *data = stream->ES_info;
  102. int data_len = stream->ES_info_size;
  103. while (data_len >= 2) {
  104. tag = data[0];
  105. this_length = data[1];
  106. data += 2;
  107. data_len -= 2;
  108. if (this_length > data_len) {
  109. // Not much we can do! Give up.
  110. ts_LOGf("!!! Descriptor 0x%02x says length %d, but only %d bytes left\n", tag, this_length, data_len);
  111. break;
  112. }
  113. switch (tag) {
  114. case 3: { // Audio stream descriptor
  115. struct {
  116. uint8_t free_format_flag : 1,
  117. ID : 1,
  118. layer : 2,
  119. vbr_flag : 1,
  120. reserved : 3;
  121. } as;
  122. if (this_length >= 1) {
  123. as.free_format_flag = bit_on(data[0], bit_8);
  124. as.ID = bit_on(data[0], bit_7);
  125. as.layer = (data[0] &~ 0xcf) >> 4; // 11xx1111
  126. as.vbr_flag = bit_on(data[0], bit_4);
  127. as.reserved = data[0] &~ 0xf0; // 1111xxxx
  128. if (as.ID) {
  129. pes->is_audio = 1;
  130. pes->is_audio_mpeg1l1 = as.layer == 3;
  131. pes->is_audio_mpeg1l2 = as.layer == 2;
  132. pes->is_audio_mpeg1l3 = as.layer == 1;
  133. }
  134. }
  135. break;
  136. }
  137. case 5: { // Registration descriptor
  138. if (this_length == 4) {
  139. uint32_t reg_ident = data[0] << 24;
  140. reg_ident |= data[1] << 16;
  141. reg_ident |= data[2] << 8;
  142. reg_ident |= data[3];
  143. // See http://smpte-ra.org/mpegreg/mpegreg.html
  144. if (reg_ident == 0x41432D33) { // AC-3
  145. pes->is_audio = 1;
  146. pes->is_audio_ac3 = 1;
  147. //ts_LOGf("reg_desc says AC-3\n");
  148. }
  149. if (reg_ident == 0x44545331 || reg_ident == 0x44545332 || reg_ident == 0x44545333) { // DTS1, DTS2, DTS2
  150. pes->is_audio = 1;
  151. pes->is_audio_dts = 1;
  152. //ts_LOGf("reg_desc says DTSx\n");
  153. }
  154. }
  155. break;
  156. }
  157. case 0x6a: { // AC-3 descriptor
  158. //ts_LOGf("ac3_desc found\n");
  159. pes->is_audio = 1;
  160. pes->is_audio_ac3 = 1;
  161. break;
  162. }
  163. case 0x7b: { // DTS descriptor
  164. //ts_LOGf("ac3_desc found\n");
  165. pes->is_audio = 1;
  166. pes->is_audio_dts = 1;
  167. break;
  168. }
  169. case 0x56: { // teletext
  170. //ts_LOGf("teletext_desc found\n");
  171. pes->is_teletext = 1;
  172. break;
  173. }
  174. case 0x59: { // Subtitling descriptor
  175. //ts_LOGf("subtitle_desc found\n");
  176. pes->is_subtitle = 1;
  177. break;
  178. }
  179. } // switch
  180. data_len -= this_length;
  181. data += this_length;
  182. } // while
  183. }
  184. }
  185. }
  186. struct ts_pes *ts_pes_push_packet(struct ts_pes *pes, uint8_t *ts_packet, struct ts_pmt *pmt, uint16_t pid) {
  187. struct ts_header ts_header;
  188. memset(&ts_header, 0, sizeof(struct ts_header));
  189. uint8_t *payload = ts_packet_header_parse(ts_packet, &ts_header);
  190. uint8_t payload_size = ts_header.payload_size;
  191. if (!payload || !payload_size)
  192. goto OUT;
  193. if (ts_header.pusi) {
  194. uint8_t stream_id = 0;
  195. int pes_packet_len = 0;
  196. if (payload[0] == 0x00 && payload[1] == 0x00 && payload[2] == 0x01) { // pes_start_code_prefix
  197. pes->ts_header = ts_header;
  198. stream_id = payload[3];
  199. pes_packet_len = (payload[4] << 8) | payload[5];
  200. // ts_LOGf("NEW PES. Stream_id=%02x pes_length=%d\n", stream_id, pes_packet_len);
  201. if (pes_packet_len == 0 && pes->real_pes_packet_len == -1) {
  202. ts_LOGf("!!! ERROR: New pes with pes_packed_len == 0, started before the old was finished\n");
  203. goto ERROR;
  204. }
  205. if (pes_packet_len > 0) {
  206. pes->real_pes_packet_len = pes_packet_len;
  207. } else {
  208. pes->real_pes_packet_len = -1;
  209. }
  210. pes->stream_id = stream_id;
  211. pes->pes_packet_len = pes_packet_len;
  212. ts_pes_fill_type(pes, pmt, pid);
  213. } else {
  214. ts_LOGf("!!! PES_start_code_prefix not found. Expected 0x00 0x00 0x01 but get 0x%02x 0x%02x 0x%02x! PID %03x\n",
  215. payload[0], payload[1], payload[2], ts_header.pid);
  216. goto ERROR;
  217. }
  218. }
  219. if (pes->stream_id) {
  220. ts_pes_add_payload_to_pes_data(pes, payload, payload_size);
  221. // ts_LOGf("Payload %d added, tot_size: %d\n", payload_size, pes->pes_data_pos);
  222. if (pes->pes_data_pos > pes_max_data_size) {
  223. ts_LOGf("PES Payload size %d exceeded pes_data_max_size: %d pid: %03x\n",
  224. pes->pes_data_pos, pes_max_data_size, pes->ts_header.pid);
  225. goto ERROR;
  226. }
  227. }
  228. if (pes->pes_data_initialized) {
  229. if (!ts_pes_parse(pes))
  230. goto ERROR;
  231. }
  232. OUT:
  233. return pes;
  234. ERROR:
  235. ts_pes_clear(pes);
  236. return pes;
  237. }
  238. int ts_pes_parse(struct ts_pes *pes) {
  239. uint8_t *data = pes->pes_data;
  240. if (!pes->pes_data_initialized) {
  241. ts_LOGf("!!! pes_data_initialized not true\n");
  242. return 0;
  243. }
  244. if (pes->real_pes_packet_len == -1) {
  245. ts_LOGf("!!! real_pes_data_len is == -1\n");
  246. return 0;
  247. }
  248. if (pes->pes_data_size < 6) {
  249. ts_LOGf("!!! PES data_size < 6\n");
  250. return 0;
  251. }
  252. if (!(data[0] == 0x00 && data[1] == 0x00 && data[2] == 0x01)) { // pes_start_code_prefix
  253. ts_LOGf("!!! PES_start_code_prefix error! Expected 0x00 0x00 0x01 but get 0x%02x 0x%02x 0x%02x! PID %03x\n",
  254. data[0], data[1], data[2], pes->ts_header.pid);
  255. return 0;
  256. }
  257. pes->stream_id = data[3]; // 8 bits
  258. pes->pes_packet_len = (data[4] << 8) | data[5]; // 16 bits
  259. int dpos = 6;
  260. if (!IS_PES_STREAM_SUPPORTED(pes->stream_id)) {
  261. // We do not handle this streams...
  262. ts_LOGf("!!! Unsupported stream, ignore! (%02x) PID %03x\n", pes->stream_id, pes->ts_header.pid);
  263. return 0;
  264. }
  265. dpos = 6;
  266. if ((data[dpos] &~ 0x3f) != 0x80) { // 10xxxxxx (first two bits must be 10 eq 0x80
  267. ts_LOGf("!!! No 10 bits at PES start, expected 0x80 get 0x%02x, org: %02x! PID %03x\n",
  268. data[dpos] &~ 0x3f, data[dpos], pes->ts_header.pid);
  269. return 0;
  270. }
  271. // data[dpos] = 0xff;
  272. pes->flags_1 = data[dpos];
  273. pes->scrambling = (data[dpos] &~ 0xCF) >> 4;
  274. pes->priority = bit_on(data[dpos], bit_4);
  275. pes->data_alignment = bit_on(data[dpos], bit_3);
  276. pes->copyright = bit_on(data[dpos], bit_2);
  277. pes->original_or_copy = bit_on(data[dpos], bit_1);
  278. dpos = 7;
  279. // data[dpos] = 0xff;
  280. pes->flags_2 = data[dpos];
  281. pes->PTS_flag = bit_on(data[dpos], bit_8);
  282. pes->DTS_flag = bit_on(data[dpos], bit_7);
  283. pes->ESCR_flag = bit_on(data[dpos], bit_6);
  284. pes->ES_rate_flag = bit_on(data[dpos], bit_5);
  285. pes->trick_mode_flag = bit_on(data[dpos], bit_4);
  286. pes->add_copy_info_flag = bit_on(data[dpos], bit_3);
  287. pes->pes_crc_flag = bit_on(data[dpos], bit_2);
  288. pes->pes_extension_flag = bit_on(data[dpos], bit_1);
  289. dpos = 8;
  290. pes->pes_header_len = data[dpos];
  291. dpos = 9;
  292. if (!pes->PTS_flag && pes->DTS_flag) // Invalid, can't have only DTS flag
  293. return 0;
  294. if (pes->PTS_flag && !pes->DTS_flag) {
  295. if (!ts_decode_pts_dts(&data[dpos], 2, &pes->PTS))
  296. return 0;
  297. dpos += 5;
  298. pes->have_pts = 1;
  299. }
  300. if (pes->PTS_flag && pes->DTS_flag) {
  301. if (!ts_decode_pts_dts(&data[dpos], 3, &pes->PTS))
  302. return 0;
  303. pes->have_pts = 1;
  304. dpos += 5;
  305. if (!ts_decode_pts_dts(&data[dpos], 1, &pes->DTS))
  306. return 0;
  307. pes->have_dts = 1;
  308. dpos += 5;
  309. }
  310. if (pes->ESCR_flag) {
  311. uint64_t ESCR_base;
  312. uint32_t ESCR_extn;
  313. ESCR_base = (data[dpos+4] >> 3) |
  314. (data[dpos+3] << 5) |
  315. (data[dpos+2] << 13) |
  316. (data[dpos+1] << 20) |
  317. ((((uint64_t)data[dpos]) & 0x03) << 28) |
  318. ((((uint64_t)data[dpos]) & 0x38) << 27);
  319. ESCR_extn = (data[dpos+5] >> 1) | (data[dpos+4] << 7);
  320. pes->ESCR = ESCR_base * 300 + ESCR_extn;
  321. dpos += 6;
  322. }
  323. if (pes->ES_rate_flag) {
  324. // Not decoded...
  325. dpos += 3;
  326. }
  327. if (pes->trick_mode_flag) {
  328. // Not decoded...
  329. dpos += 1;
  330. }
  331. if (pes->add_copy_info_flag) {
  332. // Not decoded...
  333. dpos += 1;
  334. }
  335. if (pes->pes_crc_flag) {
  336. // Not decoded...
  337. dpos += 2;
  338. }
  339. if (pes->pes_extension_flag) {
  340. // data[dpos] = 0xff;
  341. pes->flags_3 = data[dpos];
  342. pes->pes_private_data_flag = bit_on(data[dpos], bit_8); // 1xxxxxxx
  343. pes->pack_header_field_flag = bit_on(data[dpos], bit_7); // x1xxxxxx
  344. pes->program_packet_seq_counter_flag = bit_on(data[dpos], bit_6); // xx1xxxxx
  345. pes->p_std_buffer_flag = bit_on(data[dpos], bit_5); // xxx1xxxx
  346. pes->reserved2 = (data[dpos] &~ 0x0e) >> 1; // xxxx111x
  347. pes->pes_extension2_flag = bit_on(data[dpos], bit_1); // xxxxxxx1
  348. if (pes->pes_private_data_flag) {
  349. pes->pes_private_data_1 = (pes->pes_private_data_1 << 8) | data[dpos+7];
  350. pes->pes_private_data_1 = (pes->pes_private_data_1 << 8) | data[dpos+6];
  351. pes->pes_private_data_1 = (pes->pes_private_data_1 << 8) | data[dpos+5];
  352. pes->pes_private_data_1 = (pes->pes_private_data_1 << 8) | data[dpos+4];
  353. pes->pes_private_data_1 = (pes->pes_private_data_1 << 8) | data[dpos+3];
  354. pes->pes_private_data_1 = (pes->pes_private_data_1 << 8) | data[dpos+2];
  355. pes->pes_private_data_1 = (pes->pes_private_data_1 << 8) | data[dpos+1];
  356. pes->pes_private_data_1 = (pes->pes_private_data_1 << 8) | data[dpos+0];
  357. dpos += 8;
  358. pes->pes_private_data_2 = (pes->pes_private_data_2 << 8) | data[dpos+7];
  359. pes->pes_private_data_2 = (pes->pes_private_data_2 << 8) | data[dpos+6];
  360. pes->pes_private_data_2 = (pes->pes_private_data_2 << 8) | data[dpos+5];
  361. pes->pes_private_data_2 = (pes->pes_private_data_2 << 8) | data[dpos+4];
  362. pes->pes_private_data_2 = (pes->pes_private_data_2 << 8) | data[dpos+3];
  363. pes->pes_private_data_2 = (pes->pes_private_data_2 << 8) | data[dpos+2];
  364. pes->pes_private_data_2 = (pes->pes_private_data_2 << 8) | data[dpos+1];
  365. pes->pes_private_data_2 = (pes->pes_private_data_2 << 8) | data[dpos+0];
  366. dpos += 8;
  367. }
  368. if (pes->pack_header_field_flag) {
  369. pes->pack_header_len = data[dpos];
  370. pes->pack_header = pes->pes_data + 1 + dpos; // Pointer into *pes_data
  371. dpos += 1 + pes->pack_header_len;
  372. }
  373. if (pes->program_packet_seq_counter_flag) {
  374. // Not decoded
  375. dpos += 2;
  376. }
  377. if (pes->p_std_buffer_flag) {
  378. if ((data[dpos] &~ 0x3f) != 0x40) { // 01xxxxxx (first two bits must be 01 eq 0x40
  379. return 0;
  380. }
  381. // Not decoded...
  382. dpos += 2;
  383. }
  384. if (pes->pes_extension2_flag) {
  385. pes->pes_extension_field_len = data[dpos] &~ 0x80; // x1111111
  386. pes->pes_extension2 = pes->pes_data + 1 + dpos; // Pointer into *pes_data
  387. dpos += 1 + pes->pes_extension_field_len;
  388. }
  389. }
  390. int maxstuffing = 32; // Maximum 32 stuffing bytes
  391. // Skip stuffing bytes (8 is minimum PES header len)
  392. while ((--maxstuffing > 0) && (dpos-8 <= pes->pes_header_len) && (data[dpos] == 0xff)) {
  393. dpos++;
  394. }
  395. pes->es_data = pes->pes_data + dpos;
  396. pes->es_data_size = pes->real_pes_packet_len - dpos;
  397. pes->initialized = 1;
  398. if (pes->data_alignment)
  399. ts_pes_es_parse(pes);
  400. return 1;
  401. }
  402. #define min(a,b) ((a < b) ? a : b)
  403. void ts_pes_dump(struct ts_pes *pes) {
  404. if (!pes->initialized)
  405. return;
  406. ts_LOGf("PES packet\n");
  407. ts_packet_header_dump(&pes->ts_header);
  408. ts_LOGf(" * Content : %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
  409. pes->is_audio ? "Audio " : "",
  410. pes->is_audio_mpeg1 ? "MP1 " : "",
  411. pes->is_audio_mpeg1l1 ? "Layer1 " : "",
  412. pes->is_audio_mpeg1l2 ? "Layer2 " : "",
  413. pes->is_audio_mpeg1l3 ? "Layer3 " : "",
  414. pes->is_audio_mpeg2 ? "MP2 " : "",
  415. pes->is_audio_aac ? "AAC " : "",
  416. pes->is_audio_ac3 ? "AC3 " : "",
  417. pes->is_audio_dts ? "DTS " : "",
  418. pes->is_video ? "Video " : "",
  419. pes->is_video_mpeg1 ? "MPEG1 " : "",
  420. pes->is_video_mpeg2 ? "MPEG2 " : "",
  421. pes->is_video_mpeg4 ? "MPEG4p2 " : "",
  422. pes->is_video_h264 ? "H.264 " : "",
  423. pes->is_video_avs ? "AVS " : "",
  424. pes->is_teletext ? "Teletext " : "",
  425. pes->is_subtitle ? "Subtitles " : ""
  426. );
  427. char *stream_id_text = h222_stream_id_desc(pes->stream_id);
  428. ts_LOGf(" * Stream_id : 0x%02x (%d) %s\n", pes->stream_id, pes->stream_id, stream_id_text);
  429. FREE(stream_id_text);
  430. ts_LOGf(" * Packet len : 0x%04x (%d) real_len: %d %s\n",
  431. pes->pes_packet_len, pes->pes_packet_len, pes->real_pes_packet_len-pes->pes_header_len-6, pes->is_video ? "VIDEO" : (pes->is_audio ? "AUDIO" : "OTHER"));
  432. ts_LOGf(" * Header len : %d\n", pes->pes_header_len);
  433. ts_LOGf(" * Flags 1 : 0x%02x | scrambling=%d priority=%d data_align=%d copyright=%d org_or_copy=%d\n",
  434. pes->flags_1,
  435. pes->scrambling,
  436. pes->priority,
  437. pes->data_alignment,
  438. pes->copyright,
  439. pes->original_or_copy
  440. );
  441. ts_LOGf(" * Flags 2 : 0x%02x | %s%s%s%s%s%s%s%s\n",
  442. pes->flags_2,
  443. pes->PTS_flag ? "PTS " : "",
  444. pes->DTS_flag ? "DTS " : "",
  445. pes->ESCR_flag ? "ESCR " : "",
  446. pes->ES_rate_flag ? "ES_rate " : "",
  447. pes->trick_mode_flag ? "Trick_mode " : "",
  448. pes->add_copy_info_flag ? "Add_copy " : "",
  449. pes->pes_crc_flag ? "PES_CRC " : "",
  450. pes->pes_extension_flag ? "PES_Ext " : ""
  451. );
  452. if (pes->PTS_flag && pes->have_pts)
  453. ts_LOGf(" * PTS : %llu (%llu ms) (%llu.%04llu sec)\n",
  454. pes->PTS,
  455. pes->PTS / 90,
  456. pes->PTS / 90000, (pes->PTS % 90000) / 9
  457. );
  458. if (pes->DTS_flag && pes->have_dts)
  459. ts_LOGf(" * DTS : %llu (%llu ms) (%llu.%04llu sec)\n",
  460. pes->DTS,
  461. pes->DTS / 90,
  462. pes->DTS / 90000, (pes->DTS % 90000) / 9
  463. );
  464. if (pes->ESCR_flag)
  465. ts_LOGf(" * ESCR : %llu\n", pes->ESCR);
  466. if (pes->ES_rate_flag)
  467. ts_LOGf(" * ES_rate : %lu\n" , (unsigned long)pes->ES_rate * 50); // In units of 50 bytes
  468. if (pes->pes_extension_flag) {
  469. ts_LOGf(" * Ext flags : 0x%02x | %s%s%s%s%s\n",
  470. pes->flags_3,
  471. pes->pes_private_data_flag ? "Private_data_flag " : "",
  472. pes->pack_header_field_flag ? "Pack_header_flag " : "",
  473. pes->program_packet_seq_counter_flag ? "Prg_pack_seq_flag " : "",
  474. pes->p_std_buffer_flag ? "P-STD_buf_flag " : "",
  475. pes->pes_extension2_flag ? "Ext2_flag " : ""
  476. );
  477. }
  478. if (pes->pes_private_data_flag)
  479. ts_LOGf(" * PES priv_data : 0x%08llx%08llx\n", pes->pes_private_data_1, pes->pes_private_data_2);
  480. if (pes->pack_header_field_flag) {
  481. ts_LOGf(" * Pack_header ... \n");
  482. }
  483. if (pes->program_packet_seq_counter_flag) {
  484. ts_LOGf(" * Prg_seq_cnt : %d\n", pes->program_packet_seq_counter);
  485. }
  486. ts_LOGf(" - Private : pes_data_pos:%u es_data_size:%u\n",
  487. pes->pes_data_pos,
  488. pes->es_data_size
  489. );
  490. char *phex = ts_hex_dump(pes->pes_data, min(32, pes->pes_data_pos), 0);
  491. ts_LOGf(" - PES dump : %s...\n", phex);
  492. free(phex);
  493. if (pes->es_data) {
  494. char *hex = ts_hex_dump(pes->es_data, min(32, pes->es_data_size), 0);
  495. ts_LOGf(" - ES dump : %s...\n", hex);
  496. free(hex);
  497. }
  498. }