fourcc

package module
v0.0.0-...-7efda05 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2018 License: MIT Imports: 1 Imported by: 0

README

go-fourcc

Package fourcc is a Go implementation of FOURCC (four character code) (4CC) identifiers for a video codecs, compression formats, colors, and pixel format used in media files.

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-fourcc

GoDoc

Introduction

FOURCC, also sometimes written as FourCC and 4CC, are all short for “four character code”.

FOURCC, is a sequence of 4 8-bit bytes packed into an unsigned 32-bit integer which, uniquely identifies a data format.

(A character in this context is a 1 byte (i.e., 8 bit) value. Thus a FOURCC always takes up exatly 32 bits (i.e., 4 bytes).)

These data formats tend to be video codecs, compression formats, colors, and pixel format used in media files.

Samples

Here are some sample FOURCC.

Label Hexadecimal (unsigned integer)
PAL8 0x384C4150
VP80 0x30385056
H264 0x34363248
MJPG 0x47504A4D

The way you can understand how these labels get turned in unsigned integers is, if we consider the FOURCC “PAL8”, then:

ASCII ‘P’ = hexadecimal 0x50
ASCII ‘A’ = hexadecimal 0x41
ASCII ‘L’ = hexadecimal 0x4C
ASCII ‘8’ = hexadecimal 0x38

And then these 8-bit bytes as packed into an unsigned integer in reverse order.

I.e.,:

var value uint32 = (uint32('P') | (uint32('A') << 8) | (uint32('L') << 16) | (uint32('8') << 24))

Examples

Here is an example usage:

var colorspace fourcc.Type = fourcc.FOURCC('P','A','L','8')

Here is a constant:

const (
	VP8 = fourcc.Const("VP80")
)

A fourcc.Const needs to be turned into a fourcc.Type before using it. So:

var datum fourcc.Type
var err error

datum, err = VP8.FOURCC()

Some uses of Scan:

var colorspace fourcc.Type

err := colorspace.Scan("PAL8")

And:

var p [4]byte = [4]byte{'P','A','L','8'}

var colorspace fourcc.Type

err := colorspace.Scan(p)

And:

var p []byte = []byte{'P','A','L','8'}

var colorspace fourcc.Type

err := colorspace.Scan(p)

See Also

Documentation

Overview

Package fourcc is a Go implementation of FOURCC (four character code) (4CC) identifiers for a video codecs, compression formats, colors, and pixel format used in media files.

Introduction

FOURCC, also sometimes written as FourCC and 4CC, are all short for “four character code”.

FOURCC, is a sequence of 4 8-bit bytes packed into an unsigned 32-bit integer which, uniquely identifies a data format.

(A character in this context is a 1 byte (i.e., 8 bit) value. Thus a FOURCC always takes up exatly 32 bits (i.e., 4 bytes).)

These data formats tend to be video codecs, compression formats, colors, and pixel format used in media files.

Samples

Here are some sample FOURCC.

┏━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Label ┃ Hexadecimal (unsigned integer) ┃
┡━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ PAL8  │ 0x384C4150                     │
┼───────┼────────────────────────────────┤
│ VP80  │ 0x30385056                     │
┼───────┼────────────────────────────────┤
│ H264  │ 0x34363248                     │
┼───────┼────────────────────────────────┤
│ MJPG  │ 0x47504A4D                     │
└───────┴────────────────────────────────┘

The way you can understand how these labels get turned in unsigned integers is, if we consider the FOURCC “PAL8”, then:

ASCII ‘P’ = hexadecimal 0x50
ASCII ‘A’ = hexadecimal 0x41
ASCII ‘L’ = hexadecimal 0x4C
ASCII ‘8’ = hexadecimal 0x38

And then these 8-bit bytes as packed into an unsigned integer in reverse order.

I.e.,:

var value uint32 = (uint32('P') | (uint32('A') << 8) | (uint32('L') << 16) | (uint32('8') << 24))

Examples

Here is an example usage:

var colorspace fourcc.Type = fourcc.FOURCC('P','A','L','8')

Here is a constant:

const (
	VP8 = fourcc.Const("VP80")
)

A fourcc.Const needs to be turned into a fourcc.Type before using it. So:

var datum fourcc.Type
var err error

datum, err = VP8.FOURCC()

Some uses of Scan:

var colorspace fourcc.Type

err := colorspace.Scan("PAL8")

And:

var p [4]byte = [4]byte{'P','A','L','8'}

var colorspace fourcc.Type

err := colorspace.Scan(p)

And:

var p []byte = []byte{'P','A','L','8'}

var colorspace fourcc.Type

