aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/media/atomisp/pci/atomisp2/css2400/runtime/frame/src/frame.c
blob: 7562beadabefa162f4953312b1eb89c7db67d0c8 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
#ifndef ISP2401
/*
 * Support for Intel Camera Imaging ISP subsystem.
 * Copyright (c) 2015, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 */
#else
/*
Support for Intel Camera Imaging ISP subsystem.
Copyright (c) 2010 - 2015, Intel Corporation.

This program is free software; you can redistribute it and/or modify it
under the terms and conditions of the GNU General Public License,
version 2, as published by the Free Software Foundation.

This program is distributed in the hope it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
more details.
*/
#endif

#include "ia_css_frame.h"
#include <math_support.h>
#include "assert_support.h"
#include "ia_css_debug.h"
#include "isp.h"
#include "sh_css_internal.h"
#include "memory_access.h"


#define NV12_TILEY_TILE_WIDTH  128
#define NV12_TILEY_TILE_HEIGHT  32

/**************************************************************************
**	Static functions declarations
**************************************************************************/
static void frame_init_plane(struct ia_css_frame_plane *plane,
	unsigned int width,
	unsigned int stride,
	unsigned int height,
	unsigned int offset);

static void frame_init_single_plane(struct ia_css_frame *frame,
	struct ia_css_frame_plane *plane,
	unsigned int height,
	unsigned int subpixels_per_line,
	unsigned int bytes_per_pixel);

static void frame_init_raw_single_plane(
	struct ia_css_frame *frame,
	struct ia_css_frame_plane *plane,
	unsigned int height,
	unsigned int subpixels_per_line,
	unsigned int bits_per_pixel);

static void frame_init_mipi_plane(struct ia_css_frame *frame,
	struct ia_css_frame_plane *plane,
	unsigned int height,
	unsigned int subpixels_per_line,
	unsigned int bytes_per_pixel);

static void frame_init_nv_planes(struct ia_css_frame *frame,
				 unsigned int horizontal_decimation,
				 unsigned int vertical_decimation,
				 unsigned int bytes_per_element);

static void frame_init_yuv_planes(struct ia_css_frame *frame,
	unsigned int horizontal_decimation,
	unsigned int vertical_decimation,
	bool swap_uv,
	unsigned int bytes_per_element);

static void frame_init_rgb_planes(struct ia_css_frame *frame,
	unsigned int bytes_per_element);

static void frame_init_qplane6_planes(struct ia_css_frame *frame);

static enum ia_css_err frame_allocate_buffer_data(struct ia_css_frame *frame);

static enum ia_css_err frame_allocate_with_data(struct ia_css_frame **frame,
	unsigned int width,
	unsigned int height,
	enum ia_css_frame_format format,
	unsigned int padded_width,
	unsigned int raw_bit_depth,
	bool contiguous);

static struct ia_css_frame *frame_create(unsigned int width,
	unsigned int height,
	enum ia_css_frame_format format,
	unsigned int padded_width,
	unsigned int raw_bit_depth,
	bool contiguous,
	bool valid);

static unsigned
ia_css_elems_bytes_from_info(
	const struct ia_css_frame_info *info);

/**************************************************************************
**	CSS API functions, exposed by ia_css.h
**************************************************************************/

void ia_css_frame_zero(struct ia_css_frame *frame)
{
	assert(frame != NULL);
	mmgr_clear(frame->data, frame->data_bytes);
}

enum ia_css_err ia_css_frame_allocate_from_info(struct ia_css_frame **frame,
	const struct ia_css_frame_info *info)
{
	enum ia_css_err err = IA_CSS_SUCCESS;
	if (frame == NULL || info == NULL)
		return IA_CSS_ERR_INVALID_ARGUMENTS;
	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
		      "ia_css_frame_allocate_from_info() enter:\n");
	err =
	    ia_css_frame_allocate(frame, info->res.width, info->res.height,
				  info->format, info->padded_width,
				  info->raw_bit_depth);
	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
		      "ia_css_frame_allocate_from_info() leave:\n");
	return err;
}

enum ia_css_err ia_css_frame_allocate(struct ia_css_frame **frame,
	unsigned int width,
	unsigned int height,
	enum ia_css_frame_format format,
	unsigned int padded_width,
	unsigned int raw_bit_depth)
{
	enum ia_css_err err = IA_CSS_SUCCESS;

	if (frame == NULL || width == 0 || height == 0)
		return IA_CSS_ERR_INVALID_ARGUMENTS;

	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
#ifndef ISP2401
	  "ia_css_frame_allocate() enter: width=%d, height=%d, format=%d\n",
	  width, height, format);
