| 1 | import subprocess, time, json, os, sys, re |
| 2 | import numpy as np |
| 3 | S=os.path.dirname(os.path.abspath(__file__)) |
| 4 | man=open('REPO/fixtures/real-library-manifest.tsv').read().splitlines() |
| 5 | src=None |
| 6 | for l in man[1:]: |
| 7 | f=l.split('\t') |
| 8 | if len(f)>4 and f[2]=='av1' and f[3]=='1920' and os.path.exists(f[0]): src=f[0]; break |
| 9 | assert src, 'no film A' |
| 10 | y4m=os.path.join(S,'filmA.y4m') |
| 11 | if not os.path.exists(y4m): |
| 12 | subprocess.run(['ffmpeg','-y','-hide_banner','-loglevel','error','-ss','00:35:00','-i',src, |
| 13 | '-frames:v','12','-vf','crop=1920:768','-pix_fmt','yuv420p','-r','24',y4m],check=True) |
| 14 | def run(cfg,q,out): |
| 15 | a=['ffmpeg','-y','-hide_banner','-loglevel','error','-r','24','-i',y4m,'-threads','1']+cfg(q)+['-f','ivf',out] |
| 16 | t=time.time(); subprocess.run(a,check=True); return time.time()-t |
| 17 | def psnr(out): |
| 18 | p=subprocess.run(['ffmpeg','-hide_banner','-r','24','-i',out,'-r','24','-i',y4m,'-lavfi','psnr','-f','null','-'], |
| 19 | capture_output=True,text=True) |
| 20 | m=re.search(r'average:([0-9.]+)',p.stderr) or re.search(r'psnr_avg:([0-9.]+)',p.stderr) |
| 21 | y=re.search(r' y:([0-9.]+)',p.stderr) |
| 22 | return float(y.group(1)) if y else float(m.group(1)) |
| 23 | def rav1e(sp): return lambda q:['-c:v','librav1e','-rav1e-params',f'speed={sp}:quantizer={q}:tile_cols=1:tile_rows=1:threads=1'] |
| 24 | def aom(cu): return lambda q:['-c:v','libaom-av1','-cpu-used',str(cu),'-b:v','0','-crf',str(q),'-row-mt','0','-tiles','1x1'] |
| 25 | def svt(pr): return lambda q:['-c:v','libsvtav1','-preset',str(pr),'-crf',str(q),'-svtav1-params','lp=1'] |
| 26 | configs=[('rav1e speed 6',rav1e(6),[50,100,150,200]),('rav1e speed 8',rav1e(8),[50,100,150,200]), |
| 27 | ('rav1e speed 10',rav1e(10),[50,100,150,200]), |
| 28 | ('libaom cpu-used 6',aom(6),[5,20,35,45]),('libaom cpu-used 8',aom(8),[5,20,35,45]), |
| 29 | ('libaom cpu-used 9',aom(9),[5,20,35,45]),('libaom cpu-used 10',aom(10),[5,20,35,45]), |
| 30 | ('svt-av1 preset 8',svt(8),[20,35,45,55]),('svt-av1 preset 10',svt(10),[20,35,45,55]), |
| 31 | ('svt-av1 preset 12',svt(12),[20,35,45,55])] |
| 32 | res={} |
| 33 | for name,cfg,qs in configs: |
| 34 | pts=[]; wall=0.0 |
| 35 | for q in qs: |
| 36 | out=os.path.join(S,'t.ivf') |
| 37 | try: |
| 38 | w=run(cfg,q,out) |
| 39 | except Exception as e: |
| 40 | print(f'{name}: FAILED {e}',flush=True); pts=None; break |
| 41 | wall+=w; b=os.path.getsize(out); p=psnr(out); pts.append((b,p)) |
| 42 | print(f' {name} q={q}: {b} B, {p:.2f} dB, {w:.1f}s',flush=True) |
| 43 | if pts: res[name]=(pts,wall) |
| 44 | def bd(a,b): |
| 45 | |
| 46 | ra=np.log10([p[0] for p in a]); pa=[p[1] for p in a] |
| 47 | rb=np.log10([p[0] for p in b]); pb=[p[1] for p in b] |
| 48 | pa,ra=zip(*sorted(zip(pa,ra))); pb,rb=zip(*sorted(zip(pb,rb))) |
| 49 | ca=np.polyfit(pa,ra,3); cb=np.polyfit(pb,rb,3) |
| 50 | lo=max(min(pa),min(pb)); hi=min(max(pa),max(pb)) |
| 51 | ia=np.polyval(np.polyint(ca),hi)-np.polyval(np.polyint(ca),lo) |
| 52 | ib=np.polyval(np.polyint(cb),hi)-np.polyval(np.polyint(cb),lo) |
| 53 | return (10**((ib-ia)/(hi-lo))-1)*100 |
| 54 | anchor=res['rav1e speed 6'][0] |
| 55 | print('\n| encoder preset | 4-point ladder (B / dB) | BD-rate vs rav1e speed 6 | wall, 4 points (s) | fps |') |
| 56 | print('|---|---|---|---|---|') |
| 57 | for name,(pts,wall) in res.items(): |
| 58 | lad=', '.join(f'{b}/{p:.2f}' for b,p in pts) |
| 59 | print(f'| {name} | {lad} | {bd(anchor,pts):+.1f}% | {wall:.1f} | {4*12/wall:.2f} |') |