err := colorspace.Scan(p)

See Also

More information about FOURCC can be found at: https://www.fourcc.org/

Index

Examples

Constants

View Source
const (
	SBGGR8  = Const("BA81") //  8  BGBG.. GRGR..
	SGBRG8  = Const("GBRG") //  8  GBGB.. RGRG..
	SGRBG8  = Const("GRBG") //  8  GRGR.. BGBG..
	SRGGB8  = Const("RGGB") //  8  RGRG.. GBGB..
	SBGGR10 = Const("BG10") // 10  BGBG.. GRGR..
	SGBRG10 = Const("GB10") // 10  GBGB.. RGRG..
	SGRBG10 = Const("BA10") // 10  GRGR.. BGBG..
	SRGGB10 = Const("RG10") // 10  RGRG.. GBGB..

	// 10bit raw bayer packed, 5 bytes for every 4 pixels
	SBGGR10P = Const("pBAA")
	SGBRG10P = Const("pGAA")
	SGRBG10P = Const("pgAA")
	SRGGB10P = Const("pRAA")

	// 10bit raw bayer a-law compressed to 8 bits
	SBGGR10ALAW8 = Const("aBA8")
	SGBRG10ALAW8 = Const("aGA8")
	SGRBG10ALAW8 = Const("agA8")
	SRGGB10ALAW8 = Const("aRA8")

	// 10bit raw bayer DPCM compressed to 8 bits
	SBGGR10DPCM8 = Const("bBA8")
	SGBRG10DPCM8 = Const("bGA8")
	SGRBG10DPCM8 = Const("BD10")
	SRGGB10DPCM8 = Const("bRA8")
	SBGGR12      = Const("BG12") // 12  BGBG.. GRGR..
	SGBRG12      = Const("GB12") // 12  GBGB.. RGRG..
	SGRBG12      = Const("BA12") // 12  GRGR.. BGBG..
	SRGGB12      = Const("RG12") // 12  RGRG.. GBGB..

	// 12bit raw bayer packed, 6 bytes for every 4 pixels
	SBGGR12P = Const("pBCC")
	SGBRG12P = Const("pGCC")
	SGRBG12P = Const("pgCC")
	SRGGB12P = Const("pRCC")
	SBGGR16  = Const("BYR2") // 16  BGBG.. GRGR..
	SGBRG16  = Const("GB16") // 16  GBGB.. RGRG..
	SGRBG16  = Const("GR16") // 16  GRGR.. BGBG..
	SRGGB16  = Const("RG16") // 16  RGRG.. GBGB..
)

Bayer formats - see http://www.siliconimaging.com/RGB%20Bayer.htm

View Source
const (
	MJPEG       = Const("MJPG") // Motion-JPEG
	JPEG        = Const("JPEG") // JFIF JPEG
	DV          = Const("dvsd") // 1394
	MPEG        = Const("MPEG") // MPEG-1/2/4 Multiplexed
	H264        = Const("H264") // H264 with start codes
	H264_NO_SC  = Const("AVC1") // H264 without start codes
	H264_MVC    = Const("M264") // H264 MVC
	H263        = Const("H263") // H263
	MPEG1       = Const("MPG1") // MPEG-1 ES
	MPEG2       = Const("MPG2") // MPEG-2 ES
	MPEG4       = Const("MPG4") // MPEG-4 part 2 ES
	XVID        = Const("XVID") // Xvid
	VC1_ANNEX_G = Const("VC1G") // SMPTE 421M Annex G compliant stream
	VC1_ANNEX_L = Const("VC1L") // SMPTE 421M Annex L compliant stream
	VP8         = Const("VP80") // VP8
	VP9         = Const("VP90") // VP9
)

Compressed formats

View Source
const (
	GREY = Const("GREY") // depth:  8; description: Greyscale
	Y4   = Const("Y04 ") // depth:  4; description: Greyscale
	Y6   = Const("Y06 ") // depth:  6; description: Greyscale
	Y10  = Const("Y10 ") // depth: 10; description: Greyscale
	Y12  = Const("Y12 ") // depth: 12; description: Greyscale
	Y16  = Const("Y16 ") // depth: 16; description: Greyscale
)

Grey formats

View Source
const (
	HSV24 = Const("HSV3")
	HSV32 = Const("HSV4")
)

HSV formats

