summaryrefslogtreecommitdiff
path: root/drivers/video/b2r2/b2r2_utils.c
blob: 3df7a272211ec304945e99ead01029090281d9ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/*
 * Copyright (C) ST-Ericsson SA 2010
 *
 * ST-Ericsson B2R2 utils
 *
 * Author: Johan Mossberg <johan.xx.mossberg@stericsson.com> for ST-Ericsson.
 *
 * License terms: GNU General Public License (GPL), version 2.
 */

#include <linux/kernel.h>
#include <linux/errno.h>

#include <video/b2r2_blt.h>

#include "b2r2_utils.h"
#include "b2r2_debug.h"
#include "b2r2_internal.h"

const s32 b2r2_s32_max = 2147483647;


/**
 * calculate_scale_factor() - calculates the scale factor between the given
 *                            values
 */
int calculate_scale_factor(struct b2r2_control *cont,
			u32 from, u32 to, u16 *sf_out)
{
	int ret;
	u32 sf;

	b2r2_log_info(cont->dev, "%s\n", __func__);

	if (to == from) {
		*sf_out = 1 << 10;
		return 0;
	} else if (to == 0) {
		b2r2_log_err(cont->dev, "%s: To is 0!\n", __func__);
		BUG_ON(1);
	}

	sf = (from << 10) / to;

	if ((sf & 0xffff0000) != 0) {
		/* Overflow error */
		b2r2_log_warn(cont->dev, "%s: "
			"Scale factor too large\n", __func__);
		ret = -EINVAL;
		goto error;
	} else if (sf == 0) {
		b2r2_log_warn(cont->dev, "%s: "
			"Scale factor too small\n", __func__);
		ret = -EINVAL;
		goto error;
	}

	*sf_out = (u16)sf;

	b2r2_log_info(cont->dev, "%s exit\n", __func__);

	return 0;

error:
	b2r2_log_warn(cont->dev, "%s: Exit...\n", __func__);
	return ret;
}

void b2r2_get_img_bounding_rect(struct b2r2_blt_img *img,
					struct b2r2_blt_rect *bounding_rect)
{
	bounding_rect->x = 0;
	bounding_rect->y = 0;
	bounding_rect->width = img->width;
	bounding_rect->height = img->height;
}


bool b2r2_is_zero_area_rect(struct b2r2_blt_rect *rect)
{
	return rect->width == 0 || rect->height == 0;
}

bool b2r2_is_rect_inside_rect(struct b2r2_blt_rect *rect1,
						struct b2r2_blt_rect *rect2)
{
	return rect1->x >= rect2->x &&
		rect1->y >= rect2->y &&
		rect1->x + rect1->width <= rect2->x + rect2->width &&
		rect1->y + rect1->height <= rect2->y + rect2->height;
}

bool b2r2_is_rect_gte_rect(struct b2r2_blt_rect *rect1,
		struct b2r2_blt_rect *rect2)
{
	return rect1->width >= rect2->width &&
			rect1->height >= rect2->height;
}

void b2r2_intersect_rects(struct b2r2_blt_rect *rect1,
	struct b2r2_blt_rect *rect2, struct b2r2_blt_rect *intersection)
{
	struct b2r2_blt_rect tmp_rect;

	tmp_rect.x = max(rect1->x, rect2->x);
	tmp_rect.y = max(rect1->y, rect2->y);
	tmp_rect.width = min(rect1->x + rect1->width, rect2->x + rect2->width)
				- tmp_rect.x;
	if (tmp_rect.width < 0)
		tmp_rect.width = 0;
	tmp_rect.height =
		min(rect1->y + rect1->height, rect2->y + rect2->height) -
				tmp_rect.y;
	if (tmp_rect.height < 0)
		tmp_rect.height = 0;

	*intersection = tmp_rect;
}

/*
 * Calculate new rectangles for the supplied
 * request, so that clipping to destination imaage
 * can be avoided.
 * Essentially, the new destination rectangle is
 * defined inside the old one. Given the transform
 * and scaling, one has to calculate which part of
 * the old source rectangle corresponds to
 * to the new part of old destination rectangle.
 */
