go – libswscale bad dst image pointers cgo

go – libswscale bad dst image pointers cgo

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.

It turns out that both my input and output buffer are wrong, they both need to be two dimensional array as @szatmary mentioned. Here is the working code

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
}

go – libswscale bad dst image pointers cgo

Leave a Reply

Your email address will not be published.