1+ import { spawnSync } from "node:child_process" ;
12import { describe , expect , it } from "vitest" ;
2- import { hdrToSdrToneMapFilter } from "./hdrToneMap" ;
3+ import {
4+ hdrToSdrToneMapFilter ,
5+ keyframeTransferArgs ,
6+ parseKeyframeTransfers ,
7+ toneMapReferenceNits ,
8+ } from "./hdrToneMap" ;
39
410const PQ = { colorSpace : "bt2020nc" , colorPrimaries : "bt2020" , colorTransfer : "smpte2084" } ;
5- const TONE_MAP =
6- " zscale=t=linear:npl=100 ,tonemap=hable:desat=0,zscale=p=bt709:t=bt709:m=bt709:r=tv" ;
11+ const toneMap = ( npl : number ) =>
12+ ` zscale=t=linear:npl=${ npl } ,tonemap=hable:desat=0,zscale=p=bt709:t=bt709:m=bt709:r=tv` ;
713
814describe ( "hdrToSdrToneMapFilter" , ( ) => {
915 it ( "leaves every frame its own tags when the first frame carries them all" , ( ) => {
10- expect ( hdrToSdrToneMapFilter ( PQ , PQ ) ) . toBe ( TONE_MAP ) ;
16+ expect ( hdrToSdrToneMapFilter ( PQ , PQ ) ) . toBe ( toneMap ( 100 ) ) ;
1117 } ) ;
1218
1319 it ( "resizes in the first zscale, before the tone map, when it is given a size" , ( ) => {
@@ -18,10 +24,10 @@ describe("hdrToSdrToneMapFilter", () => {
1824
1925 it ( "sets the probed tags the first frame lacks" , ( ) => {
2026 expect ( hdrToSdrToneMapFilter ( PQ , { } ) ) . toBe (
21- `setparams=colorspace=bt2020nc:color_primaries=bt2020:color_trc=smpte2084,${ TONE_MAP } ` ,
27+ `setparams=colorspace=bt2020nc:color_primaries=bt2020:color_trc=smpte2084,${ toneMap ( 100 ) } ` ,
2228 ) ;
2329 expect ( hdrToSdrToneMapFilter ( PQ , { colorTransfer : "arib-std-b67" } ) ) . toBe (
24- `setparams=colorspace=bt2020nc:color_primaries=bt2020,${ TONE_MAP } ` ,
30+ `setparams=colorspace=bt2020nc:color_primaries=bt2020,${ toneMap ( 100 ) } ` ,
2531 ) ;
2632 } ) ;
2733
@@ -31,9 +37,82 @@ describe("hdrToSdrToneMapFilter", () => {
3137 { colorSpace : "unknown" , colorTransfer : "arib-std-b67" } ,
3238 { colorSpace : "unknown" , colorPrimaries : "unknown" , colorTransfer : "arib-std-b67" } ,
3339 ) ,
34- ) . toBe ( `setparams=colorspace=bt2020nc:color_primaries=bt2020,${ TONE_MAP } ` ) ;
40+ ) . toBe ( `setparams=colorspace=bt2020nc:color_primaries=bt2020,${ toneMap ( 100 ) } ` ) ;
3541 expect ( hdrToSdrToneMapFilter ( { colorPrimaries : "reserved" } , { } ) ) . toBe (
36- `setparams=colorspace=bt2020nc:color_primaries=bt2020,${ TONE_MAP } ` ,
42+ `setparams=colorspace=bt2020nc:color_primaries=bt2020,${ toneMap ( 100 ) } ` ,
3743 ) ;
3844 } ) ;
45+
46+ it ( "maps a stream at BT.2408's 203-nit HLG reference white only when every keyframe is HLG" , ( ) => {
47+ const hlg = { ...PQ , colorTransfer : "arib-std-b67" } ;
48+ const keyframes = ( ...keyframeTransfers : string [ ] ) => ( { ...hlg , keyframeTransfers } ) ;
49+ expect ( hdrToSdrToneMapFilter ( hlg , keyframes ( "arib-std-b67" , "arib-std-b67" ) ) ) . toBe (
50+ toneMap ( 203 ) ,
51+ ) ;
52+ expect ( hdrToSdrToneMapFilter ( hlg , keyframes ( "arib-std-b67" , "smpte2084" ) ) ) . toBe ( toneMap ( 100 ) ) ;
53+ expect ( hdrToSdrToneMapFilter ( hlg , keyframes ( "arib-std-b67" , "bt709" ) ) ) . toBe ( toneMap ( 100 ) ) ;
54+ expect ( hdrToSdrToneMapFilter ( hlg , keyframes ( ) ) ) . toBe ( toneMap ( 100 ) ) ;
55+ const untagged = ( keyframeTransfers : string [ ] ) => ( {
56+ colorTransfer : "unknown" ,
57+ keyframeTransfers,
58+ } ) ;
59+ expect ( hdrToSdrToneMapFilter ( hlg , untagged ( [ "unspecified" , "arib-std-b67" ] ) ) ) . toContain (
60+ "npl=203" ,
61+ ) ;
62+ expect ( hdrToSdrToneMapFilter ( PQ , untagged ( [ "unspecified" ] ) ) ) . toContain ( "npl=100" ) ;
63+ expect ( hdrToSdrToneMapFilter ( PQ , { ...PQ , keyframeTransfers : [ "smpte2084" ] } ) ) . toBe (
64+ toneMap ( 100 ) ,
65+ ) ;
66+ expect ( toneMapReferenceNits ( undefined ) ) . toBe ( 100 ) ;
67+ } ) ;
68+
69+ it ( "names each SPS transfer trace_headers prints, and reads only keyframes' headers" , ( ) => {
70+ const line = ( code : number ) =>
71+ `[trace_headers @ 0x1] 287 transfer_characteristics 00010010 = ${ code } ` ;
72+ expect ( parseKeyframeTransfers ( [ 18 , 16 , 1 , 2 , 4 ] . map ( line ) . join ( "\n" ) ) ) . toEqual ( [
73+ "arib-std-b67" ,
74+ "smpte2084" ,
75+ "bt709" ,
76+ "unspecified" ,
77+ "code 4" ,
78+ ] ) ;
79+ expect ( keyframeTransferArgs ( "clip.mov" , "hevc" ) ) . toContain (
80+ "filter_units=pass_types=33,trace_headers" ,
81+ ) ;
82+ expect ( keyframeTransferArgs ( "clip.mov" ) ) . toEqual ( expect . arrayContaining ( [ "-discard" , "nokey" ] ) ) ;
83+ } ) ;
84+ } ) ;
85+
86+ const FFMPEG_MAJOR = Number (
87+ / f f m p e g v e r s i o n n ? ( \d + ) / . exec ( spawnSync ( "ffmpeg" , [ "-version" ] ) . stdout ?. toString ( ) ?? "" ) ?. [ 1 ] ??
88+ 0 ,
89+ ) ;
90+ const HAS_ZSCALE = / \s z s c a l e \s / . test (
91+ spawnSync ( "ffmpeg" , [ "-hide_banner" , "-filters" ] ) . stdout ?. toString ( ) ?? "" ,
92+ ) ;
93+
94+ /** The 8-bit full-range grey the chain makes of one 10-bit HLG code value. */
95+ function toneMappedGrey ( code : number ) : number {
96+ const hlg = { colorSpace : "bt2020nc" , colorPrimaries : "bt2020" , colorTransfer : "arib-std-b67" } ;
97+ const tags =
98+ "setparams=color_primaries=bt2020:color_trc=arib-std-b67:colorspace=bt2020nc:range=tv" ;
99+ const run = spawnSync ( "ffmpeg" , [
100+ ...[ "-v" , "error" , "-f" , "lavfi" , "-i" , "color=c=black:s=16x16:d=0.04" ] ,
101+ ...[
102+ "-vf" ,
103+ `format=yuv420p10le,geq=lum=${ code } :cb=512:cr=512,${ tags } ,${ hdrToSdrToneMapFilter ( hlg , { ...hlg , keyframeTransfers : [ "arib-std-b67" ] } ) } ` ,
104+ ] ,
105+ ...[ "-frames:v" , "1" , "-pix_fmt" , "gray" , "-f" , "rawvideo" , "pipe:1" ] ,
106+ ] ) ;
107+ const luma = run . stdout [ 0 ] ?? Number . NaN ;
108+ return Math . round ( ( ( luma - 16 ) * 255 ) / 219 ) ;
109+ }
110+
111+ // ffmpeg before 8 bundles an older zimg that maps HLG white near 174, outside the tolerance.
112+ describe . skipIf ( ! HAS_ZSCALE || FFMPEG_MAJOR < 8 ) ( "hdrToSdrToneMapFilter on HLG grey" , ( ) => {
113+ // Apple's own SDR conversion (avconvert) of these 10-bit steps gives 105 and 161; at 100 nits ours gave 142 and 216.
114+ it ( "puts mid grey and reference white where Apple's SDR conversion does" , ( ) => {
115+ expect ( Math . abs ( toneMappedGrey ( 551 ) - 105 ) ) . toBeLessThanOrEqual ( 8 ) ;
116+ expect ( Math . abs ( toneMappedGrey ( 745 ) - 161 ) ) . toBeLessThanOrEqual ( 8 ) ;
117+ } ) ;
39118} ) ;
0 commit comments