void b2r2_trim_rects(struct b2r2_control *cont,
			const struct b2r2_blt_req *req,
			struct b2r2_blt_rect *new_bg_rect,
			struct b2r2_blt_rect *new_dst_rect,
			struct b2r2_blt_rect *new_src_rect)
{
	enum b2r2_blt_transform transform = req->transform;
	struct b2r2_blt_rect *old_src_rect =
		(struct b2r2_blt_rect *) &req->src_rect;
	struct b2r2_blt_rect *old_dst_rect =
		(struct b2r2_blt_rect *) &req->dst_rect;
	struct b2r2_blt_rect *old_bg_rect =
		(struct b2r2_blt_rect *) &req->bg_rect;
	struct b2r2_blt_rect dst_img_bounds;
	s32 src_x = 0;
	s32 src_y = 0;
	s32 src_w = 0;
	s32 src_h = 0;
	s32 dx = 0;
	s32 dy = 0;
	s16 hsf;
	s16 vsf;

	b2r2_log_info(cont->dev,
		"%s\nold_dst_rect(x,y,w,h)=(%d, %d, %d, %d)\n", __func__,
		old_dst_rect->x, old_dst_rect->y,
		old_dst_rect->width, old_dst_rect->height);
	b2r2_log_info(cont->dev,
		"%s\nold_src_rect(x,y,w,h)=(%d, %d, %d, %d)\n", __func__,
		old_src_rect->x, old_src_rect->y,
		old_src_rect->width, old_src_rect->height);

	b2r2_get_img_bounding_rect((struct b2r2_blt_img *) &req->dst_img,
		&dst_img_bounds);

	/* dst_rect inside dst_img, no clipping necessary */
	if (b2r2_is_rect_inside_rect(old_dst_rect, &dst_img_bounds))
		goto keep_rects;

	b2r2_intersect_rects(old_dst_rect, &dst_img_bounds, new_dst_rect);
	b2r2_log_info(cont->dev,
		"%s\nnew_dst_rect(x,y,w,h)=(%d, %d, %d, %d)\n", __func__,
		new_dst_rect->x, new_dst_rect->y,
		new_dst_rect->width, new_dst_rect->height);

	/* dst_rect completely outside, leave it to validation */
	if (new_dst_rect->width == 0 || new_dst_rect->height == 0)
		goto keep_rects;

	dx = new_dst_rect->x - old_dst_rect->x;
	dy = new_dst_rect->y - old_dst_rect->y;

	if (transform & B2R2_BLT_TRANSFORM_CCW_ROT_90) {
		int res = 0;
		res = calculate_scale_factor(cont, old_src_rect->width,
			old_dst_rect->height, &hsf);
		/* invalid dimensions, leave them to validation */
		if (res < 0)
			goto keep_rects;

		res = calculate_scale_factor(cont, old_src_rect->height,
			old_dst_rect->width, &vsf);
		if (res < 0)
			goto keep_rects;

		/*
		 * After applying the inverse transform
		 * for 90 degree rotation, the top-left corner
		 * becomes top-right.
		 * src_rect origin is defined as top-left,
		 * so a translation between dst and src
		 * coordinate spaces is necessary.
		 */
		src_x = (old_src_rect->width << 10) -
			hsf * (dy + new_dst_rect->height);
		src_y = dx * vsf;
		src_w = new_dst_rect->height * hsf;
		src_h = new_dst_rect->width * vsf;
	} else {
		int res = 0;
		res = calculate_scale_factor(cont, old_src_rect->width,
			old_dst_rect->width, &hsf);
		if (res < 0)
			goto keep_rects;

		res = calculate_scale_factor(cont, old_src_rect->height,
			old_dst_rect->height, &vsf);
		if (res < 0)
			goto keep_rects;

		src_x = dx * hsf;
		src_y = dy * vsf;
		src_w = new_dst_rect->width * hsf;
		src_h = new_dst_rect->height * vsf;
	}

