diff mbox series

[FFmpeg-devel,5/9] avformat/smacker: Improve timestamps

Message ID 20200624134705.14833-5-andreas.rheinhardt@gmail.com
State Accepted
Commit 6e5dbd62681c2059bb7047f0b42d33fdf5a9d40d
Headers show
Series [FFmpeg-devel,1/9] avformat/smacker: Don't increase packet counter prematurely | expand


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

Commit Message

Andreas Rheinhardt June 24, 2020, 1:47 p.m. UTC
A Smacker file can contain up to seven audio tracks. Up until now,
the pts for the i. audio packet contained in a Smacker frame was
simply the end timestamp of the last i. audio packet contained in
an earlier Smacker frame.

The problem with this is that a Smacker stream need not contain data in
every Smacker frame and so the current i. audio packet present may come
from a different underlying stream than the last i. audio packet
contained in an earlier frame.

The sample hypnotix.smk* exhibits this. It has three audio tracks and
the first of the three has a longer first packet, so that the audio for
the first track is contained in only 235 packets contained in the first
235 Smacker frames; the end timestamp of this track is 166696 (about 7.56s
at a timebase of 1/22050); the other two audio tracks both have 253 packets
contained in the first 253 Smacker frames. Up until now, the 236th
packet of the second track being the first audio packet in the 236th
Smacker frame would get the end timestamp of the last first audio packet
from the last Smacker frame containing a first audio packet and said
last audio packet is the first audio packet from the 235th Smacker frame
from the first audio track, so that the timestamp is 166696. In contrast,
the 236th packet from the third track (whose packets contain the same number
of samples as the packets from the second track) has a timestamp of
156116 (because its timestamp is derived from the end timestamp of the
235th packet of the second audio track). In the end, the second track
ended up being 177360/22050 s = 8.044s long; in contrast, the third
track was 166780/22050 s = 7.56s long which also coincided with the

This commit fixes this by not using timestamps from other tracks for
a packet's pts.

*: https://samples.ffmpeg.org/game-formats/smacker/wetlands/hypnotix.smk

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
 libavformat/smacker.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
diff mbox series


diff --git a/libavformat/smacker.c b/libavformat/smacker.c
index 4990b7d920..0138af3d14 100644
--- a/libavformat/smacker.c
+++ b/libavformat/smacker.c
@@ -58,7 +58,6 @@  typedef struct SmackerContext {
     uint8_t pal[768];
     int indexes[7];
     int videoindex;
-    int curstream;
     int64_t aud_pts[7];
 } SmackerContext;
@@ -247,7 +246,6 @@  static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt)
     if (!smk->next_audio_index) {
         smk->frame_size = smk->frm_size[smk->cur_frame] & (~3);
         smk->next_frame_pos = avio_tell(s->pb) + smk->frame_size;
-        smk->curstream = 0;
         flags = smk->frm_flags[smk->cur_frame];
         smk->flags = flags >> 1;
         /* handle palette change event */
@@ -323,10 +321,9 @@  static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt)
                     goto next_frame;
             pkt->stream_index = smk->indexes[i];
-            pkt->pts = smk->aud_pts[smk->curstream];
-            smk->aud_pts[smk->curstream] += AV_RL32(pkt->data);
+            pkt->pts          = smk->aud_pts[i];
+            smk->aud_pts[i]  += AV_RL32(pkt->data);
             smk->next_audio_index = i + 1;
-                smk->curstream++;
             return 0;