You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
6.2KB

  1. // ITU-T H.222.0(10/2014)
  2. // Information technology - Generic coding of moving pictures and associated audio information: Systems
  3. // 2.5.5 Program stream directory(p84)
  4. #include "mpeg-ps-internal.h"
  5. #include <assert.h>
  6. #if 1
  7. int psd_read(struct psd_t* psd, struct mpeg_bits_t* reader)
  8. {
  9. size_t i, end;
  10. size_t packet_length;
  11. uint8_t v8;
  12. uint64_t v64;
  13. uint16_t number_of_access_units;
  14. // Table 2-42 - Program stream directory packet(p81)
  15. packet_length = mpeg_bits_read16(reader);
  16. end = mpeg_bits_tell(reader) + packet_length;
  17. if (mpeg_bits_error(reader) || end > mpeg_bits_length(reader))
  18. return MPEG_ERROR_NEED_MORE_DATA;
  19. //assert((0x01 & data[7]) == 0x01); // 'xxxxxxx1'
  20. number_of_access_units = mpeg_bits_read15(reader); // (data[6] << 7) | ((data[7] >> 1) & 0x7F);
  21. assert(number_of_access_units <= N_ACCESS_UNIT);
  22. //assert((0x01 & data[9]) == 0x01); // 'xxxxxxx1'
  23. //assert((0x01 & data[11]) == 0x01); // 'xxxxxxx1'
  24. //assert((0x01 & data[13]) == 0x01); // 'xxxxxxx1'
  25. psd->prev_directory_offset = mpeg_bits_read45(reader);
  26. //assert((0x01 & data[15]) == 0x01); // 'xxxxxxx1'
  27. //assert((0x01 & data[17]) == 0x01); // 'xxxxxxx1'
  28. //assert((0x01 & data[19]) == 0x01); // 'xxxxxxx1'
  29. psd->next_directory_offset = mpeg_bits_read45(reader);
  30. // access unit
  31. for (i = 0; 0 == mpeg_bits_error(reader) && mpeg_bits_tell(reader) + 18 <= end && i < number_of_access_units && i < N_ACCESS_UNIT; i++)
  32. {
  33. psd->units[i].packet_stream_id = mpeg_bits_read8(reader);
  34. //assert((0x01 & data[2]) == 0x01); // 'xxxxxxx1'
  35. //assert((0x01 & data[4]) == 0x01); // 'xxxxxxx1'
  36. //assert((0x01 & data[6]) == 0x01); // 'xxxxxxx1'
  37. v64 = mpeg_bits_read45(reader);
  38. psd->units[i].pes_header_position_offset = v64 & 0x3FFFFF;
  39. psd->units[i].pes_header_position_offset_sign = (psd->units[i].pes_header_position_offset & 0x400000) ? 1 : 0;
  40. psd->units[i].reference_offset = mpeg_bits_read16(reader);
  41. //assert((0x81 & data[9]) == 0x81); // '1xxxxxx1'
  42. v8 = mpeg_bits_read8(reader);
  43. if (psd->units[i].packet_stream_id == 0xFD)
  44. {
  45. psd->units[i].packet_stream_id_extension_msbs = (v8 >> 4) & 0x07;
  46. }
  47. else
  48. {
  49. assert((0x70 & v8) == 0x00); // '1000xxx1'
  50. }
  51. //assert((0x01 & data[11]) == 0x01); // 'xxxxxxx1'
  52. //assert((0x01 & data[13]) == 0x01); // 'xxxxxxx1'
  53. psd->units[i].PTS = (((v8 >> 1) & 0x07) << 30) | mpeg_bits_read30(reader);
  54. //assert((0x01 & data[15]) == 0x01); // 'xxxxxxx1'
  55. psd->units[i].bytes_to_read = mpeg_bits_read15(reader) << 15;
  56. psd->units[i].bytes_to_read |= mpeg_bits_read8(reader);
  57. v8 = mpeg_bits_read8(reader);
  58. //assert((0x80 & data[17]) == 0x80); // '1xxxxxxx'
  59. psd->units[i].intra_coded_indicator = (v8 >> 6) & 0x01;
  60. psd->units[i].coding_parameters_indicator = (v8 >> 4) & 0x03;
  61. if (0xFD == psd->units[i].packet_stream_id)
  62. {
  63. psd->units[i].packet_stream_id_extension_lsbs = v8 & 0x0F;
  64. }
  65. else
  66. {
  67. assert((0x0F & v8) == 0x00); // '1xxx0000'
  68. }
  69. }
  70. assert(0 == mpeg_bits_error(reader));
  71. assert(end == mpeg_bits_tell(reader));
  72. return MPEG_ERROR_OK;
  73. }
  74. #else
  75. size_t psd_read(struct psd_t *psd, const uint8_t* data, size_t bytes)
  76. {
  77. size_t i, j;
  78. size_t packet_length;
  79. uint16_t number_of_access_units;
  80. // Table 2-42 - Program stream directory packet(p81)
  81. assert(0x00==data[0] && 0x00==data[1] && 0x01==data[2] && 0xFF==data[3]);
  82. packet_length = (((uint16_t)data[4]) << 8) | data[5];
  83. assert(bytes >= (size_t)packet_length + 6);
  84. if (bytes < 20 || packet_length < 20 - 6)
  85. return 0; // invalid data length
  86. assert((0x01 & data[7]) == 0x01); // 'xxxxxxx1'
  87. number_of_access_units = (data[6] << 7) | ((data[7] >> 1) & 0x7F);
  88. assert(number_of_access_units <= N_ACCESS_UNIT);
  89. assert((0x01 & data[9]) == 0x01); // 'xxxxxxx1'
  90. assert((0x01 & data[11]) == 0x01); // 'xxxxxxx1'
  91. assert((0x01 & data[13]) == 0x01); // 'xxxxxxx1'
  92. psd->prev_directory_offset = (uint64_t)(((uint64_t)data[8] << 38) | ((((uint64_t)data[9] >> 1) & 0x7F) << 30) | ((uint64_t)data[10] << 22) | ((((uint64_t)data[11] >> 1) & 0x7F) << 15) | ((uint64_t)data[12] << 7) | (((uint64_t)data[13] >> 1) & 0x7F));
  93. assert((0x01 & data[15]) == 0x01); // 'xxxxxxx1'
  94. assert((0x01 & data[17]) == 0x01); // 'xxxxxxx1'
  95. assert((0x01 & data[19]) == 0x01); // 'xxxxxxx1'
  96. psd->next_directory_offset = (uint64_t)(((uint64_t)data[14] << 38) | ((((uint64_t)data[15] >> 1) & 0x7F) << 30) | ((uint64_t)data[16] << 22) | ((((uint64_t)data[17] >> 1) & 0x7F) << 15) | ((uint64_t)data[18] << 7) | (((uint64_t)data[19] >> 1) & 0x7F));
  97. // access unit
  98. j = 20;
  99. for(i = 0; j + 18 <= packet_length + 6 && i < number_of_access_units && i < N_ACCESS_UNIT; i++)
  100. {
  101. psd->units[i].packet_stream_id = data[j];
  102. psd->units[i].pes_header_position_offset_sign = (data[j+1] >> 7) & 0x01;
  103. assert((0x01 & data[j+2]) == 0x01); // 'xxxxxxx1'
  104. assert((0x01 & data[j+4]) == 0x01); // 'xxxxxxx1'
  105. assert((0x01 & data[j+6]) == 0x01); // 'xxxxxxx1'
  106. psd->units[i].pes_header_position_offset = (uint64_t)(((uint64_t)(data[j+1] & 0x7F) << 37) | ((((uint64_t)data[j+2] >> 1) & 0x7F) << 30) | ((uint64_t)data[j+3] << 22) | ((((uint64_t)data[j+4] >> 1) & 0x7F) << 15) | ((uint64_t)data[j+5] << 7) | (((uint64_t)data[j+6] >> 1) & 0x7F));
  107. psd->units[i].reference_offset = (data[j+7] << 8) | data[j+8];
  108. assert((0x81 & data[j+9]) == 0x81); // '1xxxxxx1'
  109. if(psd->units[i].packet_stream_id == 0xFD)
  110. {
  111. psd->units[i].packet_stream_id_extension_msbs = (data[j+9] >> 4) & 0x07;
  112. }
  113. else
  114. {
  115. assert((0x70 & data[j+9]) == 0x00); // '1000xxx1'
  116. }
  117. assert((0x01 & data[j+11]) == 0x01); // 'xxxxxxx1'
  118. assert((0x01 & data[j+13]) == 0x01); // 'xxxxxxx1'
  119. psd->units[i].PTS = (uint64_t)(((uint64_t)((data[j+9] >> 1) & 0x07) << 30) | ((uint64_t)data[j+10] << 22) | ((((uint64_t)data[j+11] >> 1) & 0x7F) << 15) | ((uint64_t)data[j+12] << 7) | (((uint64_t)data[j+13] >> 1) & 0x7F));
  120. assert((0x01 & data[j+15]) == 0x01); // 'xxxxxxx1'
  121. psd->units[i].bytes_to_read = (uint32_t)( ((uint32_t)data[j+14] << 15) | (((data[j+15] >> 1) & 0x7F) << 8) | data[j+16]);
  122. assert((0x80 & data[j+17]) == 0x80); // '1xxxxxxx'
  123. psd->units[i].intra_coded_indicator = (data[j+17] >> 6) & 0x01;
  124. psd->units[i].coding_parameters_indicator = (data[j+17] >> 4) & 0x03;
  125. if(0xFD == psd->units[i].packet_stream_id)
  126. {
  127. psd->units[i].packet_stream_id_extension_lsbs = data[j+17] & 0x0F;
  128. }
  129. else
  130. {
  131. assert((0x0F & data[j+17]) == 0x00); // '1xxx0000'
  132. }
  133. j += 18;
  134. }
  135. return j;
  136. }
  137. #endif