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

本文介绍了如何在Go中使用FFmpeg库和其包装器goav来访问AVFrame结构体中的数据。核心问题在于如何通过C指针获取AVFrame的数据并进行操作。解决方案涉及将返回的指针转换为unsafe.Pointer,然后进行指针运算以访问数组元素。示例代码展示了如何访问和打印AVFrame数据数组中的元素。
摘要由CSDN通过智能技术生成

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

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

#include

*/

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?

}

解决方案

Since the library author did not construct a slice header for you to work with you will instead need to cast the return value you get to an unsafe.Pointer and then to a uintptr this will allow you to perform pointer arithmetic on it to get elements later in memory.

Here's some example code that should run as-is on the go playground.

package main

import (

"fmt"

"unsafe"

)

func main() {

nums := []uint8{1, 2, 3, 4, 5, 6, 7, 8}

val := &nums[0] // val is the equivalent of the *uint8 the Data function returns

ptr := unsafe.Pointer(val)

sixthVal := (*uint8)(unsafe.Pointer(uintptr(ptr) + 5*unsafe.Sizeof(*val)))

fmt.Println("Sixth element:", *sixthVal)

}

Of course, you will need to be very certain you know how many elements there are so that you do not access invalid memory.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值