X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=perl%2FSesse%2Fpr0n%2FCommon.pm;h=816c003cb016eac4d55b11d3dde730ceb89b19bf;hb=a2d71665db6bf86f40ae9deb956cfc6463645529;hp=8c21541287a60053d92ea08535b21546d8b86c9c;hpb=84460e4c9e238c3f3b661756796f764df2441aaa;p=pr0n diff --git a/perl/Sesse/pr0n/Common.pm b/perl/Sesse/pr0n/Common.pm index 8c21541..816c003 100644 --- a/perl/Sesse/pr0n/Common.pm +++ b/perl/Sesse/pr0n/Common.pm @@ -573,16 +573,21 @@ sub read_original_image { } sub ensure_cached { - my ($r, $avif_ok, $filename, $id, $dbwidth, $dbheight, $xres, $yres, @otherres) = @_; + my ($r, $avif_ok, $jxl_ok, $filename, $id, $dbwidth, $dbheight, $xres, $yres, @otherres) = @_; my $fname = get_disk_location($r, $id); unless (defined($xres) && (!defined($dbwidth) || !defined($dbheight) || $xres < $dbwidth || $yres < $dbheight || $xres == -1)) { return ($fname, undef); } - # See if we have an up-to-date AVIF to serve. + # See if we have an up-to-date JPEG-XL or AVIF to serve. # (We never generate them on-the-fly, since they're so slow.) - my $cachename = get_cache_location($id, $xres, $yres, 'avif'); + my $cachename = get_cache_location($id, $xres, $yres, 'jxl'); + if ($jxl_ok && -r $cachename and (-M $cachename <= -M $fname)) { + return ($cachename, 'image/jxl'); + } + + $cachename = get_cache_location($id, $xres, $yres, 'avif'); if ($avif_ok && -r $cachename and (-M $cachename <= -M $fname)) { return ($cachename, 'image/avif'); } @@ -631,11 +636,6 @@ sub make_cache { my $height = $img->Get('rows'); my ($nwidth, $nheight) = scale_aspect($width, $height, $xres, $yres); - if ($format eq 'avif') { # AVIF uses 4:2:0. - ++$nwidth if ($nwidth % 2 == 1); - ++$nheight if ($nheight % 2 == 1); - } - my $filter = 'Lanczos'; my $quality = 87; my $sf = "1x1"; @@ -665,23 +665,35 @@ sub make_cache { # and Debian hasn't packaged that even in unstable as of 2021. # So we'll need to do it the manual way. (We don't use /tmp, for security reasons.) (my $dirname = $cachename) =~ s,/[^/]*$,,; - my ($fh, $raw_filename) = File::Temp::tempfile('tmp.XXXXXXXX', DIR => $dirname, SUFFIX => '.yuv'); - # Write a Y4M header, so that we get the chroma siting and color space correct. - printf $fh "YUV4MPEG2 W%d H%d F25:1 Ip A1:1 C420jpeg XYSCSS=420JPEG XCOLORRANGE=FULL\nFRAME\n", $nwidth, $nheight; + my ($fh, $raw_filename) = File::Temp::tempfile('tmp.XXXXXXXX', DIR => $dirname, SUFFIX => '.ycbcr'); + # Write a Y4M header, so that we get the chroma range correct. + printf $fh "YUV4MPEG2 W%d H%d F25:1 Ip A1:1 C444 XYSCSS=444 XCOLORRANGE=FULL\nFRAME\n", $nwidth, $nheight; my %parms = ( file => $fh, filename => $raw_filename, - 'sampling-factor' => '2x2' + interlace => 'Plane' ); $cimg->write(%parms); close($fh); my $ivf_filename; ($fh, $ivf_filename) = File::Temp::tempfile('tmp.XXXXXXXX', DIR => $dirname, SUFFIX => '.ivf'); close($fh); - system('aomenc', '--cpu-used=0', '--bit-depth=10', '--end-usage=q', '--cq-level=10', '--target-bitrate=0', '--good', '--aq-mode=1', '--color-primaries=bt601', '--matrix-coefficients=bt601', '-w', $nwidth, '-h', $nheight, '-o', $ivf_filename, $raw_filename); + system('aomenc', '--quiet', '--cpu-used=0', '--bit-depth=10', '--end-usage=q', '--cq-level=13', '--target-bitrate=0', '--good', '--aq-mode=1', '--matrix-coefficients=bt601', '-o', $ivf_filename, $raw_filename); unlink($raw_filename); - system('MP4Box', '-add-image', "$ivf_filename:primary", '-ab', 'avif', '-ab', 'miaf', '-new', $cachename); + system('MP4Box', '-quiet', '-add-image', "$ivf_filename:primary", '-ab', 'avif', '-ab', 'miaf', '-new', $cachename); unlink($ivf_filename); + } elsif ($format eq 'jxl') { + # Similar, for JPEG-XL. + (my $dirname = $cachename) =~ s,/[^/]*$,,; + my ($fh, $raw_filename) = File::Temp::tempfile('tmp.XXXXXXXX', DIR => $dirname, SUFFIX => '.ppm'); + my %parms = ( + file => $fh, + filename => $raw_filename + ); + $cimg->write(%parms); + close($fh); + system('cjxl', '-p', $raw_filename, $cachename); + unlink($raw_filename); } else { die "Unknown format $format"; }