sps.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package codec
  2. import (
  3. "bytes"
  4. "github.com/cnotch/ipchub/av/codec/hevc"
  5. "m7s.live/engine/v4/util/bits"
  6. )
  7. type SPSInfo struct {
  8. ProfileIdc uint
  9. LevelIdc uint
  10. MbWidth uint
  11. MbHeight uint
  12. CropLeft uint
  13. CropRight uint
  14. CropTop uint
  15. CropBottom uint
  16. Width uint
  17. Height uint
  18. }
  19. func ParseSPS(data []byte) (self SPSInfo, err error) {
  20. r := &bits.GolombBitReader{R: bytes.NewReader(data)}
  21. if _, err = r.ReadBits(8); err != nil {
  22. return
  23. }
  24. if self.ProfileIdc, err = r.ReadBits(8); err != nil {
  25. return
  26. }
  27. // constraint_set0_flag-constraint_set6_flag,reserved_zero_2bits
  28. if _, err = r.ReadBits(8); err != nil {
  29. return
  30. }
  31. // level_idc
  32. if self.LevelIdc, err = r.ReadBits(8); err != nil {
  33. return
  34. }
  35. // seq_parameter_set_id
  36. if _, err = r.ReadExponentialGolombCode(); err != nil {
  37. return
  38. }
  39. if self.ProfileIdc == 100 || self.ProfileIdc == 110 ||
  40. self.ProfileIdc == 122 || self.ProfileIdc == 244 ||
  41. self.ProfileIdc == 44 || self.ProfileIdc == 83 ||
  42. self.ProfileIdc == 86 || self.ProfileIdc == 118 {
  43. var chroma_format_idc uint
  44. if chroma_format_idc, err = r.ReadExponentialGolombCode(); err != nil {
  45. return
  46. }
  47. if chroma_format_idc == 3 {
  48. // residual_colour_transform_flag
  49. if _, err = r.ReadBit(); err != nil {
  50. return
  51. }
  52. }
  53. // bit_depth_luma_minus8
  54. if _, err = r.ReadExponentialGolombCode(); err != nil {
  55. return
  56. }
  57. // bit_depth_chroma_minus8
  58. if _, err = r.ReadExponentialGolombCode(); err != nil {
  59. return
  60. }
  61. // qpprime_y_zero_transform_bypass_flag
  62. if _, err = r.ReadBit(); err != nil {
  63. return
  64. }
  65. var seq_scaling_matrix_present_flag uint
  66. if seq_scaling_matrix_present_flag, err = r.ReadBit(); err != nil {
  67. return
  68. }
  69. if seq_scaling_matrix_present_flag != 0 {
  70. for i := 0; i < 8; i++ {
  71. var seq_scaling_list_present_flag uint
  72. if seq_scaling_list_present_flag, err = r.ReadBit(); err != nil {
  73. return
  74. }
  75. if seq_scaling_list_present_flag != 0 {
  76. var sizeOfScalingList uint
  77. if i < 6 {
  78. sizeOfScalingList = 16
  79. } else {
  80. sizeOfScalingList = 64
  81. }
  82. lastScale := uint(8)
  83. nextScale := uint(8)
  84. for j := uint(0); j < sizeOfScalingList; j++ {
  85. if nextScale != 0 {
  86. var delta_scale uint
  87. if delta_scale, err = r.ReadSE(); err != nil {
  88. return
  89. }
  90. nextScale = (lastScale + delta_scale + 256) % 256
  91. }
  92. if nextScale != 0 {
  93. lastScale = nextScale
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. // log2_max_frame_num_minus4
  101. if _, err = r.ReadExponentialGolombCode(); err != nil {
  102. return
  103. }
  104. var pic_order_cnt_type uint
  105. if pic_order_cnt_type, err = r.ReadExponentialGolombCode(); err != nil {
  106. return
  107. }
  108. if pic_order_cnt_type == 0 {
  109. // log2_max_pic_order_cnt_lsb_minus4
  110. if _, err = r.ReadExponentialGolombCode(); err != nil {
  111. return
  112. }
  113. } else if pic_order_cnt_type == 1 {
  114. // delta_pic_order_always_zero_flag
  115. if _, err = r.ReadBit(); err != nil {
  116. return
  117. }
  118. // offset_for_non_ref_pic
  119. if _, err = r.ReadSE(); err != nil {
  120. return
  121. }
  122. // offset_for_top_to_bottom_field
  123. if _, err = r.ReadSE(); err != nil {
  124. return
  125. }
  126. var num_ref_frames_in_pic_order_cnt_cycle uint
  127. if num_ref_frames_in_pic_order_cnt_cycle, err = r.ReadExponentialGolombCode(); err != nil {
  128. return
  129. }
  130. for i := uint(0); i < num_ref_frames_in_pic_order_cnt_cycle; i++ {
  131. if _, err = r.ReadSE(); err != nil {
  132. return
  133. }
  134. }
  135. }
  136. // max_num_ref_frames
  137. if _, err = r.ReadExponentialGolombCode(); err != nil {
  138. return
  139. }
  140. // gaps_in_frame_num_value_allowed_flag
  141. if _, err = r.ReadBit(); err != nil {
  142. return
  143. }
  144. if self.MbWidth, err = r.ReadExponentialGolombCode(); err != nil {
  145. return
  146. }
  147. self.MbWidth++
  148. if self.MbHeight, err = r.ReadExponentialGolombCode(); err != nil {
  149. return
  150. }
  151. self.MbHeight++
  152. var frame_mbs_only_flag uint
  153. if frame_mbs_only_flag, err = r.ReadBit(); err != nil {
  154. return
  155. }
  156. if frame_mbs_only_flag == 0 {
  157. // mb_adaptive_frame_field_flag
  158. if _, err = r.ReadBit(); err != nil {
  159. return
  160. }
  161. }
  162. // direct_8x8_inference_flag
  163. if _, err = r.ReadBit(); err != nil {
  164. return
  165. }
  166. var frame_cropping_flag uint
  167. if frame_cropping_flag, err = r.ReadBit(); err != nil {
  168. return
  169. }
  170. if frame_cropping_flag != 0 {
  171. if self.CropLeft, err = r.ReadExponentialGolombCode(); err != nil {
  172. return
  173. }
  174. if self.CropRight, err = r.ReadExponentialGolombCode(); err != nil {
  175. return
  176. }
  177. if self.CropTop, err = r.ReadExponentialGolombCode(); err != nil {
  178. return
  179. }
  180. if self.CropBottom, err = r.ReadExponentialGolombCode(); err != nil {
  181. return
  182. }
  183. }
  184. self.Width = (self.MbWidth * 16) - self.CropLeft*2 - self.CropRight*2
  185. self.Height = ((2 - frame_mbs_only_flag) * self.MbHeight * 16) - self.CropTop*2 - self.CropBottom*2
  186. return
  187. }
  188. func ParseHevcSPS(data []byte) (self SPSInfo, err error) {
  189. var rawsps hevc.H265RawSPS
  190. if err = rawsps.Decode(data); err == nil {
  191. self.CropLeft, self.CropRight, self.CropTop, self.CropBottom = uint(rawsps.Conf_win_left_offset), uint(rawsps.Conf_win_right_offset), uint(rawsps.Conf_win_top_offset), uint(rawsps.Conf_win_bottom_offset)
  192. self.Width = uint(rawsps.Pic_width_in_luma_samples)
  193. self.Height = uint(rawsps.Pic_height_in_luma_samples)
  194. }
  195. return
  196. }