File size: 88,410 Bytes
5024339
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
#!/usr/bin/env python3
"""
LiveWorld 数据处理主流程

整体流程:
1. 收集源视频 -> 随机采样clip (指定帧数/fps/分辨率)
2. Qwen VL: 识别视频中的前景物体 (如 person, car)
3. SAM3: 根据文字描述分割前景mask
4. Stream3R: 估计深度、相机位姿、内参
5. Sample Building: 构建训练样本 (点云投影、参考帧选择等)

配置在 configs/data_preparation.yaml 中修改
"""
from __future__ import annotations

import os
os.environ.setdefault("SAM3_TQDM_DISABLE", "1")
os.environ.setdefault("LOG_LEVEL", "WARNING")

from omegaconf import OmegaConf
from pathlib import Path

_PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent
CONFIG = OmegaConf.load(str(_PROJECT_ROOT / "configs" / "data_preparation.yaml"))

import hashlib
import json
import logging
import os
import random
import shutil
import subprocess
from pathlib import Path
from typing import Optional, Tuple

from tqdm import tqdm

import cv2
import numpy as np
import torch
import gc
import traceback
import datetime

for _logger_name in ["sam3", "sam3_video_predictor", "sam3.model_builder", "sam3.video_predictor"]:
    logging.getLogger(_logger_name).setLevel(logging.WARNING)
from scripts.create_train_data._sample_builder import save_video, save_training_sample, save_projection_rgb_video
from scripts.create_train_data._utils import get_rank_info, shard_items
from scripts.create_train_data._sample_builder import (
    build_training_sample,
    scale_intrinsics,
    extract_foreground,
    augment_foreground,
)
from scripts.create_train_data._estimators import Stream3REstimator
from scripts.create_train_data._sample_builder import build_scene_point_cloud
from scripts.create_train_data._projection import render_projection
from scripts.create_train_data._utils import VideoGeometry
from scripts.create_train_data._entity_detector import DEFAULT_PROMPT, Qwen3VLEntityExtractor
from scripts.create_train_data._sam3_segmenter import Sam3VideoSegmenter
from scripts.create_train_data._video_io import get_video_frame_count, get_video_fps, list_video_files, load_video_frames
from scripts.create_train_data._utils import get_sample_naming


# ============================================================================
# 常量
# ============================================================================

CLIP_FILENAME = "clip.mp4"


# ============================================================================
# 工具函数
# ============================================================================

def _cleanup_memory() -> None:
    """清理GPU和CPU内存"""
    gc.collect()
    if torch.cuda.is_available():
        torch.cuda.empty_cache()
        torch.cuda.ipc_collect()


# ============================================================================
# Clip 提取函数 - 从原始视频中采样固定长度的片段
# ============================================================================

def _get_video_info(video_path: Path) -> Optional[Tuple[float, int, int, int]]:
    """获取视频基本信息: (fps, 帧数, 宽, 高)"""
    cap = cv2.VideoCapture(str(video_path))
    if not cap.isOpened():
        return None
    fps = float(cap.get(cv2.CAP_PROP_FPS) or 0.0)
    frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT) or 0)
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) or 0)
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) or 0)
    cap.release()
    if fps <= 0 or frame_count <= 0 or width <= 0 or height <= 0:
        return None
    return fps, frame_count, width, height


def _stable_rng(seed: int, video_path: Path) -> random.Random:
    """基于seed和视频路径生成确定性随机数生成器,确保同一视频每次采样结果一致"""
    key = f"{seed}:{video_path.as_posix()}".encode("utf-8")
    digest = hashlib.md5(key).digest()
    seed_int = int.from_bytes(digest[:8], "big")
    return random.Random(seed_int)


def _sample_frame_indices(
    frame_count: int,
    src_fps: float,
    target_fps: int,
    num_frames: int,
    rng: random.Random,
) -> Optional[list[int]]:
    """
    计算要提取的帧索引列表
    从源视频中按target_fps采样num_frames帧,随机选择起始时间点
    如果视频太短或fps不足则返回None
    """
    if src_fps + 1e-6 < target_fps:
        return None
    clip_seconds = num_frames / float(target_fps)
    duration = frame_count / float(src_fps)
    if duration + 1e-6 < clip_seconds:
        return None
    max_start = max(0.0, duration - clip_seconds)
    start_time = rng.uniform(0.0, max_start) if max_start > 0 else 0.0
    indices = []
    prev_idx = 0
    for i in range(num_frames):
        t = start_time + (i / float(target_fps))
        idx = int(round(t * src_fps))
        if idx < 0:
            idx = 0
        elif idx >= frame_count:
            idx = frame_count - 1
        if idx < prev_idx:
            idx = prev_idx
        indices.append(idx)
        prev_idx = idx
    return indices


def _resize_letterbox(frame: np.ndarray, target_wh: Tuple[int, int]) -> np.ndarray:
    """将帧resize到目标尺寸,保持宽高比,用黑边填充(letterbox)"""
    target_w, target_h = target_wh
    h, w = frame.shape[:2]
    if h == target_h and w == target_w:
        return frame
    scale = min(target_w / float(w), target_h / float(h))
    new_w = max(1, int(round(w * scale)))
    new_h = max(1, int(round(h * scale)))
    interp = cv2.INTER_AREA if scale < 1.0 else cv2.INTER_LINEAR
    resized = cv2.resize(frame, (new_w, new_h), interpolation=interp)
    canvas = np.zeros((target_h, target_w, 3), dtype=resized.dtype)
    x0 = (target_w - new_w) // 2
    y0 = (target_h - new_h) // 2
    canvas[y0 : y0 + new_h, x0 : x0 + new_w] = resized
    return canvas


def extract_clip(
    video_path: Path,
    output_path: Path,
    num_frames: int,
    target_fps: int,
    target_wh: Tuple[int, int],
    seed: int,
    extract_fps: int | None = None,
) -> Tuple[bool, str]:
    """
    从源视频中提取clip: 随机选起点 -> 按extract_fps采样 -> letterbox resize -> 以target_fps保存mp4
    extract_fps 控制从源视频抽帧的间隔(影响运动速度),target_fps 控制输出视频帧率。
    """
    if extract_fps is None:
        extract_fps = target_fps

    info = _get_video_info(video_path)
    if info is None:
        return False, "invalid video metadata"
    src_fps, frame_count, _, _ = info

    # 检查视频是否满足要求(源视频fps必须 >= 抽帧fps)
    if src_fps + 1e-6 < extract_fps:
        return False, f"video fps too low (src_fps={src_fps:.1f} < extract={extract_fps})"

    clip_seconds = num_frames / float(extract_fps)
    duration = frame_count / float(src_fps)
    if duration + 1e-6 < clip_seconds:
        return False, f"video too short ({duration:.1f}s < {clip_seconds:.1f}s needed)"

    # 使用确定性随机数计算起始时间
    rng = _stable_rng(seed, video_path)
    max_start = max(0.0, duration - clip_seconds)
    start_time = rng.uniform(0.0, max_start) if max_start > 0 else 0.0

    output_path.parent.mkdir(parents=True, exist_ok=True)

    # 临时文件路径(FFmpeg 需要先写入临时文件)
    temp_path = output_path.with_suffix(".tmp.mp4")

    target_w, target_h = target_wh

    # 构建 FFmpeg 命令
    # -ss 在 -i 前面可以利用关键帧快速 seek
    # scale + pad 实现 letterbox 效果
    # -frames:v 精确指定输出帧数
    cmd = [
        "ffmpeg", "-y",
        "-ss", f"{start_time:.6f}",
        "-i", str(video_path),
        "-vf", (
            f"fps={extract_fps},"
            f"scale={target_w}:{target_h}:force_original_aspect_ratio=decrease,"
            f"pad={target_w}:{target_h}:(ow-iw)/2:(oh-ih)/2:color=black"
        ),
        "-frames:v", str(num_frames),
        "-c:v", "libx264",
        "-preset", "fast",
        "-crf", "18",
        "-pix_fmt", "yuv420p",
        "-r", str(target_fps),  # 输出帧率
        "-an",  # 无音频
        "-hide_banner",
        "-loglevel", "error",
        str(temp_path),
    ]

    try:
        result = subprocess.run(cmd, capture_output=True, text=True, timeout=120)
        if result.returncode != 0:
            if temp_path.exists():
                temp_path.unlink()
            return False, f"ffmpeg error: {result.stderr[:200]}"

        # 验证输出帧数
        out_info = _get_video_info(temp_path)
        if out_info is None:
            if temp_path.exists():
                temp_path.unlink()
            return False, "failed to read output video"

        _, out_frames, out_w, out_h = out_info

        # 精确匹配帧数
        if out_frames != num_frames:
            if temp_path.exists():
                temp_path.unlink()
            return False, f"frame count mismatch: got {out_frames}, expected {num_frames}"

        if out_w != target_w or out_h != target_h:
            if temp_path.exists():
                temp_path.unlink()
            return False, f"size mismatch: got {out_w}x{out_h}, expected {target_w}x{target_h}"

        # 重命名临时文件为最终文件
        temp_path.rename(output_path)
        return True, "ok"

    except subprocess.TimeoutExpired:
        if temp_path.exists():
            temp_path.unlink()
        return False, "ffmpeg timeout"
    except Exception as e:
        if temp_path.exists():
            temp_path.unlink()
        return False, f"ffmpeg exception: {str(e)[:100]}"


# ============================================================================
# 辅助函数
# ============================================================================

def _get_video_hw(video_path: str | Path) -> tuple[int, int] | None:
    """获取视频的高宽 (H, W)"""
    cap = cv2.VideoCapture(str(video_path))
    if not cap.isOpened():
        return None
    h, w = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)), int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    cap.release()
    return (h, w) if h > 0 and w > 0 else None


def _load_cached_prompts(path: Path) -> list[str]:
    """从缓存的JSON文件中加载Qwen提取的prompts"""
    try:
        payload = json.loads(path.read_text(encoding="utf-8"))
        prompts = payload.get("prompts") or payload.get("entities") or []
        return [str(p).strip() for p in prompts if str(p).strip()]
    except Exception:
        return []


def resize_masks(masks: np.ndarray, target_hw: tuple[int, int]) -> np.ndarray:
    """将mask序列resize到目标尺寸"""
    H, W = target_hw
    resized = []
    for mask in masks:
        if mask.shape != (H, W):
            mask_u8 = (mask.astype(np.uint8) * 255)
            mask_u8 = cv2.resize(mask_u8, (W, H), interpolation=cv2.INTER_NEAREST)
            mask = mask_u8 > 0
        resized.append(mask)
    return np.stack(resized, axis=0)


