From 2ccceedda951b28eca118bd865d150959e4934b5 Mon Sep 17 00:00:00 2001 From: Martin Riedl Date: Thu, 17 Oct 2024 18:05:07 +0200 Subject: [PATCH] new fixed datatypes --- Fixed.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++ MovieHeaderBox.go | 8 +++--- 2 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 Fixed.go diff --git a/Fixed.go b/Fixed.go new file mode 100644 index 0000000..d47a912 --- /dev/null +++ b/Fixed.go @@ -0,0 +1,69 @@ +// Copyright 2024 Martin Riedl +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package gomp4 + +import "encoding/binary" + +type Fixed88 struct { + b []byte +} + +func NewFixed88ByBytes(b []byte) Fixed88 { + return Fixed88{ + b: b, + } +} + +func (fixed Fixed88) ToUInt16() uint16 { + return uint16(fixed.b[0]) +} + +func (fixed Fixed88) ToFloat32() float32 { + var result float32 = 0 + + a := uint16(fixed.b[0]) + b := uint16(fixed.b[1]) + + result += float32(a) + result += float32(b) / 1_000 // max of uint8 is 255 + + return result +} + +type Fixed1616 struct { + b []byte +} + +func NewFixed1616ByBytes(b []byte) Fixed1616 { + return Fixed1616{ + b: b, + } +} + +func (fixed Fixed1616) ToUInt16() uint16 { + return binary.BigEndian.Uint16(fixed.b[0:2]) +} + +func (fixed Fixed1616) ToFloat32() float32 { + var result float32 = 0 + + a := binary.BigEndian.Uint16(fixed.b[0:2]) + b := binary.BigEndian.Uint16(fixed.b[2:4]) + + result += float32(a) + result += float32(b) / 100_000 // max of uint16 is 65535 + + return result +} diff --git a/MovieHeaderBox.go b/MovieHeaderBox.go index 1d31f55..680ff6f 100644 --- a/MovieHeaderBox.go +++ b/MovieHeaderBox.go @@ -58,10 +58,10 @@ type MovieHeaderBox struct { DurationV1 uint64 // is a fixed point 16.16 number that indicates the preferred rate to play the presentation; 1.0 // (0x00010000) is normal forward playback - Rate int32 + Rate Fixed1616 // is a fixed point 8.8 number that indicates the preferred playback volume. 1.0 (0x0100) is // full volume. - Volume int16 + Volume Fixed88 // provides a transformation matrix for the video; (u,v,w) are restricted here to (0,0,1), hex // values (0,0,0x40000000). Matrix []int32 @@ -94,8 +94,8 @@ func ParseMovieHeaderBox(filePosition uint64, headerSize uint32, content []byte) position += 16 } - box.Rate = int32(binary.BigEndian.Uint32(content[position : position+4])) - box.Volume = int16(binary.BigEndian.Uint16(content[position+4 : position+6])) + box.Rate = NewFixed1616ByBytes(content[position : position+4]) + box.Volume = NewFixed88ByBytes(content[position+4 : position+6]) position += 4 + 2 + 2 + 8 // 4 bytes for rate, 2 bytes for volume, 2 bytes reserved and 8 bytes reserved for i := 0; i < 9; i++ { box.Matrix = append(box.Matrix, int32(binary.BigEndian.Uint32(content[position:position+4])))