diff --git a/libavformat/mpeg.c b/libavformat/mpeg.c
index ca15d9f241..c1b1c7e7de 100644
--- a/libavformat/mpeg.c
+++ b/libavformat/mpeg.c
@@ -19,6 +19,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include <stdbool.h>
+
 #include "libavutil/channel_layout.h"
 #include "avformat.h"
 #include "avio_internal.h"
@@ -480,6 +482,7 @@ static int mpegps_read_packet(AVFormatContext *s,
     AVStream *st;
     FFStream *sti;
     int len, startcode, i, es_type, ret;
+    bool identified = false;
     int pcm_dvd = 0;
     int request_probe= 0;
     enum AVCodecID codec_id = AV_CODEC_ID_NONE;
@@ -520,92 +523,138 @@ redo:
             goto found;
     }
 
+    // identify packet encoding
+    identified = true;
     es_type = m->psm_es_type[startcode & 0xff];
-    if (es_type == STREAM_TYPE_VIDEO_MPEG1) {
-        codec_id = AV_CODEC_ID_MPEG2VIDEO;
-        type     = AVMEDIA_TYPE_VIDEO;
-    } else if (es_type == STREAM_TYPE_VIDEO_MPEG2) {
-        codec_id = AV_CODEC_ID_MPEG2VIDEO;
-        type     = AVMEDIA_TYPE_VIDEO;
-    } else if (es_type == STREAM_TYPE_AUDIO_MPEG1 ||
-               es_type == STREAM_TYPE_AUDIO_MPEG2) {
-        codec_id = AV_CODEC_ID_MP3;
-        type     = AVMEDIA_TYPE_AUDIO;
-    } else if (es_type == STREAM_TYPE_AUDIO_AAC) {
-        codec_id = AV_CODEC_ID_AAC;
-        type     = AVMEDIA_TYPE_AUDIO;
-    } else if (es_type == STREAM_TYPE_VIDEO_MPEG4) {
-        codec_id = AV_CODEC_ID_MPEG4;
-        type     = AVMEDIA_TYPE_VIDEO;
-    } else if (es_type == STREAM_TYPE_VIDEO_H264) {
-        codec_id = AV_CODEC_ID_H264;
-        type     = AVMEDIA_TYPE_VIDEO;
-    } else if (es_type == STREAM_TYPE_VIDEO_HEVC) {
-        codec_id = AV_CODEC_ID_HEVC;
-        type     = AVMEDIA_TYPE_VIDEO;
-    } else if (es_type == STREAM_TYPE_AUDIO_AC3) {
-        codec_id = AV_CODEC_ID_AC3;
-        type     = AVMEDIA_TYPE_AUDIO;
-    } else if (m->imkh_cctv && es_type == 0x91) {
-        codec_id = AV_CODEC_ID_PCM_MULAW;
-        type     = AVMEDIA_TYPE_AUDIO;
-    } else if (startcode >= 0x1e0 && startcode <= 0x1ef) {
-        static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 };
-        unsigned char buf[8];
-
-        avio_read(s->pb, buf, 8);
-        avio_seek(s->pb, -8, SEEK_CUR);
-        if (!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1))
-            codec_id = AV_CODEC_ID_CAVS;
-        else
-            request_probe= 1;
-        type = AVMEDIA_TYPE_VIDEO;
-    } else if (startcode == PRIVATE_STREAM_2) {
-        type = AVMEDIA_TYPE_DATA;
-        codec_id = AV_CODEC_ID_DVD_NAV;
-    } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
-        type     = AVMEDIA_TYPE_AUDIO;
-        if (m->sofdec > 0) {
-            codec_id = AV_CODEC_ID_ADPCM_ADX;
-            // Auto-detect AC-3
-            request_probe = 50;
-        } else if (m->imkh_cctv && startcode == 0x1c0 && len > 80) {
-            codec_id = AV_CODEC_ID_PCM_ALAW;
-            request_probe = 50;
-        } else {
-            codec_id = AV_CODEC_ID_MP2;
-            if (m->imkh_cctv)
-                request_probe = 25;
+    switch (es_type) {
+        case STREAM_TYPE_VIDEO_MPEG1: // 0x01
+        case STREAM_TYPE_VIDEO_MPEG2: // 0x02
+            codec_id = AV_CODEC_ID_MPEG2VIDEO;
+            type     = AVMEDIA_TYPE_VIDEO;
+            break;
+        case STREAM_TYPE_VIDEO_MPEG4: // 0x10
+            codec_id = AV_CODEC_ID_MPEG4;
+            type     = AVMEDIA_TYPE_VIDEO;
+            break;
+        case STREAM_TYPE_VIDEO_H264:  // 0x1B
+            codec_id = AV_CODEC_ID_H264;
+            type     = AVMEDIA_TYPE_VIDEO;
+            break;
+        case STREAM_TYPE_VIDEO_HEVC:  // 0x24
+            codec_id = AV_CODEC_ID_HEVC;
+            type     = AVMEDIA_TYPE_VIDEO;
+            break;
+        case STREAM_TYPE_AUDIO_MPEG1: // 0x03
+        case STREAM_TYPE_AUDIO_MPEG2: // 0x04
+            codec_id = AV_CODEC_ID_MP3;
+            type     = AVMEDIA_TYPE_AUDIO;
+            break;
+        case STREAM_TYPE_AUDIO_AAC:   // 0x0F
+            codec_id = AV_CODEC_ID_AAC;
+            type     = AVMEDIA_TYPE_AUDIO;
+            break;
+        case STREAM_TYPE_AUDIO_AC3:   // 0x81
+            codec_id = AV_CODEC_ID_AC3;
+            type     = AVMEDIA_TYPE_AUDIO;
+            break;
+        default:
+            if (m->imkh_cctv && es_type == 0x91) {
+                codec_id = AV_CODEC_ID_PCM_MULAW;
+                type     = AVMEDIA_TYPE_AUDIO;
+                break;
+            }
+            identified = false;
+            break;
+    }
+    if (!identified) {
+        identified = true;
+        if (startcode < 0x20) {
+            identified = false;
         }
-    } else if (startcode >= 0x80 && startcode <= 0x87) {
-        type     = AVMEDIA_TYPE_AUDIO;
-        codec_id = AV_CODEC_ID_AC3;
-    } else if ((startcode >= 0x88 && startcode <= 0x8f) ||
-               (startcode >= 0x98 && startcode <= 0x9f)) {
-        /* 0x90 - 0x97 is reserved for SDDS in DVD specs */
-        type     = AVMEDIA_TYPE_AUDIO;
-        codec_id = AV_CODEC_ID_DTS;
-    } else if (startcode >= 0xa0 && startcode <= 0xaf) {
-        type     = AVMEDIA_TYPE_AUDIO;
-        if (!pcm_dvd) {
-            codec_id = AV_CODEC_ID_MLP;
-        } else {
-            codec_id = AV_CODEC_ID_PCM_DVD;
+        else if (startcode <= 0x3F) { // 0x20 to 0x3F
+            type     = AVMEDIA_TYPE_SUBTITLE;
+            codec_id = AV_CODEC_ID_DVD_SUBTITLE;
         }
-    } else if (startcode >= 0xb0 && startcode <= 0xbf) {
-        type     = AVMEDIA_TYPE_AUDIO;
-        codec_id = AV_CODEC_ID_TRUEHD;
-    } else if (startcode >= 0xc0 && startcode <= 0xcf) {
-        /* Used for both AC-3 and E-AC-3 in EVOB files */
-        type     = AVMEDIA_TYPE_AUDIO;
-        codec_id = AV_CODEC_ID_AC3;
-    } else if (startcode >= 0x20 && startcode <= 0x3f) {
-        type     = AVMEDIA_TYPE_SUBTITLE;
-        codec_id = AV_CODEC_ID_DVD_SUBTITLE;
-    } else if (startcode >= 0xfd55 && startcode <= 0xfd5f) {
-        type     = AVMEDIA_TYPE_VIDEO;
-        codec_id = AV_CODEC_ID_VC1;
-    } else {
+        else if (startcode <  0x80) { // 0x40 to 0x7F
+            identified = false;
+        }
+        else if (startcode <= 0x87) { // 0x80 to 0x87
+            type     = AVMEDIA_TYPE_AUDIO;
+            codec_id = AV_CODEC_ID_AC3;
+        }
+        else if (startcode <= 0x8F) { // 0x88 to 0x8F (to 0x9F excluding 0x90 - 0x97)
+            type     = AVMEDIA_TYPE_AUDIO;
+            codec_id = AV_CODEC_ID_DTS;
+        }
+        else if (startcode <= 0x97) { // exclude 0x90 to 0x97
+            identified = false; // 0x90 - 0x97 is reserved for SDDS in DVD specs
+        }
+        else if (startcode <= 0x9F) { // 0x98 to 0x9F (from 0x88 excluding 0x90 - 0x97)
+            type     = AVMEDIA_TYPE_AUDIO;
+            codec_id = AV_CODEC_ID_DTS;
+        }
+        else if (startcode <= 0xAF) { // 0xA0 to 0xAF
+            type     = AVMEDIA_TYPE_AUDIO;
+            codec_id = (!pcm_dvd) ? AV_CODEC_ID_MLP : AV_CODEC_ID_PCM_DVD;
+        }
+        else if (startcode <= 0xBF) { // 0xB0 to 0xBF
+            type     = AVMEDIA_TYPE_AUDIO;
+            codec_id = AV_CODEC_ID_TRUEHD;
+        }
+        else if (startcode <= 0xCF) { // 0xC0 to 0xCF
+            /* Used for both AC-3 and E-AC-3 in EVOB files */
+            type     = AVMEDIA_TYPE_AUDIO;
+            codec_id = AV_CODEC_ID_AC3;
+        }
+        else if (startcode <  0x1BF) { // 0xD0 to 0x1BE
+            identified = false;
+        }
+        else if (startcode == PRIVATE_STREAM_2) { // 0x1BF
+            type     = AVMEDIA_TYPE_DATA;
+            codec_id = AV_CODEC_ID_DVD_NAV;
+        }
+        else if (startcode <= 0x1DF) { // 0x1C0 to 0x1DF
+            type     = AVMEDIA_TYPE_AUDIO;
+            if (m->sofdec > 0) {
+                codec_id = AV_CODEC_ID_ADPCM_ADX;
+                // Auto-detect AC-3
+                request_probe = 50;
+            }
+            else if (m->imkh_cctv && startcode == 0x1c0 && len > 80) {
+                codec_id = AV_CODEC_ID_PCM_ALAW;
+                request_probe = 50;
+            }
+            else {
+                codec_id = AV_CODEC_ID_MP2;
+                if (m->imkh_cctv)
+                    request_probe = 25;
+            }
+        }
+        else if (startcode <= 0x1EF) { // 0x1E0 to 0x1EF
+            static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 };
+            unsigned char buf[8];
+
+            avio_read(s->pb, buf, 8);
+            avio_seek(s->pb, -8, SEEK_CUR);
+            if (!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1))
+                codec_id = AV_CODEC_ID_CAVS;
+            else
+                request_probe= 1;
+            type = AVMEDIA_TYPE_VIDEO;
+        }
+        else if (startcode <  0xFD55) { // 0x01F0 to 0xFD54
+            identified = false;
+        }
+        else if (startcode <= 0xFD5F) { // 0xFD55 to 0xFD5F
+            type     = AVMEDIA_TYPE_VIDEO;
+            codec_id = AV_CODEC_ID_VC1;
+        }
+        else {
+            identified = false;
+        }
+    }
+
+    if (!identified) {
 skip:
         /* skip packet */
         avio_skip(s->pb, len);