#else
	  "ia_css_frame_allocate() enter: width=%d, height=%d, format=%d, padded_width=%d, raw_bit_depth=%d\n",
	  width, height, format, padded_width, raw_bit_depth);
#endif

	err = frame_allocate_with_data(frame, width, height, format,
				       padded_width, raw_bit_depth, false);

#ifndef ISP2401
	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
		      "ia_css_frame_allocate() leave: frame=%p\n", *frame);
#else
	if ((*frame != NULL) && err == IA_CSS_SUCCESS)
		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
		      "ia_css_frame_allocate() leave: frame=%p, data(DDR address)=0x%x\n", *frame, (*frame)->data);
	else
		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
		      "ia_css_frame_allocate() leave: frame=%p, data(DDR address)=0x%x\n",
		      (void *)-1, (unsigned int)-1);
#endif

	return err;
}

enum ia_css_err ia_css_frame_map(struct ia_css_frame **frame,
	const struct ia_css_frame_info *info,
	const void *data,
	uint16_t attribute,
	void *context)
{
	enum ia_css_err err = IA_CSS_SUCCESS;
	struct ia_css_frame *me;
	assert(frame != NULL);

	/* Create the frame structure */
	err = ia_css_frame_create_from_info(&me, info);

	if (err != IA_CSS_SUCCESS)
		return err;

	if (err == IA_CSS_SUCCESS) {
		/* use mmgr_mmap to map */
		me->data = (ia_css_ptr) mmgr_mmap(data,
						  me->data_bytes,
						  attribute, context);
		if (me->data == mmgr_NULL)
			err = IA_CSS_ERR_INVALID_ARGUMENTS;
	}

	if (err != IA_CSS_SUCCESS) {
		sh_css_free(me);
#ifndef ISP2401
		return err;
#else
		me = NULL;
#endif
	}

	*frame = me;

	return err;
}

enum ia_css_err ia_css_frame_create_from_info(struct ia_css_frame **frame,
	const struct ia_css_frame_info *info)
{
	enum ia_css_err err = IA_CSS_SUCCESS;
	struct ia_css_frame *me;
	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
		"ia_css_frame_create_from_info() enter:\n");
	if (frame == NULL || info == NULL) {
		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
			"ia_css_frame_create_from_info() leave:"
			" invalid arguments\n");
		return IA_CSS_ERR_INVALID_ARGUMENTS;
	}

	me = frame_create(info->res.width,
		info->res.height,
		info->format,
		info->padded_width,
		info->raw_bit_depth,
		false,
		false);
	if (me == NULL) {
		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
			"ia_css_frame_create_from_info() leave:"
			" frame create failed\n");
		return IA_CSS_ERR_CANNOT_ALLOCATE_MEMORY;
	}

	err = ia_css_frame_init_planes(me);

#ifndef ISP2401
	if (err == IA_CSS_SUCCESS)
		*frame = me;
	else
#else
	if (err != IA_CSS_SUCCESS) {
#endif
		sh_css_free(me);
#ifdef ISP2401
		me = NULL;
	}

	*frame = me;
#endif

	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE, "ia_css_frame_create_from_info() leave:\n");

	return err;
}

enum ia_css_err ia_css_frame_set_data(struct ia_css_frame *frame,
	const ia_css_ptr mapped_data,
	size_t data_bytes)
{
	enum ia_css_err err = IA_CSS_SUCCESS;
	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
		"ia_css_frame_set_data() enter:\n");
	if (frame == NULL) {
		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
			"ia_css_frame_set_data() leave: NULL frame\n");
		return IA_CSS_ERR_INVALID_ARGUMENTS;
	}

	/* If we are setting a valid data.
	 * Make sure that there is enough
	 * room for the expected frame format
	 */
	if ((mapped_data != mmgr_NULL) && (frame->data_bytes > data_bytes)) {
		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
			"ia_css_frame_set_data() leave: invalid arguments\n");
		return IA_CSS_ERR_INVALID_ARGUMENTS;
	}

	frame->data = mapped_data;

	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE, "ia_css_frame_set_data() leave:\n");

	return err;
}

enum ia_css_err ia_css_frame_allocate_contiguous(struct ia_css_frame **frame,
	unsigned int width,
	unsigned int height,
	enum ia_css_frame_format format,
	unsigned int padded_width,
	unsigned int raw_bit_depth)
{
	enum ia_css_err err = IA_CSS_SUCCESS;

	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
		"ia_css_frame_allocate_contiguous() "
#ifndef ISP2401
		"enter: width=%d, height=%d, format=%d\n",
		width, height, format);
