-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmal_1.patch
6570 lines (6336 loc) · 217 KB
/
mmal_1.patch
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
--- a/configure.ac
+++ b/configure.ac
@@ -3406,7 +3406,7 @@
VLC_ADD_PLUGIN([mmal])
VLC_ADD_LDFLAGS([mmal],[ -L/opt/vc/lib ])
VLC_ADD_CFLAGS([mmal],[ -isystem /opt/vc/include -isystem /opt/vc/include/interface/vcos/pthreads -isystem /opt/vc/include/interface/vmcs_host/linux ])
- VLC_ADD_LIBS([mmal],[ -lbcm_host -lmmal -lmmal_core -lmmal_components -lmmal_util -lvchostif ]) ], [
+ VLC_ADD_LIBS([mmal],[ -lbcm_host -lmmal -lmmal_core -lmmal_components -lmmal_util -lvchostif -lvchiq_arm -lvcsm ]) ], [
AS_IF([test "${enable_mmal}" = "yes"],
[ AC_MSG_ERROR([Cannot find bcm library...]) ],
[ AC_MSG_WARN([Cannot find bcm library...]) ])
--- a/modules/hw/mmal/Makefile.am
+++ b/modules/hw/mmal/Makefile.am
@@ -1,23 +1,30 @@
include $(top_srcdir)/modules/common.am
mmaldir = $(pluginsdir)/mmal
-AM_CFLAGS += $(CFLAGS_mmal)
-AM_LDFLAGS += -rpath '$(mmaldir)' $(LDFLAGS_mmal)
+AM_CFLAGS += -pthread $(CFLAGS_mmal)
+AM_LDFLAGS += -pthread -rpath '$(mmaldir)' $(LDFLAGS_mmal)
-libmmal_vout_plugin_la_SOURCES = vout.c mmal_picture.c mmal_picture.h
+libmmal_vout_plugin_la_SOURCES = vout.c subpic.c mmal_picture.c subpic.h mmal_picture.h
libmmal_vout_plugin_la_CFLAGS = $(AM_CFLAGS)
libmmal_vout_plugin_la_LDFLAGS = $(AM_LDFLAGS) -lm
libmmal_vout_plugin_la_LIBADD = $(LIBS_mmal)
mmal_LTLIBRARIES = libmmal_vout_plugin.la
-libmmal_codec_plugin_la_SOURCES = codec.c
+libmmal_codec_plugin_la_SOURCES = codec.c subpic.c mmal_picture.c blend_rgba_neon.S subpic.h mmal_picture.h
libmmal_codec_plugin_la_CFLAGS = $(AM_CFLAGS)
libmmal_codec_plugin_la_LDFLAGS = $(AM_LDFLAGS)
libmmal_codec_plugin_la_LIBADD = $(LIBS_mmal)
mmal_LTLIBRARIES += libmmal_codec_plugin.la
-libmmal_deinterlace_plugin_la_SOURCES = deinterlace.c mmal_picture.c
+libmmal_deinterlace_plugin_la_SOURCES = deinterlace.c mmal_picture.c mmal_picture.h
libmmal_deinterlace_plugin_la_CFLAGS = $(AM_CFLAGS)
libmmal_deinterlace_plugin_la_LDFLAGS = $(AM_LDFLAGS)
libmmal_deinterlace_plugin_la_LIBADD = $(LIBS_mmal)
mmal_LTLIBRARIES += libmmal_deinterlace_plugin.la
+
+libmmal_xsplitter_plugin_la_SOURCES = xsplitter.c mmal_picture.c mmal_picture.h
+libmmal_xsplitter_plugin_la_CFLAGS = $(AM_CFLAGS)
+libmmal_xsplitter_plugin_la_LDFLAGS = $(AM_LDFLAGS)
+libmmal_xsplitter_plugin_la_LIBADD = $(LIBS_mmal)
+mmal_LTLIBRARIES += libmmal_xsplitter_plugin.la
+
--- /dev/null
+++ b/modules/hw/mmal/blend_rgba_neon.S
@@ -0,0 +1,185 @@
+ .syntax unified
+ .arm
+// .thumb
+ .text
+ .align 16
+
+@ Deal with tail
+@ As the calculation on each pel is independant it doesn't matter
+@ where the pels are in in the registers as long as we are consistant
+@ with what we use for load & store. (Unused register fields will just
+@ calculate garbage that we then ignore.)
+
+tail_rgbx_rgba:
+ lsls r2, r3, #30 @ b2 -> C, b1 -> N
+ mov r12, r0
+ vldmcs r1!, {d2,d3}
+ vldmcs r0!, {d30, d31}
+ vldmmi r1!, {d1}
+ vldmmi r0!, {d29}
+ tst r3, #1
+ vldrne s1, [r1]
+ vldrne s9, [r0]
+ addne r1, #4
+ addne r0, #4
+
+ @ Alpha in hi byte of each word
+ vshr.u32 q9, q1, #24
+ vshr.u32 q8, q0, #24
+ vmov d28, d4 @ Can't load "s57" so cheat
+ vmul.u32 q9, q9, d8[0]
+ vmul.u32 q8, q8, d8[0]
+ vrshr.u32 q9, q9, #16
+ vrshr.u32 q8, q8, #16 @ 4 mix alpha values
+ vmul.u32 q9, q10
+ vmul.u32 q8, q10 @ dup every alpha into all 4 bytes of its word
+
+ vmull.u8 q3, d3, d19
+ vmull.u8 q2, d2, d18
+ vsub.u8 q9, q11, q9 @ gen the cplmnt
+
+ vmull.u8 q1, d1, d17
+ vmull.u8 q0, d0, d16
+ vsub.u8 q8, q11, q8
+
+ vmlal.u8 q3, d31, d19
+ vmlal.u8 q2, d30, d18
+ vmlal.u8 q1, d29, d17
+ vmlal.u8 q0, d28, d16
+
+ vsra.u16 q3, q3, #8
+ vsra.u16 q2, q2, #8
+ vsra.u16 q1, q1, #8
+ vsra.u16 q0, q0, #8
+
+ vrshrn.u16 d7, q3, #8
+ vrshrn.u16 d6, q2, #8
+ vrshrn.u16 d5, q1, #8
+ vrshrn.u16 d4, q0, #8
+
+ vbit q3, q12, q12 @ Set alpha to #255
+ vbit q2, q12, q12
+
+ lsls r2, r3, #30 @ b2 -> C, b1 -> N
+ vstmcs r12!, {d6,d7}
+ vstmmi r12!, {d5}
+ tst r3, #1
+ vstrne s9, [r12]
+ bx lr
+
+
+@ blend_rgbx_rgba_neon
+
+@ [r0] RGBx dest (Byte order: R, G, B, x)
+@ [r1] RGBA src merge (Byte order: R, G, B, A)
+@ r2 plane alpha
+@ r3 count (pixels)
+
+@ Whilst specified as RGBx+RGBA the only important part is the position of
+@ alpha, the other components are all treated the same
+@ Assumes little endian i.e. Alpha ends up in hi 8 bits of uint32_t
+
+#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
+#error Only little endian written (should be easy fix)
+#endif
+
+@ Implements /255 as ((x * 257) + 0x8000) >> 16
+@ This generates something in the range [(x+126)/255, (x+127)/255] which is good enough
+
+@ There is advantage to aligning src and/or dest - dest gives a bit more due to being used twice
+
+ .align 16
+ .global blend_rgbx_rgba_neon
+#ifdef __ELF__
+ .type blend_rgbx_rgba_neon, %function
+#endif
+
+blend_rgbx_rgba_neon:
+
+ push { r4, lr }
+ vpush { q4 } @ With reworked reg alloc this could be avoided
+ movw r12, #257
+ vmov.i8 q11, #0xff
+ mul r2, r12
+ vmov.u8 q10, #1
+ subs r3, #8
+ vmov s16, r2
+ vmov.u32 q12, #0xff000000
+ blt 2f
+
+ @ If < 16 bytes to move then don't bother trying to align
+ @ (a) This means the the align doesn't need to worry about r3 underflow
+ @ (b) The overhead would be greater than any gain
+ cmp r3, #8
+ ble 1f
+
+ @ Align r1 on a 32 byte boundary
+ mov r4, r3
+ neg r3, r0
+ ubfx r3, r3, #2, #3
+
+ cmp r3, #0
+ blne tail_rgbx_rgba
+
+ sub r3, r4, r3
+
+1:
+ vld1.32 { q0,q1 }, [r1]
+
+1:
+ @ Alpha in hi byte of each word
+ vshr.u32 q9, q1, #24
+ vshr.u32 q8, q0, #24
+ vmul.u32 q9, q9, d8[0]
+ vmul.u32 q8, q8, d8[0]
+
+ vrshr.u32 q9, q9, #16
+ vrshr.u32 q8, q8, #16 @ 4 mix alpha values
+ vmul.u32 q9, q10
+ vmul.u32 q8, q10 @ dup every alpha into all 4 bytes of its word
+
+ vld1.32 {q14,q15}, [r0]
+ subs r3, #8
+
+ vmull.u8 q3, d3, d19
+ vmull.u8 q2, d2, d18
+ vsub.u8 q9, q11, q9 @ gen the cplmnt
+
+ vmull.u8 q1, d1, d17
+ vmull.u8 q0, d0, d16
+ vsub.u8 q8, q11, q8
+ addge r1, #32
+
+ vmlal.u8 q3, d31, d19
+ vmlal.u8 q2, d30, d18
+ vmlal.u8 q1, d29, d17
+ vmlal.u8 q0, d28, d16
+
+ vsra.u16 q3, q3, #8
+ vsra.u16 q2, q2, #8
+ vsra.u16 q1, q1, #8
+ vsra.u16 q0, q0, #8
+
+ vrshrn.u16 d7, q3, #8
+ vrshrn.u16 d6, q2, #8
+ vrshrn.u16 d5, q1, #8
+ vrshrn.u16 d4, q0, #8
+
+ vld1.32 { q0,q1 }, [r1]
+
+ vbit q3, q12, q12 @ Set alpha to #255
+ vbit q2, q12, q12
+
+ vst1.32 { q2,q3 }, [r0]!
+ bge 1b
+ add r1, #32
+
+2:
+ cmp r3, #-8
+ blgt tail_rgbx_rgba
+
+ vpop { q4 }
+ pop { r4, pc }
+
+
+
--- /dev/null
+++ b/modules/hw/mmal/blend_rgba_neon.h
@@ -0,0 +1,15 @@
+#ifndef HW_MMAL_BLEND_RGBA_NEON_H
+#define HW_MMAL_BLEND_RGBA_NEON_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern void blend_rgbx_rgba_neon(void * dest, const void * src, int alpha, unsigned int n);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
--- /dev/null
+++ b/modules/hw/mmal/blend_test.c
@@ -0,0 +1,116 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <memory.h>
+
+#include "blend_rgba_neon.h"
+
+#define RPI_PROFILE 1
+#define RPI_PROC_ALLOC 1
+#include "rpi_prof.h"
+
+static inline unsigned div255(unsigned v)
+{
+ // This models what we we do in the asm for / 255
+ // It generates something in the range [(i+126)/255, (i+127)/255] which is good enough
+ return ((v * 257) + 0x8000) >> 16;
+}
+
+static inline unsigned int a_merge(unsigned int dst, unsigned src, unsigned f)
+{
+ return div255((255 - f) * (dst) + src * f);
+}
+
+
+static void merge_line(void * dest, const void * src, int alpha, unsigned int n)
+{
+ unsigned int i;
+ const uint8_t * s_data = src;
+ uint8_t * d_data = dest;
+
+ for (i = 0; i != n; ++i) {
+ const uint32_t s_pel = ((const uint32_t *)s_data)[i];
+ const uint32_t d_pel = ((const uint32_t *)d_data)[i];
+ const unsigned int a = div255(alpha * (s_pel >> 24));
+ ((uint32_t *)d_data)[i] = 0xff000000 |
+ (a_merge((d_pel >> 16) & 0xff, (s_pel >> 16) & 0xff, a) << 16) |
+ (a_merge((d_pel >> 8) & 0xff, (s_pel >> 8) & 0xff, a) << 8 ) |
+ (a_merge((d_pel >> 0) & 0xff, (s_pel >> 0) & 0xff, a) << 0 );
+ }
+}
+
+#define BUF_SIZE 256
+#define BUF_SLACK 16
+#define BUF_ALIGN 64
+#define BUF_ALLOC (BUF_SIZE + 2*BUF_SLACK + BUF_ALIGN)
+
+static void test_line(const uint32_t * const dx, const unsigned int d_off,
+ const uint32_t * const sx, const unsigned int s_off,
+ const unsigned int alpha, const unsigned int len, const int prof_no)
+{
+ uint32_t d0_buf[BUF_ALLOC];
+ uint32_t d1_buf[BUF_ALLOC];
+ const uint32_t * const s0 = sx + s_off;
+
+ uint32_t * const d0 = (uint32_t *)(((uintptr_t)d0_buf + (BUF_ALIGN - 1)) & ~(BUF_ALIGN - 1)) + d_off;
+ uint32_t * const d1 = (uint32_t *)(((uintptr_t)d1_buf + (BUF_ALIGN - 1)) & ~(BUF_ALIGN - 1)) + d_off;
+ unsigned int i;
+
+ memcpy(d0, dx, (BUF_SIZE + BUF_SLACK*2)*4);
+ memcpy(d1, dx, (BUF_SIZE + BUF_SLACK*2)*4);
+
+ merge_line(d0 + BUF_SLACK, s0 + BUF_SLACK, alpha, len);
+
+ PROFILE_START();
+ blend_rgbx_rgba_neon(d1 + BUF_SLACK, s0 + BUF_SLACK, alpha, len);
+ PROFILE_ACC_N(prof_no);
+
+ for (i = 0; i != BUF_SIZE + BUF_SLACK*2; ++i) {
+ if (d0[i] != d1[i]) {
+ printf("%3d: %08x + %08x * %02x: %08x / %08x: len=%d\n", (int)(i - BUF_SLACK), dx[i], s0[i], alpha, d0[i], d1[i], len);
+ }
+ }
+}
+
+
+int main(int argc, char *argv[])
+{
+ unsigned int i, j;
+ uint32_t d0_buf[BUF_ALLOC];
+ uint32_t s0_buf[BUF_ALLOC];
+
+ uint32_t * const d0 = (uint32_t *)(((uintptr_t)d0_buf + 63) & ~63) + 0;
+ uint32_t * const s0 = (uint32_t *)(((uintptr_t)s0_buf + 63) & ~63) + 0;
+
+ PROFILE_INIT();
+
+ for (i = 0; i != 255*255; ++i) {
+ unsigned int a = div255(i);
+ unsigned int b = (i + 127)/255;
+ unsigned int c = (i + 126)/255;
+ if (a != b && a != c)
+ printf("%d/255: %d != %d/%d\n", i, a, b, c);
+ }
+
+ for (i = 0; i != BUF_ALLOC; ++i) {
+ d0_buf[i] = 0xff00 | i;
+ s0_buf[i] = (i << 24) | 0xffffff;
+ }
+ for (i = 0; i != 256; ++i) {
+ test_line(d0, 0, s0, 0, i, 256, -1);
+ }
+ for (i = 0; i != 256; ++i) {
+ test_line(d0, 0, s0, 0, 128, i, -1);
+ }
+
+ for (j = 0; j != 16; ++j) {
+ for (i = 0; i != 256; ++i) {
+ test_line(d0, j & 3, s0, j >> 2, i, 256, j);
+ }
+ PROFILE_PRINTF_N(j);
+ }
+
+ printf("Done\n");
+
+ return 0;
+}
+
--- a/modules/hw/mmal/codec.c
+++ b/modules/hw/mmal/codec.c
@@ -26,10 +26,12 @@
#include "config.h"
#endif
+#include <stdatomic.h>
+
#include <vlc_common.h>
-#include <vlc_atomic.h>
#include <vlc_plugin.h>
#include <vlc_codec.h>
+#include <vlc_filter.h>
#include <vlc_threads.h>
#include <bcm_host.h>
@@ -38,255 +40,380 @@
#include <interface/mmal/util/mmal_default_components.h>
#include "mmal_picture.h"
+#include "subpic.h"
+#include "blend_rgba_neon.h"
+
+#define TRACE_ALL 0
/*
* This seems to be a bit high, but reducing it causes instabilities
*/
-#define NUM_EXTRA_BUFFERS 5
+#define NUM_EXTRA_BUFFERS 3
+//#define NUM_EXTRA_BUFFERS 6
+//#define NUM_EXTRA_BUFFERS 10
#define NUM_DECODER_BUFFER_HEADERS 30
#define MIN_NUM_BUFFERS_IN_TRANSIT 2
+#define MMAL_COMPONENT_DEFAULT_RESIZER "vc.ril.resize"
+#define MMAL_COMPONENT_ISP_RESIZER "vc.ril.isp"
+#define MMAL_COMPONENT_HVS "vc.ril.hvs"
+
+#define MMAL_SLICE_HEIGHT 16
+#define MMAL_ALIGN_W 32
+#define MMAL_ALIGN_H 16
+
#define MMAL_OPAQUE_NAME "mmal-opaque"
#define MMAL_OPAQUE_TEXT N_("Decode frames directly into RPI VideoCore instead of host memory.")
#define MMAL_OPAQUE_LONGTEXT N_("Decode frames directly into RPI VideoCore instead of host memory. This option must only be used with the MMAL video output plugin.")
-static int OpenDecoder(decoder_t *dec);
-static void CloseDecoder(decoder_t *dec);
+#define MMAL_RESIZE_NAME "mmal-resize"
+#define MMAL_RESIZE_TEXT N_("Use mmal resizer rather than hvs.")
+#define MMAL_RESIZE_LONGTEXT N_("Use mmal resizer rather than isp. This uses less gpu memory than the ISP but is slower.")
+
+#define MMAL_ISP_NAME "mmal-isp"
+#define MMAL_ISP_TEXT N_("Use mmal isp rather than hvs.")
+#define MMAL_ISP_LONGTEXT N_("Use mmal isp rather than hvs. This may be faster but has no blend.")
-vlc_module_begin()
- set_shortname(N_("MMAL decoder"))
- set_description(N_("MMAL-based decoder plugin for Raspberry Pi"))
- set_capability("video decoder", 90)
- add_shortcut("mmal_decoder")
- add_bool(MMAL_OPAQUE_NAME, true, MMAL_OPAQUE_TEXT, MMAL_OPAQUE_LONGTEXT, false)
- set_callbacks(OpenDecoder, CloseDecoder)
-vlc_module_end()
-struct decoder_sys_t {
- bool opaque;
+typedef struct decoder_sys_t
+{
MMAL_COMPONENT_T *component;
MMAL_PORT_T *input;
MMAL_POOL_T *input_pool;
MMAL_PORT_T *output;
- MMAL_POOL_T *output_pool; /* only used for non-opaque mode */
+ hw_mmal_port_pool_ref_t *ppr;
MMAL_ES_FORMAT_T *output_format;
- vlc_sem_t sem;
+ MMAL_STATUS_T err_stream;
bool b_top_field_first;
bool b_progressive;
+ bool b_flushed;
+
/* statistics */
- int output_in_transit;
- int input_in_transit;
atomic_bool started;
-};
+} decoder_sys_t;
-/* Utilities */
-static int change_output_format(decoder_t *dec);
-static int send_output_buffer(decoder_t *dec);
-static void fill_output_port(decoder_t *dec);
-
-/* VLC decoder callback */
-static int decode(decoder_t *dec, block_t *block);
-static void flush_decoder(decoder_t *dec);
-
-/* MMAL callbacks */
-static void control_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer);
-static void input_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer);
-static void output_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer);
-static int OpenDecoder(decoder_t *dec)
+typedef struct supported_mmal_enc_s {
+ struct {
+ MMAL_PARAMETER_HEADER_T header;
+ MMAL_FOURCC_T encodings[64];
+ } supported;
+ int n;
+} supported_mmal_enc_t;
+
+static supported_mmal_enc_t supported_mmal_enc =
{
- int ret = VLC_SUCCESS;
- decoder_sys_t *sys;
- MMAL_PARAMETER_UINT32_T extra_buffers;
- MMAL_STATUS_T status;
+ {{MMAL_PARAMETER_SUPPORTED_ENCODINGS, sizeof(((supported_mmal_enc_t *)0)->supported)}, {0}},
+ -1
+};
- if (dec->fmt_in.i_codec != VLC_CODEC_MPGV &&
- dec->fmt_in.i_codec != VLC_CODEC_H264)
- return VLC_EGENERIC;
+#if TRACE_ALL || 1
+static const char * str_fourcc(char * buf, unsigned int fcc)
+{
+ if (fcc == 0)
+ return "----";
+ buf[0] = (fcc >> 0) & 0xff;
+ buf[1] = (fcc >> 8) & 0xff;
+ buf[2] = (fcc >> 16) & 0xff;
+ buf[3] = (fcc >> 24) & 0xff;
+ buf[4] = 0;
+ return buf;
+}
+#endif
- sys = calloc(1, sizeof(decoder_sys_t));
- if (!sys) {
- ret = VLC_ENOMEM;
- goto out;
+static bool is_enc_supported(const MMAL_FOURCC_T fcc)
+{
+ int i;
+
+ if (fcc == 0)
+ return false;
+ if (supported_mmal_enc.n == -1)
+ return true; // Unknown - say OK
+ for (i = 0; i < supported_mmal_enc.n; ++i) {
+ if (supported_mmal_enc.supported.encodings[i] == fcc)
+ return true;
}
- dec->p_sys = sys;
+ return false;
+}
- sys->opaque = var_InheritBool(dec, MMAL_OPAQUE_NAME);
- bcm_host_init();
+static bool set_and_test_enc_supported(MMAL_PORT_T * port, const MMAL_FOURCC_T fcc)
+{
+ if (supported_mmal_enc.n >= 0)
+ /* already done */;
+ else if (mmal_port_parameter_get(port, (MMAL_PARAMETER_HEADER_T *)&supported_mmal_enc.supported) != MMAL_SUCCESS)
+ supported_mmal_enc.n = 0;
+ else
+ supported_mmal_enc.n = (supported_mmal_enc.supported.header.size - sizeof(supported_mmal_enc.supported.header)) /
+ sizeof(supported_mmal_enc.supported.encodings[0]);
- status = mmal_component_create(MMAL_COMPONENT_DEFAULT_VIDEO_DECODER, &sys->component);
- if (status != MMAL_SUCCESS) {
- msg_Err(dec, "Failed to create MMAL component %s (status=%"PRIx32" %s)",
- MMAL_COMPONENT_DEFAULT_VIDEO_DECODER, status, mmal_status_to_string(status));
- ret = VLC_EGENERIC;
- goto out;
- }
+ return is_enc_supported(fcc);
+}
- sys->component->control->userdata = (struct MMAL_PORT_USERDATA_T *)dec;
- status = mmal_port_enable(sys->component->control, control_port_cb);
- if (status != MMAL_SUCCESS) {
- msg_Err(dec, "Failed to enable control port %s (status=%"PRIx32" %s)",
- sys->component->control->name, status, mmal_status_to_string(status));
- ret = VLC_EGENERIC;
- goto out;
+static MMAL_FOURCC_T vlc_to_mmal_es_fourcc(const unsigned int fcc)
+{
+ switch (fcc){
+ case VLC_CODEC_MJPG:
+ return MMAL_ENCODING_MJPEG;
+ case VLC_CODEC_MP1V:
+ return MMAL_ENCODING_MP1V;
+ case VLC_CODEC_MPGV:
+ case VLC_CODEC_MP2V:
+ return MMAL_ENCODING_MP2V;
+ case VLC_CODEC_H263:
+ return MMAL_ENCODING_H263;
+ case VLC_CODEC_MP4V:
+ return MMAL_ENCODING_MP4V;
+ case VLC_CODEC_H264:
+ return MMAL_ENCODING_H264;
+ case VLC_CODEC_VP6:
+ return MMAL_ENCODING_VP6;
+ case VLC_CODEC_VP8:
+ return MMAL_ENCODING_VP8;
+ case VLC_CODEC_WMV1:
+ return MMAL_ENCODING_WMV1;
+ case VLC_CODEC_WMV2:
+ return MMAL_ENCODING_WMV2;
+ case VLC_CODEC_WMV3:
+ return MMAL_ENCODING_WMV3;
+ case VLC_CODEC_VC1:
+ return MMAL_ENCODING_WVC1;
+ case VLC_CODEC_THEORA:
+ return MMAL_ENCODING_THEORA;
+ default:
+ break;
}
+ return 0;
+}
- sys->input = sys->component->input[0];
- sys->input->userdata = (struct MMAL_PORT_USERDATA_T *)dec;
- if (dec->fmt_in.i_codec == VLC_CODEC_MPGV)
- sys->input->format->encoding = MMAL_ENCODING_MP2V;
- else
- sys->input->format->encoding = MMAL_ENCODING_H264;
-
- if (dec->fmt_in.i_codec == VLC_CODEC_H264) {
- if (dec->fmt_in.i_extra > 0) {
- status = mmal_format_extradata_alloc(sys->input->format,
- dec->fmt_in.i_extra);
- if (status == MMAL_SUCCESS) {
- memcpy(sys->input->format->extradata, dec->fmt_in.p_extra,
- dec->fmt_in.i_extra);
- sys->input->format->extradata_size = dec->fmt_in.i_extra;
- } else {
- msg_Err(dec, "Failed to allocate extra format data on input port %s (status=%"PRIx32" %s)",
- sys->input->name, status, mmal_status_to_string(status));
- }
- }
+static MMAL_FOURCC_T vlc_to_mmal_pic_fourcc(const unsigned int fcc)
+{
+ switch (fcc){
+ case VLC_CODEC_I420:
+ return MMAL_ENCODING_I420;
+ case VLC_CODEC_RGB32: // _RGB32 doesn't exist in mmal magic mapping table
+ case VLC_CODEC_RGBA:
+ return MMAL_ENCODING_BGRA;
+ case VLC_CODEC_MMAL_OPAQUE:
+ return MMAL_ENCODING_OPAQUE;
+ default:
+ break;
}
+ return 0;
+}
- status = mmal_port_format_commit(sys->input);
- if (status != MMAL_SUCCESS) {
- msg_Err(dec, "Failed to commit format for input port %s (status=%"PRIx32" %s)",
- sys->input->name, status, mmal_status_to_string(status));
- ret = VLC_EGENERIC;
- goto out;
+static MMAL_FOURCC_T pic_to_slice_mmal_fourcc(const MMAL_FOURCC_T fcc)
+{
+ switch (fcc){
+ case MMAL_ENCODING_I420:
+ return MMAL_ENCODING_I420_SLICE;
+ case MMAL_ENCODING_I422:
+ return MMAL_ENCODING_I422_SLICE;
+ case MMAL_ENCODING_ARGB:
+ return MMAL_ENCODING_ARGB_SLICE;
+ case MMAL_ENCODING_RGBA:
+ return MMAL_ENCODING_RGBA_SLICE;
+ case MMAL_ENCODING_ABGR:
+ return MMAL_ENCODING_ABGR_SLICE;
+ case MMAL_ENCODING_BGRA:
+ return MMAL_ENCODING_BGRA_SLICE;
+ case MMAL_ENCODING_RGB16:
+ return MMAL_ENCODING_RGB16_SLICE;
+ case MMAL_ENCODING_RGB24:
+ return MMAL_ENCODING_RGB24_SLICE;
+ case MMAL_ENCODING_RGB32:
+ return MMAL_ENCODING_RGB32_SLICE;
+ case MMAL_ENCODING_BGR16:
+ return MMAL_ENCODING_BGR16_SLICE;
+ case MMAL_ENCODING_BGR24:
+ return MMAL_ENCODING_BGR24_SLICE;
+ case MMAL_ENCODING_BGR32:
+ return MMAL_ENCODING_BGR32_SLICE;
+ default:
+ break;
}
- sys->input->buffer_size = sys->input->buffer_size_recommended;
- sys->input->buffer_num = sys->input->buffer_num_recommended;
+ return 0;
+}
- status = mmal_port_enable(sys->input, input_port_cb);
- if (status != MMAL_SUCCESS) {
- msg_Err(dec, "Failed to enable input port %s (status=%"PRIx32" %s)",
- sys->input->name, status, mmal_status_to_string(status));
- ret = VLC_EGENERIC;
- goto out;
+#if 0
+static inline void draw_line(void * pic_buf, size_t pic_stride, unsigned int x, unsigned int y, unsigned int len, int inc)
+{
+ uint32_t * p = (uint32_t *)pic_buf + y * pic_stride + x;
+ while (len-- != 0) {
+ *p = ~0U;
+ p += inc;
}
+}
- sys->output = sys->component->output[0];
- sys->output->userdata = (struct MMAL_PORT_USERDATA_T *)dec;
- if (sys->opaque) {
- extra_buffers.hdr.id = MMAL_PARAMETER_EXTRA_BUFFERS;
- extra_buffers.hdr.size = sizeof(MMAL_PARAMETER_UINT32_T);
- extra_buffers.value = NUM_EXTRA_BUFFERS;
- status = mmal_port_parameter_set(sys->output, &extra_buffers.hdr);
- if (status != MMAL_SUCCESS) {
- msg_Err(dec, "Failed to set MMAL_PARAMETER_EXTRA_BUFFERS on output port (status=%"PRIx32" %s)",
- status, mmal_status_to_string(status));
- ret = VLC_EGENERIC;
- goto out;
- }
+static void draw_corners(void * pic_buf, size_t pic_stride, unsigned int x, unsigned int y, unsigned int w, unsigned int h)
+{
+ const unsigned int len = 20;
+ draw_line(pic_buf, pic_stride, x, y, len, 1);
+ draw_line(pic_buf, pic_stride, x, y, len, pic_stride);
+ draw_line(pic_buf, pic_stride, x + w - 1, y, len, -1);
+ draw_line(pic_buf, pic_stride, x + w - 1, y, len, pic_stride);
+ draw_line(pic_buf, pic_stride, x + w - 1, y + h - 1, len, -1);
+ draw_line(pic_buf, pic_stride, x + w - 1, y + h - 1, len, -(int)pic_stride);
+ draw_line(pic_buf, pic_stride, x, y + h - 1, len, 1);
+ draw_line(pic_buf, pic_stride, x, y + h - 1, len, -(int)pic_stride);
+}
+#endif
- msg_Dbg(dec, "Activate zero-copy for output port");
- MMAL_PARAMETER_BOOLEAN_T zero_copy = {
- { MMAL_PARAMETER_ZERO_COPY, sizeof(MMAL_PARAMETER_BOOLEAN_T) },
- 1
- };
+// Buffer either attached to pic or released
+static picture_t * alloc_opaque_pic(decoder_t * const dec, MMAL_BUFFER_HEADER_T * const buf)
+{
+ decoder_sys_t *const dec_sys = dec->p_sys;
+ picture_t * const pic = decoder_NewPicture(dec);
- status = mmal_port_parameter_set(sys->output, &zero_copy.hdr);
- if (status != MMAL_SUCCESS) {
- msg_Err(dec, "Failed to set zero copy on port %s (status=%"PRIx32" %s)",
- sys->output->name, status, mmal_status_to_string(status));
- goto out;
- }
- }
+ if (pic == NULL)
+ goto fail1;
- status = mmal_port_enable(sys->output, output_port_cb);
- if (status != MMAL_SUCCESS) {
- msg_Err(dec, "Failed to enable output port %s (status=%"PRIx32" %s)",
- sys->output->name, status, mmal_status_to_string(status));
- ret = VLC_EGENERIC;
- goto out;
+ if (buf->length == 0) {
+ msg_Err(dec, "%s: Empty buffer", __func__);
+ goto fail2;
}
- status = mmal_component_enable(sys->component);
- if (status != MMAL_SUCCESS) {
- msg_Err(dec, "Failed to enable component %s (status=%"PRIx32" %s)",
- sys->component->name, status, mmal_status_to_string(status));
- ret = VLC_EGENERIC;
- goto out;
- }
+ if ((pic->context = hw_mmal_gen_context(buf, dec_sys->ppr)) == NULL)
+ goto fail2;
- sys->input_pool = mmal_pool_create(sys->input->buffer_num, 0);
+ buf_to_pic_copy_props(pic, buf);
- if (sys->opaque) {
- dec->fmt_out.i_codec = VLC_CODEC_MMAL_OPAQUE;
- dec->fmt_out.video.i_chroma = VLC_CODEC_MMAL_OPAQUE;
- } else {
- dec->fmt_out.i_codec = VLC_CODEC_I420;
- dec->fmt_out.video.i_chroma = VLC_CODEC_I420;
+#if TRACE_ALL
+ msg_Dbg(dec, "pic: prog=%d, tff=%d, date=%lld", pic->b_progressive, pic->b_top_field_first, (long long)pic->date);
+#endif
+
+ return pic;
+
+fail2:
+ picture_Release(pic);
+fail1:
+ // * maybe should recycle rather than release?
+ mmal_buffer_header_release(buf);
+ return NULL;
+}
+
+static void control_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
+{
+ decoder_t *dec = (decoder_t *)port->userdata;
+ MMAL_STATUS_T status;
+
+#if TRACE_ALL
+ msg_Dbg(dec, "<<< %s: cmd=%d, data=%p", __func__, buffer->cmd, buffer->data);
+#endif
+
+ if (buffer->cmd == MMAL_EVENT_ERROR) {
+ status = *(uint32_t *)buffer->data;
+ dec->p_sys->err_stream = status;
+ msg_Err(dec, "MMAL error %"PRIx32" \"%s\"", status,
+ mmal_status_to_string(status));
}
- dec->pf_decode = decode;
- dec->pf_flush = flush_decoder;
+ mmal_buffer_header_release(buffer);
+}
+
+static void input_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
+{
+ block_t * const block = (block_t *)buffer->user_data;
- vlc_sem_init(&sys->sem, 0);
+ (void)port; // Unused
-out:
- if (ret != VLC_SUCCESS)
- CloseDecoder(dec);
+#if TRACE_ALL
+ msg_Dbg((decoder_t *)port->userdata, "<<< %s: cmd=%d, data=%p, len=%d/%d, pts=%lld", __func__,
+ buffer->cmd, buffer->data, buffer->length, buffer->alloc_size, (long long)buffer->pts);
+#endif
- return ret;
+ mmal_buffer_header_reset(buffer);
+ mmal_buffer_header_release(buffer);
+
+ if (block != NULL)
+ block_Release(block);
}
-static void CloseDecoder(decoder_t *dec)
+static void output_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
{
- decoder_sys_t *sys = dec->p_sys;
- MMAL_BUFFER_HEADER_T *buffer;
+ // decoder structure only guaranteed valid if we have contents
- if (!sys)
+ if (buffer->cmd == 0 && buffer->length != 0)
+ {
+ decoder_t * const dec = (decoder_t *)port->userdata;
+
+#if TRACE_ALL
+ msg_Dbg((decoder_t *)port->userdata, "<<< %s: cmd=%d, data=%p, len=%d/%d, pts=%lld", __func__,
+ buffer->cmd, buffer->data, buffer->length, buffer->alloc_size, (long long)buffer->pts);
+#endif
+
+ picture_t *pic = alloc_opaque_pic(dec, buffer);
+#if TRACE_ALL
+ msg_Dbg(dec, "flags=%#x, video flags=%#x", buffer->flags, buffer->type->video.flags);
+#endif
+ if (pic == NULL)
+ msg_Err(dec, "Failed to allocate new picture");
+ else
+ decoder_QueueVideo(dec, pic);
+ // Buffer released or attached to pic - do not release again
return;
+ }
+ else if (buffer->cmd == MMAL_EVENT_FORMAT_CHANGED)
+ {
+ decoder_t * const dec = (decoder_t *)port->userdata;
+ decoder_sys_t * const sys = dec->p_sys;
+ MMAL_EVENT_FORMAT_CHANGED_T * const fmt = mmal_event_format_changed_get(buffer);
+ MMAL_ES_FORMAT_T * const format = mmal_format_alloc();
- if (sys->component && sys->component->control->is_enabled)
- mmal_port_disable(sys->component->control);
+ if (format == NULL)
+ msg_Err(dec, "Failed to allocate new format");
+ else
+ {
+ mmal_format_full_copy(format, fmt->format);
+ format->encoding = MMAL_ENCODING_OPAQUE;
- if (sys->input && sys->input->is_enabled)
- mmal_port_disable(sys->input);
+ if (sys->output_format != NULL)
+ mmal_format_free(sys->output_format);
- if (sys->output && sys->output->is_enabled)
- mmal_port_disable(sys->output);
+ sys->output_format = format;
+ }
+ }
- if (sys->component && sys->component->is_enabled)
- mmal_component_disable(sys->component);
+ mmal_buffer_header_reset(buffer);
+ buffer->user_data = NULL;
+ mmal_buffer_header_release(buffer);
+}
- if (sys->input_pool)
- mmal_pool_destroy(sys->input_pool);
- if (sys->output_format)
- mmal_format_free(sys->output_format);
- if (sys->output_pool)
- mmal_pool_destroy(sys->output_pool);
+static void fill_output_port(decoder_t *dec)
+{
+ decoder_sys_t *sys = dec->p_sys;
- if (sys->component)
- mmal_component_release(sys->component);
+ if (decoder_UpdateVideoFormat(dec) != 0)
+ {
+ // If we have a new format don't bother stuffing the buffer
+ // We should get a reset RSN
+#if TRACE_ALL
+ msg_Dbg(dec, "%s: Updated", __func__);
+#endif
- vlc_sem_destroy(&sys->sem);
- free(sys);
+ return;
+ }
- bcm_host_deinit();
+ hw_mmal_port_pool_ref_fill(sys->ppr);
+ return;
}
static int change_output_format(decoder_t *dec)
{
MMAL_PARAMETER_VIDEO_INTERLACE_TYPE_T interlace_type;
- decoder_sys_t *sys = dec->p_sys;
+ decoder_sys_t * const sys = dec->p_sys;
MMAL_STATUS_T status;
- int pool_size;
int ret = 0;
+#if TRACE_ALL
+ msg_Dbg(dec, "%s: <<<", __func__);
+#endif
+
if (atomic_load(&sys->started)) {
mmal_format_full_copy(sys->output->format, sys->output_format);
status = mmal_port_format_commit(sys->output);
@@ -300,7 +427,9 @@
}
port_reset:
+#if TRACE_ALL
msg_Dbg(dec, "%s: Do full port reset", __func__);
+#endif
status = mmal_port_disable(sys->output);
if (status != MMAL_SUCCESS) {
msg_Err(dec, "Failed to disable output port (status=%"PRIx32" %s)",
@@ -318,15 +447,7 @@
goto out;
}
- if (sys->opaque) {
- sys->output->buffer_num = NUM_DECODER_BUFFER_HEADERS;
- pool_size = NUM_DECODER_BUFFER_HEADERS;
- } else {
- sys->output->buffer_num = __MAX(sys->output->buffer_num_recommended,
- MIN_NUM_BUFFERS_IN_TRANSIT);
- pool_size = sys->output->buffer_num;
- }
-
+ sys->output->buffer_num = NUM_DECODER_BUFFER_HEADERS;
sys->output->buffer_size = sys->output->buffer_size_recommended;
status = mmal_port_enable(sys->output, output_port_cb);
@@ -338,25 +459,14 @@
}
if (!atomic_load(&sys->started)) {
- if (!sys->opaque) {
- sys->output_pool = mmal_port_pool_create(sys->output, pool_size, 0);
- msg_Dbg(dec, "Created output pool with %d pictures", sys->output_pool->headers_num);
- }
-
atomic_store(&sys->started, true);
/* we need one picture from vout for each buffer header on the output
* port */
- dec->i_extra_picture_buffers = pool_size;
-
- /* remove what VLC core reserves as it is part of the pool_size
- * already */
- if (dec->fmt_in.i_codec == VLC_CODEC_H264)
- dec->i_extra_picture_buffers -= 19;
- else
- dec->i_extra_picture_buffers -= 3;
-
+ dec->i_extra_picture_buffers = 10;
+#if TRACE_ALL
msg_Dbg(dec, "Request %d extra pictures", dec->i_extra_picture_buffers);
+#endif
}
apply_fmt:
@@ -382,12 +492,17 @@
sys->b_progressive = (interlace_type.eMode == MMAL_InterlaceProgressive);