h264

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Copyright (c) 2019,CAOHONGJU All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Translate from FFmpeg cbs_h264.h cbs_h264_syntax_template.c

Index

Constants

View Source
const (
	NalUnspecified     = 0
	NalSlice           = 1  // 不分区非IDR图像的片
	NalDpa             = 2  // 片分区A
	NalDpb             = 3  // 片分区B
	NalDpc             = 4  // 片分区C
	NalIdrSlice        = 5  // IDR图像中的片(I帧)
	NalSei             = 6  // 补充增强信息单元,sei playload可以使用户自定义数据, 那么我们就可以利用它来传输数据
	NalSps             = 7  // 序列参数集
	NalPps             = 8  // 图像参数集
	NalAud             = 9  // 分界符
	NalEndSequence     = 10 // 序列结束
	NalEndStream       = 11 // 码流结束
	NalFillerData      = 12 // 填充
	NalSpsExt          = 13 //
	NalPrefix          = 14
	NalSubSps          = 15
	NalDps             = 16
	NalReserved17      = 17
	NalReserved18      = 18
	NalAuxiliarySlice  = 19
	NalExtenSlice      = 20
	NalDepthExtenSlice = 21
	NalReserved22      = 22
	NalReserved23      = 23
	NalUnspecified24   = 24
	NalUnspecified25   = 25
	NalUnspecified26   = 26
	NalUnspecified27   = 27
	NalUnspecified28   = 28
	NalUnspecified29   = 29
	NalUnspecified30   = 30
	NalUnspecified31   = 31

	// NAL 在 RTP 包中的扩展
	NalStapaInRtp  = 24 // 单一时间的组合包
	NalStapbInRtp  = 25 // 单一时间的组合包
	NalMtap16InRtp = 26 // 多个时间的组合包
	NalMtap24InRtp = 27 // 多个时间的组合包
	NalFuAInRtp    = 28 // 分片的单元
	NalFuBInRtp    = 29 // 分片的单元

	NalTypeBitmask = 0x1F
)
  • Table 7-1 – NAL unit type codes, syntax element categories, and NAL unit type classes in
  • T-REC-H.264-201704

H264 NAL 单元类型

View Source
const (
	ProfileConstrained = (1 << 9)  // 8+1; constraint_set1_flag
	ProfileIntra       = (1 << 11) // 8+3; constraint_set3_flag

	ProfileBaseline            = 66
	ProfileConstrainedBaseline = (66 | ProfileConstrained)
	ProfileMain                = 77
	ProfileExtended            = 88
	ProfileHigh                = 100
	ProfileHigh10              = 110
	ProfileHigh10Intra         = (110 | ProfileIntra)
	ProfileMultiViewHigh       = 118
	ProfileHigh422             = 122
	ProfileHigh422Intra        = (122 | ProfileIntra)
	ProfileStereoHigh          = 128
	ProfileHigh444             = 144
	ProfileHigh444Predictive   = 244
	ProfileHigh444Intra        = (244 | ProfileIntra)
	ProfileCAVLC444            = 44
)

Profile

View Source
const (
	ParameterSetSps = iota
	ParameterSetPps
)

参数集索引

View Source
const (
	// 7.4.2.1.1: seq_parameter_set_id is in [0, 31].
	MaxSpsCount = 32
	// 7.4.2.2: pic_parameter_set_id is in [0, 255].
	MaxPpsCount = 256

	// A.3: MaxDpbFrames is bounded above by 16.
	MaxDpbFrames = 16
	// 7.4.2.1.1: max_num_ref_frames is in [0, MaxDpbFrames], and
	// each reference frame can have two fields.
	MaxRefs = 2 * MaxDpbFrames

	// 7.4.3.1: modification_of_pic_nums_idc is not equal to 3 at most
	// num_ref_idx_lN_active_minus1 + 1 times (that is, once for each
	// possible reference), then equal to 3 once.
	MaxRplmCount = MaxRefs + 1

	// 7.4.3.3: in the worst case, we begin with a full short-term
	// reference picture list.  Each picture in turn is moved to the
	// long-term list (type 3) and then discarded from there (type 2).
	// Then, we set the length of the long-term list (type 4), mark
	// the current picture as long-term (type 6) and terminate the
	// process (type 0).
	MaxMmcoCount = MaxRefs*2 + 3

	// A.2.1, A.2.3: profiles supporting FMO constrain
	// num_slice_groups_minus1 to be in [0, 7].
	MaxSliceGroups = 8

	// E.2.2: cpb_cnt_minus1 is in [0, 31].
	MaxCpbCnt = 32

	// A.3: in table A-1 the highest level allows a MaxFS of 139264.
	MaxMbPicSize = 139264
	// A.3.1, A.3.2: PicWidthInMbs and PicHeightInMbs are constrained
	// to be not greater than sqrt(MaxFS * 8).  Hence height/width are
	// bounded above by sqrt(139264 * 8) = 1055.5 macroblocks.
	MaxMbWidth  = 1055
	MaxMbHeight = 1055
	MaxWidth    = MaxMbWidth * 16
	MaxHeight   = MaxMbHeight * 16
)

