@@ -103,6 +103,7 @@ FFmpeg_Glue::FFmpeg_Glue (const string &FileName_, int Scale_Width_, int Scale_H
103
103
VideoFrameCount=0 ;
104
104
VideoFrameCount_Max=0 ;
105
105
VideoDuration=0 ;
106
+ VideoFirstTimeStamp=(uint64_t )-1 ;
106
107
IsComplete=false ;
107
108
108
109
// FFmpeg pointers
@@ -180,7 +181,20 @@ FFmpeg_Glue::FFmpeg_Glue (const string &FileName_, int Scale_Width_, int Scale_H
180
181
if (VideoStream)
181
182
{
182
183
VideoFrameCount_Max=VideoFrameCount=VideoStream->nb_frames ;
183
- VideoDuration=((double )VideoStream->duration )*VideoStream->time_base .num /VideoStream->time_base .den ;
184
+ if (VideoStream->duration !=AV_NOPTS_VALUE)
185
+ VideoDuration=((double )VideoStream->duration )*VideoStream->time_base .num /VideoStream->time_base .den ;
186
+
187
+ // If video duration is not known, estimating it
188
+ if (VideoDuration==0 && FormatContext->duration !=AV_NOPTS_VALUE)
189
+ VideoDuration=((double )FormatContext->duration )/AV_TIME_BASE;
190
+
191
+ // If frame count is not known, estimating it
192
+ if (VideoFrameCount==0 && VideoStream->avg_frame_rate .num && VideoStream->avg_frame_rate .den && VideoDuration)
193
+ VideoFrameCount=VideoDuration*VideoStream->avg_frame_rate .num /VideoStream->avg_frame_rate .den ;
194
+ if (VideoFrameCount==0
195
+ && ((VideoStream->time_base .num ==1 && VideoStream->time_base .den >=24 && VideoStream->time_base .den <=60 )
196
+ || (VideoStream->time_base .num ==1001 && VideoStream->time_base .den >=24000 && VideoStream->time_base .den <=60000 )))
197
+ VideoFrameCount=VideoStream->duration ;
184
198
}
185
199
if (VideoFrameCount_Max==0 )
186
200
{
@@ -319,9 +333,9 @@ FFmpeg_Glue::FFmpeg_Glue (const string &FileName_, int Scale_Width_, int Scale_H
319
333
key_frame[x_Max]=1 ; // Forcing key_frame to 1 if it is missing from the XML, for decoding
320
334
321
335
x_Max++;
322
- if (VideoFrameCount< x_Max)
336
+ if (x_Max>=VideoFrameCount )
323
337
{
324
- VideoFrameCount=x_Max;
338
+ VideoFrameCount=x_Max+ 1 ;
325
339
if (!ts.empty () && ts!=" N/A" )
326
340
VideoDuration=x[1 ][x_Max-1 ]+d[x_Max-1 ];
327
341
if (FormatContext==NULL )
@@ -437,7 +451,7 @@ void FFmpeg_Glue::Seek(size_t Pos)
437
451
// DTS computing
438
452
long long DTS=Pos;
439
453
DTS*=VideoStream->duration ;
440
- DTS/=VideoFrameCount;
454
+ DTS/=VideoFrameCount; // TODO: seek based on time stamp
441
455
DTS_Target=(int )DTS;
442
456
443
457
// Seek
@@ -595,6 +609,8 @@ bool FFmpeg_Glue::OutputFrame(AVPacket* TempPacket, bool Decode)
595
609
Image2=NULL ;
596
610
Jpeg.Data =NULL ;
597
611
Jpeg.Size =0 ;
612
+ TempPacket->data +=TempPacket->size ;
613
+ TempPacket->size =0 ;
598
614
return true ;
599
615
}
600
616
TempPacket->data +=Bytes;
@@ -863,10 +879,27 @@ bool FFmpeg_Glue::Process(AVFrame* &FilteredFrame, AVFilterGraph* &FilterGraph,
863
879
if (WithStats)
864
880
{
865
881
x[0 ][x_Max]=x_Max;
866
- int64_t ts=(Frame->pkt_pts ==AV_NOPTS_VALUE)?Frame->pkt_dts :Frame->pkt_pts ; // Using DTS is PTS is not available
882
+ int64_t ts=Frame->pkt_dts ;
883
+ if (ts==AV_NOPTS_VALUE && x_Max)
884
+ ts=(int )((x[1 ][x_Max-1 ]*x_Max/(x_Max-1 ))/VideoStream->time_base .num *VideoStream->time_base .den )+VideoFirstTimeStamp; // TODO: understand how to do with first timestamp not being 0 and last timestamp being AV_NOPTS_VALUE e.g. op1a-mpeg2-wave_hd.mxf
885
+ // int64_t ts=(Frame->pkt_pts==AV_NOPTS_VALUE)?Frame->pkt_dts:Frame->pkt_pts; // Using DTS is PTS is not available // TODO: check if stats are based on DTS or PTS
886
+ if (VideoFirstTimeStamp==(uint64_t )-1 )
887
+ VideoFirstTimeStamp=ts;
888
+ if (ts<VideoFirstTimeStamp)
889
+ {
890
+ for (size_t Pos=0 ; Pos<x_Max; Pos++)
891
+ {
892
+ x[1 ][Pos]-=VideoFirstTimeStamp-ts;
893
+ x[2 ][Pos]=x[1 ][Pos]/60 ;
894
+ x[3 ][Pos]=x[2 ][Pos]/60 ;
895
+ }
896
+ }
897
+ ts-=VideoFirstTimeStamp;
867
898
if (ts!=AV_NOPTS_VALUE)
868
899
{
869
900
x[1 ][x_Max]=((double )ts)*VideoStream->time_base .num /VideoStream->time_base .den ;
901
+ if (x[1 ][x_Max]>VideoDuration)
902
+ VideoDuration=x[1 ][x_Max];
870
903
x[2 ][x_Max]=x[1 ][x_Max]/60 ;
871
904
x[3 ][x_Max]=x[2 ][x_Max]/60 ;
872
905
}
@@ -931,6 +964,8 @@ bool FFmpeg_Glue::Process(AVFrame* &FilteredFrame, AVFilterGraph* &FilterGraph,
931
964
key_frame[x_Max]=Frame->key_frame ;
932
965
933
966
x_Max++;
967
+ if (x_Max>=VideoFrameCount)
968
+ VideoFrameCount=x_Max+1 ;
934
969
}
935
970
936
971
if (GetAnswer==AVERROR (EAGAIN) || GetAnswer==AVERROR_EOF)
0 commit comments