def _compute_mask_bbox(mask: np.ndarray) -> tuple[int, int, int, int] | None:
    """Compute bbox (x0, y0, x1, y1) from a boolean mask."""
    ys, xs = np.where(mask)
    if ys.size == 0 or xs.size == 0:
        return None
    y0, y1 = int(ys.min()), int(ys.max()) + 1
    x0, x1 = int(xs.min()), int(xs.max()) + 1
    return x0, y0, x1, y1


def _render_instance_on_canvas(
    frame: np.ndarray,
    mask: np.ndarray,
    canvas_hw: tuple[int, int],
) -> np.ndarray | None:
    """Crop instance by mask, scale to fit canvas, and center on black background."""
    bbox = _compute_mask_bbox(mask)
    if bbox is None:
        return None
    x0, y0, x1, y1 = bbox
    if x1 <= x0 or y1 <= y0:
        return None

    crop = frame[y0:y1, x0:x1].copy()
    crop_mask = mask[y0:y1, x0:x1]
    if crop_mask.size == 0:
        return None

    crop[~crop_mask] = 0

    canvas_h, canvas_w = canvas_hw
    box_h, box_w = crop.shape[:2]
    if box_h <= 0 or box_w <= 0:
        return None

    scale = min(canvas_w / float(box_w), canvas_h / float(box_h))
    new_w = max(1, int(round(box_w * scale)))
    new_h = max(1, int(round(box_h * scale)))

    resized = cv2.resize(crop, (new_w, new_h), interpolation=cv2.INTER_LINEAR)
    resized_mask = cv2.resize(
        crop_mask.astype(np.uint8), (new_w, new_h), interpolation=cv2.INTER_NEAREST
    ) > 0
    resized[~resized_mask] = 0

    canvas = np.zeros((canvas_h, canvas_w, 3), dtype=resized.dtype)
    x_off = (canvas_w - new_w) // 2
    y_off = (canvas_h - new_h) // 2
    canvas[y_off : y_off + new_h, x_off : x_off + new_w] = resized
    return canvas


def check_dynamic_mask_quality(dynamic_masks: np.ndarray, threshold: float = 0.95) -> tuple[bool, float]:
    """检查mask质量: 如果某帧mask覆盖超过95%则认为无效(可能是全屏遮挡)"""
    if dynamic_masks is None or dynamic_masks.size == 0:
        return True, 0.0
    max_white = max(np.mean(m.astype(np.float32)) for m in dynamic_masks)
    return max_white < threshold, max_white


def is_mask_all_black(mask_path: Path) -> bool:
    """检查mask mp4文件是否全黑(没有任何分割结果)"""
    if not mask_path.exists():
        return False
    try:
        # 支持传入 .npy 或 .mp4 路径,统一转成 .mp4
        mp4_path = mask_path.with_suffix(".mp4") if mask_path.suffix == ".npy" else mask_path
        masks = load_mask_from_mp4(mp4_path)
        if masks is None or masks.size == 0:
            return True
        return not masks.any()
    except Exception:
        return False


def save_per_category_masks(
    folder_path: Path,
    category_masks: dict[str, np.ndarray],
    fps: float,
) -> None:
    """保存每个类别的mask为独立的mp4文件(不再保存npy)"""

    for prompt, masks in category_masks.items():
        safe_name = prompt.replace(" ", "_").replace("/", "_")
        mask_mp4 = folder_path / f"mask_{safe_name}.mp4"

        masks_rgb = np.repeat(masks.astype(np.uint8)[..., None] * 255, 3, axis=-1)
        save_video(masks_rgb, mask_mp4, fps=fps)


def load_mask_from_mp4(mask_mp4: Path) -> np.ndarray | None:
    """从mp4文件加载mask序列,返回 (T, H, W) 的 bool 数组"""
    if not mask_mp4.exists():
        return None
    try:
        cap = cv2.VideoCapture(str(mask_mp4))
        if not cap.isOpened():
            return None
        frames = []
        while True:
            ret, frame = cap.read()
            if not ret:
                break
            # mask mp4 是灰度的(或RGB全相同),取第一个通道 > 127 作为 bool
            if frame.ndim == 3:
                frame = frame[..., 0]
            frames.append(frame > 127)
        cap.release()
        if not frames:
            return None
        return np.stack(frames, axis=0)
    except Exception:
        return None


def load_and_merge_category_masks(folder_path: Path, prompts: list[str], exclude_prompts: list[str] = None) -> np.ndarray | None:
    """
    加载并合并多个类别的mask(从mp4文件读取)。

    Args:
        folder_path: 数据文件夹路径
        prompts: 要加载的类别列表
        exclude_prompts: 要排除的类别(如 sky)

    Returns:
        合并后的mask或None
    """
    exclude_prompts = exclude_prompts or []
    merged = None

    for prompt in prompts:
        if prompt.lower() in [p.lower() for p in exclude_prompts]:
            continue
        safe_name = prompt.replace(" ", "_").replace("/", "_")
        mask_mp4 = folder_path / f"mask_{safe_name}.mp4"
        masks = load_mask_from_mp4(mask_mp4)
        if masks is None or masks.size == 0:
            continue
        if merged is None:
            merged = masks.astype(bool)
        else:
            merged = merged | masks.astype(bool)

    return merged


def _has_foreground_mask_files(folder_path: Path) -> bool:
    """Check if any non-sky per-category mask mp4 exists (used to infer fg availability)."""
    for mask_path in folder_path.glob("mask_*.mp4"):
        if mask_path.name == "mask_sky.mp4":
            continue
        return True
    return False


def _normalize_output_size(
    output_size: object,
    geometry: VideoGeometry,
    cfg,
) -> tuple[int, int]:
    """Normalize output size (H, W) with safe fallbacks."""
    if isinstance(output_size, (list, tuple)) and len(output_size) == 2:
        try:
            out_h, out_w = int(output_size[0]), int(output_size[1])
            if out_h > 0 and out_w > 0:
                return out_h, out_w
        except Exception:
            pass
    if geometry.original_size is not None:
        return int(geometry.original_size[0]), int(geometry.original_size[1])
    if cfg is not None:
        return int(cfg.clip_target_height), int(cfg.clip_target_width)
    H, W = geometry.frames.shape[1:3]
    return H, W


def _get_projection_rgb_slice(channels: list[str]) -> Optional[slice]:
    """Find RGB slice in projection channel layout."""
    cursor = 0
    rgb_slice = None
    for name in channels:
        if name == "rgb":
            rgb_slice = slice(cursor, cursor + 3)
            cursor += 3
        else:
            cursor += 1
    return rgb_slice


def _resolve_projection_aug(sample_meta: dict, sample_config) -> Optional[dict]:
    """Resolve projection augmentation params for rerendering."""
    def _defaults() -> dict:
        return {
            "apply_prob": getattr(sample_config, "projection_aug_apply_prob", 1.0),
            "pixel_drop_min": getattr(sample_config, "projection_aug_pixel_drop_min", 0.0),
            "pixel_drop_max": getattr(sample_config, "projection_aug_pixel_drop_max", 0.0),
            "block_drop_min": getattr(sample_config, "projection_aug_block_drop_min", 0.0),
            "block_drop_max": getattr(sample_config, "projection_aug_block_drop_max", 0.0),
            "block_size_min": getattr(sample_config, "projection_aug_block_size_min", 16),
            "block_size_max": getattr(sample_config, "projection_aug_block_size_max", 64),
            "erode_iters_min": getattr(sample_config, "projection_aug_erode_iters_min", 0),
            "erode_iters_max": getattr(sample_config, "projection_aug_erode_iters_max", 0),
            "blur_sigma_min": getattr(sample_config, "projection_aug_blur_sigma_min", 0.0),
            "blur_sigma_max": getattr(sample_config, "projection_aug_blur_sigma_max", 0.0),
        }

    if "projection_aug" in sample_meta:
        meta = sample_meta.get("projection_aug")
        if not isinstance(meta, dict):
            return None
        params = _defaults()
        for key in params:
            if key in meta:
                params[key] = meta[key]
        params["seed"] = meta.get("seed")
        return params

    if not getattr(sample_config, "projection_aug_enable", False):
        return None
    params = _defaults()
    params["seed"] = None
    return params


