Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect hardware accelerated codec and use the correct scale filter #1121

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 29 additions & 10 deletions lib/options/videosize.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,35 @@
*/


function getScaleFilter(output) {
if (output.video.get('-vcodec').length > 1) {
let codec = output.video.get('-vcodec')[1];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such condition would not work in case of the video codec option is not the first one in the list.

The following approach works correct:

if (!!output.video.find('-vcodec', 1)) {
    let codec = output.video.find('-vcodec', 1)[0];


if (codec.endsWith('_vaapi')) {
return 'scale_vaapi';
}
else if (codec.endsWith('_nvenc')) {
return 'scale_npp';
}
else if (codec.endsWith('_qsv')) {
return 'scale_qsv';
}
}
return 'scale';
}

/**
* Return filters to pad video to width*height,
*
* @param {Number} width output width
* @param {Number} height output height
* @param {Number} aspect video aspect ratio (without padding)
* @param {Number} color padding color
* @param {String} scale_filter scale filter name to use
* @return scale/pad filters
* @private
*/
function getScalePadFilters(width, height, aspect, color) {
function getScalePadFilters(width, height, aspect, color, scale_filter) {
/*
let a be the input aspect ratio, A be the requested aspect ratio

Expand All @@ -30,7 +48,7 @@ function getScalePadFilters(width, height, aspect, color) {
When using computed width/height, we truncate them to multiples of 2
*/
{
filter: 'scale',
filter: scale_filter,
options: {
w: 'if(gt(a,' + aspect + '),' + width + ',trunc(' + height + '*a/2)*2)',
h: 'if(lt(a,' + aspect + '),' + height + ',trunc(' + width + '/a/2)*2)'
Expand Down Expand Up @@ -68,6 +86,7 @@ function getScalePadFilters(width, height, aspect, color) {
function createSizeFilters(output, key, value) {
// Store parameters
var data = output.sizeData = output.sizeData || {};
var scale_filter = getScaleFilter(output);
data[key] = value;

if (!('size' in data)) {
Expand All @@ -85,7 +104,7 @@ function createSizeFilters(output, key, value) {
if (percentRatio) {
var ratio = Number(percentRatio[1]) / 100;
return [{
filter: 'scale',
filter: scale_filter,
options: {
w: 'trunc(iw*' + ratio + '/2)*2',
h: 'trunc(ih*' + ratio + '/2)*2'
Expand All @@ -99,10 +118,10 @@ function createSizeFilters(output, key, value) {
aspect = width / height;

if (data.pad) {
return getScalePadFilters(width, height, aspect, data.pad);
return getScalePadFilters(width, height, aspect, data.pad, scale_filter);
} else {
// No autopad requested, rescale to target size
return [{ filter: 'scale', options: { w: width, h: height }}];
return [{ filter: scale_filter, options: { w: width, h: height }}];
}
} else if (fixedWidth || fixedHeight) {
if ('aspect' in data) {
Expand All @@ -115,25 +134,25 @@ function createSizeFilters(output, key, value) {
height = Math.round(height / 2) * 2;

if (data.pad) {
return getScalePadFilters(width, height, data.aspect, data.pad);
return getScalePadFilters(width, height, data.aspect, data.pad, scale_filter);
} else {
// No autopad requested, rescale to target size
return [{ filter: 'scale', options: { w: width, h: height }}];
return [{ filter: scale_filter, options: { w: width, h: height }}];
}
} else {
// Keep input aspect ratio

if (fixedWidth) {
return [{
filter: 'scale',
filter: scale_filter,
options: {
w: Math.round(Number(fixedWidth[1]) / 2) * 2,
h: 'trunc(ow/a/2)*2'
}
}];
} else {
return [{
filter: 'scale',
filter: scale_filter,
options: {
w: 'trunc(oh*a/2)*2',
h: Math.round(Number(fixedHeight[1]) / 2) * 2
Expand Down Expand Up @@ -171,7 +190,7 @@ module.exports = function(proto) {
proto.keepDAR = function() {
return this.videoFilters([
{
filter: 'scale',
filter: getScaleFilter(output),
options: {
w: 'if(gt(sar,1),iw*sar,iw)',
h: 'if(lt(sar,1),ih/sar,ih)'
Expand Down