diff --git a/libavdevice/decklink_common.cpp b/libavdevice/decklink_common.cpp
index 82b3a0c..a75ccc1 100644
--- a/libavdevice/decklink_common.cpp
+++ b/libavdevice/decklink_common.cpp
@@ -33,6 +33,7 @@ extern "C" {
 #include "libavformat/avformat.h"
 #include "libavformat/internal.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/intreadwrite.h"
 #include "libavutil/bswap.h"
 }
 
@@ -158,8 +159,8 @@ int ff_decklink_set_format(AVFormatContext *avctx,
     int i = 1;
     HRESULT res;
 
-    av_log(avctx, AV_LOG_DEBUG, "Trying to find mode for frame size %dx%d, frame timing %d/%d, field order %d, direction %d, mode number %d\n",
-        width, height, tb_num, tb_den, field_order, direction, num);
+    av_log(avctx, AV_LOG_DEBUG, "Trying to find mode for frame size %dx%d, frame timing %d/%d, field order %d, direction %d, mode number %d, format code %s\n",
+        width, height, tb_num, tb_den, field_order, direction, num, (cctx->format_code) ? cctx->format_code : "(unset)");
 
     if (ctx->duplex_mode) {
         DECKLINK_BOOL duplex_supported = false;
@@ -196,12 +197,18 @@ int ff_decklink_set_format(AVFormatContext *avctx,
             return AVERROR(EIO);
     }
 
+    BMDDisplayMode target_mode = 0;
+    char format_buf[] = "     ";
+    if (cctx->format_code)
+        av_strlcpy(format_buf, cctx->format_code, sizeof(format_buf));
+    target_mode = av_bswap32(AV_RL32(format_buf));
     AVRational target_tb = av_make_q(tb_num, tb_den);
     ctx->bmd_mode = bmdModeUnknown;
     while ((ctx->bmd_mode == bmdModeUnknown) && itermode->Next(&mode) == S_OK) {
         BMDTimeValue bmd_tb_num, bmd_tb_den;
         int bmd_width  = mode->GetWidth();
         int bmd_height = mode->GetHeight();
+        BMDDisplayMode bmd_mode = mode->GetDisplayMode();
         BMDFieldDominance bmd_field_dominance = mode->GetFieldDominance();
 
         mode->GetFrameRate(&bmd_tb_num, &bmd_tb_den);
@@ -210,8 +217,10 @@ int ff_decklink_set_format(AVFormatContext *avctx,
         if ((bmd_width == width &&
              bmd_height == height &&
              !av_cmp_q(mode_tb, target_tb) &&
-             field_order_eq(field_order, bmd_field_dominance)) || i == num) {
-            ctx->bmd_mode   = mode->GetDisplayMode();
+             field_order_eq(field_order, bmd_field_dominance))
+             || i == num
+             || target_mode == bmd_mode) {
+            ctx->bmd_mode   = bmd_mode;
             ctx->bmd_width  = bmd_width;
             ctx->bmd_height = bmd_height;
             ctx->bmd_tb_den = bmd_tb_den;
diff --git a/libavdevice/decklink_common_c.h b/libavdevice/decklink_common_c.h
index d565631..72c5f9a 100644
--- a/libavdevice/decklink_common_c.h
+++ b/libavdevice/decklink_common_c.h
@@ -47,6 +47,7 @@ struct decklink_cctx {
     int audio_input;
     int video_input;
     int draw_bars;
+    char *format_code;
 };
 
 #endif /* AVDEVICE_DECKLINK_COMMON_C_H */
diff --git a/libavdevice/decklink_dec.cpp b/libavdevice/decklink_dec.cpp
index 7df841b..ffe65db 100644
--- a/libavdevice/decklink_dec.cpp
+++ b/libavdevice/decklink_dec.cpp
@@ -539,9 +539,10 @@ av_cold int ff_decklink_read_header(AVFormatContext *avctx)
         goto error;
     }
 
-    if (mode_num > 0) {
+    if (mode_num > 0 || cctx->format_code) {
         if (ff_decklink_set_format(avctx, DIRECTION_IN, mode_num) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "Could not set mode %d for %s\n", mode_num, fname);
+            av_log(avctx, AV_LOG_ERROR, "Could not set mode number %d or format code %s for %s\n",
+                mode_num, (cctx->format_code) ? cctx->format_code : "(unset)", fname);
             ret = AVERROR(EIO);
             goto error;
         }
diff --git a/libavdevice/decklink_dec_c.c b/libavdevice/decklink_dec_c.c
index 31818d2..d4ae4e5 100644
--- a/libavdevice/decklink_dec_c.c
+++ b/libavdevice/decklink_dec_c.c
@@ -31,6 +31,7 @@
 static const AVOption options[] = {
     { "list_devices", "list available devices"  , OFFSET(list_devices), AV_OPT_TYPE_INT   , { .i64 = 0   }, 0, 1, DEC },
     { "list_formats", "list supported formats"  , OFFSET(list_formats), AV_OPT_TYPE_INT   , { .i64 = 0   }, 0, 1, DEC },
+    { "format_code",  "set format by fourcc"    , OFFSET(format_code),  AV_OPT_TYPE_STRING, { .str = NULL}, 3, 4, DEC },
     { "bm_v210",      "v210 10 bit per channel" , OFFSET(v210),         AV_OPT_TYPE_INT   , { .i64 = 0   }, 0, 1, DEC },
     { "teletext_lines", "teletext lines bitmask", OFFSET(teletext_lines), AV_OPT_TYPE_INT64, { .i64 = 0   }, 0, 0x7ffffffffLL, DEC, "teletext_lines"},
     { "standard",     NULL,                                           0,  AV_OPT_TYPE_CONST, { .i64 = 0x7fff9fffeLL}, 0, 0,    DEC, "teletext_lines"},
diff --git a/libavdevice/decklink_enc_c.c b/libavdevice/decklink_enc_c.c
index 03734f8..76a040c 100644
--- a/libavdevice/decklink_enc_c.c
+++ b/libavdevice/decklink_enc_c.c
@@ -30,6 +30,7 @@
 static const AVOption options[] = {
     { "list_devices", "list available devices"  , OFFSET(list_devices), AV_OPT_TYPE_INT   , { .i64 = 0   }, 0, 1, ENC },
     { "list_formats", "list supported formats"  , OFFSET(list_formats), AV_OPT_TYPE_INT   , { .i64 = 0   }, 0, 1, ENC },
+    { "format_code",  "force format by fourcc"  , OFFSET(format_code),  AV_OPT_TYPE_STRING, { .str = NULL}, 3, 4, ENC },
     { "preroll"     , "video preroll in seconds", OFFSET(preroll     ), AV_OPT_TYPE_DOUBLE, { .dbl = 0.5 }, 0, 5, ENC },
     { NULL },
 };
