summaryrefslogtreecommitdiff
path: root/drivers/media/platform/vicodec/codec-v4l2-fwht.c
blob: e9c522d209ab4ad2e965c37a0b1a73cf70519917 (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
// SPDX-License-Identifier: LGPL-2.1
/*
 * A V4L2 frontend for the FWHT codec
 *
 * Copyright 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
 */

#include <linux/errno.h>
#include <linux/string.h>
#include <linux/videodev2.h>
#include "codec-v4l2-fwht.h"

static const struct v4l2_fwht_pixfmt_info v4l2_fwht_pixfmts[] = {
	{ V4L2_PIX_FMT_YUV420,  1, 3, 2, 1, 1, 2, 2, 3, 3},
	{ V4L2_PIX_FMT_YVU420,  1, 3, 2, 1, 1, 2, 2, 3, 3},
	{ V4L2_PIX_FMT_YUV422P, 1, 2, 1, 1, 1, 2, 1, 3, 3},
	{ V4L2_PIX_FMT_NV12,    1, 3, 2, 1, 2, 2, 2, 3, 2},
	{ V4L2_PIX_FMT_NV21,    1, 3, 2, 1, 2, 2, 2, 3, 2},
	{ V4L2_PIX_FMT_NV16,    1, 2, 1, 1, 2, 2, 1, 3, 2},
	{ V4L2_PIX_FMT_NV61,    1, 2, 1, 1, 2, 2, 1, 3, 2},
	{ V4L2_PIX_FMT_NV24,    1, 3, 1, 1, 2, 1, 1, 3, 2},
	{ V4L2_PIX_FMT_NV42,    1, 3, 1, 1, 2, 1, 1, 3, 2},
	{ V4L2_PIX_FMT_YUYV,    2, 2, 1, 2, 4, 2, 1, 3, 1},
	{ V4L2_PIX_FMT_YVYU,    2, 2, 1, 2, 4, 2, 1, 3, 1},
	{ V4L2_PIX_FMT_UYVY,    2, 2, 1, 2, 4, 2, 1, 3, 1},
	{ V4L2_PIX_FMT_VYUY,    2, 2, 1, 2, 4, 2, 1, 3, 1},
	{ V4L2_PIX_FMT_BGR24,   3, 3, 1, 3, 3, 1, 1, 3, 1},
	{ V4L2_PIX_FMT_RGB24,   3, 3, 1, 3, 3, 1, 1, 3, 1},
	{ V4L2_PIX_FMT_HSV24,   3, 3, 1, 3, 3, 1, 1, 3, 1},
	{ V4L2_PIX_FMT_BGR32,   4, 4, 1, 4, 4, 1, 1, 3, 1},
	{ V4L2_PIX_FMT_XBGR32,  4, 4, 1, 4, 4, 1, 1, 3, 1},
	{ V4L2_PIX_FMT_RGB32,   4, 4, 1, 4, 4, 1, 1, 3, 1},
	{ V4L2_PIX_FMT_XRGB32,  4, 4, 1, 4, 4, 1, 1, 3, 1},
	{ V4L2_PIX_FMT_HSV32,   4, 4, 1, 4, 4, 1, 1, 3, 1},
	{ V4L2_PIX_FMT_ARGB32,  4, 4, 1, 4, 4, 1, 1, 4, 1},
	{ V4L2_PIX_FMT_ABGR32,  4, 4, 1, 4, 4, 1, 1, 4, 1},
	{ V4L2_PIX_FMT_GREY,    1, 1, 1, 1, 0, 1, 1, 1, 1},
};

const struct v4l2_fwht_pixfmt_info *v4l2_fwht_find_pixfmt(u32 pixelformat)
{
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(v4l2_fwht_pixfmts); i++)
		if (v4l2_fwht_pixfmts[i].id == pixelformat)
			return v4l2_fwht_pixfmts + i;
	return NULL;
}

const struct v4l2_fwht_pixfmt_info *v4l2_fwht_get_pixfmt(u32 idx)
{
	if (idx >= ARRAY_SIZE(v4l2_fwht_pixfmts))
		return NULL;
	return v4l2_fwht_pixfmts + idx;
}

