-
Notifications
You must be signed in to change notification settings - Fork 9
/
drm_mmal.c
1059 lines (890 loc) · 36 KB
/
drm_mmal.c
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
/*
Copyright (c) 2017 Raspberry Pi (Trading) Ltd
Based on example_basic_2.c from the Userland repo (interface/mmal/test/examples)
Amended to use GEM to allocate the video buffers from CMA, export as dmabufs,
import the dmabufs via vcsm, and then use them with MMAL.
Output frames are then presented to libdrm for display.
Plays an H264 elementary stream with no frame scheduling.
Copyright (c) 2012, Broadcom Europe Ltd
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <poll.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <drm.h>
#include <drm_mode.h>
#include <drm_fourcc.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <interface/vcsm/user-vcsm.h>
#include "bcm_host.h"
#include "interface/mmal/mmal.h"
#include "interface/mmal/util/mmal_default_components.h"
#include "interface/mmal/util/mmal_util_params.h"
#include "interface/mmal/util/mmal_util.h"
#include "interface/mmal/util/mmal_connection.h"
#include "interface/mmal/mmal_queue.h"
#include "interface/vcos/vcos.h"
#include <stdio.h>
//Tested working:
//MMAL_ENCODING_I420 (YU12)
//MMAL_ENCODING_YV12
//MMAL_ENCODING_NV12
//MMAL_ENCODING_I422 (YU16)
//MMAL_ENCODING_BGRA
//MMAL_ENCODING_RGBA
//MMAL_ENCODING_RGB16 (RGB565)
//
//Valid to the ISP, but not DRM:
//MMAL_ENCODING_NV21 (NV12 with cb/cr swapped)
//MMAL_ENCODING_RGB24
//MMAL_ENCODING_BGR24
//Patches sorted for vc4_plane.c for each of these, and then they work.
//
#define ENCODING_FOR_DRM MMAL_ENCODING_YUVUV128
#define DRM_MODULE "vc4"
#define MAX_BUFFERS 3
static inline int warn(const char *file, int line, const char *fmt, ...)
{
int errsv = errno;
va_list va;
va_start(va, fmt);
fprintf(stderr, "WARN(%s:%d): ", file, line);
vfprintf(stderr, fmt, va);
va_end(va);
errno = errsv;
return 1;
}
#define CHECK_CONDITION(cond, ...) \
do { \
if (cond) { \
int errsv = errno; \
fprintf(stderr, "ERROR(%s:%d) : ", \
__FILE__, __LINE__); \
errno = errsv; \
fprintf(stderr, __VA_ARGS__); \
abort(); \
} \
} while(0)
#define WARN_ON(cond, ...) \
((cond) ? warn(__FILE__, __LINE__, __VA_ARGS__) : 0)
#define ERRSTR strerror(errno)
#define CHECK_STATUS(status, ...) WARN_ON(status != MMAL_SUCCESS, __VA_ARGS__); \
if (status != MMAL_SUCCESS) goto error;
static uint8_t codec_header_bytes[512];
static unsigned int codec_header_bytes_size = sizeof(codec_header_bytes);
static FILE *source_file;
/* Macros abstracting the I/O, just to make the example code clearer */
#define SOURCE_OPEN(uri) \
source_file = fopen(uri, "rb"); if (!source_file) goto error;
#define SOURCE_READ_CODEC_CONFIG_DATA(bytes, size) \
size = fread(bytes, 1, size, source_file); rewind(source_file)
#define SOURCE_READ_DATA_INTO_BUFFER(a) \
a->length = fread(a->data, 1, a->alloc_size - 128, source_file); \
a->offset = 0
#define SOURCE_CLOSE() \
if (source_file) fclose(source_file)
/** Context for our application */
static struct CONTEXT_T {
VCOS_SEMAPHORE_T semaphore;
MMAL_QUEUE_T *queue;
MMAL_STATUS_T status;
} context;
struct buffer {
unsigned int bo_handle;
unsigned int fb_handle;
int dbuf_fd;
unsigned int vcsm_handle;
MMAL_BUFFER_HEADER_T *mmal_buffer;
};
struct drm_setup {
int conId;
uint32_t crtcId;
int crtcIdx;
uint32_t planeId;
unsigned int out_fourcc;
MMAL_RECT_T compose;
};
static void log_format(MMAL_ES_FORMAT_T *format, MMAL_PORT_T *port)
{
const char *name_type;
if(port)
fprintf(stderr, "%s:%s:%i", port->component->name,
port->type == MMAL_PORT_TYPE_CONTROL ? "ctr" :
port->type == MMAL_PORT_TYPE_INPUT ? "in" :
port->type == MMAL_PORT_TYPE_OUTPUT ? "out" : "invalid",
(int)port->index);
switch(format->type)
{
case MMAL_ES_TYPE_AUDIO: name_type = "audio"; break;
case MMAL_ES_TYPE_VIDEO: name_type = "video"; break;
case MMAL_ES_TYPE_SUBPICTURE: name_type = "subpicture"; break;
default: name_type = "unknown"; break;
}
fprintf(stderr, "type: %s, fourcc: %4.4s\n", name_type, (char *)&format->encoding);
fprintf(stderr, " bitrate: %i, framed: %i\n", format->bitrate,
!!(format->flags & MMAL_ES_FORMAT_FLAG_FRAMED));
fprintf(stderr, " extra data: %i, %p\n", format->extradata_size, format->extradata);
switch(format->type)
{
case MMAL_ES_TYPE_AUDIO:
fprintf(stderr, " samplerate: %i, channels: %i, bps: %i, block align: %i\n",
format->es->audio.sample_rate, format->es->audio.channels,
format->es->audio.bits_per_sample, format->es->audio.block_align);
break;
case MMAL_ES_TYPE_VIDEO:
fprintf(stderr, " width: %i, height: %i, (%i,%i,%i,%i)\n",
format->es->video.width, format->es->video.height,
format->es->video.crop.x, format->es->video.crop.y,
format->es->video.crop.width, format->es->video.crop.height);
fprintf(stderr, " pixel aspect ratio: %i/%i, frame rate: %i/%i\n",
format->es->video.par.num, format->es->video.par.den,
format->es->video.frame_rate.num, format->es->video.frame_rate.den);
break;
case MMAL_ES_TYPE_SUBPICTURE:
break;
default: break;
}
if(!port)
return;
fprintf(stderr, " buffers num: %i(opt %i, min %i), size: %i(opt %i, min: %i), align: %i\n",
port->buffer_num, port->buffer_num_recommended, port->buffer_num_min,
port->buffer_size, port->buffer_size_recommended, port->buffer_size_min,
port->buffer_alignment_min);
}
uint32_t mmal_encoding_to_drm_fourcc(uint32_t mmal_encoding)
{
switch(mmal_encoding)
{
case MMAL_ENCODING_I420:
return MMAL_FOURCC('Y','U','1','2');
case MMAL_ENCODING_YV12:
return MMAL_FOURCC('Y','V','1','2');
case MMAL_ENCODING_I422:
return MMAL_FOURCC('Y','U','1','6');
case MMAL_ENCODING_YUVUV128:
case MMAL_ENCODING_NV12:
return MMAL_FOURCC('N','V','1','2');
case MMAL_ENCODING_NV21:
return MMAL_FOURCC('N','V','2','1');
case MMAL_ENCODING_RGB16:
return MMAL_FOURCC('R','G','1','6');
case MMAL_ENCODING_RGB24:
return MMAL_FOURCC('B','G','2','4');
case MMAL_ENCODING_BGR24:
return MMAL_FOURCC('R','G','2','4');
case MMAL_ENCODING_BGR32:
case MMAL_ENCODING_BGRA:
return MMAL_FOURCC('X','R','2','4');
case MMAL_ENCODING_RGB32:
case MMAL_ENCODING_RGBA:
return MMAL_FOURCC('X','B','2','4');
case MMAL_ENCODING_OPAQUE:
fprintf(stderr, "MMAL_ENCODING_OPAQUE can't be converted to a DRM compatible format\n");
default:
return 0;
}
}
void mmal_format_to_drm_pitches_offsets(uint32_t *pitches, uint32_t *offsets,
uint64_t *modifiers, uint32_t *bo_handles, MMAL_ES_FORMAT_T *format)
{
switch (format->encoding)
{
// 3 plane YUV formats
case MMAL_ENCODING_I420:
case MMAL_ENCODING_YV12:
pitches[0] = mmal_encoding_width_to_stride(format->encoding, format->es->video.width);
pitches[1] = pitches[0] / 2;
pitches[2] = pitches[1];
offsets[1] = pitches[0] * format->es->video.height;
offsets[2] = offsets[1] + pitches[1] * format->es->video.height/2;
bo_handles[1] = bo_handles[2] = bo_handles[0];
break;
case MMAL_ENCODING_I422:
pitches[0] = mmal_encoding_width_to_stride(format->encoding, format->es->video.width);
pitches[1] = pitches[0] / 2;
pitches[2] = pitches[1];
offsets[1] = pitches[0] * format->es->video.height;
offsets[2] = offsets[1] + pitches[1] * format->es->video.height;
bo_handles[1] = bo_handles[2] = bo_handles[0];
break;
// 2 plane YUV formats
case MMAL_ENCODING_NV12:
case MMAL_ENCODING_NV21:
pitches[0] = mmal_encoding_width_to_stride(format->encoding, format->es->video.width);
pitches[1] = pitches[0];
offsets[1] = pitches[0] * format->es->video.height;
bo_handles[1] = bo_handles[0];
break;
case MMAL_ENCODING_YUVUV128:
pitches[0] = format->es->video.width; //Should be 128, but DRM rejects that.
modifiers[0] = DRM_FORMAT_MOD_BROADCOM_SAND128_COL_HEIGHT(format->es->video.width);
printf("Modifier set as %llu, param %u\n", modifiers[0], fourcc_mod_broadcom_param(modifiers[0]));
modifiers[1] = modifiers[0];
pitches[1] = pitches[0];
offsets[1] = 128 * (format->es->video.height);
bo_handles[1] = bo_handles[0];
break;
default:
pitches[0] = mmal_encoding_width_to_stride(format->encoding, format->es->video.width);
break;
}
printf("MMAL format %08x translates to pitches [%u,%u,%u,%u], and offsets [%u,%u,%u,%u]\n",
format->encoding,
pitches[0],pitches[1],pitches[2],pitches[3],
offsets[0],offsets[1],offsets[2],offsets[3]
);
}
static int buffer_create(struct buffer *b, int drmfd, MMAL_PORT_T *port)
{
struct drm_mode_create_dumb gem;
struct drm_mode_destroy_dumb gem_destroy;
int ret;
memset(&gem, 0, sizeof gem);
gem.width = port->format->es->video.width;
gem.height = port->format->es->video.height;
gem.bpp = 32;
gem.size = port->buffer_size;
ret = ioctl(drmfd, DRM_IOCTL_MODE_CREATE_DUMB, &gem);
if (ret)
{
printf("CREATE_DUMB failed: %s\n", ERRSTR);
return -1;
}
printf("bo %u %ux%u bpp %u size %lu (%u)\n", gem.handle, gem.width, gem.height, gem.bpp, (long)gem.size, port->buffer_size);
b->bo_handle = gem.handle;
struct drm_prime_handle prime;
memset(&prime, 0, sizeof prime);
prime.handle = b->bo_handle;
ret = ioctl(drmfd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &prime);
if (ret)
{
printf("PRIME_HANDLE_TO_FD failed: %s\n", ERRSTR);
goto fail_gem;
}
printf("dbuf_fd = %d\n", prime.fd);
b->dbuf_fd = prime.fd;
uint32_t offsets[4] = { 0 };
uint32_t pitches[4] = { 0 };
uint32_t bo_handles[4] = { b->bo_handle };
uint64_t modifiers[4] = { 0 };
unsigned int fourcc = mmal_encoding_to_drm_fourcc(port->format->encoding);
mmal_format_to_drm_pitches_offsets(pitches, offsets, modifiers, bo_handles, port->format);
fprintf(stderr, "FB fourcc %c%c%c%c\n",
fourcc,
fourcc >> 8,
fourcc >> 16,
fourcc >> 24);
b->vcsm_handle = vcsm_import_dmabuf(b->dbuf_fd, "DRM Buf");
if (!b->vcsm_handle)
goto fail_prime;
ret = drmModeAddFB2WithModifiers(drmfd, port->format->es->video.crop.width,
port->format->es->video.crop.height, fourcc, bo_handles,
pitches, offsets, modifiers, &b->fb_handle, DRM_MODE_FB_MODIFIERS);
if (ret)
{
printf("drmModeAddFB2 failed: %s\n", ERRSTR);
goto fail_vcsm;
}
return 0;
fail_vcsm:
vcsm_free(b->vcsm_handle);
fail_prime:
close(b->dbuf_fd);
fail_gem:
memset(&gem_destroy, 0, sizeof gem_destroy);
gem_destroy.handle = b->bo_handle,
ret = ioctl(drmfd, DRM_IOCTL_MODE_DESTROY_DUMB, &gem_destroy);
if (ret)
{
printf("DESTROY_DUMB failed: %s\n", ERRSTR);
}
return -1;
}
MMAL_POOL_T* pool_create_drm(MMAL_PORT_T *port, struct buffer *buffers, int drmfd)
{
MMAL_POOL_T *pool;
unsigned int i;
pool = mmal_port_pool_create(port,
port->buffer_num,
0);
for (i = 0; i < port->buffer_num; i++)
{
buffer_create(&buffers[i], drmfd, port);
pool->header[i]->data = (uint8_t*)vcsm_vc_hdl_from_hdl(buffers[i].vcsm_handle);
pool->header[i]->alloc_size = port->buffer_size;
pool->header[i]->length = 0;
buffers[i].mmal_buffer = pool->header[i];
}
return pool;
}
void buffer_destroy(int drmfd, struct buffer *buf)
{
struct drm_mode_destroy_dumb gem_destroy;
vcsm_free(buf->vcsm_handle);
close(buf->dbuf_fd);
memset(&gem_destroy, 0, sizeof gem_destroy);
gem_destroy.handle = buf->bo_handle;
ioctl(drmfd, DRM_IOCTL_MODE_DESTROY_DUMB, &gem_destroy);
}
void pool_destroy_drm(MMAL_PORT_T *port, MMAL_POOL_T *pool, struct buffer *buffers, int drmfd)
{
unsigned int i;
for (i = 0; i < pool->headers_num; i++)
{
buffer_destroy(drmfd, &buffers[i]);
}
mmal_port_pool_destroy(port, pool);
}
/** Callback from the control port.
* Component is sending us an event. */
static void control_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
{
struct CONTEXT_T *ctx = (struct CONTEXT_T *)port->userdata;
switch (buffer->cmd)
{
case MMAL_EVENT_EOS:
/* Only sink component generate EOS events */
break;
case MMAL_EVENT_ERROR:
/* Something went wrong. Signal this to the application */
ctx->status = *(MMAL_STATUS_T *)buffer->data;
break;
default:
break;
}
/* Done with the event, recycle it */
mmal_buffer_header_release(buffer);
/* Kick the processing thread */
vcos_semaphore_post(&ctx->semaphore);
}
static int find_crtc(int drmfd, struct drm_setup *s, uint32_t *con)
{
int ret = -1;
int i;
drmModeRes *res = drmModeGetResources(drmfd);
if(!res)
{
printf( "drmModeGetResources failed: %s\n", ERRSTR);
return -1;
}
if (res->count_crtcs <= 0)
{
printf( "drm: no crts\n");
goto fail_res;
}
if (!s->conId) {
fprintf(stderr,
"No connector ID specified. Choosing default from list:\n");
for (i = 0; i < res->count_connectors; i++) {
drmModeConnector *con =
drmModeGetConnector(drmfd, res->connectors[i]);
drmModeEncoder *enc = NULL;
drmModeCrtc *crtc = NULL;
if (con->encoder_id) {
enc = drmModeGetEncoder(drmfd, con->encoder_id);
if (enc->crtc_id) {
crtc = drmModeGetCrtc(drmfd, enc->crtc_id);
}
}
if (!s->conId && crtc) {
s->conId = con->connector_id;
s->crtcId = crtc->crtc_id;
}
printf("Connector %d (crtc %d): type %d, %dx%d%s\n",
con->connector_id,
crtc ? crtc->crtc_id : 0,
con->connector_type,
crtc ? crtc->width : 0,
crtc ? crtc->height : 0,
(s->conId == (int)con->connector_id ?
" (chosen)" : ""));
}
if (!s->conId) {
fprintf(stderr,
"No suitable enabled connector found.\n");
exit(1);
}
}
s->crtcIdx = -1;
for (i = 0; i < res->count_crtcs; ++i) {
if (s->crtcId == res->crtcs[i]) {
s->crtcIdx = i;
break;
}
}
if (WARN_ON(s->crtcIdx == -1, "drm: CRTC %u not found\n", s->crtcId))
goto fail_res;
if (WARN_ON(res->count_connectors <= 0, "drm: no connectors\n"))
goto fail_res;
drmModeConnector *c;
c = drmModeGetConnector(drmfd, s->conId);
if (WARN_ON(!c, "drmModeGetConnector failed: %s\n", ERRSTR))
goto fail_res;
if (WARN_ON(!c->count_modes, "connector supports no mode\n"))
goto fail_conn;
{
drmModeCrtc *crtc = drmModeGetCrtc(drmfd, s->crtcId);
s->compose.x = crtc->x;
s->compose.y = crtc->y;
s->compose.width = crtc->width;
s->compose.height = crtc->height;
drmModeFreeCrtc(crtc);
}
if (con)
*con = c->connector_id;
ret = 0;
fail_conn:
drmModeFreeConnector(c);
fail_res:
drmModeFreeResources(res);
return ret;
}
static int find_plane(int drmfd, struct drm_setup *s)
{
drmModePlaneResPtr planes;
drmModePlanePtr plane;
unsigned int i;
unsigned int j;
int ret = 0;
planes = drmModeGetPlaneResources(drmfd);
if (WARN_ON(!planes, "drmModeGetPlaneResources failed: %s\n", ERRSTR))
return -1;
for (i = 0; i < planes->count_planes; ++i) {
plane = drmModeGetPlane(drmfd, planes->planes[i]);
if (WARN_ON(!planes, "drmModeGetPlane failed: %s\n", ERRSTR))
break;
if (!(plane->possible_crtcs & (1 << s->crtcIdx))) {
drmModeFreePlane(plane);
continue;
}
for (j = 0; j < plane->count_formats; ++j) {
if (plane->formats[j] == s->out_fourcc)
break;
}
if (j == plane->count_formats) {
drmModeFreePlane(plane);
continue;
}
s->planeId = plane->plane_id;
drmModeFreePlane(plane);
break;
}
if (i == planes->count_planes)
ret = -1;
drmModeFreePlaneResources(planes);
return ret;
}
/** Callback from the input port.
* Buffer has been consumed and is available to be used again. */
static void input_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
{
struct CONTEXT_T *ctx = (struct CONTEXT_T *)port->userdata;
/* The decoder is done with the data, just recycle the buffer header into its pool */
mmal_buffer_header_release(buffer);
/* Kick the processing thread */
vcos_semaphore_post(&ctx->semaphore);
}
/** Callback from the output port.
* Buffer has been produced by the port and is available for processing. */
static void output_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
{
struct CONTEXT_T *ctx = (struct CONTEXT_T *)port->userdata;
/* Queue the decoded video frame */
//fprintf(stderr, "Buffer %p returned cmd %08X, length %u\n", buffer, buffer->cmd, buffer->length);
mmal_queue_put(ctx->queue, buffer);
/* Kick the processing thread */
vcos_semaphore_post(&ctx->semaphore);
}
static void drm_mmal_connection_cb(MMAL_CONNECTION_T *connection)
{
struct CONTEXT_T *ctx = (struct CONTEXT_T *)connection->user_data;
vcos_semaphore_post(&ctx->semaphore);
}
static int drm_mmal_create_buffers(MMAL_PORT_T *port, struct buffer *buffers, int drmfd, MMAL_POOL_T **pool_out)
{
MMAL_STATUS_T status;
port->buffer_size = port->buffer_size_min;
printf("Set buffer size to %d\n", port->buffer_size);
status = mmal_port_format_commit(port);
*pool_out = pool_create_drm(port,
buffers, drmfd);
status = mmal_port_enable(port, output_callback);
CHECK_STATUS(status, "failed to enable port");
return 0;
error:
return status;
}
static int drm_mmal_destroy_buffers(MMAL_PORT_T *port, struct buffer *buffers, int drmfd, MMAL_POOL_T *pool_out)
{
MMAL_STATUS_T status;
status = mmal_port_disable(port);
CHECK_STATUS(status, "failed to disable port");
//Clear the queue of all buffers
while(mmal_queue_length(pool_out->queue) != pool_out->headers_num)
{
MMAL_BUFFER_HEADER_T *buf;
fprintf(stderr, "Wait for buffers to be returned. Have %d of %d buffers\n",
mmal_queue_length(pool_out->queue), pool_out->headers_num);
vcos_semaphore_wait(&context.semaphore);
fprintf(stderr, "Got semaphore\n");
buf = mmal_queue_get(context.queue);
printf("Retrieved buf %p\n", buf);
if (buf)
mmal_buffer_header_release(buf);
}
fprintf(stderr, "Got all buffers\n");
pool_destroy_drm(port, pool_out, buffers, drmfd);
return 0;
error:
return status;
}
int main(int argc, char **argv)
{
MMAL_STATUS_T status = MMAL_EINVAL;
MMAL_COMPONENT_T *decoder = NULL, *isp = NULL;
MMAL_POOL_T *pool_in = NULL, *pool_out = NULL;
MMAL_BOOL_T eos_sent = MMAL_FALSE, eos_received = MMAL_FALSE;
struct drm_setup setup = {0};
struct buffer buffers[MAX_BUFFERS];
MMAL_BUFFER_HEADER_T *current_buffer = NULL;
MMAL_CONNECTION_T *connection = NULL;
unsigned int in_count = 0, conn_out_count = 0, conn_in_count = 0, out_count = 0;
int ret;
if (argc < 2)
{
fprintf(stderr, "invalid arguments\n");
return -1;
}
bcm_host_init();
int drmfd = drmOpen(DRM_MODULE, NULL);
CHECK_CONDITION(drmfd < 0, "drmOpen(%s) failed: %s\n", DRM_MODULE, ERRSTR);
vcsm_init();
vcos_semaphore_create(&context.semaphore, "example", 1);
SOURCE_OPEN(argv[1]);
/* Create the decoder component.
* This specific component exposes 2 ports (1 input and 1 output). Like most components
* its expects the format of its input port to be set by the client in order for it to
* know what kind of data it will be fed. */
status = mmal_component_create(MMAL_COMPONENT_DEFAULT_VIDEO_DECODER, &decoder);
CHECK_STATUS(status, "failed to create decoder");
status = mmal_component_create("vc.ril.isp", &isp);
CHECK_STATUS(status, "failed to create isp");
/* Enable control port so we can receive events from the component */
decoder->control->userdata = (void *)&context;
status = mmal_port_enable(decoder->control, control_callback);
CHECK_STATUS(status, "failed to enable control port");
/* Set the zero-copy parameter on the input port */
status = mmal_port_parameter_set_boolean(decoder->input[0], MMAL_PARAMETER_ZERO_COPY, MMAL_TRUE);
CHECK_STATUS(status, "failed to set zero copy - %s", decoder->input[0]->name);
/* Set the zero-copy parameter on the output port */
status = mmal_port_parameter_set_boolean(decoder->output[0], MMAL_PARAMETER_ZERO_COPY, MMAL_TRUE);
CHECK_STATUS(status, "failed to set zero copy - %s", decoder->output[0]->name);
status = mmal_port_parameter_set_boolean(isp->input[0], MMAL_PARAMETER_ZERO_COPY, MMAL_TRUE);
CHECK_STATUS(status, "failed to set zero copy - %s", isp->input[0]->name);
status = mmal_port_parameter_set_boolean(isp->output[0], MMAL_PARAMETER_ZERO_COPY, MMAL_TRUE);
CHECK_STATUS(status, "failed to set zero copy - %s", isp->output[0]->name);
/* Set format of video decoder input port */
MMAL_ES_FORMAT_T *format_in = decoder->input[0]->format;
format_in->type = MMAL_ES_TYPE_VIDEO;
format_in->encoding = MMAL_ENCODING_H264;
format_in->es->video.width = 1280;
format_in->es->video.height = 720;
format_in->es->video.frame_rate.num = 30;
format_in->es->video.frame_rate.den = 1;
format_in->es->video.par.num = 1;
format_in->es->video.par.den = 1;
/* If the data is known to be framed then the following flag should be set:
* format_in->flags |= MMAL_ES_FORMAT_FLAG_FRAMED; */
SOURCE_READ_CODEC_CONFIG_DATA(codec_header_bytes, codec_header_bytes_size);
status = mmal_format_extradata_alloc(format_in, codec_header_bytes_size);
CHECK_STATUS(status, "failed to allocate extradata");
format_in->extradata_size = codec_header_bytes_size;
if (format_in->extradata_size)
memcpy(format_in->extradata, codec_header_bytes, format_in->extradata_size);
status = mmal_port_format_commit(decoder->input[0]);
CHECK_STATUS(status, "failed to commit format");
MMAL_ES_FORMAT_T *format_out = decoder->output[0]->format;
format_out->encoding = MMAL_ENCODING_OPAQUE;
status = mmal_port_format_commit(decoder->output[0]);
CHECK_STATUS(status, "failed to commit format");
status = mmal_connection_create(&connection, decoder->output[0], isp->input[0], 0);
CHECK_STATUS(status, "failed to create connection");
(*connection).callback = drm_mmal_connection_cb;
(*connection).user_data = (void *)&context;
/* Display the output port format */
fprintf(stderr, "%s\n", decoder->output[0]->name);
fprintf(stderr, " type: %i, fourcc: %4.4s\n", format_out->type, (char *)&format_out->encoding);
fprintf(stderr, " bitrate: %i, framed: %i\n", format_out->bitrate,
!!(format_out->flags & MMAL_ES_FORMAT_FLAG_FRAMED));
fprintf(stderr, " extra data: %i, %p\n", format_out->extradata_size, format_out->extradata);
fprintf(stderr, " width: %i, height: %i, (%i,%i,%i,%i)\n",
format_out->es->video.width, format_out->es->video.height,
format_out->es->video.crop.x, format_out->es->video.crop.y,
format_out->es->video.crop.width, format_out->es->video.crop.height);
/* The format of both ports is now set so we can get their buffer requirements and create
* our buffer headers. We use the buffer pool API to create these. */
decoder->input[0]->buffer_num = decoder->input[0]->buffer_num_min;
decoder->input[0]->buffer_size = decoder->input[0]->buffer_size_min;
decoder->output[0]->buffer_num = decoder->output[0]->buffer_num_recommended;
decoder->output[0]->buffer_size = decoder->output[0]->buffer_size_min;
pool_in = mmal_port_pool_create(decoder->input[0],
decoder->input[0]->buffer_num,
decoder->input[0]->buffer_size);
/* Create a queue to store our decoded video frames. The callback we will get when
* a frame has been decoded will put the frame into this queue. */
context.queue = mmal_queue_create();
/* Store a reference to our context in each port (will be used during callbacks) */
decoder->input[0]->userdata = (void *)&context;
/* Enable all the input port and the output port.
* The callback specified here is the function which will be called when the buffer header
* we sent to the component has been processed. */
status = mmal_port_enable(decoder->input[0], input_callback);
CHECK_STATUS(status, "failed to enable input port");
status = mmal_connection_enable(connection);
CHECK_STATUS(status, "failed to enable connection");
/* Component won't start processing data until it is enabled. */
status = mmal_component_enable(decoder);
CHECK_STATUS(status, "failed to enable decoder component");
status = mmal_component_enable(isp);
CHECK_STATUS(status, "failed to enable isp component");
isp->output[0]->userdata = (void *)&context;
mmal_format_full_copy(isp->output[0]->format, isp->input[0]->format);
isp->output[0]->format->encoding = ENCODING_FOR_DRM;
if (ENCODING_FOR_DRM == MMAL_ENCODING_YUVUV128) {
isp->output[0]->format->flags |= MMAL_ES_FORMAT_FLAG_COL_FMTS_WIDTH_IS_COL_STRIDE;
isp->output[0]->format->es->video.width = ((isp->output[0]->format->es->video.height*3)/2);
}
status = mmal_port_format_commit(isp->output[0]);
CHECK_STATUS(status, "failed to set ISP output format");
isp->output[0]->buffer_num = MAX_BUFFERS;
isp->output[0]->buffer_size = isp->output[0]->buffer_size_min;
status = mmal_port_enable(isp->output[0], output_callback);
CHECK_STATUS(status, "failed to enable isp output port");
pool_out = pool_create_drm(isp->output[0], buffers, drmfd);
setup.out_fourcc = mmal_encoding_to_drm_fourcc(ENCODING_FOR_DRM);
uint32_t con;
ret = find_crtc(drmfd, &setup, &con);
CHECK_CONDITION(ret, "failed to find valid mode\n");
ret = find_plane(drmfd, &setup);
CHECK_CONDITION(ret, "failed to find compatible plane\n");
/* Start decoding */
fprintf(stderr, "start decoding\n");
/* This is the main processing loop */
while(!eos_received && out_count < 10000)
{
MMAL_BUFFER_HEADER_T *buffer;
VCOS_STATUS_T vcos_status;
/* Wait for buffer headers to be available on either of the decoder ports */
vcos_status = vcos_semaphore_wait_timeout(&context.semaphore, 2000);
if (vcos_status != VCOS_SUCCESS)
fprintf(stderr, "vcos_semaphore_wait_timeout failed - status %d\n", vcos_status);
/* Check for errors */
if (context.status != MMAL_SUCCESS)
break;
/* Send data to decode to the input port of the video decoder */
if (!eos_sent && (buffer = mmal_queue_get(pool_in->queue)) != NULL)
{
SOURCE_READ_DATA_INTO_BUFFER(buffer);
if(!buffer->length) eos_sent = MMAL_TRUE;
buffer->flags = buffer->length ? 0 : MMAL_BUFFER_HEADER_FLAG_EOS;
buffer->pts = buffer->dts = MMAL_TIME_UNKNOWN;
//fprintf(stderr, "sending %i bytes\n", (int)buffer->length);
status = mmal_port_send_buffer(decoder->input[0], buffer);
CHECK_STATUS(status, "failed to send buffer");
in_count++;
//fprintf(stderr, "Input buffer %p to port %s. in_count %u\n", buffer, decoder->input[0]->name, in_count);
}
/* Get buffers from the connection */
while ((buffer = mmal_queue_get(connection->queue)) != NULL)
{
conn_out_count++;
//fprintf(stderr, "Connection buffer %p to port %s. conn_out_count %u\n", buffer, connection->in->name, conn_out_count);
if (buffer->cmd)
{
fprintf(stderr, "received event %4.4s\n", (char *)&buffer->cmd);
if (buffer->cmd == MMAL_EVENT_FORMAT_CHANGED)
{
MMAL_EVENT_FORMAT_CHANGED_T *event = mmal_event_format_changed_get(buffer);
if (event)
{
fprintf(stderr, "----------Port format changed----------\n");
log_format(decoder->output[0]->format, decoder->output[0]);
fprintf(stderr, "-----------------to---------------------\n");
log_format(event->format, 0);
fprintf(stderr, " buffers num (opt %i, min %i), size (opt %i, min: %i)\n",
event->buffer_num_recommended, event->buffer_num_min,
event->buffer_size_recommended, event->buffer_size_min);
fprintf(stderr, "----------------------------------------\n");
}
status = mmal_connection_event_format_changed(connection, buffer);
//Assume we can't reuse the output buffers, so have to disable, destroy
//pool, create new pool, enable port, feed in buffers.
status = drm_mmal_destroy_buffers(isp->output[0], buffers, drmfd, pool_out);
status = mmal_format_full_copy(isp->output[0]->format, event->format);
isp->output[0]->format->encoding = ENCODING_FOR_DRM;
isp->output[0]->buffer_num = MAX_BUFFERS;
isp->output[0]->buffer_size = isp->output[0]->buffer_size_min;
if (ENCODING_FOR_DRM == MMAL_ENCODING_YUVUV128)
{
isp->output[0]->format->flags |= MMAL_ES_FORMAT_FLAG_COL_FMTS_WIDTH_IS_COL_STRIDE;
isp->output[0]->format->es->video.width = ((isp->output[0]->format->es->video.height*3)/2);
}
if (status == MMAL_SUCCESS)
status = mmal_port_format_commit(isp->output[0]);
if (status != MMAL_SUCCESS)
{
fprintf(stderr, "commit failed on output - %d\n", status);
}
status = drm_mmal_create_buffers(isp->output[0], buffers, drmfd, &pool_out);
}
mmal_buffer_header_release(buffer);
continue;
}
status = mmal_port_send_buffer(connection->in, buffer);
if (status != MMAL_SUCCESS)
{
fprintf(stderr, "mmal_port_send_buffer failed (%i)\n", status);
mmal_queue_put_back(connection->queue, buffer);
goto error;
}
buffer = mmal_queue_get(connection->queue);
}
/* Send empty buffers to the output port of the connection */
if (connection->pool)
{
while ((buffer = mmal_queue_get(connection->pool->queue)) != NULL)
{
//fprintf(stderr, "Returning buffer %p to port %s. conn_in_count %u\n", buffer, connection->out->name, conn_in_count);
status = mmal_port_send_buffer(connection->out, buffer);
if (status != MMAL_SUCCESS)
{
fprintf(stderr, "mmal_port_send_buffer failed (%i)\n", status);
mmal_queue_put_back(connection->pool->queue, buffer);
goto error;
}
buffer = mmal_queue_get(connection->pool->queue);
conn_in_count++;
}
}
/* Get our output frames */
while ((buffer = mmal_queue_get(context.queue)) != NULL)
{
/* We have a frame, do something with it (why not display it for instance?).
* Once we're done with it, we release it. It will automatically go back
* to its original pool so it can be reused for a new video frame.
*/
eos_received = buffer->flags & MMAL_BUFFER_HEADER_FLAG_EOS;
if (buffer->cmd)
{
fprintf(stderr, "received event %4.4s\n", (char *)&buffer->cmd);
if (buffer->cmd == MMAL_EVENT_FORMAT_CHANGED)
{
fprintf(stderr, "Unexpected FORMAT_CHANGED event on the output of ISP\n");
}
mmal_buffer_header_release(buffer);
}
else
{
int index;
//fprintf(stderr, "decoded frame (flags %x) count %d\n", buffer->flags, out_count);
if (buffer)
{
unsigned int i;
for (i = 0; i < pool_out->headers_num; i++) {
if (buffers[i].mmal_buffer == buffer) {
//printf("Matches buffer index %u\n", i);
index = i;
break;
}
}
if (i == pool_out->headers_num) {
printf("Failed to find matching buffer for mmal buffer %p\n", buffer);
continue;
}
}
else
continue;