diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 7103cd6ceb..66361fa90a 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -927,16 +927,19 @@ static int getlblockinc(Jpeg2000DecoderContext *s)
     return res;
 }
 
-static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index,
-                                  Jpeg2000CodingStyle *codsty,
-                                  Jpeg2000ResLevel *rlevel, int precno,
-                                  int layno, uint8_t *expn, int numgbits)
+static int jpeg2000_decode_packet_header(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
+                                         int *tp_index,
+                                         Jpeg2000CodingStyle *codsty,
+                                         Jpeg2000ResLevel *rlevel, int precno,
+                                         int layno, uint8_t *expn, int numgbits,
+                                         int *process_data)
 {
     int bandno, cblkno, ret, nb_code_blocks;
-    int cwsno;
 
-    if (layno < rlevel->band[0].prec[precno].decoded_layers)
+    if (layno < rlevel->band[0].prec[precno].decoded_layers) {
+        *process_data = 0;
         return 0;
+    }
     rlevel->band[0].prec[precno].decoded_layers = layno + 1;
 
     if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
@@ -950,6 +953,7 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
 
     if (!(ret = get_bits(s, 1))) {
         jpeg2000_flush(s);
+        *process_data = 0;
         return 0;
     } else if (ret < 0)
         return ret;
@@ -1055,6 +1059,17 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
         else
             av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
     }
+    return 0;
+}
+
+static int jpeg2000_decode_packet_data(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
+                                       int *tp_index,
+                                       Jpeg2000CodingStyle *codsty,
+                                       Jpeg2000ResLevel *rlevel, int precno,
+                                       int layno, uint8_t *expn, int numgbits)
+{
+    int bandno, cblkno, nb_code_blocks;
+    int cwsno;
 
     for (bandno = 0; bandno < rlevel->nbands; bandno++) {
         Jpeg2000Band *band = rlevel->band + bandno;
@@ -1124,13 +1139,27 @@ static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2
                         Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
                                                 reslevelno;
                         ok_reslevel = 1;
-                        for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
-                            if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
-                                                              codsty, rlevel,
-                                                              precno, layno,
-                                                              qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
-                                                              qntsty->nguardbits)) < 0)
+                        for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
+                            int process_data = 1;
+                            if ((ret = jpeg2000_decode_packet_header(s, tile, tp_index,
+                                                                     codsty, rlevel,
+                                                                     precno, layno,
+                                                                     qntsty->expn +
+                                                                     (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
+                                                                     qntsty->nguardbits, &process_data)) < 0)
                                 return ret;
+
+                            if (!process_data)
+                                continue;
+
+                            if ((ret = jpeg2000_decode_packet_data(s, tile, tp_index,
+                                                                   codsty, rlevel,
+                                                                   precno, layno,
+                                                                   qntsty->expn +
+                                                                   (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
+                                                                   qntsty->nguardbits)) < 0)
+                                return ret;
+                        }
                     }
                 }
             }
@@ -1150,13 +1179,27 @@ static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2
                         Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
                                                 reslevelno;
                         ok_reslevel = 1;
-                        for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
-                            if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
-                                                              codsty, rlevel,
-                                                              precno, layno,
-                                                              qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
-                                                              qntsty->nguardbits)) < 0)
+                        for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
+                            int process_data = 1;
+                            if ((ret = jpeg2000_decode_packet_header(s, tile, tp_index,
+                                                                     codsty, rlevel,
+                                                                     precno, layno,
+                                                                     qntsty->expn +
+                                                                     (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
+                                                                     qntsty->nguardbits, &process_data)) < 0)
+                                return ret;
+
+                            if (!process_data)
+                                continue;
+
+                            if ((ret = jpeg2000_decode_packet_data(s, tile, tp_index,
+                                                                   codsty, rlevel,
+                                                                   precno, layno,
+                                                                   qntsty->expn +
+                                                                   (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
+                                                                   qntsty->nguardbits)) < 0)
                                 return ret;
+                        }
                     }
                 }
             }
@@ -1218,10 +1261,24 @@ static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2
                         }
 
                         for (layno = 0; layno < LYEpoc; layno++) {
-                            if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
-                                                              precno, layno,
-                                                              qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
-                                                              qntsty->nguardbits)) < 0)
+                            int process_data = 1;
+                            if ((ret = jpeg2000_decode_packet_header(s, tile, tp_index,
+                                                                     codsty, rlevel,
+                                                                     precno, layno,
+                                                                     qntsty->expn +
+                                                                     (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
+                                                                     qntsty->nguardbits, &process_data)) < 0)
+                                return ret;
+
+                            if (!process_data)
+                                continue;
+
+                            if ((ret = jpeg2000_decode_packet_data(s, tile, tp_index,
+                                                                   codsty, rlevel,
+                                                                   precno, layno,
+                                                                   qntsty->expn +
+                                                                   (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
+                                                                   qntsty->nguardbits)) < 0)
                                 return ret;
                         }
                     }
@@ -1288,14 +1345,25 @@ static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2
                             continue;
                         }
 
-                            for (layno = 0; layno < LYEpoc; layno++) {
-                                if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
-                                                                codsty, rlevel,
-                                                                precno, layno,
-                                                                qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
-                                                                qntsty->nguardbits)) < 0)
-                                    return ret;
-                            }
+                        for (layno = 0; layno < LYEpoc; layno++) {
+                            int process_data = 1;
+                            if ((ret = jpeg2000_decode_packet_header(s, tile, tp_index,
+                                                                     codsty, rlevel,
+                                                                     precno, layno,
+                                                                     qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
+                                                                     qntsty->nguardbits, &process_data)) < 0)
+                                return ret;
+
+                            if (!process_data)
+                                continue;
+
+                            if ((ret = jpeg2000_decode_packet_data(s, tile, tp_index,
+                                                                   codsty, rlevel,
+                                                                   precno, layno,
+                                                                   qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
+                                                                   qntsty->nguardbits)) < 0)
+                                return ret;
+                        }
                     }
                 }
             }
@@ -1359,10 +1427,24 @@ static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2
                         }
 
                         for (layno = 0; layno < LYEpoc; layno++) {
-                            if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
-                                                              precno, layno,
-                                                              qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
-                                                              qntsty->nguardbits)) < 0)
+                            int process_data = 1;
+                            if ((ret = jpeg2000_decode_packet_header(s, tile, tp_index,
+                                                                     codsty, rlevel,
+                                                                     precno, layno,
+                                                                     qntsty->expn +
+                                                                     (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
+                                                                     qntsty->nguardbits, &process_data)) < 0)
+                                return ret;
+
+                            if (!process_data)
+                                continue;
+
+                            if ((ret = jpeg2000_decode_packet_data(s, tile, tp_index,
+                                                                   codsty, rlevel,
+                                                                   precno, layno,
+                                                                   qntsty->expn +
+                                                                   (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
+                                                                   qntsty->nguardbits)) < 0)
                                 return ret;
                         }
                     }