#else
		"enter: width=%d, height=%d, format=%d, padded_width=%d, raw_bit_depth=%d\n",
		width, height, format, padded_width, raw_bit_depth);
#endif

	err = frame_allocate_with_data(frame, width, height, format,
			padded_width, raw_bit_depth, true);

	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
		"ia_css_frame_allocate_contiguous() leave: frame=%p\n",
		frame ? *frame : (void *)-1);

	return err;
}

enum ia_css_err ia_css_frame_allocate_contiguous_from_info(
	struct ia_css_frame **frame,
	const struct ia_css_frame_info *info)
{
	enum ia_css_err err = IA_CSS_SUCCESS;
	assert(frame != NULL);
	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
		"ia_css_frame_allocate_contiguous_from_info() enter:\n");
	err = ia_css_frame_allocate_contiguous(frame,
						info->res.width,
						info->res.height,
						info->format,
						info->padded_width,
						info->raw_bit_depth);
	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
		"ia_css_frame_allocate_contiguous_from_info() leave:\n");
	return err;
}

void ia_css_frame_free(struct ia_css_frame *frame)
{
	IA_CSS_ENTER_PRIVATE("frame = %p", frame);

	if (frame != NULL) {
		hmm_free(frame->data);
		sh_css_free(frame);
	}

	IA_CSS_LEAVE_PRIVATE("void");
}

/**************************************************************************
**	Module public functions
**************************************************************************/

enum ia_css_err ia_css_frame_check_info(const struct ia_css_frame_info *info)
{
	assert(info != NULL);
	if (info->res.width == 0 || info->res.height == 0)
		return IA_CSS_ERR_INVALID_ARGUMENTS;
	return IA_CSS_SUCCESS;
}

enum ia_css_err ia_css_frame_init_planes(struct ia_css_frame *frame)
{
	assert(frame != NULL);

	switch (frame->info.format) {
	case IA_CSS_FRAME_FORMAT_MIPI:
		frame_init_mipi_plane(frame, &frame->planes.raw,
			frame->info.res.height,
			frame->info.padded_width,
			frame->info.raw_bit_depth <= 8 ? 1 : 2);
		break;
	case IA_CSS_FRAME_FORMAT_RAW_PACKED:
		frame_init_raw_single_plane(frame, &frame->planes.raw,
			frame->info.res.height,
			frame->info.padded_width,
			frame->info.raw_bit_depth);
		break;
	case IA_CSS_FRAME_FORMAT_RAW:
		frame_init_single_plane(frame, &frame->planes.raw,
			frame->info.res.height,
			frame->info.padded_width,
			frame->info.raw_bit_depth <= 8 ? 1 : 2);
		break;
	case IA_CSS_FRAME_FORMAT_RGB565:
		frame_init_single_plane(frame, &frame->planes.rgb,
			frame->info.res.height,
			frame->info.padded_width, 2);
		break;
	case IA_CSS_FRAME_FORMAT_RGBA888:
		frame_init_single_plane(frame, &frame->planes.rgb,
			frame->info.res.height,
			frame->info.padded_width * 4, 1);
		break;
	case IA_CSS_FRAME_FORMAT_PLANAR_RGB888:
		frame_init_rgb_planes(frame, 1);
		break;
		/* yuyv and uyvu have the same frame layout, only the data
		 * positioning differs.
		 */
	case IA_CSS_FRAME_FORMAT_YUYV:
	case IA_CSS_FRAME_FORMAT_UYVY:
	case IA_CSS_FRAME_FORMAT_CSI_MIPI_YUV420_8:
	case IA_CSS_FRAME_FORMAT_CSI_MIPI_LEGACY_YUV420_8:
		frame_init_single_plane(frame, &frame->planes.yuyv,
			frame->info.res.height,
			frame->info.padded_width * 2, 1);
		break;
	case IA_CSS_FRAME_FORMAT_YUV_LINE:
		/* Needs 3 extra lines to allow vf_pp prefetching */
		frame_init_single_plane(frame, &frame->planes.yuyv,
			frame->info.res.height * 3 / 2 + 3,
			frame->info.padded_width, 1);
		break;
	case IA_CSS_FRAME_FORMAT_NV11:
	  frame_init_nv_planes(frame, 4, 1, 1);
		break;
		/* nv12 and nv21 have the same frame layout, only the data
		 * positioning differs.
		 */
	case IA_CSS_FRAME_FORMAT_NV12:
	case IA_CSS_FRAME_FORMAT_NV21:
	case IA_CSS_FRAME_FORMAT_NV12_TILEY:
		frame_init_nv_planes(frame, 2, 2, 1);
		break;
	case IA_CSS_FRAME_FORMAT_NV12_16:
		frame_init_nv_planes(frame, 2, 2, 2);
		break;
		/* nv16 and nv61 have the same frame layout, only the data
		 * positioning differs.
		 */
	case IA_CSS_FRAME_FORMAT_NV16:
	case IA_CSS_FRAME_FORMAT_NV61:
		frame_init_nv_planes(frame, 2, 1, 1);
		break;
	case IA_CSS_FRAME_FORMAT_YUV420:
		frame_init_yuv_planes(frame, 2, 2, false, 1);
		break;
	case IA_CSS_FRAME_FORMAT_YUV422:
		frame_init_yuv_planes(frame, 2, 1, false, 1);
		break;
	case IA_CSS_FRAME_FORMAT_YUV444:
		frame_init_yuv_planes(frame, 1, 1, false, 1);
		break;
	case IA_CSS_FRAME_FORMAT_YUV420_16:
		frame_init_yuv_planes(frame, 2, 2, false, 2);
		break;
	case IA_CSS_FRAME_FORMAT_YUV422_16:
		frame_init_yuv_planes(frame, 2, 1, false, 2);
		break;
	case IA_CSS_FRAME_FORMAT_YV12:
		frame_init_yuv_planes(frame, 2, 2, true, 1);
		break;
	case IA_CSS_FRAME_FORMAT_YV16:
		frame_init_yuv_planes(frame, 2, 1, true, 1);
		break;
	case IA_CSS_FRAME_FORMAT_QPLANE6:
		frame_init_qplane6_planes(frame);
		break;
	case IA_CSS_FRAME_FORMAT_BINARY_8:
		frame_init_single_plane(frame, &frame->planes.binary.data,
			frame->info.res.height,
			frame->info.padded_width, 1);
		frame->planes.binary.size = 0;
		break;
	default:
		return IA_CSS_ERR_INVALID_ARGUMENTS;
	}
	return IA_CSS_SUCCESS;
}