其他常量

Variables

This section is empty.

Functions

func IsFillerData

func IsFillerData(nt byte) bool

IsFillerData .

func IsIdrSlice

func IsIdrSlice(nt byte) bool

IsIdrSlice .

func IsPps

func IsPps(nt byte) bool

IsPps .

func IsSps

func IsSps(nt byte) bool

IsSps .

func MetadataIsReady

func MetadataIsReady(vm *codec.VideoMeta) bool

MetadataIsReady .

func NulType

func NulType(nt byte) byte

NulType .

Types

type RawHRD

type RawHRD struct {
	CpbCntMinus1 uint8
	BitRateScale uint8
	CpbSizeScale uint8

	BitRateValueMinus1 [MaxCpbCnt]uint32
	CpbSizeValueMinus1 [MaxCpbCnt]uint32
	CbrFlag            [MaxCpbCnt]uint8

	InitialCpbRemovalDelayLengthMinus1 uint8
	CpbRemovalDelayLengthMinus1        uint8
	DpbOutputDelayLengthMinus1         uint8
	TimeOffsetLength                   uint8
}

RawHRD .

type RawNALUnitHeader

type RawNALUnitHeader struct {
	ForbiddenZeroBit uint8
	NalRefIdc        uint8
	NalUnitType      uint8
}

RawNALUnitHeader 原始 h264 Nal单元头

func (*RawNALUnitHeader) Set

func (h *RawNALUnitHeader) Set(nal uint8) (err error)

Set .

type RawSPS

