1/**************************************************************************
2 *
3 * Copyright 2011 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28#include "drm-uapi/drm_fourcc.h"
29#include "radeon_uvd.h"
30#include "radeon_uvd_enc.h"
31#include "radeon_vce.h"
32#include "radeon_vcn_dec.h"
33#include "radeon_vcn_enc.h"
34#include "radeon_video.h"
35#include "si_pipe.h"
36#include "util/u_video.h"
37
38/**
39 * creates an video buffer with an UVD compatible memory layout
40 */
41struct pipe_video_buffer *si_video_buffer_create(struct pipe_context *pipe,
42                                                 const struct pipe_video_buffer *tmpl)
43{
44   struct pipe_video_buffer vidbuf = *tmpl;
45   uint64_t *modifiers = NULL;
46   int modifiers_count = 0;
47   uint64_t mod = DRM_FORMAT_MOD_LINEAR;
48
49   /* To get tiled buffers, users need to explicitly provide a list of
50    * modifiers. */
51   vidbuf.bind |= PIPE_BIND_LINEAR;
52
53   if (pipe->screen->resource_create_with_modifiers) {
54      modifiers = &mod;
55      modifiers_count = 1;
56   }
57
58   return vl_video_buffer_create_as_resource(pipe, &vidbuf, modifiers,
59                                             modifiers_count);
60}
61
62struct pipe_video_buffer *si_video_buffer_create_with_modifiers(struct pipe_context *pipe,
63                                                                const struct pipe_video_buffer *tmpl,
64                                                                const uint64_t *modifiers,
65                                                                unsigned int modifiers_count)
66{
67   uint64_t *allowed_modifiers;
68   unsigned int allowed_modifiers_count, i;
69
70   /* Filter out DCC modifiers, because we don't support them for video
71    * for now. */
72   allowed_modifiers = calloc(modifiers_count, sizeof(uint64_t));
73   if (!allowed_modifiers)
74      return NULL;
75
76   allowed_modifiers_count = 0;
77   for (i = 0; i < modifiers_count; i++) {
78      if (ac_modifier_has_dcc(modifiers[i]))
79         continue;
80      allowed_modifiers[allowed_modifiers_count++] = modifiers[i];
81   }
82
83   struct pipe_video_buffer *buf =
84      vl_video_buffer_create_as_resource(pipe, tmpl, allowed_modifiers, allowed_modifiers_count);
85   free(allowed_modifiers);
86   return buf;
87}
88
89/* set the decoding target buffer offsets */
90static struct pb_buffer *si_uvd_set_dtb(struct ruvd_msg *msg, struct vl_video_buffer *buf)
91{
92   struct si_screen *sscreen = (struct si_screen *)buf->base.context->screen;
93   struct si_texture *luma = (struct si_texture *)buf->resources[0];
94   struct si_texture *chroma = (struct si_texture *)buf->resources[1];
95   enum ruvd_surface_type type =
96      (sscreen->info.gfx_level >= GFX9) ? RUVD_SURFACE_TYPE_GFX9 : RUVD_SURFACE_TYPE_LEGACY;
97
98   msg->body.decode.dt_field_mode = buf->base.interlaced;
99
100   si_uvd_set_dt_surfaces(msg, &luma->surface, (chroma) ? &chroma->surface : NULL, type);
101
102   return luma->buffer.buf;
103}
104
105/* get the radeon resources for VCE */
106static void si_vce_get_buffer(struct pipe_resource *resource, struct pb_buffer **handle,
107                              struct radeon_surf **surface)
108{
109   struct si_texture *res = (struct si_texture *)resource;
110
111   if (handle)
112      *handle = res->buffer.buf;
113
114   if (surface)
115      *surface = &res->surface;
116}
117
118/**
119 * creates an UVD compatible decoder
120 */
121struct pipe_video_codec *si_uvd_create_decoder(struct pipe_context *context,
122                                               const struct pipe_video_codec *templ)
123{
124   struct si_context *ctx = (struct si_context *)context;
125   bool vcn = ctx->family >= CHIP_RAVEN;
126
127   if (templ->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
128      if (vcn) {
129         return radeon_create_encoder(context, templ, ctx->ws, si_vce_get_buffer);
130      } else {
131         if (u_reduce_video_profile(templ->profile) == PIPE_VIDEO_FORMAT_HEVC)
132            return radeon_uvd_create_encoder(context, templ, ctx->ws, si_vce_get_buffer);
133         else
134            return si_vce_create_encoder(context, templ, ctx->ws, si_vce_get_buffer);
135      }
136   }
137
138   return (vcn) ? radeon_create_decoder(context, templ)
139                : si_common_uvd_create_decoder(context, templ, si_uvd_set_dtb);
140}
141