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

Use WebGL for rendering where available #1

Closed
wants to merge 4 commits into from

Conversation

devongovett
Copy link

Still falls back to 2d canvas for old browsers. In newer browsers supporting WebGL though, there is a good performance gain. According to a profile, YCbCrToRGBA is currently the function where the most time is spent. By moving the RGB conversion to the GPU with a WebGL shader, I see a 5-10% CPU usage decrease in Chrome and Firefox, and about 15-20% decrease in Safari with WebGL enabled. You can try it for yourself here http://devongovett.github.io/jsmpeg/test.html though the loading indicator doesn't work with WebGL so just wait for it to load.

@MichielDeMey
Copy link

+1

@phoboslab
Copy link
Owner

@devongovett better late than never! The above commit (ad6abe9) implements WebGL YCbCrToRGBA conversion based on your PR.

I was reluctant to implement your PR initially, because it had some problems (no loading screen and some minor decoding artefacts) and because the platforms that needed the speed increase the most (Android/iOS) didn't support WebGL back then anyway. Now with WebGL available basically everywhere implementing it finally makes sense!

I did a benchmark with the WebGL version and @ronag's recent performance improvements for the Bick Buck Bunny video (640x360):

http://phoboslab.org/files/jsmpeg/benchmark/

  • iPhone5S, iOS8.1: 4ms per frame (previously ~13ms)
  • OSX, Chrome: 1.5ms per frame (previously ~4ms)

Good stuff :)

Thank you!

@ronag
Copy link

ronag commented Nov 4, 2014

Would be nice if you updated the benchmark to 1080p. As I guess that would be a good target to work towards? With the current performance figures that should be possible.

@phoboslab
Copy link
Owner

720p example: http://phoboslab.org/files/jsmpeg/benchmark/benchmark-720p.html (still works in realtime on my iPhone5s)

I don't think 1080p mpeg1 video is a good idea by itself: the codec doesn't handle large sizes that well and the file sizes get prohibitively huge.

@devongovett
Copy link
Author

Awesome! 👍

@MichielDeMey
Copy link

This is amazing, thanks!

@crowjonah
Copy link

I've noticed that in browser environments supporting WebGL, it is important to have the height and width attributes of the canvas element set to the dimensions of the source video being used! In my limited tests without them (or with incorrect values), the video will only occupy a section of the canvas element, and does not respond to being dynamically resized. Having those attributes set from the get-go seems to conquer these quirks, though.

@phoboslab
Copy link
Owner

@crowjonah do you have an example?

jsmpeg should set the internal canvas width & height automatically, based on the dimensions of the video. Maybe it's broken when the decoding buffer needs some padding!?

@andyearnshaw
Copy link

Ignore my previous comment, 320x180 was the correct width, but someone here had set up a 240x135 poster on the video and that's what caused the incorrect sizing. The issue with the black borders still exists in the webgl rendered videos, however.

@andyearnshaw
Copy link

I was able to fix the problem by setting up the viewport here:

if( this.gl ) {
    this.gl.useProgram(this.program);
    this.gl.viewport(0, 0, this.width, this.height);

    // ...
@phoboslab
Copy link
Owner

Well, yeah. No wonder resizing the canvas didn't work :D
Thank you!

@crowjonah
Copy link

Good catch, @andyearnshaw! Thanks all!

@andyearnshaw
Copy link

It's no problem 😄.

It's worth noting that this PR actually makes Chrome/UIWebView on iOS 8 work at an almost watchable speed. In Chrome iOS 7.1 decoding the aforementioned 320x180 video takes ~90ms, on iOS 8 it's ~44ms (tested on iPad Mini).

phoboslab pushed a commit that referenced this pull request Apr 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
6 participants