Skip to content

Commit

Permalink
Fix cuda decoder and improve the robustness of seeking with frame bas…
Browse files Browse the repository at this point in the history
…ed av_seek_frame (#103)

* fix pts in cuda decoder

* using frame based seek rather than pts
  • Loading branch information
zhreshold authored Oct 18, 2020
1 parent 584e5f8 commit 7b6c0e9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/video/nvcodec/cuda_threaded_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ int CUThreadedDecoder::HandlePictureDisplay_(CUVIDPARSERDISPINFO* disp_info) {
if (!arr.defined()) {
return 0;
}
arr.pts = disp_info->timestamp;
bool skip = false;
{
std::lock_guard<std::mutex> lock(pts_mutex_);
Expand Down
12 changes: 4 additions & 8 deletions src/video/video_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,13 @@ bool VideoReader::Seek(int64_t pos) {
decoder_->Clear();
eof_ = false;

int64_t ts = FrameToPTS(pos);
int flag = curr_frame_ > pos ? AVSEEK_FLAG_BACKWARD : 0;
int flag = curr_frame_ > pos ? AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME : AVSEEK_FLAG_FRAME;

// std::cout << "Seek " << pos << " at pts " << ts << ", flag " << flag << std::endl;
int ret = av_seek_frame(fmt_ctx_.get(), actv_stm_idx_, ts, flag);
if (flag != AVSEEK_FLAG_BACKWARD && ret < 0){
// std::cout << "seek wrong, retry with flag " << AVSEEK_FLAG_BACKWARD << std::endl;
ret = av_seek_frame(fmt_ctx_.get(), actv_stm_idx_, ts, AVSEEK_FLAG_BACKWARD);
int ret = av_seek_frame(fmt_ctx_.get(), actv_stm_idx_, pos, flag);
if (flag == AVSEEK_FLAG_FRAME && ret < 0) {
ret = av_seek_frame(fmt_ctx_.get(), actv_stm_idx_, pos, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME);
}
if (ret < 0) LOG(WARNING) << "Failed to seek file to position: " << pos;
// LOG(INFO) << "seek return: " << ret;
decoder_->Start();
if (ret >= 0) {
curr_frame_ = pos;
Expand Down

0 comments on commit 7b6c0e9

Please sign in to comment.