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