1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2011 Advanced Micro Devices, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci/*
29bf215546Sopenharmony_ci * Authors:
30bf215546Sopenharmony_ci *	Christian König <christian.koenig@amd.com>
31bf215546Sopenharmony_ci *
32bf215546Sopenharmony_ci */
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci#include <sys/types.h>
35bf215546Sopenharmony_ci#include <assert.h>
36bf215546Sopenharmony_ci#include <errno.h>
37bf215546Sopenharmony_ci#include <unistd.h>
38bf215546Sopenharmony_ci#include <stdio.h>
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci#include "pipe/p_video_codec.h"
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci#include "util/u_memory.h"
43bf215546Sopenharmony_ci#include "util/u_video.h"
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci#include "vl/vl_defines.h"
46bf215546Sopenharmony_ci#include "vl/vl_mpeg12_decoder.h"
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci#include "r600_pipe_common.h"
49bf215546Sopenharmony_ci#include "radeon_video.h"
50bf215546Sopenharmony_ci#include "radeon_uvd.h"
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci#define NUM_BUFFERS 4
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci#define NUM_MPEG2_REFS 6
55bf215546Sopenharmony_ci#define NUM_H264_REFS 17
56bf215546Sopenharmony_ci#define NUM_VC1_REFS 5
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci#define FB_BUFFER_OFFSET 0x1000
59bf215546Sopenharmony_ci#define FB_BUFFER_SIZE 2048
60bf215546Sopenharmony_ci#define FB_BUFFER_SIZE_TONGA (2048 * 64)
61bf215546Sopenharmony_ci#define IT_SCALING_TABLE_SIZE 992
62bf215546Sopenharmony_ci#define UVD_SESSION_CONTEXT_SIZE (128 * 1024)
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci/* UVD decoder representation */
65bf215546Sopenharmony_cistruct ruvd_decoder {
66bf215546Sopenharmony_ci	struct pipe_video_codec		base;
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci	ruvd_set_dtb			set_dtb;
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci	unsigned			stream_handle;
71bf215546Sopenharmony_ci	unsigned			stream_type;
72bf215546Sopenharmony_ci	unsigned			frame_number;
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci	struct pipe_screen		*screen;
75bf215546Sopenharmony_ci	struct radeon_winsys*		ws;
76bf215546Sopenharmony_ci	struct radeon_cmdbuf	cs;
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci	unsigned			cur_buffer;
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci	struct rvid_buffer		msg_fb_it_buffers[NUM_BUFFERS];
81bf215546Sopenharmony_ci	struct ruvd_msg			*msg;
82bf215546Sopenharmony_ci	uint32_t			*fb;
83bf215546Sopenharmony_ci	unsigned			fb_size;
84bf215546Sopenharmony_ci	uint8_t				*it;
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci	struct rvid_buffer		bs_buffers[NUM_BUFFERS];
87bf215546Sopenharmony_ci	void*				bs_ptr;
88bf215546Sopenharmony_ci	unsigned			bs_size;
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci	struct rvid_buffer		dpb;
91bf215546Sopenharmony_ci	bool				use_legacy;
92bf215546Sopenharmony_ci	struct rvid_buffer		ctx;
93bf215546Sopenharmony_ci	struct rvid_buffer		sessionctx;
94bf215546Sopenharmony_ci	struct {
95bf215546Sopenharmony_ci		unsigned 		data0;
96bf215546Sopenharmony_ci		unsigned		data1;
97bf215546Sopenharmony_ci		unsigned		cmd;
98bf215546Sopenharmony_ci		unsigned		cntl;
99bf215546Sopenharmony_ci	} reg;
100bf215546Sopenharmony_ci};
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci/* flush IB to the hardware */
103bf215546Sopenharmony_cistatic int flush(struct ruvd_decoder *dec, unsigned flags)
104bf215546Sopenharmony_ci{
105bf215546Sopenharmony_ci	return dec->ws->cs_flush(&dec->cs, flags, NULL);
106bf215546Sopenharmony_ci}
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci/* add a new set register command to the IB */
109bf215546Sopenharmony_cistatic void set_reg(struct ruvd_decoder *dec, unsigned reg, uint32_t val)
110bf215546Sopenharmony_ci{
111bf215546Sopenharmony_ci	radeon_emit(&dec->cs, RUVD_PKT0(reg >> 2, 0));
112bf215546Sopenharmony_ci	radeon_emit(&dec->cs, val);
113bf215546Sopenharmony_ci}
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci/* send a command to the VCPU through the GPCOM registers */
116bf215546Sopenharmony_cistatic void send_cmd(struct ruvd_decoder *dec, unsigned cmd,
117bf215546Sopenharmony_ci		     struct pb_buffer* buf, uint32_t off,
118bf215546Sopenharmony_ci		     unsigned usage, enum radeon_bo_domain domain)
119bf215546Sopenharmony_ci{
120bf215546Sopenharmony_ci	int reloc_idx;
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci	reloc_idx = dec->ws->cs_add_buffer(&dec->cs, buf, usage | RADEON_USAGE_SYNCHRONIZED,
123bf215546Sopenharmony_ci					   domain);
124bf215546Sopenharmony_ci	if (!dec->use_legacy) {
125bf215546Sopenharmony_ci		uint64_t addr;
126bf215546Sopenharmony_ci		addr = dec->ws->buffer_get_virtual_address(buf);
127bf215546Sopenharmony_ci		addr = addr + off;
128bf215546Sopenharmony_ci		set_reg(dec, dec->reg.data0, addr);
129bf215546Sopenharmony_ci		set_reg(dec, dec->reg.data1, addr >> 32);
130bf215546Sopenharmony_ci	} else {
131bf215546Sopenharmony_ci		off += dec->ws->buffer_get_reloc_offset(buf);
132bf215546Sopenharmony_ci		set_reg(dec, RUVD_GPCOM_VCPU_DATA0, off);
133bf215546Sopenharmony_ci		set_reg(dec, RUVD_GPCOM_VCPU_DATA1, reloc_idx * 4);
134bf215546Sopenharmony_ci	}
135bf215546Sopenharmony_ci	set_reg(dec, dec->reg.cmd, cmd << 1);
136bf215546Sopenharmony_ci}
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci/* do the codec needs an IT buffer ?*/
139bf215546Sopenharmony_cistatic bool have_it(struct ruvd_decoder *dec)
140bf215546Sopenharmony_ci{
141bf215546Sopenharmony_ci	return dec->stream_type == RUVD_CODEC_H264_PERF ||
142bf215546Sopenharmony_ci		dec->stream_type == RUVD_CODEC_H265;
143bf215546Sopenharmony_ci}
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci/* map the next available message/feedback/itscaling buffer */
146bf215546Sopenharmony_cistatic void map_msg_fb_it_buf(struct ruvd_decoder *dec)
147bf215546Sopenharmony_ci{
148bf215546Sopenharmony_ci	struct rvid_buffer* buf;
149bf215546Sopenharmony_ci	uint8_t *ptr;
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_ci	/* grab the current message/feedback buffer */
152bf215546Sopenharmony_ci	buf = &dec->msg_fb_it_buffers[dec->cur_buffer];
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci	/* and map it for CPU access */
155bf215546Sopenharmony_ci	ptr = dec->ws->buffer_map(dec->ws, buf->res->buf, &dec->cs,
156bf215546Sopenharmony_ci                                  PIPE_MAP_WRITE | RADEON_MAP_TEMPORARY);
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_ci	/* calc buffer offsets */
159bf215546Sopenharmony_ci	dec->msg = (struct ruvd_msg *)ptr;
160bf215546Sopenharmony_ci	memset(dec->msg, 0, sizeof(*dec->msg));
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_ci	dec->fb = (uint32_t *)(ptr + FB_BUFFER_OFFSET);
163bf215546Sopenharmony_ci	if (have_it(dec))
164bf215546Sopenharmony_ci		dec->it = (uint8_t *)(ptr + FB_BUFFER_OFFSET + dec->fb_size);
165bf215546Sopenharmony_ci}
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci/* unmap and send a message command to the VCPU */
168bf215546Sopenharmony_cistatic void send_msg_buf(struct ruvd_decoder *dec)
169bf215546Sopenharmony_ci{
170bf215546Sopenharmony_ci	struct rvid_buffer* buf;
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_ci	/* ignore the request if message/feedback buffer isn't mapped */
173bf215546Sopenharmony_ci	if (!dec->msg || !dec->fb)
174bf215546Sopenharmony_ci		return;
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_ci	/* grab the current message buffer */
177bf215546Sopenharmony_ci	buf = &dec->msg_fb_it_buffers[dec->cur_buffer];
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_ci	/* unmap the buffer */
180bf215546Sopenharmony_ci	dec->ws->buffer_unmap(dec->ws, buf->res->buf);
181bf215546Sopenharmony_ci	dec->bs_ptr = NULL;
182bf215546Sopenharmony_ci	dec->msg = NULL;
183bf215546Sopenharmony_ci	dec->fb = NULL;
184bf215546Sopenharmony_ci	dec->it = NULL;
185bf215546Sopenharmony_ci
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci	if (dec->sessionctx.res)
188bf215546Sopenharmony_ci		send_cmd(dec, RUVD_CMD_SESSION_CONTEXT_BUFFER,
189bf215546Sopenharmony_ci			 dec->sessionctx.res->buf, 0, RADEON_USAGE_READWRITE,
190bf215546Sopenharmony_ci			 RADEON_DOMAIN_VRAM);
191bf215546Sopenharmony_ci
192bf215546Sopenharmony_ci	/* and send it to the hardware */
193bf215546Sopenharmony_ci	send_cmd(dec, RUVD_CMD_MSG_BUFFER, buf->res->buf, 0,
194bf215546Sopenharmony_ci		 RADEON_USAGE_READ, RADEON_DOMAIN_GTT);
195bf215546Sopenharmony_ci}
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_ci/* cycle to the next set of buffers */
198bf215546Sopenharmony_cistatic void next_buffer(struct ruvd_decoder *dec)
199bf215546Sopenharmony_ci{
200bf215546Sopenharmony_ci	++dec->cur_buffer;
201bf215546Sopenharmony_ci	dec->cur_buffer %= NUM_BUFFERS;
202bf215546Sopenharmony_ci}
203bf215546Sopenharmony_ci
204bf215546Sopenharmony_ci/* convert the profile into something UVD understands */
205bf215546Sopenharmony_cistatic uint32_t profile2stream_type(struct ruvd_decoder *dec, unsigned family)
206bf215546Sopenharmony_ci{
207bf215546Sopenharmony_ci	switch (u_reduce_video_profile(dec->base.profile)) {
208bf215546Sopenharmony_ci	case PIPE_VIDEO_FORMAT_MPEG4_AVC:
209bf215546Sopenharmony_ci		return RUVD_CODEC_H264;
210bf215546Sopenharmony_ci
211bf215546Sopenharmony_ci	case PIPE_VIDEO_FORMAT_VC1:
212bf215546Sopenharmony_ci		return RUVD_CODEC_VC1;
213bf215546Sopenharmony_ci
214bf215546Sopenharmony_ci	case PIPE_VIDEO_FORMAT_MPEG12:
215bf215546Sopenharmony_ci		return RUVD_CODEC_MPEG2;
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_ci	case PIPE_VIDEO_FORMAT_MPEG4:
218bf215546Sopenharmony_ci		return RUVD_CODEC_MPEG4;
219bf215546Sopenharmony_ci
220bf215546Sopenharmony_ci	case PIPE_VIDEO_FORMAT_JPEG:
221bf215546Sopenharmony_ci		return RUVD_CODEC_MJPEG;
222bf215546Sopenharmony_ci
223bf215546Sopenharmony_ci	default:
224bf215546Sopenharmony_ci		assert(0);
225bf215546Sopenharmony_ci		return 0;
226bf215546Sopenharmony_ci	}
227bf215546Sopenharmony_ci}
228bf215546Sopenharmony_ci
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_cistatic unsigned get_db_pitch_alignment(struct ruvd_decoder *dec)
231bf215546Sopenharmony_ci{
232bf215546Sopenharmony_ci	return 16;
233bf215546Sopenharmony_ci}
234bf215546Sopenharmony_ci
235bf215546Sopenharmony_ci/* calculate size of reference picture buffer */
236bf215546Sopenharmony_cistatic unsigned calc_dpb_size(struct ruvd_decoder *dec)
237bf215546Sopenharmony_ci{
238bf215546Sopenharmony_ci	unsigned width_in_mb, height_in_mb, image_size, dpb_size;
239bf215546Sopenharmony_ci
240bf215546Sopenharmony_ci	// always align them to MB size for dpb calculation
241bf215546Sopenharmony_ci	unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
242bf215546Sopenharmony_ci	unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
243bf215546Sopenharmony_ci
244bf215546Sopenharmony_ci	// always one more for currently decoded picture
245bf215546Sopenharmony_ci	unsigned max_references = dec->base.max_references + 1;
246bf215546Sopenharmony_ci
247bf215546Sopenharmony_ci	// aligned size of a single frame
248bf215546Sopenharmony_ci	image_size = align(width, get_db_pitch_alignment(dec)) * height;
249bf215546Sopenharmony_ci	image_size += image_size / 2;
250bf215546Sopenharmony_ci	image_size = align(image_size, 1024);
251bf215546Sopenharmony_ci
252bf215546Sopenharmony_ci	// picture width & height in 16 pixel units
253bf215546Sopenharmony_ci	width_in_mb = width / VL_MACROBLOCK_WIDTH;
254bf215546Sopenharmony_ci	height_in_mb = align(height / VL_MACROBLOCK_HEIGHT, 2);
255bf215546Sopenharmony_ci
256bf215546Sopenharmony_ci	switch (u_reduce_video_profile(dec->base.profile)) {
257bf215546Sopenharmony_ci	case PIPE_VIDEO_FORMAT_MPEG4_AVC: {
258bf215546Sopenharmony_ci		if (!dec->use_legacy) {
259bf215546Sopenharmony_ci			unsigned fs_in_mb = width_in_mb * height_in_mb;
260bf215546Sopenharmony_ci			unsigned alignment = 64, num_dpb_buffer;
261bf215546Sopenharmony_ci
262bf215546Sopenharmony_ci			if (dec->stream_type == RUVD_CODEC_H264_PERF)
263bf215546Sopenharmony_ci				alignment = 256;
264bf215546Sopenharmony_ci			switch(dec->base.level) {
265bf215546Sopenharmony_ci			case 30:
266bf215546Sopenharmony_ci				num_dpb_buffer = 8100 / fs_in_mb;
267bf215546Sopenharmony_ci				break;
268bf215546Sopenharmony_ci			case 31:
269bf215546Sopenharmony_ci				num_dpb_buffer = 18000 / fs_in_mb;
270bf215546Sopenharmony_ci				break;
271bf215546Sopenharmony_ci			case 32:
272bf215546Sopenharmony_ci				num_dpb_buffer = 20480 / fs_in_mb;
273bf215546Sopenharmony_ci				break;
274bf215546Sopenharmony_ci			case 41:
275bf215546Sopenharmony_ci				num_dpb_buffer = 32768 / fs_in_mb;
276bf215546Sopenharmony_ci				break;
277bf215546Sopenharmony_ci			case 42:
278bf215546Sopenharmony_ci				num_dpb_buffer = 34816 / fs_in_mb;
279bf215546Sopenharmony_ci				break;
280bf215546Sopenharmony_ci			case 50:
281bf215546Sopenharmony_ci				num_dpb_buffer = 110400 / fs_in_mb;
282bf215546Sopenharmony_ci				break;
283bf215546Sopenharmony_ci			case 51:
284bf215546Sopenharmony_ci				num_dpb_buffer = 184320 / fs_in_mb;
285bf215546Sopenharmony_ci				break;
286bf215546Sopenharmony_ci			default:
287bf215546Sopenharmony_ci				num_dpb_buffer = 184320 / fs_in_mb;
288bf215546Sopenharmony_ci				break;
289bf215546Sopenharmony_ci			}
290bf215546Sopenharmony_ci			num_dpb_buffer++;
291bf215546Sopenharmony_ci			max_references = MAX2(MIN2(NUM_H264_REFS, num_dpb_buffer), max_references);
292bf215546Sopenharmony_ci			dpb_size = image_size * max_references;
293bf215546Sopenharmony_ci			if ((dec->stream_type != RUVD_CODEC_H264_PERF)) {
294bf215546Sopenharmony_ci				dpb_size += max_references * align(width_in_mb * height_in_mb  * 192, alignment);
295bf215546Sopenharmony_ci				dpb_size += align(width_in_mb * height_in_mb * 32, alignment);
296bf215546Sopenharmony_ci			}
297bf215546Sopenharmony_ci		} else {
298bf215546Sopenharmony_ci			// the firmware seems to allways assume a minimum of ref frames
299bf215546Sopenharmony_ci			max_references = MAX2(NUM_H264_REFS, max_references);
300bf215546Sopenharmony_ci			// reference picture buffer
301bf215546Sopenharmony_ci			dpb_size = image_size * max_references;
302bf215546Sopenharmony_ci			if ((dec->stream_type != RUVD_CODEC_H264_PERF)) {
303bf215546Sopenharmony_ci				// macroblock context buffer
304bf215546Sopenharmony_ci				dpb_size += width_in_mb * height_in_mb * max_references * 192;
305bf215546Sopenharmony_ci				// IT surface buffer
306bf215546Sopenharmony_ci				dpb_size += width_in_mb * height_in_mb * 32;
307bf215546Sopenharmony_ci			}
308bf215546Sopenharmony_ci		}
309bf215546Sopenharmony_ci		break;
310bf215546Sopenharmony_ci	}
311bf215546Sopenharmony_ci
312bf215546Sopenharmony_ci	case PIPE_VIDEO_FORMAT_VC1:
313bf215546Sopenharmony_ci		// the firmware seems to allways assume a minimum of ref frames
314bf215546Sopenharmony_ci		max_references = MAX2(NUM_VC1_REFS, max_references);
315bf215546Sopenharmony_ci
316bf215546Sopenharmony_ci		// reference picture buffer
317bf215546Sopenharmony_ci		dpb_size = image_size * max_references;
318bf215546Sopenharmony_ci
319bf215546Sopenharmony_ci		// CONTEXT_BUFFER
320bf215546Sopenharmony_ci		dpb_size += width_in_mb * height_in_mb * 128;
321bf215546Sopenharmony_ci
322bf215546Sopenharmony_ci		// IT surface buffer
323bf215546Sopenharmony_ci		dpb_size += width_in_mb * 64;
324bf215546Sopenharmony_ci
325bf215546Sopenharmony_ci		// DB surface buffer
326bf215546Sopenharmony_ci		dpb_size += width_in_mb * 128;
327bf215546Sopenharmony_ci
328bf215546Sopenharmony_ci		// BP
329bf215546Sopenharmony_ci		dpb_size += align(MAX2(width_in_mb, height_in_mb) * 7 * 16, 64);
330bf215546Sopenharmony_ci		break;
331bf215546Sopenharmony_ci
332bf215546Sopenharmony_ci	case PIPE_VIDEO_FORMAT_MPEG12:
333bf215546Sopenharmony_ci		// reference picture buffer, must be big enough for all frames
334bf215546Sopenharmony_ci		dpb_size = image_size * NUM_MPEG2_REFS;
335bf215546Sopenharmony_ci		break;
336bf215546Sopenharmony_ci
337bf215546Sopenharmony_ci	case PIPE_VIDEO_FORMAT_MPEG4:
338bf215546Sopenharmony_ci		// reference picture buffer
339bf215546Sopenharmony_ci		dpb_size = image_size * max_references;
340bf215546Sopenharmony_ci
341bf215546Sopenharmony_ci		// CM
342bf215546Sopenharmony_ci		dpb_size += width_in_mb * height_in_mb * 64;
343bf215546Sopenharmony_ci
344bf215546Sopenharmony_ci		// IT surface buffer
345bf215546Sopenharmony_ci		dpb_size += align(width_in_mb * height_in_mb * 32, 64);
346bf215546Sopenharmony_ci
347bf215546Sopenharmony_ci		dpb_size = MAX2(dpb_size, 30 * 1024 * 1024);
348bf215546Sopenharmony_ci		break;
349bf215546Sopenharmony_ci
350bf215546Sopenharmony_ci	case PIPE_VIDEO_FORMAT_JPEG:
351bf215546Sopenharmony_ci		dpb_size = 0;
352bf215546Sopenharmony_ci		break;
353bf215546Sopenharmony_ci
354bf215546Sopenharmony_ci	default:
355bf215546Sopenharmony_ci		// something is missing here
356bf215546Sopenharmony_ci		assert(0);
357bf215546Sopenharmony_ci
358bf215546Sopenharmony_ci		// at least use a sane default value
359bf215546Sopenharmony_ci		dpb_size = 32 * 1024 * 1024;
360bf215546Sopenharmony_ci		break;
361bf215546Sopenharmony_ci	}
362bf215546Sopenharmony_ci	return dpb_size;
363bf215546Sopenharmony_ci}
364bf215546Sopenharmony_ci
365bf215546Sopenharmony_ci/* free associated data in the video buffer callback */
366bf215546Sopenharmony_cistatic void ruvd_destroy_associated_data(void *data)
367bf215546Sopenharmony_ci{
368bf215546Sopenharmony_ci	/* NOOP, since we only use an intptr */
369bf215546Sopenharmony_ci}
370bf215546Sopenharmony_ci
371bf215546Sopenharmony_ci/* get h264 specific message bits */
372bf215546Sopenharmony_cistatic struct ruvd_h264 get_h264_msg(struct ruvd_decoder *dec, struct pipe_h264_picture_desc *pic)
373bf215546Sopenharmony_ci{
374bf215546Sopenharmony_ci	struct ruvd_h264 result;
375bf215546Sopenharmony_ci
376bf215546Sopenharmony_ci	memset(&result, 0, sizeof(result));
377bf215546Sopenharmony_ci	switch (pic->base.profile) {
378bf215546Sopenharmony_ci	case PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE:
379bf215546Sopenharmony_ci	case PIPE_VIDEO_PROFILE_MPEG4_AVC_CONSTRAINED_BASELINE:
380bf215546Sopenharmony_ci		result.profile = RUVD_H264_PROFILE_BASELINE;
381bf215546Sopenharmony_ci		break;
382bf215546Sopenharmony_ci
383bf215546Sopenharmony_ci	case PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN:
384bf215546Sopenharmony_ci		result.profile = RUVD_H264_PROFILE_MAIN;
385bf215546Sopenharmony_ci		break;
386bf215546Sopenharmony_ci
387bf215546Sopenharmony_ci	case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH:
388bf215546Sopenharmony_ci		result.profile = RUVD_H264_PROFILE_HIGH;
389bf215546Sopenharmony_ci		break;
390bf215546Sopenharmony_ci
391bf215546Sopenharmony_ci	default:
392bf215546Sopenharmony_ci		assert(0);
393bf215546Sopenharmony_ci		break;
394bf215546Sopenharmony_ci	}
395bf215546Sopenharmony_ci
396bf215546Sopenharmony_ci	result.level = dec->base.level;
397bf215546Sopenharmony_ci
398bf215546Sopenharmony_ci	result.sps_info_flags = 0;
399bf215546Sopenharmony_ci	result.sps_info_flags |= pic->pps->sps->direct_8x8_inference_flag << 0;
400bf215546Sopenharmony_ci	result.sps_info_flags |= pic->pps->sps->mb_adaptive_frame_field_flag << 1;
401bf215546Sopenharmony_ci	result.sps_info_flags |= pic->pps->sps->frame_mbs_only_flag << 2;
402bf215546Sopenharmony_ci	result.sps_info_flags |= pic->pps->sps->delta_pic_order_always_zero_flag << 3;
403bf215546Sopenharmony_ci
404bf215546Sopenharmony_ci	result.bit_depth_luma_minus8 = pic->pps->sps->bit_depth_luma_minus8;
405bf215546Sopenharmony_ci	result.bit_depth_chroma_minus8 = pic->pps->sps->bit_depth_chroma_minus8;
406bf215546Sopenharmony_ci	result.log2_max_frame_num_minus4 = pic->pps->sps->log2_max_frame_num_minus4;
407bf215546Sopenharmony_ci	result.pic_order_cnt_type = pic->pps->sps->pic_order_cnt_type;
408bf215546Sopenharmony_ci	result.log2_max_pic_order_cnt_lsb_minus4 = pic->pps->sps->log2_max_pic_order_cnt_lsb_minus4;
409bf215546Sopenharmony_ci
410bf215546Sopenharmony_ci	switch (dec->base.chroma_format) {
411bf215546Sopenharmony_ci	case PIPE_VIDEO_CHROMA_FORMAT_NONE:
412bf215546Sopenharmony_ci		/* TODO: assert? */
413bf215546Sopenharmony_ci		break;
414bf215546Sopenharmony_ci	case PIPE_VIDEO_CHROMA_FORMAT_400:
415bf215546Sopenharmony_ci		result.chroma_format = 0;
416bf215546Sopenharmony_ci		break;
417bf215546Sopenharmony_ci	case PIPE_VIDEO_CHROMA_FORMAT_420:
418bf215546Sopenharmony_ci		result.chroma_format = 1;
419bf215546Sopenharmony_ci		break;
420bf215546Sopenharmony_ci	case PIPE_VIDEO_CHROMA_FORMAT_422:
421bf215546Sopenharmony_ci		result.chroma_format = 2;
422bf215546Sopenharmony_ci		break;
423bf215546Sopenharmony_ci	case PIPE_VIDEO_CHROMA_FORMAT_444:
424bf215546Sopenharmony_ci		result.chroma_format = 3;
425bf215546Sopenharmony_ci		break;
426bf215546Sopenharmony_ci	}
427bf215546Sopenharmony_ci
428bf215546Sopenharmony_ci	result.pps_info_flags = 0;
429bf215546Sopenharmony_ci	result.pps_info_flags |= pic->pps->transform_8x8_mode_flag << 0;
430bf215546Sopenharmony_ci	result.pps_info_flags |= pic->pps->redundant_pic_cnt_present_flag << 1;
431bf215546Sopenharmony_ci	result.pps_info_flags |= pic->pps->constrained_intra_pred_flag << 2;
432bf215546Sopenharmony_ci	result.pps_info_flags |= pic->pps->deblocking_filter_control_present_flag << 3;
433bf215546Sopenharmony_ci	result.pps_info_flags |= pic->pps->weighted_bipred_idc << 4;
434bf215546Sopenharmony_ci	result.pps_info_flags |= pic->pps->weighted_pred_flag << 6;
435bf215546Sopenharmony_ci	result.pps_info_flags |= pic->pps->bottom_field_pic_order_in_frame_present_flag << 7;
436bf215546Sopenharmony_ci	result.pps_info_flags |= pic->pps->entropy_coding_mode_flag << 8;
437bf215546Sopenharmony_ci
438bf215546Sopenharmony_ci	result.num_slice_groups_minus1 = pic->pps->num_slice_groups_minus1;
439bf215546Sopenharmony_ci	result.slice_group_map_type = pic->pps->slice_group_map_type;
440bf215546Sopenharmony_ci	result.slice_group_change_rate_minus1 = pic->pps->slice_group_change_rate_minus1;
441bf215546Sopenharmony_ci	result.pic_init_qp_minus26 = pic->pps->pic_init_qp_minus26;
442bf215546Sopenharmony_ci	result.chroma_qp_index_offset = pic->pps->chroma_qp_index_offset;
443bf215546Sopenharmony_ci	result.second_chroma_qp_index_offset = pic->pps->second_chroma_qp_index_offset;
444bf215546Sopenharmony_ci
445bf215546Sopenharmony_ci	memcpy(result.scaling_list_4x4, pic->pps->ScalingList4x4, 6*16);
446bf215546Sopenharmony_ci	memcpy(result.scaling_list_8x8, pic->pps->ScalingList8x8, 2*64);
447bf215546Sopenharmony_ci
448bf215546Sopenharmony_ci	if (dec->stream_type == RUVD_CODEC_H264_PERF) {
449bf215546Sopenharmony_ci		memcpy(dec->it, result.scaling_list_4x4, 6*16);
450bf215546Sopenharmony_ci		memcpy((dec->it + 96), result.scaling_list_8x8, 2*64);
451bf215546Sopenharmony_ci	}
452bf215546Sopenharmony_ci
453bf215546Sopenharmony_ci	result.num_ref_frames = pic->num_ref_frames;
454bf215546Sopenharmony_ci
455bf215546Sopenharmony_ci	result.num_ref_idx_l0_active_minus1 = pic->num_ref_idx_l0_active_minus1;
456bf215546Sopenharmony_ci	result.num_ref_idx_l1_active_minus1 = pic->num_ref_idx_l1_active_minus1;
457bf215546Sopenharmony_ci
458bf215546Sopenharmony_ci	result.frame_num = pic->frame_num;
459bf215546Sopenharmony_ci	memcpy(result.frame_num_list, pic->frame_num_list, 4*16);
460bf215546Sopenharmony_ci	result.curr_field_order_cnt_list[0] = pic->field_order_cnt[0];
461bf215546Sopenharmony_ci	result.curr_field_order_cnt_list[1] = pic->field_order_cnt[1];
462bf215546Sopenharmony_ci	memcpy(result.field_order_cnt_list, pic->field_order_cnt_list, 4*16*2);
463bf215546Sopenharmony_ci
464bf215546Sopenharmony_ci	result.decoded_pic_idx = pic->frame_num;
465bf215546Sopenharmony_ci
466bf215546Sopenharmony_ci	return result;
467bf215546Sopenharmony_ci}
468bf215546Sopenharmony_ci
469bf215546Sopenharmony_ci/* get vc1 specific message bits */
470bf215546Sopenharmony_cistatic struct ruvd_vc1 get_vc1_msg(struct pipe_vc1_picture_desc *pic)
471bf215546Sopenharmony_ci{
472bf215546Sopenharmony_ci	struct ruvd_vc1 result;
473bf215546Sopenharmony_ci
474bf215546Sopenharmony_ci	memset(&result, 0, sizeof(result));
475bf215546Sopenharmony_ci
476bf215546Sopenharmony_ci	switch(pic->base.profile) {
477bf215546Sopenharmony_ci	case PIPE_VIDEO_PROFILE_VC1_SIMPLE:
478bf215546Sopenharmony_ci		result.profile = RUVD_VC1_PROFILE_SIMPLE;
479bf215546Sopenharmony_ci		result.level = 1;
480bf215546Sopenharmony_ci		break;
481bf215546Sopenharmony_ci
482bf215546Sopenharmony_ci	case PIPE_VIDEO_PROFILE_VC1_MAIN:
483bf215546Sopenharmony_ci		result.profile = RUVD_VC1_PROFILE_MAIN;
484bf215546Sopenharmony_ci		result.level = 2;
485bf215546Sopenharmony_ci		break;
486bf215546Sopenharmony_ci
487bf215546Sopenharmony_ci	case PIPE_VIDEO_PROFILE_VC1_ADVANCED:
488bf215546Sopenharmony_ci		result.profile = RUVD_VC1_PROFILE_ADVANCED;
489bf215546Sopenharmony_ci		result.level = 4;
490bf215546Sopenharmony_ci		break;
491bf215546Sopenharmony_ci
492bf215546Sopenharmony_ci	default:
493bf215546Sopenharmony_ci		assert(0);
494bf215546Sopenharmony_ci	}
495bf215546Sopenharmony_ci
496bf215546Sopenharmony_ci	/* fields common for all profiles */
497bf215546Sopenharmony_ci	result.sps_info_flags |= pic->postprocflag << 7;
498bf215546Sopenharmony_ci	result.sps_info_flags |= pic->pulldown << 6;
499bf215546Sopenharmony_ci	result.sps_info_flags |= pic->interlace << 5;
500bf215546Sopenharmony_ci	result.sps_info_flags |= pic->tfcntrflag << 4;
501bf215546Sopenharmony_ci	result.sps_info_flags |= pic->finterpflag << 3;
502bf215546Sopenharmony_ci	result.sps_info_flags |= pic->psf << 1;
503bf215546Sopenharmony_ci
504bf215546Sopenharmony_ci	result.pps_info_flags |= pic->range_mapy_flag << 31;
505bf215546Sopenharmony_ci	result.pps_info_flags |= pic->range_mapy << 28;
506bf215546Sopenharmony_ci	result.pps_info_flags |= pic->range_mapuv_flag << 27;
507bf215546Sopenharmony_ci	result.pps_info_flags |= pic->range_mapuv << 24;
508bf215546Sopenharmony_ci	result.pps_info_flags |= pic->multires << 21;
509bf215546Sopenharmony_ci	result.pps_info_flags |= pic->maxbframes << 16;
510bf215546Sopenharmony_ci	result.pps_info_flags |= pic->overlap << 11;
511bf215546Sopenharmony_ci	result.pps_info_flags |= pic->quantizer << 9;
512bf215546Sopenharmony_ci	result.pps_info_flags |= pic->panscan_flag << 7;
513bf215546Sopenharmony_ci	result.pps_info_flags |= pic->refdist_flag << 6;
514bf215546Sopenharmony_ci	result.pps_info_flags |= pic->vstransform << 0;
515bf215546Sopenharmony_ci
516bf215546Sopenharmony_ci	/* some fields only apply to main/advanced profile */
517bf215546Sopenharmony_ci	if (pic->base.profile != PIPE_VIDEO_PROFILE_VC1_SIMPLE) {
518bf215546Sopenharmony_ci		result.pps_info_flags |= pic->syncmarker << 20;
519bf215546Sopenharmony_ci		result.pps_info_flags |= pic->rangered << 19;
520bf215546Sopenharmony_ci		result.pps_info_flags |= pic->loopfilter << 5;
521bf215546Sopenharmony_ci		result.pps_info_flags |= pic->fastuvmc << 4;
522bf215546Sopenharmony_ci		result.pps_info_flags |= pic->extended_mv << 3;
523bf215546Sopenharmony_ci		result.pps_info_flags |= pic->extended_dmv << 8;
524bf215546Sopenharmony_ci		result.pps_info_flags |= pic->dquant << 1;
525bf215546Sopenharmony_ci	}
526bf215546Sopenharmony_ci
527bf215546Sopenharmony_ci	result.chroma_format = 1;
528bf215546Sopenharmony_ci
529bf215546Sopenharmony_ci#if 0
530bf215546Sopenharmony_ci//(((unsigned int)(pPicParams->advance.reserved1))        << SPS_INFO_VC1_RESERVED_SHIFT)
531bf215546Sopenharmony_ciuint32_t 	slice_count
532bf215546Sopenharmony_ciuint8_t 	picture_type
533bf215546Sopenharmony_ciuint8_t 	frame_coding_mode
534bf215546Sopenharmony_ciuint8_t 	deblockEnable
535bf215546Sopenharmony_ciuint8_t 	pquant
536bf215546Sopenharmony_ci#endif
537bf215546Sopenharmony_ci
538bf215546Sopenharmony_ci	return result;
539bf215546Sopenharmony_ci}
540bf215546Sopenharmony_ci
541bf215546Sopenharmony_ci/* extract the frame number from a referenced video buffer */
542bf215546Sopenharmony_cistatic uint32_t get_ref_pic_idx(struct ruvd_decoder *dec, struct pipe_video_buffer *ref)
543bf215546Sopenharmony_ci{
544bf215546Sopenharmony_ci	uint32_t min = MAX2(dec->frame_number, NUM_MPEG2_REFS) - NUM_MPEG2_REFS;
545bf215546Sopenharmony_ci	uint32_t max = MAX2(dec->frame_number, 1) - 1;
546bf215546Sopenharmony_ci	uintptr_t frame;
547bf215546Sopenharmony_ci
548bf215546Sopenharmony_ci	/* seems to be the most sane fallback */
549bf215546Sopenharmony_ci	if (!ref)
550bf215546Sopenharmony_ci		return max;
551bf215546Sopenharmony_ci
552bf215546Sopenharmony_ci	/* get the frame number from the associated data */
553bf215546Sopenharmony_ci	frame = (uintptr_t)vl_video_buffer_get_associated_data(ref, &dec->base);
554bf215546Sopenharmony_ci
555bf215546Sopenharmony_ci	/* limit the frame number to a valid range */
556bf215546Sopenharmony_ci	return MAX2(MIN2(frame, max), min);
557bf215546Sopenharmony_ci}
558bf215546Sopenharmony_ci
559bf215546Sopenharmony_ci/* get mpeg2 specific msg bits */
560bf215546Sopenharmony_cistatic struct ruvd_mpeg2 get_mpeg2_msg(struct ruvd_decoder *dec,
561bf215546Sopenharmony_ci				       struct pipe_mpeg12_picture_desc *pic)
562bf215546Sopenharmony_ci{
563bf215546Sopenharmony_ci	const int *zscan = pic->alternate_scan ? vl_zscan_alternate : vl_zscan_normal;
564bf215546Sopenharmony_ci	struct ruvd_mpeg2 result;
565bf215546Sopenharmony_ci	unsigned i;
566bf215546Sopenharmony_ci
567bf215546Sopenharmony_ci	memset(&result, 0, sizeof(result));
568bf215546Sopenharmony_ci	result.decoded_pic_idx = dec->frame_number;
569bf215546Sopenharmony_ci	for (i = 0; i < 2; ++i)
570bf215546Sopenharmony_ci		result.ref_pic_idx[i] = get_ref_pic_idx(dec, pic->ref[i]);
571bf215546Sopenharmony_ci
572bf215546Sopenharmony_ci	result.load_intra_quantiser_matrix = 1;
573bf215546Sopenharmony_ci	result.load_nonintra_quantiser_matrix = 1;
574bf215546Sopenharmony_ci
575bf215546Sopenharmony_ci	for (i = 0; i < 64; ++i) {
576bf215546Sopenharmony_ci		result.intra_quantiser_matrix[i] = pic->intra_matrix[zscan[i]];
577bf215546Sopenharmony_ci		result.nonintra_quantiser_matrix[i] = pic->non_intra_matrix[zscan[i]];
578bf215546Sopenharmony_ci	}
579bf215546Sopenharmony_ci
580bf215546Sopenharmony_ci	result.profile_and_level_indication = 0;
581bf215546Sopenharmony_ci	result.chroma_format = 0x1;
582bf215546Sopenharmony_ci
583bf215546Sopenharmony_ci	result.picture_coding_type = pic->picture_coding_type;
584bf215546Sopenharmony_ci	result.f_code[0][0] = pic->f_code[0][0] + 1;
585bf215546Sopenharmony_ci	result.f_code[0][1] = pic->f_code[0][1] + 1;
586bf215546Sopenharmony_ci	result.f_code[1][0] = pic->f_code[1][0] + 1;
587bf215546Sopenharmony_ci	result.f_code[1][1] = pic->f_code[1][1] + 1;
588bf215546Sopenharmony_ci	result.intra_dc_precision = pic->intra_dc_precision;
589bf215546Sopenharmony_ci	result.pic_structure = pic->picture_structure;
590bf215546Sopenharmony_ci	result.top_field_first = pic->top_field_first;
591bf215546Sopenharmony_ci	result.frame_pred_frame_dct = pic->frame_pred_frame_dct;
592bf215546Sopenharmony_ci	result.concealment_motion_vectors = pic->concealment_motion_vectors;
593bf215546Sopenharmony_ci	result.q_scale_type = pic->q_scale_type;
594bf215546Sopenharmony_ci	result.intra_vlc_format = pic->intra_vlc_format;
595bf215546Sopenharmony_ci	result.alternate_scan = pic->alternate_scan;
596bf215546Sopenharmony_ci
597bf215546Sopenharmony_ci	return result;
598bf215546Sopenharmony_ci}
599bf215546Sopenharmony_ci
600bf215546Sopenharmony_ci/* get mpeg4 specific msg bits */
601bf215546Sopenharmony_cistatic struct ruvd_mpeg4 get_mpeg4_msg(struct ruvd_decoder *dec,
602bf215546Sopenharmony_ci				       struct pipe_mpeg4_picture_desc *pic)
603bf215546Sopenharmony_ci{
604bf215546Sopenharmony_ci	struct ruvd_mpeg4 result;
605bf215546Sopenharmony_ci	unsigned i;
606bf215546Sopenharmony_ci
607bf215546Sopenharmony_ci	memset(&result, 0, sizeof(result));
608bf215546Sopenharmony_ci	result.decoded_pic_idx = dec->frame_number;
609bf215546Sopenharmony_ci	for (i = 0; i < 2; ++i)
610bf215546Sopenharmony_ci		result.ref_pic_idx[i] = get_ref_pic_idx(dec, pic->ref[i]);
611bf215546Sopenharmony_ci
612bf215546Sopenharmony_ci	result.variant_type = 0;
613bf215546Sopenharmony_ci	result.profile_and_level_indication = 0xF0; // ASP Level0
614bf215546Sopenharmony_ci
615bf215546Sopenharmony_ci	result.video_object_layer_verid = 0x5; // advanced simple
616bf215546Sopenharmony_ci	result.video_object_layer_shape = 0x0; // rectangular
617bf215546Sopenharmony_ci
618bf215546Sopenharmony_ci	result.video_object_layer_width = dec->base.width;
619bf215546Sopenharmony_ci	result.video_object_layer_height = dec->base.height;
620bf215546Sopenharmony_ci
621bf215546Sopenharmony_ci	result.vop_time_increment_resolution = pic->vop_time_increment_resolution;
622bf215546Sopenharmony_ci
623bf215546Sopenharmony_ci	result.flags |= pic->short_video_header << 0;
624bf215546Sopenharmony_ci	//result.flags |= obmc_disable << 1;
625bf215546Sopenharmony_ci	result.flags |= pic->interlaced << 2;
626bf215546Sopenharmony_ci        result.flags |= 1 << 3; // load_intra_quant_mat
627bf215546Sopenharmony_ci	result.flags |= 1 << 4; // load_nonintra_quant_mat
628bf215546Sopenharmony_ci	result.flags |= pic->quarter_sample << 5;
629bf215546Sopenharmony_ci	result.flags |= 1 << 6; // complexity_estimation_disable
630bf215546Sopenharmony_ci	result.flags |= pic->resync_marker_disable << 7;
631bf215546Sopenharmony_ci	//result.flags |= data_partitioned << 8;
632bf215546Sopenharmony_ci	//result.flags |= reversible_vlc << 9;
633bf215546Sopenharmony_ci	result.flags |= 0 << 10; // newpred_enable
634bf215546Sopenharmony_ci	result.flags |= 0 << 11; // reduced_resolution_vop_enable
635bf215546Sopenharmony_ci	//result.flags |= scalability << 12;
636bf215546Sopenharmony_ci	//result.flags |= is_object_layer_identifier << 13;
637bf215546Sopenharmony_ci	//result.flags |= fixed_vop_rate << 14;
638bf215546Sopenharmony_ci	//result.flags |= newpred_segment_type << 15;
639bf215546Sopenharmony_ci
640bf215546Sopenharmony_ci	result.quant_type = pic->quant_type;
641bf215546Sopenharmony_ci
642bf215546Sopenharmony_ci	for (i = 0; i < 64; ++i) {
643bf215546Sopenharmony_ci		result.intra_quant_mat[i] = pic->intra_matrix[vl_zscan_normal[i]];
644bf215546Sopenharmony_ci		result.nonintra_quant_mat[i] = pic->non_intra_matrix[vl_zscan_normal[i]];
645bf215546Sopenharmony_ci	}
646bf215546Sopenharmony_ci
647bf215546Sopenharmony_ci	/*
648bf215546Sopenharmony_ci	int32_t 	trd [2]
649bf215546Sopenharmony_ci	int32_t 	trb [2]
650bf215546Sopenharmony_ci	uint8_t 	vop_coding_type
651bf215546Sopenharmony_ci	uint8_t 	vop_fcode_forward
652bf215546Sopenharmony_ci	uint8_t 	vop_fcode_backward
653bf215546Sopenharmony_ci	uint8_t 	rounding_control
654bf215546Sopenharmony_ci	uint8_t 	alternate_vertical_scan_flag
655bf215546Sopenharmony_ci	uint8_t 	top_field_first
656bf215546Sopenharmony_ci	*/
657bf215546Sopenharmony_ci
658bf215546Sopenharmony_ci	return result;
659bf215546Sopenharmony_ci}
660bf215546Sopenharmony_ci
661bf215546Sopenharmony_cistatic void get_mjpeg_slice_header(struct ruvd_decoder *dec, struct pipe_mjpeg_picture_desc *pic)
662bf215546Sopenharmony_ci{
663bf215546Sopenharmony_ci	int size = 0, saved_size, len_pos, i;
664bf215546Sopenharmony_ci	uint16_t *bs;
665bf215546Sopenharmony_ci	uint8_t *buf = dec->bs_ptr;
666bf215546Sopenharmony_ci
667bf215546Sopenharmony_ci	/* SOI */
668bf215546Sopenharmony_ci	buf[size++] = 0xff;
669bf215546Sopenharmony_ci	buf[size++] = 0xd8;
670bf215546Sopenharmony_ci
671bf215546Sopenharmony_ci	/* DQT */
672bf215546Sopenharmony_ci	buf[size++] = 0xff;
673bf215546Sopenharmony_ci	buf[size++] = 0xdb;
674bf215546Sopenharmony_ci
675bf215546Sopenharmony_ci	len_pos = size++;
676bf215546Sopenharmony_ci	size++;
677bf215546Sopenharmony_ci
678bf215546Sopenharmony_ci	for (i = 0; i < 4; ++i) {
679bf215546Sopenharmony_ci		if (pic->quantization_table.load_quantiser_table[i] == 0)
680bf215546Sopenharmony_ci			continue;
681bf215546Sopenharmony_ci
682bf215546Sopenharmony_ci		buf[size++] = i;
683bf215546Sopenharmony_ci		memcpy((buf + size), &pic->quantization_table.quantiser_table[i], 64);
684bf215546Sopenharmony_ci		size += 64;
685bf215546Sopenharmony_ci	}
686bf215546Sopenharmony_ci
687bf215546Sopenharmony_ci	bs = (uint16_t*)&buf[len_pos];
688bf215546Sopenharmony_ci	*bs = util_bswap16(size - 4);
689bf215546Sopenharmony_ci
690bf215546Sopenharmony_ci	saved_size = size;
691bf215546Sopenharmony_ci
692bf215546Sopenharmony_ci	/* DHT */
693bf215546Sopenharmony_ci	buf[size++] = 0xff;
694bf215546Sopenharmony_ci	buf[size++] = 0xc4;
695bf215546Sopenharmony_ci
696bf215546Sopenharmony_ci	len_pos = size++;
697bf215546Sopenharmony_ci	size++;
698bf215546Sopenharmony_ci
699bf215546Sopenharmony_ci	for (i = 0; i < 2; ++i) {
700bf215546Sopenharmony_ci		if (pic->huffman_table.load_huffman_table[i] == 0)
701bf215546Sopenharmony_ci			continue;
702bf215546Sopenharmony_ci
703bf215546Sopenharmony_ci		buf[size++] = 0x00 | i;
704bf215546Sopenharmony_ci		memcpy((buf + size), &pic->huffman_table.table[i].num_dc_codes, 16);
705bf215546Sopenharmony_ci		size += 16;
706bf215546Sopenharmony_ci		memcpy((buf + size), &pic->huffman_table.table[i].dc_values, 12);
707bf215546Sopenharmony_ci		size += 12;
708bf215546Sopenharmony_ci	}
709bf215546Sopenharmony_ci
710bf215546Sopenharmony_ci	for (i = 0; i < 2; ++i) {
711bf215546Sopenharmony_ci		if (pic->huffman_table.load_huffman_table[i] == 0)
712bf215546Sopenharmony_ci			continue;
713bf215546Sopenharmony_ci
714bf215546Sopenharmony_ci		buf[size++] = 0x10 | i;
715bf215546Sopenharmony_ci		memcpy((buf + size), &pic->huffman_table.table[i].num_ac_codes, 16);
716bf215546Sopenharmony_ci		size += 16;
717bf215546Sopenharmony_ci		memcpy((buf + size), &pic->huffman_table.table[i].ac_values, 162);
718bf215546Sopenharmony_ci		size += 162;
719bf215546Sopenharmony_ci	}
720bf215546Sopenharmony_ci
721bf215546Sopenharmony_ci	bs = (uint16_t*)&buf[len_pos];
722bf215546Sopenharmony_ci	*bs = util_bswap16(size - saved_size - 2);
723bf215546Sopenharmony_ci
724bf215546Sopenharmony_ci	saved_size = size;
725bf215546Sopenharmony_ci
726bf215546Sopenharmony_ci	/* DRI */
727bf215546Sopenharmony_ci	if (pic->slice_parameter.restart_interval) {
728bf215546Sopenharmony_ci		buf[size++] = 0xff;
729bf215546Sopenharmony_ci		buf[size++] = 0xdd;
730bf215546Sopenharmony_ci		buf[size++] = 0x00;
731bf215546Sopenharmony_ci		buf[size++] = 0x04;
732bf215546Sopenharmony_ci		bs = (uint16_t*)&buf[size++];
733bf215546Sopenharmony_ci		*bs = util_bswap16(pic->slice_parameter.restart_interval);
734bf215546Sopenharmony_ci		saved_size = ++size;
735bf215546Sopenharmony_ci	}
736bf215546Sopenharmony_ci
737bf215546Sopenharmony_ci	/* SOF */
738bf215546Sopenharmony_ci	buf[size++] = 0xff;
739bf215546Sopenharmony_ci	buf[size++] = 0xc0;
740bf215546Sopenharmony_ci
741bf215546Sopenharmony_ci	len_pos = size++;
742bf215546Sopenharmony_ci	size++;
743bf215546Sopenharmony_ci
744bf215546Sopenharmony_ci	buf[size++] = 0x08;
745bf215546Sopenharmony_ci
746bf215546Sopenharmony_ci	bs = (uint16_t*)&buf[size++];
747bf215546Sopenharmony_ci	*bs = util_bswap16(pic->picture_parameter.picture_height);
748bf215546Sopenharmony_ci	size++;
749bf215546Sopenharmony_ci
750bf215546Sopenharmony_ci	bs = (uint16_t*)&buf[size++];
751bf215546Sopenharmony_ci	*bs = util_bswap16(pic->picture_parameter.picture_width);
752bf215546Sopenharmony_ci	size++;
753bf215546Sopenharmony_ci
754bf215546Sopenharmony_ci	buf[size++] = pic->picture_parameter.num_components;
755bf215546Sopenharmony_ci
756bf215546Sopenharmony_ci	for (i = 0; i < pic->picture_parameter.num_components; ++i) {
757bf215546Sopenharmony_ci		buf[size++] = pic->picture_parameter.components[i].component_id;
758bf215546Sopenharmony_ci		buf[size++] = pic->picture_parameter.components[i].h_sampling_factor << 4 |
759bf215546Sopenharmony_ci			pic->picture_parameter.components[i].v_sampling_factor;
760bf215546Sopenharmony_ci		buf[size++] = pic->picture_parameter.components[i].quantiser_table_selector;
761bf215546Sopenharmony_ci	}
762bf215546Sopenharmony_ci
763bf215546Sopenharmony_ci	bs = (uint16_t*)&buf[len_pos];
764bf215546Sopenharmony_ci	*bs = util_bswap16(size - saved_size - 2);
765bf215546Sopenharmony_ci
766bf215546Sopenharmony_ci	saved_size = size;
767bf215546Sopenharmony_ci
768bf215546Sopenharmony_ci	/* SOS */
769bf215546Sopenharmony_ci	buf[size++] = 0xff;
770bf215546Sopenharmony_ci	buf[size++] = 0xda;
771bf215546Sopenharmony_ci
772bf215546Sopenharmony_ci	len_pos = size++;
773bf215546Sopenharmony_ci	size++;
774bf215546Sopenharmony_ci
775bf215546Sopenharmony_ci	buf[size++] = pic->slice_parameter.num_components;
776bf215546Sopenharmony_ci
777bf215546Sopenharmony_ci	for (i = 0; i < pic->slice_parameter.num_components; ++i) {
778bf215546Sopenharmony_ci		buf[size++] = pic->slice_parameter.components[i].component_selector;
779bf215546Sopenharmony_ci		buf[size++] = pic->slice_parameter.components[i].dc_table_selector << 4 |
780bf215546Sopenharmony_ci			pic->slice_parameter.components[i].ac_table_selector;
781bf215546Sopenharmony_ci	}
782bf215546Sopenharmony_ci
783bf215546Sopenharmony_ci	buf[size++] = 0x00;
784bf215546Sopenharmony_ci	buf[size++] = 0x3f;
785bf215546Sopenharmony_ci	buf[size++] = 0x00;
786bf215546Sopenharmony_ci
787bf215546Sopenharmony_ci	bs = (uint16_t*)&buf[len_pos];
788bf215546Sopenharmony_ci	*bs = util_bswap16(size - saved_size - 2);
789bf215546Sopenharmony_ci
790bf215546Sopenharmony_ci	dec->bs_ptr += size;
791bf215546Sopenharmony_ci	dec->bs_size += size;
792bf215546Sopenharmony_ci}
793bf215546Sopenharmony_ci
794bf215546Sopenharmony_ci/**
795bf215546Sopenharmony_ci * destroy this video decoder
796bf215546Sopenharmony_ci */
797bf215546Sopenharmony_cistatic void ruvd_destroy(struct pipe_video_codec *decoder)
798bf215546Sopenharmony_ci{
799bf215546Sopenharmony_ci	struct ruvd_decoder *dec = (struct ruvd_decoder*)decoder;
800bf215546Sopenharmony_ci	unsigned i;
801bf215546Sopenharmony_ci
802bf215546Sopenharmony_ci	assert(decoder);
803bf215546Sopenharmony_ci
804bf215546Sopenharmony_ci	map_msg_fb_it_buf(dec);
805bf215546Sopenharmony_ci	dec->msg->size = sizeof(*dec->msg);
806bf215546Sopenharmony_ci	dec->msg->msg_type = RUVD_MSG_DESTROY;
807bf215546Sopenharmony_ci	dec->msg->stream_handle = dec->stream_handle;
808bf215546Sopenharmony_ci	send_msg_buf(dec);
809bf215546Sopenharmony_ci
810bf215546Sopenharmony_ci	flush(dec, 0);
811bf215546Sopenharmony_ci
812bf215546Sopenharmony_ci	dec->ws->cs_destroy(&dec->cs);
813bf215546Sopenharmony_ci
814bf215546Sopenharmony_ci	for (i = 0; i < NUM_BUFFERS; ++i) {
815bf215546Sopenharmony_ci		rvid_destroy_buffer(&dec->msg_fb_it_buffers[i]);
816bf215546Sopenharmony_ci		rvid_destroy_buffer(&dec->bs_buffers[i]);
817bf215546Sopenharmony_ci	}
818bf215546Sopenharmony_ci
819bf215546Sopenharmony_ci	rvid_destroy_buffer(&dec->dpb);
820bf215546Sopenharmony_ci	rvid_destroy_buffer(&dec->ctx);
821bf215546Sopenharmony_ci	rvid_destroy_buffer(&dec->sessionctx);
822bf215546Sopenharmony_ci
823bf215546Sopenharmony_ci	FREE(dec);
824bf215546Sopenharmony_ci}
825bf215546Sopenharmony_ci
826bf215546Sopenharmony_ci/**
827bf215546Sopenharmony_ci * start decoding of a new frame
828bf215546Sopenharmony_ci */
829bf215546Sopenharmony_cistatic void ruvd_begin_frame(struct pipe_video_codec *decoder,
830bf215546Sopenharmony_ci			     struct pipe_video_buffer *target,
831bf215546Sopenharmony_ci			     struct pipe_picture_desc *picture)
832bf215546Sopenharmony_ci{
833bf215546Sopenharmony_ci	struct ruvd_decoder *dec = (struct ruvd_decoder*)decoder;
834bf215546Sopenharmony_ci	uintptr_t frame;
835bf215546Sopenharmony_ci
836bf215546Sopenharmony_ci	assert(decoder);
837bf215546Sopenharmony_ci
838bf215546Sopenharmony_ci	frame = ++dec->frame_number;
839bf215546Sopenharmony_ci	vl_video_buffer_set_associated_data(target, decoder, (void *)frame,
840bf215546Sopenharmony_ci					    &ruvd_destroy_associated_data);
841bf215546Sopenharmony_ci
842bf215546Sopenharmony_ci	dec->bs_size = 0;
843bf215546Sopenharmony_ci	dec->bs_ptr = dec->ws->buffer_map(dec->ws,
844bf215546Sopenharmony_ci		dec->bs_buffers[dec->cur_buffer].res->buf,
845bf215546Sopenharmony_ci		&dec->cs, PIPE_MAP_WRITE | RADEON_MAP_TEMPORARY);
846bf215546Sopenharmony_ci}
847bf215546Sopenharmony_ci
848bf215546Sopenharmony_ci/**
849bf215546Sopenharmony_ci * decode a macroblock
850bf215546Sopenharmony_ci */
851bf215546Sopenharmony_cistatic void ruvd_decode_macroblock(struct pipe_video_codec *decoder,
852bf215546Sopenharmony_ci				   struct pipe_video_buffer *target,
853bf215546Sopenharmony_ci				   struct pipe_picture_desc *picture,
854bf215546Sopenharmony_ci				   const struct pipe_macroblock *macroblocks,
855bf215546Sopenharmony_ci				   unsigned num_macroblocks)
856bf215546Sopenharmony_ci{
857bf215546Sopenharmony_ci	/* not supported (yet) */
858bf215546Sopenharmony_ci	assert(0);
859bf215546Sopenharmony_ci}
860bf215546Sopenharmony_ci
861bf215546Sopenharmony_ci/**
862bf215546Sopenharmony_ci * decode a bitstream
863bf215546Sopenharmony_ci */
864bf215546Sopenharmony_cistatic void ruvd_decode_bitstream(struct pipe_video_codec *decoder,
865bf215546Sopenharmony_ci				  struct pipe_video_buffer *target,
866bf215546Sopenharmony_ci				  struct pipe_picture_desc *picture,
867bf215546Sopenharmony_ci				  unsigned num_buffers,
868bf215546Sopenharmony_ci				  const void * const *buffers,
869bf215546Sopenharmony_ci				  const unsigned *sizes)
870bf215546Sopenharmony_ci{
871bf215546Sopenharmony_ci	struct ruvd_decoder *dec = (struct ruvd_decoder*)decoder;
872bf215546Sopenharmony_ci	enum pipe_video_format format = u_reduce_video_profile(picture->profile);
873bf215546Sopenharmony_ci	unsigned i;
874bf215546Sopenharmony_ci
875bf215546Sopenharmony_ci	assert(decoder);
876bf215546Sopenharmony_ci
877bf215546Sopenharmony_ci	if (!dec->bs_ptr)
878bf215546Sopenharmony_ci		return;
879bf215546Sopenharmony_ci
880bf215546Sopenharmony_ci	if (format == PIPE_VIDEO_FORMAT_JPEG)
881bf215546Sopenharmony_ci		get_mjpeg_slice_header(dec, (struct pipe_mjpeg_picture_desc*)picture);
882bf215546Sopenharmony_ci
883bf215546Sopenharmony_ci	for (i = 0; i < num_buffers; ++i) {
884bf215546Sopenharmony_ci		struct rvid_buffer *buf = &dec->bs_buffers[dec->cur_buffer];
885bf215546Sopenharmony_ci		unsigned new_size = dec->bs_size + sizes[i];
886bf215546Sopenharmony_ci
887bf215546Sopenharmony_ci		if (format == PIPE_VIDEO_FORMAT_JPEG)
888bf215546Sopenharmony_ci			new_size += 2; /* save for EOI */
889bf215546Sopenharmony_ci
890bf215546Sopenharmony_ci		if (new_size > buf->res->buf->size) {
891bf215546Sopenharmony_ci			dec->ws->buffer_unmap(dec->ws, buf->res->buf);
892bf215546Sopenharmony_ci			dec->bs_ptr = NULL;
893bf215546Sopenharmony_ci			if (!rvid_resize_buffer(dec->screen, &dec->cs, buf, new_size)) {
894bf215546Sopenharmony_ci				RVID_ERR("Can't resize bitstream buffer!");
895bf215546Sopenharmony_ci				return;
896bf215546Sopenharmony_ci			}
897bf215546Sopenharmony_ci
898bf215546Sopenharmony_ci			dec->bs_ptr = dec->ws->buffer_map(dec->ws, buf->res->buf, &dec->cs,
899bf215546Sopenharmony_ci							  PIPE_MAP_WRITE |
900bf215546Sopenharmony_ci							  RADEON_MAP_TEMPORARY);
901bf215546Sopenharmony_ci			if (!dec->bs_ptr)
902bf215546Sopenharmony_ci				return;
903bf215546Sopenharmony_ci
904bf215546Sopenharmony_ci			dec->bs_ptr += dec->bs_size;
905bf215546Sopenharmony_ci		}
906bf215546Sopenharmony_ci
907bf215546Sopenharmony_ci		memcpy(dec->bs_ptr, buffers[i], sizes[i]);
908bf215546Sopenharmony_ci		dec->bs_size += sizes[i];
909bf215546Sopenharmony_ci		dec->bs_ptr += sizes[i];
910bf215546Sopenharmony_ci	}
911bf215546Sopenharmony_ci
912bf215546Sopenharmony_ci	if (format == PIPE_VIDEO_FORMAT_JPEG) {
913bf215546Sopenharmony_ci		((uint8_t *)dec->bs_ptr)[0] = 0xff;	/* EOI */
914bf215546Sopenharmony_ci		((uint8_t *)dec->bs_ptr)[1] = 0xd9;
915bf215546Sopenharmony_ci		dec->bs_size += 2;
916bf215546Sopenharmony_ci		dec->bs_ptr += 2;
917bf215546Sopenharmony_ci	}
918bf215546Sopenharmony_ci}
919bf215546Sopenharmony_ci
920bf215546Sopenharmony_ci/**
921bf215546Sopenharmony_ci * end decoding of the current frame
922bf215546Sopenharmony_ci */
923bf215546Sopenharmony_cistatic void ruvd_end_frame(struct pipe_video_codec *decoder,
924bf215546Sopenharmony_ci			   struct pipe_video_buffer *target,
925bf215546Sopenharmony_ci			   struct pipe_picture_desc *picture)
926bf215546Sopenharmony_ci{
927bf215546Sopenharmony_ci	struct ruvd_decoder *dec = (struct ruvd_decoder*)decoder;
928bf215546Sopenharmony_ci	struct pb_buffer *dt;
929bf215546Sopenharmony_ci	struct rvid_buffer *msg_fb_it_buf, *bs_buf;
930bf215546Sopenharmony_ci	unsigned bs_size;
931bf215546Sopenharmony_ci
932bf215546Sopenharmony_ci	assert(decoder);
933bf215546Sopenharmony_ci
934bf215546Sopenharmony_ci	if (!dec->bs_ptr)
935bf215546Sopenharmony_ci		return;
936bf215546Sopenharmony_ci
937bf215546Sopenharmony_ci	msg_fb_it_buf = &dec->msg_fb_it_buffers[dec->cur_buffer];
938bf215546Sopenharmony_ci	bs_buf = &dec->bs_buffers[dec->cur_buffer];
939bf215546Sopenharmony_ci
940bf215546Sopenharmony_ci	bs_size = align(dec->bs_size, 128);
941bf215546Sopenharmony_ci	memset(dec->bs_ptr, 0, bs_size - dec->bs_size);
942bf215546Sopenharmony_ci	dec->ws->buffer_unmap(dec->ws, bs_buf->res->buf);
943bf215546Sopenharmony_ci	dec->bs_ptr = NULL;
944bf215546Sopenharmony_ci
945bf215546Sopenharmony_ci	map_msg_fb_it_buf(dec);
946bf215546Sopenharmony_ci	dec->msg->size = sizeof(*dec->msg);
947bf215546Sopenharmony_ci	dec->msg->msg_type = RUVD_MSG_DECODE;
948bf215546Sopenharmony_ci	dec->msg->stream_handle = dec->stream_handle;
949bf215546Sopenharmony_ci	dec->msg->status_report_feedback_number = dec->frame_number;
950bf215546Sopenharmony_ci
951bf215546Sopenharmony_ci	dec->msg->body.decode.stream_type = dec->stream_type;
952bf215546Sopenharmony_ci	dec->msg->body.decode.decode_flags = 0x1;
953bf215546Sopenharmony_ci	dec->msg->body.decode.width_in_samples = dec->base.width;
954bf215546Sopenharmony_ci	dec->msg->body.decode.height_in_samples = dec->base.height;
955bf215546Sopenharmony_ci
956bf215546Sopenharmony_ci	if ((picture->profile == PIPE_VIDEO_PROFILE_VC1_SIMPLE) ||
957bf215546Sopenharmony_ci	    (picture->profile == PIPE_VIDEO_PROFILE_VC1_MAIN)) {
958bf215546Sopenharmony_ci		dec->msg->body.decode.width_in_samples = align(dec->msg->body.decode.width_in_samples, 16) / 16;
959bf215546Sopenharmony_ci		dec->msg->body.decode.height_in_samples = align(dec->msg->body.decode.height_in_samples, 16) / 16;
960bf215546Sopenharmony_ci	}
961bf215546Sopenharmony_ci
962bf215546Sopenharmony_ci	if (dec->dpb.res)
963bf215546Sopenharmony_ci		dec->msg->body.decode.dpb_size = dec->dpb.res->buf->size;
964bf215546Sopenharmony_ci	dec->msg->body.decode.bsd_size = bs_size;
965bf215546Sopenharmony_ci	dec->msg->body.decode.db_pitch = align(dec->base.width, get_db_pitch_alignment(dec));
966bf215546Sopenharmony_ci
967bf215546Sopenharmony_ci	dt = dec->set_dtb(dec->msg, (struct vl_video_buffer *)target);
968bf215546Sopenharmony_ci
969bf215546Sopenharmony_ci	switch (u_reduce_video_profile(picture->profile)) {
970bf215546Sopenharmony_ci	case PIPE_VIDEO_FORMAT_MPEG4_AVC:
971bf215546Sopenharmony_ci		dec->msg->body.decode.codec.h264 = get_h264_msg(dec, (struct pipe_h264_picture_desc*)picture);
972bf215546Sopenharmony_ci		break;
973bf215546Sopenharmony_ci
974bf215546Sopenharmony_ci	case PIPE_VIDEO_FORMAT_VC1:
975bf215546Sopenharmony_ci		dec->msg->body.decode.codec.vc1 = get_vc1_msg((struct pipe_vc1_picture_desc*)picture);
976bf215546Sopenharmony_ci		break;
977bf215546Sopenharmony_ci
978bf215546Sopenharmony_ci	case PIPE_VIDEO_FORMAT_MPEG12:
979bf215546Sopenharmony_ci		dec->msg->body.decode.codec.mpeg2 = get_mpeg2_msg(dec, (struct pipe_mpeg12_picture_desc*)picture);
980bf215546Sopenharmony_ci		break;
981bf215546Sopenharmony_ci
982bf215546Sopenharmony_ci	case PIPE_VIDEO_FORMAT_MPEG4:
983bf215546Sopenharmony_ci		dec->msg->body.decode.codec.mpeg4 = get_mpeg4_msg(dec, (struct pipe_mpeg4_picture_desc*)picture);
984bf215546Sopenharmony_ci		break;
985bf215546Sopenharmony_ci
986bf215546Sopenharmony_ci	case PIPE_VIDEO_FORMAT_JPEG:
987bf215546Sopenharmony_ci		break;
988bf215546Sopenharmony_ci
989bf215546Sopenharmony_ci	default:
990bf215546Sopenharmony_ci		assert(0);
991bf215546Sopenharmony_ci		return;
992bf215546Sopenharmony_ci	}
993bf215546Sopenharmony_ci
994bf215546Sopenharmony_ci	dec->msg->body.decode.db_surf_tile_config = dec->msg->body.decode.dt_surf_tile_config;
995bf215546Sopenharmony_ci	dec->msg->body.decode.extension_support = 0x1;
996bf215546Sopenharmony_ci
997bf215546Sopenharmony_ci	/* set at least the feedback buffer size */
998bf215546Sopenharmony_ci	dec->fb[0] = dec->fb_size;
999bf215546Sopenharmony_ci
1000bf215546Sopenharmony_ci	send_msg_buf(dec);
1001bf215546Sopenharmony_ci
1002bf215546Sopenharmony_ci	if (dec->dpb.res)
1003bf215546Sopenharmony_ci		send_cmd(dec, RUVD_CMD_DPB_BUFFER, dec->dpb.res->buf, 0,
1004bf215546Sopenharmony_ci			RADEON_USAGE_READWRITE, RADEON_DOMAIN_VRAM);
1005bf215546Sopenharmony_ci
1006bf215546Sopenharmony_ci	if (dec->ctx.res)
1007bf215546Sopenharmony_ci		send_cmd(dec, RUVD_CMD_CONTEXT_BUFFER, dec->ctx.res->buf, 0,
1008bf215546Sopenharmony_ci			RADEON_USAGE_READWRITE, RADEON_DOMAIN_VRAM);
1009bf215546Sopenharmony_ci	send_cmd(dec, RUVD_CMD_BITSTREAM_BUFFER, bs_buf->res->buf,
1010bf215546Sopenharmony_ci		 0, RADEON_USAGE_READ, RADEON_DOMAIN_GTT);
1011bf215546Sopenharmony_ci	send_cmd(dec, RUVD_CMD_DECODING_TARGET_BUFFER, dt, 0,
1012bf215546Sopenharmony_ci		 RADEON_USAGE_WRITE, RADEON_DOMAIN_VRAM);
1013bf215546Sopenharmony_ci	send_cmd(dec, RUVD_CMD_FEEDBACK_BUFFER, msg_fb_it_buf->res->buf,
1014bf215546Sopenharmony_ci		 FB_BUFFER_OFFSET, RADEON_USAGE_WRITE, RADEON_DOMAIN_GTT);
1015bf215546Sopenharmony_ci	if (have_it(dec))
1016bf215546Sopenharmony_ci		send_cmd(dec, RUVD_CMD_ITSCALING_TABLE_BUFFER, msg_fb_it_buf->res->buf,
1017bf215546Sopenharmony_ci			 FB_BUFFER_OFFSET + dec->fb_size, RADEON_USAGE_READ, RADEON_DOMAIN_GTT);
1018bf215546Sopenharmony_ci	set_reg(dec, dec->reg.cntl, 1);
1019bf215546Sopenharmony_ci
1020bf215546Sopenharmony_ci	flush(dec, PIPE_FLUSH_ASYNC);
1021bf215546Sopenharmony_ci	next_buffer(dec);
1022bf215546Sopenharmony_ci}
1023bf215546Sopenharmony_ci
1024bf215546Sopenharmony_ci/**
1025bf215546Sopenharmony_ci * flush any outstanding command buffers to the hardware
1026bf215546Sopenharmony_ci */
1027bf215546Sopenharmony_cistatic void ruvd_flush(struct pipe_video_codec *decoder)
1028bf215546Sopenharmony_ci{
1029bf215546Sopenharmony_ci}
1030bf215546Sopenharmony_ci
1031bf215546Sopenharmony_ci/**
1032bf215546Sopenharmony_ci * create and UVD decoder
1033bf215546Sopenharmony_ci */
1034bf215546Sopenharmony_cistruct pipe_video_codec *ruvd_create_decoder(struct pipe_context *context,
1035bf215546Sopenharmony_ci					     const struct pipe_video_codec *templ,
1036bf215546Sopenharmony_ci					     ruvd_set_dtb set_dtb)
1037bf215546Sopenharmony_ci{
1038bf215546Sopenharmony_ci	struct radeon_winsys* ws = ((struct r600_common_context *)context)->ws;
1039bf215546Sopenharmony_ci	struct r600_common_context *rctx = (struct r600_common_context*)context;
1040bf215546Sopenharmony_ci	unsigned dpb_size;
1041bf215546Sopenharmony_ci	unsigned width = templ->width, height = templ->height;
1042bf215546Sopenharmony_ci	unsigned bs_buf_size;
1043bf215546Sopenharmony_ci	struct radeon_info info;
1044bf215546Sopenharmony_ci	struct ruvd_decoder *dec;
1045bf215546Sopenharmony_ci	int r, i;
1046bf215546Sopenharmony_ci
1047bf215546Sopenharmony_ci	ws->query_info(ws, &info, false, false);
1048bf215546Sopenharmony_ci
1049bf215546Sopenharmony_ci	switch(u_reduce_video_profile(templ->profile)) {
1050bf215546Sopenharmony_ci	case PIPE_VIDEO_FORMAT_MPEG12:
1051bf215546Sopenharmony_ci		if (templ->entrypoint > PIPE_VIDEO_ENTRYPOINT_BITSTREAM || info.family < CHIP_PALM)
1052bf215546Sopenharmony_ci			return vl_create_mpeg12_decoder(context, templ);
1053bf215546Sopenharmony_ci
1054bf215546Sopenharmony_ci		FALLTHROUGH;
1055bf215546Sopenharmony_ci	case PIPE_VIDEO_FORMAT_MPEG4:
1056bf215546Sopenharmony_ci		width = align(width, VL_MACROBLOCK_WIDTH);
1057bf215546Sopenharmony_ci		height = align(height, VL_MACROBLOCK_HEIGHT);
1058bf215546Sopenharmony_ci		break;
1059bf215546Sopenharmony_ci	case PIPE_VIDEO_FORMAT_MPEG4_AVC:
1060bf215546Sopenharmony_ci		width = align(width, VL_MACROBLOCK_WIDTH);
1061bf215546Sopenharmony_ci		height = align(height, VL_MACROBLOCK_HEIGHT);
1062bf215546Sopenharmony_ci		break;
1063bf215546Sopenharmony_ci
1064bf215546Sopenharmony_ci	default:
1065bf215546Sopenharmony_ci		break;
1066bf215546Sopenharmony_ci	}
1067bf215546Sopenharmony_ci
1068bf215546Sopenharmony_ci
1069bf215546Sopenharmony_ci	dec = CALLOC_STRUCT(ruvd_decoder);
1070bf215546Sopenharmony_ci
1071bf215546Sopenharmony_ci	if (!dec)
1072bf215546Sopenharmony_ci		return NULL;
1073bf215546Sopenharmony_ci
1074bf215546Sopenharmony_ci	dec->use_legacy = true;
1075bf215546Sopenharmony_ci
1076bf215546Sopenharmony_ci	dec->base = *templ;
1077bf215546Sopenharmony_ci	dec->base.context = context;
1078bf215546Sopenharmony_ci	dec->base.width = width;
1079bf215546Sopenharmony_ci	dec->base.height = height;
1080bf215546Sopenharmony_ci
1081bf215546Sopenharmony_ci	dec->base.destroy = ruvd_destroy;
1082bf215546Sopenharmony_ci	dec->base.begin_frame = ruvd_begin_frame;
1083bf215546Sopenharmony_ci	dec->base.decode_macroblock = ruvd_decode_macroblock;
1084bf215546Sopenharmony_ci	dec->base.decode_bitstream = ruvd_decode_bitstream;
1085bf215546Sopenharmony_ci	dec->base.end_frame = ruvd_end_frame;
1086bf215546Sopenharmony_ci	dec->base.flush = ruvd_flush;
1087bf215546Sopenharmony_ci
1088bf215546Sopenharmony_ci	dec->stream_type = profile2stream_type(dec, info.family);
1089bf215546Sopenharmony_ci	dec->set_dtb = set_dtb;
1090bf215546Sopenharmony_ci	dec->stream_handle = rvid_alloc_stream_handle();
1091bf215546Sopenharmony_ci	dec->screen = context->screen;
1092bf215546Sopenharmony_ci	dec->ws = ws;
1093bf215546Sopenharmony_ci
1094bf215546Sopenharmony_ci	if (!ws->cs_create(&dec->cs, rctx->ctx, AMD_IP_UVD, NULL, NULL, false)) {
1095bf215546Sopenharmony_ci		RVID_ERR("Can't get command submission context.\n");
1096bf215546Sopenharmony_ci		goto error;
1097bf215546Sopenharmony_ci	}
1098bf215546Sopenharmony_ci
1099bf215546Sopenharmony_ci	dec->fb_size = FB_BUFFER_SIZE;
1100bf215546Sopenharmony_ci	bs_buf_size = width * height * (512 / (16 * 16));
1101bf215546Sopenharmony_ci	for (i = 0; i < NUM_BUFFERS; ++i) {
1102bf215546Sopenharmony_ci		unsigned msg_fb_it_size = FB_BUFFER_OFFSET + dec->fb_size;
1103bf215546Sopenharmony_ci		STATIC_ASSERT(sizeof(struct ruvd_msg) <= FB_BUFFER_OFFSET);
1104bf215546Sopenharmony_ci		if (have_it(dec))
1105bf215546Sopenharmony_ci			msg_fb_it_size += IT_SCALING_TABLE_SIZE;
1106bf215546Sopenharmony_ci		if (!rvid_create_buffer(dec->screen, &dec->msg_fb_it_buffers[i],
1107bf215546Sopenharmony_ci					msg_fb_it_size, PIPE_USAGE_STAGING)) {
1108bf215546Sopenharmony_ci			RVID_ERR("Can't allocated message buffers.\n");
1109bf215546Sopenharmony_ci			goto error;
1110bf215546Sopenharmony_ci		}
1111bf215546Sopenharmony_ci
1112bf215546Sopenharmony_ci		if (!rvid_create_buffer(dec->screen, &dec->bs_buffers[i],
1113bf215546Sopenharmony_ci					bs_buf_size, PIPE_USAGE_STAGING)) {
1114bf215546Sopenharmony_ci			RVID_ERR("Can't allocated bitstream buffers.\n");
1115bf215546Sopenharmony_ci			goto error;
1116bf215546Sopenharmony_ci		}
1117bf215546Sopenharmony_ci
1118bf215546Sopenharmony_ci		rvid_clear_buffer(context, &dec->msg_fb_it_buffers[i]);
1119bf215546Sopenharmony_ci		rvid_clear_buffer(context, &dec->bs_buffers[i]);
1120bf215546Sopenharmony_ci	}
1121bf215546Sopenharmony_ci
1122bf215546Sopenharmony_ci	dpb_size = calc_dpb_size(dec);
1123bf215546Sopenharmony_ci	if (dpb_size) {
1124bf215546Sopenharmony_ci		if (!rvid_create_buffer(dec->screen, &dec->dpb, dpb_size, PIPE_USAGE_DEFAULT)) {
1125bf215546Sopenharmony_ci			RVID_ERR("Can't allocated dpb.\n");
1126bf215546Sopenharmony_ci			goto error;
1127bf215546Sopenharmony_ci		}
1128bf215546Sopenharmony_ci		rvid_clear_buffer(context, &dec->dpb);
1129bf215546Sopenharmony_ci	}
1130bf215546Sopenharmony_ci
1131bf215546Sopenharmony_ci	dec->reg.data0 = RUVD_GPCOM_VCPU_DATA0;
1132bf215546Sopenharmony_ci	dec->reg.data1 = RUVD_GPCOM_VCPU_DATA1;
1133bf215546Sopenharmony_ci	dec->reg.cmd = RUVD_GPCOM_VCPU_CMD;
1134bf215546Sopenharmony_ci	dec->reg.cntl = RUVD_ENGINE_CNTL;
1135bf215546Sopenharmony_ci
1136bf215546Sopenharmony_ci	map_msg_fb_it_buf(dec);
1137bf215546Sopenharmony_ci	dec->msg->size = sizeof(*dec->msg);
1138bf215546Sopenharmony_ci	dec->msg->msg_type = RUVD_MSG_CREATE;
1139bf215546Sopenharmony_ci	dec->msg->stream_handle = dec->stream_handle;
1140bf215546Sopenharmony_ci	dec->msg->body.create.stream_type = dec->stream_type;
1141bf215546Sopenharmony_ci	dec->msg->body.create.width_in_samples = dec->base.width;
1142bf215546Sopenharmony_ci	dec->msg->body.create.height_in_samples = dec->base.height;
1143bf215546Sopenharmony_ci	dec->msg->body.create.dpb_size = dpb_size;
1144bf215546Sopenharmony_ci	send_msg_buf(dec);
1145bf215546Sopenharmony_ci	r = flush(dec, 0);
1146bf215546Sopenharmony_ci	if (r)
1147bf215546Sopenharmony_ci		goto error;
1148bf215546Sopenharmony_ci
1149bf215546Sopenharmony_ci	next_buffer(dec);
1150bf215546Sopenharmony_ci
1151bf215546Sopenharmony_ci	return &dec->base;
1152bf215546Sopenharmony_ci
1153bf215546Sopenharmony_cierror:
1154bf215546Sopenharmony_ci	dec->ws->cs_destroy(&dec->cs);
1155bf215546Sopenharmony_ci
1156bf215546Sopenharmony_ci	for (i = 0; i < NUM_BUFFERS; ++i) {
1157bf215546Sopenharmony_ci		rvid_destroy_buffer(&dec->msg_fb_it_buffers[i]);
1158bf215546Sopenharmony_ci		rvid_destroy_buffer(&dec->bs_buffers[i]);
1159bf215546Sopenharmony_ci	}
1160bf215546Sopenharmony_ci
1161bf215546Sopenharmony_ci	rvid_destroy_buffer(&dec->dpb);
1162bf215546Sopenharmony_ci	rvid_destroy_buffer(&dec->ctx);
1163bf215546Sopenharmony_ci	rvid_destroy_buffer(&dec->sessionctx);
1164bf215546Sopenharmony_ci
1165bf215546Sopenharmony_ci	FREE(dec);
1166bf215546Sopenharmony_ci
1167bf215546Sopenharmony_ci	return NULL;
1168bf215546Sopenharmony_ci}
1169bf215546Sopenharmony_ci
1170bf215546Sopenharmony_ci/* calculate top/bottom offset */
1171bf215546Sopenharmony_cistatic unsigned texture_offset(struct radeon_surf *surface, unsigned layer)
1172bf215546Sopenharmony_ci{
1173bf215546Sopenharmony_ci	return (uint64_t)surface->u.legacy.level[0].offset_256B * 256 +
1174bf215546Sopenharmony_ci		layer * (uint64_t)surface->u.legacy.level[0].slice_size_dw * 4;
1175bf215546Sopenharmony_ci}
1176bf215546Sopenharmony_ci
1177bf215546Sopenharmony_ci/* hw encode the aspect of macro tiles */
1178bf215546Sopenharmony_cistatic unsigned macro_tile_aspect(unsigned macro_tile_aspect)
1179bf215546Sopenharmony_ci{
1180bf215546Sopenharmony_ci	switch (macro_tile_aspect) {
1181bf215546Sopenharmony_ci	default:
1182bf215546Sopenharmony_ci	case 1: macro_tile_aspect = 0;  break;
1183bf215546Sopenharmony_ci	case 2: macro_tile_aspect = 1;  break;
1184bf215546Sopenharmony_ci	case 4: macro_tile_aspect = 2;  break;
1185bf215546Sopenharmony_ci	case 8: macro_tile_aspect = 3;  break;
1186bf215546Sopenharmony_ci	}
1187bf215546Sopenharmony_ci	return macro_tile_aspect;
1188bf215546Sopenharmony_ci}
1189bf215546Sopenharmony_ci
1190bf215546Sopenharmony_ci/* hw encode the bank width and height */
1191bf215546Sopenharmony_cistatic unsigned bank_wh(unsigned bankwh)
1192bf215546Sopenharmony_ci{
1193bf215546Sopenharmony_ci	switch (bankwh) {
1194bf215546Sopenharmony_ci	default:
1195bf215546Sopenharmony_ci	case 1: bankwh = 0;     break;
1196bf215546Sopenharmony_ci	case 2: bankwh = 1;     break;
1197bf215546Sopenharmony_ci	case 4: bankwh = 2;     break;
1198bf215546Sopenharmony_ci	case 8: bankwh = 3;     break;
1199bf215546Sopenharmony_ci	}
1200bf215546Sopenharmony_ci	return bankwh;
1201bf215546Sopenharmony_ci}
1202bf215546Sopenharmony_ci
1203bf215546Sopenharmony_ci/**
1204bf215546Sopenharmony_ci * fill decoding target field from the luma and chroma surfaces
1205bf215546Sopenharmony_ci */
1206bf215546Sopenharmony_civoid ruvd_set_dt_surfaces(struct ruvd_msg *msg, struct radeon_surf *luma,
1207bf215546Sopenharmony_ci			  struct radeon_surf *chroma)
1208bf215546Sopenharmony_ci{
1209bf215546Sopenharmony_ci	msg->body.decode.dt_pitch = luma->u.legacy.level[0].nblk_x * luma->blk_w;
1210bf215546Sopenharmony_ci	switch (luma->u.legacy.level[0].mode) {
1211bf215546Sopenharmony_ci	case RADEON_SURF_MODE_LINEAR_ALIGNED:
1212bf215546Sopenharmony_ci		msg->body.decode.dt_tiling_mode = RUVD_TILE_LINEAR;
1213bf215546Sopenharmony_ci		msg->body.decode.dt_array_mode = RUVD_ARRAY_MODE_LINEAR;
1214bf215546Sopenharmony_ci		break;
1215bf215546Sopenharmony_ci	case RADEON_SURF_MODE_1D:
1216bf215546Sopenharmony_ci		msg->body.decode.dt_tiling_mode = RUVD_TILE_8X8;
1217bf215546Sopenharmony_ci		msg->body.decode.dt_array_mode = RUVD_ARRAY_MODE_1D_THIN;
1218bf215546Sopenharmony_ci		break;
1219bf215546Sopenharmony_ci	case RADEON_SURF_MODE_2D:
1220bf215546Sopenharmony_ci		msg->body.decode.dt_tiling_mode = RUVD_TILE_8X8;
1221bf215546Sopenharmony_ci		msg->body.decode.dt_array_mode = RUVD_ARRAY_MODE_2D_THIN;
1222bf215546Sopenharmony_ci		break;
1223bf215546Sopenharmony_ci	default:
1224bf215546Sopenharmony_ci		assert(0);
1225bf215546Sopenharmony_ci		break;
1226bf215546Sopenharmony_ci	}
1227bf215546Sopenharmony_ci
1228bf215546Sopenharmony_ci	msg->body.decode.dt_luma_top_offset = texture_offset(luma, 0);
1229bf215546Sopenharmony_ci	if (chroma)
1230bf215546Sopenharmony_ci		msg->body.decode.dt_chroma_top_offset = texture_offset(chroma, 0);
1231bf215546Sopenharmony_ci	if (msg->body.decode.dt_field_mode) {
1232bf215546Sopenharmony_ci		msg->body.decode.dt_luma_bottom_offset = texture_offset(luma, 1);
1233bf215546Sopenharmony_ci		if (chroma)
1234bf215546Sopenharmony_ci			msg->body.decode.dt_chroma_bottom_offset = texture_offset(chroma, 1);
1235bf215546Sopenharmony_ci	} else {
1236bf215546Sopenharmony_ci		msg->body.decode.dt_luma_bottom_offset = msg->body.decode.dt_luma_top_offset;
1237bf215546Sopenharmony_ci		msg->body.decode.dt_chroma_bottom_offset = msg->body.decode.dt_chroma_top_offset;
1238bf215546Sopenharmony_ci	}
1239bf215546Sopenharmony_ci
1240bf215546Sopenharmony_ci	if (chroma) {
1241bf215546Sopenharmony_ci		assert(luma->u.legacy.bankw == chroma->u.legacy.bankw);
1242bf215546Sopenharmony_ci		assert(luma->u.legacy.bankh == chroma->u.legacy.bankh);
1243bf215546Sopenharmony_ci		assert(luma->u.legacy.mtilea == chroma->u.legacy.mtilea);
1244bf215546Sopenharmony_ci	}
1245bf215546Sopenharmony_ci
1246bf215546Sopenharmony_ci	msg->body.decode.dt_surf_tile_config |= RUVD_BANK_WIDTH(bank_wh(luma->u.legacy.bankw));
1247bf215546Sopenharmony_ci	msg->body.decode.dt_surf_tile_config |= RUVD_BANK_HEIGHT(bank_wh(luma->u.legacy.bankh));
1248bf215546Sopenharmony_ci	msg->body.decode.dt_surf_tile_config |= RUVD_MACRO_TILE_ASPECT_RATIO(macro_tile_aspect(luma->u.legacy.mtilea));
1249bf215546Sopenharmony_ci}
1250