1848b8605Smrg/**************************************************************************
2848b8605Smrg *
3848b8605Smrg * Copyright 2011 Advanced Micro Devices, Inc.
4848b8605Smrg * All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the
8848b8605Smrg * "Software"), to deal in the Software without restriction, including
9848b8605Smrg * without limitation the rights to use, copy, modify, merge, publish,
10848b8605Smrg * distribute, sub license, and/or sell copies of the Software, and to
11848b8605Smrg * permit persons to whom the Software is furnished to do so, subject to
12848b8605Smrg * the following conditions:
13848b8605Smrg *
14848b8605Smrg * The above copyright notice and this permission notice (including the
15848b8605Smrg * next paragraph) shall be included in all copies or substantial portions
16848b8605Smrg * of the Software.
17848b8605Smrg *
18848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20848b8605Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21848b8605Smrg * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22848b8605Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23848b8605Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24848b8605Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25848b8605Smrg *
26848b8605Smrg **************************************************************************/
27848b8605Smrg
28848b8605Smrg#include "si_pipe.h"
29848b8605Smrg#include "radeon/radeon_video.h"
30848b8605Smrg#include "radeon/radeon_uvd.h"
31848b8605Smrg#include "radeon/radeon_vce.h"
32b8e80941Smrg#include "radeon/radeon_vcn_dec.h"
33b8e80941Smrg#include "radeon/radeon_vcn_enc.h"
34b8e80941Smrg#include "radeon/radeon_uvd_enc.h"
35b8e80941Smrg#include "util/u_video.h"
36848b8605Smrg
37848b8605Smrg/**
38848b8605Smrg * creates an video buffer with an UVD compatible memory layout
39848b8605Smrg */
40848b8605Smrgstruct pipe_video_buffer *si_video_buffer_create(struct pipe_context *pipe,
41848b8605Smrg						 const struct pipe_video_buffer *tmpl)
42848b8605Smrg{
43848b8605Smrg	struct si_context *ctx = (struct si_context *)pipe;
44b8e80941Smrg	struct si_texture *resources[VL_NUM_COMPONENTS] = {};
45b8e80941Smrg	struct radeon_surf *surfaces[VL_NUM_COMPONENTS] = {};
46848b8605Smrg	struct pb_buffer **pbs[VL_NUM_COMPONENTS] = {};
47848b8605Smrg	const enum pipe_format *resource_formats;
48b8e80941Smrg	struct pipe_video_buffer vidtemplate;
49848b8605Smrg	struct pipe_resource templ;
50848b8605Smrg	unsigned i, array_size;
51848b8605Smrg
52848b8605Smrg	assert(pipe);
53848b8605Smrg
54848b8605Smrg	/* first create the needed resources as "normal" textures */
55848b8605Smrg	resource_formats = vl_video_buffer_formats(pipe->screen, tmpl->buffer_format);
56848b8605Smrg	if (!resource_formats)
57848b8605Smrg		return NULL;
58848b8605Smrg
59848b8605Smrg	array_size = tmpl->interlaced ? 2 : 1;
60b8e80941Smrg	vidtemplate = *tmpl;
61b8e80941Smrg	vidtemplate.width = align(tmpl->width, VL_MACROBLOCK_WIDTH);
62b8e80941Smrg	vidtemplate.height = align(tmpl->height / array_size, VL_MACROBLOCK_HEIGHT);
63848b8605Smrg
64b8e80941Smrg	assert(resource_formats[0] != PIPE_FORMAT_NONE);
65b8e80941Smrg
66b8e80941Smrg	for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
67b8e80941Smrg		if (resource_formats[i] != PIPE_FORMAT_NONE) {
68b8e80941Smrg			vl_video_buffer_template(&templ, &vidtemplate,
69b8e80941Smrg			                         resource_formats[i], 1,
70b8e80941Smrg			                         array_size, PIPE_USAGE_DEFAULT, i);
71b8e80941Smrg			/* Set PIPE_BIND_SHARED to avoid reallocation in si_texture_get_handle,
72b8e80941Smrg			 * which can't handle joined surfaces. */
73b8e80941Smrg			/* TODO: get tiling working */
74b8e80941Smrg			templ.bind = PIPE_BIND_LINEAR | PIPE_BIND_SHARED;
75b8e80941Smrg			resources[i] = (struct si_texture *)
76b8e80941Smrg			                pipe->screen->resource_create(pipe->screen, &templ);
77b8e80941Smrg			if (!resources[i])
78b8e80941Smrg				goto error;
79b8e80941Smrg		}
80848b8605Smrg	}
81848b8605Smrg
82848b8605Smrg	for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
83848b8605Smrg		if (!resources[i])
84848b8605Smrg			continue;
85848b8605Smrg
86848b8605Smrg		surfaces[i] = & resources[i]->surface;
87b8e80941Smrg		pbs[i] = &resources[i]->buffer.buf;
88848b8605Smrg	}
89848b8605Smrg
90b8e80941Smrg	si_vid_join_surfaces(ctx, pbs, surfaces);
91848b8605Smrg
92848b8605Smrg	for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
93848b8605Smrg		if (!resources[i])
94848b8605Smrg			continue;
95848b8605Smrg
96b8e80941Smrg		/* reset the address */
97b8e80941Smrg		resources[i]->buffer.gpu_address = ctx->ws->buffer_get_virtual_address(
98b8e80941Smrg			resources[i]->buffer.buf);
99848b8605Smrg	}
100848b8605Smrg
101b8e80941Smrg	vidtemplate.height *= array_size;
102b8e80941Smrg	return vl_video_buffer_create_ex2(pipe, &vidtemplate, (struct pipe_resource **)resources);
103848b8605Smrg
104848b8605Smrgerror:
105848b8605Smrg	for (i = 0; i < VL_NUM_COMPONENTS; ++i)
106b8e80941Smrg		si_texture_reference(&resources[i], NULL);
107848b8605Smrg
108848b8605Smrg	return NULL;
109848b8605Smrg}
110848b8605Smrg
111848b8605Smrg/* set the decoding target buffer offsets */
112b8e80941Smrgstatic struct pb_buffer* si_uvd_set_dtb(struct ruvd_msg *msg, struct vl_video_buffer *buf)
113848b8605Smrg{
114b8e80941Smrg	struct si_screen *sscreen = (struct si_screen*)buf->base.context->screen;
115b8e80941Smrg	struct si_texture *luma = (struct si_texture *)buf->resources[0];
116b8e80941Smrg	struct si_texture *chroma = (struct si_texture *)buf->resources[1];
117b8e80941Smrg	enum ruvd_surface_type type =  (sscreen->info.chip_class >= GFX9) ?
118b8e80941Smrg					RUVD_SURFACE_TYPE_GFX9 :
119b8e80941Smrg					RUVD_SURFACE_TYPE_LEGACY;
120848b8605Smrg
121848b8605Smrg	msg->body.decode.dt_field_mode = buf->base.interlaced;
122848b8605Smrg
123b8e80941Smrg	si_uvd_set_dt_surfaces(msg, &luma->surface, (chroma) ? &chroma->surface : NULL, type);
124848b8605Smrg
125b8e80941Smrg	return luma->buffer.buf;
126848b8605Smrg}
127848b8605Smrg
128848b8605Smrg/* get the radeon resources for VCE */
129848b8605Smrgstatic void si_vce_get_buffer(struct pipe_resource *resource,
130b8e80941Smrg			      struct pb_buffer **handle,
131b8e80941Smrg			      struct radeon_surf **surface)
132848b8605Smrg{
133b8e80941Smrg	struct si_texture *res = (struct si_texture *)resource;
134848b8605Smrg
135848b8605Smrg	if (handle)
136b8e80941Smrg		*handle = res->buffer.buf;
137848b8605Smrg
138848b8605Smrg	if (surface)
139848b8605Smrg		*surface = &res->surface;
140848b8605Smrg}
141848b8605Smrg
142848b8605Smrg/**
143848b8605Smrg * creates an UVD compatible decoder
144848b8605Smrg */
145848b8605Smrgstruct pipe_video_codec *si_uvd_create_decoder(struct pipe_context *context,
146848b8605Smrg					       const struct pipe_video_codec *templ)
147848b8605Smrg{
148848b8605Smrg	struct si_context *ctx = (struct si_context *)context;
149b8e80941Smrg	bool vcn = ctx->family == CHIP_RAVEN ||
150b8e80941Smrg		   ctx->family == CHIP_RAVEN2;
151b8e80941Smrg
152b8e80941Smrg	if (templ->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
153b8e80941Smrg		if (vcn) {
154b8e80941Smrg			return radeon_create_encoder(context, templ, ctx->ws, si_vce_get_buffer);
155b8e80941Smrg		} else {
156b8e80941Smrg			if (u_reduce_video_profile(templ->profile) == PIPE_VIDEO_FORMAT_HEVC)
157b8e80941Smrg				return radeon_uvd_create_encoder(context, templ, ctx->ws, si_vce_get_buffer);
158b8e80941Smrg			else
159b8e80941Smrg				return si_vce_create_encoder(context, templ, ctx->ws, si_vce_get_buffer);
160b8e80941Smrg		}
161b8e80941Smrg	}
162848b8605Smrg
163b8e80941Smrg	return (vcn) ? 	radeon_create_decoder(context, templ) :
164b8e80941Smrg		si_common_uvd_create_decoder(context, templ, si_uvd_set_dtb);
165848b8605Smrg}
166