gpt4 book ai didi

c - 如何从 Golang 访问 C 指针数组

转载 作者:IT王子 更新时间:2023-10-29 01:04:22 28 4
gpt4 key购买 nike

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

我试图让存储在 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 中,看起来我只需要做 streams[i],但这在 go 中行不通,所以我使用我的问题 here 中的技术向包装器添加了一个函数。 .但是我没有得到数据;看起来我得到了一个指向内存中随机某处的指针。那么,我怎样才能从 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 端避免指针运算,你可以定义一个 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 函数,或者只接受指向 AVFormatContext< 的指针的更具体的 stream_at 函数 作为参数。前一种方法可用于从任何双指针访问元素,例如:AVProgram **AVChapter ** 等。如果我们需要实现,则后一种方法更可取额外的处理,例如边界检查。

关于c - 如何从 Golang 访问 C 指针数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49987098/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com