libswscale 错误的 dst 图像指针 cgo
libswscale bad dst image pointers cgo
我正在尝试使用 libswscale 在使用 cgo 编码为 h264 之前缩放图像。在这里我写了一个简单的demo(不好意思的代码风格,我只是想快速验证一下):
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
}
我正在尝试使用 libswscale 在使用 cgo 编码为 h264 之前缩放图像。在这里我写了一个简单的demo(不好意思的代码风格,我只是想快速验证一下):
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
}