cptv

package module
v0.0.0-...-ae7134e Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2020 License: Apache-2.0 Imports: 13 Imported by: 2

README

go-cptv

Build Status

This package implements a Go package for generating and consuming Cacophony Project Thermal Video (CPTV) files. For more details on these files see the specifications:

Example Usage

Writing CPTV Files

import (
    "github.com/TheCacophonyProject/go-cptv"
    "github.com/TheCacophonyProject/go-cptv/cptvframe"
)

type TestCamera struct {
}

func (cam *TestCamera) ResX() int {
    return 200
}

func (cam *TestCamera) ResY() int {
    return 20
}

func writeFrames(frames []*cptvframe.Frame) error {
    camera := new(TestCamera)
    w := cptv.NewFileWriter("out.cptv", camera)
    defer w.Close()
    err := w.WriterHeader("device-name")
    if err != nil {
        return err
    }
    for _, frame := range frames {
        err := w.WriteFrame(frame)
        if err != nil {
            return err
        }
    }
    return nil
}
Reading CPTV Files

See cptvtool for a read example.

Documentation

Index

Constants

View Source
const (
	HeaderSection = 'H'
	FrameSection  = 'F'

	// Header field keys
	Timestamp    byte = 'T'
	XResolution  byte = 'X'
	YResolution  byte = 'Y'
	Compression  byte = 'C'
	DeviceName   byte = 'D'
	DeviceID     byte = 'I'
	MotionConfig byte = 'M'
	PreviewSecs  byte = 'P'
	Latitude     byte = 'L'
	Longitude    byte = 'O'
	LocTimestamp byte = 'S'
	Altitude     byte = 'A'
	Accuracy     byte = 'U'
	FPS          byte = 'Z'
	Model        byte = 'E'
	Brand        byte = 'B'
	Firmware     byte = 'V'
	CameraSerial byte = 'N'
	// Frame field keys
	TimeOn          byte = 't'
	BitWidth        byte = 'w'
	FrameSize       byte = 'f'
	LastFFCTime     byte = 'c'
	TempC           byte = 'a'
	LastFFCTempC    byte = 'b'
	BackgroundFrame byte = 'g'
)

Variables

This section is empty.

Functions

func PackBits

func PackBits(width uint8, input []int32, w io.ByteWriter)

PackBits takes a slice of signed integers and packs them into an abitrary (smaller) bit width. The most significant bit is written out first.

Types

type BitUnpacker

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

BitUnpacker extracts signed integers, packed at some bit width, from a bitstream.

func NewBitUnpacker

func NewBitUnpacker(width uint8, r io.ByteReader) *BitUnpacker

NewBitUnpacker creates a new BitUnpacker. Integers will be extracted from the ByteReader and are expected to be packed at the bit width specified.

func (*BitUnpacker) Next

func (u *BitUnpacker) Next() (int32, error)

Next returns the next signed integer from the bitstream.

type Builder

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

Builder handles the low-level construction of CPTV sections and fields. See Writer for a higher-level interface.

func NewBuilder

func NewBuilder(w io.Writer) *Builder

NewBuilder returns a new Builder instance, ready to emit a gzip compressed CPTV file to the provided Writer.

func (*Builder) Close

func (b *Builder) Close() error

Close closes the current Writer

func (*Builder) WriteFrame

func (b *Builder) WriteFrame(f *FieldWriter, frameData []byte) error

WriteFrame writes a CPTV frame to the current Writer

func (*Builder) WriteHeader

func (b *Builder) WriteHeader(f *FieldWriter) error

WriteHeader writes a CPTV header to the current Writer

type ByteReaderReader

type ByteReaderReader interface {
	io.Reader
	io.ByteReader
}

ByteReaderReader combines io.Reader and io.ByteReader.

type Compressor

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

Compressor generates a compressed representation of successive Frames, returning CPTV frames.

func NewCompressor

func NewCompressor(c cptvframe.CameraSpec) *Compressor

NewCompressor creates a new Compressor.

func (*Compressor) Next

func (c *Compressor) Next(curr *cptvframe.Frame) (uint8, []byte)

Next takes the next Frame in a recording and converts it to a compressed stream of bytes. The bit width used for packing is also returned (this is required for unpacking).

IMPORTANT: The returned byte slice is reused and therefore is only valid until the next call to Next.

type Decompressor

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

Decompressor is used to decompress successive CPTV frames. See the Next() method.

func NewDecompressor

func NewDecompressor(c cptvframe.CameraSpec) *Decompressor

NewDecompressor creates a new Decompressor.

func (*Decompressor) Next

func (d *Decompressor) Next(bitWidth uint8, compressed ByteReaderReader, out *cptvframe.Frame) error