void ia_css_frame_info_set_width(struct ia_css_frame_info *info,
	unsigned int width,
	unsigned int min_padded_width)
{
	unsigned int align;

	IA_CSS_ENTER_PRIVATE("info = %p,width = %d, minimum padded width = %d",
			     info, width, min_padded_width);
	if (info == NULL) {
		IA_CSS_ERROR("NULL input parameter");
		IA_CSS_LEAVE_PRIVATE("");
		return;
	}
	if (min_padded_width > width)
		align = min_padded_width;
	else
		align = width;

	info->res.width = width;
	/* frames with a U and V plane of 8 bits per pixel need to have
	   all planes aligned, this means double the alignment for the
	   Y plane if the horizontal decimation is 2. */
	if (info->format == IA_CSS_FRAME_FORMAT_YUV420 ||
	    info->format == IA_CSS_FRAME_FORMAT_YV12 ||
	    info->format == IA_CSS_FRAME_FORMAT_NV12 ||
	    info->format == IA_CSS_FRAME_FORMAT_NV21 ||
	    info->format == IA_CSS_FRAME_FORMAT_BINARY_8 ||
	    info->format == IA_CSS_FRAME_FORMAT_YUV_LINE)
		info->padded_width =
		    CEIL_MUL(align, 2 * HIVE_ISP_DDR_WORD_BYTES);
	else if (info->format == IA_CSS_FRAME_FORMAT_NV12_TILEY)
		info->padded_width = CEIL_MUL(align, NV12_TILEY_TILE_WIDTH);
	else if (info->format == IA_CSS_FRAME_FORMAT_RAW ||
		 info->format == IA_CSS_FRAME_FORMAT_RAW_PACKED)
		info->padded_width = CEIL_MUL(align, 2 * ISP_VEC_NELEMS);
	else {
		info->padded_width = CEIL_MUL(align, HIVE_ISP_DDR_WORD_BYTES);
	}
	IA_CSS_LEAVE_PRIVATE("");
}

void ia_css_frame_info_set_format(struct ia_css_frame_info *info,
	enum ia_css_frame_format format)
{
	assert(info != NULL);
	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
		"ia_css_frame_info_set_format() enter:\n");
	info->format = format;
}