int v4l2_fwht_encode(struct v4l2_fwht_state *state, u8 *p_in, u8 *p_out)
{
	unsigned int size = state->stride * state->coded_height;
	unsigned int chroma_stride = state->stride;
	const struct v4l2_fwht_pixfmt_info *info = state->info;
	struct fwht_cframe_hdr *p_hdr;
	struct fwht_cframe cf;
	struct fwht_raw_frame rf;
	u32 encoding;
	u32 flags = 0;

	if (!info)
		return -EINVAL;

	rf.luma = p_in;
	rf.width_div = info->width_div;
	rf.height_div = info->height_div;
	rf.luma_alpha_step = info->luma_alpha_step;
	rf.chroma_step = info->chroma_step;
	rf.alpha = NULL;
	rf.components_num = info->components_num;

	switch (info->id) {
	case V4L2_PIX_FMT_GREY:
		rf.cb = NULL;
		rf.cr = NULL;
		break;
	case V4L2_PIX_FMT_YUV420:
		rf.cb = rf.luma + size;
		rf.cr = rf.cb + size / 4;
		chroma_stride /= 2;
		break;
	case V4L2_PIX_FMT_YVU420:
		rf.cr = rf.luma + size;
		rf.cb = rf.cr + size / 4;
		chroma_stride /= 2;
		break;
	case V4L2_PIX_FMT_YUV422P:
		rf.cb = rf.luma + size;
		rf.cr = rf.cb + size / 2;
		chroma_stride /= 2;
		break;
	case V4L2_PIX_FMT_NV12:
	case V4L2_PIX_FMT_NV16:
	case V4L2_PIX_FMT_NV24:
		rf.cb = rf.luma + size;
		rf.cr = rf.cb + 1;
		break;
	case V4L2_PIX_FMT_NV21:
	case V4L2_PIX_FMT_NV61:
	case V4L2_PIX_FMT_NV42:
		rf.cr = rf.luma + size;
		rf.cb = rf.cr + 1;
		break;
	case V4L2_PIX_FMT_YUYV:
		rf.cb = rf.luma + 1;
		rf.cr = rf.cb + 2;
		break;
	case V4L2_PIX_FMT_YVYU:
		rf.cr = rf.luma + 1;
		rf.cb = rf.cr + 2;
		break;
	case V4L2_PIX_FMT_UYVY:
		rf.cb = rf.luma;
		rf.cr = rf.cb + 2;
		rf.luma++;
		break;
	case V4L2_PIX_FMT_VYUY:
		rf.cr = rf.luma;
		rf.cb = rf.cr + 2;
		rf.luma++;
		break;
	case V4L2_PIX_FMT_RGB24:
	case V4L2_PIX_FMT_HSV24:
		rf.cr = rf.luma;
		rf.cb = rf.cr + 2;
		rf.luma++;
		break;
	case V4L2_PIX_FMT_BGR24:
		rf.cb = rf.luma;
		rf.cr = rf.cb + 2;
		rf.luma++;
		break;
	case V4L2_PIX_FMT_RGB32:
	case V4L2_PIX_FMT_XRGB32:
	case V4L2_PIX_FMT_HSV32:
		rf.cr = rf.luma + 1;
		rf.cb = rf.cr + 2;
		rf.luma += 2;
		break;
	case V4L2_PIX_FMT_BGR32:
	case V4L2_PIX_FMT_XBGR32:
		rf.cb = rf.luma;
		rf.cr = rf.cb + 2;
		rf.luma++;
		break;
	case V4L2_PIX_FMT_ARGB32:
		rf.alpha = rf.luma;
		rf.cr = rf.luma + 1;
		rf.cb = rf.cr + 2;
		rf.luma += 2;
		break;
	case V4L2_PIX_FMT_ABGR32:
		rf.cb = rf.luma;
		rf.cr = rf.cb + 2;
		rf.luma++;
		rf.alpha = rf.cr + 1;
		break;
	default:
		return -EINVAL;
	}

	cf.i_frame_qp = state->i_frame_qp;
	cf.p_frame_qp = state->p_frame_qp;
	cf.rlc_data = (__be16 *)(p_out + sizeof(*p_hdr));

	encoding = fwht_encode_frame(&rf, &state->ref_frame, &cf,
				     !state->gop_cnt,
				     state->gop_cnt == state->gop_size - 1,
				     state->visible_width,
				     state->visible_height,
				     state->stride, chroma_stride);
	if (!(encoding & FWHT_FRAME_PCODED))
		state->gop_cnt = 0;
	if (++state->gop_cnt >= state->gop_size)
		state->gop_cnt = 0;

	p_hdr = (struct fwht_cframe_hdr *)p_out;
	p_hdr->magic1 = FWHT_MAGIC1;
	p_hdr->magic2 = FWHT_MAGIC2;
	p_hdr->version = htonl(FWHT_VERSION);
	p_hdr->width = htonl(state->visible_width);
	p_hdr->height = htonl(state->visible_height);
	flags |= (info->components_num - 1) << FWHT_FL_COMPONENTS_NUM_OFFSET;
	if (encoding & FWHT_LUMA_UNENCODED)
		flags |= FWHT_FL_LUMA_IS_UNCOMPRESSED;
	if (encoding & FWHT_CB_UNENCODED)
		flags |= FWHT_FL_CB_IS_UNCOMPRESSED;
	if (encoding & FWHT_CR_UNENCODED)
		flags |= FWHT_FL_CR_IS_UNCOMPRESSED;
	if (encoding & FWHT_ALPHA_UNENCODED)
		flags |= FWHT_FL_ALPHA_IS_UNCOMPRESSED;
	if (rf.height_div == 1)
		flags |= FWHT_FL_CHROMA_FULL_HEIGHT;
	if (rf.width_div == 1)
		flags |= FWHT_FL_CHROMA_FULL_WIDTH;
	p_hdr->flags = htonl(flags);
	p_hdr->colorspace = htonl(state->colorspace);
	p_hdr->xfer_func = htonl(state->xfer_func);
	p_hdr->ycbcr_enc = htonl(state->ycbcr_enc);
	p_hdr->quantization = htonl(state->quantization);
	p_hdr->size = htonl(cf.size);
	return cf.size + sizeof(*p_hdr);
}