	/*
	 * src_w must contain all the pixels that contribute
	 * to a particular destination rectangle.
	 * ((x + 0x3ff) >> 10) is equivalent to ceiling(x),
	 * expressed in 6.10 fixed point format.
	 * Every destination rectangle, maps to a certain area in the source
	 * rectangle. The area in source will most likely not be a rectangle
	 * with exact integer dimensions whenever arbitrary scaling is involved.
	 * Consider the following example.
	 * Suppose, that width of the current destination rectangle maps
	 * to 1.7 pixels in source, starting at x == 5.4, as calculated
	 * using the scaling factor.
	 * This means that while the destination rectangle is written,
	 * the source should be read from x == 5.4 up to x == 5.4 + 1.7 == 7.1
	 * Consequently, color from 3 pixels (x == 5, 6 and 7)
	 * needs to be read from source.
	 * The formula below the comment yields:
	 * ceil(0.4 + 1.7) == ceil(2.1) == 3
	 * (src_x & 0x3ff) is the fractional part of src_x,
	 * which is expressed in 6.10 fixed point format.
	 * Thus, width of the source area should be 3 pixels wide,
	 * starting at x == 5.
	 */
	src_w = ((src_x & 0x3ff) + src_w + 0x3ff) >> 10;
	src_h = ((src_y & 0x3ff) + src_h + 0x3ff) >> 10;

	src_x >>= 10;
	src_y >>= 10;

	if (transform & B2R2_BLT_TRANSFORM_FLIP_H)
		src_x = old_src_rect->width - src_x - src_w;

	if (transform & B2R2_BLT_TRANSFORM_FLIP_V)
		src_y = old_src_rect->height - src_y - src_h;

	/*
	 * Translate the src_rect coordinates into true
	 * src_buffer coordinates.
	 */
	src_x += old_src_rect->x;
	src_y += old_src_rect->y;

	new_src_rect->x = src_x;
	new_src_rect->y = src_y;
	new_src_rect->width = src_w;
	new_src_rect->height = src_h;

	b2r2_log_info(cont->dev,
		"%s\nnew_src_rect(x,y,w,h)=(%d, %d, %d, %d)\n", __func__,
		new_src_rect->x, new_src_rect->y,
		new_src_rect->width, new_src_rect->height);

	if (req->flags & B2R2_BLT_FLAG_BG_BLEND) {
		/* Modify bg_rect in the same way as dst_rect */
		s32 dw = new_dst_rect->width - old_dst_rect->width;
		s32 dh = new_dst_rect->height - old_dst_rect->height;
		b2r2_log_info(cont->dev,
			"%s\nold bg_rect(x,y,w,h)=(%d, %d, %d, %d)\n",
			__func__, old_bg_rect->x, old_bg_rect->y,
			old_bg_rect->width, old_bg_rect->height);
		new_bg_rect->x = old_bg_rect->x + dx;
		new_bg_rect->y = old_bg_rect->y + dy;
		new_bg_rect->width = old_bg_rect->width + dw;
		new_bg_rect->height = old_bg_rect->height + dh;
		b2r2_log_info(cont->dev,
			"%s\nnew bg_rect(x,y,w,h)=(%d, %d, %d, %d)\n",
			__func__, new_bg_rect->x, new_bg_rect->y,
			new_bg_rect->width, new_bg_rect->height);
	}
	return;
keep_rects:
	/*
	 * Recalculation was not possible, or not necessary.
	 * Do not change anything, leave it to validation.
	 */
	*new_src_rect = *old_src_rect;
	*new_dst_rect = *old_dst_rect;
	*new_bg_rect = *old_bg_rect;
	b2r2_log_info(cont->dev, "%s original rectangles preserved.\n", __func__);
	return;
}