void ia_css_frame_info_init(struct ia_css_frame_info *info,
	unsigned int width,
	unsigned int height,
	enum ia_css_frame_format format,
	unsigned int aligned)
{
	IA_CSS_ENTER_PRIVATE("info = %p, width = %d, height = %d, format = %d, aligned = %d",
			     info, width, height, format, aligned);
	if (info == NULL) {
		IA_CSS_ERROR("NULL input parameter");
		IA_CSS_LEAVE_PRIVATE("");
		return;
	}
	info->res.height = height;
	info->format     = format;
	ia_css_frame_info_set_width(info, width, aligned);
	IA_CSS_LEAVE_PRIVATE("");
}

void ia_css_frame_free_multiple(unsigned int num_frames,
	struct ia_css_frame **frames_array)
{
	unsigned int i;
	for (i = 0; i < num_frames; i++) {
		if (frames_array[i]) {
			ia_css_frame_free(frames_array[i]);
			frames_array[i] = NULL;
		}
	}
}

enum ia_css_err ia_css_frame_allocate_with_buffer_size(
	struct ia_css_frame **frame,
	const unsigned int buffer_size_bytes,
	const bool contiguous)
{
	/* AM: Body coppied from frame_allocate_with_data(). */
	enum ia_css_err err;
	struct ia_css_frame *me = frame_create(0, 0,
		IA_CSS_FRAME_FORMAT_NUM,/* Not valid format yet */
		0, 0, contiguous, false);

	if (me == NULL)
		return IA_CSS_ERR_CANNOT_ALLOCATE_MEMORY;

	/* Get the data size */
	me->data_bytes = buffer_size_bytes;

	err = frame_allocate_buffer_data(me);

	if (err != IA_CSS_SUCCESS) {
		sh_css_free(me);
#ifndef ISP2401
		return err;
#else
		me = NULL;
#endif
	}

	*frame = me;

	return err;
}

bool ia_css_frame_info_is_same_resolution(
	const struct ia_css_frame_info *info_a,
	const struct ia_css_frame_info *info_b)
{
	if (!info_a || !info_b)
		return false;
	return (info_a->res.width == info_b->res.width) &&
	    (info_a->res.height == info_b->res.height);
}

bool ia_css_frame_is_same_type(const struct ia_css_frame *frame_a,
	const struct ia_css_frame *frame_b)
{
	bool is_equal = false;
	const struct ia_css_frame_info *info_a = &frame_a->info,
	    *info_b = &frame_b->info;

	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
		      "ia_css_frame_is_same_type() enter:\n");

	if (!info_a || !info_b)
		return false;
	if (info_a->format != info_b->format)
		return false;
	if (info_a->padded_width != info_b->padded_width)
		return false;
	is_equal = ia_css_frame_info_is_same_resolution(info_a, info_b);

	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
		      "ia_css_frame_is_same_type() leave:\n");

	return is_equal;
}

void
ia_css_dma_configure_from_info(
	struct dma_port_config *config,
	const struct ia_css_frame_info *info)
{
	unsigned is_raw_packed = info->format == IA_CSS_FRAME_FORMAT_RAW_PACKED;
	unsigned bits_per_pixel = is_raw_packed ? info->raw_bit_depth : ia_css_elems_bytes_from_info(info)*8;
	unsigned pix_per_ddrword = HIVE_ISP_DDR_WORD_BITS / bits_per_pixel;
	unsigned words_per_line = CEIL_DIV(info->padded_width, pix_per_ddrword);
	unsigned elems_b = pix_per_ddrword;

	config->stride = HIVE_ISP_DDR_WORD_BYTES * words_per_line;
	config->elems  = (uint8_t)elems_b;
	config->width  = (uint16_t)info->res.width;
	config->crop   = 0;
	assert(config->width <= info->padded_width);
}

/**************************************************************************
**	Static functions
**************************************************************************/

static void frame_init_plane(struct ia_css_frame_plane *plane,
	unsigned int width,
	unsigned int stride,
	unsigned int height,
	unsigned int offset)
{
	plane->height = height;
	plane->width = width;
	plane->stride = stride;
	plane->offset = offset;
}

static void frame_init_single_plane(struct ia_css_frame *frame,
	struct ia_css_frame_plane *plane,
	unsigned int height,
	unsigned int subpixels_per_line,
	unsigned int bytes_per_pixel)
{
	unsigned int stride;

	stride = subpixels_per_line * bytes_per_pixel;
	/* Frame height needs to be even number - needed by hw ISYS2401
	   In case of odd number, round up to even.
	   Images won't be impacted by this round up,
	   only needed by jpeg/embedded data.
	   As long as buffer allocation and release are using data_bytes,
	   there won't be memory leak. */
	frame->data_bytes = stride * CEIL_MUL2(height, 2);
	frame_init_plane(plane, subpixels_per_line, stride, height, 0);
	return;
}

