"""本主题专属离线算法，仅读取同目录合成JSON，不联网、不控制设备。"""
import csv, json, math, re, struct
from pathlib import Path
from fractions import Fraction
BASE=Path(__file__).resolve().parent
data=json.loads((BASE/'input.json').read_text(encoding='utf-8'))['data']
previous=None;rows=[];generation=0
for i,f in enumerate(data['frames']):
 key=(f['w'],f['h'],f['fourcc'],f['stride'],f['field']);changed=key!=previous
 if changed:generation+=1
 rows.append({'frame':i+1,'format_key':key,'rebuild_required':changed,'generation':generation,'buffer_bytes':f['stride']*f['h']})
 previous=key
assert [r['frame'] for r in rows if r['rebuild_required']]==[1,3,5]
result={'frames':rows,'actual_GPU_resources_created':False}
(BASE/'result.json').write_text(json.dumps(result,ensure_ascii=False,indent=2)+'\n',encoding='utf-8')
print(json.dumps(result,ensure_ascii=False))
