On 01/06/2013 01:48 AM, MrOrdinaire wrote:

> My question is how this function declaration is written in D.
> int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
> int w, int h, enum AVPixelFormat pix_fmt, int align);

1) No array is passed to a function as-is in C. Even though there seem to be arrays of 4 elements above, they are both passed as pointers to their first elements. (That 4 has no bearing at all.)

2) uint8_t *pointers[4] means an array of elements of type 'uint8_t*'.

3) int linesizes[4] means an array of ints.

4) uint8_t and friends are defined in the std.stdint module in Phobos.

5) enum should not be used in that signature in D.

6) align is a keyword and cannot be used.

This is the declaration of that function in D:

extern(C)
int av_image_alloc(uint8_t** pointers, int* linesizes,
                   int w, int h, AVPixelFormat pix_fmt, int alignment);


Here is a program that I used to test it:

import std.stdint;

extern(C)
{

enum AVPixelFormat
{
    one, two
}

int av_image_alloc(uint8_t** pointers, int* linesizes,
                   int w, int h, AVPixelFormat pix_fmt, int alignment);

}

void main()
{
    uint8_t ui0 = 7;
    uint8_t ui1 = 8;

    uint8_t*[] pointers = [ &ui0, &ui1 ];

    // Ditto
    int[] linesizes = [ 20, 30, 40 ];

    int result = av_image_alloc(pointers.ptr, linesizes.ptr,
                                44, 55, AVPixelFormat.two, 100);

    assert(result == 42);
}

I tested it with the following C file:

#include <stdint.h>
#include <assert.h>

enum AVPixelFormat
{
    AVPixelFormat_one, AVPixelFormat_two
};

int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
                   int w, int h, enum AVPixelFormat pix_fmt, int align)
{
    assert(*pointers[0] == 7);
    assert(*pointers[1] == 8);

    assert(linesizes[0] == 20);
    assert(linesizes[1] == 30);
    assert(linesizes[2] == 40);

    assert(w == 44);
    assert(h == 55);

    assert(pix_fmt == AVPixelFormat_two);
    assert(align == 100);

    return 42;
}

I compiled the C file like this:

$ gcc -c deneme.c

Then compiled the D program like this (note that the C .o was in a different directory):

$ dmd ../c/deneme.o deneme.d

And ran it like this:

$ ./deneme

All assertions passed. Yay! :)

Ali

P.S. To prove the point that that 4 in the signature has no meaning, I tested the C function also with the following C program:

int main()
{
    uint8_t ui0 = 7;
    uint8_t ui1 = 8;

    // Deliberately 10 (not 4) to prove a point
    uint8_t* pointers[10] = { &ui0, &ui1 };

    // Ditto
    int linesizes[10] = { 20, 30, 40 };

    av_image_alloc(pointers, linesizes, 44, 55, AVPixelFormat_two, 100);
}

Reply via email to