Documentation
¶
Overview ¶
Package mvc implements an H.264/AVC decoder with Multiview Video Coding (MVC, Annex H) support for stereo streams such as 3D Blu-ray, producing both the base view and the dependent view for each access unit.
Example ¶
Decode a 3D Blu-ray transport stream into pairs of full frames.
package main
import (
"fmt"
"io"
"os"
"github.com/brunoga/bdtools/m2ts"
"github.com/brunoga/bdtools/mvc"
)
func main() {
f, err := os.Open("movie.m2ts")
if err != nil {
return
}
defer f.Close()
dec := mvc.NewDecoder(mvc.Options{})
dm := m2ts.NewDemuxer(f)
consume := func(sf *mvc.StereoFrame) {
left, right := sf.Base, sf.Dependent // right is nil for 2D access units
fmt.Println(left.Width, left.Height, left.PTS, right != nil)
sf.Release() // return the buffers to the decoder
}
for {
au, err := dm.Next()
if err == io.EOF {
break
} else if err != nil {
return
}
dec.DecodeAU(au.Base, au.PTS)
dec.DecodeAU(au.Dep, au.PTS)
dm.Recycle(&au)
for {
sf, ok := dec.NextFrame()
if !ok {
break
}
consume(sf)
}
}
dec.Flush()
for {
sf, ok := dec.NextFrame()
if !ok {
break
}
consume(sf)
}
}
Output:
Index ¶
- Variables
- func BaseView(au []byte) []byte
- func ConfigRecord(stream []byte) ([]byte, error)
- func IsIDR(au []byte) bool
- func IsUnsupported(err error) bool
- func SetWavefront(on bool)
- func WritePlanes(w io.Writer, f *Frame) error
- type AUReader
- type Accel
- type AccelFiller
- type AccelPicture
- type AccelRef
- type AccelSlice
- type BaseEncoder
- type BaseOptions
- type ColorInfo
- type DecodeOptions
- type Decoder
- func (d *Decoder) ColorInfo() (primaries, transfer, matrix int, fullRange bool)
- func (d *Decoder) Decode(data []byte) error
- func (d *Decoder) DecodeAU(data []byte, pts int64) error
- func (d *Decoder) DecodeNAL(nal []byte) (err error)
- func (d *Decoder) DecodeStream(src Source, opts DecodeOptions, emit func(*StereoFrame) error) (Stats, error)
- func (d *Decoder) Flush() error
- func (d *Decoder) FrameRate() (num, den int)
- func (d *Decoder) NextFrame() (*StereoFrame, bool)
- func (d *Decoder) SetAccel(a Accel)
- func (d *Decoder) WaitFrame() (*StereoFrame, bool)
- type DepSource
- type DepStats
- type DepWriter
- func (dw *DepWriter) AccessUnit(au []byte, src *DepSource) ([]byte, error)
- func (dw *DepWriter) AccessUnitFor(au []byte, src func(poc int32, idr bool) (*DepSource, error)) ([]byte, error)
- func (dw *DepWriter) Flush() ([][]byte, error)
- func (dw *DepWriter) Stats() DepStats
- func (dw *DepWriter) Submit(au []byte, src func(poc int32, idr bool) (*DepSource, error)) ([][]byte, error)
- type Format
- type Frame
- type Layout
- type Options
- type SPSInfo
- type Source
- type Stats
- type StereoFrame
- type Y4MWriter
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var AQAbsolute bool
AQAbsolute makes adaptive quantisation offset each macroblock from a fixed energy (x264's), not the picture's mean: a measurement switch.
Functions ¶
func BaseView ¶ added in v0.9.0
BaseView strips an access unit of what an MVC stream adds to the base view (prefix NAL units, the subset SPS, the dependent view's slices and delimiter), leaving the plain H.264 base view a DepWriter takes.
func ConfigRecord ¶ added in v0.9.0
ConfigRecord returns the MVCDecoderConfigurationRecord (ISO/IEC 14496-15) of an MVC stream's start, Annex B: its first subset SPS and the PPS after it, which the dependent view's slices use. Matroska carries it in an 'mvcC' BlockAdditionMapping, which is what tells players the track holds a second view.
func IsUnsupported ¶ added in v0.14.0
IsUnsupported reports whether err says the stream uses a feature the decoder does not decode.
func SetWavefront ¶ added in v0.9.0
func SetWavefront(on bool)
SetWavefront turns the encoders' wavefront (a slice's macroblock rows decided in parallel) on or off, for measurements. It is on.
Types ¶
type AUReader ¶
type AUReader struct {
// contains filtered or unexported fields
}
AUReader splits an Annex B byte stream into access units. Use two readers to pair a base view stream with a separately stored dependent view stream (e.g. the .264 and .mvc files written by tsMuxeR).
func NewAUReader ¶
NewAUReader returns an AUReader reading from r.
type Accel ¶ added in v0.8.0
type Accel interface {
// NewSurface gives a surface for pictures of the size (in luma
// samples: whole macroblocks). A picture keeps its surface for its
// life; a new size comes with an IDR picture.
NewSurface(width, height int) (int, error)
// DecodePicture decodes a picture from its slices, in order.
DecodePicture(p *AccelPicture, slices []AccelSlice) error
}
Accel decodes pictures for the decoder.
type AccelFiller ¶ added in v0.8.0
AccelFiller is an Accel that can fill a surface with one value: for the picture standing in for a missing reference, mid-grey as the decoder here makes it.
type AccelPicture ¶ added in v0.8.0
type AccelPicture struct {
Surface int
TopPOC, BottomPOC int32
FrameNum int
Reference bool // nal_ref_idc != 0
// Refs are the reference frames the buffer holds before the picture.
Refs []AccelRef
// From the SPS.
WidthMbs, HeightMbs int
NumRefFrames int
ChromaFormat int
GapsAllowed bool
FrameMbsOnly, MBAFF bool
Direct8x8Inference bool
MinLumaBiPred8x8 bool // level 3.1 and above (A.3.3.2)
Log2MaxFrameNum int
POCType int
Log2MaxPOCLsb int
DeltaPicOrderAlwaysZero bool
// From the PPS.
PicInitQP, PicInitQS int
ChromaQPOffset [2]int
CABAC bool
WeightedPred bool
WeightedBipredIdc int
Transform8x8 bool
ConstrainedIntraPred bool
BottomFieldPicOrder bool
DeblockingControl bool
RedundantPicCntPresent bool
// The scaling lists in effect, in raster order (8x8: intra and inter
// luma).
Scaling4x4 [6][16]uint8
Scaling8x8 [2][64]uint8
}
AccelPicture is a picture's parameters, the standard's syntax elements.
type AccelRef ¶ added in v0.8.0
type AccelRef struct {
Surface int
// FrameIdx is the frame_num of a short-term reference, the
// LongTermFrameIdx of a long-term one.
FrameIdx int
LongTerm bool
TopPOC, BottomPOC int32
}
AccelRef is a reference picture.
type AccelSlice ¶ added in v0.8.0
type AccelSlice struct {
Data []byte
// BitOffset is where the slice data starts, in bits from the NAL
// unit's start, not counting emulation prevention bytes.
BitOffset int
FirstMb int
SliceType int // 0 P, 1 B, 2 I
DirectSpatial bool
NumRefIdx [2]int
CabacInitIdc int
QPDelta int
DisableDeblock int
AlphaDiv2 int
BetaDiv2 int
RefList [2][]AccelRef
// Explicit weighted prediction (all zero without).
LumaLog2Denom, ChromaLog2Denom int
LumaWeighted, ChromaWeighted [2]bool // any of the list's weights is explicit
LumaWeight, LumaOffset [2][32]int16
ChromaWeight, ChromaOffset [2][32][2]int16
}
AccelSlice is a slice: its NAL unit, as in the stream (from its header byte, emulation prevention bytes in), and its header.
type BaseEncoder ¶ added in v0.9.0
type BaseEncoder struct {
// contains filtered or unexported fields
}
BaseEncoder encodes a view's frames.
Pictures are coded concurrently, each on its own coder as soon as the pictures it predicts from are decoded: the B pictures between two references together, and the next reference picture along with them. One goroutine owns the decoder and decodes the pictures in decoding order as they are coded.
func NewBaseEncoder ¶ added in v0.9.0
func NewBaseEncoder(o BaseOptions) (*BaseEncoder, error)
NewBaseEncoder returns an encoder for frames of the options' size.
func (*BaseEncoder) Encode ¶ added in v0.9.0
func (e *BaseEncoder) Encode(src *DepSource, pts int64) ([][]byte, error)
Encode takes the next frame (display order) and returns the access units coded so far, in decoding order: a reference picture once the frames before it are in, then the B pictures between it and the one before. The source is used until the frame is coded, so the caller must not touch it again. Nothing may be encoded after Flush.
func (*BaseEncoder) Flush ¶ added in v0.9.0
func (e *BaseEncoder) Flush() ([][]byte, error)
Flush codes the frames still queued, returns their access units and ends the encoder.
func (*BaseEncoder) Stats ¶ added in v0.9.0
func (e *BaseEncoder) Stats() DepStats
Stats returns how the macroblocks were coded so far.
type BaseOptions ¶ added in v0.9.0
type BaseOptions struct {
Width, Height int // the frames' size; the coded size is whole macroblocks
QP int // the P pictures' quantiser; I pictures 3 below, B pictures 2 above
// AQ is the strength of adaptive quantisation, each macroblock's
// quantiser moved by its energy about the picture's mean (flat and
// dark ones lower, busy ones higher), as x264's variance mode does
// about a fixed energy (AQAbsolute): 1 is its usual setting, 0 none.
// Measured on Gravity, either loses about a VMAF point at equal size.
AQ float64
Keyint int // frames between IDR pictures (0: 250)
BFrames int // B pictures between references (0: none)
Refs int // reference pictures kept (0: 3)
Slices int // slices per picture, each coded on its own goroutine (0: 1; players' MVC decoders stutter on more, so pictures are coded concurrently instead)
Level int // level_idc (0: 41)
FPSNum int // frame rate, for the VUI (0: none)
FPSDen int
Color *ColorInfo // colour description for the VUI, or nil
}
BaseOptions configure a BaseEncoder.
type DecodeOptions ¶
type DecodeOptions struct {
// MaxFrames stops after that many access units (0 decodes everything).
MaxFrames int
// Mux, when set, receives the access units as an interleaved Annex B
// stream (base view then dependent view), which is how the two views of
// a split source are combined.
Mux io.Writer
// OnError receives decoding errors of individual access units, which do
// not stop the decode; nil ignores them.
OnError func(error)
}
DecodeOptions control DecodeStream.
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder decodes H.264 / MVC NAL units.
func (*Decoder) ColorInfo ¶ added in v0.8.0
ColorInfo returns the colour description of the most recent sequence parameter set's VUI (H.273 code points; 2, unspecified, when absent).
func (*Decoder) Decode ¶
Decode feeds Annex B byte stream data (start code delimited NAL units). Data may be split at arbitrary boundaries across calls.
func (*Decoder) DecodeAU ¶
DecodeAU decodes one complete access unit (or one view of it) in Annex B format and tags the pictures it starts with the timestamp pts, which is returned in Frame.PTS. Unlike Decode, the end of data is treated as the end of the last NAL unit. Use it when the input is already split into access units, e.g. by a transport stream demuxer.
func (*Decoder) DecodeStream ¶
func (d *Decoder) DecodeStream(src Source, opts DecodeOptions, emit func(*StereoFrame) error) (Stats, error)
DecodeStream decodes src to the end, calling emit for every frame in output order; emit must not retain the frame beyond the call. It returns the first error reading the source or returned by emit.
func (*Decoder) FrameRate ¶
FrameRate returns the frame rate carried by the most recent sequence parameter set's VUI timing information, as a fraction (e.g. 24000/1001), or 0/0 when the stream does not say.
func (*Decoder) NextFrame ¶
func (d *Decoder) NextFrame() (*StereoFrame, bool)
NextFrame returns the next decoded frame in output order, if any.
func (*Decoder) SetAccel ¶ added in v0.8.0
SetAccel makes the decoder decode the base view through a. Set before decoding.
func (*Decoder) WaitFrame ¶
func (d *Decoder) WaitFrame() (*StereoFrame, bool)
WaitFrame returns the next frame in output order, waiting for its decoding to complete. It returns false if no frame is pending output.
type DepSource ¶ added in v0.9.0
DepSource is a dependent view picture to encode: 4:2:0 8-bit planes of the coded size (whole macroblocks).
type DepStats ¶ added in v0.9.0
type DepStats struct {
Skip int // P_Skip or B_Skip
Direct int // B_Direct_16x16
InterView int // from the base view picture
Temporal int // from one of the dependent view's own pictures
Bi int // from two references averaged
Partitioned int // in two partitions
Intra int // intra predicted
}
DepStats counts the dependent view's macroblocks by prediction.
type DepWriter ¶ added in v0.9.0
type DepWriter struct {
// InterViewOnly predicts the dependent view from the base view alone,
// never from its own earlier pictures.
InterViewOnly bool
// Slices is how many slices a dependent view picture is cut into, each
// coded on its own goroutine; 0 is depSlices.
Slices int
// AQ is the strength of adaptive quantisation (BaseOptions.AQ).
AQ float64
// NoBSlices codes every dependent view picture as P slices, never
// predicting from two references at once.
NoBSlices bool
// contains filtered or unexported fields
}
DepWriter adds a dependent view to a base view stream.
func NewDepWriter ¶ added in v0.9.0
NewDepWriter returns a writer whose dependent view is coded at quantiser qp.
func (*DepWriter) AccessUnit ¶ added in v0.9.0
AccessUnit takes one base view access unit (Annex B) and the dependent view's picture for it and returns the MVC access unit: the base view's NAL units unchanged, with the MVC parameter sets (subset SPS and the dependent view's PPS) after its own when it carries them, then a dependent view delimiter and the dependent view's slices, as Blu-ray discs have them (but without prefix NAL units: the base view is plain H.264). Without a source picture the dependent view repeats the base view.
func (*DepWriter) AccessUnitFor ¶ added in v0.9.0
func (dw *DepWriter) AccessUnitFor(au []byte, src func(poc int32, idr bool) (*DepSource, error)) ([]byte, error)
AccessUnitFor is AccessUnit with the dependent view's picture asked for once the base view picture is decoded: src is given its picture order count and whether it is an IDR picture (which starts the count over). Encoders number frames' POC twice their display position since the last IDR picture, so a caller holding the frames in display order can find the one. A nil src makes the dependent view repeat the base view.
func (*DepWriter) Flush ¶ added in v0.9.0
Flush waits for the pictures being coded and gives their access units.
func (*DepWriter) Stats ¶ added in v0.9.0
Stats returns how the dependent view's macroblocks were predicted so far.
func (*DepWriter) Submit ¶ added in v0.9.0
func (dw *DepWriter) Submit(au []byte, src func(poc int32, idr bool) (*DepSource, error)) ([][]byte, error)
Submit takes the next base view access unit and starts coding its dependent view picture, giving back the MVC access units finished so far, in order (none, one or several). Flush gives the rest.
type Format ¶
type Format int
Format is the container of a stereo source.
const ( // FormatAnnexB is an Annex B byte stream with both views interleaved // (the base view's NAL units, then the dependent view's, per picture). FormatAnnexB Format = iota // FormatM2TS is a Blu-ray transport stream (PIDs 0x1011 and 0x1012). FormatM2TS // FormatSplit is a pair of Annex B streams: the base view and, separately, // the dependent view, as a demuxer writes them. FormatSplit // FormatAccessUnits takes paired access units from a function, for a // caller doing its own demultiplexing. FormatAccessUnits )
func FormatByName ¶
FormatByName guesses the format from a file name: transport streams by extension, everything else Annex B.
type Frame ¶
type Frame struct {
Y, Cb, Cr []byte
StrideY int
StrideC int
Width int // cropped luma width
Height int // cropped luma height
ViewID int
POC int32
PTS int64 // timestamp passed to DecodeAU, or -1
// With an accelerator, the picture is in Surface (no planes), its
// cropping window at CropLeft, CropTop.
Surface int
CropLeft, CropTop int
// Interlaced: coded as fields (field pictures or an MBAFF frame), its
// field shown first the top one when TopFieldFirst.
Interlaced, TopFieldFirst bool
// contains filtered or unexported fields
}
Frame is one decoded view component. The planes are sub-slices of internal buffers and remain valid until the owning StereoFrame is released.
type Layout ¶
type Layout string
Layout is how the two views of a stereo frame are arranged in a Y4M frame.
const ( // LayoutSideBySide puts the base view on the left and the dependent view // on the right, at full width each (3840x1080 from a 1080p pair). LayoutSideBySide Layout = "sbs" // LayoutTopBottom stacks the views, base view on top. LayoutTopBottom Layout = "tab" // LayoutBase writes the base view only. LayoutBase Layout = "base" // LayoutDependent writes the dependent view only (the base view when an // access unit has none). LayoutDependent Layout = "dep" )
type Options ¶
type Options struct {
// BaseOnly skips decoding of the dependent view.
BaseOnly bool
// Threads is the maximum number of pictures decoded concurrently
// (0 = number of CPUs).
Threads int
}
Options configure a Decoder.
type SPSInfo ¶
type SPSInfo struct {
ProfileIdc, LevelIdc int
Width, Height int // cropped luma size
FrameMbsOnly bool
FPSNum, FPSDen int // 0 when the VUI carries no timing
}
SPSInfo is what a stream listing needs to know about a video stream.
func ParseSPSInfo ¶
ParseSPSInfo reads a sequence parameter set NAL unit (with or without its start code; the first byte after the start code is the NAL header).
type Source ¶
type Source struct {
Format Format
// R is the stream, or the base view for FormatSplit.
R io.Reader
// Dependent is the dependent view for FormatSplit.
Dependent io.Reader
// AccessUnits returns the next access unit for FormatAccessUnits: the
// base view's NAL units, the dependent view's (nil for a 2D picture),
// both Annex B, and a timestamp carried through to the frame. io.EOF
// ends the stream.
AccessUnits func() (base, dep []byte, pts int64, err error)
}
Source is a stereo stream to decode.
type Stats ¶
type Stats struct {
Frames int // frames output
DependentFrames int // of which carried a dependent view
Errors int // access units reported to OnError
}
Stats counts what a decode produced.
type StereoFrame ¶
type StereoFrame struct {
Base *Frame
Dependent *Frame // nil when the access unit has no dependent view
// contains filtered or unexported fields
}
StereoFrame holds the output of one access unit: the base view and, for MVC streams, the dependent view.
func (*StereoFrame) ColorInfo ¶ added in v0.10.0
func (f *StereoFrame) ColorInfo() (primaries, transfer, matrix int, fullRange bool)
ColorInfo returns the colour description the frame's stream states (see Decoder.ColorInfo).
func (*StereoFrame) Release ¶
func (f *StereoFrame) Release()
Release returns the frame buffers to the decoder for reuse. The Frame planes must not be used afterwards.
type Y4MWriter ¶
type Y4MWriter struct {
// SwapViews exchanges the two views, for a disc whose base view is the
// right eye: the dependent view then comes first (left, or top).
SwapViews bool
// FPSNum and FPSDen are the frame rate written in the stream header.
// They are read when the first frame is written, so a caller can fill
// them from Decoder.FrameRate once the stream's parameter sets have been
// seen.
FPSNum, FPSDen int
// Depth is the sample depth written: 8 (or 0), or 10, which writes each
// sample as the 10-bit value four times it, two bytes little-endian
// (C420p10), for an encoder that is to work at 10 bits.
Depth int
// contains filtered or unexported fields
}
Y4MWriter writes stereo frames as a YUV4MPEG2 stream.
func NewY4MWriter ¶
NewY4MWriter returns a writer emitting frames in the given layout. The frame rate defaults to 24000/1001 until the fields are set.
func (*Y4MWriter) Write ¶
func (y *Y4MWriter) Write(sf *StereoFrame) error
Write emits one stereo frame. A 2D access unit (no dependent view) has its base view used for both.
Source Files
¶
- accel.go
- aureader.go
- baseenc.go
- bitreader.go
- cabac.go
- cabac_amd64.go
- cabac_blocks.go
- cabac_tables.go
- cavlc.go
- deblock.go
- deblock_amd64.go
- deblock_ctl_amd64.go
- deblock_mbaff.go
- decoder.go
- depenc.go
- depmb.go
- dpb.go
- enc_amd64.go
- encbits.go
- encintra.go
- fdctquant.go
- idct_amd64.go
- inter.go
- intra.go
- intra_amd64.go
- mb.go
- mb_amd64.go
- mb_cabac.go
- mbaff.go
- mbtree.go
- mc_amd64.go
- mvpred.go
- params.go
- picture.go
- recon.go
- slice.go
- slicedec.go
- spsinfo.go
- stream.go
- transform.go
- trellis.go
- y4m.go