I am trying to use libswscale to scale image before encoding to h264 using cgo. Here I wrote a simple demo(sorry for the bad code style, I just want to do quick verification):
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)
}
}
Everytime I run the code snippet, I got an error saying bad dst image pointers
, what is the problem of my code. I am new to cgo, so the code is probably silly to you, I apology for that.
If you have more elegant way to achieve the functionality, I am all ears. Any suggestion would be appreciated.
swscale expects a two dimensional array. That is a pointer to an array of pointers. Each pointer points to a different plane of the image (y,u,v). You are making a single buffer and passing a pointer to a pointer of that. There is no pointer to the U and V planes given to swscale. Hence bad pointers.