static void frame_init_raw_single_plane(
	struct ia_css_frame *frame,
	struct ia_css_frame_plane *plane,
	unsigned int height,
	unsigned int subpixels_per_line,
	unsigned int bits_per_pixel)
{
	unsigned int stride;
	assert(frame != NULL);

	stride = HIVE_ISP_DDR_WORD_BYTES *
			CEIL_DIV(subpixels_per_line,
				HIVE_ISP_DDR_WORD_BITS / bits_per_pixel);
	frame->data_bytes = stride * height;
	frame_init_plane(plane, subpixels_per_line, stride, height, 0);
	return;
}

static void frame_init_mipi_plane(struct ia_css_frame *frame,
	struct ia_css_frame_plane *plane,
	unsigned int height,
	unsigned int subpixels_per_line,
	unsigned int bytes_per_pixel)
{
	unsigned int stride;

	stride = subpixels_per_line * bytes_per_pixel;
	frame->data_bytes = 8388608; /* 8*1024*1024 */
	frame->valid = false;
	frame->contiguous = true;
	frame_init_plane(plane, subpixels_per_line, stride, height, 0);
	return;
}

static void frame_init_nv_planes(struct ia_css_frame *frame,
				 unsigned int horizontal_decimation,
				 unsigned int vertical_decimation,
				 unsigned int bytes_per_element)
{
	unsigned int y_width = frame->info.padded_width;
	unsigned int y_height = frame->info.res.height;
	unsigned int uv_width;
	unsigned int uv_height;
	unsigned int y_bytes;
	unsigned int uv_bytes;
	unsigned int y_stride;
	unsigned int uv_stride;

	assert(horizontal_decimation != 0 && vertical_decimation != 0);

	uv_width = 2 * (y_width / horizontal_decimation);
	uv_height = y_height / vertical_decimation;

	if (IA_CSS_FRAME_FORMAT_NV12_TILEY == frame->info.format) {
		y_width   = CEIL_MUL(y_width,   NV12_TILEY_TILE_WIDTH);
		uv_width  = CEIL_MUL(uv_width,  NV12_TILEY_TILE_WIDTH);
		y_height  = CEIL_MUL(y_height,  NV12_TILEY_TILE_HEIGHT);
		uv_height = CEIL_MUL(uv_height, NV12_TILEY_TILE_HEIGHT);
	}

	y_stride = y_width * bytes_per_element;
	uv_stride = uv_width * bytes_per_element;
	y_bytes = y_stride * y_height;
	uv_bytes = uv_stride * uv_height;

	frame->data_bytes = y_bytes + uv_bytes;
	frame_init_plane(&frame->planes.nv.y, y_width, y_stride, y_height, 0);
	frame_init_plane(&frame->planes.nv.uv, uv_width,
			 uv_stride, uv_height, y_bytes);
	return;
}

static void frame_init_yuv_planes(struct ia_css_frame *frame,
	unsigned int horizontal_decimation,
	unsigned int vertical_decimation,
	bool swap_uv,
	unsigned int bytes_per_element)
{
	unsigned int y_width = frame->info.padded_width,
	    y_height = frame->info.res.height,
	    uv_width = y_width / horizontal_decimation,
	    uv_height = y_height / vertical_decimation,
	    y_stride, y_bytes, uv_bytes, uv_stride;

	y_stride = y_width * bytes_per_element;
	uv_stride = uv_width * bytes_per_element;
	y_bytes = y_stride * y_height;
	uv_bytes = uv_stride * uv_height;

	frame->data_bytes = y_bytes + 2 * uv_bytes;
	frame_init_plane(&frame->planes.yuv.y, y_width, y_stride, y_height, 0);
	if (swap_uv) {
		frame_init_plane(&frame->planes.yuv.v, uv_width, uv_stride,
				 uv_height, y_bytes);
		frame_init_plane(&frame->planes.yuv.u, uv_width, uv_stride,
				 uv_height, y_bytes + uv_bytes);
	} else {
		frame_init_plane(&frame->planes.yuv.u, uv_width, uv_stride,
				 uv_height, y_bytes);
		frame_init_plane(&frame->planes.yuv.v, uv_width, uv_stride,
				 uv_height, y_bytes + uv_bytes);
	}
	return;
}

