The attached patch implements accurate video pts calculation for videos with B frames, and allows filters to modify pts values. It's not finished code but should be useful for people who want to test filters that modify pts values or who want accurate playback (I've modified the ASS subtitle patch to use the filter timestamps so subtitle changes timed at scene changes work correctly). The patch adds an MPlayer option called -correct-pts. If that is not enabled things should work about the same; turning it on enables accurate pts tracking and allows filters to manipulate pts, but also breaks some things. The internal mpeg demuxer won't work (because currently code in video.c with mpeg-specific cases isn't run). If the demuxer doesn't give video timestamps for every frame things won't work right (no attempt to calculate timestamps based on default fps). Video decoders other than lavc won't work right if the video stream contains skipped or broken frames. There are also some other smaller known problems. vf_ptstest.diff contains a simple video filter to demonstrate the pts manipulation. It compresses each second of video into the first half of the second with no frames in the second half. The pts calculation with B frames works by keeping a buffer of pts values, where the timestamp of each frame fed to the decoder is added. Whenever the decoder outputs a frame the smallest timestamp is taken out of the buffer and used as the output timestamp of that frame. This works without decoder assistance and whether the demuxer returns timestamps in coded or display order as long as the only reason the decoder doesn't return a frame is decoder lag. However to handle broken or skipped frames cooperation from the decoder is necessary, as it's necessary to distinguish between decoder lag (a timestamp should be buffered for that frame) and skipped frames (a timestamp should be dropped). The code tries to query the decoder for the number of buffered frames that have not been shown yet (those that should be timestamps buffered for them). The changes to vd_ffmpeg.c (ab)use the buffer interface to get this information out of lavc, it seems to work at least for the files I've tested.
Hi On Tue, May 09, 2006 at 08:08:56PM +0300, Uoti Urpala wrote:
The attached patch implements accurate video pts calculation for videos with B frames, and allows filters to modify pts values. It's not finished code but should be useful for people who want to test filters that modify pts values or who want accurate playback (I've modified the ASS subtitle patch to use the filter timestamps so subtitle changes timed at scene changes work correctly).
The patch adds an MPlayer option called -correct-pts. If that is not enabled things should work about the same; turning it on enables accurate pts tracking and allows filters to manipulate pts, but also breaks some things. The internal mpeg demuxer won't work (because currently code in video.c with mpeg-specific cases isn't run). If the demuxer doesn't give video timestamps for every frame things won't work right (no attempt to calculate timestamps based on default fps). Video decoders other than lavc won't work right if the video stream contains skipped or broken frames. There are also some other smaller known problems.
vf_ptstest.diff contains a simple video filter to demonstrate the pts manipulation. It compresses each second of video into the first half of the second with no frames in the second half.
The pts calculation with B frames works by keeping a buffer of pts values, where the timestamp of each frame fed to the decoder is added. Whenever the decoder outputs a frame the smallest timestamp is taken out of the buffer and used as the output timestamp of that frame. This works without decoder assistance and whether the demuxer returns timestamps in coded or display order
then i must missunderstand you decoder input: pts 1423756 dts 0123456 IPBBPBB decoder output: .IBBPBBP ts 1234567 if we feed the reorder buffer from pts we get: 1234567, if we use dts we get: 0123456, thats not the same
as long as the only reason the decoder doesn't return a frame is decoder lag. However to handle broken or skipped frames cooperation from the decoder is necessary, as it's necessary to distinguish between decoder lag (a timestamp should be buffered for that frame) and skipped frames (a timestamp should be dropped). The code tries to query the decoder for the number of buffered frames that have not been shown yet (those that should be timestamps buffered for them). The changes to vd_ffmpeg.c (ab)use the buffer interface to get this information out of lavc, it seems to work at least for the files I've tested.
whats the problem with using AVCodecContext.has_b_frames as the delay? alternatively if dts are available they can be used if not pts can be feeded into the decoder and reordered in it either way i disslike your changes to vd_ffmpeg.c, they are messy [...] -- Michael In the past you could go to a library and read, borrow or copy any book Today you'd get arrested for mere telling someone where the library is
On Tue, 2006-05-09 at 20:27 +0200, Michael Niedermayer wrote:
of the buffer and used as the output timestamp of that frame. This works without decoder assistance and whether the demuxer returns timestamps in coded or display order
then i must missunderstand you
if we feed the reorder buffer from pts we get: 1234567, if we use dts we get: 0123456, thats not the same
By "timestamps in coded or display order" I mean pts values only. Using dts values with formats which have those separately from pts won't work right.
whats the problem with using AVCodecContext.has_b_frames as the delay?
I think it doesn't always have the correct value for that use. It doesn't seem to be reset after seeks or errors which make lavc drop buffered frames.
alternatively if dts are available they can be used if not pts can be feeded into the decoder and reordered in it
dts values only work if you make assumptions about decoder behavior (whether it does unnecessary buffering to nominal decode delay or not), and aren't available in formats like mkv. Current libavcodec doesn't seem to support feeding pts info with frames (and adding that would probably be no simpler than adding internal counting of unseen buffered frames, though might be preferable aesthetically). It would also require that the demuxers give timestamps in coded order only, which current mplayer demuxers do not.
either way i disslike your changes to vd_ffmpeg.c, they are messy
I agree they're messy. As I said, the patch was not meant to be finished code. It could be cleaned up somewhat, but I think the general approach is the only one that works with current libavcodec. Changing libavcodec instead would enable a cleaner solution. Just doing the same buffer counting on the libavcodec side would make it much less messy, though it would require numerically more code changes.
Hi On Tue, May 09, 2006 at 10:19:06PM +0300, Uoti Urpala wrote: [...]
whats the problem with using AVCodecContext.has_b_frames as the delay?
I think it doesn't always have the correct value for that use. It doesn't seem to be reset after seeks or errors which make lavc drop buffered frames.
could you elaborate? if we seek, we flush the buffers so the number of frames in the decoder becomes 0 (you also could flush the pts buffer here), and at the time when we output the next frame it should be AVCodecContext.has_b_frames again, this seems correct? if theres an error lavc might try to recover part of the frame and output that or drop it, ffmpeg should probably provide more status info about what happened ...
alternatively if dts are available they can be used if not pts can be feeded into the decoder and reordered in it
dts values only work if you make assumptions about decoder behavior (whether it does unnecessary buffering to nominal decode delay or not),
if a decoder does unnecessary buffering then its buggy (the specs like mpeg* is very explicit on these things IIRC) [...]
either way i disslike your changes to vd_ffmpeg.c, they are messy
I agree they're messy. As I said, the patch was not meant to be finished code. It could be cleaned up somewhat, but I think the general approach is the only one that works with current libavcodec. Changing libavcodec instead would enable a cleaner solution. Just doing the same buffer counting on the libavcodec side would make it much less messy, though it would require numerically more code changes.
theres also something entirely different ... what about remuxing without a decoder (-vcodec copy / -acodec copy)? this also needs correct timestamps [...] -- Michael In the past you could go to a library and read, borrow or copy any book Today you'd get arrested for mere telling someone where the library is
On Wed, 2006-05-10 at 00:24 +0200, Michael Niedermayer wrote:
On Tue, May 09, 2006 at 10:19:06PM +0300, Uoti Urpala wrote:
whats the problem with using AVCodecContext.has_b_frames as the delay?
I think it doesn't always have the correct value for that use. It doesn't seem to be reset after seeks or errors which make lavc drop buffered frames.
could you elaborate? if we seek, we flush the buffers so the number of frames in the decoder becomes 0 (you also could flush the pts buffer here), and at the time when we output the next frame it should be AVCodecContext.has_b_frames again, this seems correct?
It's not for h264 at least, the first keyframe after seek is output immediately.
theres also something entirely different ... what about remuxing without a decoder (-vcodec copy / -acodec copy)? this also needs correct timestamps
That's mainly a question of demuxers. Muxing to containers like mkv without decoding is not possible with the current MPlayer demuxers, they'd all need to be changed to give timestamps in the order the pictures are coded rather than monotonously (and remuxing from containers like avi without codec-level parsing is impossible I suppose...).
Hi On Wed, May 10, 2006 at 02:04:56AM +0300, Uoti Urpala wrote:
On Wed, 2006-05-10 at 00:24 +0200, Michael Niedermayer wrote:
On Tue, May 09, 2006 at 10:19:06PM +0300, Uoti Urpala wrote:
whats the problem with using AVCodecContext.has_b_frames as the delay?
I think it doesn't always have the correct value for that use. It doesn't seem to be reset after seeks or errors which make lavc drop buffered frames.
could you elaborate? if we seek, we flush the buffers so the number of frames in the decoder becomes 0 (you also could flush the pts buffer here), and at the time when we output the next frame it should be AVCodecContext.has_b_frames again, this seems correct?
It's not for h264 at least, the first keyframe after seek is output immediately.
if so thats a bug in h264.c [...] -- Michael In the past you could go to a library and read, borrow or copy any book Today you'd get arrested for mere telling someone where the library is
participants (2)
-
Michael Niedermayer -
Uoti Urpala