Skip to content

Commit 9812ca7

Browse files
committed
Add env var MLT_AVFORMAT_HWACCEL_PPS
1 parent 795c2e3 commit 9812ca7

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

src/modules/avformat/producer_avformat.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,6 +1469,32 @@ static int producer_open(
14691469
AVDictionaryEntry *hwaccel = av_dict_get(params, "hwaccel", NULL, 0);
14701470
AVDictionaryEntry *hwaccel_device = av_dict_get(params, "hwaccel_device", NULL, 0);
14711471
const char *hwaccel_env = getenv("MLT_AVFORMAT_HWACCEL");
1472+
const char *pps_env = getenv("MLT_AVFORMAT_HWACCEL_PPS");
1473+
1474+
if (pps_env && self->video_index >= 0) {
1475+
int64_t pps_threshold = strtoll(pps_env, NULL, 10);
1476+
if (pps_threshold > 0) {
1477+
// Calculate PPS while avoiding overflow
1478+
AVStream *stream = self->video_format->streams[self->video_index];
1479+
int64_t width = stream->codecpar->width;
1480+
int64_t height = stream->codecpar->height;
1481+
double fps = av_q2d(guess_frame_rate(self, stream));
1482+
1483+
if (width > 0 && height > 0 && fps > 0.0) {
1484+
// Calculate pixels per second using double to avoid overflow
1485+
double pps = (double) width * (double) height * fps;
1486+
1487+
if (pps > (double) pps_threshold) {
1488+
mlt_log_verbose(MLT_PRODUCER_SERVICE(self->parent),
1489+
"Disabling hwaccel: PPS %.0f exceeds threshold %lld\n",
1490+
pps,
1491+
(long long) pps_threshold);
1492+
hwaccel = NULL;
1493+
hwaccel_env = NULL;
1494+
}
1495+
}
1496+
}
1497+
}
14721498

14731499
if (((hwaccel && hwaccel->value) || hwaccel_env) && !test_open) {
14741500
// Leaving `device=NULL` will cause query string parameter `hwaccel_device` to be ignored

src/modules/avformat/producer_avformat.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ schema_version: 0.3
22
type: producer
33
identifier: avformat
44
title: FFmpeg Reader
5-
version: 4
5+
version: 5
66
copyright: Meltytech, LLC
77
license: LGPLv2.1
88
language: en
@@ -26,8 +26,11 @@ notes: >
2626
producer cache size. One can set the environment variable
2727
MLT_AVFORMAT_PRODUCER_CACHE to a number to override and increase the size of
2828
this cache (or to lower it for limited use cases and seeking to minimize RAM).
29-
One can set the environment variables MLT_AVFORMAT_HWACCEL and
30-
MLT_AVFORMAT_HWACCEL_DEVICE to affect the usage of hwaccel decoding globally.
29+
One can set the environment variables MLT_AVFORMAT_HWACCEL,
30+
MLT_AVFORMAT_HWACCEL_DEVICE, and MLT_AVFORMAT_HWACCEL_PPS to affect the usage
31+
of hwaccel decoding globally. MLT_AVFORMAT_HWACCEL_PPS sets a threshold for
32+
pixels per second (width * height * fps); hwaccel is only used when the
33+
video's PPS is less than or equal to this threshold.
3134
Hardware decoding gracefully falls back to software decoding.
3235
3336
bugs:

0 commit comments

Comments
 (0)