我正在使用 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 https://stackoverflow.com/questions/49931051/cgo-how-do-you-use-pointers-in-golang-to-access-data-from-an-array-in-c。
但是我没有得到数据;看起来我得到了一个指向内存中随机位置的指针。那么,如何从 golang 访问这些元素呢?任何资源也会有帮助;我将在这方面投入相当多的时间。