Documentation ¶
Overview ¶
Package video implements the basic structures for simple MPEG-2 video decoding as defined in ISO/IEC 13818-1.
This package is experimental and is not intended for use in production environments.
Presently, this library supports decoding a subset of the entire MPEG-2 decoding specification: namely frame based pictures, subsampled by 4:2:0 can be decoded. However, this package is an active work in progress and slowly inching toward broader support the spec.
Index ¶
- Constants
- Variables
- type ChromaFormat
- type ExtensionID
- type GroupOfPicturesHeader
- type Macroblock
- type MacroblockType
- type PictureCodingExtension
- type PictureCodingType
- type PictureHeader
- type PictureStructure
- type PictureTemporalScalableExtension
- type ScalableMode
- type SequenceDisplayExtension
- type SequenceExtension
- type SequenceHeader
- type SequenceScalableExtension
- type Slice
- type StartCode
- type TimeCode
- type UserData
- type VideoSequence
Constants ¶
const ( PictureCodingType_IntraCoded PictureCodingType PictureCodingType_PredictiveCoded PictureCodingType_BidirectionallyPredictiveCoded PictureCodingType_DCIntraCoded // Shall not be used (Used in ISO/IEC11172-2) IFrame = PictureCodingType_IntraCoded PFrame = PictureCodingType_PredictiveCoded BFrame = PictureCodingType_BidirectionallyPredictiveCoded )
Variables ¶
var EOS = errors.New("end of sequence")
var ErrInvalidReservedBits = errors.New("invalid reserved bits")
var ErrMissingMarkerBit = errors.New("missing marker bit")
var ErrUnexpectedDecodedValueType = errors.New("unexpected decoded value type")
var ErrUnexpectedExtensionID = errors.New("unexpected sequence extension id")
ErrUnexpectedExtensionID indicates that a Extension ID was read from the bitstream that was unexpected.
var ErrUnexpectedNonZeroByte = errors.New("unexpected non-zero byte")
var ErrUnexpectedStartCode = errors.New("unexpected start code")
ErrUnexpectedStartCode indicates that a start code was read from the bitstream that was unexpected.
var ErrUnsupportedVideoStream_ISO_IEC_11172_2 = errors.New("unsupported video stream ISO/IEC 11172-2")
Functions ¶
This section is empty.
Types ¶
type ChromaFormat ¶
type ChromaFormat uint32
ChromaFormat is chroma subsample ratio used in the video bitstream. It defines the number and location of the blocks encoded in a macroblock.
A macroblock in a 4:2:0 image contains 6 blocks: 4 Y, and 1 Cb, and 1 Cr, arranged in the following order:
┌───┬───┐ │ 0 │ 1 │ ┌───┐ ┌───┐ ├───┼───┤ │ 4 │ │ 5 │ │ 2 │ 3 │ └───┘ └───┘ └───┴───┘ Y Cb Cr
A macroblock in a 4:2:2 image contains 8 blocks: 4 Y, and 2 Cb, and 2 Cr, arranged in the following order:
┌───┬───┐ ┌───┐ ┌───┐ │ 0 │ 1 │ │ 4 │ │ 5 │ ├───┼───┤ ├───┤ ├───┤ │ 2 │ 3 │ │ 6 │ │ 7 │ └───┴───┘ └───┘ └───┘ Y Cb Cr
A macroblock in a 4:4:4 image contains 12 blocks: 4 Y, and 4 Cb, and 4 Cr, arranged in the following order:
┌───┬───┐ ┌───┬───┐ ┌───┬───┐ │ 0 │ 1 │ │ 4 │ 8 │ │ 5 │ 9 │ ├───┼───┤ ├───┼───┤ ├───┼───┤ │ 2 │ 3 │ │ 6 │10 │ │ 7 │11 │ └───┴───┘ └───┴───┘ └───┴───┘ Y Cb Cr
Note: At present, this library only supports decoding video with subsample ratio of 4:2:0.
const ( ChromaFormat420 ChromaFormat ChromaFormat422 ChromaFormat444 )
func (ChromaFormat) String ¶
func (cf ChromaFormat) String() string
type ExtensionID ¶
type ExtensionID uint32
ExtensionID is a 4 bit code, that immediately follows an ExtensionStartCode, used identify the following data.
const ( SequenceExtensionID ExtensionID SequenceDisplayExtensionID QuantMatrixExtensionID CopyrightExtensionID SequenceScalableExtensionID PictureDisplayExtensionID PictureCodingExtensionID PictureSpatialScalableExtensionID PictureTemporalScalableExtensionID )
func (ExtensionID) Assert ¶
func (expected ExtensionID) Assert(br bitreader.BitReader) error
Assert() returns an ErrUnexpectedExtensionID if the following bits in the bitstream do not match the expected extension id. If the expected code is present, the the bits are consumed from the bitstream.
func (ExtensionID) Check ¶
func (expected ExtensionID) Check(br bitreader.BitReader) (bool, error)
Check() returns true if the following bits in the bitstream match the expected extension id. Check() does not consume any bits from the bitstream and will only return an error if there is a underlying error attempting to peek into the bitstream.
func (ExtensionID) IsReserved ¶
func (id ExtensionID) IsReserved() bool
IsReserved() returns true if the extension id is described as "reserved".
type GroupOfPicturesHeader ¶
type GroupOfPicturesHeader struct { ClosedGOP bool // 1 uimsbf BrokenLink bool // 1 uimsbf // contains filtered or unexported fields }
func ReadGOPHeader ¶
func ReadGOPHeader(br bitreader.BitReader) (*GroupOfPicturesHeader, error)
ReadGOPHeader parses a group_of_pictures header from the given bitstream.
func (*GroupOfPicturesHeader) TimeCode ¶
func (gop *GroupOfPicturesHeader) TimeCode() TimeCode
Returns a parsed TimeCode from the raw GOP header data.
type Macroblock ¶
type Macroblock struct {
// contains filtered or unexported fields
}
type MacroblockType ¶
type MacroblockType struct {
// contains filtered or unexported fields
}
type PictureCodingExtension ¶
type PictureCodingExtension struct {
// contains filtered or unexported fields
}
type PictureCodingType ¶
type PictureCodingType uint32
PictureCodingType defines the encoding used for a frame within a video bitstream.
Intra coded pictures are encoded independently of any other pictures. They are similar to other self-contained image formats like JPEG.
Predictive coded pictures are pictures coded using a motion compensated sample from a previous decoded reference frame to be reconstructed. If a macroblock is encoded with motion vectors, then to decode the block a motion prediction is formed by projecting a portion of a "past" reference frame "forward" into the current frame. Using the motion vector to sample image data from a previously decoded reference frame, the samples are then added to the encoded samples in the bitstream to reconstruct the final block.
x──────────────────────┐ x───┬──────────────────┐ │╲ ┌ ─ ─ ┼ ─ ▶ │ │ │ MV │ ├───┘ │ │ ╲───┐ │ │ │ │ │ │ │─ ─ ─ ─ ─ │ │ │ │ └───┘ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ └──────────────────────┘ └──────────────────────┘ Reference Frame P-Frame
Bi-directionally coded picture is a picture coded using motion compensated sample from either a past frame and/or a future frame. B-Frames can contain "forward" and/or "backward" projections.
x──────────────────────┐ x───┬──────────────────┐ x──────────────────────┐ │╲ ┌ ─ ─ ┼ ─ ▶ ◀ ─ ─ ─ ─ ─ ─ ─ │ │╲─MV──┌───┐ │ │ MV │ ├───┘ └ ─ ┼ ─ ┼ ─ ─ ─│ │ │ │ ╲───┐ │ │ │ │ │ └───┘ │ │ │ │─ ─ ─ ─ ─ │ │ │ │ │ │ └───┘ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ └──────────────────────┘ └──────────────────────┘ └──────────────────────┘ Past Reference Frame B-Frame Future Reference Frame
func (PictureCodingType) String ¶
func (pct PictureCodingType) String() string
type PictureHeader ¶
type PictureHeader struct {
// contains filtered or unexported fields
}
func (PictureHeader) String ¶
func (ph PictureHeader) String() string
type PictureStructure ¶
type PictureStructure uint32
const ( PictureStructure_TopField PictureStructure PictureStructure_BottomField PictureStructure_FramePicture )
type PictureTemporalScalableExtension ¶
type PictureTemporalScalableExtension struct {
// contains filtered or unexported fields
}
type ScalableMode ¶
type ScalableMode uint32
const ( DataPartitioning ScalableMode = iota SpatialScalability SNRScalability TemporalScalability )
type SequenceDisplayExtension ¶
type SequenceDisplayExtension struct {
// contains filtered or unexported fields
}
type SequenceExtension ¶
type SequenceExtension struct {
// contains filtered or unexported fields
}
type SequenceHeader ¶
type SequenceHeader struct {
// contains filtered or unexported fields
}
func ReadSequenceHeader ¶
func ReadSequenceHeader(br bitreader.BitReader) (*SequenceHeader, error)
ReadSequenceHeader reads a sequence header from the bit stream.
type SequenceScalableExtension ¶
type SequenceScalableExtension struct {
// contains filtered or unexported fields
}
type StartCode ¶
type StartCode uint32
StartCode is a 32 bit code that acts as a marker in a coded bitstream. They usually signal the structure of following bits and/or how the bits should be interpreted.
Start codes always begin with the 24 bit integer 0x000001 followed by a specific identifier.
const ( StartCodePrefix = 0x000001 PictureStartCode StartCode = (StartCodePrefix << 8) | 0x00 // slice_start_code 01 through AF MinSliceStartCode StartCode = (StartCodePrefix << 8) | 0x01 MaxSliceStartCode StartCode = (StartCodePrefix << 8) | 0xAF UserDataStartCode StartCode = (StartCodePrefix << 8) | 0xB2 SequenceHeaderStartCode StartCode = (StartCodePrefix << 8) | 0xB3 ExtensionStartCode StartCode = (StartCodePrefix << 8) | 0xB5 SequenceEndStartCode StartCode = (StartCodePrefix << 8) | 0xB7 GroupStartCode StartCode = (StartCodePrefix << 8) | 0xB8 )
func (StartCode) Assert ¶
Assert() returns an ErrUnexpectedStartCode if the next bits in the bitstream do not match the expected code. If the expected code is present, the the bits are consumed from the bitstream.
type TimeCode ¶
type TimeCode struct { DropFrame bool // 1-bit Hours int32 // 5-bit Minutes int32 // 6-bit Seconds int32 // 6-bit Pictures int32 // 6-bit }
TimeCode represents the associated time code with the first picture following the Group of Pictures header with a TemporalReference = 0. DropFrame will only be true if the desired framerate is 29.97Hz.
TimeCode appears in the bitstream as a 25-bit integer that has the following layout:
┌──┬──────────────────┬──────────────────────┬──┬──────────────────────┬──────────────────────┐ │DF│Hours │Minutes │MB│Seconds │Pictures │ └──┴──────────────────┴──────────────────────┴──┴──────────────────────┴──────────────────────┘ ├───────────────────────────────────────────25 bits───────────────────────────────────────────┤
type VideoSequence ¶
func NewVideoSequence ¶
func NewVideoSequence(r io.Reader) *VideoSequence
func (*VideoSequence) AlignTo ¶
func (br *VideoSequence) AlignTo(startCode StartCode) error
AlignTo will trash all bits until the stream is aligned with the desired start code or error is produced.
func (*VideoSequence) Next ¶
func (vs *VideoSequence) Next() (*image.YCbCr, error)
Next() will return the next frame of video decoded from the video stream.
func (*VideoSequence) Size ¶
func (vs *VideoSequence) Size() (width int, height int)
Source Files ¶
- block.go
- chroma_format.go
- coding_block_pattern.go
- copy_macroblocks.go
- dc_dct_predictors.go
- decode_block.go
- dtc_coefficients.go
- extension_and_user_data.go
- extension_id.go
- frame_provider.go
- frame_store.go
- group_of_pictures_header.go
- idct.go
- log.go
- macroblock.go
- macroblock_addressing.go
- macroblock_type.go
- motion_compensation.go
- motion_vector_info.go
- motion_vectors.go
- next_start_code.go
- picture_coding_extension.go
- picture_coding_type.go
- picture_data.go
- picture_header.go
- picture_structure.go
- picture_temporal_scalable_extension.go
- quant_matrix_extension.go
- quantisation_matrix.go
- quantiser_scale_table.go
- sequence_display_extension.go
- sequence_extension.go
- sequence_header.go
- sequence_scalable_extension.go
- slice.go
- start_codes.go
- user_data.go
- video.go
- video_sequence.go