View Source
const (
	YUYV   = Const("YUYV") // depth: 16; description: YUV 4:2:2
	YYUV   = Const("YYUV") // depth: 16; description: YUV 4:2:2
	YVYU   = Const("YVYU") // depth: 16; description: YVU 4:2:2
	UYVY   = Const("UYVY") // depth: 16; description: YUV 4:2:2
	VYUY   = Const("VYUY") // depth: 16; description: YUV 4:2:2
	Y41P   = Const("Y41P") // depth: 12; description: YUV 4:1:1
	YUV444 = Const("Y444") // depth: 16; description: xxxxyyyy uuuuvvvv
	YUV555 = Const("YUVO") // depth: 16; description: YUV-5-5-5
	YUV565 = Const("YUVP") // depth: 16; description: YUV-5-6-5
	YUV32  = Const("YUV4") // depth: 32; description: YUV-8-8-8-8
	HI240  = Const("HI24") // depth:  8; description: 8-bit color
	HM12   = Const("HM12") // depth:  8; description: YUV 4:2:0 16x16 macroblocks
	M420   = Const("M420") // depth: 12; description: YUV 4:2:0 2 lines y, 1 line uv interleaved

)

Luminance+Chrominance formats

View Source
const (
	RGB332  = Const("RGB1") // depth:  8; description: RGB-3-3-2
	RGB444  = Const("R444") // depth: 16; description: xxxxrrrr ggggbbbb
	ARGB444 = Const("AR12") // depth: 16; description: aaaarrrr ggggbbbb
	XRGB444 = Const("XR12") // depth: 16; description: xxxxrrrr ggggbbbb
	RGB555  = Const("RGBO") // depth: 16; description: RGB-5-5-5
	ARGB555 = Const("AR15") // depth: 16; description: ARGB-1-5-5-5
	XRGB555 = Const("XR15") // depth: 16; description: XRGB-1-5-5-5
	RGB565  = Const("RGBP") // depth: 16; description: RGB-5-6-5
	RGB555X = Const("RGBQ") // depth: 16; description: RGB-5-5-5 BE

	RGB565X = Const("RGBR") // depth: 16; description: RGB-5-6-5 BE
	BGR666  = Const("BGRH") // depth: 18; description: BGR-6-6-6
	BGR24   = Const("BGR3") // depth: 24; description: BGR-8-8-8
	RGB24   = Const("RGB3") // depth: 24; description: RGB-8-8-8
	BGR32   = Const("BGR4") // depth: 32; description: BGR-8-8-8-8
	ABGR32  = Const("AR24") // depth: 32; description: BGRA-8-8-8-8
	XBGR32  = Const("XR24") // depth: 32; description: BGRX-8-8-8-8
	RGB32   = Const("RGB4") // depth: 32; description: RGB-8-8-8-8
	ARGB32  = Const("BA24") // depth: 32; description: AR GB-8-8-8-8
	XRGB32  = Const("BX24") // depth: 32; description: XRGB-8-8-8-8
)

RGB formats

View Source
const (
	YUV420M = Const("YM12") // depth: 12; description: YUV420 planar
	YVU420M = Const("YM21") // depth: 12; description: YVU420 planar
	YUV422M = Const("YM16") // depth: 16; description: YUV422 planar
	YVU422M = Const("YM61") // depth: 16; description: YVU422 planar
	YUV444M = Const("YM24") // depth: 24; description: YUV444 planar
	YVU444M = Const("YM42") // depth: 24; description: YVU444 planar
)

Three non contiguous planes - Y, Cb, Cr

View Source
const (
	YUV410  = Const("YUV9") // depth:  9; description: YUV 4:1:0
	YVU410  = Const("YVU9") // depth:  9; description: YVU 4:1:0
	YUV411P = Const("411P") // depth: 12; description: YVU411 planar
	YUV420  = Const("YU12") // depth: 12; description: YUV 4:2:0
	YVU420  = Const("YV12") // depth: 12; description: YVU 4:2:0
	YUV422P = Const("422P") // depth: 16; description: YVU422 planar
)

Three planes - Y Cb, Cr

View Source
const (
	NV12M        = Const("NM12") // depth: 12; description: Y/CbCr 4:2:0
	NV21M        = Const("NM21") // depth: 21; description: Y/CrCb 4:2:0
	NV16M        = Const("NM16") // depth: 16; description: Y/CbCr 4:2:2
	NV61M        = Const("NM61") // depth: 16; description: Y/CrCb 4:2:2
	NV12MT       = Const("TM12") // depth: 12; description: Y/CbCr 4:2:0 64x32 macroblocks
	NV12MT_16X16 = Const("VM12") // depth: 12; description: Y/CbCr 4:2:0 16x16 macroblocks
)

Two non contiguous planes - one Y, one Cr + Cb interleaved

