diff mbox series

[FFmpeg-devel,5/9] avformat/mpjpegdec: make sure we seek back to the ensured buffer

Message ID 20200929211021.25030-5-cus@passwd.hu
State Accepted
Commit 1490a682dc06d868c5170635cad92948c7f28cfb
Headers show
Series [FFmpeg-devel,1/9] avformat/aviobuf: write data into the IO buffer till the very end of the buffer | expand

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Marton Balint Sept. 29, 2020, 9:10 p.m. UTC
It was possible for the old code to seek back before the most recently read
data if start of a new multipart was across read boundaries. Now we read some
small sections multiple times to avoid this, but that is OK.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavformat/mpjpegdec.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/libavformat/mpjpegdec.c b/libavformat/mpjpegdec.c
index df2880412d..5fe00fd162 100644
--- a/libavformat/mpjpegdec.c
+++ b/libavformat/mpjpegdec.c
@@ -328,18 +328,18 @@  static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
         ret = av_get_packet(s->pb, pkt, size);
     } else {
         /* no size was given -- we read until the next boundary or end-of-file */
-        int remaining = 0, len;
+        int len;
 
         const int read_chunk = 2048;
 
         pkt->pos  = avio_tell(s->pb);
 
-        while ((ret = ffio_ensure_seekback(s->pb, read_chunk - remaining)) >= 0 && /* we may need to return as much as all we've read back to the buffer */
-               (ret = av_append_packet(s->pb, pkt, read_chunk - remaining)) >= 0) {
+        while ((ret = ffio_ensure_seekback(s->pb, read_chunk)) >= 0 && /* we may need to return as much as all we've read back to the buffer */
+               (ret = av_append_packet(s->pb, pkt, read_chunk)) >= 0) {
             /* scan the new data */
             char *start;
 
-            len = ret + remaining;
+            len = ret;
             start = pkt->data + pkt->size - len;
             do {
                 if (!memcmp(start, mpjpeg->searchstr, mpjpeg->searchstr_len)) {
@@ -351,7 +351,8 @@  static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
                 len--;
                 start++;
             } while (len >= mpjpeg->searchstr_len);
-            remaining = len;
+            avio_seek(s->pb, -len, SEEK_CUR);
+            pkt->size -= len;
         }
 
         /* error or EOF occurred */