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 "radeon_uvd.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "pipe/p_video_codec.h"
31bf215546Sopenharmony_ci#include "radeon_video.h"
32bf215546Sopenharmony_ci#include "radeonsi/si_pipe.h"
33bf215546Sopenharmony_ci#include "util/u_memory.h"
34bf215546Sopenharmony_ci#include "util/u_video.h"
35bf215546Sopenharmony_ci#include "vl/vl_defines.h"
36bf215546Sopenharmony_ci#include "vl/vl_mpeg12_decoder.h"
37bf215546Sopenharmony_ci#include <sys/types.h>
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci#include <assert.h>
40bf215546Sopenharmony_ci#include <errno.h>
41bf215546Sopenharmony_ci#include <stdio.h>
42bf215546Sopenharmony_ci#include <unistd.h>
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci#define NUM_BUFFERS 4
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci#define NUM_MPEG2_REFS 6
47bf215546Sopenharmony_ci#define NUM_H264_REFS  17
48bf215546Sopenharmony_ci#define NUM_VC1_REFS   5
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci#define FB_BUFFER_OFFSET         0x1000
51bf215546Sopenharmony_ci#define FB_BUFFER_SIZE           2048
52bf215546Sopenharmony_ci#define FB_BUFFER_SIZE_TONGA     (2048 * 64)
53bf215546Sopenharmony_ci#define IT_SCALING_TABLE_SIZE    992
54bf215546Sopenharmony_ci#define UVD_SESSION_CONTEXT_SIZE (128 * 1024)
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci/* UVD decoder representation */
57bf215546Sopenharmony_cistruct ruvd_decoder {
58bf215546Sopenharmony_ci   struct pipe_video_codec base;
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci   ruvd_set_dtb set_dtb;
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci   unsigned stream_handle;
63bf215546Sopenharmony_ci   unsigned stream_type;
64bf215546Sopenharmony_ci   unsigned frame_number;
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci   struct pipe_screen *screen;
67bf215546Sopenharmony_ci   struct radeon_winsys *ws;
68bf215546Sopenharmony_ci   struct radeon_cmdbuf cs;
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci   unsigned cur_buffer;
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   struct rvid_buffer msg_fb_it_buffers[NUM_BUFFERS];
73bf215546Sopenharmony_ci   struct ruvd_msg *msg;
74bf215546Sopenharmony_ci   uint32_t *fb;
75bf215546Sopenharmony_ci   unsigned fb_size;
76bf215546Sopenharmony_ci   uint8_t *it;
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci   struct rvid_buffer bs_buffers[NUM_BUFFERS];
79bf215546Sopenharmony_ci   void *bs_ptr;
80bf215546Sopenharmony_ci   unsigned bs_size;
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci   struct rvid_buffer dpb;
83bf215546Sopenharmony_ci   bool use_legacy;
84bf215546Sopenharmony_ci   struct rvid_buffer ctx;
85bf215546Sopenharmony_ci   struct rvid_buffer sessionctx;
86bf215546Sopenharmony_ci   struct {
87bf215546Sopenharmony_ci      unsigned data0;
88bf215546Sopenharmony_ci      unsigned data1;
89bf215546Sopenharmony_ci      unsigned cmd;
90bf215546Sopenharmony_ci      unsigned cntl;
91bf215546Sopenharmony_ci   } reg;
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci   void *render_pic_list[16];
94bf215546Sopenharmony_ci};
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci/* flush IB to the hardware */
97bf215546Sopenharmony_cistatic int flush(struct ruvd_decoder *dec, unsigned flags)
98bf215546Sopenharmony_ci{
99bf215546Sopenharmony_ci   return dec->ws->cs_flush(&dec->cs, flags, NULL);
100bf215546Sopenharmony_ci}
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci/* add a new set register command to the IB */
103bf215546Sopenharmony_cistatic void set_reg(struct ruvd_decoder *dec, unsigned reg, uint32_t val)
104bf215546Sopenharmony_ci{
105bf215546Sopenharmony_ci   radeon_emit(&dec->cs, RUVD_PKT0(reg >> 2, 0));
106bf215546Sopenharmony_ci   radeon_emit(&dec->cs, val);
107bf215546Sopenharmony_ci}
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci/* send a command to the VCPU through the GPCOM registers */
110bf215546Sopenharmony_cistatic void send_cmd(struct ruvd_decoder *dec, unsigned cmd, struct pb_buffer *buf, uint32_t off,
111bf215546Sopenharmony_ci                     unsigned usage, enum radeon_bo_domain domain)
112bf215546Sopenharmony_ci{
113bf215546Sopenharmony_ci   int reloc_idx;
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci   reloc_idx = dec->ws->cs_add_buffer(&dec->cs, buf, usage | RADEON_USAGE_SYNCHRONIZED, domain);
116bf215546Sopenharmony_ci   if (!dec->use_legacy) {
117bf215546Sopenharmony_ci      uint64_t addr;
118bf215546Sopenharmony_ci      addr = dec->ws->buffer_get_virtual_address(buf);
119bf215546Sopenharmony_ci      addr = addr + off;
120bf215546Sopenharmony_ci      set_reg(dec, dec->reg.data0, addr);
121bf215546Sopenharmony_ci      set_reg(dec, dec->reg.data1, addr >> 32);
122bf215546Sopenharmony_ci   } else {
123bf215546Sopenharmony_ci      off += dec->ws->buffer_get_reloc_offset(buf);
124bf215546Sopenharmony_ci      set_reg(dec, RUVD_GPCOM_VCPU_DATA0, off);
125bf215546Sopenharmony_ci      set_reg(dec, RUVD_GPCOM_VCPU_DATA1, reloc_idx * 4);
126bf215546Sopenharmony_ci   }
127bf215546Sopenharmony_ci   set_reg(dec, dec->reg.cmd, cmd << 1);
128bf215546Sopenharmony_ci}
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci/* do the codec needs an IT buffer ?*/
131bf215546Sopenharmony_cistatic bool have_it(struct ruvd_decoder *dec)
132bf215546Sopenharmony_ci{
133bf215546Sopenharmony_ci   return dec->stream_type == RUVD_CODEC_H264_PERF || dec->stream_type == RUVD_CODEC_H265;
134bf215546Sopenharmony_ci}
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci/* map the next available message/feedback/itscaling buffer */
137bf215546Sopenharmony_cistatic void map_msg_fb_it_buf(struct ruvd_decoder *dec)
138bf215546Sopenharmony_ci{
139bf215546Sopenharmony_ci   struct rvid_buffer *buf;
140bf215546Sopenharmony_ci   uint8_t *ptr;
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_ci   /* grab the current message/feedback buffer */
143bf215546Sopenharmony_ci   buf = &dec->msg_fb_it_buffers[dec->cur_buffer];
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci   /* and map it for CPU access */
146bf215546Sopenharmony_ci   ptr =
147bf215546Sopenharmony_ci      dec->ws->buffer_map(dec->ws, buf->res->buf, &dec->cs, PIPE_MAP_WRITE | RADEON_MAP_TEMPORARY);
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci   /* calc buffer offsets */
150bf215546Sopenharmony_ci   dec->msg = (struct ruvd_msg *)ptr;
151bf215546Sopenharmony_ci   memset(dec->msg, 0, sizeof(*dec->msg));
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci   dec->fb = (uint32_t *)(ptr + FB_BUFFER_OFFSET);
154bf215546Sopenharmony_ci   if (have_it(dec))
155bf215546Sopenharmony_ci      dec->it = (uint8_t *)(ptr + FB_BUFFER_OFFSET + dec->fb_size);
156bf215546Sopenharmony_ci}
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_ci/* unmap and send a message command to the VCPU */
159bf215546Sopenharmony_cistatic void send_msg_buf(struct ruvd_decoder *dec)
160bf215546Sopenharmony_ci{
161bf215546Sopenharmony_ci   struct rvid_buffer *buf;
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci   /* ignore the request if message/feedback buffer isn't mapped */
164bf215546Sopenharmony_ci   if (!dec->msg || !dec->fb)
165bf215546Sopenharmony_ci      return;
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci   /* grab the current message buffer */
168bf215546Sopenharmony_ci   buf = &dec->msg_fb_it_buffers[dec->cur_buffer];
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci   /* unmap the buffer */
171bf215546Sopenharmony_ci   dec->ws->buffer_unmap(dec->ws, buf->res->buf);
172bf215546Sopenharmony_ci   dec->msg = NULL;
173bf215546Sopenharmony_ci   dec->fb = NULL;
174bf215546Sopenharmony_ci   dec->it = NULL;
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_ci   if (dec->sessionctx.res)
177bf215546Sopenharmony_ci      send_cmd(dec, RUVD_CMD_SESSION_CONTEXT_BUFFER, dec->sessionctx.res->buf, 0,
178bf215546Sopenharmony_ci               RADEON_USAGE_READWRITE, RADEON_DOMAIN_VRAM);
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_ci   /* and send it to the hardware */
181bf215546Sopenharmony_ci   send_cmd(dec, RUVD_CMD_MSG_BUFFER, buf->res->buf, 0, RADEON_USAGE_READ, RADEON_DOMAIN_GTT);
182bf215546Sopenharmony_ci}
183bf215546Sopenharmony_ci
184bf215546Sopenharmony_ci/* cycle to the next set of buffers */
185bf215546Sopenharmony_cistatic void next_buffer(struct ruvd_decoder *dec)
186bf215546Sopenharmony_ci{
187bf215546Sopenharmony_ci   ++dec->cur_buffer;
188bf215546Sopenharmony_ci   dec->cur_buffer %= NUM_BUFFERS;
189bf215546Sopenharmony_ci}
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_ci/* convert the profile into something UVD understands */
192bf215546Sopenharmony_cistatic uint32_t profile2stream_type(struct ruvd_decoder *dec, unsigned family)
193bf215546Sopenharmony_ci{
194bf215546Sopenharmony_ci   switch (u_reduce_video_profile(dec->base.profile)) {
195bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_MPEG4_AVC:
196bf215546Sopenharmony_ci      return (family >= CHIP_TONGA) ? RUVD_CODEC_H264_PERF : RUVD_CODEC_H264;
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_VC1:
199bf215546Sopenharmony_ci      return RUVD_CODEC_VC1;
200bf215546Sopenharmony_ci
201bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_MPEG12:
202bf215546Sopenharmony_ci      return RUVD_CODEC_MPEG2;
203bf215546Sopenharmony_ci
204bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_MPEG4:
205bf215546Sopenharmony_ci      return RUVD_CODEC_MPEG4;
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_HEVC:
208bf215546Sopenharmony_ci      return RUVD_CODEC_H265;
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_JPEG:
211bf215546Sopenharmony_ci      return RUVD_CODEC_MJPEG;
212bf215546Sopenharmony_ci
213bf215546Sopenharmony_ci   default:
214bf215546Sopenharmony_ci      assert(0);
215bf215546Sopenharmony_ci      return 0;
216bf215546Sopenharmony_ci   }
217bf215546Sopenharmony_ci}
218bf215546Sopenharmony_ci
219bf215546Sopenharmony_cistatic unsigned calc_ctx_size_h264_perf(struct ruvd_decoder *dec)
220bf215546Sopenharmony_ci{
221bf215546Sopenharmony_ci   unsigned width_in_mb, height_in_mb, ctx_size;
222bf215546Sopenharmony_ci   unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
223bf215546Sopenharmony_ci   unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
224bf215546Sopenharmony_ci
225bf215546Sopenharmony_ci   unsigned max_references = dec->base.max_references + 1;
226bf215546Sopenharmony_ci
227bf215546Sopenharmony_ci   // picture width & height in 16 pixel units
228bf215546Sopenharmony_ci   width_in_mb = width / VL_MACROBLOCK_WIDTH;
229bf215546Sopenharmony_ci   height_in_mb = align(height / VL_MACROBLOCK_HEIGHT, 2);
230bf215546Sopenharmony_ci
231bf215546Sopenharmony_ci   if (!dec->use_legacy) {
232bf215546Sopenharmony_ci      unsigned fs_in_mb = width_in_mb * height_in_mb;
233bf215546Sopenharmony_ci      unsigned num_dpb_buffer;
234bf215546Sopenharmony_ci      switch (dec->base.level) {
235bf215546Sopenharmony_ci      case 30:
236bf215546Sopenharmony_ci         num_dpb_buffer = 8100 / fs_in_mb;
237bf215546Sopenharmony_ci         break;
238bf215546Sopenharmony_ci      case 31:
239bf215546Sopenharmony_ci         num_dpb_buffer = 18000 / fs_in_mb;
240bf215546Sopenharmony_ci         break;
241bf215546Sopenharmony_ci      case 32:
242bf215546Sopenharmony_ci         num_dpb_buffer = 20480 / fs_in_mb;
243bf215546Sopenharmony_ci         break;
244bf215546Sopenharmony_ci      case 41:
245bf215546Sopenharmony_ci         num_dpb_buffer = 32768 / fs_in_mb;
246bf215546Sopenharmony_ci         break;
247bf215546Sopenharmony_ci      case 42:
248bf215546Sopenharmony_ci         num_dpb_buffer = 34816 / fs_in_mb;
249bf215546Sopenharmony_ci         break;
250bf215546Sopenharmony_ci      case 50:
251bf215546Sopenharmony_ci         num_dpb_buffer = 110400 / fs_in_mb;
252bf215546Sopenharmony_ci         break;
253bf215546Sopenharmony_ci      case 51:
254bf215546Sopenharmony_ci         num_dpb_buffer = 184320 / fs_in_mb;
255bf215546Sopenharmony_ci         break;
256bf215546Sopenharmony_ci      default:
257bf215546Sopenharmony_ci         num_dpb_buffer = 184320 / fs_in_mb;
258bf215546Sopenharmony_ci         break;
259bf215546Sopenharmony_ci      }
260bf215546Sopenharmony_ci      num_dpb_buffer++;
261bf215546Sopenharmony_ci      max_references = MAX2(MIN2(NUM_H264_REFS, num_dpb_buffer), max_references);
262bf215546Sopenharmony_ci      ctx_size = max_references * align(width_in_mb * height_in_mb * 192, 256);
263bf215546Sopenharmony_ci   } else {
264bf215546Sopenharmony_ci      // the firmware seems to always assume a minimum of ref frames
265bf215546Sopenharmony_ci      max_references = MAX2(NUM_H264_REFS, max_references);
266bf215546Sopenharmony_ci      // macroblock context buffer
267bf215546Sopenharmony_ci      ctx_size = align(width_in_mb * height_in_mb * max_references * 192, 256);
268bf215546Sopenharmony_ci   }
269bf215546Sopenharmony_ci
270bf215546Sopenharmony_ci   return ctx_size;
271bf215546Sopenharmony_ci}
272bf215546Sopenharmony_ci
273bf215546Sopenharmony_cistatic unsigned calc_ctx_size_h265_main(struct ruvd_decoder *dec)
274bf215546Sopenharmony_ci{
275bf215546Sopenharmony_ci   unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
276bf215546Sopenharmony_ci   unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
277bf215546Sopenharmony_ci
278bf215546Sopenharmony_ci   unsigned max_references = dec->base.max_references + 1;
279bf215546Sopenharmony_ci
280bf215546Sopenharmony_ci   if (dec->base.width * dec->base.height >= 4096 * 2000)
281bf215546Sopenharmony_ci      max_references = MAX2(max_references, 8);
282bf215546Sopenharmony_ci   else
283bf215546Sopenharmony_ci      max_references = MAX2(max_references, 17);
284bf215546Sopenharmony_ci
285bf215546Sopenharmony_ci   width = align(width, 16);
286bf215546Sopenharmony_ci   height = align(height, 16);
287bf215546Sopenharmony_ci   return ((width + 255) / 16) * ((height + 255) / 16) * 16 * max_references + 52 * 1024;
288bf215546Sopenharmony_ci}
289bf215546Sopenharmony_ci
290bf215546Sopenharmony_cistatic unsigned calc_ctx_size_h265_main10(struct ruvd_decoder *dec,
291bf215546Sopenharmony_ci                                          struct pipe_h265_picture_desc *pic)
292bf215546Sopenharmony_ci{
293bf215546Sopenharmony_ci   unsigned log2_ctb_size, width_in_ctb, height_in_ctb, num_16x16_block_per_ctb;
294bf215546Sopenharmony_ci   unsigned context_buffer_size_per_ctb_row, cm_buffer_size, max_mb_address, db_left_tile_pxl_size;
295bf215546Sopenharmony_ci   unsigned db_left_tile_ctx_size = 4096 / 16 * (32 + 16 * 4);
296bf215546Sopenharmony_ci
297bf215546Sopenharmony_ci   unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
298bf215546Sopenharmony_ci   unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
299bf215546Sopenharmony_ci   unsigned coeff_10bit =
300bf215546Sopenharmony_ci      (pic->pps->sps->bit_depth_luma_minus8 || pic->pps->sps->bit_depth_chroma_minus8) ? 2 : 1;
301bf215546Sopenharmony_ci
302bf215546Sopenharmony_ci   unsigned max_references = dec->base.max_references + 1;
303bf215546Sopenharmony_ci
304bf215546Sopenharmony_ci   if (dec->base.width * dec->base.height >= 4096 * 2000)
305bf215546Sopenharmony_ci      max_references = MAX2(max_references, 8);
306bf215546Sopenharmony_ci   else
307bf215546Sopenharmony_ci      max_references = MAX2(max_references, 17);
308bf215546Sopenharmony_ci
309bf215546Sopenharmony_ci   log2_ctb_size = pic->pps->sps->log2_min_luma_coding_block_size_minus3 + 3 +
310bf215546Sopenharmony_ci                   pic->pps->sps->log2_diff_max_min_luma_coding_block_size;
311bf215546Sopenharmony_ci
312bf215546Sopenharmony_ci   width_in_ctb = (width + ((1 << log2_ctb_size) - 1)) >> log2_ctb_size;
313bf215546Sopenharmony_ci   height_in_ctb = (height + ((1 << log2_ctb_size) - 1)) >> log2_ctb_size;
314bf215546Sopenharmony_ci
315bf215546Sopenharmony_ci   num_16x16_block_per_ctb = ((1 << log2_ctb_size) >> 4) * ((1 << log2_ctb_size) >> 4);
316bf215546Sopenharmony_ci   context_buffer_size_per_ctb_row = align(width_in_ctb * num_16x16_block_per_ctb * 16, 256);
317bf215546Sopenharmony_ci   max_mb_address = (unsigned)ceil(height * 8 / 2048.0);
318bf215546Sopenharmony_ci
319bf215546Sopenharmony_ci   cm_buffer_size = max_references * context_buffer_size_per_ctb_row * height_in_ctb;
320bf215546Sopenharmony_ci   db_left_tile_pxl_size = coeff_10bit * (max_mb_address * 2 * 2048 + 1024);
321bf215546Sopenharmony_ci
322bf215546Sopenharmony_ci   return cm_buffer_size + db_left_tile_ctx_size + db_left_tile_pxl_size;
323bf215546Sopenharmony_ci}
324bf215546Sopenharmony_ci
325bf215546Sopenharmony_cistatic unsigned get_db_pitch_alignment(struct ruvd_decoder *dec)
326bf215546Sopenharmony_ci{
327bf215546Sopenharmony_ci   if (((struct si_screen *)dec->screen)->info.family < CHIP_VEGA10)
328bf215546Sopenharmony_ci      return 16;
329bf215546Sopenharmony_ci   else
330bf215546Sopenharmony_ci      return 32;
331bf215546Sopenharmony_ci}
332bf215546Sopenharmony_ci
333bf215546Sopenharmony_ci/* calculate size of reference picture buffer */
334bf215546Sopenharmony_cistatic unsigned calc_dpb_size(struct ruvd_decoder *dec)
335bf215546Sopenharmony_ci{
336bf215546Sopenharmony_ci   unsigned width_in_mb, height_in_mb, image_size, dpb_size;
337bf215546Sopenharmony_ci
338bf215546Sopenharmony_ci   // always align them to MB size for dpb calculation
339bf215546Sopenharmony_ci   unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
340bf215546Sopenharmony_ci   unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
341bf215546Sopenharmony_ci
342bf215546Sopenharmony_ci   // always one more for currently decoded picture
343bf215546Sopenharmony_ci   unsigned max_references = dec->base.max_references + 1;
344bf215546Sopenharmony_ci
345bf215546Sopenharmony_ci   // aligned size of a single frame
346bf215546Sopenharmony_ci   image_size = align(width, get_db_pitch_alignment(dec)) * height;
347bf215546Sopenharmony_ci   image_size += image_size / 2;
348bf215546Sopenharmony_ci   image_size = align(image_size, 1024);
349bf215546Sopenharmony_ci
350bf215546Sopenharmony_ci   // picture width & height in 16 pixel units
351bf215546Sopenharmony_ci   width_in_mb = width / VL_MACROBLOCK_WIDTH;
352bf215546Sopenharmony_ci   height_in_mb = align(height / VL_MACROBLOCK_HEIGHT, 2);
353bf215546Sopenharmony_ci
354bf215546Sopenharmony_ci   switch (u_reduce_video_profile(dec->base.profile)) {
355bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_MPEG4_AVC: {
356bf215546Sopenharmony_ci      if (!dec->use_legacy) {
357bf215546Sopenharmony_ci         unsigned fs_in_mb = width_in_mb * height_in_mb;
358bf215546Sopenharmony_ci         unsigned alignment = 64, num_dpb_buffer;
359bf215546Sopenharmony_ci
360bf215546Sopenharmony_ci         if (dec->stream_type == RUVD_CODEC_H264_PERF)
361bf215546Sopenharmony_ci            alignment = 256;
362bf215546Sopenharmony_ci         switch (dec->base.level) {
363bf215546Sopenharmony_ci         case 30:
364bf215546Sopenharmony_ci            num_dpb_buffer = 8100 / fs_in_mb;
365bf215546Sopenharmony_ci            break;
366bf215546Sopenharmony_ci         case 31:
367bf215546Sopenharmony_ci            num_dpb_buffer = 18000 / fs_in_mb;
368bf215546Sopenharmony_ci            break;
369bf215546Sopenharmony_ci         case 32:
370bf215546Sopenharmony_ci            num_dpb_buffer = 20480 / fs_in_mb;
371bf215546Sopenharmony_ci            break;
372bf215546Sopenharmony_ci         case 41:
373bf215546Sopenharmony_ci            num_dpb_buffer = 32768 / fs_in_mb;
374bf215546Sopenharmony_ci            break;
375bf215546Sopenharmony_ci         case 42:
376bf215546Sopenharmony_ci            num_dpb_buffer = 34816 / fs_in_mb;
377bf215546Sopenharmony_ci            break;
378bf215546Sopenharmony_ci         case 50:
379bf215546Sopenharmony_ci            num_dpb_buffer = 110400 / fs_in_mb;
380bf215546Sopenharmony_ci            break;
381bf215546Sopenharmony_ci         case 51:
382bf215546Sopenharmony_ci            num_dpb_buffer = 184320 / fs_in_mb;
383bf215546Sopenharmony_ci            break;
384bf215546Sopenharmony_ci         default:
385bf215546Sopenharmony_ci            num_dpb_buffer = 184320 / fs_in_mb;
386bf215546Sopenharmony_ci            break;
387bf215546Sopenharmony_ci         }
388bf215546Sopenharmony_ci         num_dpb_buffer++;
389bf215546Sopenharmony_ci         max_references = MAX2(MIN2(NUM_H264_REFS, num_dpb_buffer), max_references);
390bf215546Sopenharmony_ci         dpb_size = image_size * max_references;
391bf215546Sopenharmony_ci         if ((dec->stream_type != RUVD_CODEC_H264_PERF) ||
392bf215546Sopenharmony_ci             (((struct si_screen *)dec->screen)->info.family < CHIP_POLARIS10)) {
393bf215546Sopenharmony_ci            dpb_size += max_references * align(width_in_mb * height_in_mb * 192, alignment);
394bf215546Sopenharmony_ci            dpb_size += align(width_in_mb * height_in_mb * 32, alignment);
395bf215546Sopenharmony_ci         }
396bf215546Sopenharmony_ci      } else {
397bf215546Sopenharmony_ci         // the firmware seems to allways assume a minimum of ref frames
398bf215546Sopenharmony_ci         max_references = MAX2(NUM_H264_REFS, max_references);
399bf215546Sopenharmony_ci         // reference picture buffer
400bf215546Sopenharmony_ci         dpb_size = image_size * max_references;
401bf215546Sopenharmony_ci         if ((dec->stream_type != RUVD_CODEC_H264_PERF) ||
402bf215546Sopenharmony_ci             (((struct si_screen *)dec->screen)->info.family < CHIP_POLARIS10)) {
403bf215546Sopenharmony_ci            // macroblock context buffer
404bf215546Sopenharmony_ci            dpb_size += width_in_mb * height_in_mb * max_references * 192;
405bf215546Sopenharmony_ci            // IT surface buffer
406bf215546Sopenharmony_ci            dpb_size += width_in_mb * height_in_mb * 32;
407bf215546Sopenharmony_ci         }
408bf215546Sopenharmony_ci      }
409bf215546Sopenharmony_ci      break;
410bf215546Sopenharmony_ci   }
411bf215546Sopenharmony_ci
412bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_HEVC:
413bf215546Sopenharmony_ci      if (dec->base.width * dec->base.height >= 4096 * 2000)
414bf215546Sopenharmony_ci         max_references = MAX2(max_references, 8);
415bf215546Sopenharmony_ci      else
416bf215546Sopenharmony_ci         max_references = MAX2(max_references, 17);
417bf215546Sopenharmony_ci
418bf215546Sopenharmony_ci      width = align(width, 16);
419bf215546Sopenharmony_ci      height = align(height, 16);
420bf215546Sopenharmony_ci      if (dec->base.profile == PIPE_VIDEO_PROFILE_HEVC_MAIN_10)
421bf215546Sopenharmony_ci         dpb_size = align((align(width, get_db_pitch_alignment(dec)) * height * 9) / 4, 256) *
422bf215546Sopenharmony_ci                    max_references;
423bf215546Sopenharmony_ci      else
424bf215546Sopenharmony_ci         dpb_size = align((align(width, get_db_pitch_alignment(dec)) * height * 3) / 2, 256) *
425bf215546Sopenharmony_ci                    max_references;
426bf215546Sopenharmony_ci      break;
427bf215546Sopenharmony_ci
428bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_VC1:
429bf215546Sopenharmony_ci      // the firmware seems to allways assume a minimum of ref frames
430bf215546Sopenharmony_ci      max_references = MAX2(NUM_VC1_REFS, max_references);
431bf215546Sopenharmony_ci
432bf215546Sopenharmony_ci      // reference picture buffer
433bf215546Sopenharmony_ci      dpb_size = image_size * max_references;
434bf215546Sopenharmony_ci
435bf215546Sopenharmony_ci      // CONTEXT_BUFFER
436bf215546Sopenharmony_ci      dpb_size += width_in_mb * height_in_mb * 128;
437bf215546Sopenharmony_ci
438bf215546Sopenharmony_ci      // IT surface buffer
439bf215546Sopenharmony_ci      dpb_size += width_in_mb * 64;
440bf215546Sopenharmony_ci
441bf215546Sopenharmony_ci      // DB surface buffer
442bf215546Sopenharmony_ci      dpb_size += width_in_mb * 128;
443bf215546Sopenharmony_ci
444bf215546Sopenharmony_ci      // BP
445bf215546Sopenharmony_ci      dpb_size += align(MAX2(width_in_mb, height_in_mb) * 7 * 16, 64);
446bf215546Sopenharmony_ci      break;
447bf215546Sopenharmony_ci
448bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_MPEG12:
449bf215546Sopenharmony_ci      // reference picture buffer, must be big enough for all frames
450bf215546Sopenharmony_ci      dpb_size = image_size * NUM_MPEG2_REFS;
451bf215546Sopenharmony_ci      break;
452bf215546Sopenharmony_ci
453bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_MPEG4:
454bf215546Sopenharmony_ci      // reference picture buffer
455bf215546Sopenharmony_ci      dpb_size = image_size * max_references;
456bf215546Sopenharmony_ci
457bf215546Sopenharmony_ci      // CM
458bf215546Sopenharmony_ci      dpb_size += width_in_mb * height_in_mb * 64;
459bf215546Sopenharmony_ci
460bf215546Sopenharmony_ci      // IT surface buffer
461bf215546Sopenharmony_ci      dpb_size += align(width_in_mb * height_in_mb * 32, 64);
462bf215546Sopenharmony_ci
463bf215546Sopenharmony_ci      dpb_size = MAX2(dpb_size, 30 * 1024 * 1024);
464bf215546Sopenharmony_ci      break;
465bf215546Sopenharmony_ci
466bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_JPEG:
467bf215546Sopenharmony_ci      dpb_size = 0;
468bf215546Sopenharmony_ci      break;
469bf215546Sopenharmony_ci
470bf215546Sopenharmony_ci   default:
471bf215546Sopenharmony_ci      // something is missing here
472bf215546Sopenharmony_ci      assert(0);
473bf215546Sopenharmony_ci
474bf215546Sopenharmony_ci      // at least use a sane default value
475bf215546Sopenharmony_ci      dpb_size = 32 * 1024 * 1024;
476bf215546Sopenharmony_ci      break;
477bf215546Sopenharmony_ci   }
478bf215546Sopenharmony_ci   return dpb_size;
479bf215546Sopenharmony_ci}
480bf215546Sopenharmony_ci
481bf215546Sopenharmony_ci/* free associated data in the video buffer callback */
482bf215546Sopenharmony_cistatic void ruvd_destroy_associated_data(void *data)
483bf215546Sopenharmony_ci{
484bf215546Sopenharmony_ci   /* NOOP, since we only use an intptr */
485bf215546Sopenharmony_ci}
486bf215546Sopenharmony_ci
487bf215546Sopenharmony_ci/* get h264 specific message bits */
488bf215546Sopenharmony_cistatic struct ruvd_h264 get_h264_msg(struct ruvd_decoder *dec, struct pipe_h264_picture_desc *pic)
489bf215546Sopenharmony_ci{
490bf215546Sopenharmony_ci   struct ruvd_h264 result;
491bf215546Sopenharmony_ci
492bf215546Sopenharmony_ci   memset(&result, 0, sizeof(result));
493bf215546Sopenharmony_ci   switch (pic->base.profile) {
494bf215546Sopenharmony_ci   case PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE:
495bf215546Sopenharmony_ci   case PIPE_VIDEO_PROFILE_MPEG4_AVC_CONSTRAINED_BASELINE:
496bf215546Sopenharmony_ci      result.profile = RUVD_H264_PROFILE_BASELINE;
497bf215546Sopenharmony_ci      break;
498bf215546Sopenharmony_ci
499bf215546Sopenharmony_ci   case PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN:
500bf215546Sopenharmony_ci      result.profile = RUVD_H264_PROFILE_MAIN;
501bf215546Sopenharmony_ci      break;
502bf215546Sopenharmony_ci
503bf215546Sopenharmony_ci   case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH:
504bf215546Sopenharmony_ci      result.profile = RUVD_H264_PROFILE_HIGH;
505bf215546Sopenharmony_ci      break;
506bf215546Sopenharmony_ci
507bf215546Sopenharmony_ci   default:
508bf215546Sopenharmony_ci      assert(0);
509bf215546Sopenharmony_ci      break;
510bf215546Sopenharmony_ci   }
511bf215546Sopenharmony_ci
512bf215546Sopenharmony_ci   result.level = dec->base.level;
513bf215546Sopenharmony_ci
514bf215546Sopenharmony_ci   result.sps_info_flags = 0;
515bf215546Sopenharmony_ci   result.sps_info_flags |= pic->pps->sps->direct_8x8_inference_flag << 0;
516bf215546Sopenharmony_ci   result.sps_info_flags |= pic->pps->sps->mb_adaptive_frame_field_flag << 1;
517bf215546Sopenharmony_ci   result.sps_info_flags |= pic->pps->sps->frame_mbs_only_flag << 2;
518bf215546Sopenharmony_ci   result.sps_info_flags |= pic->pps->sps->delta_pic_order_always_zero_flag << 3;
519bf215546Sopenharmony_ci
520bf215546Sopenharmony_ci   result.bit_depth_luma_minus8 = pic->pps->sps->bit_depth_luma_minus8;
521bf215546Sopenharmony_ci   result.bit_depth_chroma_minus8 = pic->pps->sps->bit_depth_chroma_minus8;
522bf215546Sopenharmony_ci   result.log2_max_frame_num_minus4 = pic->pps->sps->log2_max_frame_num_minus4;
523bf215546Sopenharmony_ci   result.pic_order_cnt_type = pic->pps->sps->pic_order_cnt_type;
524bf215546Sopenharmony_ci   result.log2_max_pic_order_cnt_lsb_minus4 = pic->pps->sps->log2_max_pic_order_cnt_lsb_minus4;
525bf215546Sopenharmony_ci
526bf215546Sopenharmony_ci   switch (dec->base.chroma_format) {
527bf215546Sopenharmony_ci   case PIPE_VIDEO_CHROMA_FORMAT_NONE:
528bf215546Sopenharmony_ci      /* TODO: assert? */
529bf215546Sopenharmony_ci      break;
530bf215546Sopenharmony_ci   case PIPE_VIDEO_CHROMA_FORMAT_400:
531bf215546Sopenharmony_ci      result.chroma_format = 0;
532bf215546Sopenharmony_ci      break;
533bf215546Sopenharmony_ci   case PIPE_VIDEO_CHROMA_FORMAT_420:
534bf215546Sopenharmony_ci      result.chroma_format = 1;
535bf215546Sopenharmony_ci      break;
536bf215546Sopenharmony_ci   case PIPE_VIDEO_CHROMA_FORMAT_422:
537bf215546Sopenharmony_ci      result.chroma_format = 2;
538bf215546Sopenharmony_ci      break;
539bf215546Sopenharmony_ci   case PIPE_VIDEO_CHROMA_FORMAT_444:
540bf215546Sopenharmony_ci      result.chroma_format = 3;
541bf215546Sopenharmony_ci      break;
542bf215546Sopenharmony_ci   }
543bf215546Sopenharmony_ci
544bf215546Sopenharmony_ci   result.pps_info_flags = 0;
545bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->transform_8x8_mode_flag << 0;
546bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->redundant_pic_cnt_present_flag << 1;
547bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->constrained_intra_pred_flag << 2;
548bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->deblocking_filter_control_present_flag << 3;
549bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->weighted_bipred_idc << 4;
550bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->weighted_pred_flag << 6;
551bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->bottom_field_pic_order_in_frame_present_flag << 7;
552bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->entropy_coding_mode_flag << 8;
553bf215546Sopenharmony_ci
554bf215546Sopenharmony_ci   result.num_slice_groups_minus1 = pic->pps->num_slice_groups_minus1;
555bf215546Sopenharmony_ci   result.slice_group_map_type = pic->pps->slice_group_map_type;
556bf215546Sopenharmony_ci   result.slice_group_change_rate_minus1 = pic->pps->slice_group_change_rate_minus1;
557bf215546Sopenharmony_ci   result.pic_init_qp_minus26 = pic->pps->pic_init_qp_minus26;
558bf215546Sopenharmony_ci   result.chroma_qp_index_offset = pic->pps->chroma_qp_index_offset;
559bf215546Sopenharmony_ci   result.second_chroma_qp_index_offset = pic->pps->second_chroma_qp_index_offset;
560bf215546Sopenharmony_ci
561bf215546Sopenharmony_ci   memcpy(result.scaling_list_4x4, pic->pps->ScalingList4x4, 6 * 16);
562bf215546Sopenharmony_ci   memcpy(result.scaling_list_8x8, pic->pps->ScalingList8x8, 2 * 64);
563bf215546Sopenharmony_ci
564bf215546Sopenharmony_ci   if (dec->stream_type == RUVD_CODEC_H264_PERF) {
565bf215546Sopenharmony_ci      memcpy(dec->it, result.scaling_list_4x4, 6 * 16);
566bf215546Sopenharmony_ci      memcpy((dec->it + 96), result.scaling_list_8x8, 2 * 64);
567bf215546Sopenharmony_ci   }
568bf215546Sopenharmony_ci
569bf215546Sopenharmony_ci   result.num_ref_frames = pic->num_ref_frames;
570bf215546Sopenharmony_ci
571bf215546Sopenharmony_ci   result.num_ref_idx_l0_active_minus1 = pic->num_ref_idx_l0_active_minus1;
572bf215546Sopenharmony_ci   result.num_ref_idx_l1_active_minus1 = pic->num_ref_idx_l1_active_minus1;
573bf215546Sopenharmony_ci
574bf215546Sopenharmony_ci   result.frame_num = pic->frame_num;
575bf215546Sopenharmony_ci   memcpy(result.frame_num_list, pic->frame_num_list, 4 * 16);
576bf215546Sopenharmony_ci   result.curr_field_order_cnt_list[0] = pic->field_order_cnt[0];
577bf215546Sopenharmony_ci   result.curr_field_order_cnt_list[1] = pic->field_order_cnt[1];
578bf215546Sopenharmony_ci   memcpy(result.field_order_cnt_list, pic->field_order_cnt_list, 4 * 16 * 2);
579bf215546Sopenharmony_ci
580bf215546Sopenharmony_ci   result.decoded_pic_idx = pic->frame_num;
581bf215546Sopenharmony_ci
582bf215546Sopenharmony_ci   return result;
583bf215546Sopenharmony_ci}
584bf215546Sopenharmony_ci
585bf215546Sopenharmony_ci/* get h265 specific message bits */
586bf215546Sopenharmony_cistatic struct ruvd_h265 get_h265_msg(struct ruvd_decoder *dec, struct pipe_video_buffer *target,
587bf215546Sopenharmony_ci                                     struct pipe_h265_picture_desc *pic)
588bf215546Sopenharmony_ci{
589bf215546Sopenharmony_ci   struct ruvd_h265 result;
590bf215546Sopenharmony_ci   unsigned i, j;
591bf215546Sopenharmony_ci
592bf215546Sopenharmony_ci   memset(&result, 0, sizeof(result));
593bf215546Sopenharmony_ci
594bf215546Sopenharmony_ci   result.sps_info_flags = 0;
595bf215546Sopenharmony_ci   result.sps_info_flags |= pic->pps->sps->scaling_list_enabled_flag << 0;
596bf215546Sopenharmony_ci   result.sps_info_flags |= pic->pps->sps->amp_enabled_flag << 1;
597bf215546Sopenharmony_ci   result.sps_info_flags |= pic->pps->sps->sample_adaptive_offset_enabled_flag << 2;
598bf215546Sopenharmony_ci   result.sps_info_flags |= pic->pps->sps->pcm_enabled_flag << 3;
599bf215546Sopenharmony_ci   result.sps_info_flags |= pic->pps->sps->pcm_loop_filter_disabled_flag << 4;
600bf215546Sopenharmony_ci   result.sps_info_flags |= pic->pps->sps->long_term_ref_pics_present_flag << 5;
601bf215546Sopenharmony_ci   result.sps_info_flags |= pic->pps->sps->sps_temporal_mvp_enabled_flag << 6;
602bf215546Sopenharmony_ci   result.sps_info_flags |= pic->pps->sps->strong_intra_smoothing_enabled_flag << 7;
603bf215546Sopenharmony_ci   result.sps_info_flags |= pic->pps->sps->separate_colour_plane_flag << 8;
604bf215546Sopenharmony_ci   if (((struct si_screen *)dec->screen)->info.family == CHIP_CARRIZO)
605bf215546Sopenharmony_ci      result.sps_info_flags |= 1 << 9;
606bf215546Sopenharmony_ci   if (pic->UseRefPicList == true)
607bf215546Sopenharmony_ci      result.sps_info_flags |= 1 << 10;
608bf215546Sopenharmony_ci
609bf215546Sopenharmony_ci   result.chroma_format = pic->pps->sps->chroma_format_idc;
610bf215546Sopenharmony_ci   result.bit_depth_luma_minus8 = pic->pps->sps->bit_depth_luma_minus8;
611bf215546Sopenharmony_ci   result.bit_depth_chroma_minus8 = pic->pps->sps->bit_depth_chroma_minus8;
612bf215546Sopenharmony_ci   result.log2_max_pic_order_cnt_lsb_minus4 = pic->pps->sps->log2_max_pic_order_cnt_lsb_minus4;
613bf215546Sopenharmony_ci   result.sps_max_dec_pic_buffering_minus1 = pic->pps->sps->sps_max_dec_pic_buffering_minus1;
614bf215546Sopenharmony_ci   result.log2_min_luma_coding_block_size_minus3 =
615bf215546Sopenharmony_ci      pic->pps->sps->log2_min_luma_coding_block_size_minus3;
616bf215546Sopenharmony_ci   result.log2_diff_max_min_luma_coding_block_size =
617bf215546Sopenharmony_ci      pic->pps->sps->log2_diff_max_min_luma_coding_block_size;
618bf215546Sopenharmony_ci   result.log2_min_transform_block_size_minus2 =
619bf215546Sopenharmony_ci      pic->pps->sps->log2_min_transform_block_size_minus2;
620bf215546Sopenharmony_ci   result.log2_diff_max_min_transform_block_size =
621bf215546Sopenharmony_ci      pic->pps->sps->log2_diff_max_min_transform_block_size;
622bf215546Sopenharmony_ci   result.max_transform_hierarchy_depth_inter = pic->pps->sps->max_transform_hierarchy_depth_inter;
623bf215546Sopenharmony_ci   result.max_transform_hierarchy_depth_intra = pic->pps->sps->max_transform_hierarchy_depth_intra;
624bf215546Sopenharmony_ci   result.pcm_sample_bit_depth_luma_minus1 = pic->pps->sps->pcm_sample_bit_depth_luma_minus1;
625bf215546Sopenharmony_ci   result.pcm_sample_bit_depth_chroma_minus1 = pic->pps->sps->pcm_sample_bit_depth_chroma_minus1;
626bf215546Sopenharmony_ci   result.log2_min_pcm_luma_coding_block_size_minus3 =
627bf215546Sopenharmony_ci      pic->pps->sps->log2_min_pcm_luma_coding_block_size_minus3;
628bf215546Sopenharmony_ci   result.log2_diff_max_min_pcm_luma_coding_block_size =
629bf215546Sopenharmony_ci      pic->pps->sps->log2_diff_max_min_pcm_luma_coding_block_size;
630bf215546Sopenharmony_ci   result.num_short_term_ref_pic_sets = pic->pps->sps->num_short_term_ref_pic_sets;
631bf215546Sopenharmony_ci
632bf215546Sopenharmony_ci   result.pps_info_flags = 0;
633bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->dependent_slice_segments_enabled_flag << 0;
634bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->output_flag_present_flag << 1;
635bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->sign_data_hiding_enabled_flag << 2;
636bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->cabac_init_present_flag << 3;
637bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->constrained_intra_pred_flag << 4;
638bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->transform_skip_enabled_flag << 5;
639bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->cu_qp_delta_enabled_flag << 6;
640bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->pps_slice_chroma_qp_offsets_present_flag << 7;
641bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->weighted_pred_flag << 8;
642bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->weighted_bipred_flag << 9;
643bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->transquant_bypass_enabled_flag << 10;
644bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->tiles_enabled_flag << 11;
645bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->entropy_coding_sync_enabled_flag << 12;
646bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->uniform_spacing_flag << 13;
647bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->loop_filter_across_tiles_enabled_flag << 14;
648bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->pps_loop_filter_across_slices_enabled_flag << 15;
649bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->deblocking_filter_override_enabled_flag << 16;
650bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->pps_deblocking_filter_disabled_flag << 17;
651bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->lists_modification_present_flag << 18;
652bf215546Sopenharmony_ci   result.pps_info_flags |= pic->pps->slice_segment_header_extension_present_flag << 19;
653bf215546Sopenharmony_ci   // result.pps_info_flags |= pic->pps->deblocking_filter_control_present_flag; ???
654bf215546Sopenharmony_ci
655bf215546Sopenharmony_ci   result.num_extra_slice_header_bits = pic->pps->num_extra_slice_header_bits;
656bf215546Sopenharmony_ci   result.num_long_term_ref_pic_sps = pic->pps->sps->num_long_term_ref_pics_sps;
657bf215546Sopenharmony_ci   result.num_ref_idx_l0_default_active_minus1 = pic->pps->num_ref_idx_l0_default_active_minus1;
658bf215546Sopenharmony_ci   result.num_ref_idx_l1_default_active_minus1 = pic->pps->num_ref_idx_l1_default_active_minus1;
659bf215546Sopenharmony_ci   result.pps_cb_qp_offset = pic->pps->pps_cb_qp_offset;
660bf215546Sopenharmony_ci   result.pps_cr_qp_offset = pic->pps->pps_cr_qp_offset;
661bf215546Sopenharmony_ci   result.pps_beta_offset_div2 = pic->pps->pps_beta_offset_div2;
662bf215546Sopenharmony_ci   result.pps_tc_offset_div2 = pic->pps->pps_tc_offset_div2;
663bf215546Sopenharmony_ci   result.diff_cu_qp_delta_depth = pic->pps->diff_cu_qp_delta_depth;
664bf215546Sopenharmony_ci   result.num_tile_columns_minus1 = pic->pps->num_tile_columns_minus1;
665bf215546Sopenharmony_ci   result.num_tile_rows_minus1 = pic->pps->num_tile_rows_minus1;
666bf215546Sopenharmony_ci   result.log2_parallel_merge_level_minus2 = pic->pps->log2_parallel_merge_level_minus2;
667bf215546Sopenharmony_ci   result.init_qp_minus26 = pic->pps->init_qp_minus26;
668bf215546Sopenharmony_ci
669bf215546Sopenharmony_ci   for (i = 0; i < 19; ++i)
670bf215546Sopenharmony_ci      result.column_width_minus1[i] = pic->pps->column_width_minus1[i];
671bf215546Sopenharmony_ci
672bf215546Sopenharmony_ci   for (i = 0; i < 21; ++i)
673bf215546Sopenharmony_ci      result.row_height_minus1[i] = pic->pps->row_height_minus1[i];
674bf215546Sopenharmony_ci
675bf215546Sopenharmony_ci   result.num_delta_pocs_ref_rps_idx = pic->NumDeltaPocsOfRefRpsIdx;
676bf215546Sopenharmony_ci   result.curr_poc = pic->CurrPicOrderCntVal;
677bf215546Sopenharmony_ci
678bf215546Sopenharmony_ci   for (i = 0; i < 16; i++) {
679bf215546Sopenharmony_ci      for (j = 0; (pic->ref[j] != NULL) && (j < 16); j++) {
680bf215546Sopenharmony_ci         if (dec->render_pic_list[i] == pic->ref[j])
681bf215546Sopenharmony_ci            break;
682bf215546Sopenharmony_ci         if (j == 15)
683bf215546Sopenharmony_ci            dec->render_pic_list[i] = NULL;
684bf215546Sopenharmony_ci         else if (pic->ref[j + 1] == NULL)
685bf215546Sopenharmony_ci            dec->render_pic_list[i] = NULL;
686bf215546Sopenharmony_ci      }
687bf215546Sopenharmony_ci   }
688bf215546Sopenharmony_ci   for (i = 0; i < 16; i++) {
689bf215546Sopenharmony_ci      if (dec->render_pic_list[i] == NULL) {
690bf215546Sopenharmony_ci         dec->render_pic_list[i] = target;
691bf215546Sopenharmony_ci         result.curr_idx = i;
692bf215546Sopenharmony_ci         break;
693bf215546Sopenharmony_ci      }
694bf215546Sopenharmony_ci   }
695bf215546Sopenharmony_ci
696bf215546Sopenharmony_ci   vl_video_buffer_set_associated_data(target, &dec->base, (void *)(uintptr_t)result.curr_idx,
697bf215546Sopenharmony_ci                                       &ruvd_destroy_associated_data);
698bf215546Sopenharmony_ci
699bf215546Sopenharmony_ci   for (i = 0; i < 16; ++i) {
700bf215546Sopenharmony_ci      struct pipe_video_buffer *ref = pic->ref[i];
701bf215546Sopenharmony_ci      uintptr_t ref_pic = 0;
702bf215546Sopenharmony_ci
703bf215546Sopenharmony_ci      result.poc_list[i] = pic->PicOrderCntVal[i];
704bf215546Sopenharmony_ci
705bf215546Sopenharmony_ci      if (ref)
706bf215546Sopenharmony_ci         ref_pic = (uintptr_t)vl_video_buffer_get_associated_data(ref, &dec->base);
707bf215546Sopenharmony_ci      else
708bf215546Sopenharmony_ci         ref_pic = 0x7F;
709bf215546Sopenharmony_ci      result.ref_pic_list[i] = ref_pic;
710bf215546Sopenharmony_ci   }
711bf215546Sopenharmony_ci
712bf215546Sopenharmony_ci   for (i = 0; i < 8; ++i) {
713bf215546Sopenharmony_ci      result.ref_pic_set_st_curr_before[i] = 0xFF;
714bf215546Sopenharmony_ci      result.ref_pic_set_st_curr_after[i] = 0xFF;
715bf215546Sopenharmony_ci      result.ref_pic_set_lt_curr[i] = 0xFF;
716bf215546Sopenharmony_ci   }
717bf215546Sopenharmony_ci
718bf215546Sopenharmony_ci   for (i = 0; i < pic->NumPocStCurrBefore; ++i)
719bf215546Sopenharmony_ci      result.ref_pic_set_st_curr_before[i] = pic->RefPicSetStCurrBefore[i];
720bf215546Sopenharmony_ci
721bf215546Sopenharmony_ci   for (i = 0; i < pic->NumPocStCurrAfter; ++i)
722bf215546Sopenharmony_ci      result.ref_pic_set_st_curr_after[i] = pic->RefPicSetStCurrAfter[i];
723bf215546Sopenharmony_ci
724bf215546Sopenharmony_ci   for (i = 0; i < pic->NumPocLtCurr; ++i)
725bf215546Sopenharmony_ci      result.ref_pic_set_lt_curr[i] = pic->RefPicSetLtCurr[i];
726bf215546Sopenharmony_ci
727bf215546Sopenharmony_ci   for (i = 0; i < 6; ++i)
728bf215546Sopenharmony_ci      result.ucScalingListDCCoefSizeID2[i] = pic->pps->sps->ScalingListDCCoeff16x16[i];
729bf215546Sopenharmony_ci
730bf215546Sopenharmony_ci   for (i = 0; i < 2; ++i)
731bf215546Sopenharmony_ci      result.ucScalingListDCCoefSizeID3[i] = pic->pps->sps->ScalingListDCCoeff32x32[i];
732bf215546Sopenharmony_ci
733bf215546Sopenharmony_ci   memcpy(dec->it, pic->pps->sps->ScalingList4x4, 6 * 16);
734bf215546Sopenharmony_ci   memcpy(dec->it + 96, pic->pps->sps->ScalingList8x8, 6 * 64);
735bf215546Sopenharmony_ci   memcpy(dec->it + 480, pic->pps->sps->ScalingList16x16, 6 * 64);
736bf215546Sopenharmony_ci   memcpy(dec->it + 864, pic->pps->sps->ScalingList32x32, 2 * 64);
737bf215546Sopenharmony_ci
738bf215546Sopenharmony_ci   for (i = 0; i < 2; i++) {
739bf215546Sopenharmony_ci      for (j = 0; j < 15; j++)
740bf215546Sopenharmony_ci         result.direct_reflist[i][j] = pic->RefPicList[i][j];
741bf215546Sopenharmony_ci   }
742bf215546Sopenharmony_ci
743bf215546Sopenharmony_ci   if (pic->base.profile == PIPE_VIDEO_PROFILE_HEVC_MAIN_10) {
744bf215546Sopenharmony_ci      if (target->buffer_format == PIPE_FORMAT_P010 || target->buffer_format == PIPE_FORMAT_P016) {
745bf215546Sopenharmony_ci         result.p010_mode = 1;
746bf215546Sopenharmony_ci         result.msb_mode = 1;
747bf215546Sopenharmony_ci      } else {
748bf215546Sopenharmony_ci         result.luma_10to8 = 5;
749bf215546Sopenharmony_ci         result.chroma_10to8 = 5;
750bf215546Sopenharmony_ci         result.sclr_luma10to8 = 4;
751bf215546Sopenharmony_ci         result.sclr_chroma10to8 = 4;
752bf215546Sopenharmony_ci      }
753bf215546Sopenharmony_ci   }
754bf215546Sopenharmony_ci
755bf215546Sopenharmony_ci   /* TODO
756bf215546Sopenharmony_ci   result.highestTid;
757bf215546Sopenharmony_ci   result.isNonRef;
758bf215546Sopenharmony_ci
759bf215546Sopenharmony_ci   IDRPicFlag;
760bf215546Sopenharmony_ci   RAPPicFlag;
761bf215546Sopenharmony_ci   NumPocTotalCurr;
762bf215546Sopenharmony_ci   NumShortTermPictureSliceHeaderBits;
763bf215546Sopenharmony_ci   NumLongTermPictureSliceHeaderBits;
764bf215546Sopenharmony_ci
765bf215546Sopenharmony_ci   IsLongTerm[16];
766bf215546Sopenharmony_ci   */
767bf215546Sopenharmony_ci
768bf215546Sopenharmony_ci   return result;
769bf215546Sopenharmony_ci}
770bf215546Sopenharmony_ci
771bf215546Sopenharmony_ci/* get vc1 specific message bits */
772bf215546Sopenharmony_cistatic struct ruvd_vc1 get_vc1_msg(struct pipe_vc1_picture_desc *pic)
773bf215546Sopenharmony_ci{
774bf215546Sopenharmony_ci   struct ruvd_vc1 result;
775bf215546Sopenharmony_ci
776bf215546Sopenharmony_ci   memset(&result, 0, sizeof(result));
777bf215546Sopenharmony_ci
778bf215546Sopenharmony_ci   switch (pic->base.profile) {
779bf215546Sopenharmony_ci   case PIPE_VIDEO_PROFILE_VC1_SIMPLE:
780bf215546Sopenharmony_ci      result.profile = RUVD_VC1_PROFILE_SIMPLE;
781bf215546Sopenharmony_ci      result.level = 1;
782bf215546Sopenharmony_ci      break;
783bf215546Sopenharmony_ci
784bf215546Sopenharmony_ci   case PIPE_VIDEO_PROFILE_VC1_MAIN:
785bf215546Sopenharmony_ci      result.profile = RUVD_VC1_PROFILE_MAIN;
786bf215546Sopenharmony_ci      result.level = 2;
787bf215546Sopenharmony_ci      break;
788bf215546Sopenharmony_ci
789bf215546Sopenharmony_ci   case PIPE_VIDEO_PROFILE_VC1_ADVANCED:
790bf215546Sopenharmony_ci      result.profile = RUVD_VC1_PROFILE_ADVANCED;
791bf215546Sopenharmony_ci      result.level = 4;
792bf215546Sopenharmony_ci      break;
793bf215546Sopenharmony_ci
794bf215546Sopenharmony_ci   default:
795bf215546Sopenharmony_ci      assert(0);
796bf215546Sopenharmony_ci   }
797bf215546Sopenharmony_ci
798bf215546Sopenharmony_ci   /* fields common for all profiles */
799bf215546Sopenharmony_ci   result.sps_info_flags |= pic->postprocflag << 7;
800bf215546Sopenharmony_ci   result.sps_info_flags |= pic->pulldown << 6;
801bf215546Sopenharmony_ci   result.sps_info_flags |= pic->interlace << 5;
802bf215546Sopenharmony_ci   result.sps_info_flags |= pic->tfcntrflag << 4;
803bf215546Sopenharmony_ci   result.sps_info_flags |= pic->finterpflag << 3;
804bf215546Sopenharmony_ci   result.sps_info_flags |= pic->psf << 1;
805bf215546Sopenharmony_ci
806bf215546Sopenharmony_ci   result.pps_info_flags |= pic->range_mapy_flag << 31;
807bf215546Sopenharmony_ci   result.pps_info_flags |= pic->range_mapy << 28;
808bf215546Sopenharmony_ci   result.pps_info_flags |= pic->range_mapuv_flag << 27;
809bf215546Sopenharmony_ci   result.pps_info_flags |= pic->range_mapuv << 24;
810bf215546Sopenharmony_ci   result.pps_info_flags |= pic->multires << 21;
811bf215546Sopenharmony_ci   result.pps_info_flags |= pic->maxbframes << 16;
812bf215546Sopenharmony_ci   result.pps_info_flags |= pic->overlap << 11;
813bf215546Sopenharmony_ci   result.pps_info_flags |= pic->quantizer << 9;
814bf215546Sopenharmony_ci   result.pps_info_flags |= pic->panscan_flag << 7;
815bf215546Sopenharmony_ci   result.pps_info_flags |= pic->refdist_flag << 6;
816bf215546Sopenharmony_ci   result.pps_info_flags |= pic->vstransform << 0;
817bf215546Sopenharmony_ci
818bf215546Sopenharmony_ci   /* some fields only apply to main/advanced profile */
819bf215546Sopenharmony_ci   if (pic->base.profile != PIPE_VIDEO_PROFILE_VC1_SIMPLE) {
820bf215546Sopenharmony_ci      result.pps_info_flags |= pic->syncmarker << 20;
821bf215546Sopenharmony_ci      result.pps_info_flags |= pic->rangered << 19;
822bf215546Sopenharmony_ci      result.pps_info_flags |= pic->loopfilter << 5;
823bf215546Sopenharmony_ci      result.pps_info_flags |= pic->fastuvmc << 4;
824bf215546Sopenharmony_ci      result.pps_info_flags |= pic->extended_mv << 3;
825bf215546Sopenharmony_ci      result.pps_info_flags |= pic->extended_dmv << 8;
826bf215546Sopenharmony_ci      result.pps_info_flags |= pic->dquant << 1;
827bf215546Sopenharmony_ci   }
828bf215546Sopenharmony_ci
829bf215546Sopenharmony_ci   result.chroma_format = 1;
830bf215546Sopenharmony_ci
831bf215546Sopenharmony_ci#if 0
832bf215546Sopenharmony_ci//(((unsigned int)(pPicParams->advance.reserved1)) << SPS_INFO_VC1_RESERVED_SHIFT)
833bf215546Sopenharmony_ciuint32_t  slice_count
834bf215546Sopenharmony_ciuint8_t   picture_type
835bf215546Sopenharmony_ciuint8_t   frame_coding_mode
836bf215546Sopenharmony_ciuint8_t   deblockEnable
837bf215546Sopenharmony_ciuint8_t   pquant
838bf215546Sopenharmony_ci#endif
839bf215546Sopenharmony_ci
840bf215546Sopenharmony_ci   return result;
841bf215546Sopenharmony_ci}
842bf215546Sopenharmony_ci
843bf215546Sopenharmony_ci/* extract the frame number from a referenced video buffer */
844bf215546Sopenharmony_cistatic uint32_t get_ref_pic_idx(struct ruvd_decoder *dec, struct pipe_video_buffer *ref)
845bf215546Sopenharmony_ci{
846bf215546Sopenharmony_ci   uint32_t min = MAX2(dec->frame_number, NUM_MPEG2_REFS) - NUM_MPEG2_REFS;
847bf215546Sopenharmony_ci   uint32_t max = MAX2(dec->frame_number, 1) - 1;
848bf215546Sopenharmony_ci   uintptr_t frame;
849bf215546Sopenharmony_ci
850bf215546Sopenharmony_ci   /* seems to be the most sane fallback */
851bf215546Sopenharmony_ci   if (!ref)
852bf215546Sopenharmony_ci      return max;
853bf215546Sopenharmony_ci
854bf215546Sopenharmony_ci   /* get the frame number from the associated data */
855bf215546Sopenharmony_ci   frame = (uintptr_t)vl_video_buffer_get_associated_data(ref, &dec->base);
856bf215546Sopenharmony_ci
857bf215546Sopenharmony_ci   /* limit the frame number to a valid range */
858bf215546Sopenharmony_ci   return MAX2(MIN2(frame, max), min);
859bf215546Sopenharmony_ci}
860bf215546Sopenharmony_ci
861bf215546Sopenharmony_ci/* get mpeg2 specific msg bits */
862bf215546Sopenharmony_cistatic struct ruvd_mpeg2 get_mpeg2_msg(struct ruvd_decoder *dec,
863bf215546Sopenharmony_ci                                       struct pipe_mpeg12_picture_desc *pic)
864bf215546Sopenharmony_ci{
865bf215546Sopenharmony_ci   const int *zscan = pic->alternate_scan ? vl_zscan_alternate : vl_zscan_normal;
866bf215546Sopenharmony_ci   struct ruvd_mpeg2 result;
867bf215546Sopenharmony_ci   unsigned i;
868bf215546Sopenharmony_ci
869bf215546Sopenharmony_ci   memset(&result, 0, sizeof(result));
870bf215546Sopenharmony_ci   result.decoded_pic_idx = dec->frame_number;
871bf215546Sopenharmony_ci   for (i = 0; i < 2; ++i)
872bf215546Sopenharmony_ci      result.ref_pic_idx[i] = get_ref_pic_idx(dec, pic->ref[i]);
873bf215546Sopenharmony_ci
874bf215546Sopenharmony_ci   if (pic->intra_matrix) {
875bf215546Sopenharmony_ci      result.load_intra_quantiser_matrix = 1;
876bf215546Sopenharmony_ci      for (i = 0; i < 64; ++i) {
877bf215546Sopenharmony_ci         result.intra_quantiser_matrix[i] = pic->intra_matrix[zscan[i]];
878bf215546Sopenharmony_ci      }
879bf215546Sopenharmony_ci   }
880bf215546Sopenharmony_ci   if (pic->non_intra_matrix) {
881bf215546Sopenharmony_ci      result.load_nonintra_quantiser_matrix = 1;
882bf215546Sopenharmony_ci      for (i = 0; i < 64; ++i) {
883bf215546Sopenharmony_ci         result.nonintra_quantiser_matrix[i] = pic->non_intra_matrix[zscan[i]];
884bf215546Sopenharmony_ci      }
885bf215546Sopenharmony_ci   }
886bf215546Sopenharmony_ci
887bf215546Sopenharmony_ci   result.profile_and_level_indication = 0;
888bf215546Sopenharmony_ci   result.chroma_format = 0x1;
889bf215546Sopenharmony_ci
890bf215546Sopenharmony_ci   result.picture_coding_type = pic->picture_coding_type;
891bf215546Sopenharmony_ci   result.f_code[0][0] = pic->f_code[0][0] + 1;
892bf215546Sopenharmony_ci   result.f_code[0][1] = pic->f_code[0][1] + 1;
893bf215546Sopenharmony_ci   result.f_code[1][0] = pic->f_code[1][0] + 1;
894bf215546Sopenharmony_ci   result.f_code[1][1] = pic->f_code[1][1] + 1;
895bf215546Sopenharmony_ci   result.intra_dc_precision = pic->intra_dc_precision;
896bf215546Sopenharmony_ci   result.pic_structure = pic->picture_structure;
897bf215546Sopenharmony_ci   result.top_field_first = pic->top_field_first;
898bf215546Sopenharmony_ci   result.frame_pred_frame_dct = pic->frame_pred_frame_dct;
899bf215546Sopenharmony_ci   result.concealment_motion_vectors = pic->concealment_motion_vectors;
900bf215546Sopenharmony_ci   result.q_scale_type = pic->q_scale_type;
901bf215546Sopenharmony_ci   result.intra_vlc_format = pic->intra_vlc_format;
902bf215546Sopenharmony_ci   result.alternate_scan = pic->alternate_scan;
903bf215546Sopenharmony_ci
904bf215546Sopenharmony_ci   return result;
905bf215546Sopenharmony_ci}
906bf215546Sopenharmony_ci
907bf215546Sopenharmony_ci/* get mpeg4 specific msg bits */
908bf215546Sopenharmony_cistatic struct ruvd_mpeg4 get_mpeg4_msg(struct ruvd_decoder *dec,
909bf215546Sopenharmony_ci                                       struct pipe_mpeg4_picture_desc *pic)
910bf215546Sopenharmony_ci{
911bf215546Sopenharmony_ci   struct ruvd_mpeg4 result;
912bf215546Sopenharmony_ci   unsigned i;
913bf215546Sopenharmony_ci
914bf215546Sopenharmony_ci   memset(&result, 0, sizeof(result));
915bf215546Sopenharmony_ci   result.decoded_pic_idx = dec->frame_number;
916bf215546Sopenharmony_ci   for (i = 0; i < 2; ++i)
917bf215546Sopenharmony_ci      result.ref_pic_idx[i] = get_ref_pic_idx(dec, pic->ref[i]);
918bf215546Sopenharmony_ci
919bf215546Sopenharmony_ci   result.variant_type = 0;
920bf215546Sopenharmony_ci   result.profile_and_level_indication = 0xF0; // ASP Level0
921bf215546Sopenharmony_ci
922bf215546Sopenharmony_ci   result.video_object_layer_verid = 0x5; // advanced simple
923bf215546Sopenharmony_ci   result.video_object_layer_shape = 0x0; // rectangular
924bf215546Sopenharmony_ci
925bf215546Sopenharmony_ci   result.video_object_layer_width = dec->base.width;
926bf215546Sopenharmony_ci   result.video_object_layer_height = dec->base.height;
927bf215546Sopenharmony_ci
928bf215546Sopenharmony_ci   result.vop_time_increment_resolution = pic->vop_time_increment_resolution;
929bf215546Sopenharmony_ci
930bf215546Sopenharmony_ci   result.flags |= pic->short_video_header << 0;
931bf215546Sopenharmony_ci   // result.flags |= obmc_disable << 1;
932bf215546Sopenharmony_ci   result.flags |= pic->interlaced << 2;
933bf215546Sopenharmony_ci   result.flags |= 1 << 3; // load_intra_quant_mat
934bf215546Sopenharmony_ci   result.flags |= 1 << 4; // load_nonintra_quant_mat
935bf215546Sopenharmony_ci   result.flags |= pic->quarter_sample << 5;
936bf215546Sopenharmony_ci   result.flags |= 1 << 6; // complexity_estimation_disable
937bf215546Sopenharmony_ci   result.flags |= pic->resync_marker_disable << 7;
938bf215546Sopenharmony_ci   // result.flags |= data_partitioned << 8;
939bf215546Sopenharmony_ci   // result.flags |= reversible_vlc << 9;
940bf215546Sopenharmony_ci   result.flags |= 0 << 10; // newpred_enable
941bf215546Sopenharmony_ci   result.flags |= 0 << 11; // reduced_resolution_vop_enable
942bf215546Sopenharmony_ci   // result.flags |= scalability << 12;
943bf215546Sopenharmony_ci   // result.flags |= is_object_layer_identifier << 13;
944bf215546Sopenharmony_ci   // result.flags |= fixed_vop_rate << 14;
945bf215546Sopenharmony_ci   // result.flags |= newpred_segment_type << 15;
946bf215546Sopenharmony_ci
947bf215546Sopenharmony_ci   result.quant_type = pic->quant_type;
948bf215546Sopenharmony_ci
949bf215546Sopenharmony_ci   for (i = 0; i < 64; ++i) {
950bf215546Sopenharmony_ci      result.intra_quant_mat[i] = pic->intra_matrix[vl_zscan_normal[i]];
951bf215546Sopenharmony_ci      result.nonintra_quant_mat[i] = pic->non_intra_matrix[vl_zscan_normal[i]];
952bf215546Sopenharmony_ci   }
953bf215546Sopenharmony_ci
954bf215546Sopenharmony_ci   /*
955bf215546Sopenharmony_ci   int32_t    trd [2]
956bf215546Sopenharmony_ci   int32_t    trb [2]
957bf215546Sopenharmony_ci   uint8_t    vop_coding_type
958bf215546Sopenharmony_ci   uint8_t    vop_fcode_forward
959bf215546Sopenharmony_ci   uint8_t    vop_fcode_backward
960bf215546Sopenharmony_ci   uint8_t    rounding_control
961bf215546Sopenharmony_ci   uint8_t    alternate_vertical_scan_flag
962bf215546Sopenharmony_ci   uint8_t    top_field_first
963bf215546Sopenharmony_ci   */
964bf215546Sopenharmony_ci
965bf215546Sopenharmony_ci   return result;
966bf215546Sopenharmony_ci}
967bf215546Sopenharmony_ci
968bf215546Sopenharmony_ci/**
969bf215546Sopenharmony_ci * destroy this video decoder
970bf215546Sopenharmony_ci */
971bf215546Sopenharmony_cistatic void ruvd_destroy(struct pipe_video_codec *decoder)
972bf215546Sopenharmony_ci{
973bf215546Sopenharmony_ci   struct ruvd_decoder *dec = (struct ruvd_decoder *)decoder;
974bf215546Sopenharmony_ci   unsigned i;
975bf215546Sopenharmony_ci
976bf215546Sopenharmony_ci   assert(decoder);
977bf215546Sopenharmony_ci
978bf215546Sopenharmony_ci   map_msg_fb_it_buf(dec);
979bf215546Sopenharmony_ci   dec->msg->size = sizeof(*dec->msg);
980bf215546Sopenharmony_ci   dec->msg->msg_type = RUVD_MSG_DESTROY;
981bf215546Sopenharmony_ci   dec->msg->stream_handle = dec->stream_handle;
982bf215546Sopenharmony_ci   send_msg_buf(dec);
983bf215546Sopenharmony_ci
984bf215546Sopenharmony_ci   flush(dec, 0);
985bf215546Sopenharmony_ci
986bf215546Sopenharmony_ci   dec->ws->cs_destroy(&dec->cs);
987bf215546Sopenharmony_ci
988bf215546Sopenharmony_ci   for (i = 0; i < NUM_BUFFERS; ++i) {
989bf215546Sopenharmony_ci      si_vid_destroy_buffer(&dec->msg_fb_it_buffers[i]);
990bf215546Sopenharmony_ci      si_vid_destroy_buffer(&dec->bs_buffers[i]);
991bf215546Sopenharmony_ci   }
992bf215546Sopenharmony_ci
993bf215546Sopenharmony_ci   si_vid_destroy_buffer(&dec->dpb);
994bf215546Sopenharmony_ci   si_vid_destroy_buffer(&dec->ctx);
995bf215546Sopenharmony_ci   si_vid_destroy_buffer(&dec->sessionctx);
996bf215546Sopenharmony_ci
997bf215546Sopenharmony_ci   FREE(dec);
998bf215546Sopenharmony_ci}
999bf215546Sopenharmony_ci
1000bf215546Sopenharmony_ci/**
1001bf215546Sopenharmony_ci * start decoding of a new frame
1002bf215546Sopenharmony_ci */
1003bf215546Sopenharmony_cistatic void ruvd_begin_frame(struct pipe_video_codec *decoder, struct pipe_video_buffer *target,
1004bf215546Sopenharmony_ci                             struct pipe_picture_desc *picture)
1005bf215546Sopenharmony_ci{
1006bf215546Sopenharmony_ci   struct ruvd_decoder *dec = (struct ruvd_decoder *)decoder;
1007bf215546Sopenharmony_ci   uintptr_t frame;
1008bf215546Sopenharmony_ci
1009bf215546Sopenharmony_ci   assert(decoder);
1010bf215546Sopenharmony_ci
1011bf215546Sopenharmony_ci   frame = ++dec->frame_number;
1012bf215546Sopenharmony_ci   vl_video_buffer_set_associated_data(target, decoder, (void *)frame,
1013bf215546Sopenharmony_ci                                       &ruvd_destroy_associated_data);
1014bf215546Sopenharmony_ci
1015bf215546Sopenharmony_ci   dec->bs_size = 0;
1016bf215546Sopenharmony_ci   dec->bs_ptr = dec->ws->buffer_map(dec->ws, dec->bs_buffers[dec->cur_buffer].res->buf, &dec->cs,
1017bf215546Sopenharmony_ci                                     PIPE_MAP_WRITE | RADEON_MAP_TEMPORARY);
1018bf215546Sopenharmony_ci}
1019bf215546Sopenharmony_ci
1020bf215546Sopenharmony_ci/**
1021bf215546Sopenharmony_ci * decode a macroblock
1022bf215546Sopenharmony_ci */
1023bf215546Sopenharmony_cistatic void ruvd_decode_macroblock(struct pipe_video_codec *decoder,
1024bf215546Sopenharmony_ci                                   struct pipe_video_buffer *target,
1025bf215546Sopenharmony_ci                                   struct pipe_picture_desc *picture,
1026bf215546Sopenharmony_ci                                   const struct pipe_macroblock *macroblocks,
1027bf215546Sopenharmony_ci                                   unsigned num_macroblocks)
1028bf215546Sopenharmony_ci{
1029bf215546Sopenharmony_ci   /* not supported (yet) */
1030bf215546Sopenharmony_ci   assert(0);
1031bf215546Sopenharmony_ci}
1032bf215546Sopenharmony_ci
1033bf215546Sopenharmony_ci/**
1034bf215546Sopenharmony_ci * decode a bitstream
1035bf215546Sopenharmony_ci */
1036bf215546Sopenharmony_cistatic void ruvd_decode_bitstream(struct pipe_video_codec *decoder,
1037bf215546Sopenharmony_ci                                  struct pipe_video_buffer *target,
1038bf215546Sopenharmony_ci                                  struct pipe_picture_desc *picture, unsigned num_buffers,
1039bf215546Sopenharmony_ci                                  const void *const *buffers, const unsigned *sizes)
1040bf215546Sopenharmony_ci{
1041bf215546Sopenharmony_ci   struct ruvd_decoder *dec = (struct ruvd_decoder *)decoder;
1042bf215546Sopenharmony_ci   unsigned i;
1043bf215546Sopenharmony_ci
1044bf215546Sopenharmony_ci   assert(decoder);
1045bf215546Sopenharmony_ci
1046bf215546Sopenharmony_ci   if (!dec->bs_ptr)
1047bf215546Sopenharmony_ci      return;
1048bf215546Sopenharmony_ci
1049bf215546Sopenharmony_ci   for (i = 0; i < num_buffers; ++i) {
1050bf215546Sopenharmony_ci      struct rvid_buffer *buf = &dec->bs_buffers[dec->cur_buffer];
1051bf215546Sopenharmony_ci      unsigned new_size = dec->bs_size + sizes[i];
1052bf215546Sopenharmony_ci
1053bf215546Sopenharmony_ci      if (new_size > buf->res->buf->size) {
1054bf215546Sopenharmony_ci         dec->ws->buffer_unmap(dec->ws, buf->res->buf);
1055bf215546Sopenharmony_ci         if (!si_vid_resize_buffer(dec->screen, &dec->cs, buf, new_size)) {
1056bf215546Sopenharmony_ci            RVID_ERR("Can't resize bitstream buffer!");
1057bf215546Sopenharmony_ci            return;
1058bf215546Sopenharmony_ci         }
1059bf215546Sopenharmony_ci
1060bf215546Sopenharmony_ci         dec->bs_ptr = dec->ws->buffer_map(dec->ws, buf->res->buf, &dec->cs,
1061bf215546Sopenharmony_ci                                           PIPE_MAP_WRITE | RADEON_MAP_TEMPORARY);
1062bf215546Sopenharmony_ci         if (!dec->bs_ptr)
1063bf215546Sopenharmony_ci            return;
1064bf215546Sopenharmony_ci
1065bf215546Sopenharmony_ci         dec->bs_ptr += dec->bs_size;
1066bf215546Sopenharmony_ci      }
1067bf215546Sopenharmony_ci
1068bf215546Sopenharmony_ci      memcpy(dec->bs_ptr, buffers[i], sizes[i]);
1069bf215546Sopenharmony_ci      dec->bs_size += sizes[i];
1070bf215546Sopenharmony_ci      dec->bs_ptr += sizes[i];
1071bf215546Sopenharmony_ci   }
1072bf215546Sopenharmony_ci}
1073bf215546Sopenharmony_ci
1074bf215546Sopenharmony_ci/**
1075bf215546Sopenharmony_ci * end decoding of the current frame
1076bf215546Sopenharmony_ci */
1077bf215546Sopenharmony_cistatic void ruvd_end_frame(struct pipe_video_codec *decoder, struct pipe_video_buffer *target,
1078bf215546Sopenharmony_ci                           struct pipe_picture_desc *picture)
1079bf215546Sopenharmony_ci{
1080bf215546Sopenharmony_ci   struct ruvd_decoder *dec = (struct ruvd_decoder *)decoder;
1081bf215546Sopenharmony_ci   struct pb_buffer *dt;
1082bf215546Sopenharmony_ci   struct rvid_buffer *msg_fb_it_buf, *bs_buf;
1083bf215546Sopenharmony_ci   unsigned bs_size;
1084bf215546Sopenharmony_ci
1085bf215546Sopenharmony_ci   assert(decoder);
1086bf215546Sopenharmony_ci
1087bf215546Sopenharmony_ci   if (!dec->bs_ptr)
1088bf215546Sopenharmony_ci      return;
1089bf215546Sopenharmony_ci
1090bf215546Sopenharmony_ci   msg_fb_it_buf = &dec->msg_fb_it_buffers[dec->cur_buffer];
1091bf215546Sopenharmony_ci   bs_buf = &dec->bs_buffers[dec->cur_buffer];
1092bf215546Sopenharmony_ci
1093bf215546Sopenharmony_ci   bs_size = align(dec->bs_size, 128);
1094bf215546Sopenharmony_ci   memset(dec->bs_ptr, 0, bs_size - dec->bs_size);
1095bf215546Sopenharmony_ci   dec->ws->buffer_unmap(dec->ws, bs_buf->res->buf);
1096bf215546Sopenharmony_ci
1097bf215546Sopenharmony_ci   map_msg_fb_it_buf(dec);
1098bf215546Sopenharmony_ci   dec->msg->size = sizeof(*dec->msg);
1099bf215546Sopenharmony_ci   dec->msg->msg_type = RUVD_MSG_DECODE;
1100bf215546Sopenharmony_ci   dec->msg->stream_handle = dec->stream_handle;
1101bf215546Sopenharmony_ci   dec->msg->status_report_feedback_number = dec->frame_number;
1102bf215546Sopenharmony_ci
1103bf215546Sopenharmony_ci   dec->msg->body.decode.stream_type = dec->stream_type;
1104bf215546Sopenharmony_ci   dec->msg->body.decode.decode_flags = 0x1;
1105bf215546Sopenharmony_ci   dec->msg->body.decode.width_in_samples = dec->base.width;
1106bf215546Sopenharmony_ci   dec->msg->body.decode.height_in_samples = dec->base.height;
1107bf215546Sopenharmony_ci
1108bf215546Sopenharmony_ci   if ((picture->profile == PIPE_VIDEO_PROFILE_VC1_SIMPLE) ||
1109bf215546Sopenharmony_ci       (picture->profile == PIPE_VIDEO_PROFILE_VC1_MAIN)) {
1110bf215546Sopenharmony_ci      dec->msg->body.decode.width_in_samples =
1111bf215546Sopenharmony_ci         align(dec->msg->body.decode.width_in_samples, 16) / 16;
1112bf215546Sopenharmony_ci      dec->msg->body.decode.height_in_samples =
1113bf215546Sopenharmony_ci         align(dec->msg->body.decode.height_in_samples, 16) / 16;
1114bf215546Sopenharmony_ci   }
1115bf215546Sopenharmony_ci
1116bf215546Sopenharmony_ci   if (dec->dpb.res)
1117bf215546Sopenharmony_ci      dec->msg->body.decode.dpb_size = dec->dpb.res->buf->size;
1118bf215546Sopenharmony_ci   dec->msg->body.decode.bsd_size = bs_size;
1119bf215546Sopenharmony_ci   dec->msg->body.decode.db_pitch = align(dec->base.width, get_db_pitch_alignment(dec));
1120bf215546Sopenharmony_ci
1121bf215546Sopenharmony_ci   if (dec->stream_type == RUVD_CODEC_H264_PERF &&
1122bf215546Sopenharmony_ci       ((struct si_screen *)dec->screen)->info.family >= CHIP_POLARIS10)
1123bf215546Sopenharmony_ci      dec->msg->body.decode.dpb_reserved = dec->ctx.res->buf->size;
1124bf215546Sopenharmony_ci
1125bf215546Sopenharmony_ci   dt = dec->set_dtb(dec->msg, (struct vl_video_buffer *)target);
1126bf215546Sopenharmony_ci   if (((struct si_screen *)dec->screen)->info.family >= CHIP_STONEY)
1127bf215546Sopenharmony_ci      dec->msg->body.decode.dt_wa_chroma_top_offset = dec->msg->body.decode.dt_pitch / 2;
1128bf215546Sopenharmony_ci
1129bf215546Sopenharmony_ci   switch (u_reduce_video_profile(picture->profile)) {
1130bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_MPEG4_AVC:
1131bf215546Sopenharmony_ci      dec->msg->body.decode.codec.h264 =
1132bf215546Sopenharmony_ci         get_h264_msg(dec, (struct pipe_h264_picture_desc *)picture);
1133bf215546Sopenharmony_ci      break;
1134bf215546Sopenharmony_ci
1135bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_HEVC:
1136bf215546Sopenharmony_ci      dec->msg->body.decode.codec.h265 =
1137bf215546Sopenharmony_ci         get_h265_msg(dec, target, (struct pipe_h265_picture_desc *)picture);
1138bf215546Sopenharmony_ci      if (dec->ctx.res == NULL) {
1139bf215546Sopenharmony_ci         unsigned ctx_size;
1140bf215546Sopenharmony_ci         if (dec->base.profile == PIPE_VIDEO_PROFILE_HEVC_MAIN_10)
1141bf215546Sopenharmony_ci            ctx_size = calc_ctx_size_h265_main10(dec, (struct pipe_h265_picture_desc *)picture);
1142bf215546Sopenharmony_ci         else
1143bf215546Sopenharmony_ci            ctx_size = calc_ctx_size_h265_main(dec);
1144bf215546Sopenharmony_ci         if (!si_vid_create_buffer(dec->screen, &dec->ctx, ctx_size, PIPE_USAGE_DEFAULT)) {
1145bf215546Sopenharmony_ci            RVID_ERR("Can't allocated context buffer.\n");
1146bf215546Sopenharmony_ci         }
1147bf215546Sopenharmony_ci         si_vid_clear_buffer(decoder->context, &dec->ctx);
1148bf215546Sopenharmony_ci      }
1149bf215546Sopenharmony_ci
1150bf215546Sopenharmony_ci      if (dec->ctx.res)
1151bf215546Sopenharmony_ci         dec->msg->body.decode.dpb_reserved = dec->ctx.res->buf->size;
1152bf215546Sopenharmony_ci      break;
1153bf215546Sopenharmony_ci
1154bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_VC1:
1155bf215546Sopenharmony_ci      dec->msg->body.decode.codec.vc1 = get_vc1_msg((struct pipe_vc1_picture_desc *)picture);
1156bf215546Sopenharmony_ci      break;
1157bf215546Sopenharmony_ci
1158bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_MPEG12:
1159bf215546Sopenharmony_ci      dec->msg->body.decode.codec.mpeg2 =
1160bf215546Sopenharmony_ci         get_mpeg2_msg(dec, (struct pipe_mpeg12_picture_desc *)picture);
1161bf215546Sopenharmony_ci      break;
1162bf215546Sopenharmony_ci
1163bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_MPEG4:
1164bf215546Sopenharmony_ci      dec->msg->body.decode.codec.mpeg4 =
1165bf215546Sopenharmony_ci         get_mpeg4_msg(dec, (struct pipe_mpeg4_picture_desc *)picture);
1166bf215546Sopenharmony_ci      break;
1167bf215546Sopenharmony_ci
1168bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_JPEG:
1169bf215546Sopenharmony_ci      break;
1170bf215546Sopenharmony_ci
1171bf215546Sopenharmony_ci   default:
1172bf215546Sopenharmony_ci      assert(0);
1173bf215546Sopenharmony_ci      return;
1174bf215546Sopenharmony_ci   }
1175bf215546Sopenharmony_ci
1176bf215546Sopenharmony_ci   dec->msg->body.decode.db_surf_tile_config = dec->msg->body.decode.dt_surf_tile_config;
1177bf215546Sopenharmony_ci   dec->msg->body.decode.extension_support = 0x1;
1178bf215546Sopenharmony_ci
1179bf215546Sopenharmony_ci   /* set at least the feedback buffer size */
1180bf215546Sopenharmony_ci   dec->fb[0] = dec->fb_size;
1181bf215546Sopenharmony_ci
1182bf215546Sopenharmony_ci   send_msg_buf(dec);
1183bf215546Sopenharmony_ci
1184bf215546Sopenharmony_ci   if (dec->dpb.res)
1185bf215546Sopenharmony_ci      send_cmd(dec, RUVD_CMD_DPB_BUFFER, dec->dpb.res->buf, 0, RADEON_USAGE_READWRITE,
1186bf215546Sopenharmony_ci               RADEON_DOMAIN_VRAM);
1187bf215546Sopenharmony_ci
1188bf215546Sopenharmony_ci   if (dec->ctx.res)
1189bf215546Sopenharmony_ci      send_cmd(dec, RUVD_CMD_CONTEXT_BUFFER, dec->ctx.res->buf, 0, RADEON_USAGE_READWRITE,
1190bf215546Sopenharmony_ci               RADEON_DOMAIN_VRAM);
1191bf215546Sopenharmony_ci   send_cmd(dec, RUVD_CMD_BITSTREAM_BUFFER, bs_buf->res->buf, 0, RADEON_USAGE_READ,
1192bf215546Sopenharmony_ci            RADEON_DOMAIN_GTT);
1193bf215546Sopenharmony_ci   send_cmd(dec, RUVD_CMD_DECODING_TARGET_BUFFER, dt, 0, RADEON_USAGE_WRITE, RADEON_DOMAIN_VRAM);
1194bf215546Sopenharmony_ci   send_cmd(dec, RUVD_CMD_FEEDBACK_BUFFER, msg_fb_it_buf->res->buf, FB_BUFFER_OFFSET,
1195bf215546Sopenharmony_ci            RADEON_USAGE_WRITE, RADEON_DOMAIN_GTT);
1196bf215546Sopenharmony_ci   if (have_it(dec))
1197bf215546Sopenharmony_ci      send_cmd(dec, RUVD_CMD_ITSCALING_TABLE_BUFFER, msg_fb_it_buf->res->buf,
1198bf215546Sopenharmony_ci               FB_BUFFER_OFFSET + dec->fb_size, RADEON_USAGE_READ, RADEON_DOMAIN_GTT);
1199bf215546Sopenharmony_ci   set_reg(dec, dec->reg.cntl, 1);
1200bf215546Sopenharmony_ci
1201bf215546Sopenharmony_ci   flush(dec, PIPE_FLUSH_ASYNC);
1202bf215546Sopenharmony_ci   next_buffer(dec);
1203bf215546Sopenharmony_ci}
1204bf215546Sopenharmony_ci
1205bf215546Sopenharmony_ci/**
1206bf215546Sopenharmony_ci * flush any outstanding command buffers to the hardware
1207bf215546Sopenharmony_ci */
1208bf215546Sopenharmony_cistatic void ruvd_flush(struct pipe_video_codec *decoder)
1209bf215546Sopenharmony_ci{
1210bf215546Sopenharmony_ci}
1211bf215546Sopenharmony_ci
1212bf215546Sopenharmony_ci/**
1213bf215546Sopenharmony_ci * create and UVD decoder
1214bf215546Sopenharmony_ci */
1215bf215546Sopenharmony_cistruct pipe_video_codec *si_common_uvd_create_decoder(struct pipe_context *context,
1216bf215546Sopenharmony_ci                                                      const struct pipe_video_codec *templ,
1217bf215546Sopenharmony_ci                                                      ruvd_set_dtb set_dtb)
1218bf215546Sopenharmony_ci{
1219bf215546Sopenharmony_ci   struct si_context *sctx = (struct si_context *)context;
1220bf215546Sopenharmony_ci   struct radeon_winsys *ws = sctx->ws;
1221bf215546Sopenharmony_ci   unsigned dpb_size;
1222bf215546Sopenharmony_ci   unsigned width = templ->width, height = templ->height;
1223bf215546Sopenharmony_ci   unsigned bs_buf_size;
1224bf215546Sopenharmony_ci   struct ruvd_decoder *dec;
1225bf215546Sopenharmony_ci   int r, i;
1226bf215546Sopenharmony_ci
1227bf215546Sopenharmony_ci   switch (u_reduce_video_profile(templ->profile)) {
1228bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_MPEG12:
1229bf215546Sopenharmony_ci      if (templ->entrypoint > PIPE_VIDEO_ENTRYPOINT_BITSTREAM)
1230bf215546Sopenharmony_ci         return vl_create_mpeg12_decoder(context, templ);
1231bf215546Sopenharmony_ci
1232bf215546Sopenharmony_ci      FALLTHROUGH;
1233bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_MPEG4:
1234bf215546Sopenharmony_ci      width = align(width, VL_MACROBLOCK_WIDTH);
1235bf215546Sopenharmony_ci      height = align(height, VL_MACROBLOCK_HEIGHT);
1236bf215546Sopenharmony_ci      break;
1237bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_MPEG4_AVC:
1238bf215546Sopenharmony_ci      width = align(width, VL_MACROBLOCK_WIDTH);
1239bf215546Sopenharmony_ci      height = align(height, VL_MACROBLOCK_HEIGHT);
1240bf215546Sopenharmony_ci      break;
1241bf215546Sopenharmony_ci
1242bf215546Sopenharmony_ci   default:
1243bf215546Sopenharmony_ci      break;
1244bf215546Sopenharmony_ci   }
1245bf215546Sopenharmony_ci
1246bf215546Sopenharmony_ci   dec = CALLOC_STRUCT(ruvd_decoder);
1247bf215546Sopenharmony_ci
1248bf215546Sopenharmony_ci   if (!dec)
1249bf215546Sopenharmony_ci      return NULL;
1250bf215546Sopenharmony_ci
1251bf215546Sopenharmony_ci   if (!sctx->screen->info.is_amdgpu)
1252bf215546Sopenharmony_ci      dec->use_legacy = true;
1253bf215546Sopenharmony_ci
1254bf215546Sopenharmony_ci   dec->base = *templ;
1255bf215546Sopenharmony_ci   dec->base.context = context;
1256bf215546Sopenharmony_ci   dec->base.width = width;
1257bf215546Sopenharmony_ci   dec->base.height = height;
1258bf215546Sopenharmony_ci
1259bf215546Sopenharmony_ci   dec->base.destroy = ruvd_destroy;
1260bf215546Sopenharmony_ci   dec->base.begin_frame = ruvd_begin_frame;
1261bf215546Sopenharmony_ci   dec->base.decode_macroblock = ruvd_decode_macroblock;
1262bf215546Sopenharmony_ci   dec->base.decode_bitstream = ruvd_decode_bitstream;
1263bf215546Sopenharmony_ci   dec->base.end_frame = ruvd_end_frame;
1264bf215546Sopenharmony_ci   dec->base.flush = ruvd_flush;
1265bf215546Sopenharmony_ci
1266bf215546Sopenharmony_ci   dec->stream_type = profile2stream_type(dec, sctx->family);
1267bf215546Sopenharmony_ci   dec->set_dtb = set_dtb;
1268bf215546Sopenharmony_ci   dec->stream_handle = si_vid_alloc_stream_handle();
1269bf215546Sopenharmony_ci   dec->screen = context->screen;
1270bf215546Sopenharmony_ci   dec->ws = ws;
1271bf215546Sopenharmony_ci
1272bf215546Sopenharmony_ci   if (!ws->cs_create(&dec->cs, sctx->ctx, AMD_IP_UVD, NULL, NULL, false)) {
1273bf215546Sopenharmony_ci      RVID_ERR("Can't get command submission context.\n");
1274bf215546Sopenharmony_ci      goto error;
1275bf215546Sopenharmony_ci   }
1276bf215546Sopenharmony_ci
1277bf215546Sopenharmony_ci   for (i = 0; i < 16; i++)
1278bf215546Sopenharmony_ci      dec->render_pic_list[i] = NULL;
1279bf215546Sopenharmony_ci   dec->fb_size = (sctx->family == CHIP_TONGA) ? FB_BUFFER_SIZE_TONGA : FB_BUFFER_SIZE;
1280bf215546Sopenharmony_ci   bs_buf_size = width * height * (512 / (16 * 16));
1281bf215546Sopenharmony_ci   for (i = 0; i < NUM_BUFFERS; ++i) {
1282bf215546Sopenharmony_ci      unsigned msg_fb_it_size = FB_BUFFER_OFFSET + dec->fb_size;
1283bf215546Sopenharmony_ci      STATIC_ASSERT(sizeof(struct ruvd_msg) <= FB_BUFFER_OFFSET);
1284bf215546Sopenharmony_ci      if (have_it(dec))
1285bf215546Sopenharmony_ci         msg_fb_it_size += IT_SCALING_TABLE_SIZE;
1286bf215546Sopenharmony_ci      if (!si_vid_create_buffer(dec->screen, &dec->msg_fb_it_buffers[i], msg_fb_it_size,
1287bf215546Sopenharmony_ci                                PIPE_USAGE_STAGING)) {
1288bf215546Sopenharmony_ci         RVID_ERR("Can't allocated message buffers.\n");
1289bf215546Sopenharmony_ci         goto error;
1290bf215546Sopenharmony_ci      }
1291bf215546Sopenharmony_ci
1292bf215546Sopenharmony_ci      if (!si_vid_create_buffer(dec->screen, &dec->bs_buffers[i], bs_buf_size,
1293bf215546Sopenharmony_ci                                PIPE_USAGE_STAGING)) {
1294bf215546Sopenharmony_ci         RVID_ERR("Can't allocated bitstream buffers.\n");
1295bf215546Sopenharmony_ci         goto error;
1296bf215546Sopenharmony_ci      }
1297bf215546Sopenharmony_ci
1298bf215546Sopenharmony_ci      si_vid_clear_buffer(context, &dec->msg_fb_it_buffers[i]);
1299bf215546Sopenharmony_ci      si_vid_clear_buffer(context, &dec->bs_buffers[i]);
1300bf215546Sopenharmony_ci   }
1301bf215546Sopenharmony_ci
1302bf215546Sopenharmony_ci   dpb_size = calc_dpb_size(dec);
1303bf215546Sopenharmony_ci   if (dpb_size) {
1304bf215546Sopenharmony_ci      if (!si_vid_create_buffer(dec->screen, &dec->dpb, dpb_size, PIPE_USAGE_DEFAULT)) {
1305bf215546Sopenharmony_ci         RVID_ERR("Can't allocated dpb.\n");
1306bf215546Sopenharmony_ci         goto error;
1307bf215546Sopenharmony_ci      }
1308bf215546Sopenharmony_ci      si_vid_clear_buffer(context, &dec->dpb);
1309bf215546Sopenharmony_ci   }
1310bf215546Sopenharmony_ci
1311bf215546Sopenharmony_ci   if (dec->stream_type == RUVD_CODEC_H264_PERF && sctx->family >= CHIP_POLARIS10) {
1312bf215546Sopenharmony_ci      unsigned ctx_size = calc_ctx_size_h264_perf(dec);
1313bf215546Sopenharmony_ci      if (!si_vid_create_buffer(dec->screen, &dec->ctx, ctx_size, PIPE_USAGE_DEFAULT)) {
1314bf215546Sopenharmony_ci         RVID_ERR("Can't allocated context buffer.\n");
1315bf215546Sopenharmony_ci         goto error;
1316bf215546Sopenharmony_ci      }
1317bf215546Sopenharmony_ci      si_vid_clear_buffer(context, &dec->ctx);
1318bf215546Sopenharmony_ci   }
1319bf215546Sopenharmony_ci
1320bf215546Sopenharmony_ci   if (sctx->family >= CHIP_POLARIS10) {
1321bf215546Sopenharmony_ci      if (!si_vid_create_buffer(dec->screen, &dec->sessionctx, UVD_SESSION_CONTEXT_SIZE,
1322bf215546Sopenharmony_ci                                PIPE_USAGE_DEFAULT)) {
1323bf215546Sopenharmony_ci         RVID_ERR("Can't allocated session ctx.\n");
1324bf215546Sopenharmony_ci         goto error;
1325bf215546Sopenharmony_ci      }
1326bf215546Sopenharmony_ci      si_vid_clear_buffer(context, &dec->sessionctx);
1327bf215546Sopenharmony_ci   }
1328bf215546Sopenharmony_ci
1329bf215546Sopenharmony_ci   if (sctx->family >= CHIP_VEGA10) {
1330bf215546Sopenharmony_ci      dec->reg.data0 = RUVD_GPCOM_VCPU_DATA0_SOC15;
1331bf215546Sopenharmony_ci      dec->reg.data1 = RUVD_GPCOM_VCPU_DATA1_SOC15;
1332bf215546Sopenharmony_ci      dec->reg.cmd = RUVD_GPCOM_VCPU_CMD_SOC15;
1333bf215546Sopenharmony_ci      dec->reg.cntl = RUVD_ENGINE_CNTL_SOC15;
1334bf215546Sopenharmony_ci   } else {
1335bf215546Sopenharmony_ci      dec->reg.data0 = RUVD_GPCOM_VCPU_DATA0;
1336bf215546Sopenharmony_ci      dec->reg.data1 = RUVD_GPCOM_VCPU_DATA1;
1337bf215546Sopenharmony_ci      dec->reg.cmd = RUVD_GPCOM_VCPU_CMD;
1338bf215546Sopenharmony_ci      dec->reg.cntl = RUVD_ENGINE_CNTL;
1339bf215546Sopenharmony_ci   }
1340bf215546Sopenharmony_ci
1341bf215546Sopenharmony_ci   map_msg_fb_it_buf(dec);
1342bf215546Sopenharmony_ci   dec->msg->size = sizeof(*dec->msg);
1343bf215546Sopenharmony_ci   dec->msg->msg_type = RUVD_MSG_CREATE;
1344bf215546Sopenharmony_ci   dec->msg->stream_handle = dec->stream_handle;
1345bf215546Sopenharmony_ci   dec->msg->body.create.stream_type = dec->stream_type;
1346bf215546Sopenharmony_ci   dec->msg->body.create.width_in_samples = dec->base.width;
1347bf215546Sopenharmony_ci   dec->msg->body.create.height_in_samples = dec->base.height;
1348bf215546Sopenharmony_ci   dec->msg->body.create.dpb_size = dpb_size;
1349bf215546Sopenharmony_ci   send_msg_buf(dec);
1350bf215546Sopenharmony_ci   r = flush(dec, 0);
1351bf215546Sopenharmony_ci   if (r)
1352bf215546Sopenharmony_ci      goto error;
1353bf215546Sopenharmony_ci
1354bf215546Sopenharmony_ci   next_buffer(dec);
1355bf215546Sopenharmony_ci
1356bf215546Sopenharmony_ci   return &dec->base;
1357bf215546Sopenharmony_ci
1358bf215546Sopenharmony_cierror:
1359bf215546Sopenharmony_ci   dec->ws->cs_destroy(&dec->cs);
1360bf215546Sopenharmony_ci
1361bf215546Sopenharmony_ci   for (i = 0; i < NUM_BUFFERS; ++i) {
1362bf215546Sopenharmony_ci      si_vid_destroy_buffer(&dec->msg_fb_it_buffers[i]);
1363bf215546Sopenharmony_ci      si_vid_destroy_buffer(&dec->bs_buffers[i]);
1364bf215546Sopenharmony_ci   }
1365bf215546Sopenharmony_ci
1366bf215546Sopenharmony_ci   si_vid_destroy_buffer(&dec->dpb);
1367bf215546Sopenharmony_ci   si_vid_destroy_buffer(&dec->ctx);
1368bf215546Sopenharmony_ci   si_vid_destroy_buffer(&dec->sessionctx);
1369bf215546Sopenharmony_ci
1370bf215546Sopenharmony_ci   FREE(dec);
1371bf215546Sopenharmony_ci
1372bf215546Sopenharmony_ci   return NULL;
1373bf215546Sopenharmony_ci}
1374bf215546Sopenharmony_ci
1375bf215546Sopenharmony_ci/* calculate top/bottom offset */
1376bf215546Sopenharmony_cistatic unsigned texture_offset(struct radeon_surf *surface, unsigned layer,
1377bf215546Sopenharmony_ci                               enum ruvd_surface_type type)
1378bf215546Sopenharmony_ci{
1379bf215546Sopenharmony_ci   switch (type) {
1380bf215546Sopenharmony_ci   default:
1381bf215546Sopenharmony_ci   case RUVD_SURFACE_TYPE_LEGACY:
1382bf215546Sopenharmony_ci      return (uint64_t)surface->u.legacy.level[0].offset_256B * 256 +
1383bf215546Sopenharmony_ci             layer * (uint64_t)surface->u.legacy.level[0].slice_size_dw * 4;
1384bf215546Sopenharmony_ci      break;
1385bf215546Sopenharmony_ci   case RUVD_SURFACE_TYPE_GFX9:
1386bf215546Sopenharmony_ci      return surface->u.gfx9.surf_offset + layer * surface->u.gfx9.surf_slice_size;
1387bf215546Sopenharmony_ci      break;
1388bf215546Sopenharmony_ci   }
1389bf215546Sopenharmony_ci}
1390bf215546Sopenharmony_ci
1391bf215546Sopenharmony_ci/* hw encode the aspect of macro tiles */
1392bf215546Sopenharmony_cistatic unsigned macro_tile_aspect(unsigned macro_tile_aspect)
1393bf215546Sopenharmony_ci{
1394bf215546Sopenharmony_ci   switch (macro_tile_aspect) {
1395bf215546Sopenharmony_ci   default:
1396bf215546Sopenharmony_ci   case 1:
1397bf215546Sopenharmony_ci      macro_tile_aspect = 0;
1398bf215546Sopenharmony_ci      break;
1399bf215546Sopenharmony_ci   case 2:
1400bf215546Sopenharmony_ci      macro_tile_aspect = 1;
1401bf215546Sopenharmony_ci      break;
1402bf215546Sopenharmony_ci   case 4:
1403bf215546Sopenharmony_ci      macro_tile_aspect = 2;
1404bf215546Sopenharmony_ci      break;
1405bf215546Sopenharmony_ci   case 8:
1406bf215546Sopenharmony_ci      macro_tile_aspect = 3;
1407bf215546Sopenharmony_ci      break;
1408bf215546Sopenharmony_ci   }
1409bf215546Sopenharmony_ci   return macro_tile_aspect;
1410bf215546Sopenharmony_ci}
1411bf215546Sopenharmony_ci
1412bf215546Sopenharmony_ci/* hw encode the bank width and height */
1413bf215546Sopenharmony_cistatic unsigned bank_wh(unsigned bankwh)
1414bf215546Sopenharmony_ci{
1415bf215546Sopenharmony_ci   switch (bankwh) {
1416bf215546Sopenharmony_ci   default:
1417bf215546Sopenharmony_ci   case 1:
1418bf215546Sopenharmony_ci      bankwh = 0;
1419bf215546Sopenharmony_ci      break;
1420bf215546Sopenharmony_ci   case 2:
1421bf215546Sopenharmony_ci      bankwh = 1;
1422bf215546Sopenharmony_ci      break;
1423bf215546Sopenharmony_ci   case 4:
1424bf215546Sopenharmony_ci      bankwh = 2;
1425bf215546Sopenharmony_ci      break;
1426bf215546Sopenharmony_ci   case 8:
1427bf215546Sopenharmony_ci      bankwh = 3;
1428bf215546Sopenharmony_ci      break;
1429bf215546Sopenharmony_ci   }
1430bf215546Sopenharmony_ci   return bankwh;
1431bf215546Sopenharmony_ci}
1432bf215546Sopenharmony_ci
1433bf215546Sopenharmony_ci/**
1434bf215546Sopenharmony_ci * fill decoding target field from the luma and chroma surfaces
1435bf215546Sopenharmony_ci */
1436bf215546Sopenharmony_civoid si_uvd_set_dt_surfaces(struct ruvd_msg *msg, struct radeon_surf *luma,
1437bf215546Sopenharmony_ci                            struct radeon_surf *chroma, enum ruvd_surface_type type)
1438bf215546Sopenharmony_ci{
1439bf215546Sopenharmony_ci   switch (type) {
1440bf215546Sopenharmony_ci   default:
1441bf215546Sopenharmony_ci   case RUVD_SURFACE_TYPE_LEGACY:
1442bf215546Sopenharmony_ci      msg->body.decode.dt_pitch = luma->u.legacy.level[0].nblk_x * luma->blk_w;
1443bf215546Sopenharmony_ci      switch (luma->u.legacy.level[0].mode) {
1444bf215546Sopenharmony_ci      case RADEON_SURF_MODE_LINEAR_ALIGNED:
1445bf215546Sopenharmony_ci         msg->body.decode.dt_tiling_mode = RUVD_TILE_LINEAR;
1446bf215546Sopenharmony_ci         msg->body.decode.dt_array_mode = RUVD_ARRAY_MODE_LINEAR;
1447bf215546Sopenharmony_ci         break;
1448bf215546Sopenharmony_ci      case RADEON_SURF_MODE_1D:
1449bf215546Sopenharmony_ci         msg->body.decode.dt_tiling_mode = RUVD_TILE_8X8;
1450bf215546Sopenharmony_ci         msg->body.decode.dt_array_mode = RUVD_ARRAY_MODE_1D_THIN;
1451bf215546Sopenharmony_ci         break;
1452bf215546Sopenharmony_ci      case RADEON_SURF_MODE_2D:
1453bf215546Sopenharmony_ci         msg->body.decode.dt_tiling_mode = RUVD_TILE_8X8;
1454bf215546Sopenharmony_ci         msg->body.decode.dt_array_mode = RUVD_ARRAY_MODE_2D_THIN;
1455bf215546Sopenharmony_ci         break;
1456bf215546Sopenharmony_ci      default:
1457bf215546Sopenharmony_ci         assert(0);
1458bf215546Sopenharmony_ci         break;
1459bf215546Sopenharmony_ci      }
1460bf215546Sopenharmony_ci
1461bf215546Sopenharmony_ci      msg->body.decode.dt_luma_top_offset = texture_offset(luma, 0, type);
1462bf215546Sopenharmony_ci      if (chroma)
1463bf215546Sopenharmony_ci         msg->body.decode.dt_chroma_top_offset = texture_offset(chroma, 0, type);
1464bf215546Sopenharmony_ci      if (msg->body.decode.dt_field_mode) {
1465bf215546Sopenharmony_ci         msg->body.decode.dt_luma_bottom_offset = texture_offset(luma, 1, type);
1466bf215546Sopenharmony_ci         if (chroma)
1467bf215546Sopenharmony_ci            msg->body.decode.dt_chroma_bottom_offset = texture_offset(chroma, 1, type);
1468bf215546Sopenharmony_ci      } else {
1469bf215546Sopenharmony_ci         msg->body.decode.dt_luma_bottom_offset = msg->body.decode.dt_luma_top_offset;
1470bf215546Sopenharmony_ci         msg->body.decode.dt_chroma_bottom_offset = msg->body.decode.dt_chroma_top_offset;
1471bf215546Sopenharmony_ci      }
1472bf215546Sopenharmony_ci
1473bf215546Sopenharmony_ci      if (chroma) {
1474bf215546Sopenharmony_ci         assert(luma->u.legacy.bankw == chroma->u.legacy.bankw);
1475bf215546Sopenharmony_ci         assert(luma->u.legacy.bankh == chroma->u.legacy.bankh);
1476bf215546Sopenharmony_ci         assert(luma->u.legacy.mtilea == chroma->u.legacy.mtilea);
1477bf215546Sopenharmony_ci      }
1478bf215546Sopenharmony_ci
1479bf215546Sopenharmony_ci      msg->body.decode.dt_surf_tile_config |= RUVD_BANK_WIDTH(bank_wh(luma->u.legacy.bankw));
1480bf215546Sopenharmony_ci      msg->body.decode.dt_surf_tile_config |= RUVD_BANK_HEIGHT(bank_wh(luma->u.legacy.bankh));
1481bf215546Sopenharmony_ci      msg->body.decode.dt_surf_tile_config |=
1482bf215546Sopenharmony_ci         RUVD_MACRO_TILE_ASPECT_RATIO(macro_tile_aspect(luma->u.legacy.mtilea));
1483bf215546Sopenharmony_ci      break;
1484bf215546Sopenharmony_ci   case RUVD_SURFACE_TYPE_GFX9:
1485bf215546Sopenharmony_ci      msg->body.decode.dt_pitch = luma->u.gfx9.surf_pitch * luma->blk_w;
1486bf215546Sopenharmony_ci      /* SWIZZLE LINEAR MODE */
1487bf215546Sopenharmony_ci      msg->body.decode.dt_tiling_mode = RUVD_TILE_LINEAR;
1488bf215546Sopenharmony_ci      msg->body.decode.dt_array_mode = RUVD_ARRAY_MODE_LINEAR;
1489bf215546Sopenharmony_ci      msg->body.decode.dt_luma_top_offset = texture_offset(luma, 0, type);
1490bf215546Sopenharmony_ci      msg->body.decode.dt_chroma_top_offset = texture_offset(chroma, 0, type);
1491bf215546Sopenharmony_ci      if (msg->body.decode.dt_field_mode) {
1492bf215546Sopenharmony_ci         msg->body.decode.dt_luma_bottom_offset = texture_offset(luma, 1, type);
1493bf215546Sopenharmony_ci         msg->body.decode.dt_chroma_bottom_offset = texture_offset(chroma, 1, type);
1494bf215546Sopenharmony_ci      } else {
1495bf215546Sopenharmony_ci         msg->body.decode.dt_luma_bottom_offset = msg->body.decode.dt_luma_top_offset;
1496bf215546Sopenharmony_ci         msg->body.decode.dt_chroma_bottom_offset = msg->body.decode.dt_chroma_top_offset;
1497bf215546Sopenharmony_ci      }
1498bf215546Sopenharmony_ci      msg->body.decode.dt_surf_tile_config = 0;
1499bf215546Sopenharmony_ci      break;
1500bf215546Sopenharmony_ci   }
1501bf215546Sopenharmony_ci}
1502