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

pes.c 18KB

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