av/stream/mts/psi/op.go

97 lines
2.2 KiB
Go

/*
NAME
op.go
DESCRIPTION
op.go provides functionality for editing and reading bytes slices
directly in order to insert/read timestamp and gps data in psi.
AUTHOR
Saxon Milton <saxon@ausocean.org>
LICENSE
op.go is Copyright (C) 2018 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
*/
package psi
import (
"encoding/binary"
"errors"
)
func TimeBytes(time uint64) (s []byte) {
s = make([]byte, timeDataSize)
binary.BigEndian.PutUint64(s[:], time)
return s
}
func chkTime(d []byte) error {
if d[timeTagIndx] != timeDescTag {
return errors.New("PSI does not contain a time descriptor, cannot update")
}
return nil
}
func chkGps(d []byte) error {
if d[gpsTagIndx] != gpsDescTag {
return errors.New("PSI does not contain a gps descriptor, cannot update")
}
return nil
}
// Updatetime
func UpdateTime(d []byte, t int) error {
err := chkTime(d)
if err != nil {
return err
}
ts := TimeBytes(uint64(t))
for i := range d[timeDataIndx : timeDataIndx+timeDataSize] {
d[i+timeDataIndx] = ts[i]
}
return nil
}
func TimeOf(d []byte) (t uint64, err error) {
err = chkTime(d)
if err != nil {
return 0, err
}
for i := range d[timeDataIndx : timeDataIndx+timeDataSize] {
t |= uint64(d[i+timeDataIndx]) << uint(56-i*timeDataSize)
}
return t, nil
}
func GpsStrBytes(l string) (out []byte) {
out = make([]byte, gpsDataSize)
copy(out, []byte(l))
return
}
func UpdateGps(d []byte, s string) error {
// First check if there is a GPS descriptor
err := chkGps(d)
if err != nil {
return err
}
gb := GpsStrBytes(s)
for i := range d[gpsDataIndx : gpsDataIndx+gpsDataSize] {
d[i+gpsDataIndx] = gb[i]
}
return nil
}