-
Notifications
You must be signed in to change notification settings - Fork 102
/
helpers.c
1838 lines (1507 loc) · 48.2 KB
/
helpers.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
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
* Copyright (C) 2017 Linaro Ltd.
*/
#include <linux/idr.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/kernel.h>
#include <media/videobuf2-dma-contig.h>
#include <media/v4l2-mem2mem.h>
#include <asm/div64.h>
#include "core.h"
#include "helpers.h"
#include "hfi_helper.h"
#include "pm_helpers.h"
#include "hfi_platform.h"
#include "hfi_parser.h"
#define NUM_MBS_720P (((ALIGN(1280, 16)) >> 4) * ((ALIGN(736, 16)) >> 4))
#define NUM_MBS_4K (((ALIGN(4096, 16)) >> 4) * ((ALIGN(2304, 16)) >> 4))
enum dpb_buf_owner {
DRIVER,
FIRMWARE,
};
struct intbuf {
struct list_head list;
u32 type;
size_t size;
void *va;
dma_addr_t da;
unsigned long attrs;
enum dpb_buf_owner owned_by;
u32 dpb_out_tag;
};
bool venus_helper_check_codec(struct venus_inst *inst, u32 v4l2_pixfmt)
{
struct venus_core *core = inst->core;
u32 session_type = inst->session_type;
u32 codec;
switch (v4l2_pixfmt) {
case V4L2_PIX_FMT_H264:
codec = HFI_VIDEO_CODEC_H264;
break;
case V4L2_PIX_FMT_H263:
codec = HFI_VIDEO_CODEC_H263;
break;
case V4L2_PIX_FMT_MPEG1:
codec = HFI_VIDEO_CODEC_MPEG1;
break;
case V4L2_PIX_FMT_MPEG2:
codec = HFI_VIDEO_CODEC_MPEG2;
break;
case V4L2_PIX_FMT_MPEG4:
codec = HFI_VIDEO_CODEC_MPEG4;
break;
case V4L2_PIX_FMT_VC1_ANNEX_G:
case V4L2_PIX_FMT_VC1_ANNEX_L:
codec = HFI_VIDEO_CODEC_VC1;
break;
case V4L2_PIX_FMT_VP8:
codec = HFI_VIDEO_CODEC_VP8;
break;
case V4L2_PIX_FMT_VP9:
codec = HFI_VIDEO_CODEC_VP9;
break;
case V4L2_PIX_FMT_XVID:
codec = HFI_VIDEO_CODEC_DIVX;
break;
case V4L2_PIX_FMT_HEVC:
codec = HFI_VIDEO_CODEC_HEVC;
break;
default:
return false;
}
if (session_type == VIDC_SESSION_TYPE_ENC && core->enc_codecs & codec)
return true;
if (session_type == VIDC_SESSION_TYPE_DEC && core->dec_codecs & codec)
return true;
return false;
}
EXPORT_SYMBOL_GPL(venus_helper_check_codec);
static void free_dpb_buf(struct venus_inst *inst, struct intbuf *buf)
{
ida_free(&inst->dpb_ids, buf->dpb_out_tag);
list_del_init(&buf->list);
dma_free_attrs(inst->core->dev, buf->size, buf->va, buf->da,
buf->attrs);
kfree(buf);
}
int venus_helper_queue_dpb_bufs(struct venus_inst *inst)
{
struct intbuf *buf, *next;
unsigned int dpb_size = 0;
int ret = 0;
if (inst->dpb_buftype == HFI_BUFFER_OUTPUT)
dpb_size = inst->output_buf_size;
else if (inst->dpb_buftype == HFI_BUFFER_OUTPUT2)
dpb_size = inst->output2_buf_size;
list_for_each_entry_safe(buf, next, &inst->dpbbufs, list) {
struct hfi_frame_data fdata;
memset(&fdata, 0, sizeof(fdata));
fdata.alloc_len = buf->size;
fdata.device_addr = buf->da;
fdata.buffer_type = buf->type;
if (buf->owned_by == FIRMWARE)
continue;
/* free buffer from previous sequence which was released later */
if (dpb_size > buf->size) {
free_dpb_buf(inst, buf);
continue;
}
fdata.clnt_data = buf->dpb_out_tag;
ret = hfi_session_process_buf(inst, &fdata);
if (ret)
goto fail;
buf->owned_by = FIRMWARE;
}
fail:
return ret;
}
EXPORT_SYMBOL_GPL(venus_helper_queue_dpb_bufs);
int venus_helper_free_dpb_bufs(struct venus_inst *inst)
{
struct intbuf *buf, *n;
list_for_each_entry_safe(buf, n, &inst->dpbbufs, list) {
if (buf->owned_by == FIRMWARE)
continue;
free_dpb_buf(inst, buf);
}
if (list_empty(&inst->dpbbufs))
INIT_LIST_HEAD(&inst->dpbbufs);
return 0;
}
EXPORT_SYMBOL_GPL(venus_helper_free_dpb_bufs);
int venus_helper_alloc_dpb_bufs(struct venus_inst *inst)
{
struct venus_core *core = inst->core;
struct device *dev = core->dev;
enum hfi_version ver = core->res->hfi_version;
struct hfi_buffer_requirements bufreq;
u32 buftype = inst->dpb_buftype;
unsigned int dpb_size = 0;
struct intbuf *buf;
unsigned int i;
u32 count;
int ret;
int id;
/* no need to allocate dpb buffers */
if (!inst->dpb_fmt)
return 0;
if (inst->dpb_buftype == HFI_BUFFER_OUTPUT)
dpb_size = inst->output_buf_size;
else if (inst->dpb_buftype == HFI_BUFFER_OUTPUT2)
dpb_size = inst->output2_buf_size;
if (!dpb_size)
return 0;
ret = venus_helper_get_bufreq(inst, buftype, &bufreq);
if (ret)
return ret;
count = HFI_BUFREQ_COUNT_MIN(&bufreq, ver);
for (i = 0; i < count; i++) {
buf = kzalloc(sizeof(*buf), GFP_KERNEL);
if (!buf) {
ret = -ENOMEM;
goto fail;
}
buf->type = buftype;
buf->size = dpb_size;
buf->attrs = DMA_ATTR_WRITE_COMBINE |
DMA_ATTR_NO_KERNEL_MAPPING;
buf->va = dma_alloc_attrs(dev, buf->size, &buf->da, GFP_KERNEL,
buf->attrs);
if (!buf->va) {
ret = -ENOMEM;
goto fail;
}
buf->owned_by = DRIVER;
id = ida_alloc_min(&inst->dpb_ids, VB2_MAX_FRAME, GFP_KERNEL);
if (id < 0) {
ret = id;
goto fail;
}
buf->dpb_out_tag = id;
list_add_tail(&buf->list, &inst->dpbbufs);
}
return 0;
fail:
kfree(buf);
venus_helper_free_dpb_bufs(inst);
return ret;
}
EXPORT_SYMBOL_GPL(venus_helper_alloc_dpb_bufs);
static int intbufs_set_buffer(struct venus_inst *inst, u32 type)
{
struct venus_core *core = inst->core;
struct device *dev = core->dev;
struct hfi_buffer_requirements bufreq;
struct hfi_buffer_desc bd;
struct intbuf *buf;
unsigned int i;
int ret;
ret = venus_helper_get_bufreq(inst, type, &bufreq);
if (ret)
return 0;
if (!bufreq.size)
return 0;
for (i = 0; i < bufreq.count_actual; i++) {
buf = kzalloc(sizeof(*buf), GFP_KERNEL);
if (!buf) {
ret = -ENOMEM;
goto fail;
}
buf->type = bufreq.type;
buf->size = bufreq.size;
buf->attrs = DMA_ATTR_WRITE_COMBINE |
DMA_ATTR_NO_KERNEL_MAPPING;
buf->va = dma_alloc_attrs(dev, buf->size, &buf->da, GFP_KERNEL,
buf->attrs);
if (!buf->va) {
ret = -ENOMEM;
goto fail;
}
memset(&bd, 0, sizeof(bd));
bd.buffer_size = buf->size;
bd.buffer_type = buf->type;
bd.num_buffers = 1;
bd.device_addr = buf->da;
ret = hfi_session_set_buffers(inst, &bd);
if (ret) {
dev_err(dev, "set session buffers failed\n");
goto dma_free;
}
list_add_tail(&buf->list, &inst->internalbufs);
}
return 0;
dma_free:
dma_free_attrs(dev, buf->size, buf->va, buf->da, buf->attrs);
fail:
kfree(buf);
return ret;
}
static int intbufs_unset_buffers(struct venus_inst *inst)
{
struct hfi_buffer_desc bd = {0};
struct intbuf *buf, *n;
int ret = 0;
list_for_each_entry_safe(buf, n, &inst->internalbufs, list) {
bd.buffer_size = buf->size;
bd.buffer_type = buf->type;
bd.num_buffers = 1;
bd.device_addr = buf->da;
bd.response_required = true;
ret = hfi_session_unset_buffers(inst, &bd);
list_del_init(&buf->list);
dma_free_attrs(inst->core->dev, buf->size, buf->va, buf->da,
buf->attrs);
kfree(buf);
}
return ret;
}
static const unsigned int intbuf_types_1xx[] = {
HFI_BUFFER_INTERNAL_SCRATCH(HFI_VERSION_1XX),
HFI_BUFFER_INTERNAL_SCRATCH_1(HFI_VERSION_1XX),
HFI_BUFFER_INTERNAL_SCRATCH_2(HFI_VERSION_1XX),
HFI_BUFFER_INTERNAL_PERSIST,
HFI_BUFFER_INTERNAL_PERSIST_1,
};
static const unsigned int intbuf_types_4xx[] = {
HFI_BUFFER_INTERNAL_SCRATCH(HFI_VERSION_4XX),
HFI_BUFFER_INTERNAL_SCRATCH_1(HFI_VERSION_4XX),
HFI_BUFFER_INTERNAL_SCRATCH_2(HFI_VERSION_4XX),
HFI_BUFFER_INTERNAL_PERSIST,
HFI_BUFFER_INTERNAL_PERSIST_1,
};
static const unsigned int intbuf_types_6xx[] = {
HFI_BUFFER_INTERNAL_SCRATCH(HFI_VERSION_6XX),
HFI_BUFFER_INTERNAL_SCRATCH_1(HFI_VERSION_6XX),
HFI_BUFFER_INTERNAL_SCRATCH_2(HFI_VERSION_6XX),
HFI_BUFFER_INTERNAL_PERSIST,
HFI_BUFFER_INTERNAL_PERSIST_1,
};
int venus_helper_intbufs_alloc(struct venus_inst *inst)
{
const unsigned int *intbuf;
size_t arr_sz, i;
int ret;
if (IS_V6(inst->core)) {
arr_sz = ARRAY_SIZE(intbuf_types_6xx);
intbuf = intbuf_types_6xx;
} else if (IS_V4(inst->core)) {
arr_sz = ARRAY_SIZE(intbuf_types_4xx);
intbuf = intbuf_types_4xx;
} else {
arr_sz = ARRAY_SIZE(intbuf_types_1xx);
intbuf = intbuf_types_1xx;
}
for (i = 0; i < arr_sz; i++) {
ret = intbufs_set_buffer(inst, intbuf[i]);
if (ret)
goto error;
}
return 0;
error:
intbufs_unset_buffers(inst);
return ret;
}
EXPORT_SYMBOL_GPL(venus_helper_intbufs_alloc);
int venus_helper_intbufs_free(struct venus_inst *inst)
{
return intbufs_unset_buffers(inst);
}
EXPORT_SYMBOL_GPL(venus_helper_intbufs_free);
int venus_helper_intbufs_realloc(struct venus_inst *inst)
{
enum hfi_version ver = inst->core->res->hfi_version;
struct hfi_buffer_desc bd;
struct intbuf *buf, *n;
int ret;
list_for_each_entry_safe(buf, n, &inst->internalbufs, list) {
if (buf->type == HFI_BUFFER_INTERNAL_PERSIST ||
buf->type == HFI_BUFFER_INTERNAL_PERSIST_1)
continue;
memset(&bd, 0, sizeof(bd));
bd.buffer_size = buf->size;
bd.buffer_type = buf->type;
bd.num_buffers = 1;
bd.device_addr = buf->da;
bd.response_required = true;
ret = hfi_session_unset_buffers(inst, &bd);
dma_free_attrs(inst->core->dev, buf->size, buf->va, buf->da,
buf->attrs);
list_del_init(&buf->list);
kfree(buf);
}
ret = intbufs_set_buffer(inst, HFI_BUFFER_INTERNAL_SCRATCH(ver));
if (ret)
goto err;
ret = intbufs_set_buffer(inst, HFI_BUFFER_INTERNAL_SCRATCH_1(ver));
if (ret)
goto err;
ret = intbufs_set_buffer(inst, HFI_BUFFER_INTERNAL_SCRATCH_2(ver));
if (ret)
goto err;
return 0;
err:
return ret;
}
EXPORT_SYMBOL_GPL(venus_helper_intbufs_realloc);
static void fill_buffer_desc(const struct venus_buffer *buf,
struct hfi_buffer_desc *bd, bool response)
{
memset(bd, 0, sizeof(*bd));
bd->buffer_type = HFI_BUFFER_OUTPUT;
bd->buffer_size = buf->size;
bd->num_buffers = 1;
bd->device_addr = buf->dma_addr;
bd->response_required = response;
}
static void return_buf_error(struct venus_inst *inst,
struct vb2_v4l2_buffer *vbuf)
{
struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
if (vbuf->vb2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
v4l2_m2m_src_buf_remove_by_buf(m2m_ctx, vbuf);
else
v4l2_m2m_dst_buf_remove_by_buf(m2m_ctx, vbuf);
v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
}
static void
put_ts_metadata(struct venus_inst *inst, struct vb2_v4l2_buffer *vbuf)
{
struct vb2_buffer *vb = &vbuf->vb2_buf;
unsigned int i;
int slot = -1;
u64 ts_us = vb->timestamp;
for (i = 0; i < ARRAY_SIZE(inst->tss); i++) {
if (!inst->tss[i].used) {
slot = i;
break;
}
}
if (slot == -1) {
dev_dbg(inst->core->dev, VDBGL "no free slot\n");
return;
}
do_div(ts_us, NSEC_PER_USEC);
inst->tss[slot].used = true;
inst->tss[slot].flags = vbuf->flags;
inst->tss[slot].tc = vbuf->timecode;
inst->tss[slot].ts_us = ts_us;
inst->tss[slot].ts_ns = vb->timestamp;
}
void venus_helper_get_ts_metadata(struct venus_inst *inst, u64 timestamp_us,
struct vb2_v4l2_buffer *vbuf)
{
struct vb2_buffer *vb = &vbuf->vb2_buf;
unsigned int i;
for (i = 0; i < ARRAY_SIZE(inst->tss); ++i) {
if (!inst->tss[i].used)
continue;
if (inst->tss[i].ts_us != timestamp_us)
continue;
inst->tss[i].used = false;
vbuf->flags |= inst->tss[i].flags;
vbuf->timecode = inst->tss[i].tc;
vb->timestamp = inst->tss[i].ts_ns;
break;
}
}
EXPORT_SYMBOL_GPL(venus_helper_get_ts_metadata);
static int
session_process_buf(struct venus_inst *inst, struct vb2_v4l2_buffer *vbuf)
{
struct venus_buffer *buf = to_venus_buffer(vbuf);
struct vb2_buffer *vb = &vbuf->vb2_buf;
unsigned int type = vb->type;
struct hfi_frame_data fdata;
int ret;
memset(&fdata, 0, sizeof(fdata));
fdata.alloc_len = buf->size;
fdata.device_addr = buf->dma_addr;
fdata.timestamp = vb->timestamp;
do_div(fdata.timestamp, NSEC_PER_USEC);
fdata.flags = 0;
fdata.clnt_data = vbuf->vb2_buf.index;
if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
fdata.buffer_type = HFI_BUFFER_INPUT;
fdata.filled_len = vb2_get_plane_payload(vb, 0);
fdata.offset = vb->planes[0].data_offset;
if (vbuf->flags & V4L2_BUF_FLAG_LAST || !fdata.filled_len)
fdata.flags |= HFI_BUFFERFLAG_EOS;
if (inst->session_type == VIDC_SESSION_TYPE_DEC)
put_ts_metadata(inst, vbuf);
venus_pm_load_scale(inst);
} else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
if (inst->session_type == VIDC_SESSION_TYPE_ENC)
fdata.buffer_type = HFI_BUFFER_OUTPUT;
else
fdata.buffer_type = inst->opb_buftype;
fdata.filled_len = 0;
fdata.offset = 0;
}
ret = hfi_session_process_buf(inst, &fdata);
if (ret)
return ret;
return 0;
}
static bool is_dynamic_bufmode(struct venus_inst *inst)
{
struct venus_core *core = inst->core;
struct hfi_plat_caps *caps;
/*
* v4 doesn't send BUFFER_ALLOC_MODE_SUPPORTED property and supports
* dynamic buffer mode by default for HFI_BUFFER_OUTPUT/OUTPUT2.
*/
if (IS_V4(core) || IS_V6(core))
return true;
caps = venus_caps_by_codec(core, inst->hfi_codec, inst->session_type);
if (!caps)
return false;
return caps->cap_bufs_mode_dynamic;
}
int venus_helper_unregister_bufs(struct venus_inst *inst)
{
struct venus_buffer *buf, *n;
struct hfi_buffer_desc bd;
int ret = 0;
if (is_dynamic_bufmode(inst))
return 0;
list_for_each_entry_safe(buf, n, &inst->registeredbufs, reg_list) {
fill_buffer_desc(buf, &bd, true);
ret = hfi_session_unset_buffers(inst, &bd);
list_del_init(&buf->reg_list);
}
return ret;
}
EXPORT_SYMBOL_GPL(venus_helper_unregister_bufs);
static int session_register_bufs(struct venus_inst *inst)
{
struct venus_core *core = inst->core;
struct device *dev = core->dev;
struct hfi_buffer_desc bd;
struct venus_buffer *buf;
int ret = 0;
if (is_dynamic_bufmode(inst))
return 0;
list_for_each_entry(buf, &inst->registeredbufs, reg_list) {
fill_buffer_desc(buf, &bd, false);
ret = hfi_session_set_buffers(inst, &bd);
if (ret) {
dev_err(dev, "%s: set buffer failed\n", __func__);
break;
}
}
return ret;
}
static u32 to_hfi_raw_fmt(u32 v4l2_fmt)
{
switch (v4l2_fmt) {
case V4L2_PIX_FMT_NV12:
return HFI_COLOR_FORMAT_NV12;
case V4L2_PIX_FMT_NV21:
return HFI_COLOR_FORMAT_NV21;
case V4L2_PIX_FMT_QC08C:
return HFI_COLOR_FORMAT_NV12_UBWC;
case V4L2_PIX_FMT_QC10C:
return HFI_COLOR_FORMAT_YUV420_TP10_UBWC;
default:
break;
}
return 0;
}
static int platform_get_bufreq(struct venus_inst *inst, u32 buftype,
struct hfi_buffer_requirements *req)
{
enum hfi_version version = inst->core->res->hfi_version;
const struct hfi_platform *hfi_plat;
struct hfi_plat_buffers_params params;
bool is_dec = inst->session_type == VIDC_SESSION_TYPE_DEC;
struct venc_controls *enc_ctr = &inst->controls.enc;
hfi_plat = hfi_platform_get(version);
if (!hfi_plat || !hfi_plat->bufreq)
return -EINVAL;
params.version = version;
params.num_vpp_pipes = inst->core->res->num_vpp_pipes;
if (is_dec) {
params.width = inst->width;
params.height = inst->height;
params.codec = inst->fmt_out->pixfmt;
params.hfi_color_fmt = to_hfi_raw_fmt(inst->fmt_cap->pixfmt);
params.dec.max_mbs_per_frame = mbs_per_frame_max(inst);
params.dec.buffer_size_limit = 0;
params.dec.is_secondary_output =
inst->opb_buftype == HFI_BUFFER_OUTPUT2;
params.dec.is_interlaced =
inst->pic_struct != HFI_INTERLACE_FRAME_PROGRESSIVE;
} else {
params.width = inst->out_width;
params.height = inst->out_height;
params.codec = inst->fmt_cap->pixfmt;
params.hfi_color_fmt = to_hfi_raw_fmt(inst->fmt_out->pixfmt);
params.enc.work_mode = VIDC_WORK_MODE_2;
params.enc.rc_type = HFI_RATE_CONTROL_OFF;
if (enc_ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CQ)
params.enc.rc_type = HFI_RATE_CONTROL_CQ;
params.enc.num_b_frames = enc_ctr->num_b_frames;
params.enc.is_tenbit = inst->bit_depth == VIDC_BITDEPTH_10;
}
return hfi_plat->bufreq(¶ms, inst->session_type, buftype, req);
}
int venus_helper_get_bufreq(struct venus_inst *inst, u32 type,
struct hfi_buffer_requirements *req)
{
u32 ptype = HFI_PROPERTY_CONFIG_BUFFER_REQUIREMENTS;
union hfi_get_property hprop;
unsigned int i;
int ret;
memset(req, 0, sizeof(*req));
if (type == HFI_BUFFER_OUTPUT || type == HFI_BUFFER_OUTPUT2)
req->count_min = inst->fw_min_cnt;
ret = platform_get_bufreq(inst, type, req);
if (!ret) {
if (type == HFI_BUFFER_OUTPUT || type == HFI_BUFFER_OUTPUT2)
inst->fw_min_cnt = req->count_min;
return 0;
}
ret = hfi_session_get_property(inst, ptype, &hprop);
if (ret)
return ret;
ret = -EINVAL;
for (i = 0; i < HFI_BUFFER_TYPE_MAX; i++) {
if (hprop.bufreq[i].type != type)
continue;
memcpy(req, &hprop.bufreq[i], sizeof(*req));
ret = 0;
break;
}
return ret;
}
EXPORT_SYMBOL_GPL(venus_helper_get_bufreq);
struct id_mapping {
u32 hfi_id;
u32 v4l2_id;
};
static const struct id_mapping mpeg4_profiles[] = {
{ HFI_MPEG4_PROFILE_SIMPLE, V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE },
{ HFI_MPEG4_PROFILE_ADVANCEDSIMPLE, V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE },
};
static const struct id_mapping mpeg4_levels[] = {
{ HFI_MPEG4_LEVEL_0, V4L2_MPEG_VIDEO_MPEG4_LEVEL_0 },
{ HFI_MPEG4_LEVEL_0b, V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B },
{ HFI_MPEG4_LEVEL_1, V4L2_MPEG_VIDEO_MPEG4_LEVEL_1 },
{ HFI_MPEG4_LEVEL_2, V4L2_MPEG_VIDEO_MPEG4_LEVEL_2 },
{ HFI_MPEG4_LEVEL_3, V4L2_MPEG_VIDEO_MPEG4_LEVEL_3 },
{ HFI_MPEG4_LEVEL_4, V4L2_MPEG_VIDEO_MPEG4_LEVEL_4 },
{ HFI_MPEG4_LEVEL_5, V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 },
};
static const struct id_mapping mpeg2_profiles[] = {
{ HFI_MPEG2_PROFILE_SIMPLE, V4L2_MPEG_VIDEO_MPEG2_PROFILE_SIMPLE },
{ HFI_MPEG2_PROFILE_MAIN, V4L2_MPEG_VIDEO_MPEG2_PROFILE_MAIN },
{ HFI_MPEG2_PROFILE_SNR, V4L2_MPEG_VIDEO_MPEG2_PROFILE_SNR_SCALABLE },
{ HFI_MPEG2_PROFILE_SPATIAL, V4L2_MPEG_VIDEO_MPEG2_PROFILE_SPATIALLY_SCALABLE },
{ HFI_MPEG2_PROFILE_HIGH, V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH },
};
static const struct id_mapping mpeg2_levels[] = {
{ HFI_MPEG2_LEVEL_LL, V4L2_MPEG_VIDEO_MPEG2_LEVEL_LOW },
{ HFI_MPEG2_LEVEL_ML, V4L2_MPEG_VIDEO_MPEG2_LEVEL_MAIN },
{ HFI_MPEG2_LEVEL_H14, V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH_1440 },
{ HFI_MPEG2_LEVEL_HL, V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH },
};
static const struct id_mapping h264_profiles[] = {
{ HFI_H264_PROFILE_BASELINE, V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE },
{ HFI_H264_PROFILE_MAIN, V4L2_MPEG_VIDEO_H264_PROFILE_MAIN },
{ HFI_H264_PROFILE_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_HIGH },
{ HFI_H264_PROFILE_STEREO_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH },
{ HFI_H264_PROFILE_MULTIVIEW_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH },
{ HFI_H264_PROFILE_CONSTRAINED_BASE, V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE },
{ HFI_H264_PROFILE_CONSTRAINED_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_HIGH },
};
static const struct id_mapping h264_levels[] = {
{ HFI_H264_LEVEL_1, V4L2_MPEG_VIDEO_H264_LEVEL_1_0 },
{ HFI_H264_LEVEL_1b, V4L2_MPEG_VIDEO_H264_LEVEL_1B },
{ HFI_H264_LEVEL_11, V4L2_MPEG_VIDEO_H264_LEVEL_1_1 },
{ HFI_H264_LEVEL_12, V4L2_MPEG_VIDEO_H264_LEVEL_1_2 },
{ HFI_H264_LEVEL_13, V4L2_MPEG_VIDEO_H264_LEVEL_1_3 },
{ HFI_H264_LEVEL_2, V4L2_MPEG_VIDEO_H264_LEVEL_2_0 },
{ HFI_H264_LEVEL_21, V4L2_MPEG_VIDEO_H264_LEVEL_2_1 },
{ HFI_H264_LEVEL_22, V4L2_MPEG_VIDEO_H264_LEVEL_2_2 },
{ HFI_H264_LEVEL_3, V4L2_MPEG_VIDEO_H264_LEVEL_3_0 },
{ HFI_H264_LEVEL_31, V4L2_MPEG_VIDEO_H264_LEVEL_3_1 },
{ HFI_H264_LEVEL_32, V4L2_MPEG_VIDEO_H264_LEVEL_3_2 },
{ HFI_H264_LEVEL_4, V4L2_MPEG_VIDEO_H264_LEVEL_4_0 },
{ HFI_H264_LEVEL_41, V4L2_MPEG_VIDEO_H264_LEVEL_4_1 },
{ HFI_H264_LEVEL_42, V4L2_MPEG_VIDEO_H264_LEVEL_4_2 },
{ HFI_H264_LEVEL_5, V4L2_MPEG_VIDEO_H264_LEVEL_5_0 },
{ HFI_H264_LEVEL_51, V4L2_MPEG_VIDEO_H264_LEVEL_5_1 },
{ HFI_H264_LEVEL_52, V4L2_MPEG_VIDEO_H264_LEVEL_5_1 },
};
static const struct id_mapping hevc_profiles[] = {
{ HFI_HEVC_PROFILE_MAIN, V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN },
{ HFI_HEVC_PROFILE_MAIN_STILL_PIC, V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE },
{ HFI_HEVC_PROFILE_MAIN10, V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10 },
};
static const struct id_mapping hevc_levels[] = {
{ HFI_HEVC_LEVEL_1, V4L2_MPEG_VIDEO_HEVC_LEVEL_1 },
{ HFI_HEVC_LEVEL_2, V4L2_MPEG_VIDEO_HEVC_LEVEL_2 },
{ HFI_HEVC_LEVEL_21, V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1 },
{ HFI_HEVC_LEVEL_3, V4L2_MPEG_VIDEO_HEVC_LEVEL_3 },
{ HFI_HEVC_LEVEL_31, V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1 },
{ HFI_HEVC_LEVEL_4, V4L2_MPEG_VIDEO_HEVC_LEVEL_4 },
{ HFI_HEVC_LEVEL_41, V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1 },
{ HFI_HEVC_LEVEL_5, V4L2_MPEG_VIDEO_HEVC_LEVEL_5 },
{ HFI_HEVC_LEVEL_51, V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1 },
{ HFI_HEVC_LEVEL_52, V4L2_MPEG_VIDEO_HEVC_LEVEL_5_2 },
{ HFI_HEVC_LEVEL_6, V4L2_MPEG_VIDEO_HEVC_LEVEL_6 },
{ HFI_HEVC_LEVEL_61, V4L2_MPEG_VIDEO_HEVC_LEVEL_6_1 },
{ HFI_HEVC_LEVEL_62, V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2 },
};
static const struct id_mapping vp8_profiles[] = {
{ HFI_VPX_PROFILE_VERSION_0, V4L2_MPEG_VIDEO_VP8_PROFILE_0 },
{ HFI_VPX_PROFILE_VERSION_1, V4L2_MPEG_VIDEO_VP8_PROFILE_1 },
{ HFI_VPX_PROFILE_VERSION_2, V4L2_MPEG_VIDEO_VP8_PROFILE_2 },
{ HFI_VPX_PROFILE_VERSION_3, V4L2_MPEG_VIDEO_VP8_PROFILE_3 },
};
static const struct id_mapping vp9_profiles[] = {
{ HFI_VP9_PROFILE_P0, V4L2_MPEG_VIDEO_VP9_PROFILE_0 },
{ HFI_VP9_PROFILE_P2_10B, V4L2_MPEG_VIDEO_VP9_PROFILE_2 },
};
static const struct id_mapping vp9_levels[] = {
{ HFI_VP9_LEVEL_1, V4L2_MPEG_VIDEO_VP9_LEVEL_1_0 },
{ HFI_VP9_LEVEL_11, V4L2_MPEG_VIDEO_VP9_LEVEL_1_1 },
{ HFI_VP9_LEVEL_2, V4L2_MPEG_VIDEO_VP9_LEVEL_2_0},
{ HFI_VP9_LEVEL_21, V4L2_MPEG_VIDEO_VP9_LEVEL_2_1 },
{ HFI_VP9_LEVEL_3, V4L2_MPEG_VIDEO_VP9_LEVEL_3_0},
{ HFI_VP9_LEVEL_31, V4L2_MPEG_VIDEO_VP9_LEVEL_3_1 },
{ HFI_VP9_LEVEL_4, V4L2_MPEG_VIDEO_VP9_LEVEL_4_0 },
{ HFI_VP9_LEVEL_41, V4L2_MPEG_VIDEO_VP9_LEVEL_4_1 },
{ HFI_VP9_LEVEL_5, V4L2_MPEG_VIDEO_VP9_LEVEL_5_0 },
{ HFI_VP9_LEVEL_51, V4L2_MPEG_VIDEO_VP9_LEVEL_5_1 },
{ HFI_VP9_LEVEL_6, V4L2_MPEG_VIDEO_VP9_LEVEL_6_0 },
{ HFI_VP9_LEVEL_61, V4L2_MPEG_VIDEO_VP9_LEVEL_6_1 },
};
static u32 find_v4l2_id(u32 hfi_id, const struct id_mapping *array, unsigned int array_sz)
{
unsigned int i;
if (!array || !array_sz)
return 0;
for (i = 0; i < array_sz; i++)
if (hfi_id == array[i].hfi_id)
return array[i].v4l2_id;
return 0;
}
static u32 find_hfi_id(u32 v4l2_id, const struct id_mapping *array, unsigned int array_sz)
{
unsigned int i;
if (!array || !array_sz)
return 0;
for (i = 0; i < array_sz; i++)
if (v4l2_id == array[i].v4l2_id)
return array[i].hfi_id;
return 0;
}
static void
v4l2_id_profile_level(u32 hfi_codec, struct hfi_profile_level *pl, u32 *profile, u32 *level)
{
u32 hfi_pf = pl->profile;
u32 hfi_lvl = pl->level;
switch (hfi_codec) {
case HFI_VIDEO_CODEC_H264:
*profile = find_v4l2_id(hfi_pf, h264_profiles, ARRAY_SIZE(h264_profiles));
*level = find_v4l2_id(hfi_lvl, h264_levels, ARRAY_SIZE(h264_levels));
break;
case HFI_VIDEO_CODEC_MPEG2:
*profile = find_v4l2_id(hfi_pf, mpeg2_profiles, ARRAY_SIZE(mpeg2_profiles));
*level = find_v4l2_id(hfi_lvl, mpeg2_levels, ARRAY_SIZE(mpeg2_levels));
break;
case HFI_VIDEO_CODEC_MPEG4:
*profile = find_v4l2_id(hfi_pf, mpeg4_profiles, ARRAY_SIZE(mpeg4_profiles));
*level = find_v4l2_id(hfi_lvl, mpeg4_levels, ARRAY_SIZE(mpeg4_levels));
break;
case HFI_VIDEO_CODEC_VP8:
*profile = find_v4l2_id(hfi_pf, vp8_profiles, ARRAY_SIZE(vp8_profiles));
*level = 0;
break;
case HFI_VIDEO_CODEC_VP9:
*profile = find_v4l2_id(hfi_pf, vp9_profiles, ARRAY_SIZE(vp9_profiles));
*level = find_v4l2_id(hfi_lvl, vp9_levels, ARRAY_SIZE(vp9_levels));
break;
case HFI_VIDEO_CODEC_HEVC:
*profile = find_v4l2_id(hfi_pf, hevc_profiles, ARRAY_SIZE(hevc_profiles));
*level = find_v4l2_id(hfi_lvl, hevc_levels, ARRAY_SIZE(hevc_levels));
break;
default:
break;
}
}
static void
hfi_id_profile_level(u32 hfi_codec, u32 v4l2_pf, u32 v4l2_lvl, struct hfi_profile_level *pl)
{
switch (hfi_codec) {
case HFI_VIDEO_CODEC_H264:
pl->profile = find_hfi_id(v4l2_pf, h264_profiles, ARRAY_SIZE(h264_profiles));
pl->level = find_hfi_id(v4l2_lvl, h264_levels, ARRAY_SIZE(h264_levels));
break;
case HFI_VIDEO_CODEC_MPEG2:
pl->profile = find_hfi_id(v4l2_pf, mpeg2_profiles, ARRAY_SIZE(mpeg2_profiles));
pl->level = find_hfi_id(v4l2_lvl, mpeg2_levels, ARRAY_SIZE(mpeg2_levels));
break;
case HFI_VIDEO_CODEC_MPEG4:
pl->profile = find_hfi_id(v4l2_pf, mpeg4_profiles, ARRAY_SIZE(mpeg4_profiles));
pl->level = find_hfi_id(v4l2_lvl, mpeg4_levels, ARRAY_SIZE(mpeg4_levels));
break;
case HFI_VIDEO_CODEC_VP8:
pl->profile = find_hfi_id(v4l2_pf, vp8_profiles, ARRAY_SIZE(vp8_profiles));
pl->level = 0;
break;
case HFI_VIDEO_CODEC_VP9:
pl->profile = find_hfi_id(v4l2_pf, vp9_profiles, ARRAY_SIZE(vp9_profiles));
pl->level = find_hfi_id(v4l2_lvl, vp9_levels, ARRAY_SIZE(vp9_levels));
break;
case HFI_VIDEO_CODEC_HEVC:
pl->profile = find_hfi_id(v4l2_pf, hevc_profiles, ARRAY_SIZE(hevc_profiles));
pl->level = find_hfi_id(v4l2_lvl, hevc_levels, ARRAY_SIZE(hevc_levels));
break;
default:
break;
}
}
int venus_helper_get_profile_level(struct venus_inst *inst, u32 *profile, u32 *level)
{
const u32 ptype = HFI_PROPERTY_PARAM_PROFILE_LEVEL_CURRENT;
union hfi_get_property hprop;
int ret;
ret = hfi_session_get_property(inst, ptype, &hprop);
if (ret)
return ret;
v4l2_id_profile_level(inst->hfi_codec, &hprop.profile_level, profile, level);
return 0;
}
EXPORT_SYMBOL_GPL(venus_helper_get_profile_level);
int venus_helper_set_profile_level(struct venus_inst *inst, u32 profile, u32 level)
{
const u32 ptype = HFI_PROPERTY_PARAM_PROFILE_LEVEL_CURRENT;
struct hfi_profile_level pl;
hfi_id_profile_level(inst->hfi_codec, profile, level, &pl);
return hfi_session_set_property(inst, ptype, &pl);
}
EXPORT_SYMBOL_GPL(venus_helper_set_profile_level);
static u32 get_framesize_raw_nv12(u32 width, u32 height)
{
u32 y_stride, uv_stride, y_plane;
u32 y_sclines, uv_sclines, uv_plane;
u32 size;
y_stride = ALIGN(width, 128);
uv_stride = ALIGN(width, 128);
y_sclines = ALIGN(height, 32);
uv_sclines = ALIGN(((height + 1) >> 1), 16);
y_plane = y_stride * y_sclines;
uv_plane = uv_stride * uv_sclines + SZ_4K;
size = y_plane + uv_plane + SZ_8K;
return ALIGN(size, SZ_4K);
}
static u32 get_framesize_raw_nv12_ubwc(u32 width, u32 height)
{
u32 y_meta_stride, y_meta_plane;
u32 y_stride, y_plane;
u32 uv_meta_stride, uv_meta_plane;
u32 uv_stride, uv_plane;
u32 extradata = SZ_16K;
y_meta_stride = ALIGN(DIV_ROUND_UP(width, 32), 64);
y_meta_plane = y_meta_stride * ALIGN(DIV_ROUND_UP(height, 8), 16);
y_meta_plane = ALIGN(y_meta_plane, SZ_4K);
y_stride = ALIGN(width, 128);
y_plane = ALIGN(y_stride * ALIGN(height, 32), SZ_4K);
uv_meta_stride = ALIGN(DIV_ROUND_UP(width / 2, 16), 64);
uv_meta_plane = uv_meta_stride * ALIGN(DIV_ROUND_UP(height / 2, 8), 16);
uv_meta_plane = ALIGN(uv_meta_plane, SZ_4K);
uv_stride = ALIGN(width, 128);
uv_plane = ALIGN(uv_stride * ALIGN(height / 2, 32), SZ_4K);
return ALIGN(y_meta_plane + y_plane + uv_meta_plane + uv_plane +
max(extradata, y_stride * 48), SZ_4K);
}
static u32 get_framesize_raw_p010(u32 width, u32 height)
{
u32 y_plane, uv_plane, y_stride, uv_stride, y_sclines, uv_sclines;
y_stride = ALIGN(width * 2, 256);
uv_stride = ALIGN(width * 2, 256);
y_sclines = ALIGN(height, 32);
uv_sclines = ALIGN((height + 1) >> 1, 16);
y_plane = y_stride * y_sclines;
uv_plane = uv_stride * uv_sclines;
return ALIGN((y_plane + uv_plane), SZ_4K);
}