Skip to content

Commit

Permalink
[demuxers/Matroska] Take early B-frames into account when enforcing f…
Browse files Browse the repository at this point in the history
…ixed frame rate

Else we end up with bad DTS and fail to save in copy mode to MP4.
  • Loading branch information
eumagga0x2a committed Aug 26, 2021
1 parent 3efa7b0 commit 0ad7461
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions avidemux_plugins/ADM_demuxers/Matroska/ADM_mkv.cpp
Expand Up @@ -420,18 +420,27 @@ bool mkvHeader::enforceFixedFrameRate(int num, int den)
int first=0;
while( track->index[first].Pts==ADM_NO_PTS && first<nb) first++; // we should have some at least
uint64_t zero= track->index[first].Pts;
for(int i=0;i<32;i++)
{
if(first+i >= nb) break;
uint64_t pts = track->index[first+i].Pts;
if(pts == ADM_NO_PTS) continue;
if(pts < zero) zero = pts;
}
uint64_t minimumPts32 = zero;
{
double dmultiple=zero+half;
dmultiple*=den;
dmultiple/=(1000000.*(double)num);
zero=((uint64_t)dmultiple*1000000*num)/den;
}
ADM_info("Num=%d Den=%d half=%d zero=%d first=%d\n",num,den,half,(int)zero,first);
for(int i=first+1;i<nb;i++)
for(int i=first;i<nb;i++)
{
uint64_t pts=track->index[i].Pts;
if(pts < track->index[first].Pts) continue;
pts-=track->index[first].Pts;
if(pts == ADM_NO_PTS) continue;
if(pts < minimumPts32) continue;
pts -= minimumPts32;
double dmultiple=(pts+half);
dmultiple*=den;
dmultiple/=(1000000.*(double)num);
Expand All @@ -446,8 +455,6 @@ bool mkvHeader::enforceFixedFrameRate(int num, int den)
#endif
track->index[i].Pts=reconstructed;
}
track->index[first].Pts = zero;

_videostream.dwScale=num;
_videostream.dwRate=den;
double f=num;
Expand Down Expand Up @@ -568,6 +575,15 @@ bool mkvHeader::ComputeDeltaAndCheckBFrames(uint32_t *minDeltaX, uint32_t *maxDe
first++; // we should have some at least
}
uint64_t zero= track->index[first].Pts;
for(int i=0;i<32;i++)
{
if(first+i >= nb)
break;
uint64_t pts = track->index[first+i].Pts;
if(pts == ADM_NO_PTS)
continue;
if(pts < zero) zero = pts;
}
ADM_info("Num=%d Den=%d zero=%d first=%d\n",num,den,(int)zero,first);
int valid=0;
for(int i=first;i<nb;i++)
Expand All @@ -578,7 +594,7 @@ bool mkvHeader::ComputeDeltaAndCheckBFrames(uint32_t *minDeltaX, uint32_t *maxDe
_framesNoPts.push_back(i);
continue;
}
if(pts<zero) // early B-frames
if(pts<zero) // bogus timing
{
_framesNoPts.push_back(i);
continue;
Expand Down

0 comments on commit 0ad7461

Please sign in to comment.