2

I'm having a hard time converting my images from AV_PIX_FMT_BGRA to PIX_FMT_PAL8. Unfortunately sws_getCachedContext doesn't support the conversion to PIX_FMT_PAL8.

What I'm trying to do is convert my images into a GIF video with higher quality output. It seems that PIX_FMT_PAL8 could potentially provide the higher quality output I'm looking for.

According to this documentation I need to palettize the pixel data, but I have no clue how to do that.

When the pixel format is palettized RGB (PIX_FMT_PAL8), the palettized image data is stored in AVFrame.data[0]. The palette is transported in AVFrame.data[1], is 1024 bytes long (256 4-byte entries) and is formatted the same as in PIX_FMT_RGB32 described above (i.e., it is also endian-specific). Note also that the individual RGB palette components stored in AVFrame.data[1] should be in the range 0..255. This is important as many custom PAL8 video codecs that were designed to run on the IBM VGA graphics adapter use 6-bit palette components.

Any help or direction would be appreciated.

2
  • Are you trying to write code to perform the conversion? Or are you trying to find the right FFmpeg commands to do it? Jun 21, 2014 at 1:33
  • I'm trying to write code to do this. I actually have the code working and all but I was trying to improve the GIF output quality. I think I have a better understanding about the whole pallet deal but I'm not sure if ffmpeg contains something to produce a best fit pallet based on the given image. I feel this way I could get a better quality GIF. I'm not sure of other ways to achieve better GIF quality or if it is even possible.
    – Jona
    Jul 2, 2014 at 21:14

1 Answer 1

2

The code below is adapted from my own export-to-ARGB/RGB(A) code. I haven't tested it, but the main idea should be clear enough.

The code below was modified for making the colour conversion more readable, there's probably a faster way to do this.

uint32_t* pal8_to_bgra(AVPicture* pict, int width, int height)
{
    size_t size = width * height * 4; /* times 4 because 4 bytes per pixel */
    uint32_t colours[255];
    uint32_t *buff = NULL;

    buff = malloc(size);
    if (buff == NULL) {
        fprintf(stderr,
                "Error allocating memory for subtitle bitmap.\n");
        return NULL;
    }

    for (int i = 0; i < 256; ++i) {
        /* Colour conversion. */
        int idx = i * 4; /* again, 4 bytes per pixel */
        uint8_t r = pict->data[1][idx],
                g = pict->data[1][idx + 1],
                b = pict->data[1][idx + 2],
                a = pict->data[1][idx + 3];
        colours[i] = (b << 24) | (g << 16) | (r << 8) | a;
    }

    for (int y = 0; y < rect->h; ++y) {
        for (int x = 0; x < rect->w; ++x) {
            /* 1 byte per pixel */
            int coordinate = x + y * pict->linesize[0];
            /* 32bpp color table */
            int idx = pict->data[0][coordinate];
            buff[x + (y * rect->w)] = colours[idx];
        }
    }

    return buff;
}
2
  • Your snippet gives the PAL8->RGBA conversion. Requested RGBA->PAL8 is not that straightforward
    – rusxg
    Nov 27, 2018 at 4:26
  • Hi, thanks for your great snippet. Just one question: you declared colours[255], but for loop < 256, is it a typo?
    – Justin
    Mar 21, 2022 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.