change from JS to TS, adapt to allow software decode

This commit is contained in:
Théo Le Calvar 2023-05-06 16:54:41 +02:00
parent 303f96a139
commit 2f012ad75e
6 changed files with 8782 additions and 182 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules/
dist/

177
main.js
View File

@ -1,177 +0,0 @@
async function register ({
transcodingManager,
peertubeHelpers
}) {
const { logger } = peertubeHelpers
const initVaapiOptions = [
// enable hardware acceleration
'-hwaccel vaapi',
'-hwaccel_output_format vaapi',
'-vaapi_device /dev/dri/renderD128'
]
let latestStreamNum = 9999
// Add hardware encode through vaapi
{
const VODBuilder = (options) => {
const { input, resolution, fps, streamNum } = options
const streamSuffix = streamNum == undefined ? '' : `:${streamNum}`
const targetBitrate = getTargetBitrate(resolution, fps)
let shouldInitVaapi = (streamNum == undefined || streamNum <= latestStreamNum)
if (shouldInitVaapi && streamNum != undefined) {
latestStreamNum = streamNum
}
// You can also return a promise
return {
scaleFilter: {
name: 'scale_vaapi'
},
inputOptions: shouldInitVaapi ? initVaapiOptions : [],
outputOptions: [
'-bf 8', // override hardcoded bf value which cause memory error
'-pix_fmt vaapi_vld',
`-preset veryfast`,
`-b:v${streamSuffix} ${targetBitrate}`,
`-maxrate ${targetBitrate}`,
`-bufsize ${targetBitrate * 2}`
]
}
}
const liveBuilder = (options) => {
const { input, resolution, fps, streamNum } = options
const streamSuffix = streamNum == undefined ? '' : `:${streamNum}`
const targetBitrate = getTargetBitrate(resolution, fps)
let shouldInitVaapi = (streamNum == undefined || streamNum <= latestStreamNum)
if (shouldInitVaapi && streamNum != undefined) {
latestStreamNum = streamNum
}
// You can also return a promise
return {
scaleFilter: {
name: 'scale_vaapi'
},
inputOptions: shouldInitVaapi ? initVaapiOptions : [],
outputOptions: [
'-bf 8', // override hardcoded bf value which cause memory error
'-pix_fmt vaapi_vld',
`-preset veryfast`,
`-r:v${streamSuffix} ${fps}`,
`-profile:v${streamSuffix} high`,
`-level:v${streamSuffix} 3.1`,
`-g:v${streamSuffix} ${fps*2}`,
`-b:v${streamSuffix} ${targetBitrate}`,
`-maxrate ${targetBitrate}`,
`-bufsize ${targetBitrate * 2}`
]
}
}
const encoder = 'h264_vaapi'
const profileName = 'vaapi'
// Support this profile for VOD transcoding
transcodingManager.addVODProfile(encoder, profileName, VODBuilder)
transcodingManager.addVODEncoderPriority('video', encoder, 1000)
// And/Or support this profile for live transcoding
transcodingManager.addLiveProfile(encoder, profileName, liveBuilder)
transcodingManager.addLiveEncoderPriority('video', encoder, 1000)
}
}
async function unregister() {
transcodingManager.removeAllProfilesAndEncoderPriorities()
return true;
}
module.exports = {
register,
unregister
}
// copied from Peertube, is it possible to import it instead of copying it ?
/**
* Bitrate targets for different resolutions, at VideoTranscodingFPS.AVERAGE.
*
* Sources for individual quality levels:
* Google Live Encoder: https://support.google.com/youtube/answer/2853702?hl=en
* YouTube Video Info: youtube-dl --list-formats, with sample videos
*/
function getBaseBitrate (resolution) {
if (resolution === 0) {
// audio-only
return 64 * 1000
}
if (resolution <= 240) {
// quality according to Google Live Encoder: 300 - 700 Kbps
// Quality according to YouTube Video Info: 285 Kbps
return 320 * 1000
}
if (resolution <= 360) {
// quality according to Google Live Encoder: 400 - 1,000 Kbps
// Quality according to YouTube Video Info: 700 Kbps
return 780 * 1000
}
if (resolution <= 480) {
// quality according to Google Live Encoder: 500 - 2,000 Kbps
// Quality according to YouTube Video Info: 1300 Kbps
return 1500 * 1000
}
if (resolution <= 720) {
// quality according to Google Live Encoder: 1,500 - 4,000 Kbps
// Quality according to YouTube Video Info: 2680 Kbps
return 2800 * 1000
}
if (resolution <= 1080) {
// quality according to Google Live Encoder: 3000 - 6000 Kbps
// Quality according to YouTube Video Info: 5081 Kbps
return 5200 * 1000
}
if (resolution <= 1440) {
// quality according to Google Live Encoder: 6000 - 13000 Kbps
// Quality according to YouTube Video Info: 8600 (av01) - 17000 (vp9.2) Kbps
return 10_000 * 1000
}
// 4K
// quality according to Google Live Encoder: 13000 - 34000 Kbps
return 22_000 * 1000
}
/**
* Calculate the target bitrate based on video resolution and FPS.
*
* The calculation is based on two values:
* Bitrate at VideoTranscodingFPS.AVERAGE is always the same as
* getBaseBitrate(). Bitrate at VideoTranscodingFPS.MAX is always
* getBaseBitrate() * 1.4. All other values are calculated linearly
* between these two points.
*/
function getTargetBitrate (resolution, fps) {
const baseBitrate = getBaseBitrate(resolution)
// The maximum bitrate, used when fps === VideoTranscodingFPS.MAX
// Based on numbers from Youtube, 60 fps bitrate divided by 30 fps bitrate:
// 720p: 2600 / 1750 = 1.49
// 1080p: 4400 / 3300 = 1.33
const maxBitrate = baseBitrate * 1.4
const maxBitrateDifference = maxBitrate - baseBitrate
const maxFpsDifference = 60 - 30
// For 1080p video with default settings, this results in the following formula:
// 3300 + (x - 30) * (1320/30)
// Example outputs:
// 1080p10: 2420 kbps, 1080p30: 3300 kbps, 1080p60: 4620 kbps
// 720p10: 1283 kbps, 720p30: 1750 kbps, 720p60: 2450 kbps
return Math.floor(baseBitrate + (fps - 30) * (maxBitrateDifference / maxFpsDifference))
}

