Skip to content

Commit c79356b

Browse files
committed
Refactor VMAF frame processing with improved PTS handling and frame recognition
1 parent 95edadf commit c79356b

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/vmaf.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { FastStats } from './stats'
99
const log = logger('webrtcperf:vmaf')
1010

1111
export interface IvfFrame {
12+
pts: number
13+
recognizedPts?: number
1214
index: number
1315
position: number
1416
size: number
@@ -204,7 +206,7 @@ async function parseIvf(fpath: string, runRecognizer = false) {
204206
let index = 0
205207
let position = 32
206208
let bytesRead = 0
207-
let frames = new Map<number, IvfFrame>()
209+
const frames = new Map<number, IvfFrame>()
208210
let firstTimestamp = 0
209211
let lastTimestamp = 0
210212
do {
@@ -222,7 +224,7 @@ async function parseIvf(fpath: string, runRecognizer = false) {
222224
/* log.debug(`IVF file ${fname}: pts ${pts} already present, skipping`) */
223225
skipped++
224226
} else {
225-
frames.set(pts, { index, position, size: size + 12 })
227+
frames.set(pts, { pts, index, position, size: size + 12 })
226228
index++
227229
if (!firstTimestamp) {
228230
firstTimestamp = pts / frameRate
@@ -242,19 +244,11 @@ ts: ${firstTimestamp.toFixed(2)}-${lastTimestamp.toFixed(2)} (${(lastTimestamp -
242244
if (runRecognizer) {
243245
const { frames: ptsToRecognized, participantDisplayName: name } = await recognizeFrames(fpath)
244246
participantDisplayName = name
245-
const recognizedFrames = new Map<number, IvfFrame>()
246-
//log.debug(path.basename(fpath), 'frames', [...frames.keys()])
247-
for (const [pts, frame] of frames) {
248-
let recognizedPts = ptsToRecognized.get(pts)
247+
for (const [pts, frame] of frames.entries()) {
248+
const recognizedPts = ptsToRecognized.get(pts)
249249
if (!recognizedPts) continue
250-
while (recognizedFrames.has(recognizedPts)) {
251-
recognizedPts += 1
252-
}
253-
recognizedFrames.set(recognizedPts, frame)
250+
frame.recognizedPts = recognizedPts
254251
}
255-
//log.debug(path.basename(fpath), 'recognizedFrames', [...recognizedFrames.keys()])
256-
frames.clear()
257-
frames = recognizedFrames
258252
}
259253

260254
return {
@@ -299,15 +293,21 @@ export async function fixIvfFrames(filePath: string, keepSourceFile = true) {
299293
let writtenFrames = 0
300294

301295
const ptsIndex = Array.from(frames.keys()).sort((a, b) => a - b)
296+
const writtenPts = new Set<number>()
302297
for (const pts of ptsIndex) {
303298
const frame = frames.get(pts)
304-
if (!frame) {
299+
if (!frame || !frame.recognizedPts) {
305300
log.warn(`fixIvfFrames ${fname}: pts ${pts} not found, skipping`)
306301
continue
307302
}
303+
let recognizedPts = frame.recognizedPts
304+
while (writtenPts.has(recognizedPts)) {
305+
recognizedPts += 1
306+
}
307+
writtenPts.add(recognizedPts)
308308
const frameView = new DataView(new ArrayBuffer(frame.size))
309309
await fd.read(frameView, 0, frame.size, frame.position)
310-
frameView.setBigUint64(4, BigInt(pts), true)
310+
frameView.setBigUint64(4, BigInt(recognizedPts), true)
311311
await fixedFd.write(new Uint8Array(frameView.buffer), 0, frameView.byteLength, position)
312312
position += frameView.byteLength
313313
writtenFrames++
@@ -716,9 +716,11 @@ if (require.main === module) {
716716
case 'convert':
717717
await convertToIvf(process.argv[3], process.argv[4], false)
718718
break
719-
case 'parse':
720-
await parseIvf(process.argv[3], true)
719+
case 'parse': {
720+
const { frames } = await parseIvf(process.argv[3], true)
721+
console.log(frames)
721722
break
723+
}
722724
case 'fix':
723725
await fixIvfFrames(process.argv[3], true)
724726
break

0 commit comments

Comments
 (0)