Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

go - 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*?

question from:https://stackoverflow.com/questions/65646869/how-do-i-convert-c-avframe-to-image-image

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...