def _rerender_projection_videos(
    folder_path: Path,
    sample_meta: dict,
    geometry: VideoGeometry,
    frames: np.ndarray,
    dynamic_masks_proc: Optional[np.ndarray],
    foreground_masks_proc: Optional[np.ndarray],
    cfg,
    sample_config,
    fps: float,
    prefix: str,
) -> str:
    output_size = _normalize_output_size(sample_meta.get("output_size"), geometry, cfg)
    proj_h, proj_w = output_size
    H, W = geometry.frames.shape[1:3]
    if H <= 0 or W <= 0:
        return "invalid_geometry_size"
    scale_x = proj_w / W
    scale_y = proj_h / H

    geometry.depths



    scene_idx = sample_meta.get("scene_idx")
    if scene_idx is None:
        return "missing_scene_idx"
    scene_idx = int(scene_idx)
    if scene_idx < 0 or scene_idx >= geometry.depths.shape[0]:
        return "scene_idx_oob"

    projection_channels = list(sample_meta.get("projection_channels") or cfg.projection_channels)
    rgb_slice = _get_projection_rgb_slice(projection_channels)
    if rgb_slice is None:
        return "no_rgb_channel"

    naming_style = sample_meta.get("naming") or cfg.naming_style
    names = get_sample_naming(naming_style)

    scene_depth = geometry.depths[scene_idx]
    scene_K = geometry.intrinsics[scene_idx]
    scene_rgb = geometry.frames[scene_idx]
    scene_valid_mask = None if geometry.masks is None else geometry.masks[scene_idx]
    scene_dynamic_mask = None if dynamic_masks_proc is None else dynamic_masks_proc[scene_idx]

    if sample_config.upsample_geometry_to_output and output_size is not None:
        scene_depth = cv2.resize(scene_depth, (proj_w, proj_h), interpolation=cv2.INTER_NEAREST)
        scene_K = scale_intrinsics(scene_K, scale_x, scale_y)
        if frames is not None:
            scene_rgb = np.asarray(frames)[scene_idx]
        if scene_rgb.shape[:2] != (proj_h, proj_w):
            scene_rgb = cv2.resize(scene_rgb, (proj_w, proj_h), interpolation=cv2.INTER_LINEAR)
        if scene_valid_mask is not None and scene_valid_mask.shape != (proj_h, proj_w):
            scene_valid_mask = cv2.resize(
                scene_valid_mask.astype(np.uint8),
                (proj_w, proj_h),
                interpolation=cv2.INTER_NEAREST,
            ).astype(bool)
        if scene_dynamic_mask is not None and scene_dynamic_mask.shape != (proj_h, proj_w):
            scene_dynamic_mask = cv2.resize(
                scene_dynamic_mask.astype(np.uint8),
                (proj_w, proj_h),
                interpolation=cv2.INTER_NEAREST,
            ).astype(bool)

    scene_xyz, scene_rgb_pts = build_scene_point_cloud(
        depth=scene_depth,
        K=scene_K,
        c2w=geometry.poses_c2w[scene_idx],
        rgb=scene_rgb,
        valid_mask=scene_valid_mask,
        dynamic_mask=scene_dynamic_mask,
        voxel_size=sample_config.scene_voxel_size,
    )

    if scene_xyz.size == 0:
        return "empty_scene"
    if "rgb" in projection_channels and scene_rgb_pts is None:
        return "missing_scene_rgb"

    # Get indices (support both old P_idx and new P9_idx/P1_idx)
    preceding_idx_9 = sample_meta.get("P9_idx", sample_meta.get("P_idx", []))
    preceding_idx_1 = sample_meta.get("P1_idx", [])
    target_idx = sample_meta.get("T_idx", [])
    if not preceding_idx_9 or not target_idx:
        return "missing_indices"

    # P9 scene projection (9帧)
    # NOTE: Hole filling is disabled to preserve geometry.
    preceding_proj_9 = []
    for idx in preceding_idx_9:
        idx = int(idx)
        if idx < 0 or idx >= geometry.intrinsics.shape[0]:
            return "P9_idx_oob"
        K_scaled = scale_intrinsics(geometry.intrinsics[idx], scale_x, scale_y)
        proj = render_projection(
            scene_xyz,
            K_scaled,
            geometry.poses_c2w[idx],
            (proj_h, proj_w),
            projection_channels,
            colors=scene_rgb_pts,
            fill_holes_kernel=0,
        )
        preceding_proj_9.append(proj)
    preceding_proj_9 = np.stack(preceding_proj_9, axis=0).astype(np.float32)

    # P1 scene projection (1帧)
    if preceding_idx_1:
        preceding_proj_1 = []
        for idx in preceding_idx_1:
            idx = int(idx)
            if idx < 0 or idx >= geometry.intrinsics.shape[0]:
                return "P1_idx_oob"
            K_scaled = scale_intrinsics(geometry.intrinsics[idx], scale_x, scale_y)
            proj = render_projection(
                scene_xyz,
                K_scaled,
                geometry.poses_c2w[idx],
                (proj_h, proj_w),
                projection_channels,
                colors=scene_rgb_pts,
                fill_holes_kernel=0,
            )
            preceding_proj_1.append(proj)
        preceding_proj_1 = np.stack(preceding_proj_1, axis=0).astype(np.float32)
    else:
        # Fallback: use last frame of P9
        preceding_proj_1 = preceding_proj_9[-1:, ...]

    # Target scene projection
    target_proj = []
    for idx in target_idx:
        idx = int(idx)
        if idx < 0 or idx >= geometry.intrinsics.shape[0]:
            return "T_idx_oob"
        K_scaled = scale_intrinsics(geometry.intrinsics[idx], scale_x, scale_y)
        proj = render_projection(
            scene_xyz,
            K_scaled,
            geometry.poses_c2w[idx],
            (proj_h, proj_w),
            projection_channels,
            colors=scene_rgb_pts,
            fill_holes_kernel=0,
        )
        target_proj.append(proj)
    target_proj = np.stack(target_proj, axis=0).astype(np.float32)

    save_projection_rgb_video(
        preceding_proj_9[..., rgb_slice],
        folder_path / f"{prefix}{names.preceding_scene_proj_rgb_9}.mp4",
        fps=fps,
    )
    save_projection_rgb_video(
        preceding_proj_1[..., rgb_slice],
        folder_path / f"{prefix}{names.preceding_scene_proj_rgb_1}.mp4",
        fps=fps,
    )
    save_projection_rgb_video(
        target_proj[..., rgb_slice],
        folder_path / f"{prefix}{names.target_scene_proj_rgb}.mp4",
        fps=fps,
    )

    # Foreground projections (augmented foreground on black)
    preceding_idx_9_int = [int(i) for i in preceding_idx_9]
    target_idx_int = [int(i) for i in target_idx]
    if preceding_idx_1:
        preceding_idx_1_int = [int(i) for i in preceding_idx_1]
    else:
        preceding_idx_1_int = [preceding_idx_9_int[-1]]

    proj_P9_fg = np.zeros((len(preceding_idx_9_int), proj_h, proj_w, 3), dtype=np.uint8)
    proj_P1_fg = np.zeros((len(preceding_idx_1_int), proj_h, proj_w, 3), dtype=np.uint8)
    proj_T_fg = np.zeros((len(target_idx_int), proj_h, proj_w, 3), dtype=np.uint8)

    if frames is not None and foreground_masks_proc is not None and foreground_masks_proc.size > 0:
        frames_np = np.asarray(frames)

        def _resize_rgb_stack(stack: np.ndarray) -> np.ndarray:
            if stack.shape[1:3] == (proj_h, proj_w):
                return stack
            resized = [
                cv2.resize(frame, (proj_w, proj_h), interpolation=cv2.INTER_LINEAR)
                for frame in stack
            ]
            return np.stack(resized, axis=0)

        fg_preceding_9 = foreground_masks_proc[preceding_idx_9_int]
        fg_preceding_1 = foreground_masks_proc[preceding_idx_1_int]
        fg_target = foreground_masks_proc[target_idx_int]
        fg_preceding_9 = resize_masks(fg_preceding_9, (proj_h, proj_w))
        fg_preceding_1 = resize_masks(fg_preceding_1, (proj_h, proj_w))
        fg_target = resize_masks(fg_target, (proj_h, proj_w))

        preceding_rgb_9 = _resize_rgb_stack(frames_np[preceding_idx_9_int])
        preceding_rgb_1 = _resize_rgb_stack(frames_np[preceding_idx_1_int])
        target_rgb = _resize_rgb_stack(frames_np[target_idx_int])

        preceding_fg_9 = extract_foreground(preceding_rgb_9, fg_preceding_9)
        preceding_fg_1 = extract_foreground(preceding_rgb_1, fg_preceding_1)
        target_fg = extract_foreground(target_rgb, fg_target)

        depth_preceding_9 = geometry.depths[preceding_idx_9_int]
        depth_preceding_1 = geometry.depths[preceding_idx_1_int]
        depth_target = geometry.depths[target_idx_int]
        depth_preceding_9 = np.stack(
            [cv2.resize(d, (proj_w, proj_h), interpolation=cv2.INTER_NEAREST) for d in depth_preceding_9]
        )
        depth_preceding_1 = np.stack(
            [cv2.resize(d, (proj_w, proj_h), interpolation=cv2.INTER_NEAREST) for d in depth_preceding_1]
        )
        depth_target = np.stack(
            [cv2.resize(d, (proj_w, proj_h), interpolation=cv2.INTER_NEAREST) for d in depth_target]
        )

        rng = np.random.default_rng(sample_config.random_seed)
        proj_P9_fg = augment_foreground(
            preceding_fg_9,
            fg_preceding_9,
            depth_preceding_9,
            dot_ratio_min=sample_config.fg_dot_ratio_min,
            dot_ratio_max=sample_config.fg_dot_ratio_max,
            rng=rng,
        ).astype(np.uint8)
        proj_P1_fg = augment_foreground(
            preceding_fg_1,
            fg_preceding_1,
            depth_preceding_1,
            dot_ratio_min=sample_config.fg_dot_ratio_min,
            dot_ratio_max=sample_config.fg_dot_ratio_max,
            rng=rng,
        ).astype(np.uint8)
        proj_T_fg = augment_foreground(
            target_fg,
            fg_target,
            depth_target,
            dot_ratio_min=sample_config.fg_dot_ratio_min,
            dot_ratio_max=sample_config.fg_dot_ratio_max,
            rng=rng,
        ).astype(np.uint8)

    save_projection_rgb_video(
        proj_P9_fg,
        folder_path / f"{prefix}{names.preceding_proj_fg_rgb_9}.mp4",
        fps=fps,
    )
    save_projection_rgb_video(
        proj_P1_fg,
        folder_path / f"{prefix}{names.preceding_proj_fg_rgb_1}.mp4",
        fps=fps,
    )
    save_projection_rgb_video(
        proj_T_fg,
        folder_path / f"{prefix}{names.target_proj_fg_rgb}.mp4",
        fps=fps,
    )
    return "ok"


def log_failed_sample(log_path: Path, video_id: str, reason: str, rank: int) -> None:
    """记录处理失败的样本"""
    ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    with open(log_path, "a", encoding="utf-8") as f:
        f.write(f"[{ts}][R{rank}] {video_id}: {reason}\n")


def cleanup_failed_output(output_dir: Path) -> None:
    """清理失败的输出目录"""
    if output_dir.exists():
        shutil.rmtree(output_dir, ignore_errors=True)


def mark_as_failed(output_dir: Path, reason: str) -> None:
    """创建.skip标记文件,后续运行时跳过此文件夹"""
    output_dir.mkdir(parents=True, exist_ok=True)
    skip_file = output_dir / ".skip"
    ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    skip_file.write_text(f"[{ts}] {reason}\n", encoding="utf-8")


def is_marked_failed(output_dir: Path) -> tuple[bool, str]:
    """检查文件夹是否已被标记为失败"""
    skip_file = output_dir / ".skip"
    if skip_file.exists():
        try:
            reason = skip_file.read_text(encoding="utf-8").strip()
            return True, reason
        except Exception:
            return True, "unknown"
    return False, ""


# ============================================================================
# 背景光流运动检测 (GPU)
# ============================================================================

def _resize_short_side(img: np.ndarray, short_side: int) -> np.ndarray:
    """Resize image so its short side equals short_side."""
    h, w = img.shape[:2]
    if min(h, w) == short_side:
        return img
    scale = short_side / float(min(h, w))
    new_w, new_h = int(round(w * scale)), int(round(h * scale))
    return cv2.resize(img, (new_w, new_h), interpolation=cv2.INTER_AREA)


