-
Notifications
You must be signed in to change notification settings - Fork 116
/
ADM_edSearch.cpp
577 lines (553 loc) · 18.4 KB
/
ADM_edSearch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/***************************************************************************
ADM_edSearch.cpp - description
-------------------
begin : Sat Apr 13 2002
copyright : (C) 2002 by mean
email : fixounet@free.fr
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "ADM_cpp.h"
using std::string;
#include "ADM_default.h"
#include "fourcc.h"
#include "ADM_edit.hxx"
#include "ADM_vidMisc.h"
#if 1
#define pivotPrintf(...) {}
#else
#define pivotPrintf printf
#endif
/**
\fn getNKFramePTS
*/
bool ADM_Composer::getNKFramePTS(uint64_t *frameTime)
{
uint64_t refTime,nkTime,segTime;
int lastSeg=_segments.getNbSegments();
uint32_t seg;
bool switchedSeg=false;
bool r=false;
// 1- Convert frameTime to segments
if(false== _segments.convertLinearTimeToSeg( *frameTime, &seg, &segTime))
{
ADM_warning(" Cannot find seg for time %" PRId64"\n",*frameTime);
return false;
}
//
again:
_SEGMENT *s=_segments.getSegment(seg);
uint32_t ref=s->_reference;
// 2- Now search the previous keyframe in the ref image...
// The time in reference = relTime+segmentStartTime
refTime=s->_refStartTimeUs+segTime; // Absolute time in the reference image
if(switchedSeg && !refTime)
{
_VIDEOS *vid=_segments.getRefVideo(ref);
if(!vid->firstFramePts)
{
nkTime=0;
r=true;
}
}
if(switchedSeg && refTime>0)
refTime--; // The next segment may start with a keyframe
if(r==false)
r=searchNextKeyFrameInRef(ref,refTime,&nkTime);
// 3- if it does not belong to the same seg ....
if(r==false || nkTime >= (s->_refStartTimeUs+s->_durationUs))
{
if(seg>=lastSeg-1)
{
ADM_warning(" No next keyframe keyfr for frameTime \n");
return false;
}
// Go to the next segment
if(seg==_segments.getNbSegments()-1)
{
ADM_warning("Last segment\n");
return false;
}
seg++;
switchedSeg=true;
segTime=0;
r=false;
goto again;
}
// Gotit, now convert it to the linear time
// We have the time in the ref video, convert it to relatative to this segment
nkTime-=s->_refStartTimeUs; // Ref to segment...
_segments.convertSegTimeToLinear(seg,nkTime,frameTime);
return true;
}
/**
\fn getPKFramePTS
*/
bool ADM_Composer::getPKFramePTS(uint64_t *frameTime)
{
uint64_t refTime,nkTime,segTime;
uint32_t seg;
bool r;
// 1- Convert frameTime to segments
if(false== _segments.convertLinearTimeToSeg( *frameTime, &seg, &segTime))
{
ADM_warning(" Cannot find seg for time %" PRId64"\n",*frameTime);
return false;
}
// Special case : The very first frame FIXME
// Only applies if the first segment as a 0 ref time, i.e. beginning of video..
if(*frameTime<=1 && seg==0)
{
_VIDEOS *vid=_segments.getRefVideo(0);
_SEGMENT *s=_segments.getSegment(seg);
if(!s->_refStartTimeUs)
{
uint64_t pts=vid->firstFramePts;
if(pts)
{
*frameTime+=pts;
ADM_warning("This video does not start at 0 but at %" PRIu64" ms, compensating\n",pts/1000);
_segments.convertLinearTimeToSeg(*frameTime, &seg, &segTime);
}
}
}
//
again:
_SEGMENT *s=_segments.getSegment(seg);
int64_t delta=*frameTime-s->_startTimeUs; // Delta compared to the beginning of this seg
if(delta >= s->_durationUs) delta=s->_durationUs-1;
delta+=s->_refStartTimeUs;
if(delta<0)
{
ADM_error("Time is negative\n");
return false;
}
uint32_t ref=s->_reference;
// 2- Now search the previous keyframe in the ref image...
// The time in reference = relTime+segmentStartTime
refTime=delta;
r=searchPreviousKeyFrameInRef(ref,refTime,&nkTime);
// 3- if it does not belong to the same seg ....
if(r==false || nkTime < (s->_refStartTimeUs))
{
if(!seg)
{
ADM_warning("No previous keyframe for frameTime %" PRIu64" in ref %" PRIu32" seg: %" PRIu32" nkTime %" PRIu64" refTime: %" PRIu64" ms startTime=%" PRIu64" r=%d\n",
*frameTime,ref,seg,nkTime/1000,refTime/1000,s->_refStartTimeUs/1000,r);
return false;
}
// Go to the next segment
seg--;
goto again;
}
// Gotit, now convert it to the linear time
nkTime-=s->_refStartTimeUs; // Ref to segment...
_segments.convertSegTimeToLinear(seg,nkTime,frameTime);
return true;
}
/**
\fn getPtsDtsDelta
*/
bool ADM_Composer::getPtsDtsDelta(uint64_t frameTime, uint64_t *outDelta)
{
uint64_t refTime,segTime;
uint32_t seg;
// 1- Convert frameTime to segments
if(false== _segments.convertLinearTimeToSeg( frameTime, &seg, &segTime))
{
ADM_warning(" Cannot find seg for time %" PRId64"\n",frameTime);
return false;
}
//
_SEGMENT *s=_segments.getSegment(seg);
int64_t delta=(int64_t)frameTime-(int64_t)s->_startTimeUs; // Delta compared to the beginning of this seg
delta+=s->_refStartTimeUs;
if(delta<0)
{
ADM_error("Time is negative\n");
return false;
}
// Delta is now the absolute PTS time in reference video
uint32_t ref=s->_reference;
refTime=delta;
uint64_t dts;
if(false==_segments.dtsFromPts(ref,refTime,&dts))
{
ADM_error("Cannot get dtsFromDts for time %" PRIu64"\n",refTime);
*outDelta=0;
return false;
}
// Ok we have PTS and DTS, returns difference
*outDelta=refTime-dts;
return true;
}
/**
\fn searchNextKeyFrameInRef
\brief Search next key frame in ref video ref
@param ref: # of ref video
@param refTime : PTS to search keyframe after
@param nkTime : Time of the ref video
*/
bool ADM_Composer::searchNextKeyFrameInRef(int ref,uint64_t refTime,uint64_t *nkTime)
{
int curFrame=0;
_VIDEOS *v=_segments.getRefVideo(ref);
// look up the frame we are starting from...
if(!getFrameNumFromPtsOrBefore(v,refTime,curFrame))
{
ADM_warning("Cannot identify the frame for time %s in reference.\n",ADM_us2plain(refTime));
}
uint32_t nbFrame=v->_nb_video_frames;
uint64_t pts,dts;
curFrame -= 16; // safety margin to deal with early B-frames
if(curFrame < 0) curFrame = 0;
for(int i=curFrame;i<nbFrame;i++)
{
uint32_t flags;
v->_aviheader->getFlags(i,&flags);
if(!(flags & AVI_KEY_FRAME)) continue;
v->_aviheader->getPtsDts(i,&pts,&dts);
if(pts==ADM_NO_PTS) continue;
if(pts>refTime)
{
ADM_info("Found next keyframe: frame %" PRIu32", ref time %s (%" PRIu64")\n", i, ADM_us2plain(pts), pts);
*nkTime=pts;
return true;
}
}
return false;
}
/**
* \fn getFrameNumBeforePtsOrBefore
* \brief Search the framenumber that has a valid PTS = what's given or before
* it can be equal to refTime
* @param v
* @param refTime
* @param frameNumber
* @return
*/
bool ADM_Composer::getFrameNumFromPtsOrBefore(_VIDEOS *v,uint64_t refTime,int &frameNumber)
{
if(refTime == ADM_NO_PTS)
return false;
if(refTime <= v->firstFramePts)
{
frameNumber = 0;
return true;
}
uint32_t nbFrame = v->_nb_video_frames;
uint64_t pts, dts;
uint32_t curFrame = nbFrame >> 1;
uint32_t splitMoval = (curFrame + 1) >> 1;
uint32_t minFrame = 0;
bool worked = false;
#define MAX_REORDER_DEPTH 32 /* max_ref * 2 fields */
pivotPrintf("Looking for frame with pts %" PRIu64" us (%s)\n",refTime,ADM_us2plain(refTime));
// Try to find the frame that as the timestamp close enough to refTime, while being smaller
do
{
// seems like the pts determination, not always works -> retry if necessary
uint32_t tmpFrame = curFrame;
do
{
pts = ADM_NO_PTS;
worked = v->_aviheader->getPtsDts(tmpFrame,&pts,&dts);
if(worked && pts != ADM_NO_PTS)
break; // found
if(tmpFrame <= minFrame)
{
ADM_warning("Frames between %" PRIu32" and %" PRIu32" have no PTS.\n",minFrame,curFrame);
break;
}
tmpFrame--;
} while(true);
if(pts == ADM_NO_PTS)
{
tmpFrame = curFrame + 1;
do
{
if(tmpFrame >= nbFrame)
{
ADM_warning("The whole segment is corrupted, aborting seek.\n");
return false;
}
minFrame = tmpFrame;
worked = v->_aviheader->getPtsDts(tmpFrame,&pts,&dts);
if(worked && pts != ADM_NO_PTS)
break; // found
tmpFrame++;
} while(true);
}
curFrame = tmpFrame; // resync
// are we lucky?
if(pts == refTime)
{
pivotPrintf("Perfect match for frame %" PRIu32"\n",curFrame);
frameNumber = curFrame;
return true;
}
// nope, continue to bisect
pivotPrintf("SplitMoval=%d\n",splitMoval);
if(splitMoval < MAX_REORDER_DEPTH / 2)
break;
pivotPrintf("Pivot frame is %" PRIu32" at %s\n",curFrame,ADM_us2plain(pts));
if (pts > refTime)
{
if (curFrame <= splitMoval + minFrame)
{
curFrame = minFrame;
}else
{
curFrame -= splitMoval;
}
}else
{
curFrame += splitMoval;
if(curFrame >= nbFrame)
curFrame = nbFrame - 1;
}
splitMoval++;
splitMoval >>= 1;
} while(refTime != pts);
// linear search with a generous safety margin
curFrame = (curFrame < MAX_REORDER_DEPTH)? 0 : curFrame - MAX_REORDER_DEPTH;
pivotPrintf("Starting linear search at frame %" PRIu32", total frames: %" PRIu32"\n",curFrame,nbFrame);
int candidate = -1;
uint64_t last = 0;
int overrun = MAX_REORDER_DEPTH;
for(int i = 0; i < MAX_REORDER_DEPTH * 2; i++)
{
if(curFrame >= nbFrame) break;
if(overrun < 1) break;
pts = ADM_NO_PTS;
worked = v->_aviheader->getPtsDts(curFrame,&pts,&dts);
if(!worked || pts == ADM_NO_PTS)
{
pivotPrintf("Linear search, skipping frame %" PRIu32"\n",curFrame);
curFrame++;
continue;
}
if(pts == refTime)
{
pivotPrintf("Perfect match for frame %" PRIu32"\n",curFrame);
frameNumber = curFrame;
return true;
}
if(pts < refTime)
{
if(!last || pts > last)
{
candidate = curFrame;
last = pts;
}
} else
{
overrun--;
}
curFrame++;
}
if(candidate >= 0)
{
ADM_info("Best candidate for time %s in reference is frame %" PRIu32"\n",ADM_us2plain(refTime),candidate);
frameNumber = candidate;
return true;
}
ADM_warning("Search for frame matching time %s in reference failed.\n",ADM_us2plain(refTime));
return false;
}
/**
\fn searchPreviousKeyFrameInRef
\brief Search next key frame in ref video ref
@param ref: # of ref video
@param refTime : PTS to search keyframe after
@param nkTime : Time of the ref video
*/
bool ADM_Composer::searchPreviousKeyFrameInRef(int ref,uint64_t refTime,uint64_t *nkTime)
{
if(!refTime)
return false;
// Search for the current frame with quick search
_VIDEOS *v = _segments.getRefVideo(ref);
uint64_t pts, dts;
int curFrame;
// look up the frame we are starting from...
if(!getFrameNumFromPtsOrBefore(v,refTime,curFrame))
{
ADM_warning("Cannot locate the frame we are talking about at PTS=%s\n",ADM_us2plain(refTime));
return false;
}
// rewind until we find a keyframe
for (int i=curFrame; i>=0; i--)
{
uint32_t flags;
v->_aviheader->getFlags(i,&flags);
if(!(flags & AVI_KEY_FRAME))
continue;
if(!v->_aviheader->getPtsDts(i,&pts,&dts))
continue;
if(pts == ADM_NO_PTS)
continue;
pivotPrintf("-- Previous Kf ! This one is %s\n",ADM_us2plain(pts));
pivotPrintf("-- Ref is %s\n",ADM_us2plain(refTime));
pivotPrintf("-- DTS %s\n",ADM_us2plain(dts));
if(pts>=refTime)
continue;
*nkTime = pts; // ok found it
#if 0
for(int j=0;j<5;j++)
{
if(!v->_aviheader->getPtsDts(i+j,&pts,&dts)) continue ;
printf("Neighbor [%d] = %s\n",j,ADM_us2plain(pts));
}
#endif
return true;
}
ADM_warning("Cannot find keyframe with PTS less than %s\n",ADM_us2plain(refTime));
return false;
}
/**
\fn getDtsFromPts
\brief Estimate DTS from PTS
*/
bool ADM_Composer::getDtsFromPts(uint64_t *time)
{
uint64_t segTime;
uint32_t seg;
// 1- Convert frameTime to segments
if(false== _segments.convertLinearTimeToSeg( *time, &seg, &segTime))
{
ADM_warning(" Cannot find seg for time %" PRId64"\n",*time);
return false;
}
_SEGMENT *s=_segments.getSegment(seg);
//
// Search the frame with correct PTS
uint64_t pts=segTime+s->_refStartTimeUs;
uint64_t dts;
if(false==_segments.dtsFromPts(s->_reference,pts,&dts))
{
ADM_warning("Cannot get DTS from PTS=%" PRIu64"ms\n",pts/1000);
return false;
}
dts=dts+s->_startTimeUs;
if(dts<s->_refStartTimeUs)
{
ADM_warning("Warning DTS time is negative\n");
dts=0;
}else
dts=dts-s->_refStartTimeUs;
*time=dts;
return true;
}
/**
* \fn getLastKeyFramePts
* \brief returns the pts of the last accessible keyframe
* @return
*/
uint64_t ADM_Composer::getLastKeyFramePts(void)
{
int lastSeg=_segments.getNbSegments();
int seg;
uint64_t pts,dts;
int lastRefFrameInSegment;
if(!lastSeg) return ADM_NO_PTS;
for(seg=lastSeg-1;seg>=0;seg--)
{
_SEGMENT *s=_segments.getSegment(seg);
_VIDEOS *v=_segments.getRefVideo(s->_reference);
uint64_t endTimeInRef=(s->_refStartTimeUs)+(s->_durationUs);
// we dont want the last kf of the video, but the last kf of the segment
// so we truncate the search to the part we are interested in taking start + duraiton of the seg to get a starting point
// If we can't get it, it cannot work properly anyway
if(!getFrameNumFromPtsOrBefore(v,endTimeInRef,lastRefFrameInSegment))
{
ADM_warning("Cannot map the last frame in segment to reference at PTS=%s\n",ADM_us2plain(endTimeInRef));
lastRefFrameInSegment=v->_nb_video_frames;
}
if(lastRefFrameInSegment==v->_nb_video_frames) continue;
for(int frame=lastRefFrameInSegment;frame>=0;frame--)
{
uint32_t flags;
pts=ADM_NO_PTS;
v->_aviheader->getFlags(frame,&flags);
if(!(flags & AVI_KEY_FRAME)) continue;
// if B-frames are present, pts doesn't necessarily become smaller when frame is decremented,
// we must check whether pts is still in range and effectively go a full GOP backwards if not
ADM_info("frame %d is an intra, checking whether pts is in range...\n",frame);
v->_aviheader->getPtsDts(frame,&pts,&dts);
if(pts==ADM_NO_PTS) continue;
if(pts>=endTimeInRef)
{
ADM_warning("frame %d pts in ref = %" PRIu64" is not in range, skipping\n",frame,pts);
continue;
}
ADM_info("found last keyframe at %d, time in reference=%s\n",frame,ADM_us2plain(pts));
break;
}
if(pts!=ADM_NO_PTS)
{
if(pts<s->_refStartTimeUs) continue; // no keyframe in the last segment, retry with the previous one
pts+=s->_startTimeUs-s->_refStartTimeUs;
ADM_info("found last keyframe at %s\n",ADM_us2plain(pts));
return pts;
}
}
ADM_info("Cannot find lastKeyFrame with a valid PTS\n");
return ADM_NO_PTS;
}
/**
* \fn getFirstFrameInSegmentPts
*/
uint64_t ADM_Composer::getFirstFrameInSegmentPts(uint32_t segment)
{
if(segment >= _segments.getNbSegments())
return ADM_NO_PTS;
_SEGMENT *s = _segments.getSegment(segment);
ADM_assert(s);
_VIDEOS *v = _segments.getRefVideo(s->_reference);
ADM_assert(v);
vidHeader *h = v->_aviheader;
ADM_assert(h);
int frame = 0;
uint64_t start = s->_refStartTimeUs;
if(!getFrameNumFromPtsOrBefore(v,start,frame))
return ADM_NO_PTS;
#define MAX_REF_FRAMES (32) /* 16 max ref frames for H.264 * 2 fields */
frame = (frame > MAX_REF_FRAMES)? frame - MAX_REF_FRAMES : 0;
int depth = MAX_REF_FRAMES;
uint64_t candidate = ADM_NO_PTS;
for(int i=frame; i < v->_nb_video_frames; i++)
{
uint64_t pts,dts;
h->getPtsDts(i,&pts,&dts);
if(pts == ADM_NO_PTS)
continue;
if(pts == start) // perfect match
return s->_startTimeUs;
if(pts > start)
{
if(depth < 0)
break;
depth--;
pts += s->_startTimeUs;
pts -= start;
if(pts >= s->_startTimeUs + s->_durationUs)
continue;
if(candidate == ADM_NO_PTS)
{
candidate = pts;
continue;
}
if(candidate > pts)
candidate = pts;
}
}
return candidate;
}
//EOF