1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2011 Maarten Lankhorst
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
12bf215546Sopenharmony_ci * all copies or substantial portions of the Software.
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18bf215546Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20bf215546Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
21bf215546Sopenharmony_ci */
22bf215546Sopenharmony_ci
23bf215546Sopenharmony_ci#include "vl/vl_decoder.h"
24bf215546Sopenharmony_ci#include "vl/vl_video_buffer.h"
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "nouveau_screen.h"
27bf215546Sopenharmony_ci#include "nouveau_context.h"
28bf215546Sopenharmony_ci#include "nouveau_video.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "nouveau_buffer.h"
31bf215546Sopenharmony_ci#include "util/u_video.h"
32bf215546Sopenharmony_ci#include "util/format/u_format.h"
33bf215546Sopenharmony_ci#include "util/u_sampler.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_cistatic int
36bf215546Sopenharmony_cinouveau_vpe_init(struct nouveau_decoder *dec) {
37bf215546Sopenharmony_ci   int ret;
38bf215546Sopenharmony_ci   if (dec->cmds)
39bf215546Sopenharmony_ci      return 0;
40bf215546Sopenharmony_ci   ret = nouveau_bo_map(dec->cmd_bo, NOUVEAU_BO_RDWR, dec->client);
41bf215546Sopenharmony_ci   if (ret) {
42bf215546Sopenharmony_ci      debug_printf("Mapping cmd bo: %s\n", strerror(-ret));
43bf215546Sopenharmony_ci      return ret;
44bf215546Sopenharmony_ci   }
45bf215546Sopenharmony_ci   ret = nouveau_bo_map(dec->data_bo, NOUVEAU_BO_RDWR, dec->client);
46bf215546Sopenharmony_ci   if (ret) {
47bf215546Sopenharmony_ci      debug_printf("Mapping data bo: %s\n", strerror(-ret));
48bf215546Sopenharmony_ci      return ret;
49bf215546Sopenharmony_ci   }
50bf215546Sopenharmony_ci   dec->cmds = dec->cmd_bo->map;
51bf215546Sopenharmony_ci   dec->data = dec->data_bo->map;
52bf215546Sopenharmony_ci   return ret;
53bf215546Sopenharmony_ci}
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_cistatic void
56bf215546Sopenharmony_cinouveau_vpe_synch(struct nouveau_decoder *dec) {
57bf215546Sopenharmony_ci   struct nouveau_pushbuf *push = dec->push;
58bf215546Sopenharmony_ci#if 0
59bf215546Sopenharmony_ci   if (dec->fence_map) {
60bf215546Sopenharmony_ci      BEGIN_NV04(push, NV84_MPEG(QUERY_COUNTER), 1);
61bf215546Sopenharmony_ci      PUSH_DATA (push, ++dec->fence_seq);
62bf215546Sopenharmony_ci      PUSH_KICK (push);
63bf215546Sopenharmony_ci      while (dec->fence_map[0] != dec->fence_seq)
64bf215546Sopenharmony_ci         usleep(1000);
65bf215546Sopenharmony_ci   } else
66bf215546Sopenharmony_ci#endif
67bf215546Sopenharmony_ci      PUSH_KICK(push);
68bf215546Sopenharmony_ci}
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_cistatic void
71bf215546Sopenharmony_cinouveau_vpe_fini(struct nouveau_decoder *dec) {
72bf215546Sopenharmony_ci   struct nouveau_pushbuf *push = dec->push;
73bf215546Sopenharmony_ci   if (!dec->cmds)
74bf215546Sopenharmony_ci      return;
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci   nouveau_pushbuf_space(push, 16, 2, 0);
77bf215546Sopenharmony_ci   nouveau_bufctx_reset(dec->bufctx, NV31_VIDEO_BIND_CMD);
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ci#define BCTX_ARGS dec->bufctx, NV31_VIDEO_BIND_CMD, NOUVEAU_BO_RD
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   BEGIN_NV04(push, NV31_MPEG(CMD_OFFSET), 2);
82bf215546Sopenharmony_ci   PUSH_MTHDl(push, NV31_MPEG(CMD_OFFSET), dec->cmd_bo, 0, BCTX_ARGS);
83bf215546Sopenharmony_ci   PUSH_DATA (push, dec->ofs * 4);
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci   BEGIN_NV04(push, NV31_MPEG(DATA_OFFSET), 2);
86bf215546Sopenharmony_ci   PUSH_MTHDl(push, NV31_MPEG(DATA_OFFSET), dec->data_bo, 0, BCTX_ARGS);
87bf215546Sopenharmony_ci   PUSH_DATA (push, dec->data_pos * 4);
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci#undef BCTX_ARGS
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci   if (unlikely(nouveau_pushbuf_validate(dec->push)))
92bf215546Sopenharmony_ci      return;
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci   BEGIN_NV04(push, NV31_MPEG(EXEC), 1);
95bf215546Sopenharmony_ci   PUSH_DATA (push, 1);
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci   nouveau_vpe_synch(dec);
98bf215546Sopenharmony_ci   dec->ofs = dec->data_pos = dec->num_surfaces = 0;
99bf215546Sopenharmony_ci   dec->cmds = dec->data = NULL;
100bf215546Sopenharmony_ci   dec->current = dec->future = dec->past = 8;
101bf215546Sopenharmony_ci}
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_cistatic inline void
104bf215546Sopenharmony_cinouveau_vpe_mb_dct_blocks(struct nouveau_decoder *dec, const struct pipe_mpeg12_macroblock *mb)
105bf215546Sopenharmony_ci{
106bf215546Sopenharmony_ci   int cbb;
107bf215546Sopenharmony_ci   unsigned cbp = mb->coded_block_pattern;
108bf215546Sopenharmony_ci   short *db = mb->blocks;
109bf215546Sopenharmony_ci   for (cbb = 0x20; cbb > 0; cbb >>= 1) {
110bf215546Sopenharmony_ci      if (cbb & cbp) {
111bf215546Sopenharmony_ci         int i, found = 0;
112bf215546Sopenharmony_ci         for (i = 0; i < 64; ++i) {
113bf215546Sopenharmony_ci            if (!db[i]) continue;
114bf215546Sopenharmony_ci            dec->data[dec->data_pos++] = (db[i] << 16) | (i * 2);
115bf215546Sopenharmony_ci            found = 1;
116bf215546Sopenharmony_ci         }
117bf215546Sopenharmony_ci         if (found)
118bf215546Sopenharmony_ci            dec->data[dec->data_pos - 1] |= 1;
119bf215546Sopenharmony_ci         else
120bf215546Sopenharmony_ci            dec->data[dec->data_pos++] = 1;
121bf215546Sopenharmony_ci         db += 64;
122bf215546Sopenharmony_ci      } else if (mb->macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA) {
123bf215546Sopenharmony_ci         dec->data[dec->data_pos++] = 1;
124bf215546Sopenharmony_ci      }
125bf215546Sopenharmony_ci   }
126bf215546Sopenharmony_ci}
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_cistatic inline void
129bf215546Sopenharmony_cinouveau_vpe_mb_data_blocks(struct nouveau_decoder *dec, const struct pipe_mpeg12_macroblock *mb)
130bf215546Sopenharmony_ci{
131bf215546Sopenharmony_ci   int cbb;
132bf215546Sopenharmony_ci   unsigned cbp = mb->coded_block_pattern;
133bf215546Sopenharmony_ci   short *db = mb->blocks;
134bf215546Sopenharmony_ci   for (cbb = 0x20; cbb > 0; cbb >>= 1) {
135bf215546Sopenharmony_ci      if (cbb & cbp) {
136bf215546Sopenharmony_ci         memcpy(&dec->data[dec->data_pos], db, 128);
137bf215546Sopenharmony_ci         dec->data_pos += 32;
138bf215546Sopenharmony_ci         db += 64;
139bf215546Sopenharmony_ci      } else if (mb->macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA) {
140bf215546Sopenharmony_ci         memset(&dec->data[dec->data_pos], 0, 128);
141bf215546Sopenharmony_ci         dec->data_pos += 32;
142bf215546Sopenharmony_ci      }
143bf215546Sopenharmony_ci   }
144bf215546Sopenharmony_ci}
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_cistatic inline void
147bf215546Sopenharmony_cinouveau_vpe_mb_dct_header(struct nouveau_decoder *dec,
148bf215546Sopenharmony_ci                          const struct pipe_mpeg12_macroblock *mb,
149bf215546Sopenharmony_ci                          bool luma)
150bf215546Sopenharmony_ci{
151bf215546Sopenharmony_ci   unsigned base_dct, cbp;
152bf215546Sopenharmony_ci   bool intra = mb->macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA;
153bf215546Sopenharmony_ci   unsigned x = mb->x * 16;
154bf215546Sopenharmony_ci   unsigned y = luma ? mb->y * 16 : mb->y * 8;
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci   /* Setup the base dct header */
157bf215546Sopenharmony_ci   base_dct = dec->current << NV17_MPEG_CMD_CHROMA_MB_HEADER_SURFACE__SHIFT;
158bf215546Sopenharmony_ci   base_dct |= NV17_MPEG_CMD_CHROMA_MB_HEADER_RUN_SINGLE;
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ci   if (!(mb->x & 1))
161bf215546Sopenharmony_ci      base_dct |= NV17_MPEG_CMD_CHROMA_MB_HEADER_X_COORD_EVEN;
162bf215546Sopenharmony_ci   if (intra)
163bf215546Sopenharmony_ci      cbp = 0x3f;
164bf215546Sopenharmony_ci   else
165bf215546Sopenharmony_ci      cbp = mb->coded_block_pattern;
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci   if (dec->picture_structure == PIPE_MPEG12_PICTURE_STRUCTURE_FRAME) {
168bf215546Sopenharmony_ci      base_dct |= NV17_MPEG_CMD_CHROMA_MB_HEADER_TYPE_FRAME;
169bf215546Sopenharmony_ci      if (luma && mb->macroblock_modes.bits.dct_type == PIPE_MPEG12_DCT_TYPE_FIELD)
170bf215546Sopenharmony_ci         base_dct |= NV17_MPEG_CMD_CHROMA_MB_HEADER_FRAME_DCT_TYPE_FIELD;
171bf215546Sopenharmony_ci   } else {
172bf215546Sopenharmony_ci      if (dec->picture_structure == PIPE_MPEG12_PICTURE_STRUCTURE_FIELD_BOTTOM)
173bf215546Sopenharmony_ci         base_dct |= NV17_MPEG_CMD_CHROMA_MB_HEADER_FIELD_BOTTOM;
174bf215546Sopenharmony_ci      if (!intra)
175bf215546Sopenharmony_ci         y *= 2;
176bf215546Sopenharmony_ci   }
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_ci   if (luma) {
179bf215546Sopenharmony_ci      base_dct |= NV17_MPEG_CMD_LUMA_MB_HEADER_OP_LUMA_MB_HEADER;
180bf215546Sopenharmony_ci      base_dct |= (cbp >> 2) << NV17_MPEG_CMD_LUMA_MB_HEADER_CBP__SHIFT;
181bf215546Sopenharmony_ci   } else {
182bf215546Sopenharmony_ci      base_dct |= NV17_MPEG_CMD_CHROMA_MB_HEADER_OP_CHROMA_MB_HEADER;
183bf215546Sopenharmony_ci      base_dct |= (cbp & 3) << NV17_MPEG_CMD_CHROMA_MB_HEADER_CBP__SHIFT;
184bf215546Sopenharmony_ci   }
185bf215546Sopenharmony_ci   nouveau_vpe_write(dec, base_dct);
186bf215546Sopenharmony_ci   nouveau_vpe_write(dec, NV17_MPEG_CMD_MB_COORDS_OP_MB_COORDS |
187bf215546Sopenharmony_ci                     x | (y << NV17_MPEG_CMD_MB_COORDS_Y__SHIFT));
188bf215546Sopenharmony_ci}
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_cistatic inline unsigned int
191bf215546Sopenharmony_cinouveau_vpe_mb_mv_flags(bool luma, int mv_h, int mv_v, bool forward, bool first, bool vert)
192bf215546Sopenharmony_ci{
193bf215546Sopenharmony_ci   unsigned mc_header = 0;
194bf215546Sopenharmony_ci   if (luma)
195bf215546Sopenharmony_ci      mc_header |= NV17_MPEG_CMD_LUMA_MV_HEADER_OP_LUMA_MV_HEADER;
196bf215546Sopenharmony_ci   else
197bf215546Sopenharmony_ci      mc_header |= NV17_MPEG_CMD_CHROMA_MV_HEADER_OP_CHROMA_MV_HEADER;
198bf215546Sopenharmony_ci   if (mv_h & 1)
199bf215546Sopenharmony_ci      mc_header |= NV17_MPEG_CMD_CHROMA_MV_HEADER_X_HALF;
200bf215546Sopenharmony_ci   if (mv_v & 1)
201bf215546Sopenharmony_ci      mc_header |= NV17_MPEG_CMD_CHROMA_MV_HEADER_Y_HALF;
202bf215546Sopenharmony_ci   if (!forward)
203bf215546Sopenharmony_ci      mc_header |= NV17_MPEG_CMD_CHROMA_MV_HEADER_DIRECTION_BACKWARD;
204bf215546Sopenharmony_ci   if (!first)
205bf215546Sopenharmony_ci      mc_header |= NV17_MPEG_CMD_CHROMA_MV_HEADER_IDX;
206bf215546Sopenharmony_ci   if (vert)
207bf215546Sopenharmony_ci      mc_header |= NV17_MPEG_CMD_LUMA_MV_HEADER_FIELD_BOTTOM;
208bf215546Sopenharmony_ci   return mc_header;
209bf215546Sopenharmony_ci}
210bf215546Sopenharmony_ci
211bf215546Sopenharmony_cistatic unsigned pos(int pos, int mov, int max) {
212bf215546Sopenharmony_ci   int ret = pos + mov;
213bf215546Sopenharmony_ci   if (pos < 0)
214bf215546Sopenharmony_ci      return 0;
215bf215546Sopenharmony_ci   if (pos >= max)
216bf215546Sopenharmony_ci      return max-1;
217bf215546Sopenharmony_ci   return ret;
218bf215546Sopenharmony_ci}
219bf215546Sopenharmony_ci
220bf215546Sopenharmony_ci/* because we want -1 / 2 = -1 */
221bf215546Sopenharmony_cistatic int div_down(int val, int mult) {
222bf215546Sopenharmony_ci   val &= ~(mult - 1);
223bf215546Sopenharmony_ci   return val / mult;
224bf215546Sopenharmony_ci}
225bf215546Sopenharmony_ci
226bf215546Sopenharmony_cistatic int div_up(int val, int mult) {
227bf215546Sopenharmony_ci   val += mult - 1;
228bf215546Sopenharmony_ci   return val / mult;
229bf215546Sopenharmony_ci}
230bf215546Sopenharmony_ci
231bf215546Sopenharmony_cistatic inline void
232bf215546Sopenharmony_cinouveau_vpe_mb_mv(struct nouveau_decoder *dec, unsigned mc_header,
233bf215546Sopenharmony_ci                   bool luma, bool frame, bool forward, bool vert,
234bf215546Sopenharmony_ci                   int x, int y, const short motions[2],
235bf215546Sopenharmony_ci                   unsigned surface, bool first)
236bf215546Sopenharmony_ci{
237bf215546Sopenharmony_ci   unsigned mc_vector;
238bf215546Sopenharmony_ci   int mv_horizontal = motions[0];
239bf215546Sopenharmony_ci   int mv_vertical = motions[1];
240bf215546Sopenharmony_ci   int mv2 = mc_header & NV17_MPEG_CMD_CHROMA_MV_HEADER_COUNT_2;
241bf215546Sopenharmony_ci   unsigned width = dec->base.width;
242bf215546Sopenharmony_ci   unsigned height = dec->base.height;
243bf215546Sopenharmony_ci   if (mv2)
244bf215546Sopenharmony_ci      mv_vertical = div_down(mv_vertical, 2);
245bf215546Sopenharmony_ci   assert(frame); // Untested for non-frames
246bf215546Sopenharmony_ci   if (!frame)
247bf215546Sopenharmony_ci      height *= 2;
248bf215546Sopenharmony_ci
249bf215546Sopenharmony_ci   mc_header |= surface << NV17_MPEG_CMD_CHROMA_MV_HEADER_SURFACE__SHIFT;
250bf215546Sopenharmony_ci   if (!luma) {
251bf215546Sopenharmony_ci      mv_vertical = div_up(mv_vertical, 2);
252bf215546Sopenharmony_ci      mv_horizontal = div_up(mv_horizontal, 2);
253bf215546Sopenharmony_ci      height /= 2;
254bf215546Sopenharmony_ci   }
255bf215546Sopenharmony_ci   mc_header |= nouveau_vpe_mb_mv_flags(luma, mv_horizontal, mv_vertical, forward, first, vert);
256bf215546Sopenharmony_ci   nouveau_vpe_write(dec, mc_header);
257bf215546Sopenharmony_ci
258bf215546Sopenharmony_ci   mc_vector = NV17_MPEG_CMD_MV_COORDS_OP_MV_COORDS;
259bf215546Sopenharmony_ci   if (luma)
260bf215546Sopenharmony_ci      mc_vector |= pos(x, div_down(mv_horizontal, 2), width);
261bf215546Sopenharmony_ci   else
262bf215546Sopenharmony_ci      mc_vector |= pos(x, mv_horizontal & ~1, width);
263bf215546Sopenharmony_ci   if (!mv2)
264bf215546Sopenharmony_ci      mc_vector |= pos(y, div_down(mv_vertical, 2), height) << NV17_MPEG_CMD_MV_COORDS_Y__SHIFT;
265bf215546Sopenharmony_ci   else
266bf215546Sopenharmony_ci      mc_vector |= pos(y, mv_vertical & ~1, height) << NV17_MPEG_CMD_MV_COORDS_Y__SHIFT;
267bf215546Sopenharmony_ci   nouveau_vpe_write(dec, mc_vector);
268bf215546Sopenharmony_ci}
269bf215546Sopenharmony_ci
270bf215546Sopenharmony_cistatic void
271bf215546Sopenharmony_cinouveau_vpe_mb_mv_header(struct nouveau_decoder *dec,
272bf215546Sopenharmony_ci                         const struct pipe_mpeg12_macroblock *mb,
273bf215546Sopenharmony_ci                         bool luma)
274bf215546Sopenharmony_ci{
275bf215546Sopenharmony_ci   bool frame = dec->picture_structure == PIPE_MPEG12_PICTURE_STRUCTURE_FRAME;
276bf215546Sopenharmony_ci   unsigned base;
277bf215546Sopenharmony_ci   bool forward, backward;
278bf215546Sopenharmony_ci   int y, y2, x = mb->x * 16;
279bf215546Sopenharmony_ci   if (luma)
280bf215546Sopenharmony_ci      y = mb->y * (frame ? 16 : 32);
281bf215546Sopenharmony_ci   else
282bf215546Sopenharmony_ci      y = mb->y * (frame ? 8 : 16);
283bf215546Sopenharmony_ci   if (frame)
284bf215546Sopenharmony_ci      y2 = y;
285bf215546Sopenharmony_ci   else
286bf215546Sopenharmony_ci      y2 = y + (luma ? 16 : 8);
287bf215546Sopenharmony_ci
288bf215546Sopenharmony_ci   forward = mb->macroblock_type & PIPE_MPEG12_MB_TYPE_MOTION_FORWARD;
289bf215546Sopenharmony_ci   backward = mb->macroblock_type & PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD;
290bf215546Sopenharmony_ci   assert(!forward || dec->past < 8);
291bf215546Sopenharmony_ci   assert(!backward || dec->future < 8);
292bf215546Sopenharmony_ci   if (frame) {
293bf215546Sopenharmony_ci      switch (mb->macroblock_modes.bits.frame_motion_type) {
294bf215546Sopenharmony_ci      case PIPE_MPEG12_MO_TYPE_FRAME: goto mv1;
295bf215546Sopenharmony_ci      case PIPE_MPEG12_MO_TYPE_FIELD: goto mv2;
296bf215546Sopenharmony_ci      case PIPE_MPEG12_MO_TYPE_DUAL_PRIME: {
297bf215546Sopenharmony_ci         base = NV17_MPEG_CMD_CHROMA_MV_HEADER_COUNT_2;
298bf215546Sopenharmony_ci         if (forward) {
299bf215546Sopenharmony_ci            nouveau_vpe_mb_mv(dec, base, luma, frame, true, false,
300bf215546Sopenharmony_ci                              x, y, mb->PMV[0][0], dec->past, true);
301bf215546Sopenharmony_ci            nouveau_vpe_mb_mv(dec, base, luma, frame, true, true,
302bf215546Sopenharmony_ci                              x, y2, mb->PMV[0][0], dec->past, false);
303bf215546Sopenharmony_ci         }
304bf215546Sopenharmony_ci         if (backward && forward) {
305bf215546Sopenharmony_ci            nouveau_vpe_mb_mv(dec, base, luma, frame, !forward, true,
306bf215546Sopenharmony_ci                              x, y, mb->PMV[1][0], dec->future, true);
307bf215546Sopenharmony_ci            nouveau_vpe_mb_mv(dec, base, luma, frame, !forward, false,
308bf215546Sopenharmony_ci                              x, y2, mb->PMV[1][1], dec->future, false);
309bf215546Sopenharmony_ci         } else assert(!backward);
310bf215546Sopenharmony_ci         break;
311bf215546Sopenharmony_ci      }
312bf215546Sopenharmony_ci      default: assert(0);
313bf215546Sopenharmony_ci      }
314bf215546Sopenharmony_ci   } else {
315bf215546Sopenharmony_ci      switch (mb->macroblock_modes.bits.field_motion_type) {
316bf215546Sopenharmony_ci      case PIPE_MPEG12_MO_TYPE_FIELD: goto mv1;
317bf215546Sopenharmony_ci      case PIPE_MPEG12_MO_TYPE_16x8: goto mv2;
318bf215546Sopenharmony_ci      case PIPE_MPEG12_MO_TYPE_DUAL_PRIME: {
319bf215546Sopenharmony_ci      base = NV17_MPEG_CMD_CHROMA_MV_HEADER_MV_SPLIT_HALF_MB;
320bf215546Sopenharmony_ci         if (frame)
321bf215546Sopenharmony_ci            base |= NV17_MPEG_CMD_CHROMA_MV_HEADER_TYPE_FRAME;
322bf215546Sopenharmony_ci         if (forward)
323bf215546Sopenharmony_ci            nouveau_vpe_mb_mv(dec, base, luma, frame, true,
324bf215546Sopenharmony_ci                              dec->picture_structure != PIPE_MPEG12_PICTURE_STRUCTURE_FIELD_TOP,
325bf215546Sopenharmony_ci                              x, y, mb->PMV[0][0], dec->past, true);
326bf215546Sopenharmony_ci         if (backward && forward)
327bf215546Sopenharmony_ci            nouveau_vpe_mb_mv(dec, base, luma, frame, false,
328bf215546Sopenharmony_ci                              dec->picture_structure == PIPE_MPEG12_PICTURE_STRUCTURE_FIELD_TOP,
329bf215546Sopenharmony_ci                              x, y, mb->PMV[0][1], dec->future, true);
330bf215546Sopenharmony_ci         else assert(!backward);
331bf215546Sopenharmony_ci         break;
332bf215546Sopenharmony_ci      }
333bf215546Sopenharmony_ci      default: assert(0);
334bf215546Sopenharmony_ci      }
335bf215546Sopenharmony_ci   }
336bf215546Sopenharmony_ci   return;
337bf215546Sopenharmony_ci
338bf215546Sopenharmony_cimv1:
339bf215546Sopenharmony_ci   base = NV17_MPEG_CMD_CHROMA_MV_HEADER_MV_SPLIT_HALF_MB;
340bf215546Sopenharmony_ci   if (frame)
341bf215546Sopenharmony_ci       base |= NV17_MPEG_CMD_CHROMA_MV_HEADER_TYPE_FRAME;
342bf215546Sopenharmony_ci    /* frame 16x16 */
343bf215546Sopenharmony_ci   if (forward)
344bf215546Sopenharmony_ci       nouveau_vpe_mb_mv(dec, base, luma, frame, true, false,
345bf215546Sopenharmony_ci                         x, y, mb->PMV[0][0], dec->past, true);
346bf215546Sopenharmony_ci   if (backward)
347bf215546Sopenharmony_ci       nouveau_vpe_mb_mv(dec, base, luma, frame, !forward, false,
348bf215546Sopenharmony_ci                         x, y, mb->PMV[0][1], dec->future, true);
349bf215546Sopenharmony_ci    return;
350bf215546Sopenharmony_ci
351bf215546Sopenharmony_cimv2:
352bf215546Sopenharmony_ci   base = NV17_MPEG_CMD_CHROMA_MV_HEADER_COUNT_2;
353bf215546Sopenharmony_ci   if (!frame)
354bf215546Sopenharmony_ci      base |= NV17_MPEG_CMD_CHROMA_MV_HEADER_MV_SPLIT_HALF_MB;
355bf215546Sopenharmony_ci   if (forward) {
356bf215546Sopenharmony_ci      nouveau_vpe_mb_mv(dec, base, luma, frame, true,
357bf215546Sopenharmony_ci                        mb->motion_vertical_field_select & PIPE_MPEG12_FS_FIRST_FORWARD,
358bf215546Sopenharmony_ci                        x, y, mb->PMV[0][0], dec->past, true);
359bf215546Sopenharmony_ci      nouveau_vpe_mb_mv(dec, base, luma, frame, true,
360bf215546Sopenharmony_ci                        mb->motion_vertical_field_select & PIPE_MPEG12_FS_SECOND_FORWARD,
361bf215546Sopenharmony_ci                        x, y2, mb->PMV[1][0], dec->past, false);
362bf215546Sopenharmony_ci   }
363bf215546Sopenharmony_ci   if (backward) {
364bf215546Sopenharmony_ci      nouveau_vpe_mb_mv(dec, base, luma, frame, !forward,
365bf215546Sopenharmony_ci                        mb->motion_vertical_field_select & PIPE_MPEG12_FS_FIRST_BACKWARD,
366bf215546Sopenharmony_ci                        x, y, mb->PMV[0][1], dec->future, true);
367bf215546Sopenharmony_ci      nouveau_vpe_mb_mv(dec, base, luma, frame, !forward,
368bf215546Sopenharmony_ci                        mb->motion_vertical_field_select & PIPE_MPEG12_FS_SECOND_BACKWARD,
369bf215546Sopenharmony_ci                        x, y2, mb->PMV[1][1], dec->future, false);
370bf215546Sopenharmony_ci   }
371bf215546Sopenharmony_ci}
372bf215546Sopenharmony_ci
373bf215546Sopenharmony_cistatic unsigned
374bf215546Sopenharmony_cinouveau_decoder_surface_index(struct nouveau_decoder *dec,
375bf215546Sopenharmony_ci                              struct pipe_video_buffer *buffer)
376bf215546Sopenharmony_ci{
377bf215546Sopenharmony_ci   struct nouveau_video_buffer *buf = (struct nouveau_video_buffer *)buffer;
378bf215546Sopenharmony_ci   struct nouveau_pushbuf *push = dec->push;
379bf215546Sopenharmony_ci   struct nouveau_bo *bo_y = nv04_resource(buf->resources[0])->bo;
380bf215546Sopenharmony_ci   struct nouveau_bo *bo_c = nv04_resource(buf->resources[1])->bo;
381bf215546Sopenharmony_ci
382bf215546Sopenharmony_ci   unsigned i;
383bf215546Sopenharmony_ci
384bf215546Sopenharmony_ci   for (i = 0; i < dec->num_surfaces; ++i) {
385bf215546Sopenharmony_ci      if (dec->surfaces[i] == buf)
386bf215546Sopenharmony_ci         return i;
387bf215546Sopenharmony_ci   }
388bf215546Sopenharmony_ci   assert(i < 8);
389bf215546Sopenharmony_ci   dec->surfaces[i] = buf;
390bf215546Sopenharmony_ci   dec->num_surfaces++;
391bf215546Sopenharmony_ci
392bf215546Sopenharmony_ci   nouveau_bufctx_reset(dec->bufctx, NV31_VIDEO_BIND_IMG(i));
393bf215546Sopenharmony_ci
394bf215546Sopenharmony_ci#define BCTX_ARGS dec->bufctx, NV31_VIDEO_BIND_IMG(i), NOUVEAU_BO_RDWR
395bf215546Sopenharmony_ci   BEGIN_NV04(push, NV31_MPEG(IMAGE_Y_OFFSET(i)), 2);
396bf215546Sopenharmony_ci   PUSH_MTHDl(push, NV31_MPEG(IMAGE_Y_OFFSET(i)), bo_y, 0, BCTX_ARGS);
397bf215546Sopenharmony_ci   PUSH_MTHDl(push, NV31_MPEG(IMAGE_C_OFFSET(i)), bo_c, 0, BCTX_ARGS);
398bf215546Sopenharmony_ci#undef BCTX_ARGS
399bf215546Sopenharmony_ci
400bf215546Sopenharmony_ci   return i;
401bf215546Sopenharmony_ci}
402bf215546Sopenharmony_ci
403bf215546Sopenharmony_cistatic void
404bf215546Sopenharmony_cinouveau_decoder_begin_frame(struct pipe_video_codec *decoder,
405bf215546Sopenharmony_ci                            struct pipe_video_buffer *target,
406bf215546Sopenharmony_ci                            struct pipe_picture_desc *picture)
407bf215546Sopenharmony_ci{
408bf215546Sopenharmony_ci}
409bf215546Sopenharmony_ci
410bf215546Sopenharmony_cistatic void
411bf215546Sopenharmony_cinouveau_decoder_decode_macroblock(struct pipe_video_codec *decoder,
412bf215546Sopenharmony_ci                                  struct pipe_video_buffer *target,
413bf215546Sopenharmony_ci                                  struct pipe_picture_desc *picture,
414bf215546Sopenharmony_ci                                  const struct pipe_macroblock *pipe_mb,
415bf215546Sopenharmony_ci                                  unsigned num_macroblocks)
416bf215546Sopenharmony_ci{
417bf215546Sopenharmony_ci   struct nouveau_decoder *dec = (struct nouveau_decoder *)decoder;
418bf215546Sopenharmony_ci   struct pipe_mpeg12_picture_desc *desc = (struct pipe_mpeg12_picture_desc*)picture;
419bf215546Sopenharmony_ci   const struct pipe_mpeg12_macroblock *mb;
420bf215546Sopenharmony_ci   unsigned i;
421bf215546Sopenharmony_ci   assert(target->width == decoder->width);
422bf215546Sopenharmony_ci   assert(target->height == decoder->height);
423bf215546Sopenharmony_ci
424bf215546Sopenharmony_ci   dec->current = nouveau_decoder_surface_index(dec, target);
425bf215546Sopenharmony_ci   assert(dec->current < 8);
426bf215546Sopenharmony_ci   dec->picture_structure = desc->picture_structure;
427bf215546Sopenharmony_ci   if (desc->ref[1])
428bf215546Sopenharmony_ci      dec->future = nouveau_decoder_surface_index(dec, desc->ref[1]);
429bf215546Sopenharmony_ci   if (desc->ref[0])
430bf215546Sopenharmony_ci      dec->past = nouveau_decoder_surface_index(dec, desc->ref[0]);
431bf215546Sopenharmony_ci
432bf215546Sopenharmony_ci   if (nouveau_vpe_init(dec)) return;
433bf215546Sopenharmony_ci
434bf215546Sopenharmony_ci   /* initialize scan order */
435bf215546Sopenharmony_ci   nouveau_vpe_write(dec, 0x720000c0);
436bf215546Sopenharmony_ci   nouveau_vpe_write(dec, dec->data_pos);
437bf215546Sopenharmony_ci
438bf215546Sopenharmony_ci   mb = (const struct pipe_mpeg12_macroblock *)pipe_mb;
439bf215546Sopenharmony_ci   for (i = 0; i < num_macroblocks; ++i, mb++) {
440bf215546Sopenharmony_ci      if (mb->macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA) {
441bf215546Sopenharmony_ci         nouveau_vpe_mb_dct_header(dec, mb, true);
442bf215546Sopenharmony_ci         nouveau_vpe_mb_dct_header(dec, mb, false);
443bf215546Sopenharmony_ci      } else {
444bf215546Sopenharmony_ci         nouveau_vpe_mb_mv_header(dec, mb, true);
445bf215546Sopenharmony_ci         nouveau_vpe_mb_dct_header(dec, mb, true);
446bf215546Sopenharmony_ci
447bf215546Sopenharmony_ci         nouveau_vpe_mb_mv_header(dec, mb, false);
448bf215546Sopenharmony_ci         nouveau_vpe_mb_dct_header(dec, mb, false);
449bf215546Sopenharmony_ci      }
450bf215546Sopenharmony_ci      if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
451bf215546Sopenharmony_ci         nouveau_vpe_mb_dct_blocks(dec, mb);
452bf215546Sopenharmony_ci      else
453bf215546Sopenharmony_ci         nouveau_vpe_mb_data_blocks(dec, mb);
454bf215546Sopenharmony_ci   }
455bf215546Sopenharmony_ci}
456bf215546Sopenharmony_ci
457bf215546Sopenharmony_cistatic void
458bf215546Sopenharmony_cinouveau_decoder_end_frame(struct pipe_video_codec *decoder,
459bf215546Sopenharmony_ci                          struct pipe_video_buffer *target,
460bf215546Sopenharmony_ci                          struct pipe_picture_desc *picture)
461bf215546Sopenharmony_ci{
462bf215546Sopenharmony_ci}
463bf215546Sopenharmony_ci
464bf215546Sopenharmony_cistatic void
465bf215546Sopenharmony_cinouveau_decoder_flush(struct pipe_video_codec *decoder)
466bf215546Sopenharmony_ci{
467bf215546Sopenharmony_ci   struct nouveau_decoder *dec = (struct nouveau_decoder *)decoder;
468bf215546Sopenharmony_ci   if (dec->ofs)
469bf215546Sopenharmony_ci      nouveau_vpe_fini(dec);
470bf215546Sopenharmony_ci}
471bf215546Sopenharmony_ci
472bf215546Sopenharmony_cistatic void
473bf215546Sopenharmony_cinouveau_decoder_destroy(struct pipe_video_codec *decoder)
474bf215546Sopenharmony_ci{
475bf215546Sopenharmony_ci   struct nouveau_decoder *dec = (struct nouveau_decoder*)decoder;
476bf215546Sopenharmony_ci
477bf215546Sopenharmony_ci   if (dec->data_bo)
478bf215546Sopenharmony_ci      nouveau_bo_ref(NULL, &dec->data_bo);
479bf215546Sopenharmony_ci   if (dec->cmd_bo)
480bf215546Sopenharmony_ci      nouveau_bo_ref(NULL, &dec->cmd_bo);
481bf215546Sopenharmony_ci   if (dec->fence_bo)
482bf215546Sopenharmony_ci      nouveau_bo_ref(NULL, &dec->fence_bo);
483bf215546Sopenharmony_ci
484bf215546Sopenharmony_ci   nouveau_object_del(&dec->mpeg);
485bf215546Sopenharmony_ci
486bf215546Sopenharmony_ci   if (dec->bufctx)
487bf215546Sopenharmony_ci      nouveau_bufctx_del(&dec->bufctx);
488bf215546Sopenharmony_ci   if (dec->push)
489bf215546Sopenharmony_ci      nouveau_pushbuf_del(&dec->push);
490bf215546Sopenharmony_ci   if (dec->client)
491bf215546Sopenharmony_ci      nouveau_client_del(&dec->client);
492bf215546Sopenharmony_ci   if (dec->chan)
493bf215546Sopenharmony_ci      nouveau_object_del(&dec->chan);
494bf215546Sopenharmony_ci
495bf215546Sopenharmony_ci   FREE(dec);
496bf215546Sopenharmony_ci}
497bf215546Sopenharmony_ci
498bf215546Sopenharmony_cistatic struct pipe_video_codec *
499bf215546Sopenharmony_cinouveau_create_decoder(struct pipe_context *context,
500bf215546Sopenharmony_ci                       const struct pipe_video_codec *templ,
501bf215546Sopenharmony_ci                       struct nouveau_screen *screen)
502bf215546Sopenharmony_ci{
503bf215546Sopenharmony_ci   struct nv04_fifo nv04_data = { .vram = 0xbeef0201, .gart = 0xbeef0202 };
504bf215546Sopenharmony_ci   unsigned width = templ->width, height = templ->height;
505bf215546Sopenharmony_ci   struct nouveau_object *mpeg = NULL;
506bf215546Sopenharmony_ci   struct nouveau_decoder *dec;
507bf215546Sopenharmony_ci   struct nouveau_pushbuf *push;
508bf215546Sopenharmony_ci   int ret;
509bf215546Sopenharmony_ci   bool is8274 = screen->device->chipset > 0x80;
510bf215546Sopenharmony_ci
511bf215546Sopenharmony_ci   debug_printf("Acceleration level: %s\n", templ->entrypoint <= PIPE_VIDEO_ENTRYPOINT_BITSTREAM ? "bit":
512bf215546Sopenharmony_ci                                            templ->entrypoint == PIPE_VIDEO_ENTRYPOINT_IDCT ? "IDCT" : "MC");
513bf215546Sopenharmony_ci
514bf215546Sopenharmony_ci   if (getenv("XVMC_VL"))
515bf215546Sopenharmony_ci      goto vl;
516bf215546Sopenharmony_ci   if (u_reduce_video_profile(templ->profile) != PIPE_VIDEO_FORMAT_MPEG12)
517bf215546Sopenharmony_ci      goto vl;
518bf215546Sopenharmony_ci   if (screen->device->chipset >= 0x98 && screen->device->chipset != 0xa0)
519bf215546Sopenharmony_ci      goto vl;
520bf215546Sopenharmony_ci   if (screen->device->chipset < 0x40)
521bf215546Sopenharmony_ci      goto vl;
522bf215546Sopenharmony_ci
523bf215546Sopenharmony_ci   dec = CALLOC_STRUCT(nouveau_decoder);
524bf215546Sopenharmony_ci   if (!dec)
525bf215546Sopenharmony_ci      return NULL;
526bf215546Sopenharmony_ci
527bf215546Sopenharmony_ci   ret = nouveau_object_new(&screen->device->object, 0,
528bf215546Sopenharmony_ci                            NOUVEAU_FIFO_CHANNEL_CLASS,
529bf215546Sopenharmony_ci                            &nv04_data, sizeof(nv04_data), &dec->chan);
530bf215546Sopenharmony_ci   if (ret)
531bf215546Sopenharmony_ci      goto fail;
532bf215546Sopenharmony_ci   ret = nouveau_client_new(screen->device, &dec->client);
533bf215546Sopenharmony_ci   if (ret)
534bf215546Sopenharmony_ci      goto fail;
535bf215546Sopenharmony_ci   ret = nouveau_pushbuf_new(dec->client, dec->chan, 2, 4096, 1, &dec->push);
536bf215546Sopenharmony_ci   if (ret)
537bf215546Sopenharmony_ci      goto fail;
538bf215546Sopenharmony_ci   ret = nouveau_bufctx_new(dec->client, NV31_VIDEO_BIND_COUNT, &dec->bufctx);
539bf215546Sopenharmony_ci   if (ret)
540bf215546Sopenharmony_ci      goto fail;
541bf215546Sopenharmony_ci   push = dec->push;
542bf215546Sopenharmony_ci
543bf215546Sopenharmony_ci   width = align(width, 64);
544bf215546Sopenharmony_ci   height = align(height, 64);
545bf215546Sopenharmony_ci
546bf215546Sopenharmony_ci   if (is8274)
547bf215546Sopenharmony_ci      ret = nouveau_object_new(dec->chan, 0xbeef8274, NV84_MPEG_CLASS, NULL, 0,
548bf215546Sopenharmony_ci                               &mpeg);
549bf215546Sopenharmony_ci   else
550bf215546Sopenharmony_ci      ret = nouveau_object_new(dec->chan, 0xbeef3174, NV31_MPEG_CLASS, NULL, 0,
551bf215546Sopenharmony_ci                               &mpeg);
552bf215546Sopenharmony_ci   if (ret < 0) {
553bf215546Sopenharmony_ci      debug_printf("Creation failed: %s (%i)\n", strerror(-ret), ret);
554bf215546Sopenharmony_ci      goto fail;
555bf215546Sopenharmony_ci   }
556bf215546Sopenharmony_ci
557bf215546Sopenharmony_ci   dec->mpeg = mpeg;
558bf215546Sopenharmony_ci   dec->base = *templ;
559bf215546Sopenharmony_ci   dec->base.context = context;
560bf215546Sopenharmony_ci   dec->base.width = width;
561bf215546Sopenharmony_ci   dec->base.height = height;
562bf215546Sopenharmony_ci   dec->base.destroy = nouveau_decoder_destroy;
563bf215546Sopenharmony_ci   dec->base.begin_frame = nouveau_decoder_begin_frame;
564bf215546Sopenharmony_ci   dec->base.decode_macroblock = nouveau_decoder_decode_macroblock;
565bf215546Sopenharmony_ci   dec->base.end_frame = nouveau_decoder_end_frame;
566bf215546Sopenharmony_ci   dec->base.flush = nouveau_decoder_flush;
567bf215546Sopenharmony_ci   dec->screen = screen;
568bf215546Sopenharmony_ci
569bf215546Sopenharmony_ci   ret = nouveau_bo_new(dec->screen->device, NOUVEAU_BO_GART | NOUVEAU_BO_MAP,
570bf215546Sopenharmony_ci                        0, 1024 * 1024, NULL, &dec->cmd_bo);
571bf215546Sopenharmony_ci   if (ret)
572bf215546Sopenharmony_ci      goto fail;
573bf215546Sopenharmony_ci
574bf215546Sopenharmony_ci   ret = nouveau_bo_new(dec->screen->device, NOUVEAU_BO_GART | NOUVEAU_BO_MAP,
575bf215546Sopenharmony_ci                        0, width * height * 6, NULL, &dec->data_bo);
576bf215546Sopenharmony_ci   if (ret)
577bf215546Sopenharmony_ci      goto fail;
578bf215546Sopenharmony_ci
579bf215546Sopenharmony_ci   /* we don't need the fence, the kernel sync's for us */
580bf215546Sopenharmony_ci#if 0
581bf215546Sopenharmony_ci   ret = nouveau_bo_new(dec->screen->device, NOUVEAU_BO_GART | NOUVEAU_BO_MAP,
582bf215546Sopenharmony_ci                        0, 4096, NULL, &dec->fence_bo);
583bf215546Sopenharmony_ci   if (ret)
584bf215546Sopenharmony_ci      goto fail;
585bf215546Sopenharmony_ci   nouveau_bo_map(dec->fence_bo, NOUVEAU_BO_RDWR, NULL);
586bf215546Sopenharmony_ci   dec->fence_map = dec->fence_bo->map;
587bf215546Sopenharmony_ci   dec->fence_map[0] = 0;
588bf215546Sopenharmony_ci#endif
589bf215546Sopenharmony_ci
590bf215546Sopenharmony_ci   nouveau_pushbuf_bufctx(dec->push, dec->bufctx);
591bf215546Sopenharmony_ci   nouveau_pushbuf_space(push, 32, 4, 0);
592bf215546Sopenharmony_ci
593bf215546Sopenharmony_ci   BEGIN_NV04(push, SUBC_MPEG(NV01_SUBCHAN_OBJECT), 1);
594bf215546Sopenharmony_ci   PUSH_DATA (push, dec->mpeg->handle);
595bf215546Sopenharmony_ci
596bf215546Sopenharmony_ci   BEGIN_NV04(push, NV31_MPEG(DMA_CMD), 1);
597bf215546Sopenharmony_ci   PUSH_DATA (push, nv04_data.gart);
598bf215546Sopenharmony_ci
599bf215546Sopenharmony_ci   BEGIN_NV04(push, NV31_MPEG(DMA_DATA), 1);
600bf215546Sopenharmony_ci   PUSH_DATA (push, nv04_data.gart);
601bf215546Sopenharmony_ci
602bf215546Sopenharmony_ci   BEGIN_NV04(push, NV31_MPEG(DMA_IMAGE), 1);
603bf215546Sopenharmony_ci   PUSH_DATA (push, nv04_data.vram);
604bf215546Sopenharmony_ci
605bf215546Sopenharmony_ci   BEGIN_NV04(push, NV31_MPEG(PITCH), 2);
606bf215546Sopenharmony_ci   PUSH_DATA (push, width | NV31_MPEG_PITCH_UNK);
607bf215546Sopenharmony_ci   PUSH_DATA (push, (height << NV31_MPEG_SIZE_H__SHIFT) | width);
608bf215546Sopenharmony_ci
609bf215546Sopenharmony_ci   BEGIN_NV04(push, NV31_MPEG(FORMAT), 2);
610bf215546Sopenharmony_ci   PUSH_DATA (push, 0);
611bf215546Sopenharmony_ci   switch (templ->entrypoint) {
612bf215546Sopenharmony_ci      case PIPE_VIDEO_ENTRYPOINT_IDCT: PUSH_DATA (push, 1); break;
613bf215546Sopenharmony_ci      case PIPE_VIDEO_ENTRYPOINT_MC: PUSH_DATA (push, 0); break;
614bf215546Sopenharmony_ci      default: assert(0);
615bf215546Sopenharmony_ci   }
616bf215546Sopenharmony_ci
617bf215546Sopenharmony_ci   if (is8274) {
618bf215546Sopenharmony_ci      BEGIN_NV04(push, NV84_MPEG(DMA_QUERY), 1);
619bf215546Sopenharmony_ci      PUSH_DATA (push, nv04_data.vram);
620bf215546Sopenharmony_ci#if 0
621bf215546Sopenharmony_ci      BEGIN_NV04(push, NV84_MPEG(QUERY_OFFSET), 2);
622bf215546Sopenharmony_ci      PUSH_DATA (push, dec->fence_bo->offset);
623bf215546Sopenharmony_ci      PUSH_DATA (push, dec->fence_seq);
624bf215546Sopenharmony_ci#endif
625bf215546Sopenharmony_ci   }
626bf215546Sopenharmony_ci
627bf215546Sopenharmony_ci   ret = nouveau_vpe_init(dec);
628bf215546Sopenharmony_ci   if (ret)
629bf215546Sopenharmony_ci      goto fail;
630bf215546Sopenharmony_ci   nouveau_vpe_fini(dec);
631bf215546Sopenharmony_ci   return &dec->base;
632bf215546Sopenharmony_ci
633bf215546Sopenharmony_cifail:
634bf215546Sopenharmony_ci   nouveau_decoder_destroy(&dec->base);
635bf215546Sopenharmony_ci   return NULL;
636bf215546Sopenharmony_ci
637bf215546Sopenharmony_civl:
638bf215546Sopenharmony_ci   debug_printf("Using g3dvl renderer\n");
639bf215546Sopenharmony_ci   return vl_create_decoder(context, templ);
640bf215546Sopenharmony_ci}
641bf215546Sopenharmony_ci
642bf215546Sopenharmony_cistatic struct pipe_sampler_view **
643bf215546Sopenharmony_cinouveau_video_buffer_sampler_view_planes(struct pipe_video_buffer *buffer)
644bf215546Sopenharmony_ci{
645bf215546Sopenharmony_ci   struct nouveau_video_buffer *buf = (struct nouveau_video_buffer *)buffer;
646bf215546Sopenharmony_ci   struct pipe_sampler_view sv_templ;
647bf215546Sopenharmony_ci   struct pipe_context *pipe;
648bf215546Sopenharmony_ci   unsigned i;
649bf215546Sopenharmony_ci
650bf215546Sopenharmony_ci   assert(buf);
651bf215546Sopenharmony_ci
652bf215546Sopenharmony_ci   pipe = buf->base.context;
653bf215546Sopenharmony_ci
654bf215546Sopenharmony_ci   for (i = 0; i < buf->num_planes; ++i ) {
655bf215546Sopenharmony_ci      if (!buf->sampler_view_planes[i]) {
656bf215546Sopenharmony_ci         memset(&sv_templ, 0, sizeof(sv_templ));
657bf215546Sopenharmony_ci         u_sampler_view_default_template(&sv_templ, buf->resources[i], buf->resources[i]->format);
658bf215546Sopenharmony_ci
659bf215546Sopenharmony_ci         if (util_format_get_nr_components(buf->resources[i]->format) == 1)
660bf215546Sopenharmony_ci            sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = sv_templ.swizzle_a = PIPE_SWIZZLE_X;
661bf215546Sopenharmony_ci
662bf215546Sopenharmony_ci         buf->sampler_view_planes[i] = pipe->create_sampler_view(pipe, buf->resources[i], &sv_templ);
663bf215546Sopenharmony_ci         if (!buf->sampler_view_planes[i])
664bf215546Sopenharmony_ci            goto error;
665bf215546Sopenharmony_ci      }
666bf215546Sopenharmony_ci   }
667bf215546Sopenharmony_ci
668bf215546Sopenharmony_ci   return buf->sampler_view_planes;
669bf215546Sopenharmony_ci
670bf215546Sopenharmony_cierror:
671bf215546Sopenharmony_ci   for (i = 0; i < buf->num_planes; ++i )
672bf215546Sopenharmony_ci      pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL);
673bf215546Sopenharmony_ci
674bf215546Sopenharmony_ci   return NULL;
675bf215546Sopenharmony_ci}
676bf215546Sopenharmony_ci
677bf215546Sopenharmony_cistatic struct pipe_sampler_view **
678bf215546Sopenharmony_cinouveau_video_buffer_sampler_view_components(struct pipe_video_buffer *buffer)
679bf215546Sopenharmony_ci{
680bf215546Sopenharmony_ci   struct nouveau_video_buffer *buf = (struct nouveau_video_buffer *)buffer;
681bf215546Sopenharmony_ci   struct pipe_sampler_view sv_templ;
682bf215546Sopenharmony_ci   struct pipe_context *pipe;
683bf215546Sopenharmony_ci   unsigned i, j, component;
684bf215546Sopenharmony_ci
685bf215546Sopenharmony_ci   assert(buf);
686bf215546Sopenharmony_ci
687bf215546Sopenharmony_ci   pipe = buf->base.context;
688bf215546Sopenharmony_ci
689bf215546Sopenharmony_ci   for (component = 0, i = 0; i < buf->num_planes; ++i ) {
690bf215546Sopenharmony_ci      unsigned nr_components = util_format_get_nr_components(buf->resources[i]->format);
691bf215546Sopenharmony_ci
692bf215546Sopenharmony_ci      for (j = 0; j < nr_components; ++j, ++component) {
693bf215546Sopenharmony_ci         assert(component < VL_NUM_COMPONENTS);
694bf215546Sopenharmony_ci
695bf215546Sopenharmony_ci         if (!buf->sampler_view_components[component]) {
696bf215546Sopenharmony_ci            memset(&sv_templ, 0, sizeof(sv_templ));
697bf215546Sopenharmony_ci            u_sampler_view_default_template(&sv_templ, buf->resources[i], buf->resources[i]->format);
698bf215546Sopenharmony_ci            sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = PIPE_SWIZZLE_X + j;
699bf215546Sopenharmony_ci            sv_templ.swizzle_a = PIPE_SWIZZLE_1;
700bf215546Sopenharmony_ci            buf->sampler_view_components[component] = pipe->create_sampler_view(pipe, buf->resources[i], &sv_templ);
701bf215546Sopenharmony_ci            if (!buf->sampler_view_components[component])
702bf215546Sopenharmony_ci               goto error;
703bf215546Sopenharmony_ci         }
704bf215546Sopenharmony_ci      }
705bf215546Sopenharmony_ci   }
706bf215546Sopenharmony_ci
707bf215546Sopenharmony_ci   return buf->sampler_view_components;
708bf215546Sopenharmony_ci
709bf215546Sopenharmony_cierror:
710bf215546Sopenharmony_ci   for (i = 0; i < 3; ++i )
711bf215546Sopenharmony_ci      pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);
712bf215546Sopenharmony_ci
713bf215546Sopenharmony_ci   return NULL;
714bf215546Sopenharmony_ci}
715bf215546Sopenharmony_ci
716bf215546Sopenharmony_cistatic struct pipe_surface **
717bf215546Sopenharmony_cinouveau_video_buffer_surfaces(struct pipe_video_buffer *buffer)
718bf215546Sopenharmony_ci{
719bf215546Sopenharmony_ci   struct nouveau_video_buffer *buf = (struct nouveau_video_buffer *)buffer;
720bf215546Sopenharmony_ci   struct pipe_surface surf_templ;
721bf215546Sopenharmony_ci   struct pipe_context *pipe;
722bf215546Sopenharmony_ci   unsigned i;
723bf215546Sopenharmony_ci
724bf215546Sopenharmony_ci   assert(buf);
725bf215546Sopenharmony_ci
726bf215546Sopenharmony_ci   pipe = buf->base.context;
727bf215546Sopenharmony_ci
728bf215546Sopenharmony_ci   for (i = 0; i < buf->num_planes; ++i ) {
729bf215546Sopenharmony_ci      if (!buf->surfaces[i]) {
730bf215546Sopenharmony_ci         memset(&surf_templ, 0, sizeof(surf_templ));
731bf215546Sopenharmony_ci         surf_templ.format = buf->resources[i]->format;
732bf215546Sopenharmony_ci         buf->surfaces[i] = pipe->create_surface(pipe, buf->resources[i], &surf_templ);
733bf215546Sopenharmony_ci         if (!buf->surfaces[i])
734bf215546Sopenharmony_ci            goto error;
735bf215546Sopenharmony_ci      }
736bf215546Sopenharmony_ci   }
737bf215546Sopenharmony_ci
738bf215546Sopenharmony_ci   return buf->surfaces;
739bf215546Sopenharmony_ci
740bf215546Sopenharmony_cierror:
741bf215546Sopenharmony_ci   for (i = 0; i < buf->num_planes; ++i )
742bf215546Sopenharmony_ci      pipe_surface_reference(&buf->surfaces[i], NULL);
743bf215546Sopenharmony_ci
744bf215546Sopenharmony_ci   return NULL;
745bf215546Sopenharmony_ci}
746bf215546Sopenharmony_ci
747bf215546Sopenharmony_cistatic void
748bf215546Sopenharmony_cinouveau_video_buffer_destroy(struct pipe_video_buffer *buffer)
749bf215546Sopenharmony_ci{
750bf215546Sopenharmony_ci   struct nouveau_video_buffer *buf = (struct nouveau_video_buffer *)buffer;
751bf215546Sopenharmony_ci   unsigned i;
752bf215546Sopenharmony_ci
753bf215546Sopenharmony_ci   assert(buf);
754bf215546Sopenharmony_ci
755bf215546Sopenharmony_ci   for (i = 0; i < buf->num_planes; ++i) {
756bf215546Sopenharmony_ci      pipe_surface_reference(&buf->surfaces[i], NULL);
757bf215546Sopenharmony_ci      pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL);
758bf215546Sopenharmony_ci      pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);
759bf215546Sopenharmony_ci      pipe_resource_reference(&buf->resources[i], NULL);
760bf215546Sopenharmony_ci   }
761bf215546Sopenharmony_ci   for (;i < 3;++i)
762bf215546Sopenharmony_ci      pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);
763bf215546Sopenharmony_ci
764bf215546Sopenharmony_ci   FREE(buffer);
765bf215546Sopenharmony_ci}
766bf215546Sopenharmony_ci
767bf215546Sopenharmony_cistatic struct pipe_video_buffer *
768bf215546Sopenharmony_cinouveau_video_buffer_create(struct pipe_context *pipe,
769bf215546Sopenharmony_ci                            struct nouveau_screen *screen,
770bf215546Sopenharmony_ci                            const struct pipe_video_buffer *templat)
771bf215546Sopenharmony_ci{
772bf215546Sopenharmony_ci   struct nouveau_video_buffer *buffer;
773bf215546Sopenharmony_ci   struct pipe_resource templ;
774bf215546Sopenharmony_ci   unsigned width, height;
775bf215546Sopenharmony_ci
776bf215546Sopenharmony_ci   /* Only do a linear surface when a hardware decoder is used
777bf215546Sopenharmony_ci    * hardware decoder is only supported on some chipsets
778bf215546Sopenharmony_ci    * and it only supports the NV12 format
779bf215546Sopenharmony_ci    */
780bf215546Sopenharmony_ci   if (templat->buffer_format != PIPE_FORMAT_NV12 || getenv("XVMC_VL") ||
781bf215546Sopenharmony_ci       (screen->device->chipset >= 0x98 && screen->device->chipset != 0xa0) ||
782bf215546Sopenharmony_ci       screen->device->chipset < 0x40)
783bf215546Sopenharmony_ci      return vl_video_buffer_create(pipe, templat);
784bf215546Sopenharmony_ci
785bf215546Sopenharmony_ci   assert(pipe_format_to_chroma_format(templat->buffer_format) == PIPE_VIDEO_CHROMA_FORMAT_420);
786bf215546Sopenharmony_ci   width = align(templat->width, 64);
787bf215546Sopenharmony_ci   height = align(templat->height, 64);
788bf215546Sopenharmony_ci
789bf215546Sopenharmony_ci   buffer = CALLOC_STRUCT(nouveau_video_buffer);
790bf215546Sopenharmony_ci   if (!buffer)
791bf215546Sopenharmony_ci      return NULL;
792bf215546Sopenharmony_ci
793bf215546Sopenharmony_ci   buffer->base.context = pipe;
794bf215546Sopenharmony_ci   buffer->base.destroy = nouveau_video_buffer_destroy;
795bf215546Sopenharmony_ci   buffer->base.get_sampler_view_planes = nouveau_video_buffer_sampler_view_planes;
796bf215546Sopenharmony_ci   buffer->base.get_sampler_view_components = nouveau_video_buffer_sampler_view_components;
797bf215546Sopenharmony_ci   buffer->base.get_surfaces = nouveau_video_buffer_surfaces;
798bf215546Sopenharmony_ci   buffer->base.buffer_format = templat->buffer_format;
799bf215546Sopenharmony_ci   buffer->base.width = width;
800bf215546Sopenharmony_ci   buffer->base.height = height;
801bf215546Sopenharmony_ci   buffer->num_planes = 2;
802bf215546Sopenharmony_ci
803bf215546Sopenharmony_ci   memset(&templ, 0, sizeof(templ));
804bf215546Sopenharmony_ci   templ.target = PIPE_TEXTURE_2D;
805bf215546Sopenharmony_ci   templ.format = PIPE_FORMAT_R8_UNORM;
806bf215546Sopenharmony_ci   templ.width0 = width;
807bf215546Sopenharmony_ci   templ.height0 = height;
808bf215546Sopenharmony_ci   templ.depth0 = 1;
809bf215546Sopenharmony_ci   templ.array_size = 1;
810bf215546Sopenharmony_ci   templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
811bf215546Sopenharmony_ci   templ.usage = PIPE_USAGE_DEFAULT;
812bf215546Sopenharmony_ci   templ.flags = NOUVEAU_RESOURCE_FLAG_LINEAR;
813bf215546Sopenharmony_ci
814bf215546Sopenharmony_ci   buffer->resources[0] = pipe->screen->resource_create(pipe->screen, &templ);
815bf215546Sopenharmony_ci   if (!buffer->resources[0])
816bf215546Sopenharmony_ci      goto error;
817bf215546Sopenharmony_ci   templ.width0 /= 2;
818bf215546Sopenharmony_ci   templ.height0 /= 2;
819bf215546Sopenharmony_ci   templ.format = PIPE_FORMAT_R8G8_UNORM;
820bf215546Sopenharmony_ci   buffer->resources[1] = pipe->screen->resource_create(pipe->screen, &templ);
821bf215546Sopenharmony_ci   if (!buffer->resources[1])
822bf215546Sopenharmony_ci      goto error;
823bf215546Sopenharmony_ci   return &buffer->base;
824bf215546Sopenharmony_ci
825bf215546Sopenharmony_cierror:
826bf215546Sopenharmony_ci   nouveau_video_buffer_destroy(&buffer->base);
827bf215546Sopenharmony_ci   return NULL;
828bf215546Sopenharmony_ci}
829bf215546Sopenharmony_ci
830bf215546Sopenharmony_cistatic int
831bf215546Sopenharmony_cinouveau_screen_get_video_param(struct pipe_screen *pscreen,
832bf215546Sopenharmony_ci                               enum pipe_video_profile profile,
833bf215546Sopenharmony_ci                               enum pipe_video_entrypoint entrypoint,
834bf215546Sopenharmony_ci                               enum pipe_video_cap param)
835bf215546Sopenharmony_ci{
836bf215546Sopenharmony_ci   switch (param) {
837bf215546Sopenharmony_ci   case PIPE_VIDEO_CAP_SUPPORTED:
838bf215546Sopenharmony_ci      return entrypoint >= PIPE_VIDEO_ENTRYPOINT_IDCT &&
839bf215546Sopenharmony_ci         u_reduce_video_profile(profile) == PIPE_VIDEO_FORMAT_MPEG12;
840bf215546Sopenharmony_ci   case PIPE_VIDEO_CAP_NPOT_TEXTURES:
841bf215546Sopenharmony_ci      return 1;
842bf215546Sopenharmony_ci   case PIPE_VIDEO_CAP_MAX_WIDTH:
843bf215546Sopenharmony_ci   case PIPE_VIDEO_CAP_MAX_HEIGHT:
844bf215546Sopenharmony_ci      return vl_video_buffer_max_size(pscreen);
845bf215546Sopenharmony_ci   case PIPE_VIDEO_CAP_PREFERED_FORMAT:
846bf215546Sopenharmony_ci      return PIPE_FORMAT_NV12;
847bf215546Sopenharmony_ci   case PIPE_VIDEO_CAP_PREFERS_INTERLACED:
848bf215546Sopenharmony_ci      return false;
849bf215546Sopenharmony_ci   case PIPE_VIDEO_CAP_SUPPORTS_INTERLACED:
850bf215546Sopenharmony_ci      return false;
851bf215546Sopenharmony_ci   case PIPE_VIDEO_CAP_SUPPORTS_PROGRESSIVE:
852bf215546Sopenharmony_ci      return true;
853bf215546Sopenharmony_ci   case PIPE_VIDEO_CAP_MAX_LEVEL:
854bf215546Sopenharmony_ci      return vl_level_supported(pscreen, profile);
855bf215546Sopenharmony_ci   default:
856bf215546Sopenharmony_ci      debug_printf("unknown video param: %d\n", param);
857bf215546Sopenharmony_ci      return 0;
858bf215546Sopenharmony_ci   }
859bf215546Sopenharmony_ci}
860bf215546Sopenharmony_ci
861bf215546Sopenharmony_civoid
862bf215546Sopenharmony_cinouveau_screen_init_vdec(struct nouveau_screen *screen)
863bf215546Sopenharmony_ci{
864bf215546Sopenharmony_ci   screen->base.get_video_param = nouveau_screen_get_video_param;
865bf215546Sopenharmony_ci   screen->base.is_video_format_supported = vl_video_buffer_is_format_supported;
866bf215546Sopenharmony_ci}
867bf215546Sopenharmony_ci
868bf215546Sopenharmony_cistatic struct pipe_video_codec *
869bf215546Sopenharmony_cinouveau_context_create_decoder(struct pipe_context *context,
870bf215546Sopenharmony_ci                               const struct pipe_video_codec *templ)
871bf215546Sopenharmony_ci{
872bf215546Sopenharmony_ci   struct nouveau_screen *screen = nouveau_context(context)->screen;
873bf215546Sopenharmony_ci   return nouveau_create_decoder(context, templ, screen);
874bf215546Sopenharmony_ci}
875bf215546Sopenharmony_ci
876bf215546Sopenharmony_cistatic struct pipe_video_buffer *
877bf215546Sopenharmony_cinouveau_context_video_buffer_create(struct pipe_context *pipe,
878bf215546Sopenharmony_ci                                    const struct pipe_video_buffer *templat)
879bf215546Sopenharmony_ci{
880bf215546Sopenharmony_ci   struct nouveau_screen *screen = nouveau_context(pipe)->screen;
881bf215546Sopenharmony_ci   return nouveau_video_buffer_create(pipe, screen, templat);
882bf215546Sopenharmony_ci}
883bf215546Sopenharmony_ci
884bf215546Sopenharmony_civoid
885bf215546Sopenharmony_cinouveau_context_init_vdec(struct nouveau_context *nv)
886bf215546Sopenharmony_ci{
887bf215546Sopenharmony_ci   nv->pipe.create_video_codec = nouveau_context_create_decoder;
888bf215546Sopenharmony_ci   nv->pipe.create_video_buffer = nouveau_context_video_buffer_create;
889bf215546Sopenharmony_ci}
890