• Stars
    star
    662
  • Rank 65,403 (Top 2 %)
  • Language
    JavaScript
  • License
    Creative Commons ...
  • Created about 5 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

:shipit: AVIF polyfill for the browser


AVIF (AV1 Still Image File Format) polyfill for the browser
Start using superior image compression today! :shipit:
🎊 🎉 DEMO 🎉 🎊

Features

  • Small, optional dependency, <4kb minified & gzipped
  • Intercepts AVIF fetch requests so works in any scenario
  • Uses native decoder if possible and should be reasonably fast

Supported browsers

With native decoder:

  • Chrome Desktop 70+
  • Firefox 63+ (with media.av1.enabled activated)
  • Firefox for Android 64+ (with media.av1.enabled and media.av1.use-dav1d activated)
  • Edge 18+ (with AV1 Video Extension installed)
  • Bromite 71+

With AV1 polyfill:

  • Chrome 57+
  • Firefox 53+
  • Edge 17+
  • Safari 11+

Usage

npm install avif.js
// Put this to reg.js and serve avif-sw.js from web root
// Both scripts should be transpilled (either manually with e.g. browserify or
// automatically by parcel)
require("avif.js").register("/avif-sw.js");
<body>
  <!-- Register worker -->
  <script src="reg.js"></script>

  <!-- Can embed AVIF with IMG tag now -->
  <img src="image.avif">

  <!-- Or via CSS property -->
  <div style="background: url(image2.avif)">
    some content
  </div>
</body>

That's it! Service worker will detect all fetch requests for AVIF files and decode them on the fly. It works with any complex scenarios of image embedding you might have, e.g. background-image in external CSS or XMLHttpRequest from a script.

See demo directory for the usage example.

To generate AVIF files you may use go-avif CLI utility.

Technical details for nerds

AVIF file is basically an AV1 keyframe packed inside ISOBMFF container, almost identical to the HEIF structure, except AV1 video format instead of HEVC is used. Latest versions of Chrome and Firefox support AV1 video decoding, but still can't display AVIF images, it usually takes some time before new format will be added. See e.g. Firefox issue.

Though abovementioned technical aspects of AVIF make it quite easy to implement as a tiny polyfill. All we need to do is repack AVIF as a single-frame AV1 video and decode it using native decoder. This is exactly what avif.js does. First it fetches the AVIF file into binary buffer, then parses the ISOBMFF structure, then searches and extracts the actual frame data (OBUs) and finally embeds it into MP4 video file. Now we can decode that video with standard <video> element and dump raw pixel data to temporary <canvas>.

Instead of forcing users to call some function every time they need to display AVIF file, fetch event interceptor powered by Service Worker API is used. It allows us to replace image data that browser doesn't know how to decode with some known one. avif.js uses BMP to avoid spending time on second compression of already decoded pixel data. It's very fast operation, we just need to write BMP header and copy color values in order. Finally we can deliver that created on the fly .bmp back to the browser and our image will appear on the page.

The actual process is a bit more complex, e.g. we can't create <video> element in a worker, so pass decoding request to the main thread and get result back. Also container fields of the video should correspond to properties of the still image, some offsets need to be fixed and so on. But you got the general idea.

Limitations of Service Worker API

  • Needs to be served from HTTPS
  • Doesn't work in Firefox/Edge Private Window
  • Requires page reload on first visit to display static assets

TODO

  • Benchmark
  • Check for AVIF support
  • Support for browsers without Service Workers

License

avif.js is licensed under CC0.
Demo images are taken from av1-avif repo.

More Repositories

1

ffmpeg.js

Port of FFmpeg with Emscripten
JavaScript
3,160
star
2

go-face

🔍 Face recognition with Go
Go
1,052
star
3

webm.js

JavaScript WebM converter
JavaScript
452
star
4

boram

🎞️ Cross-platform graphical WebM converter
JavaScript
428
star
5

mpv.js

🎥 mpv pepper plugin
C++
395
star
6

vmsg

🎵 Library for creating voice messages
JavaScript
337
star
7

go-avif

🎨 Go AVIF library
Go
304
star
8

gulp-ng-annotate

📌 Add angularjs dependency injection annotations with ng-annotate
JavaScript
265
star
9

webm.py

🎞️ Cross-platform command-line WebM converter
Python
142
star
10

chaptcha

Break 2ch CAPTCHA using OpenCV and FANN
Python
85
star
11

mpv_slicing

Cut video fragments with mpv
Lua
76
star
12

wybm

✂️ Extract and cut youtube webms
JavaScript
56
star
13

dav1d.js

⚗️ WebAssembly AV1 decoder
C
31
star
14

tistore

📷 Tistory photo grabber
JavaScript
23
star
15

kagome

Application cage
Shell
19
star
16

mpv_frame_info

Show frame info with mpv
Lua
16
star
17

go-face-testdata

🗃️ Test data for go-face
14
star
18

github-social-graph

Build simple social graphs for GitHub
Python
14
star
19

video-tools

Various video tools
Jupyter Notebook
13
star
20

kisa

XMPP stress tool
Python
13
star
21

bnw-meow

Kawaii single-page web interface for BnW
CoffeeScript
11
star
22

docker_cve-2015-2925

Docker + CVE-2015-2925 = escaping from --volume
10
star
23

nacl_sdk

🍴 Fork of Native Client SDK
Python
9
star
24

wasm-polyfill.js

🍴 Fork of rfk's project
JavaScript
9
star
25

av1-bench

🏎️ AV1 encoders benchmarks
Python
8
star
26

dotfiles

Dotfiles the easy way
Vim Script
6
star
27

jade-pages-brunch

Adds Jade static pages support to brunch
CoffeeScript
6
star
28

kpopnet

[MOVED]
Go
6
star
29

ng-annotate-uglify-js-brunch

Adds ng-annotate AND UglifyJS support to brunch
JavaScript
4
star
30

SVT-AV1

🍴 Welcome to the GitHub repo for the SVT-AV1 encoder! To see a list of feature request and view what is planned for the SVT-AV1 encoder, visit our Trello page: bit.ly/SVT-AV1 Help us grow the community by subscribing to our SVT-AV1 mailing list! http://bit.ly/svt-av1-mailing
C
4
star
31

jade-ngtemplates-brunch

Adds Jade AngularJS templates support to brunch
CoffeeScript
3
star
32

awesome-cloud-cli

A curated list of useful CLI tools for cloud hostings
JavaScript
3
star
33

hangeul.js

🇰🇷 Hangeul transliteration library
JavaScript
2
star
34

cirno

(OBSOLETE) Dumb XMPP library, the successor of the XMPP lib
Haskell
2
star
35

dogfood

🔩 Basic blocks of operating system
C
2
star
36

parcel-plugin-disable-loaders

📦 Allow to disable parcel loaders
JavaScript
2
star
37

webmify

Allow to watch WebMs in Edge
JavaScript
2
star
38

web-bench

Benchmarks for various web platforms
Perl
2
star
39

ninnin

🎞️ mpv-based video encoding tool
TypeScript
2
star
40

shitsu

Tiny and flexible XMPP bot framework
Python
2
star
41

skip-loader

A loader that returns an empty string module
JavaScript
1
star
42

lame-svn

Git mirror
C
1
star
43

gulp-recipes

Tiny gulp recipes
1
star