h265.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. package codec
  2. import (
  3. "bytes"
  4. "errors"
  5. "github.com/q191201771/naza/pkg/nazabits"
  6. "m7s.live/engine/v4/util"
  7. )
  8. type H265NALUType byte
  9. func (H265NALUType) Parse(b byte) H265NALUType {
  10. return H265NALUType(b & 0x7E >> 1)
  11. }
  12. func ParseH265NALUType(b byte) H265NALUType {
  13. return H265NALUType(b & 0x7E >> 1)
  14. }
  15. const (
  16. // HEVC_VPS = 0x40
  17. // HEVC_SPS = 0x42
  18. // HEVC_PPS = 0x44
  19. // HEVC_SEI = 0x4E
  20. // HEVC_IDR = 0x26
  21. // HEVC_PSLICE = 0x02
  22. NAL_UNIT_CODED_SLICE_TRAIL_N H265NALUType = iota // 0
  23. NAL_UNIT_CODED_SLICE_TRAIL_R // 1
  24. NAL_UNIT_CODED_SLICE_TSA_N // 2
  25. NAL_UNIT_CODED_SLICE_TLA // 3 // Current name in the spec: TSA_R
  26. NAL_UNIT_CODED_SLICE_STSA_N // 4
  27. NAL_UNIT_CODED_SLICE_STSA_R // 5
  28. NAL_UNIT_CODED_SLICE_RADL_N // 6
  29. NAL_UNIT_CODED_SLICE_DLP // 7 // Current name in the spec: RADL_R
  30. NAL_UNIT_CODED_SLICE_RASL_N // 8
  31. NAL_UNIT_CODED_SLICE_TFD // 9 // Current name in the spec: RASL_R
  32. NAL_UNIT_RESERVED_10
  33. NAL_UNIT_RESERVED_11
  34. NAL_UNIT_RESERVED_12
  35. NAL_UNIT_RESERVED_13
  36. NAL_UNIT_RESERVED_14
  37. NAL_UNIT_RESERVED_15
  38. NAL_UNIT_CODED_SLICE_BLA // 16 // Current name in the spec: BLA_W_LP
  39. NAL_UNIT_CODED_SLICE_BLANT // 17 // Current name in the spec: BLA_W_DLP
  40. NAL_UNIT_CODED_SLICE_BLA_N_LP // 18
  41. NAL_UNIT_CODED_SLICE_IDR // 19// Current name in the spec: IDR_W_DLP
  42. NAL_UNIT_CODED_SLICE_IDR_N_LP // 20
  43. NAL_UNIT_CODED_SLICE_CRA // 21
  44. NAL_UNIT_RESERVED_22
  45. NAL_UNIT_RESERVED_23
  46. NAL_UNIT_RESERVED_24
  47. NAL_UNIT_RESERVED_25
  48. NAL_UNIT_RESERVED_26
  49. NAL_UNIT_RESERVED_27
  50. NAL_UNIT_RESERVED_28
  51. NAL_UNIT_RESERVED_29
  52. NAL_UNIT_RESERVED_30
  53. NAL_UNIT_RESERVED_31
  54. NAL_UNIT_VPS // 32
  55. NAL_UNIT_SPS // 33
  56. NAL_UNIT_PPS // 34
  57. NAL_UNIT_ACCESS_UNIT_DELIMITER // 35
  58. NAL_UNIT_EOS // 36
  59. NAL_UNIT_EOB // 37
  60. NAL_UNIT_FILLER_DATA // 38
  61. NAL_UNIT_SEI // 39 Prefix SEI
  62. NAL_UNIT_SEI_SUFFIX // 40 Suffix SEI
  63. NAL_UNIT_RESERVED_41
  64. NAL_UNIT_RESERVED_42
  65. NAL_UNIT_RESERVED_43
  66. NAL_UNIT_RESERVED_44
  67. NAL_UNIT_RESERVED_45
  68. NAL_UNIT_RESERVED_46
  69. NAL_UNIT_RESERVED_47
  70. NAL_UNIT_RTP_AP
  71. NAL_UNIT_RTP_FU
  72. NAL_UNIT_UNSPECIFIED_50
  73. NAL_UNIT_UNSPECIFIED_51
  74. NAL_UNIT_UNSPECIFIED_52
  75. NAL_UNIT_UNSPECIFIED_53
  76. NAL_UNIT_UNSPECIFIED_54
  77. NAL_UNIT_UNSPECIFIED_55
  78. NAL_UNIT_UNSPECIFIED_56
  79. NAL_UNIT_UNSPECIFIED_57
  80. NAL_UNIT_UNSPECIFIED_58
  81. NAL_UNIT_UNSPECIFIED_59
  82. NAL_UNIT_UNSPECIFIED_60
  83. NAL_UNIT_UNSPECIFIED_61
  84. NAL_UNIT_UNSPECIFIED_62
  85. NAL_UNIT_UNSPECIFIED_63
  86. NAL_UNIT_INVALID
  87. )
  88. var AudNalu = []byte{0x00, 0x00, 0x00, 0x01, 0x46, 0x01, 0x10}
  89. var ErrHevc = errors.New("hevc parse config error")
  90. var FourCC_H265_32 = util.BigEndian.Uint32([]byte{'h', 'v', 'c', '1'})
  91. var FourCC_AV1_32 = util.BigEndian.Uint32([]byte{'a', 'v', '0', '1'})
  92. // HVCC
  93. type HVCDecoderConfigurationRecord struct {
  94. PicWidthInLumaSamples uint32 // sps
  95. PicHeightInLumaSamples uint32 // sps
  96. configurationVersion uint8
  97. generalProfileSpace uint8
  98. generalTierFlag uint8
  99. generalProfileIdc uint8
  100. generalProfileCompatibilityFlags uint32
  101. generalConstraintIndicatorFlags uint64
  102. generalLevelIdc uint8
  103. lengthSizeMinusOne uint8
  104. numTemporalLayers uint8
  105. temporalIdNested uint8
  106. parallelismType uint8
  107. chromaFormat uint8
  108. bitDepthLumaMinus8 uint8
  109. bitDepthChromaMinus8 uint8
  110. avgFrameRate uint16
  111. }
  112. func ParseVpsSpsPpsFromSeqHeaderWithoutMalloc(payload []byte) (vps, sps, pps []byte, err error) {
  113. if len(payload) < 5 {
  114. return nil, nil, nil, ErrHevc
  115. }
  116. // if payload[0] != 0x1c || payload[1] != 0x00 {
  117. // return nil, nil, nil, ErrHevc
  118. // }
  119. if len(payload) < 33 {
  120. return nil, nil, nil, ErrHevc
  121. }
  122. index := 27
  123. if numOfArrays := payload[index]; numOfArrays != 3 && numOfArrays != 4 {
  124. return nil, nil, nil, ErrHevc
  125. }
  126. index++
  127. if payload[index]&0x7f != byte(NAL_UNIT_VPS) {
  128. return nil, nil, nil, ErrHevc
  129. }
  130. if numNalus := util.ReadBE[int](payload[index+1 : index+3]); numNalus != 1 {
  131. return nil, nil, nil, ErrHevc
  132. }
  133. vpsLen := util.ReadBE[int](payload[index+3 : index+5])
  134. if len(payload) < 33+vpsLen {
  135. return nil, nil, nil, ErrHevc
  136. }
  137. vps = payload[index+5 : index+5+vpsLen]
  138. index += 5 + vpsLen
  139. if len(payload) < 38+vpsLen {
  140. return nil, nil, nil, ErrHevc
  141. }
  142. if payload[index]&0x7f != byte(NAL_UNIT_SPS) {
  143. return nil, nil, nil, ErrHevc
  144. }
  145. if numNalus := util.ReadBE[int](payload[index+1 : index+3]); numNalus != 1 {
  146. return nil, nil, nil, ErrHevc
  147. }
  148. spsLen := util.ReadBE[int](payload[index+3 : index+5])
  149. if len(payload) < 38+vpsLen+spsLen {
  150. return nil, nil, nil, ErrHevc
  151. }
  152. sps = payload[index+5 : index+5+spsLen]
  153. index += 5 + spsLen
  154. if len(payload) < 43+vpsLen+spsLen {
  155. return nil, nil, nil, ErrHevc
  156. }
  157. if payload[index]&0x7f != byte(NAL_UNIT_PPS) {
  158. return nil, nil, nil, ErrHevc
  159. }
  160. // if numNalus := util.ReadBE[int](payload[index+1 : index+3]); numNalus != 1 {
  161. // return nil, nil, nil, ErrHevc
  162. // }
  163. ppsLen := util.ReadBE[int](payload[index+3 : index+5])
  164. if len(payload) < 43+vpsLen+spsLen+ppsLen {
  165. return nil, nil, nil, ErrHevc
  166. }
  167. pps = payload[index+5 : index+5+ppsLen]
  168. return
  169. }
  170. func BuildH265SeqHeaderFromVpsSpsPps(vps, sps, pps []byte) ([]byte, error) {
  171. sh := make([]byte, 43+len(vps)+len(sps)+len(pps))
  172. sh[0] = 0b1001_0000 | byte(PacketTypeSequenceStart)
  173. util.BigEndian.PutUint32(sh[1:], FourCC_H265_32)
  174. // unsigned int(8) configurationVersion = 1;
  175. sh[5] = 0x1
  176. ctx := HVCDecoderConfigurationRecord{
  177. configurationVersion: 1,
  178. lengthSizeMinusOne: 3, // 4 bytes
  179. generalProfileCompatibilityFlags: 0xffffffff,
  180. generalConstraintIndicatorFlags: 0xffffffffffff,
  181. }
  182. if err := ctx.ParseVps(vps); err != nil {
  183. return nil, err
  184. }
  185. if err := ctx.ParseSps(sps); err != nil {
  186. return nil, err
  187. }
  188. // unsigned int(2) general_profile_space;
  189. // unsigned int(1) general_tier_flag;
  190. // unsigned int(5) general_profile_idc;
  191. sh[6] = ctx.generalProfileSpace<<6 | ctx.generalTierFlag<<5 | ctx.generalProfileIdc
  192. // unsigned int(32) general_profile_compatibility_flags
  193. util.PutBE(sh[7:7+4], ctx.generalProfileCompatibilityFlags)
  194. // unsigned int(48) general_constraint_indicator_flags
  195. util.PutBE(sh[11:11+4], uint32(ctx.generalConstraintIndicatorFlags>>16))
  196. util.PutBE(sh[15:15+2], uint16(ctx.generalConstraintIndicatorFlags))
  197. // unsigned int(8) general_level_idc;
  198. sh[17] = ctx.generalLevelIdc
  199. // bit(4) reserved = ‘1111’b;
  200. // unsigned int(12) min_spatial_segmentation_idc;
  201. // bit(6) reserved = ‘111111’b;
  202. // unsigned int(2) parallelismType;
  203. // TODO chef: 这两个字段没有解析
  204. util.PutBE(sh[18:20], 0xf000)
  205. sh[20] = ctx.parallelismType | 0xfc
  206. // bit(6) reserved = ‘111111’b;
  207. // unsigned int(2) chromaFormat;
  208. sh[21] = ctx.chromaFormat | 0xfc
  209. // bit(5) reserved = ‘11111’b;
  210. // unsigned int(3) bitDepthLumaMinus8;
  211. sh[22] = ctx.bitDepthLumaMinus8 | 0xf8
  212. // bit(5) reserved = ‘11111’b;
  213. // unsigned int(3) bitDepthChromaMinus8;
  214. sh[23] = ctx.bitDepthChromaMinus8 | 0xf8
  215. // bit(16) avgFrameRate;
  216. util.PutBE(sh[24:26], ctx.avgFrameRate)
  217. // bit(2) constantFrameRate;
  218. // bit(3) numTemporalLayers;
  219. // bit(1) temporalIdNested;
  220. // unsigned int(2) lengthSizeMinusOne;
  221. sh[26] = 0<<6 | ctx.numTemporalLayers<<3 | ctx.temporalIdNested<<2 | ctx.lengthSizeMinusOne
  222. // num of vps sps pps
  223. sh[27] = 0x03
  224. i := 28
  225. sh[i] = byte(NAL_UNIT_VPS)
  226. // num of vps
  227. util.PutBE(sh[i+1:i+3], 1)
  228. // length
  229. util.PutBE(sh[i+3:i+5], len(vps))
  230. copy(sh[i+5:], vps)
  231. i = i + 5 + len(vps)
  232. sh[i] = byte(NAL_UNIT_SPS)
  233. util.PutBE(sh[i+1:i+3], 1)
  234. util.PutBE(sh[i+3:i+5], len(sps))
  235. copy(sh[i+5:], sps)
  236. i = i + 5 + len(sps)
  237. sh[i] = byte(NAL_UNIT_PPS)
  238. util.PutBE(sh[i+1:i+3], 1)
  239. util.PutBE(sh[i+3:i+5], len(pps))
  240. copy(sh[i+5:], pps)
  241. return sh, nil
  242. }
  243. func (ctx *HVCDecoderConfigurationRecord) ParseVps(vps []byte) error {
  244. if len(vps) < 2 {
  245. return ErrHevc
  246. }
  247. rbsp := nal2rbsp(vps[2:])
  248. br := nazabits.NewBitReader(rbsp)
  249. // skip
  250. // vps_video_parameter_set_id u(4)
  251. // vps_reserved_three_2bits u(2)
  252. // vps_max_layers_minus1 u(6)
  253. if _, err := br.ReadBits16(12); err != nil {
  254. return ErrHevc
  255. }
  256. vpsMaxSubLayersMinus1, err := br.ReadBits8(3)
  257. if err != nil {
  258. return ErrHevc
  259. }
  260. if vpsMaxSubLayersMinus1+1 > ctx.numTemporalLayers {
  261. ctx.numTemporalLayers = vpsMaxSubLayersMinus1 + 1
  262. }
  263. // skip
  264. // vps_temporal_id_nesting_flag u(1)
  265. // vps_reserved_0xffff_16bits u(16)
  266. if _, err := br.ReadBits32(17); err != nil {
  267. return ErrHevc
  268. }
  269. return ctx.parsePtl(&br, vpsMaxSubLayersMinus1)
  270. }
  271. func (ctx *HVCDecoderConfigurationRecord) ParseSps(sps []byte) error {
  272. var err error
  273. if len(sps) < 2 {
  274. return ErrHevc
  275. }
  276. rbsp := nal2rbsp(sps[2:])
  277. br := nazabits.NewBitReader(rbsp)
  278. // sps_video_parameter_set_id
  279. if _, err = br.ReadBits8(4); err != nil {
  280. return err
  281. }
  282. spsMaxSubLayersMinus1, err := br.ReadBits8(3)
  283. if err != nil {
  284. return err
  285. }
  286. if spsMaxSubLayersMinus1+1 > ctx.numTemporalLayers {
  287. ctx.numTemporalLayers = spsMaxSubLayersMinus1 + 1
  288. }
  289. // sps_temporal_id_nesting_flag
  290. if ctx.temporalIdNested, err = br.ReadBit(); err != nil {
  291. return err
  292. }
  293. if err = ctx.parsePtl(&br, spsMaxSubLayersMinus1); err != nil {
  294. return err
  295. }
  296. // sps_seq_parameter_set_id
  297. if _, err = br.ReadGolomb(); err != nil {
  298. return err
  299. }
  300. var cf uint32
  301. if cf, err = br.ReadGolomb(); err != nil {
  302. return err
  303. }
  304. ctx.chromaFormat = uint8(cf)
  305. if ctx.chromaFormat == 3 {
  306. if _, err = br.ReadBit(); err != nil {
  307. return err
  308. }
  309. }
  310. if ctx.PicWidthInLumaSamples, err = br.ReadGolomb(); err != nil {
  311. return err
  312. }
  313. if ctx.PicHeightInLumaSamples, err = br.ReadGolomb(); err != nil {
  314. return err
  315. }
  316. conformanceWindowFlag, err := br.ReadBit()
  317. if err != nil {
  318. return err
  319. }
  320. if conformanceWindowFlag != 0 {
  321. if _, err = br.ReadGolomb(); err != nil {
  322. return err
  323. }
  324. if _, err = br.ReadGolomb(); err != nil {
  325. return err
  326. }
  327. if _, err = br.ReadGolomb(); err != nil {
  328. return err
  329. }
  330. if _, err = br.ReadGolomb(); err != nil {
  331. return err
  332. }
  333. }
  334. var bdlm8 uint32
  335. if bdlm8, err = br.ReadGolomb(); err != nil {
  336. return err
  337. }
  338. ctx.bitDepthLumaMinus8 = uint8(bdlm8)
  339. var bdcm8 uint32
  340. if bdcm8, err = br.ReadGolomb(); err != nil {
  341. return err
  342. }
  343. ctx.bitDepthChromaMinus8 = uint8(bdcm8)
  344. _, err = br.ReadGolomb()
  345. if err != nil {
  346. return err
  347. }
  348. spsSubLayerOrderingInfoPresentFlag, err := br.ReadBit()
  349. if err != nil {
  350. return err
  351. }
  352. var i uint8
  353. if spsSubLayerOrderingInfoPresentFlag != 0 {
  354. i = 0
  355. } else {
  356. i = spsMaxSubLayersMinus1
  357. }
  358. for ; i <= spsMaxSubLayersMinus1; i++ {
  359. if _, err = br.ReadGolomb(); err != nil {
  360. return err
  361. }
  362. if _, err = br.ReadGolomb(); err != nil {
  363. return err
  364. }
  365. if _, err = br.ReadGolomb(); err != nil {
  366. return err
  367. }
  368. }
  369. if _, err = br.ReadGolomb(); err != nil {
  370. return err
  371. }
  372. if _, err = br.ReadGolomb(); err != nil {
  373. return err
  374. }
  375. if _, err = br.ReadGolomb(); err != nil {
  376. return err
  377. }
  378. if _, err = br.ReadGolomb(); err != nil {
  379. return err
  380. }
  381. if _, err = br.ReadGolomb(); err != nil {
  382. return err
  383. }
  384. if _, err = br.ReadGolomb(); err != nil {
  385. return err
  386. }
  387. return nil
  388. }
  389. func (ctx *HVCDecoderConfigurationRecord) parsePtl(br *nazabits.BitReader, maxSubLayersMinus1 uint8) error {
  390. var err error
  391. var ptl HVCDecoderConfigurationRecord
  392. if ptl.generalProfileSpace, err = br.ReadBits8(2); err != nil {
  393. return err
  394. }
  395. if ptl.generalTierFlag, err = br.ReadBit(); err != nil {
  396. return err
  397. }
  398. if ptl.generalProfileIdc, err = br.ReadBits8(5); err != nil {
  399. return err
  400. }
  401. if ptl.generalProfileCompatibilityFlags, err = br.ReadBits32(32); err != nil {
  402. return err
  403. }
  404. if ptl.generalConstraintIndicatorFlags, err = br.ReadBits64(48); err != nil {
  405. return err
  406. }
  407. if ptl.generalLevelIdc, err = br.ReadBits8(8); err != nil {
  408. return err
  409. }
  410. ctx.updatePtl(&ptl)
  411. if maxSubLayersMinus1 == 0 {
  412. return nil
  413. }
  414. subLayerProfilePresentFlag := make([]uint8, maxSubLayersMinus1)
  415. subLayerLevelPresentFlag := make([]uint8, maxSubLayersMinus1)
  416. for i := uint8(0); i < maxSubLayersMinus1; i++ {
  417. if subLayerProfilePresentFlag[i], err = br.ReadBit(); err != nil {
  418. return err
  419. }
  420. if subLayerLevelPresentFlag[i], err = br.ReadBit(); err != nil {
  421. return err
  422. }
  423. }
  424. if maxSubLayersMinus1 > 0 {
  425. for i := maxSubLayersMinus1; i < 8; i++ {
  426. if _, err = br.ReadBits8(2); err != nil {
  427. return err
  428. }
  429. }
  430. }
  431. for i := uint8(0); i < maxSubLayersMinus1; i++ {
  432. if subLayerProfilePresentFlag[i] != 0 {
  433. if _, err = br.ReadBits32(32); err != nil {
  434. return err
  435. }
  436. if _, err = br.ReadBits32(32); err != nil {
  437. return err
  438. }
  439. if _, err = br.ReadBits32(24); err != nil {
  440. return err
  441. }
  442. }
  443. if subLayerLevelPresentFlag[i] != 0 {
  444. if _, err = br.ReadBits8(8); err != nil {
  445. return err
  446. }
  447. }
  448. }
  449. return nil
  450. }
  451. func (ctx *HVCDecoderConfigurationRecord) updatePtl(ptl *HVCDecoderConfigurationRecord) {
  452. ctx.generalProfileSpace = ptl.generalProfileSpace
  453. if ptl.generalTierFlag > ctx.generalTierFlag {
  454. ctx.generalLevelIdc = ptl.generalLevelIdc
  455. ctx.generalTierFlag = ptl.generalTierFlag
  456. } else {
  457. if ptl.generalLevelIdc > ctx.generalLevelIdc {
  458. ctx.generalLevelIdc = ptl.generalLevelIdc
  459. }
  460. }
  461. if ptl.generalProfileIdc > ctx.generalProfileIdc {
  462. ctx.generalProfileIdc = ptl.generalProfileIdc
  463. }
  464. ctx.generalProfileCompatibilityFlags &= ptl.generalProfileCompatibilityFlags
  465. ctx.generalConstraintIndicatorFlags &= ptl.generalConstraintIndicatorFlags
  466. }
  467. func nal2rbsp(nal []byte) []byte {
  468. // TODO chef:
  469. // 1. 输出应该可由外部申请
  470. // 2. 替换性能
  471. // 3. 该函数应该放入avc中
  472. return bytes.Replace(nal, []byte{0x0, 0x0, 0x3}, []byte{0x0, 0x0}, -1)
  473. }