如何从Golang访问C指针阵列



我正在使用ffmpeg编写一个用于Windows平台的应用程序,它是Golang包装器GOAV,但是我很难理解如何使用C Pointers访问阵列。<<<<<<<<<<<</p>

我正在尝试将流存储在AvformatContext类中以用于GO中,并最终将框架添加到OpenGL中的纹理中,以使视频播放器具有凉爽的过渡。

我认为了解如何施放和访问C数据将使编码变得更加容易。

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

C代码-libavformat/avformat.h

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

golang goav包装器

package avutil
//#cgo pkg-config: libavformat
//#include <libavformat/avformat.h>
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中,看起来我只需要做流[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 double Pointer ,因此将偏移添加到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侧中的指针算术,则可以定义A helper 函数,以访问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函数,或者更具体的stream_at函数,该功能仅接受指向AVFormatContext作为其参数的指针。以前的方法可用于从任何双重指针中访问元素,例如:AVProgram **AVChapter **等。如果需要实现其他处理(例如边界检查(,则更优选的方法。

最新更新