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

How to enhance rendering of Bitmap TTML subtitles ? #6950

Closed
xirac opened this issue Feb 6, 2020 · 7 comments
Closed

How to enhance rendering of Bitmap TTML subtitles ? #6950

xirac opened this issue Feb 6, 2020 · 7 comments
Assignees

Comments

@xirac
Copy link

xirac commented Feb 6, 2020

[REQUIRED] Searched documentation and issues

Searched in Exoplayer documentation, current and past issues #4657 and look had a look into SubtitleView.java

[REQUIRED] Question

I have lot of Dash streams with TTML tracks where subtitles are Bitmap, not text.

The subtitles are displayed as expected but the Bitmap rendering could be enhanced:
image

Is there a hook or a way to process the Cue containing those Bitmap before rendering those on screen?
I had a look into SubtitleView.onCues(List cues) method but I don't see how extend/override it when building the player:

player = SimpleExoPlayer.Builder(context, renderersFactory).setTrackSelector(trackSelector!!).build().apply {
                addListener(PlayerEventListener())
                playWhenReady = true
                addAnalyticsListener(EventLogger(trackSelector))
                val style = CaptionStyleCompat(defaultSubtitleColor,
                    Color.TRANSPARENT, Color.TRANSPARENT,
                    CaptionStyleCompat.EDGE_TYPE_DROP_SHADOW,
                    outlineColor, null)
                subtitleView?.setStyle(style) // Change text subtitles style
}

I'm using exoplayer 2.11.1

@icbaker
Copy link
Collaborator

icbaker commented Feb 6, 2020

What do you mean by 'enhanced'? It looks like maybe the subtitles in your media are just low resolution and you're seeing the resultant pixelation.

There's no easy hook like you've described to mutate the cues just before they get displayed. I'm not sure whether it's something that would be generally useful, so I think we'd be hesitant to add it for this use-case specifically.

A possible solution:
You could create your own ImageEnhancingTtmlDecoder that delegates to the existing TtmlDecoder and wraps the Subtitle objects coming from decode() in a new Subtitle implementation that mutates the image cues as they're returned from getCues().

You can register this custom decoder by creating a new implementation of SubtitleDecoderFactory and then overriding ExoPlayerFactory#createTextRenderer and passing this factory into the TextRenderer constructor.

class ImageEnhancingTtmlDecoder extends SimpleSubtitleDecoder {

  TtmlDecoder delegate;

  @Override
  Subtitle decode(byte[] bytes, int length, boolean reset)
      throws SubtitleDecoderException {
    Subtitle subtitle = delegate.decde(bytes, length, reset);
    return new ImageEnhancingTtmlSubtitle(subtitle);
  }
}

class ImageEnhancingTtmlSubtitle implements Subtitle {

  Subtitle delegate;

  @Override 
  List<Cue> getCues(long timeUs) {
    List<Cue> cues = delegate.getCues(timeUs);
    return enhanceImageCues(cues);
  }
}

@xirac
Copy link
Author

xirac commented Feb 10, 2020

The subtitles are low resolution in some source I get, you're right.
Thanks for pointing to this possible solution, I'll give a try.

@xirac xirac closed this as completed Feb 10, 2020
@xirac
Copy link
Author

xirac commented Apr 7, 2020

The change was actually simple.

In library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitlePainter.java
by changing function drawBitmapLayout like this:

@RequiresNonNull({"cueBitmap", "bitmapRect"})
private void drawBitmapLayout(Canvas canvas) {
-    canvas.drawBitmap(cueBitmap, /* src= */ null, bitmapRect, /* paint= */ null);
+    Paint paint = new Paint();
+    paint.setFilterBitmap(true);
+    canvas.drawBitmap(cueBitmap, /* src= */ null, bitmapRect, /* paint= */ paint);
}

The rendering looks pettier.
Before
image
After
image

Of course the enhancement is less visible on higher resolution bitmap subtitles:
image

Do you think it could be proposed as a PR?

@icbaker
Copy link
Collaborator

icbaker commented Apr 7, 2020

Ah interesting, thanks for pointing this out.

I tried to reproduce this locally but it doesn't seem to make any difference to the bitmap subtitles I have access to.

Could you provide the bitmap subtitles you're using to test this, so I can confirm the improvement?

@xirac
Copy link
Author

xirac commented Apr 8, 2020

Stream URL provided via private message.

@icbaker
Copy link
Collaborator

icbaker commented Apr 14, 2020

Thanks for the test stream, I can see a significant improvement. I'll submit a change to enable this.

icbaker added a commit that referenced this issue Apr 27, 2020
@icbaker
Copy link
Collaborator

icbaker commented Apr 27, 2020

This is now available in dev-v2

@icbaker icbaker closed this as completed Apr 27, 2020
ojw28 pushed a commit that referenced this issue May 28, 2020
@google google locked and limited conversation to collaborators Jun 27, 2020
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

3 participants