Skip to content

Display and control your Android device

Fail if a forced hardware decoder cannot be used

Romain Vimont912df691ea21
diff --git a/app/src/decoder.c b/app/src/decoder.c
index c6911153..a8dfc569 100644
--- a/app/src/decoder.c
+++ b/app/src/decoder.c
@@ -38,6 +38,11 @@ sc_decoder_open(struct sc_decoder *decoder, const AVCodec *codec,
}
if (decoder->hwdec && !sc_hwdec_configure(decoder->hwdec, decoder->ctx)) {
+ if (decoder->hwdec->hw_forced) {
+ LOGE("Decoder '%s': hardware decoding unavailable", decoder->name);
+ goto error_free_context;
+ }
+
// Use software decoding (the codec context is left untouched)
LOGW("Decoder '%s': hardware decoding unavailable, using software "
"decoding", decoder->name);
diff --git a/app/src/hwdec.c b/app/src/hwdec.c
index 3960b3a8..fe52f13f 100644
--- a/app/src/hwdec.c
+++ b/app/src/hwdec.c
@@ -2,14 +2,17 @@
#include <assert.h>
+#include <libavutil/pixdesc.h>
+
#include "util/log.h"
bool
sc_hwdec_init(struct sc_hwdec *hwdec, enum AVHWDeviceType hw_type,
- SDL_Renderer *renderer) {
+ bool hw_forced, SDL_Renderer *renderer) {
(void) renderer; // only used by some hardware decoders
hwdec->hw_type = hw_type;
+ hwdec->hw_forced = hw_forced;
switch (hw_type) {
case AV_HWDEVICE_TYPE_NONE:
hwdec->name = "software";
@@ -42,6 +45,20 @@ sc_hwdec_is_supported_by_codec(const AVCodec *codec,
}
}
+static enum AVPixelFormat
+sc_hwdec_get_format_forced(AVCodecContext *ctx,
+ const enum AVPixelFormat *formats) {
+ enum AVPixelFormat format = avcodec_default_get_format(ctx, formats);
+ const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
+ if (!desc || !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
+ struct sc_hwdec *hwdec = ctx->opaque;
+ assert(hwdec);
+ LOGE("%s cannot decode this stream", hwdec->name);
+ return AV_PIX_FMT_NONE;
+ }
+ return format;
+}
+
bool
sc_hwdec_configure(struct sc_hwdec *hwdec, AVCodecContext *ctx) {
assert(ctx->codec_type == AVMEDIA_TYPE_VIDEO);
@@ -70,6 +87,13 @@ sc_hwdec_configure(struct sc_hwdec *hwdec, AVCodecContext *ctx) {
// Hardware supporting Main/High can still decode Baseline.
ctx->hwaccel_flags |= AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH;
+ if (hwdec->hw_forced) {
+ // By default, FFmpeg falls back to a software format if the hardware
+ // decoder cannot decode the stream: refuse it
+ ctx->opaque = hwdec;
+ ctx->get_format = sc_hwdec_get_format_forced;
+ }
+
return true;
}
diff --git a/app/src/hwdec.h b/app/src/hwdec.h
index 7960798c..4c09ec05 100644
--- a/app/src/hwdec.h
+++ b/app/src/hwdec.h
@@ -14,12 +14,15 @@ struct sc_hwdec {
const char *name; // must be statically allocated (e.g. a string literal)
enum AVHWDeviceType hw_type;
+ // If true, the hw_type is mandatory, no fallback to software decoding is
+ // permitted
+ bool hw_forced;
AVBufferRef *hw_device_ctx;
};
bool
sc_hwdec_init(struct sc_hwdec *hwdec, enum AVHWDeviceType hw_type,
- SDL_Renderer *renderer);
+ bool hw_forced, SDL_Renderer *renderer);
bool
sc_hwdec_configure(struct sc_hwdec *hwdec, AVCodecContext *ctx);
diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c
index 77a51c27..68e074e8 100644
--- a/app/src/scrcpy.c
+++ b/app/src/scrcpy.c
@@ -343,7 +343,9 @@ sc_init_video_hwdec(struct sc_hwdec *hwdec, enum sc_hwdec_mode mode,
hw_type = sc_screen_get_hw_type(screen);
renderer = screen->renderer;
}
- if (sc_hwdec_init(hwdec, hw_type, renderer)) {
+ bool hw_forced = hw_type != AV_HWDEVICE_TYPE_NONE
+ && mode != SC_HWDEC_MODE_AUTO;
+ if (sc_hwdec_init(hwdec, hw_type, hw_forced, renderer)) {
return true;
}
@@ -366,7 +368,7 @@ sc_init_video_hwdec(struct sc_hwdec *hwdec, enum sc_hwdec_mode mode,
}
// Initialize software decoder
- return sc_hwdec_init(hwdec, AV_HWDEVICE_TYPE_NONE, NULL);
+ return sc_hwdec_init(hwdec, AV_HWDEVICE_TYPE_NONE, false, NULL);
}
enum scrcpy_exit_code