int b2r2_get_fmt_bpp(struct b2r2_control *cont, enum b2r2_blt_fmt fmt)
{
	/*
	 * Currently this function is not used that often but if that changes a
	 * lookup table could make it a lot faster.
	 */
	switch (fmt) {
	case B2R2_BLT_FMT_1_BIT_A1:
		return 1;

	case B2R2_BLT_FMT_8_BIT_A8:
		return 8;

	case B2R2_BLT_FMT_YUV420_PACKED_PLANAR:
	case B2R2_BLT_FMT_YVU420_PACKED_PLANAR:
	case B2R2_BLT_FMT_YUV420_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YVU420_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YUV420_PACKED_SEMIPLANAR_MB_STE:
		return 12;

	case B2R2_BLT_FMT_16_BIT_ARGB4444:
	case B2R2_BLT_FMT_16_BIT_ARGB1555:
	case B2R2_BLT_FMT_16_BIT_RGB565:
	case B2R2_BLT_FMT_Y_CB_Y_CR:
	case B2R2_BLT_FMT_CB_Y_CR_Y:
	case B2R2_BLT_FMT_YUV422_PACKED_PLANAR:
	case B2R2_BLT_FMT_YVU422_PACKED_PLANAR:
	case B2R2_BLT_FMT_YUV422_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YVU422_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YUV422_PACKED_SEMIPLANAR_MB_STE:
		return 16;

	case B2R2_BLT_FMT_24_BIT_RGB888:
	case B2R2_BLT_FMT_24_BIT_ARGB8565:
	case B2R2_BLT_FMT_24_BIT_YUV888:
	case B2R2_BLT_FMT_24_BIT_VUY888:
	case B2R2_BLT_FMT_YUV444_PACKED_PLANAR:
		return 24;

	case B2R2_BLT_FMT_32_BIT_ARGB8888:
	case B2R2_BLT_FMT_32_BIT_ABGR8888:
	case B2R2_BLT_FMT_32_BIT_AYUV8888:
	case B2R2_BLT_FMT_32_BIT_VUYA8888:
		return 32;

	default:
		b2r2_log_err(cont->dev,
			"%s: Internal error! Format %#x not recognized.\n",
			__func__, fmt);
		return 32;
	}
}

int b2r2_get_fmt_y_bpp(struct b2r2_control *cont, enum b2r2_blt_fmt fmt)
{
	switch (fmt) {
	case B2R2_BLT_FMT_YUV420_PACKED_PLANAR:
	case B2R2_BLT_FMT_YVU420_PACKED_PLANAR:
	case B2R2_BLT_FMT_YUV420_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YVU420_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YUV420_PACKED_SEMIPLANAR_MB_STE:
	case B2R2_BLT_FMT_Y_CB_Y_CR:
	case B2R2_BLT_FMT_CB_Y_CR_Y:
	case B2R2_BLT_FMT_YUV422_PACKED_PLANAR:
	case B2R2_BLT_FMT_YVU422_PACKED_PLANAR:
	case B2R2_BLT_FMT_YUV422_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YVU422_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YUV422_PACKED_SEMIPLANAR_MB_STE:
	case B2R2_BLT_FMT_YUV444_PACKED_PLANAR:
	case B2R2_BLT_FMT_24_BIT_YUV888:
	case B2R2_BLT_FMT_32_BIT_AYUV8888:
	case B2R2_BLT_FMT_24_BIT_VUY888:
	case B2R2_BLT_FMT_32_BIT_VUYA8888:
		return 8;

	default:
		b2r2_log_err(cont->dev,
			"%s: Internal error! Non YCbCr format supplied.\n",
			__func__);
		return 8;
	}
}


bool b2r2_is_single_plane_fmt(enum b2r2_blt_fmt fmt)
{
	switch (fmt) {
	case B2R2_BLT_FMT_1_BIT_A1:
	case B2R2_BLT_FMT_8_BIT_A8:
	case B2R2_BLT_FMT_16_BIT_ARGB4444:
	case B2R2_BLT_FMT_16_BIT_ARGB1555:
	case B2R2_BLT_FMT_16_BIT_RGB565:
	case B2R2_BLT_FMT_24_BIT_RGB888:
	case B2R2_BLT_FMT_24_BIT_ARGB8565:
	case B2R2_BLT_FMT_24_BIT_YUV888:
	case B2R2_BLT_FMT_24_BIT_VUY888:
	case B2R2_BLT_FMT_32_BIT_ARGB8888:
	case B2R2_BLT_FMT_32_BIT_ABGR8888:
	case B2R2_BLT_FMT_32_BIT_AYUV8888:
	case B2R2_BLT_FMT_32_BIT_VUYA8888:
	case B2R2_BLT_FMT_Y_CB_Y_CR:
	case B2R2_BLT_FMT_CB_Y_CR_Y:
		return true;

	default:
		return false;
	}
}

