From 2692067a45e4cb4ce0028e170673f2b2f5039721 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <michaelni@gmx.at>
Date: Thu, 26 May 2005 20:17:12 +0000
Subject: [PATCH] AVPacket.pos

Originally committed as revision 4314 to svn://svn.ffmpeg.org/ffmpeg/trunk
---
 libavformat/4xm.c            | 10 ++++------
 libavformat/amr.c            |  3 ++-
 libavformat/asf.c            |  1 +
 libavformat/au.c             |  6 ++----
 libavformat/avformat.h       | 13 +++++++------
 libavformat/avidec.c         |  3 +--
 libavformat/electronicarts.c |  8 ++------
 libavformat/ffm.c            |  1 +
 libavformat/flic.c           |  1 +
 libavformat/flvdec.c         |  6 +-----
 libavformat/idcin.c          | 14 +++++---------
 libavformat/idroq.c          |  7 +++----
 libavformat/ipmovie.c        | 10 +++-------
 libavformat/matroska.c       |  2 ++
 libavformat/mov.c            | 17 +----------------
 libavformat/mp3.c            |  5 +----
 libavformat/mpegts.c         |  1 +
 libavformat/nsvdec.c         |  7 ++-----
 libavformat/nut.c            |  3 +--
 libavformat/psxstr.c         |  1 +
 libavformat/raw.c            | 20 +++++---------------
 libavformat/rm.c             |  7 ++-----
 libavformat/segafilm.c       |  6 +++---
 libavformat/sierravmd.c      |  1 +
 libavformat/sol.c            |  6 +-----
 libavformat/swf.c            |  6 ++----
 libavformat/utils.c          | 25 +++++++++++++++++++++++++
 libavformat/wc3movie.c       |  8 ++------
 libavformat/westwood.c       | 12 +++---------
 libavformat/yuv4mpeg.c       | 10 ++--------
 30 files changed, 88 insertions(+), 132 deletions(-)