type RawSPS struct {
	NalUnitHeader RawNALUnitHeader

	// 指明所用  profile、level、及对附录A.2的遵循情况
	// Set0 -> A.2.1 ,依次递推
	ProfileIdc         uint8
	ConstraintSet0Flag uint8
	ConstraintSet1Flag uint8
	ConstraintSet2Flag uint8
	ConstraintSet3Flag uint8
	ConstraintSet4Flag uint8
	ConstraintSet5Flag uint8
	ReservedZero2Bits  uint8
	LevelIdc           uint8

	// 指明本序列参数集的  id 号,这个 id 号将被 picture 参数集引用,
	// 本句法元素的值应该在[0,31]。
	// 编码需要产生新的序列集时,使用新的id,而不是改变原来参数集的内容
	SeqParameterSetID uint8

	ChromaFormatIdc                 uint8
	SeparateColourPlaneFlag         uint8
	BitDepthLumaMinus8              uint8
	BitDepthChromaMinus8            uint8
	QpprimeYZeroTransformBypassFlag uint8

	SeqScalingMatrixPresentFlag uint8
	SeqScalingListPresentFlag   [12]uint8
	ScalingList4x4              [6][64]int8
	ScalingList8x8              [6][64]int8

	// 为读取另一个句法元素 frame_num 服务的,frame_num 是最重要的句法元素之一,
	// 它标识所属图像的解码顺序 。这个句法元素同时也指明了 frame_num 的所能达到的最大值:
	// MaxFrameNum = 2*exp( Log2MaxFrameNumMinus4 + 4 )
	Log2MaxFrameNumMinus4          uint8
	PicOrderCntType                uint8      // 指明了 poc  (picture  order  count)  的编码方法,poc 标识图像的播放顺序。由poc 可以由 frame-num 通过映射关系计算得来,也可以索性由编码器显式地传送。
	Log2MaxPicOrderCntLsbMinus4    uint8      // 指明了变量  MaxPicOrderCntLsb 的值: MaxPicOrderCntLsb = pow(2, (log2_max_pic_order_cnt_lsb_minus4 + 4) )
	DeltaPicOrderAlwaysZeroFlag    uint8      // 等于 1 时,句法元素 delta_pic_order_cnt[0]和 delta_pic_order_cnt[1]
	OffsetForNonRefPic             int32      // 被用来计算非参考帧或场的 POC,本句法元素的值应该在[pow(-2, 31)  , pow(2, 31)  – 1]。
	OffsetForTopToBottomField      int32      // 被用来计算帧的底场的 POC,  本句法元素的值应该在[pow(-2, 31)  , pow(2, 31)  – 1]。
	NumRefFramesInPicOrderCntCycle uint8      // 被用来解码POC, 本句法元素的值应该在[0,255]。
	OffsetForRefFrame              [256]int32 // offset_for_ref__frame[i]  用于解码 POC,本句法元素对循环num_ref_frames_in_pic_order_cycle 中的每一个元素指定一个偏移。

	// max_num_ref_frames
	// 指定参考帧队列可能达到的最大长度,解码器依照这个句法元素的值开辟存储区,
	// 这个存储区用于存放已解码的参考帧,H.264 规定最多可用 16 个参考帧,本句法元素的值最大为 16。
	// 值得注意的是这个长度以帧为单位,如果在场模式下,应该相应地扩展一倍。
	// gaps_in_frame_num_value_allowed_flag
	// 这个句法元素等于 1 时,表示允许句法元素 frame_num 可以不连续。
	// 当传输信道堵塞严重时,编码器来不及将编码后的图像全部发出,这时允许丢弃若干帧图像。
	MaxNumRefFrames           uint8
	GapsInFrameNumAllowedFlag uint8

	// pic_width_in_mbs_minus1
	// 本句法元素加 1 后指明图像宽度,以宏块为单位: PicWidthInMbs = PicWidthInMbsMinus1 + 1。
	// 通过这个句法元素解码器可以计算得到亮度分量以像素为单位的图像宽度: PicWidthInSamples = PicWidthInMbs * 16
	// pic_height_in_map_units_minus1 同理
	PicWidthInMbsMinus1       uint16
	PicHeightInMapUnitsMinus1 uint16

	// frame_mbs_only_flag
	// 本句法元素等于 0 时表示本序列中所有图像的编码模式都是帧,没有其他编码模式存在;
	// 本句法元素等于 1 时  ,表示本序列中图像的编码模式可能是帧,也可能是场或帧场自适应,某个图像具体是哪一种要由其他句法元素决定。
	// mb_adaptive_frame_field_flag
	// 指明本序列是否属于帧场自适应模式。
	// mb_adaptive_frame_field_flag等于1时表明在本序列中的图像如果不是场模式就是帧场自适应模式
	// 等于0时表示本序列中的图像如果不是场模式就是帧模式。
	// direct_8x8_inference_flag    用于指明 B 片的直接和 skip 模式下运动矢量的预测方法。
	FrameMbsOnlyFlag         uint8
	MbAdaptiveFrameFieldFlag uint8
	Direct8x8InferenceFlag   uint8

	// frame_cropping_flag
	// 用于指明解码器是否要将图像裁剪后输出,如果是的话,后面紧跟着的四个句法元素分别指出左右、上下裁剪的宽度。
	FrameCroppingFlag     uint8
	FrameCropLeftOffset   uint16
	FrameCropRightOffset  uint16
	FrameCropTopOffset    uint16
	FrameCropBottomOffset uint16

	// vui_parameters_present_flag
	// 指明 vui 子结构是否出现在码流中,vui 用以表征视频格式等额外信息。
	VuiParametersPresentFlag uint8
	Vui                      RawVUI
}

RawSPS .

func (*RawSPS) Decode

func (sps *RawSPS) Decode(data []byte) (err error)

Decode 从字节序列中解码 sps NAL

func (*RawSPS) DecodeString

func (sps *RawSPS) DecodeString(b64 string) error

DecodeString 从 base64 字串解码 sps NAL

func (*RawSPS) FrameRate

func (sps *RawSPS) FrameRate() float64

FrameRate Video frame rate

func (*RawSPS) Height

func (sps *RawSPS) Height() int

Height 视频高度(像素)

func (*RawSPS) IsFixedFrameRate

func (sps *RawSPS) IsFixedFrameRate() bool

IsFixedFrameRate 是否固定帧率

func (*RawSPS) Width

func (sps *RawSPS) Width() int

Width 视频宽度(像素)

type RawVUI

