Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Transformer] The color of BitmapOverlay is a little off #1050

Closed
DeweyReed opened this issue Jan 30, 2024 · 17 comments
Closed

[Transformer] The color of BitmapOverlay is a little off #1050

DeweyReed opened this issue Jan 30, 2024 · 17 comments

Comments

@DeweyReed
Copy link

Hi, thank you for the excellent work!

I'm using media3(1.3.0-alpha01) to add a bitmap overlay to a video. However, the resulting overlay slightly differs from the original file. For example, the blue is a little dark, and the green gets lighter. Does this problem have anything to do with the color space? How to solve it?

@droid-girl
Copy link
Contributor

Hi @DeweyReed, thank you for reporting. If it is possible, could you share the input files? Or a screenshot with the input bitmap compared with the output overlay

@DeweyReed
Copy link
Author

Thank you for replying so quickly!

Overlay image(It's a LUT image, but I'm using it as an overlay to validate my custom effect shader):

lut

Result(The left is the overlay preview in Android Studio, and the right is the generated video):
Screenshot 2024-01-30 215627

The difference is subtle. You can observe the top-left corner of each grid as it's darker in the video than in the image.

I noticed the difference when I tried to apply the LUT with my custom shader program, and it had an unexpected effect.

@kleiren
Copy link

kleiren commented Feb 9, 2024

I have a somewhat related issue. The result of the shaders applied through the Transformer and applied to a GLSurfaceView is different: #1080

Maybe the texture colors are modified when applying a shader? No idea what is happening.

@dway123
Copy link
Contributor

dway123 commented Feb 9, 2024

You mention that the this may have to do with the color space. What color space is your input media using? The input colors expected by overlays is sRGB, per the code you linked. This is different from the color-space videos tend to use, of smpte 170m, so we convert in OverlayShaderProgram to get a consistent intermediate colorspace for mixing.

Also, to make sure I'm understanding correctly, am I right that the main video's colors are correct, and the overlay is experiencing some color shift?

@DeweyReed
Copy link
Author

DeweyReed commented Feb 10, 2024

@dway123 The color space of my overlays is sRGB. I get bitmaps from drawable resources and use them as bitmap overlays.
I don't know if the color space is involved in this situation. After comparing the original and the output videos with a screen color picker, there seems to be a color shift, but I'm not 100% sure.
The color of overlays is experiencing some color shifts.

The code I use is pretty simple:

Transformer.Builder(this).build().start(
    EditedMediaItem.Builder(
        MediaItem.Builder()
            .setUri(getResourceUri(R.raw.video))
            .build()
    ).setEffects(
        Effects(
            emptyList(),
            buildList {
                this  = OverlayEffect(
                    ImmutableList.of(
                        BitmapOverlay.createStaticBitmapOverlay(
                            drawable(R.drawable.lut).toBitmap(),
                        ),
                    )
                )
            },
        )
    ).build(),
    File(filesDir, "output.mp4").canonicalPath
)

I tried to adjust getMixColor in OverlayShaderProgram because the current implementation doesn't work well for my other custom effect, but I failed this time.

@dway123
Copy link
Contributor

dway123 commented Apr 5, 2024

Oops sorry I missed this issue.

Does this improve if you replace fragment_shader_transformation_sdr_oetf_es2, L71 here with return gamma22Oetf(linearColor);? Also, can you please confirm that the color of the input video is an SDR color (not HDR)?

I wonder if the issue is that we're using an incorrect color transfer for video display here (relevant internal link: http://b/310160336).

@DeweyReed
Copy link
Author

@dway123 Thanks for your response.

After changing the output color transfer to 10 using the debugging mode, there are some subtle color changes, but it's still different from the original image.

The video is an SDR video. It's a low-resolution and clipped version of Big Buck Bunny.

I can reproduce the situation with any video. Overlaying the image mentioned above on top of the resulting video makes the difference clearer.

@dway123
Copy link
Contributor

dway123 commented Apr 9, 2024

Hmm thanks, I think @tof-tof is more knowledgeable about overlays, so I'll defer to them here.

@dway123 dway123 assigned tof-tof and unassigned droid-girl Apr 9, 2024
@tof-tof
Copy link
Contributor

tof-tof commented Apr 10, 2024

hmmm... we are going to undergoing a project this quarter that effects how we will process colors in the pipeline. I'll check with this image after and see if it fixes the issue.

An aside: we currently have DrawableOverlay as well as BitmapOverlay, so you can input drawables directly

@DeweyReed
Copy link
Author

Hi, I created a reproducible repo. https://github.com/DeweyReed/TransformerOverlay

@tof-tof
Copy link
Contributor

tof-tof commented Apr 26, 2024

Hi, thanks for the repo and your patience. I tried out your lut image with our new changes in the pipeline (moving away from linear colors in effect shaders by default) and the colors look much better.
Screenshot 2024-04-26 at 12 54 48

The change is still in staging, but I'll link this github issue to it so that when it is submitted you can see it on the main branch. I hope to have the changes in in time for the 1.4.0-beta01 release.

@droid-girl
Copy link
Contributor

@DeweyReed could you let us know when you verify, so we can close the issue

copybara-service bot pushed a commit that referenced this issue May 2, 2024
The second stage of the changes remove the conversion to linear colors in the SDR effects pipeline by default.

also resolves Issue: #1050

PiperOrigin-RevId: 630108296
@DeweyReed
Copy link
Author

Thank you! @droid-girl

It's better than before, but there is still a tiny difference. However, I'm unsure if it is a problem with my monitor or a lack of required modification to my code.

@droid-girl
Copy link
Contributor

I am closing this issue for now. Please reopen for any follow up work

@DeweyReed
Copy link
Author

Recently, I finally found some time to continue the work on video editing. The result has been improved a lot after using setSdrWorkingColorSpace. I'll put my code below for anybody interested.

Thank you very much for your patience and excellent work!

Transformer.Builder(this)
    .setVideoFrameProcessorFactory(
        DefaultVideoFrameProcessor.Factory.Builder()
            .setSdrWorkingColorSpace(
                DefaultVideoFrameProcessor.WORKING_COLOR_SPACE_ORIGINAL
            )
            .build()
    )

@yuriikonovalov
Copy link

Hi @DeweyReed! Has it improved colors of a BitmapOverlay for you? I tried the same settings for VideoFrameProcessorFactory and it changed nothing for my BitmapOverlays 😐

Recently, I finally found some time to continue the work on video editing. The result has been improved a lot after using setSdrWorkingColorSpace. I'll put my code below for anybody interested.

Thank you very much for your patience and excellent work!

Transformer.Builder(this)
    .setVideoFrameProcessorFactory(
        DefaultVideoFrameProcessor.Factory.Builder()
            .setSdrWorkingColorSpace(
                DefaultVideoFrameProcessor.WORKING_COLOR_SPACE_ORIGINAL
            )
            .build()
    )

@DeweyReed
Copy link
Author

@yuriikonovalov I used a custom shader, but a BitmapOverlay made the issue more noticeable. You may want to open a new issue if the change of color spaces doesn't work.

@androidx androidx locked and limited conversation to collaborators Jul 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants