c - 如何从 AV_PIX_FMT_BGRA 转换为 PIX_FMT_PAL8?

标签 c ffmpeg

我很难将图像从 AV_PIX_FMT_BGRA 转换为 PIX_FMT_PAL8。遗憾的是,sws_getCachedContext 不支持转换为 PIX_FMT_PAL8

我想做的是将图像转换为具有更高质量输出的 GIF 视频。看来 PIX_FMT_PAL8 可能会提供我正在寻找的更高质量的输出。

根据this documentation我需要对像素数据进行调色,但我不知道如何做到这一点。

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.

任何帮助或指导将不胜感激。

最佳答案

下面的代码改编 self 自己的导出到 ARGB/RGB(A) 代码。我没有测试过,但是主要思想应该足够清晰了。

下面的代码经过修改,使颜色转换更具可读性,可能有更快的方法来做到这一点。

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;
}

关于c - 如何从 AV_PIX_FMT_BGRA 转换为 PIX_FMT_PAL8?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24297407/

相关文章:

ffmpeg - 使用 FFMPEG 缩小视频尺寸 - 设置最大宽度和高度

ffmpeg 如何将 header 信息添加到 pcm 中?

php - FFMPEG - 短视频 - 循环使其更大

c - 文件 I/O 未附加

c - Openssl 未知协议(protocol)

c++ - 在 C 或 C++ 中定义结构的宏

c - FFMPEG:将视频的一部分写入新文件时 AV 不同步

ffmpeg - ffprobe级别和H.264级别有什么区别?

c - execvp 忽略带有 ' 的参数

无法在 Arduino 环境中定义 A8