8559
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,10 +1,10 @@
{
"name": "peertube-plugin-hardware-transcode-vaapi",
"version": "0.1.0",
"version": "0.2.0",
"license": "MIT",
"description": "Plugin that adds transcode profiles which use vaapi for hardware acceleration",
"engine": {
"peertube": ">=3.2.0"
"peertube": ">=5.1.0"
},
"keywords": [
"peertube",
@ -13,9 +13,18 @@
"homepage": "https://git.kher.nl/tlc/peertube-plugin-hardware-transcode-vaapi",
"author": "gdsn",
"bugs": "https://git.kher.nl/tlc/peertube-plugin-hardware-transcode-vaapi",
"library": "./main.js",
"library": "./dist/main.js",
"staticDirs": {},
"css": [],
"clientScripts": [],
"translations": {}
"translations": {},
"scripts": {
"clean": "rm -rf dist/*",
"build": "npx tsc --build src/tsconfig.json"
},
"devDependencies": {
"@peertube/peertube-types": "^5.1.0",
"@tsconfig/node16": "^1.0.3",
"typescript": "^5.0.4"
}
}

185
src/main.ts Normal file
View File

@ -0,0 +1,185 @@
import { PluginTranscodingManager } from "@peertube/peertube-types"
import { EncoderOptions, EncoderOptionsBuilderParams, RegisterServerOptions, VideoResolution } from "@peertube/peertube-types"
import { Logger } from 'winston'
let logger : Logger
let transcodingManager : PluginTranscodingManager
export async function register(options :RegisterServerOptions) {
logger = options.peertubeHelpers.logger
transcodingManager = options.transcodingManager
logger.info("Registering peertube-plugin-hardware-encode");
const encoder = 'h264_vaapi'
const profileName = 'vaapi'
logger.info("adding VOD profile: " + transcodingManager.addVODProfile(encoder, profileName, vodBuilder))
transcodingManager.addVODEncoderPriority('video', encoder, 1000)
logger.info("adding live profile: " + transcodingManager.addLiveProfile(encoder, profileName, liveBuilder))
transcodingManager.addLiveEncoderPriority('video', encoder, 1000)
}
export async function unregister() {
logger.info("Unregistering peertube-plugin-hardware-encode")
transcodingManager.removeAllProfilesAndEncoderPriorities()
return true
}
const initVaapiOptions = [
// enable hardware acceleration
'-vaapi_device /dev/dri/renderD128'
]
let latestStreamNum = 9999
async function vodBuilder(params: EncoderOptionsBuilderParams) : Promise<EncoderOptions> {
const { resolution, fps, streamNum } = params
const streamSuffix = streamNum == undefined ? '' : `:${streamNum}`
const targetBitrate = getTargetBitrate(resolution, fps)
let shouldInitVaapi = (streamNum == undefined || streamNum <= latestStreamNum)
logger.info(`Building encoder options, received ${JSON.stringify(params)}`)
if (shouldInitVaapi && streamNum != undefined) {
latestStreamNum = streamNum
}
// You can also return a promise
let options : EncoderOptions = {
scaleFilter: {
name: 'format=nv12,hwupload,scale_vaapi'
},
inputOptions: shouldInitVaapi ? initVaapiOptions : [],
outputOptions: [
'-bf 8', // override hardcoded bf value which cause memory error
`-preset veryfast`,
`-b:v${streamSuffix} ${targetBitrate}`,
`-maxrate ${targetBitrate}`,
`-bufsize ${targetBitrate * 2}`
]
}
logger.info(`EncoderOptions: ${JSON.stringify(options)}`)
return options
}
async function liveBuilder(params: EncoderOptionsBuilderParams) : Promise<EncoderOptions> {
const { resolution, fps, streamNum } = params
const streamSuffix = streamNum == undefined ? '' : `:${streamNum}`
const targetBitrate = getTargetBitrate(resolution, fps)
let shouldInitVaapi = (streamNum == undefined || streamNum <= latestStreamNum)
logger.info(`Building encoder options, received ${JSON.stringify(params)}`)
if (shouldInitVaapi && streamNum != undefined) {
latestStreamNum = streamNum
}
// You can also return a promise
const options = {
scaleFilter: {
name: 'scale_vaapi'
},
inputOptions: shouldInitVaapi ? initVaapiOptions : [],
outputOptions: [
'-bf 8', // override hardcoded bf value which cause memory error
`-preset veryfast`,
`-r:v${streamSuffix} ${fps}`,
`-profile:v${streamSuffix} high`,
`-level:v${streamSuffix} 3.1`,
`-g:v${streamSuffix} ${fps*2}`,
`-b:v${streamSuffix} ${targetBitrate}`,
`-maxrate ${targetBitrate}`,
`-bufsize ${targetBitrate * 2}`
]
}
logger.info(`EncoderOptions: ${JSON.stringify(options)}`)
return options
}
// copied from Peertube, is it possible to import it instead of copying it ?
/**
* Bitrate targets for different resolutions, at VideoTranscodingFPS.AVERAGE.
*
* Sources for individual quality levels:
* Google Live Encoder: https://support.google.com/youtube/answer/2853702?hl=en
* YouTube Video Info: youtube-dl --list-formats, with sample videos
*/
function getBaseBitrate (resolution : VideoResolution) : number {
if (resolution === 0) {
// audio-only
return 64 * 1000
}
if (resolution <= 240) {
// quality according to Google Live Encoder: 300 - 700 Kbps
// Quality according to YouTube Video Info: 285 Kbps
return 320 * 1000
}
if (resolution <= 360) {
// quality according to Google Live Encoder: 400 - 1,000 Kbps
// Quality according to YouTube Video Info: 700 Kbps
return 780 * 1000
}
if (resolution <= 480) {
// quality according to Google Live Encoder: 500 - 2,000 Kbps
// Quality according to YouTube Video Info: 1300 Kbps
return 1500 * 1000
}
if (resolution <= 720) {
// quality according to Google Live Encoder: 1,500 - 4,000 Kbps
// Quality according to YouTube Video Info: 2680 Kbps
return 2800 * 1000
}
if (resolution <= 1080) {
// quality according to Google Live Encoder: 3000 - 6000 Kbps
// Quality according to YouTube Video Info: 5081 Kbps
return 5200 * 1000
}
if (resolution <= 1440) {
// quality according to Google Live Encoder: 6000 - 13000 Kbps
// Quality according to YouTube Video Info: 8600 (av01) - 17000 (vp9.2) Kbps
return 10_000 * 1000
}
// 4K
// quality according to Google Live Encoder: 13000 - 34000 Kbps
return 22_000 * 1000
}
/**
* Calculate the target bitrate based on video resolution and FPS.
*
* The calculation is based on two values:
* Bitrate at VideoTranscodingFPS.AVERAGE is always the same as
* getBaseBitrate(). Bitrate at VideoTranscodingFPS.MAX is always
* getBaseBitrate() * 1.4. All other values are calculated linearly
* between these two points.
*/
function getTargetBitrate (resolution : VideoResolution, fps : number) : number {
const baseBitrate = getBaseBitrate(resolution)
// The maximum bitrate, used when fps === VideoTranscodingFPS.MAX
// Based on numbers from Youtube, 60 fps bitrate divided by 30 fps bitrate:
// 720p: 2600 / 1750 = 1.49
// 1080p: 4400 / 3300 = 1.33
const maxBitrate = baseBitrate * 1.4
const maxBitrateDifference = maxBitrate - baseBitrate
const maxFpsDifference = 60 - 30
// For 1080p video with default settings, this results in the following formula:
// 3300 + (x - 30) * (1320/30)
// Example outputs:
// 1080p10: 2420 kbps, 1080p30: 3300 kbps, 1080p60: 4620 kbps
// 720p10: 1283 kbps, 720p30: 1750 kbps, 720p60: 2450 kbps
return Math.floor(baseBitrate + (fps - 30) * (maxBitrateDifference / maxFpsDifference))
}

24
src/tsconfig.json Normal file
View File

@ -0,0 +1,24 @@
{
"extends": "@tsconfig/node16/tsconfig.json",
"compilerOptions": {
"moduleResolution": "node", // Tell tsc to look in node_modules for modules
"strict": true, // That implies alwaysStrict, noImplicitAny, noImplicitThis
"alwaysStrict": true, // should already be true because of strict:true
"noImplicitAny": true, // should already be true because of strict:true
"noImplicitThis": true, // should already be true because of strict:true
"noImplicitReturns": true,
"strictBindCallApply": true, // should already be true because of strict:true
"noUnusedLocals": true,
"removeComments": true,
"sourceMap": true,
"outDir": "../dist/",
"paths": {}
},
"include": [
"./**/*"
],
"exclude": []
}