diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
index f6bbb7051e3fa03236c3dc8a5835cb79ffb76a63..664e4ac14b7c5c6b84c4562c587a67d8396ab409 100644
--- a/libavcodec/hevcdec.c
+++ b/libavcodec/hevcdec.c
@@ -490,13 +490,14 @@ static int hls_slice_header(HEVCContext *s)
 
         ff_hevc_clear_refs(s);
 
+        ret = set_sps(s, sps, sps->pix_fmt);
+        if (ret < 0)
+            return ret;
+
         pix_fmt = get_format(s, sps);
         if (pix_fmt < 0)
             return pix_fmt;
-
-        ret = set_sps(s, sps, pix_fmt);
-        if (ret < 0)
-            return ret;
+        s->avctx->pix_fmt = pix_fmt;
 
         s->seq_decode = (s->seq_decode + 1) & 0xff;
         s->max_ra     = INT_MAX;