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
39bf215546Sopenharmony_ci#include "pipe/p_video_codec.h"
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci#include "util/u_memory.h"
42bf215546Sopenharmony_ci#include "util/u_video.h"
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci#include "vl/vl_defines.h"
45bf215546Sopenharmony_ci#include "vl/vl_mpeg12_decoder.h"
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci#include "r600_pipe.h"
48bf215546Sopenharmony_ci#include "radeon_video.h"
49bf215546Sopenharmony_ci#include "radeon_uvd.h"
50bf215546Sopenharmony_ci#include "radeon_vce.h"
51bf215546Sopenharmony_ci#include "r600d.h"
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci#define R600_UVD_ENABLE_TILING 0
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci/**
56bf215546Sopenharmony_ci * creates an video buffer with an UVD compatible memory layout
57bf215546Sopenharmony_ci */
58bf215546Sopenharmony_cistruct pipe_video_buffer *r600_video_buffer_create(struct pipe_context *pipe,
59bf215546Sopenharmony_ci						   const struct pipe_video_buffer *tmpl)
60bf215546Sopenharmony_ci{
61bf215546Sopenharmony_ci	struct r600_context *ctx = (struct r600_context *)pipe;
62bf215546Sopenharmony_ci	struct r600_texture *resources[VL_NUM_COMPONENTS] = {};
63bf215546Sopenharmony_ci	struct radeon_surf* surfaces[VL_NUM_COMPONENTS] = {};
64bf215546Sopenharmony_ci	struct pb_buffer **pbs[VL_NUM_COMPONENTS] = {};
65bf215546Sopenharmony_ci	enum pipe_format resource_formats[3];
66bf215546Sopenharmony_ci	struct pipe_video_buffer template;
67bf215546Sopenharmony_ci	struct pipe_resource templ;
68bf215546Sopenharmony_ci	unsigned i, array_size;
69bf215546Sopenharmony_ci	enum pipe_video_chroma_format chroma_format =
70bf215546Sopenharmony_ci		pipe_format_to_chroma_format(tmpl->buffer_format);
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci	assert(pipe);
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci	/* first create the needed resources as "normal" textures */
75bf215546Sopenharmony_ci	vl_get_video_buffer_formats(pipe->screen, tmpl->buffer_format, resource_formats);
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci	array_size = tmpl->interlaced ? 2 : 1;
78bf215546Sopenharmony_ci	template = *tmpl;
79bf215546Sopenharmony_ci	template.width = align(tmpl->width, VL_MACROBLOCK_WIDTH);
80bf215546Sopenharmony_ci	template.height = align(tmpl->height / array_size, VL_MACROBLOCK_HEIGHT);
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci	vl_video_buffer_template(&templ, &template, resource_formats[0], 1, array_size,
83bf215546Sopenharmony_ci									 PIPE_USAGE_DEFAULT, 0, chroma_format);
84bf215546Sopenharmony_ci	if (ctx->b.gfx_level < EVERGREEN || tmpl->interlaced || !R600_UVD_ENABLE_TILING)
85bf215546Sopenharmony_ci		templ.bind = PIPE_BIND_LINEAR;
86bf215546Sopenharmony_ci	resources[0] = (struct r600_texture *)
87bf215546Sopenharmony_ci		pipe->screen->resource_create(pipe->screen, &templ);
88bf215546Sopenharmony_ci	if (!resources[0])
89bf215546Sopenharmony_ci		goto error;
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci	if (resource_formats[1] != PIPE_FORMAT_NONE) {
92bf215546Sopenharmony_ci		vl_video_buffer_template(&templ, &template, resource_formats[1], 1, array_size,
93bf215546Sopenharmony_ci										 PIPE_USAGE_DEFAULT, 1, chroma_format);
94bf215546Sopenharmony_ci		if (ctx->b.gfx_level < EVERGREEN || tmpl->interlaced || !R600_UVD_ENABLE_TILING)
95bf215546Sopenharmony_ci			templ.bind = PIPE_BIND_LINEAR;
96bf215546Sopenharmony_ci		resources[1] = (struct r600_texture *)
97bf215546Sopenharmony_ci			pipe->screen->resource_create(pipe->screen, &templ);
98bf215546Sopenharmony_ci		if (!resources[1])
99bf215546Sopenharmony_ci			goto error;
100bf215546Sopenharmony_ci	}
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci	if (resource_formats[2] != PIPE_FORMAT_NONE) {
103bf215546Sopenharmony_ci		vl_video_buffer_template(&templ, &template, resource_formats[2], 1, array_size,
104bf215546Sopenharmony_ci										 PIPE_USAGE_DEFAULT, 2, chroma_format);
105bf215546Sopenharmony_ci		if (ctx->b.gfx_level < EVERGREEN || tmpl->interlaced || !R600_UVD_ENABLE_TILING)
106bf215546Sopenharmony_ci			templ.bind = PIPE_BIND_LINEAR;
107bf215546Sopenharmony_ci		resources[2] = (struct r600_texture *)
108bf215546Sopenharmony_ci			pipe->screen->resource_create(pipe->screen, &templ);
109bf215546Sopenharmony_ci		if (!resources[2])
110bf215546Sopenharmony_ci			goto error;
111bf215546Sopenharmony_ci	}
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci	for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
114bf215546Sopenharmony_ci		if (!resources[i])
115bf215546Sopenharmony_ci			continue;
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci		pbs[i] = &resources[i]->resource.buf;
118bf215546Sopenharmony_ci		surfaces[i] = &resources[i]->surface;
119bf215546Sopenharmony_ci	}
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci	rvid_join_surfaces(&ctx->b, pbs, surfaces);
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci	for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
124bf215546Sopenharmony_ci		if (!resources[i])
125bf215546Sopenharmony_ci			continue;
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci		/* reset the address */
128bf215546Sopenharmony_ci		resources[i]->resource.gpu_address = ctx->b.ws->buffer_get_virtual_address(
129bf215546Sopenharmony_ci			resources[i]->resource.buf);
130bf215546Sopenharmony_ci	}
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci	template.height *= array_size;
133bf215546Sopenharmony_ci	return vl_video_buffer_create_ex2(pipe, &template, (struct pipe_resource **)resources);
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_cierror:
136bf215546Sopenharmony_ci	for (i = 0; i < VL_NUM_COMPONENTS; ++i)
137bf215546Sopenharmony_ci		r600_texture_reference(&resources[i], NULL);
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ci	return NULL;
140bf215546Sopenharmony_ci}
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_ci/* hw encode the number of memory banks */
143bf215546Sopenharmony_cistatic uint32_t eg_num_banks(uint32_t nbanks)
144bf215546Sopenharmony_ci{
145bf215546Sopenharmony_ci	switch (nbanks) {
146bf215546Sopenharmony_ci	case 2:
147bf215546Sopenharmony_ci		return 0;
148bf215546Sopenharmony_ci	case 4:
149bf215546Sopenharmony_ci		return 1;
150bf215546Sopenharmony_ci	case 8:
151bf215546Sopenharmony_ci	default:
152bf215546Sopenharmony_ci		return 2;
153bf215546Sopenharmony_ci	case 16:
154bf215546Sopenharmony_ci		return 3;
155bf215546Sopenharmony_ci	}
156bf215546Sopenharmony_ci}
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_ci/* set the decoding target buffer offsets */
159bf215546Sopenharmony_cistatic struct pb_buffer* r600_uvd_set_dtb(struct ruvd_msg *msg, struct vl_video_buffer *buf)
160bf215546Sopenharmony_ci{
161bf215546Sopenharmony_ci	struct r600_screen *rscreen = (struct r600_screen*)buf->base.context->screen;
162bf215546Sopenharmony_ci	struct r600_texture *luma = (struct r600_texture *)buf->resources[0];
163bf215546Sopenharmony_ci	struct r600_texture *chroma = (struct r600_texture *)buf->resources[1];
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci	msg->body.decode.dt_field_mode = buf->base.interlaced;
166bf215546Sopenharmony_ci	msg->body.decode.dt_surf_tile_config |= RUVD_NUM_BANKS(eg_num_banks(rscreen->b.info.r600_num_banks));
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_ci	ruvd_set_dt_surfaces(msg, &luma->surface, &chroma->surface);
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci	return luma->resource.buf;
171bf215546Sopenharmony_ci}
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci/* get the radeon resources for VCE */
174bf215546Sopenharmony_cistatic void r600_vce_get_buffer(struct pipe_resource *resource,
175bf215546Sopenharmony_ci				struct pb_buffer **handle,
176bf215546Sopenharmony_ci				struct radeon_surf **surface)
177bf215546Sopenharmony_ci{
178bf215546Sopenharmony_ci	struct r600_texture *res = (struct r600_texture *)resource;
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_ci	if (handle)
181bf215546Sopenharmony_ci		*handle = res->resource.buf;
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_ci	if (surface)
184bf215546Sopenharmony_ci		*surface = &res->surface;
185bf215546Sopenharmony_ci}
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci/* create decoder */
188bf215546Sopenharmony_cistruct pipe_video_codec *r600_uvd_create_decoder(struct pipe_context *context,
189bf215546Sopenharmony_ci						 const struct pipe_video_codec *templat)
190bf215546Sopenharmony_ci{
191bf215546Sopenharmony_ci	struct r600_context *ctx = (struct r600_context *)context;
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_ci        if (templat->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE)
194bf215546Sopenharmony_ci                return rvce_create_encoder(context, templat, ctx->b.ws, r600_vce_get_buffer);
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ci	return ruvd_create_decoder(context, templat, r600_uvd_set_dtb);
197bf215546Sopenharmony_ci}
198