def check_bg_motion(
    clip_path: Path,
    masks: np.ndarray | None,
    cfg,
    device: str,
) -> tuple[bool, str]:
    """
    用 GPU 光流检测背景是否有足够的 camera motion。

    流程:
    1. 均匀采样 N 对相邻帧
    2. 灰度化 + resize 到 short_side
    3. 前景 mask erode 后扩展为 "不可用区域"
    4. 用 cv2.calcOpticalFlowFarneback 计算光流 (CPU, 但在 resize 后的小图上很快)
    5. 只在可见背景像素上统计光流幅度中位数
    6. 跳过背景面积太小的帧对
    7. 如果足够多帧对的背景运动都 < threshold → 判定静止

    Returns: (is_ok, reason_str)
        is_ok=True: 背景有运动,通过
        is_ok=False: 背景静止,应该拒绝
    """
    num_samples = cfg.bg_motion_num_samples
    short_side = cfg.bg_motion_short_side
    erode_px = cfg.bg_motion_mask_erode
    min_bg_ratio = cfg.bg_motion_min_bg_ratio
    threshold = cfg.bg_motion_threshold

    # 读取视频帧
    cap = cv2.VideoCapture(str(clip_path))
    if not cap.isOpened():
        return True, "cannot_open"
    frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    if frame_count < 2:
        cap.release()
        return True, "too_few_frames"

    # 均匀采样帧索引对
    indices = np.linspace(0, frame_count - 1, num=min(num_samples + 1, frame_count), dtype=int)
    pairs = [(indices[i], indices[i + 1]) for i in range(len(indices) - 1) if indices[i] != indices[i + 1]]

    # 腐蚀 kernel
    erode_kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (erode_px * 2 + 1, erode_px * 2 + 1)) if erode_px > 0 else None

    flow_magnitudes: list[float] = []
    skipped = 0

    for idx_a, idx_b in pairs:
        # 读取两帧
        cap.set(cv2.CAP_PROP_POS_FRAMES, int(idx_a))
        ok_a, frame_a = cap.read()
        cap.set(cv2.CAP_PROP_POS_FRAMES, int(idx_b))
        ok_b, frame_b = cap.read()
        if not ok_a or not ok_b or frame_a is None or frame_b is None:
            skipped += 1
            continue

        # 灰度 + resize
        gray_a = _resize_short_side(cv2.cvtColor(frame_a, cv2.COLOR_BGR2GRAY), short_side)
        gray_b = _resize_short_side(cv2.cvtColor(frame_b, cv2.COLOR_BGR2GRAY), short_side)
        h, w = gray_a.shape[:2]

        # 构建背景 mask (True = 可用背景)
        if masks is not None and len(masks) > 0:
            # 取两帧的前景 mask 的并集,然后 erode 扩展不可用区域
            mask_a = masks[min(idx_a, len(masks) - 1)]
            mask_b = masks[min(idx_b, len(masks) - 1)]
            fg_union = (mask_a | mask_b).astype(np.uint8) * 255
            fg_resized = cv2.resize(fg_union, (w, h), interpolation=cv2.INTER_NEAREST)
            if erode_kernel is not None:
                # dilate foreground = erode background
                fg_resized = cv2.dilate(fg_resized, erode_kernel)
            bg_mask = fg_resized < 128  # True = background
        else:
            bg_mask = np.ones((h, w), dtype=bool)

        bg_ratio = bg_mask.sum() / float(h * w)
        if bg_ratio < min_bg_ratio:
            skipped += 1
            continue

        # 计算光流 (Farneback, CPU, 小图很快)
        flow = cv2.calcOpticalFlowFarneback(
            gray_a, gray_b, None,
            pyr_scale=0.5, levels=3, winsize=15,
            iterations=3, poly_n=5, poly_sigma=1.2, flags=0,
        )
        # flow shape: (h, w, 2)
        mag = np.sqrt(flow[..., 0] ** 2 + flow[..., 1] ** 2)

        # 只统计背景像素的光流
        bg_mag = mag[bg_mask]
        if len(bg_mag) == 0:
            skipped += 1
            continue

        median_mag = float(np.median(bg_mag))
        flow_magnitudes.append(median_mag)

    cap.release()

    if not flow_magnitudes:
        return True, "no_valid_pairs"

    overall_median = float(np.median(flow_magnitudes))

    # 静止检测
    if overall_median < threshold:
        return False, f"static_bg(flow={overall_median:.2f}<{threshold}, pairs={len(flow_magnitudes)}, skip={skipped})"

    # 转场检测: 背景光流突变 (max/median 比值过大说明某帧对发生了场景切换)
    scene_cut_ratio = cfg.bg_motion_scene_cut_ratio
    if scene_cut_ratio > 0 and len(flow_magnitudes) >= 3:
        max_flow = max(flow_magnitudes)
        if overall_median > 1e-6 and max_flow / overall_median > scene_cut_ratio:
            return False, f"scene_cut(max/med={max_flow/overall_median:.1f}>{scene_cut_ratio}, max={max_flow:.2f}, med={overall_median:.2f})"

    return True, f"ok(flow={overall_median:.2f}, pairs={len(flow_magnitudes)}, skip={skipped})"


# ============================================================================
# 日志函数
# ============================================================================

def log_stage_header(stage_name: str, rank: int) -> None:
    """打印阶段开始的header"""
    ts = datetime.datetime.now().strftime("%H:%M:%S")
    print(f"\n{'=' * 60}", flush=True)
    print(f"[{ts}][R{rank}] STAGE: {stage_name}", flush=True)
    print(f"{'=' * 60}", flush=True)


def log_stage_progress(stage: str, idx: int, total: int, video_id: str, rank: int, status: str = "", skipped: int = 0) -> None:
    ts = datetime.datetime.now().strftime("%H:%M:%S")
    skip_info = f" (skipped={skipped})" if skipped > 0 else ""
    status_str = f" [{status}]" if status else ""
    print(f"[{ts}][R{rank}][{stage}] {idx}/{total}{skip_info} {video_id}{status_str}", flush=True)


def log_stage_summary(stage: str, rank: int, processed: int, skipped: int, failed: int = 0) -> None:
    ts = datetime.datetime.now().strftime("%H:%M:%S")
    parts = [f"processed={processed}", f"skipped={skipped}"]
    if failed > 0:
        parts.append(f"failed={failed}")
    print(f"[{ts}][R{rank}][{stage}] DONE: {', '.join(parts)}", flush=True)


def log_message(msg: str, rank: int, stage: str = None, only_rank0: bool = False) -> None:
    if only_rank0 and rank != 0:
        return
    ts = datetime.datetime.now().strftime("%H:%M:%S")
    tag = f"[{ts}][R{rank}]"
    if stage:
        tag += f"[{stage}]"
    print(f"{tag} {msg}", flush=True)


# ============================================================================
# 视频收集
# ============================================================================

def collect_all_source_videos(video_dirs: list[str], rank: int,
                              video_dirs_ratio: list[int] | None = None) -> list[Path]:
    """从配置的视频目录中收集所有源视频路径,按 ratio 比例采样。

    ratio 控制每个目录在最终池中的相对份额。例如 ratio=[1,2] 表示
    第二个目录的视频列表会被重复2次放入池中。下游 shuffle 和消费逻辑不变。
    """
    per_dir_videos: list[list[Path]] = []
    for vdir in video_dirs:
        vdir_path = Path(vdir)
        if not vdir_path.exists():
            log_message(f"Warning: video_dir not found: {vdir}", rank, "Init")
            per_dir_videos.append([])
            continue
        videos = sorted(list_video_files(vdir))
        log_message(f"Found {len(videos)} videos in {vdir}", rank, "Init", only_rank0=True)
        per_dir_videos.append(videos)

    # 没有 ratio 或长度不匹配时退回等权
    if video_dirs_ratio is None or len(video_dirs_ratio) != len(video_dirs):
        all_videos = []
        for videos in per_dir_videos:
            all_videos.extend(videos)
        return sorted(all_videos)

    # 按比例重复每个目录的视频列表
    all_videos = []
    for videos, r in zip(per_dir_videos, video_dirs_ratio):
        for _ in range(r):
            all_videos.extend(videos)
    log_message(
        f"Video pool after ratio weighting: {len(all_videos)} "
        f"(ratio={video_dirs_ratio})", rank, "Init", only_rank0=True,
    )
    return sorted(all_videos)


def prepare_folder_ids(max_videos: int) -> list[str]:
    """生成输出文件夹ID列表 (00000000, 00000001, ...)"""
    return [f"{idx:08d}" for idx in range(max_videos)]


def shuffle_videos(all_videos: list[Path], shuffle_seed: int | None) -> list[Path]:
    """使用固定seed打乱视频顺序"""
    if shuffle_seed is not None:
        rng = random.Random(shuffle_seed)
        all_videos = list(all_videos)
        rng.shuffle(all_videos)
    return all_videos


# ============================================================================
# 完成状态检查 - 用于增量处理和断点续传
# ============================================================================

def _check_clip_complete(folder_path: Path, cfg) -> bool:
    """检查clip.mp4是否存在且符合配置要求"""
    clip_path = folder_path / CLIP_FILENAME
    if not clip_path.exists():
        return False
    try:
        info = _get_video_info(clip_path)
        if info is None:
            return False
        _, frame_count, w, h = info
        return (frame_count == cfg.clip_num_frames and
                w == cfg.clip_target_width and
                h == cfg.clip_target_height)
    except Exception:
        return False


def _check_stage3_geometry_complete(video_out: Path) -> bool:
    """检查geometry.npz是否已生成"""
    return (video_out / "geometry.npz").exists()


def _check_folder_complete(folder_path: Path, cfg) -> tuple[bool, str]:
    """检查文件夹是否完整 (clip + geometry + samples都存在)"""
    # Check if marked as failed - this is an error, needs re-sample
    is_failed, fail_reason = is_marked_failed(folder_path)
    if is_failed:
        return False, f"error:{fail_reason}"

    # Check clip
    if not _check_clip_complete(folder_path, cfg):
        return False, "interrupted:clip_incomplete"

    # Check geometry
    if not _check_stage3_geometry_complete(folder_path):
        return False, "interrupted:geometry_incomplete"

    # Check samples
    if not _check_stage3_samples_complete(folder_path, cfg):
        return False, "interrupted:samples_incomplete"

    return True, "ok"


def cleanup_error_folder(folder_path: Path, rank: int) -> None:
    """清理错误文件夹,下次迭代时会重新采样新视频"""
    if folder_path.exists():
        shutil.rmtree(folder_path, ignore_errors=True)
        log_message(f"Cleaned up error folder (will re-sample): {folder_path.name}", rank, "Cleanup")


