在使用cgo编码到h264之前,我正在尝试使用libswscale来缩放图像。在这里我写了一个简单的演示(很抱歉代码风格不好,我只想做快速验证(:
func scale(img []byte, scaleFactor int) {
input, _, _ := image.Decode(bytes.NewReader(img))
if a, ok := input.(*image.YCbCr); ok {
width, height := a.Rect.Dx(), a.Rect.Dy()
var format C.enum_AVPixelFormat = C.AV_PIX_FMT_YUV420P
context := C.sws_getContext(C.int(width), C.int(height), format, C.int(width/scaleFactor), C.int(height/scaleFactor), 0, C.int(0x10), nil, nil, nil)
in := make([]uint8, 0)
in = append(in, a.Y...)
in = append(in, a.Cb...)
in = append(in, a.Cr...)
stride := []C.int{C.int(width), C.int(width / 2), C.int(width / 2), 0}
outstride := []C.int{C.int(width / scaleFactor), C.int(width / scaleFactor / 2), C.int(width / scaleFactor / 2), 0}
out := make([]uint8, width*height/scaleFactor/scaleFactor*3/2)
C.sws_scale(context, (**C.uint8_t)(unsafe.Pointer(&in[0])), (*C.int)(&stride[0]), 0,
C.int(height), (**C.uint8_t)(unsafe.Pointer(&out[0])), (*C.int)(&outstride[0]))
min := image.Point{0, 0}
max := image.Point{width / scaleFactor, height / scaleFactor}
output := image.NewYCbCr(image.Rectangle{Min: min, Max: max}, image.YCbCrSubsampleRatio420)
paneSize := width * height / scaleFactor / scaleFactor
output.Y = out[:paneSize]
output.Cb = out[paneSize : paneSize*5/4]
output.Cr = out[paneSize*5/4:]
opt := jpeg.Options{
Quality: 90,
}
f, _ := os.Create("img.jpeg")
jpeg.Encode(f, output, &opt)
}
}
每次我运行代码片段时,我都会收到一个错误,说bad dst image pointers
,我的代码有什么问题。我是cgo的新手,所以这个代码对你来说可能很傻,我为此道歉。如果你有更优雅的方式来实现功能,我会洗耳恭听。任何建议都将不胜感激。
swscale需要一个二维数组。这是指向指针数组的指针。每个指针指向图像的不同平面(y,u,v(。您正在创建一个缓冲区,并将指针传递给该缓冲区的指针。没有指向swscale的U和V平面的指针。因此指针不正确。
事实证明,我的输入和输出缓冲区都是错误的,它们都需要是@szatmary提到的二维数组。这是的工作代码
func Scale(img []byte, outw, outh int) []byte {
input, _, _ := image.Decode(bytes.NewReader(img))
if a, ok := input.(*image.YCbCr); ok {
width, height := a.Rect.Dx(), a.Rect.Dy()
var format C.enum_AVPixelFormat = C.AV_PIX_FMT_YUV420P
context := C.sws_getContext(C.int(width), C.int(height), format, C.int(outw), C.int(outh), 0, C.int(0x10), nil, nil, nil)
y := (*C.uint8_t)(C.malloc(C.ulong(len(a.Y))))
C.memcpy(unsafe.Pointer(y), unsafe.Pointer(&a.Y[0]), (C.size_t)(len(a.Y)))
cb := (*C.uint8_t)(C.malloc(C.ulong(len(a.Cb))))
C.memcpy(unsafe.Pointer(cb), unsafe.Pointer(&a.Cb[0]), (C.size_t)(len(a.Cb)))
cr := (*C.uint8_t)(C.malloc(C.ulong(len(a.Cr))))
C.memcpy(unsafe.Pointer(cr), unsafe.Pointer(&a.Cr[0]), (C.size_t)(len(a.Cr)))
in := []*C.uint8_t{y, cb, cr}
stride := []C.int{C.int(a.YStride), C.int(a.CStride), C.int(a.CStride), 0}
outstride := []C.int{C.int(outw), C.int(outw / 2), C.int(outw / 2), 0}
paneSize := outw * outh
a := (*C.uint8_t)(C.malloc(C.ulong(paneSize)))
b := (*C.uint8_t)(C.malloc(C.ulong(paneSize >> 2)))
c := (*C.uint8_t)(C.malloc(C.ulong(paneSize >> 2)))
out := []*C.uint8_t{a, b, c}
C.sws_scale(context, (**C.uint8_t)(unsafe.Pointer(&in[0])), (*C.int)(&stride[0]), 0,
C.int(height), (**C.uint8_t)(unsafe.Pointer(&out[0])), (*C.int)(&outstride[0]))
min := image.Point{0, 0}
max := image.Point{outw, outh}
output := image.NewYCbCr(image.Rectangle{Min: min, Max: max}, image.YCbCrSubsampleRatio420)
C.memcpy(unsafe.Pointer(&output.Y[0]), unsafe.Pointer(a), (C.size_t)(paneSize))
C.memcpy(unsafe.Pointer(&output.Cb[0]), unsafe.Pointer(b), (C.size_t)(paneSize>>2))
C.memcpy(unsafe.Pointer(&output.Cr[0]), unsafe.Pointer(c), (C.size_t)(paneSize>>2))
opt := jpeg.Options{
Quality: 75,
}
var buf bytes.Buffer
w := bufio.NewWriter(&buf)
jpeg.Encode(w, output, &opt)
return buf.Bytes()
}
return nil
}