I am trying to wrap ffmpeg. I am having an issue with a C array.
The issue is with the following code libavutil/channel_layout.h. I have tried with c2nim, futhark and nimterop without success.
I understand that AV_CHANNEL_LAYOUT_MASK creates an array. For example, I understand that AV_CHANNEL_LAYOUT_STEREO`will be `{AV_CHANNEL_ORDER_NATIVE, 2, AV_CH_LAYOUT_STEREO, NULL} where AV_CH_LAYOUT_STEREO is (AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT) so it will be an interger.
I tried something like:
{.passL:"-lavutil".}
let AV_CH_FRONT_LEFT {.importc:"AV_CH_FRONT_LEFT",header:"libavutil/channel_layout.h".} :cint
echo AV_CH_FRONT_LEFT # This works
let AV_CHANNEL_LAYOUT_STEREO {.importc:"AV_CHANNEL_LAYOUT_STEREO", header:"libavutil/channel_layout.h".} :ptr UncheckedArray[cint]
echo AV_CHANNEL_LAYOUT_STEREO[0] # not working
it fails:
/@mprueba.nim.c:184:98: error: expected ‘)’ before ‘[’ token
184 | = dollar___systemZdollars_u8(((NI) (AV_CHANNEL_LAYOUT_STEREO[((NI)0)])));
| ^
Error: execution of an external compiler program 'gcc -c -w -fmax-errors=3 -pthread -I/home/jose/.choosenim/toolchains/nim-2.0.2/lib -I/tmp -o /home/jose/.cache/nim/prueba_d/@mprueba.nim.c.o /home/jose/.cache/nim/prueba_d/@mprueba.nim.c' failed with exit code: 1
Any suggestion about how to access AV_CHANNEL_LAYOUT_STEREO.
I tried to explain this in my reply in the Futhark issue, but I was on my way to the airport so the answer was a bit brief. What you're seeing in AV_CHANNEL_LAYOUT_MASK is an object constructor, however since C doesn't have typed macros it doesn't tell us which object its constructing for. The following define statements then expand this snippet of text and replaces nb, and m in the template with the given arguments. We still don't have any type information as these are only text expansions. Whenever you try to use e.g. AV_CHANNEL_LAYOUT_STEREO it will then simply paste in this object constructor, and if you are using it correctly this will be a place where such an object constructor is allowed and it will create an object of whatever type was required. Since Nim has fully typed macros and not just text expansion this isn't really something you could easily do in Nim. However by looking at the surrounding code (and comments), we are able to grok what this is actually supposed to be doing and can create a template a bit something like this:
template AV_CHANNEL_LAYOUT_MASK(nb, m: untyped): AVChannelLayout =
AVChannelLayout(order: AV_CHANNEL_ORDER_NATIVE, nb_channels: nb, u: AVChannelLayoutAnonUnion(mask: m), opaque: nil)
This will create an object of kind AVChannelLayout which can then be used just like the ones in the C code. The name of AVChannelLayoutAnonUnion might of course vary, I don't remember exactly what Futhark generates in this case, but it's something similar to that.