Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

164 linhas
5.5KB

  1. #include "mpeg-ps-internal.h"
  2. #include "mpeg-util.h"
  3. #include <assert.h>
  4. // 2.5.3.5 System header (p79)
  5. // Table 2-40 - Program stream system header
  6. #if 1
  7. int system_header_read(struct ps_system_header_t* h, struct mpeg_bits_t* reader)
  8. {
  9. uint8_t v8;
  10. uint16_t v16;
  11. size_t i, len, end;
  12. len = mpeg_bits_read16(reader);
  13. end = mpeg_bits_tell(reader) + len;
  14. if (mpeg_bits_error(reader) || end > mpeg_bits_length(reader))
  15. return MPEG_ERROR_NEED_MORE_DATA;
  16. //assert((0x80 & data[6]) == 0x80); // '1xxxxxxx'
  17. //assert((0x01 & data[8]) == 0x01); // 'xxxxxxx1'
  18. h->rate_bound = (mpeg_bits_read8(reader) & 0x7F) << 15;
  19. h->rate_bound |= mpeg_bits_read15(reader);
  20. v8 = mpeg_bits_read8(reader);
  21. h->audio_bound = (v8 >> 2) & 0x3F;
  22. h->fixed_flag = (v8 >> 1) & 0x01;
  23. h->CSPS_flag = (v8 >> 0) & 0x01;
  24. v8 = mpeg_bits_read8(reader);
  25. assert((0x20 & v8) == 0x20); // 'xx1xxxxx'
  26. h->system_audio_lock_flag = (v8 >> 7) & 0x01;
  27. h->system_video_lock_flag = (v8 >> 6) & 0x01;
  28. h->video_bound = v8 & 0x1F;
  29. // assert((0x7F & data[11]) == 0x00); // 'x0000000'
  30. h->packet_rate_restriction_flag = (mpeg_bits_read8(reader) >> 7) & 0x01;
  31. for (i = 0; 0 == mpeg_bits_error(reader) && mpeg_bits_tell(reader) + 1 < end && i < sizeof(h->streams) / sizeof(h->streams[0]); i++)
  32. {
  33. v8 = mpeg_bits_read8(reader);
  34. if ((v8 & 0x80) != 0x80)
  35. break;
  36. h->streams[i].stream_id = v8;
  37. if (h->streams[i].stream_id == PES_SID_EXTENSION) // '10110111'
  38. {
  39. v8 = mpeg_bits_read8(reader); assert(v8 == 0xC0); // '11000000'
  40. h->streams[i].stream_id = mpeg_bits_read8(reader) & 0x7F;
  41. v8 = mpeg_bits_read8(reader); assert(v8 == 0xB6); // '10110110'
  42. }
  43. v16 = mpeg_bits_read16(reader);
  44. assert((v16 & 0xC000) == 0xC000); // '11xxxxxx'
  45. h->streams[i].buffer_bound_scale = (v16 >> 13) & 0x01;
  46. h->streams[i].buffer_size_bound = v16 & 0x1FFF;
  47. }
  48. assert(0 == mpeg_bits_error(reader));
  49. assert(end == mpeg_bits_tell(reader));
  50. return MPEG_ERROR_OK;
  51. }
  52. #else
  53. size_t system_header_read(struct ps_system_header_t *h, const uint8_t* data, size_t bytes)
  54. {
  55. size_t i, j;
  56. size_t len;
  57. if (bytes < 12) return 0;
  58. assert(0x00 == data[0] && 0x00 == data[1] && 0x01 == data[2] && PES_SID_SYS == data[3]);
  59. len = (data[4] << 8) | data[5];
  60. if(len + 6 > bytes)
  61. {
  62. assert(0);
  63. return 0;
  64. }
  65. assert((0x80 & data[6]) == 0x80); // '1xxxxxxx'
  66. assert((0x01 & data[8]) == 0x01); // 'xxxxxxx1'
  67. h->rate_bound = ((data[6] & 0x7F) << 15) | (data[7] << 7) | ((data[8] >> 1) & 0x7F);
  68. h->audio_bound = (data[9] >> 2) & 0x3F;
  69. h->fixed_flag = (data[9] >> 1) & 0x01;
  70. h->CSPS_flag = (data[9] >> 0) & 0x01;
  71. assert((0x20 & data[10]) == 0x20); // 'xx1xxxxx'
  72. h->system_audio_lock_flag = (data[10] >> 7) & 0x01;
  73. h->system_video_lock_flag = (data[10] >> 6) & 0x01;
  74. h->video_bound = data[10] & 0x1F;
  75. // assert((0x7F & data[11]) == 0x00); // 'x0000000'
  76. h->packet_rate_restriction_flag = (data[11] >> 7) & 0x01;
  77. i = 12;
  78. for (j = 0; (data[i] & 0x80) == 0x80 && j < sizeof(h->streams) / sizeof(h->streams[0]) && i < bytes; j++)
  79. {
  80. h->streams[j].stream_id = data[i++];
  81. if (h->streams[j].stream_id == PES_SID_EXTENSION) // '10110111'
  82. {
  83. assert(data[i] == 0xC0); // '11000000'
  84. assert((data[i + 1] & 80) == 0); // '1xxxxxxx'
  85. h->streams[j].stream_id = (h->streams[j].stream_id << 7) | (data[i + 1] & 0x7F);
  86. assert(data[i + 2] == 0xB6); // '10110110'
  87. i += 3;
  88. }
  89. assert((data[i] & 0xC0) == 0xC0); // '11xxxxxx'
  90. h->streams[j].buffer_bound_scale = (data[i] >> 5) & 0x01;
  91. h->streams[j].buffer_size_bound = (data[i] & 0x1F) | data[i + 1];
  92. i += 2;
  93. }
  94. return len + 4 + 2;
  95. }
  96. #endif
  97. size_t system_header_write(const struct ps_system_header_t *h, uint8_t *data)
  98. {
  99. size_t i, j;
  100. // system_header_start_code
  101. nbo_w32(data, 0x000001BB);
  102. // header length
  103. //put16(data + 4, 6 + h->stream_count*3);
  104. // rate_bound
  105. // 1xxxxxxx xxxxxxxx xxxxxxx1
  106. data[6] = 0x80 | ((h->rate_bound >> 15) & 0x7F);
  107. data[7] = (h->rate_bound >> 7) & 0xFF;
  108. data[8] = 0x01 | ((h->rate_bound & 0x7F) << 1);
  109. // 6-audio_bound + 1-fixed_flag + 1-CSPS_flag
  110. data[9] = ((h->audio_bound & 0x3F) << 2) | ((h->fixed_flag & 0x01) << 1) | (h->CSPS_flag & 0x01);
  111. // 1-system_audio_lock_flag + 1-system_video_lock_flag + 1-maker + 5-video_bound
  112. data[10] = 0x20 | ((h->system_audio_lock_flag & 0x01) << 7) | ((h->system_video_lock_flag & 0x01) << 6) | (h->video_bound & 0x1F);
  113. // 1-packet_rate_restriction_flag + 7-reserved
  114. data[11] = 0x7F | ((h->packet_rate_restriction_flag & 0x01) << 7);
  115. i = 12;
  116. for (j = 0; j < h->stream_count; j++)
  117. {
  118. data[i++] = (uint8_t)h->streams[j].stream_id;
  119. if (PES_SID_EXTENSION == h->streams[j].stream_id) // '10110111'
  120. {
  121. data[i++] = 0xD0; // '11000000'
  122. data[i++] = h->streams[j].stream_extid & 0x7F; // '0xxxxxxx'
  123. data[i++] = 0xB6; // '10110110'
  124. }
  125. // '11' + 1-P-STD_buffer_bound_scale + 13-P-STD_buffer_size_bound
  126. // '11xxxxxx xxxxxxxx'
  127. data[i++] = 0xC0 | ((h->streams[j].buffer_bound_scale & 0x01) << 5) | ((h->streams[j].buffer_size_bound >> 8) & 0x1F);
  128. data[i++] = h->streams[j].buffer_size_bound & 0xFF;
  129. }
  130. // header length
  131. nbo_w16(data + 4, (uint16_t)(i - 6));
  132. return i;
  133. }