def _check_stage3_samples_complete(video_out: Path, cfg, projection_only: bool = False) -> bool:
    """检查训练样本是否完整生成"""
    names = get_sample_naming(cfg.naming_style)
    prefixes = ["train_"] if cfg.num_samples == 1 else [f"train_sample{i:03d}_" for i in range(cfg.num_samples)]
    has_rgb_proj = "rgb" in cfg.projection_channels

    for prefix in prefixes:
        if projection_only:
            # projection_only 模式只检查 scene projection 和 fg projection 文件
            if has_rgb_proj:
                required = [
                    video_out / f"{prefix}{names.preceding_scene_proj_rgb_9}.mp4",
                    video_out / f"{prefix}{names.preceding_scene_proj_rgb_1}.mp4",
                    video_out / f"{prefix}{names.target_scene_proj_rgb}.mp4",
                    video_out / f"{prefix}{names.preceding_proj_fg_rgb_9}.mp4",
                    video_out / f"{prefix}{names.preceding_proj_fg_rgb_1}.mp4",
                    video_out / f"{prefix}{names.target_proj_fg_rgb}.mp4",
                ]
            else:
                required = []
        else:
            # 完整模式检查所有文件
            required = [
                video_out / f"{prefix}sample.json",
                # 两种 preceding 模式
                video_out / f"{prefix}{names.preceding_rgb_9}.mp4",
                video_out / f"{prefix}{names.preceding_rgb_1}.mp4",
                video_out / f"{prefix}{names.target_rgb}.mp4",
            ]
            if has_rgb_proj:
                required.extend([
                    video_out / f"{prefix}{names.preceding_scene_proj_rgb_9}.mp4",
                    video_out / f"{prefix}{names.preceding_scene_proj_rgb_1}.mp4",
                    video_out / f"{prefix}{names.target_scene_proj_rgb}.mp4",
                ])
            # Reference scene output is only expected when reference exists AND fg masks were produced
            reference_path = video_out / f"{prefix}{names.reference_rgb}.mp4"
            if reference_path.exists() and _has_foreground_mask_files(video_out):
                required.extend([
                    video_out / f"{prefix}{names.reference_scene_rgb}.mp4",
                ])
        if not all(p.exists() for p in required):
            return False
    return True


def _save_reference_scene(
    ref_frames: np.ndarray,
    fg_masks: np.ndarray,
    out_scene: Path,
    fps: float,
) -> None:
    reference_scene = ref_frames.copy()
    reference_scene[fg_masks] = 0
    save_video(reference_scene, out_scene, fps=fps)


def _collect_instance_only_items(
    output_root: Path,
    folder_ids: list[str],
    cfg,
) -> list[tuple[Path, str, Path]]:
    """Collect existing folders with target videos for instance-only runs."""
    names = get_sample_naming(cfg.naming_style)
    items = []
    for folder_id in folder_ids:
        folder_path = output_root / folder_id
        if not folder_path.is_dir():
            continue
        if is_marked_failed(folder_path)[0]:
            continue
        sample_json_paths = sorted(folder_path.glob("train*sample.json"))
        if not sample_json_paths:
            continue
        target_found = False
        target_path = None
        for sample_json in sample_json_paths:
            prefix = sample_json.name[: -len("sample.json")]
            candidate = folder_path / f"{prefix}{names.target_rgb}.mp4"
            if candidate.exists():
                target_found = True
                target_path = candidate
                break
        if not target_found:
            continue
        clip_path = folder_path / CLIP_FILENAME
        if not clip_path.exists() and target_path is not None:
            clip_path = target_path
        items.append((clip_path, folder_id, folder_path))
    return items


def _run_instance_reference_stage(
    cfg,
    rank: int,
    local_rank: int,
    video_items: list[tuple[Path, str, Path]],
    output_root: Path,
) -> None:
    log_stage_header("Instance Reference", rank)

    instance_segmenter = Sam3VideoSegmenter(
        gpus_to_use=[local_rank],
        propagation_direction=cfg.sam3_propagation_direction,
        score_threshold_detection=cfg.sam3_score_threshold,
        new_det_thresh=cfg.sam3_new_det_thresh,
        checkpoint_path=cfg.sam3_checkpoint_path,
        multi_prompt=cfg.sam3_multi_prompt,
        resize_input_to=cfg.sam3_resize_input_to,
    )

    inst_processed, inst_skipped, inst_failed = 0, 0, 0
    inst_failed_log = output_root / "instance_ref_failed_samples.txt"
    names = get_sample_naming(cfg.naming_style)

    for idx, (_, folder_id, folder_path) in enumerate(video_items, start=1):
        is_failed, _ = is_marked_failed(folder_path)
        if is_failed:
            inst_skipped += 1
            continue

        sample_json_paths = sorted(folder_path.glob("train*sample.json"))
        if not sample_json_paths:
            inst_skipped += 1
            log_stage_progress("InstRef", idx, len(video_items), folder_id, rank, status="skip(no-sample)", skipped=inst_skipped)
            continue

        prompts_path = folder_path / "dynamic_prompts.json"
        prompts = Sam3VideoSegmenter.normalize_prompts(
            _load_cached_prompts(prompts_path) if prompts_path.exists() else []
        )
        prompts = [p for p in prompts if p.strip().lower() != "sky"]
        if not prompts:
            inst_skipped += 1
            log_stage_progress("InstRef", idx, len(video_items), folder_id, rank, status="skip(no-prompts)", skipped=inst_skipped)
            continue

        sample_total = len(sample_json_paths)
        sample_done = 0
        sample_skipped = 0

        for sample_json in sample_json_paths:
            prefix = sample_json.name[: -len("sample.json")]
            target_path = folder_path / f"{prefix}{names.target_rgb}.mp4"
            if not target_path.exists():
                sample_skipped += 1
                continue

            meta_out = folder_path / f"{prefix}reference_instances.json"
            if cfg.skip_existing and meta_out.exists():
                sample_skipped += 1
                continue

            try:
                frames = load_video_frames(target_path)
            except Exception as exc:
                log_failed_sample(inst_failed_log, folder_id, f"{target_path.name}: {exc}", rank)
                inst_failed += 1
                continue

            fps = cfg.fps_override or get_video_fps(target_path)
            frame_count = len(frames)
            if frame_count == 0:
                sample_skipped += 1
                continue

            instance_masks = instance_segmenter.segment_instances(
                video_path=str(target_path),
                prompts=prompts,
                frame_index=cfg.sam3_frame_index,
                start_frame_index=cfg.sam3_start_frame_index,
                max_frame_num_to_track=cfg.sam3_max_frame_num_to_track,
                expected_frames=frame_count,
                frames=frames,
            )

            H, W = frames[0].shape[:2]
            total_area = float(H * W)

            ranked = []
            for obj_id, masks in instance_masks.items():
                if masks.ndim != 3 or masks.shape[0] == 0:
                    continue
                areas = masks.reshape(masks.shape[0], -1).sum(axis=1)
                max_area = float(areas.max())
                if max_area <= 0:
                    continue
                ranked.append((obj_id, max_area / total_area, areas, masks))

            ranked.sort(key=lambda x: x[1], reverse=True)
            selected = ranked[: cfg.instance_ref_max_instances]

            # Remove old instance videos for this prefix if reprocessing
            for old in folder_path.glob(f"{prefix}reference_instance_*.mp4"):
                old.unlink(missing_ok=True)

            instances_meta = []
            for rank_idx, (obj_id, max_ratio, areas, masks) in enumerate(selected):
                valid_indices = np.where(areas > 0)[0].tolist()
                if not valid_indices:
                    continue

                base_seed = cfg.instance_ref_random_seed
                if base_seed is None:
                    base_seed = cfg.shuffle_seed or 0
                rng = _stable_rng(int(base_seed), Path(f"{target_path.as_posix()}#{obj_id}"))
                k = min(cfg.instance_ref_max_frames, len(valid_indices))
                chosen = rng.sample(valid_indices, k)
                chosen = sorted(chosen)

                instance_frames = []
                for frame_idx in chosen:
                    frame = frames[frame_idx]
                    mask = masks[frame_idx]
                    canvas = _render_instance_on_canvas(frame, mask, (H, W))
                    if canvas is not None:
                        instance_frames.append(canvas)

                if not instance_frames:
                    continue

                out_path = folder_path / f"{prefix}reference_instance_{rank_idx:02d}.mp4"
                save_video(np.stack(instance_frames, axis=0), out_path, fps=fps)

                instances_meta.append(
                    {
                        "rank": rank_idx,
                        "obj_id": int(obj_id),
                        "max_area_ratio": float(max_ratio),
                        "frame_indices": chosen,
                    }
                )

            meta_payload = {
                "target_video": target_path.name,
                "prompts": prompts,
                "instances": instances_meta,
            }
            meta_out.write_text(json.dumps(meta_payload, indent=2), encoding="utf-8")
            sample_done += 1

            if cfg.cleanup_interval and idx % cfg.cleanup_interval == 0:
                _cleanup_memory()

        if sample_done > 0:
            inst_processed += 1
            log_stage_progress(
                "InstRef",
                idx,
                len(video_items),
                folder_id,
                rank,
                status=f"instances({sample_done}/{sample_total})",
                skipped=inst_skipped,
            )
        else:
            inst_skipped += 1
            status = "skip(inst-exist)" if sample_skipped == sample_total else "skip(inst-empty)"
            log_stage_progress(
                "InstRef",
                idx,
                len(video_items),
                folder_id,
                rank,
                status=status,
                skipped=inst_skipped,
            )

    log_stage_summary("InstRef", rank, inst_processed, inst_skipped, inst_failed)
    del instance_segmenter
    _cleanup_memory()

# ============================================================================
# 主处理流程
# ============================================================================

