About

Michael Zucchi

 B.E. (Comp. Sys. Eng.)

  also known as zed
  & handle of notzed

Tags

android (44)
beagle (63)
biographical (89)
blogz (7)
business (1)
code (63)
cooking (30)
dez (7)
dusk (30)
ffts (3)
forth (3)
free software (4)
games (32)
gloat (2)
globalisation (1)
gnu (4)
graphics (16)
gsoc (4)
hacking (435)
haiku (2)
horticulture (10)
house (23)
hsa (6)
humour (7)
imagez (28)
java (225)
java ee (3)
javafx (49)
jjmpeg (79)
junk (3)
kobo (15)
libeze (7)
linux (5)
mediaz (27)
ml (15)
nativez (9)
opencl (119)
os (17)
parallella (97)
pdfz (8)
philosophy (26)
picfx (2)
playerz (2)
politics (7)
ps3 (12)
puppybits (17)
rants (137)
readerz (8)
rez (1)
socles (36)
termz (3)
videoz (6)
wanki (3)
workshop (3)
zcl (2)
zedzone (21)
Sunday, 20 October 2019, 09:29

Frame Accurate Seeking in H.264 streams with libavformat, FFmpeg

I had the need for accurate seeking in H.264 streams using jjmpeg (FFmpeg/libavformat) and spent some time over the last few days trying to work it out. JJMediaReader has a seek that works quite well but I knew it wasn't correct as it was offsetting the read timestamp by the stream start - I can't exactly remember why I did this but I think its because it didn't work very well otherwise.

Anyway ... I finally found out why. Depending on the container, when seeking it will go to the next or equal keyframe rather than the previous or equal. Sigh. So that offset I was using just happened to be enough to work most of the time and as it reported the same start-relative time it appeared to work properly.

So I changed the seek code to use a reverse offset for the seek point, and then it will run forward to get to the correct frame. This is based on the framerate rather than a fixed time offset because some of the video I have uses low framerates. This works but can be a bit slow as it might seek back too far.

To reduce some of the redundant work I thought of using the packet byte position to read the exact packet of the closest previous keyframe. Unfortunately it seems the container formats i'm using don't support AVSEEK_FLAG_BYTE so that idea went nowhere. Fuck.

The best solution so far has been to implement a two-value seek function. The first argument is the keyframe timestamp and the second argument is the desired frame timestamp. Thus it seeks to the keyframe first and then advances by single frames until the desired frame is decoded. Short forward seeks bypass the seek step This requires an external index but the application in question needed it anyway as it requires a wall-clock-time to frame-time mapping as well.

Summary

Tagged hacking, jjmpeg.
Fuck Adelaide, Fuck South Australia | Spring
Copyright (C) 2019 Michael Zucchi, All Rights Reserved. Powered by gcc & me!