Fix VideoPlayer from stop after just few frames

This commit is contained in:
2026-05-07 18:17:33 +10:00
parent a526aa6cdb
commit 2e0fee5e3d
6 changed files with 55 additions and 32 deletions
Submodule .claude/worktrees/lucid-grothendieck-201ca6 added at a526aa6cdb
+10 -3
View File
@@ -59,6 +59,7 @@ CFilePlayer::CFilePlayer()
, m_bSeek(0)
, m_dSeekPos(0)
, m_nNalLength(0)
, m_bLooped(FALSE)
, m_pFormatContext(NULL)
, m_hReadThread(0)
, m_hVideoThread(0)
@@ -280,8 +281,9 @@ BOOL CFilePlayer::play()
{
return TRUE;
}
m_bPlaying = TRUE;
m_bLooped = FALSE; // fresh playback — no wrap yet
if (m_nVideoIndex >= 0)
{
@@ -440,12 +442,17 @@ BOOL CFilePlayer::readFrame()
return FALSE;
}
}
// File wrapped — flag it so the consumer (ANSVideoPlayer) can detect
// EOF without watching m_nCurPos, which is non-monotonic during normal
// playback (decode-order packet PTS + audio thread also writes to it).
m_bLooped = TRUE;
if (av_read_frame(m_pFormatContext, &pkt) != 0)
{
return FALSE;
}
}
}
else if (0 != rret)
{
return FALSE;
+8 -1
View File
@@ -53,6 +53,12 @@ public:
BOOL seek(int pos);
int64 getElapse() {return m_nCurPos;};
int64 getDuration() {return m_nDuration;}
// Set inside readFrame() when av_read_frame returns AVERROR_EOF and the
// reader auto-loops via av_seek_frame. Caller (ANSVideoPlayer) polls this
// to detect EOF without relying on m_nCurPos, which is non-monotonic
// (decode-order packet PTS + audio thread also writes to it).
BOOL hasLooped() {return m_bLooped;}
void clearLooped() {m_bLooped = FALSE;}
int getVideoCodec();
int getAudioCodec();
BOOL onRecord();
@@ -79,7 +85,8 @@ private:
int m_bSeek; // seek request
double m_dSeekPos; // seek position, nit is millisecond
int m_nNalLength;
BOOL m_bLooped; // set by readFrame() when EOF triggered an av_seek_frame back to start
AVFormatContext * m_pFormatContext; // format context
pthread_t m_hReadThread; // read thread
+22 -13
View File
@@ -252,9 +252,9 @@ namespace ANSCENTER {
// --- HW decode path ---
if (_hwDecodeActive && _hwPlayer) {
_hwPlayer->play(); // starts read/video/audio threads
_hwPlayer->clearLooped(); // drop any wrap state from a prior session
_hwEOF = false;
_hwFrameCount = 0;
_hwLastElapseMs = 0;
_isPlaying = true;
// Wait for first frame outside the mutex to let decode threads run
@@ -312,7 +312,6 @@ namespace ANSCENTER {
_hwCudaAccel = false;
_hwEOF = false;
_hwFrameCount = 0;
_hwLastElapseMs = 0;
}
else {
// --- cv::VideoCapture fallback ---
@@ -526,21 +525,30 @@ namespace ANSCENTER {
_hwCudaAccel = _hwPlayer->isCudaHWAccel();
_totalFrames = static_cast<int64_t>(_hwPlayer->getDuration() / 1000.0 * 30.0);
_fps = 30.0;
std::string hwType = _hwCudaAccel ? "CUDA" : "D3D11VA/DXVA2";
// Decoder mode: CUDA when zero-copy NVDEC is active; otherwise the
// underlying CFilePlayer is running software decode (HW decoding is
// disabled by default in Setup() — saves VRAM). The old log message
// always said "D3D11VA/DXVA2" which was misleading.
const std::string hwType = _hwCudaAccel ? "CUDA" : "SW";
_logger.LogInfo("ANSVIDEOPLAYER::GetImage",
"HW decode ACTIVE (" + hwType + ") GPU[" + std::to_string(_hwGpuIndex) +
"Decode ACTIVE (" + hwType + ") GPU[" + std::to_string(_hwGpuIndex) +
"] " + std::to_string(_resWidth) + "x" + std::to_string(_resHeight),
__FILE__, __LINE__);
ANS_DBG("ANSVP_EOF",
"first frame received: %dx%d totalFrames~=%lld duration=%lldms",
_resWidth, _resHeight,
(long long)_totalFrames, (long long)_hwPlayer->getDuration());
}
// EOF detection: CFilePlayer auto-loops via av_seek_frame on AVERROR_EOF
// (file_player.cpp:434), so ANSVideoPlayer must detect the wrap itself.
// imgPts from getImage() is a monotonic call-counter, not a real PTS —
// use getElapse() (ms of real video position, updated from packet.pts
// in file_player.cpp:608). When the file loops, elapse drops back to ~0.
const int64_t elapseMs = _hwPlayer->getElapse();
if (_hwFrameCount > 10 && _hwLastElapseMs > 0 &&
elapseMs + 500 < _hwLastElapseMs) {
// (file_player.cpp:434). It now flips m_bLooped when that happens; we
// poll hasLooped() to detect the wrap.
//
// Why not getElapse(): m_nCurPos is non-monotonic in normal playback —
// MP4 packets are in decode order so video PTS oscillates with B-frames,
// AND the audio thread also writes m_nCurPos with audio PTS. Either can
// cause a >500ms backwards drop and falsely trip EOF mid-playback.
if (_hwPlayer->hasLooped()) {
_hwEOF = true;
if (_resHeight <= 0 || _resWidth <= 0) { _resHeight = imgH; _resWidth = imgW; }
_previousImage = cv::Mat(_resHeight, _resWidth, CV_8UC3, cv::Scalar(0, 0, 0));
@@ -549,11 +557,12 @@ namespace ANSCENTER {
if (_previousPTS < std::numeric_limits<int64_t>::max()) _previousPTS++;
else _previousPTS = 0;
pts = _previousPTS;
ANS_DBG("ANSVP_EOF",
"hasLooped() -> EOF locked at frameCount=%lld elapseMs=%lld",
(long long)_hwFrameCount, (long long)_hwPlayer->getElapse());
return _previousImage;
}
_hwLastElapseMs = elapseMs;
cv::Mat result = frame; // CFilePlayer returns owned Mat (already cloned internally)
// Apply cropping if enabled
+6 -7
View File
@@ -22,12 +22,12 @@ namespace ANSCENTER
{
protected:
cv::VideoCapture cap;
int _resWidth;
int _resHeight;
int64_t _totalFrames;
int64_t _currentFrame;
int64_t _previousPTS;
double _fps;
int _resWidth = 0;
int _resHeight = 0;
int64_t _totalFrames = 0;
int64_t _currentFrame = 0;
int64_t _previousPTS = 0;
double _fps = 0.0;
bool _isPlaying;
std::string _lastJpegImage;
cv::Mat _previousImage;
@@ -50,7 +50,6 @@ namespace ANSCENTER
bool _hwCudaAccel = false; // true = NVIDIA CUDA zero-copy available
bool _hwEOF = false; // true when video reached end of file
int64_t _hwFrameCount = 0; // frame counter for EOF detection
int64_t _hwLastElapseMs = 0; // last CFilePlayer elapse (ms); EOF detected when it wraps backwards
ANSVIDEOPLAYER();
~ANSVIDEOPLAYER() noexcept;
[[nodiscard]] bool Init(std::string licenseKey, std::string url);
+8 -8
View File
@@ -1481,7 +1481,7 @@ int VideoPlayerClientTest() {
int64_t pts = 0;
ANSVIDEOPLAYER* videoClient;
std::string testVideoFile = "E:\\Programs\\DemoAssets\\Videos\\classroom.mp4";
std::string testVideoFile = "E:\\Programs\\DemoAssets\\Videos\\classroom.mp4";//"E:\\Programs\\DemoAssets\\ALPR_Test\\cam17.mp4"
CreateANSVideoPlayerHandle(&videoClient, "", testVideoFile.c_str());
StartVideoPlayer(&videoClient);
@@ -1580,7 +1580,7 @@ int FilePlayerClientCVTest() {
if ((index == 1800) || (index == 2200) || (index == 2500)) { StopFilePlayer(&filePlayerClient); }
if ((index == 2000) || (index == 2300) || (index == 2700)) { StartFilePlayer(&filePlayerClient); }
if (index > 20000) break;*/
if (index > 2000) break;
// if (index > 2000) break;
auto start = std::chrono::system_clock::now();
cv::Mat* image = nullptr;
GetFilePlayerCVImage(&filePlayerClient, width, height, pts, &image);
@@ -1647,17 +1647,17 @@ int main()
// resolved inside ANSCV.dll (which is linked against libavcodec etc.),
// so this works without the unit test having to link FFmpeg itself.
//ANSCV_PrintFFmpegLicense_S();
FilePlayerClientDoubleDestroy();
FilePlayerClientCVTest();
VideoPlayerClientDoubleDestroy();
VideoPlayerClientTest();
//FilePlayerClientDoubleDestroy();
//FilePlayerClientCVTest();
//VideoPlayerClientDoubleDestroy();
// VideoPlayerClientTest();
//OpenCVFunctionTest();
//GenerateVideo();
//VideoTestClient();
// TestGetImage();
//PureOpenCV();
// RSTPTestClient();
//RSTPTestCVClient();
//RSTPTestClient();
RSTPTestCVClient();
//TestCreateImageFromJpegStringFile();
//TestCreateImageFromFile();
//for (int i = 0; i < 100; i++) {