diff --git a/libavformat/4xm.c b/libavformat/4xm.c
index 5ce1f43f5e9..7c0f78e6b9b 100644
--- a/libavformat/4xm.c
+++ b/libavformat/4xm.c
@@ -277,6 +277,7 @@ static int fourxm_read_packet(AVFormatContext *s,
                 return AVERROR_IO;
             pkt->stream_index = fourxm->video_stream_index;
             pkt->pts = fourxm->video_pts;
+            pkt->pos = url_ftell(&s->pb);
             memcpy(pkt->data, header, 8);
             ret = get_buffer(&s->pb, &pkt->data[8], size);
 
@@ -293,16 +294,13 @@ static int fourxm_read_packet(AVFormatContext *s,
             size-=8;
 
             if (track_number == fourxm->selected_track) {
-                if (av_new_packet(pkt, size))
+                ret= av_get_packet(&s->pb, pkt, size);
+                if(ret<0)
                     return AVERROR_IO;
                 pkt->stream_index = 
                     fourxm->tracks[fourxm->selected_track].stream_index;
                 pkt->pts = fourxm->audio_pts;
-                ret = get_buffer(&s->pb, pkt->data, size);
-                if (ret < 0)
-                    av_free_packet(pkt);
-                else
-                    packet_read = 1;
+                packet_read = 1;
 
                 /* pts accounting */
                 audio_frame_count = size;
diff --git a/libavformat/amr.c b/libavformat/amr.c
index 21f41b7619a..64c387b5eed 100644
--- a/libavformat/amr.c
+++ b/libavformat/amr.c
@@ -154,7 +154,7 @@ static int amr_read_packet(AVFormatContext *s,
             return AVERROR_IO;
         }
         pkt->stream_index = 0;
-        
+        pkt->pos= url_ftell(&s->pb);
         pkt->data[0]=toc;
     
         read = get_buffer(&s->pb, pkt->data+1, size);
@@ -189,6 +189,7 @@ static int amr_read_packet(AVFormatContext *s,
         }
     
         pkt->stream_index = 0;
+        pkt->pos= url_ftell(&s->pb);
         pkt->data[0]=toc;
     
         read = get_buffer(&s->pb, pkt->data+1, size-1);
diff --git a/libavformat/asf.c b/libavformat/asf.c
index 4a480bf45cd..045766dbff0 100644
--- a/libavformat/asf.c
+++ b/libavformat/asf.c
@@ -604,6 +604,7 @@ static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
 	    asf_st->seq = asf->packet_seq;
 	    asf_st->pkt.pts = asf->packet_frag_timestamp;
 	    asf_st->pkt.stream_index = asf->stream_index;
+            asf_st->pkt.pos = 
             asf_st->packet_pos= asf->packet_pos;            
 //printf("new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n", 
 //asf->stream_index, asf->packet_key_frame, asf_st->pkt.flags & PKT_FLAG_KEY,
diff --git a/libavformat/au.c b/libavformat/au.c
index 1709c874ec8..984180ab4b1 100644
--- a/libavformat/au.c
+++ b/libavformat/au.c
@@ -161,13 +161,11 @@ static int au_read_packet(AVFormatContext *s,
 
     if (url_feof(&s->pb))
         return AVERROR_IO;
-    if (av_new_packet(pkt, MAX_SIZE))
+    ret= av_get_packet(&s->pb, pkt, MAX_SIZE);
+    if (ret < 0)
         return AVERROR_IO;
     pkt->stream_index = 0;
 
-    ret = get_buffer(&s->pb, pkt->data, pkt->size);
-    if (ret < 0)
-        av_free_packet(pkt);
     /* note: we need to modify the packet size here to handle the last
        packet */
     pkt->size = ret;
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index f19ea89e3ee..0f73e8c7f0e 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -5,7 +5,7 @@
 extern "C" {
 #endif
 
-#define LIBAVFORMAT_BUILD       4624
+#define LIBAVFORMAT_BUILD       4625
 
 #define LIBAVFORMAT_VERSION_INT FFMPEG_VERSION_INT
 #define LIBAVFORMAT_VERSION     FFMPEG_VERSION
@@ -28,17 +28,16 @@ extern "C" {
 #endif
 
 typedef struct AVPacket {
-    int64_t pts; /* presentation time stamp in AV_TIME_BASE units (or
-                    pts_den units in muxers or demuxers) */
-    int64_t dts; /* decompression time stamp in AV_TIME_BASE units (or
-                    pts_den units in muxers or demuxers) */
+    int64_t pts;                            ///< presentation time stamp in time_base units
+    int64_t dts;                            ///< decompression time stamp in time_base units
     uint8_t *data;
     int   size;
     int   stream_index;
     int   flags;
-    int   duration; /* presentation duration (0 if not available) */
+    int   duration;                         ///< presentation duration in time_base units (0 if not available)
     void  (*destruct)(struct AVPacket *);
     void  *priv;
+    int64_t pos;                            ///< byte position in stream, -1 if unknown
 } AVPacket; 
 #define PKT_FLAG_KEY   0x0001
 
@@ -49,6 +48,7 @@ static inline void av_init_packet(AVPacket *pkt)
 {
     pkt->pts   = AV_NOPTS_VALUE;
     pkt->dts   = AV_NOPTS_VALUE;
+    pkt->pos   = -1;
     pkt->duration = 0;
     pkt->flags = 0;
     pkt->stream_index = 0;
@@ -56,6 +56,7 @@ static inline void av_init_packet(AVPacket *pkt)
 }
 
 int av_new_packet(AVPacket *pkt, int size);
+int av_get_packet(ByteIOContext *s, AVPacket *pkt, int size);
 int av_dup_packet(AVPacket *pkt);
 
 /**
diff --git a/libavformat/avidec.c b/libavformat/avidec.c
index 1f636a0a505..14fa2b5eff2 100644
--- a/libavformat/avidec.c
+++ b/libavformat/avidec.c
@@ -423,8 +423,7 @@ resync:
 
         if(size > ast->remaining)
             size= ast->remaining;
-        av_new_packet(pkt, size);
-        get_buffer(pb, pkt->data, size);
+        av_get_packet(pb, pkt, size);
         
         if (avi->dv_demux) {
             dstr = pkt->destruct;
diff --git a/libavformat/electronicarts.c b/libavformat/electronicarts.c
index 6bb86af0b35..2d8bdadb573 100644
--- a/libavformat/electronicarts.c
+++ b/libavformat/electronicarts.c
@@ -234,13 +234,10 @@ static int ea_read_packet(AVFormatContext *s,
         switch (chunk_type) {
         /* audio data */
         case SCDl_TAG:
-            if (av_new_packet(pkt, chunk_size))
+            ret = av_get_packet(pb, pkt, chunk_size);
+            if (ret != chunk_size)
                 ret = AVERROR_IO;
             else {
-                ret = get_buffer(pb, pkt->data, chunk_size);
-                if (ret != chunk_size)
-                    ret = AVERROR_IO;
-                else {
                     pkt->stream_index = ea->audio_stream_index;
                     pkt->pts = 90000;
                     pkt->pts *= ea->audio_frame_counter;
@@ -250,7 +247,6 @@ static int ea_read_packet(AVFormatContext *s,
                      * on stereo; chunk also has 12-byte header */
                     ea->audio_frame_counter += ((chunk_size - 12) * 2) /
                         ea->num_channels;
-                }
             }
 
             packet_read = 1;
diff --git a/libavformat/ffm.c b/libavformat/ffm.c
index c8641ec87ba..6fb069ace2c 100644
--- a/libavformat/ffm.c
+++ b/libavformat/ffm.c
@@ -603,6 +603,7 @@ static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
 
         av_new_packet(pkt, size);
         pkt->stream_index = ffm->header[0];
+        pkt->pos = url_ftell(&s->pb); 
         if (ffm->header[1] & FLAG_KEY_FRAME)
             pkt->flags |= PKT_FLAG_KEY;
 
diff --git a/libavformat/flic.c b/libavformat/flic.c
index 7fc3b9a33f7..3fa27335db8 100644
--- a/libavformat/flic.c
+++ b/libavformat/flic.c
@@ -178,6 +178,7 @@ static int flic_read_packet(AVFormatContext *s,
             }
             pkt->stream_index = flic->video_stream_index;
             pkt->pts = flic->pts;
+            pkt->pos = url_ftell(pb); 
             memcpy(pkt->data, preamble, FLIC_PREAMBLE_SIZE);
             ret = get_buffer(pb, pkt->data + FLIC_PREAMBLE_SIZE, 
                 size - FLIC_PREAMBLE_SIZE);
diff --git a/libavformat/flvdec.c b/libavformat/flvdec.c
index 942e188edc7..187a692ea93 100644
--- a/libavformat/flvdec.c
+++ b/libavformat/flvdec.c
@@ -147,12 +147,8 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
             }
     }
 
-    if (av_new_packet(pkt, size) < 0)
-        return AVERROR_IO;
-
-    ret = get_buffer(&s->pb, pkt->data, size);
+    ret= av_get_packet(&s->pb, pkt, size);
     if (ret <= 0) {
-        av_free_packet(pkt);
         return AVERROR_IO;
     }
     /* note: we need to modify the packet size here to handle the last
diff --git a/libavformat/idcin.c b/libavformat/idcin.c
index 164cf556c6c..aecc1098ddc 100644
--- a/libavformat/idcin.c
+++ b/libavformat/idcin.c
@@ -255,26 +255,22 @@ static int idcin_read_packet(AVFormatContext *s,
         /* skip the number of decoded bytes (always equal to width * height) */
         url_fseek(pb, 4, SEEK_CUR);
         chunk_size -= 4;
-        if (av_new_packet(pkt, chunk_size))
-            ret = AVERROR_IO;
+        ret= av_get_packet(pb, pkt, chunk_size); 
+        if (ret != chunk_size)
+            return AVERROR_IO;
         pkt->stream_index = idcin->video_stream_index;
         pkt->pts = idcin->pts;
-        ret = get_buffer(pb, pkt->data, chunk_size);
-        if (ret != chunk_size)
-            ret = AVERROR_IO;
     } else {
         /* send out the audio chunk */
         if (idcin->current_audio_chunk)
             chunk_size = idcin->audio_chunk_size2;
         else
             chunk_size = idcin->audio_chunk_size1;
-        if (av_new_packet(pkt, chunk_size))
+        ret= av_get_packet(pb, pkt, chunk_size);
+        if (ret != chunk_size)
             return AVERROR_IO;
         pkt->stream_index = idcin->audio_stream_index;
         pkt->pts = idcin->pts;
-        ret = get_buffer(&s->pb, pkt->data, chunk_size);
-        if (ret != chunk_size)
-            ret = AVERROR_IO;
 
         idcin->current_audio_chunk ^= 1;
         idcin->pts += FRAME_PTS_INC;
diff --git a/libavformat/idroq.c b/libavformat/idroq.c
index 13553ed847f..add882eef3d 100644
--- a/libavformat/idroq.c
+++ b/libavformat/idroq.c
@@ -221,13 +221,11 @@ static int roq_read_packet(AVFormatContext *s,
             url_fseek(pb, codebook_offset, SEEK_SET);
 
             /* load up the packet */
-            if (av_new_packet(pkt, chunk_size))
+            ret= av_get_packet(pb, pkt, chunk_size);
+            if (ret != chunk_size)
                 return AVERROR_IO;
             pkt->stream_index = roq->video_stream_index;
             pkt->pts = roq->video_pts;
-            ret = get_buffer(pb, pkt->data, chunk_size);
-            if (ret != chunk_size)
-                ret = AVERROR_IO;
 
             roq->video_pts += roq->frame_pts_inc;
             packet_read = 1;
@@ -254,6 +252,7 @@ static int roq_read_packet(AVFormatContext *s,
                 roq->audio_frame_count += (chunk_size / roq->audio_channels);
             }
 
+            pkt->pos= url_ftell(pb);
             ret = get_buffer(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
                 chunk_size);
             if (ret != chunk_size)
diff --git a/libavformat/ipmovie.c b/libavformat/ipmovie.c
index fc176fd633d..f2f0e9e2c17 100644
--- a/libavformat/ipmovie.c
+++ b/libavformat/ipmovie.c
@@ -140,16 +140,11 @@ static int load_ipmovie_packet(IPMVEContext *s, ByteIOContext *pb,
         audio_pts *= s->audio_frame_count;
         audio_pts /= s->audio_sample_rate;
 
-        if (av_new_packet(pkt, s->audio_chunk_size))
-            return CHUNK_NOMEM;
+        if (s->audio_chunk_size != av_get_packet(pb, pkt, s->audio_chunk_size))
+            return CHUNK_EOF;
 
         pkt->stream_index = s->audio_stream_index;
         pkt->pts = audio_pts;
-        if (get_buffer(pb, pkt->data, s->audio_chunk_size) != 
-            s->audio_chunk_size) {
-            av_free_packet(pkt);
-            return CHUNK_EOF;
-        }
 
         /* audio frame maintenance */
         if (s->audio_type != CODEC_ID_INTERPLAY_DPCM)
@@ -171,6 +166,7 @@ static int load_ipmovie_packet(IPMVEContext *s, ByteIOContext *pb,
         if (av_new_packet(pkt, s->decode_map_chunk_size + s->video_chunk_size))
             return CHUNK_NOMEM;
 
+        pkt->pos= s->decode_map_chunk_offset;
         url_fseek(pb, s->decode_map_chunk_offset, SEEK_SET);
         s->decode_map_chunk_offset = 0;
 
diff --git a/libavformat/matroska.c b/libavformat/matroska.c
index cc23781d856..0cb1efc6361 100644
--- a/libavformat/matroska.c
+++ b/libavformat/matroska.c
@@ -2366,6 +2366,7 @@ matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
                 uint32_t *lace_size = NULL;
                 int n, track, flags, laces = 0;
                 uint64_t num;
+                int64_t pos= url_ftell(&matroska->ctx->pb);
 
                 if ((res = ebml_read_binary(matroska, &id, &data, &size)) < 0)
                     break;
@@ -2507,6 +2508,7 @@ matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
                             matroska->tracks[track]->stream_index;
 
                         pkt->pts = timecode / 1000000; /* ns to ms */
+                        pkt->pos= pos;
 
                         matroska_queue_packet(matroska, pkt);
                     }
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 7779ac0a472..c2f7abffa01 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -253,9 +253,6 @@ typedef struct MOVStreamContext {
     int time_scale;
     long current_sample;
     long left_in_chunk; /* how many samples before next chunk */
-    /* specific MPEG4 header which is added at the beginning of the stream */
-    unsigned int header_len;
-    uint8_t *header_data;
     MOV_esds_t esds;
 } MOVStreamContext;
 
@@ -1672,7 +1669,6 @@ static void mov_free_stream_context(MOVStreamContext *sc)
         av_freep(&sc->sample_to_chunk);
         av_freep(&sc->sample_sizes);
         av_freep(&sc->keyframes);
-        av_freep(&sc->header_data);
         av_freep(&sc->stts_data);        
         av_freep(&sc->ctts_data);        
         av_freep(&sc);
@@ -1974,18 +1970,7 @@ readchunk:
         return -1;
     url_fseek(&s->pb, offset, SEEK_SET);
 
-    //av_log(NULL, AV_LOG_DEBUG, "READCHUNK hlen: %d  %d off: %Ld   pos:%Ld\n", size, sc->header_len, offset, url_ftell(&s->pb));
-    if (sc->header_len > 0) {
-        av_new_packet(pkt, size + sc->header_len);
-        memcpy(pkt->data, sc->header_data, sc->header_len);
-        get_buffer(&s->pb, pkt->data + sc->header_len, size);
-        /* free header */
-        av_freep(&sc->header_data);
-        sc->header_len = 0;
-    } else {
-        av_new_packet(pkt, size);
-        get_buffer(&s->pb, pkt->data, pkt->size);
-    }
+    av_get_packet(&s->pb, pkt, size);
     pkt->stream_index = sc->ffindex;
     
     // If the keyframes table exists, mark any samples that are in the table as key frames.
diff --git a/libavformat/mp3.c b/libavformat/mp3.c
index ebd7ef4be13..ddc27dea6f8 100644
--- a/libavformat/mp3.c
+++ b/libavformat/mp3.c
@@ -297,13 +297,10 @@ static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
     
     size= MP3_PACKET_SIZE;
 
-    if (av_new_packet(pkt, size) < 0)
-        return AVERROR_IO;
+    ret= av_get_packet(&s->pb, pkt, size);
 
     pkt->stream_index = 0;
-    ret = get_buffer(&s->pb, pkt->data, size);
     if (ret <= 0) {
-        av_free_packet(pkt);
         return AVERROR_IO;
     }
     /* note: we need to modify the packet size here to handle the last
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index d100d22af3b..be966dd5b2e 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -1234,6 +1234,7 @@ static int mpegts_raw_read_packet(AVFormatContext *s,
 
     if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
         return -ENOMEM;
+    pkt->pos= url_ftell(&s->pb);
     ret = read_packet(&s->pb, pkt->data, ts->raw_packet_size);
     if (ret < 0) {
         av_free_packet(pkt);
diff --git a/libavformat/nsvdec.c b/libavformat/nsvdec.c
index fa8adf07658..91c2aabe69c 100644
--- a/libavformat/nsvdec.c
+++ b/libavformat/nsvdec.c
@@ -592,8 +592,7 @@ null_chunk_retry:
     if (vsize/* && st[NSV_ST_VIDEO]*/) {
         nst = st[NSV_ST_VIDEO]->priv_data;
         pkt = &nsv->ahead[NSV_ST_VIDEO];
-        av_new_packet(pkt, vsize);
-        get_buffer(pb, pkt->data, vsize);
+        av_get_packet(pb, pkt, vsize);
         pkt->stream_index = st[NSV_ST_VIDEO]->index;//NSV_ST_VIDEO;
         pkt->dts = nst->frame_offset++;
         pkt->flags |= PKT_FLAG_KEY; /* stupid format has no way to tell XXX: try the index */
@@ -633,9 +632,7 @@ null_chunk_retry:
                 PRINT(("NSV RAWAUDIO: bps %d, nchan %d, srate %ld\n", bps, channels, samplerate));
             }
         }
-        av_new_packet(pkt, asize);
-        if (asize)
-            get_buffer(pb, pkt->data, asize);
+        av_get_packet(pb, pkt, asize);
         pkt->stream_index = st[NSV_ST_AUDIO]->index;//NSV_ST_AUDIO;
         //pkt->dts = nst->frame_offset;
         //if (nst->sample_size)
diff --git a/libavformat/nut.c b/libavformat/nut.c
index 7c1557f12aa..7d3d8b8d523 100644
--- a/libavformat/nut.c
+++ b/libavformat/nut.c
@@ -1236,8 +1236,7 @@ static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code, int fram
         return 1;
     }
 
-    av_new_packet(pkt, size);
-    get_buffer(bc, pkt->data, size);
+    av_get_packet(bc, pkt, size);
     pkt->stream_index = stream_id;
     if (key_frame)
 	pkt->flags |= PKT_FLAG_KEY;
diff --git a/libavformat/psxstr.c b/libavformat/psxstr.c
index eca6316f6cf..c8c3b2915ad 100644
--- a/libavformat/psxstr.c
+++ b/libavformat/psxstr.c
@@ -283,6 +283,7 @@ static int str_read_packet(AVFormatContext *s,
                     if (av_new_packet(pkt, frame_size))
                         return AVERROR_IO;
 
+                    pkt->pos= url_ftell(pb) - RAW_CD_SECTOR_SIZE;
                     pkt->stream_index = 
                         str->channels[channel].video_stream_index;
                //     pkt->pts = str->pts;
diff --git a/libavformat/raw.c b/libavformat/raw.c
index 243c03d01f9..8ae9a1b887e 100644
--- a/libavformat/raw.c
+++ b/libavformat/raw.c
@@ -88,13 +88,10 @@ static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
     
     size= RAW_PACKET_SIZE;
 
-    if (av_new_packet(pkt, size) < 0)
-        return AVERROR_IO;
+    ret= av_get_packet(&s->pb, pkt, size);
 
     pkt->stream_index = 0;
-    ret = get_buffer(&s->pb, pkt->data, size);
     if (ret <= 0) {
-        av_free_packet(pkt);
         return AVERROR_IO;
     }
     /* note: we need to modify the packet size here to handle the last
@@ -111,7 +108,8 @@ static int raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
 
     if (av_new_packet(pkt, size) < 0)
         return AVERROR_IO;
-
+    
+    pkt->pos= url_ftell(&s->pb);
     pkt->stream_index = 0;
     ret = get_partial_buffer(&s->pb, pkt->data, size);
     if (ret <= 0) {
@@ -618,18 +616,10 @@ static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
     if (packet_size < 0)
         return -1;
 
-    if (av_new_packet(pkt, packet_size) < 0)
-        return AVERROR_IO;
+    ret= av_get_packet(&s->pb, pkt, packet_size);
 
     pkt->stream_index = 0;
-#if 0
-    /* bypass buffered I/O */
-    ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
-#else
-    ret = get_buffer(&s->pb, pkt->data, pkt->size);
-#endif
-    if (ret != pkt->size) {
-        av_free_packet(pkt);
+    if (ret != packet_size) {
         return AVERROR_IO;
     } else {
         return 0;
diff --git a/libavformat/rm.c b/libavformat/rm.c
index 58a3cab4603..f0fe17974b8 100644
--- a/libavformat/rm.c
+++ b/libavformat/rm.c
@@ -812,11 +812,9 @@ static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
     if (rm->old_format) {
         /* just read raw bytes */
         len = RAW_PACKET_SIZE;
-        av_new_packet(pkt, len);
+        len= av_get_packet(pb, pkt, len);
         pkt->stream_index = 0;
-        len = get_buffer(pb, pkt->data, len);
         if (len <= 0) {
-            av_free_packet(pkt);
             return AVERROR_IO;
         }
         pkt->size = len;
@@ -860,9 +858,8 @@ resync:
             goto resync;
         }
         
-        av_new_packet(pkt, len);
+        av_get_packet(pb, pkt, len);
         pkt->stream_index = i;
-        get_buffer(pb, pkt->data, len);
 
 #if 0
         if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
diff --git a/libavformat/segafilm.c b/libavformat/segafilm.c
index d984a3b10bc..d2954881ed9 100644
--- a/libavformat/segafilm.c
+++ b/libavformat/segafilm.c
@@ -233,6 +233,7 @@ static int film_read_packet(AVFormatContext *s,
             return AVERROR_NOMEM;
         if(pkt->size < 10)
             return -1;
+        pkt->pos= url_ftell(pb);
         ret = get_buffer(pb, pkt->data, 10);
         /* skip the non-spec CVID bytes */
         url_fseek(pb, film->cvid_extra_bytes, SEEK_CUR);
@@ -254,6 +255,7 @@ static int film_read_packet(AVFormatContext *s,
             film->stereo_buffer = av_malloc(film->stereo_buffer_size);
         }
 
+        pkt->pos= url_ftell(pb);
         ret = get_buffer(pb, film->stereo_buffer, sample->sample_size);
         if (ret != sample->sample_size)
             ret = AVERROR_IO;
@@ -272,9 +274,7 @@ static int film_read_packet(AVFormatContext *s,
             }
         }
     } else {
-        if (av_new_packet(pkt, sample->sample_size))
-            return AVERROR_NOMEM;
-        ret = get_buffer(pb, pkt->data, sample->sample_size);
+        ret= av_get_packet(pb, pkt, sample->sample_size);
         if (ret != sample->sample_size)
             ret = AVERROR_IO;
     }
diff --git a/libavformat/sierravmd.c b/libavformat/sierravmd.c
index b1484b56057..152b4fb8f71 100644
--- a/libavformat/sierravmd.c
+++ b/libavformat/sierravmd.c
@@ -269,6 +269,7 @@ static int vmd_read_packet(AVFormatContext *s,
 
     if (av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD))
         return AVERROR_NOMEM;
+    pkt->pos= url_ftell(pb);
     memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
     ret = get_buffer(pb, pkt->data + BYTES_PER_FRAME_RECORD, 
         frame->frame_size);
diff --git a/libavformat/sol.c b/libavformat/sol.c
index 038feeaac67..eed1b38f320 100644
--- a/libavformat/sol.c
+++ b/libavformat/sol.c
@@ -131,13 +131,9 @@ static int sol_read_packet(AVFormatContext *s,
 
     if (url_feof(&s->pb))
         return -EIO;
-    if (av_new_packet(pkt, MAX_SIZE))
-        return -EIO;
+    ret= av_get_packet(&s->pb, pkt, MAX_SIZE);
     pkt->stream_index = 0;
 
-    ret = get_buffer(&s->pb, pkt->data, pkt->size);
-    if (ret < 0)
-        av_free_packet(pkt);
     /* note: we need to modify the packet size here to handle the last
        packet */
     pkt->size = ret;
diff --git a/libavformat/swf.c b/libavformat/swf.c
index 5f868994e4f..1a254b82ca3 100644
--- a/libavformat/swf.c
+++ b/libavformat/swf.c
@@ -858,10 +858,9 @@ static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
                 if (st->id == 0) {
                     if ( get_le16(pb) == swf->ch_id ) {
                         frame = get_le16(pb);
-                        av_new_packet(pkt, len-4);
+                        av_get_packet(pb, pkt, len-4);
                         pkt->pts = frame * swf->ms_per_frame;
                         pkt->stream_index = st->index;
-                        get_buffer(pb, pkt->data, pkt->size);
                         return pkt->size;
                     } else {
                         url_fskip(pb, len-2);
@@ -874,9 +873,8 @@ static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
             for( i=0; i<s->nb_streams; i++ ) {
         	st = s->streams[i];
                 if (st->id == 1) {
-                    av_new_packet(pkt, len);
+                    av_get_packet(pb, pkt, len);
                     pkt->stream_index = st->index;
-                    get_buffer(pb, pkt->data, pkt->size);
                     return pkt->size;
                 }
             }
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 9791cc26869..45972e1f4a2 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -195,6 +195,31 @@ int av_new_packet(AVPacket *pkt, int size)
     return 0;
 }
 
+/**
+ * Allocate and read the payload of a packet and intialized its fields to default values.
+ *
+ * @param pkt packet
+ * @param size wanted payload size
+ * @return >0 (read size) if OK. AVERROR_xxx otherwise.
+ */
+int av_get_packet(ByteIOContext *s, AVPacket *pkt, int size)
+{
+    int ret= av_new_packet(pkt, size);
+
+    if(ret<0)
+        return ret;
+
+    pkt->pos= url_ftell(s);
+
+    ret= get_buffer(s, pkt->data, size);
+    if(ret<=0)
+        av_free_packet(pkt);
+    else
+        pkt->size= ret;
+
+    return ret;
+}
+
 /* This is a hack - the packet memory allocation stuff is broken. The
    packet is allocated if it was not really allocated */
 int av_dup_packet(AVPacket *pkt)
diff --git a/libavformat/wc3movie.c b/libavformat/wc3movie.c
index 6f9fedd3db3..30417fef156 100644
--- a/libavformat/wc3movie.c
+++ b/libavformat/wc3movie.c
@@ -318,11 +318,9 @@ static int wc3_read_packet(AVFormatContext *s,
 
         case VGA__TAG:
             /* send out video chunk */
-            if (av_new_packet(pkt, size))
-                ret = AVERROR_IO;
+            ret= av_get_packet(pb, pkt, size);
             pkt->stream_index = wc3->video_stream_index;
             pkt->pts = wc3->pts;
-            ret = get_buffer(pb, pkt->data, size);
             if (ret != size)
                 ret = AVERROR_IO;
             packet_read = 1;
@@ -349,11 +347,9 @@ static int wc3_read_packet(AVFormatContext *s,
 
         case AUDI_TAG:
             /* send out audio chunk */
-            if (av_new_packet(pkt, size))
-                ret = AVERROR_IO;
+            ret= av_get_packet(pb, pkt, size);
             pkt->stream_index = wc3->audio_stream_index;
             pkt->pts = wc3->pts;
-            ret = get_buffer(pb, pkt->data, size);
             if (ret != size)
                 ret = AVERROR_IO;
 
diff --git a/libavformat/westwood.c b/libavformat/westwood.c
index 6fcb39ea71f..22150e2a641 100644
--- a/libavformat/westwood.c
+++ b/libavformat/westwood.c
@@ -170,15 +170,12 @@ static int wsaud_read_packet(AVFormatContext *s,
         return AVERROR_INVALIDDATA;
 
     chunk_size = LE_16(&preamble[0]);
-    if (av_new_packet(pkt, chunk_size))
+    ret= av_get_packet(pb, pkt, chunk_size);
+    if (ret != chunk_size)
         return AVERROR_IO;
     pkt->stream_index = wsaud->audio_stream_index;
     pkt->pts = wsaud->audio_frame_counter;
     pkt->pts /= wsaud->audio_samplerate;
-    if ((ret = get_buffer(pb, pkt->data, chunk_size)) != chunk_size) {
-        av_free_packet(pkt);
-        ret = AVERROR_IO;
-    }
 
     /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
     wsaud->audio_frame_counter += (chunk_size * 2) / wsaud->audio_channels;
@@ -322,11 +319,8 @@ static int wsvqa_read_packet(AVFormatContext *s,
 
     if ((chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
 
-        if (av_new_packet(pkt, chunk_size))
-            return AVERROR_IO;
-        ret = get_buffer(pb, pkt->data, chunk_size);
+        av_get_packet(pb, pkt, chunk_size);
         if (ret != chunk_size) {
-            av_free_packet(pkt);
             ret = AVERROR_IO;
         }
 
diff --git a/libavformat/yuv4mpeg.c b/libavformat/yuv4mpeg.c
index 7aead7b056f..74519fa1b66 100644
--- a/libavformat/yuv4mpeg.c
+++ b/libavformat/yuv4mpeg.c
@@ -356,17 +356,11 @@ static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
     if (packet_size < 0)
         return -1;
 
-    if (av_new_packet(pkt, packet_size) < 0)
+    if (av_get_packet(&s->pb, pkt, packet_size) != packet_size)
         return AVERROR_IO;
 
     pkt->stream_index = 0;
-    ret = get_buffer(&s->pb, pkt->data, pkt->size);
-    if (ret != pkt->size) {
-        av_free_packet(pkt);
-        return AVERROR_IO;
-    } else {
-        return 0;
-    }
+    return 0;
 }
 
 static int yuv4_read_close(AVFormatContext *s)
-- 
GitLab