Next reads of stream of bytes as a ByteReaderReader and decompresses them using the bit width provided into the Frame provided.

type FieldWriter

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

FieldWriter generates CPTV encoded fields.

func NewFieldWriter

func NewFieldWriter() *FieldWriter

NewFieldWriter creates a new FieldWriter

func (*FieldWriter) Bytes

func (f *FieldWriter) Bytes() ([]byte, int)

Bytes returns the encoded header and the number of fields represented.

func (*FieldWriter) Float32

func (f *FieldWriter) Float32(code byte, v float32)

Float32 writes a float32 field with key 'code' and value 'v'

func (*FieldWriter) String

func (f *FieldWriter) String(code byte, v string) error

String writes a character string field with key 'code' and value 'v'

func (*FieldWriter) Timestamp

func (f *FieldWriter) Timestamp(code byte, t time.Time)

Timestamp writes a time field with key 'code' and value 'v'

func (*FieldWriter) Uint16

func (f *FieldWriter) Uint16(code byte, v uint16)

Uint16 writes a uint16 field with key 'code' and value 'v'

func (*FieldWriter) Uint32

func (f *FieldWriter) Uint32(code byte, v uint32)

Uint32 writes a uint32 field with key 'code' and value 'v'

func (*FieldWriter) Uint64

func (f *FieldWriter) Uint64(code byte, v uint64)

Uint64 writes a uint64 field with key 'code' and value 'v'

func (*FieldWriter) Uint8

func (f *FieldWriter) Uint8(code byte, v uint8)

Uint8 writes a uint8 field with key 'code' and value 'v'

type Fields

type Fields map[byte][]byte

Fields maps from field key -> field data

func ReadFields

func ReadFields(r io.Reader) (Fields, error)

ReadFields reads the fields for a CPTV section, returning a new Fields instance.

func (Fields) FPS

func (f Fields) FPS() int

func (Fields) Float32

func (f Fields) Float32(key byte) (float32, error)

Float32 returns the field at 'key' as a float32

func (Fields) ResX

func (f Fields) ResX() int

func (Fields) ResY

func (f Fields) ResY() int

func (Fields) String

func (f Fields) String(key byte) (string, error)

String returns the field at 'key' as a character string

func (Fields) Timestamp

func (f Fields) Timestamp(key byte) (time.Time, error)

Timestamp returns the field at 'key' as a time value

func (Fields) Uint16

func (f Fields) Uint16(key byte) (uint16, error)

Uint16 returns the field at 'key' as a uint16

func (Fields) Uint32

func (f Fields) Uint32(key byte) (uint32, error)

Uint32 returns the field at 'key' as a uint32

func (Fields) Uint64

func (f Fields) Uint64(key byte) (uint64, error)

Uint64 returns the field at 'key' as a uint64

func (Fields) Uint8

func (f Fields) Uint8(key byte) (uint8, error)

Uint8 returns the field at 'key' as a uint8

type FileReader

type FileReader struct {
	*Reader
	// contains filtered or unexported fields
}

FileReader wraps a Reader and provides a convenient way of reading a CPTV stream from a disk file.

func NewFileReader

func NewFileReader(filename string) (*FileReader, error)

NewFileReader returns a new FileReader from the filename.

func (*FileReader) Close

func (fr *FileReader) Close()

Close closes the file

func (*FileReader) Name

func (fr *FileReader) Name() string

Name returns the name of the file being read

type FileWriter

type FileWriter struct {
	*Writer
	// contains filtered or unexported fields
}

FileWriter wraps a Writer and provides a convenient way of writing a CPTV stream to a disk file.

func NewFileWriter

func NewFileWriter(filename string, c cptvframe.CameraSpec) (*FileWriter, error)

NewFileWriter creates file 'filename' and returns a new FileWriter with underlying buffer (bufio) Writer

func (*FileWriter) Close

func (fw *FileWriter) Close()

Close flushes the buffered writer and closes the open file

func (*FileWriter) Name

func (fw *FileWriter) Name() string

Name returns the name of the open File

type Header struct {
	Timestamp       time.Time
	DeviceName      string
	DeviceID        int
	CameraSerial    int
	Firmware        string
	PreviewSecs     int
	MotionConfig    string
	Latitude        float32
	Longitude       float32
	LocTimestamp    time.Time
	Altitude        float32
	Accuracy        float32
	FPS             int
	Brand           string
	Model           string
	BackgroundFrame *cptvframe.Frame
}

Header defines the information stored in the header of a CPTV file. All the fields are optional.

type Parser

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

Parser is the low-level type for pulling apart the sections and fields of a CPTV file. See Reader for a high-level interface.

func NewParser

func NewParser(r io.Reader) (*Parser, error)

