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#include "drm-uapi/drm_fourcc.h"
29bf215546Sopenharmony_ci#include "radeon_uvd.h"
30bf215546Sopenharmony_ci#include "radeon_uvd_enc.h"
31bf215546Sopenharmony_ci#include "radeon_vce.h"
32bf215546Sopenharmony_ci#include "radeon_vcn_dec.h"
33bf215546Sopenharmony_ci#include "radeon_vcn_enc.h"
34bf215546Sopenharmony_ci#include "radeon_video.h"
35bf215546Sopenharmony_ci#include "si_pipe.h"
36bf215546Sopenharmony_ci#include "util/u_video.h"
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci/**
39bf215546Sopenharmony_ci * creates an video buffer with an UVD compatible memory layout
40bf215546Sopenharmony_ci */
41bf215546Sopenharmony_cistruct pipe_video_buffer *si_video_buffer_create(struct pipe_context *pipe,
42bf215546Sopenharmony_ci                                                 const struct pipe_video_buffer *tmpl)
43bf215546Sopenharmony_ci{
44bf215546Sopenharmony_ci   struct pipe_video_buffer vidbuf = *tmpl;
45bf215546Sopenharmony_ci   uint64_t *modifiers = NULL;
46bf215546Sopenharmony_ci   int modifiers_count = 0;
47bf215546Sopenharmony_ci   uint64_t mod = DRM_FORMAT_MOD_LINEAR;
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   /* To get tiled buffers, users need to explicitly provide a list of
50bf215546Sopenharmony_ci    * modifiers. */
51bf215546Sopenharmony_ci   vidbuf.bind |= PIPE_BIND_LINEAR;
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci   if (pipe->screen->resource_create_with_modifiers) {
54bf215546Sopenharmony_ci      modifiers = &mod;
55bf215546Sopenharmony_ci      modifiers_count = 1;
56bf215546Sopenharmony_ci   }
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci   return vl_video_buffer_create_as_resource(pipe, &vidbuf, modifiers,
59bf215546Sopenharmony_ci                                             modifiers_count);
60bf215546Sopenharmony_ci}
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_cistruct pipe_video_buffer *si_video_buffer_create_with_modifiers(struct pipe_context *pipe,
63bf215546Sopenharmony_ci                                                                const struct pipe_video_buffer *tmpl,
64bf215546Sopenharmony_ci                                                                const uint64_t *modifiers,
65bf215546Sopenharmony_ci                                                                unsigned int modifiers_count)
66bf215546Sopenharmony_ci{
67bf215546Sopenharmony_ci   uint64_t *allowed_modifiers;
68bf215546Sopenharmony_ci   unsigned int allowed_modifiers_count, i;
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci   /* Filter out DCC modifiers, because we don't support them for video
71bf215546Sopenharmony_ci    * for now. */
72bf215546Sopenharmony_ci   allowed_modifiers = calloc(modifiers_count, sizeof(uint64_t));
73bf215546Sopenharmony_ci   if (!allowed_modifiers)
74bf215546Sopenharmony_ci      return NULL;
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci   allowed_modifiers_count = 0;
77bf215546Sopenharmony_ci   for (i = 0; i < modifiers_count; i++) {
78bf215546Sopenharmony_ci      if (ac_modifier_has_dcc(modifiers[i]))
79bf215546Sopenharmony_ci         continue;
80bf215546Sopenharmony_ci      allowed_modifiers[allowed_modifiers_count++] = modifiers[i];
81bf215546Sopenharmony_ci   }
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci   struct pipe_video_buffer *buf =
84bf215546Sopenharmony_ci      vl_video_buffer_create_as_resource(pipe, tmpl, allowed_modifiers, allowed_modifiers_count);
85bf215546Sopenharmony_ci   free(allowed_modifiers);
86bf215546Sopenharmony_ci   return buf;
87bf215546Sopenharmony_ci}
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci/* set the decoding target buffer offsets */
90bf215546Sopenharmony_cistatic struct pb_buffer *si_uvd_set_dtb(struct ruvd_msg *msg, struct vl_video_buffer *buf)
91bf215546Sopenharmony_ci{
92bf215546Sopenharmony_ci   struct si_screen *sscreen = (struct si_screen *)buf->base.context->screen;
93bf215546Sopenharmony_ci   struct si_texture *luma = (struct si_texture *)buf->resources[0];
94bf215546Sopenharmony_ci   struct si_texture *chroma = (struct si_texture *)buf->resources[1];
95bf215546Sopenharmony_ci   enum ruvd_surface_type type =
96bf215546Sopenharmony_ci      (sscreen->info.gfx_level >= GFX9) ? RUVD_SURFACE_TYPE_GFX9 : RUVD_SURFACE_TYPE_LEGACY;
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci   msg->body.decode.dt_field_mode = buf->base.interlaced;
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci   si_uvd_set_dt_surfaces(msg, &luma->surface, (chroma) ? &chroma->surface : NULL, type);
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci   return luma->buffer.buf;
103bf215546Sopenharmony_ci}
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci/* get the radeon resources for VCE */
106bf215546Sopenharmony_cistatic void si_vce_get_buffer(struct pipe_resource *resource, struct pb_buffer **handle,
107bf215546Sopenharmony_ci                              struct radeon_surf **surface)
108bf215546Sopenharmony_ci{
109bf215546Sopenharmony_ci   struct si_texture *res = (struct si_texture *)resource;
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci   if (handle)
112bf215546Sopenharmony_ci      *handle = res->buffer.buf;
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci   if (surface)
115bf215546Sopenharmony_ci      *surface = &res->surface;
116bf215546Sopenharmony_ci}
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci/**
119bf215546Sopenharmony_ci * creates an UVD compatible decoder
120bf215546Sopenharmony_ci */
121bf215546Sopenharmony_cistruct pipe_video_codec *si_uvd_create_decoder(struct pipe_context *context,
122bf215546Sopenharmony_ci                                               const struct pipe_video_codec *templ)
123bf215546Sopenharmony_ci{
124bf215546Sopenharmony_ci   struct si_context *ctx = (struct si_context *)context;
125bf215546Sopenharmony_ci   bool vcn = ctx->family >= CHIP_RAVEN;
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci   if (templ->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
128bf215546Sopenharmony_ci      if (vcn) {
129bf215546Sopenharmony_ci         return radeon_create_encoder(context, templ, ctx->ws, si_vce_get_buffer);
130bf215546Sopenharmony_ci      } else {
131bf215546Sopenharmony_ci         if (u_reduce_video_profile(templ->profile) == PIPE_VIDEO_FORMAT_HEVC)
132bf215546Sopenharmony_ci            return radeon_uvd_create_encoder(context, templ, ctx->ws, si_vce_get_buffer);
133bf215546Sopenharmony_ci         else
134bf215546Sopenharmony_ci            return si_vce_create_encoder(context, templ, ctx->ws, si_vce_get_buffer);
135bf215546Sopenharmony_ci      }
136bf215546Sopenharmony_ci   }
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci   return (vcn) ? radeon_create_decoder(context, templ)
139bf215546Sopenharmony_ci                : si_common_uvd_create_decoder(context, templ, si_uvd_set_dtb);
140bf215546Sopenharmony_ci}
141