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.

464 lines
14KB

  1. #include "mpeg4-hevc.h"
  2. #include "mpeg4-avc.h"
  3. #include <string.h>
  4. #include <assert.h>
  5. #define H265_NAL_VPS 32
  6. #define H265_NAL_SPS 33
  7. #define H265_NAL_PPS 34
  8. #define H265_NAL_AUD 35
  9. #define H265_NAL_SEI_PREFIX 39
  10. #define H265_NAL_SEI_SUFFIX 40
  11. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  12. #define BIT(ptr, off) (((ptr)[(off) / 8] >> (7 - ((off) % 8))) & 0x01)
  13. struct h265_annexbtomp4_handle_t
  14. {
  15. struct mpeg4_hevc_t* hevc;
  16. int errcode;
  17. int* update; // avc sps/pps update flags
  18. int* vcl;
  19. uint8_t* out;
  20. size_t bytes;
  21. size_t capacity;
  22. };
  23. uint8_t mpeg4_h264_read_ue(const uint8_t* data, size_t bytes, size_t* offset);
  24. static size_t hevc_rbsp_decode(const uint8_t* nalu, size_t bytes, uint8_t* sodb, size_t len)
  25. {
  26. size_t i, j;
  27. const size_t max_sps_luma_bit_depth_offset = 256;
  28. for (j = i = 0; i < bytes && j < len && i < max_sps_luma_bit_depth_offset; i++)
  29. {
  30. if (i + 2 < bytes && 0 == nalu[i] && 0 == nalu[i + 1] && 0x03 == nalu[i + 2])
  31. {
  32. sodb[j++] = nalu[i];
  33. sodb[j++] = nalu[i + 1];
  34. i += 2;
  35. }
  36. else
  37. {
  38. sodb[j++] = nalu[i];
  39. }
  40. }
  41. return j;
  42. }
  43. static int hevc_profile_tier_level(const uint8_t* nalu, size_t bytes, uint8_t maxNumSubLayersMinus1, struct mpeg4_hevc_t* hevc)
  44. {
  45. size_t n;
  46. uint8_t i;
  47. uint8_t sub_layer_profile_present_flag[8];
  48. uint8_t sub_layer_level_present_flag[8];
  49. if (bytes < 12)
  50. return -1;
  51. hevc->general_profile_space = (nalu[0] >> 6) & 0x03;
  52. hevc->general_tier_flag = (nalu[0] >> 5) & 0x01;
  53. hevc->general_profile_idc = nalu[0] & 0x1f;
  54. hevc->general_profile_compatibility_flags = 0;
  55. hevc->general_profile_compatibility_flags |= nalu[1] << 24;
  56. hevc->general_profile_compatibility_flags |= nalu[2] << 16;
  57. hevc->general_profile_compatibility_flags |= nalu[3] << 8;
  58. hevc->general_profile_compatibility_flags |= nalu[4];
  59. hevc->general_constraint_indicator_flags = 0;
  60. hevc->general_constraint_indicator_flags |= ((uint64_t)nalu[5]) << 40;
  61. hevc->general_constraint_indicator_flags |= ((uint64_t)nalu[6]) << 32;
  62. hevc->general_constraint_indicator_flags |= ((uint64_t)nalu[7]) << 24;
  63. hevc->general_constraint_indicator_flags |= ((uint64_t)nalu[8]) << 16;
  64. hevc->general_constraint_indicator_flags |= ((uint64_t)nalu[9]) << 8;
  65. hevc->general_constraint_indicator_flags |= nalu[10];
  66. hevc->general_level_idc = nalu[11];
  67. if (maxNumSubLayersMinus1 < 1)
  68. return 12;
  69. if (bytes < 14)
  70. return -1; // error
  71. for (i = 0; i < maxNumSubLayersMinus1; i++)
  72. {
  73. sub_layer_profile_present_flag[i] = BIT(nalu, 12 * 8 + i * 2);
  74. sub_layer_level_present_flag[i] = BIT(nalu, 12 * 8 + i * 2 + 1);
  75. }
  76. n = 12 + 2;
  77. for (i = 0; i < maxNumSubLayersMinus1; i++)
  78. {
  79. if(sub_layer_profile_present_flag[i])
  80. n += 11;
  81. if (sub_layer_level_present_flag[i])
  82. n += 1;
  83. }
  84. return bytes >= n ? (int)n : -1;
  85. }
  86. static uint8_t hevc_vps_id(const uint8_t* rbsp, size_t bytes, struct mpeg4_hevc_t* hevc, uint8_t* ptr, size_t len)
  87. {
  88. size_t sodb;
  89. uint8_t vps;
  90. uint8_t vps_max_sub_layers_minus1;
  91. uint8_t vps_temporal_id_nesting_flag;
  92. sodb = hevc_rbsp_decode(rbsp, bytes, ptr, len);
  93. if (sodb < 16 + 2)
  94. return 0xFF;
  95. vps = ptr[2] >> 4; // 2-nalu type
  96. vps_max_sub_layers_minus1 = (ptr[3] >> 1) & 0x07;
  97. vps_temporal_id_nesting_flag = ptr[3] & 0x01;
  98. hevc->numTemporalLayers = MAX(hevc->numTemporalLayers, vps_max_sub_layers_minus1 + 1);
  99. hevc->temporalIdNested = (hevc->temporalIdNested || vps_temporal_id_nesting_flag) ? 1 : 0;
  100. hevc_profile_tier_level(ptr + 6, sodb - 6, vps_max_sub_layers_minus1, hevc);
  101. return vps;
  102. }
  103. static uint8_t hevc_sps_id(const uint8_t* rbsp, size_t bytes, struct mpeg4_hevc_t* hevc, uint8_t* ptr, size_t len, uint8_t* vps)
  104. {
  105. size_t n;
  106. size_t sodb;
  107. uint8_t sps;
  108. uint8_t sps_max_sub_layers_minus1;
  109. uint8_t sps_temporal_id_nesting_flag;
  110. uint8_t conformance_window_flag;
  111. sodb = hevc_rbsp_decode(rbsp, bytes, ptr, len);
  112. if (sodb < 12+3)
  113. return 0xFF;
  114. *vps = ptr[2] >> 4; // 2-nalu type
  115. sps_max_sub_layers_minus1 = (ptr[2] >> 1) & 0x07;
  116. sps_temporal_id_nesting_flag = ptr[2] & 0x01;
  117. n = hevc_profile_tier_level(ptr + 3, sodb - 3, sps_max_sub_layers_minus1, hevc);
  118. if (n <= 0)
  119. return 0xFF;
  120. n = (n + 3) * 8;
  121. sps = mpeg4_h264_read_ue(ptr, sodb, &n);
  122. hevc->chromaFormat = mpeg4_h264_read_ue(ptr, sodb, &n);
  123. if (3 == hevc->chromaFormat)
  124. n++;
  125. mpeg4_h264_read_ue(ptr, sodb, &n); // pic_width_in_luma_samples
  126. mpeg4_h264_read_ue(ptr, sodb, &n); // pic_height_in_luma_samples
  127. conformance_window_flag = BIT(ptr, n); n++; // conformance_window_flag
  128. if (conformance_window_flag)
  129. {
  130. mpeg4_h264_read_ue(ptr, sodb, &n); // conf_win_left_offset
  131. mpeg4_h264_read_ue(ptr, sodb, &n); // conf_win_right_offset
  132. mpeg4_h264_read_ue(ptr, sodb, &n); // conf_win_top_offset
  133. mpeg4_h264_read_ue(ptr, sodb, &n); // conf_win_bottom_offset
  134. }
  135. hevc->bitDepthLumaMinus8 = mpeg4_h264_read_ue(ptr, sodb, &n);
  136. hevc->bitDepthChromaMinus8 = mpeg4_h264_read_ue(ptr, sodb, &n);
  137. // TODO: vui_parameters
  138. //mp4->hevc->min_spatial_segmentation_idc; // min_spatial_segmentation_idc
  139. return sps;
  140. }
  141. static uint8_t hevc_pps_id(const uint8_t* rbsp, size_t bytes, struct mpeg4_hevc_t* hevc, uint8_t* ptr, size_t len, uint8_t* sps)
  142. {
  143. size_t sodb;
  144. size_t offset = 2 * 8; // 2-nalu type
  145. sodb = hevc_rbsp_decode(rbsp, bytes, ptr, len);
  146. if (sodb < 3)
  147. return 0xFF; (void)hevc;
  148. *sps = mpeg4_h264_read_ue(ptr, sodb, &offset);
  149. return mpeg4_h264_read_ue(ptr, sodb, &offset);
  150. }
  151. static void mpeg4_hevc_remove(struct mpeg4_hevc_t* hevc, uint8_t* ptr, size_t bytes, const uint8_t* end)
  152. {
  153. uint8_t i;
  154. assert(ptr >= hevc->data && ptr + bytes <= end && end <= hevc->data + sizeof(hevc->data));
  155. memmove(ptr, ptr + bytes, end - ptr - bytes);
  156. for (i = 0; i < hevc->numOfArrays; i++)
  157. {
  158. if (hevc->nalu[i].data > ptr)
  159. hevc->nalu[i].data -= bytes;
  160. }
  161. }
  162. static int mpeg4_hevc_update2(struct mpeg4_hevc_t* hevc, int i, const uint8_t* nalu, size_t bytes)
  163. {
  164. if (bytes == hevc->nalu[i].bytes && 0 == memcmp(nalu, hevc->nalu[i].data, bytes))
  165. return 0; // do nothing
  166. if (bytes > hevc->nalu[i].bytes && hevc->off + (bytes - hevc->nalu[i].bytes) > sizeof(hevc->data))
  167. {
  168. assert(0);
  169. return -1; // too big
  170. }
  171. mpeg4_hevc_remove(hevc, hevc->nalu[i].data, hevc->nalu[i].bytes, hevc->data + hevc->off);
  172. hevc->off -= hevc->nalu[i].bytes;
  173. hevc->nalu[i].data = hevc->data + hevc->off;
  174. hevc->nalu[i].bytes = (uint16_t)bytes;
  175. memcpy(hevc->nalu[i].data, nalu, bytes);
  176. hevc->off += bytes;
  177. return 1;
  178. }
  179. static int mpeg4_hevc_add(struct mpeg4_hevc_t* hevc, uint8_t type, const uint8_t* nalu, size_t bytes)
  180. {
  181. // copy new
  182. assert(hevc->numOfArrays < sizeof(hevc->nalu) / sizeof(hevc->nalu[0]));
  183. if (hevc->numOfArrays >= sizeof(hevc->nalu) / sizeof(hevc->nalu[0])
  184. || hevc->off + bytes > sizeof(hevc->data))
  185. {
  186. assert(0);
  187. return -1;
  188. }
  189. hevc->nalu[hevc->numOfArrays].type = type;
  190. hevc->nalu[hevc->numOfArrays].bytes = (uint16_t)bytes;
  191. hevc->nalu[hevc->numOfArrays].array_completeness = 1;
  192. hevc->nalu[hevc->numOfArrays].data = hevc->data + hevc->off;
  193. memcpy(hevc->nalu[hevc->numOfArrays].data, nalu, bytes);
  194. hevc->off += bytes;
  195. ++hevc->numOfArrays;
  196. return 1;
  197. }
  198. static int h265_vps_copy(struct mpeg4_hevc_t* hevc, const uint8_t* nalu, size_t bytes)
  199. {
  200. int i;
  201. uint8_t vpsid;
  202. if (bytes < 3)
  203. {
  204. assert(0);
  205. return -1; // invalid length
  206. }
  207. vpsid = hevc_vps_id(nalu, bytes, hevc, hevc->data + hevc->off, sizeof(hevc->data)-hevc->off);
  208. for (i = 0; i < hevc->numOfArrays; i++)
  209. {
  210. if (H265_NAL_VPS == hevc->nalu[i].type && vpsid == hevc_vps_id(hevc->nalu[i].data, hevc->nalu[i].bytes, hevc, hevc->data + hevc->off, sizeof(hevc->data) - hevc->off))
  211. return mpeg4_hevc_update2(hevc, i, nalu, bytes);
  212. }
  213. return mpeg4_hevc_add(hevc, H265_NAL_VPS, nalu, bytes);
  214. }
  215. static int h265_sps_copy(struct mpeg4_hevc_t* hevc, const uint8_t* nalu, size_t bytes)
  216. {
  217. int i;
  218. uint8_t spsid;
  219. uint8_t vpsid, vpsid2;
  220. if (bytes < 13 + 2)
  221. {
  222. assert(0);
  223. return -1; // invalid length
  224. }
  225. spsid = hevc_sps_id(nalu, bytes, hevc, hevc->data + hevc->off, sizeof(hevc->data) - hevc->off, &vpsid);
  226. for (i = 0; i < hevc->numOfArrays; i++)
  227. {
  228. if (H265_NAL_SPS == hevc->nalu[i].type && spsid == hevc_sps_id(hevc->nalu[i].data, hevc->nalu[i].bytes, hevc, hevc->data + hevc->off, sizeof(hevc->data) - hevc->off, &vpsid2) && vpsid == vpsid2)
  229. return mpeg4_hevc_update2(hevc, i, nalu, bytes);
  230. }
  231. return mpeg4_hevc_add(hevc, H265_NAL_SPS, nalu, bytes);
  232. }
  233. static int h265_pps_copy(struct mpeg4_hevc_t* hevc, const uint8_t* nalu, size_t bytes)
  234. {
  235. int i;
  236. uint8_t ppsid;
  237. uint8_t spsid, spsid2;
  238. if (bytes < 1 + 2)
  239. {
  240. assert(0);
  241. return -1; // invalid length
  242. }
  243. ppsid = hevc_pps_id(nalu, bytes, hevc, hevc->data + hevc->off, sizeof(hevc->data) - hevc->off, &spsid);
  244. for (i = 0; i < hevc->numOfArrays; i++)
  245. {
  246. if (H265_NAL_PPS == hevc->nalu[i].type && ppsid == hevc_pps_id(hevc->nalu[i].data, hevc->nalu[i].bytes, hevc, hevc->data + hevc->off, sizeof(hevc->data) - hevc->off, &spsid2) && spsid == spsid2)
  247. return mpeg4_hevc_update2(hevc, i, nalu, bytes);
  248. }
  249. return mpeg4_hevc_add(hevc, H265_NAL_PPS, nalu, bytes);
  250. }
  251. static int h265_sei_clear(struct mpeg4_hevc_t* hevc)
  252. {
  253. int i;
  254. for (i = 0; i < hevc->numOfArrays; i++)
  255. {
  256. if (H265_NAL_SEI_PREFIX == hevc->nalu[i].type || H265_NAL_SEI_SUFFIX == hevc->nalu[i].type)
  257. {
  258. mpeg4_hevc_remove(hevc, hevc->nalu[i].data, hevc->nalu[i].bytes, hevc->data + hevc->off);
  259. hevc->off -= hevc->nalu[i].bytes;
  260. if(i + 1 < hevc->numOfArrays)
  261. memmove(hevc->nalu + i, hevc->nalu + i + 1, sizeof(hevc->nalu[0]) * (hevc->numOfArrays - i - 1));
  262. --hevc->numOfArrays;
  263. --i;
  264. }
  265. }
  266. return 0;
  267. }
  268. int mpeg4_hevc_update(struct mpeg4_hevc_t* hevc, const uint8_t* nalu, size_t bytes)
  269. {
  270. int r;
  271. switch ((nalu[0] >> 1) & 0x3f)
  272. {
  273. case H265_NAL_VPS:
  274. h265_sei_clear(hevc); // remove all prefix/suffix sei
  275. r = h265_vps_copy(hevc, nalu, bytes);
  276. break;
  277. case H265_NAL_SPS:
  278. r = h265_sps_copy(hevc, nalu, bytes);
  279. break;
  280. case H265_NAL_PPS:
  281. r = h265_pps_copy(hevc, nalu, bytes);
  282. break;
  283. #if defined(H265_FILTER_SEI)
  284. case H265_NAL_SEI_PREFIX:
  285. r = mpeg4_hevc_add(hevc, H265_NAL_SEI_PREFIX, nalu, bytes);
  286. break;
  287. case H265_NAL_SEI_SUFFIX:
  288. r = mpeg4_hevc_add(hevc, H265_NAL_SEI_SUFFIX, nalu, bytes);
  289. break;
  290. #endif
  291. default:
  292. r = 0;
  293. break;
  294. }
  295. return r;
  296. }
  297. static void hevc_handler(void* param, const uint8_t* nalu, size_t bytes)
  298. {
  299. int r;
  300. uint8_t nalutype;
  301. struct h265_annexbtomp4_handle_t* mp4;
  302. mp4 = (struct h265_annexbtomp4_handle_t*)param;
  303. nalutype = (nalu[0] >> 1) & 0x3f;
  304. #if defined(H2645_FILTER_AUD)
  305. if(H265_NAL_AUD == nalutype)
  306. return; // ignore AUD
  307. #endif
  308. r = mpeg4_hevc_update(mp4->hevc, nalu, bytes);
  309. if (1 == r && mp4->update)
  310. *mp4->update = 1;
  311. else if (r < 0)
  312. mp4->errcode = r;
  313. // IRAP-1, B/P-2, other-0
  314. if (mp4->vcl && nalutype < H265_NAL_VPS)
  315. *mp4->vcl = 16<=nalutype && nalutype<=23 ? 1 : 2;
  316. if (mp4->capacity >= mp4->bytes + bytes + 4)
  317. {
  318. mp4->out[mp4->bytes + 0] = (uint8_t)((bytes >> 24) & 0xFF);
  319. mp4->out[mp4->bytes + 1] = (uint8_t)((bytes >> 16) & 0xFF);
  320. mp4->out[mp4->bytes + 2] = (uint8_t)((bytes >> 8) & 0xFF);
  321. mp4->out[mp4->bytes + 3] = (uint8_t)((bytes >> 0) & 0xFF);
  322. memmove(mp4->out + mp4->bytes + 4, nalu, bytes);
  323. mp4->bytes += bytes + 4;
  324. }
  325. else
  326. {
  327. mp4->errcode = -1;
  328. }
  329. }
  330. int h265_annexbtomp4(struct mpeg4_hevc_t* hevc, const void* data, size_t bytes, void* out, size_t size, int *vcl, int* update)
  331. {
  332. struct h265_annexbtomp4_handle_t h;
  333. memset(&h, 0, sizeof(h));
  334. h.hevc = hevc;
  335. h.vcl = vcl;
  336. h.update = update;
  337. h.out = (uint8_t*)out;
  338. h.capacity = size;
  339. if (vcl) *vcl = 0;
  340. if (update) *update = 0;
  341. // hevc->numTemporalLayers = 0;
  342. // hevc->temporalIdNested = 0;
  343. // hevc->min_spatial_segmentation_idc = 0;
  344. // hevc->general_profile_compatibility_flags = 0xffffffff;
  345. // hevc->general_constraint_indicator_flags = 0xffffffffffULL;
  346. // hevc->chromaFormat = 1; // 4:2:0
  347. mpeg4_h264_annexb_nalu((const uint8_t*)data, bytes, hevc_handler, &h);
  348. hevc->configurationVersion = 1;
  349. hevc->lengthSizeMinusOne = 3; // 4 bytes
  350. return 0 == h.errcode ? (int)h.bytes : 0;
  351. }
  352. int h265_is_new_access_unit(const uint8_t* nalu, size_t bytes)
  353. {
  354. enum { NAL_VPS = 32, NAL_SPS = 33, NAL_PPS = 34, NAL_AUD = 35, NAL_PREFIX_SEI = 39, };
  355. uint8_t nal_type;
  356. uint8_t nuh_layer_id;
  357. if(bytes < 3)
  358. return 0;
  359. nal_type = (nalu[0] >> 1) & 0x3f;
  360. nuh_layer_id = ((nalu[0] & 0x01) << 5) | ((nalu[1] >> 3) &0x1F);
  361. // 7.4.2.4.4 Order of NAL units and coded pictures and their association to access units
  362. if(NAL_VPS == nal_type || NAL_SPS == nal_type || NAL_PPS == nal_type ||
  363. (nuh_layer_id == 0 && (NAL_AUD == nal_type || NAL_PREFIX_SEI == nal_type || (41 <= nal_type && nal_type <= 44) || (48 <= nal_type && nal_type <= 55))))
  364. return 1;
  365. // 7.4.2.4.5 Order of VCL NAL units and association to coded pictures
  366. if (nal_type <= 31)
  367. {
  368. //first_slice_segment_in_pic_flag 0x80
  369. return (nalu[2] & 0x80) ? 1 : 0;
  370. }
  371. return 0;
  372. }
  373. #if defined(_DEBUG) || defined(DEBUG)
  374. void hevc_annexbtomp4_test(void)
  375. {
  376. const uint8_t vps[] = { 0x40, 0x01, 0x0c, 0x01, 0xff, 0xff, 0x01, 0x60, 0x00, 0x00, 0x03, 0x00, 0x80, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x78, 0x9d, 0xc0, 0x90 };
  377. const uint8_t sps[] = { 0x42, 0x01, 0x01, 0x01, 0x60, 0x00, 0x00, 0x03, 0x00, 0x80, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x78, 0xa0, 0x03, 0xc0, 0x80, 0x32, 0x16, 0x59, 0xde, 0x49, 0x1b, 0x6b, 0x80, 0x40, 0x00, 0x00, 0xfa, 0x00, 0x00, 0x17, 0x70, 0x02 };
  378. const uint8_t pps[] = { 0x44, 0x01, 0xc1, 0x73, 0xd1, 0x89 };
  379. const uint8_t annexb[] = { 0x00, 0x00, 0x00, 0x01, 0x4e, 0x01, 0x06, 0x01, 0xd0, 0x80, 0x00, 0x00, 0x00, 0x01, 0x40, 0x01, 0x0c, 0x01, 0xff, 0xff, 0x01, 0x60, 0x00, 0x00, 0x03, 0x00, 0x80, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x78, 0x9d, 0xc0, 0x90, 0x00, 0x00, 0x00, 0x01, 0x42, 0x01, 0x01, 0x01, 0x60, 0x00, 0x00, 0x03, 0x00, 0x80, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x78, 0xa0, 0x03, 0xc0, 0x80, 0x32, 0x16, 0x59, 0xde, 0x49, 0x1b, 0x6b, 0x80, 0x40, 0x00, 0x00, 0xfa, 0x00, 0x00, 0x17, 0x70, 0x02, 0x00, 0x00, 0x00, 0x01, 0x44, 0x01, 0xc1, 0x73, 0xd1, 0x89 };
  380. uint8_t output[512];
  381. int vcl, update;
  382. struct mpeg4_hevc_t hevc;
  383. memset(&hevc, 0, sizeof(hevc));
  384. assert(h265_annexbtomp4(&hevc, annexb, sizeof(annexb), output, sizeof(output), &vcl, &update) > 0);
  385. assert(3 == hevc.numOfArrays && vcl == 0 && update == 1);
  386. assert(hevc.nalu[0].bytes == sizeof(vps) && 0 == memcmp(hevc.nalu[0].data, vps, sizeof(vps)));
  387. assert(hevc.nalu[1].bytes == sizeof(sps) && 0 == memcmp(hevc.nalu[1].data, sps, sizeof(sps)));
  388. assert(hevc.nalu[2].bytes == sizeof(pps) && 0 == memcmp(hevc.nalu[2].data, pps, sizeof(pps)));
  389. }
  390. #endif