How to extract the audio of a WebM file using pure C

133 Views Asked by At

I have a simple C program all it does is it opens a webm file and it reads the top elements of it without any external libraries. Let's say I have a function that finds each SimpleBlock on the file. How can I get the audio out of it?

typedef struct simple_vint
{
    uint8_t width;
    uint8_t data[8];
} simple_vint;

unsigned char bits = buffer[0];

simple_vint id;
        id.width = 1;
        mask = 0x80;

        while (!(buffer[0] & mask))
        {
            mask >>= 1;
            id.width++;
        }

        id.data[0] = buffer[0];

        if ((len = read(fd, buffer, id.width - 1)) != id.width - 1)
        {
            printf("Uh oh, read id data error!\n");
            break;
        }

        // Get EBML Element ID.
        for (int i = 1; i < id.width; ++i)
        {
            id.data[i] = buffer[i - 1];
            bits = buffer[i - 1];
            printBinaryInt(bits); // This just prints each bit of a byte
        }

        // Get EBML Element Size first byte.
        if ((len = read(fd, buffer, 1)) != 1)
        {
            printf("Uh oh, read first size byte error!\n");
            break;
        }

        simple_vint size;
        size.width = 1;
        mask = 0x80;

        // Get EBML Element Size vint width.
        while (!(buffer[0] & mask))
        {
            mask >>= 1;
            size.width++;
        }

        buffer[0] ^= mask;
        size.data[0] = buffer[0];

        // Get EBML Element Size vint data.
        if ((len = read(fd, buffer, size.width - 1)) != size.width - 1)
        {
            printf("Uh oh, read id data error!\n");
            break;
        }

        // Get EBML Element Size.
        for (int i = 1; i < size.width; ++i)
        {
            size.data[i] = buffer[i - 1];
            bits = buffer[i - 1];
            printf(" ");
            printBinaryInt(bits);
        }

        uint64_t data_len = vint_get_uint(&size);
        printf("Element Size: %lu\n", data_len);

        if ((len = read(fd, buffer, data_len) != data_len))
        {
            printf("Uh oh... Could not read all the data. Read %u instead of %lu", len, data_len);
        }

        printf("DATA:\n");

        for (size_t i = 0; i < data_len; i++)
        {
            printBinaryInt(buffer[i]);
        }

I tried to create a simple program that reads the info of the elements and then read the element it self with the data. The thing is I cannot understand how are the SimpleBlocks structured so I can get the opus audio data out of them.

0

There are 0 best solutions below