简体   繁体   中英

libswscale bad dst image pointers cgo

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM