h264.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. package codec
  2. import (
  3. "bytes"
  4. "errors"
  5. "io"
  6. "m7s.live/engine/v4/util"
  7. "m7s.live/engine/v4/util/bits/pio"
  8. )
  9. // Start Code + NAL Unit -> NALU Header + NALU Body
  10. // RTP Packet -> NALU Header + NALU Body
  11. // NALU Body -> Slice Header + Slice data
  12. // Slice data -> flags + Macroblock layer1 + Macroblock layer2 + ...
  13. // Macroblock layer1 -> mb_type + PCM Data
  14. // Macroblock layer2 -> mb_type + Sub_mb_pred or mb_pred + Residual Data
  15. // Residual Data ->
  16. type H264NALUType byte
  17. func (b H264NALUType) Or(b2 byte) byte {
  18. return byte(b) | b2
  19. }
  20. func (b H264NALUType) Offset() int {
  21. switch b {
  22. case NALU_STAPA:
  23. return 1
  24. case NALU_STAPB:
  25. return 3
  26. case NALU_FUA:
  27. return 2
  28. case NALU_FUB:
  29. return 4
  30. }
  31. return 0
  32. }
  33. func (b H264NALUType) Byte() byte {
  34. return byte(b)
  35. }
  36. func ParseH264NALUType(b byte) H264NALUType {
  37. return H264NALUType(b & 0x1F)
  38. }
  39. func (H264NALUType) Parse(b byte) H264NALUType {
  40. return H264NALUType(b & 0x1F)
  41. }
  42. func (H264NALUType) ParseBytes(bs []byte) H264NALUType {
  43. return H264NALUType(bs[0] & 0x1F)
  44. }
  45. const (
  46. // NALU Type
  47. NALU_Unspecified H264NALUType = iota
  48. NALU_Non_IDR_Picture // 1
  49. NALU_Data_Partition_A // 2
  50. NALU_Data_Partition_B // 3
  51. NALU_Data_Partition_C // 4
  52. NALU_IDR_Picture // 5
  53. NALU_SEI // 6
  54. NALU_SPS // 7
  55. NALU_PPS // 8
  56. NALU_Access_Unit_Delimiter // 9
  57. NALU_Sequence_End // 10
  58. NALU_Stream_End // 11
  59. NALU_Filler_Data // 12
  60. NALU_SPS_Extension // 13
  61. NALU_Prefix // 14
  62. NALU_SPS_Subset // 15
  63. NALU_DPS // 16
  64. NALU_Reserved1 // 17
  65. NALU_Reserved2 // 18
  66. NALU_Not_Auxiliary_Coded // 19
  67. NALU_Coded_Slice_Extension // 20
  68. NALU_Reserved3 // 21
  69. NALU_Reserved4 // 22
  70. NALU_Reserved5 // 23
  71. NALU_STAPA // 24
  72. NALU_STAPB
  73. NALU_MTAP16
  74. NALU_MTAP24
  75. NALU_FUA // 28
  76. NALU_FUB
  77. // 24 - 31 NALU_NotReserved
  78. )
  79. var (
  80. NALU_AUD_BYTE = []byte{0x00, 0x00, 0x00, 0x01, 0x09, 0xF0}
  81. NALU_Delimiter1 = []byte{0x00, 0x00, 0x01}
  82. NALU_Delimiter2 = []byte{0x00, 0x00, 0x00, 0x01}
  83. // 0x17 keyframe 7:AVC
  84. // 0x00 AVC sequence header
  85. // 0x00 0x00 0x00
  86. // 0x01 configurationVersion
  87. // 0x42 AVCProfileIndication
  88. // 0x00 profile_compatibility
  89. // 0x1E AVCLevelIndication
  90. // 0xFF lengthSizeMinusOne
  91. RTMP_AVC_HEAD = []byte{0x17, 0x00, 0x00, 0x00, 0x00, 0x01, 0x42, 0x00, 0x1E, 0xFF}
  92. RTMP_KEYFRAME_HEAD = []byte{0x17, 0x01, 0x00, 0x00, 0x00}
  93. RTMP_NORMALFRAME_HEAD = []byte{0x27, 0x01, 0x00, 0x00, 0x00}
  94. )
  95. // H.264/AVC视频编码标准中,整个系统框架被分为了两个层面:视频编码层面(VCL)和网络抽象层面(NAL)
  96. // NAL - Network Abstract Layer
  97. // raw byte sequence payload (RBSP) 原始字节序列载荷
  98. // SplitH264 以0x00000001分割H264裸数据
  99. func SplitH264(payload []byte) (nalus [][]byte) {
  100. for _, v := range bytes.SplitN(payload, NALU_Delimiter2, -1) {
  101. if len(v) == 0 {
  102. continue
  103. }
  104. nalus = append(nalus, bytes.SplitN(v, NALU_Delimiter1, -1)...)
  105. }
  106. return
  107. }
  108. func BuildH264SeqHeaderFromSpsPps(sps, pps []byte) (seqHeader []byte) {
  109. lenSPS, lenPPS := len(sps), len(pps)
  110. seqHeader = append([]byte{}, RTMP_AVC_HEAD...)
  111. if lenSPS > 3 {
  112. copy(seqHeader[6:], sps[1:4])
  113. }
  114. seqHeader = append(seqHeader, 0xE1, byte(lenSPS>>8), byte(lenSPS))
  115. seqHeader = append(seqHeader, sps...)
  116. seqHeader = append(append(seqHeader, 0x01, byte(lenPPS>>8), byte(lenPPS)), pps...)
  117. return
  118. }
  119. // ISO/IEC 14496-15 11(16)/page
  120. //
  121. // Advanced Video Coding
  122. //
  123. // AVCC
  124. type AVCDecoderConfigurationRecord struct {
  125. ConfigurationVersion byte // 8 bits Version
  126. AVCProfileIndication byte // 8 bits
  127. ProfileCompatibility byte // 8 bits
  128. AVCLevelIndication byte // 8 bits
  129. Reserved1 byte // 6 bits
  130. LengthSizeMinusOne byte // 2 bits 非常重要,每个NALU包前面都(lengthSizeMinusOne & 3)+1个字节的NAL包长度描述
  131. Reserved2 byte // 3 bits
  132. NumOfSequenceParameterSets byte // 5 bits SPS 的个数,计算方法是 numOfSequenceParameterSets & 0x1F
  133. NumOfPictureParameterSets byte // 8 bits PPS 的个数
  134. SequenceParameterSetLength uint16 // 16 byte SPS Length
  135. SequenceParameterSetNALUnit []byte // n byte SPS
  136. PictureParameterSetLength uint16 // 16 byte PPS Length
  137. PictureParameterSetNALUnit []byte // n byte PPS
  138. }
  139. func (p *AVCDecoderConfigurationRecord) Marshal(b []byte) (n int) {
  140. b[0] = 1
  141. b[1] = p.AVCProfileIndication
  142. b[2] = p.ProfileCompatibility
  143. b[3] = p.AVCLevelIndication
  144. b[4] = p.LengthSizeMinusOne | 0xfc
  145. b[5] = uint8(1) | 0xe0
  146. n += 6
  147. pio.PutU16BE(b[n:], p.SequenceParameterSetLength)
  148. n += 2
  149. copy(b[n:], p.SequenceParameterSetNALUnit)
  150. n += len(p.SequenceParameterSetNALUnit)
  151. b[n] = uint8(1)
  152. n++
  153. pio.PutU16BE(b[n:], p.PictureParameterSetLength)
  154. n += 2
  155. copy(b[n:], p.PictureParameterSetNALUnit)
  156. n += len(p.PictureParameterSetNALUnit)
  157. return
  158. }
  159. var ErrDecconfInvalid = errors.New("decode error")
  160. func (p *AVCDecoderConfigurationRecord) Unmarshal(b []byte) (n int, err error) {
  161. if len(b) < 7 {
  162. err = errors.New("not enough len")
  163. return
  164. }
  165. p.AVCProfileIndication = b[1]
  166. p.ProfileCompatibility = b[2]
  167. p.AVCLevelIndication = b[3]
  168. p.LengthSizeMinusOne = b[4] & 0x03
  169. spscount := int(b[5] & 0x1f)
  170. n += 6
  171. var sps, pps [][]byte
  172. for i := 0; i < spscount; i++ {
  173. if len(b) < n+2 {
  174. err = ErrDecconfInvalid
  175. return
  176. }
  177. spslen := util.ReadBE[int](b[n : n+2])
  178. n += 2
  179. if len(b) < n+spslen {
  180. err = ErrDecconfInvalid
  181. return
  182. }
  183. sps = append(sps, b[n:n+spslen])
  184. n += spslen
  185. }
  186. p.SequenceParameterSetLength = uint16(len(sps[0]))
  187. p.SequenceParameterSetNALUnit = sps[0]
  188. if len(b) < n+1 {
  189. err = ErrDecconfInvalid
  190. return
  191. }
  192. ppscount := int(b[n])
  193. n++
  194. for i := 0; i < ppscount; i++ {
  195. if len(b) < n+2 {
  196. err = ErrDecconfInvalid
  197. return
  198. }
  199. ppslen := util.ReadBE[int](b[n : n+2])
  200. n += 2
  201. if len(b) < n+ppslen {
  202. err = ErrDecconfInvalid
  203. return
  204. }
  205. pps = append(pps, b[n:n+ppslen])
  206. n += ppslen
  207. }
  208. if ppscount >= 1 {
  209. p.PictureParameterSetLength = uint16(len(pps[0]))
  210. p.PictureParameterSetNALUnit = pps[0]
  211. } else {
  212. err = ErrDecconfInvalid
  213. }
  214. return
  215. }
  216. type NALUnit struct {
  217. NALUHeader
  218. RBSP
  219. }
  220. type NALUHeader struct {
  221. forbidden_zero_bit byte // 1 bit 0
  222. nal_ref_idc byte // 2 bits nal_unit_type等于6,9,10,11或12的NAL单元其nal_ref_idc都应等于 0
  223. nal_uint_type byte // 5 bits 包含在 NAL 单元中的 RBSP 数据结构的类型
  224. }
  225. type RBSP interface {
  226. }
  227. /*
  228. 0 Unspecified non-VCL
  229. 1 Coded slice of a non-IDR picture VCL
  230. 2 Coded slice data partition A VCL
  231. 3 Coded slice data partition B VCL
  232. 4 Coded slice data partition C VCL
  233. 5 Coded slice of an IDR picture VCL
  234. 6 Supplemental enhancement information (SEI) non-VCL
  235. 7 Sequence parameter set non-VCL
  236. 8 Picture parameter set non-VCL
  237. 9 Access unit delimiter non-VCL
  238. 10 End of sequence non-VCL
  239. 11 End of stream non-VCL
  240. 12 Filler data non-VCL
  241. 13 Sequence parameter set extension non-VCL
  242. 14 Prefix NAL unit non-VCL
  243. 15 Subset sequence parameter set non-VCL
  244. 16 Depth parameter set non-VCL
  245. 17..18 Reserved non-VCL
  246. 19 Coded slice of an auxiliary coded picture without partitioning non-VCL
  247. 20 Coded slice extension non-VCL
  248. 21 Coded slice extension for depth view components non-VCL
  249. 22..23 Reserved non-VCL
  250. 24..31 Unspecified non-VCL
  251. 0:未规定
  252. 1:非IDR图像中不采用数据划分的片段
  253. 2:非IDR图像中A类数据划分片段
  254. 3:非IDR图像中B类数据划分片段
  255. 4:非IDR图像中C类数据划分片段
  256. 5:IDR图像的片段
  257. 6:补充增强信息(SEI)
  258. 7:序列参数集(SPS)
  259. 8:图像参数集(PPS)
  260. 9:分割符
  261. 10:序列结束符
  262. 11:流结束符
  263. 12:填充数据
  264. 13:序列参数集扩展
  265. 14:带前缀的NAL单元
  266. 15:子序列参数集
  267. 16 – 18:保留
  268. 19:不采用数据划分的辅助编码图像片段
  269. 20:编码片段扩展
  270. 21 – 23:保留
  271. 24 – 31:未规定
  272. nal_unit_type NAL类型 nal_reference_bit
  273. 0 未使用 0
  274. 1 非IDR的片 此片属于参考帧,则不等于0,不属于参考帧,则等与0
  275. 2 片数据A分区 同上
  276. 3 片数据B分区 同上
  277. 4 片数据C分区 同上
  278. 5 IDR图像的片 5
  279. 6 补充增强信息单元(SEI) 0
  280. 7 序列参数集 非0
  281. 8 图像参数集 非0
  282. 9 分界符 0
  283. 10 序列结束 0
  284. 11 码流结束 0
  285. 12 填充 0
  286. 13..23 保留 0
  287. 24..31 不保留 0
  288. */
  289. func ReadPPS(w io.Writer) {
  290. }