-
Notifications
You must be signed in to change notification settings - Fork 402
MSC2448: Using BlurHash as a Placeholder for Matrix Media #2448
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
base: old_master
Are you sure you want to change the base?
Changes from 3 commits
8ba6071
f71535a
8adf2b2
56703fc
793107d
777c30c
b80822e
b240d92
1afad01
60773e4
df5b6d7
571ce2a
e885eae
4b83c51
b893c21
6e8eb59
3695ecf
842c2a0
3994010
616bc81
e0a7442
385be8a
708b756
b761b06
40d71ff
7f13184
1300a6e
2a02d2c
7ea82b4
f93d708
ed9ed5e
48c4d55
fba60db
c837c83
b3f1915
5b8c191
63d4966
676571f
a302197
594bcee
64116ae
9bd0ba6
e7e0fb7
1d954f0
9e981ba
75a4fa6
934b6d7
974d368
5668397
234877c
abf5283
754fa7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# MSC2448: Using BlurHash in Media Events | ||
|
||
[BlurHash](https://blurha.sh) is a compact representation of a placeholder | ||
for an image (or the frame of a video). Currently in Matrix, clients must | ||
anoadragon453 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
display a placeholder image in the message timeline while a piece of media is | ||
loading. Some clients, such as Riot, simply display an empty space. | ||
|
||
While thumbnails exist to combat this to some degree, they still need to be | ||
downloaded from a homeserver, which is not instantaneous. | ||
|
||
Instead, a blurhash can be sent inside the `m.room.message` event, which upon | ||
receipt other clients can render for a pretty preview while the actual | ||
thumbnail downloads. | ||
anoadragon453 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Proposal | ||
anoadragon453 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### m.room.message | ||
|
||
A new optional field is added in `m.room.message`'s content field called | ||
anoadragon453 marked this conversation as resolved.
Show resolved
Hide resolved
anoadragon453 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
`blurhash`. It is a BlurHash of the original piece of media. Clients could | ||
then render this using [one of the available BlurHash | ||
implementations](https://github.com/woltapp/blurhash). | ||
|
||
This would be optionally displayed while the thumbnail of the media is loaded | ||
in parallel. | ||
|
||
Example `m.room.message` content field: | ||
|
||
``` | ||
{ | ||
"body": "image.png", | ||
"info": { | ||
"size": 149234, | ||
"mimetype": "image/png", | ||
"thumbnail_info": { | ||
"w": 301, | ||
"h": 193, | ||
"mimetype": "image/png", | ||
"size": 172958 | ||
anoadragon453 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
"w": 301, | ||
"h": 193, | ||
"thumbnail_url": "mxc://example.org/abcdefg" | ||
}, | ||
"msgtype": "m.image", | ||
"url": "mxc://example.org/abcde", | ||
"blurhash": "JadR*.7kCMdnj" | ||
anoadragon453 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm: why is this on the top level but the room avatar isn't? should we just move the room avatar one up a level and call it good? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The justification for it is that The inconsistency is indeed a bit awkward. If we move I'm curious what others think the best way forward is. |
||
} | ||
``` | ||
|
||
### Inline images | ||
|
||
An optional attribute is added to `<img>` tags in messages: `blurhash`, where | ||
the value of the attribute is the blurhash representation of the inline | ||
image. | ||
|
||
This would be optionally displayed with the inline image itself is loaded in parallel. | ||
|
||
Example `m.room.message.formatted_body`: | ||
|
||
``` | ||
"formatted_body": This is awesome <img alt=\"flutterjoy\" title=\"flutterjoy\" height=\"32\" src=\"mxc://matrix.example.org/abc\" blurhash=\"LEHV6nWB2yk8pyo\" /> | ||
anoadragon453 marked this conversation as resolved.
Show resolved
Hide resolved
anoadragon453 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
Note that a BlurHash representation is really only applicable to media, and | ||
as such should only be used in conjunction with the following | ||
`m.room.message` msgtypes: | ||
|
||
* `m.image` | ||
* `m.video` | ||
|
||
To be clear: This does not replace thumbnails - it will be shown before they | ||
are downloaded. | ||
|
||
## Calculating a blurhash | ||
|
||
BlurHashes are inserted into `m.room.message` events by the client, however | ||
some clients may not be able to implement the BlurHash library for whatever | ||
reason. In this case, it would be nice to allow the media repository to | ||
calculate the BlurHash of a piece of media for the client, similar to how | ||
thumbnails are calculated by media repositories today. | ||
|
||
The [`/_matrix/media/r0/upload`](https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-media-r0-upload) endpoint response is modified to include an optional `blurhash` parameter, which the client may use to insert into messages if desired: | ||
anoadragon453 marked this conversation as resolved.
Show resolved
Hide resolved
anoadragon453 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Example response: | ||
|
||
``` | ||
{ | ||
"content_uri": "mxc://example.com/abcde123", | ||
"blurhash": "LKO2?U%2Tw=w]~RB" | ||
} | ||
``` | ||
|
||
## Visualisation | ||
|
||
Viewing an image message that is loading: | ||
|
||
 | ||
|
||
Once the image loads: | ||
|
||
 | ||
|
||
For reference, the current state of things in Riot is: | ||
|
||
 | ||
|
||
## Alternatives | ||
|
||
We could include a base64 thumbnail of the image in the event, but blurhash | ||
produces much more efficient textual representations. | ||
Comment on lines
+323
to
+326
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discuss the alternative algorithm (and self-proclaimed improvement over BlurHash) ThumbHash. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The alpha support is nice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A comparison of ThumbHash vs. BlurHash. ProsAlpha supportThumbHash handles images with transparent backgrounds in a much nicer fashion than BlurHash. While the obvious use case for this is stickers, as mentioned elsewhere, I think that vector outlines would be the best way to represent stickers before they are downloaded in full resolution. Still, being able to somewhat make out what an image with a transparent background is before it has loaded is valuable. Better qualityThumbHash appears to generally create a better representation of the image than BlurHash (examples taken from https://evanw.github.io/thumbhash/): ConsLimited Library SupportBlurHash was one of the first to widely publicise this use case, and thus it is a lot more popular than ThumbHash. Compare the number of implementations for ThumbHash versus BlurHash. Still, the algorithm is so simple that you could presumably translate it into your chosen language in about 30m. BlurHash is already widely used in MatrixAgain, due to BlurHash coming out much earlier than ThumbHash, Matrix clients have already implemented BlurHash (through this MSC) widely. If we switch, clients with concern for backwards-compatibility will likely need to implement both BlurHash and ThumbHash. However, currently BlurHash mostly applies to media sent in the timeline, which quickly becomes stale. Element Web only supports The more pressing concern would be interacting with older clients that still only send BlurHashes instead of ThumbHashes. However the failure mode here during the transition period wouldn't be too bad - you just won't see blurred thumbnails. BandwidthTODO: I'd like to conduct a test of both algorithms over a range of, say, 100 images. Using base83 encoding for both. Currently I'm not sure whether BlurHash or ThumbHash generally produces smaller encoding sizes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ThumbHash is also around 10x faster to generate, at least on iOS. I can run a little experiment and report the results if there's interest. The Circles client is no longer generating BlurHashes. We will continue to display them but we are moving entirely to ThumbHash for the future. The slow performance was a big part of this, but also ThumbHash just seems to work better all around. For example, we were having issues on Android where the BlurHash code crashed the app on an invalid input. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As the person who implemented Blurhash based on this MSC in Element Web I'm all for switching to something which supports alpha and has better performance, maintaining blurhash rendering similar to how @cvwright described another client handling it for some time is acceptable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I downsample the source image once to 100x100. Then from that 100x100 I create the ThumbHash and the BlurHash. Maybe the Swift ThumbHash implementation is just more optimized than the BlurHash version? The author went to some pretty great lengths to make it fast. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't look particularly optimized, but neither was the blurhash implementation, I guess. But feel free to bench it. Seems like I was also wrong, the hashing doesn't downscale first, but rendering the blurhash does create a 32x32 pixels image at best, which would be 10x faster than creating a 100x100 pixels image. So would be interesting to see comparisons, especially if you include my lib: https://github.com/Nheko-Reborn/blurhash :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cvwright If you are doing a benchmark, reporting on the resulting filesizes of the hashes would be appreciated! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @anoadragon453 if this is to progress we probably need to update the MSC to define the field for the thumbhash to live. The lack of alpha channel in blurhash makes it insufficient in my opinion. I have a PoC impl for EW which sends & renders both blurhash & thumbhash, preferring the latter for rendering when available. PoC: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ruma has implemented support for ThumbHash in ruma/ruma#2059. |
||
|
||
## Backwards compatibility | ||
|
||
Older clients would ignore the new `blurhash` parameter. | ||
|
||
Newer clients would only show it if it exists. |
Uh oh!
There was an error while loading. Please reload this page.