You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Increase Default Number of Sample Sizes for Local Files #2574
Description of the feature or enhancement you'd like to see in HandBrake:
Currently the default number of samples taken for all media sources is 10; this is fine for many cases, but it means fewer samples for accurate automatic cropping. While more samples doesn't necessarily solve auto-cropping issues (see #2219), they can help, and currently this default seems very low.
Now, more sample sizes obviously increases scan-time, and on very slow sources this can be noticeable, for example on DVDs and Blu-Rays, or on network drives. However, local drives are generally a lot faster, and getting faster as more computers come up with SSDs by default, so it might be time to consider increasing the default samples for local drives.
My proposal is to add a new setting to set a different number of samples for local files, with a suggested default of 30, decreasing to 20 if this gets negative feedback in beta.
I'd also suggest possibly having a separate setting for network drives; while these can be very slow, it very much depends on a user's network. Someone with a good NAS and a gigabit or better network for example may happy to increase the number of samples when sourcing a file over their network, but the default for this setting should remain at 10 (for those using slower networks, or even online sources).
This leaves DVD/Blu-Ray sources which should remain as-is, as larger numbers of samples can have a big impact to scan-time for these.
What version of HandBrake are you currently running (e.g., 1.3.0)?
1.3.1
What operating system and version are you running (e.g., Ubuntu 19.10, macOS 10.15 Catalina, Windows 10 1909)?
Given that auto-crop works in the vast majority of cases, this is a lot of effort for very little gain. We are only a small team doing this in our free time so low return features don't really make much sense.
The speed of storage was not a factor in the choice of 10. Standard HDD's are usually not taxed by the process. It's CPU / Memory bound.
The default setting of 10 already get's it's fair share of complaints about being too high. Funnily enough, it's users with SSDs that complain the most about this.
The default setting of 10 already get's it's fair share of complaints about being too high. Funnily enough, it's users with SSDs that complain the most about this.
That's surprising to me; I have 60 samples enabled and it takes less than a second even when my CPU is already pretty much maxed out by current encoding jobs. And that's using a drive that, while an SSD, is an older SATA one running externally via USB (it was pulled from a dead machine), so not the fastest by modern SSD standards.
Are people really complaining about that? While I do tend to batch encode we're talking maybe 30-40 jobs scanned at a time, an extra 30-40 seconds doesn't bother me in the slightest.
Performance isn't an issue with SSDs. Not even an issue with HDDs either for the most part. It's mostly CPU/Memory bound.
It's actually storage that's an issue (since many people are running 250GB boot drives). An 4K source is going to hit somewhere in the 400MB per title range of temp storage
There is also concern about SSD Wear but until recently, that wasn't really an issue. QLC NAND is still a bit of an unknown but given the manufacturer numbers for them are pretty low, it's probably a genuine concern now.
I'm actually more confused, can I please check we're definitely talking about the same thing?
I'm referring to sample frames grabbed to determine auto-cropping, and to show in the preview pane; these are 10-60 still images grabbed from the video source. The CPU demand doesn't appear to be high at all (it barely registers in my CPU monitor when grabbing 60 frames, doesn't appear to be pushing all cores or even just one) so it seems mainly disk bound as Handbrake rushes through the video. These also don't appear to be stored anywhere that I can find (not in any of Handbrake's cache folders etc.) so appear to be in memory, and only for the source that's currently being edited in the UI; if I choose to edit a job already in my queue, the sample frames are regenerated again.
Are you thinking of some other sampling feature such as a movie sample or live preview? Or is the behaviour radically different in Handbrake on other platforms?
I have 86 jobs in my queue, with one 4K job currently being edited (60 preview images) and my total for all Handbrake folders I can find (Caches, Application Support, Containers) is 3mb, and I can't find any sign of preview images on disk (there's a Previews folder, but it has nothing in it).
In that case, since HandBrake only keeps sample images for the source(s) currently being edited, then is there a specific reason why these are stored on disk?
Couldn't they just be held in memory, leaving the OS to page them to disk only if necessary? That would eliminate any concerns about additional disk wear, and on any system with plenty of RAM they wouldn't consume any storage at all.
This may already sort-of be the case on most flavours of Linux I think, since /tmp uses tmpfs which is RAM based, but that's still not case on macOS or Windows.
Could be further optimised if adding all job(s) being edited to the queue (i.e- add one to queue when only editing one, or doing add all titles to queue) caused the UI to reset, as this could free up the samples immediately (currently you have to quit and re-open HandBrake to do this).
Is it worth filing any of these things as separate issues?
Ideally, we'd like to keep as much RAM free for encoding as is possible.
As far as increasing the number of samples, it only benefits outliers and performance (and usability!) is a concern. Increasing the number of samples does not scale with disc-based sources having many titles, especially as resolutions increase. And since most content is not mixed aspect, 10 samples works the vast majority of the time.
Ideally, we'd like to keep as much RAM free for encoding as is possible.
RAM being used to hold sample frames doesn't mean it isn't available for encoding; a modern OS will page out the memory used for the samples if they aren't being actively used (since at most only one or two are visible in the UI at once), in favour of memory that is being actively used (for encoding).
i.e- worst case, the images are loaded from disk anyway just as they are now, but in the best case (a system with plenty of RAM) they'll never take up disk space at all.
Paging is not a good thing. It really hurts performance of a system overall so you shouldn't be throwing stuff into RAM that doesn't need to be there. Preview data can be multiple gigabytes in size depending on the source, config and use case.
Exactly. Consider a 4K source with 30 titles. 8 megapixel frames plus chroma, so ~16 megabytes per uncompressed frame. With ten previews per title, that's 4.8 GB. Increasing the number of previews exasperates the situation.
If we did go this route, we would at minimum need to seriously consider using JPEG (or whatever) compression before hitting the disk.
That sounds to me like that's something that needs to be done either way?
Either the previews waste excessive storage, or they occupy too much RAM, the problem there isn't the choice of RAM vs. storage, surely? Even PNG or JPEG2000 lossless should be a major improvement in either case.
Bare in mind that to maintain the lossless nature of preview you can rule out jpg and a 4K PNG is going to be 8MB. At that point, your far better using BitMap as it's stupidly fast from a performance perspective and easy to manipulate in code without having to worry about decoding/encoding of it.
That sounds to me like that's something that needs to be done either way?
Perhaps (patches welcome, of course). Most of the time, a couple gigs on disk isn't a major issue, but might become one as 4K and higher sources become the norm.
Remember also, previews for the title undergoing scan must be uncompressed and in memory for analysis, so there's always some hit. Increasing the number of previews will increase RAM usage during scan.
Given our previous example and correcting for the fact I failed to include chroma planes, a 4K 4:2:0 title scan should use ~160 MB for 10 previews, which is pretty good given the autocropping result is nearly always reliable. Increasing to 60 previews yields ~960 MB which IMHO is unreasonable, given it only benefits edge cases.
This is why we make previews configurable, so users encountering mixed aspect sources and the like can choose to utilize more resources if they have them.
Someone very interested in this area is more than welcome to do some analysis with a range of sources and try to come up with 1) improvements to the cropping algorithm in general, and 2) a more optimal number of previews that suits all sources without using significant resources. No one has spent much time on this since the default is fast and works the vast majority of the time, but improvements are always welcome!
All this to say, simply increasing the number of default number of previews is probably not a very good way to make the situation better overall. It can sometimes make the situation better in edge cases. We should at least do further analysis before adjusting the default.
Bare in mind that to maintain the lossless nature of preview you can rule out jpg and a 4K PNG is going to be 8MB. At that point, your far better using BitMap as it's stupidly fast from a performance perspective and easy to manipulate in code without having to worry about decoding/encoding of it.
Indeed, though I'd argue we don't need previews to be lossless, only very high quality compressed and fast.
Fun fact: Adobe Lightroom's editing previews are compressed (probably JPEG), clearly seen when zoomed in. I'm in the habit of always exporting to TIFF first and opening elsewhere, if I really need to pixel peep a photo.
Remember also, previews for the title undergoing scan must be uncompressed and in memory for analysis, so there's always some hit. Increasing the number of previews will increase RAM usage during scan.
Only a single uncompressed image needs to be in RAM at a time though I believe?
Once each sample frame has been analysed only the relevant data (cropping boundaries, is there anything else?) needs to be retained, at that point the image can be lossy, as it's only needed for the user to look at while picking settings (if they even look at them at all).
Only a single uncompressed image needs to be in RAM at a time though I believe?
For the active title in HandBrake's main window, all previews should be in RAM to facilitate navigating between them without delay. This becomes more of an issue with compressed formats. @sr55 mentioned privately that in previous testing, he's seen 80-450ms per frame decoding latency, depending on the compression format.
I'm not sure if libhb stores the active title in memory or not. The WinUI certainly doesn't cache them on the basis they are invalidated as soon as the user starts tuning the picture settings.
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.
Activity
sr55 commentedon Jan 11, 2020
Thanks for the suggestion.
Given that auto-crop works in the vast majority of cases, this is a lot of effort for very little gain. We are only a small team doing this in our free time so low return features don't really make much sense.
The speed of storage was not a factor in the choice of 10. Standard HDD's are usually not taxed by the process. It's CPU / Memory bound.
The default setting of 10 already get's it's fair share of complaints about being too high. Funnily enough, it's users with SSDs that complain the most about this.
Haravikk commentedon Jan 12, 2020
That's surprising to me; I have 60 samples enabled and it takes less than a second even when my CPU is already pretty much maxed out by current encoding jobs. And that's using a drive that, while an SSD, is an older SATA one running externally via USB (it was pulled from a dead machine), so not the fastest by modern SSD standards.
Are people really complaining about that? While I do tend to batch encode we're talking maybe 30-40 jobs scanned at a time, an extra 30-40 seconds doesn't bother me in the slightest.
sr55 commentedon Jan 12, 2020
Performance isn't an issue with SSDs. Not even an issue with HDDs either for the most part. It's mostly CPU/Memory bound.
It's actually storage that's an issue (since many people are running 250GB boot drives). An 4K source is going to hit somewhere in the 400MB per title range of temp storage
There is also concern about SSD Wear but until recently, that wasn't really an issue. QLC NAND is still a bit of an unknown but given the manufacturer numbers for them are pretty low, it's probably a genuine concern now.
Haravikk commentedon Jan 13, 2020
I'm actually more confused, can I please check we're definitely talking about the same thing?
I'm referring to sample frames grabbed to determine auto-cropping, and to show in the preview pane; these are 10-60 still images grabbed from the video source. The CPU demand doesn't appear to be high at all (it barely registers in my CPU monitor when grabbing 60 frames, doesn't appear to be pushing all cores or even just one) so it seems mainly disk bound as Handbrake rushes through the video. These also don't appear to be stored anywhere that I can find (not in any of Handbrake's cache folders etc.) so appear to be in memory, and only for the source that's currently being edited in the UI; if I choose to edit a job already in my queue, the sample frames are regenerated again.
Are you thinking of some other sampling feature such as a movie sample or live preview? Or is the behaviour radically different in Handbrake on other platforms?
galad87 commentedon Jan 13, 2020
Preview images are stored in temporary folder.
Haravikk commentedon Jan 13, 2020
Where are they stored on macOS?
I have 86 jobs in my queue, with one 4K job currently being edited (60 preview images) and my total for all Handbrake folders I can find (Caches, Application Support, Containers) is 3mb, and I can't find any sign of preview images on disk (there's a Previews folder, but it has nothing in it).
galad87 commentedon Jan 13, 2020
The temporary folder is provided by macOS, and it's something like: /private/var/folders/jb/lb8_f0_92pvdjftw3q3ns6qr0000gn/T/fr.handbrake.HandBrake
bradleysepos commentedon Jan 13, 2020
In Terminal, run
open $TMPDIR.Haravikk commentedon Jan 13, 2020
Thanks!
In that case, since HandBrake only keeps sample images for the source(s) currently being edited, then is there a specific reason why these are stored on disk?
Couldn't they just be held in memory, leaving the OS to page them to disk only if necessary? That would eliminate any concerns about additional disk wear, and on any system with plenty of RAM they wouldn't consume any storage at all.
This may already sort-of be the case on most flavours of Linux I think, since /tmp uses tmpfs which is RAM based, but that's still not case on macOS or Windows.
Could be further optimised if adding all job(s) being edited to the queue (i.e- add one to queue when only editing one, or doing add all titles to queue) caused the UI to reset, as this could free up the samples immediately (currently you have to quit and re-open HandBrake to do this).
Is it worth filing any of these things as separate issues?
bradleysepos commentedon Jan 13, 2020
Ideally, we'd like to keep as much RAM free for encoding as is possible.
As far as increasing the number of samples, it only benefits outliers and performance (and usability!) is a concern. Increasing the number of samples does not scale with disc-based sources having many titles, especially as resolutions increase. And since most content is not mixed aspect, 10 samples works the vast majority of the time.
Haravikk commentedon Jan 13, 2020
RAM being used to hold sample frames doesn't mean it isn't available for encoding; a modern OS will page out the memory used for the samples if they aren't being actively used (since at most only one or two are visible in the UI at once), in favour of memory that is being actively used (for encoding).
i.e- worst case, the images are loaded from disk anyway just as they are now, but in the best case (a system with plenty of RAM) they'll never take up disk space at all.
sr55 commentedon Jan 13, 2020
Paging is not a good thing. It really hurts performance of a system overall so you shouldn't be throwing stuff into RAM that doesn't need to be there. Preview data can be multiple gigabytes in size depending on the source, config and use case.
bradleysepos commentedon Jan 13, 2020
Exactly. Consider a 4K source with 30 titles. 8 megapixel frames plus chroma, so ~16 megabytes per uncompressed frame. With ten previews per title, that's 4.8 GB. Increasing the number of previews exasperates the situation.
If we did go this route, we would at minimum need to seriously consider using JPEG (or whatever) compression before hitting the disk.
Edit: Account for chroma planes.
Haravikk commentedon Jan 13, 2020
That sounds to me like that's something that needs to be done either way?
Either the previews waste excessive storage, or they occupy too much RAM, the problem there isn't the choice of RAM vs. storage, surely? Even PNG or JPEG2000 lossless should be a major improvement in either case.
sr55 commentedon Jan 13, 2020
Bare in mind that to maintain the lossless nature of preview you can rule out jpg and a 4K PNG is going to be 8MB. At that point, your far better using BitMap as it's stupidly fast from a performance perspective and easy to manipulate in code without having to worry about decoding/encoding of it.
bradleysepos commentedon Jan 13, 2020
Perhaps (patches welcome, of course). Most of the time, a couple gigs on disk isn't a major issue, but might become one as 4K and higher sources become the norm.
Remember also, previews for the title undergoing scan must be uncompressed and in memory for analysis, so there's always some hit. Increasing the number of previews will increase RAM usage during scan.
Given our previous example and correcting for the fact I failed to include chroma planes, a 4K 4:2:0 title scan should use ~160 MB for 10 previews, which is pretty good given the autocropping result is nearly always reliable. Increasing to 60 previews yields ~960 MB which IMHO is unreasonable, given it only benefits edge cases.
This is why we make previews configurable, so users encountering mixed aspect sources and the like can choose to utilize more resources if they have them.
Someone very interested in this area is more than welcome to do some analysis with a range of sources and try to come up with 1) improvements to the cropping algorithm in general, and 2) a more optimal number of previews that suits all sources without using significant resources. No one has spent much time on this since the default is fast and works the vast majority of the time, but improvements are always welcome!
All this to say, simply increasing the number of default number of previews is probably not a very good way to make the situation better overall. It can sometimes make the situation better in edge cases. We should at least do further analysis before adjusting the default.
bradleysepos commentedon Jan 13, 2020
Indeed, though I'd argue we don't need previews to be lossless, only very high quality compressed and fast.
Fun fact: Adobe Lightroom's editing previews are compressed (probably JPEG), clearly seen when zoomed in. I'm in the habit of always exporting to TIFF first and opening elsewhere, if I really need to pixel peep a photo.
Haravikk commentedon Jan 13, 2020
Only a single uncompressed image needs to be in RAM at a time though I believe?
Once each sample frame has been analysed only the relevant data (cropping boundaries, is there anything else?) needs to be retained, at that point the image can be lossy, as it's only needed for the user to look at while picking settings (if they even look at them at all).
bradleysepos commentedon Jan 13, 2020
For the active title in HandBrake's main window, all previews should be in RAM to facilitate navigating between them without delay. This becomes more of an issue with compressed formats. @sr55 mentioned privately that in previous testing, he's seen 80-450ms per frame decoding latency, depending on the compression format.
sr55 commentedon Jan 13, 2020
I'm not sure if libhb stores the active title in memory or not. The WinUI certainly doesn't cache them on the basis they are invalidated as soon as the user starts tuning the picture settings.
bradleysepos commentedon Jan 14, 2020
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:
Decoding (to RGB, no speed impact vs. YUV):
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:
sr55 commentedon Jan 14, 2020
Since we don't intend to change the default value and it's controllable, closing.
Any future plans, if any, with regards to cropping will be discussed under #2219