-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpicture.go
203 lines (171 loc) · 4.54 KB
/
picture.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package prores
import (
"encoding/binary"
"fmt"
"image"
"io"
"sync"
)
const MacroblockWidth = 16
const MacroblockHeight = 16
type PictureHeader struct {
HeaderSize int64
NumberOfSlices int
SliceWidthFactor int
SliceHeightFactor int
}
func (h *PictureHeader) SliceWidthMacroblocks() int {
return 1 << uint(h.SliceWidthFactor)
}
func (h *PictureHeader) SliceHeightMacroblocks() int {
return 1 << uint(h.SliceHeightFactor)
}
func (h *PictureHeader) Decode(r io.ReaderAt) error {
var hdrSizeBuf [1]byte
if _, err := r.ReadAt(hdrSizeBuf[:], 0); err != nil {
return err
}
if hdrSizeBuf[0] < 64 {
return fmt.Errorf("picture header size must be at least 64")
} else if hdrSizeBuf[0]%8 != 0 {
return fmt.Errorf("picture header size not divisible by 8")
}
hdrSize := hdrSizeBuf[0] / 8
buf := make([]byte, hdrSize)
if _, err := r.ReadAt(buf, 0); err != nil {
return err
}
decoded := PictureHeader{
HeaderSize: int64(hdrSize),
NumberOfSlices: int(binary.BigEndian.Uint16(buf[5:])),
SliceWidthFactor: int(buf[7] >> 4),
SliceHeightFactor: int(buf[7] & 0x0f),
}
*h = decoded
return nil
}
var ProgressiveScanOrder = []int{
0, 1, 8, 9, 2, 3, 10, 11,
16, 17, 24, 25, 18, 19, 26, 27,
4, 5, 12, 20, 13, 6, 7, 14,
21, 28, 29, 22, 15, 23, 30, 31,
32, 33, 40, 48, 41, 34, 35, 42,
49, 56, 57, 50, 43, 36, 37, 44,
51, 58, 59, 52, 45, 38, 39, 46,
53, 60, 61, 54, 47, 55, 62, 63,
}
var InterlacedScanOrder = []int{
0, 8, 1, 9, 16, 24, 17, 25,
2, 10, 3, 11, 18, 26, 19, 27,
32, 40, 33, 34, 41, 48, 56, 49,
42, 35, 43, 50, 57, 58, 51, 59,
4, 12, 5, 6, 13, 20, 28, 21,
14, 7, 15, 22, 29, 36, 44, 37,
30, 23, 31, 38, 45, 52, 60, 53,
46, 39, 47, 54, 61, 62, 55, 63,
}
type decodeSliceJob struct {
offset int64
x int
y int
width int
dataLen int64
}
type FieldOrder int
const (
FieldOrderFirst FieldOrder = 1
FieldOrderSecond FieldOrder = 2
)
func DecodePicture(r io.ReaderAt, frameHeader *FrameHeader, fieldOrder FieldOrder) (image.Image, error) {
if frameHeader.AlphaInfo.HasAlpha() {
return nil, fmt.Errorf("alpha channels not supported")
}
scanOrder := ProgressiveScanOrder
height := frameHeader.Height
switch frameHeader.Flags.InterlaceMode() {
case InterlaceModeTopFirst:
scanOrder = InterlacedScanOrder
if fieldOrder == FieldOrderFirst {
height = (height + 1) / 2
} else {
height = height / 2
}
case InterlaceModeTopSecond:
scanOrder = InterlacedScanOrder
if fieldOrder == FieldOrderFirst {
height = height / 2
} else {
height = (height + 1) / 2
}
}
widthMacroblocks := (frameHeader.Width + MacroblockWidth - 1) / MacroblockWidth
heightMacroblocks := (height + MacroblockHeight - 1) / MacroblockHeight
img := image.NewYCbCr(image.Rect(0, 0, widthMacroblocks*MacroblockWidth, heightMacroblocks*MacroblockHeight), frameHeader.Flags.SubsampleRatio())
var header PictureHeader
if err := header.Decode(r); err != nil {
return nil, err
}
indexTableBuf := make([]byte, 2*header.NumberOfSlices)
if _, err := r.ReadAt(indexTableBuf, header.HeaderSize); err != nil {
return nil, err
}
sliceHeight := header.SliceHeightMacroblocks() * MacroblockHeight
jobCh := make(chan *decodeSliceJob, header.NumberOfSlices)
errCh := make(chan error, 1)
var wg sync.WaitGroup
const numberOfWorkers = 8
wg.Add(numberOfWorkers)
for i := 0; i < numberOfWorkers; i++ {
go func() {
defer wg.Done()
decoder := NewSliceDecoder()
for {
job := <-jobCh
if job == nil {
return
}
r := io.NewSectionReader(r, job.offset, job.dataLen)
rect := image.Rect(job.x, job.y, job.x+job.width, job.y+sliceHeight).Intersect(img.Bounds())
if err := decoder.DecodeSlice(r, frameHeader, img, rect, scanOrder); err != nil {
select {
case errCh <- err:
default:
}
}
}
}()
}
offset := header.HeaderSize + int64(len(indexTableBuf))
x := 0
y := 0
for i := 0; i < header.NumberOfSlices; i++ {
sliceDataLen := int64(binary.BigEndian.Uint16(indexTableBuf[i*2:]))
sliceWidth := header.SliceWidthMacroblocks() * MacroblockWidth
for sliceWidth > MacroblockWidth && x+sliceWidth > frameHeader.Width {
sliceWidth >>= 1
}
jobCh <- &decodeSliceJob{
offset: offset,
x: x,
y: y,
width: sliceWidth,
dataLen: sliceDataLen,
}
offset += sliceDataLen
x += sliceWidth
if x >= frameHeader.Width {
x = 0
y += sliceHeight
}
}
for i := 0; i < numberOfWorkers; i++ {
jobCh <- nil
}
wg.Wait()
select {
case err := <-errCh:
return nil, err
default:
}
return img.SubImage(image.Rect(0, 0, frameHeader.Width, height)), nil
}