static void frame_init_rgb_planes(struct ia_css_frame *frame,
	unsigned int bytes_per_element)
{
	unsigned int width = frame->info.res.width,
	    height = frame->info.res.height, stride, bytes;

	stride = width * bytes_per_element;
	bytes = stride * height;
	frame->data_bytes = 3 * bytes;
	frame_init_plane(&frame->planes.planar_rgb.r, width, stride, height, 0);
	frame_init_plane(&frame->planes.planar_rgb.g,
			 width, stride, height, 1 * bytes);
	frame_init_plane(&frame->planes.planar_rgb.b,
			 width, stride, height, 2 * bytes);
	return;
}

static void frame_init_qplane6_planes(struct ia_css_frame *frame)
{
	unsigned int width = frame->info.padded_width / 2,
	    height = frame->info.res.height / 2, bytes, stride;

	stride = width * 2;
	bytes = stride * height;

	frame->data_bytes = 6 * bytes;
	frame_init_plane(&frame->planes.plane6.r,
			 width, stride, height, 0 * bytes);
	frame_init_plane(&frame->planes.plane6.r_at_b,
			 width, stride, height, 1 * bytes);
	frame_init_plane(&frame->planes.plane6.gr,
			 width, stride, height, 2 * bytes);
	frame_init_plane(&frame->planes.plane6.gb,
			 width, stride, height, 3 * bytes);
	frame_init_plane(&frame->planes.plane6.b,
			 width, stride, height, 4 * bytes);
	frame_init_plane(&frame->planes.plane6.b_at_r,
			 width, stride, height, 5 * bytes);
	return;
}

static enum ia_css_err frame_allocate_buffer_data(struct ia_css_frame *frame)
{
#ifdef ISP2401
	IA_CSS_ENTER_LEAVE_PRIVATE("frame->data_bytes=%d\n", frame->data_bytes);
#endif
	frame->data = mmgr_alloc_attr(frame->data_bytes,
				      frame->contiguous ?
				      MMGR_ATTRIBUTE_CONTIGUOUS :
				      MMGR_ATTRIBUTE_DEFAULT);

	if (frame->data == mmgr_NULL)
		return IA_CSS_ERR_CANNOT_ALLOCATE_MEMORY;
	return IA_CSS_SUCCESS;
}

static enum ia_css_err frame_allocate_with_data(struct ia_css_frame **frame,
	unsigned int width,
	unsigned int height,
	enum ia_css_frame_format format,
	unsigned int padded_width,
	unsigned int raw_bit_depth,
	bool contiguous)
{
	enum ia_css_err err;
	struct ia_css_frame *me = frame_create(width,
		height,
		format,
		padded_width,
		raw_bit_depth,
		contiguous,
		true);

	if (me == NULL)
		return IA_CSS_ERR_CANNOT_ALLOCATE_MEMORY;

	err = ia_css_frame_init_planes(me);

	if (err == IA_CSS_SUCCESS)
		err = frame_allocate_buffer_data(me);

	if (err != IA_CSS_SUCCESS) {
		sh_css_free(me);
#ifndef ISP2401
		return err;
#else
		me = NULL;
#endif
	}

	*frame = me;

	return err;
}

static struct ia_css_frame *frame_create(unsigned int width,
	unsigned int height,
	enum ia_css_frame_format format,
	unsigned int padded_width,
	unsigned int raw_bit_depth,
	bool contiguous,
	bool valid)
{
	struct ia_css_frame *me = sh_css_malloc(sizeof(*me));

	if (me == NULL)
		return NULL;

	memset(me, 0, sizeof(*me));
	me->info.res.width = width;
	me->info.res.height = height;
	me->info.format = format;
	me->info.padded_width = padded_width;
	me->info.raw_bit_depth = raw_bit_depth;
	me->contiguous = contiguous;
	me->valid = valid;
	me->data_bytes = 0;
	me->data = mmgr_NULL;
	/* To indicate it is not valid frame. */
	me->dynamic_queue_id = (int)SH_CSS_INVALID_QUEUE_ID;
	me->buf_type = IA_CSS_BUFFER_TYPE_INVALID;

	return me;
}

