forked from bluenviron/gortsplib
/
header_jpeg.go
48 lines (39 loc) · 1.06 KB
/
header_jpeg.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
package rtpmjpeg
import (
"fmt"
)
type headerJPEG struct {
TypeSpecific uint8
FragmentOffset uint32
Type uint8
Quantization uint8
Width int
Height int
}
func (h *headerJPEG) unmarshal(byts []byte) (int, error) {
if len(byts) < 8 {
return 0, fmt.Errorf("buffer is too short")
}
h.TypeSpecific = byts[0]
h.FragmentOffset = uint32(byts[1])<<16 | uint32(byts[2])<<8 | uint32(byts[3])
h.Type = byts[4]
if h.Type != 1 {
return 0, fmt.Errorf("Type %d is not supported", h.Type)
}
h.Quantization = byts[5]
if h.Quantization != 255 {
return 0, fmt.Errorf("Q %d is not supported", h.Quantization)
}
h.Width = int(byts[6]) * 8
h.Height = int(byts[7]) * 8
return 8, nil
}
func (h headerJPEG) marshal(byts []byte) []byte {
byts = append(byts, h.TypeSpecific)
byts = append(byts, []byte{byte(h.FragmentOffset >> 16), byte(h.FragmentOffset >> 8), byte(h.FragmentOffset)}...)
byts = append(byts, h.Type)
byts = append(byts, h.Quantization)
byts = append(byts, byte(h.Width/8))
byts = append(byts, byte(h.Height/8))
return byts
}