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ů.

312 řádky
10.0KB

  1. #include "mpeg4-hevc.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #define H265_VPS 32
  6. #define H265_SPS 33
  7. #define H265_PPS 34
  8. #define H265_PREFIX_SEI 39
  9. #define H265_SUFFIX_SEI 40
  10. static uint8_t* w32(uint8_t* p, uint32_t v)
  11. {
  12. *p++ = (uint8_t)(v >> 24);
  13. *p++ = (uint8_t)(v >> 16);
  14. *p++ = (uint8_t)(v >> 8);
  15. *p++ = (uint8_t)v;
  16. return p;
  17. }
  18. static uint8_t* w16(uint8_t* p, uint16_t v)
  19. {
  20. *p++ = (uint8_t)(v >> 8);
  21. *p++ = (uint8_t)v;
  22. return p;
  23. }
  24. /*
  25. ISO/IEC 14496-15:2017(E) 8.3.3.1.2 Syntax (p71)
  26. aligned(8) class HEVCDecoderConfigurationRecord {
  27. unsigned int(8) configurationVersion = 1;
  28. unsigned int(2) general_profile_space;
  29. unsigned int(1) general_tier_flag;
  30. unsigned int(5) general_profile_idc;
  31. unsigned int(32) general_profile_compatibility_flags;
  32. unsigned int(48) general_constraint_indicator_flags;
  33. unsigned int(8) general_level_idc;
  34. bit(4) reserved = '1111'b;
  35. unsigned int(12) min_spatial_segmentation_idc;
  36. bit(6) reserved = '111111'b;
  37. unsigned int(2) parallelismType;
  38. bit(6) reserved = '111111'b;
  39. unsigned int(2) chromaFormat;
  40. bit(5) reserved = '11111'b;
  41. unsigned int(3) bitDepthLumaMinus8;
  42. bit(5) reserved = '11111'b;
  43. unsigned int(3) bitDepthChromaMinus8;
  44. bit(16) avgFrameRate;
  45. bit(2) constantFrameRate;
  46. bit(3) numTemporalLayers;
  47. bit(1) temporalIdNested;
  48. unsigned int(2) lengthSizeMinusOne;
  49. unsigned int(8) numOfArrays;
  50. for (j=0; j < numOfArrays; j++) {
  51. bit(1) array_completeness;
  52. unsigned int(1) reserved = 0;
  53. unsigned int(6) NAL_unit_type;
  54. unsigned int(16) numNalus;
  55. for (i=0; i< numNalus; i++) {
  56. unsigned int(16) nalUnitLength;
  57. bit(8*nalUnitLength) nalUnit;
  58. }
  59. }
  60. }
  61. */
  62. int mpeg4_hevc_decoder_configuration_record_load(const uint8_t* data, size_t bytes, struct mpeg4_hevc_t* hevc)
  63. {
  64. uint8_t nalutype;
  65. uint16_t i, j, k, n, numOfArrays;
  66. const uint8_t* p;
  67. uint8_t* dst;
  68. if (bytes < 23)
  69. return -1;
  70. hevc->configurationVersion = data[0];
  71. if (1 != hevc->configurationVersion)
  72. return -1;
  73. hevc->general_profile_space = (data[1] >> 6) & 0x03;
  74. hevc->general_tier_flag = (data[1] >> 5) & 0x01;
  75. hevc->general_profile_idc = data[1] & 0x1F;
  76. hevc->general_profile_compatibility_flags = (data[2] << 24) | (data[3] << 16) | (data[4] << 8) | data[5];
  77. hevc->general_constraint_indicator_flags = ((uint32_t)data[6] << 24) | ((uint32_t)data[7] << 16) | ((uint32_t)data[8] << 8) | (uint32_t)data[9];
  78. hevc->general_constraint_indicator_flags = (hevc->general_constraint_indicator_flags << 16) | (((uint64_t)data[10]) << 8) | data[11];
  79. hevc->general_level_idc = data[12];
  80. hevc->min_spatial_segmentation_idc = ((data[13] & 0x0F) << 8) | data[14];
  81. hevc->parallelismType = data[15] & 0x03;
  82. hevc->chromaFormat = data[16] & 0x03;
  83. hevc->bitDepthLumaMinus8 = data[17] & 0x07;
  84. hevc->bitDepthChromaMinus8 = data[18] & 0x07;
  85. hevc->avgFrameRate = (data[19] << 8) | data[20];
  86. hevc->constantFrameRate = (data[21] >> 6) & 0x03;
  87. hevc->numTemporalLayers = (data[21] >> 3) & 0x07;
  88. hevc->temporalIdNested = (data[21] >> 2) & 0x01;
  89. hevc->lengthSizeMinusOne = data[21] & 0x03;
  90. numOfArrays = data[22];
  91. p = data + 23;
  92. dst = hevc->data;
  93. hevc->numOfArrays = 0;
  94. for (i = 0; i < numOfArrays; i++)
  95. {
  96. if (p + 3 > data + bytes)
  97. return -1;
  98. nalutype = p[0];
  99. n = (p[1] << 8) | p[2];
  100. p += 3;
  101. for (j = 0; j < n; j++)
  102. {
  103. if (hevc->numOfArrays >= sizeof(hevc->nalu) / sizeof(hevc->nalu[0]))
  104. {
  105. assert(0);
  106. return -1; // too many nalu(s)
  107. }
  108. if (p + 2 > data + bytes)
  109. return -1;
  110. k = (p[0] << 8) | p[1];
  111. if (p + 2 + k > data + bytes || dst + k > hevc->data + sizeof(hevc->data))
  112. {
  113. assert(0);
  114. return -1;
  115. }
  116. assert((nalutype & 0x3F) == ((p[2] >> 1) & 0x3F));
  117. hevc->nalu[hevc->numOfArrays].array_completeness = (nalutype >> 7) & 0x01;
  118. hevc->nalu[hevc->numOfArrays].type = nalutype & 0x3F;
  119. hevc->nalu[hevc->numOfArrays].bytes = k;
  120. hevc->nalu[hevc->numOfArrays].data = dst;
  121. memcpy(hevc->nalu[hevc->numOfArrays].data, p + 2, k);
  122. hevc->numOfArrays++;
  123. p += 2 + k;
  124. dst += k;
  125. }
  126. }
  127. hevc->off = (int)(dst - hevc->data);
  128. return (int)(p - data);
  129. }
  130. int mpeg4_hevc_decoder_configuration_record_save(const struct mpeg4_hevc_t* hevc, uint8_t* data, size_t bytes)
  131. {
  132. uint16_t n;
  133. uint8_t i, j, k;
  134. uint8_t *ptr, *end;
  135. uint8_t *p = data;
  136. uint8_t array_completeness = 1;
  137. const uint8_t nalu[] = {H265_VPS, H265_SPS, H265_PPS, H265_PREFIX_SEI, H265_SUFFIX_SEI};
  138. assert(hevc->lengthSizeMinusOne <= 3);
  139. end = data + bytes;
  140. if (bytes < 23)
  141. return 0; // don't have enough memory
  142. // HEVCDecoderConfigurationRecord
  143. // ISO/IEC 14496-15:2017
  144. // 8.3.3.1.2 Syntax
  145. assert(1 == hevc->configurationVersion);
  146. data[0] = hevc->configurationVersion;
  147. // general_profile_space + general_tier_flag + general_profile_idc
  148. data[1] = ((hevc->general_profile_space & 0x03) << 6) | ((hevc->general_tier_flag & 0x01) << 5) | (hevc->general_profile_idc & 0x1F);
  149. // general_profile_compatibility_flags
  150. w32(data + 2, hevc->general_profile_compatibility_flags);
  151. // general_constraint_indicator_flags
  152. w32(data + 6, (uint32_t)(hevc->general_constraint_indicator_flags >> 16));
  153. w16(data + 10, (uint16_t)hevc->general_constraint_indicator_flags);
  154. // general_level_idc
  155. data[12] = hevc->general_level_idc;
  156. // min_spatial_segmentation_idc
  157. w16(data + 13, 0xF000 | hevc->min_spatial_segmentation_idc);
  158. data[15] = 0xFC | hevc->parallelismType;
  159. data[16] = 0xFC | hevc->chromaFormat;
  160. data[17] = 0xF8 | hevc->bitDepthLumaMinus8;
  161. data[18] = 0xF8 | hevc->bitDepthChromaMinus8;
  162. w16(data + 19, hevc->avgFrameRate);
  163. data[21] = (hevc->constantFrameRate << 6) | ((hevc->numTemporalLayers & 0x07) << 3) | ((hevc->temporalIdNested & 0x01) << 2) | (hevc->lengthSizeMinusOne & 0x03);
  164. // data[22] = hevc->numOfArrays;
  165. p = data + 23;
  166. for (k = i = 0; i < sizeof(nalu)/sizeof(nalu[0]); i++)
  167. {
  168. ptr = p + 3;
  169. for (n = j = 0; j < hevc->numOfArrays; j++)
  170. {
  171. assert(hevc->nalu[j].type == ((hevc->nalu[j].data[0] >> 1) & 0x3F));
  172. if(nalu[i] != hevc->nalu[j].type)
  173. continue;
  174. if (ptr + 2 + hevc->nalu[j].bytes > end)
  175. return 0; // don't have enough memory
  176. array_completeness = hevc->nalu[j].array_completeness;
  177. assert(hevc->nalu[i].data + hevc->nalu[j].bytes <= hevc->data + sizeof(hevc->data));
  178. w16(ptr, hevc->nalu[j].bytes);
  179. memcpy(ptr + 2, hevc->nalu[j].data, hevc->nalu[j].bytes);
  180. ptr += 2 + hevc->nalu[j].bytes;
  181. n++;
  182. }
  183. if (n > 0)
  184. {
  185. // array_completeness + NAL_unit_type
  186. p[0] = (array_completeness << 7) | (nalu[i] & 0x3F);
  187. w16(p + 1, n);
  188. p = ptr;
  189. k++;
  190. }
  191. }
  192. data[22] = k;
  193. return (int)(p - data);
  194. }
  195. int mpeg4_hevc_to_nalu(const struct mpeg4_hevc_t* hevc, uint8_t* data, size_t bytes)
  196. {
  197. uint8_t i;
  198. uint8_t* p, *end;
  199. const uint8_t startcode[] = { 0, 0, 0, 1 };
  200. p = data;
  201. end = p + bytes;
  202. for (i = 0; i < hevc->numOfArrays; i++)
  203. {
  204. if (p + hevc->nalu[i].bytes + 4 > end)
  205. return -1;
  206. memcpy(p, startcode, 4);
  207. memcpy(p + 4, hevc->nalu[i].data, hevc->nalu[i].bytes);
  208. assert(hevc->nalu[i].type == ((hevc->nalu[i].data[0] >> 1) & 0x3F));
  209. p += 4 + hevc->nalu[i].bytes;
  210. }
  211. return (int)(p - data);
  212. }
  213. int mpeg4_hevc_codecs(const struct mpeg4_hevc_t* hevc, char* codecs, size_t bytes)
  214. {
  215. // ISO/IEC 14496-15:2017(E)
  216. // Annex E Sub-parameters of the MIME type "codecs" parameter (p154)
  217. // 'hev1.' or 'hvc1.' prefix (5 chars)
  218. // profile, e.g. '.A12' (max 4 chars)
  219. // profile_compatibility reserve bit order, dot + 32-bit hex number (max 9 chars)
  220. // tier and level, e.g. '.H120' (max 5 chars)
  221. // up to 6 constraint bytes, bytes are dot-separated and hex-encoded.
  222. const char* tier = "LH";
  223. const char* space[] = { "", "A", "B", "C" };
  224. uint32_t x;
  225. x = hevc->general_profile_compatibility_flags;
  226. x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1);
  227. x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2);
  228. x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4);
  229. x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8);
  230. x = (x >> 16) | (x << 16);
  231. return snprintf(codecs, bytes, "hvc1.%s%u.%x.%c%u", space[hevc->general_profile_space%4], (unsigned int)hevc->general_profile_idc, (unsigned int)x, tier[hevc->general_tier_flag%2], (unsigned int)hevc->general_level_idc);
  232. }
  233. #if defined(_DEBUG) || defined(DEBUG)
  234. void hevc_annexbtomp4_test(void);
  235. void mpeg4_hevc_test(void)
  236. {
  237. const unsigned char src[] = {
  238. 0x01,0x01,0x60,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0xb4,0xf0,0x00,
  239. 0xfc,0xfd,0xf8,0xf8,0x00,0x00,0x0f,0x03,0xa0,0x00,0x01,0x00,0x18,0x40,0x01,
  240. 0x0c,0x01,0xff,0xff,0x01,0x60,0x00,0x00,0x03,0x00,0x80,0x00,0x00,0x03,0x00,
  241. 0x00,0x03,0x00,0xb4,0x9d,0xc0,0x90,0xa1,0x00,0x01,0x00,0x29,0x42,0x01,0x01,
  242. 0x01,0x60,0x00,0x00,0x03,0x00,0x80,0x00,0x00,0x03,0x00,0x00,0x03,0x00,0xb4,
  243. 0xa0,0x01,0xe0,0x20,0x02,0x1c,0x59,0x67,0x79,0x24,0x6d,0xae,0x01,0x00,0x00,
  244. 0x03,0x03,0xe8,0x00,0x00,0x5d,0xc0,0x08,0xa2,0x00,0x01,0x00,0x06,0x44,0x01,
  245. 0xc1,0x73,0xd1,0x89
  246. };
  247. const unsigned char nalu[] = {
  248. 0x00,0x00,0x00,0x01,0x40,0x01,0x0c,0x01,0xff,0xff,0x01,0x60,0x00,0x00,0x03,
  249. 0x00,0x80,0x00,0x00,0x03,0x00,0x00,0x03,0x00,0xb4,0x9d,0xc0,0x90,0x00,0x00,
  250. 0x00,0x01,0x42,0x01,0x01,0x01,0x60,0x00,0x00,0x03,0x00,0x80,0x00,0x00,0x03,
  251. 0x00,0x00,0x03,0x00,0xb4,0xa0,0x01,0xe0,0x20,0x02,0x1c,0x59,0x67,0x79,0x24,
  252. 0x6d,0xae,0x01,0x00,0x00,0x03,0x03,0xe8,0x00,0x00,0x5d,0xc0,0x08,0x00,0x00,
  253. 0x00,0x01,0x44,0x01,0xc1,0x73,0xd1,0x89
  254. };
  255. unsigned char data[sizeof(src)];
  256. struct mpeg4_hevc_t hevc;
  257. assert(sizeof(src) == mpeg4_hevc_decoder_configuration_record_load(src, sizeof(src), &hevc));
  258. assert(0 == hevc.general_profile_space && 0 == hevc.general_tier_flag);
  259. assert(1 == hevc.general_profile_idc && 0xb4 == hevc.general_level_idc);
  260. assert(1 == hevc.numTemporalLayers && 1 == hevc.temporalIdNested);
  261. assert(3 == hevc.numOfArrays);
  262. assert(sizeof(src) == mpeg4_hevc_decoder_configuration_record_save(&hevc, data, sizeof(data)));
  263. assert(0 == memcmp(src, data, sizeof(src)));
  264. mpeg4_hevc_codecs(&hevc, (char*)data, sizeof(data));
  265. assert(0 == memcmp("hvc1.1.6.L180", data, 13));
  266. assert(sizeof(nalu) == mpeg4_hevc_to_nalu(&hevc, data, sizeof(data)));
  267. assert(0 == memcmp(nalu, data, sizeof(nalu)));
  268. hevc_annexbtomp4_test();
  269. }
  270. #endif