-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathFrames.cs
More file actions
498 lines (406 loc) · 18.4 KB
/
Frames.cs
File metadata and controls
498 lines (406 loc) · 18.4 KB
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
using OxyPlot;
using System;
using System.Collections.Generic;
namespace FFBitrateViewer
{
public enum FramePictType
{
I,
P,
B
};
public class FramesBitRates
{
public BitRate? Avg { get; set; }
public BitRate? Avg2 { get; set; }
public BitRate? Max { get; set; }
public BitRate? Min { get; set; }
}
public class Frame
{
public double Duration { get; set; }
public double EndTime { get { return StartTime + Duration; } }
public FramePictType? FrameType { get; set; } // I, P, B
public bool IsOrdered { get; set; }
public long? Pos { get; set; }
public int Size { get; set; } // Frame size in bytes
public double StartTime { get { return StartTimeRaw ?? 0; } }
public double? StartTimeRaw { get; set; }
public static Frame? CreateFrame(FFProbePacket packet)
{
if (packet.DurationTime == null || packet.Size == null) return null;
return new Frame()
{
Duration = (double)packet.DurationTime,
FrameType = packet.Flags?.IndexOf('K') >= 0 ? FramePictType.I : null,
IsOrdered = false, // 'Packets' returned by FFProbe are ordered by DTS, not PTS so will need to order them later when adding onto list
Pos = packet.Pos,
Size = (int)packet.Size,
StartTimeRaw = packet.PTSTime
};
}
public static Frame? CreateFrame(FFProbeFrame frame)
{
if (frame.DurationTime == null || frame.Size == null) return null;
FramePictType? pictType = frame.PictType?[0] switch
{
'I' => FramePictType.I,
'P' => FramePictType.P,
'B' => FramePictType.B,
_ => null
};
return new Frame()
{
Duration = (double)frame.DurationTime,
FrameType = pictType,
IsOrdered = true, // 'Frames' returned by FFProbe are already ordered by BestEffortTimestampTime
Pos = frame.Pos,
Size = (int)frame.Size,
StartTimeRaw = frame.BestEffortTimestampTime ?? frame.PTSTime
};
}
// Calculate what size of the frame accounts for specified interval
public int GetSize(double? intervalStartTime = null, double? intervalEndTime = null)
{
// No interval specified
if (intervalStartTime == null || intervalEndTime == null) return Size;
// The frame is outside of the interval so none of its size is taken into account
if (EndTime <= (double)intervalStartTime || StartTime >= (double)intervalEndTime) return 0;
// The frame is fully inside of the interval so all of its size is taken into account
if (StartTime >= (double)intervalStartTime && EndTime <= (double)intervalEndTime) return Size;
// Only a part of the frame is inside the interval, so calculating what part of its size accounts for the size of the interval
var start = double.Max(StartTime, (double)intervalStartTime);
var end = double.Min(EndTime, (double)intervalEndTime);
return (int)double.Round(Size * ((end - start) / Duration));
}
public List<DataPoint> DataPointGet(double startTimeOffset, bool isLast = false, int sizeDivider = 1000 /* kilo */)
{
List<DataPoint> data = [];
data.Add(new DataPoint(StartTime - startTimeOffset, Size / sizeDivider));
if(isLast) data.Add(new DataPoint(EndTime - startTimeOffset, Size / sizeDivider));
return data;
}
}
public class GOP
{
private List<Frame> Frames { get; set; } = [];
public bool IsEmpty { get { return Frames.Count == 0; } }
public bool IsRealGOP { get { return FixedTimeDuration == null; } }
public int Size { get; private set; } = 0;
public int BitRate { get { return Duration == 0 ? 0 : (int)double.Round(Size / (double)Duration); } }
public double StartTimeRaw { get; private set; } = 0;
public double StartTimeOffset { get; private set; } = 0;
public double StartTime { get { return IsRealGOP ? FramesStartTime - StartTimeOffset : Math.Max(FixedTimeStartTime, FramesStartTime - StartTimeOffset); } }
public double EndTime { get { return IsRealGOP ? FramesEndTime - StartTimeOffset : Math.Min(FixedTimeEndTime, FramesEndTime - StartTimeOffset); } }
public double Duration { get { return EndTime - StartTime; } }
public double? FixedTimeDuration { get; private set; } // NULL -- for real GOPs, GOP duration (in seconds) for fixed length GOPs as 'per second'
public double FixedTimeStartTime { get { return StartTimeRaw; } }
public double FixedTimeEndTime { get { return StartTimeRaw + (FixedTimeDuration ?? 0); } }
public double FramesDuration { get { return FramesEndTime - FramesStartTime; } }
public double FramesStartTime { get { return IsEmpty ? StartTimeRaw : Frames[0].StartTime; } }
public double FramesEndTime { get { return IsEmpty ? StartTimeRaw : Frames[^1].EndTime; } }
public GOP(double startTimeOffset, Frame? frame, double? startTime = null, double? duration = null)
{
if (duration < 0) throw new ArgumentException("Must be greater then 0", nameof(duration)); // todo@ can it be 0?
FixedTimeDuration = duration;
StartTimeOffset = startTimeOffset;
StartTimeRaw = startTime ?? frame?.StartTime ?? 0;
if (frame != null) Add(frame);
}
public void Clear()
{
Frames.Clear();
Size = 0;
}
public void Add(Frame frame)
{
//if (IsEmpty) StartTimeRaw = frame?.StartTime ?? 0;
if (FixedTimeDuration == null) // Not SECOND based
{
if (frame.FrameType == FramePictType.I)
{
if (!IsEmpty) throw new ArgumentException("I-frame can only be the first in GOP");
}
else
{
// No exception as frames could be added out of order
// So it is possible that P-frame will be added 1st and then I-frame with smaller start time will be added
}
}
Frames.Add(frame);
SizeAdd(frame);
}
private void SizeAdd(Frame frame)
{
Size += frame.GetSize(StartTime + StartTimeOffset, StartTime + StartTimeOffset + FixedTimeDuration);
}
public List<DataPoint> DataPointsGet(bool isLast = false, int sizeDivider = 1000 /* kilo */)
{
List<DataPoint> data = [];
if (IsEmpty) return data;
var bitRate = (int)double.Round(8 /* Byte => bit */ * BitRate / sizeDivider);
data.Add(new DataPoint(StartTime, bitRate));
if(isLast) data.Add(new DataPoint(EndTime, bitRate));
return data;
}
}
public class GOPsBy
{
protected List<Frame> Frames { get; private set; } = [];
protected List<GOP> GOPs { get; set; } = [];
public int? MaxSize { get; private set; }
public int? MinSize { get; private set; }
public ulong? TotalSize { get; private set; }
public double StartTimeOffset { get; protected set; } = 0;
public GOPsBy() { }
protected void CalcMinMax()
{
int? max = null;
int? min = null;
ulong? total = null;
foreach (var gop in GOPs)
{
if (total == null)
{
total = (ulong)gop.Size;
}
else
{
total += (ulong)gop.Size;
}
if (max == null || gop.BitRate > max) max = gop.BitRate;
if (min == null || gop.BitRate < min) min = gop.BitRate;
}
MaxSize = max;
MinSize = min;
TotalSize = total;
}
public void Clear()
{
GOPs.Clear();
MaxSize = null;
MinSize = null;
TotalSize = null;
StartTimeOffset = 0;
}
public List<DataPoint> DataPointsGet(int sizeDivider = 1000 /* kilo */)
{
List<DataPoint> data = [];
for (var idx = 0; idx < GOPs.Count; ++idx) data.AddRange(GOPs[idx].DataPointsGet(idx == GOPs.Count - 1, sizeDivider));
return data;
}
public void SetFrames(List<Frame> frames)
{
Frames = frames;
}
}
public class GOPsByGOP : GOPsBy
{
public GOPsByGOP() : base() { }
public bool Calc(double startTimeOffset)
{
if (Frames.Count == 0 || (GOPs.Count > 0 && StartTimeOffset == startTimeOffset)) return false; // no data or calculated already
StartTimeOffset = startTimeOffset;
GOP? gop = null;
foreach (var frame in Frames)
{
if (gop == null)
{
gop = new(StartTimeOffset, frame);
continue;
}
// On every I-frame finalyzing current GOP and creating a new one
if (frame.FrameType == FramePictType.I)
{
GOPs.Add(gop);
gop = new(StartTimeOffset, frame);
continue;
}
gop.Add(frame);
}
if (gop != null) GOPs.Add(gop);
CalcMinMax();
return true;
}
}
public class GOPsByTime : GOPsBy
{
public double IntervalDuration { get; private set; } = 1;
public GOPsByTime(double intervalDuration) : base() {
IntervalDuration = intervalDuration;
}
public bool Calc(double startTimeOffset)
{
if (Frames.Count == 0 || (GOPs.Count > 0 && StartTimeOffset == startTimeOffset)) return false; // no data or calculated already
StartTimeOffset = startTimeOffset;
GOP gop = new(StartTimeOffset, null, 0, IntervalDuration);
foreach (var frame in Frames)
{
// The frame is started in one of the next GOP, so finallizing current GOP and creating a new one
// It is possible that the frame if far away from prev GOP, so adding a number of GOPs if needed
while ((frame.StartTime - StartTimeOffset) >= gop.FixedTimeEndTime)
{
GOPs.Add(gop);
gop = new(StartTimeOffset, null, gop.FixedTimeEndTime, IntervalDuration);
}
gop.Add(frame);
// The frame is ended in one of the next GOPs, so finallizing current GOP and creating a new one
while ((frame.EndTime - StartTimeOffset) > gop.FixedTimeEndTime)
{
GOPs.Add(gop);
gop = new(StartTimeOffset, frame, gop.FixedTimeEndTime, IntervalDuration);
}
}
if (gop != null) GOPs.Add(gop);
CalcMinMax();
return true;
}
}
public class Frames
{
public int Count { get { return FramesList.Count; } }
public double? Duration { get { return FramesList.Count > 0 ? (FramesList[^1].EndTime - (IsAdjustStartTime ? StartTime : 0)) : null; } }
private List<Frame> FramesList { get; set; } = [];
private GOPsByGOP FramesByGOP { get; set; } = new();
private GOPsByTime FramesByTime { get; set; } = new(1);
public double? FramesDuration { get { return FramesList.Count > 0 ? (FramesList[^1].EndTime - FramesList[0].StartTime) : null; } }
public double? FramesEndTime { get { return FramesList.Count > 0 ? FramesList[^1].EndTime : null; } }
public double? FramesStartTime { get { return FramesList.Count > 0 ? FramesList[0].StartTime : null; } }
public bool IsAdjustStartTime { get; private set; } = true;
private bool IsCalcStartTime { get; set; } = false;
private int MaxFrameSize { get; set; } = 0;
public double StartTime { get; set; } = 0;
public int? Add(Frame frame, bool? isForceOrder = null)
{
var isOrder = isForceOrder == true || !frame.IsOrdered;
if (frame.Size > MaxFrameSize) MaxFrameSize = frame.Size;
if (isOrder)
{
var pos = PosFind(frame);
if(pos != null) FramesList.Insert((int)pos, frame);
return pos;
}
else
{
FramesList.Add(frame);
return FramesList.Count - 1;
}
}
public void Analyze()
{
if (IsCalcStartTime) FillFramesStartTime(StartTime);
FramesByGOP.SetFrames(FramesList);
FramesByTime.SetFrames(FramesList);
}
// todo@ caching?
public List<DataPoint> DataPointsGet(string? plotViewType, int sizeDivider = 1000/* kilo */)
{
List<DataPoint> data = [];
var startTimeOffset = IsAdjustStartTime ? StartTime : 0;
switch (plotViewType?.ToUpper() ?? "")
{
case "FRAME":
for (var idx = 0; idx < FramesList.Count; ++idx) data.AddRange(FramesList[idx].DataPointGet(startTimeOffset, idx == FramesList.Count - 1, sizeDivider));
break;
case "GOP":
FramesByGOP.Calc(startTimeOffset);
data.AddRange(FramesByGOP.DataPointsGet(sizeDivider));
break;
case "SECOND":
FramesByTime.Calc(startTimeOffset);
data.AddRange(FramesByTime.DataPointsGet(sizeDivider));
break;
}
return data;
}
public void IsAdjustStartTimeSet(bool isAdjustStartTime)
{
if (isAdjustStartTime != IsAdjustStartTime)
{
IsAdjustStartTime = isAdjustStartTime;
FramesByGOP.Clear();
FramesByTime.Clear();
}
}
public double? MaxXGet(string? plotViewType)
{
switch (plotViewType?.ToUpper() ?? "")
{
case "SECOND":
return Duration == null ? null : Math.Ceiling((double)Duration);
default:
return Duration;
}
}
public int MaxYGet(string? plotViewType, int sizeDivider = 1000/* kilo */)
{
var value = 0;
var startTimeOffset = (IsAdjustStartTime ? StartTime : 0);
switch (plotViewType?.ToUpper() ?? "")
{
case "FRAME":
value = MaxFrameSize;
break;
case "GOP":
FramesByGOP.Calc(startTimeOffset);
value = (FramesByGOP.MaxSize ?? 0) * 8 /* Byte/s => bit/s */;
break;
case "SECOND":
FramesByTime.Calc(startTimeOffset);
value = (FramesByTime.MaxSize ?? 0) * 8 /* Byte/s => bit/s */;
break;
}
return (int)double.Round(value / sizeDivider);
}
public FramesBitRates BitRatesCals()
{
if (Duration == null || Duration <= 0) return new();
var startTimeOffset = (IsAdjustStartTime ? StartTime : 0);
FramesByTime.Calc(startTimeOffset);
return new FramesBitRates
{
Avg = (FramesByTime.TotalSize == null) ? null : new BitRate((int)double.Round((double)FramesByTime.TotalSize * 8/* Byte/s => bit/s */ / (double)Duration)),
Max = (FramesByTime.MaxSize == null) ? null : new BitRate((int)FramesByTime.MaxSize * 8/* Byte/s => bit/s */),
Min = (FramesByTime.MinSize == null) ? null : new BitRate((int)FramesByTime.MinSize * 8/* Byte/s => bit/s */)
};
}
private int? PosFind(Frame frame)
{
// Searching position from the end as usually the frame that we are adding will be somewhere close to the end (but not always the last)
if (frame.StartTimeRaw == null)
{
// Frame does not have StartTime, use Pos instead to order and we will re-calculate all frames StartTime late
IsCalcStartTime = true;
for (int idx = FramesList.Count - 1; idx >= 0; --idx)
{
if (frame.Pos == FramesList[idx].Pos) return null;
if (frame.Pos > FramesList[idx].Pos) return idx + 1;
}
}
else
{
for (int idx = FramesList.Count - 1; idx >= 0; --idx)
{
if (frame.StartTime == FramesList[idx].StartTime) return null;
if (frame.StartTime > FramesList[idx].StartTime) return idx + 1;
}
}
return 0;
}
private void FillFramesStartTime(double startTime = 0)
{
foreach(var frame in FramesList)
{
if (frame.StartTimeRaw == null)
{
frame.StartTimeRaw = startTime;
startTime += frame.Duration;
}
else
{
startTime = frame.EndTime;
}
}
}
}
}