VIDEO: Check validity of granulepos in TheoraVideoTrack::decodePacket

A fix was introduced in 43c5d3e7c9, which
adds handling for dropped frames in theora decoding. However, this
didn't handle the case where the granule position for the current packet
is invalid.

These changes introduce a check on the granule position of the current
packet. If the granule position is valid, proceed to properly calculate
the frame number and the next frame start time from the granule
position. If the granule position is not valid, use best estimation of
for these values.

These changes also refactor to combine the checks for the two cases
where the granule position is passed to theora functions. The
documentation for both of these functions states that they will return
-1 in the case that the provided granule position is negative.
This commit is contained in:
Graham Brereton
2026-04-13 12:47:48 -04:00
committed by Filippos Karapetis
parent 85dc44b4b9
commit 6469df1baa
+9 -12
View File
@@ -316,19 +316,16 @@ bool TheoraDecoder::TheoraVideoTrack::decodePacket(ogg_packet &oggPacket) {
translateYUVtoRGBA(yuv); translateYUVtoRGBA(yuv);
} }
// We set the current frame counter, delegating the calculation to libtheora // If we have a valid granule position for this packet, use it to calculate the next
_curFrame = (int) th_granule_frame(_theoraDecode, oggPacket.granulepos); // frame information. If we don't have a valid granule position, we need to do our
// calculation for the frame number and timing.
double time = th_granule_time(_theoraDecode, oggPacket.granulepos); if (oggPacket.granulepos >= 0) {
_curFrame = (int)th_granule_frame(_theoraDecode, oggPacket.granulepos);
// We need to calculate when the next frame should be shown _nextFrameStartTime = th_granule_time(_theoraDecode, oggPacket.granulepos);
// This is all in floating point because that's what the Ogg code gives us } else {
// Ogg is a lossy container format, so it doesn't always list the time to the _curFrame++;
// next frame. In such cases, we need to calculate it ourselves.
if (time == -1.0)
_nextFrameStartTime += _frameRate.getInverse().toDouble(); _nextFrameStartTime += _frameRate.getInverse().toDouble();
else }
_nextFrameStartTime = time;
return true; return true;
} }