diff --git a/ssdv.c b/ssdv.c index 7817e39..757a208 100644 --- a/ssdv.c +++ b/ssdv.c @@ -588,6 +588,7 @@ static char ssdv_process(ssdv_t *s) { s->mcupart = 0; s->mcu_id++; + s->reset_mcu = s->next_reset_mcu; /* Test for the end of image */ if(s->mcu_id >= s->mcu_count) @@ -603,13 +604,15 @@ static char ssdv_process(ssdv_t *s) /* The first MCU of each packet should be byte aligned */ ssdv_outbits_sync(s); - s->reset_mcu = s->mcu_id; + s->next_reset_mcu = s->reset_mcu = s->mcu_id; s->packet_mcu_id = s->mcu_id; s->packet_mcu_offset = s->pkt_size_payload - s->out_len; } if(s->mode == S_DECODING && s->mcu_id == s->reset_mcu) + { s->workbits = s->worklen = 0; + } /* Test for a reset marker */ if(s->dri > 0 && s->mcu_id > 0 && s->mcu_id % s->dri == 0) @@ -1028,9 +1031,10 @@ char ssdv_enc_get_packet(ssdv_t *s) if(r == SSDV_BUFFER_FULL || r == SSDV_EOI) { - uint16_t mcu_id = s->packet_mcu_id; - uint8_t i, mcu_offset = s->packet_mcu_offset; + uint16_t mcu_id = s->packet_mcu_id; + uint8_t mcu_offset = s->packet_mcu_offset; uint32_t x; + uint8_t i; if(mcu_offset != 0xFF && mcu_offset >= s->pkt_size_payload) { @@ -1251,7 +1255,13 @@ char ssdv_dec_feed(ssdv_t *s, uint8_t *packet) s->packet_mcu_offset = packet[12]; s->packet_mcu_id = (packet[13] << 8) | packet[14]; - if(s->packet_mcu_id != 0xFFFF) s->reset_mcu = s->packet_mcu_id; + if(s->packet_mcu_id != 0xFFFF) + { + /* Set the next reset MCU ID. We can't set it + * directly here as the previous MCU may still + * be being processed. */ + s->next_reset_mcu = s->packet_mcu_id; + } /* If this is the first packet, write the JPEG headers */ if(s->packet_id == 0) diff --git a/ssdv.h b/ssdv.h index 0b9393c..a536562 100644 --- a/ssdv.h +++ b/ssdv.h @@ -114,6 +114,7 @@ typedef struct S_DECODING, } mode; uint32_t reset_mcu; /* MCU block to do absolute encoding */ + uint32_t next_reset_mcu; char needbits; /* Number of bits needed to decode integer */ /* The input huffman and quantisation tables */