NewParser returns a new Parser instance, for parsing a gzip compressed CPTV stream using the provided io.Reader.

Providing a buffered Reader is preferable.

func (*Parser) Frame

func (p *Parser) Frame() (Fields, io.Reader, error)

Frame parses a CPTV frame section header from the open file and returns a subreader that allows access to the frame bytes.

func (*Parser) Header

func (p *Parser) Header() (Fields, error)

Header parses a CPTV file header from the open file.

type Reader

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

Reader uses a Parser and Decompressor to read CPTV recordings.

func NewReader

func NewReader(r io.Reader) (*Reader, error)

NewReader returns a new Reader from the io.Reader given.

func (*Reader) Accuracy

func (r *Reader) Accuracy() float32

Accuracy returns the estimated accuracy of the location setting of the device when this CPTV file was recorded. Returns 0 if the field is not included.

func (*Reader) Altitude

func (r *Reader) Altitude() float32

Altitude returns the altitude part of the location of the device when this CPTV file was recorded. Returns 0 if the field is not included.

func (*Reader) BrandName

func (r *Reader) BrandName() string

header returns the camera brand name field from the CPTV recording. Returns an empty string if the brand name field wasn't present.

func (*Reader) DeviceID

func (r *Reader) DeviceID() int

DeviceID returns the device id field from the CPTV recording. Returns an empty int if the device id field wasn't present.

func (*Reader) DeviceName

func (r *Reader) DeviceName() string

DeviceName returns the device name field from the CPTV recording. Returns an empty string if the device name field wasn't present.

func (*Reader) EmptyFrame

func (r *Reader) EmptyFrame() *cptvframe.Frame

EmptyFrame returns an initialized cptvframe.Frame sized accordingly to the CPTV file frames.

func (*Reader) FPS

func (r *Reader) FPS() int

func (*Reader) FirmwareVersion

func (r *Reader) FirmwareVersion() string

Get the firmware version of the camera module, if present

func (*Reader) FrameCount

func (r *Reader) FrameCount() (int, error)

FrameCount returns the remaining number of frames in a CPTV file. After this call, all remaining frames will have been consumed.

func (*Reader) HasBackgroundFrame

func (r *Reader) HasBackgroundFrame() bool

func (*Reader) Latitude

func (r *Reader) Latitude() float32

Latitude returns the latitude part of the location of the device when this CPTV file was recorded. Returns 0 if the field is not included.

func (*Reader) LocTimestamp

func (r *Reader) LocTimestamp() time.Time

LocTimestamp returns the timestamp at which the location of the device. Returns the nil time.Time value if the field is not included.

func (*Reader) Longitude

func (r *Reader) Longitude() float32

Longitude returns the longitude part of the location of the device when this CPTV file was recorded. Returns 0 if the field is not included.

func (*Reader) ModelName

func (r *Reader) ModelName() string

Model returns the camera model name field from the CPTV recording. Returns an empty string if the model name field wasn't present.

func (*Reader) MotionConfig

func (r *Reader) MotionConfig() string

MotionConfig returns the YAML configuration for the motion detector that was in use when this CPTV file was recorded. Returns an empty string if this field is not included.

func (*Reader) PreviewSecs

func (r *Reader) PreviewSecs() int

PreviewSecs returns the number of seconds included in the recording before motion was detected. Returns 0 if this field is not included.

func (*Reader) ReadFrame

func (r *Reader) ReadFrame(out *cptvframe.Frame) error

ReadFrame extracts and decompresses the next frame in a CPTV recording. At the end of the recording an io.EOF error will be returned.

func (*Reader) ResX

func (r *Reader) ResX() int

ResX returns the x resolution of the CPTV file.

func (*Reader) ResY

func (r *Reader) ResY() int

ResY returns the y resolution of the CPTV file.

func (*Reader) SerialNumber

func (r *Reader) SerialNumber() int

Get the camera module serial number if present

func (*Reader) Timestamp

func (r *Reader) Timestamp() time.Time

Timestamp returns the CPTV timestamp. A zero time is returned if the field wasn't present (shouldn't happen).

func (*Reader) Version

func (r *Reader) Version() int

Version returns the version number of the CPTV file.

type Writer

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

Writer uses a Builder and Compressor to create CPTV files.

func NewWriter

func NewWriter(w io.Writer, c cptvframe.CameraSpec) *Writer

NewWriter creates and returns a new Writer component

func (*Writer) Close

func (w *Writer) Close() error

Close closes the CPTV file

func (*Writer) WriteFrame

func (w *Writer) WriteFrame(frame *cptvframe.Frame) error

WriteFrame writes a CPTV frame

func (*Writer) WriteHeader

func (w *Writer) WriteHeader(header Header) error

WriteHeader writes a CPTV file header

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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