Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

109 lines
2.9KB

  1. #include "mpeg-types.h"
  2. #include "mpeg-util.h"
  3. #include <assert.h>
  4. #include <string.h>
  5. #define H265_NAL_AUD 35
  6. /// @param[out] leading optional leading zero bytes
  7. /// @return -1-not found, other-AUD position(include start code)
  8. static int mpeg_h265_find_access_unit_delimiter(const uint8_t* p, size_t bytes, size_t* leading)
  9. {
  10. size_t i, zeros;
  11. for (zeros = i = 0; i + 1 < bytes; i++)
  12. {
  13. if (0x01 == p[i] && zeros >= 2 && H265_NAL_AUD == ((p[i + 1] >> 1) & 0x3f))
  14. {
  15. assert(i >= zeros);
  16. if (leading)
  17. *leading = (zeros > 2 ? 3 : 2) + 1; // zeros - (zeros > 2 ? 3 : 2);
  18. return (int)(i - (zeros > 2 ? 3 : 2));
  19. }
  20. zeros = 0x00 != p[i] ? 0 : (zeros + 1);
  21. }
  22. return -1;
  23. }
  24. // Rec. ITU-T H.265 v4 (12/2016) (p26)
  25. // intra random access point (IRAP) picture:
  26. // A coded picture for which each VCL NAL unit has nal_unit_type
  27. // in the range of BLA_W_LP to RSV_IRAP_VCL23, inclusive.
  28. static int mpeg_h265_find_keyframe(const uint8_t* p, size_t bytes)
  29. {
  30. size_t i;
  31. uint8_t type;
  32. for (i = 2; i + 1 < bytes; i++)
  33. {
  34. if (0x01 == p[i] && 0x00 == p[i - 1] && 0x00 == p[i - 2])
  35. {
  36. type = (p[i + 1] >> 1) & 0x3f;
  37. if (type < 32)
  38. return (16 <= type && type <= 23) ? 1 : 0;
  39. }
  40. }
  41. return 0;
  42. }
  43. static int mpeg_h265_is_new_access_unit(const uint8_t* nalu, size_t bytes)
  44. {
  45. enum { NAL_VPS = 32, NAL_SPS = 33, NAL_PPS = 34, NAL_AUD = 35, NAL_PREFIX_SEI = 39, };
  46. uint8_t nal_type;
  47. uint8_t nuh_layer_id;
  48. if(bytes < 3)
  49. return 0;
  50. nal_type = (nalu[0] >> 1) & 0x3f;
  51. nuh_layer_id = ((nalu[0] & 0x01) << 5) | ((nalu[1] >> 3) &0x1F);
  52. // 7.4.2.4.4 Order of NAL units and coded pictures and their association to access units
  53. if(NAL_VPS == nal_type || NAL_SPS == nal_type || NAL_PPS == nal_type ||
  54. (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))))
  55. return 1;
  56. // 7.4.2.4.5 Order of VCL NAL units and association to coded pictures
  57. if (nal_type <= 31)
  58. {
  59. //first_slice_segment_in_pic_flag 0x80
  60. return (nalu[2] & 0x80) ? 1 : 0;
  61. }
  62. return 0;
  63. }
  64. int mpeg_h265_find_new_access_unit(const uint8_t* data, size_t bytes, int* vcl)
  65. {
  66. int n;
  67. size_t leading;
  68. uint8_t nal_type;
  69. const uint8_t* p, *end;
  70. end = data + bytes;
  71. for (p = data; p && p < end; p += n)
  72. {
  73. n = mpeg_h264_find_nalu(p, end - p, &leading);
  74. if (n < 0)
  75. return -1;
  76. nal_type = (p[n] >> 1) & 0x3f;
  77. if (*vcl > 0 && mpeg_h265_is_new_access_unit(p+n, end - p - n))
  78. {
  79. return (int)(p - data + n - leading);
  80. }
  81. else if (nal_type <= 31)
  82. {
  83. *vcl = (16 <= nal_type && nal_type <= 23) ? 1 : 2;
  84. }
  85. else
  86. {
  87. // nothing to do
  88. }
  89. }
  90. return -1;
  91. }