I've been stuck on a problem for some time now and I'm not getting anywhere. May one of you have mercy and help me.
I have transcribed some header files with c2nim. Debugging and many methods seem to work fine, but there is one section where I just can't get anywhere. The section I want to map looks like
__C__
typedef struct AVFormatContext {
...
AVStream **streams;
...
}
...
AVFormatContext *pFormatCtxInCam = NULL;
...
pFormatCtxInCam = avformat_alloc_context();
ret = avformat_open_input(&pFormatCtxInCam, "video=Venus USB2.0 Camera", inFrmt, &inOptions);
for (i = 0; i < pFormatCtxInCam->nb_streams; i++)
if (pFormatCtxInCam->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
video_stream_idx_cam = i;
__Nim__
type AVFormatContext* {.bycopy.} = object
...
streams*: ptr ptr AVStream
...
var pFormatCtxInCam: ptr AVCodecContext = nil
pFormatCtxInCam = avformat_alloc_context()
ret = avformat_open_input(pFormatCtxInCam.addr, "video=Venus USB2.0 Camera", inFrmt, inOptions.addr)
-> Problem
-> How can I access Streams as array ??? I can understand the basics in C and most of the memory layout, but here I reach my limits.
I try to cast in some ways. Examples:
var streams = cast[array[0..0, ptr AVStream]](pFormatCtxInCam[].streams[].addr)
var streams = cast[array[0..0, AVStream]](pFormatCtxInCam[].streams[].addr)
var streams = cast[ref array[0..0, ptr AVStream]](pFormatCtxInCam[].streams)
var streams = cast[array[0..5, AVStream]](pFormatCtxInCam[].streams)
var streams = cast[ptr UncheckedArray[ptr AVStream]](pFormatCtxInCam[].streams)
var streams = cast[ptr UncheckedArray[ptr AVStream]](pFormatCtxInCam[].streams[])
var streams = cast[ptr UncheckedArray[ptr AVStream]](pFormatCtxInCam[].streams[][])
As you can see, I've tried a lot of things because I just don't know what to do... -> How can I access ptr ptr in nim as an array?
Thx for help
Is a 2D array perhaps?
In that case you need to cast[ptr UncheckedArray[ptr UncheckedArray[AVStream]]?
This worked in my example here
var streams = cast[ptr UncheckedArray[ptr AVStream]](pFormatContext[].streams)
I had issues with that too but I fixed my FFMpeg binding too.
I even also had some success with snippet example below
var stream = pFormatContext[].streams[i]
Did you type that up by hand? If so, then you really are a machinegun on the keyboard ;). Respect. Also, the avfilter is missing there partly, but I'm very curious about the final result. And I will watch the progress. Keep up the good work!!!
About your answer:
var streams = cast[ptr UncheckedArray[ptr AVStream]](pFormatContext[].streams)
That seems to be the RIGHT answer. It is a pointer to an array of pointers to the elements. Unfortunately, my bindings are still wrong and the result = nil ... Somewhere I still have a mistake in my bindings.
Thx
And good comments everywhere.
That's from Ffmpeg header files comment :D , it's preferable to refer the c header file directly as the header also written with documentation there.
and sometimes there are misspelled variable ...
This very likely because of :%s/_t//g :P , I'll fix whenever I find them.
Did you type that up by hand?
Nah, I only edited from source after c2nim to make sure it's working. For several examples the binding works fine. But I'll try many more examples to make sure it's ok.
Also, the avfilter is missing there partly
Will add after learning more about Ffmpeg and its APIs. TBH, I'm noob about AV-thingy here so tried to make the binding to familiarize myself with the APIs.
That seems to be the RIGHT answer. It is a pointer to an array of pointers to the elements. Unfortunately, my bindings are still wrong and the result = nil ... Somewhere I still have a mistake in my bindings.
tldr; Edit the source after converted using c2nim to use ``importc`` and ``header`` pragma
This was the exact case when I converted avcodec.h, avformat.h etc using c2nim. At first, I didn't use header pragma to point which header file it should be referred and when debugging it, it's always return nil (or when testing with isNil is false but crash when dereferencing the pointer).
So I changed to pragma to properly point which header file and it worked ok. Even when debugging the struct name appeared too.
i used c2nim with importc but no header pragma ... maybe is that my problem. will try it later.
thx