def run_pipeline_iteration(
    cfg,
    rank: int,
    world_size: int,
    local_rank: int,
    device: str,
    output_root: Path,
    all_videos: list[Path],
    my_folder_ids: list[str],
    iteration: int,
) -> int:
    """
    执行一次完整的处理迭代,返回未完成的文件夹数量

    处理流程:
    1. Clip提取: 从源视频池中为每个folder_id提取clip.mp4
    2. Qwen阶段: 识别视频中的前景物体,输出dynamic_prompts.json
    3. SAM3阶段: 根据prompts分割前景mask,输出dynamic_masks.npy
    4. MapAnything+Sample阶段: 估计几何信息,构建训练样本
    """
    if getattr(cfg, "instance_only", False):
        log_stage_header(f"Instance Only Setup (iter={iteration})", rank)
        video_items = _collect_instance_only_items(output_root, my_folder_ids, cfg)
        if not video_items:
            log_message("No eligible folders for instance-only stage.", rank, "InstRef")
            return 0
        _run_instance_reference_stage(
            cfg=cfg,
            rank=rank,
            local_rank=local_rank,
            video_items=video_items,
            output_root=output_root,
        )
        return 0

    # 每个rank使用不同的随机种子打乱视频顺序
    rank_seed = (cfg.shuffle_seed or 42) + rank + (iteration * 1000)
    shuffled_videos = shuffle_videos(all_videos, rank_seed)

    # ============ 第1步: Clip提取 ============
    # 从源视频池中提取固定长度的clip,每个folder_id对应一个clip
    log_stage_header(f"Clip Extraction (iter={iteration})", rank)

    clip_target_wh = (cfg.clip_target_width, cfg.clip_target_height)
    clip_seed = (cfg.shuffle_seed or 42) + iteration

    # 构建待处理列表: (video_path, folder_id, folder_path)
    video_items = []
    setup_extracted, setup_cached, setup_skipped = 0, 0, 0
    video_pool_idx = 0  # 视频池索引

    # 遍历分配给当前rank的所有folder_id
    for folder_idx, folder_id in enumerate(my_folder_ids, start=1):
        folder_path = output_root / folder_id

        # 跳过已完成的文件夹
        is_complete, _ = _check_folder_complete(folder_path, cfg)
        if is_complete:
            clip_path = folder_path / CLIP_FILENAME
            video_items.append((clip_path, folder_id, folder_path))
            setup_cached += 1
            continue

        # 跳过之前已标记失败的文件夹
        is_failed, fail_reason = is_marked_failed(folder_path)
        if is_failed:
            setup_skipped += 1
            continue

        # 检查clip是否已存在且有效
        clip_path = folder_path / CLIP_FILENAME
        if clip_path.exists():
            try:
                info = _get_video_info(clip_path)
                if info is not None:
                    _, frame_count, w, h = info
                    if frame_count == cfg.clip_num_frames and w == clip_target_wh[0] and h == clip_target_wh[1]:
                        video_items.append((clip_path, folder_id, folder_path))
                        setup_cached += 1
                        continue
            except Exception:
                pass
            clip_path.unlink(missing_ok=True)

        # 从视频池中尝试提取clip,失败则尝试下一个视频
        extraction_success = False
        attempts = 0
        max_attempts = len(shuffled_videos)

        while not extraction_success and attempts < max_attempts:
            if video_pool_idx >= len(shuffled_videos):
                break

            source_video = shuffled_videos[video_pool_idx]
            video_pool_idx += 1
            attempts += 1

            chosen_extract_fps = random.choice(cfg.clip_extract_fps)
            success, message = extract_clip(
                video_path=source_video,
                output_path=clip_path,
                num_frames=cfg.clip_num_frames,
                target_fps=cfg.clip_target_fps,
                target_wh=clip_target_wh,
                seed=clip_seed,
                extract_fps=chosen_extract_fps,
            )

            if success:
                video_items.append((clip_path, folder_id, folder_path))
                setup_extracted += 1
                extraction_success = True
                log_stage_progress(
                    "Extract", folder_idx, len(my_folder_ids), folder_id, rank,
                    status=f"ok (attempts={attempts})" if attempts > 1 else "ok",
                    skipped=setup_cached + setup_skipped
                )

        if not extraction_success:
            log_message(f"WARN: {folder_id} - exhausted video pool after {attempts} attempts", rank, "Extract")
            setup_skipped += 1

    log_message(
        f"Clip extraction: extracted={setup_extracted}, cached={setup_cached}, skipped={setup_skipped}, "
        f"videos_used={video_pool_idx}/{len(shuffled_videos)}",
        rank, "Extract"
    )

    if not video_items:
        log_message("No video items to process!", rank, "Init")
        return 0

    # ============ 第2步: Qwen VL 前景物体识别 ============
    # 输入: clip.mp4
    # 输出: dynamic_prompts.json (包含识别出的前景物体描述,如 "person", "car")
    if not cfg.disable_qwen:
        log_stage_header("Qwen (entity extraction)", rank)
        qwen = Qwen3VLEntityExtractor(model_path=cfg.qwen_model_path, device=device)
        qwen_processed, qwen_skipped = 0, 0

        for idx, (video_path, folder_id, folder_path) in enumerate(video_items, start=1):
            is_failed, fail_reason = is_marked_failed(folder_path)
            if is_failed:
                qwen_skipped += 1
                continue

            prompts_path = folder_path / "dynamic_prompts.json"
            if cfg.skip_existing and prompts_path.exists():
                try:
                    json.loads(prompts_path.read_text(encoding="utf-8"))
                    qwen_skipped += 1
                    continue
                except Exception:
                    pass

            try:
                # 使用Qwen VL模型识别视频中的前景物体
                qwen_prompt = cfg.qwen_prompt or DEFAULT_PROMPT
                raw_prompts, raw_text = qwen.extract(str(video_path), prompt=qwen_prompt)
                raw_prompts = raw_prompts[:cfg.max_prompts]
                prompts = Sam3VideoSegmenter.normalize_prompts(raw_prompts)

                payload = {"raw": raw_text, "entities": raw_prompts, "prompts": prompts, "status": "nothing" if not prompts else "ok"}
                prompts_path.write_text(json.dumps(payload, indent=2), encoding="utf-8")
                qwen_processed += 1

                log_stage_progress("Qwen", idx, len(video_items), folder_id, rank, status=", ".join(prompts) or "none", skipped=qwen_skipped)
            except Exception as e:
                log_stage_progress("Qwen", idx, len(video_items), folder_id, rank, status=f"ERROR: {str(e)[:50]}", skipped=qwen_skipped)
                mark_as_failed(folder_path, f"Qwen error: {e}")
                qwen_skipped += 1

            if cfg.cleanup_interval and idx % cfg.cleanup_interval == 0:
                _cleanup_memory()

        log_stage_summary("Qwen", rank, qwen_processed, qwen_skipped)
        del qwen
        _cleanup_memory()

    # ============ 第3步: SAM3 前景分割 ============
    # 输入: clip.mp4 + dynamic_prompts.json
    # 输出:
    #   - mask_{category}.npy / mask_{category}.mp4: 每个类别的独立mask
    #   - dynamic_masks.npy / dynamic_masks.mp4: 合并后的前景mask (兼容旧流程)
    if not cfg.disable_sam3:
        log_stage_header("SAM3 (segmentation)", rank)
        sam3 = Sam3VideoSegmenter(
            gpus_to_use=[local_rank],
            propagation_direction=cfg.sam3_propagation_direction,
            score_threshold_detection=cfg.sam3_score_threshold,
            new_det_thresh=cfg.sam3_new_det_thresh,
            checkpoint_path=cfg.sam3_checkpoint_path,
            multi_prompt=cfg.sam3_multi_prompt,
            resize_input_to=cfg.sam3_resize_input_to,
        )

        sam3_failed_log = output_root / "sam3_failed_samples.txt"
        sam3_processed, sam3_skipped, sam3_failed = 0, 0, 0

        for idx, (video_path, folder_id, folder_path) in enumerate(video_items, start=1):
            is_failed, fail_reason = is_marked_failed(folder_path)
            if is_failed:
                sam3_skipped += 1
                continue

            # 加载Qwen识别的prompts
            prompts_path = folder_path / "dynamic_prompts.json"
            prompts = Sam3VideoSegmenter.normalize_prompts(_load_cached_prompts(prompts_path) if prompts_path.exists() else [])

            masks_npy = folder_path / "dynamic_masks.npy"
            masks_mp4 = folder_path / "dynamic_masks.mp4"

            # 跳过逻辑:
            # 1. 如果dynamic_masks.mp4存在且全黑 -> 跳过 (没有分割到任何物体)
            # 2. 如果per-category masks mp4已经存在 -> 跳过 (已分割过)
            if cfg.skip_existing:
                # 检查是否有per-category masks (mp4)
                has_category_masks = False
                if prompts:
                    for prompt in prompts:
                        safe_name = prompt.replace(" ", "_").replace("/", "_")
                        if (folder_path / f"mask_{safe_name}.mp4").exists():
                            has_category_masks = True
                            break

                if has_category_masks or masks_mp4.exists():
                    sam3_skipped += 1
                    continue

            try:
                num_frames = get_video_frame_count(video_path)
                video_fps = get_video_fps(video_path)

                # 如果没有识别到前景物体,生成全零mask
                if not prompts:
                    hw = _get_video_hw(video_path)
                    if hw is None:
                        sam3_failed += 1
                        continue
                    dynamic_masks = np.zeros((num_frames, hw[0], hw[1]), dtype=bool)
                    status = "empty"
                else:
                    # 使用SAM3分割每个类别的前景物体
                    category_masks = sam3.segment_per_category(
                        str(video_path), prompts=prompts,
                        frame_index=cfg.sam3_frame_index,
                        start_frame_index=cfg.sam3_start_frame_index,
                        max_frame_num_to_track=cfg.sam3_max_frame_num_to_track,
                        expected_frames=num_frames,
                    )

                    # 保存每个类别的独立mask
                    save_per_category_masks(folder_path, category_masks, video_fps)

                    # 合并所有类别的mask (用于后续流程)
                    if category_masks:
                        dynamic_masks = None
                        for masks in category_masks.values():
                            if dynamic_masks is None:
                                dynamic_masks = masks.astype(bool)
                            else:
                                dynamic_masks = dynamic_masks | masks.astype(bool)
                        status = ", ".join(category_masks.keys())
                    else:
                        hw = _get_video_hw(video_path)
                        if hw is None:
                            sam3_failed += 1
                            continue
                        dynamic_masks = np.zeros((num_frames, hw[0], hw[1]), dtype=bool)
                        status = "no-mask"

                # 保存合并后的mask为mp4(不再保存npy)
                masks_rgb = np.repeat(dynamic_masks.astype(np.uint8)[..., None] * 255, 3, axis=-1)
                save_video(masks_rgb, masks_mp4, fps=video_fps)
                sam3_processed += 1
                log_stage_progress("SAM3", idx, len(video_items), folder_id, rank, status=status, skipped=sam3_skipped)

            except (torch.cuda.OutOfMemoryError, RuntimeError) as e:
                if "out of memory" in str(e).lower() or isinstance(e, torch.cuda.OutOfMemoryError):
                    log_stage_progress("SAM3", idx, len(video_items), folder_id, rank, status="OOM", skipped=sam3_skipped)
                    log_failed_sample(sam3_failed_log, folder_id, f"OOM: {e}", rank)
                    mark_as_failed(folder_path, f"SAM3 OOM: {e}")
                    sam3_failed += 1
                    _cleanup_memory()
                else:
                    log_stage_progress("SAM3", idx, len(video_items), folder_id, rank, status=f"ERROR: {str(e)[:50]}", skipped=sam3_skipped)
                    log_failed_sample(sam3_failed_log, folder_id, f"RuntimeError: {e}", rank)
                    mark_as_failed(folder_path, f"SAM3 error: {e}")
                    sam3_failed += 1

            if cfg.cleanup_interval and idx % cfg.cleanup_interval == 0:
                _cleanup_memory()

        log_stage_summary("SAM3", rank, sam3_processed, sam3_skipped, sam3_failed)
        del sam3
        _cleanup_memory()

    # ============ 第3.5步: 背景运动过滤 (光流 + mask) ============
    if cfg.bg_motion_filter:
        log_stage_header("Background Motion Filter", rank)
        bg_ok, bg_reject, bg_skip = 0, 0, 0
        filtered_items = []
        for idx, (video_path, folder_id, folder_path) in enumerate(video_items, start=1):
            is_failed, _ = is_marked_failed(folder_path)
            if is_failed:
                bg_skip += 1
                continue

            # 天空面积检查
            sky_mask_mp4 = folder_path / "mask_sky.mp4"
            sky_masks = load_mask_from_mp4(sky_mask_mp4)
            if sky_masks is not None and len(sky_masks) > 0:
                sky_ratio = float(sky_masks.mean())
                if sky_ratio > cfg.bg_motion_max_sky_ratio:
                    mark_as_failed(folder_path, f"sky_too_large: {sky_ratio:.1%} > {cfg.bg_motion_max_sky_ratio:.0%}")
                    bg_reject += 1
                    log_stage_progress("BgMotion", idx, len(video_items), folder_id, rank,
                                       status=f"sky={sky_ratio:.1%}, reject")
                    continue

            # 加载前景 mask
            masks_mp4 = folder_path / "dynamic_masks.mp4"
            masks = load_mask_from_mp4(masks_mp4)

            passed, reason = check_bg_motion(video_path, masks, cfg, device)
            if passed:
                filtered_items.append((video_path, folder_id, folder_path))
                bg_ok += 1
                log_stage_progress("BgMotion", idx, len(video_items), folder_id, rank, status=reason)
            else:
                mark_as_failed(folder_path, f"bg_motion_static: {reason}")
                bg_reject += 1
                log_stage_progress("BgMotion", idx, len(video_items), folder_id, rank, status=reason)

        log_message(f"Background motion filter: passed={bg_ok}, rejected={bg_reject}, skipped={bg_skip}", rank, "BgMotion")
        video_items = filtered_items

    # ============ 第4步: Stream3R几何估计 + 训练样本构建 ============
    # 输入: clip.mp4 + dynamic_masks.npy
    # 输出:
    #   - geometry.npz: 深度图、相机位姿(c2w)、相机内参
    #   - train_*.mp4: 训练样本视频 (preceding/target/reference RGB, scene投影等)
    #   - train_sample.json: 样本元信息
    log_stage_header("Geometry (Stream3R) + Sample Building", rank)

    stream3r_config = OmegaConf.create({
        "model_path": cfg.get("stream3r_model_path", "/home/zhangzhilu/anaconda3/pretrained_models/STream3R"),
        "device": device,
        "preprocess_mode": cfg.get("stream3r_preprocess_mode", "crop"),
        "target_size": cfg.get("stream3r_target_size", 518),
        "window_size": cfg.get("stream3r_window_size", 32),
        "conf_threshold": cfg.get("stream3r_conf_threshold", 0.0),
    })
    estimator = Stream3REstimator(stream3r_config)

    sample_config = CONFIG  # Use the global config directly

    failed_log = output_root / "failed_samples.txt"
    s3_processed, s3_skipped, s3_failed = 0, 0, 0

    _show_pbar = (local_rank == 0)
    pbar = tqdm(total=len(video_items), desc=f"[R{rank}] Stage3", disable=not _show_pbar,
                dynamic_ncols=True, leave=True)

    for idx, (video_path, folder_id, folder_path) in enumerate(video_items, start=1):
        is_failed, fail_reason = is_marked_failed(folder_path)
        if is_failed:
            s3_skipped += 1
            pbar.update(1)
            pbar.set_postfix_str(f"ok={s3_processed} skip={s3_skipped} fail={s3_failed}")
            continue

        geo_complete = _check_stage3_geometry_complete(folder_path) if cfg.skip_existing else False
        sample_complete = _check_stage3_samples_complete(folder_path, cfg, projection_only=cfg.projection_only) if cfg.skip_existing else False

        if getattr(cfg, "reference_only", False):
            names = get_sample_naming(cfg.naming_style)
            sample_json_paths = sorted(folder_path.glob("train*sample.json"))
            if not sample_json_paths:
                s3_skipped += 1
                pbar.update(1); pbar.set_postfix_str(f"ok={s3_processed} skip={s3_skipped} fail={s3_failed}")
                continue

            # Load foreground masks (exclude sky). If missing, nothing to do.
            prompts_path = folder_path / "dynamic_prompts.json"
            prompts = Sam3VideoSegmenter.normalize_prompts(_load_cached_prompts(prompts_path) if prompts_path.exists() else [])
            fg_masks_all = load_and_merge_category_masks(folder_path, prompts, exclude_prompts=["sky"])
            if fg_masks_all is None or fg_masks_all.size == 0:
                s3_skipped += 1
                pbar.update(1); pbar.set_postfix_str(f"ok={s3_processed} skip={s3_skipped} fail={s3_failed}")
                continue

            fps = cfg.fps_override or get_video_fps(video_path)
            updated = 0
            for sample_json in sample_json_paths:
                try:
                    meta = json.loads(sample_json.read_text(encoding="utf-8"))
                except Exception:
                    continue
                prefix = sample_json.name[: -len("sample.json")]

                ref_path = folder_path / f"{prefix}{names.reference_rgb}.mp4"
                out_scene = folder_path / f"{prefix}{names.reference_scene_rgb}.mp4"

                if not ref_path.exists():
                    continue
                if out_scene.exists():
                    continue

                ref_indices = meta.get("R_idx", [])
                if not ref_indices:
                    continue
                if max(ref_indices) >= len(fg_masks_all):
                    continue

                ref_frames = np.asarray(load_video_frames(ref_path))
                if ref_frames.size == 0:
                    continue

                fg_masks = fg_masks_all[ref_indices]
                if fg_masks.shape[1:3] != ref_frames.shape[1:3]:
                    fg_masks = resize_masks(fg_masks, ref_frames.shape[1:3])

                _save_reference_scene(ref_frames, fg_masks, out_scene, fps)
                updated += 1

            if updated > 0:
                s3_processed += 1
            else:
                s3_skipped += 1
            pbar.update(1); pbar.set_postfix_str(f"ok={s3_processed} skip={s3_skipped} fail={s3_failed}")
            continue

        if cfg.projection_only:
            # projection_only 模式: 只要 projection 文件完整就跳过
            if sample_complete:
                s3_skipped += 1
                pbar.update(1); pbar.set_postfix_str(f"ok={s3_processed} skip={s3_skipped} fail={s3_failed}")
                continue
        elif geo_complete and sample_complete:
            s3_skipped += 1
            pbar.update(1); pbar.set_postfix_str(f"ok={s3_processed} skip={s3_skipped} fail={s3_failed}")
            continue

        try:
            folder_path.mkdir(parents=True, exist_ok=True)

            # 加载前景mask
            prompts_path = folder_path / "dynamic_prompts.json"
            prompts = Sam3VideoSegmenter.normalize_prompts(_load_cached_prompts(prompts_path) if prompts_path.exists() else [])

            dynamic_masks = None
            foreground_masks = None  # 前景mask (排除sky)
            if not cfg.disable_sam3 and prompts:
                # 从mp4加载dynamic_masks
                masks_mp4 = folder_path / "dynamic_masks.mp4"
                dynamic_masks = load_mask_from_mp4(masks_mp4)
                # 加载前景mask: 合并除sky以外的所有类别
                foreground_masks = load_and_merge_category_masks(
                    folder_path, prompts, exclude_prompts=["sky"]
                )

            # 检查mask质量
            if dynamic_masks is not None:
                valid, max_white = check_dynamic_mask_quality(dynamic_masks, 0.95)
                if not valid:
                    pbar.update(1); pbar.set_postfix_str(f"ok={s3_processed} skip={s3_skipped} fail={s3_failed}")
                    log_failed_sample(failed_log, folder_id, f"mask white ratio {max_white:.0%}", rank)
                    mark_as_failed(folder_path, f"mask white ratio {max_white:.0%}")
                    s3_failed += 1
                    continue

            fps = cfg.fps_override or get_video_fps(video_path)
            frames = load_video_frames(video_path)

            # 几何估计: 使用Stream3R估计深度和相机位姿
            if not geo_complete:
                geometry = estimator.estimate_video_geometry(frames, frame_indices=list(range(len(frames))))
                np.savez_compressed(
                    folder_path / "geometry.npz",
                    depths=geometry.depths.astype(np.float32),
                    poses_c2w=geometry.poses_c2w.astype(np.float64),
                    intrinsics=geometry.intrinsics.astype(np.float64),
                    fps=np.array([fps]),
                    original_size=np.array(geometry.original_size),
                    processed_size=np.array(geometry.processed_size),
                )
                H, W = geometry.frames.shape[1:3]
                dynamic_masks_proc = resize_masks(dynamic_masks, (H, W)) if dynamic_masks is not None and dynamic_masks.size > 0 else None
                foreground_masks_proc = resize_masks(foreground_masks, (H, W)) if foreground_masks is not None and foreground_masks.size > 0 else None
                geo_status = "geo"
            else:
                # 加载缓存的几何信息
                geo_data = dict(np.load(folder_path / "geometry.npz"))
                proc_h, proc_w = int(geo_data["processed_size"][0]), int(geo_data["processed_size"][1])
                orig_h, orig_w = int(geo_data["original_size"][0]), int(geo_data["original_size"][1])

                # Online upgrade: if depths are at old proc resolution, upsample to original
                if proc_h != orig_h or proc_w != orig_w:
                    depths = geo_data["depths"]
                    if depths.shape[1] != orig_h or depths.shape[2] != orig_w:
                        scale_x, scale_y = orig_w / proc_w, orig_h / proc_h
                        depths_up = np.stack([
                            cv2.resize(d, (orig_w, orig_h), interpolation=cv2.INTER_LINEAR)
                            for d in depths
                        ])
                        intrinsics_up = geo_data["intrinsics"].copy()
                        intrinsics_up[:, 0, 0] *= scale_x
                        intrinsics_up[:, 1, 1] *= scale_y
                        intrinsics_up[:, 0, 2] *= scale_x
                        intrinsics_up[:, 1, 2] *= scale_y
                        geo_data["depths"] = depths_up.astype(np.float32)
                        geo_data["intrinsics"] = intrinsics_up
                    geo_data["processed_size"] = np.array([orig_h, orig_w], dtype=np.int64)
                    np.savez_compressed(folder_path / "geometry.npz", **geo_data)

                H, W = int(geo_data["processed_size"][0]), int(geo_data["processed_size"][1])
                frames_resized = np.stack([cv2.resize(f, (W, H), interpolation=cv2.INTER_LINEAR) for f in frames], axis=0)
                geometry = VideoGeometry(
                    frames=frames_resized, depths=geo_data["depths"], intrinsics=geo_data["intrinsics"],
                    poses_c2w=geo_data["poses_c2w"], masks=None, frame_indices=np.arange(len(frames), dtype=np.int32),
                    original_size=tuple(geo_data["original_size"]), processed_size=(H, W),
                )
                dynamic_masks_proc = resize_masks(dynamic_masks, (H, W)) if dynamic_masks is not None and dynamic_masks.size > 0 else None
                foreground_masks_proc = resize_masks(foreground_masks, (H, W)) if foreground_masks is not None and foreground_masks.size > 0 else None
                geo_status = "geo-cached"

            # 构建训练样本
            ref_status = ""
            if cfg.projection_only:
                sample_json_paths = sorted(folder_path.glob("train*sample.json"))
                if not sample_json_paths:
                    sample_status = "projection-only(no-sample)"
                else:
                    total = len(sample_json_paths)
                    ok = 0
                    errors: list[str] = []
                    for sample_json in sample_json_paths:
                        try:
                            meta = json.loads(sample_json.read_text(encoding="utf-8"))
                        except Exception:
                            errors.append(f"{sample_json.name}:json")
                            continue
                        prefix = sample_json.name[: -len("sample.json")]
                        status = _rerender_projection_videos(
                            folder_path=folder_path,
                            sample_meta=meta,
                            geometry=geometry,
                            frames=frames,
                            dynamic_masks_proc=dynamic_masks_proc,
                            foreground_masks_proc=foreground_masks_proc,
                            cfg=cfg,
                            sample_config=sample_config,
                            fps=fps,
                            prefix=prefix,
                        )
                        if status == "ok":
                            ok += 1
                        else:
                            errors.append(f"{prefix}{status}")
                    if errors:
                        pbar.write(f"[R{rank}] proj-only warnings {folder_id}: {', '.join(errors[:3])}")
                    sample_status = f"projection-only({ok}/{total})"
            elif not sample_complete:
                original_frames = frames
                output_size = (cfg.clip_target_height, cfg.clip_target_width)

                rng = np.random.default_rng(cfg.random_seed)
                for sample_idx in range(cfg.num_samples):
                    # 调用build_training_sample构建完整训练样本
                    sample = build_training_sample(
                        geometry=geometry, config=sample_config,
                        dynamic_masks=dynamic_masks_proc,
                        foreground_masks=foreground_masks_proc,
                        rng=rng,
                        projection_fill_kernel=0,
                        original_frames=original_frames,
                        output_size=output_size,
                    )

                    meta = sample.get("meta", {})
                    r_stats = meta.get("R_stats", {})
                    ref_count = len(meta.get("R_idx", []))
                    best_iou = r_stats.get("best_iou", 0)
                    threshold = r_stats.get("threshold", 0)

                    if ref_count > 0:
                        ref_status = f"ref={ref_count}, iou={best_iou:.3f}"
                    else:
                        reason = r_stats.get("no_ref_reason", "unknown")
                        if reason == "iou_below_threshold":
                            ref_status = f"NO_REF(iou={best_iou:.3f}<{threshold})"
                        elif reason == "no_candidates":
                            ref_status = "NO_REF(no_candidates)"
                        else:
                            ref_status = f"NO_REF({reason})"

                    prefix = "train_" if cfg.num_samples == 1 else f"train_sample{sample_idx:03d}_"
                    _fg_overlay_aug = None
                    if getattr(cfg, "fg_overlay_aug_enable", False):
                        _fg_overlay_aug = {
                            k.replace("fg_overlay_aug_", ""): getattr(cfg, k)
                            for k in dir(cfg)
                            if k.startswith("fg_overlay_aug_") and k != "fg_overlay_aug_enable"
                        }
                    save_training_sample(sample, folder_path, projection_channels=cfg.projection_channels,
                                         fps=fps, naming=cfg.naming_style, name_prefix=prefix,
                                         fg_overlay_aug=_fg_overlay_aug)
                    # Save augmented reference frame for visual inspection
                    if "scene_rgb_augmented" in sample:
                        cv2.imwrite(str(folder_path / f"{prefix}scene_rgb_augmented.png"),
                                    cv2.cvtColor(sample["scene_rgb_augmented"], cv2.COLOR_RGB2BGR))
                        cv2.imwrite(str(folder_path / f"{prefix}scene_rgb_original.png"),
                                    cv2.cvtColor(sample["scene_rgb_original"], cv2.COLOR_RGB2BGR))
                sample_status = f"sample({ref_status})"
            else:
                sample_json = folder_path / "train_sample.json"
                if sample_json.exists():
                    try:
                        sample_meta = json.loads(sample_json.read_text(encoding="utf-8"))
                        ref_count = len(sample_meta.get("R_idx", []))
                        r_stats = sample_meta.get("R_stats", {})
                        if ref_count > 0:
                            ref_status = f"ref={ref_count}, iou={r_stats.get('best_iou', 0):.3f}"
                        else:
                            reason = r_stats.get("no_ref_reason", "unknown")
                            best_iou = r_stats.get("best_iou", 0)
                            threshold = r_stats.get("threshold", 0)
                            if reason == "iou_below_threshold":
                                ref_status = f"NO_REF(iou={best_iou:.3f}<{threshold})"
                            else:
                                ref_status = f"NO_REF({reason})"
                    except Exception:
                        ref_status = "ref=?"
                sample_status = f"sample-cached({ref_status})"

            s3_processed += 1
            pbar.update(1); pbar.set_postfix_str(f"ok={s3_processed} skip={s3_skipped} fail={s3_failed}")

        except Exception as e:
            tb = traceback.format_exc()
            pbar.write(f"[R{rank}] Stage3 ERROR {folder_id}: {str(e)[:80]}")
            log_failed_sample(failed_log, folder_id, f"{e}\n{tb}", rank)
            mark_as_failed(folder_path, f"Stage3 error: {e}")
            if not geo_complete:
                skip_file = folder_path / ".skip"
                skip_content = skip_file.read_text(encoding="utf-8") if skip_file.exists() else None
                cleanup_failed_output(folder_path)
                if skip_content:
                    folder_path.mkdir(parents=True, exist_ok=True)
                    skip_file.write_text(skip_content, encoding="utf-8")
            s3_failed += 1
            pbar.update(1); pbar.set_postfix_str(f"ok={s3_processed} skip={s3_skipped} fail={s3_failed}")
            continue

        if cfg.cleanup_interval and idx % cfg.cleanup_interval == 0:
            _cleanup_memory()

    pbar.close()
    log_stage_summary("Stage3", rank, s3_processed, s3_skipped, s3_failed)
    del estimator
    _cleanup_memory()

    # ============ 第5步: Instance Reference (target instances) ============
    if getattr(cfg, "instance_ref_enable", False):
        _run_instance_reference_stage(
            cfg=cfg,
            rank=rank,
            local_rank=local_rank,
            video_items=video_items,
            output_root=output_root,
        )

    # ============ 完成状态检查 ============
    # 检查哪些文件夹完成、哪些失败需要重新采样
    log_stage_header(f"Completeness Check (iter={iteration})", rank)

    error_folders = []      # 有.skip标记,需要删除并重新采样
    interrupted_folders = []  # 无.skip但不完整,可以增量处理
    complete_count = 0

    for folder_id in my_folder_ids:
        folder_path = output_root / folder_id
        is_complete, reason = _check_folder_complete(folder_path, cfg)
        if is_complete:
            complete_count += 1
        elif reason.startswith("error:"):
            error_folders.append((folder_id, reason))
        else:
            interrupted_folders.append((folder_id, reason))

    log_message(
        f"Completeness: complete={complete_count}, errors={len(error_folders)}, interrupted={len(interrupted_folders)}",
        rank, "Check"
    )

    # Clean up error folders (will re-sample new videos next iteration)
    if error_folders:
        log_message(f"Cleaning up {len(error_folders)} error folders for re-sampling:", rank, "Cleanup")
        for folder_id, reason in error_folders:
            folder_path = output_root / folder_id
            cleanup_error_folder(folder_path, rank)
            log_message(f"  {folder_id}: {reason}", rank, "Cleanup")

    # Interrupted folders are kept as-is, will continue incrementally
    if interrupted_folders:
        log_message(f"Keeping {len(interrupted_folders)} interrupted folders for incremental processing:", rank, "Check")
        for folder_id, reason in interrupted_folders:
            log_message(f"  {folder_id}: {reason}", rank, "Check")

    # Return total incomplete count (both error and interrupted need more work)
    return len(error_folders) + len(interrupted_folders)


