-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpx_writer.go
74 lines (65 loc) · 1.76 KB
/
vpx_writer.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
package gdesk
import (
"os"
"encoding/binary"
"bytes"
"io"
)
type VpxWriter struct {
file *os.File
cfg *EncoderCfg
count int
}
const VP8_FOURCC = 0x30385056
const VP9_FOURCC = 0x30395056
func NewVpxWriter(fileName string, cfg *EncoderCfg) (*VpxWriter, error) {
var writer VpxWriter
var err error
if writer.file, err = os.Create(fileName); err != nil {
return nil, err
}
writer.cfg = cfg
writer.writeHeader()
return &writer, nil
}
func (w *VpxWriter) writeHeader() {
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, []byte("DKIF"))
binary.Write(buf, binary.LittleEndian, uint16(0))
binary.Write(buf, binary.LittleEndian, uint16(32))
var fourcc int32 = VP8_FOURCC
if w.cfg.Codec == VP9 {
fourcc = VP9_FOURCC
}
var cfg = w.cfg
binary.Write(buf, binary.LittleEndian, fourcc)
binary.Write(buf, binary.LittleEndian, uint16(cfg.Width))
binary.Write(buf, binary.LittleEndian, uint16(cfg.Height))
binary.Write(buf, binary.LittleEndian, uint32(cfg.Denominator))
binary.Write(buf, binary.LittleEndian, uint32(cfg.Numerator))
binary.Write(buf, binary.LittleEndian, uint32(w.count))
binary.Write(buf, binary.LittleEndian, uint32(0))
w.file.Write(buf.Bytes())
}
func (w *VpxWriter) writeFrameHeader(len int, pts uint64) {
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, uint32(len))
binary.Write(buf, binary.LittleEndian, uint32(pts&0xFFFFFFFF))
binary.Write(buf, binary.LittleEndian, uint32(pts>>32))
w.file.Write(buf.Bytes())
}
func (w *VpxWriter) WriteFrame(f *EncodeFrame) {
w.writeFrameHeader(len(f.Data), f.Pts)
w.file.Write(f.Data)
w.count++
}
func (w *VpxWriter) Close() {
if w.file != nil {
if _, err := w.file.Seek(0, io.SeekStart); err != nil {
panic(err)
}
w.writeHeader()
w.file.Close()
w.file = nil
}
}