-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage09-prbs.html
More file actions
985 lines (854 loc) · 38.9 KB
/
Copy pathpage09-prbs.html
File metadata and controls
985 lines (854 loc) · 38.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
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
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Page 09 - Randomisation PRBS</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background: #1a1a2e;
color: #eee;
}
h1 {
color: #00d4ff;
text-align: center;
}
.description {
background: #16213e;
padding: 15px;
border-radius: 8px;
margin-bottom: 20px;
border-left: 4px solid #00d4ff;
}
.pipeline {
display: flex;
justify-content: center;
align-items: center;
gap: 10px;
margin-bottom: 20px;
font-family: monospace;
font-size: 14px;
flex-wrap: wrap;
}
.pipeline-step {
background: #0f3460;
padding: 8px 15px;
border-radius: 5px;
}
.pipeline-step.active {
background: #00d4ff;
color: #000;
}
.pipeline-step.done {
background: #006644;
}
.pipeline-arrow {
color: #00d4ff;
font-size: 20px;
}
.canvas-container {
display: flex;
gap: 20px;
justify-content: center;
flex-wrap: wrap;
}
.canvas-box {
background: #16213e;
padding: 15px;
border-radius: 8px;
text-align: center;
}
.canvas-box h3 {
margin-top: 0;
color: #00d4ff;
}
canvas {
border: 2px solid #0f3460;
cursor: pointer;
}
.controls {
margin-top: 10px;
}
button {
background: #0f3460;
color: #fff;
border: none;
padding: 10px 20px;
margin: 5px;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
}
button:hover {
background: #00d4ff;
color: #000;
}
button:disabled {
background: #333;
cursor: not-allowed;
}
input[type="file"] {
display: none;
}
.info {
margin-top: 10px;
font-size: 12px;
color: #888;
}
.stats {
background: #0f3460;
padding: 10px;
border-radius: 5px;
margin-top: 15px;
font-size: 12px;
text-align: left;
}
.detail-section {
max-width: 1100px;
margin: 20px auto;
}
.hex-display {
font-family: monospace;
font-size: 11px;
background: #0a0a15;
padding: 10px;
border-radius: 5px;
max-height: 120px;
overflow-y: auto;
word-break: break-all;
text-align: left;
}
.hex-display .sync { color: #ff4444; font-weight: bold; }
.hex-display .sync-inv { color: #ff00ff; font-weight: bold; }
.hex-display .data { color: #00d4ff; }
.legend {
display: flex;
flex-wrap: wrap;
gap: 10px;
justify-content: center;
margin-top: 10px;
}
.legend-item {
display: flex;
align-items: center;
gap: 5px;
font-size: 10px;
}
.legend-color {
width: 16px;
height: 12px;
border-radius: 2px;
}
.section-detail {
background: #0f3460;
padding: 15px;
border-radius: 8px;
margin-top: 15px;
}
.section-detail h4 {
margin-top: 0;
color: #00d4ff;
}
.field-list {
font-family: monospace;
font-size: 11px;
line-height: 1.6;
}
.field-list .field-name { color: #ff8800; }
.field-list .field-value { color: #00ff88; }
.prbs-diagram {
font-family: monospace;
font-size: 11px;
background: #0a0a15;
padding: 15px;
border-radius: 5px;
overflow-x: auto;
}
.register-display {
font-family: monospace;
font-size: 14px;
background: #0a0a15;
padding: 10px;
border-radius: 5px;
text-align: center;
margin: 10px 0;
}
.register-display .bit-1 { color: #00ff88; }
.register-display .bit-0 { color: #666; }
table.info-table {
border-collapse: collapse;
font-size: 11px;
width: 100%;
margin: 10px 0;
}
table.info-table th, table.info-table td {
border: 1px solid #333;
padding: 5px 8px;
text-align: left;
}
table.info-table th {
background: #0f3460;
}
</style>
</head>
<body>
<h1>🎲 Page 09 - Randomisation PRBS</h1>
<div class="description">
<strong>Fonction :</strong> Dispersion d'énergie (Energy Dispersal) selon DVB-S EN 300 421.<br>
XOR du flux TS avec une séquence pseudo-aléatoire PRBS pour éviter les longues suites de 0 ou 1.<br>
<strong>Polynôme :</strong> 1 + X¹⁴ + X¹⁵ — <strong>Init :</strong> 100101010000000 au début de chaque super-trame (8 paquets)<br>
<strong>Entrée :</strong> Fichier JSON du TS (sortie Page 08)<br>
<strong>Sortie :</strong> Flux TS randomisé
</div>
<div class="pipeline">
<span class="pipeline-step done">TS</span>
<span class="pipeline-arrow">→</span>
<span class="pipeline-step active">PRBS</span>
<span class="pipeline-arrow">→</span>
<span class="pipeline-step">Reed-Solomon</span>
<span class="pipeline-arrow">→</span>
<span class="pipeline-step">Entrelacement</span>
<span class="pipeline-arrow">→</span>
<span class="pipeline-step">Viterbi</span>
<span class="pipeline-arrow">→</span>
<span class="pipeline-step">QPSK</span>
</div>
<div class="canvas-container">
<!-- CANVAS 1 : TS en entrée -->
<div class="canvas-box">
<h3>1. ENTRÉE - Transport Stream</h3>
<canvas id="canvasInput" width="352" height="288"></canvas>
<div class="controls">
<button onclick="document.getElementById('fileInput').click()">📁 Charger JSON</button>
<input type="file" id="fileInput" accept=".json" onchange="loadJSON(this.files)">
</div>
<div class="info">TS MPEG-2 (sortie Page 08)</div>
<div id="statsInput" class="stats" style="display:none;"></div>
</div>
<!-- CANVAS 2 : Visualisation PRBS -->
<div class="canvas-box">
<h3>2. TRAITEMENT - PRBS Generator</h3>
<canvas id="canvasProcess" width="352" height="288"></canvas>
<div class="controls">
<button id="btnRandomize" onclick="randomize()" disabled>⚙️ Randomiser</button>
<button id="btnAnimate" onclick="animatePRBS()" disabled>▶️ Animer</button>
<button id="btnStop" onclick="stopAnimation()" disabled>⏹️ Stop</button>
</div>
<div class="info">Registre LFSR 15 bits</div>
</div>
<!-- CANVAS 3 : TS randomisé -->
<div class="canvas-box">
<h3>3. SORTIE - TS Randomisé</h3>
<canvas id="canvasOutput" width="352" height="288"></canvas>
<div class="controls">
<button id="btnExportJSON" onclick="exportJSON()" disabled>💾 JSON</button>
<button id="btnExportBin" onclick="exportBinary()" disabled>💾 .ts</button>
</div>
<div class="info">Cliquez sur un paquet pour comparer</div>
<div class="legend">
<div class="legend-item"><div class="legend-color" style="background:#ff00ff"></div> Sync inversé (0xB8)</div>
<div class="legend-item"><div class="legend-color" style="background:#00d4ff"></div> Données XOR PRBS</div>
</div>
</div>
</div>
<!-- Détails -->
<div class="detail-section">
<div id="packetDetail" class="section-detail" style="display:none;">
<h4 id="detailTitle">Comparaison Paquet</h4>
<div style="display: flex; gap: 20px; flex-wrap: wrap;">
<div style="flex: 1; min-width: 280px;">
<strong>Avant randomisation :</strong>
<div id="detailBefore" class="hex-display"></div>
</div>
<div style="flex: 1; min-width: 280px;">
<strong>Après randomisation :</strong>
<div id="detailAfter" class="hex-display"></div>
</div>
</div>
<div style="margin-top: 10px;">
<strong>Séquence PRBS appliquée :</strong>
<div id="detailPRBS" class="hex-display"></div>
</div>
<div id="detailStats" style="margin-top: 10px; font-size: 11px; color: #888;"></div>
</div>
<div id="globalStats" class="stats" style="display:none; margin-top: 15px;"></div>
<h4 style="color: #00d4ff; margin-top: 20px;">Registre LFSR (Linear Feedback Shift Register)</h4>
<div class="prbs-diagram">
┌─────────────────────────────────────────────────────────────────────────────────┐
│ Polynôme: 1 + X¹⁴ + X¹⁵ │
│ │
│ ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐ │
│ │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │10 │11 │12 │13 │14 │15 │──→ Sortie │
│ └─┬─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─┬─┴─┬─┴───┘ │
│ │ │ │ │
│ │ ┌──────────────────────┘ │ │
│ │ │ │ │
│ │ XOR ←──────────────────────┘ │
│ │ │ │
│ └────────────────────────┘ │
│ │
│ Initialisation: 100101010000000 (0x4A80) au début de chaque super-trame │
└─────────────────────────────────────────────────────────────────────────────────┘
Super-trame = 8 paquets TS = 8 × 188 = 1504 octets
• Paquet 1: sync = 0x47 → 0xB8 (inversé), PRBS réinitialisé
• Paquets 2-8: sync = 0x47 (non modifié), PRBS continue
• Les 187 octets de données sont XORés avec PRBS
</div>
<h4 style="color: #00d4ff; margin-top: 20px;">Fonctionnement</h4>
<table class="info-table">
<tr>
<th>Élément</th>
<th>Valeur</th>
<th>Description</th>
</tr>
<tr>
<td>Polynôme PRBS</td>
<td>1 + X¹⁴ + X¹⁵</td>
<td>Feedback des bits 14 et 15 (XOR)</td>
</tr>
<tr>
<td>Registre</td>
<td>15 bits</td>
<td>Période = 2¹⁵ - 1 = 32767 bits</td>
</tr>
<tr>
<td>Initialisation</td>
<td>100101010000000</td>
<td>= 0x4A80, reset tous les 8 paquets</td>
</tr>
<tr>
<td>Sync byte paquet 1</td>
<td>0x47 → 0xB8</td>
<td>Inversé pour synchronisation super-trame</td>
</tr>
<tr>
<td>Sync bytes 2-8</td>
<td>0x47 (inchangé)</td>
<td>Non randomisés mais comptés</td>
</tr>
</table>
</div>
<script>
// ============================================================
// CONSTANTES
// ============================================================
const WIDTH = 352;
const HEIGHT = 288;
const TS_PACKET_SIZE = 188;
const SUPER_FRAME_SIZE = 8; // 8 paquets TS par super-trame
// PRBS : polynôme 1 + X^14 + X^15
const PRBS_INIT = 0x4A80; // 100101010000000 en binaire
// Sync bytes
const SYNC_BYTE = 0x47;
const SYNC_BYTE_INVERTED = 0xB8;
// ============================================================
// VARIABLES GLOBALES
// ============================================================
let inputData = null;
let tsPackets = [];
let randomizedPackets = [];
let prbsSequences = []; // Séquences PRBS pour chaque paquet
let randomized = false;
let selectedPacket = null;
let animationId = null;
// ============================================================
// INITIALISATION
// ============================================================
window.onload = function() {
clearCanvas('canvasInput');
displayProcessInit();
clearCanvas('canvasOutput');
};
// ============================================================
// CHARGEMENT JSON
// ============================================================
function loadJSON(files) {
if (!files || files.length === 0) return;
const reader = new FileReader();
reader.onload = function(e) {
try {
inputData = JSON.parse(e.target.result);
if (!inputData.format || inputData.format !== 'MPEG2_TS_V1') {
alert('Format non reconnu. Utilisez la sortie de Page 08.');
return;
}
// Décoder le binaire base64
const binaryString = atob(inputData.binary);
const tsBinary = [];
for (let i = 0; i < binaryString.length; i++) {
tsBinary.push(binaryString.charCodeAt(i));
}
// Reconstruire les paquets
tsPackets = [];
for (let i = 0; i < tsBinary.length; i += TS_PACKET_SIZE) {
tsPackets.push({
index: tsPackets.length,
bytes: tsBinary.slice(i, i + TS_PACKET_SIZE)
});
}
randomizedPackets = [];
prbsSequences = [];
randomized = false;
selectedPacket = null;
displayInputTS();
displayProcessInit();
clearCanvas('canvasOutput');
document.getElementById('btnRandomize').disabled = false;
document.getElementById('btnAnimate').disabled = false;
document.getElementById('btnExportJSON').disabled = true;
document.getElementById('btnExportBin').disabled = true;
document.getElementById('packetDetail').style.display = 'none';
document.getElementById('globalStats').style.display = 'none';
const stats = document.getElementById('statsInput');
stats.style.display = 'block';
stats.innerHTML = `
<strong>TS chargé :</strong><br>
• Paquets : ${tsPackets.length}<br>
• Super-trames : ${Math.ceil(tsPackets.length / SUPER_FRAME_SIZE)}<br>
• Taille : ${tsBinary.length.toLocaleString()} octets
`;
} catch (err) {
alert('Erreur JSON : ' + err.message);
}
};
reader.readAsText(files[0]);
}
// ============================================================
// AFFICHAGE TS ENTRÉE
// ============================================================
function displayInputTS() {
const canvas = document.getElementById('canvasInput');
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#1a1a2e';
ctx.fillRect(0, 0, WIDTH, HEIGHT);
ctx.font = 'bold 12px Arial';
ctx.fillStyle = '#00d4ff';
ctx.textAlign = 'center';
ctx.fillText(`${tsPackets.length} paquets TS`, WIDTH / 2, 20);
// Afficher les paquets groupés par super-trames
const packetsPerRow = 8; // 1 super-trame par ligne
const packetWidth = Math.floor((WIDTH - 20) / packetsPerRow);
const packetHeight = 15;
const startY = 35;
for (let i = 0; i < tsPackets.length; i++) {
const superFrame = Math.floor(i / SUPER_FRAME_SIZE);
const posInSuperFrame = i % SUPER_FRAME_SIZE;
const x = 10 + posInSuperFrame * packetWidth;
const y = startY + superFrame * (packetHeight + 8);
if (y + packetHeight > HEIGHT - 20) break;
// Couleur selon position dans super-trame
if (posInSuperFrame === 0) {
ctx.fillStyle = '#ff6600'; // Premier paquet
} else {
ctx.fillStyle = '#0066aa';
}
ctx.fillRect(x, y, packetWidth - 2, packetHeight);
// Numéro de super-trame
if (posInSuperFrame === 0) {
ctx.fillStyle = '#888';
ctx.font = '9px Arial';
ctx.textAlign = 'right';
ctx.fillText(`SF${superFrame}`, x - 2, y + packetHeight / 2 + 3);
}
}
ctx.fillStyle = '#888';
ctx.font = '10px Arial';
ctx.textAlign = 'left';
ctx.fillText('Orange = 1er paquet super-trame (sync inversé)', 10, HEIGHT - 10);
}
// ============================================================
// AFFICHAGE PROCESS
// ============================================================
function displayProcessInit() {
const canvas = document.getElementById('canvasProcess');
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#1a1a2e';
ctx.fillRect(0, 0, WIDTH, HEIGHT);
ctx.font = 'bold 14px Arial';
ctx.fillStyle = '#00d4ff';
ctx.textAlign = 'center';
ctx.fillText('Générateur PRBS', WIDTH / 2, 25);
// Dessiner le registre
drawRegister(ctx, PRBS_INIT, 50);
// Explication
ctx.font = '11px Arial';
ctx.textAlign = 'left';
ctx.fillStyle = '#fff';
let y = 130;
ctx.fillText('Fonctionnement:', 20, y);
ctx.fillStyle = '#888';
ctx.fillText('1. Init registre = 0x4A80', 20, y + 18);
ctx.fillText('2. Sortie = bit 15', 20, y + 33);
ctx.fillText('3. Feedback = bit14 XOR bit15', 20, y + 48);
ctx.fillText('4. Décalage à droite', 20, y + 63);
ctx.fillText('5. XOR avec donnée TS', 20, y + 78);
y += 100;
ctx.fillStyle = '#fff';
ctx.fillText('Super-trame (8 paquets):', 20, y);
ctx.fillStyle = '#888';
ctx.fillText('• Paquet 1: sync 0x47→0xB8, reset PRBS', 20, y + 15);
ctx.fillText('• Paquets 2-8: sync inchangé', 20, y + 30);
}
function drawRegister(ctx, value, y) {
const bitWidth = 20;
const startX = (WIDTH - 15 * bitWidth) / 2;
ctx.font = '10px monospace';
for (let i = 14; i >= 0; i--) {
const bit = (value >> i) & 1;
const x = startX + (14 - i) * bitWidth;
// Case
ctx.fillStyle = bit ? '#00aa00' : '#333';
ctx.fillRect(x, y, bitWidth - 2, 25);
ctx.strokeStyle = '#666';
ctx.strokeRect(x, y, bitWidth - 2, 25);
// Bit
ctx.fillStyle = bit ? '#fff' : '#666';
ctx.textAlign = 'center';
ctx.fillText(bit.toString(), x + bitWidth / 2 - 1, y + 16);
// Numéro
ctx.fillStyle = '#888';
ctx.font = '8px monospace';
ctx.fillText((15 - i).toString(), x + bitWidth / 2 - 1, y + 35);
ctx.font = '10px monospace';
}
// Valeur hex
ctx.fillStyle = '#00d4ff';
ctx.font = '12px monospace';
ctx.textAlign = 'center';
ctx.fillText(`0x${value.toString(16).toUpperCase().padStart(4, '0')}`, WIDTH / 2, y + 55);
// Marqueurs feedback
ctx.fillStyle = '#ff8800';
ctx.font = '9px Arial';
const x14 = startX + 0 * bitWidth + bitWidth / 2;
const x15 = startX + 1 * bitWidth + bitWidth / 2;
ctx.fillText('↓XOR', (x14 + x15) / 2, y - 5);
}
// ============================================================
// RANDOMISATION PRBS
// ============================================================
function randomize() {
if (!tsPackets.length) return;
randomizedPackets = [];
prbsSequences = [];
let prbsRegister = PRBS_INIT;
for (let i = 0; i < tsPackets.length; i++) {
const posInSuperFrame = i % SUPER_FRAME_SIZE;
const isFirstInSuperFrame = posInSuperFrame === 0;
// Reset PRBS au début de chaque super-trame
if (isFirstInSuperFrame) {
prbsRegister = PRBS_INIT;
}
const originalBytes = tsPackets[i].bytes;
const randomizedBytes = new Array(TS_PACKET_SIZE);
const prbsBytes = new Array(TS_PACKET_SIZE);
// Sync byte
if (isFirstInSuperFrame) {
randomizedBytes[0] = SYNC_BYTE_INVERTED; // 0xB8
} else {
randomizedBytes[0] = SYNC_BYTE; // 0x47
}
prbsBytes[0] = 0x00; // Sync non randomisé
// Générer 8 bits PRBS pour le sync (mais ne pas les utiliser)
// On fait tourner le registre quand même pour la synchro
for (let bit = 0; bit < 8; bit++) {
prbsRegister = prbsStep(prbsRegister);
}
// Randomiser les 187 octets suivants
for (let j = 1; j < TS_PACKET_SIZE; j++) {
let prbsByte = 0;
for (let bit = 7; bit >= 0; bit--) {
const prbsBit = (prbsRegister >> 14) & 1; // Bit de sortie
prbsByte |= (prbsBit << bit);
prbsRegister = prbsStep(prbsRegister);
}
prbsBytes[j] = prbsByte;
randomizedBytes[j] = originalBytes[j] ^ prbsByte;
}
randomizedPackets.push({
index: i,
bytes: randomizedBytes,
isFirstInSuperFrame: isFirstInSuperFrame,
superFrame: Math.floor(i / SUPER_FRAME_SIZE)
});
prbsSequences.push(prbsBytes);
}
randomized = true;
displayOutputRandomized();
document.getElementById('btnExportJSON').disabled = false;
document.getElementById('btnExportBin').disabled = false;
// Stats
const globalStats = document.getElementById('globalStats');
globalStats.style.display = 'block';
// Calculer la dispersion
let zerosBeforeTotal = 0, zerosAfterTotal = 0;
let onesBeforeTotal = 0, onesAfterTotal = 0;
for (let i = 0; i < tsPackets.length; i++) {
for (let j = 0; j < TS_PACKET_SIZE; j++) {
const before = tsPackets[i].bytes[j];
const after = randomizedPackets[i].bytes[j];
for (let b = 0; b < 8; b++) {
if ((before >> b) & 1) onesBeforeTotal++; else zerosBeforeTotal++;
if ((after >> b) & 1) onesAfterTotal++; else zerosAfterTotal++;
}
}
}
const totalBits = tsPackets.length * TS_PACKET_SIZE * 8;
globalStats.innerHTML = `
<strong>Randomisation terminée :</strong><br>
• Paquets traités : ${tsPackets.length}<br>
• Super-trames : ${Math.ceil(tsPackets.length / SUPER_FRAME_SIZE)}<br>
• Syncs inversés (0xB8) : ${Math.ceil(tsPackets.length / SUPER_FRAME_SIZE)}<br><br>
<strong>Dispersion d'énergie :</strong><br>
• Avant: ${((onesBeforeTotal / totalBits) * 100).toFixed(1)}% de 1, ${((zerosBeforeTotal / totalBits) * 100).toFixed(1)}% de 0<br>
• Après: ${((onesAfterTotal / totalBits) * 100).toFixed(1)}% de 1, ${((zerosAfterTotal / totalBits) * 100).toFixed(1)}% de 0
`;
}
function prbsStep(register) {
// Polynôme 1 + X^14 + X^15
// Feedback = bit14 XOR bit15
const bit14 = (register >> 13) & 1;
const bit15 = (register >> 14) & 1;
const feedback = bit14 ^ bit15;
// Décalage à droite, nouveau bit à gauche
return ((register << 1) | feedback) & 0x7FFF;
}
// ============================================================
// ANIMATION PRBS
// ============================================================
function animatePRBS() {
if (animationId) return;
let step = 0;
let register = PRBS_INIT;
document.getElementById('btnAnimate').disabled = true;
document.getElementById('btnStop').disabled = false;
function animate() {
const canvas = document.getElementById('canvasProcess');
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#1a1a2e';
ctx.fillRect(0, 0, WIDTH, HEIGHT);
ctx.font = 'bold 14px Arial';
ctx.fillStyle = '#00d4ff';
ctx.textAlign = 'center';
ctx.fillText(`PRBS Step ${step}`, WIDTH / 2, 25);
// Dessiner le registre actuel
drawRegister(ctx, register, 50);
// Afficher le bit de sortie
const outputBit = (register >> 14) & 1;
ctx.fillStyle = outputBit ? '#00ff00' : '#666';
ctx.font = 'bold 20px monospace';
ctx.fillText(`Sortie: ${outputBit}`, WIDTH / 2, 130);
// Feedback
const bit14 = (register >> 13) & 1;
const bit15 = (register >> 14) & 1;
ctx.fillStyle = '#ff8800';
ctx.font = '12px monospace';
ctx.fillText(`Feedback: ${bit14} XOR ${bit15} = ${bit14 ^ bit15}`, WIDTH / 2, 155);
// Historique des bits
ctx.fillStyle = '#888';
ctx.font = '10px Arial';
ctx.textAlign = 'left';
ctx.fillText('Séquence générée:', 20, 180);
// Faire le pas
register = prbsStep(register);
step++;
if (step < 100) {
animationId = setTimeout(animate, 100);
} else {
stopAnimation();
randomize();
}
}
animate();
}
function stopAnimation() {
if (animationId) {
clearTimeout(animationId);
animationId = null;
}
document.getElementById('btnAnimate').disabled = false;
document.getElementById('btnStop').disabled = true;
}
// ============================================================
// AFFICHAGE TS RANDOMISÉ
// ============================================================
function displayOutputRandomized() {
const canvas = document.getElementById('canvasOutput');
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#1a1a2e';
ctx.fillRect(0, 0, WIDTH, HEIGHT);
ctx.font = 'bold 12px Arial';
ctx.fillStyle = '#00d4ff';
ctx.textAlign = 'center';
ctx.fillText('TS Randomisé', WIDTH / 2, 20);
// Afficher les paquets
const packetsPerRow = 8;
const packetWidth = Math.floor((WIDTH - 20) / packetsPerRow);
const packetHeight = 15;
const startY = 35;
for (let i = 0; i < randomizedPackets.length; i++) {
const packet = randomizedPackets[i];
const superFrame = packet.superFrame;
const posInSuperFrame = i % SUPER_FRAME_SIZE;
const x = 10 + posInSuperFrame * packetWidth;
const y = startY + superFrame * (packetHeight + 8);
if (y + packetHeight > HEIGHT - 20) break;
// Couleur selon sync
if (packet.isFirstInSuperFrame) {
ctx.fillStyle = '#ff00ff'; // Sync inversé
} else {
ctx.fillStyle = '#00aaaa'; // Randomisé
}
ctx.fillRect(x, y, packetWidth - 2, packetHeight);
// Sélection
if (selectedPacket === i) {
ctx.strokeStyle = '#fff';
ctx.lineWidth = 2;
ctx.strokeRect(x, y, packetWidth - 2, packetHeight);
}
// Stocker pour clic
packet.displayX = x;
packet.displayY = y;
packet.displayWidth = packetWidth;
packet.displayHeight = packetHeight;
}
ctx.fillStyle = '#888';
ctx.font = '10px Arial';
ctx.textAlign = 'left';
ctx.fillText('Magenta = sync 0xB8 | Cyan = données XOR PRBS', 10, HEIGHT - 10);
}
// ============================================================
// CLIC SUR PAQUET
// ============================================================
document.getElementById('canvasOutput').addEventListener('click', function(e) {
if (!randomized) return;
const rect = this.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
for (let i = 0; i < randomizedPackets.length; i++) {
const packet = randomizedPackets[i];
if (x >= packet.displayX && x < packet.displayX + packet.displayWidth &&
y >= packet.displayY && y < packet.displayY + packet.displayHeight) {
selectedPacket = i;
displayOutputRandomized();
displayPacketComparison(i);
return;
}
}
});
function displayPacketComparison(index) {
const detailDiv = document.getElementById('packetDetail');
detailDiv.style.display = 'block';
const original = tsPackets[index].bytes;
const randomizedBytes = randomizedPackets[index].bytes;
const prbs = prbsSequences[index];
const isFirst = randomizedPackets[index].isFirstInSuperFrame;
document.getElementById('detailTitle').textContent =
`Paquet #${index} — Super-trame ${randomizedPackets[index].superFrame} ` +
`(${isFirst ? '1er paquet, sync inversé' : 'continuation'})`;
// Avant
const beforeDiv = document.getElementById('detailBefore');
let beforeHtml = '';
for (let i = 0; i < Math.min(48, TS_PACKET_SIZE); i++) {
const hex = original[i].toString(16).padStart(2, '0').toUpperCase();
if (i === 0) {
beforeHtml += `<span class="sync">${hex}</span> `;
} else {
beforeHtml += hex + ' ';
}
if ((i + 1) % 16 === 0) beforeHtml += '\n';
}
if (TS_PACKET_SIZE > 48) beforeHtml += '...';
beforeDiv.innerHTML = beforeHtml;
// Après
const afterDiv = document.getElementById('detailAfter');
let afterHtml = '';
for (let i = 0; i < Math.min(48, TS_PACKET_SIZE); i++) {
const hex = randomizedBytes[i].toString(16).padStart(2, '0').toUpperCase();
if (i === 0) {
afterHtml += `<span class="${isFirst ? 'sync-inv' : 'sync'}">${hex}</span> `;
} else {
afterHtml += `<span class="data">${hex}</span> `;
}
if ((i + 1) % 16 === 0) afterHtml += '\n';
}
if (TS_PACKET_SIZE > 48) afterHtml += '...';
afterDiv.innerHTML = afterHtml;
// PRBS
const prbsDiv = document.getElementById('detailPRBS');
let prbsHtml = '';
for (let i = 0; i < Math.min(48, TS_PACKET_SIZE); i++) {
const hex = prbs[i].toString(16).padStart(2, '0').toUpperCase();
prbsHtml += hex + ' ';
if ((i + 1) % 16 === 0) prbsHtml += '\n';
}
if (TS_PACKET_SIZE > 48) prbsHtml += '...';
prbsDiv.innerHTML = prbsHtml;
// Stats
document.getElementById('detailStats').innerHTML =
`Original[1] XOR PRBS[1] = Randomisé[1] : ` +
`0x${original[1].toString(16).toUpperCase().padStart(2, '0')} XOR ` +
`0x${prbs[1].toString(16).toUpperCase().padStart(2, '0')} = ` +
`0x${randomizedBytes[1].toString(16).toUpperCase().padStart(2, '0')}`;
}
// ============================================================
// EXPORT
// ============================================================
function exportJSON() {
if (!randomized) return;
let tsBinary = [];
for (const packet of randomizedPackets) {
tsBinary.push(...packet.bytes);
}
const output = {
format: 'DVB_RANDOMIZED_V1',
description: 'DVB-S Randomized Transport Stream',
date: new Date().toISOString(),
source: inputData.format,
image: inputData.image,
randomization: {
polynomial: '1 + X^14 + X^15',
initValue: '0x4A80',
superFrameSize: SUPER_FRAME_SIZE,
superFrameCount: Math.ceil(randomizedPackets.length / SUPER_FRAME_SIZE),
packetCount: randomizedPackets.length,
totalBytes: tsBinary.length
},
ts: inputData.ts,
binary: btoa(String.fromCharCode.apply(null, tsBinary))
};
const json = JSON.stringify(output, null, 2);
const blob = new Blob([json], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'randomized_ts.json';
a.click();
URL.revokeObjectURL(url);
}
function exportBinary() {
if (!randomized) return;
let tsBinary = [];
for (const packet of randomizedPackets) {
tsBinary.push(...packet.bytes);
}
const blob = new Blob([new Uint8Array(tsBinary)], { type: 'application/octet-stream' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'randomized.ts';
a.click();
URL.revokeObjectURL(url);
}
// ============================================================
// UTILITAIRES
// ============================================================
function clearCanvas(canvasId) {
const canvas = document.getElementById(canvasId);
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#1a1a2e';
ctx.fillRect(0, 0, WIDTH, HEIGHT);
}
</script>
</body>
</html>