/
vpx_decoder.go
282 lines (243 loc) · 5.9 KB
/
vpx_decoder.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package vpx
// #cgo LDFLAGS: -lvpx
// #include <stdlib.h>
// #include <stdint.h>
// #include <vpx/vpx_decoder.h>
// #include <vpx/vpx_image.h>
// #include <vpx/vp8dx.h>
//
// vpx_codec_iface_t *ifaceVP8() {
// return vpx_codec_vp8_dx();
// }
// vpx_codec_iface_t *ifaceVP9() {
// return vpx_codec_vp9_dx();
// }
// vpx_codec_ctx_t *newCtx() {
// return malloc(sizeof(vpx_codec_ctx_t));
// }
//
// void yuv420_to_rgb(uint32_t width, uint32_t height,
// const uint8_t *y, const uint8_t *u, const uint8_t *v,
// int ystride, int ustride, int vstride,
// uint8_t *out, int fmt)
// {
// unsigned long int i, j;
// for (i = 0; i < height; ++i) {
// for (j = 0; j < width; ++j) {
// uint8_t *point = out + 4 * ((i * width) + j);
// int t_y = y[((i * ystride) + j)];
// int t_u = u[(((i / 2) * ustride) + (j / 2))];
// int t_v = v[(((i / 2) * vstride) + (j / 2))];
// t_y = t_y < 16 ? 16 : t_y;
//
// int r = (298 * (t_y - 16) + 409 * (t_v - 128) + 128) >> 8;
// int g = (298 * (t_y - 16) - 100 * (t_u - 128) - 208 * (t_v - 128) + 128) >> 8;
// int b = (298 * (t_y - 16) + 516 * (t_u - 128) + 128) >> 8;
//
// if (fmt == 0 /* RGBA */) {
// point[0] = r>255? 255 : r<0 ? 0 : r;
// point[1] = g>255? 255 : g<0 ? 0 : g;
// point[2] = b>255? 255 : b<0 ? 0 : b;
// point[3] = ~0;
// } else { /* BGRA */
// point[0] = b>255? 255 : b<0 ? 0 : b;
// point[1] = g>255? 255 : g<0 ? 0 : g;
// point[2] = r>255? 255 : r<0 ? 0 : r;
// point[3] = ~0;
// }
// }
// }
// }
import "C"
import (
"errors"
"fmt"
"image"
"log"
"reflect"
"strings"
"sync"
"unsafe"
"github.com/pion/rtp"
"github.com/pion/rtp/codecs"
"github.com/pion/webrtc/v2/pkg/media"
"github.com/pion/webrtc/v2/pkg/media/samplebuilder"
)
type ColorFormat int
const (
ColorRGBA ColorFormat = iota
ColorBGRA
)
type VpxFrame struct {
IsKeyframe bool
image *C.vpx_image_t
}
func (f *VpxFrame) Width() uint32 {
return uint32(f.image.d_w)
}
func (f *VpxFrame) Height() uint32 {
return uint32(f.image.d_h)
}
func (f *VpxFrame) Plane(n int) []byte {
var p *C.uchar
switch n {
case 0:
p = f.image.planes[C.VPX_PLANE_Y]
case 1:
p = f.image.planes[C.VPX_PLANE_U]
case 2:
p = f.image.planes[C.VPX_PLANE_V]
}
lenData := f.Width() * f.Height()
return (*[1 << 30]byte)(unsafe.Pointer(p))[:lenData:lenData]
}
func (f *VpxFrame) Stride(n int) int {
switch n {
case 0:
return int(f.image.stride[C.VPX_PLANE_Y])
case 1:
return int(f.image.stride[C.VPX_PLANE_U])
case 2:
return int(f.image.stride[C.VPX_PLANE_V])
default:
return -1
}
}
func (f *VpxFrame) ToBytes(format ColorFormat) []uint8 {
img := f.image
out := make([]uint8, img.d_w*img.d_h*4)
C.yuv420_to_rgb(
img.d_w,
img.d_h,
img.planes[C.VPX_PLANE_Y],
img.planes[C.VPX_PLANE_U],
img.planes[C.VPX_PLANE_V],
img.stride[C.VPX_PLANE_Y],
img.stride[C.VPX_PLANE_U],
img.stride[C.VPX_PLANE_V],
(*C.uint8_t)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&out)).Data)),
(C.int)(format),
)
return out
}
func (f *VpxFrame) ToRGBA() *image.RGBA {
out := f.ToBytes(ColorRGBA)
return &image.RGBA{
Pix: out,
Stride: int(f.image.d_w) * 4,
Rect: image.Rect(0, 0, int(f.image.d_w), int(f.image.d_h)),
}
}
type VpxDecoder struct {
codec *C.vpx_codec_ctx_t
iface *C.vpx_codec_iface_t
format string
mu sync.Mutex
closed bool
}
func NewDecoder(format string) (*VpxDecoder, error) {
iface := (*C.vpx_codec_iface_t)(nil)
f := strings.ToUpper(format)
switch f {
case "VP8":
iface = C.ifaceVP8()
case "VP9":
iface = C.ifaceVP9()
default:
return nil, fmt.Errorf("Invalid format: %s", format)
}
ctx := C.newCtx()
d := &VpxDecoder{
codec: ctx,
iface: iface,
format: f,
closed: false,
}
log.Printf("Using %s\n", C.GoString(C.vpx_codec_iface_name(iface)))
err := d.init()
if err != nil {
d.Close()
return nil, err
}
return d, nil
}
func (d *VpxDecoder) NewSampleBuilder() *samplebuilder.SampleBuilder {
var depacketizer rtp.Depacketizer = nil
switch d.format {
case "VP8":
depacketizer = &codecs.VP8Packet{}
case "VP9":
depacketizer = &codecs.VP9Packet{}
}
return samplebuilder.New(10, depacketizer)
}
func (d *VpxDecoder) Process(src <-chan *media.Sample, out chan<- VpxFrame) {
if d.closed {
return
}
defer close(out)
receiveFirstKeyFrame := false
for pkt := range src {
isKeyframe := (pkt.Data[0]&0x1 == 0)
if !isKeyframe && !receiveFirstKeyFrame {
continue
}
if isKeyframe && !receiveFirstKeyFrame {
receiveFirstKeyFrame = true
}
err := d.decode(pkt)
if err != nil {
log.Println("[WARN]", err)
continue
}
var iter C.vpx_codec_iter_t
img := C.vpx_codec_get_frame(d.codec, &iter)
for img != nil {
out <- VpxFrame{
image: img,
IsKeyframe: isKeyframe,
}
img = C.vpx_codec_get_frame(d.codec, &iter)
}
}
}
func (d *VpxDecoder) Close() error {
d.mu.Lock()
defer d.mu.Unlock()
if d.closed {
return nil
}
d.closed = true
defer C.free(unsafe.Pointer(d.codec))
if C.vpx_codec_destroy(d.codec) != 0 {
return errors.New("vpx_codec_destroy failed")
}
return nil
}
func (d *VpxDecoder) init() error {
d.mu.Lock()
defer d.mu.Unlock()
if d.closed {
return errors.New("decoder is already closed")
}
ret := C.vpx_codec_dec_init_ver(d.codec, d.iface, nil, 0, C.VPX_DECODER_ABI_VERSION)
if ret != 0 {
return d.handleError("Failed to initialize decoder.")
}
return nil
}
func (d *VpxDecoder) decode(sample *media.Sample) error {
p := (*reflect.SliceHeader)(unsafe.Pointer(&sample.Data))
data := (*C.uchar)(unsafe.Pointer(p.Data))
size := (C.uint)(len(sample.Data))
ret := C.vpx_codec_decode(d.codec, data, size, nil, 0)
if ret != 0 {
return d.handleError("Failed to decode frame.")
}
return nil
}
func (d *VpxDecoder) handleError(msg string) error {
code := C.GoString(C.vpx_codec_error(d.codec))
detail := C.GoString(C.vpx_codec_error_detail(d.codec))
return fmt.Errorf("%s %s : %s", msg, code, detail)
}