int v4l2_fwht_decode(struct v4l2_fwht_state *state, u8 *p_in, u8 *p_out)
{
	unsigned int i, j, k;
	u32 flags;
	struct fwht_cframe_hdr *p_hdr;
	struct fwht_cframe cf;
	u8 *p, *ref_p;
	unsigned int components_num = 3;
	unsigned int version;
	const struct v4l2_fwht_pixfmt_info *info;
	unsigned int hdr_width_div, hdr_height_div;

	if (!state->info)
		return -EINVAL;

	info = state->info;
	p_hdr = (struct fwht_cframe_hdr *)p_in;

	version = ntohl(p_hdr->version);
	if (!version || version > FWHT_VERSION) {
		pr_err("version %d is not supported, current version is %d\n",
		       version, FWHT_VERSION);
		return -EINVAL;
	}

	if (p_hdr->magic1 != FWHT_MAGIC1 ||
	    p_hdr->magic2 != FWHT_MAGIC2)
		return -EINVAL;

	/* TODO: support resolution changes */
	if (ntohl(p_hdr->width)  != state->visible_width ||
	    ntohl(p_hdr->height) != state->visible_height)
		return -EINVAL;

	flags = ntohl(p_hdr->flags);

	if (version == FWHT_VERSION) {
		components_num = 1 + ((flags & FWHT_FL_COMPONENTS_NUM_MSK) >>
			FWHT_FL_COMPONENTS_NUM_OFFSET);
	}

	state->colorspace = ntohl(p_hdr->colorspace);
	state->xfer_func = ntohl(p_hdr->xfer_func);
	state->ycbcr_enc = ntohl(p_hdr->ycbcr_enc);
	state->quantization = ntohl(p_hdr->quantization);
	cf.rlc_data = (__be16 *)(p_in + sizeof(*p_hdr));

	hdr_width_div = (flags & FWHT_FL_CHROMA_FULL_WIDTH) ? 1 : 2;
	hdr_height_div = (flags & FWHT_FL_CHROMA_FULL_HEIGHT) ? 1 : 2;
	if (hdr_width_div != info->width_div ||
	    hdr_height_div != info->height_div)
		return -EINVAL;

	fwht_decode_frame(&cf, &state->ref_frame, flags, components_num,
			  state->visible_width, state->visible_height,
			  state->coded_width);

	/*
	 * TODO - handle the case where the compressed stream encodes a
	 * different format than the requested decoded format.
	 */
	switch (state->info->id) {
	case V4L2_PIX_FMT_GREY:
		ref_p = state->ref_frame.luma;
		for (i = 0; i < state->coded_height; i++)  {
			memcpy(p_out, ref_p, state->visible_width);
			p_out += state->stride;
			ref_p += state->coded_width;
		}
		break;
	case V4L2_PIX_FMT_YUV420:
	case V4L2_PIX_FMT_YUV422P:
		ref_p = state->ref_frame.luma;
		for (i = 0; i < state->coded_height; i++)  {
			memcpy(p_out, ref_p, state->visible_width);
			p_out += state->stride;
			ref_p += state->coded_width;
		}

		ref_p = state->ref_frame.cb;
		for (i = 0; i < state->coded_height / 2; i++)  {
			memcpy(p_out, ref_p, state->visible_width / 2);
			p_out += state->stride / 2;
			ref_p += state->coded_width / 2;
		}
		ref_p = state->ref_frame.cr;
		for (i = 0; i < state->coded_height / 2; i++)  {
			memcpy(p_out, ref_p, state->visible_width / 2);
			p_out += state->stride / 2;
			ref_p += state->coded_width / 2;
		}
		break;
	case V4L2_PIX_FMT_YVU420:
		ref_p = state->ref_frame.luma;
		for (i = 0; i < state->coded_height; i++)  {
			memcpy(p_out, ref_p, state->visible_width);
			p_out += state->stride;
			ref_p += state->coded_width;
		}

		ref_p = state->ref_frame.cr;
		for (i = 0; i < state->coded_height / 2; i++)  {
			memcpy(p_out, ref_p, state->visible_width / 2);
			p_out += state->stride / 2;
			ref_p += state->coded_width / 2;
		}
		ref_p = state->ref_frame.cb;
		for (i = 0; i < state->coded_height / 2; i++)  {
			memcpy(p_out, ref_p, state->visible_width / 2);
			p_out += state->stride / 2;
			ref_p += state->coded_width / 2;
		}
		break;
	case V4L2_PIX_FMT_NV12:
	case V4L2_PIX_FMT_NV16:
	case V4L2_PIX_FMT_NV24:
		ref_p = state->ref_frame.luma;
		for (i = 0; i < state->coded_height; i++)  {
			memcpy(p_out, ref_p, state->visible_width);
			p_out += state->stride;
			ref_p += state->coded_width;
		}

		k = 0;
		for (i = 0; i < state->coded_height / 2; i++) {
			for (j = 0, p = p_out; j < state->coded_width / 2; j++) {
				*p++ = state->ref_frame.cb[k];
				*p++ = state->ref_frame.cr[k];
				k++;
			}
			p_out += state->stride;
		}
		break;
	case V4L2_PIX_FMT_NV21:
	case V4L2_PIX_FMT_NV61:
	case V4L2_PIX_FMT_NV42:
		ref_p = state->ref_frame.luma;
		for (i = 0; i < state->coded_height; i++)  {
			memcpy(p_out, ref_p, state->visible_width);
			p_out += state->stride;
			ref_p += state->coded_width;
		}

		k = 0;
		for (i = 0; i < state->coded_height / 2; i++) {
			for (j = 0, p = p_out; j < state->coded_width / 2; j++) {
				*p++ = state->ref_frame.cr[k];
				*p++ = state->ref_frame.cb[k];
				k++;
			}
			p_out += state->stride;
		}
		break;
	case V4L2_PIX_FMT_YUYV:
		k = 0;
		for (i = 0; i < state->coded_height; i++) {
			for (j = 0, p = p_out; j < state->coded_width / 2; j++) {
				*p++ = state->ref_frame.luma[k];
				*p++ = state->ref_frame.cb[k / 2];
				*p++ = state->ref_frame.luma[k + 1];
				*p++ = state->ref_frame.cr[k / 2];
				k += 2;
			}
			p_out += state->stride;
		}
		break;
	case V4L2_PIX_FMT_YVYU:
		k = 0;
		for (i = 0; i < state->coded_height; i++) {
			for (j = 0, p = p_out; j < state->coded_width / 2; j++) {
				*p++ = state->ref_frame.luma[k];
				*p++ = state->ref_frame.cr[k / 2];
				*p++ = state->ref_frame.luma[k + 1];
				*p++ = state->ref_frame.cb[k / 2];
				k += 2;
			}
			p_out += state->stride;
		}
		break;
	case V4L2_PIX_FMT_UYVY:
		k = 0;
		for (i = 0; i < state->coded_height; i++) {
			for (j = 0, p = p_out; j < state->coded_width / 2; j++) {
				*p++ = state->ref_frame.cb[k / 2];
				*p++ = state->ref_frame.luma[k];
				*p++ = state->ref_frame.cr[k / 2];
				*p++ = state->ref_frame.luma[k + 1];
				k += 2;
			}
			p_out += state->stride;
		}
		break;
	case V4L2_PIX_FMT_VYUY:
		k = 0;
		for (i = 0; i < state->coded_height; i++) {
			for (j = 0, p = p_out; j < state->coded_width / 2; j++) {
				*p++ = state->ref_frame.cr[k / 2];
				*p++ = state->ref_frame.luma[k];
				*p++ = state->ref_frame.cb[k / 2];
				*p++ = state->ref_frame.luma[k + 1];
				k += 2;
			}
			p_out += state->stride;
		}
		break;
	case V4L2_PIX_FMT_RGB24:
	case V4L2_PIX_FMT_HSV24:
		k = 0;
		for (i = 0; i < state->coded_height; i++) {
			for (j = 0, p = p_out; j < state->coded_width; j++) {
				*p++ = state->ref_frame.cr[k];
				*p++ = state->ref_frame.luma[k];
				*p++ = state->ref_frame.cb[k];
				k++;
			}
			p_out += state->stride;
		}
		break;
	case V4L2_PIX_FMT_BGR24:
		k = 0;
		for (i = 0; i < state->coded_height; i++) {
			for (j = 0, p = p_out; j < state->coded_width; j++) {
				*p++ = state->ref_frame.cb[k];
				*p++ = state->ref_frame.luma[k];
				*p++ = state->ref_frame.cr[k];
				k++;
			}
			p_out += state->stride;
		}
		break;
	case V4L2_PIX_FMT_RGB32:
	case V4L2_PIX_FMT_XRGB32:
	case V4L2_PIX_FMT_HSV32:
		k = 0;
		for (i = 0; i < state->coded_height; i++) {
			for (j = 0, p = p_out; j < state->coded_width; j++) {
				*p++ = 0;
				*p++ = state->ref_frame.cr[k];
				*p++ = state->ref_frame.luma[k];
				*p++ = state->ref_frame.cb[k];
				k++;
			}
			p_out += state->stride;
		}
		break;
	case V4L2_PIX_FMT_BGR32:
	case V4L2_PIX_FMT_XBGR32:
		k = 0;
		for (i = 0; i < state->coded_height; i++) {
			for (j = 0, p = p_out; j < state->coded_width; j++) {
				*p++ = state->ref_frame.cb[k];
				*p++ = state->ref_frame.luma[k];
				*p++ = state->ref_frame.cr[k];
				*p++ = 0;
				k++;
			}
			p_out += state->stride;
		}
		break;
	case V4L2_PIX_FMT_ARGB32:
		k = 0;
		for (i = 0; i < state->coded_height; i++) {
			for (j = 0, p = p_out; j < state->coded_width; j++) {
				*p++ = state->ref_frame.alpha[k];
				*p++ = state->ref_frame.cr[k];
				*p++ = state->ref_frame.luma[k];
				*p++ = state->ref_frame.cb[k];
				k++;
			}
			p_out += state->stride;
		}
		break;
	case V4L2_PIX_FMT_ABGR32:
		k = 0;
		for (i = 0; i < state->coded_height; i++) {
			for (j = 0, p = p_out; j < state->coded_width; j++) {
				*p++ = state->ref_frame.cb[k];
				*p++ = state->ref_frame.luma[k];
				*p++ = state->ref_frame.cr[k];
				*p++ = state->ref_frame.alpha[k];
				k++;
			}
			p_out += state->stride;
		}
		break;
	default:
		return -EINVAL;
	}
	return 0;
}