new fixed datatypes

This commit is contained in:
Martin Riedl 2024-10-17 18:05:07 +02:00
parent 4292bd5358
commit 2ccceedda9
Signed by: martinr92
GPG key ID: FB68DA65516A804C
2 changed files with 73 additions and 4 deletions

69
Fixed.go Normal file
View file

@ -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
}

View file

@ -58,10 +58,10 @@ type MovieHeaderBox struct {
DurationV1 uint64 DurationV1 uint64
// is a fixed point 16.16 number that indicates the preferred rate to play the presentation; 1.0 // is a fixed point 16.16 number that indicates the preferred rate to play the presentation; 1.0
// (0x00010000) is normal forward playback // (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 // is a fixed point 8.8 number that indicates the preferred playback volume. 1.0 (0x0100) is
// full volume. // full volume.
Volume int16 Volume Fixed88
// provides a transformation matrix for the video; (u,v,w) are restricted here to (0,0,1), hex // provides a transformation matrix for the video; (u,v,w) are restricted here to (0,0,1), hex
// values (0,0,0x40000000). // values (0,0,0x40000000).
Matrix []int32 Matrix []int32
@ -94,8 +94,8 @@ func ParseMovieHeaderBox(filePosition uint64, headerSize uint32, content []byte)
position += 16 position += 16
} }
box.Rate = int32(binary.BigEndian.Uint32(content[position : position+4])) box.Rate = NewFixed1616ByBytes(content[position : position+4])
box.Volume = int16(binary.BigEndian.Uint16(content[position+4 : position+6])) 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 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++ { for i := 0; i < 9; i++ {
box.Matrix = append(box.Matrix, int32(binary.BigEndian.Uint32(content[position:position+4]))) box.Matrix = append(box.Matrix, int32(binary.BigEndian.Uint32(content[position:position+4])))