如何从Golang访问C指针数组

我正在使用FFmpeg编写一个用于Windows平台的应用程序,它是golang包装器goav,但我无法理解如何使用C指针来获取对数组的访问权限。

我正在尝试将存储在AVFormatContext类中的流用于go,并最终将帧添加到OpenGl中的纹理以使video播放器具有很酷的过渡。

我认为理解如何投射和访问C数据将使编码变得更加容易。

我已经删除了C代码的所有相关部分,包装器和我的代码,如下所示:

C代码 – libavformat / avformat.h

typedef struct AVFormatContext { unsigned int nb_streams; AVStream **streams; } 

Golang goav包装纸

 package avutil //#cgo pkg-config: libavformat //#include  import "C" import ( "unsafe" ) type Context C.struct_AVFormatContext; func (ctxt *Context) StreamsGet(i uintptr) *Stream { streams := (**Stream)(unsafe.Pointer(ctxt.streams)); // I think this is where it's going wrong, I'm brand new to this stuff return (*Stream)(unsafe.Pointer(uintptr(unsafe.Pointer(streams)) + i*unsafe.Sizeof(*streams))); } 

我的Golang代码

 package main import "github.com/giorgisio/goav/avformat" func main() { ctx := &avformat.Context{} // the actual function to initiate this does an mallocz for the streams stream := ctx.StreamsGet(0) //do stuff with stream... } 

在C中看起来我只需要只做stream [i],但是在go中不能工作,所以我使用我的问题中的技术向包装器添加了一个函数。 但是我没有收到数据; 看起来我正在指向内存中随机的某个地方。 那么,我如何从golang访问这些元素? 任何资源也会有所帮助; 我将在这方面投入相当多的时间。

如您所知,问题出现在以下代码中:

 func (ctxt *Context) StreamsGet(i uintptr) *Stream { streams := (**Stream)(unsafe.Pointer(ctxt.streams)); // I think this is where it's going wrong, I'm brand new to this stuff return (*Stream)(unsafe.Pointer(uintptr(unsafe.Pointer(streams)) + i*unsafe.Sizeof(*streams))); } 

在代码中,变量streams双指针 ,因此向streams添加偏移量的结果也是双指针(即类型是**Stream )。 但是,在你的片段中,它被转换为*Stream ,这是不正确的。 正确的代码是:

 func (ctxt *Context) StreamsGet(i uintptr) *Stream { streams := (**Stream)(unsafe.Pointer(ctxt.streams)) // Add offset i then cast it to **Stream ptrPtr := (**Stream)(unsafe.Pointer(uintptr(unsafe.Pointer(streams)) + i*unsafe.Sizeof(*streams))) return *ptrPtr } 

附加说明:
如果你想在Go端避免指针运算,你可以定义一个辅助函数来访问C端的指针元素(即流),如下所示:

 /* void * ptr_at(void **ptr, int idx) { return ptr[idx]; } struct AVStream * stream_at(struct AVFormatContext *c, int idx) { if (i >= 0 && i < c->nb_streams) return c->streams[idx]; return NULL; } */ import "C" import ( "unsafe" ) type Context C.struct_AVFormatContext type Stream C.struct_AVStream func (ctx *Context) StreamAt(i int) *Stream { p := (*unsafe.Pointer)(unsafe.Pointer(ctx.streams)) ret := C.ptr_at(p, C.int(i)) return (*Stream)(ret) } func (ctx *Context) StreamAt2(i int) *Stream { ret := C.stream_at((*C.struct_AVFormatContext)(ctx), C.int(i)) return (*Stream)(ret) } 

您可以选择接受通用(任意)双指针作为其参数的ptr_at函数,或者选择仅接受指向AVFormatContext指针作为其参数的更具体的stream_at函数。 前一种方法可用于从任何双指针访问元素,例如: AVProgram **AVChapter **等。如果我们需要实现额外的处理(如边界检查),则后一种方法更可取。