Skip to content

Missing parsing of 'displayAlign' from <style> tags from Ttml subtitles #2559

Open
@klilian

Description

@klilian

Version

Media3 main branch

More version details

Issue present in main 1.8.0-beta01 branch

Example of ttml subtitles where 'displayAlign' is not correctly parsed from existing TtmlParser.java class:

<?xml version="1.0" encoding="UTF-8"?><tt xml:lang="it" xmlns="http://www.w3.org/ns/ttml" xmlns:ttm="http://www.w3.org/ns/ttml#metadata" xmlns:tts="http://www.w3.org/ns/ttml#styling" xmlns:smpte="http://www.smpte-ra.org/schemas/2052-1/2010/smpte-tt" ><head>
<smpte:information smpte:mode="Enhanced" />
<styling>
<style xml:id="backgroundStyle" tts:fontFamily="proportionalSansSerif" tts:textAlign="center" tts:origin="0% 66%" tts:extent="100% 33%" tts:backgroundColor="rgba(0,0,0,0)" tts:displayAlign="center" />
<style xml:id="speakerStyle" style="backgroundStyle" tts:color="white" tts:textOutline="black 1px" tts:backgroundColor="transparent" />
<style xml:id="textStyle" style="speakerStyle" tts:color="white" tts:textOutline="none" tts:backgroundColor="black" />
</styling><layout>
<region xml:id="full" tts:origin="0% 0%" tts:extent="100% 100%" tts:zIndex="1" />
<region xml:id="speaker" style="speakerStyle" tts:zIndex="1" />
<region xml:id="background" style="backgroundStyle" tts:zIndex="0" />
</layout>
</head><body>
<div>
<p region="speaker" begin="00:19:32.640" end="00:19:36.160"><span style="textStyle">Lorem ipsum dolor sit amet consectetur adipiscing elit<br/>Quisque faucibus ex sapien vitae pellentesque sem placerat</span></p>
<p region="speaker" begin="00:19:36.320" end="00:19:38.560"><span style="textStyle">Ad litora torquent per conubia nostra inceptos himenaeos.</span></p>
<p region="speaker" begin="00:19:45.880" end="00:19:48.800"><span style="textStyle">Quisque faucibus ex sapien vitae pellentesque sem placerat</span></p>
<p region="speaker" begin="00:19:52.040" end="00:19:55.040"><span style="textStyle">Ad litora torquent per conubia nostra inceptos himenaeos.</span></p>
</div>
</body></tt>
 

'displayAlign' is currently only parsed from 'region' and not from 'style' xml tags. Then subtitles may not be aligned vertically correctly.

Idea to fix that:

  1. Add these setter/getter into TtmlStyle class:
...
@Nullable private String displayAlign;
@Nullable private String parentStyleId;
...
public TtmlStyle setDisplayAlign(@Nullable String displayAlign) {
  this.displayAlign = displayAlign;
  return this;
}

  @Nullable
public String getDisplayAlign() {
  return displayAlign;
}

public TtmlStyle setParentStyleId(@Nullable String parentStyleId) {
  this.parentStyleId = parentStyleId;
  return this;
}

@Nullable
public String getParentStyleId() {
  return parentStyleId;
}
  1. Add parsing of 'displayAlign' and 'style' attributes from 'style' tags in TtmlParser class
