aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/bcm27xx/patches-5.10/950-0610-media-rpivid-Remove-the-need-to-have-num_entry_point.patch
blob: 7a8f2d3892fb9488d2f41d12ce4017e9dd5b6c80 (plain)
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
From 0962d9e4a57c3faa29000c63c1cb28ad39a6c80c Mon Sep 17 00:00:00 2001
From: John Cox <jc@kynesim.co.uk>
Date: Thu, 21 May 2020 11:49:37 +0100
Subject: [PATCH] media: rpivid: Remove the need to have
 num_entry_points set

VAAPI H265 has num entry points but never sets it. Allow a VAAPI
shim to work without requiring rewriting the VAAPI driver.
num_entry_points can be calculated from the slice_segment_addr
of the next slice so delay processing until we have that.

Also includes some minor cosmetics.

Signed-off-by: John Cox <jc@kynesim.co.uk>
---
 drivers/staging/media/rpivid/rpivid_h265.c | 699 +++++++++++----------
 1 file changed, 365 insertions(+), 334 deletions(-)

--- a/drivers/staging/media/rpivid/rpivid_h265.c
+++ b/drivers/staging/media/rpivid/rpivid_h265.c
@@ -202,8 +202,17 @@ struct rpivid_dec_env {
 	unsigned int dpbno_col;
 	u32 reg_slicestart;
 	int collocated_from_l0_flag;
-	unsigned int wpp_entry_x;
-	unsigned int wpp_entry_y;
+	/*
+	 * Last CTB/Tile X,Y processed by (wpp_)entry_point
+	 * Could be in _state as P0 only but needs updating where _state
+	 * is const
+	 */
+	unsigned int entry_ctb_x;
+	unsigned int entry_ctb_y;
+	unsigned int entry_tile_x;
+	unsigned int entry_tile_y;
+	unsigned int entry_qp;
+	u32 entry_slice;
 
 	u32 rpi_config2;
 	u32 rpi_framesize;
@@ -239,22 +248,17 @@ struct rpivid_dec_state {
 	struct v4l2_ctrl_hevc_pps pps;
 
 	// Helper vars & tables derived from sps/pps
-	unsigned int log2_ctb_size; /* log2 width of a CTB */
-	unsigned int ctb_width; /* Width in CTBs */
-	unsigned int ctb_height; /* Height in CTBs */
-	unsigned int ctb_size; /* Pic area in CTBs */
-	unsigned int num_tile_columns;
-	unsigned int num_tile_rows;
-	u8 column_width[member_size(struct v4l2_ctrl_hevc_pps,
-				    column_width_minus1)];
-	u8 row_height[member_size(struct v4l2_ctrl_hevc_pps,
-				  row_height_minus1)];
+	unsigned int log2_ctb_size;     /* log2 width of a CTB */
+	unsigned int ctb_width;         /* Width in CTBs */
+	unsigned int ctb_height;        /* Height in CTBs */
+	unsigned int ctb_size;          /* Pic area in CTBs */
+	unsigned int tile_width;        /* Width in tiles */
+	unsigned int tile_height;       /* Height in tiles */
 
 	int *col_bd;
 	int *row_bd;
 	int *ctb_addr_rs_to_ts;
 	int *ctb_addr_ts_to_rs;
-	int *tile_id;
 
 	// Aux starage for DPB
 	// Hold refs
@@ -274,6 +278,12 @@ struct rpivid_dec_state {
 	unsigned int slice_qp;
 	unsigned int max_num_merge_cand; // 0 if I-slice
 	bool dependent_slice_segment_flag;
+
+	unsigned int start_ts;          /* slice_segment_addr -> ts */
+	unsigned int start_ctb_x;       /* CTB X,Y of start_ts */
+	unsigned int start_ctb_y;
+	unsigned int prev_ctb_x;        /* CTB X,Y of start_ts - 1 */
+	unsigned int prev_ctb_y;
 };
 
 static inline int clip_int(const int x, const int lo, const int hi)
@@ -319,15 +329,16 @@ static int ctb_to_tile(unsigned int ctb,
 	return i - 1;
 }
 
-static int ctb_to_slice_w_h(unsigned int ctb, int ctb_size, int width,
-			    unsigned int *bd, int num)
+static unsigned int ctb_to_tile_x(const struct rpivid_dec_state *const s,
+				  const unsigned int ctb_x)
 {
-	if (ctb < bd[num - 1])
-		return ctb_size;
-	else if (width % ctb_size)
-		return width % ctb_size;
-	else
-		return ctb_size;
+	return ctb_to_tile(ctb_x, s->col_bd, s->tile_width);
+}
+
+static unsigned int ctb_to_tile_y(const struct rpivid_dec_state *const s,
+				  const unsigned int ctb_y)
+{
+	return ctb_to_tile(ctb_y, s->row_bd, s->tile_height);
 }
 
 static void aux_q_free(struct rpivid_ctx *const ctx,
@@ -532,6 +543,15 @@ static void write_prob(struct rpivid_dec
 		p1_apb_write(de, 0x1000 + i,
 			     dst[i] + (dst[i + 1] << 8) + (dst[i + 2] << 16) +
 				     (dst[i + 3] << 24));
+
+	/*
+	 * Having written the prob array back it up
+	 * This is not always needed but is a small overhead that simplifies
+	 * (and speeds up) some multi-tile & WPP scenarios
+	 * There are no scenarios where having written a prob we ever want
+	 * a previous (non-initial) state back
+	 */
+	p1_apb_write(de, RPI_TRANSFER, PROB_BACKUP);
 }
 
 static void write_scaling_factors(struct rpivid_dec_env *const de)
@@ -552,8 +572,8 @@ static inline __u32 dma_to_axi_addr(dma_
 static void write_bitstream(struct rpivid_dec_env *const de,
 			    const struct rpivid_dec_state *const s)
 {
-	// Note that FFmpeg removes emulation prevention bytes, so this is
-	// matched in the configuration here.
+	// Note that FFmpeg V4L2 does not remove emulation prevention bytes,
+	// so this is matched in the configuration here.
 	// Whether that is the correct behaviour or not is not clear in the
 	// spec.
 	const int rpi_use_emu = 1;
@@ -579,78 +599,26 @@ static void write_bitstream(struct rpivi
 
 //////////////////////////////////////////////////////////////////////////////
 
-static void write_slice(struct rpivid_dec_env *const de,
-			const struct rpivid_dec_state *const s,
-			const unsigned int slice_w,
-			const unsigned int slice_h)
-{
-	u32 u32 = (s->sh->slice_type << 12) +
-		  (((s->sh->flags &
-		     V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_SAO_LUMA) != 0)
-		   << 14) +
-		  (((s->sh->flags &
-		     V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_SAO_CHROMA) != 0)
-		   << 15) +
-		  (slice_w << 17) + (slice_h << 24);
-
-	u32 |= (s->max_num_merge_cand << 0) + (s->nb_refs[L0] << 4) +
-	       (s->nb_refs[L1] << 8);
-
-	if (s->sh->slice_type == HEVC_SLICE_B)
-		u32 |= ((s->sh->flags &
-			 V4L2_HEVC_SLICE_PARAMS_FLAG_MVD_L1_ZERO) != 0)
-		       << 16;
-	p1_apb_write(de, RPI_SLICE, u32);
-}
-
-//////////////////////////////////////////////////////////////////////////////
-// Tiles mode
-
-static void new_entry_point(struct rpivid_dec_env *const de,
-			    const struct rpivid_dec_state *const s,
-			    const int do_bte,
-			    const int reset_qp_y, const int ctb_addr_ts)
+/*
+ * The slice constant part of the slice register - width and height need to
+ * be ORed in later as they are per-tile / WPP-row
+ */
+static u32 slice_reg_const(const struct rpivid_dec_state *const s)
 {
-	int ctb_col = s->ctb_addr_ts_to_rs[ctb_addr_ts] %
-							de->pic_width_in_ctbs_y;
-	int ctb_row = s->ctb_addr_ts_to_rs[ctb_addr_ts] /
-							de->pic_width_in_ctbs_y;
-
-	int tile_x = ctb_to_tile(ctb_col, s->col_bd, s->num_tile_columns);
-	int tile_y = ctb_to_tile(ctb_row, s->row_bd, s->num_tile_rows);
-
-	int endx = s->col_bd[tile_x + 1] - 1;
-	int endy = s->row_bd[tile_y + 1] - 1;
-
-	u8 slice_w = ctb_to_slice_w_h(ctb_col, 1 << s->log2_ctb_size,
-				      s->sps.pic_width_in_luma_samples,
-				      s->col_bd, s->num_tile_columns);
-	u8 slice_h = ctb_to_slice_w_h(ctb_row, 1 << s->log2_ctb_size,
-				      s->sps.pic_height_in_luma_samples,
-				      s->row_bd, s->num_tile_rows);
-
-	p1_apb_write(de, RPI_TILESTART,
-		     s->col_bd[tile_x] + (s->row_bd[tile_y] << 16));
-	p1_apb_write(de, RPI_TILEEND, endx + (endy << 16));
-
-	if (do_bte)
-		p1_apb_write(de, RPI_BEGINTILEEND, endx + (endy << 16));
+	u32 x = (s->max_num_merge_cand << 0) |
+		(s->nb_refs[L0] << 4) |
+		(s->nb_refs[L1] << 8) |
+		(s->sh->slice_type << 12);
+
+	if (s->sh->flags & V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_SAO_LUMA)
+		x |= BIT(14);
+	if (s->sh->flags & V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_SAO_CHROMA)
+		x |= BIT(15);
+	if (s->sh->slice_type == HEVC_SLICE_B &&
+	    (s->sh->flags & V4L2_HEVC_SLICE_PARAMS_FLAG_MVD_L1_ZERO))
+		x |= BIT(16);
 
-	write_slice(de, s, slice_w, slice_h);
-
-	if (reset_qp_y) {
-		unsigned int sps_qp_bd_offset =
-			6 * s->sps.bit_depth_luma_minus8;
-
-		p1_apb_write(de, RPI_QP, sps_qp_bd_offset + s->slice_qp);
-	}
-
-	p1_apb_write(de, RPI_MODE,
-		     (0xFFFF << 0) + (0x0 << 16) +
-			     ((tile_x == s->num_tile_columns - 1) << 17) +
-			     ((tile_y == s->num_tile_rows - 1) << 18));
-
-	p1_apb_write(de, RPI_CONTROL, (ctb_col << 0) + (ctb_row << 16));
+	return x;
 }
 
 //////////////////////////////////////////////////////////////////////////////
@@ -934,197 +902,256 @@ static void pre_slice_decode(struct rpiv
 		       (sh->slice_cb_qp_offset & 31)); // CMD_QPOFF
 }
 
-//////////////////////////////////////////////////////////////////////////////
-// Write STATUS register with expected end CTU address of previous slice
-
-static void end_previous_slice(struct rpivid_dec_env *const de,
-			       const struct rpivid_dec_state *const s,
-			       const int ctb_addr_ts)
-{
-	int last_x =
-		s->ctb_addr_ts_to_rs[ctb_addr_ts - 1] % de->pic_width_in_ctbs_y;
-	int last_y =
-		s->ctb_addr_ts_to_rs[ctb_addr_ts - 1] / de->pic_width_in_ctbs_y;
-
-	p1_apb_write(de, RPI_STATUS, 1 + (last_x << 5) + (last_y << 18));
-}
-
-static void wpp_pause(struct rpivid_dec_env *const de, int ctb_row)
-{
-	p1_apb_write(de, RPI_STATUS, (ctb_row << 18) + 0x25);
-	p1_apb_write(de, RPI_TRANSFER, PROB_BACKUP);
-	p1_apb_write(de, RPI_MODE,
-		     ctb_row == de->pic_height_in_ctbs_y - 1 ?
-							0x70000 : 0x30000);
-	p1_apb_write(de, RPI_CONTROL, (ctb_row << 16) + 2);
-}
-
-static void wpp_end_previous_slice(struct rpivid_dec_env *const de,
-				   const struct rpivid_dec_state *const s,
-				   int ctb_addr_ts)
-{
-	int new_x = s->sh->slice_segment_addr % de->pic_width_in_ctbs_y;
-	int new_y = s->sh->slice_segment_addr / de->pic_width_in_ctbs_y;
-	int last_x =
-		s->ctb_addr_ts_to_rs[ctb_addr_ts - 1] % de->pic_width_in_ctbs_y;
-	int last_y =
-		s->ctb_addr_ts_to_rs[ctb_addr_ts - 1] / de->pic_width_in_ctbs_y;
-
-	if (de->wpp_entry_x < 2 && (de->wpp_entry_y < new_y || new_x > 2) &&
-	    de->pic_width_in_ctbs_y > 2)
-		wpp_pause(de, last_y);
-	p1_apb_write(de, RPI_STATUS, 1 + (last_x << 5) + (last_y << 18));
-	if (new_x == 2 || (de->pic_width_in_ctbs_y == 2 &&
-			   de->wpp_entry_y < new_y))
-		p1_apb_write(de, RPI_TRANSFER, PROB_BACKUP);
+static void write_slice(struct rpivid_dec_env *const de,
+			const struct rpivid_dec_state *const s,
+			const u32 slice_const,
+			const unsigned int ctb_col,
+			const unsigned int ctb_row)
+{
+	const unsigned int cs = (1 << s->log2_ctb_size);
+	const unsigned int w_last = s->sps.pic_width_in_luma_samples & (cs - 1);
+	const unsigned int h_last = s->sps.pic_height_in_luma_samples & (cs - 1);
+
+	p1_apb_write(de, RPI_SLICE,
+		     slice_const |
+		     ((ctb_col + 1 < s->ctb_width || !w_last ?
+				cs : w_last) << 17) |
+		     ((ctb_row + 1 < s->ctb_height || !h_last ?
+				cs : h_last) << 24));
 }
 
-//////////////////////////////////////////////////////////////////////////////
-// Wavefront mode
+#define PAUSE_MODE_WPP  1
+#define PAUSE_MODE_TILE 0xffff
 
-static void wpp_entry_point(struct rpivid_dec_env *const de,
+/*
+ * N.B. This can be called to fill in data from the previous slice so must not
+ * use any state data that may change from slice to slice (e.g. qp)
+ */
+static void new_entry_point(struct rpivid_dec_env *const de,
 			    const struct rpivid_dec_state *const s,
-			    const int do_bte,
-			    const int reset_qp_y, const int ctb_addr_ts)
-{
-	int ctb_size = 1 << s->log2_ctb_size;
-	int ctb_addr_rs = s->ctb_addr_ts_to_rs[ctb_addr_ts];
-
-	int ctb_col = de->wpp_entry_x = ctb_addr_rs % de->pic_width_in_ctbs_y;
-	int ctb_row = de->wpp_entry_y = ctb_addr_rs / de->pic_width_in_ctbs_y;
+			    const bool do_bte,
+			    const bool reset_qp_y,
+			    const u32 pause_mode,
+			    const unsigned int tile_x,
+			    const unsigned int tile_y,
+			    const unsigned int ctb_col,
+			    const unsigned int ctb_row,
+			    const unsigned int slice_qp,
+			    const u32 slice_const)
+{
+	const unsigned int endx = s->col_bd[tile_x + 1] - 1;
+	const unsigned int endy = (pause_mode == PAUSE_MODE_WPP) ?
+		ctb_row : s->row_bd[tile_y + 1] - 1;
 
-	int endx = de->pic_width_in_ctbs_y - 1;
-	int endy = ctb_row;
-
-	u8 slice_w = ctb_to_slice_w_h(ctb_col, ctb_size,
-				      s->sps.pic_width_in_luma_samples,
-				      s->col_bd, s->num_tile_columns);
-	u8 slice_h = ctb_to_slice_w_h(ctb_row, ctb_size,
-				      s->sps.pic_height_in_luma_samples,
-				      s->row_bd, s->num_tile_rows);
-
-	p1_apb_write(de, RPI_TILESTART, 0);
-	p1_apb_write(de, RPI_TILEEND, endx + (endy << 16));
+	p1_apb_write(de, RPI_TILESTART,
+		     s->col_bd[tile_x] | (s->row_bd[tile_y] << 16));
+	p1_apb_write(de, RPI_TILEEND, endx | (endy << 16));
 
 	if (do_bte)
-		p1_apb_write(de, RPI_BEGINTILEEND, endx + (endy << 16));
+		p1_apb_write(de, RPI_BEGINTILEEND, endx | (endy << 16));
 
-	write_slice(de, s, slice_w,
-		    ctb_row == de->pic_height_in_ctbs_y - 1 ?
-							slice_h : ctb_size);
+	write_slice(de, s, slice_const, endx, endy);
 
 	if (reset_qp_y) {
 		unsigned int sps_qp_bd_offset =
 			6 * s->sps.bit_depth_luma_minus8;
 
-		p1_apb_write(de, RPI_QP, sps_qp_bd_offset + s->slice_qp);
+		p1_apb_write(de, RPI_QP, sps_qp_bd_offset + slice_qp);
 	}
 
 	p1_apb_write(de, RPI_MODE,
-		     ctb_row == de->pic_height_in_ctbs_y - 1 ?
-							0x60001 : 0x20001);
-	p1_apb_write(de, RPI_CONTROL, (ctb_col << 0) + (ctb_row << 16));
+		     pause_mode |
+			((endx == s->ctb_width - 1) << 17) |
+			((endy == s->ctb_height - 1) << 18));
+
+	p1_apb_write(de, RPI_CONTROL, (ctb_col << 0) | (ctb_row << 16));
+
+	de->entry_tile_x = tile_x;
+	de->entry_tile_y = tile_y;
+	de->entry_ctb_x = ctb_col;
+	de->entry_ctb_y = ctb_row;
+	de->entry_qp = slice_qp;
+	de->entry_slice = slice_const;
 }
 
 //////////////////////////////////////////////////////////////////////////////
 // Wavefront mode
 
+static void wpp_pause(struct rpivid_dec_env *const de, int ctb_row)
+{
+	p1_apb_write(de, RPI_STATUS, (ctb_row << 18) | 0x25);
+	p1_apb_write(de, RPI_TRANSFER, PROB_BACKUP);
+	p1_apb_write(de, RPI_MODE,
+		     ctb_row == de->pic_height_in_ctbs_y - 1 ?
+							0x70000 : 0x30000);
+	p1_apb_write(de, RPI_CONTROL, (ctb_row << 16) + 2);
+}
+
+static void wpp_entry_fill(struct rpivid_dec_env *const de,
+			   const struct rpivid_dec_state *const s,
+			   const unsigned int last_y)
+{
+	const unsigned int last_x = s->ctb_width - 1;
+
+	while (de->entry_ctb_y < last_y) {
+		/* wpp_entry_x/y set by wpp_entry_point */
+		if (s->ctb_width > 2)
+			wpp_pause(de, de->entry_ctb_y);
+		p1_apb_write(de, RPI_STATUS,
+			     (de->entry_ctb_y << 18) | (last_x << 5) | 2);
+
+		/* if width == 1 then the saved state is the init one */
+		if (s->ctb_width == 2)
+			p1_apb_write(de, RPI_TRANSFER, PROB_BACKUP);
+		else
+			p1_apb_write(de, RPI_TRANSFER, PROB_RELOAD);
+
+		new_entry_point(de, s, false, true, PAUSE_MODE_WPP,
+				0, 0, 0, de->entry_ctb_y + 1,
+				de->entry_qp, de->entry_slice);
+	}
+}
+
+static void wpp_end_previous_slice(struct rpivid_dec_env *const de,
+				   const struct rpivid_dec_state *const s)
+{
+	wpp_entry_fill(de, s, s->prev_ctb_y);
+
+	if (de->entry_ctb_x < 2 &&
+	    (de->entry_ctb_y < s->start_ctb_y || s->start_ctb_x > 2) &&
+	    s->ctb_width > 2)
+		wpp_pause(de, s->prev_ctb_y);
+	p1_apb_write(de, RPI_STATUS,
+		     1 | (s->prev_ctb_x << 5) | (s->prev_ctb_y << 18));
+	if (s->start_ctb_x == 2 ||
+	    (s->ctb_width == 2 && de->entry_ctb_y < s->start_ctb_y))
+		p1_apb_write(de, RPI_TRANSFER, PROB_BACKUP);
+}
+
+/* Only main profile supported so WPP => !Tiles which makes some of the
+ * next chunk code simpler
+ */
 static void wpp_decode_slice(struct rpivid_dec_env *const de,
-			     const struct rpivid_dec_state *const s,
-			     const struct v4l2_ctrl_hevc_slice_params *sh,
-			     int ctb_addr_ts)
-{
-	int i, reset_qp_y = 1;
-	int indep = !s->dependent_slice_segment_flag;
-	int ctb_col = s->sh->slice_segment_addr % de->pic_width_in_ctbs_y;
+			     const struct rpivid_dec_state *const s)
+{
+	bool reset_qp_y = true;
+	const bool indep = !s->dependent_slice_segment_flag;
 
-	if (ctb_addr_ts)
-		wpp_end_previous_slice(de, s, ctb_addr_ts);
+	if (s->start_ts)
+		wpp_end_previous_slice(de, s);
 	pre_slice_decode(de, s);
 	write_bitstream(de, s);
-	if (ctb_addr_ts == 0 || indep || de->pic_width_in_ctbs_y == 1)
+
+	if (!s->start_ts || indep || s->ctb_width == 1)
 		write_prob(de, s);
-	else if (ctb_col == 0)
+	else if (!s->start_ctb_x)
 		p1_apb_write(de, RPI_TRANSFER, PROB_RELOAD);
 	else
-		reset_qp_y = 0;
+		reset_qp_y = false;
+
 	program_slicecmds(de, s->slice_idx);
 	new_slice_segment(de, s);
-	wpp_entry_point(de, s, indep, reset_qp_y, ctb_addr_ts);
+	new_entry_point(de, s, indep, reset_qp_y, PAUSE_MODE_WPP,
+			0, 0, s->start_ctb_x, s->start_ctb_y,
+			s->slice_qp, slice_reg_const(s));
 
-	for (i = 0; i < s->sh->num_entry_point_offsets; i++) {
-		int ctb_addr_rs = s->ctb_addr_ts_to_rs[ctb_addr_ts];
-		int ctb_row = ctb_addr_rs / de->pic_width_in_ctbs_y;
-		int last_x = de->pic_width_in_ctbs_y - 1;
+	if (s->frame_end) {
+		wpp_entry_fill(de, s, s->ctb_height - 1);
+
+		if (de->entry_ctb_x < 2 && s->ctb_width > 2)
+			wpp_pause(de, s->ctb_height - 1);
 
-		if (de->pic_width_in_ctbs_y > 2)
-			wpp_pause(de, ctb_row);
 		p1_apb_write(de, RPI_STATUS,
-			     (ctb_row << 18) + (last_x << 5) + 2);
-		if (de->pic_width_in_ctbs_y == 2)
-			p1_apb_write(de, RPI_TRANSFER, PROB_BACKUP);
-		if (de->pic_width_in_ctbs_y == 1)
-			write_prob(de, s);
-		else
-			p1_apb_write(de, RPI_TRANSFER, PROB_RELOAD);
-		ctb_addr_ts += s->column_width[0];
-		wpp_entry_point(de, s, 0, 1, ctb_addr_ts);
+			     1 | ((s->ctb_width - 1) << 5) |
+				((s->ctb_height - 1) << 18));
 	}
+
 }
 
 //////////////////////////////////////////////////////////////////////////////
 // Tiles mode
 
+static void tile_entry_fill(struct rpivid_dec_env *const de,
+			    const struct rpivid_dec_state *const s,
+			    const unsigned int last_tile_x,
+			    const unsigned int last_tile_y)
+{
+	while (de->entry_tile_y < last_tile_y ||
+	       (de->entry_tile_y == last_tile_y &&
+		de->entry_tile_x < last_tile_x)) {
+		unsigned int t_x = de->entry_tile_x;
+		unsigned int t_y = de->entry_tile_y;
+		const unsigned int last_x = s->col_bd[t_x + 1] - 1;
+		const unsigned int last_y = s->row_bd[t_y + 1] - 1;
+
+		p1_apb_write(de, RPI_STATUS,
+			     2 | (last_x << 5) | (last_y << 18));
+		p1_apb_write(de, RPI_TRANSFER, PROB_RELOAD);
+
+		// Inc tile
+		if (++t_x >= s->tile_width) {
+			t_x = 0;
+			++t_y;
+		}
+
+		new_entry_point(de, s, false, true, PAUSE_MODE_TILE,
+				t_x, t_y, s->col_bd[t_x], s->row_bd[t_y],
+				de->entry_qp, de->entry_slice);
+	}
+}
+
+/*
+ * Write STATUS register with expected end CTU address of previous slice
+ */
+static void end_previous_slice(struct rpivid_dec_env *const de,
+			       const struct rpivid_dec_state *const s)
+{
+	tile_entry_fill(de, s,
+			ctb_to_tile_x(s, s->prev_ctb_x),
+			ctb_to_tile_y(s, s->prev_ctb_y));
+	p1_apb_write(de, RPI_STATUS,
+		     1 | (s->prev_ctb_x << 5) | (s->prev_ctb_y << 18));
+}
+
 static void decode_slice(struct rpivid_dec_env *const de,
-			 const struct rpivid_dec_state *const s,
-			 const struct v4l2_ctrl_hevc_slice_params *const sh,
-			 int ctb_addr_ts)
+			 const struct rpivid_dec_state *const s)
 {
-	int i, reset_qp_y;
+	bool reset_qp_y;
+	unsigned int tile_x = ctb_to_tile_x(s, s->start_ctb_x);
+	unsigned int tile_y = ctb_to_tile_y(s, s->start_ctb_y);
 
-	if (ctb_addr_ts)
-		end_previous_slice(de, s, ctb_addr_ts);
+	if (s->start_ts)
+		end_previous_slice(de, s);
 
 	pre_slice_decode(de, s);
 	write_bitstream(de, s);
 
-#if DEBUG_TRACE_P1_CMD
-	if (p1_z < 256) {
-		v4l2_info(&de->ctx->dev->v4l2_dev,
-			  "TS=%d, tile=%d/%d, dss=%d, flags=%#llx\n",
-			  ctb_addr_ts, s->tile_id[ctb_addr_ts],
-			  s->tile_id[ctb_addr_ts - 1],
-			  s->dependent_slice_segment_flag, sh->flags);
-	}
-#endif
-
-	reset_qp_y = ctb_addr_ts == 0 ||
-		   s->tile_id[ctb_addr_ts] != s->tile_id[ctb_addr_ts - 1] ||
-		   !s->dependent_slice_segment_flag;
+	reset_qp_y = !s->start_ts ||
+		!s->dependent_slice_segment_flag ||
+		tile_x != ctb_to_tile_x(s, s->prev_ctb_x) ||
+		tile_y != ctb_to_tile_y(s, s->prev_ctb_y);
 	if (reset_qp_y)
 		write_prob(de, s);
 
 	program_slicecmds(de, s->slice_idx);
 	new_slice_segment(de, s);
 	new_entry_point(de, s, !s->dependent_slice_segment_flag, reset_qp_y,
-			ctb_addr_ts);
-
-	for (i = 0; i < s->sh->num_entry_point_offsets; i++) {
-		int ctb_addr_rs = s->ctb_addr_ts_to_rs[ctb_addr_ts];
-		int ctb_col = ctb_addr_rs % de->pic_width_in_ctbs_y;
-		int ctb_row = ctb_addr_rs / de->pic_width_in_ctbs_y;
-		int tile_x = ctb_to_tile(ctb_col, s->col_bd,
-					 s->num_tile_columns - 1);
-		int tile_y =
-			ctb_to_tile(ctb_row, s->row_bd, s->num_tile_rows - 1);
-		int last_x = s->col_bd[tile_x + 1] - 1;
-		int last_y = s->row_bd[tile_y + 1] - 1;
+			PAUSE_MODE_TILE,
+			tile_x, tile_y, s->start_ctb_x, s->start_ctb_y,
+			s->slice_qp, slice_reg_const(s));
 
+	/*
+	 * If this is the last slice then fill in the other tile entries
+	 * now, otherwise this will be done at the start of the next slice
+	 * when it will be known where this slice finishes
+	 */
+	if (s->frame_end) {
+		tile_entry_fill(de, s,
+				s->tile_width - 1,
+				s->tile_height - 1);
 		p1_apb_write(de, RPI_STATUS,
-			     2 + (last_x << 5) + (last_y << 18));
-		write_prob(de, s);
-		ctb_addr_ts += s->column_width[tile_x] * s->row_height[tile_y];
-		new_entry_point(de, s, 0, 1, ctb_addr_ts);
+			     1 | ((s->ctb_width - 1) << 5) |
+				((s->ctb_height - 1) << 18));
 	}
 }
 
@@ -1132,13 +1159,12 @@ static void decode_slice(struct rpivid_d
 // Scaling factors
 
 static void expand_scaling_list(const unsigned int size_id,
-				const unsigned int matrix_id, u8 *const dst0,
+				u8 *const dst0,
 				const u8 *const src0, uint8_t dc)
 {
 	u8 *d;
 	unsigned int x, y;
 
-	// FIXME: matrix_id is unused ?
 	switch (size_id) {
 	case 0:
 		memcpy(dst0, src0, 16);
@@ -1199,24 +1225,20 @@ static void populate_scaling_factors(con
 	unsigned int mid;
 
 	for (mid = 0; mid < 6; mid++)
-		expand_scaling_list(0, mid,
-				    de->scaling_factors +
+		expand_scaling_list(0, de->scaling_factors +
 					    scaling_factor_offsets[0][mid],
 				    sl->scaling_list_4x4[mid], 0);
 	for (mid = 0; mid < 6; mid++)
-		expand_scaling_list(1, mid,
-				    de->scaling_factors +
+		expand_scaling_list(1, de->scaling_factors +
 					    scaling_factor_offsets[1][mid],
 				    sl->scaling_list_8x8[mid], 0);
 	for (mid = 0; mid < 6; mid++)
-		expand_scaling_list(2, mid,
-				    de->scaling_factors +
+		expand_scaling_list(2, de->scaling_factors +
 					    scaling_factor_offsets[2][mid],
 				    sl->scaling_list_16x16[mid],
 				    sl->scaling_list_dc_coef_16x16[mid]);
-	for (mid = 0; mid < 2; mid += 1)
-		expand_scaling_list(3, mid,
-				    de->scaling_factors +
+	for (mid = 0; mid < 2; mid++)
+		expand_scaling_list(3, de->scaling_factors +
 					    scaling_factor_offsets[3][mid],
 				    sl->scaling_list_32x32[mid],
 				    sl->scaling_list_dc_coef_32x32[mid]);
@@ -1228,8 +1250,6 @@ static void free_ps_info(struct rpivid_d
 	s->ctb_addr_rs_to_ts = NULL;
 	kfree(s->ctb_addr_ts_to_rs);
 	s->ctb_addr_ts_to_rs = NULL;
-	kfree(s->tile_id);
-	s->tile_id = NULL;
 
 	kfree(s->col_bd);
 	s->col_bd = NULL;
@@ -1237,10 +1257,52 @@ static void free_ps_info(struct rpivid_d
 	s->row_bd = NULL;
 }
 
+static unsigned int tile_width(const struct rpivid_dec_state *const s,
+			       const unsigned int t_x)
+{
+	return s->col_bd[t_x + 1] - s->col_bd[t_x];
+}
+
+static unsigned int tile_height(const struct rpivid_dec_state *const s,
+				const unsigned int t_y)
+{
+	return s->row_bd[t_y + 1] - s->row_bd[t_y];
+}
+
+static void fill_rs_to_ts(struct rpivid_dec_state *const s)
+{
+	unsigned int ts = 0;
+	unsigned int t_y;
+	unsigned int tr_rs = 0;
+
+	for (t_y = 0; t_y != s->tile_height; ++t_y) {
+		const unsigned int t_h = tile_height(s, t_y);
+		unsigned int t_x;
+		unsigned int tc_rs = tr_rs;
+
+		for (t_x = 0; t_x != s->tile_width; ++t_x) {
+			const unsigned int t_w = tile_width(s, t_x);
+			unsigned int y;
+			unsigned int rs = tc_rs;
+
+			for (y = 0; y != t_h; ++y) {
+				unsigned int x;
+
+				for (x = 0; x != t_w; ++x) {
+					s->ctb_addr_rs_to_ts[rs + x] = ts;
+					s->ctb_addr_ts_to_rs[ts] = rs + x;
+					++ts;
+				}
+				rs += s->ctb_width;
+			}
+			tc_rs += t_w;
+		}
+		tr_rs += t_h * s->ctb_width;
+	}
+}
+
 static int updated_ps(struct rpivid_dec_state *const s)
 {
-	unsigned int ctb_addr_rs;
-	int j, x, y, tile_id;
 	unsigned int i;
 
 	free_ps_info(s);
@@ -1259,104 +1321,49 @@ static int updated_ps(struct rpivid_dec_
 
 	// Inferred parameters
 
+	s->ctb_addr_rs_to_ts = kmalloc_array(s->ctb_size,
+					     sizeof(*s->ctb_addr_rs_to_ts),
+					     GFP_KERNEL);
+	s->ctb_addr_ts_to_rs = kmalloc_array(s->ctb_size,
+					     sizeof(*s->ctb_addr_ts_to_rs),
+					     GFP_KERNEL);
+
 	if (!(s->pps.flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED)) {
-		s->num_tile_columns = 1;
-		s->num_tile_rows = 1;
-		s->column_width[0] = s->ctb_width;
-		s->row_height[0] = s->ctb_height;
+		s->tile_width = 1;
+		s->tile_height = 1;
 	} else {
-		s->num_tile_columns = s->pps.num_tile_columns_minus1 + 1;
-		s->num_tile_rows = s->pps.num_tile_rows_minus1 + 1;
-		for (i = 0; i < s->num_tile_columns; ++i)
-			s->column_width[i] = s->pps.column_width_minus1[i] + 1;
-		for (i = 0; i < s->num_tile_rows; ++i)
-			s->row_height[i] = s->pps.row_height_minus1[i] + 1;
+		s->tile_width = s->pps.num_tile_columns_minus1 + 1;
+		s->tile_height = s->pps.num_tile_rows_minus1 + 1;
 	}
 
-	s->col_bd = kmalloc((s->num_tile_columns + 1) * sizeof(*s->col_bd),
+	s->col_bd = kmalloc((s->tile_width + 1) * sizeof(*s->col_bd),
 			    GFP_KERNEL);
-	s->row_bd = kmalloc((s->num_tile_rows + 1) * sizeof(*s->row_bd),
+	s->row_bd = kmalloc((s->tile_height + 1) * sizeof(*s->row_bd),
 			    GFP_KERNEL);
 
 	s->col_bd[0] = 0;
-	for (i = 0; i < s->num_tile_columns; i++)
-		s->col_bd[i + 1] = s->col_bd[i] + s->column_width[i];
+	for (i = 1; i < s->tile_width; i++)
+		s->col_bd[i] = s->col_bd[i - 1] +
+			s->pps.column_width_minus1[i - 1] + 1;
+	s->col_bd[s->tile_width] = s->ctb_width;
 
 	s->row_bd[0] = 0;
-	for (i = 0; i < s->num_tile_rows; i++)
-		s->row_bd[i + 1] = s->row_bd[i] + s->row_height[i];
+	for (i = 1; i < s->tile_height; i++)
+		s->row_bd[i] = s->row_bd[i - 1] +
+			s->pps.row_height_minus1[i - 1] + 1;
+	s->row_bd[s->tile_height] = s->ctb_height;
 
-	s->ctb_addr_rs_to_ts = kmalloc_array(s->ctb_size,
-					     sizeof(*s->ctb_addr_rs_to_ts),
-					     GFP_KERNEL);
-	s->ctb_addr_ts_to_rs = kmalloc_array(s->ctb_size,
-					     sizeof(*s->ctb_addr_ts_to_rs),
-					     GFP_KERNEL);
-	s->tile_id = kmalloc_array(s->ctb_size, sizeof(*s->tile_id),
-				   GFP_KERNEL);
-
-	for (ctb_addr_rs = 0; ctb_addr_rs < s->ctb_size; ctb_addr_rs++) {
-		int tb_x = ctb_addr_rs % s->ctb_width;
-		int tb_y = ctb_addr_rs / s->ctb_width;
-		int tile_x = 0;
-		int tile_y = 0;
-		int val = 0;
-
-		for (i = 0; i < s->num_tile_columns; i++) {
-			if (tb_x < s->col_bd[i + 1]) {
-				tile_x = i;
-				break;
-			}
-		}
-
-		for (i = 0; i < s->num_tile_rows; i++) {
-			if (tb_y < s->row_bd[i + 1]) {
-				tile_y = i;
-				break;
-			}
-		}
-
-		for (i = 0; i < tile_x; i++)
-			val += s->row_height[tile_y] * s->column_width[i];
-		for (i = 0; i < tile_y; i++)
-			val += s->ctb_width * s->row_height[i];
-
-		val += (tb_y - s->row_bd[tile_y]) * s->column_width[tile_x] +
-		       tb_x - s->col_bd[tile_x];
-
-		s->ctb_addr_rs_to_ts[ctb_addr_rs] = val;
-		s->ctb_addr_ts_to_rs[val] = ctb_addr_rs;
-	}
-
-	for (j = 0, tile_id = 0; j < s->num_tile_rows; j++)
-		for (i = 0; i < s->num_tile_columns; i++, tile_id++)
-			for (y = s->row_bd[j]; y < s->row_bd[j + 1]; y++)
-				for (x = s->col_bd[i];
-				     x < s->col_bd[i + 1];
-				     x++)
-					s->tile_id[s->ctb_addr_rs_to_ts
-							   [y * s->ctb_width +
-							    x]] = tile_id;
+	fill_rs_to_ts(s);
 
 	return 0;
 }
 
-static int frame_end(struct rpivid_dev *const dev,
-		     struct rpivid_dec_env *const de,
-		     const struct rpivid_dec_state *const s)
-{
-	const unsigned int last_x = s->col_bd[s->num_tile_columns] - 1;
-	const unsigned int last_y = s->row_bd[s->num_tile_rows] - 1;
-	size_t cmd_size;
-
-	if (s->pps.flags & V4L2_HEVC_PPS_FLAG_ENTROPY_CODING_SYNC_ENABLED) {
-		if (de->wpp_entry_x < 2 && de->pic_width_in_ctbs_y > 2)
-			wpp_pause(de, last_y);
-	}
-	p1_apb_write(de, RPI_STATUS, 1 + (last_x << 5) + (last_y << 18));
-
+static int write_cmd_buffer(struct rpivid_dev *const dev,
+			    struct rpivid_dec_env *const de,
+			    const struct rpivid_dec_state *const s)
+{
 	// Copy commands out to dma buf
-	cmd_size = de->cmd_len * sizeof(de->cmd_fifo[0]);
+	const size_t cmd_size = de->cmd_len * sizeof(de->cmd_fifo[0]);
 
 	if (!de->cmd_copy_gptr->ptr || cmd_size > de->cmd_copy_gptr->size) {
 		size_t cmd_alloc = round_up_size(cmd_size);
@@ -1521,18 +1528,19 @@ static void rpivid_h265_setup(struct rpi
 	struct rpivid_q_aux *dpb_q_aux[V4L2_HEVC_DPB_ENTRIES_NUM_MAX];
 	struct rpivid_dec_state *const s = ctx->state;
 	struct vb2_queue *vq;
-	struct rpivid_dec_env *de;
-	int ctb_addr_ts;
+	struct rpivid_dec_env *de = ctx->dec0;
+	unsigned int prev_rs;
 	unsigned int i;
 	int use_aux;
 	bool slice_temporal_mvp;
 
+	xtrace_in(dev, de);
+
 	pred_weight_table = &sh->pred_weight_table;
 
 	s->frame_end =
 		((run->src->flags & V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF) == 0);
 
-	de = ctx->dec0;
 	slice_temporal_mvp = (sh->flags &
 		   V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_TEMPORAL_MVP_ENABLED);
 
@@ -1662,6 +1670,13 @@ static void rpivid_h265_setup(struct rpi
 				  s->sps.pic_height_in_luma_samples);
 			goto fail;
 		}
+		if ((s->tile_width != 1 || s->tile_height != 1) &&
+		    (s->pps.flags &
+		     V4L2_HEVC_PPS_FLAG_ENTROPY_CODING_SYNC_ENABLED)) {
+			v4l2_warn(&dev->v4l2_dev,
+				  "Tiles + WPP not supported\n");
+			goto fail;
+		}
 
 		// Fill in ref planes with our address s.t. if we mess
 		// up refs somehow then we still have a valid address
@@ -1760,15 +1775,24 @@ static void rpivid_h265_setup(struct rpi
 	if (s->sps.flags & V4L2_HEVC_SPS_FLAG_SCALING_LIST_ENABLED)
 		populate_scaling_factors(run, de, s);
 
-	ctb_addr_ts = s->ctb_addr_rs_to_ts[sh->slice_segment_addr];
+	// Calc all the random coord info to avoid repeated conversion in/out
+	s->start_ts = s->ctb_addr_rs_to_ts[sh->slice_segment_addr];
+	s->start_ctb_x = sh->slice_segment_addr % de->pic_width_in_ctbs_y;
+	s->start_ctb_y = sh->slice_segment_addr / de->pic_width_in_ctbs_y;
+	// Last CTB of previous slice
+	prev_rs = !s->start_ts ? 0 : s->ctb_addr_ts_to_rs[s->start_ts - 1];
+	s->prev_ctb_x = prev_rs % de->pic_width_in_ctbs_y;
+	s->prev_ctb_y = prev_rs / de->pic_width_in_ctbs_y;
 
 	if ((s->pps.flags & V4L2_HEVC_PPS_FLAG_ENTROPY_CODING_SYNC_ENABLED))
-		wpp_decode_slice(de, s, sh, ctb_addr_ts);
+		wpp_decode_slice(de, s);
 	else
-		decode_slice(de, s, sh, ctb_addr_ts);
+		decode_slice(de, s);
 
-	if (!s->frame_end)
+	if (!s->frame_end) {
+		xtrace_ok(dev, de);
 		return;
+	}
 
 	// Frame end
 	memset(dpb_q_aux, 0,
@@ -1776,8 +1800,9 @@ static void rpivid_h265_setup(struct rpi
 	/*
 	 * Need Aux ents for all (ref) DPB ents if temporal MV could
 	 * be enabled for any pic
-	 * ** At the moment we have aux ents for all pics whether or not
-	 *    they are ref
+	 * ** At the moment we create aux ents for all pics whether or not
+	 *    they are ref - they should then be discarded by the DPB-aux
+	 *    garbage collection code
 	 */
 	use_aux = ((s->sps.flags &
 		  V4L2_HEVC_SPS_FLAG_SPS_TEMPORAL_MVP_ENABLED) != 0);
@@ -1795,7 +1820,7 @@ static void rpivid_h265_setup(struct rpi
 	}
 
 	//        v4l2_info(&dev->v4l2_dev, "rpivid_h265_end of frame\n");
-	if (frame_end(dev, de, s))
+	if (write_cmd_buffer(dev, de, s))
 		goto fail;
 
 	for (i = 0; i < sh->num_active_dpb_entries; ++i) {
@@ -1876,6 +1901,7 @@ static void rpivid_h265_setup(struct rpi
 	}
 
 	de->state = RPIVID_DECODE_PHASE1;
+	xtrace_ok(dev, de);
 	return;
 
 fail:
@@ -1883,6 +1909,7 @@ fail:
 		// Actual error reporting happens in Trigger
 		de->state = s->frame_end ? RPIVID_DECODE_ERROR_DONE :
 					   RPIVID_DECODE_ERROR_CONTINUE;
+	xtrace_fail(dev, de);
 }
 
 //////////////////////////////////////////////////////////////////////////////
@@ -2210,6 +2237,10 @@ static int rpivid_h265_start(struct rpiv
 	size_t pu_alloc;
 	size_t coeff_alloc;
 
+#if DEBUG_TRACE_P1_CMD
+	p1_z = 0;
+#endif
+
 	// Generate a sanitised WxH for memory alloc
 	// Assume HD if unset
 	if (w == 0)