type RawVUI struct {
	// aspect_ratio_info_present_flag
	// 等于1 表示aspect_ratio_idc 存在。
	// aspect_ratio_info_present_flag 等于0 表示 aspect_ratio_idc不存在。
	// aspect_ratio_idc
	// 表示亮度样值的样点高宽比的取值。表E-1 给出代码的含义。
	// 当aspect_ratio_idc 的取值表示是Extended_SAR,样点高宽比由sar_width 和sar_height 描述。
	// 当aspect_ratio_idc 语法元素不存在,aspect_ratio_idc的值应被推定为0。
	// sar_width和sar_height应是互质的或等于0。
	// 当aspect_ratio_idc等于0或sar_width等于0或sar_height等于0时,
	// 样点高宽比应被视为本建议书 | 国际标准未定义的。
	AspectRatioInfoPresentFlag uint8
	AspectRatioIdc             uint8
	SarWidth                   uint16 // 表示样点高宽比的水平尺寸(以任意单位)。
	SarHeight                  uint16 // 表示样点高宽比的垂直尺寸(以与sar_width相同的任意单位)。

	// overscan_info_present_flag
	// 等于1 表示overscan_appropriate_flag 存在。
	// 等于0或不存在时,视频信号的优选显示方法未定义。
	// overscan_appropriate_flag
	// 等于1 表示被剪切的解码图像输出适合以过扫描显示。
	// overscan_appropriate_flag等于0 表示被剪切的解码图像输出在向外到图像剪切矩形边缘的整个区域包含重要的可视信息,
	// 因此被剪切的解码图像输出不应以过扫描显示。
	// 相反地,它应以显示区域和剪切矩形的完全匹配方式显示或以欠扫描显示。
	// 注1 — 例如,overscan_appropriate_flag等于1可以用于娱乐电视节目,或视频会议中人物的现场图像,
	// 而 overscan_appropriate_flag 等于0可以用于计算机屏幕捕捉或保安摄像内容。
	OverscanInfoPresentFlag uint8
	OverscanAppropriateFlag uint8

	// video_signal_type_present_flag
	// 等于1表示video_format,video_full_range_flag和 colour_description_present_flag 存在。
	// 等于0 表示video_format,video_full_range_flag 和 colour_description_present_flag不存在。
	// video_format
	// 表示图像在国际标准编码前的制式,见表E-2的规定。
	// 当video_format语法元素不存在,video_format的值应被推定为5。
	// video_full_range_flag
	// 表示黑电平和亮度与色度信号的范围由E’Y, E’PB, 和E’PR 或 E’R, E’G, 和 E’B模拟信号分量得到。
	// 当video_full_range_flag语法元素不存在时,video_full_range_flag的值应被推定为等于0。
	// colour_description_present_flag
	// 等于1表示colour_primaries,transfer_characteristics和 matrix_coefficients存在。
	// 等于0表示colour_primaries,transfer_characteristics和 matrix_coefficients不存在。
	// colour_primaries
	// 表示最初的原色的色度坐标,按照CIE 1931的规定(见表E-3),x和y的定义由SO/CIE10527规定。
	// 当colour_primaries 语法元素不存在时,colour_primaries 的值应被推定为等于2(色度未定义或由应用决定)。
	VideoSignalTypePresentFlag   uint8
	VideoFormat                  uint8
	VideoFullRangeFlag           uint8
	ColourDescriptionPresentFlag uint8
	ColourPrimaries              uint8
	TransferCharacteristics      uint8
	MatrixCoefficients           uint8

	ChromaLocInfoPresentFlag       uint8
	ChromaSampleLocTypeTopField    uint8
	ChromaSampleLocTypeBottomField uint8

	// 和帧率相关
	TimingInfoPresentFlag uint8
	NumUnitsInTick        uint32
	TimeScale             uint32
	FixedFrameRateFlag    uint8

	NalHrdParametersPresentFlag uint8
	NalHrdParameters            RawHRD
	VclHrdParametersPresentFlag uint8
	VclHrdParameters            RawHRD
	LowDelayHrdFlag             uint8

	PicStructPresentFlag uint8

	BitstreamRestrictionFlag           uint8
	MotionVectorsOverPicBoundariesFlag uint8
	MaxBytesPerPicDenom                uint8
	MaxBitsPerMbDenom                  uint8
	Log2MaxMvLengthHorizontal          uint8
	Log2MaxMvLengthVertical            uint8
	MaxNumReorderFrames                uint8
	MaxDecFrameBuffering               uint8
}

RawVUI .

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL