summaryrefslogtreecommitdiff
path: root/drivers/media/platform/sti/delta/delta.h
blob: 74a42404bc500fe7003b0145f4f4d97d527b2a0e (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
/*
 * Copyright (C) STMicroelectronics SA 2015
 * Author: Hugues Fruchet <hugues.fruchet@st.com> for STMicroelectronics.
 * License terms:  GNU General Public License (GPL), version 2
 */

#ifndef DELTA_H
#define DELTA_H

#include <media/v4l2-device.h>
#include <media/v4l2-mem2mem.h>

#include "delta-cfg.h"

/*
 * enum delta_state - state of decoding instance
 *
 *@DELTA_STATE_WF_FORMAT:
 *	Wait for compressed format to be set by V4L2 client in order
 *	to know what is the relevant decoder to open.
 *
 *@DELTA_STATE_WF_STREAMINFO:
 *	Wait for stream information to be available (bitstream
 *	header parsing is done).
 *
 *@DELTA_STATE_READY:
 *	Decoding instance is ready to decode compressed access unit.
 *
 */
enum delta_state {
	DELTA_STATE_WF_FORMAT,
	DELTA_STATE_WF_STREAMINFO,
	DELTA_STATE_READY,
};

/*
 * struct delta_streaminfo - information about stream to decode
 *
 * @flags:		validity of fields (crop, pixelaspect, other)
 * @width:		width of video stream
 * @height:		height ""
 * @streamformat:	fourcc compressed format of video (MJPEG, MPEG2, ...)
 * @dpb:		number of frames needed to decode a single frame
 *			(h264 dpb, up to 16)
 * @crop:		cropping window inside decoded frame (1920x1080@0,0
 *			inside 1920x1088 frame for ex.)
 * @pixelaspect:	pixel aspect ratio of video (4/3, 5/4)
 * @field:		interlaced or not
 * @profile:		profile string
 * @level:		level string
 * @other:		other string information from codec
 * @colorspace:		colorspace identifier
 * @xfer_func:		transfer function identifier
 * @ycbcr_enc:		Y'CbCr encoding identifier
 * @quantization:	quantization identifier
 */
struct delta_streaminfo {
	u32 flags;
	u32 streamformat;
	u32 width;
	u32 height;
	u32 dpb;
	struct v4l2_rect crop;
	struct v4l2_fract pixelaspect;
	enum v4l2_field field;
	u8 profile[32];
	u8 level[32];
	u8 other[32];
	enum v4l2_colorspace colorspace;
	enum v4l2_xfer_func xfer_func;
	enum v4l2_ycbcr_encoding ycbcr_enc;
	enum v4l2_quantization quantization;
};

#define DELTA_STREAMINFO_FLAG_CROP		0x0001
#define DELTA_STREAMINFO_FLAG_PIXELASPECT	0x0002
#define DELTA_STREAMINFO_FLAG_OTHER		0x0004

/*
 * struct delta_au - access unit structure.
 *
 * @vbuf:	video buffer information for V4L2
 * @list:	V4L2 m2m list that the frame belongs to
 * @prepared:	if set vaddr/paddr are resolved
 * @vaddr:	virtual address (kernel can read/write)
 * @paddr:	physical address (for hardware)
 * @flags:	access unit type (V4L2_BUF_FLAG_KEYFRAME/PFRAME/BFRAME)
 * @dts:	decoding timestamp of this access unit
 */
struct delta_au {
	struct vb2_v4l2_buffer vbuf;	/* keep first */
	struct list_head list;	/* keep second */

	bool prepared;
	u32 size;
	void *vaddr;
	dma_addr_t paddr;
	u32 flags;
	u64 dts;
};

/*
 * struct delta_frameinfo - information about decoded frame
 *
 * @flags:		validity of fields (crop, pixelaspect)
 * @pixelformat:	fourcc code for uncompressed video format
 * @width:		width of frame
 * @height:		height of frame
 * @aligned_width:	width of frame (with encoder or decoder alignment
 *			constraint)
 * @aligned_height:	height of frame (with encoder or decoder alignment
 *			constraint)
 * @size:		maximum size in bytes required for data
 * @crop:		cropping window inside frame (1920x1080@0,0
 *			inside 1920x1088 frame for ex.)
 * @pixelaspect:	pixel aspect ratio of video (4/3, 5/4)
 * @field:		interlaced mode
 * @colorspace:		colorspace identifier
 * @xfer_func:		transfer function identifier
 * @ycbcr_enc:		Y'CbCr encoding identifier
 * @quantization:	quantization identifier
 */
struct delta_frameinfo {
	u32 flags;
	u32 pixelformat;
	u32 width;
	u32 height;
	u32 aligned_width;
	u32 aligned_height;
	u32 size;
	struct v4l2_rect crop;
	struct v4l2_fract pixelaspect;
	enum v4l2_field field;
	enum v4l2_colorspace colorspace;
	enum v4l2_xfer_func xfer_func;
	enum v4l2_ycbcr_encoding ycbcr_enc;
	enum v4l2_quantization quantization;
};

#define DELTA_FRAMEINFO_FLAG_CROP		0x0001
#define DELTA_FRAMEINFO_FLAG_PIXELASPECT	0x0002

/*
 * struct delta_frame - frame structure.
 *
 * @vbuf:	video buffer information for V4L2
 * @list:	V4L2 m2m list that the frame belongs to
 * @info:	frame information (width, height, format, alignment...)
 * @prepared:	if set pix/vaddr/paddr are resolved
 * @index:	frame index, aligned on V4L2 wow
 * @vaddr:	virtual address (kernel can read/write)
 * @paddr:	physical address (for hardware)
 * @state:	frame state for frame lifecycle tracking
 *		(DELTA_FRAME_FREE/DEC/OUT/REC/...)
 * @flags:	frame type (V4L2_BUF_FLAG_KEYFRAME/PFRAME/BFRAME)
 * @dts:	decoding timestamp of this frame
 * @field:	field order for interlaced frame
 */
struct delta_frame {
	struct vb2_v4l2_buffer vbuf;	/* keep first */
	struct list_head list;	/* keep second */

	struct delta_frameinfo info;
	bool prepared;
	u32 index;
	void *vaddr;
	dma_addr_t paddr;
	u32 state;
	u32 flags;
	u64 dts;
	enum v4l2_field field;
};

/* frame state for frame lifecycle tracking */
#define DELTA_FRAME_FREE	0x00 /* is free and can be used for decoding */
#define DELTA_FRAME_REF		0x01 /* is a reference frame */
#define DELTA_FRAME_BSY		0x02 /* is owned by decoder and busy */
#define DELTA_FRAME_DEC		0x04 /* contains decoded content */
#define DELTA_FRAME_OUT		0x08 /* has been given to user */
#define DELTA_FRAME_RDY		0x10 /* is ready but still held by decoder */
#define DELTA_FRAME_M2M		0x20 /* is owned by mem2mem framework */

/*
 * struct delta_dts - decoding timestamp.
 *
 * @list:	list to chain timestamps
 * @val:	timestamp in microseconds
 */
struct delta_dts {
	struct list_head list;
	u64 val;
};

struct delta_ctx;

/*
 * struct delta_dec - decoder structure.
 *
 * @name:		name of this decoder
 * @streamformat:	input stream format that this decoder support
 * @pixelformat:	pixel format of decoded frame that this decoder support
 * @max_width:		(optional) maximum width that can decode this decoder
 *			if not set, maximum width is DELTA_MAX_WIDTH
 * @max_height:		(optional) maximum height that can decode this decoder
 *			if not set, maximum height is DELTA_MAX_HEIGHT
 * @pm:			(optional) if set, decoder will manage power on its own
 * @open:		open this decoder
 * @close:		close this decoder
 * @setup_frame:	setup frame to be used by decoder, see below
 * @get_streaminfo:	get stream related infos, see below
 * @get_frameinfo:	get decoded frame related infos, see below
 * @set_frameinfo:	(optional) set decoded frame related infos, see below
 * @setup_frame:	setup frame to be used by decoder, see below
 * @decode:		decode a single access unit, see below
 * @get_frame:		get the next decoded frame available, see below
 * @recycle:		recycle the given frame, see below
 * @flush:		(optional) flush decoder, see below
 */
struct delta_dec {
	const char *name;
	u32 streamformat;
	u32 pixelformat;
	u32 max_width;
	u32 max_height;
	bool pm;

	/*
	 * decoder ops
	 */
	int (*open)(struct delta_ctx *ctx);
	int (*close)(struct delta_ctx *ctx);

	/*
	 * setup_frame() - setup frame to be used by decoder
	 * @ctx:	(in) instance
	 * @frame:	(in) frame to use
	 *  @frame.index	(in) identifier of frame
	 *  @frame.vaddr	(in) virtual address (kernel can read/write)
	 *  @frame.paddr	(in) physical address (for hardware)
	 *
	 * Frame is to be allocated by caller, then given
	 * to decoder through this call.
	 * Several frames must be given to decoder (dpb),
	 * each frame is identified using its index.
	 */
	int (*setup_frame)(struct delta_ctx *ctx, struct delta_frame *frame);

	/*
	 * get_streaminfo() - get stream related infos
	 * @ctx:	(in) instance
	 * @streaminfo:	(out) width, height, dpb,...
	 *
	 * Precondition: stream header must have been successfully
	 * parsed to have this call successful & @streaminfo valid.
	 * Header parsing must be done using decode(), giving
	 * explicitly header access unit or first access unit of bitstream.
	 * If no valid header is found, get_streaminfo will return -ENODATA,
	 * in this case the next bistream access unit must be decoded till
	 * get_streaminfo becomes successful.
	 */
	int (*get_streaminfo)(struct delta_ctx *ctx,
			      struct delta_streaminfo *streaminfo);

	/*
	 * get_frameinfo() - get decoded frame related infos
	 * @ctx:	(in) instance
	 * @frameinfo:	(out) width, height, alignment, crop, ...
	 *
	 * Precondition: get_streaminfo() must be successful
	 */
	int (*get_frameinfo)(struct delta_ctx *ctx,
			     struct delta_frameinfo *frameinfo);

	/*
	 * set_frameinfo() - set decoded frame related infos
	 * @ctx:	(in) instance
	 * @frameinfo:	(out) width, height, alignment, crop, ...
	 *
	 * Optional.
	 * Typically used to negotiate with decoder the output
	 * frame if decoder can do post-processing.
	 */
	int (*set_frameinfo)(struct delta_ctx *ctx,
			     struct delta_frameinfo *frameinfo);

	/*
	 * decode() - decode a single access unit
	 * @ctx:	(in) instance
	 * @au:		(in/out) access unit
	 *  @au.size	(in) size of au to decode
	 *  @au.vaddr	(in) virtual address (kernel can read/write)
	 *  @au.paddr	(in) physical address (for hardware)
	 *  @au.flags	(out) au type (V4L2_BUF_FLAG_KEYFRAME/
	 *			PFRAME/BFRAME)
	 *
	 * Decode the access unit given. Decode is synchronous;
	 * access unit memory is no more needed after this call.
	 * After this call, none, one or several frames could
	 * have been decoded, which can be retrieved using
	 * get_frame().
	 */
	int (*decode)(struct delta_ctx *ctx, struct delta_au *au);

	/*
	 * get_frame() - get the next decoded frame available
	 * @ctx:	(in) instance
	 * @frame:	(out) frame with decoded data:
	 *  @frame.index	(out) identifier of frame
	 *  @frame.field	(out) field order for interlaced frame
	 *  @frame.state	(out) frame state for frame lifecycle tracking
	 *  @frame.flags	(out) frame type (V4L2_BUF_FLAG_KEYFRAME/
	 *			PFRAME/BFRAME)
	 *
	 * Get the next available decoded frame.
	 * If no frame is available, -ENODATA is returned.
	 * If a frame is available, frame structure is filled with
	 * relevant data, frame.index identifying this exact frame.
	 * When this frame is no more needed by upper layers,
	 * recycle() must be called giving this frame identifier.
	 */
	int (*get_frame)(struct delta_ctx *ctx, struct delta_frame **frame);

	/*
	 * recycle() - recycle the given frame
	 * @ctx:	(in) instance
	 * @frame:	(in) frame to recycle:
	 *  @frame.index	(in) identifier of frame
	 *
	 * recycle() is to be called by user when the decoded frame
	 * is no more needed (composition/display done).
	 * This frame will then be reused by decoder to proceed
	 * with next frame decoding.
	 * If not enough frames have been provided through setup_frame(),
	 * or recycle() is not called fast enough, the decoder can run out
	 * of available frames to proceed with decoding (starvation).
	 * This case is guarded by wq_recycle wait queue which ensures that
	 * decoder is called only if at least one frame is available.
	 */
	int (*recycle)(struct delta_ctx *ctx, struct delta_frame *frame);

	/*
	 * flush() - flush decoder
	 * @ctx:	(in) instance
	 *
	 * Optional.
	 * Reset decoder context and discard all internal buffers.
	 * This allows implementation of seek, which leads to discontinuity
	 * of input bitstream that decoder must know to restart its internal
	 * decoding logic.
	 */
	int (*flush)(struct delta_ctx *ctx);
};

struct delta_dev;

/*
 * struct delta_ctx - instance structure.
 *
 * @flags:		validity of fields (streaminfo)
 * @fh:			V4L2 file handle
 * @dev:		device context
 * @dec:		selected decoder context for this instance
 * @state:		instance state
 * @frame_num:		frame number
 * @au_num:		access unit number
 * @max_au_size:	max size of an access unit
 * @streaminfo:		stream information (width, height, dpb, interlacing...)
 * @frameinfo:		frame information (width, height, format, alignment...)
 * @nb_of_frames:	number of frames available for decoding
 * @frames:		array of decoding frames to keep track of frame
 *			state and manage frame recycling
 * @decoded_frames:	nb of decoded frames from opening
 * @output_frames:	nb of output frames from opening
 * @dropped_frames:	nb of frames dropped (ie access unit not parsed
 *			or frame decoded but not output)
 * @stream_errors:	nb of stream errors (corrupted, not supported, ...)
 * @decode_errors:	nb of decode errors (firmware error)
 * @sys_errors:		nb of system errors (memory, ipc, ...)
 * @dts:		FIFO of decoding timestamp.
 *			output frames are timestamped with incoming access
 *			unit timestamps using this fifo.
 * @name:		string naming this instance (debug purpose)
 * @run_work:		decoding work
 * @lock:		lock for decoding work serialization
 * @aborting:		true if current job aborted
 * @priv:		private decoder context for this instance, allocated
 *			by decoder @open time.
 */
struct delta_ctx {
	u32 flags;
	struct v4l2_fh fh;
	struct delta_dev *dev;
	const struct delta_dec *dec;
	enum delta_state state;
	u32 frame_num;
	u32 au_num;
	size_t max_au_size;
	struct delta_streaminfo streaminfo;
	struct delta_frameinfo frameinfo;
	u32 nb_of_frames;
	struct delta_frame *frames[DELTA_MAX_FRAMES];
	u32 decoded_frames;
	u32 output_frames;
	u32 dropped_frames;
	u32 stream_errors;
	u32 decode_errors;
	u32 sys_errors;
	struct list_head dts;
	char name[100];
	struct work_struct run_work;
	struct mutex lock;
	bool aborting;
	void *priv;
};

#define DELTA_FLAG_STREAMINFO 0x0001
#define DELTA_FLAG_FRAMEINFO 0x0002

#define DELTA_MAX_FORMATS  DELTA_MAX_DECODERS

/*
 * struct delta_dev - device struct, 1 per probe (so single one for
 * all platform life)
 *
 * @v4l2_dev:		v4l2 device
 * @vdev:		v4l2 video device
 * @pdev:		platform device
 * @dev:		device
 * @m2m_dev:		memory-to-memory V4L2 device
 * @lock:		device lock, for crit section & V4L2 ops serialization.
 * @clk_delta:		delta main clock
 * @clk_st231:		st231 coprocessor main clock
 * @clk_flash_promip:	flash promip clock
 * @decoders:		list of registered decoders
 * @nb_of_decoders:	nb of registered decoders
 * @pixelformats:	supported uncompressed video formats
 * @nb_of_pixelformats:	number of supported umcompressed video formats
 * @streamformats:	supported compressed video formats
 * @nb_of_streamformats:number of supported compressed video formats
 * @instance_id:	rolling counter identifying an instance (debug purpose)
 * @work_queue:		decoding job work queue
 */
struct delta_dev {
	struct v4l2_device v4l2_dev;
	struct video_device *vdev;
	struct platform_device *pdev;
	struct device *dev;
	struct v4l2_m2m_dev *m2m_dev;
	struct mutex lock;
	struct clk *clk_delta;
	struct clk *clk_st231;
	struct clk *clk_flash_promip;
	const struct delta_dec *decoders[DELTA_MAX_DECODERS];
	u32 nb_of_decoders;
	u32 pixelformats[DELTA_MAX_FORMATS];
	u32 nb_of_pixelformats;
	u32 streamformats[DELTA_MAX_FORMATS];
	u32 nb_of_streamformats;
	u8 instance_id;
	struct workqueue_struct *work_queue;
};

static inline char *frame_type_str(u32 flags)
{
	if (flags & V4L2_BUF_FLAG_KEYFRAME)
		return "I";
	if (flags & V4L2_BUF_FLAG_PFRAME)
		return "P";
	if (flags & V4L2_BUF_FLAG_BFRAME)
		return "B";
	return "?";
}

static inline char *frame_field_str(enum v4l2_field field)
{
	if (field == V4L2_FIELD_NONE)
		return "-";
	if (field == V4L2_FIELD_TOP)
		return "T";
	if (field == V4L2_FIELD_BOTTOM)
		return "B";
	if (field == V4L2_FIELD_INTERLACED)
		return "I";
	if (field == V4L2_FIELD_INTERLACED_TB)
		return "TB";
	if (field == V4L2_FIELD_INTERLACED_BT)
		return "BT";
	return "?";
}

static inline char *frame_state_str(u32 state, char *str, unsigned int len)
{
	snprintf(str, len, "%s %s %s %s %s %s",
		 (state & DELTA_FRAME_REF)  ? "ref" : "   ",
		 (state & DELTA_FRAME_BSY)  ? "bsy" : "   ",
		 (state & DELTA_FRAME_DEC)  ? "dec" : "   ",
		 (state & DELTA_FRAME_OUT)  ? "out" : "   ",
		 (state & DELTA_FRAME_M2M)  ? "m2m" : "   ",
		 (state & DELTA_FRAME_RDY)  ? "rdy" : "   ");
	return str;
}

int delta_get_frameinfo_default(struct delta_ctx *ctx,
				struct delta_frameinfo *frameinfo);
int delta_recycle_default(struct delta_ctx *pctx,
			  struct delta_frame *frame);

int delta_get_free_frame(struct delta_ctx *ctx,
			 struct delta_frame **pframe);

int delta_get_sync(struct delta_ctx *ctx);
void delta_put_autosuspend(struct delta_ctx *ctx);

#endif /* DELTA_H */