From f8b346a3caa0fa5bc663faeb3bd0aa84ac7c773b Mon Sep 17 00:00:00 2001 From: lyyn <79758863+lyynd@users.noreply.github.com> Date: Tue, 15 Oct 2024 14:09:05 +0200 Subject: [PATCH 1/3] Fix ffmpeg matrices --- server/src/services/media.service.spec.ts | 16 +++++++------- server/src/utils/media.ts | 26 +++++++++++------------ 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/server/src/services/media.service.spec.ts b/server/src/services/media.service.spec.ts index 08e49f088a0bd..ed75a9e284b6b 100644 --- a/server/src/services/media.service.spec.ts +++ b/server/src/services/media.service.spec.ts @@ -410,7 +410,7 @@ describe(MediaService.name, () => { '-frames:v 1', '-update 1', '-v verbose', - String.raw`-vf fps=12:eof_action=pass:round=down,thumbnail=12,select=gt(scene\,0.1)-eq(prev_selected_n\,n)+isnan(prev_selected_n)+gt(n\,20),trim=end_frame=2,reverse,scale=-2:1440:flags=lanczos+accurate_rnd+full_chroma_int:out_color_matrix=601:out_range=pc,format=yuv420p`, + String.raw`-vf fps=12:eof_action=pass:round=down,thumbnail=12,select=gt(scene\,0.1)-eq(prev_selected_n\,n)+isnan(prev_selected_n)+gt(n\,20),trim=end_frame=2,reverse,scale=-2:1440:flags=lanczos+accurate_rnd+full_chroma_int:out_color_matrix=bt601:out_range=pc,format=yuv420p`, ], twoPass: false, }), @@ -445,7 +445,7 @@ describe(MediaService.name, () => { '-frames:v 1', '-update 1', '-v verbose', - String.raw`-vf fps=12:eof_action=pass:round=down,thumbnail=12,select=gt(scene\,0.1)-eq(prev_selected_n\,n)+isnan(prev_selected_n)+gt(n\,20),trim=end_frame=2,reverse,zscale=t=linear:npl=100,tonemap=hable:desat=0,zscale=p=bt709:t=601:m=bt470bg:range=pc,format=yuv420p`, + String.raw`-vf fps=12:eof_action=pass:round=down,thumbnail=12,select=gt(scene\,0.1)-eq(prev_selected_n\,n)+isnan(prev_selected_n)+gt(n\,20),trim=end_frame=2,reverse,zscale=t=linear:npl=100,tonemap=hable:desat=0,zscale=p=709:t=601:m=470bg:range=pc,format=yuv420p`, ], twoPass: false, }), @@ -482,7 +482,7 @@ describe(MediaService.name, () => { '-frames:v 1', '-update 1', '-v verbose', - String.raw`-vf fps=12:eof_action=pass:round=down,thumbnail=12,select=gt(scene\,0.1)-eq(prev_selected_n\,n)+isnan(prev_selected_n)+gt(n\,20),trim=end_frame=2,reverse,zscale=t=linear:npl=100,tonemap=hable:desat=0,zscale=p=bt709:t=601:m=bt470bg:range=pc,format=yuv420p`, + String.raw`-vf fps=12:eof_action=pass:round=down,thumbnail=12,select=gt(scene\,0.1)-eq(prev_selected_n\,n)+isnan(prev_selected_n)+gt(n\,20),trim=end_frame=2,reverse,zscale=t=linear:npl=100,tonemap=hable:desat=0,zscale=p=709:t=601:m=470bg:range=pc,format=yuv420p`, ], twoPass: false, }), @@ -2096,7 +2096,7 @@ describe(MediaService.name, () => { inputOptions: [], outputOptions: expect.arrayContaining([ expect.stringContaining( - 'zscale=t=linear:npl=100,tonemap=hable:desat=0,zscale=p=bt709:t=bt709:m=bt709:range=pc,format=yuv420p', + 'zscale=t=linear:npl=100,tonemap=hable:desat=0,zscale=p=709:t=709:m=709:range=pc,format=yuv420p', ), ]), twoPass: false, @@ -2120,7 +2120,7 @@ describe(MediaService.name, () => { inputOptions: [], outputOptions: expect.arrayContaining([ expect.stringContaining( - 'zscale=t=linear:npl=100,tonemap=hable:desat=0,zscale=p=bt709:t=bt709:m=bt709:range=pc,format=yuv420p', + 'zscale=t=linear:npl=100,tonemap=hable:desat=0,zscale=p=709:t=709:m=709:range=pc,format=yuv420p', ), ]), twoPass: false, @@ -2141,7 +2141,7 @@ describe(MediaService.name, () => { outputOptions: expect.arrayContaining([ '-c:v h264', '-c:a copy', - '-vf zscale=t=linear:npl=100,tonemap=hable:desat=0,zscale=p=bt709:t=bt709:m=bt709:range=pc,format=yuv420p', + '-vf zscale=t=linear:npl=100,tonemap=hable:desat=0,zscale=p=709:t=709:m=709:range=pc,format=yuv420p', ]), twoPass: false, }), @@ -2161,7 +2161,7 @@ describe(MediaService.name, () => { outputOptions: expect.arrayContaining([ '-c:v h264', '-c:a copy', - '-vf zscale=t=linear:npl=100,tonemap=hable:desat=0,zscale=p=bt709:t=bt709:m=bt709:range=pc,format=yuv420p', + '-vf zscale=t=linear:npl=100,tonemap=hable:desat=0,zscale=p=709:t=709:m=709:range=pc,format=yuv420p', ]), twoPass: false, }), @@ -2181,7 +2181,7 @@ describe(MediaService.name, () => { outputOptions: expect.arrayContaining([ '-c:v h264', '-c:a copy', - '-vf zscale=t=linear:npl=250,tonemap=mobius:desat=0,zscale=p=bt709:t=bt709:m=bt709:range=pc,format=yuv420p', + '-vf zscale=t=linear:npl=250,tonemap=mobius:desat=0,zscale=p=709:t=709:m=709:range=pc,format=yuv420p', ]), twoPass: false, }), diff --git a/server/src/utils/media.ts b/server/src/utils/media.ts index 6f0ab4ef81d90..48a4c5c424852 100644 --- a/server/src/utils/media.ts +++ b/server/src/utils/media.ts @@ -270,9 +270,9 @@ export class BaseConfig implements VideoCodecSWConfig { getColors() { return { - primaries: 'bt709', - transfer: 'bt709', - matrix: 'bt709', + primaries: '709', + transfer: '709', + matrix: '709', }; } @@ -424,16 +424,16 @@ export class ThumbnailConfig extends BaseConfig { getScaling(videoStream: VideoStreamInfo) { let options = super.getScaling(videoStream) + ':flags=lanczos+accurate_rnd+full_chroma_int'; if (!this.shouldToneMap(videoStream)) { - options += ':out_color_matrix=601:out_range=pc'; + options += ':out_color_matrix=bt601:out_range=pc'; } return options; } getColors() { return { - primaries: 'bt709', + primaries: '709', transfer: '601', - matrix: 'bt470bg', + matrix: '470bg', }; } } @@ -629,11 +629,11 @@ export class NvencHwDecodeConfig extends NvencSwDecodeConfig { const colors = this.getColors(); const tonemapOptions = [ 'desat=0', - `matrix=${colors.matrix}`, - `primaries=${colors.primaries}`, + `matrix=bt${colors.matrix}`, + `primaries=bt${colors.primaries}`, 'range=pc', `tonemap=${this.config.tonemap}`, - `transfer=${colors.transfer}`, + `transfer=bt${colors.transfer}`, ]; return [`tonemap_cuda=${tonemapOptions.join(':')}`]; @@ -772,11 +772,11 @@ export class QsvHwDecodeConfig extends QsvSwDecodeConfig { const tonemapOptions = [ 'desat=0', 'format=nv12', - `matrix=${colors.matrix}`, - `primaries=${colors.primaries}`, + `matrix=bt${colors.matrix}`, + `primaries=bt${colors.primaries}`, 'range=pc', `tonemap=${this.config.tonemap}`, - `transfer=${colors.transfer}`, + `transfer=bt${colors.transfer}`, ]; return [ @@ -927,7 +927,7 @@ export class RkmppHwDecodeConfig extends RkmppSwDecodeConfig { return [ `scale_rkrga=${this.getScaling(videoStream)}:format=p010:afbc=1`, 'hwmap=derive_device=opencl:mode=read', - `tonemap_opencl=format=nv12:r=pc:p=${colors.primaries}:t=${colors.transfer}:m=${colors.matrix}:tonemap=${this.config.tonemap}:desat=0`, + `tonemap_opencl=format=nv12:r=pc:p=bt${colors.primaries}:t=bt${colors.transfer}:m=bt${colors.matrix}:tonemap=${this.config.tonemap}:desat=0`, 'hwmap=derive_device=rkmpp:mode=write:reverse=1', 'format=drm_prime', ]; From 294e44cba56fc844f3c7bd11a94301e691fd8a60 Mon Sep 17 00:00:00 2001 From: mertalev <101130780+mertalev@users.noreply.github.com> Date: Wed, 16 Oct 2024 14:12:19 -0400 Subject: [PATCH 2/3] override `getColors` --- server/src/utils/media.ts | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/server/src/utils/media.ts b/server/src/utils/media.ts index 48a4c5c424852..f138957e8de34 100644 --- a/server/src/utils/media.ts +++ b/server/src/utils/media.ts @@ -629,11 +629,11 @@ export class NvencHwDecodeConfig extends NvencSwDecodeConfig { const colors = this.getColors(); const tonemapOptions = [ 'desat=0', - `matrix=bt${colors.matrix}`, - `primaries=bt${colors.primaries}`, + `matrix=${colors.matrix}`, + `primaries=${colors.primaries}`, 'range=pc', `tonemap=${this.config.tonemap}`, - `transfer=bt${colors.transfer}`, + `transfer=${colors.transfer}`, ]; return [`tonemap_cuda=${tonemapOptions.join(':')}`]; @@ -646,6 +646,14 @@ export class NvencHwDecodeConfig extends NvencSwDecodeConfig { getOutputThreadOptions() { return []; } + + getColors() { + return { + primaries: 'bt709', + transfer: 'bt709', + matrix: 'bt709', + } + } } export class QsvSwDecodeConfig extends BaseHWConfig { @@ -772,11 +780,11 @@ export class QsvHwDecodeConfig extends QsvSwDecodeConfig { const tonemapOptions = [ 'desat=0', 'format=nv12', - `matrix=bt${colors.matrix}`, - `primaries=bt${colors.primaries}`, + `matrix=${colors.matrix}`, + `primaries=${colors.primaries}`, 'range=pc', `tonemap=${this.config.tonemap}`, - `transfer=bt${colors.transfer}`, + `transfer=${colors.transfer}`, ]; return [ @@ -789,6 +797,14 @@ export class QsvHwDecodeConfig extends QsvSwDecodeConfig { getInputThreadOptions() { return [`-threads 1`]; } + + getColors() { + return { + primaries: 'bt709', + transfer: 'bt709', + matrix: 'bt709', + } + } } export class VAAPIConfig extends BaseHWConfig { @@ -927,7 +943,7 @@ export class RkmppHwDecodeConfig extends RkmppSwDecodeConfig { return [ `scale_rkrga=${this.getScaling(videoStream)}:format=p010:afbc=1`, 'hwmap=derive_device=opencl:mode=read', - `tonemap_opencl=format=nv12:r=pc:p=bt${colors.primaries}:t=bt${colors.transfer}:m=bt${colors.matrix}:tonemap=${this.config.tonemap}:desat=0`, + `tonemap_opencl=format=nv12:r=pc:p=${colors.primaries}:t=${colors.transfer}:m=${colors.matrix}:tonemap=${this.config.tonemap}:desat=0`, 'hwmap=derive_device=rkmpp:mode=write:reverse=1', 'format=drm_prime', ]; @@ -936,4 +952,12 @@ export class RkmppHwDecodeConfig extends RkmppSwDecodeConfig { } return []; } + + getColors() { + return { + primaries: 'bt709', + transfer: 'bt709', + matrix: 'bt709', + } + } } From fc39a54b0d1ecc4b3359ce0daf7972eca48aa11a Mon Sep 17 00:00:00 2001 From: mertalev <101130780+mertalev@users.noreply.github.com> Date: Wed, 16 Oct 2024 14:18:21 -0400 Subject: [PATCH 3/3] formatting --- server/src/utils/media.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/src/utils/media.ts b/server/src/utils/media.ts index f138957e8de34..896ccc82369ba 100644 --- a/server/src/utils/media.ts +++ b/server/src/utils/media.ts @@ -652,7 +652,7 @@ export class NvencHwDecodeConfig extends NvencSwDecodeConfig { primaries: 'bt709', transfer: 'bt709', matrix: 'bt709', - } + }; } } @@ -803,7 +803,7 @@ export class QsvHwDecodeConfig extends QsvSwDecodeConfig { primaries: 'bt709', transfer: 'bt709', matrix: 'bt709', - } + }; } } @@ -958,6 +958,6 @@ export class RkmppHwDecodeConfig extends RkmppSwDecodeConfig { primaries: 'bt709', transfer: 'bt709', matrix: 'bt709', - } + }; } }