bool b2r2_is_independent_pixel_fmt(enum b2r2_blt_fmt fmt)
{
	switch (fmt) {
	case B2R2_BLT_FMT_1_BIT_A1:
	case B2R2_BLT_FMT_8_BIT_A8:
	case B2R2_BLT_FMT_16_BIT_ARGB4444:
	case B2R2_BLT_FMT_16_BIT_ARGB1555:
	case B2R2_BLT_FMT_16_BIT_RGB565:
	case B2R2_BLT_FMT_24_BIT_RGB888:
	case B2R2_BLT_FMT_24_BIT_ARGB8565:
	case B2R2_BLT_FMT_24_BIT_YUV888:
	case B2R2_BLT_FMT_24_BIT_VUY888:
	case B2R2_BLT_FMT_32_BIT_ARGB8888:
	case B2R2_BLT_FMT_32_BIT_ABGR8888:
	case B2R2_BLT_FMT_32_BIT_AYUV8888:
	case B2R2_BLT_FMT_32_BIT_VUYA8888:
	case B2R2_BLT_FMT_YUV444_PACKED_PLANAR:
		return true;

	default:
		return false;
	}
}

bool b2r2_is_ycbcri_fmt(enum b2r2_blt_fmt fmt)
{
	switch (fmt) {
	case B2R2_BLT_FMT_Y_CB_Y_CR:
	case B2R2_BLT_FMT_CB_Y_CR_Y:
	case B2R2_BLT_FMT_24_BIT_YUV888:
	case B2R2_BLT_FMT_32_BIT_AYUV8888:
	case B2R2_BLT_FMT_24_BIT_VUY888:
	case B2R2_BLT_FMT_32_BIT_VUYA8888:
		return true;

	default:
		return false;
	}
}

bool b2r2_is_ycbcrsp_fmt(enum b2r2_blt_fmt fmt)
{
	switch (fmt) {
	case B2R2_BLT_FMT_YUV420_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YVU420_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YUV422_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YVU422_PACKED_SEMI_PLANAR:
		return true;

	default:
		return false;
	}
}

bool b2r2_is_ycbcrp_fmt(enum b2r2_blt_fmt fmt)
{
	switch (fmt) {
	case B2R2_BLT_FMT_YUV420_PACKED_PLANAR:
	case B2R2_BLT_FMT_YVU420_PACKED_PLANAR:
	case B2R2_BLT_FMT_YUV422_PACKED_PLANAR:
	case B2R2_BLT_FMT_YVU422_PACKED_PLANAR:
	case B2R2_BLT_FMT_YUV444_PACKED_PLANAR:
		return true;

	default:
		return false;
	}
}

bool b2r2_is_ycbcr420_fmt(enum b2r2_blt_fmt fmt)
{
	switch (fmt) {
	case B2R2_BLT_FMT_YUV420_PACKED_PLANAR:
	case B2R2_BLT_FMT_YVU420_PACKED_PLANAR:
	case B2R2_BLT_FMT_YUV420_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YVU420_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YUV420_PACKED_SEMIPLANAR_MB_STE:
		return true;

	default:
		return false;
	}
}

bool b2r2_is_ycbcr422_fmt(enum b2r2_blt_fmt fmt)
{
	switch (fmt) {
	case B2R2_BLT_FMT_Y_CB_Y_CR:
	case B2R2_BLT_FMT_CB_Y_CR_Y:
	case B2R2_BLT_FMT_YUV422_PACKED_PLANAR:
	case B2R2_BLT_FMT_YVU422_PACKED_PLANAR:
	case B2R2_BLT_FMT_YUV422_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YVU422_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YUV422_PACKED_SEMIPLANAR_MB_STE:
		return true;

	default:
		return false;
	}
}