private static @PolyNull TtmlStyle parseStyleAttributes(
      XmlPullParser parser, @PolyNull TtmlStyle style) {
...
case TtmlNode.ATTR_STYLE:
  style = createIfNull(style).setParentStyleId(attributeValue);
  break;
case TtmlNode.ATTR_TTS_DISPLAY_ALIGN:
  style = createIfNull(style).setDisplayAlign(attributeValue);
  break;
....
  1. Retrieve 'displayAlign' from 'style' or parentStyle if not present from 'region' in TtmlParser class
....
@Cue.AnchorType int lineAnchor = Cue.ANCHOR_TYPE_START;
@Nullable
String displayAlign =
    XmlPullParserUtil.getAttributeValue(xmlParser, TtmlNode.ATTR_TTS_DISPLAY_ALIGN);
if (displayAlign == null) {
  String styleId = XmlPullParserUtil.getAttributeValue(xmlParser, TtmlNode.ATTR_STYLE);
  if (styleId != null) {
    TtmlStyle style = globalStyles.get(styleId);
    if (style != null) {
      displayAlign = style.getDisplayAlign();
    }
  }
}
if (displayAlign == null) {
  String styleId = XmlPullParserUtil.getAttributeValue(xmlParser, TtmlNode.ATTR_STYLE);
  if (styleId != null) {
    TtmlStyle style = globalStyles.get(styleId);
    if (style != null) {
      String parentStyleId = style.getParentStyleId();
      if (parentStyleId != null) {
        TtmlStyle parentStyle = globalStyles.get(parentStyleId);
        if (parentStyle != null) {
          displayAlign = parentStyle.getDisplayAlign();
        }
      }
    }
  }
}

if (displayAlign != null) {
  switch (Ascii.toLowerCase(displayAlign)) {
    case "center":
      lineAnchor = Cue.ANCHOR_TYPE_MIDDLE;
      line += height / 2;
      break;
    case "after":
      lineAnchor = Cue.ANCHOR_TYPE_END;
      line += height;
      break;
    default:
      // Default "before" case. Do nothing.
      break;
  }
}
....

That's it.

Devices that reproduce the issue

All

Devices that do not reproduce the issue

None

Reproducible in the demo app?

Yes

Reproduction steps

Mock for example a stream with this ttml:

<?xml version="1.0" encoding="UTF-8"?><tt xml:lang="it" xmlns="http://www.w3.org/ns/ttml" xmlns:ttm="http://www.w3.org/ns/ttml#metadata" xmlns:tts="http://www.w3.org/ns/ttml#styling" xmlns:smpte="http://www.smpte-ra.org/schemas/2052-1/2010/smpte-tt" ><head>
<smpte:information smpte:mode="Enhanced" />
<styling>
<style xml:id="backgroundStyle" tts:fontFamily="proportionalSansSerif" tts:textAlign="center" tts:origin="0% 66%" tts:extent="100% 33%" tts:backgroundColor="rgba(0,0,0,0)" tts:displayAlign="center" />
<style xml:id="speakerStyle" style="backgroundStyle" tts:color="white" tts:textOutline="black 1px" tts:backgroundColor="transparent" />
<style xml:id="textStyle" style="speakerStyle" tts:color="white" tts:textOutline="none" tts:backgroundColor="black" />
</styling><layout>
<region xml:id="full" tts:origin="0% 0%" tts:extent="100% 100%" tts:zIndex="1" />
<region xml:id="speaker" style="speakerStyle" tts:zIndex="1" />
<region xml:id="background" style="backgroundStyle" tts:zIndex="0" />
</layout>
</head><body>
<div>
<p region="speaker" begin="00:19:32.640" end="00:19:36.160"><span style="textStyle">Lorem ipsum dolor sit amet consectetur adipiscing elit<br/>Quisque faucibus ex sapien vitae pellentesque sem placerat</span></p>
<p region="speaker" begin="00:19:36.320" end="00:19:38.560"><span style="textStyle">Ad litora torquent per conubia nostra inceptos himenaeos.</span></p>
<p region="speaker" begin="00:19:45.880" end="00:19:48.800"><span style="textStyle">Quisque faucibus ex sapien vitae pellentesque sem placerat</span></p>
<p region="speaker" begin="00:19:52.040" end="00:19:55.040"><span style="textStyle">Ad litora torquent per conubia nostra inceptos himenaeos.</span></p>
</div>
</body></tt>

Expected result

In the above example 'speakerStyle' should be positioned at line value 0.825, but is placed at 0.66 because the current parsing does not parse parent style and displayAlign from style xml tags.

Actual result

In the above example 'speakerStyle' should be positioned at line value 0.825, but is placed at 0.66 because the current parsing does not parse parent style and displayAlign from style xml tags.

Media

<?xml version="1.0" encoding="UTF-8"?><tt xml:lang="it" xmlns="http://www.w3.org/ns/ttml" xmlns:ttm="http://www.w3.org/ns/ttml#metadata" xmlns:tts="http://www.w3.org/ns/ttml#styling" xmlns:smpte="http://www.smpte-ra.org/schemas/2052-1/2010/smpte-tt" ><head>
<smpte:information smpte:mode="Enhanced" />
<styling>
<style xml:id="backgroundStyle" tts:fontFamily="proportionalSansSerif" tts:textAlign="center" tts:origin="0% 66%" tts:extent="100% 33%" tts:backgroundColor="rgba(0,0,0,0)" tts:displayAlign="center" />
<style xml:id="speakerStyle" style="backgroundStyle" tts:color="white" tts:textOutline="black 1px" tts:backgroundColor="transparent" />
<style xml:id="textStyle" style="speakerStyle" tts:color="white" tts:textOutline="none" tts:backgroundColor="black" />
</styling><layout>
<region xml:id="full" tts:origin="0% 0%" tts:extent="100% 100%" tts:zIndex="1" />
<region xml:id="speaker" style="speakerStyle" tts:zIndex="1" />
<region xml:id="background" style="backgroundStyle" tts:zIndex="0" />
</layout>
</head><body>
<div>
<p region="speaker" begin="00:19:32.640" end="00:19:36.160"><span style="textStyle">Lorem ipsum dolor sit amet consectetur adipiscing elit<br/>Quisque faucibus ex sapien vitae pellentesque sem placerat</span></p>
<p region="speaker" begin="00:19:36.320" end="00:19:38.560"><span style="textStyle">Ad litora torquent per conubia nostra inceptos himenaeos.</span></p>
<p region="speaker" begin="00:19:45.880" end="00:19:48.800"><span style="textStyle">Quisque faucibus ex sapien vitae pellentesque sem placerat</span></p>
<p region="speaker" begin="00:19:52.040" end="00:19:55.040"><span style="textStyle">Ad litora torquent per conubia nostra inceptos himenaeos.</span></p>
</div>
</body></tt>

Bug Report

  • You will email the zip file produced by adb bugreport to [email protected] after filing this issue.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions