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