bool b2r2_is_ycbcr444_fmt(enum b2r2_blt_fmt fmt)
{
	switch (fmt) {
	case B2R2_BLT_FMT_YUV444_PACKED_PLANAR:
	case B2R2_BLT_FMT_32_BIT_AYUV8888:
	case B2R2_BLT_FMT_24_BIT_YUV888:
	case B2R2_BLT_FMT_32_BIT_VUYA8888:
	case B2R2_BLT_FMT_24_BIT_VUY888:
		return true;

	default:
		return false;
	}
}

bool b2r2_is_mb_fmt(enum b2r2_blt_fmt fmt)
{
	switch (fmt) {
	case B2R2_BLT_FMT_YUV420_PACKED_SEMIPLANAR_MB_STE:
	case B2R2_BLT_FMT_YUV422_PACKED_SEMIPLANAR_MB_STE:
		return true;

	default:
		return false;
	}
}

u32 b2r2_calc_pitch_from_width(struct b2r2_control *cont,
		s32 width, enum b2r2_blt_fmt fmt)
{
	if (b2r2_is_single_plane_fmt(fmt)) {
		return (u32)b2r2_div_round_up(width *
			b2r2_get_fmt_bpp(cont, fmt), 8);
	} else if (b2r2_is_ycbcrsp_fmt(fmt) || b2r2_is_ycbcrp_fmt(fmt)) {
		return (u32)b2r2_div_round_up(width *
			b2r2_get_fmt_y_bpp(cont, fmt), 8);
	} else {
		b2r2_log_err(cont->dev, "%s: Internal error! "
			"Pitchless format supplied.\n",
			__func__);
		return 0;
	}
}

u32 b2r2_get_img_pitch(struct b2r2_control *cont, struct b2r2_blt_img *img)
{
	if (img->pitch != 0)
		return img->pitch;
	else
		return b2r2_calc_pitch_from_width(cont, img->width, img->fmt);
}

s32 b2r2_get_img_size(struct b2r2_control *cont, struct b2r2_blt_img *img)
{
	if (b2r2_is_single_plane_fmt(img->fmt)) {
		return (s32)b2r2_get_img_pitch(cont, img) * img->height;
	} else if (b2r2_is_ycbcrsp_fmt(img->fmt) ||
			b2r2_is_ycbcrp_fmt(img->fmt)) {
		s32 y_plane_size;

		y_plane_size = (s32)b2r2_get_img_pitch(cont, img) * img->height;

		if (b2r2_is_ycbcr420_fmt(img->fmt)) {
			return y_plane_size + y_plane_size / 2;
		} else if (b2r2_is_ycbcr422_fmt(img->fmt)) {
			return y_plane_size * 2;
		} else if (b2r2_is_ycbcr444_fmt(img->fmt)) {
			return y_plane_size * 3;
		} else {
			b2r2_log_err(cont->dev,	"%s: Internal error!"
				" Format %#x not recognized.\n",
				__func__, img->fmt);
			return 0;
		}
	} else if (b2r2_is_mb_fmt(img->fmt)) {
		return (img->width * img->height *
			b2r2_get_fmt_bpp(cont, img->fmt)) / 8;
	} else {
		b2r2_log_err(cont->dev, "%s: Internal error! "
			"Format %#x not recognized.\n",
			__func__, img->fmt);
		return 0;
	}
}


s32 b2r2_div_round_up(s32 dividend, s32 divisor)
{
	s32 quotient = dividend / divisor;
	if (dividend % divisor != 0)
		quotient++;

	return quotient;
}

bool b2r2_is_aligned(s32 value, s32 alignment)
{
	return value % alignment == 0;
}

s32 b2r2_align_up(s32 value, s32 alignment)
{
	s32 remainder = abs(value) % abs(alignment);
	s32 value_to_add;

	if (remainder > 0) {
		if (value >= 0)
			value_to_add = alignment - remainder;
		else
			value_to_add = remainder;
	} else {
		value_to_add = 0;
	}

	return value + value_to_add;
}