forked from andrewzeneski/mp4
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mdhd.go
96 lines (87 loc) · 2.66 KB
/
mdhd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package mp4
import (
"encoding/binary"
"fmt"
"io"
"time"
)
// MdhdBox - Media Header Box (mdhd - mandatory)
//
// Contained in : Media Box (mdia)
//
// Status : only version 0 is decoded. version 1 is not supported
//
// Timescale defines the timescale used for tracks.
// Language is a ISO-639-2/T language code stored as 1bit padding + [3]int5
type MdhdBox struct {
Version byte
Flags [3]byte
CreationTime uint64
ModificationTime uint64
Timescale uint32
Duration uint64
Language uint16
}
// DecodeMdhd decodes mdhd
func DecodeMdhd(r io.Reader, size uint64) (Box, error) {
data, err := read(r, size)
if err != nil {
return nil, err
}
mdhd := &MdhdBox{
Version: data[0],
Flags: [3]byte{data[1], data[2], data[3]},
}
if mdhd.Version == 1 {
mdhd.CreationTime = binary.BigEndian.Uint64(data[4:12])
mdhd.ModificationTime = binary.BigEndian.Uint64(data[12:20])
mdhd.Timescale = binary.BigEndian.Uint32(data[20:24])
mdhd.Duration = binary.BigEndian.Uint64(data[24:32])
mdhd.Language = binary.BigEndian.Uint16(data[32:34])
} else {
mdhd.Version = 0
mdhd.CreationTime = uint64(binary.BigEndian.Uint32(data[4:8]))
mdhd.ModificationTime = uint64(binary.BigEndian.Uint32(data[8:12]))
mdhd.Timescale = binary.BigEndian.Uint32(data[12:16])
mdhd.Duration = uint64(binary.BigEndian.Uint32(data[16:20]))
mdhd.Language = binary.BigEndian.Uint16(data[20:22])
}
return mdhd, nil
}
// Type returns mdhd
func (b *MdhdBox) Type() string {
return "mdhd"
}
// Size returns the size of the mdhd
func (b *MdhdBox) Size() uint64 {
return uint64(24 + uint64(b.Version)*24)
}
// Dump dumps the mdhd
func (b *MdhdBox) Dump() {
fmt.Printf("Media Header:\n Timescale: %d units/sec\n Duration: %d units (%s)\n", b.Timescale, b.Duration, time.Duration(b.Duration/uint64(b.Timescale))*time.Second)
}
// Encode encodes
func (b *MdhdBox) Encode(w io.Writer) error {
err := EncodeHeader(b, w)
if err != nil {
return err
}
buf := makebuf(b)
buf[0] = b.Version
buf[1], buf[2], buf[3] = b.Flags[0], b.Flags[1], b.Flags[2]
if b.Version == 1 {
binary.BigEndian.PutUint64(buf[4:], b.CreationTime)
binary.BigEndian.PutUint64(buf[12:], b.ModificationTime)
binary.BigEndian.PutUint32(buf[20:], b.Timescale)
binary.BigEndian.PutUint64(buf[24:], b.Duration)
binary.BigEndian.PutUint16(buf[32:], b.Language)
} else {
binary.BigEndian.PutUint32(buf[4:], uint32(b.CreationTime))
binary.BigEndian.PutUint32(buf[8:], uint32(b.ModificationTime))
binary.BigEndian.PutUint32(buf[12:], b.Timescale)
binary.BigEndian.PutUint32(buf[16:], uint32(b.Duration))
binary.BigEndian.PutUint16(buf[20:], b.Language)
}
_, err = w.Write(buf)
return err
}