-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmime_cgo.go
92 lines (79 loc) · 2.22 KB
/
mime_cgo.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
package viamrtsp
/*
#cgo pkg-config: libavutil libavcodec
#include <libavcodec/avcodec.h>
#include <libavutil/frame.h>
#include <libavutil/imgutils.h>
#include <stdlib.h>
*/
import "C"
import "unsafe"
// This is a CGO helper file for mime_test.go. We cannot use CGO directly in test files
// so we have to place C interop code here.
func createTestFrame(width, height int, format C.enum_AVPixelFormat) *C.AVFrame {
frame := C.av_frame_alloc()
if frame == nil {
return nil
}
frame.format = C.int(format)
frame.width = C.int(width)
frame.height = C.int(height)
if res := C.av_frame_get_buffer(frame, 32); res < 0 {
// If this fails, free the frame and return nil
C.av_frame_free(&frame)
return nil
}
// Mark frame as writable, so we can safely fill the data arrays.
if res := C.av_frame_make_writable(frame); res < 0 {
C.av_frame_free(&frame)
return nil
}
return frame
}
func createTestYUV420PFrame(width, height int) *C.AVFrame {
return createTestFrame(width, height, C.AV_PIX_FMT_YUV420P)
}
func createTestYUVJ420PFrame(width, height int) *C.AVFrame {
return createTestFrame(width, height, C.AV_PIX_FMT_YUVJ420P)
}
func createInvalidFrame() *C.AVFrame {
frame := C.av_frame_alloc()
if frame == nil {
return nil
}
frame.format = C.AV_PIX_FMT_NONE
frame.width = 0
frame.height = 0
return frame
}
func fillDummyYUV420PData(frame *C.AVFrame) {
width := int(frame.width)
height := int(frame.height)
yPlane := (*[1 << 30]uint8)(unsafe.Pointer(frame.data[0]))[: width*height : width*height]
uPlane := (*[1 << 30]uint8)(unsafe.Pointer(frame.data[1]))[: width*height/4 : width*height/4]
vPlane := (*[1 << 30]uint8)(unsafe.Pointer(frame.data[2]))[: width*height/4 : width*height/4]
// Fill Y plane
for y := range height {
for x := range width {
yPlane[y*int(frame.linesize[0])+x] = 128
}
}
// Fill U plane
for y := range height / 2 {
for x := range width / 2 {
uPlane[y*int(frame.linesize[1])+x] = 64
}
}
// Fill V plane
for y := range height / 2 {
for x := range width / 2 {
vPlane[y*int(frame.linesize[2])+x] = 64
}
}
}
func freeFrame(frame *C.AVFrame) {
C.av_frame_free(&frame)
}
func createTestRGBAFrame(width, height int) *C.AVFrame {
return createTestFrame(width, height, C.AV_PIX_FMT_RGBA)
}