Skip to content

Add libjpeg-turbo for preview image size reduction on disk. - #2696

Merged
bradleysepos merged 4 commits into
HandBrake:masterfrom
bradleysepos:libjpeg-turbo
Jun 15, 2020
Merged

bradleysepos merged 4 commits into
HandBrake:masterfrom
bradleysepos:libjpeg-turbo

Conversation

@bradleysepos

@bradleysepos bradleysepos commented Mar 16, 2020 •

Copy link
Copy Markdown
Contributor

Update: Wired up and functional (#2696 (comment)).

As the title states, we may wish to use this for experimentation, specifically with improving on-disk cache sizes for preview images as storage resolutions increase. Speed will likely also improve due to reducing storage/retrieval times, offloading part of the workload to the CPU which is usually not under high load during scan.

API Documentation. libjpeg.a|so exposes the original libjpeg API while libturbojpeg.a|so exposes the TurboJPEG API, which is simpler albeit less powerful. Both are linked. If all platforms decide on the same API, we can exclude the other lib. There's no major pressure to do so as each should be under 1 MB. This would seem to affect libhb only and I implemented compression/decompression using the TurboJPEG API.

On Fedora, please install the turbojpeg-devel package. Not libturbojpeg-devel, which is probably installed already, but does not provide libturbojpeg.so.

I have not yet tested on other distros or architectures other than x86_64. I hope I properly ensured module.defs is compatible with other architectures, including aarch64.

This builds and statically links on macOS/mingw-w64. My mingw-w64 cross build was from macOS.

Quoting my previous comments on #2574:

For grins, I ran tests with libjpeg-turbo 2.0.4 against 30 images from Tears Of Steel 4K (3840x2160 letterboxed) on my dual X5680 3.33 GHz (12 total cores, 24 threads). Encoding/decoding is to/from memory and benchmarks are an average of four runs. I used GNU parallel (which adds ~0.2s latency in this case) for parallelism.

Encoding:

Encode Threads Time - To YUV Time - To RGB
1 3.01s 4.25s
2 1.67s 2.29s
4 1.03s 1.36s
8 0.69s 0.87s
16 0.60s 0.71s
24 0.61s 0.68s

Decoding (to RGB, no speed impact vs. YUV):

Decode Threads Time - From YUV Time - From RGB
1 1.83s 2.80s
2 1.04s 1.54s
4 0.68s 0.94s
8 0.50s 0.64s
16 0.49s 0.55s
24 0.49s 0.55s

It would seem simultaneously encoding/decoding 30 4K images to/from JPEG can be accomplished in around a second on consumer hardware (say, 4 reasonably fast cores).

In reality, HandBrake would probably not be trying to compress or decompress 30 images simultaneously. Encoding a single image on a single thread takes ~120-145ms, and decoding a single image on a single thread is as fast as 45ms, including SSD load times. This is probably fast enough to be transparent for images stored/loaded on the fly, should we choose to use such a technique to reduce temporary storage.

Size comparison, 30 total files:

Format Size
BMP Original 748.3 MB
JPEG 90% RGB 86.7 MB
JPEG 90% YUV 32.0 MB

Test on:

Smoke tested building and launching the GUI on each platform.

  • Windows 10
  • macOS 10.14
  • Fedora Linux 31

@Nomis101

Copy link
Copy Markdown
Contributor

Is this patch, that Google and Mozilla are using, also important for HandBrake?
https://hg.mozilla.org/mozilla-central/file/tip/media/libjpeg/1050342.diff#l1

@sr55

sr55 commented Mar 16, 2020

Copy link
Copy Markdown
Contributor

I assume you've not committed the libhb code yet?

@bradleysepos

bradleysepos commented Mar 16, 2020 •

Copy link
Copy Markdown
Contributor Author

Is this patch, that Google and Mozilla are using, also important for HandBrake?

Judging by the Blink repo commit message, it seems to mostly affect packetized data input. I'm guessing it won't be an issue for local use.

Curious why the patch is thought unlikely to be accepted upstream.

I assume you've not committed the libhb code yet?

Just got the libs linking and building succeeding. CMake wasn't picking up the arch automatically so fixed that. Haven't yet started any implementation.

@sr55

sr55 commented Mar 29, 2020

Copy link
Copy Markdown
Contributor

LGTM!

@bradleysepos

Copy link
Copy Markdown
Contributor Author

Wired up. Works great using MacGUI.

30 previews, cropped 4K source (single title): original 296.1 MB, compressed 24.5 MB.

I initially created hb_save_preview2() and hb_read_preview2(), however this seemed unnecessary since the original functions are only used internally and not by the GUIs directly. So I've modified hb_save_preview() and hb_read_preview() to help avoid code rot.

Also note that every preview image save/load creates a new compressor/decompressor context, which seems inconsequential in my limited testing.

bradleysepos added a commit to bradleysepos/HandBrake that referenced this pull request Mar 30, 2020
@bradleysepos

bradleysepos commented Mar 30, 2020 •

Copy link
Copy Markdown
Contributor Author

Blu-ray source with 71 usable titles, 10 previews per title (default): original 2 GB, compressed 150 MB. 😸

Update: I did not Valgrind this; however, watching macOS Activity Monitor while scanning a ~230 title Blu-ray source showed HandBrake maintaining 95 MB (+7/-3) memory usage. Over 2300 previews totaling more than 5 GB compressed to 377 MB. 🎉

@bradleysepos bradleysepos changed the title Add libjpeg-turbo for preview image experimentation. Add libjpeg-turbo for preview image size reduction on disk. Mar 30, 2020
@bradleysepos

Copy link
Copy Markdown
Contributor Author

Looks like this built on Ubuntu, but I have not yet tested there. I'll need to figure out the additional package dependencies for all distros we document at some point.

@bradleysepos bradleysepos added this to the 1.4.0 milestone Mar 30, 2020
@bradleysepos bradleysepos mentioned this pull request Mar 30, 2020
@sr55

sr55 commented Mar 30, 2020

Copy link
Copy Markdown
Contributor

As a fun bonus:
Windows explorer can easily render them now.
image

@sr55

sr55 commented Mar 30, 2020

Copy link
Copy Markdown
Contributor

Over 60 previews, it's approx 1~2 seconds slower on an NVME SSD where storage isn't the bottleneck. I suspect spinning rust might gain an advantage due to the large writes.

878MB -> 51MB

Think it's a good compromise.

@bradleysepos

Copy link
Copy Markdown
Contributor Author

Windows explorer can easily render them now.

Same for macOS Finder.

I debated adding the file extension as people might want to obfuscate certain source previews, but decided it's probably fine.

@sr55

sr55 commented Mar 30, 2020

Copy link
Copy Markdown
Contributor

I'm sure there will be 1 user who takes issue with it. Especially since there is the occasional risk the files won't be cleared up until a system cleanup runs.

@bradleysepos

Copy link
Copy Markdown
Contributor Author

Package libturbojpeg0-dev on Ubuntu. It looks like version 2.0.0+ is not available on Bionic, but everything seems to work fine on Focal.

bradleysepos added a commit to bradleysepos/HandBrake that referenced this pull request Apr 3, 2020
@bradleysepos

Copy link
Copy Markdown
Contributor Author

Improved a few things in the last commit. Should be ready to rock, though I'd wait until our CI is on Focal so TurboJPEG API v2 is available.

@Nomis101

Nomis101 commented Apr 4, 2020

Copy link
Copy Markdown
Contributor

Windows explorer can easily render them now.

Same for macOS Finder.

In what folder are they getting saved on macOS?

@bradleysepos

bradleysepos commented Apr 4, 2020 •

Copy link
Copy Markdown
Contributor Author

In what folder are they getting saved on macOS?

${TMPDIR}/fr.handbrake.HandBrake/something

@Nomis101

Nomis101 commented Apr 4, 2020

Copy link
Copy Markdown
Contributor

In what folder are they getting saved on macOS?

${TMPDIR}/fr.handbrake.HandBrake/something

Thanks, found it in /private/var/folders/qh/gtfyt26d59nff_mf4033r7rc0000gn/T/fr.handbrake.HandBrake/hb.15496
🤓

echo $TMPDIR
/var/folders/qh/gtfyt26d59nff_mf4033r7rc0000gn/T/

Even for insane 100 preview pictures of a 4k movie I just get 66.8 MB 😀👍🏻 And it seems a little bit faster then before.

@bradleysepos

Copy link
Copy Markdown
Contributor Author

Note also, macOS has open at /usr/bin/open. So you can do open $TMPDIR in Terminal to open the temporary directory in Finder directly.

@Nomis101

Nomis101 commented Apr 4, 2020

Copy link
Copy Markdown
Contributor

Is this patch, that Google and Mozilla are using, also important for HandBrake?

Judging by the Blink repo commit message, it seems to mostly affect packetized data input. I'm guessing it won't be an issue for local use.

Curious why the patch is thought unlikely to be accepted upstream.

I was also curious, so I've asked:

I’ll dig up the original discussion and post a link to it, but if memory serves, the patch caused a significant performance regression, and the sole purpose of the patch was to produce deterministic and canonical behavior (“canonical”=like libjpeg) for a certain set of corrupt JPEG images. My position was that libjpeg-turbo should behave like libjpeg for valid images, but in the spirit of GIGO (garbage in/garbage out), corrupt images should not crash libjpeg-turbo, but they can’t be guaranteed to always produce deterministic output or the same output as libjpeg, particularly if doing so would cause a performance regression.

libjpeg-turbo/libjpeg-turbo#420 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

5 participants