Skip to content

[Encoder] Colors got inverted #103

@abdullahsultan

Description

@abdullahsultan

Original Image
Image
Encoded Image
Image

I am trying to Encode frames in Unreal Engine to create a video later, I saved fames in yuv and converted to mp4 via Ffmpeg and colors are fine but if I encode via PopH264 (save as .h264) and convert .h264 to mp4 via ffmpeg colors get inverted as shown in Images.
Following is the code I am using:
Creating Encoder

EncoderWrapper::CreateEncoder(int32 InViewportWidth, int32 InViewportHeight, int32 InBitRate)
{
    ViewportWidth = InViewportWidth;
    ViewportHeight = InViewportHeight;
    BitRate = InBitRate;
    EncodedFrames.Empty();
    const FString Options = FString::Printf(
        TEXT("{\"Width\":%d,\"Height\":%d,\"AverageKbps\":%d,\"Realtime\":true}"),
        InViewportWidth,
        InViewportHeight,
        InBitRate
    );

    char Error[1024] = { 0 };

    PopH264Encoder = PopH264_CreateEncoder(TCHAR_TO_UTF8(*Options), Error, sizeof(Error));

    if (PopH264Encoder <= 0)
    {
        UE_LOG(LogEncoderWrapper, Error, TEXT("PopH264 create failed: %s"), UTF8_TO_TCHAR(Error));
    }
}

Pushing Frames:

bool EncoderWrapper::PushFrameToPopH264(
    int32 Encoder,
    int Width,
    int Height,
    const uint8* YPlane,
    const uint8* UPlane,
    const uint8* VPlane,
    TArray<TArray<uint8>>& OutPackets
)
{
    if (Encoder <= 0 || !YPlane || !UPlane || !VPlane)
    {
        return false;
    }

    const int32 LumaSize = Width * Height;
    const int32 ChromaSize = (Width / 2) * (Height / 2);

    FString Meta = FString::Printf(
        TEXT("{\"Width\":%d,\"Height\":%d,\"Format\":\"Yuv_8_8_8\",\"LumaSize\":%d,\"ChromaUSize\":%d,\"ChromaVSize\":%d}"),
        Width,
        Height,
        LumaSize,
        ChromaSize,
        ChromaSize
    );

    char ErrorBuffer[1024] = { 0 };

    PopH264_EncoderPushFrame(
        Encoder,
        TCHAR_TO_UTF8(*Meta),
        YPlane,
        UPlane,
        VPlane,
        ErrorBuffer,
        sizeof(ErrorBuffer)
    );

    if (ErrorBuffer[0] != 0)
    {
        UE_LOG(LogEncoderWrapper, Error, TEXT("PopH264 Encode Error: %s"), UTF8_TO_TCHAR(ErrorBuffer));
        return false;
    }

    // Drain all available encoded packets
    while (true)
    {
        uint8 TempBuffer[1024 * 1024];

        int32 PacketSize = PopH264_EncoderPopData(
            Encoder,
            TempBuffer,
            sizeof(TempBuffer)
        );

        if (PacketSize <= 0)
        {
            break;
        }

        TArray<uint8> Packet;
        Packet.Append(TempBuffer, PacketSize);

        OutPackets.Add(MoveTemp(Packet));
    }

    return true;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions