douyin7829
2018-04-19 22:19
浏览 493
已采纳

CGO:如何在Golang中使用指针从C中的数组访问数据

I'm writing an app for the windows platform using FFmpeg and it's golang wrapper goav, but I'm having trouble understanding how to use the C pointers to gain access to the data array they point to.

I'm trying to get the data stored in the AVFrame class and use Go to write it to a file, and eventually a texture in OpenGl to make a video player with cool transitions.

I think understanding how to cast and access the C data will make coding this a lot easier.

I've stripped out all the relevant parts of the C code, the wrapper and my code, shown below:

C code - libavutil/frame.h

#include <stdint.h>

typedef struct AVFrame {
#define AV_NUM_DATA_POINTERS 8
    uint8_t *data[AV_NUM_DATA_POINTERS];
}

Golang goav wrapper - I don't really know whats going on here with the unsafe.Pointers and casting but it gives me access to the underlying C code

package avutil

/*
    #cgo pkg-config: libavutil
    #include <libavutil/frame.h>
    #include <stdlib.h>
*/
import "C"
import (
    "unsafe"
)

type Frame C.struct_AVFrame

func AvFrameAlloc() *Frame {
    return (*Frame)(unsafe.Pointer(C.av_frame_alloc()))
}

func Data(f *Frame) *uint8 {
    return (*uint8)(unsafe.Pointer((*C.uint8_t)(unsafe.Pointer(&f.data))))
}

My Golang code

package main

import "github.com/giorgisio/goav/avutil"

func main() {
    videoFrame := avutil.AvFrameAlloc()

    data := avutil.Data(videoFrame)

    fmt.Println(data) // here i want the values from data[0] to data[7], but how?
}
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题