Skip to content

Commit

Permalink
Merge pull request #388 from jellyfin/prepare-vt-trickplay
Browse files Browse the repository at this point in the history
lavc/videotoolbox: prepare for trickplay
  • Loading branch information
nyanmisaka authored May 25, 2024
2 parents 7d10d6d + f5a5027 commit df454b6
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 13 deletions.
1 change: 1 addition & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
jellyfin-ffmpeg (6.0.1-7) unstable; urgency=medium

* Add DOVI descriptor support in mpegtsenc
* Add low priority decoding and key-frame only decoding to VideoToolbox
* Sync RKMPP fixes from ffmpeg-rockchip
* Fix a typo in the CUDA/OpenCL transpose filter
* Fix some warnings-as-errors in GCC 14
Expand Down
30 changes: 17 additions & 13 deletions debian/patches/0072-add-mjpeg-videotoolbox.patch
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ Index: FFmpeg/libavcodec/videotoolboxenc.c
===================================================================
--- FFmpeg.orig/libavcodec/videotoolboxenc.c
+++ FFmpeg/libavcodec/videotoolboxenc.c
@@ -547,6 +547,7 @@ static CMVideoCodecType get_cm_codec_typ
@@ -547,6 +547,7 @@ static CMVideoCodecType get_cm_codec_type(AVCodecContext *avctx,
else
return MKBETAG('a','p','c','n'); // kCMVideoCodecType_AppleProRes422
}
+ case AV_CODEC_ID_MJPEG: return kCMVideoCodecType_JPEG;
default: return 0;
}
}
@@ -1233,7 +1234,7 @@ static int vtenc_create_encoder(AVCodecC
@@ -1233,7 +1234,7 @@ static int vtenc_create_encoder(AVCodecContext *avctx,
kVTCompressionPropertyKey_Quality,
quality_num);
CFRelease(quality_num);
Expand All @@ -58,28 +58,28 @@ Index: FFmpeg/libavcodec/videotoolboxenc.c
bit_rate_num = CFNumberCreate(kCFAllocatorDefault,
kCFNumberSInt32Type,
&bit_rate);
@@ -1347,7 +1348,7 @@ static int vtenc_create_encoder(AVCodecC
@@ -1347,7 +1348,7 @@ static int vtenc_create_encoder(AVCodecContext *avctx,
}
}

- if (avctx->gop_size > 0 && avctx->codec_id != AV_CODEC_ID_PRORES) {
+ if (avctx->gop_size > 0 && avctx->codec_id != AV_CODEC_ID_PRORES && avctx->codec_id != AV_CODEC_ID_MJPEG) {
CFNumberRef interval = CFNumberCreate(kCFAllocatorDefault,
kCFNumberIntType,
&avctx->gop_size);
@@ -1496,7 +1497,7 @@ static int vtenc_create_encoder(AVCodecC
@@ -1496,7 +1497,7 @@ static int vtenc_create_encoder(AVCodecContext *avctx,
}
}

- if (!vtctx->has_b_frames && avctx->codec_id != AV_CODEC_ID_PRORES) {
+ if (!vtctx->has_b_frames && avctx->codec_id != AV_CODEC_ID_PRORES && avctx->codec_id != AV_CODEC_ID_MJPEG) {
status = VTSessionSetProperty(vtctx->session,
kVTCompressionPropertyKey_AllowFrameReordering,
kCFBooleanFalse);
@@ -2844,6 +2845,13 @@ static const enum AVPixelFormat prores_p
@@ -2844,6 +2845,13 @@ static const enum AVPixelFormat prores_pix_fmts[] = {
AV_PIX_FMT_NONE
};

+static const enum AVPixelFormat mjpeg_pix_fmts[] = {
+ AV_PIX_FMT_VIDEOTOOLBOX,
+ AV_PIX_FMT_NV12,
Expand All @@ -90,16 +90,20 @@ Index: FFmpeg/libavcodec/videotoolboxenc.c
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
#define COMMON_OPTIONS \
{ "allow_sw", "Allow software encoding", OFFSET(allow_sw), AV_OPT_TYPE_BOOL, \
@@ -3003,4 +3011,29 @@ const FFCodec ff_prores_videotoolbox_enc
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
@@ -3004,3 +3012,33 @@ const FFCodec ff_prores_videotoolbox_encoder = {
.p.wrapper_name = "videotoolbox",
.hw_configs = vt_encode_hw_configs,
};
+
+static const AVOption mjpeg_options[] = {
+ { "allow_sw", "Allow software encoding", OFFSET(allow_sw), AV_OPT_TYPE_BOOL,{ .i64 = 0 }, 0, 1, VE },
+ { NULL },
+};
+
+static const AVClass mjpeg_videotoolbox_class = {
+ .class_name = "mjpeg_videotoolbox",
+ .item_name = av_default_item_name,
+ .option = NULL,
+ .option = mjpeg_options,
+ .version = LIBAVUTIL_VERSION_INT,
+};
+
Expand All @@ -111,12 +115,12 @@ Index: FFmpeg/libavcodec/videotoolboxenc.c
+ .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
+ AV_CODEC_CAP_HARDWARE,
+ .priv_data_size = sizeof(VTEncContext),
+ .p.pix_fmts = prores_pix_fmts,
+ .p.pix_fmts = mjpeg_pix_fmts,
+ .init = vtenc_init,
+ FF_CODEC_ENCODE_CB(vtenc_frame),
+ .close = vtenc_close,
+ .p.priv_class = &mjpeg_videotoolbox_class,
+ .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
+ .p.wrapper_name = "videotoolbox",
+ .hw_configs = vt_encode_hw_configs,
};
+};
72 changes: 72 additions & 0 deletions debian/patches/0076-vt-low-priority-keyframe-decoding.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
Subject: [PATCH] lavcvideotoolbox: Add low_priority key_frame_only decoding
---
Index: libavcodec/avcodec.h
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
--- a/libavcodec/avcodec.h (revision 019c5b13ace26af67613d27f333104e90bde8b31)
+++ b/libavcodec/avcodec.h (revision c58e99fd3e45d898d4726456f6bddb0453592bac)
@@ -2275,6 +2275,13 @@
*/
#define AV_HWACCEL_FLAG_UNSAFE_OUTPUT (1 << 3)

+/**
+ * Some hardware decoders (like VideoToolbox) supports decode session priority
+ * that run decode pipeline at a lower priority than is used for realtime decoding.
+ * This will be useful for background processing without interrupting normal playback.
+ */
+#define AV_HWACCEL_FLAG_LOW_PRIORITY (1 << 4)
+
/**
* @}
*/
Index: libavcodec/options_table.h
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/libavcodec/options_table.h b/libavcodec/options_table.h
--- a/libavcodec/options_table.h (revision 019c5b13ace26af67613d27f333104e90bde8b31)
+++ b/libavcodec/options_table.h (revision c58e99fd3e45d898d4726456f6bddb0453592bac)
@@ -397,6 +397,7 @@
{"unsafe_output", "allow potentially unsafe hwaccel frame output that might require special care to process successfully", 0, AV_OPT_TYPE_CONST, {.i64 = AV_HWACCEL_FLAG_UNSAFE_OUTPUT }, INT_MIN, INT_MAX, V | D, "hwaccel_flags"},
{"extra_hw_frames", "Number of extra hardware frames to allocate for the user", OFFSET(extra_hw_frames), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, V|D },
{"discard_damaged_percentage", "Percentage of damaged samples to discard a frame", OFFSET(discard_damaged_percentage), AV_OPT_TYPE_INT, {.i64 = 95 }, 0, 100, V|D },
+{"low_priority", "attempt to run decode pipeline at a lower priority than is used for realtime decoding", 0, AV_OPT_TYPE_CONST, {.i64 = AV_HWACCEL_FLAG_LOW_PRIORITY }, INT_MIN, INT_MAX, V | D, "hwaccel_flags"},
{NULL},
};

Index: libavcodec/videotoolbox.c
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/libavcodec/videotoolbox.c b/libavcodec/videotoolbox.c
--- a/libavcodec/videotoolbox.c (revision 019c5b13ace26af67613d27f333104e90bde8b31)
+++ b/libavcodec/videotoolbox.c (revision c58e99fd3e45d898d4726456f6bddb0453592bac)
@@ -983,6 +983,23 @@
av_log(avctx, AV_LOG_ERROR, "VideoToolbox reported invalid data.\n");
return AVERROR_INVALIDDATA;
case 0:
+ if (avctx->skip_frame >= AVDISCARD_NONKEY) {
+ status = VTSessionSetProperty(videotoolbox->session,
+ kVTDecompressionPropertyKey_OnlyTheseFrames,
+ kVTDecompressionProperty_OnlyTheseFrames_KeyFrames);
+ if (status) {
+ av_log(avctx, AV_LOG_WARNING, "kVTDecompressionProperty_OnlyTheseFrames_KeyFrames is not supported on this device. Ignoring.\n");
+ }
+ }
+ if (avctx->hwaccel_flags & AV_HWACCEL_FLAG_LOW_PRIORITY) {
+ status = VTSessionSetProperty(videotoolbox->session,
+ kVTDecompressionPropertyKey_RealTime,
+ kCFBooleanFalse);
+ av_log(avctx, AV_LOG_INFO, "Decoder running at lower priority.\n");
+ if (status) {
+ av_log(avctx, AV_LOG_WARNING, "kVTDecompressionPropertyKey_RealTime is not supported on this device. Ignoring.\n");
+ }
+ }
return 0;
default:
av_log(avctx, AV_LOG_ERROR, "Unknown VideoToolbox session creation error %d\n", (int)status);
1 change: 1 addition & 0 deletions debian/patches/series
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,4 @@
0073-opus-allow-5point1-side-inputs.patch
0074-add-dovi-descriptor-support-in-mpegtsenc.patch
0075-fix-gcc-14-warnings-in-qsv-hwcontext.patch
0076-vt-low-priority-keyframe-decoding.patch

0 comments on commit df454b6

Please sign in to comment.