static unsigned
ia_css_elems_bytes_from_info(const struct ia_css_frame_info *info)
{
	if (info->format == IA_CSS_FRAME_FORMAT_RGB565)
		return 2; /* bytes per pixel */
	if (info->format == IA_CSS_FRAME_FORMAT_YUV420_16)
		return 2; /* bytes per pixel */
	if (info->format == IA_CSS_FRAME_FORMAT_YUV422_16)
		return 2; /* bytes per pixel */
	/* Note: Essentially NV12_16 is a 2 bytes per pixel format, this return value is used
	 * to configure DMA for the output buffer,
	 * At least in SKC this data is overwriten by isp_output_init.sp.c except for elements(elems),
	 * which is configured from this return value,
	 * NV12_16 is implemented by a double buffer of 8 bit elements hence elems should be configured as 8 */
	if (info->format == IA_CSS_FRAME_FORMAT_NV12_16)
		return 1; /* bytes per pixel */

	if (info->format == IA_CSS_FRAME_FORMAT_RAW
		|| (info->format == IA_CSS_FRAME_FORMAT_RAW_PACKED)) {
		if (info->raw_bit_depth)
			return CEIL_DIV(info->raw_bit_depth,8);
		else
			return 2; /* bytes per pixel */
	}
	if (info->format == IA_CSS_FRAME_FORMAT_PLANAR_RGB888)
		return 3; /* bytes per pixel */
	if (info->format == IA_CSS_FRAME_FORMAT_RGBA888)
		return 4; /* bytes per pixel */
	if (info->format == IA_CSS_FRAME_FORMAT_QPLANE6)
		return 2; /* bytes per pixel */
	return 1; /* Default is 1 byte per pixel */
}

void ia_css_frame_info_to_frame_sp_info(
	struct ia_css_frame_sp_info *to,
	const struct ia_css_frame_info *from)
{
	ia_css_resolution_to_sp_resolution(&to->res, &from->res);
	to->padded_width = (uint16_t)from->padded_width;
	to->format = (uint8_t)from->format;
	to->raw_bit_depth = (uint8_t)from->raw_bit_depth;
	to->raw_bayer_order = from->raw_bayer_order;
}

void ia_css_resolution_to_sp_resolution(
	struct ia_css_sp_resolution *to,
	const struct ia_css_resolution *from)
{
	to->width  = (uint16_t)from->width;
	to->height = (uint16_t)from->height;
}
#ifdef ISP2401

enum ia_css_err
ia_css_frame_find_crop_resolution(const struct ia_css_resolution *in_res,
	const struct ia_css_resolution *out_res,
	struct ia_css_resolution *crop_res)
{
	uint32_t wd_even_ceil, ht_even_ceil;
	uint32_t in_ratio, out_ratio;

	if ((in_res == NULL) || (out_res == NULL) || (crop_res == NULL))
		return IA_CSS_ERR_INVALID_ARGUMENTS;

	IA_CSS_ENTER_PRIVATE("in(%ux%u) -> out(%ux%u)", in_res->width,
		in_res->height, out_res->width, out_res->height);

	if ((in_res->width == 0)
		|| (in_res->height == 0)
		|| (out_res->width == 0)
		|| (out_res->height == 0))
			return IA_CSS_ERR_INVALID_ARGUMENTS;

	if ((out_res->width > in_res->width) ||
		 (out_res->height > in_res->height))
			return IA_CSS_ERR_INVALID_ARGUMENTS;

	/* If aspect ratio (width/height) of out_res is higher than the aspect
	 * ratio of the in_res, then we crop vertically, otherwise we crop
	 * horizontally.
	 */
	in_ratio = in_res->width * out_res->height;
	out_ratio = out_res->width * in_res->height;

	if (in_ratio == out_ratio) {
		crop_res->width = in_res->width;
		crop_res->height = in_res->height;
	} else if (out_ratio > in_ratio) {
		crop_res->width = in_res->width;
		crop_res->height = ROUND_DIV(out_res->height * crop_res->width,
			out_res->width);
	} else {
		crop_res->height = in_res->height;
		crop_res->width = ROUND_DIV(out_res->width * crop_res->height,
			out_res->height);
	}

	/* Round new (cropped) width and height to an even number.
	 * binarydesc_calculate_bds_factor is such that we should consider as
	 * much of the input as possible. This is different only when we end up
	 * with an odd number in the last step. So, we take the next even number
	 * if it falls within the input, otherwise take the previous even no.
	 */
	wd_even_ceil = EVEN_CEIL(crop_res->width);
	ht_even_ceil = EVEN_CEIL(crop_res->height);
	if ((wd_even_ceil > in_res->width) || (ht_even_ceil > in_res->height)) {
		crop_res->width = EVEN_FLOOR(crop_res->width);
		crop_res->height = EVEN_FLOOR(crop_res->height);
	} else {
		crop_res->width = wd_even_ceil;
		crop_res->height = ht_even_ceil;
	}

	IA_CSS_LEAVE_PRIVATE("in(%ux%u) -> out(%ux%u)", crop_res->width,
		crop_res->height, out_res->width, out_res->height);
	return IA_CSS_SUCCESS;
}
#endif