def main():
    cfg = CONFIG  # Use global config directly

    rank, world_size, local_rank = get_rank_info()
    device = f"cuda:{local_rank}" if torch.cuda.is_available() else "cpu"
    if torch.cuda.is_available():
        torch.cuda.set_device(local_rank)

    # Print config on rank 0
    if rank == 0:
        from omegaconf import OmegaConf as _OC
        print("=" * 60)
        print("LiveWorld Data Preparation Config")
        print("=" * 60)
        print(_OC.to_yaml(cfg))
        print("=" * 60)

    output_root = Path(cfg.output_root)
    output_root.mkdir(parents=True, exist_ok=True)

    # ============ Collect source videos ============
    log_stage_header("Video Collection", rank)

    # Collect all source videos
    all_videos = collect_all_source_videos(cfg.video_dirs, rank, cfg.video_dirs_ratio)
    log_message(f"Total source videos: {len(all_videos)}", rank, "Init", only_rank0=True)

    if not all_videos:
        log_message("No source videos found!", rank, "Init")
        return

    # Prepare folder IDs and shard across ranks
    max_videos = cfg.max_videos or len(all_videos)
    folder_ids = prepare_folder_ids(max_videos)
    my_folder_ids = shard_items(folder_ids, rank, world_size)
    log_message(f"This rank processing {len(my_folder_ids)} folders", rank, "Init")

    # ============ Run pipeline with retry loop ============
    max_iterations = 10  # Safety limit
    iteration = 0

    while iteration < max_iterations:
        log_stage_header(f"PIPELINE ITERATION {iteration}", rank)

        incomplete_count = run_pipeline_iteration(
            cfg=cfg,
            rank=rank,
            world_size=world_size,
            local_rank=local_rank,
            device=device,
            output_root=output_root,
            all_videos=all_videos,
            my_folder_ids=my_folder_ids,
            iteration=iteration,
        )

        if incomplete_count == 0:
            log_message(f"All {len(my_folder_ids)} folders complete!", rank, "Done")
            break

        log_message(
            f"Iteration {iteration} done. {incomplete_count} folders incomplete, retrying...",
            rank, "Retry"
        )
        iteration += 1

    if iteration >= max_iterations:
        log_message(f"WARNING: Reached max iterations ({max_iterations}), some folders may be incomplete", rank, "Done")

    ts = datetime.datetime.now().strftime("%H:%M:%S")
    print(f"\n{'=' * 60}", flush=True)
    print(f"[{ts}][R{rank}] ALL STAGES COMPLETE (iterations={iteration + 1})", flush=True)
    print(f"{'=' * 60}", flush=True)


if __name__ == "__main__":
    main()