View Source
const (
	NV12 = Const("NV12") // depth: 12; description: Y/CbCr 4:2:0
	NV21 = Const("NV21") // depth: 12; description: Y/CrCb 4:2:0
	NV16 = Const("NV16") // depth: 16; description: Y/CbCr 4:2:2
	NV61 = Const("NV61") // depth: 16; description: Y/CrCb 4:2:2
	NV24 = Const("NV24") // depth: 24; description: Y/CbCr 4:4:4
	NV42 = Const("NV42") // depth: 24; description: Y/CrCb 4:4:4
)

Two planes -- one Y, one Cr + Cb interleaved

View Source
const (
	CPIA1        = Const("CPIA") // cpia1 YUV
	WNVA         = Const("WNVA") // Winnov hw compress
	SN9C10X      = Const("S910") // SN9C10x compression
	SN9C20X_I420 = Const("S920") // SN9C20x YUV 4:2:0
	PWC1         = Const("PWC1") // pwc older webcam
	PWC2         = Const("PWC2") // pwc newer webcam
	ET61X251     = Const("E625") // ET61X251 compression
	SPCA501      = Const("S501") // YUYV per line
	SPCA505      = Const("S505") // YYUV per line
	SPCA508      = Const("S508") // YUVY per line
	SPCA561      = Const("S561") // compressed GBRG bayer
	PAC207       = Const("P207") // compressed BGGR bayer
	MR97310A     = Const("M310") // compressed BGGR bayer
	JL2005BCD    = Const("JL20") // compressed RGGB bayer
	SN9C2028     = Const("SONX") // compressed GBRG bayer
	SQ905C       = Const("905C") // compressed RGGB bayer
	PJPG         = Const("PJPG") // Pixart 73xx JPEG
	OV511        = Const("O511") // ov511 JPEG
	OV518        = Const("O518") // ov518 JPEG
	STV0680      = Const("S680") // stv0680 bayer
	TM6000       = Const("TM60") // tm5600/tm60x0
	CIT_YYVYUY   = Const("CITV") // one line of Y then 1 line of VYUY
	KONICA420    = Const("KONI") // YUV420 planar in blocks of 256 pixels
	JPGL         = Const("JPGL") // JPEG-Lite
	SE401        = Const("S401") // se401 janggu compressed rgb
	S5C_UYVY_JPG = Const("S5CI") // S5C73M3 interleaved UYVY/JPEG
	Y8I          = Const("Y8I ") // Greyscale 8-bit L/R interleaved
	Y12I         = Const("Y12I") // Greyscale 12-bit L/R interleaved
	Z16          = Const("Z16 ") // Depth data 16-bit
	MT21C        = Const("MT21") // Mediatek compressed block mode
	INZI         = Const("INZI") // Intel Planar Greyscale 10-bit and Depth 16-bit
)

Vendor-specific formats

View Source
const (
	PAL8 = Const("PAL8") // depth: 8; description: 8-bit palette
)

Palette formats

View Source
const (
	UV8 = Const("UV8 ") // depth: 8; description: UV 4:4
)

Chrominance formats

View Source
const (
	Y10BPACK = Const("Y10B") // depth: 10; description: Greyscale bit-packed
)

Grey bit-packed formats

Variables

This section is empty.

Functions

func Uint32

func Uint32(a uint8, b uint8, c uint8, d uint8) uint32

Uint32 turns 4 characters (where characters are uint8), into a (compact) machine readble version of a FOURCC (as an uint32).

If you want stronger protection from the type system, use the FOURCC() func instead.

Example
var colorspace uint32 = fourcc.Uint32('P', 'A', 'L', '8')

// Since:
//
// ASCII 'P' = 0x50
// ASCII 'A' = 0x41
// ASCII 'L' = 0x4C
// ASCII '8' = 0x38
//
// Then, the FOURCC uint32 code, as a hexadecimal, should be those in reverse order.
// I.e.,:
//
// 38,4C,41,50

fmt.Printf("%X", colorspace)
Output:

384C4150

Types

type Const

type Const string

Const exists as a workaround for Go not supporting const structs.

This, you can use this to create a const FOURCC.

func (Const) FOURCC

func (receiver Const) FOURCC() (Type, error)
Example
const (
	PAL8 = fourcc.Const("PAL8")
)

var colorspace fourcc.Type
var err error

colorspace, err = PAL8.FOURCC()
if nil != err {
	fmt.Printf("Problem with FOURCC Const: (%T) %q", err, err)
	return
}

s := colorspace.String()

u32, _ := colorspace.Uint32() // <--- We ignored the error here (to make the example easier to read), but you should NOT do that.

fmt.Println(s)
fmt.Printf("%X", u32)
Output:

