geeeek
geeeek

Reputation: 415

How do I convert C.AVFrame* to image.Image?

I have C.AVFrame* raw image.

After converting from C.AVFrame* to jpg, I am dealing with Go images after jpg decoding.

Is there any way to create Go image directly from C.AVFrame*?

Upvotes: 1

Views: 396

Answers (1)

maxm
maxm

Reputation: 3667

Here is a good place to start: https://github.com/nareix/joy4/blob/05a4ffbb53695aaacf9a2e2624472686280ab6dc/cgo/ffmpeg/video.go#L64-L88

Once you have the *C.AVFrame as frame you could:

func fromCPtr(buf unsafe.Pointer, size int) (ret []uint8) {
    hdr := (*reflect.SliceHeader)((unsafe.Pointer(&ret)))
    hdr.Cap = size
    hdr.Len = size
    hdr.Data = uintptr(buf)
    return
}


w := int(frame.width)
h := int(frame.height)
ys := int(frame.linesize[0])
cs := int(frame.linesize[1])

img = image.YCbCr{
    Y: fromCPtr(unsafe.Pointer(frame.data[0]), ys*h),
    Cb: fromCPtr(unsafe.Pointer(frame.data[1]), cs*h/2),
    Cr: fromCPtr(unsafe.Pointer(frame.data[2]), cs*h/2),
    YStride: ys,
    CStride: cs,
    SubsampleRatio: image.YCbCrSubsampleRatio420,
    Rect: image.Rect(0, 0, w, h),
}

To populate the image in an image.YCbCr which implements image.Image

Upvotes: 2

Related Questions