h264

package
v3.0.5 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package h264 implements cgo bindings for [x264](https://www.videolan.org/developers/x264.html) library.

Index

Constants

View Source
const Build = C.X264_BUILD
View Source
const (
	CspI420 = 0x0002 // yuv 4:2:0 planar

)
View Source
const RcCrf = 1

Variables

This section is empty.

Functions

func EncoderClose

func EncoderClose(enc *T)

EncoderClose closes an encoder handler.

func EncoderEncode

func EncoderEncode(enc *T, ppNal []*Nal, piNal *int32, picIn *Picture, picOut *Picture) int32

EncoderEncode - encode one picture. Returns the number of bytes in the returned NALs, negative on error and zero if no NAL units returned.

func LibVersion

func LibVersion() int

func ParamApplyProfile

func ParamApplyProfile(param *Param, profile string) int32

ParamApplyProfile - applies the restrictions of the given profile.

Currently available profiles are, from most to least restrictive: "baseline", "main", "high", "high10", "high422", "high444". (can be nil, in which case the function will do nothing).

Returns 0 on success, negative on failure (e.g. invalid profile name).

func ParamDefault

func ParamDefault(param *Param)

ParamDefault - fill Param with default values and do CPU detection.

func ParamDefaultPreset

func ParamDefaultPreset(param *Param, preset string, tune string) int32

ParamDefaultPreset - the same as ParamDefault, but also use the passed preset and tune to modify the default settings (either can be nil, which implies no preset or no tune, respectively).

Currently available presets are, ordered from fastest to slowest: "ultrafast", "superfast", "veryfast", "faster", "fast", "medium", "slow", "slower", "veryslow", "placebo".

Currently available tunings are: "film", "animation", "grain", "stillimage", "psnr", "ssim", "fastdecode", "zerolatency".

Returns 0 on success, negative on failure (e.g. invalid preset/tune name).

Types

type H264

type H264 struct {
	// contains filtered or unexported fields
}

func NewEncoder

func NewEncoder(w, h int, opts *Options) (encoder *H264, err error)

func (*H264) Encode

func (e *H264) Encode() []byte

func (*H264) IntraRefresh

func (e *H264) IntraRefresh()

func (*H264) LoadBuf

func (e *H264) LoadBuf(yuv []byte)

func (*H264) Shutdown

func (e *H264) Shutdown() error

type Hrd

type Hrd struct {
	CpbInitialArrivalTime float64
	CpbFinalArrivalTime   float64
	CpbRemovalTime        float64

	DpbOutputTime float64
}

type Image

type Image struct {
	ICsp    int32             /* Colorspace */
	IPlane  int32             /* Number of image planes */
	IStride [4]int32          /* Strides for each plane */
	Plane   [4]unsafe.Pointer /* Pointers to each plane */
}

type ImageProperties

type ImageProperties struct {
	/* In: an array of quantizer offsets to be applied to this image during encoding.
	 *     These are added on top of the decisions made by x264.
	 *     Offsets can be fractional; they are added before QPs are rounded to integer.
	 *     Adaptive quantization must be enabled to use this feature.  Behavior if quant
	 *     offsets differ between encoding passes is undefined. */
	QuantOffsets *float32
	/* In: optional callback to free quant_offsets when used.
	*     Useful if one wants to use a different quant_offset array for each frame. */
	QuantOffsetsFree *func(arg0 unsafe.Pointer)

	/* In: optional array of flags for each macroblock.
	 *     Allows specifying additional information for the encoder such as which macroblocks
	 *     remain unchanged.  Usable flags are listed below.
	 *     x264_param_t.analyse.b_mb_info must be set to use this, since x264 needs to track
	 *     extra data internally to make full use of this information.
	 *
	 * Out: if b_mb_info_update is set, x264 will update this array as a result of encoding.
	 *
	 *      For "MBINFO_CONSTANT", it will remove this flag on any macroblock whose decoded
	 *      pixels have changed.  This can be useful for e.g. noting which areas of the
	 *      frame need to actually be blitted. Note: this intentionally ignores the effects
	 *      of deblocking for the current frame, which should be fine unless one needs exact
	 *      pixel-perfect accuracy.
	 *
	 *      Results for MBINFO_CONSTANT are currently only set for P-frames, and are not
	 *      guaranteed to enumerate all blocks which haven't changed.  (There may be false
	 *      negatives, but no false positives.)
	 */
	MbInfo *byte
	/* In: optional callback to free mb_info when used. */
	MbInfoFree *func(arg0 unsafe.Pointer)

	/* Out: SSIM of the the frame luma (if x264_param_t.b_ssim is set) */
	FSsim float64
	/* Out: Average PSNR of the frame (if x264_param_t.b_psnr is set) */
	FPsnrAvg float64
	/* Out: PSNR of Y, U, and V (if x264_param_t.b_psnr is set) */
	FPsnr [3]float64

	/* Out: Average effective CRF of the encoded frame */
	FCrfAvg float64
}

type Level

type Level struct {
	LevelIdc  byte
	Mbps      int32  /* max macroblock processing rate (macroblocks/sec) */
	FrameSize int32  /* max frame size (macroblocks) */
	Dpb       int32  /* max decoded picture buffer (mbs) */
	Bitrate   int32  /* max bitrate (kbit/sec) */
	Cpb       int32  /* max vbv buffer (kbit) */
	MvRange   uint16 /* max vertical mv component range (pixels) */
	MvsPer2mb byte   /* max mvs per 2 consecutive mbs. */
	SliceRate byte   /* ?? */
	Mincr     byte   /* min compression ratio */
	Bipred8x8 byte   /* limit bipred to >=8x8 */
	Direct8x8 byte   /* limit b_direct to >=8x8 */
	FrameOnly byte   /* forbid interlacing */
}

type Nal

type Nal struct {
	IRefIdc        int32 /* nal_priority_e */
	IType          int32 /* nal_unit_type_e */
	BLongStartcode int32
	IFirstMb       int32 /* If this NAL is a slice, the index of the first MB in the slice. */
	ILastMb        int32 /* If this NAL is a slice, the index of the last MB in the slice. */

	/* Size of payload (including any padding) in bytes. */
	IPayload int32
	/* If param->b_annexb is set, Annex-B bytestream with startcode.
	 * Otherwise, startcode is replaced with a 4-byte size.
	 * This size is the size used in mp4/similar muxing; it is equal to i_payload-4 */
	/* C.uint8_t */
	PPayload unsafe.Pointer

	/* Size of padding in bytes. */
	IPadding int32
}

Nal is The data within the payload is already NAL-encapsulated; the ref_idc and type are merely in the struct for easy access by the calling application. All data returned in x264_nal_t, including the data in p_payload, is no longer valid after the next call to x264_encoder_encode. Thus, it must be used or copied before calling x264_encoder_encode or x264_encoder_headers again.

type Options

type Options struct {
	// Constant Rate Factor (CRF)
	// This method allows the encoder to attempt to achieve a certain output quality for the whole file
	// when output file size is of less importance.
	// The range of the CRF scale is 0–51, where 0 is lossless, 23 is the default, and 51 is the worst quality possible.
	Crf uint8
	// film, animation, grain, stillimage, psnr, ssim, fastdecode, zerolatency.
	Tune string
	// ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo.
	Preset string
	// baseline, main, high, high10, high422, high444.
	Profile  string
	LogLevel int32
}

type Param

type Param struct {
	/* CPU flags */
	Cpu               uint32
	IThreads          int32 /* encode multiple frames in parallel */
	ILookaheadThreads int32 /* multiple threads for lookahead analysis */
	BSlicedThreads    int32 /* Whether to use slice-based threading. */
	BDeterministic    int32 /* whether to allow non-deterministic optimizations when threaded */
	BCpuIndependent   int32 /* force canonical behavior rather than cpu-dependent optimal algorithms */
	ISyncLookahead    int32 /* threaded lookahead buffer */

	/* Video Properties */
	IWidth      int32
	IHeight     int32
	ICsp        int32 /* CSP of encoded bitstream */
	IBitdepth   int32
	ILevelIdc   int32
	IFrameTotal int32 /* number of frames to encode if known, else 0 */

	/* NAL HRD
	 * Uses Buffering and Picture Timing SEIs to signal HRD
	 * The HRD in H.264 was not designed with VFR in mind.
	 * It is therefore not recommended to use NAL HRD with VFR.
	 * Furthermore, reconfiguring the VBV (via x264_encoder_reconfig)
	 * will currently generate invalid HRD. */
	INalHrd int32

	Vui struct {
		/* they will be reduced to be 0 < x <= 65535 and prime */
		ISarHeight int32
		ISarWidth  int32

		IOverscan int32 /* 0=undef, 1=no overscan, 2=overscan */

		/* see h264 annex E for the values of the following */
		IVidformat int32
		BFullrange int32
		IColorprim int32
		ITransfer  int32
		IColmatrix int32
		IChromaLoc int32 /* both top & bottom */
	}

	/* Bitstream parameters */
	IFrameReference int32 /* Maximum number of reference frames */
	IDpbSize        int32 /* Force a DPB size larger than that implied by B-frames and reference frames.
	 * Useful in combination with interactive error resilience. */
	IKeyintMax         int32 /* Force an IDR keyframe at this interval */
	IKeyintMin         int32 /* Scenecuts closer together than this are coded as I, not IDR. */
	IScenecutThreshold int32 /* how aggressively to insert extra I frames */
	BIntraRefresh      int32 /* Whether or not to use periodic intra refresh instead of IDR frames. */

	IBframe         int32 /* how many b-frame between 2 references pictures */
	IBframeAdaptive int32
	IBframeBias     int32
	IBframePyramid  int32 /* Keep some B-frames as references: 0=off, 1=strict hierarchical, 2=normal */
	BOpenGop        int32
	BBlurayCompat   int32
	IAvcintraClass  int32
	IAvcintraFlavor int32

	BDeblockingFilter        int32
	IDeblockingFilterAlphac0 int32 /* [-6, 6] -6 light filter, 6 strong */
	IDeblockingFilterBeta    int32 /* [-6, 6]  idem */

	BCabac        int32
	ICabacInitIdc int32

	BInterlaced       int32
	BConstrainedIntra int32

	ICqmPreset int32
	PszCqmFile *int8    /* filename (in UTF-8) of CQM file, JM format */
	Cqm4iy     [16]byte /* used only if i_cqm_preset == X264_CQM_CUSTOM */
	Cqm4py     [16]byte
	Cqm4ic     [16]byte
	Cqm4pc     [16]byte
	Cqm8iy     [64]byte
	Cqm8py     [64]byte
	Cqm8ic     [64]byte
	Cqm8pc     [64]byte

	/* Log */
	PfLog       *[0]byte
	PLogPrivate unsafe.Pointer
	ILogLevel   int32
	BFullRecon  int32 /* fully reconstruct frames, even when not necessary for encoding.  Implied by psz_dump_yuv */
	PszDumpYuv  *int8 /* filename (in UTF-8) for reconstructed frames */

	/* Encoder analyser parameters */
	Analyse struct {
		Intra uint32 /* intra partitions */
		Inter uint32 /* inter partitions */

		BTransform8x8   int32
		IWeightedPred   int32 /* weighting for P-frames */
		BWeightedBipred int32 /* implicit weighting for B-frames */
		IDirectMvPred   int32 /* spatial vs temporal mv prediction */
		IChromaQpOffset int32

		IMeMethod        int32   /* motion estimation algorithm to use (X264_ME_*) */
		IMeRange         int32   /* integer pixel motion estimation search range (from predicted mv) */
		IMvRange         int32   /* maximum length of a mv (in pixels). -1 = auto, based on level */
		IMvRangeThread   int32   /* minimum space between threads. -1 = auto, based on number of threads. */
		ISubpelRefine    int32   /* subpixel motion estimation quality */
		BChromaMe        int32   /* chroma ME for subpel and mode decision in P-frames */
		BMixedReferences int32   /* allow each mb partition to have its own reference number */
		ITrellis         int32   /* trellis RD quantization */
		BFastPskip       int32   /* early SKIP detection on P-frames */
		BDctDecimate     int32   /* transform coefficient thresholding on P-frames */
		INoiseReduction  int32   /* adaptive pseudo-deadzone */
		FPsyRd           float32 /* Psy RD strength */
		FPsyTrellis      float32 /* Psy trellis strength */
		BPsy             int32   /* Toggle all psy optimizations */

		BMbInfo       int32 /* Use input mb_info data in x264_picture_t */
		BMbInfoUpdate int32 /* Update the values in mb_info according to the results of encoding. */

		/* the deadzone size that will be used in luma quantization */
		ILumaDeadzone [2]int32

		BPsnr int32 /* compute and print PSNR stats */
		BSsim int32 /* compute and print SSIM stats */
	}

	/* Rate control parameters */
	Rc struct {
		IRcMethod int32 /* X264_RC_* */

		IQpConstant int32 /* 0=lossless */
		IQpMin      int32 /* min allowed QP value */
		IQpMax      int32 /* max allowed QP value */
		IQpStep     int32 /* max QP step between frames */

		IBitrate       int32
		FRfConstant    float32 /* 1pass VBR, nominal QP */
		FRfConstantMax float32 /* In CRF mode, maximum CRF as caused by VBV */
		FRateTolerance float32
		IVbvMaxBitrate int32
		IVbvBufferSize int32
		FVbvBufferInit float32 /* <=1: fraction of buffer_size. >1: kbit */
		FIpFactor      float32
		FPbFactor      float32

		/* VBV filler: force CBR VBV and use filler bytes to ensure hard-CBR.
		 * Implied by NAL-HRD CBR. */
		BFiller int32

		IAqMode     int32 /* psy adaptive QP. (X264_AQ_*) */
		FAqStrength float32
		BMbTree     int32 /* Macroblock-tree ratecontrol. */
		ILookahead  int32

		/* 2pass */
		BStatWrite int32 /* Enable stat writing in psz_stat_out */
		PszStatOut *int8 /* output filename (in UTF-8) of the 2pass stats file */
		BStatRead  int32 /* Read stat from psz_stat_in and use it */
		PszStatIn  *int8 /* input filename (in UTF-8) of the 2pass stats file */

		/* 2pass params (same as ffmpeg ones) */
		FQcompress      float32 /* 0.0 => cbr, 1.0 => constant qp */
		FQblur          float32 /* temporally blur quants */
		FComplexityBlur float32 /* temporally blur complexity */
		Zones           *Zone   /* ratecontrol overrides */
		IZones          int32   /* number of zone_t's */
		PszZones        *int8   /* alternate method of specifying zones */
	}

	/* Cropping Rectangle parameters: added to those implicitly defined by
	   non-mod16 video resolutions. */
	CropRect struct {
		ILeft   int32
		ITop    int32
		IRight  int32
		IBottom int32
	}

	/* frame packing arrangement flag */
	IFramePacking int32

	/* alternative transfer SEI */
	IAlternativeTransfer int32

	/* Muxing parameters */
	BAud           int32 /* generate access unit delimiters */
	BRepeatHeaders int32 /* put SPS/PPS before each keyframe */
	BAnnexb        int32 /* if set, place start codes (4 bytes) before NAL units,
	 * otherwise place size (4 bytes) before NAL units. */
	ISpsId    int32 /* SPS and PPS id number */
	BVfrInput int32 /* VFR input.  If 1, use timebase and timestamps for ratecontrol purposes.
	 * If 0, use fps only. */
	BPulldown    int32 /* use explicity set timebase for CFR */
	IFpsNum      uint32
	IFpsDen      uint32
	ITimebaseNum uint32 /* Timebase numerator */
	ITimebaseDen uint32 /* Timebase denominator */

	BTff int32

	BPicStruct int32

	/* Fake Interlaced.
	 *
	 * Used only when b_interlaced=0. Setting this flag makes it possible to flag the stream as PAFF interlaced yet
	 * encode all frames progressively. It is useful for encoding 25p and 30p Blu-Ray streams.
	 */
	BFakeInterlaced int32

	/* Don't optimize header parameters based on video content, e.g. ensure that splitting an input video, compressing
	 * each part, and stitching them back together will result in identical SPS/PPS. This is necessary for stitching
	 * with container formats that don't allow multiple SPS/PPS. */
	BStitchable int32

	BOpencl        int32          /* use OpenCL when available */
	IOpenclDevice  int32          /* specify count of GPU devices to skip, for CLI users */
	OpenclDeviceId unsafe.Pointer /* pass explicit cl_device_id as void*, for API users */
	PszClbinFile   *int8          /* filename (in UTF-8) of the compiled OpenCL kernel cache file */

	ParamFree   *func(arg unsafe.Pointer)
	NaluProcess *func(H []T, Nal []Nal, Opaque unsafe.Pointer)

	Opaque unsafe.Pointer
	// contains filtered or unexported fields
}

type PicStruct

type PicStruct int32

type Picture

type Picture struct {
	/* In: force picture type (if not auto)
	 *     If x264 encoding parameters are violated in the forcing of picture types,
	 *     x264 will correct the input picture type and log a warning.
	 * Out: type of the picture encoded */
	IType int32
	/* In: force quantizer for != X264_QP_AUTO */
	IQpplus1 int32
	/* In: pic_struct, for pulldown/doubling/etc...used only if b_pic_struct=1.
	 *     use pic_struct_e for pic_struct inputs
	 * Out: pic_struct element associated with frame */
	IPicStruct int32
	/* Out: whether this frame is a keyframe.  Important when using modes that result in
	 * SEI recovery points being used instead of IDR frames. */
	BKeyframe int32
	/* In: user pts, Out: pts of encoded picture (user)*/
	IPts int64
	/* Out: frame dts. When the pts of the first frame is close to zero,
	 *      initial frames may have a negative dts which must be dealt with by any muxer */
	IDts int64
	/* In: custom encoding parameters to be set from this frame forwards
	   (in coded order, not display order). If NULL, continue using
	   parameters from the previous frame.  Some parameters, such as
	   aspect ratio, can only be changed per-GOP due to the limitations
	   of H.264 itself; in this case, the caller must force an IDR frame
	   if it needs the changed parameter to apply immediately. */
	Param *Param
	/* In: raw image data */
	/* Out: reconstructed image data.  x264 may skip part of the reconstruction process,
	   e.g. deblocking, in frames where it isn't necessary.  To force complete
	   reconstruction, at a small speed cost, set b_full_recon. */
	Img Image
	/* In: optional information to modify encoder decisions for this frame
	 * Out: information about the encoded frame */
	Prop ImageProperties
	/* Out: HRD timing information. Output only when i_nal_hrd is set. */
	Hrdiming Hrd
	/* In: arbitrary user SEI (e.g subtitles, AFDs) */
	ExtraSei Sei
	/* private user data. copied from input to output frames. */
	Opaque unsafe.Pointer
}

type Sei

type Sei struct {
	NumPayloads int32
	Payloads    *SeiPayload
	/* In: optional callback to free each payload AND x264_sei_payload_t when used. */
	SeiFree *func(arg0 unsafe.Pointer)
}

type SeiPayload

type SeiPayload struct {
	PayloadSize int32
	PayloadType int32
	Payload     *byte
}

type T

type T struct{}

T is opaque handler for encoder

func EncoderOpen

func EncoderOpen(param *Param) *T

EncoderOpen - create a new encoder handler, all parameters from Param are copied.

type Zone

type Zone struct {
	IStart, IEnd   int32 /* range of frame numbers */
	BForceQp       int32 /* whether to use qp vs bitrate factor */
	IQp            int32
	FBitrateFactor float32
	Param          *Param
}

Jump to

Keyboard shortcuts

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