PAL8
384C4150

func (Const) Uint32

func (receiver Const) Uint32() (uint32, error)

type Type

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

Type represents the compact way of storing a FOURCC.

The compact way of storing a FOURCC is as a uint32.

Effectively Type is just an uint32.

func FOURCC

func FOURCC(a uint8, b uint8, c uint8, d uint8) Type

FOURCC turns 4 characters (where characters are uint8), into a (compact) machine readble version of a FOURCC.

Example
var colorspace fourcc.Type = fourcc.FOURCC('V', 'P', '8', '0')

s := colorspace.String()

fmt.Println(s)
Output:

VP80

func (Type) MarshalText

func (receiver Type) MarshalText() ([]byte, error)

MarshalText makes Type fit the encoding.TextUnmarshaler interface.

Example
type ScreenInfo struct {
	Width      uint64      `json:"width"`
	Height     uint64      `json:"height"`
	Colorspace fourcc.Type `json:"colorspace"`
}

var info ScreenInfo

info.Width = 640
info.Height = 480
info.Colorspace = fourcc.FOURCC('V', 'P', '8', '0')

data, err := json.Marshal(&info) // calls MarshalText, to convert FOURCC into a text representation.
if nil != err {
	fmt.Printf("Problem creating JSON data: (%T) %q", err, err)
	return
}

fmt.Printf("%s", data)
Output:

{"width":640,"height":480,"colorspace":"VP80"}

func (*Type) Scan

func (receiver *Type) Scan(value interface{}) error

Scan tries to interpret ‘value’ in a way that it can convert it into a Type.

Scan makes Type fit the sql/database.Scanner interface.

Example (Bytes)
var p []byte = []byte{'P', 'A', 'L', '8'}

var colorspace fourcc.Type

colorspace.Scan(p)

s := colorspace.String()

u32, _ := colorspace.Uint32() // <--- We ignored the error here (to make the example easier to read), but you should NOT do that.

fmt.Println(s)
fmt.Printf("%X", u32)
Output:

PAL8
384C4150
Example (Int64)
var i64 int64 = 0x384C4150

var colorspace fourcc.Type

colorspace.Scan(i64)

s := colorspace.String()

u32, _ := colorspace.Uint32() // <--- We ignored the error here (to make the example easier to read), but you should NOT do that.

fmt.Println(s)
fmt.Printf("%X", u32)
Output:

PAL8
384C4150
Example (String)
var colorspace fourcc.Type

colorspace.Scan("PAL8")

s := colorspace.String()

u32, _ := colorspace.Uint32() // <--- We ignored the error here (to make the example easier to read), but you should NOT do that.

fmt.Println(s)
fmt.Printf("%X", u32)
Output:

PAL8
384C4150
Example (Uint32)
var ui32 uint32 = 0x384C4150

var colorspace fourcc.Type

colorspace.Scan(ui32)

s := colorspace.String()

u32, _ := colorspace.Uint32() // <--- We ignored the error here (to make the example easier to read), but you should NOT do that.

fmt.Println(s)
fmt.Printf("%X", u32)
Output:

PAL8
384C4150
Example (Uint64)
var ui64 uint64 = 0x384C4150

var colorspace fourcc.Type

colorspace.Scan(ui64)

s := colorspace.String()

u32, _ := colorspace.Uint32() // <--- We ignored the error here (to make the example easier to read), but you should NOT do that.

fmt.Println(s)
fmt.Printf("%X", u32)
Output:

PAL8
384C4150

func (Type) String

func (receiver Type) String() string

String returns a human readable version of a FOURCC.

Example
var colorspace fourcc.Type = fourcc.FOURCC('P', 'A', 'L', '8')

s := colorspace.String()

fmt.Println(s)
Output:

PAL8

func (Type) Uint32

func (receiver Type) Uint32() (uint32, error)

Uint32 returns a (compact) machine readble version of a FOURCC.

Example
var colorspace fourcc.Type = fourcc.FOURCC('P', 'A', 'L', '8')

// Since:
//
// ASCII 'P' = 0x50
// ASCII 'A' = 0x41
// ASCII 'L' = 0x4C
// ASCII '8' = 0x38
//
// Then, the FOURCC uint32 code, as a hexadecimal, should be those in reverse order.
// I.e.,:
//
// 38,4C,41,50

u32, err := colorspace.Uint32()
if nil != err {
	fmt.Printf("THIS SHOULD NEVER HAPPEN: (%T) %v", err, err)
	return
}

fmt.Printf("%X", u32)
Output:

384C4150

Jump to

Keyboard shortcuts

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